@avlcodemonkey/nilla-components 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/css/alert.css +48 -0
- package/css/autocomplete.css +38 -0
- package/css/dialog.css +61 -0
- package/css/file-to-textarea.css +8 -0
- package/css/html-editor.css +172 -0
- package/css/json.css +44 -0
- package/css/list.css +37 -0
- package/css/nav-toggle.css +92 -0
- package/css/nilla.css +16 -0
- package/css/pjax.css +58 -0
- package/css/slider-input.css +16 -0
- package/css/table.css +99 -0
- package/css/tokens.css +44 -0
- package/css/unsaved-changes.css +10 -0
- package/css/vendor/wysi.css +406 -0
- package/docs/llms.txt +319 -0
- package/package.json +121 -5
- package/src/components/Alert.js +24 -0
- package/src/components/Autocomplete.js +127 -0
- package/src/components/BaseComponent.js +78 -0
- package/src/components/BaseDialog.js +151 -0
- package/src/components/ConfirmDialog.js +34 -0
- package/src/components/CopyToClipboard.js +66 -0
- package/src/components/DateFormatter.js +51 -0
- package/src/components/DateTimeInput.js +63 -0
- package/src/components/DisplayToggle.js +53 -0
- package/src/components/FileToTextarea.js +81 -0
- package/src/components/HtmlEditor.js +309 -0
- package/src/components/InfoDialog.js +38 -0
- package/src/components/JsonFormatter.js +23 -0
- package/src/components/List.js +237 -0
- package/src/components/ListSortable.js +8 -0
- package/src/components/NavToggle.js +119 -0
- package/src/components/PJax.js +888 -0
- package/src/components/Share.js +41 -0
- package/src/components/SliderInput.js +26 -0
- package/src/components/Table.js +758 -0
- package/src/components/TimeOffsetSelect.js +29 -0
- package/src/components/UnsavedChanges.js +187 -0
- package/src/constants/Fetch.js +7 -0
- package/src/constants/HttpHeaders.js +19 -0
- package/src/constants/HttpMethods.js +14 -0
- package/src/constants/SortDirection.js +11 -0
- package/src/constants/TableElements.js +25 -0
- package/src/constants/TableSettings.js +14 -0
- package/src/errors/FetchError.js +7 -0
- package/src/events/ListReorderEvent.js +19 -0
- package/src/events/PJaxErrorEvent.js +38 -0
- package/src/events/PJaxNavigatingEvent.js +46 -0
- package/src/index.js +26 -0
- package/src/locales/en.json +55 -0
- package/src/locales/es.js +9 -0
- package/src/locales/es.json +55 -0
- package/src/models/TableSort.js +20 -0
- package/src/utils/ajaxHeaders.js +10 -0
- package/src/utils/announce.js +40 -0
- package/src/utils/defineElement.js +13 -0
- package/src/utils/editor/blockTools.js +256 -0
- package/src/utils/editor/elements.js +18 -0
- package/src/utils/editor/input.js +350 -0
- package/src/utils/editor/selection.js +125 -0
- package/src/utils/editor/tableTools.js +497 -0
- package/src/utils/escape.js +20 -0
- package/src/utils/fetchJsonArray.js +29 -0
- package/src/utils/filterArray.js +18 -0
- package/src/utils/formActionUrl.js +10 -0
- package/src/utils/formData.js +118 -0
- package/src/utils/index.js +18 -0
- package/src/utils/indexedCompare.js +18 -0
- package/src/utils/isSameOrigin.js +12 -0
- package/src/utils/locale.js +36 -0
- package/src/utils/notifyChange.js +10 -0
- package/src/utils/numberSetting.js +14 -0
- package/src/utils/pageLanguage.js +13 -0
- package/src/utils/parseUtcDate.js +22 -0
- package/src/utils/randomId.js +17 -0
- package/src/utils/registerTabTrap.js +60 -0
- package/src/utils/renderTemplate.js +44 -0
- package/src/utils/requestTimeout.js +12 -0
- package/src/utils/response.js +28 -0
- package/src/utils/sortedCompare.js +67 -0
- package/src/utils/storage.js +40 -0
- package/src/vendor/autocomplete.js +515 -0
- package/src/vendor/formatDate.js +236 -0
- package/src/vendor/licenses/autocomplete.LICENSE.md +21 -0
- package/src/vendor/licenses/fecha.LICENSE.md +21 -0
- package/src/vendor/licenses/pretty-print-json.LICENSE.md +21 -0
- package/src/vendor/licenses/wysi.LICENSE.md +21 -0
- package/src/vendor/prettyPrintJson.js +108 -0
- package/src/vendor/wyzi.js +2024 -0
|
@@ -0,0 +1,2024 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// Modified for nilla-components: code, quote and table tools (in ./editor), H5 and H6, keeping more tags and attributes,
|
|
3
|
+
// wrapping rather than re-parsing top level content, indenting only in lists, escaping link and image attributes,
|
|
4
|
+
// keeping popover fields out of the form, naming the editor by aria-label, drawing the bold, italic, and delete icons, leaving out
|
|
5
|
+
// tools the editor doesn't use, and getting text by key.
|
|
6
|
+
/*!
|
|
7
|
+
* Copyright (c) 2023 Momo Bassit.
|
|
8
|
+
* Licensed under the MIT License (MIT)
|
|
9
|
+
* https://github.com/mdbassit/Wysi
|
|
10
|
+
*/
|
|
11
|
+
import escape from '../utils/escape.js';
|
|
12
|
+
import { toggleCodeBlock, toggleInlineCode, toggleQuote } from '../utils/editor/blockTools.js';
|
|
13
|
+
import { isUnboldWrapper } from '../utils/editor/elements.js';
|
|
14
|
+
import { handleEditorBeforeInput, handleEditorKeydown, handleEditorPaste } from '../utils/editor/input.js';
|
|
15
|
+
import { handleTableMenuClick, handleTableMenuInput, renderTableMenu } from '../utils/editor/tableTools.js';
|
|
16
|
+
(function(window, document) {
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
// Default settings
|
|
20
|
+
var settings = {
|
|
21
|
+
// Default selector
|
|
22
|
+
el: '[data-wysi], .wysi-field',
|
|
23
|
+
// Default tools in the toolbar
|
|
24
|
+
tools: ['format', '|', 'bold', 'italic', '|', 'ul', 'ol', '|', 'indent', 'outdent', '|', 'link', 'image'],
|
|
25
|
+
// Enable dark mode (toolbar only)
|
|
26
|
+
darkMode: false,
|
|
27
|
+
// Height of the editable region
|
|
28
|
+
height: 200,
|
|
29
|
+
// Grow the editable region's height to fit its content
|
|
30
|
+
autoGrow: false,
|
|
31
|
+
// Hide the toolbar when the editable region is out of focus
|
|
32
|
+
autoHide: false,
|
|
33
|
+
// Default list of allowed tags
|
|
34
|
+
// These tags are always allowed regardless of the instance options
|
|
35
|
+
allowedTags: {
|
|
36
|
+
br: {
|
|
37
|
+
attributes: [],
|
|
38
|
+
styles: [],
|
|
39
|
+
isEmpty: true
|
|
40
|
+
},
|
|
41
|
+
p: {
|
|
42
|
+
attributes: [],
|
|
43
|
+
styles: [],
|
|
44
|
+
isEmpty: false
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
// Custom tags to allow when filtering inserted content
|
|
48
|
+
customTags: [
|
|
49
|
+
/* Example:
|
|
50
|
+
{
|
|
51
|
+
tags: ['table', 'thead', 'tbody', 'tr', 'td', 'th'], // Tags to allow
|
|
52
|
+
attributes: ['id', 'class'], // These attributes will be permitted for all the tags above
|
|
53
|
+
styles: ['width'],
|
|
54
|
+
isEmpty: false
|
|
55
|
+
}
|
|
56
|
+
*/
|
|
57
|
+
]
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Supported tools
|
|
61
|
+
var toolset = {
|
|
62
|
+
format: {
|
|
63
|
+
tags: ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
|
|
64
|
+
styles: ['text-align'],
|
|
65
|
+
label: 'editor.format',
|
|
66
|
+
paragraph: 'editor.paragraph',
|
|
67
|
+
heading: 'editor.heading'
|
|
68
|
+
},
|
|
69
|
+
quote: {
|
|
70
|
+
tags: ['blockquote'],
|
|
71
|
+
label: 'editor.quote'
|
|
72
|
+
},
|
|
73
|
+
bold: {
|
|
74
|
+
tags: ['strong'],
|
|
75
|
+
alias: ['b'],
|
|
76
|
+
label: 'editor.bold'
|
|
77
|
+
},
|
|
78
|
+
italic: {
|
|
79
|
+
tags: ['em'],
|
|
80
|
+
alias: ['i'],
|
|
81
|
+
label: 'editor.italic'
|
|
82
|
+
},
|
|
83
|
+
code: {
|
|
84
|
+
tags: ['code'],
|
|
85
|
+
label: 'editor.code'
|
|
86
|
+
},
|
|
87
|
+
codeBlock: {
|
|
88
|
+
tags: ['pre'],
|
|
89
|
+
label: 'editor.codeBlock'
|
|
90
|
+
},
|
|
91
|
+
table: {
|
|
92
|
+
tags: ['table'],
|
|
93
|
+
extraTags: ['thead', 'tbody', 'tfoot', 'tr'],
|
|
94
|
+
label: 'editor.table'
|
|
95
|
+
},
|
|
96
|
+
strike: {
|
|
97
|
+
tags: ['s'],
|
|
98
|
+
alias: ['del', 'strike'],
|
|
99
|
+
label: 'editor.strike',
|
|
100
|
+
command: 'strikeThrough'
|
|
101
|
+
},
|
|
102
|
+
ul: {
|
|
103
|
+
tags: ['ul'],
|
|
104
|
+
extraTags: ['li'],
|
|
105
|
+
styles: ['text-align'],
|
|
106
|
+
label: 'editor.ul',
|
|
107
|
+
command: 'insertUnorderedList'
|
|
108
|
+
},
|
|
109
|
+
ol: {
|
|
110
|
+
tags: ['ol'],
|
|
111
|
+
extraTags: ['li'],
|
|
112
|
+
attributes: ['start'],
|
|
113
|
+
styles: ['text-align'],
|
|
114
|
+
label: 'editor.ol',
|
|
115
|
+
command: 'insertOrderedList'
|
|
116
|
+
},
|
|
117
|
+
indent: {
|
|
118
|
+
label: 'editor.indent'
|
|
119
|
+
},
|
|
120
|
+
outdent: {
|
|
121
|
+
label: 'editor.outdent'
|
|
122
|
+
},
|
|
123
|
+
link: {
|
|
124
|
+
tags: ['a'],
|
|
125
|
+
attributes: ['href', 'target'],
|
|
126
|
+
attributeLabels: ['editor.url', 'editor.openIn'],
|
|
127
|
+
keepAttributes: ['title', 'rel'],
|
|
128
|
+
hasForm: true,
|
|
129
|
+
formOptions: {
|
|
130
|
+
target: [{
|
|
131
|
+
label: 'editor.currentTab',
|
|
132
|
+
value: ''
|
|
133
|
+
}, {
|
|
134
|
+
label: 'editor.newTab',
|
|
135
|
+
value: '_blank'
|
|
136
|
+
}]
|
|
137
|
+
},
|
|
138
|
+
label: 'editor.link'
|
|
139
|
+
},
|
|
140
|
+
image: {
|
|
141
|
+
tags: ['img'],
|
|
142
|
+
attributes: ['src', 'alt'],
|
|
143
|
+
attributeLabels: ['editor.url', 'editor.alt'],
|
|
144
|
+
keepAttributes: ['title'],
|
|
145
|
+
extraSettings: ['size', 'position'],
|
|
146
|
+
extraSettingLabels: ['editor.size', 'editor.position'],
|
|
147
|
+
styles: ['width', 'display', 'margin', 'float'],
|
|
148
|
+
isEmpty: true,
|
|
149
|
+
hasForm: true,
|
|
150
|
+
formOptions: {
|
|
151
|
+
size: [{
|
|
152
|
+
label: 'editor.sizeNone',
|
|
153
|
+
value: '',
|
|
154
|
+
criterion: null
|
|
155
|
+
}, {
|
|
156
|
+
label: '100%',
|
|
157
|
+
value: '100%',
|
|
158
|
+
criterion: {
|
|
159
|
+
width: '100%'
|
|
160
|
+
}
|
|
161
|
+
}, {
|
|
162
|
+
label: '50%',
|
|
163
|
+
value: '50%',
|
|
164
|
+
criterion: {
|
|
165
|
+
width: '50%'
|
|
166
|
+
}
|
|
167
|
+
}, {
|
|
168
|
+
label: '25%',
|
|
169
|
+
value: '25%',
|
|
170
|
+
criterion: {
|
|
171
|
+
width: '25%'
|
|
172
|
+
}
|
|
173
|
+
}],
|
|
174
|
+
position: [{
|
|
175
|
+
label: 'editor.positionNone',
|
|
176
|
+
value: '',
|
|
177
|
+
criterion: null
|
|
178
|
+
}, {
|
|
179
|
+
label: 'editor.left',
|
|
180
|
+
value: 'left',
|
|
181
|
+
criterion: {
|
|
182
|
+
float: 'left'
|
|
183
|
+
}
|
|
184
|
+
}, {
|
|
185
|
+
label: 'editor.center',
|
|
186
|
+
value: 'center',
|
|
187
|
+
criterion: {
|
|
188
|
+
margin: 'auto'
|
|
189
|
+
}
|
|
190
|
+
}, {
|
|
191
|
+
label: 'editor.right',
|
|
192
|
+
value: 'right',
|
|
193
|
+
criterion: {
|
|
194
|
+
float: 'right'
|
|
195
|
+
}
|
|
196
|
+
}]
|
|
197
|
+
},
|
|
198
|
+
label: 'editor.image'
|
|
199
|
+
},
|
|
200
|
+
hr: {
|
|
201
|
+
tags: ['hr'],
|
|
202
|
+
isEmpty: true,
|
|
203
|
+
label: 'editor.hr',
|
|
204
|
+
command: 'insertHorizontalRule'
|
|
205
|
+
},
|
|
206
|
+
unlink: {
|
|
207
|
+
label: 'editor.removeLink'
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Instances storage
|
|
212
|
+
const instances = {};
|
|
213
|
+
|
|
214
|
+
// The CSS class to use for selected elements
|
|
215
|
+
const selectedClass = 'wysi-selected';
|
|
216
|
+
|
|
217
|
+
// Placeholder elements CSS class
|
|
218
|
+
const placeholderClass = 'wysi-fragment-placeholder';
|
|
219
|
+
|
|
220
|
+
// Heading elements
|
|
221
|
+
const headingElements = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'];
|
|
222
|
+
|
|
223
|
+
// Block type HTML elements
|
|
224
|
+
const blockElements = ['BLOCKQUOTE', 'HR', 'P', 'PRE', 'OL', 'UL', 'TABLE'].concat(headingElements);
|
|
225
|
+
|
|
226
|
+
// Detect Firefox browser
|
|
227
|
+
const isFirefox = navigator.userAgent.search(/Gecko\//) > -1;
|
|
228
|
+
|
|
229
|
+
// Shortcuts
|
|
230
|
+
const dispatchEvent = (element, event) => element.dispatchEvent(new Event(event, {
|
|
231
|
+
bubbles: true
|
|
232
|
+
}));
|
|
233
|
+
const execCommand = (command, value = null) => document.execCommand(command, false, value);
|
|
234
|
+
const hasClass = (element, classes) => element.classList && element.classList.contains(classes);
|
|
235
|
+
|
|
236
|
+
// Used to store the current DOM selection for later use
|
|
237
|
+
let currentSelection;
|
|
238
|
+
|
|
239
|
+
// Gets the text for a key, from the editor being set up
|
|
240
|
+
let translate = key => key;
|
|
241
|
+
|
|
242
|
+
// Polyfill for Nodelist.forEach
|
|
243
|
+
if (NodeList !== undefined && NodeList.prototype && !NodeList.prototype.forEach) {
|
|
244
|
+
NodeList.prototype.forEach = Array.prototype.forEach;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Shortcut for addEventListener to optimize the minified JS.
|
|
249
|
+
* @param {object} context The context to which the listener is attached.
|
|
250
|
+
* @param {string} type Event type.
|
|
251
|
+
* @param {(string|function)} selector Event target if delegation is used, event handler if not.
|
|
252
|
+
* @param {function} [fn] Event handler if delegation is used.
|
|
253
|
+
*/
|
|
254
|
+
function addListener(context, type, selector, fn) {
|
|
255
|
+
// Delegate event to the target of the selector
|
|
256
|
+
if (typeof selector === 'string') {
|
|
257
|
+
context.addEventListener(type, event => {
|
|
258
|
+
const target = event.target;
|
|
259
|
+
if (target.matches(selector)) {
|
|
260
|
+
fn.call(target, event);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// If the selector is not a string then it's a function
|
|
265
|
+
// in which case we need a regular event listener
|
|
266
|
+
} else {
|
|
267
|
+
fn = selector;
|
|
268
|
+
context.addEventListener(type, fn);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Build an html fragment from a string.
|
|
274
|
+
* @param {string} html The HTML code.
|
|
275
|
+
* @return {object} A document fragment.
|
|
276
|
+
*/
|
|
277
|
+
function buildFragment(html) {
|
|
278
|
+
const template = createElement('template');
|
|
279
|
+
template.innerHTML = html.trim();
|
|
280
|
+
return template.content;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Deep clone an object.
|
|
285
|
+
* @param {object} obj The object to clone.
|
|
286
|
+
* @return {object} The clone object.
|
|
287
|
+
*/
|
|
288
|
+
function cloneObject(obj) {
|
|
289
|
+
return obj ? JSON.parse(JSON.stringify(obj)) : obj;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Create an element and optionally set its attributes.
|
|
294
|
+
* @param {string} tag The HTML tag of the new element.
|
|
295
|
+
* @param {object} [attributes] The element's attributes.
|
|
296
|
+
* @return {object} An HTML element.
|
|
297
|
+
*/
|
|
298
|
+
function createElement(tag, attributes) {
|
|
299
|
+
const element = document.createElement(tag);
|
|
300
|
+
if (attributes) {
|
|
301
|
+
for (const attributeName in attributes) {
|
|
302
|
+
// Attribute names starting with underscore are actually properties
|
|
303
|
+
if (attributeName[0] === '_') {
|
|
304
|
+
element[attributeName.substring(1)] = attributes[attributeName];
|
|
305
|
+
} else {
|
|
306
|
+
element.setAttribute(attributeName, attributes[attributeName]);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return element;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Call a function only when the DOM is ready.
|
|
315
|
+
* @param {function} fn The function to call.
|
|
316
|
+
* @param {array} [args] Arguments to pass to the function.
|
|
317
|
+
*/
|
|
318
|
+
function DOMReady(fn, args) {
|
|
319
|
+
args = args !== undefined ? args : [];
|
|
320
|
+
if (document.readyState !== 'loading') {
|
|
321
|
+
fn(...args);
|
|
322
|
+
} else {
|
|
323
|
+
addListener(document, 'DOMContentLoaded', () => {
|
|
324
|
+
fn(...args);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Find the the deepest child of a node.
|
|
331
|
+
* @param {object} node The target node.
|
|
332
|
+
* @return {object} The deepest child node of our target node.
|
|
333
|
+
*/
|
|
334
|
+
function findDeepestChildNode(node) {
|
|
335
|
+
while (node.firstChild !== null) {
|
|
336
|
+
node = node.firstChild;
|
|
337
|
+
}
|
|
338
|
+
return node;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Find WYSIWYG editor instances.
|
|
343
|
+
* @param {string} selector One or more selectors pointing to textarea fields.
|
|
344
|
+
*/
|
|
345
|
+
function findEditorInstances(selector) {
|
|
346
|
+
const editorInstances = [];
|
|
347
|
+
getTargetElements(selector).forEach(textarea => {
|
|
348
|
+
const wrapper = textarea.previousElementSibling;
|
|
349
|
+
if (wrapper && hasClass(wrapper, 'wysi-wrapper')) {
|
|
350
|
+
const children = wrapper.children;
|
|
351
|
+
const toolbar = children[0];
|
|
352
|
+
const editor = children[1];
|
|
353
|
+
const instanceId = getInstanceId(editor);
|
|
354
|
+
editorInstances.push({
|
|
355
|
+
textarea,
|
|
356
|
+
wrapper,
|
|
357
|
+
toolbar,
|
|
358
|
+
editor,
|
|
359
|
+
instanceId
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
return editorInstances;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Find the current editor instance.
|
|
368
|
+
* @param {object} currentNode The possible child node of the editor instance.
|
|
369
|
+
* @return {object} The instance's editable region and toolbar, and an array of nodes that lead to it.
|
|
370
|
+
*/
|
|
371
|
+
function findInstance(currentNode) {
|
|
372
|
+
const nodes = [];
|
|
373
|
+
let ancestor, toolbar, editor;
|
|
374
|
+
|
|
375
|
+
// Find all HTML tags between the current node and the editable ancestor
|
|
376
|
+
while (currentNode && currentNode !== document.body) {
|
|
377
|
+
const tag = currentNode.tagName;
|
|
378
|
+
if (tag) {
|
|
379
|
+
if (hasClass(currentNode, 'wysi-wrapper')) {
|
|
380
|
+
// Editable ancestor found
|
|
381
|
+
ancestor = currentNode;
|
|
382
|
+
break;
|
|
383
|
+
} else {
|
|
384
|
+
nodes.push(currentNode);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
currentNode = currentNode.parentNode;
|
|
388
|
+
}
|
|
389
|
+
if (ancestor) {
|
|
390
|
+
const children = ancestor.children;
|
|
391
|
+
toolbar = children[0];
|
|
392
|
+
editor = children[1];
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
toolbar,
|
|
396
|
+
editor,
|
|
397
|
+
nodes
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Get the current selection.
|
|
403
|
+
* @return {object} The current selection.
|
|
404
|
+
*/
|
|
405
|
+
function getCurrentSelection() {
|
|
406
|
+
return currentSelection;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Get the html content of a document fragment.
|
|
411
|
+
* @param {string} fragment A document fragment.
|
|
412
|
+
* @return {string} The html content of the fragment.
|
|
413
|
+
*/
|
|
414
|
+
function getFragmentContent(fragment) {
|
|
415
|
+
const wrapper = createElement('div');
|
|
416
|
+
wrapper.appendChild(fragment);
|
|
417
|
+
return wrapper.innerHTML;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Get an editor's instance id.
|
|
422
|
+
* @param {object} editor The editor element.
|
|
423
|
+
* @return {string} The instance id.
|
|
424
|
+
*/
|
|
425
|
+
function getInstanceId(editor) {
|
|
426
|
+
return editor.dataset.wid;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Get a list of DOM elements based on a selector value.
|
|
431
|
+
* @param {(string|object)} selector A CSS selector string, a DOM element or a list of DOM elements.
|
|
432
|
+
* @return {array} A list of DOM elements.
|
|
433
|
+
*/
|
|
434
|
+
function getTargetElements(selector) {
|
|
435
|
+
// If selector is a string, get the elements that it represents
|
|
436
|
+
if (typeof selector === 'string') {
|
|
437
|
+
return Array.from(document.querySelectorAll(selector));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// If selector is a DOM element, wrap it in an array
|
|
441
|
+
if (selector instanceof Node) {
|
|
442
|
+
return [selector];
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// If selector is a NodeList or an HTMLCollection, convert it to an array
|
|
446
|
+
if (selector instanceof NodeList || selector instanceof HTMLCollection) {
|
|
447
|
+
return Array.from(selector);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// If selector is an array, find any DOM elements it contains
|
|
451
|
+
if (Array.isArray(selector)) {
|
|
452
|
+
return selector.filter(el => el instanceof Node);
|
|
453
|
+
}
|
|
454
|
+
return [];
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Try to guess the textarea element's label if any.
|
|
459
|
+
* @param {object} textarea The textarea element.
|
|
460
|
+
* @return {string} The textarea element's label or an empty string.
|
|
461
|
+
*/
|
|
462
|
+
function getTextAreaLabel(textarea) {
|
|
463
|
+
if (textarea.hasAttribute('aria-label')) {
|
|
464
|
+
return textarea.getAttribute('aria-label');
|
|
465
|
+
}
|
|
466
|
+
const parent = textarea.parentNode;
|
|
467
|
+
const id = textarea.id;
|
|
468
|
+
let labelElement;
|
|
469
|
+
|
|
470
|
+
// If the textarea element is inside a label element
|
|
471
|
+
if (parent.nodeName === 'LABEL') {
|
|
472
|
+
labelElement = parent;
|
|
473
|
+
|
|
474
|
+
// Or if the textarea element has an id, and there is a label element
|
|
475
|
+
// with an attribute "for" that points to that id
|
|
476
|
+
} else if (id !== undefined) {
|
|
477
|
+
labelElement = document.querySelector(`label[for="${id}"]`);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// If a label element is found, return the first non empty child text node
|
|
481
|
+
if (labelElement) {
|
|
482
|
+
const textNodes = [].filter.call(labelElement.childNodes, n => n.nodeType === 3);
|
|
483
|
+
const texts = textNodes.map(n => n.textContent.replace(/\s+/g, ' ').trim());
|
|
484
|
+
const label = texts.filter(l => l !== '')[0];
|
|
485
|
+
if (label) {
|
|
486
|
+
return label;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return '';
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Restore a previous selection if any.
|
|
494
|
+
*/
|
|
495
|
+
function restoreSelection() {
|
|
496
|
+
if (currentSelection) {
|
|
497
|
+
setSelection(currentSelection);
|
|
498
|
+
currentSelection = undefined;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Set the value of the current selection.
|
|
504
|
+
* @param {object} range The range to set.
|
|
505
|
+
*/
|
|
506
|
+
function setCurrentSelection(range) {
|
|
507
|
+
currentSelection = range;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Set the selection to a range.
|
|
512
|
+
* @param {object} range The range to select.
|
|
513
|
+
*/
|
|
514
|
+
function setSelection(range) {
|
|
515
|
+
const selection = document.getSelection();
|
|
516
|
+
selection.removeAllRanges();
|
|
517
|
+
selection.addRange(range);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Set the expanded state of a button.
|
|
522
|
+
* @param {object} button The button.
|
|
523
|
+
* @param {boolean} expanded The expanded state.
|
|
524
|
+
*/
|
|
525
|
+
function toggleButton(button, expanded) {
|
|
526
|
+
button.setAttribute('aria-expanded', expanded);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Execute an action.
|
|
531
|
+
* @param {string} action The action to execute.
|
|
532
|
+
* @param {object} editor The editor instance.
|
|
533
|
+
* @param {array} [options] Optional action parameters.
|
|
534
|
+
*/
|
|
535
|
+
function execAction(action, editor, options = []) {
|
|
536
|
+
const tool = toolset[action];
|
|
537
|
+
if (tool) {
|
|
538
|
+
const command = tool.command || action;
|
|
539
|
+
|
|
540
|
+
// Restore selection if any
|
|
541
|
+
restoreSelection();
|
|
542
|
+
|
|
543
|
+
// Execute the tool's action
|
|
544
|
+
const blockedInside = { hr: 'pre, td, th', format: 'pre' }[action];
|
|
545
|
+
const customTools = { code: toggleInlineCode, codeBlock: toggleCodeBlock, quote: toggleQuote };
|
|
546
|
+
const anchor = document.getSelection().anchorNode;
|
|
547
|
+
const element = anchor && (anchor.nodeType === 1 ? anchor : anchor.parentElement);
|
|
548
|
+
if (customTools[action]) {
|
|
549
|
+
customTools[action](editor);
|
|
550
|
+
} else if (action === 'indent' || action === 'outdent') {
|
|
551
|
+
// outside a list these add or remove quotes, which the quote button handles
|
|
552
|
+
if (element && element.closest('li') && editor.contains(element)) {
|
|
553
|
+
execEditorCommand(command, options);
|
|
554
|
+
}
|
|
555
|
+
} else if (!(blockedInside && element && element.closest(blockedInside))) {
|
|
556
|
+
execEditorCommand(command, options);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Focus the editor instance
|
|
560
|
+
editor.focus();
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Execute an editor command.
|
|
566
|
+
* @param {string} command The command to execute.
|
|
567
|
+
* @param {array} [options] Optional command parameters.
|
|
568
|
+
*/
|
|
569
|
+
function execEditorCommand(command, options) {
|
|
570
|
+
switch (command) {
|
|
571
|
+
// Block level formatting
|
|
572
|
+
case 'quote':
|
|
573
|
+
options[0] = 'blockquote';
|
|
574
|
+
case 'format':
|
|
575
|
+
execCommand('formatBlock', `<${options[0]}>`);
|
|
576
|
+
break;
|
|
577
|
+
|
|
578
|
+
// Links
|
|
579
|
+
case 'link':
|
|
580
|
+
const [linkUrl, linkTarget = '', linkText] = options;
|
|
581
|
+
if (linkText) {
|
|
582
|
+
const targetAttr = linkTarget !== '' ? ` target="${linkTarget}"` : '';
|
|
583
|
+
const linkTag = `<a href="${escape(linkUrl)}"${targetAttr}>${linkText}</a>`;
|
|
584
|
+
execCommand('insertHTML', linkTag);
|
|
585
|
+
}
|
|
586
|
+
break;
|
|
587
|
+
|
|
588
|
+
// Images
|
|
589
|
+
case 'image':
|
|
590
|
+
const styles = [];
|
|
591
|
+
const [imageUrl, altText = '', size, position, originalHtml] = options;
|
|
592
|
+
if (size !== '') {
|
|
593
|
+
styles.push(`width: ${size};`);
|
|
594
|
+
}
|
|
595
|
+
if (position !== '') {
|
|
596
|
+
if (position === 'center') {
|
|
597
|
+
styles.push('display: block; margin: auto;');
|
|
598
|
+
} else {
|
|
599
|
+
styles.push(`float: ${position};`);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const styleAttr = styles.length > 0 ? ` style="${styles.join(' ')}"` : '';
|
|
603
|
+
const image = `<img src="${escape(imageUrl)}" alt="${escape(altText)}" class="wysi-selected"${styleAttr}>`;
|
|
604
|
+
let imageTag = image;
|
|
605
|
+
if (originalHtml) {
|
|
606
|
+
// Swap the image inside its link
|
|
607
|
+
const link = buildFragment(originalHtml).firstElementChild;
|
|
608
|
+
link.querySelector('img').replaceWith(buildFragment(image));
|
|
609
|
+
imageTag = link.outerHTML;
|
|
610
|
+
}
|
|
611
|
+
execCommand('insertHTML', imageTag);
|
|
612
|
+
break;
|
|
613
|
+
|
|
614
|
+
// All the other commands
|
|
615
|
+
default:
|
|
616
|
+
execCommand(command);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/**
|
|
621
|
+
* Render a list box.
|
|
622
|
+
* @param {object} details The list box properties and data.
|
|
623
|
+
* @return {object} A DOM element containing the list box.
|
|
624
|
+
*/
|
|
625
|
+
function renderListBox(details) {
|
|
626
|
+
const label = details.label;
|
|
627
|
+
const items = details.items;
|
|
628
|
+
const firstItem = items[0];
|
|
629
|
+
const classes = ['wysi-listbox'].concat(details.classes || []);
|
|
630
|
+
|
|
631
|
+
// List box wrapper
|
|
632
|
+
const listBox = createElement('div', {
|
|
633
|
+
class: classes.join(' ')
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
// List box button
|
|
637
|
+
const button = createElement('button', {
|
|
638
|
+
type: 'button',
|
|
639
|
+
title: label,
|
|
640
|
+
'aria-label': `${label} ${firstItem.label}`,
|
|
641
|
+
'aria-haspopup': 'listbox',
|
|
642
|
+
'aria-expanded': false,
|
|
643
|
+
_innerHTML: renderListBoxItem(firstItem)
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
// List box menu
|
|
647
|
+
const menu = createElement('div', {
|
|
648
|
+
role: 'listbox',
|
|
649
|
+
tabindex: -1,
|
|
650
|
+
'aria-label': label
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
// List box items
|
|
654
|
+
items.forEach(item => {
|
|
655
|
+
const option = createElement('button', {
|
|
656
|
+
type: 'button',
|
|
657
|
+
role: 'option',
|
|
658
|
+
tabindex: -1,
|
|
659
|
+
'aria-label': item.label,
|
|
660
|
+
'aria-selected': false,
|
|
661
|
+
'data-action': item.action,
|
|
662
|
+
'data-option': item.name || '',
|
|
663
|
+
_innerHTML: renderListBoxItem(item)
|
|
664
|
+
});
|
|
665
|
+
menu.appendChild(option);
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
// Tie it all together
|
|
669
|
+
listBox.appendChild(button);
|
|
670
|
+
listBox.appendChild(menu);
|
|
671
|
+
return listBox;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Render a list box item.
|
|
676
|
+
* @param {object} item The list box item.
|
|
677
|
+
* @return {string} The list box item's content.
|
|
678
|
+
*/
|
|
679
|
+
function renderListBoxItem(item) {
|
|
680
|
+
return item.icon ? `<svg><use href="#wysi-${item.icon}"></use></svg>` : item.label;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Open a list box.
|
|
685
|
+
* @param {object} button The list box's button.
|
|
686
|
+
*/
|
|
687
|
+
function openListBox(button) {
|
|
688
|
+
const isOpen = button.getAttribute('aria-expanded') === 'true';
|
|
689
|
+
const listBox = button.nextElementSibling;
|
|
690
|
+
let selectedItem = listBox.querySelector('[aria-selected="true"]');
|
|
691
|
+
if (!selectedItem) {
|
|
692
|
+
selectedItem = listBox.firstElementChild;
|
|
693
|
+
}
|
|
694
|
+
toggleButton(button, !isOpen);
|
|
695
|
+
selectedItem.focus();
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* Select a list box item.
|
|
700
|
+
* @param {object} item The list box item.
|
|
701
|
+
*/
|
|
702
|
+
function selectListBoxItem(item) {
|
|
703
|
+
const listBox = item.parentNode;
|
|
704
|
+
const button = listBox.previousElementSibling;
|
|
705
|
+
const selectedItem = listBox.querySelector('[aria-selected="true"]');
|
|
706
|
+
if (selectedItem) {
|
|
707
|
+
selectedItem.setAttribute('aria-selected', 'false');
|
|
708
|
+
}
|
|
709
|
+
item.setAttribute('aria-selected', 'true');
|
|
710
|
+
button.innerHTML = item.innerHTML;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Close the currently open list box if any.
|
|
715
|
+
*/
|
|
716
|
+
function closeListBox() {
|
|
717
|
+
const activeListBox = document.querySelector('.wysi-listbox [aria-expanded="true"]');
|
|
718
|
+
if (activeListBox) {
|
|
719
|
+
toggleButton(activeListBox, false);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// list box button click
|
|
724
|
+
addListener(document, 'click', '.wysi-listbox > button', event => {
|
|
725
|
+
closeListBox();
|
|
726
|
+
openListBox(event.target);
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
// On key press on the list box button
|
|
730
|
+
addListener(document, 'keydown', '.wysi-listbox > button', event => {
|
|
731
|
+
switch (event.key) {
|
|
732
|
+
case 'ArrowUp':
|
|
733
|
+
case 'ArrowDown':
|
|
734
|
+
case 'Enter':
|
|
735
|
+
case ' ':
|
|
736
|
+
openListBox(event.target);
|
|
737
|
+
event.preventDefault();
|
|
738
|
+
break;
|
|
739
|
+
}
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
// When the mouse moves on a list box item, focus it
|
|
743
|
+
addListener(document.documentElement, 'mousemove', '.wysi-listbox > div > button', event => {
|
|
744
|
+
event.target.focus();
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
// On click on an list box item
|
|
748
|
+
addListener(document, 'click', '.wysi-listbox > div > button', event => {
|
|
749
|
+
const item = event.target;
|
|
750
|
+
const action = item.dataset.action;
|
|
751
|
+
const option = item.dataset.option;
|
|
752
|
+
const {
|
|
753
|
+
editor
|
|
754
|
+
} = findInstance(item);
|
|
755
|
+
const selection = document.getSelection();
|
|
756
|
+
if (selection && editor.contains(selection.anchorNode)) {
|
|
757
|
+
execAction(action, editor, [option]);
|
|
758
|
+
}
|
|
759
|
+
selectListBoxItem(item);
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
// On key press on an item
|
|
763
|
+
addListener(document, 'keydown', '.wysi-listbox > div > button', event => {
|
|
764
|
+
const item = event.target;
|
|
765
|
+
const listBox = item.parentNode;
|
|
766
|
+
const button = listBox.previousElementSibling;
|
|
767
|
+
let preventDefault = true;
|
|
768
|
+
switch (event.key) {
|
|
769
|
+
case 'ArrowUp':
|
|
770
|
+
const prev = item.previousElementSibling;
|
|
771
|
+
if (prev) {
|
|
772
|
+
prev.focus();
|
|
773
|
+
}
|
|
774
|
+
break;
|
|
775
|
+
case 'ArrowDown':
|
|
776
|
+
const next = item.nextElementSibling;
|
|
777
|
+
if (next) {
|
|
778
|
+
next.focus();
|
|
779
|
+
}
|
|
780
|
+
break;
|
|
781
|
+
case 'Home':
|
|
782
|
+
listBox.firstElementChild.focus();
|
|
783
|
+
break;
|
|
784
|
+
case 'End':
|
|
785
|
+
listBox.lastElementChild.focus();
|
|
786
|
+
break;
|
|
787
|
+
case 'Tab':
|
|
788
|
+
item.click();
|
|
789
|
+
break;
|
|
790
|
+
case 'Escape':
|
|
791
|
+
toggleButton(button, false);
|
|
792
|
+
break;
|
|
793
|
+
default:
|
|
794
|
+
preventDefault = false;
|
|
795
|
+
}
|
|
796
|
+
if (preventDefault) {
|
|
797
|
+
event.preventDefault();
|
|
798
|
+
event.stopImmediatePropagation();
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
let isOpeningInProgress = false;
|
|
802
|
+
|
|
803
|
+
// Close open popups and dropdowns on click outside
|
|
804
|
+
addListener(document, 'click', event => {
|
|
805
|
+
if (!isOpeningInProgress) {
|
|
806
|
+
closeListBox();
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
// This prevents closing a listbox immediately after opening it
|
|
811
|
+
addListener(document, 'mousedown', '.wysi-listbox > button', event => isOpeningInProgress = true);
|
|
812
|
+
addListener(document, 'mouseup', event => setTimeout(() => {
|
|
813
|
+
isOpeningInProgress = false;
|
|
814
|
+
}));
|
|
815
|
+
|
|
816
|
+
// Used to give form fields unique ids
|
|
817
|
+
let uniqueFieldId = 0;
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Render a popover form to set a tool's parameters.
|
|
821
|
+
* @param {string} toolName The tool name.
|
|
822
|
+
* @param {object} button The tool's toolbar button.
|
|
823
|
+
* @return {object} A DOM element containing the button and the popover.
|
|
824
|
+
*/
|
|
825
|
+
function renderPopover(toolName, button) {
|
|
826
|
+
const tool = toolset[toolName];
|
|
827
|
+
const labels = tool.attributeLabels;
|
|
828
|
+
const fields = tool.attributes.map((attribute, i) => {
|
|
829
|
+
return {
|
|
830
|
+
name: attribute,
|
|
831
|
+
label: translate(labels[i])
|
|
832
|
+
};
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
// Popover wrapper
|
|
836
|
+
const wrapper = createElement('div', {
|
|
837
|
+
class: 'wysi-popover'
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
// Popover
|
|
841
|
+
const popover = createElement('div', {
|
|
842
|
+
tabindex: -1
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
// Toolbar Button
|
|
846
|
+
button.setAttribute('aria-haspopup', true);
|
|
847
|
+
button.setAttribute('aria-expanded', false);
|
|
848
|
+
wrapper.appendChild(button);
|
|
849
|
+
wrapper.appendChild(popover);
|
|
850
|
+
fields.forEach(field => {
|
|
851
|
+
// Link target requires special handling later
|
|
852
|
+
if (toolName !== 'link' || field.name !== 'target') {
|
|
853
|
+
const label = createElement('label');
|
|
854
|
+
const span = createElement('span', {
|
|
855
|
+
_textContent: field.label
|
|
856
|
+
});
|
|
857
|
+
const input = createElement('input', {
|
|
858
|
+
type: 'text',
|
|
859
|
+
form: 'wysi-detached',
|
|
860
|
+
name: `wysi-${field.name}`,
|
|
861
|
+
'data-attribute': field.name
|
|
862
|
+
});
|
|
863
|
+
label.appendChild(span);
|
|
864
|
+
label.appendChild(input);
|
|
865
|
+
popover.appendChild(label);
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
// Link popover
|
|
870
|
+
if (toolName === 'link') {
|
|
871
|
+
// Add the target attribute
|
|
872
|
+
const targetField = fields.find(f => f.name === 'target');
|
|
873
|
+
if (targetField) {
|
|
874
|
+
targetField.toolName = toolName;
|
|
875
|
+
targetField.options = tool.formOptions ? tool.formOptions.target || [] : [];
|
|
876
|
+
popover.appendChild(createElement('span', {
|
|
877
|
+
_textContent: targetField.label
|
|
878
|
+
}));
|
|
879
|
+
popover.appendChild(renderSegmentedField(targetField));
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
// The link popover needs an extra "Remove link" button
|
|
883
|
+
const extraTool = 'unlink';
|
|
884
|
+
const label = translate(toolset[extraTool].label);
|
|
885
|
+
popover.appendChild(createElement('button', {
|
|
886
|
+
type: 'button',
|
|
887
|
+
title: label,
|
|
888
|
+
'aria-label': label,
|
|
889
|
+
'data-action': extraTool,
|
|
890
|
+
_innerHTML: `<svg><use href="#wysi-delete"></use></svg>`
|
|
891
|
+
}));
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// Image popover
|
|
895
|
+
if (toolName === 'image') {
|
|
896
|
+
const imageSettings = tool.extraSettings.map((setting, i) => {
|
|
897
|
+
return {
|
|
898
|
+
name: setting,
|
|
899
|
+
label: translate(tool.extraSettingLabels[i])
|
|
900
|
+
};
|
|
901
|
+
});
|
|
902
|
+
imageSettings.forEach(setting => {
|
|
903
|
+
setting.toolName = toolName;
|
|
904
|
+
setting.options = tool.formOptions ? tool.formOptions[setting.name] || [] : [];
|
|
905
|
+
popover.appendChild(createElement('span', {
|
|
906
|
+
_textContent: setting.label
|
|
907
|
+
}));
|
|
908
|
+
popover.appendChild(renderSegmentedField(setting));
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
const cancel = createElement('button', {
|
|
912
|
+
type: 'button',
|
|
913
|
+
_textContent: translate('cancel')
|
|
914
|
+
});
|
|
915
|
+
const save = createElement('button', {
|
|
916
|
+
type: 'button',
|
|
917
|
+
'data-action': toolName,
|
|
918
|
+
_textContent: translate('save')
|
|
919
|
+
});
|
|
920
|
+
popover.appendChild(cancel);
|
|
921
|
+
popover.appendChild(save);
|
|
922
|
+
return wrapper;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/**
|
|
926
|
+
* Render a segmented form field.
|
|
927
|
+
* @param {object} field The field attributes.
|
|
928
|
+
* @return {object} A DOM element representing the segmented field.
|
|
929
|
+
*/
|
|
930
|
+
function renderSegmentedField(field) {
|
|
931
|
+
const fieldId = uniqueFieldId++;
|
|
932
|
+
const segmented = createElement('fieldset', {
|
|
933
|
+
class: 'wysi-segmented'
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
// Add the fieldset legend for accessibility
|
|
937
|
+
segmented.appendChild(createElement('legend', {
|
|
938
|
+
_textContent: field.label
|
|
939
|
+
}));
|
|
940
|
+
|
|
941
|
+
// Add field options
|
|
942
|
+
field.options.forEach(option => {
|
|
943
|
+
const segmentId = uniqueFieldId++;
|
|
944
|
+
segmented.appendChild(createElement('input', {
|
|
945
|
+
id: `wysi-seg-${segmentId}`,
|
|
946
|
+
name: `wysi-${field.toolName}-${field.name}-${fieldId}`,
|
|
947
|
+
type: 'radio',
|
|
948
|
+
form: 'wysi-detached',
|
|
949
|
+
'data-attribute': field.name,
|
|
950
|
+
value: option.value
|
|
951
|
+
}));
|
|
952
|
+
segmented.appendChild(createElement('label', {
|
|
953
|
+
for: `wysi-seg-${segmentId}`,
|
|
954
|
+
_textContent: translate(option.label)
|
|
955
|
+
}));
|
|
956
|
+
});
|
|
957
|
+
return segmented;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Open a popover.
|
|
962
|
+
* @param {object} button The popover's button.
|
|
963
|
+
*/
|
|
964
|
+
function openPopover(button) {
|
|
965
|
+
const inputs = button.nextElementSibling.querySelectorAll('input[type="text"]');
|
|
966
|
+
const radioButtons = button.nextElementSibling.querySelectorAll('input[type="radio"]');
|
|
967
|
+
const selection = document.getSelection();
|
|
968
|
+
const anchorNode = selection.anchorNode;
|
|
969
|
+
const {
|
|
970
|
+
editor,
|
|
971
|
+
nodes
|
|
972
|
+
} = findInstance(anchorNode);
|
|
973
|
+
const values = {};
|
|
974
|
+
if (editor) {
|
|
975
|
+
// Try to find an existing target of the popover's action from the DOM selection
|
|
976
|
+
const action = button.dataset.action;
|
|
977
|
+
const tool = toolset[action];
|
|
978
|
+
let target = editor.querySelector(`.${selectedClass}`);
|
|
979
|
+
let selectContents = false;
|
|
980
|
+
|
|
981
|
+
// If that fails, look for an element with the selection CSS class
|
|
982
|
+
if (!target) {
|
|
983
|
+
target = nodes.filter(node => tool.tags.includes(node.tagName.toLowerCase()))[0];
|
|
984
|
+
selectContents = true;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
// If an existing target is found, we will be in modification mode
|
|
988
|
+
if (target) {
|
|
989
|
+
const range = document.createRange();
|
|
990
|
+
|
|
991
|
+
// Add the target to a selection range
|
|
992
|
+
// Depending on the type of the target, select the whole node or just its contents
|
|
993
|
+
if (selectContents) {
|
|
994
|
+
range.selectNodeContents(target);
|
|
995
|
+
} else {
|
|
996
|
+
range.selectNode(target);
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
// Save the current selection for later use
|
|
1000
|
+
setCurrentSelection(range);
|
|
1001
|
+
|
|
1002
|
+
// Retrieve the current attribute values of the target for modification
|
|
1003
|
+
tool.attributes.forEach(attribute => {
|
|
1004
|
+
values[attribute] = target.getAttribute(attribute);
|
|
1005
|
+
});
|
|
1006
|
+
|
|
1007
|
+
// Process extra popover settings
|
|
1008
|
+
if (tool.extraSettings) {
|
|
1009
|
+
tool.extraSettings.forEach(setting => {
|
|
1010
|
+
const settingOptions = tool.formOptions[setting];
|
|
1011
|
+
for (const option of settingOptions) {
|
|
1012
|
+
if (!option.criterion) {
|
|
1013
|
+
continue;
|
|
1014
|
+
}
|
|
1015
|
+
const key = Object.keys(option.criterion)[0];
|
|
1016
|
+
const value = option.criterion[key];
|
|
1017
|
+
if (target.style[key] && target.style[key] === value) {
|
|
1018
|
+
values[setting] = option.value;
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
// If no existing target is found, we are adding new content
|
|
1026
|
+
} else if (selection && editor.contains(anchorNode) && selection.rangeCount) {
|
|
1027
|
+
// Save the current selection to keep track of where to insert the content
|
|
1028
|
+
setCurrentSelection(selection.getRangeAt(0));
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
// Populate the input fields with the existing values if any
|
|
1033
|
+
inputs.forEach(input => {
|
|
1034
|
+
input.value = values[input.dataset.attribute] || '';
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
// Check the relevent radio fields if any
|
|
1038
|
+
radioButtons.forEach(radio => {
|
|
1039
|
+
const value = values[radio.dataset.attribute] || '';
|
|
1040
|
+
if (radio.value === value) {
|
|
1041
|
+
radio.checked = true;
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
|
|
1045
|
+
// Open this popover
|
|
1046
|
+
toggleButton(button, true);
|
|
1047
|
+
|
|
1048
|
+
// Focus the first input field
|
|
1049
|
+
inputs[0].focus();
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
/**
|
|
1053
|
+
* Execute a popover's action.
|
|
1054
|
+
* @param {object} button The popover's action button.
|
|
1055
|
+
*/
|
|
1056
|
+
function execPopoverAction(button) {
|
|
1057
|
+
const action = button.dataset.action;
|
|
1058
|
+
const selection = getCurrentSelection();
|
|
1059
|
+
const inputs = button.parentNode.querySelectorAll('input[type="text"]');
|
|
1060
|
+
const radioButtons = button.parentNode.querySelectorAll('input[type="radio"]');
|
|
1061
|
+
const {
|
|
1062
|
+
editor
|
|
1063
|
+
} = findInstance(button);
|
|
1064
|
+
const options = [];
|
|
1065
|
+
inputs.forEach(input => {
|
|
1066
|
+
options.push(input.value);
|
|
1067
|
+
});
|
|
1068
|
+
radioButtons.forEach(radio => {
|
|
1069
|
+
if (radio.checked) {
|
|
1070
|
+
options.push(radio.value);
|
|
1071
|
+
}
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
// Workaround for links being removed when updating images
|
|
1075
|
+
if (action === 'image') {
|
|
1076
|
+
const selected = editor.querySelector(`.${selectedClass}`);
|
|
1077
|
+
const parent = selected ? selected.parentNode : {};
|
|
1078
|
+
if (selected && parent.tagName === 'A') {
|
|
1079
|
+
options.push(parent.outerHTML);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
// Save the content of the current selection to use as a link text
|
|
1083
|
+
} else if (action === 'link' && selection) {
|
|
1084
|
+
options.push(getFragmentContent(selection.cloneContents()));
|
|
1085
|
+
}
|
|
1086
|
+
execAction(action, editor, options);
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
/**
|
|
1090
|
+
* Close the open popover if any.
|
|
1091
|
+
* @param {boolean} ignoreSelection If true, do not restore the previous selection.
|
|
1092
|
+
*/
|
|
1093
|
+
function closePopover(ignoreSelection) {
|
|
1094
|
+
const popover = document.querySelector('.wysi-popover [aria-expanded="true"]');
|
|
1095
|
+
if (popover) {
|
|
1096
|
+
toggleButton(popover, false);
|
|
1097
|
+
}
|
|
1098
|
+
if (!ignoreSelection) {
|
|
1099
|
+
restoreSelection();
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
// Open a popover
|
|
1104
|
+
addListener(document, 'click', '.wysi-popover > button', event => {
|
|
1105
|
+
closePopover();
|
|
1106
|
+
openPopover(event.target);
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
// On key press on the popover button
|
|
1110
|
+
addListener(document, 'keydown', '.wysi-popover > button', event => {
|
|
1111
|
+
switch (event.key) {
|
|
1112
|
+
case 'ArrowUp':
|
|
1113
|
+
case 'ArrowDown':
|
|
1114
|
+
case 'Enter':
|
|
1115
|
+
case ' ':
|
|
1116
|
+
openPopover(event.target);
|
|
1117
|
+
event.preventDefault();
|
|
1118
|
+
break;
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
|
|
1122
|
+
// Execute the popover action
|
|
1123
|
+
addListener(document, 'click', '.wysi-popover > div > button[data-action]', event => {
|
|
1124
|
+
execPopoverAction(event.target);
|
|
1125
|
+
closePopover(true);
|
|
1126
|
+
});
|
|
1127
|
+
|
|
1128
|
+
// Cancel the popover
|
|
1129
|
+
addListener(document, 'click', '.wysi-popover > div > button:not([data-action])', event => {
|
|
1130
|
+
closePopover();
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
// Prevent clicks on the popover content to propagate (keep popover open)
|
|
1134
|
+
addListener(document, 'click', '.wysi-popover *:not(button)', event => {
|
|
1135
|
+
event.stopImmediatePropagation();
|
|
1136
|
+
});
|
|
1137
|
+
|
|
1138
|
+
// Trap focus inside a popover until it's closed
|
|
1139
|
+
addListener(document, 'keydown', '.wysi-popover *', event => {
|
|
1140
|
+
const target = event.target;
|
|
1141
|
+
const parent = target.parentNode;
|
|
1142
|
+
const form = parent.tagName === 'DIV' ? parent : parent.parentNode;
|
|
1143
|
+
switch (event.key) {
|
|
1144
|
+
case 'Tab':
|
|
1145
|
+
const firstField = form.querySelector('input');
|
|
1146
|
+
if (event.shiftKey) {
|
|
1147
|
+
if (target === firstField) {
|
|
1148
|
+
form.lastElementChild.focus();
|
|
1149
|
+
event.preventDefault();
|
|
1150
|
+
}
|
|
1151
|
+
} else {
|
|
1152
|
+
if (!target.nextElementSibling && !target.parentNode.nextElementSibling) {
|
|
1153
|
+
firstField.focus();
|
|
1154
|
+
event.preventDefault();
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
break;
|
|
1158
|
+
case 'Enter':
|
|
1159
|
+
if (target.tagName === 'INPUT') {
|
|
1160
|
+
const actionButton = form.querySelector('[data-action]:last-of-type');
|
|
1161
|
+
actionButton.click();
|
|
1162
|
+
event.preventDefault();
|
|
1163
|
+
}
|
|
1164
|
+
break;
|
|
1165
|
+
case 'Escape':
|
|
1166
|
+
closePopover();
|
|
1167
|
+
event.stopImmediatePropagation();
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
let isSelectionInProgress = false;
|
|
1172
|
+
|
|
1173
|
+
// Close open popups and dropdowns on click outside
|
|
1174
|
+
addListener(document, 'click', event => {
|
|
1175
|
+
if (!isSelectionInProgress) {
|
|
1176
|
+
closePopover();
|
|
1177
|
+
}
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
// Text selection within a popover is in progress
|
|
1181
|
+
// This helps avoid closing a popover when the end of a text selection is outside it
|
|
1182
|
+
addListener(document, 'mousedown', '.wysi-popover, .wysi-popover *', event => {
|
|
1183
|
+
isSelectionInProgress = true;
|
|
1184
|
+
});
|
|
1185
|
+
|
|
1186
|
+
// The text selection ended
|
|
1187
|
+
addListener(document, 'mouseup', event => {
|
|
1188
|
+
setTimeout(() => {
|
|
1189
|
+
isSelectionInProgress = false;
|
|
1190
|
+
});
|
|
1191
|
+
});
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* Render the toolbar.
|
|
1195
|
+
* @param {array} tools The list of tools in the toolbar.
|
|
1196
|
+
* @return {string} The toolbars HTML string.
|
|
1197
|
+
*/
|
|
1198
|
+
function renderToolbar(tools) {
|
|
1199
|
+
const toolbar = createElement('div', {
|
|
1200
|
+
class: 'wysi-toolbar'
|
|
1201
|
+
});
|
|
1202
|
+
|
|
1203
|
+
// Generate toolbar buttons
|
|
1204
|
+
tools.forEach(toolName => {
|
|
1205
|
+
switch (toolName) {
|
|
1206
|
+
// Toolbar separator
|
|
1207
|
+
case '|':
|
|
1208
|
+
toolbar.appendChild(createElement('div', {
|
|
1209
|
+
class: 'wysi-separator'
|
|
1210
|
+
}));
|
|
1211
|
+
break;
|
|
1212
|
+
|
|
1213
|
+
// Toolbar new line
|
|
1214
|
+
case '-':
|
|
1215
|
+
toolbar.appendChild(createElement('div', {
|
|
1216
|
+
class: 'wysi-newline'
|
|
1217
|
+
}));
|
|
1218
|
+
break;
|
|
1219
|
+
|
|
1220
|
+
// The format tool renders as a list box
|
|
1221
|
+
case 'format':
|
|
1222
|
+
toolbar.appendChild(renderFormatTool());
|
|
1223
|
+
break;
|
|
1224
|
+
|
|
1225
|
+
// Tables render as a menu
|
|
1226
|
+
case 'table':
|
|
1227
|
+
toolbar.appendChild(renderTableMenu(translate));
|
|
1228
|
+
break;
|
|
1229
|
+
|
|
1230
|
+
// All the other tools render as buttons
|
|
1231
|
+
default:
|
|
1232
|
+
renderTool(toolName, toolbar);
|
|
1233
|
+
}
|
|
1234
|
+
});
|
|
1235
|
+
return toolbar;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* Render a tool.
|
|
1240
|
+
* @param {string} name The tool's name.
|
|
1241
|
+
* @param {object} toolbar The toolbar to which the tool will be appended.
|
|
1242
|
+
*/
|
|
1243
|
+
function renderTool(name, toolbar) {
|
|
1244
|
+
const tool = toolset[name];
|
|
1245
|
+
const label = translate(tool.label);
|
|
1246
|
+
const button = createElement('button', {
|
|
1247
|
+
type: 'button',
|
|
1248
|
+
title: label,
|
|
1249
|
+
'aria-label': label,
|
|
1250
|
+
'aria-pressed': false,
|
|
1251
|
+
'data-action': name,
|
|
1252
|
+
_innerHTML: `<svg><use href="#wysi-${name}"></use></svg>`
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
// Tools that require parameters (e.g: image, link) need a popover
|
|
1256
|
+
if (tool.hasForm) {
|
|
1257
|
+
const popover = renderPopover(name, button);
|
|
1258
|
+
toolbar.appendChild(popover);
|
|
1259
|
+
|
|
1260
|
+
// The other tools only display a button
|
|
1261
|
+
} else {
|
|
1262
|
+
toolbar.appendChild(button);
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* Render format tool.
|
|
1268
|
+
* @return {object} A DOM element containing the format tool.
|
|
1269
|
+
*/
|
|
1270
|
+
function renderFormatTool() {
|
|
1271
|
+
const toolName = 'format';
|
|
1272
|
+
const label = translate(toolset.format.label);
|
|
1273
|
+
const paragraphLabel = translate(toolset.format.paragraph);
|
|
1274
|
+
const headingLabel = translate(toolset.format.heading);
|
|
1275
|
+
const classes = 'wysi-format';
|
|
1276
|
+
const items = toolset.format.tags.map(tag => {
|
|
1277
|
+
const name = tag;
|
|
1278
|
+
const label = tag === 'p' ? paragraphLabel : `${headingLabel} ${tag.substring(1)}`;
|
|
1279
|
+
const action = 'format';
|
|
1280
|
+
return {
|
|
1281
|
+
name,
|
|
1282
|
+
label,
|
|
1283
|
+
action
|
|
1284
|
+
};
|
|
1285
|
+
});
|
|
1286
|
+
return renderListBox({
|
|
1287
|
+
label,
|
|
1288
|
+
items,
|
|
1289
|
+
classes
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* Update toolbar buttons state.
|
|
1295
|
+
*/
|
|
1296
|
+
function updateToolbarState() {
|
|
1297
|
+
const selection = document.getSelection();
|
|
1298
|
+
const anchorNode = selection.anchorNode;
|
|
1299
|
+
if (!anchorNode) {
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
const range = selection.getRangeAt(0);
|
|
1303
|
+
|
|
1304
|
+
// This is to fix double click selection on Firefox not highlighting the relevant tool in some cases
|
|
1305
|
+
// We want to find the deepest child node to properly handle nested styles
|
|
1306
|
+
const candidateNode = findDeepestChildNode(range.startContainer.nextElementSibling || range.startContainer);
|
|
1307
|
+
|
|
1308
|
+
// Fallback to the original selection.anchorNode if a more suitable node is not found
|
|
1309
|
+
const selectedNode = range.intersectsNode(candidateNode) ? candidateNode : anchorNode;
|
|
1310
|
+
|
|
1311
|
+
// Get editor instance
|
|
1312
|
+
const {
|
|
1313
|
+
toolbar,
|
|
1314
|
+
editor,
|
|
1315
|
+
nodes
|
|
1316
|
+
} = findInstance(selectedNode);
|
|
1317
|
+
const tags = nodes.map(node => node.tagName.toLowerCase());
|
|
1318
|
+
|
|
1319
|
+
// Abort if the selection is not within an editor instance
|
|
1320
|
+
if (!editor) {
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
// Check for an element with the selection class (likely an image)
|
|
1325
|
+
const selectedObject = editor.querySelector(`.${selectedClass}`);
|
|
1326
|
+
|
|
1327
|
+
// If such element exists, add its tag to the list of active tags
|
|
1328
|
+
if (selectedObject) {
|
|
1329
|
+
tags.push(selectedObject.tagName.toLowerCase());
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
// Get the list of allowed tags in the current editor instance
|
|
1333
|
+
const instanceId = getInstanceId(editor);
|
|
1334
|
+
const allowedTags = instances[instanceId].allowedTags;
|
|
1335
|
+
|
|
1336
|
+
// Reset the state of all buttons
|
|
1337
|
+
toolbar.querySelectorAll('[aria-pressed="true"]').forEach(button => button.setAttribute('aria-pressed', 'false'));
|
|
1338
|
+
|
|
1339
|
+
// Reset the state of all list boxes
|
|
1340
|
+
toolbar.querySelectorAll('.wysi-listbox > div > button:first-of-type').forEach(button => selectListBoxItem(button));
|
|
1341
|
+
|
|
1342
|
+
// Update the buttons states
|
|
1343
|
+
tags.forEach((tag, i) => {
|
|
1344
|
+
switch (tag) {
|
|
1345
|
+
case 'p':
|
|
1346
|
+
case 'h1':
|
|
1347
|
+
case 'h2':
|
|
1348
|
+
case 'h3':
|
|
1349
|
+
case 'h4':
|
|
1350
|
+
case 'h5':
|
|
1351
|
+
case 'h6':
|
|
1352
|
+
case 'li':
|
|
1353
|
+
const format = toolbar.querySelector(`[data-action="format"][data-option="${tag}"]`);
|
|
1354
|
+
const textAlign = nodes[i].style.textAlign || nodes[i].getAttribute('align');
|
|
1355
|
+
if (format) {
|
|
1356
|
+
selectListBoxItem(format);
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
// Check for text align
|
|
1360
|
+
if (textAlign) {
|
|
1361
|
+
const action = 'align' + textAlign.charAt(0).toUpperCase() + textAlign.slice(1);
|
|
1362
|
+
const button = toolbar.querySelector(`[data-action="${action}"]`);
|
|
1363
|
+
if (button) {
|
|
1364
|
+
if (button.parentNode.getAttribute('role') === 'listbox') {
|
|
1365
|
+
selectListBoxItem(button);
|
|
1366
|
+
} else {
|
|
1367
|
+
button.setAttribute('aria-pressed', 'true');
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
break;
|
|
1372
|
+
default:
|
|
1373
|
+
if (tag === 'code' && tags.includes('pre')) {
|
|
1374
|
+
break;
|
|
1375
|
+
}
|
|
1376
|
+
const allowedTag = allowedTags[tag];
|
|
1377
|
+
const action = allowedTag ? allowedTag.toolName : undefined;
|
|
1378
|
+
if (action) {
|
|
1379
|
+
const button = toolbar.querySelector(`[data-action="${action}"]`);
|
|
1380
|
+
button.setAttribute('aria-pressed', 'true');
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* Embed SVG icons in the HTML document.
|
|
1388
|
+
*/
|
|
1389
|
+
function embedSVGIcons() {
|
|
1390
|
+
// The icons will be included during the build process
|
|
1391
|
+
const icons = '<svg id="wysi-svg-icons" xmlns="http://www.w3.org/2000/svg"><defs><symbol id="wysi-bold" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" d="M8 6h4.5a3 3 0 0 1 0 6H8zm0 6h5.5a3 3 0 0 1 0 6H8z"></path></symbol><symbol id="wysi-italic" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M11 6h6M7 18h6M14 6l-4 12"></path></symbol><symbol id="wysi-code" viewBox="0 0 24 24"><path d="M9.7 7.3a1 1 0 0 1 0 1.4L6.4 12l3.3 3.3a1 1 0 0 1-1.4 1.4l-4-4a1 1 0 0 1 0-1.4l4-4a1 1 0 0 1 1.4 0zM14.3 7.3a1 1 0 0 1 1.4 0l4 4a1 1 0 0 1 0 1.4l-4 4a1 1 0 0 1-1.4-1.4l3.3-3.3-3.3-3.3a1 1 0 0 1 0-1.4z"></path></symbol><symbol id="wysi-codeBlock" viewBox="0 0 24 24"><path d="M6 4a3 3 0 0 0-3 3v10a3 3 0 0 0 3 3h12a3 3 0 0 0 3-3V7a3 3 0 0 0-3-3H6zm0 2h12a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1zM10.7 9.3a1 1 0 0 1 0 1.4L9.4 12l1.3 1.3a1 1 0 0 1-1.4 1.4l-2-2a1 1 0 0 1 0-1.4l2-2a1 1 0 0 1 1.4 0zM13.3 9.3a1 1 0 0 1 1.4 0l2 2a1 1 0 0 1 0 1.4l-2 2a1 1 0 0 1-1.4-1.4l1.3-1.3-1.3-1.3a1 1 0 0 1 0-1.4z"></path></symbol><symbol id="wysi-table" viewBox="0 0 24 24"><path d="M6 4a3 3 0 0 0-3 3v10a3 3 0 0 0 3 3h12a3 3 0 0 0 3-3V7a3 3 0 0 0-3-3H6zM5 10V7a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v3H5zm0 2h6v6H6a1 1 0 0 1-1-1v-5zm8 0h6v5a1 1 0 0 1-1 1h-5v-6z"></path></symbol><symbol id="wysi-strike" viewBox="0 0 24 24"><path d="M12 6C9.33 6 7.5 7.34 7.5 9.5c0 .58.12 1.07.35 1.5H13c-1.49-.34-3.49-.48-3.5-1.5 0-1.03 1.08-1.75 2.5-1.75s2.5.83 2.5 1.75h2C16.5 7.4 14.67 6 12 6zm-5.5 6c-.67 0-.67 1 0 1h4.35c.5.17 1.04.34 1.65.5.58.15 1.75.23 1.75 1s-.66 1.75-2.25 1.75-2.5-1.01-2.5-1.75h-2c0 1.64 1.33 3.5 4.5 3.5s4.5-2.08 4.5-3.5c0-.58-.05-1.07-.2-1.5h1.2c.67 0 .67-1 0-1z"></path></symbol><symbol id="wysi-ul" viewBox="0 0 24 24"><path d="M3 6a1 1 0 0 0-1 1 1 1 0 0 0 1 1 1 1 0 0 0 1-1 1 1 0 0 0-1-1zm4 0a1 1 0 0 0 0 2h14a1 1 0 0 0 0-2H7zm-4 5a1 1 0 0 0-1 1 1 1 0 0 0 1 1 1 1 0 0 0 1-1 1 1 0 0 0-1-1zm4 0a1 1 0 0 0 0 2h14a1 1 0 0 0 0-2H7zm-4 5a1 1 0 0 0-1 1 1 1 0 0 0 1 1 1 1 0 0 0 1-1 1 1 0 0 0-1-1zm4 0a1 1 0 0 0 0 2h14a1 1 0 0 0 0-2H7z"></path></symbol><symbol id="wysi-ol" viewBox="0 0 24 24"><path d="M4 5c-.25 0-.5.17-.5.5v3c0 .67 1 .67 1 0v-3c0-.33-.25-.5-.5-.5zm4.5 1c-1.33 0-1.33 2 0 2h12c1.33 0 1.33-2 0-2zm-6 5.5h.75c0-.43.34-.75.75-.75.4 0 .75.28.75.75L2.5 13.25V14h3v-.75H3.75L5.5 12v-.5c0-.9-.73-1.49-1.5-1.5-.77 0-1.5.59-1.5 1.5zm6-.5c-1.33 0-1.33 2 0 2h12c1.33 0 1.33-2 0-2zM4 15c-.83 0-1.5.63-1.5 1.25h.75c0-.28.34-.5.75-.5s.75.22.75.5-.34.5-.75.5v.5c.41 0 .75.22.75.5s-.34.5-.75.5-.75-.22-.75-.5H2.5c0 .62.67 1.25 1.5 1.25s1.5-.5 1.5-1.12c0-.34-.2-.66-.56-.88.35-.2.56-.53.56-.87 0-.62-.67-1.12-1.5-1.12zm4.5 1c-1.33 0-1.33 2 0 2h12c1.33 0 1.33-2 0-2z"></path></symbol><symbol id="wysi-indent" viewBox="0 0 24 24"><path d="m20 8h-15.9c-1.33 0-1.33-2 0-2h15.9c1.33 0 1.33 2 0 2zm2.86e-4 5h-9.08c-1.33 0-1.33-2 0-2h9.08c1.33 0 1.33 2 0 2zm-16.7-3.31c0.356-0.423 0.988-0.477 1.41-0.12l2 1.66c0.483 0.4 0.483 1.14 0 1.54l-2 1.66c-0.179 0.153-0.405 0.238-0.64 0.24-0.297 4.83e-4 -0.58-0.131-0.77-0.36-0.354-0.425-0.296-1.06 0.13-1.41l1.08-0.9-1.08-0.9c-0.426-0.353-0.484-0.985-0.13-1.41zm0.77 6.31h15.9c1.33 0 1.33 2 0 2h-15.9c-1.33 0-1.33-2 0-2z"></path></symbol><symbol id="wysi-outdent" viewBox="0 0 24 24"><path d="m4.1 6c-1.33 0-1.33 2 0 2h15.9c1.33 0 1.33-2 0-2h-15.9zm1.96 3.33c-0.224 0.00238-0.448 0.0803-0.633 0.236l-2 1.66c-0.483 0.4-0.483 1.14 0 1.54l2 1.66c0.179 0.153 0.404 0.238 0.639 0.24 0.297 4.83e-4 0.581-0.131 0.771-0.359 0.354-0.425 0.295-1.06-0.131-1.41l-1.08-0.9 1.08-0.9c0.426-0.353 0.485-0.985 0.131-1.41-0.2-0.238-0.489-0.359-0.777-0.355zm4.88 1.67c-1.33 0-1.33 2 0 2h9.08c1.33 0 1.33-2 0-2h-9.08zm-6.87 5c-1.33 0-1.33 2 0 2h15.9c1.33 0 1.33-2 0-2h-15.9z"></path></symbol><symbol id="wysi-link" viewBox="0 0 24 24"><path d="M8,12a1,1,0,0,0,1,1h6a1,1,0,0,0,0-2H9A1,1,0,0,0,8,12Zm2,3H7A3,3,0,0,1,7,9h3a1,1,0,0,0,0-2H7A5,5,0,0,0,7,17h3a1,1,0,0,0,0-2Zm7-8H14a1,1,0,0,0,0,2h3a3,3,0,0,1,0,6H14a1,1,0,0,0,0,2h3A5,5,0,0,0,17,7Z"></path></symbol><symbol id="wysi-image" viewBox="0 0 24 24"><path d="M6 5a3 3 0 0 0-3 3v8a3 3 0 0 0 3 3h12a3 3 0 0 0 3-3V8a3 3 0 0 0-3-3H6zm0 2h12a1 1 0 0 1 1 1v5.73l-.88-.88a3.06 3.06 0 0 0-4.24 0l-.88.88-2.88-2.88A3.06 3.06 0 0 0 8 10a3.06 3.06 0 0 0-2.12.85l-.88.88V8a1 1 0 0 1 1-1zm1.85 4.98a1 1 0 0 1 .85.27L13.45 17H6a1 1 0 0 1-.98-.92H5v-1.53l2.3-2.3a1 1 0 0 1 .55-.26zm8 2a1 1 0 0 1 .85.27l2.17 2.16c-.19.33-.55.59-.86.59h-1.72l-1.86-1.87.88-.88a1 1 0 0 1 .54-.28z"></path></symbol><symbol id="wysi-quote" viewBox="0 0 24 24"><path d="m9 6c-2.2 0-4 1.96-4 4.36v6c0 0.903 0.672 1.64 1.5 1.64h3c0.828 0 1.5-0.733 1.5-1.64v-3.27c0-0.903-0.672-1.64-1.5-1.64h-1.75c-0.414 0-0.75-0.367-0.75-0.818v-0.273c0-1.2 0.899-2.18 2-2.18h0.5c0.274 0 0.5-0.246 0.5-0.545v-1.09c0-0.298-0.226-0.545-0.5-0.545zm8 0c-2.2 0-4 1.96-4 4.36v6c0 0.903 0.672 1.64 1.5 1.64h3c0.828 0 1.5-0.733 1.5-1.64v-3.27c0-0.903-0.672-1.64-1.5-1.64h-1.75c-0.414 0-0.75-0.367-0.75-0.818v-0.273c0-1.2 0.899-2.18 2-2.18h0.5c0.274 0 0.5-0.246 0.5-0.545v-1.09c0-0.298-0.226-0.545-0.5-0.545z"></path></symbol><symbol id="wysi-hr" viewBox="0 0 24 24"><path d="m20 11h-16c-1.33 0-1.33 2 0 2 0 0 16-0.018 16 0 1.33 0 1.33-2 0-2z"></path></symbol><symbol id="wysi-delete" viewBox="0 0 24 24"><path fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M4 7h16M9 7V4h6v3M6 7l1 13h10l1-13M10 11v5M14 11v5"></path></symbol></defs></svg>';
|
|
1392
|
+
const svgElement = buildFragment(icons);
|
|
1393
|
+
document.body.appendChild(svgElement);
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
// Deselect selected element when clicking outside
|
|
1397
|
+
addListener(document, 'mousedown', '.wysi-editor, .wysi-editor *', event => {
|
|
1398
|
+
const selected = document.querySelector(`.${selectedClass}`);
|
|
1399
|
+
if (selected && selected !== event.target) {
|
|
1400
|
+
selected.classList.remove(selectedClass);
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
|
|
1404
|
+
// Select an image when it's clicked
|
|
1405
|
+
addListener(document, 'mousedown', '.wysi-editor img', event => {
|
|
1406
|
+
const image = event.target;
|
|
1407
|
+
const range = document.createRange();
|
|
1408
|
+
image.classList.add(selectedClass);
|
|
1409
|
+
range.selectNode(image);
|
|
1410
|
+
setSelection(range);
|
|
1411
|
+
});
|
|
1412
|
+
|
|
1413
|
+
// Toolbar button click
|
|
1414
|
+
addListener(document, 'click', '.wysi-toolbar > button', event => {
|
|
1415
|
+
const button = event.target;
|
|
1416
|
+
const action = button.dataset.action;
|
|
1417
|
+
const {
|
|
1418
|
+
editor
|
|
1419
|
+
} = findInstance(button);
|
|
1420
|
+
const selection = document.getSelection();
|
|
1421
|
+
if (selection && editor.contains(selection.anchorNode)) {
|
|
1422
|
+
execAction(action, editor);
|
|
1423
|
+
}
|
|
1424
|
+
});
|
|
1425
|
+
|
|
1426
|
+
// Update the toolbar buttons state
|
|
1427
|
+
addListener(document, 'selectionchange', updateToolbarState);
|
|
1428
|
+
addListener(document, 'input', '.wysi-editor', updateToolbarState);
|
|
1429
|
+
|
|
1430
|
+
// include SVG icons
|
|
1431
|
+
DOMReady(embedSVGIcons);
|
|
1432
|
+
|
|
1433
|
+
const STYLE_ATTRIBUTE = 'style';
|
|
1434
|
+
const ALIGN_ATTRIBUTE = 'align';
|
|
1435
|
+
const DEFAULT_PARAGRAPH = '<p><br></p>';
|
|
1436
|
+
|
|
1437
|
+
/**
|
|
1438
|
+
* Enable HTML tags belonging to a set of tools.
|
|
1439
|
+
* @param {array} tools A array of tool objects.
|
|
1440
|
+
* @return {object} The list of allowed tags.
|
|
1441
|
+
*/
|
|
1442
|
+
function enableTags(tools) {
|
|
1443
|
+
const allowedTags = cloneObject(settings.allowedTags);
|
|
1444
|
+
tools.forEach(toolName => {
|
|
1445
|
+
const tool = cloneObject(toolset[toolName]);
|
|
1446
|
+
if (!tool || !tool.tags) {
|
|
1447
|
+
return;
|
|
1448
|
+
}
|
|
1449
|
+
const isEmpty = !!tool.isEmpty;
|
|
1450
|
+
const extraTags = tool.extraTags || [];
|
|
1451
|
+
const aliasList = tool.alias || [];
|
|
1452
|
+
const alias = aliasList.length ? tool.tags[0] : undefined;
|
|
1453
|
+
const tags = [...tool.tags, ...extraTags, ...aliasList];
|
|
1454
|
+
const attributes = (tool.attributes || []).concat(tool.keepAttributes || []);
|
|
1455
|
+
const styles = tool.styles || [];
|
|
1456
|
+
tags.forEach(tag => {
|
|
1457
|
+
allowedTags[tag] = {
|
|
1458
|
+
attributes,
|
|
1459
|
+
styles,
|
|
1460
|
+
alias,
|
|
1461
|
+
isEmpty
|
|
1462
|
+
};
|
|
1463
|
+
if (!extraTags.includes(tag)) {
|
|
1464
|
+
allowedTags[tag].toolName = toolName;
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
return allowedTags;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* Prepare raw content for editing.
|
|
1473
|
+
* @param {string} content The raw content.
|
|
1474
|
+
* @param {array} allowedTags The list of allowed tags.
|
|
1475
|
+
* @param {boolean} filterOnly If true, only filter the content, without further cleaning.
|
|
1476
|
+
* @return {string} The filtered HTML content.
|
|
1477
|
+
*/
|
|
1478
|
+
function prepareContent(content, allowedTags, filterOnly) {
|
|
1479
|
+
const container = createElement('div');
|
|
1480
|
+
const fragment = buildFragment(content);
|
|
1481
|
+
filterContent(fragment, allowedTags);
|
|
1482
|
+
if (!filterOnly) {
|
|
1483
|
+
wrapTextNodes(fragment);
|
|
1484
|
+
cleanContent(fragment, allowedTags);
|
|
1485
|
+
}
|
|
1486
|
+
container.appendChild(fragment);
|
|
1487
|
+
return container.innerHTML;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
/**
|
|
1491
|
+
* Replace a DOM element with another while preserving its content.
|
|
1492
|
+
* @param {object} node The element to replace.
|
|
1493
|
+
* @param {string} tag The HTML tag of the new element.
|
|
1494
|
+
* @param {boolean} [copyAttributes] If true, also copy the original element's attributes.
|
|
1495
|
+
*/
|
|
1496
|
+
function replaceNode(node, tag, copyAttributes) {
|
|
1497
|
+
const newElement = createElement(tag);
|
|
1498
|
+
const parentNode = node.parentNode;
|
|
1499
|
+
const attributes = node.attributes;
|
|
1500
|
+
|
|
1501
|
+
// Copy the original element's content
|
|
1502
|
+
newElement.innerHTML = node.innerHTML || node.textContent || node.outerHTML;
|
|
1503
|
+
|
|
1504
|
+
// Copy the original element's attributes
|
|
1505
|
+
if (copyAttributes && attributes) {
|
|
1506
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
1507
|
+
newElement.setAttribute(attributes[i].name, attributes[i].value);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
// Replace the element
|
|
1512
|
+
parentNode.replaceChild(newElement, node);
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/**
|
|
1516
|
+
* Remove unsupported CSS styles from a node.
|
|
1517
|
+
* @param {object} node The element to filter.
|
|
1518
|
+
* @param {array} allowedStyles An array of supported styles.
|
|
1519
|
+
*/
|
|
1520
|
+
function filterStyles(node, allowedStyles) {
|
|
1521
|
+
const styleAttribute = node.getAttribute(STYLE_ATTRIBUTE);
|
|
1522
|
+
if (styleAttribute) {
|
|
1523
|
+
// Parse the styles
|
|
1524
|
+
const styles = styleAttribute.split(';').map(style => {
|
|
1525
|
+
const prop = style.split(':');
|
|
1526
|
+
return {
|
|
1527
|
+
name: prop[0].trim(),
|
|
1528
|
+
value: prop[1]
|
|
1529
|
+
};
|
|
1530
|
+
})
|
|
1531
|
+
// Filter the styles
|
|
1532
|
+
.filter(style => allowedStyles.includes(style.name))
|
|
1533
|
+
|
|
1534
|
+
// Remove text-align: left
|
|
1535
|
+
.filter(style => style.name !== 'text-align' || style.value.trim() !== 'left')
|
|
1536
|
+
|
|
1537
|
+
// Convert back to a style string
|
|
1538
|
+
.map(({
|
|
1539
|
+
name,
|
|
1540
|
+
value
|
|
1541
|
+
}) => `${name}: ${value.trim()};`).join('');
|
|
1542
|
+
if (styles !== '') {
|
|
1543
|
+
node.setAttribute(STYLE_ATTRIBUTE, styles);
|
|
1544
|
+
} else {
|
|
1545
|
+
node.removeAttribute(STYLE_ATTRIBUTE);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
/**
|
|
1551
|
+
* Remove unsupported HTML tags and attributes.
|
|
1552
|
+
* @param {object} node The parent element to filter recursively.
|
|
1553
|
+
* @param {array} allowedTags The list of allowed tags.
|
|
1554
|
+
*/
|
|
1555
|
+
function filterContent(node, allowedTags) {
|
|
1556
|
+
const children = Array.from(node.childNodes);
|
|
1557
|
+
if (!children || !children.length) {
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
children.forEach(childNode => {
|
|
1561
|
+
// Element nodes
|
|
1562
|
+
if (childNode.nodeType === 1) {
|
|
1563
|
+
// Filter recursively (deeper nodes first)
|
|
1564
|
+
filterContent(childNode, allowedTags);
|
|
1565
|
+
|
|
1566
|
+
// Check if the current element is allowed
|
|
1567
|
+
const tag = childNode.tagName.toLowerCase();
|
|
1568
|
+
if (tag === 'input') {
|
|
1569
|
+
if (childNode.getAttribute('type') !== 'checkbox') {
|
|
1570
|
+
childNode.remove();
|
|
1571
|
+
return;
|
|
1572
|
+
}
|
|
1573
|
+
childNode.setAttribute('disabled', '');
|
|
1574
|
+
}
|
|
1575
|
+
if (isUnboldWrapper(childNode)) {
|
|
1576
|
+
childNode.replaceWith(...childNode.childNodes);
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
const allowedTag = allowedTags[tag];
|
|
1580
|
+
const attributes = Array.from(childNode.attributes);
|
|
1581
|
+
|
|
1582
|
+
// Check for the deprecated align attribute (mainly in Firefox)
|
|
1583
|
+
const deprecatedAlignAttribute = childNode.getAttribute(ALIGN_ATTRIBUTE);
|
|
1584
|
+
if (allowedTag) {
|
|
1585
|
+
const allowedAttributes = allowedTag.attributes || [];
|
|
1586
|
+
const allowedStyles = allowedTag.styles || [];
|
|
1587
|
+
|
|
1588
|
+
// Remove attributes that are not allowed
|
|
1589
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
1590
|
+
const attributeName = attributes[i].name;
|
|
1591
|
+
if (!allowedAttributes.includes(attributes[i].name)) {
|
|
1592
|
+
// Replace deprecated align attribute with text-align style
|
|
1593
|
+
if (attributeName === ALIGN_ATTRIBUTE) {
|
|
1594
|
+
if (deprecatedAlignAttribute !== 'left') {
|
|
1595
|
+
childNode.style.textAlign = deprecatedAlignAttribute;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
if (attributeName === STYLE_ATTRIBUTE && allowedStyles.length) {
|
|
1599
|
+
filterStyles(childNode, allowedStyles);
|
|
1600
|
+
} else {
|
|
1601
|
+
childNode.removeAttribute(attributes[i].name);
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
// If the tag is an alias, replace it with the standard tag
|
|
1607
|
+
// e.g: <b> tags will be replaced with <strong> tags
|
|
1608
|
+
if (allowedTag.alias) {
|
|
1609
|
+
replaceNode(childNode, allowedTag.alias, true);
|
|
1610
|
+
}
|
|
1611
|
+
} else {
|
|
1612
|
+
// Remove style nodes
|
|
1613
|
+
if (tag === 'style') {
|
|
1614
|
+
node.removeChild(childNode);
|
|
1615
|
+
|
|
1616
|
+
// And unwrap the other nodes
|
|
1617
|
+
} else {
|
|
1618
|
+
// Fix bad alignment handling on Firefox
|
|
1619
|
+
if (deprecatedAlignAttribute !== null) {
|
|
1620
|
+
if (childNode.parentNode && childNode.parentNode.tagName === 'LI') {
|
|
1621
|
+
childNode.parentNode.style.textAlign = deprecatedAlignAttribute;
|
|
1622
|
+
} else {
|
|
1623
|
+
for (const divChild of childNode.childNodes) {
|
|
1624
|
+
divChild.style.textAlign = deprecatedAlignAttribute;
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
childNode.replaceWith(...childNode.childNodes);
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
// Remove comment nodes
|
|
1633
|
+
} else if (childNode.nodeType === 8) {
|
|
1634
|
+
node.removeChild(childNode);
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/**
|
|
1640
|
+
* Remove empty nodes.
|
|
1641
|
+
* @param {object} node The parent element to filter recursively.
|
|
1642
|
+
* @param {array} allowedTags The list of allowed tags.
|
|
1643
|
+
*/
|
|
1644
|
+
function cleanContent(node, allowedTags) {
|
|
1645
|
+
const children = Array.from(node.childNodes);
|
|
1646
|
+
if (!children || !children.length) {
|
|
1647
|
+
return;
|
|
1648
|
+
}
|
|
1649
|
+
children.forEach(childNode => {
|
|
1650
|
+
// Remove empty element nodes
|
|
1651
|
+
if (childNode.nodeType === 1) {
|
|
1652
|
+
// Filter recursively (deeper nodes first)
|
|
1653
|
+
cleanContent(childNode, allowedTags);
|
|
1654
|
+
|
|
1655
|
+
// Check if the element can be empty
|
|
1656
|
+
const tag = childNode.tagName.toLowerCase();
|
|
1657
|
+
const allowedTag = allowedTags[tag];
|
|
1658
|
+
if (allowedTag && !allowedTag.isEmpty && trimText(childNode.innerHTML) === '') {
|
|
1659
|
+
node.removeChild(childNode);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
/**
|
|
1666
|
+
* Wrap the child text nodes in a paragraph (non-recursively).
|
|
1667
|
+
* @param {object} node The parent element of the text nodes.
|
|
1668
|
+
*/
|
|
1669
|
+
function wrapTextNodes(node) {
|
|
1670
|
+
const children = Array.from(node.childNodes);
|
|
1671
|
+
if (!children || !children.length) {
|
|
1672
|
+
return;
|
|
1673
|
+
}
|
|
1674
|
+
let appendToPrev = false;
|
|
1675
|
+
children.forEach(childNode => {
|
|
1676
|
+
if (childNode.nodeType !== 3 && blockElements.includes(childNode.tagName)) {
|
|
1677
|
+
appendToPrev = false;
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
|
|
1681
|
+
// Remove empty text node
|
|
1682
|
+
/*if (trimText(childNode.textContent) === '') {
|
|
1683
|
+
node.removeChild(childNode);
|
|
1684
|
+
// Wrap text node in a paragraph
|
|
1685
|
+
} else {*/
|
|
1686
|
+
if (appendToPrev) {
|
|
1687
|
+
const prev = childNode.previousElementSibling;
|
|
1688
|
+
if (prev) {
|
|
1689
|
+
prev.appendChild(childNode);
|
|
1690
|
+
}
|
|
1691
|
+
} else {
|
|
1692
|
+
const paragraph = createElement('p');
|
|
1693
|
+
childNode.replaceWith(paragraph);
|
|
1694
|
+
paragraph.appendChild(childNode);
|
|
1695
|
+
appendToPrev = true;
|
|
1696
|
+
}
|
|
1697
|
+
/*}*/
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* Trim whitespace from the start and end of a text.
|
|
1703
|
+
* @param {string} text The text to trim.
|
|
1704
|
+
* @return {string} The trimmed text.
|
|
1705
|
+
*/
|
|
1706
|
+
function trimText(text) {
|
|
1707
|
+
return text.replace(/^\s+|\s+$/g, '').trim();
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Fall back to the default paragraph if content is empty.
|
|
1712
|
+
* @param {string} content The prepared content.
|
|
1713
|
+
* @return {string} The content, or the default paragraph if it was empty.
|
|
1714
|
+
*/
|
|
1715
|
+
function withDefaultParagraph(content) {
|
|
1716
|
+
return content === '' ? DEFAULT_PARAGRAPH : content;
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
/**
|
|
1720
|
+
* Check whether prepared content amounts to an empty editable region.
|
|
1721
|
+
* @param {string} content The prepared content.
|
|
1722
|
+
* @return {boolean} Whether the content is empty.
|
|
1723
|
+
*/
|
|
1724
|
+
function isContentEmpty(content) {
|
|
1725
|
+
return content === '' || content === DEFAULT_PARAGRAPH;
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
// Next available instance id
|
|
1729
|
+
let nextId = 0;
|
|
1730
|
+
|
|
1731
|
+
/**
|
|
1732
|
+
* Init WYSIWYG editor instances.
|
|
1733
|
+
* @param {object} options Configuration options.
|
|
1734
|
+
*/
|
|
1735
|
+
function init(options) {
|
|
1736
|
+
translate = options.translate || translate;
|
|
1737
|
+
const tools = options.tools || settings.tools;
|
|
1738
|
+
const selector = options.el || settings.el;
|
|
1739
|
+
const targetEls = getTargetElements(selector);
|
|
1740
|
+
const toolbar = renderToolbar(tools);
|
|
1741
|
+
const allowedTags = enableTags(tools);
|
|
1742
|
+
const customTags = options.customTags || [];
|
|
1743
|
+
|
|
1744
|
+
// Add custom tags if any to the allowed tags list
|
|
1745
|
+
customTags.forEach(custom => {
|
|
1746
|
+
if (custom.tags) {
|
|
1747
|
+
const attributes = custom.attributes || [];
|
|
1748
|
+
const styles = custom.styles || [];
|
|
1749
|
+
const isEmpty = !!custom.isEmpty;
|
|
1750
|
+
custom.tags.forEach(tag => {
|
|
1751
|
+
allowedTags[tag] = {
|
|
1752
|
+
attributes,
|
|
1753
|
+
styles,
|
|
1754
|
+
isEmpty
|
|
1755
|
+
};
|
|
1756
|
+
});
|
|
1757
|
+
}
|
|
1758
|
+
});
|
|
1759
|
+
|
|
1760
|
+
// Append an editor instance to target elements
|
|
1761
|
+
targetEls.forEach(field => {
|
|
1762
|
+
const sibling = field.previousElementSibling;
|
|
1763
|
+
if (!sibling || !hasClass(sibling, 'wysi-wrapper')) {
|
|
1764
|
+
const instanceId = nextId++;
|
|
1765
|
+
|
|
1766
|
+
// Store the instance's options
|
|
1767
|
+
instances[instanceId] = options;
|
|
1768
|
+
|
|
1769
|
+
// Cache the list of allowed tags in the instance
|
|
1770
|
+
instances[instanceId].allowedTags = cloneObject(allowedTags);
|
|
1771
|
+
|
|
1772
|
+
// Wrapper
|
|
1773
|
+
const wrapper = createElement('div', {
|
|
1774
|
+
class: 'wysi-wrapper'
|
|
1775
|
+
});
|
|
1776
|
+
|
|
1777
|
+
// Editable region
|
|
1778
|
+
const editor = createElement('div', {
|
|
1779
|
+
class: 'wysi-editor',
|
|
1780
|
+
contenteditable: true,
|
|
1781
|
+
role: 'textbox',
|
|
1782
|
+
'aria-multiline': true,
|
|
1783
|
+
'aria-label': getTextAreaLabel(field),
|
|
1784
|
+
'data-wid': instanceId,
|
|
1785
|
+
_innerHTML: withDefaultParagraph(prepareContent(field.value, allowedTags))
|
|
1786
|
+
});
|
|
1787
|
+
|
|
1788
|
+
// Insert the editor instance in the document
|
|
1789
|
+
wrapper.appendChild(toolbar.cloneNode(true));
|
|
1790
|
+
wrapper.appendChild(editor);
|
|
1791
|
+
field.before(wrapper);
|
|
1792
|
+
|
|
1793
|
+
// The content of the editable region when it received focus
|
|
1794
|
+
let valueOnFocus;
|
|
1795
|
+
|
|
1796
|
+
// Store the current content when the instance receives focus
|
|
1797
|
+
addListener(wrapper, 'focusin', event => {
|
|
1798
|
+
if (!wrapper.contains(event.relatedTarget)) {
|
|
1799
|
+
valueOnFocus = field.value;
|
|
1800
|
+
}
|
|
1801
|
+
});
|
|
1802
|
+
|
|
1803
|
+
// Dispatch a change event when the instance loses focus and its content has changed.
|
|
1804
|
+
// Focus moves within the instance (e.g: toolbar buttons) are ignored
|
|
1805
|
+
addListener(wrapper, 'focusout', event => {
|
|
1806
|
+
if (!wrapper.contains(event.relatedTarget)) {
|
|
1807
|
+
if (field.value !== valueOnFocus) {
|
|
1808
|
+
dispatchEvent(field, 'change');
|
|
1809
|
+
}
|
|
1810
|
+
valueOnFocus = undefined;
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
|
|
1814
|
+
// Apply configuration
|
|
1815
|
+
configure(wrapper, options);
|
|
1816
|
+
|
|
1817
|
+
// Reconfigure instance
|
|
1818
|
+
} else {
|
|
1819
|
+
configure(sibling, options);
|
|
1820
|
+
}
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
/**
|
|
1825
|
+
* Configure a WYSIWYG editor instance.
|
|
1826
|
+
* @param {object} instance The editor instance to configure.
|
|
1827
|
+
* @param {object} options The configuration options.
|
|
1828
|
+
*/
|
|
1829
|
+
function configure(instance, options) {
|
|
1830
|
+
if (typeof options !== 'object') {
|
|
1831
|
+
return;
|
|
1832
|
+
}
|
|
1833
|
+
for (const key in options) {
|
|
1834
|
+
switch (key) {
|
|
1835
|
+
case 'darkMode':
|
|
1836
|
+
case 'autoGrow':
|
|
1837
|
+
case 'autoHide':
|
|
1838
|
+
instance.classList.toggle(`wysi-${key.toLowerCase()}`, !!options[key]);
|
|
1839
|
+
break;
|
|
1840
|
+
case 'height':
|
|
1841
|
+
const height = options.height;
|
|
1842
|
+
if (!isNaN(height)) {
|
|
1843
|
+
const editor = instance.lastChild;
|
|
1844
|
+
editor.style.minHeight = `${height}px`;
|
|
1845
|
+
editor.style.maxHeight = `${height}px`;
|
|
1846
|
+
}
|
|
1847
|
+
break;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
/**
|
|
1853
|
+
* Update the content of a WYSIWYG editor instance.
|
|
1854
|
+
* @param {object} textarea The textarea eleement.
|
|
1855
|
+
* @param {object} editor The editable region.
|
|
1856
|
+
* @param {string} instanceId The id of the instance.
|
|
1857
|
+
* @param {string} rawContent The new unfiltered content of the instance.
|
|
1858
|
+
* @param {boolean} setEditorContent Whether to update the content of the editable region.
|
|
1859
|
+
*/
|
|
1860
|
+
function updateContent(textarea, editor, instanceId, rawContent, setEditorContent) {
|
|
1861
|
+
const instance = instances[instanceId];
|
|
1862
|
+
const content = prepareContent(rawContent, instance.allowedTags);
|
|
1863
|
+
const onChange = instance.onChange;
|
|
1864
|
+
if (setEditorContent === true) {
|
|
1865
|
+
editor.innerHTML = withDefaultParagraph(content);
|
|
1866
|
+
}
|
|
1867
|
+
textarea.value = content;
|
|
1868
|
+
dispatchEvent(textarea, 'input');
|
|
1869
|
+
if (onChange) {
|
|
1870
|
+
onChange(content);
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
/**
|
|
1875
|
+
* Destroy a WYSIWYG editor instance.
|
|
1876
|
+
* @param {string} selector One or more selectors pointing to textarea fields.
|
|
1877
|
+
*/
|
|
1878
|
+
function destroy(selector) {
|
|
1879
|
+
const editorInstances = findEditorInstances(selector);
|
|
1880
|
+
for (const editorInstance of editorInstances) {
|
|
1881
|
+
const {
|
|
1882
|
+
instanceId,
|
|
1883
|
+
wrapper
|
|
1884
|
+
} = editorInstance;
|
|
1885
|
+
delete instances[instanceId];
|
|
1886
|
+
wrapper.remove();
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
/**
|
|
1891
|
+
* Set the content of a WYSIWYG editor instance programmatically.
|
|
1892
|
+
* @param {string} selector One or more selectors pointing to textarea fields.
|
|
1893
|
+
*/
|
|
1894
|
+
function setContent(selector, content) {
|
|
1895
|
+
const editorInstances = findEditorInstances(selector);
|
|
1896
|
+
for (const editorInstance of editorInstances) {
|
|
1897
|
+
const {
|
|
1898
|
+
textarea,
|
|
1899
|
+
editor,
|
|
1900
|
+
instanceId
|
|
1901
|
+
} = editorInstance;
|
|
1902
|
+
updateContent(textarea, editor, instanceId, content, true);
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
/**
|
|
1907
|
+
* Clean up content before pasting it in an editor.
|
|
1908
|
+
* @param {object} event The browser's paste event.
|
|
1909
|
+
*/
|
|
1910
|
+
function cleanPastedContent(event) {
|
|
1911
|
+
const {
|
|
1912
|
+
editor,
|
|
1913
|
+
nodes
|
|
1914
|
+
} = findInstance(event.target);
|
|
1915
|
+
const clipboardData = event.clipboardData;
|
|
1916
|
+
if (editor && handleEditorPaste(event, editor)) {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
if (editor && clipboardData.types.includes('text/html')) {
|
|
1920
|
+
const pasted = clipboardData.getData('text/html');
|
|
1921
|
+
const instanceId = getInstanceId(editor);
|
|
1922
|
+
const allowedTags = instances[instanceId].allowedTags;
|
|
1923
|
+
let content = prepareContent(pasted, allowedTags);
|
|
1924
|
+
|
|
1925
|
+
// Detect a heading tag in the current selection
|
|
1926
|
+
const splitHeadingTag = nodes.filter(n => headingElements.includes(n.tagName)).length > 0;
|
|
1927
|
+
|
|
1928
|
+
// Force split the heading tag if any.
|
|
1929
|
+
// This fixes a bug in Webkit/Blink browsers where the whole content is converted to a heading
|
|
1930
|
+
if (splitHeadingTag && !isFirefox) {
|
|
1931
|
+
const splitter = `<h1 class="${placeholderClass}"><br></h1><p class="${placeholderClass}"><br></p>`;
|
|
1932
|
+
content = splitter + content + splitter;
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
// Manually paste the cleaned content
|
|
1936
|
+
execCommand('insertHTML', content);
|
|
1937
|
+
if (splitHeadingTag && !isFirefox) {
|
|
1938
|
+
// Remove placeholder elements if any
|
|
1939
|
+
editor.querySelectorAll(`.${placeholderClass}`).forEach(fragment => {
|
|
1940
|
+
fragment.remove();
|
|
1941
|
+
});
|
|
1942
|
+
|
|
1943
|
+
// Unwrap nested heading elements to fix a bug in Webkit/Blink browsers
|
|
1944
|
+
editor.querySelectorAll(headingElements.join()).forEach(heading => {
|
|
1945
|
+
const firstChild = heading.firstElementChild;
|
|
1946
|
+
if (firstChild && blockElements.includes(firstChild.tagName)) {
|
|
1947
|
+
heading.replaceWith(...heading.childNodes);
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
// Prevent the default paste action
|
|
1953
|
+
event.preventDefault();
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
/**
|
|
1958
|
+
* Bootstrap the WYSIWYG editor.
|
|
1959
|
+
*/
|
|
1960
|
+
function bootstrap() {
|
|
1961
|
+
// Configure editable regions
|
|
1962
|
+
execCommand('styleWithCSS', false);
|
|
1963
|
+
execCommand('enableObjectResizing', false);
|
|
1964
|
+
execCommand('enableInlineTableEditing', false);
|
|
1965
|
+
execCommand('defaultParagraphSeparator', 'p');
|
|
1966
|
+
|
|
1967
|
+
// Update the textarea value when the editor's content changes
|
|
1968
|
+
addListener(document, 'input', '.wysi-editor', event => {
|
|
1969
|
+
const editor = event.target;
|
|
1970
|
+
const textarea = editor.parentNode.nextElementSibling;
|
|
1971
|
+
const instanceId = getInstanceId(editor);
|
|
1972
|
+
const content = editor.innerHTML;
|
|
1973
|
+
updateContent(textarea, editor, instanceId, content);
|
|
1974
|
+
});
|
|
1975
|
+
|
|
1976
|
+
// Block deleting the default empty paragraph away (nothing to delete)
|
|
1977
|
+
addListener(document, 'keydown', '.wysi-editor', event => {
|
|
1978
|
+
if (event.isComposing || event.key !== 'Backspace' && event.key !== 'Delete') {
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
const editor = event.target;
|
|
1982
|
+
const instanceId = getInstanceId(editor);
|
|
1983
|
+
const content = prepareContent(editor.innerHTML, instances[instanceId].allowedTags);
|
|
1984
|
+
if (isContentEmpty(content) && editor.innerHTML === withDefaultParagraph(content)) {
|
|
1985
|
+
event.preventDefault();
|
|
1986
|
+
}
|
|
1987
|
+
});
|
|
1988
|
+
|
|
1989
|
+
// Handle code blocks, quotes and tables
|
|
1990
|
+
addListener(document, 'keydown', '.wysi-editor', event => handleEditorKeydown(event, event.target));
|
|
1991
|
+
addListener(document, 'beforeinput', '.wysi-editor', event => handleEditorBeforeInput(event, event.target));
|
|
1992
|
+
|
|
1993
|
+
// Table menu
|
|
1994
|
+
addListener(document, 'click', handleTableMenuClick);
|
|
1995
|
+
addListener(document, 'mousedown', handleTableMenuInput);
|
|
1996
|
+
addListener(document, 'keydown', handleTableMenuInput);
|
|
1997
|
+
|
|
1998
|
+
// Clean up pasted content
|
|
1999
|
+
addListener(document, 'paste', cleanPastedContent);
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
// Expose Wysi to the global scope
|
|
2003
|
+
window.Wysi = (() => {
|
|
2004
|
+
const methods = {
|
|
2005
|
+
destroy,
|
|
2006
|
+
setContent
|
|
2007
|
+
};
|
|
2008
|
+
function Wysi(options) {
|
|
2009
|
+
DOMReady(() => {
|
|
2010
|
+
init(options || {});
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
for (const key in methods) {
|
|
2014
|
+
Wysi[key] = (...args) => {
|
|
2015
|
+
DOMReady(methods[key], args);
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
return Wysi;
|
|
2019
|
+
})();
|
|
2020
|
+
|
|
2021
|
+
// Bootstrap Wysi when the DOM is ready
|
|
2022
|
+
DOMReady(bootstrap);
|
|
2023
|
+
|
|
2024
|
+
})(window, document);
|