Skip to content

Commit 9bda446

Browse files
committed
chore: internal improvements to quill editor input js
1 parent 533471c commit 9bda446

2 files changed

Lines changed: 55 additions & 47 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ lint:
4040
@docker compose -f extra/docker-compose.yml exec app bin/rubocop
4141

4242
server: seed
43+
@rm -f spec/dummy/tmp/pids/server.pid
4344
@docker compose -f extra/docker-compose.yml exec app bin/rails server -b 0.0.0.0 -p ${SERVER_PORT}
4445

4546
specs:
Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,77 @@
1-
/* eslint-disable no-undef */
1+
/* globals $ Quill ImageUploader */
22
(function () {
3-
'use strict'
3+
'use strict';
44

55
// --- functions ---------------------------------------------------------------
6-
function initQuillEditors() {
7-
let default_theme = 'snow'
8-
let default_toolbar = [
6+
const initQuillEditors = () => {
7+
const defaultTheme = 'snow';
8+
const defaultToolbar = [
99
['bold', 'italic', 'underline'],
1010
['link', 'blockquote', 'code-block'],
1111
[{ 'script': 'sub' }, { 'script': 'super' }],
1212
[{ 'align': [] }, { list: 'ordered' }, { list: 'bullet' }],
1313
[{ 'color': [] }, { 'background': [] }],
1414
['image'],
1515
['clean'],
16-
]
17-
let editors = document.querySelectorAll('[data-aa-quill-editor]')
18-
let registered_plugins = {}
16+
];
17+
const editors = document.querySelectorAll('[data-aa-quill-editor]');
18+
const registeredPlugins = {};
1919

2020
for (let i = 0; i < editors.length; i++) {
21-
let content = editors[i].querySelector('[data-aa-quill-content]')
22-
let isActive = editors[i].classList.contains('quill-editor--active')
21+
const content = editors[i].querySelector('[data-aa-quill-content]');
22+
const isActive = editors[i].classList.contains('quill-editor--active');
23+
2324
if (content && !isActive) {
2425
// Setup editor options
25-
let options = editors[i].getAttribute('data-options') ? JSON.parse(editors[i].getAttribute('data-options')) : {}
26-
if (!options.theme) options.theme = default_theme
27-
if (!options.modules) options.modules = {}
28-
if (!options.modules.toolbar) options.modules.toolbar = default_toolbar
26+
const options = editors[i].getAttribute('data-options') ? JSON.parse(editors[i].getAttribute('data-options')) : {};
27+
28+
if (!options.theme) options.theme = defaultTheme;
29+
if (!options.modules) options.modules = {};
30+
if (!options.modules.toolbar) options.modules.toolbar = defaultToolbar;
2931

3032
// Setup plugin options
31-
let plugin_options = editors[i].getAttribute('data-plugins') ? JSON.parse(editors[i].getAttribute('data-plugins')) : {}
32-
if (plugin_options.image_uploader && plugin_options.image_uploader.server_url) {
33-
if (!registered_plugins.image_uploader) {
34-
Quill.register('modules/imageUploader', ImageUploader)
35-
registered_plugins.image_uploader = true
33+
const pluginOptions = editors[i].getAttribute('data-plugins') ? JSON.parse(editors[i].getAttribute('data-plugins')) : {};
34+
35+
if (pluginOptions.image_uploader && pluginOptions.image_uploader.server_url) {
36+
if (!registeredPlugins.image_uploader) {
37+
Quill.register('modules/imageUploader', ImageUploader);
38+
registeredPlugins.image_uploader = true;
3639
}
37-
let opts = plugin_options.image_uploader
38-
options.modules.imageUploader = setupImageUploader(opts.server_url, opts.field_name)
40+
const opts = pluginOptions.image_uploader;
41+
42+
options.modules.imageUploader = setupImageUploader(opts.server_url, opts.field_name);
3943
}
4044

4145
// Init editor
42-
editors[i]['_quill-editor'] = new Quill(content, options)
43-
editors[i].classList += ' quill-editor--active'
46+
editors[i]['_quill-editor'] = new Quill(content, options);
47+
editors[i].classList += ' quill-editor--active';
4448
}
4549
}
4650

47-
let formtastic = document.querySelector('form.formtastic')
51+
const formtastic = document.querySelector('form.formtastic');
52+
4853
if (formtastic) {
4954
formtastic.onsubmit = () => {
5055
for (let i = 0; i < editors.length; i++) {
51-
let input = editors[i].querySelector('input[type="hidden"]')
56+
const input = editors[i].querySelector('input[type="hidden"]');
57+
5258
if (editors[i]['_quill-editor'].editor.isBlank()) {
53-
input.value = ''
59+
input.value = '';
5460
} else {
55-
input.value = editors[i]['_quill-editor'].root.innerHTML
61+
input.value = editors[i]['_quill-editor'].root.innerHTML;
5662
}
5763
}
5864
};
5965
}
60-
}
66+
};
6167

62-
function setupImageUploader(server_url, field_name) {
68+
const setupImageUploader = (server_url, field_name) => {
6369
return {
64-
upload: file => {
70+
upload: (file) => {
6571
return new Promise((resolve, reject) => {
66-
const formData = new FormData()
67-
formData.append(field_name || 'file_upload', file)
72+
const formData = new FormData();
6873

74+
formData.append(field_name || 'file_upload', file);
6975
fetch(server_url, {
7076
body: formData,
7177
headers: {
@@ -75,13 +81,13 @@
7581
}).then(response => response.json())
7682
.then(result => {
7783
if (!result.url) {
78-
reject('Upload failed')
84+
reject('Upload failed');
7985
}
8086
resolve(result.url);
8187
})
8288
.catch(error => {
83-
reject('Upload failed')
84-
console.error('Error: ', error)
89+
reject('Upload failed');
90+
console.error('Error: ', error);
8591
})
8692
})
8793
}
@@ -90,27 +96,28 @@
9096

9197
// --- public functions --------------------------------------------------------
9298
window.getQuillEditors = function() {
93-
const editors = document.querySelectorAll('[data-aa-quill-editor]')
94-
let list = []
99+
const editors = document.querySelectorAll('[data-aa-quill-editor]');
100+
const list = [];
101+
102+
editors.forEach(function(editor) { list.push(editor['_quill-editor']) });
95103

96-
editors.forEach(function(editor) { list.push(editor['_quill-editor']) })
97-
return list
104+
return list;
98105
}
99106

100107
window.getQuillEditorByIndex = function(index) {
101-
const editors = document.querySelectorAll('[data-aa-quill-editor]')
108+
const editors = document.querySelectorAll('[data-aa-quill-editor]');
102109

103-
return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null
110+
return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null;
104111
}
105112

106113
window.getQuillEditorByElementId = function(id) {
107-
const editor = document.querySelector(`[data-aa-quill-editor]#${id}`)
114+
const editor = document.querySelector(`[data-aa-quill-editor]#${id}`);
108115

109-
return editor ? editor['_quill-editor'] : null
116+
return editor ? editor['_quill-editor'] : null;
110117
}
111118

112119
// --- events ------------------------------------------------------------------
113-
$(document).ready(initQuillEditors)
114-
$(document).on('has_many_add:after', '.has_many_container', initQuillEditors)
115-
$(document).on('turbolinks:load', initQuillEditors)
116-
})()
120+
$(document).ready(initQuillEditors);
121+
$(document).on('has_many_add:after', '.has_many_container', initQuillEditors);
122+
$(document).on('turbolinks:load', initQuillEditors);
123+
})();

0 commit comments

Comments
 (0)