-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathfiller.ts
163 lines (147 loc) · 7.02 KB
/
filler.ts
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
/**
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import { keyCodes, type KeystrokeInfo } from '@ckeditor/ckeditor5-utils/src/keyboard';
import isText from '@ckeditor/ckeditor5-utils/src/dom/istext';
import type View from './view';
import type DomEventData from './observer/domeventdata';
/**
* Set of utilities related to handling block and inline fillers.
*
* Browsers do not allow to put caret in elements which does not have height. Because of it, we need to fill all
* empty elements which should be selectable with elements or characters called "fillers". Unfortunately there is no one
* universal filler, this is why two types are uses:
*
* * Block filler is an element which fill block elements, like `<p>`. CKEditor uses `<br>` as a block filler during the editing,
* as browsers do natively. So instead of an empty `<p>` there will be `<p><br></p>`. The advantage of block filler is that
* it is transparent for the selection, so when the caret is before the `<br>` and user presses right arrow he will be
* moved to the next paragraph, not after the `<br>`. The disadvantage is that it breaks a block, so it can not be used
* in the middle of a line of text. The {@link module:engine/view/filler~BR_FILLER `<br>` filler} can be replaced with any other
* character in the data output, for instance {@link module:engine/view/filler~NBSP_FILLER non-breaking space} or
* {@link module:engine/view/filler~MARKED_NBSP_FILLER marked non-breaking space}.
*
* * Inline filler is a filler which does not break a line of text, so it can be used inside the text, for instance in the empty
* `<b>` surrendered by text: `foo<b></b>bar`, if we want to put the caret there. CKEditor uses a sequence of the zero-width
* spaces as an {@link module:engine/view/filler~INLINE_FILLER inline filler} having the predetermined
* {@link module:engine/view/filler~INLINE_FILLER_LENGTH length}. A sequence is used, instead of a single character to
* avoid treating random zero-width spaces as the inline filler. Disadvantage of the inline filler is that it is not
* transparent for the selection. The arrow key moves the caret between zero-width spaces characters, so the additional
* code is needed to handle the caret.
*
* Both inline and block fillers are handled by the {@link module:engine/view/renderer~Renderer renderer} and are not present in the
* view.
*
* @module engine/view/filler
*/
/**
* Non-breaking space filler creator. This function creates the ` ` text node.
* It defines how the filler is created.
*
* @see module:engine/view/filler~MARKED_NBSP_FILLER
* @see module:engine/view/filler~BR_FILLER
* @function
*/
export const NBSP_FILLER = ( domDocument: Document ): Text => domDocument.createTextNode( '\u00A0' );
/**
* Marked non-breaking space filler creator. This function creates the `<span data-cke-filler="true"> </span>` element.
* It defines how the filler is created.
*
* @see module:engine/view/filler~NBSP_FILLER
* @see module:engine/view/filler~BR_FILLER
* @function
*/
export const MARKED_NBSP_FILLER = ( domDocument: Document ): HTMLSpanElement => {
const span = domDocument.createElement( 'span' );
span.dataset.ckeFiller = 'true';
span.innerText = '\u00A0';
return span;
};
/**
* `<br>` filler creator. This function creates the `<br data-cke-filler="true">` element.
* It defines how the filler is created.
*
* @see module:engine/view/filler~NBSP_FILLER
* @see module:engine/view/filler~MARKED_NBSP_FILLER
* @function
*/
export const BR_FILLER = ( domDocument: Document ): HTMLBRElement => {
const fillerBr = domDocument.createElement( 'br' );
fillerBr.dataset.ckeFiller = 'true';
return fillerBr;
};
/**
* Length of the {@link module:engine/view/filler~INLINE_FILLER INLINE_FILLER}.
*/
export const INLINE_FILLER_LENGTH = 7;
/**
* Inline filler which is a sequence of the word joiners.
*
* @type {String}
*/
export const INLINE_FILLER = '\u2060'.repeat( INLINE_FILLER_LENGTH );
/**
* Checks if the node is a text node which starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
*
* startsWithFiller( document.createTextNode( INLINE_FILLER ) ); // true
* startsWithFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // true
* startsWithFiller( document.createTextNode( 'foo' ) ); // false
* startsWithFiller( document.createElement( 'p' ) ); // false
*
* @param {Node} domNode DOM node.
* @returns {Boolean} True if the text node starts with the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
*/
export function startsWithFiller( domNode: Node ): boolean {
return isText( domNode ) && ( domNode.data.substr( 0, INLINE_FILLER_LENGTH ) === INLINE_FILLER );
}
/**
* Checks if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
*
* isInlineFiller( document.createTextNode( INLINE_FILLER ) ); // true
* isInlineFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ); // false
*
* @param {Text} domText DOM text node.
* @returns {Boolean} True if the text node contains only the {@link module:engine/view/filler~INLINE_FILLER inline filler}.
*/
export function isInlineFiller( domText: Text ): boolean {
return domText.data.length == INLINE_FILLER_LENGTH && startsWithFiller( domText );
}
/**
* Get string data from the text node, removing an {@link module:engine/view/filler~INLINE_FILLER inline filler} from it,
* if text node contains it.
*
* getDataWithoutFiller( document.createTextNode( INLINE_FILLER + 'foo' ) ) == 'foo' // true
* getDataWithoutFiller( document.createTextNode( 'foo' ) ) == 'foo' // true
*
* @param {Text} domText DOM text node, possible with inline filler.
* @returns {String} Data without filler.
*/
export function getDataWithoutFiller( domText: Text ): string {
if ( startsWithFiller( domText ) ) {
return domText.data.slice( INLINE_FILLER_LENGTH );
} else {
return domText.data;
}
}
/**
* Assign key observer which move cursor from the end of the inline filler to the beginning of it when
* the left arrow is pressed, so the filler does not break navigation.
*
* @param {module:engine/view/view~View} view View controller instance we should inject quirks handling on.
*/
export function injectQuirksHandling( view: View ): void {
view.document.on( 'arrowKey', jumpOverInlineFiller, { priority: 'low' } );
}
// Move cursor from the end of the inline filler to the beginning of it when, so the filler does not break navigation.
function jumpOverInlineFiller( evt: unknown, data: DomEventData & KeystrokeInfo ) {
if ( data.keyCode == keyCodes.arrowleft ) {
const domSelection = data.domTarget.ownerDocument.defaultView!.getSelection()!;
if ( domSelection.rangeCount == 1 && domSelection.getRangeAt( 0 ).collapsed ) {
const domParent = domSelection.getRangeAt( 0 ).startContainer;
const domOffset = domSelection.getRangeAt( 0 ).startOffset;
if ( startsWithFiller( domParent ) && domOffset <= INLINE_FILLER_LENGTH ) {
domSelection.collapse( domParent, 0 );
}
}
}
}