Repository navigation
Expand file tree
/
Copy pathui.js
More file actions
447 lines (316 loc) · 12.5 KB
/
Copy pathui.js
File metadata and controls
447 lines (316 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Codex Editor UI module
*
* @author Codex Team
* @version 1.1
*/
let editor = codex.editor;
module.exports = (function (ui) {
/**
* Basic editor classnames
*/
ui.className = {
/**
* @const {string} BLOCK_CLASSNAME - redactor blocks name
*/
BLOCK_CLASSNAME : 'ce-block',
/**
* @const {String} wrapper for plugins content
*/
BLOCK_CONTENT : 'ce-block__content',
/**
* @const {String} BLOCK_STRETCHED - makes block stretched
*/
BLOCK_STRETCHED : 'ce-block--stretched',
/**
* @const {String} BLOCK_HIGHLIGHTED - adds background
*/
BLOCK_HIGHLIGHTED : 'ce-block--focused',
/**
* @const {String} - highlights covered blocks
*/
BLOCK_IN_FEED_MODE : 'ce-block--feed-mode',
/**
* @const {String} - for all default settings
*/
SETTINGS_ITEM : 'ce-settings__item'
};
/**
* @protected
*
* Making main interface
*/
ui.make = function () {
var wrapper,
toolbar,
toolbarContent,
redactor,
commentsSidebar,
notifications,
blockButtons,
blockSettings,
showSettingsButton,
showTrashButton,
showCommentButton,
toolbox,
plusButton;
/** Make editor wrapper */
wrapper = editor.draw.wrapper();
/** Append editor wrapper after initial textarea */
editor.core.insertAfter(editor.nodes.textarea, wrapper);
/** Append block with notifications to the document */
notifications = editor.draw.alertsHolder();
editor.nodes.notifications = document.body.appendChild(notifications);
/** Make toolbar and content-editable redactor */
toolbar = editor.draw.toolbar();
toolbarContent = editor.draw.toolbarContent();
plusButton = editor.draw.plusButton();
showSettingsButton = editor.draw.settingsButton();
showCommentButton = editor.draw.commentButton();
showTrashButton = editor.toolbar.settings.makeRemoveBlockButton();
blockSettings = editor.draw.blockSettings();
blockButtons = editor.draw.blockButtons();
toolbox = editor.draw.toolbox();
redactor = editor.draw.redactor();
commentsSidebar = editor.draw.commentsSidebar();
/** settings */
var defaultSettings = editor.draw.defaultSettings(),
pluginSettings = editor.draw.pluginsSettings();
/** Add default and plugins settings */
blockSettings.appendChild(pluginSettings);
blockSettings.appendChild(defaultSettings);
/** Make blocks buttons
* This block contains settings button and remove block button
*/
blockButtons.appendChild(showSettingsButton);
blockButtons.appendChild(showTrashButton);
blockButtons.appendChild(showCommentButton);
blockButtons.appendChild(blockSettings);
/** Append plus button */
toolbarContent.appendChild(plusButton);
/** Appending toolbar tools */
toolbarContent.appendChild(toolbox);
/** Appending first-level block buttons */
toolbar.appendChild(blockButtons);
/** Append toolbarContent to toolbar */
toolbar.appendChild(toolbarContent);
wrapper.appendChild(toolbar);
wrapper.appendChild(commentsSidebar);
wrapper.appendChild(redactor);
/** Save created ui-elements to static nodes state */
editor.nodes.wrapper = wrapper;
editor.nodes.toolbar = toolbar;
editor.nodes.plusButton = plusButton;
editor.nodes.toolbox = toolbox;
editor.nodes.blockSettings = blockSettings;
editor.nodes.pluginSettings = pluginSettings;
editor.nodes.defaultSettings = defaultSettings;
editor.nodes.showSettingsButton = showSettingsButton;
editor.nodes.showTrashButton = showTrashButton;
editor.nodes.showCommentButton = showCommentButton;
editor.nodes.commentsSidebar = commentsSidebar;
editor.nodes.redactor = redactor;
/** Make container for inline toolbar */
editor.ui.makeInlineToolbar();
/** fill in default settings */
editor.toolbar.settings.addDefaultSettings();
};
ui.makeInlineToolbar = function () {
var container = editor.draw.inlineToolbar();
/** Append to redactor new inline block */
editor.nodes.inlineToolbar.wrapper = container;
/** Draw toolbar buttons */
editor.nodes.inlineToolbar.buttons = editor.draw.inlineToolbarButtons();
/** Buttons action or settings */
editor.nodes.inlineToolbar.actions = editor.draw.inlineToolbarActions();
/** Append to inline toolbar buttons as part of it */
editor.nodes.inlineToolbar.wrapper.appendChild(editor.nodes.inlineToolbar.buttons);
editor.nodes.inlineToolbar.wrapper.appendChild(editor.nodes.inlineToolbar.actions);
editor.nodes.wrapper.appendChild(editor.nodes.inlineToolbar.wrapper);
};
/**
* @private
* Append tools passed in editor.tools
*/
ui.addTools = function () {
var tool,
toolName,
toolButton;
for ( toolName in editor.settings.tools ) {
tool = editor.settings.tools[toolName];
editor.tools[toolName] = tool;
if (!tool.iconClassname) {
editor.core.log('Toolbar icon classname missed. Tool %o skipped', 'warn', toolName);
continue;
}
if (typeof tool.render != 'function') {
editor.core.log('render method missed. Tool %o skipped', 'warn', toolName);
continue;
}
if (!tool.displayInToolbox) {
continue;
} else {
/** if tools is for toolbox */
toolButton = editor.draw.toolbarButton(toolName, tool.iconClassname);
editor.nodes.toolbox.appendChild(toolButton);
editor.nodes.toolbarButtons[toolName] = toolButton;
}
}
/**
* Add inline toolbar tools
*/
editor.ui.addInlineToolbarTools();
};
ui.addInlineToolbarTools = function () {
var tools = {
bold: {
icon : 'ce-icon-bold',
command : 'bold'
},
italic: {
icon : 'ce-icon-italic',
command : 'italic'
},
underline: {
icon : 'ce-icon-underline',
command : 'underline'
},
link: {
icon : 'ce-icon-link',
command : 'createLink'
}
};
var toolButton,
tool;
for(var name in tools) {
tool = tools[name];
toolButton = editor.draw.toolbarButtonInline(name, tool.icon);
editor.nodes.inlineToolbar.buttons.appendChild(toolButton);
/**
* Add callbacks to this buttons
*/
editor.ui.setInlineToolbarButtonBehaviour(toolButton, tool.command);
}
};
/**
* @private
* Bind editor UI events
*/
ui.bindEvents = function () {
editor.core.log('ui.bindEvents fired', 'info');
// window.addEventListener('error', function (errorMsg, url, lineNumber) {
// editor.notifications.errorThrown(errorMsg, event);
// }, false );
/** All keydowns on Document */
document.addEventListener('keydown', editor.callback.globalKeydown, false );
/** All keydowns on Redactor zone */
editor.nodes.redactor.addEventListener('keydown', editor.callback.redactorKeyDown, false);
/** All keydowns on Document */
document.addEventListener('keyup', editor.callback.globalKeyup, false );
/**
* Mouse click to radactor
*/
editor.nodes.redactor.addEventListener('click', editor.callback.redactorClicked, false );
/**
* Clicks to the Plus button
*/
editor.nodes.plusButton.addEventListener('click', editor.callback.plusButtonClicked, false);
/**
* Clicks to SETTINGS button in toolbar
*/
editor.nodes.showSettingsButton.addEventListener('click', editor.callback.showSettingsButtonClicked, false );
/**
* Clicks to COMMENT button in toolbar
*/
editor.nodes.showCommentButton.addEventListener('click', editor.callback.showCommentButtonClicked, false );
/**
* @deprecated ( but now in use for syncronization );
* Any redactor changes: keyboard input, mouse cut/paste, drag-n-drop text
*/
// editor.nodes.redactor.addEventListener('input', editor.callback.redactorInputEvent, false );
/** Bind click listeners on toolbar buttons */
for (var button in editor.nodes.toolbarButtons) {
editor.nodes.toolbarButtons[button].addEventListener('click', editor.callback.toolbarButtonClicked, false);
}
};
/**
* Initialize plugins before using
* Ex. Load scripts or call some internal methods
* @return Promise
*/
ui.preparePlugins = function () {
return new Promise(function (resolve, reject) {
let pluginName,
plugin;
for ( pluginName in editor.tools ) {
plugin = editor.tools[pluginName];
if (typeof plugin.prepare != 'function') {
continue;
}
plugin.prepare(plugin.config || {}).then(function () {
resolve();
}).catch(function (error) {
reject(error);
});
}
});
};
ui.addBlockHandlers = function (block) {
if (!block) return;
/**
* Block keydowns
*/
block.addEventListener('keydown', editor.callback.blockKeydown, false);
/**
* Pasting content from another source
* We have two type of sanitization
* First - uses deep-first search algorithm to get sub nodes,
* sanitizes whole Block_content and replaces cleared nodes
* This method is deprecated
* Method is used in editor.callback.blockPaste(event)
*
* Secont - uses Mutation observer.
* Observer "observe" DOM changes and send changings to callback.
* Callback gets changed node, not whole Block_content.
* Inserted or changed node, which we've gotten have been cleared and replaced with diry node
*
* Method is used in editor.callback.blockPasteViaSanitize(event)
*
* @uses html-janitor
* @example editor.callback.blockPasteViaSanitize(event), the second method.
*
*/
block.addEventListener('paste', editor.callback.blockPasteCallback, false);
block.addEventListener('mouseup', editor.toolbar.inline.show, false);
};
/** getting all contenteditable elements */
ui.saveInputs = function () {
var redactor = editor.nodes.redactor;
/** Save all inputs in global variable state */
editor.state.inputs = redactor.querySelectorAll('[contenteditable], input');
};
/**
* Adds first initial block on empty redactor
*/
ui.addInitialBlock = function () {
var initialBlockType = editor.settings.initialBlockPlugin,
initialBlock;
if ( !editor.tools[initialBlockType] ) {
editor.core.log('Plugin %o was not implemented and can\'t be used as initial block', 'warn', initialBlockType);
return;
}
initialBlock = editor.tools[initialBlockType].render();
initialBlock.setAttribute('data-placeholder', 'Расскажите свою историю...');
editor.content.insertBlock({
type : initialBlockType,
block : initialBlock
});
editor.content.workingNodeChanged(initialBlock);
};
ui.setInlineToolbarButtonBehaviour = function (button, type) {
button.addEventListener('mousedown', function (event) {
editor.toolbar.inline.toolClicked(event, type);
}, false);
};
return ui;
})({});