|
1 | | -/* eslint-disable no-undef */ |
| 1 | +/* globals $ Quill ImageUploader */ |
2 | 2 | (function () { |
3 | | - 'use strict' |
| 3 | + 'use strict'; |
4 | 4 |
|
5 | 5 | // --- functions --------------------------------------------------------------- |
6 | | - function initQuillEditors() { |
7 | | - let default_theme = 'snow' |
8 | | - let default_toolbar = [ |
| 6 | + const initQuillEditors = () => { |
| 7 | + const defaultTheme = 'snow'; |
| 8 | + const defaultToolbar = [ |
9 | 9 | ['bold', 'italic', 'underline'], |
10 | 10 | ['link', 'blockquote', 'code-block'], |
11 | 11 | [{ 'script': 'sub' }, { 'script': 'super' }], |
12 | 12 | [{ 'align': [] }, { list: 'ordered' }, { list: 'bullet' }], |
13 | 13 | [{ 'color': [] }, { 'background': [] }], |
14 | 14 | ['image'], |
15 | 15 | ['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 = {}; |
19 | 19 |
|
20 | 20 | 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 | + |
23 | 24 | if (content && !isActive) { |
24 | 25 | // 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; |
29 | 31 |
|
30 | 32 | // 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; |
36 | 39 | } |
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); |
39 | 43 | } |
40 | 44 |
|
41 | 45 | // 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'; |
44 | 48 | } |
45 | 49 | } |
46 | 50 |
|
47 | | - let formtastic = document.querySelector('form.formtastic') |
| 51 | + const formtastic = document.querySelector('form.formtastic'); |
| 52 | + |
48 | 53 | if (formtastic) { |
49 | 54 | formtastic.onsubmit = () => { |
50 | 55 | 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 | + |
52 | 58 | if (editors[i]['_quill-editor'].editor.isBlank()) { |
53 | | - input.value = '' |
| 59 | + input.value = ''; |
54 | 60 | } else { |
55 | | - input.value = editors[i]['_quill-editor'].root.innerHTML |
| 61 | + input.value = editors[i]['_quill-editor'].root.innerHTML; |
56 | 62 | } |
57 | 63 | } |
58 | 64 | }; |
59 | 65 | } |
60 | | - } |
| 66 | + }; |
61 | 67 |
|
62 | | - function setupImageUploader(server_url, field_name) { |
| 68 | + const setupImageUploader = (server_url, field_name) => { |
63 | 69 | return { |
64 | | - upload: file => { |
| 70 | + upload: (file) => { |
65 | 71 | return new Promise((resolve, reject) => { |
66 | | - const formData = new FormData() |
67 | | - formData.append(field_name || 'file_upload', file) |
| 72 | + const formData = new FormData(); |
68 | 73 |
|
| 74 | + formData.append(field_name || 'file_upload', file); |
69 | 75 | fetch(server_url, { |
70 | 76 | body: formData, |
71 | 77 | headers: { |
|
75 | 81 | }).then(response => response.json()) |
76 | 82 | .then(result => { |
77 | 83 | if (!result.url) { |
78 | | - reject('Upload failed') |
| 84 | + reject('Upload failed'); |
79 | 85 | } |
80 | 86 | resolve(result.url); |
81 | 87 | }) |
82 | 88 | .catch(error => { |
83 | | - reject('Upload failed') |
84 | | - console.error('Error: ', error) |
| 89 | + reject('Upload failed'); |
| 90 | + console.error('Error: ', error); |
85 | 91 | }) |
86 | 92 | }) |
87 | 93 | } |
|
90 | 96 |
|
91 | 97 | // --- public functions -------------------------------------------------------- |
92 | 98 | 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']) }); |
95 | 103 |
|
96 | | - editors.forEach(function(editor) { list.push(editor['_quill-editor']) }) |
97 | | - return list |
| 104 | + return list; |
98 | 105 | } |
99 | 106 |
|
100 | 107 | window.getQuillEditorByIndex = function(index) { |
101 | | - const editors = document.querySelectorAll('[data-aa-quill-editor]') |
| 108 | + const editors = document.querySelectorAll('[data-aa-quill-editor]'); |
102 | 109 |
|
103 | | - return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null |
| 110 | + return (index >= 0 && index < editors.length) ? editors[index]['_quill-editor'] : null; |
104 | 111 | } |
105 | 112 |
|
106 | 113 | window.getQuillEditorByElementId = function(id) { |
107 | | - const editor = document.querySelector(`[data-aa-quill-editor]#${id}`) |
| 114 | + const editor = document.querySelector(`[data-aa-quill-editor]#${id}`); |
108 | 115 |
|
109 | | - return editor ? editor['_quill-editor'] : null |
| 116 | + return editor ? editor['_quill-editor'] : null; |
110 | 117 | } |
111 | 118 |
|
112 | 119 | // --- 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