@ckeditor/ckeditor5-typing 39.0.1 → 40.0.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.
@@ -1,435 +1,435 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module typing/twostepcaretmovement
7
- */
8
- import { Plugin } from '@ckeditor/ckeditor5-core';
9
- import { keyCodes } from '@ckeditor/ckeditor5-utils';
10
- /**
11
- * This plugin enables the two-step caret (phantom) movement behavior for
12
- * {@link module:typing/twostepcaretmovement~TwoStepCaretMovement#registerAttribute registered attributes}
13
- * on arrow right (<kbd>→</kbd>) and left (<kbd>←</kbd>) key press.
14
- *
15
- * Thanks to this (phantom) caret movement the user is able to type before/after as well as at the
16
- * beginning/end of an attribute.
17
- *
18
- * **Note:** This plugin support right–to–left (Arabic, Hebrew, etc.) content by mirroring its behavior
19
- * but for the sake of simplicity examples showcase only left–to–right use–cases.
20
- *
21
- * # Forward movement
22
- *
23
- * ## "Entering" an attribute:
24
- *
25
- * When this plugin is enabled and registered for the `a` attribute and the selection is right before it
26
- * (at the attribute boundary), pressing the right arrow key will not move the selection but update its
27
- * attributes accordingly:
28
- *
29
- * * When enabled:
30
- *
31
- * ```xml
32
- * foo{}<$text a="true">bar</$text>
33
- * ```
34
- *
35
- * <kbd>→</kbd>
36
- *
37
- * ```xml
38
- * foo<$text a="true">{}bar</$text>
39
- * ```
40
- *
41
- * * When disabled:
42
- *
43
- * ```xml
44
- * foo{}<$text a="true">bar</$text>
45
- * ```
46
- *
47
- * <kbd>→</kbd>
48
- *
49
- * ```xml
50
- * foo<$text a="true">b{}ar</$text>
51
- * ```
52
- *
53
- *
54
- * ## "Leaving" an attribute:
55
- *
56
- * * When enabled:
57
- *
58
- * ```xml
59
- * <$text a="true">bar{}</$text>baz
60
- * ```
61
- *
62
- * <kbd>→</kbd>
63
- *
64
- * ```xml
65
- * <$text a="true">bar</$text>{}baz
66
- * ```
67
- *
68
- * * When disabled:
69
- *
70
- * ```xml
71
- * <$text a="true">bar{}</$text>baz
72
- * ```
73
- *
74
- * <kbd>→</kbd>
75
- *
76
- * ```xml
77
- * <$text a="true">bar</$text>b{}az
78
- * ```
79
- *
80
- * # Backward movement
81
- *
82
- * * When enabled:
83
- *
84
- * ```xml
85
- * <$text a="true">bar</$text>{}baz
86
- * ```
87
- *
88
- * <kbd>←</kbd>
89
- *
90
- * ```xml
91
- * <$text a="true">bar{}</$text>baz
92
- * ```
93
- *
94
- * * When disabled:
95
- *
96
- * ```xml
97
- * <$text a="true">bar</$text>{}baz
98
- * ```
99
- *
100
- * <kbd>←</kbd>
101
- *
102
- * ```xml
103
- * <$text a="true">ba{}r</$text>b{}az
104
- * ```
105
- *
106
- * # Multiple attributes
107
- *
108
- * * When enabled and many attributes starts or ends at the same position:
109
- *
110
- * ```xml
111
- * <$text a="true" b="true">bar</$text>{}baz
112
- * ```
113
- *
114
- * <kbd>←</kbd>
115
- *
116
- * ```xml
117
- * <$text a="true" b="true">bar{}</$text>baz
118
- * ```
119
- *
120
- * * When enabled and one procedes another:
121
- *
122
- * ```xml
123
- * <$text a="true">bar</$text><$text b="true">{}bar</$text>
124
- * ```
125
- *
126
- * <kbd>←</kbd>
127
- *
128
- * ```xml
129
- * <$text a="true">bar{}</$text><$text b="true">bar</$text>
130
- * ```
131
- *
132
- */
133
- export default class TwoStepCaretMovement extends Plugin {
134
- /**
135
- * @inheritDoc
136
- */
137
- static get pluginName() {
138
- return 'TwoStepCaretMovement';
139
- }
140
- /**
141
- * @inheritDoc
142
- */
143
- constructor(editor) {
144
- super(editor);
145
- this.attributes = new Set();
146
- this._overrideUid = null;
147
- }
148
- /**
149
- * @inheritDoc
150
- */
151
- init() {
152
- const editor = this.editor;
153
- const model = editor.model;
154
- const view = editor.editing.view;
155
- const locale = editor.locale;
156
- const modelSelection = model.document.selection;
157
- // Listen to keyboard events and handle the caret movement according to the 2-step caret logic.
158
- this.listenTo(view.document, 'arrowKey', (evt, data) => {
159
- // This implementation works only for collapsed selection.
160
- if (!modelSelection.isCollapsed) {
161
- return;
162
- }
163
- // When user tries to expand the selection or jump over the whole word or to the beginning/end then
164
- // two-steps movement is not necessary.
165
- if (data.shiftKey || data.altKey || data.ctrlKey) {
166
- return;
167
- }
168
- const arrowRightPressed = data.keyCode == keyCodes.arrowright;
169
- const arrowLeftPressed = data.keyCode == keyCodes.arrowleft;
170
- // When neither left or right arrow has been pressed then do noting.
171
- if (!arrowRightPressed && !arrowLeftPressed) {
172
- return;
173
- }
174
- const contentDirection = locale.contentLanguageDirection;
175
- let isMovementHandled = false;
176
- if ((contentDirection === 'ltr' && arrowRightPressed) || (contentDirection === 'rtl' && arrowLeftPressed)) {
177
- isMovementHandled = this._handleForwardMovement(data);
178
- }
179
- else {
180
- isMovementHandled = this._handleBackwardMovement(data);
181
- }
182
- // Stop the keydown event if the two-step caret movement handled it. Avoid collisions
183
- // with other features which may also take over the caret movement (e.g. Widget).
184
- if (isMovementHandled === true) {
185
- evt.stop();
186
- }
187
- }, { context: '$text', priority: 'highest' });
188
- this._isNextGravityRestorationSkipped = false;
189
- // The automatic gravity restoration logic.
190
- this.listenTo(modelSelection, 'change:range', (evt, data) => {
191
- // Skipping the automatic restoration is needed if the selection should change
192
- // but the gravity must remain overridden afterwards. See the #handleBackwardMovement
193
- // to learn more.
194
- if (this._isNextGravityRestorationSkipped) {
195
- this._isNextGravityRestorationSkipped = false;
196
- return;
197
- }
198
- // Skip automatic restore when the gravity is not overridden — simply, there's nothing to restore
199
- // at this moment.
200
- if (!this._isGravityOverridden) {
201
- return;
202
- }
203
- // Skip automatic restore when the change is indirect AND the selection is at the attribute boundary.
204
- // It means that e.g. if the change was external (collaboration) and the user had their
205
- // selection around the link, its gravity should remain intact in this change:range event.
206
- if (!data.directChange && isBetweenDifferentAttributes(modelSelection.getFirstPosition(), this.attributes)) {
207
- return;
208
- }
209
- this._restoreGravity();
210
- });
211
- }
212
- /**
213
- * Registers a given attribute for the two-step caret movement.
214
- *
215
- * @param attribute Name of the attribute to handle.
216
- */
217
- registerAttribute(attribute) {
218
- this.attributes.add(attribute);
219
- }
220
- /**
221
- * Updates the document selection and the view according to the two–step caret movement state
222
- * when moving **forwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
223
- *
224
- * @param data Data of the key press.
225
- * @returns `true` when the handler prevented caret movement.
226
- */
227
- _handleForwardMovement(data) {
228
- const attributes = this.attributes;
229
- const model = this.editor.model;
230
- const selection = model.document.selection;
231
- const position = selection.getFirstPosition();
232
- // DON'T ENGAGE 2-SCM if gravity is already overridden. It means that we just entered
233
- //
234
- // <paragraph>foo<$text attribute>{}bar</$text>baz</paragraph>
235
- //
236
- // or left the attribute
237
- //
238
- // <paragraph>foo<$text attribute>bar</$text>{}baz</paragraph>
239
- //
240
- // and the gravity will be restored automatically.
241
- if (this._isGravityOverridden) {
242
- return false;
243
- }
244
- // DON'T ENGAGE 2-SCM when the selection is at the beginning of the block AND already has the
245
- // attribute:
246
- // * when the selection was initially set there using the mouse,
247
- // * when the editor has just started
248
- //
249
- // <paragraph><$text attribute>{}bar</$text>baz</paragraph>
250
- //
251
- if (position.isAtStart && hasAnyAttribute(selection, attributes)) {
252
- return false;
253
- }
254
- // ENGAGE 2-SCM When at least one of the observed attributes changes its value (incl. starts, ends).
255
- //
256
- // <paragraph>foo<$text attribute>bar{}</$text>baz</paragraph>
257
- // <paragraph>foo<$text attribute>bar{}</$text><$text otherAttribute>baz</$text></paragraph>
258
- // <paragraph>foo<$text attribute=1>bar{}</$text><$text attribute=2>baz</$text></paragraph>
259
- // <paragraph>foo{}<$text attribute>bar</$text>baz</paragraph>
260
- //
261
- if (isBetweenDifferentAttributes(position, attributes)) {
262
- preventCaretMovement(data);
263
- this._overrideGravity();
264
- return true;
265
- }
266
- return false;
267
- }
268
- /**
269
- * Updates the document selection and the view according to the two–step caret movement state
270
- * when moving **backwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
271
- *
272
- * @param data Data of the key press.
273
- * @returns `true` when the handler prevented caret movement
274
- */
275
- _handleBackwardMovement(data) {
276
- const attributes = this.attributes;
277
- const model = this.editor.model;
278
- const selection = model.document.selection;
279
- const position = selection.getFirstPosition();
280
- // When the gravity is already overridden (by this plugin), it means we are on the two-step position.
281
- // Prevent the movement, restore the gravity and update selection attributes.
282
- //
283
- // <paragraph>foo<$text attribute=1>bar</$text><$text attribute=2>{}baz</$text></paragraph>
284
- // <paragraph>foo<$text attribute>bar</$text><$text otherAttribute>{}baz</$text></paragraph>
285
- // <paragraph>foo<$text attribute>{}bar</$text>baz</paragraph>
286
- // <paragraph>foo<$text attribute>bar</$text>{}baz</paragraph>
287
- //
288
- if (this._isGravityOverridden) {
289
- preventCaretMovement(data);
290
- this._restoreGravity();
291
- setSelectionAttributesFromTheNodeBefore(model, attributes, position);
292
- return true;
293
- }
294
- else {
295
- // REMOVE SELECTION ATTRIBUTE when restoring gravity towards a non-existent content at the
296
- // beginning of the block.
297
- //
298
- // <paragraph>{}<$text attribute>bar</$text></paragraph>
299
- //
300
- if (position.isAtStart) {
301
- if (hasAnyAttribute(selection, attributes)) {
302
- preventCaretMovement(data);
303
- setSelectionAttributesFromTheNodeBefore(model, attributes, position);
304
- return true;
305
- }
306
- return false;
307
- }
308
- // When we are moving from natural gravity, to the position of the 2SCM, we need to override the gravity,
309
- // and make sure it won't be restored. Unless it's at the end of the block and an observed attribute.
310
- // We need to check if the caret is a one position before the attribute boundary:
311
- //
312
- // <paragraph>foo<$text attribute=1>bar</$text><$text attribute=2>b{}az</$text></paragraph>
313
- // <paragraph>foo<$text attribute>bar</$text><$text otherAttribute>b{}az</$text></paragraph>
314
- // <paragraph>foo<$text attribute>b{}ar</$text>baz</paragraph>
315
- // <paragraph>foo<$text attribute>bar</$text>b{}az</paragraph>
316
- //
317
- if (isStepAfterAnyAttributeBoundary(position, attributes)) {
318
- // ENGAGE 2-SCM if the selection has no attribute. This may happen when the user
319
- // left the attribute using a FORWARD 2-SCM.
320
- //
321
- // <paragraph><$text attribute>bar</$text>{}</paragraph>
322
- //
323
- if (position.isAtEnd &&
324
- !hasAnyAttribute(selection, attributes) &&
325
- isBetweenDifferentAttributes(position, attributes)) {
326
- preventCaretMovement(data);
327
- setSelectionAttributesFromTheNodeBefore(model, attributes, position);
328
- return true;
329
- }
330
- // Skip the automatic gravity restore upon the next selection#change:range event.
331
- // If not skipped, it would automatically restore the gravity, which should remain
332
- // overridden.
333
- this._isNextGravityRestorationSkipped = true;
334
- this._overrideGravity();
335
- // Don't return "true" here because we didn't call _preventCaretMovement.
336
- // Returning here will destabilize the filler logic, which also listens to
337
- // keydown (and the event would be stopped).
338
- return false;
339
- }
340
- }
341
- return false;
342
- }
343
- /**
344
- * `true` when the gravity is overridden for the plugin.
345
- */
346
- get _isGravityOverridden() {
347
- return !!this._overrideUid;
348
- }
349
- /**
350
- * Overrides the gravity using the {@link module:engine/model/writer~Writer model writer}
351
- * and stores the information about this fact in the {@link #_overrideUid}.
352
- *
353
- * A shorthand for {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
354
- */
355
- _overrideGravity() {
356
- this._overrideUid = this.editor.model.change(writer => {
357
- return writer.overrideSelectionGravity();
358
- });
359
- }
360
- /**
361
- * Restores the gravity using the {@link module:engine/model/writer~Writer model writer}.
362
- *
363
- * A shorthand for {@link module:engine/model/writer~Writer#restoreSelectionGravity}.
364
- */
365
- _restoreGravity() {
366
- this.editor.model.change(writer => {
367
- writer.restoreSelectionGravity(this._overrideUid);
368
- this._overrideUid = null;
369
- });
370
- }
371
- }
372
- /**
373
- * Checks whether the selection has any of given attributes.
374
- */
375
- function hasAnyAttribute(selection, attributes) {
376
- for (const observedAttribute of attributes) {
377
- if (selection.hasAttribute(observedAttribute)) {
378
- return true;
379
- }
380
- }
381
- return false;
382
- }
383
- /**
384
- * Applies the given attributes to the current selection using using the
385
- * values from the node before the current position. Uses
386
- * the {@link module:engine/model/writer~Writer model writer}.
387
- */
388
- function setSelectionAttributesFromTheNodeBefore(model, attributes, position) {
389
- const nodeBefore = position.nodeBefore;
390
- model.change(writer => {
391
- if (nodeBefore) {
392
- const attributes = [];
393
- const isInlineObject = model.schema.isObject(nodeBefore) && model.schema.isInline(nodeBefore);
394
- for (const [key, value] of nodeBefore.getAttributes()) {
395
- if (model.schema.checkAttribute('$text', key) &&
396
- (!isInlineObject || model.schema.getAttributeProperties(key).copyFromObject !== false)) {
397
- attributes.push([key, value]);
398
- }
399
- }
400
- writer.setSelectionAttribute(attributes);
401
- }
402
- else {
403
- writer.removeSelectionAttribute(attributes);
404
- }
405
- });
406
- }
407
- /**
408
- * Prevents the caret movement in the view by calling `preventDefault` on the event data.
409
- *
410
- * @alias data.preventDefault
411
- */
412
- function preventCaretMovement(data) {
413
- data.preventDefault();
414
- }
415
- /**
416
- * Checks whether the step before `isBetweenDifferentAttributes()`.
417
- */
418
- function isStepAfterAnyAttributeBoundary(position, attributes) {
419
- const positionBefore = position.getShiftedBy(-1);
420
- return isBetweenDifferentAttributes(positionBefore, attributes);
421
- }
422
- /**
423
- * Checks whether the given position is between different values of given attributes.
424
- */
425
- function isBetweenDifferentAttributes(position, attributes) {
426
- const { nodeBefore, nodeAfter } = position;
427
- for (const observedAttribute of attributes) {
428
- const attrBefore = nodeBefore ? nodeBefore.getAttribute(observedAttribute) : undefined;
429
- const attrAfter = nodeAfter ? nodeAfter.getAttribute(observedAttribute) : undefined;
430
- if (attrAfter !== attrBefore) {
431
- return true;
432
- }
433
- }
434
- return false;
435
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module typing/twostepcaretmovement
7
+ */
8
+ import { Plugin } from '@ckeditor/ckeditor5-core';
9
+ import { keyCodes } from '@ckeditor/ckeditor5-utils';
10
+ /**
11
+ * This plugin enables the two-step caret (phantom) movement behavior for
12
+ * {@link module:typing/twostepcaretmovement~TwoStepCaretMovement#registerAttribute registered attributes}
13
+ * on arrow right (<kbd>→</kbd>) and left (<kbd>←</kbd>) key press.
14
+ *
15
+ * Thanks to this (phantom) caret movement the user is able to type before/after as well as at the
16
+ * beginning/end of an attribute.
17
+ *
18
+ * **Note:** This plugin support right–to–left (Arabic, Hebrew, etc.) content by mirroring its behavior
19
+ * but for the sake of simplicity examples showcase only left–to–right use–cases.
20
+ *
21
+ * # Forward movement
22
+ *
23
+ * ## "Entering" an attribute:
24
+ *
25
+ * When this plugin is enabled and registered for the `a` attribute and the selection is right before it
26
+ * (at the attribute boundary), pressing the right arrow key will not move the selection but update its
27
+ * attributes accordingly:
28
+ *
29
+ * * When enabled:
30
+ *
31
+ * ```xml
32
+ * foo{}<$text a="true">bar</$text>
33
+ * ```
34
+ *
35
+ * <kbd>→</kbd>
36
+ *
37
+ * ```xml
38
+ * foo<$text a="true">{}bar</$text>
39
+ * ```
40
+ *
41
+ * * When disabled:
42
+ *
43
+ * ```xml
44
+ * foo{}<$text a="true">bar</$text>
45
+ * ```
46
+ *
47
+ * <kbd>→</kbd>
48
+ *
49
+ * ```xml
50
+ * foo<$text a="true">b{}ar</$text>
51
+ * ```
52
+ *
53
+ *
54
+ * ## "Leaving" an attribute:
55
+ *
56
+ * * When enabled:
57
+ *
58
+ * ```xml
59
+ * <$text a="true">bar{}</$text>baz
60
+ * ```
61
+ *
62
+ * <kbd>→</kbd>
63
+ *
64
+ * ```xml
65
+ * <$text a="true">bar</$text>{}baz
66
+ * ```
67
+ *
68
+ * * When disabled:
69
+ *
70
+ * ```xml
71
+ * <$text a="true">bar{}</$text>baz
72
+ * ```
73
+ *
74
+ * <kbd>→</kbd>
75
+ *
76
+ * ```xml
77
+ * <$text a="true">bar</$text>b{}az
78
+ * ```
79
+ *
80
+ * # Backward movement
81
+ *
82
+ * * When enabled:
83
+ *
84
+ * ```xml
85
+ * <$text a="true">bar</$text>{}baz
86
+ * ```
87
+ *
88
+ * <kbd>←</kbd>
89
+ *
90
+ * ```xml
91
+ * <$text a="true">bar{}</$text>baz
92
+ * ```
93
+ *
94
+ * * When disabled:
95
+ *
96
+ * ```xml
97
+ * <$text a="true">bar</$text>{}baz
98
+ * ```
99
+ *
100
+ * <kbd>←</kbd>
101
+ *
102
+ * ```xml
103
+ * <$text a="true">ba{}r</$text>b{}az
104
+ * ```
105
+ *
106
+ * # Multiple attributes
107
+ *
108
+ * * When enabled and many attributes starts or ends at the same position:
109
+ *
110
+ * ```xml
111
+ * <$text a="true" b="true">bar</$text>{}baz
112
+ * ```
113
+ *
114
+ * <kbd>←</kbd>
115
+ *
116
+ * ```xml
117
+ * <$text a="true" b="true">bar{}</$text>baz
118
+ * ```
119
+ *
120
+ * * When enabled and one procedes another:
121
+ *
122
+ * ```xml
123
+ * <$text a="true">bar</$text><$text b="true">{}bar</$text>
124
+ * ```
125
+ *
126
+ * <kbd>←</kbd>
127
+ *
128
+ * ```xml
129
+ * <$text a="true">bar{}</$text><$text b="true">bar</$text>
130
+ * ```
131
+ *
132
+ */
133
+ export default class TwoStepCaretMovement extends Plugin {
134
+ /**
135
+ * @inheritDoc
136
+ */
137
+ static get pluginName() {
138
+ return 'TwoStepCaretMovement';
139
+ }
140
+ /**
141
+ * @inheritDoc
142
+ */
143
+ constructor(editor) {
144
+ super(editor);
145
+ this.attributes = new Set();
146
+ this._overrideUid = null;
147
+ }
148
+ /**
149
+ * @inheritDoc
150
+ */
151
+ init() {
152
+ const editor = this.editor;
153
+ const model = editor.model;
154
+ const view = editor.editing.view;
155
+ const locale = editor.locale;
156
+ const modelSelection = model.document.selection;
157
+ // Listen to keyboard events and handle the caret movement according to the 2-step caret logic.
158
+ this.listenTo(view.document, 'arrowKey', (evt, data) => {
159
+ // This implementation works only for collapsed selection.
160
+ if (!modelSelection.isCollapsed) {
161
+ return;
162
+ }
163
+ // When user tries to expand the selection or jump over the whole word or to the beginning/end then
164
+ // two-steps movement is not necessary.
165
+ if (data.shiftKey || data.altKey || data.ctrlKey) {
166
+ return;
167
+ }
168
+ const arrowRightPressed = data.keyCode == keyCodes.arrowright;
169
+ const arrowLeftPressed = data.keyCode == keyCodes.arrowleft;
170
+ // When neither left or right arrow has been pressed then do noting.
171
+ if (!arrowRightPressed && !arrowLeftPressed) {
172
+ return;
173
+ }
174
+ const contentDirection = locale.contentLanguageDirection;
175
+ let isMovementHandled = false;
176
+ if ((contentDirection === 'ltr' && arrowRightPressed) || (contentDirection === 'rtl' && arrowLeftPressed)) {
177
+ isMovementHandled = this._handleForwardMovement(data);
178
+ }
179
+ else {
180
+ isMovementHandled = this._handleBackwardMovement(data);
181
+ }
182
+ // Stop the keydown event if the two-step caret movement handled it. Avoid collisions
183
+ // with other features which may also take over the caret movement (e.g. Widget).
184
+ if (isMovementHandled === true) {
185
+ evt.stop();
186
+ }
187
+ }, { context: '$text', priority: 'highest' });
188
+ this._isNextGravityRestorationSkipped = false;
189
+ // The automatic gravity restoration logic.
190
+ this.listenTo(modelSelection, 'change:range', (evt, data) => {
191
+ // Skipping the automatic restoration is needed if the selection should change
192
+ // but the gravity must remain overridden afterwards. See the #handleBackwardMovement
193
+ // to learn more.
194
+ if (this._isNextGravityRestorationSkipped) {
195
+ this._isNextGravityRestorationSkipped = false;
196
+ return;
197
+ }
198
+ // Skip automatic restore when the gravity is not overridden — simply, there's nothing to restore
199
+ // at this moment.
200
+ if (!this._isGravityOverridden) {
201
+ return;
202
+ }
203
+ // Skip automatic restore when the change is indirect AND the selection is at the attribute boundary.
204
+ // It means that e.g. if the change was external (collaboration) and the user had their
205
+ // selection around the link, its gravity should remain intact in this change:range event.
206
+ if (!data.directChange && isBetweenDifferentAttributes(modelSelection.getFirstPosition(), this.attributes)) {
207
+ return;
208
+ }
209
+ this._restoreGravity();
210
+ });
211
+ }
212
+ /**
213
+ * Registers a given attribute for the two-step caret movement.
214
+ *
215
+ * @param attribute Name of the attribute to handle.
216
+ */
217
+ registerAttribute(attribute) {
218
+ this.attributes.add(attribute);
219
+ }
220
+ /**
221
+ * Updates the document selection and the view according to the two–step caret movement state
222
+ * when moving **forwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
223
+ *
224
+ * @param data Data of the key press.
225
+ * @returns `true` when the handler prevented caret movement.
226
+ */
227
+ _handleForwardMovement(data) {
228
+ const attributes = this.attributes;
229
+ const model = this.editor.model;
230
+ const selection = model.document.selection;
231
+ const position = selection.getFirstPosition();
232
+ // DON'T ENGAGE 2-SCM if gravity is already overridden. It means that we just entered
233
+ //
234
+ // <paragraph>foo<$text attribute>{}bar</$text>baz</paragraph>
235
+ //
236
+ // or left the attribute
237
+ //
238
+ // <paragraph>foo<$text attribute>bar</$text>{}baz</paragraph>
239
+ //
240
+ // and the gravity will be restored automatically.
241
+ if (this._isGravityOverridden) {
242
+ return false;
243
+ }
244
+ // DON'T ENGAGE 2-SCM when the selection is at the beginning of the block AND already has the
245
+ // attribute:
246
+ // * when the selection was initially set there using the mouse,
247
+ // * when the editor has just started
248
+ //
249
+ // <paragraph><$text attribute>{}bar</$text>baz</paragraph>
250
+ //
251
+ if (position.isAtStart && hasAnyAttribute(selection, attributes)) {
252
+ return false;
253
+ }
254
+ // ENGAGE 2-SCM When at least one of the observed attributes changes its value (incl. starts, ends).
255
+ //
256
+ // <paragraph>foo<$text attribute>bar{}</$text>baz</paragraph>
257
+ // <paragraph>foo<$text attribute>bar{}</$text><$text otherAttribute>baz</$text></paragraph>
258
+ // <paragraph>foo<$text attribute=1>bar{}</$text><$text attribute=2>baz</$text></paragraph>
259
+ // <paragraph>foo{}<$text attribute>bar</$text>baz</paragraph>
260
+ //
261
+ if (isBetweenDifferentAttributes(position, attributes)) {
262
+ preventCaretMovement(data);
263
+ this._overrideGravity();
264
+ return true;
265
+ }
266
+ return false;
267
+ }
268
+ /**
269
+ * Updates the document selection and the view according to the two–step caret movement state
270
+ * when moving **backwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
271
+ *
272
+ * @param data Data of the key press.
273
+ * @returns `true` when the handler prevented caret movement
274
+ */
275
+ _handleBackwardMovement(data) {
276
+ const attributes = this.attributes;
277
+ const model = this.editor.model;
278
+ const selection = model.document.selection;
279
+ const position = selection.getFirstPosition();
280
+ // When the gravity is already overridden (by this plugin), it means we are on the two-step position.
281
+ // Prevent the movement, restore the gravity and update selection attributes.
282
+ //
283
+ // <paragraph>foo<$text attribute=1>bar</$text><$text attribute=2>{}baz</$text></paragraph>
284
+ // <paragraph>foo<$text attribute>bar</$text><$text otherAttribute>{}baz</$text></paragraph>
285
+ // <paragraph>foo<$text attribute>{}bar</$text>baz</paragraph>
286
+ // <paragraph>foo<$text attribute>bar</$text>{}baz</paragraph>
287
+ //
288
+ if (this._isGravityOverridden) {
289
+ preventCaretMovement(data);
290
+ this._restoreGravity();
291
+ setSelectionAttributesFromTheNodeBefore(model, attributes, position);
292
+ return true;
293
+ }
294
+ else {
295
+ // REMOVE SELECTION ATTRIBUTE when restoring gravity towards a non-existent content at the
296
+ // beginning of the block.
297
+ //
298
+ // <paragraph>{}<$text attribute>bar</$text></paragraph>
299
+ //
300
+ if (position.isAtStart) {
301
+ if (hasAnyAttribute(selection, attributes)) {
302
+ preventCaretMovement(data);
303
+ setSelectionAttributesFromTheNodeBefore(model, attributes, position);
304
+ return true;
305
+ }
306
+ return false;
307
+ }
308
+ // When we are moving from natural gravity, to the position of the 2SCM, we need to override the gravity,
309
+ // and make sure it won't be restored. Unless it's at the end of the block and an observed attribute.
310
+ // We need to check if the caret is a one position before the attribute boundary:
311
+ //
312
+ // <paragraph>foo<$text attribute=1>bar</$text><$text attribute=2>b{}az</$text></paragraph>
313
+ // <paragraph>foo<$text attribute>bar</$text><$text otherAttribute>b{}az</$text></paragraph>
314
+ // <paragraph>foo<$text attribute>b{}ar</$text>baz</paragraph>
315
+ // <paragraph>foo<$text attribute>bar</$text>b{}az</paragraph>
316
+ //
317
+ if (isStepAfterAnyAttributeBoundary(position, attributes)) {
318
+ // ENGAGE 2-SCM if the selection has no attribute. This may happen when the user
319
+ // left the attribute using a FORWARD 2-SCM.
320
+ //
321
+ // <paragraph><$text attribute>bar</$text>{}</paragraph>
322
+ //
323
+ if (position.isAtEnd &&
324
+ !hasAnyAttribute(selection, attributes) &&
325
+ isBetweenDifferentAttributes(position, attributes)) {
326
+ preventCaretMovement(data);
327
+ setSelectionAttributesFromTheNodeBefore(model, attributes, position);
328
+ return true;
329
+ }
330
+ // Skip the automatic gravity restore upon the next selection#change:range event.
331
+ // If not skipped, it would automatically restore the gravity, which should remain
332
+ // overridden.
333
+ this._isNextGravityRestorationSkipped = true;
334
+ this._overrideGravity();
335
+ // Don't return "true" here because we didn't call _preventCaretMovement.
336
+ // Returning here will destabilize the filler logic, which also listens to
337
+ // keydown (and the event would be stopped).
338
+ return false;
339
+ }
340
+ }
341
+ return false;
342
+ }
343
+ /**
344
+ * `true` when the gravity is overridden for the plugin.
345
+ */
346
+ get _isGravityOverridden() {
347
+ return !!this._overrideUid;
348
+ }
349
+ /**
350
+ * Overrides the gravity using the {@link module:engine/model/writer~Writer model writer}
351
+ * and stores the information about this fact in the {@link #_overrideUid}.
352
+ *
353
+ * A shorthand for {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
354
+ */
355
+ _overrideGravity() {
356
+ this._overrideUid = this.editor.model.change(writer => {
357
+ return writer.overrideSelectionGravity();
358
+ });
359
+ }
360
+ /**
361
+ * Restores the gravity using the {@link module:engine/model/writer~Writer model writer}.
362
+ *
363
+ * A shorthand for {@link module:engine/model/writer~Writer#restoreSelectionGravity}.
364
+ */
365
+ _restoreGravity() {
366
+ this.editor.model.change(writer => {
367
+ writer.restoreSelectionGravity(this._overrideUid);
368
+ this._overrideUid = null;
369
+ });
370
+ }
371
+ }
372
+ /**
373
+ * Checks whether the selection has any of given attributes.
374
+ */
375
+ function hasAnyAttribute(selection, attributes) {
376
+ for (const observedAttribute of attributes) {
377
+ if (selection.hasAttribute(observedAttribute)) {
378
+ return true;
379
+ }
380
+ }
381
+ return false;
382
+ }
383
+ /**
384
+ * Applies the given attributes to the current selection using using the
385
+ * values from the node before the current position. Uses
386
+ * the {@link module:engine/model/writer~Writer model writer}.
387
+ */
388
+ function setSelectionAttributesFromTheNodeBefore(model, attributes, position) {
389
+ const nodeBefore = position.nodeBefore;
390
+ model.change(writer => {
391
+ if (nodeBefore) {
392
+ const attributes = [];
393
+ const isInlineObject = model.schema.isObject(nodeBefore) && model.schema.isInline(nodeBefore);
394
+ for (const [key, value] of nodeBefore.getAttributes()) {
395
+ if (model.schema.checkAttribute('$text', key) &&
396
+ (!isInlineObject || model.schema.getAttributeProperties(key).copyFromObject !== false)) {
397
+ attributes.push([key, value]);
398
+ }
399
+ }
400
+ writer.setSelectionAttribute(attributes);
401
+ }
402
+ else {
403
+ writer.removeSelectionAttribute(attributes);
404
+ }
405
+ });
406
+ }
407
+ /**
408
+ * Prevents the caret movement in the view by calling `preventDefault` on the event data.
409
+ *
410
+ * @alias data.preventDefault
411
+ */
412
+ function preventCaretMovement(data) {
413
+ data.preventDefault();
414
+ }
415
+ /**
416
+ * Checks whether the step before `isBetweenDifferentAttributes()`.
417
+ */
418
+ function isStepAfterAnyAttributeBoundary(position, attributes) {
419
+ const positionBefore = position.getShiftedBy(-1);
420
+ return isBetweenDifferentAttributes(positionBefore, attributes);
421
+ }
422
+ /**
423
+ * Checks whether the given position is between different values of given attributes.
424
+ */
425
+ function isBetweenDifferentAttributes(position, attributes) {
426
+ const { nodeBefore, nodeAfter } = position;
427
+ for (const observedAttribute of attributes) {
428
+ const attrBefore = nodeBefore ? nodeBefore.getAttribute(observedAttribute) : undefined;
429
+ const attrAfter = nodeAfter ? nodeAfter.getAttribute(observedAttribute) : undefined;
430
+ if (attrAfter !== attrBefore) {
431
+ return true;
432
+ }
433
+ }
434
+ return false;
435
+ }