-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathindex.js
477 lines (425 loc) · 9.5 KB
/
index.js
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* Build styles
*/
import './index.css';
import { IconH1, IconH2, IconH3, IconH4, IconH5, IconH6, IconHeading } from '@codexteam/icons';
/**
* @typedef {object} HeaderData
* @description Tool's input and output data format
* @property {string} text — Header's content
* @property {number} level - Header's level from 1 to 6
*/
/**
* @typedef {object} HeaderConfig
* @description Tool's config from Editor
* @property {string} placeholder — Block's placeholder
* @property {number[]} levels — Heading levels
* @property {number} defaultLevel — default level
*/
/**
* Header block for the Editor.js.
*
* @author CodeX (team@ifmo.su)
* @copyright CodeX 2018
* @license MIT
* @version 2.0.0
*/
export default class Header {
/**
* Render plugin`s main Element and fill it with saved data
*
* @param {{data: HeaderData, config: HeaderConfig, api: object}}
* data — previously saved data
* config - user config for Tool
* api - Editor.js API
* readOnly - read only mode flag
*/
constructor({ data, config, api, readOnly }) {
this.api = api;
this.readOnly = readOnly;
/**
* Styles
*
* @type {object}
*/
this._CSS = {
block: this.api.styles.block,
wrapper: 'ce-header',
};
/**
* Tool's settings passed from Editor
*
* @type {HeaderConfig}
* @private
*/
this._settings = config;
/**
* Block's data
*
* @type {HeaderData}
* @private
*/
this._data = this.normalizeData(data);
/**
* Main Block wrapper
*
* @type {HTMLElement}
* @private
*/
this._element = this.getTag();
}
/**
* Normalize input data
*
* @param {HeaderData} data - saved data to process
*
* @returns {HeaderData}
* @private
*/
normalizeData(data) {
const newData = {};
if (typeof data !== 'object') {
data = {};
}
newData.text = data.text || '';
newData.level = parseInt(data.level) || this.defaultLevel.number;
return newData;
}
/**
* Return Tool's view
*
* @returns {HTMLHeadingElement}
* @public
*/
render() {
return this._element;
}
/**
* Returns header block tunes config
*
* @returns {Array}
*/
renderSettings() {
return this.levels.map(level => ({
icon: level.svg,
name: level.tag,
label: this.api.i18n.t(`Heading ${level.number}`),
onActivate: () => this.setLevel(level.number),
closeOnActivate: true,
isActive: this.currentLevel.number === level.number,
}));
}
/**
* Callback for Block's settings buttons
*
* @param {number} level - level to set
*/
setLevel(level) {
this.data = {
level: level,
text: this.data.text,
};
}
/**
* Method that specified how to merge two Text blocks.
* Called by Editor.js by backspace at the beginning of the Block
*
* @param {HeaderData} data - saved data to merger with current block
* @public
*/
merge(data) {
const newData = {
text: this.data.text + data.text,
level: this.data.level,
};
this.data = newData;
}
/**
* Validate Text block data:
* - check for emptiness
*
* @param {HeaderData} blockData — data received after saving
* @returns {boolean} false if saved data is not correct, otherwise true
* @public
*/
validate(blockData) {
return blockData.text.trim() !== '';
}
/**
* Extract Tool's data from the view
*
* @param {HTMLHeadingElement} toolsContent - Text tools rendered view
* @returns {HeaderData} - saved data
* @public
*/
save(toolsContent) {
return {
text: toolsContent.innerHTML,
level: this.currentLevel.number,
};
}
/**
* Allow Header to be converted to/from other blocks
*/
static get conversionConfig() {
return {
export: 'text', // use 'text' property for other blocks
import: 'text', // fill 'text' property from other block's export string
};
}
/**
* Sanitizer Rules
*/
static get sanitize() {
return {
level: false,
text: {},
};
}
/**
* Returns true to notify core that read-only is supported
*
* @returns {boolean}
*/
static get isReadOnlySupported() {
return true;
}
/**
* Get current Tools`s data
*
* @returns {HeaderData} Current data
* @private
*/
get data() {
this._data.text = this._element.innerHTML;
this._data.level = this.currentLevel.number;
return this._data;
}
/**
* Store data in plugin:
* - at the this._data property
* - at the HTML
*
* @param {HeaderData} data — data to set
* @private
*/
set data(data) {
this._data = this.normalizeData(data);
/**
* If level is set and block in DOM
* then replace it to a new block
*/
if (data.level !== undefined && this._element.parentNode) {
/**
* Create a new tag
*
* @type {HTMLHeadingElement}
*/
const newHeader = this.getTag();
/**
* Save Block's content
*/
newHeader.innerHTML = this._element.innerHTML;
/**
* Replace blocks
*/
this._element.parentNode.replaceChild(newHeader, this._element);
/**
* Save new block to private variable
*
* @type {HTMLHeadingElement}
* @private
*/
this._element = newHeader;
}
/**
* If data.text was passed then update block's content
*/
if (data.text !== undefined) {
this._element.innerHTML = this._data.text || '';
}
}
/**
* Get tag for target level
* By default returns second-leveled header
*
* @returns {HTMLElement}
*/
getTag() {
/**
* Create element for current Block's level
*/
const tag = document.createElement(this.currentLevel.tag);
/**
* Add text to block
*/
tag.innerHTML = this._data.text || '';
/**
* Add styles class
*/
tag.classList.add(this._CSS.wrapper);
/**
* Make tag editable
*/
tag.contentEditable = this.readOnly ? 'false' : 'true';
/**
* Add Placeholder
*/
tag.dataset.placeholder = this.api.i18n.t(this._settings.placeholder || '');
return tag;
}
/**
* Get current level
*
* @returns {level}
*/
get currentLevel() {
let level = this.levels.find(levelItem => levelItem.number === this._data.level);
if (!level) {
level = this.defaultLevel;
}
return level;
}
/**
* Return default level
*
* @returns {level}
*/
get defaultLevel() {
/**
* User can specify own default level value
*/
if (this._settings.defaultLevel) {
const userSpecified = this.levels.find(levelItem => {
return levelItem.number === this._settings.defaultLevel;
});
if (userSpecified) {
return userSpecified;
} else {
console.warn('(ง\'̀-\'́)ง Heading Tool: the default level specified was not found in available levels');
}
}
/**
* With no additional options, there will be H2 by default
*
* @type {level}
*/
return this.levels[1];
}
/**
* @typedef {object} level
* @property {number} number - level number
* @property {string} tag - tag corresponds with level number
* @property {string} svg - icon
*/
/**
* Available header levels
*
* @returns {level[]}
*/
get levels() {
const availableLevels = [
{
number: 1,
tag: 'H1',
svg: IconH1,
},
{
number: 2,
tag: 'H2',
svg: IconH2,
},
{
number: 3,
tag: 'H3',
svg: IconH3,
},
{
number: 4,
tag: 'H4',
svg: IconH4,
},
{
number: 5,
tag: 'H5',
svg: IconH5,
},
{
number: 6,
tag: 'H6',
svg: IconH6,
},
];
return this._settings.levels ? availableLevels.filter(
l => this._settings.levels.includes(l.number)
) : availableLevels;
}
/**
* Handle H1-H6 tags on paste to substitute it with header Tool
*
* @param {PasteEvent} event - event with pasted content
*/
onPaste(event) {
const content = event.detail.data;
/**
* Define default level value
*
* @type {number}
*/
let level = this.defaultLevel.number;
switch (content.tagName) {
case 'H1':
level = 1;
break;
case 'H2':
level = 2;
break;
case 'H3':
level = 3;
break;
case 'H4':
level = 4;
break;
case 'H5':
level = 5;
break;
case 'H6':
level = 6;
break;
}
if (this._settings.levels) {
// Fallback to nearest level when specified not available
level = this._settings.levels.reduce((prevLevel, currLevel) => {
return Math.abs(currLevel - level) < Math.abs(prevLevel - level) ? currLevel : prevLevel;
});
}
this.data = {
level,
text: content.innerHTML,
};
}
/**
* Used by Editor.js paste handling API.
* Provides configuration to handle H1-H6 tags.
*
* @returns {{handler: (function(HTMLElement): {text: string}), tags: string[]}}
*/
static get pasteConfig() {
return {
tags: ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'],
};
}
/**
* Get Tool toolbox settings
* icon - Tool icon's SVG
* title - title to show in toolbox
*
* @returns {{icon: string, title: string}}
*/
static get toolbox() {
return {
icon: IconHeading,
title: 'Heading',
};
}
}