@ckeditor/ckeditor5-utils 0.0.0-internal-20241017.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.
Files changed (213) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +22 -0
  4. package/dist/abortabledebounce.d.ts +21 -0
  5. package/dist/areconnectedthroughproperties.d.ts +15 -0
  6. package/dist/ckeditorerror.d.ts +127 -0
  7. package/dist/collection.d.ts +437 -0
  8. package/dist/comparearrays.d.ts +34 -0
  9. package/dist/config.d.ts +167 -0
  10. package/dist/count.d.ts +22 -0
  11. package/dist/crc32.d.ts +30 -0
  12. package/dist/delay.d.ts +23 -0
  13. package/dist/diff.d.ts +35 -0
  14. package/dist/difftochanges.d.ts +63 -0
  15. package/dist/dom/createelement.d.ts +61 -0
  16. package/dist/dom/emittermixin.d.ts +146 -0
  17. package/dist/dom/findclosestscrollableancestor.d.ts +15 -0
  18. package/dist/dom/getancestors.d.ts +21 -0
  19. package/dist/dom/getborderwidths.d.ts +28 -0
  20. package/dist/dom/getcommonancestor.d.ts +16 -0
  21. package/dist/dom/getdatafromelement.d.ts +18 -0
  22. package/dist/dom/getpositionedancestor.d.ts +14 -0
  23. package/dist/dom/getrangefrommouseevent.d.ts +21 -0
  24. package/dist/dom/global.d.ts +36 -0
  25. package/dist/dom/indexof.d.ts +18 -0
  26. package/dist/dom/insertat.d.ts +19 -0
  27. package/dist/dom/iscomment.d.ts +15 -0
  28. package/dist/dom/isnode.d.ts +15 -0
  29. package/dist/dom/isrange.d.ts +15 -0
  30. package/dist/dom/istext.d.ts +15 -0
  31. package/dist/dom/isvalidattributename.d.ts +14 -0
  32. package/dist/dom/isvisible.d.ts +19 -0
  33. package/dist/dom/iswindow.d.ts +15 -0
  34. package/dist/dom/position.d.ts +215 -0
  35. package/dist/dom/rect.d.ts +199 -0
  36. package/dist/dom/remove.d.ts +17 -0
  37. package/dist/dom/resizeobserver.d.ts +78 -0
  38. package/dist/dom/scroll.d.ts +77 -0
  39. package/dist/dom/setdatainelement.d.ts +18 -0
  40. package/dist/dom/tounit.d.ts +26 -0
  41. package/dist/elementreplacer.d.ts +35 -0
  42. package/dist/emittermixin.d.ts +316 -0
  43. package/dist/env.d.ts +144 -0
  44. package/dist/eventinfo.d.ts +62 -0
  45. package/dist/fastdiff.d.ts +116 -0
  46. package/dist/first.d.ts +15 -0
  47. package/dist/focustracker.d.ts +83 -0
  48. package/dist/index-content.css +4 -0
  49. package/dist/index-editor.css +4 -0
  50. package/dist/index.css +4 -0
  51. package/dist/index.d.ts +70 -0
  52. package/dist/index.js +5871 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/inserttopriorityarray.d.ts +34 -0
  55. package/dist/isiterable.d.ts +18 -0
  56. package/dist/keyboard.d.ts +130 -0
  57. package/dist/keystrokehandler.d.ts +101 -0
  58. package/dist/language.d.ts +21 -0
  59. package/dist/locale.d.ts +145 -0
  60. package/dist/mapsequal.d.ts +19 -0
  61. package/dist/mix.d.ts +89 -0
  62. package/dist/nth.d.ts +20 -0
  63. package/dist/objecttomap.d.ts +27 -0
  64. package/dist/observablemixin.d.ts +564 -0
  65. package/dist/parsebase64encodedobject.d.ts +15 -0
  66. package/dist/priorities.d.ts +37 -0
  67. package/dist/retry.d.ts +37 -0
  68. package/dist/splicearray.d.ts +30 -0
  69. package/dist/spy.d.ts +25 -0
  70. package/dist/toarray.d.ts +29 -0
  71. package/dist/tomap.d.ts +23 -0
  72. package/dist/translation-service.d.ts +178 -0
  73. package/dist/uid.d.ts +19 -0
  74. package/dist/unicode.d.ts +58 -0
  75. package/dist/version.d.ts +14 -0
  76. package/dist/wait.d.ts +20 -0
  77. package/package.json +36 -0
  78. package/src/abortabledebounce.d.ts +17 -0
  79. package/src/abortabledebounce.js +22 -0
  80. package/src/areconnectedthroughproperties.d.ts +11 -0
  81. package/src/areconnectedthroughproperties.js +73 -0
  82. package/src/ckeditorerror.d.ts +123 -0
  83. package/src/ckeditorerror.js +176 -0
  84. package/src/collection.d.ts +433 -0
  85. package/src/collection.js +583 -0
  86. package/src/comparearrays.d.ts +30 -0
  87. package/src/comparearrays.js +47 -0
  88. package/src/config.d.ts +163 -0
  89. package/src/config.js +163 -0
  90. package/src/count.d.ts +18 -0
  91. package/src/count.js +25 -0
  92. package/src/crc32.d.ts +26 -0
  93. package/src/crc32.js +61 -0
  94. package/src/delay.d.ts +19 -0
  95. package/src/delay.js +26 -0
  96. package/src/diff.d.ts +31 -0
  97. package/src/diff.js +115 -0
  98. package/src/difftochanges.d.ts +59 -0
  99. package/src/difftochanges.js +79 -0
  100. package/src/dom/createelement.d.ts +57 -0
  101. package/src/dom/createelement.js +40 -0
  102. package/src/dom/emittermixin.d.ts +142 -0
  103. package/src/dom/emittermixin.js +239 -0
  104. package/src/dom/findclosestscrollableancestor.d.ts +11 -0
  105. package/src/dom/findclosestscrollableancestor.js +31 -0
  106. package/src/dom/getancestors.d.ts +17 -0
  107. package/src/dom/getancestors.js +27 -0
  108. package/src/dom/getborderwidths.d.ts +24 -0
  109. package/src/dom/getborderwidths.js +24 -0
  110. package/src/dom/getcommonancestor.d.ts +12 -0
  111. package/src/dom/getcommonancestor.js +25 -0
  112. package/src/dom/getdatafromelement.d.ts +14 -0
  113. package/src/dom/getdatafromelement.js +20 -0
  114. package/src/dom/getpositionedancestor.d.ts +10 -0
  115. package/src/dom/getpositionedancestor.js +22 -0
  116. package/src/dom/getrangefrommouseevent.d.ts +17 -0
  117. package/src/dom/getrangefrommouseevent.js +33 -0
  118. package/src/dom/global.d.ts +32 -0
  119. package/src/dom/global.js +35 -0
  120. package/src/dom/indexof.d.ts +14 -0
  121. package/src/dom/indexof.js +21 -0
  122. package/src/dom/insertat.d.ts +15 -0
  123. package/src/dom/insertat.js +17 -0
  124. package/src/dom/iscomment.d.ts +11 -0
  125. package/src/dom/iscomment.js +14 -0
  126. package/src/dom/isnode.d.ts +11 -0
  127. package/src/dom/isnode.js +21 -0
  128. package/src/dom/isrange.d.ts +11 -0
  129. package/src/dom/isrange.js +13 -0
  130. package/src/dom/istext.d.ts +11 -0
  131. package/src/dom/istext.js +13 -0
  132. package/src/dom/isvalidattributename.d.ts +10 -0
  133. package/src/dom/isvalidattributename.js +22 -0
  134. package/src/dom/isvisible.d.ts +15 -0
  135. package/src/dom/isvisible.js +30 -0
  136. package/src/dom/iswindow.d.ts +11 -0
  137. package/src/dom/iswindow.js +22 -0
  138. package/src/dom/position.d.ts +211 -0
  139. package/src/dom/position.js +313 -0
  140. package/src/dom/rect.d.ts +195 -0
  141. package/src/dom/rect.js +474 -0
  142. package/src/dom/remove.d.ts +13 -0
  143. package/src/dom/remove.js +18 -0
  144. package/src/dom/resizeobserver.d.ts +74 -0
  145. package/src/dom/resizeobserver.js +127 -0
  146. package/src/dom/scroll.d.ts +73 -0
  147. package/src/dom/scroll.js +383 -0
  148. package/src/dom/setdatainelement.d.ts +14 -0
  149. package/src/dom/setdatainelement.js +20 -0
  150. package/src/dom/tounit.d.ts +22 -0
  151. package/src/dom/tounit.js +16 -0
  152. package/src/elementreplacer.d.ts +31 -0
  153. package/src/elementreplacer.js +43 -0
  154. package/src/emittermixin.d.ts +312 -0
  155. package/src/emittermixin.js +453 -0
  156. package/src/env.d.ts +140 -0
  157. package/src/env.js +145 -0
  158. package/src/eventinfo.d.ts +58 -0
  159. package/src/eventinfo.js +26 -0
  160. package/src/fastdiff.d.ts +112 -0
  161. package/src/fastdiff.js +248 -0
  162. package/src/first.d.ts +11 -0
  163. package/src/first.js +17 -0
  164. package/src/focustracker.d.ts +79 -0
  165. package/src/focustracker.js +101 -0
  166. package/src/index.d.ts +66 -0
  167. package/src/index.js +65 -0
  168. package/src/inserttopriorityarray.d.ts +30 -0
  169. package/src/inserttopriorityarray.js +21 -0
  170. package/src/isiterable.d.ts +14 -0
  171. package/src/isiterable.js +16 -0
  172. package/src/keyboard.d.ts +126 -0
  173. package/src/keyboard.js +252 -0
  174. package/src/keystrokehandler.d.ts +97 -0
  175. package/src/keystrokehandler.js +122 -0
  176. package/src/language.d.ts +17 -0
  177. package/src/language.js +20 -0
  178. package/src/locale.d.ts +141 -0
  179. package/src/locale.js +78 -0
  180. package/src/mapsequal.d.ts +15 -0
  181. package/src/mapsequal.js +27 -0
  182. package/src/mix.d.ts +85 -0
  183. package/src/mix.js +50 -0
  184. package/src/nth.d.ts +16 -0
  185. package/src/nth.js +24 -0
  186. package/src/objecttomap.d.ts +23 -0
  187. package/src/objecttomap.js +27 -0
  188. package/src/observablemixin.d.ts +560 -0
  189. package/src/observablemixin.js +580 -0
  190. package/src/parsebase64encodedobject.d.ts +11 -0
  191. package/src/parsebase64encodedobject.js +22 -0
  192. package/src/priorities.d.ts +33 -0
  193. package/src/priorities.js +23 -0
  194. package/src/retry.d.ts +33 -0
  195. package/src/retry.js +47 -0
  196. package/src/splicearray.d.ts +26 -0
  197. package/src/splicearray.js +40 -0
  198. package/src/spy.d.ts +21 -0
  199. package/src/spy.js +22 -0
  200. package/src/toarray.d.ts +25 -0
  201. package/src/toarray.js +7 -0
  202. package/src/tomap.d.ts +19 -0
  203. package/src/tomap.js +29 -0
  204. package/src/translation-service.d.ts +174 -0
  205. package/src/translation-service.js +209 -0
  206. package/src/uid.d.ts +15 -0
  207. package/src/uid.js +45 -0
  208. package/src/unicode.d.ts +54 -0
  209. package/src/unicode.js +85 -0
  210. package/src/version.d.ts +10 -0
  211. package/src/version.js +179 -0
  212. package/src/wait.d.ts +16 -0
  213. package/src/wait.js +29 -0
@@ -0,0 +1,252 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ import CKEditorError from './ckeditorerror.js';
6
+ import env from './env.js';
7
+ const modifiersToGlyphsMac = {
8
+ ctrl: '⌃',
9
+ cmd: '⌘',
10
+ alt: '⌥',
11
+ shift: '⇧'
12
+ };
13
+ const modifiersToGlyphsNonMac = {
14
+ ctrl: 'Ctrl+',
15
+ alt: 'Alt+',
16
+ shift: 'Shift+'
17
+ };
18
+ const keyCodesToGlyphs = {
19
+ 37: '←',
20
+ 38: '↑',
21
+ 39: '→',
22
+ 40: '↓',
23
+ 9: '⇥',
24
+ 33: 'Page Up',
25
+ 34: 'Page Down'
26
+ };
27
+ /**
28
+ * An object with `keyName => keyCode` pairs for a set of known keys.
29
+ *
30
+ * Contains:
31
+ *
32
+ * * `a-z`,
33
+ * * `0-9`,
34
+ * * `f1-f12`,
35
+ * * `` ` ``, `-`, `=`, `[`, `]`, `;`, `'`, `,`, `.`, `/`, `\`,
36
+ * * `arrow(left|up|right|bottom)`,
37
+ * * `backspace`, `delete`, `enter`, `esc`, `tab`,
38
+ * * `ctrl`, `cmd`, `shift`, `alt`.
39
+ */
40
+ export const keyCodes = /* #__PURE__ */ generateKnownKeyCodes();
41
+ const keyCodeNames = /* #__PURE__ */ Object.fromEntries(
42
+ /* #__PURE__ */ Object.entries(keyCodes).map(([name, code]) => {
43
+ let prettyKeyName;
44
+ if (code in keyCodesToGlyphs) {
45
+ prettyKeyName = keyCodesToGlyphs[code];
46
+ }
47
+ else {
48
+ prettyKeyName = name.charAt(0).toUpperCase() + name.slice(1);
49
+ }
50
+ return [code, prettyKeyName];
51
+ }));
52
+ /**
53
+ * Converts a key name or {@link module:utils/keyboard~KeystrokeInfo keystroke info} into a key code.
54
+ *
55
+ * Note: Key names are matched with {@link module:utils/keyboard#keyCodes} in a case-insensitive way.
56
+ *
57
+ * @param key A key name (see {@link module:utils/keyboard#keyCodes}) or a keystroke data object.
58
+ * @returns Key or keystroke code.
59
+ */
60
+ export function getCode(key) {
61
+ let keyCode;
62
+ if (typeof key == 'string') {
63
+ keyCode = keyCodes[key.toLowerCase()];
64
+ if (!keyCode) {
65
+ /**
66
+ * Unknown key name. Only key names included in the {@link module:utils/keyboard#keyCodes} can be used.
67
+ *
68
+ * @error keyboard-unknown-key
69
+ * @param {String} key
70
+ */
71
+ throw new CKEditorError('keyboard-unknown-key', null, { key });
72
+ }
73
+ }
74
+ else {
75
+ keyCode = key.keyCode +
76
+ (key.altKey ? keyCodes.alt : 0) +
77
+ (key.ctrlKey ? keyCodes.ctrl : 0) +
78
+ (key.shiftKey ? keyCodes.shift : 0) +
79
+ (key.metaKey ? keyCodes.cmd : 0);
80
+ }
81
+ return keyCode;
82
+ }
83
+ /**
84
+ * Parses the keystroke and returns a keystroke code that will match the code returned by
85
+ * {@link module:utils/keyboard~getCode} for the corresponding {@link module:utils/keyboard~KeystrokeInfo keystroke info}.
86
+ *
87
+ * The keystroke can be passed in two formats:
88
+ *
89
+ * * as a single string – e.g. `ctrl + A`,
90
+ * * as an array of {@link module:utils/keyboard~keyCodes known key names} and key codes – e.g.:
91
+ * * `[ 'ctrl', 32 ]` (ctrl + space),
92
+ * * `[ 'ctrl', 'a' ]` (ctrl + A).
93
+ *
94
+ * Note: Key names are matched with {@link module:utils/keyboard#keyCodes} in a case-insensitive way.
95
+ *
96
+ * Note: Only keystrokes with a single non-modifier key are supported (e.g. `ctrl+A` is OK, but `ctrl+A+B` is not).
97
+ *
98
+ * Note: On macOS, keystroke handling is translating the `Ctrl` key to the `Cmd` key and handling only that keystroke.
99
+ * For example, a registered keystroke `Ctrl+A` will be translated to `Cmd+A` on macOS. To disable the translation of some keystroke,
100
+ * use the forced modifier: `Ctrl!+A` (note the exclamation mark).
101
+ *
102
+ * @param keystroke The keystroke definition.
103
+ * @returns Keystroke code.
104
+ */
105
+ export function parseKeystroke(keystroke) {
106
+ if (typeof keystroke == 'string') {
107
+ keystroke = splitKeystrokeText(keystroke);
108
+ }
109
+ return keystroke
110
+ .map(key => (typeof key == 'string') ? getEnvKeyCode(key) : key)
111
+ .reduce((key, sum) => sum + key, 0);
112
+ }
113
+ /**
114
+ * Translates any keystroke string text like `"Ctrl+A"` to an
115
+ * environment–specific keystroke, i.e. `"⌘A"` on macOS.
116
+ *
117
+ * @param keystroke The keystroke text.
118
+ * @returns The keystroke text specific for the environment.
119
+ */
120
+ export function getEnvKeystrokeText(keystroke) {
121
+ let keystrokeCode = parseKeystroke(keystroke);
122
+ const modifiersToGlyphs = Object.entries((env.isMac || env.isiOS) ? modifiersToGlyphsMac : modifiersToGlyphsNonMac);
123
+ const modifiers = modifiersToGlyphs.reduce((modifiers, [name, glyph]) => {
124
+ // Modifier keys are stored as a bit mask so extract those from the keystroke code.
125
+ if ((keystrokeCode & keyCodes[name]) != 0) {
126
+ keystrokeCode &= ~keyCodes[name];
127
+ modifiers += glyph;
128
+ }
129
+ return modifiers;
130
+ }, '');
131
+ return modifiers + (keystrokeCode ? keyCodeNames[keystrokeCode] : '');
132
+ }
133
+ /**
134
+ * Returns `true` if the provided key code represents one of the arrow keys.
135
+ *
136
+ * @param keyCode A key code as in {@link module:utils/keyboard~KeystrokeInfo#keyCode}.
137
+ */
138
+ export function isArrowKeyCode(keyCode) {
139
+ return keyCode == keyCodes.arrowright ||
140
+ keyCode == keyCodes.arrowleft ||
141
+ keyCode == keyCodes.arrowup ||
142
+ keyCode == keyCodes.arrowdown;
143
+ }
144
+ /**
145
+ * Returns the direction in which the {@link module:engine/model/documentselection~DocumentSelection selection}
146
+ * will move when the provided arrow key code is pressed considering the language direction of the editor content.
147
+ *
148
+ * For instance, in right–to–left (RTL) content languages, pressing the left arrow means moving the selection right (forward)
149
+ * in the model structure. Similarly, pressing the right arrow moves the selection left (backward).
150
+ *
151
+ * @param keyCode A key code as in {@link module:utils/keyboard~KeystrokeInfo#keyCode}.
152
+ * @param contentLanguageDirection The content language direction, corresponding to
153
+ * {@link module:utils/locale~Locale#contentLanguageDirection}.
154
+ * @returns Localized arrow direction or `undefined` for non-arrow key codes.
155
+ */
156
+ export function getLocalizedArrowKeyCodeDirection(keyCode, contentLanguageDirection) {
157
+ const isLtrContent = contentLanguageDirection === 'ltr';
158
+ switch (keyCode) {
159
+ case keyCodes.arrowleft:
160
+ return isLtrContent ? 'left' : 'right';
161
+ case keyCodes.arrowright:
162
+ return isLtrContent ? 'right' : 'left';
163
+ case keyCodes.arrowup:
164
+ return 'up';
165
+ case keyCodes.arrowdown:
166
+ return 'down';
167
+ }
168
+ }
169
+ /**
170
+ * Converts a key name to the key code with mapping based on the env.
171
+ *
172
+ * See: {@link module:utils/keyboard~getCode}.
173
+ *
174
+ * @param key The key name (see {@link module:utils/keyboard#keyCodes}).
175
+ * @returns Key code.
176
+ */
177
+ function getEnvKeyCode(key) {
178
+ // Don't remap modifier key for forced modifiers.
179
+ if (key.endsWith('!')) {
180
+ return getCode(key.slice(0, -1));
181
+ }
182
+ const code = getCode(key);
183
+ return (env.isMac || env.isiOS) && code == keyCodes.ctrl ? keyCodes.cmd : code;
184
+ }
185
+ /**
186
+ * Determines if the provided key code moves the {@link module:engine/model/documentselection~DocumentSelection selection}
187
+ * forward or backward considering the language direction of the editor content.
188
+ *
189
+ * For instance, in right–to–left (RTL) languages, pressing the left arrow means moving forward
190
+ * in the model structure. Similarly, pressing the right arrow moves the selection backward.
191
+ *
192
+ * @param keyCode A key code as in {@link module:utils/keyboard~KeystrokeInfo#keyCode}.
193
+ * @param contentLanguageDirection The content language direction, corresponding to
194
+ * {@link module:utils/locale~Locale#contentLanguageDirection}.
195
+ */
196
+ export function isForwardArrowKeyCode(keyCode, contentLanguageDirection) {
197
+ const localizedKeyCodeDirection = getLocalizedArrowKeyCodeDirection(keyCode, contentLanguageDirection);
198
+ return localizedKeyCodeDirection === 'down' || localizedKeyCodeDirection === 'right';
199
+ }
200
+ function generateKnownKeyCodes() {
201
+ const keyCodes = {
202
+ pageup: 33,
203
+ pagedown: 34,
204
+ arrowleft: 37,
205
+ arrowup: 38,
206
+ arrowright: 39,
207
+ arrowdown: 40,
208
+ backspace: 8,
209
+ delete: 46,
210
+ enter: 13,
211
+ space: 32,
212
+ esc: 27,
213
+ tab: 9,
214
+ // The idea about these numbers is that they do not collide with any real key codes, so we can use them
215
+ // like bit masks.
216
+ ctrl: 0x110000,
217
+ shift: 0x220000,
218
+ alt: 0x440000,
219
+ cmd: 0x880000
220
+ };
221
+ // a-z
222
+ for (let code = 65; code <= 90; code++) {
223
+ const letter = String.fromCharCode(code);
224
+ keyCodes[letter.toLowerCase()] = code;
225
+ }
226
+ // 0-9
227
+ for (let code = 48; code <= 57; code++) {
228
+ keyCodes[code - 48] = code;
229
+ }
230
+ // F1-F12
231
+ for (let code = 112; code <= 123; code++) {
232
+ keyCodes['f' + (code - 111)] = code;
233
+ }
234
+ // other characters
235
+ Object.assign(keyCodes, {
236
+ '\'': 222,
237
+ ',': 108,
238
+ '-': 109,
239
+ '.': 110,
240
+ '/': 111,
241
+ ';': 186,
242
+ '=': 187,
243
+ '[': 219,
244
+ '\\': 220,
245
+ ']': 221,
246
+ '`': 223
247
+ });
248
+ return keyCodes;
249
+ }
250
+ function splitKeystrokeText(keystroke) {
251
+ return keystroke.split('+').map(key => key.trim());
252
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ import type { Emitter } from './emittermixin.js';
6
+ import { type KeystrokeInfo } from './keyboard.js';
7
+ import type { PriorityString } from './priorities.js';
8
+ /**
9
+ * Keystroke handler allows registering callbacks for given keystrokes.
10
+ *
11
+ * The most frequent use of this class is through the {@link module:core/editor/editor~Editor#keystrokes `editor.keystrokes`}
12
+ * property. It allows listening to keystrokes executed in the editing view:
13
+ *
14
+ * ```ts
15
+ * editor.keystrokes.set( 'Ctrl+A', ( keyEvtData, cancel ) => {
16
+ * console.log( 'Ctrl+A has been pressed' );
17
+ * cancel();
18
+ * } );
19
+ * ```
20
+ *
21
+ * However, this utility class can be used in various part of the UI. For instance, a certain {@link module:ui/view~View}
22
+ * can use it like this:
23
+ *
24
+ * ```ts
25
+ * class MyView extends View {
26
+ * constructor() {
27
+ * this.keystrokes = new KeystrokeHandler();
28
+ *
29
+ * this.keystrokes.set( 'tab', handleTabKey );
30
+ * }
31
+ *
32
+ * render() {
33
+ * super.render();
34
+ *
35
+ * this.keystrokes.listenTo( this.element );
36
+ * }
37
+ * }
38
+ * ```
39
+ *
40
+ * That keystroke handler will listen to `keydown` events fired in this view's main element.
41
+ *
42
+ */
43
+ export default class KeystrokeHandler {
44
+ /**
45
+ * Listener used to listen to events for easier keystroke handler destruction.
46
+ */
47
+ private readonly _listener;
48
+ /**
49
+ * Creates an instance of the keystroke handler.
50
+ */
51
+ constructor();
52
+ /**
53
+ * Starts listening for `keydown` events from a given emitter.
54
+ */
55
+ listenTo(emitter: Emitter | HTMLElement | Window): void;
56
+ /**
57
+ * Registers a handler for the specified keystroke.
58
+ *
59
+ * @param keystroke Keystroke defined in a format accepted by
60
+ * the {@link module:utils/keyboard~parseKeystroke} function.
61
+ * @param callback A function called with the
62
+ * {@link module:engine/view/observer/keyobserver~KeyEventData key event data} object and
63
+ * a helper function to call both `preventDefault()` and `stopPropagation()` on the underlying event.
64
+ * @param options Additional options.
65
+ */
66
+ set(keystroke: string | ReadonlyArray<string | number>, callback: (ev: KeyboardEvent, cancel: () => void) => void, options?: KeystrokeHandlerOptions): void;
67
+ /**
68
+ * Triggers a keystroke handler for a specified key combination, if such a keystroke was {@link #set defined}.
69
+ *
70
+ * @param keyEvtData Key event data.
71
+ * @returns Whether the keystroke was handled.
72
+ */
73
+ press(keyEvtData: Readonly<KeystrokeInfo>): boolean;
74
+ /**
75
+ * Stops listening to `keydown` events from the given emitter.
76
+ */
77
+ stopListening(emitter?: Emitter | HTMLElement | Window): void;
78
+ /**
79
+ * Destroys the keystroke handler.
80
+ */
81
+ destroy(): void;
82
+ }
83
+ /**
84
+ * {@link module:utils/keystrokehandler~KeystrokeHandler#set} method options.
85
+ */
86
+ export interface KeystrokeHandlerOptions {
87
+ /**
88
+ * The priority of the keystroke callback. The higher the priority value the sooner the callback will be executed.
89
+ * Keystrokes having the same priority are called in the order they were added.
90
+ */
91
+ readonly priority?: PriorityString;
92
+ /**
93
+ * An optional callback function allowing for filtering keystrokes based on arbitrary criteria.
94
+ * The callback function receives `keydown` DOM event as a parameter.
95
+ */
96
+ readonly filter?: (keyEvtData: KeyboardEvent) => boolean;
97
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 utils/keystrokehandler
7
+ */
8
+ import DomEmitterMixin from './dom/emittermixin.js';
9
+ import { getCode, parseKeystroke } from './keyboard.js';
10
+ /**
11
+ * Keystroke handler allows registering callbacks for given keystrokes.
12
+ *
13
+ * The most frequent use of this class is through the {@link module:core/editor/editor~Editor#keystrokes `editor.keystrokes`}
14
+ * property. It allows listening to keystrokes executed in the editing view:
15
+ *
16
+ * ```ts
17
+ * editor.keystrokes.set( 'Ctrl+A', ( keyEvtData, cancel ) => {
18
+ * console.log( 'Ctrl+A has been pressed' );
19
+ * cancel();
20
+ * } );
21
+ * ```
22
+ *
23
+ * However, this utility class can be used in various part of the UI. For instance, a certain {@link module:ui/view~View}
24
+ * can use it like this:
25
+ *
26
+ * ```ts
27
+ * class MyView extends View {
28
+ * constructor() {
29
+ * this.keystrokes = new KeystrokeHandler();
30
+ *
31
+ * this.keystrokes.set( 'tab', handleTabKey );
32
+ * }
33
+ *
34
+ * render() {
35
+ * super.render();
36
+ *
37
+ * this.keystrokes.listenTo( this.element );
38
+ * }
39
+ * }
40
+ * ```
41
+ *
42
+ * That keystroke handler will listen to `keydown` events fired in this view's main element.
43
+ *
44
+ */
45
+ export default class KeystrokeHandler {
46
+ /**
47
+ * Creates an instance of the keystroke handler.
48
+ */
49
+ constructor() {
50
+ this._listener = new (DomEmitterMixin())();
51
+ }
52
+ /**
53
+ * Starts listening for `keydown` events from a given emitter.
54
+ */
55
+ listenTo(emitter) {
56
+ // The #_listener works here as a kind of dispatcher. It groups the events coming from the same
57
+ // keystroke so the listeners can be attached to them with different priorities.
58
+ //
59
+ // E.g. all the keystrokes with the `keyCode` of 42 coming from the `emitter` are propagated
60
+ // as a `_keydown:42` event by the `_listener`. If there's a callback created by the `set`
61
+ // method for this 42 keystroke, it listens to the `_listener#_keydown:42` event only and interacts
62
+ // only with other listeners of this particular event, thus making it possible to prioritize
63
+ // the listeners and safely cancel execution, when needed. Instead of duplicating the Emitter logic,
64
+ // the KeystrokeHandler re–uses it to do its job.
65
+ this._listener.listenTo(emitter, 'keydown', (evt, keyEvtData) => {
66
+ this._listener.fire('_keydown:' + getCode(keyEvtData), keyEvtData);
67
+ });
68
+ }
69
+ /**
70
+ * Registers a handler for the specified keystroke.
71
+ *
72
+ * @param keystroke Keystroke defined in a format accepted by
73
+ * the {@link module:utils/keyboard~parseKeystroke} function.
74
+ * @param callback A function called with the
75
+ * {@link module:engine/view/observer/keyobserver~KeyEventData key event data} object and
76
+ * a helper function to call both `preventDefault()` and `stopPropagation()` on the underlying event.
77
+ * @param options Additional options.
78
+ */
79
+ set(keystroke, callback, options = {}) {
80
+ const keyCode = parseKeystroke(keystroke);
81
+ const priority = options.priority;
82
+ // Execute the passed callback on KeystrokeHandler#_keydown.
83
+ // TODO: https://github.com/ckeditor/ckeditor5-utils/issues/144
84
+ this._listener.listenTo(this._listener, '_keydown:' + keyCode, (evt, keyEvtData) => {
85
+ if (options.filter && !options.filter(keyEvtData)) {
86
+ return;
87
+ }
88
+ callback(keyEvtData, () => {
89
+ // Stop the event in the DOM: no listener in the web page
90
+ // will be triggered by this event.
91
+ keyEvtData.preventDefault();
92
+ keyEvtData.stopPropagation();
93
+ // Stop the event in the KeystrokeHandler: no more callbacks
94
+ // will be executed for this keystroke.
95
+ evt.stop();
96
+ });
97
+ // Mark this keystroke as handled by the callback. See: #press.
98
+ evt.return = true;
99
+ }, { priority });
100
+ }
101
+ /**
102
+ * Triggers a keystroke handler for a specified key combination, if such a keystroke was {@link #set defined}.
103
+ *
104
+ * @param keyEvtData Key event data.
105
+ * @returns Whether the keystroke was handled.
106
+ */
107
+ press(keyEvtData) {
108
+ return !!this._listener.fire('_keydown:' + getCode(keyEvtData), keyEvtData);
109
+ }
110
+ /**
111
+ * Stops listening to `keydown` events from the given emitter.
112
+ */
113
+ stopListening(emitter) {
114
+ this._listener.stopListening(emitter);
115
+ }
116
+ /**
117
+ * Destroys the keystroke handler.
118
+ */
119
+ destroy() {
120
+ this.stopListening();
121
+ }
122
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 utils/language
7
+ */
8
+ /**
9
+ * String representing a language direction.
10
+ */
11
+ export type LanguageDirection = 'ltr' | 'rtl';
12
+ /**
13
+ * Helps determine whether a language text direction is LTR or RTL.
14
+ *
15
+ * @param languageCode The ISO 639-1 or ISO 639-2 language code.
16
+ */
17
+ export declare function getLanguageDirection(languageCode: string): LanguageDirection;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ const RTL_LANGUAGE_CODES = [
6
+ 'ar', 'ara',
7
+ 'dv', 'div',
8
+ 'fa', 'per', 'fas',
9
+ 'he', 'heb',
10
+ 'ku', 'kur',
11
+ 'ug', 'uig' // Uighur, Uyghur
12
+ ];
13
+ /**
14
+ * Helps determine whether a language text direction is LTR or RTL.
15
+ *
16
+ * @param languageCode The ISO 639-1 or ISO 639-2 language code.
17
+ */
18
+ export function getLanguageDirection(languageCode) {
19
+ return RTL_LANGUAGE_CODES.includes(languageCode) ? 'rtl' : 'ltr';
20
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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 utils/locale
7
+ */
8
+ import { type ArrayOrItem } from './toarray.js';
9
+ import { type Message } from './translation-service.js';
10
+ import { type LanguageDirection } from './language.js';
11
+ /**
12
+ * Represents the localization services.
13
+ */
14
+ export default class Locale {
15
+ /**
16
+ * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
17
+ *
18
+ * If the {@link #contentLanguage content language} was not specified in the `Locale` constructor,
19
+ * it also defines the language of the content.
20
+ */
21
+ readonly uiLanguage: string;
22
+ /**
23
+ * Text direction of the {@link #uiLanguage editor UI language}. Either `'ltr'` or `'rtl'`.
24
+ */
25
+ readonly uiLanguageDirection: LanguageDirection;
26
+ /**
27
+ * The editor content language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
28
+ *
29
+ * Usually the same as the {@link #uiLanguage editor language}, it can be customized by passing an optional
30
+ * argument to the `Locale` constructor.
31
+ */
32
+ readonly contentLanguage: string;
33
+ /**
34
+ * Text direction of the {@link #contentLanguage editor content language}.
35
+ *
36
+ * If the content language was passed directly to the `Locale` constructor, this property represents the
37
+ * direction of that language.
38
+ *
39
+ * If the {@link #contentLanguage editor content language} was derived from the {@link #uiLanguage editor language},
40
+ * the content language direction is the same as the {@link #uiLanguageDirection UI language direction}.
41
+ *
42
+ * The value is either `'ltr'` or `'rtl'`.
43
+ */
44
+ readonly contentLanguageDirection: LanguageDirection;
45
+ /**
46
+ * Translates the given message to the {@link #uiLanguage}. This method is also available in
47
+ * {@link module:core/editor/editor~Editor#t `Editor`} and {@link module:ui/view~View#t `View`}.
48
+ *
49
+ * This method's context is statically bound to the `Locale` instance and **should always be called as a function**:
50
+ *
51
+ * ```ts
52
+ * const t = locale.t;
53
+ * t( 'Label' );
54
+ * ```
55
+ *
56
+ * The message can be either a string or an object implementing the {@link module:utils/translation-service~Message} interface.
57
+ *
58
+ * The message may contain placeholders (`%<index>`) for value(s) that are passed as a `values` parameter.
59
+ * For an array of values, the `%<index>` will be changed to an element of that array at the given index.
60
+ * For a single value passed as the second argument, only the `%0` placeholders will be changed to the provided value.
61
+ *
62
+ * ```ts
63
+ * t( 'Created file "%0" in %1ms.', [ fileName, timeTaken ] );
64
+ * t( 'Created file "%0", fileName );
65
+ * ```
66
+ *
67
+ * The message supports plural forms. To specify the plural form, use the `plural` property. Singular or plural form
68
+ * will be chosen depending on the first value from the passed `values`. The value of the `plural` property is used
69
+ * as a default plural translation when the translation for the target language is missing.
70
+ *
71
+ * ```ts
72
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Add a space' for the English language.
73
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Add 5 spaces' for the English language.
74
+ * t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Add 2 spaces' for the English language.
75
+ *
76
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 1 ); // 'Dodaj spację' for the Polish language.
77
+ * t( { string: 'Add a space', plural: 'Add %0 spaces' }, 5 ); // 'Dodaj 5 spacji' for the Polish language.
78
+ * t( { string: '%1 a space', plural: '%1 %0 spaces' }, [ 2, 'Add' ] ); // 'Dodaj 2 spacje' for the Polish language.
79
+ * ```
80
+ *
81
+ * * The message should provide an ID using the `id` property when the message strings are not unique and their
82
+ * translations should be different.
83
+ *
84
+ * ```ts
85
+ * translate( 'en', { string: 'image', id: 'ADD_IMAGE' } );
86
+ * translate( 'en', { string: 'image', id: 'AN_IMAGE' } );
87
+ * ```
88
+ */
89
+ readonly t: LocaleTranslate;
90
+ /**
91
+ * Object that contains translations.
92
+ */
93
+ translations?: Translations;
94
+ /**
95
+ * Creates a new instance of the locale class. Learn more about
96
+ * {@glink getting-started/setup/ui-language configuring the language of the editor}.
97
+ *
98
+ * @param options Locale configuration.
99
+ * @param options.uiLanguage The editor UI language code in the
100
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. See {@link #uiLanguage}.
101
+ * @param options.contentLanguage The editor content language code in the
102
+ * [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format. If not specified, the same as `options.language`.
103
+ * See {@link #contentLanguage}.
104
+ * @param translations Translations passed as a editor config parameter.
105
+ */
106
+ constructor({ uiLanguage, contentLanguage, translations }?: {
107
+ readonly uiLanguage?: string;
108
+ readonly contentLanguage?: string;
109
+ readonly translations?: ArrayOrItem<Translations>;
110
+ });
111
+ /**
112
+ * The editor UI language code in the [ISO 639-1](https://en.wikipedia.org/wiki/ISO_639-1) format.
113
+ *
114
+ * **Note**: This property was deprecated. Please use {@link #uiLanguage} and {@link #contentLanguage}
115
+ * properties instead.
116
+ *
117
+ * @deprecated
118
+ */
119
+ get language(): string;
120
+ /**
121
+ * An unbound version of the {@link #t} method.
122
+ */
123
+ private _t;
124
+ }
125
+ /**
126
+ * @param message A message that will be localized (translated).
127
+ * @param values A value or an array of values that will fill message placeholders.
128
+ * For messages supporting plural forms the first value will determine the plural form.
129
+ */
130
+ export type LocaleTranslate = (message: string | Message, values?: number | string | ReadonlyArray<number | string>) => string;
131
+ /**
132
+ * Translations object definition.
133
+ */
134
+ export type Translations = {
135
+ [language: string]: {
136
+ dictionary: {
137
+ [messageId: string]: string | ReadonlyArray<string>;
138
+ };
139
+ getPluralForm?: ((n: number) => number | boolean) | null;
140
+ };
141
+ };