зеркало из
https://github.com/rs-pro/activeadmin-quill_editor.git
synced 2026-08-28 15:36:17 +03:00
chore: internal improvements to quill editor input js
Этот коммит содержится в:
1
Makefile
1
Makefile
@@ -40,6 +40,7 @@ lint:
|
|||||||
@docker compose -f extra/docker-compose.yml exec app bin/rubocop
|
@docker compose -f extra/docker-compose.yml exec app bin/rubocop
|
||||||
|
|
||||||
server: seed
|
server: seed
|
||||||
|
@rm -f spec/dummy/tmp/pids/server.pid
|
||||||
@docker compose -f extra/docker-compose.yml exec app bin/rails server -b 0.0.0.0 -p ${SERVER_PORT}
|
@docker compose -f extra/docker-compose.yml exec app bin/rails server -b 0.0.0.0 -p ${SERVER_PORT}
|
||||||
|
|
||||||
specs:
|
specs:
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
/* eslint-disable no-undef */
|
/* globals $ Quill ImageUploader */
|
||||||
(function () {
|
(function () {
|
||||||
'use strict'
|
'use strict';
|
||||||
|
|
||||||
// --- functions ---------------------------------------------------------------
|
// --- functions ---------------------------------------------------------------
|
||||||
function initQuillEditors() {
|
const initQuillEditors = () => {
|
||||||
let default_theme = 'snow'
|
const defaultTheme = 'snow';
|
||||||
let default_toolbar = [
|
const defaultToolbar = [
|
||||||
['bold', 'italic', 'underline'],
|
['bold', 'italic', 'underline'],
|
||||||
['link', 'blockquote', 'code-block'],
|
['link', 'blockquote', 'code-block'],
|
||||||
[{ 'script': 'sub' }, { 'script': 'super' }],
|
[{ 'script': 'sub' }, { 'script': 'super' }],
|
||||||
@@ -13,59 +13,65 @@
|
|||||||
[{ 'color': [] }, { 'background': [] }],
|
[{ 'color': [] }, { 'background': [] }],
|
||||||
['image'],
|
['image'],
|
||||||
['clean'],
|
['clean'],
|
||||||
]
|
];
|
||||||
let editors = document.querySelectorAll('[data-aa-quill-editor]')
|
const editors = document.querySelectorAll('[data-aa-quill-editor]');
|
||||||
let registered_plugins = {}
|
const registeredPlugins = {};
|
||||||
|
|
||||||
for (let i = 0; i < editors.length; i++) {
|
for (let i = 0; i < editors.length; i++) {
|
||||||
let content = editors[i].querySelector('[data-aa-quill-content]')
|
const content = editors[i].querySelector('[data-aa-quill-content]');
|
||||||
let isActive = editors[i].classList.contains('quill-editor--active')
|
const isActive = editors[i].classList.contains('quill-editor--active');
|
||||||
|
|
||||||
if (content && !isActive) {
|
if (content && !isActive) {
|
||||||
// Setup editor options
|
// Setup editor options
|
||||||
let options = editors[i].getAttribute('data-options') ? JSON.parse(editors[i].getAttribute('data-options')) : {}
|
const options = editors[i].getAttribute('data-options') ? JSON.parse(editors[i].getAttribute('data-options')) : {};
|
||||||
if (!options.theme) options.theme = default_theme
|
|
||||||
if (!options.modules) options.modules = {}
|
if (!options.theme) options.theme = defaultTheme;
|
||||||
if (!options.modules.toolbar) options.modules.toolbar = default_toolbar
|
if (!options.modules) options.modules = {};
|
||||||
|
if (!options.modules.toolbar) options.modules.toolbar = defaultToolbar;
|
||||||
|
|
||||||
// Setup plugin options
|
// Setup plugin options
|
||||||
let plugin_options = editors[i].getAttribute('data-plugins') ? JSON.parse(editors[i].getAttribute('data-plugins')) : {}
|
const pluginOptions = editors[i].getAttribute('data-plugins') ? JSON.parse(editors[i].getAttribute('data-plugins')) : {};
|
||||||
if (plugin_options.image_uploader && plugin_options.image_uploader.server_url) {
|
|
||||||
if (!registered_plugins.image_uploader) {
|
if (pluginOptions.image_uploader && pluginOptions.image_uploader.server_url) {
|
||||||
Quill.register('modules/imageUploader', ImageUploader)
|
if (!registeredPlugins.image_uploader) {
|
||||||
registered_plugins.image_uploader = true
|
Quill.register('modules/imageUploader', ImageUploader);
|
||||||
|
registeredPlugins.image_uploader = true;
|
||||||
}
|
}
|
||||||
let opts = plugin_options.image_uploader
|
const opts = pluginOptions.image_uploader;
|
||||||
options.modules.imageUploader = setupImageUploader(opts.server_url, opts.field_name)
|
|
||||||
|
options.modules.imageUploader = setupImageUploader(opts.server_url, opts.field_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init editor
|
// Init editor
|
||||||
editors[i]['_quill-editor'] = new Quill(content, options)
|
editors[i]['_quill-editor'] = new Quill(content, options);
|
||||||
editors[i].classList += ' quill-editor--active'
|
editors[i].classList += ' quill-editor--active';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let formtastic = document.querySelector('form.formtastic')
|
const formtastic = document.querySelector('form.formtastic');
|
||||||
|
|
||||||
if (formtastic) {
|
if (formtastic) {
|
||||||
formtastic.onsubmit = () => {
|
formtastic.onsubmit = () => {
|
||||||
for (let i = 0; i < editors.length; i++) {
|
for (let i = 0; i < editors.length; i++) {
|
||||||
let input = editors[i].querySelector('input[type="hidden"]')
|
const input = editors[i].querySelector('input[type="hidden"]');
|
||||||
|
|
||||||
if (editors[i]['_quill-editor'].editor.isBlank()) {
|
if (editors[i]['_quill-editor'].editor.isBlank()) {
|
||||||
input.value = ''
|
input.value = '';
|
||||||
} else {
|
} else {
|
||||||
input.value = editors[i]['_quill-editor'].root.innerHTML
|
input.value = editors[i]['_quill-editor'].root.innerHTML;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function setupImageUploader(server_url, field_name) {
|
const setupImageUploader = (server_url, field_name) => {
|
||||||
return {
|
return {
|
||||||
upload: file => {
|
upload: (file) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const formData = new FormData()
|
const formData = new FormData();
|
||||||
formData.append(field_name || 'file_upload', file)
|
|
||||||
|
|
||||||
|
formData.append(field_name || 'file_upload', file);
|
||||||
fetch(server_url, {
|
fetch(server_url, {
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -75,13 +81,13 @@
|
|||||||
}).then(response => response.json())
|
}).then(response => response.json())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
if (!result.url) {
|
if (!result.url) {
|
||||||
reject('Upload failed')
|
reject('Upload failed');
|
||||||
}
|
}
|
||||||
resolve(result.url);
|
resolve(result.url);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
reject('Upload failed')
|
reject('Upload failed');
|
||||||
console.error('Error: ', error)
|
console.error('Error: ', error);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -90,27 +96,28 @@
|
|||||||
|
|
||||||
// --- public functions --------------------------------------------------------
|
// --- public functions --------------------------------------------------------
|
||||||
window.getQuillEditors = function() {
|
window.getQuillEditors = function() {
|
||||||
const editors = document.querySelectorAll('[data-aa-quill-editor]')
|
const editors = document.querySelectorAll('[data-aa-quill-editor]');
|
||||||
let list = []
|
const list = [];
|
||||||
|
|
||||||
editors.forEach(function(editor) { list.push(editor['_quill-editor']) })
|
editors.forEach(function(editor) { list.push(editor['_quill-editor']) });
|
||||||
return list
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.getQuillEditorByIndex = function(index) {
|
window.getQuillEditorByIndex = function(index) {
|
||||||
const editors = document.querySelectorAll('[data-aa-quill-editor]')
|
const editors = document.querySelectorAll('[data-aa-quill-editor]');
|
||||||
|
|
||||||
return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null
|
return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.getQuillEditorByElementId = function(id) {
|
window.getQuillEditorByElementId = function(id) {
|
||||||
const editor = document.querySelector(`[data-aa-quill-editor]#${id}`)
|
const editor = document.querySelector(`[data-aa-quill-editor]#${id}`);
|
||||||
|
|
||||||
return editor ? editor['_quill-editor'] : null
|
return editor ? editor['_quill-editor'] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- events ------------------------------------------------------------------
|
// --- events ------------------------------------------------------------------
|
||||||
$(document).ready(initQuillEditors)
|
$(document).ready(initQuillEditors);
|
||||||
$(document).on('has_many_add:after', '.has_many_container', initQuillEditors)
|
$(document).on('has_many_add:after', '.has_many_container', initQuillEditors);
|
||||||
$(document).on('turbolinks:load', initQuillEditors)
|
$(document).on('turbolinks:load', initQuillEditors);
|
||||||
})()
|
})();
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user