@ckeditor/ckeditor5-typing 40.0.0 → 40.2.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,199 +1,232 @@
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, type Editor } from '@ckeditor/ckeditor5-core';
9
- /**
10
- * This plugin enables the two-step caret (phantom) movement behavior for
11
- * {@link module:typing/twostepcaretmovement~TwoStepCaretMovement#registerAttribute registered attributes}
12
- * on arrow right (<kbd>→</kbd>) and left (<kbd>←</kbd>) key press.
13
- *
14
- * Thanks to this (phantom) caret movement the user is able to type before/after as well as at the
15
- * beginning/end of an attribute.
16
- *
17
- * **Note:** This plugin support right–to–left (Arabic, Hebrew, etc.) content by mirroring its behavior
18
- * but for the sake of simplicity examples showcase only left–to–right use–cases.
19
- *
20
- * # Forward movement
21
- *
22
- * ## "Entering" an attribute:
23
- *
24
- * When this plugin is enabled and registered for the `a` attribute and the selection is right before it
25
- * (at the attribute boundary), pressing the right arrow key will not move the selection but update its
26
- * attributes accordingly:
27
- *
28
- * * When enabled:
29
- *
30
- * ```xml
31
- * foo{}<$text a="true">bar</$text>
32
- * ```
33
- *
34
- * <kbd>→</kbd>
35
- *
36
- * ```xml
37
- * foo<$text a="true">{}bar</$text>
38
- * ```
39
- *
40
- * * When disabled:
41
- *
42
- * ```xml
43
- * foo{}<$text a="true">bar</$text>
44
- * ```
45
- *
46
- * <kbd>→</kbd>
47
- *
48
- * ```xml
49
- * foo<$text a="true">b{}ar</$text>
50
- * ```
51
- *
52
- *
53
- * ## "Leaving" an attribute:
54
- *
55
- * * When enabled:
56
- *
57
- * ```xml
58
- * <$text a="true">bar{}</$text>baz
59
- * ```
60
- *
61
- * <kbd>→</kbd>
62
- *
63
- * ```xml
64
- * <$text a="true">bar</$text>{}baz
65
- * ```
66
- *
67
- * * When disabled:
68
- *
69
- * ```xml
70
- * <$text a="true">bar{}</$text>baz
71
- * ```
72
- *
73
- * <kbd>→</kbd>
74
- *
75
- * ```xml
76
- * <$text a="true">bar</$text>b{}az
77
- * ```
78
- *
79
- * # Backward movement
80
- *
81
- * * When enabled:
82
- *
83
- * ```xml
84
- * <$text a="true">bar</$text>{}baz
85
- * ```
86
- *
87
- * <kbd>←</kbd>
88
- *
89
- * ```xml
90
- * <$text a="true">bar{}</$text>baz
91
- * ```
92
- *
93
- * * When disabled:
94
- *
95
- * ```xml
96
- * <$text a="true">bar</$text>{}baz
97
- * ```
98
- *
99
- * <kbd>←</kbd>
100
- *
101
- * ```xml
102
- * <$text a="true">ba{}r</$text>b{}az
103
- * ```
104
- *
105
- * # Multiple attributes
106
- *
107
- * * When enabled and many attributes starts or ends at the same position:
108
- *
109
- * ```xml
110
- * <$text a="true" b="true">bar</$text>{}baz
111
- * ```
112
- *
113
- * <kbd>←</kbd>
114
- *
115
- * ```xml
116
- * <$text a="true" b="true">bar{}</$text>baz
117
- * ```
118
- *
119
- * * When enabled and one procedes another:
120
- *
121
- * ```xml
122
- * <$text a="true">bar</$text><$text b="true">{}bar</$text>
123
- * ```
124
- *
125
- * <kbd>←</kbd>
126
- *
127
- * ```xml
128
- * <$text a="true">bar{}</$text><$text b="true">bar</$text>
129
- * ```
130
- *
131
- */
132
- export default class TwoStepCaretMovement extends Plugin {
133
- /**
134
- * A set of attributes to handle.
135
- */
136
- private attributes;
137
- /**
138
- * The current UID of the overridden gravity, as returned by
139
- * {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
140
- */
141
- private _overrideUid;
142
- /**
143
- * A flag indicating that the automatic gravity restoration should not happen upon the next
144
- * gravity restoration.
145
- * {@link module:engine/model/selection~Selection#event:change:range} event.
146
- */
147
- private _isNextGravityRestorationSkipped;
148
- /**
149
- * @inheritDoc
150
- */
151
- static get pluginName(): "TwoStepCaretMovement";
152
- /**
153
- * @inheritDoc
154
- */
155
- constructor(editor: Editor);
156
- /**
157
- * @inheritDoc
158
- */
159
- init(): void;
160
- /**
161
- * Registers a given attribute for the two-step caret movement.
162
- *
163
- * @param attribute Name of the attribute to handle.
164
- */
165
- registerAttribute(attribute: string): void;
166
- /**
167
- * Updates the document selection and the view according to the two–step caret movement state
168
- * when moving **forwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
169
- *
170
- * @param data Data of the key press.
171
- * @returns `true` when the handler prevented caret movement.
172
- */
173
- private _handleForwardMovement;
174
- /**
175
- * Updates the document selection and the view according to the two–step caret movement state
176
- * when moving **backwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
177
- *
178
- * @param data Data of the key press.
179
- * @returns `true` when the handler prevented caret movement
180
- */
181
- private _handleBackwardMovement;
182
- /**
183
- * `true` when the gravity is overridden for the plugin.
184
- */
185
- private get _isGravityOverridden();
186
- /**
187
- * Overrides the gravity using the {@link module:engine/model/writer~Writer model writer}
188
- * and stores the information about this fact in the {@link #_overrideUid}.
189
- *
190
- * A shorthand for {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
191
- */
192
- private _overrideGravity;
193
- /**
194
- * Restores the gravity using the {@link module:engine/model/writer~Writer model writer}.
195
- *
196
- * A shorthand for {@link module:engine/model/writer~Writer#restoreSelectionGravity}.
197
- */
198
- private _restoreGravity;
199
- }
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, type Editor } from '@ckeditor/ckeditor5-core';
9
+ /**
10
+ * This plugin enables the two-step caret (phantom) movement behavior for
11
+ * {@link module:typing/twostepcaretmovement~TwoStepCaretMovement#registerAttribute registered attributes}
12
+ * on arrow right (<kbd>→</kbd>) and left (<kbd>←</kbd>) key press.
13
+ *
14
+ * Thanks to this (phantom) caret movement the user is able to type before/after as well as at the
15
+ * beginning/end of an attribute.
16
+ *
17
+ * **Note:** This plugin support right–to–left (Arabic, Hebrew, etc.) content by mirroring its behavior
18
+ * but for the sake of simplicity examples showcase only left–to–right use–cases.
19
+ *
20
+ * # Forward movement
21
+ *
22
+ * ## "Entering" an attribute:
23
+ *
24
+ * When this plugin is enabled and registered for the `a` attribute and the selection is right before it
25
+ * (at the attribute boundary), pressing the right arrow key will not move the selection but update its
26
+ * attributes accordingly:
27
+ *
28
+ * * When enabled:
29
+ *
30
+ * ```xml
31
+ * foo{}<$text a="true">bar</$text>
32
+ * ```
33
+ *
34
+ * <kbd>→</kbd>
35
+ *
36
+ * ```xml
37
+ * foo<$text a="true">{}bar</$text>
38
+ * ```
39
+ *
40
+ * * When disabled:
41
+ *
42
+ * ```xml
43
+ * foo{}<$text a="true">bar</$text>
44
+ * ```
45
+ *
46
+ * <kbd>→</kbd>
47
+ *
48
+ * ```xml
49
+ * foo<$text a="true">b{}ar</$text>
50
+ * ```
51
+ *
52
+ *
53
+ * ## "Leaving" an attribute:
54
+ *
55
+ * * When enabled:
56
+ *
57
+ * ```xml
58
+ * <$text a="true">bar{}</$text>baz
59
+ * ```
60
+ *
61
+ * <kbd>→</kbd>
62
+ *
63
+ * ```xml
64
+ * <$text a="true">bar</$text>{}baz
65
+ * ```
66
+ *
67
+ * * When disabled:
68
+ *
69
+ * ```xml
70
+ * <$text a="true">bar{}</$text>baz
71
+ * ```
72
+ *
73
+ * <kbd>→</kbd>
74
+ *
75
+ * ```xml
76
+ * <$text a="true">bar</$text>b{}az
77
+ * ```
78
+ *
79
+ * # Backward movement
80
+ *
81
+ * * When enabled:
82
+ *
83
+ * ```xml
84
+ * <$text a="true">bar</$text>{}baz
85
+ * ```
86
+ *
87
+ * <kbd>←</kbd>
88
+ *
89
+ * ```xml
90
+ * <$text a="true">bar{}</$text>baz
91
+ * ```
92
+ *
93
+ * * When disabled:
94
+ *
95
+ * ```xml
96
+ * <$text a="true">bar</$text>{}baz
97
+ * ```
98
+ *
99
+ * <kbd>←</kbd>
100
+ *
101
+ * ```xml
102
+ * <$text a="true">ba{}r</$text>b{}az
103
+ * ```
104
+ *
105
+ * # Multiple attributes
106
+ *
107
+ * * When enabled and many attributes starts or ends at the same position:
108
+ *
109
+ * ```xml
110
+ * <$text a="true" b="true">bar</$text>{}baz
111
+ * ```
112
+ *
113
+ * <kbd>←</kbd>
114
+ *
115
+ * ```xml
116
+ * <$text a="true" b="true">bar{}</$text>baz
117
+ * ```
118
+ *
119
+ * * When enabled and one procedes another:
120
+ *
121
+ * ```xml
122
+ * <$text a="true">bar</$text><$text b="true">{}bar</$text>
123
+ * ```
124
+ *
125
+ * <kbd>←</kbd>
126
+ *
127
+ * ```xml
128
+ * <$text a="true">bar{}</$text><$text b="true">bar</$text>
129
+ * ```
130
+ *
131
+ */
132
+ export default class TwoStepCaretMovement extends Plugin {
133
+ /**
134
+ * A set of attributes to handle.
135
+ */
136
+ private attributes;
137
+ /**
138
+ * The current UID of the overridden gravity, as returned by
139
+ * {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
140
+ */
141
+ private _overrideUid;
142
+ /**
143
+ * A flag indicating that the automatic gravity restoration should not happen upon the next
144
+ * gravity restoration.
145
+ * {@link module:engine/model/selection~Selection#event:change:range} event.
146
+ */
147
+ private _isNextGravityRestorationSkipped;
148
+ /**
149
+ * @inheritDoc
150
+ */
151
+ static get pluginName(): "TwoStepCaretMovement";
152
+ /**
153
+ * @inheritDoc
154
+ */
155
+ constructor(editor: Editor);
156
+ /**
157
+ * @inheritDoc
158
+ */
159
+ init(): void;
160
+ /**
161
+ * Registers a given attribute for the two-step caret movement.
162
+ *
163
+ * @param attribute Name of the attribute to handle.
164
+ */
165
+ registerAttribute(attribute: string): void;
166
+ /**
167
+ * Updates the document selection and the view according to the two–step caret movement state
168
+ * when moving **forwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
169
+ *
170
+ * @param data Data of the key press.
171
+ * @returns `true` when the handler prevented caret movement.
172
+ */
173
+ private _handleForwardMovement;
174
+ /**
175
+ * Updates the document selection and the view according to the two–step caret movement state
176
+ * when moving **backwards**. Executed upon `keypress` in the {@link module:engine/view/view~View}.
177
+ *
178
+ * @param data Data of the key press.
179
+ * @returns `true` when the handler prevented caret movement
180
+ */
181
+ private _handleBackwardMovement;
182
+ /**
183
+ * Starts listening to {@link module:engine/view/document~Document#event:mousedown} and
184
+ * {@link module:engine/view/document~Document#event:selectionChange} and puts the selection before/after a 2-step node
185
+ * if clicked at the beginning/ending of the 2-step node.
186
+ *
187
+ * The purpose of this action is to allow typing around the 2-step node directly after a click.
188
+ *
189
+ * See https://github.com/ckeditor/ckeditor5/issues/1016.
190
+ */
191
+ private _enableClickingAfterNode;
192
+ /**
193
+ * Starts listening to {@link module:engine/model/model~Model#event:insertContent} and corrects the model
194
+ * selection attributes if the selection is at the end of a two-step node after inserting the content.
195
+ *
196
+ * The purpose of this action is to improve the overall UX because the user is no longer "trapped" by the
197
+ * two-step attribute of the selection, and they can type a "clean" (`linkHref`–less) text right away.
198
+ *
199
+ * See https://github.com/ckeditor/ckeditor5/issues/6053.
200
+ */
201
+ private _enableInsertContentSelectionAttributesFixer;
202
+ /**
203
+ * Starts listening to {@link module:engine/model/model~Model#deleteContent} and checks whether
204
+ * removing a content right after the tow-step attribute.
205
+ *
206
+ * If so, the selection should not preserve the two-step attribute. However, if
207
+ * the {@link module:typing/twostepcaretmovement~TwoStepCaretMovement} plugin is active and
208
+ * the selection has the two-step attribute due to overridden gravity (at the end), the two-step attribute should stay untouched.
209
+ *
210
+ * The purpose of this action is to allow removing the link text and keep the selection outside the link.
211
+ *
212
+ * See https://github.com/ckeditor/ckeditor5/issues/7521.
213
+ */
214
+ private _handleDeleteContentAfterNode;
215
+ /**
216
+ * `true` when the gravity is overridden for the plugin.
217
+ */
218
+ private get _isGravityOverridden();
219
+ /**
220
+ * Overrides the gravity using the {@link module:engine/model/writer~Writer model writer}
221
+ * and stores the information about this fact in the {@link #_overrideUid}.
222
+ *
223
+ * A shorthand for {@link module:engine/model/writer~Writer#overrideSelectionGravity}.
224
+ */
225
+ private _overrideGravity;
226
+ /**
227
+ * Restores the gravity using the {@link module:engine/model/writer~Writer model writer}.
228
+ *
229
+ * A shorthand for {@link module:engine/model/writer~Writer#restoreSelectionGravity}.
230
+ */
231
+ private _restoreGravity;
232
+ }