-
Notifications
You must be signed in to change notification settings - Fork 4k
/
to-tree.js
368 lines (320 loc) · 8.86 KB
/
to-tree.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
/**
* Internal dependencies
*/
import { getActiveFormats } from './get-active-formats';
import { getFormatType } from './get-format-type';
import {
LINE_SEPARATOR,
OBJECT_REPLACEMENT_CHARACTER,
ZWNBSP,
} from './special-characters';
function restoreOnAttributes( attributes, isEditableTree ) {
if ( isEditableTree ) {
return attributes;
}
const newAttributes = {};
for ( const key in attributes ) {
let newKey = key;
if ( key.startsWith( 'data-disable-rich-text-' ) ) {
newKey = key.slice( 'data-disable-rich-text-'.length );
}
newAttributes[ newKey ] = attributes[ key ];
}
return newAttributes;
}
/**
* Converts a format object to information that can be used to create an element
* from (type, attributes and object).
*
* @param {Object} $1 Named parameters.
* @param {string} $1.type The format type.
* @param {string} $1.tagName The tag name.
* @param {Object} $1.attributes The format attributes.
* @param {Object} $1.unregisteredAttributes The unregistered format
* attributes.
* @param {boolean} $1.object Whether or not it is an object
* format.
* @param {boolean} $1.boundaryClass Whether or not to apply a boundary
* class.
* @param {boolean} $1.isEditableTree
*
* @return {Object} Information to be used for element creation.
*/
function fromFormat( {
type,
tagName,
attributes,
unregisteredAttributes,
object,
boundaryClass,
isEditableTree,
} ) {
const formatType = getFormatType( type );
let elementAttributes = {};
if ( boundaryClass ) {
elementAttributes[ 'data-rich-text-format-boundary' ] = 'true';
}
if ( ! formatType ) {
if ( attributes ) {
elementAttributes = { ...attributes, ...elementAttributes };
}
return {
type,
attributes: restoreOnAttributes(
elementAttributes,
isEditableTree
),
object,
};
}
elementAttributes = { ...unregisteredAttributes, ...elementAttributes };
for ( const name in attributes ) {
const key = formatType.attributes
? formatType.attributes[ name ]
: false;
if ( key ) {
elementAttributes[ key ] = attributes[ name ];
} else {
elementAttributes[ name ] = attributes[ name ];
}
}
if ( formatType.className ) {
if ( elementAttributes.class ) {
elementAttributes.class = `${ formatType.className } ${ elementAttributes.class }`;
} else {
elementAttributes.class = formatType.className;
}
}
return {
type: formatType.tagName === '*' ? tagName : formatType.tagName,
object: formatType.object,
attributes: restoreOnAttributes( elementAttributes, isEditableTree ),
};
}
/**
* Checks if both arrays of formats up until a certain index are equal.
*
* @param {Array} a Array of formats to compare.
* @param {Array} b Array of formats to compare.
* @param {number} index Index to check until.
*/
function isEqualUntil( a, b, index ) {
do {
if ( a[ index ] !== b[ index ] ) {
return false;
}
} while ( index-- );
return true;
}
export function toTree( {
value,
multilineTag,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText,
onStartIndex,
onEndIndex,
isEditableTree,
placeholder,
} ) {
const { formats, replacements, text, start, end } = value;
const formatsLength = formats.length + 1;
const tree = createEmpty();
const multilineFormat = { type: multilineTag };
const activeFormats = getActiveFormats( value );
const deepestActiveFormat = activeFormats[ activeFormats.length - 1 ];
let lastSeparatorFormats;
let lastCharacterFormats;
let lastCharacter;
// If we're building a multiline tree, start off with a multiline element.
if ( multilineTag ) {
append( append( tree, { type: multilineTag } ), '' );
lastCharacterFormats = lastSeparatorFormats = [ multilineFormat ];
} else {
append( tree, '' );
}
for ( let i = 0; i < formatsLength; i++ ) {
const character = text.charAt( i );
const shouldInsertPadding =
isEditableTree &&
// Pad the line if the line is empty.
( ! lastCharacter ||
lastCharacter === LINE_SEPARATOR ||
// Pad the line if the previous character is a line break, otherwise
// the line break won't be visible.
lastCharacter === '\n' );
let characterFormats = formats[ i ];
// Set multiline tags in queue for building the tree.
if ( multilineTag ) {
if ( character === LINE_SEPARATOR ) {
characterFormats = lastSeparatorFormats = (
replacements[ i ] || []
).reduce(
( accumulator, format ) => {
accumulator.push( format, multilineFormat );
return accumulator;
},
[ multilineFormat ]
);
} else {
characterFormats = [
...lastSeparatorFormats,
...( characterFormats || [] ),
];
}
}
let pointer = getLastChild( tree );
if ( shouldInsertPadding && character === LINE_SEPARATOR ) {
let node = pointer;
while ( ! isText( node ) ) {
node = getLastChild( node );
}
append( getParent( node ), ZWNBSP );
}
// Set selection for the start of line.
if ( lastCharacter === LINE_SEPARATOR ) {
let node = pointer;
while ( ! isText( node ) ) {
node = getLastChild( node );
}
if ( onStartIndex && start === i ) {
onStartIndex( tree, node );
}
if ( onEndIndex && end === i ) {
onEndIndex( tree, node );
}
}
if ( characterFormats ) {
characterFormats.forEach( ( format, formatIndex ) => {
if (
pointer &&
lastCharacterFormats &&
// Reuse the last element if all formats remain the same.
isEqualUntil(
characterFormats,
lastCharacterFormats,
formatIndex
) &&
// Do not reuse the last element if the character is a
// line separator.
( character !== LINE_SEPARATOR ||
characterFormats.length - 1 !== formatIndex )
) {
pointer = getLastChild( pointer );
return;
}
const { type, tagName, attributes, unregisteredAttributes } =
format;
const boundaryClass =
isEditableTree &&
character !== LINE_SEPARATOR &&
format === deepestActiveFormat;
const parent = getParent( pointer );
const newNode = append(
parent,
fromFormat( {
type,
tagName,
attributes,
unregisteredAttributes,
boundaryClass,
isEditableTree,
} )
);
if ( isText( pointer ) && getText( pointer ).length === 0 ) {
remove( pointer );
}
pointer = append( newNode, '' );
} );
}
// No need for further processing if the character is a line separator.
if ( character === LINE_SEPARATOR ) {
lastCharacterFormats = characterFormats;
lastCharacter = character;
continue;
}
// If there is selection at 0, handle it before characters are inserted.
if ( i === 0 ) {
if ( onStartIndex && start === 0 ) {
onStartIndex( tree, pointer );
}
if ( onEndIndex && end === 0 ) {
onEndIndex( tree, pointer );
}
}
if ( character === OBJECT_REPLACEMENT_CHARACTER ) {
if ( ! isEditableTree && replacements[ i ]?.type === 'script' ) {
pointer = append(
getParent( pointer ),
fromFormat( {
type: 'script',
isEditableTree,
} )
);
append( pointer, {
html: decodeURIComponent(
replacements[ i ].attributes[ 'data-rich-text-script' ]
),
} );
} else {
pointer = append(
getParent( pointer ),
fromFormat( {
...replacements[ i ],
object: true,
isEditableTree,
} )
);
}
// Ensure pointer is text node.
pointer = append( getParent( pointer ), '' );
} else if ( ! preserveWhiteSpace && character === '\n' ) {
pointer = append( getParent( pointer ), {
type: 'br',
attributes: isEditableTree
? {
'data-rich-text-line-break': 'true',
}
: undefined,
object: true,
} );
// Ensure pointer is text node.
pointer = append( getParent( pointer ), '' );
} else if ( ! isText( pointer ) ) {
pointer = append( getParent( pointer ), character );
} else {
appendText( pointer, character );
}
if ( onStartIndex && start === i + 1 ) {
onStartIndex( tree, pointer );
}
if ( onEndIndex && end === i + 1 ) {
onEndIndex( tree, pointer );
}
if ( shouldInsertPadding && i === text.length ) {
append( getParent( pointer ), ZWNBSP );
if ( placeholder && text.length === 0 ) {
append( getParent( pointer ), {
type: 'span',
attributes: {
'data-rich-text-placeholder': placeholder,
// Necessary to prevent the placeholder from catching
// selection. The placeholder is also not editable after
// all.
contenteditable: 'false',
style: 'pointer-events:none;user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;',
},
} );
}
}
lastCharacterFormats = characterFormats;
lastCharacter = character;
}
return tree;
}