@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,138 +1,138 @@
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
- import type { Batch, Model, Range } from '@ckeditor/ckeditor5-engine';
6
- declare const TextWatcher_base: {
7
- new (): import("@ckeditor/ckeditor5-utils").Observable;
8
- prototype: import("@ckeditor/ckeditor5-utils").Observable;
9
- };
10
- /**
11
- * The text watcher feature.
12
- *
13
- * Fires the {@link module:typing/textwatcher~TextWatcher#event:matched:data `matched:data`},
14
- * {@link module:typing/textwatcher~TextWatcher#event:matched:selection `matched:selection`} and
15
- * {@link module:typing/textwatcher~TextWatcher#event:unmatched `unmatched`} events on typing or selection changes.
16
- */
17
- export default class TextWatcher extends TextWatcher_base {
18
- /**
19
- * The editor's model.
20
- */
21
- readonly model: Model;
22
- /**
23
- * The function used to match the text.
24
- *
25
- * The test callback can return 3 values:
26
- *
27
- * * `false` if there is no match,
28
- * * `true` if there is a match,
29
- * * an object if there is a match and we want to pass some additional information to the {@link #event:matched:data} event.
30
- */
31
- testCallback: (text: string) => unknown;
32
- /**
33
- * Whether there is a match currently.
34
- */
35
- private _hasMatch;
36
- /**
37
- * Flag indicating whether the `TextWatcher` instance is enabled or disabled.
38
- * A disabled TextWatcher will not evaluate text.
39
- *
40
- * To disable TextWatcher:
41
- *
42
- * ```ts
43
- * const watcher = new TextWatcher( editor.model, testCallback );
44
- *
45
- * // After this a testCallback will not be called.
46
- * watcher.isEnabled = false;
47
- * ```
48
- */
49
- isEnabled: boolean;
50
- /**
51
- * Creates a text watcher instance.
52
- *
53
- * @param testCallback See {@link module:typing/textwatcher~TextWatcher#testCallback}.
54
- */
55
- constructor(model: Model, testCallback: (text: string) => unknown);
56
- /**
57
- * Flag indicating whether there is a match currently.
58
- */
59
- get hasMatch(): boolean;
60
- /**
61
- * Starts listening to the editor for typing and selection events.
62
- */
63
- private _startListening;
64
- /**
65
- * Checks the editor content for matched text.
66
- *
67
- * @fires matched:data
68
- * @fires matched:selection
69
- * @fires unmatched
70
- *
71
- * @param suffix A suffix used for generating the event name.
72
- * @param data Data object for event.
73
- */
74
- private _evaluateTextBeforeSelection;
75
- }
76
- export type TextWatcherMatchedEvent<TCallbackResult extends Record<string, unknown> = Record<string, unknown>> = {
77
- name: 'matched' | 'matched:data' | 'matched:selection';
78
- args: [
79
- {
80
- text: string;
81
- range: Range;
82
- batch?: Batch;
83
- } & TCallbackResult
84
- ];
85
- };
86
- /**
87
- * Fired whenever the text watcher found a match for data changes.
88
- *
89
- * @eventName ~TextWatcher#matched:data
90
- * @param data Event data.
91
- * @param data.testResult The additional data returned from the {@link module:typing/textwatcher~TextWatcher#testCallback}.
92
- */
93
- export type TextWatcherMatchedDataEvent<TCallbackResult extends Record<string, unknown>> = {
94
- name: 'matched:data';
95
- args: [data: TextWatcherMatchedDataEventData & TCallbackResult];
96
- };
97
- export interface TextWatcherMatchedDataEventData {
98
- /**
99
- * The full text before selection to which the regexp was applied.
100
- */
101
- text: string;
102
- /**
103
- * The range representing the position of the `data.text`.
104
- */
105
- range: Range;
106
- batch: Batch;
107
- }
108
- /**
109
- * Fired whenever the text watcher found a match for selection changes.
110
- *
111
- * @eventName ~TextWatcher#matched:selection
112
- * @param data Event data.
113
- * @param data.testResult The additional data returned from the {@link module:typing/textwatcher~TextWatcher#testCallback}.
114
- */
115
- export type TextWatcherMatchedSelectionEvent<TCallbackResult extends Record<string, unknown>> = {
116
- name: 'matched:selection';
117
- args: [data: TextWatcherMatchedSelectionEventData & TCallbackResult];
118
- };
119
- export interface TextWatcherMatchedSelectionEventData {
120
- /**
121
- * The full text before selection.
122
- */
123
- text: string;
124
- /**
125
- * The range representing the position of the `data.text`.
126
- */
127
- range: Range;
128
- }
129
- /**
130
- * Fired whenever the text does not match anymore. Fired only when the text watcher found a match.
131
- *
132
- * @eventName ~TextWatcher#unmatched
133
- */
134
- export type TextWatcherUnmatchedEvent = {
135
- name: 'unmatched';
136
- args: [];
137
- };
138
- export {};
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
+ import type { Batch, Model, Range } from '@ckeditor/ckeditor5-engine';
6
+ declare const TextWatcher_base: {
7
+ new (): import("@ckeditor/ckeditor5-utils").Observable;
8
+ prototype: import("@ckeditor/ckeditor5-utils").Observable;
9
+ };
10
+ /**
11
+ * The text watcher feature.
12
+ *
13
+ * Fires the {@link module:typing/textwatcher~TextWatcher#event:matched:data `matched:data`},
14
+ * {@link module:typing/textwatcher~TextWatcher#event:matched:selection `matched:selection`} and
15
+ * {@link module:typing/textwatcher~TextWatcher#event:unmatched `unmatched`} events on typing or selection changes.
16
+ */
17
+ export default class TextWatcher extends TextWatcher_base {
18
+ /**
19
+ * The editor's model.
20
+ */
21
+ readonly model: Model;
22
+ /**
23
+ * The function used to match the text.
24
+ *
25
+ * The test callback can return 3 values:
26
+ *
27
+ * * `false` if there is no match,
28
+ * * `true` if there is a match,
29
+ * * an object if there is a match and we want to pass some additional information to the {@link #event:matched:data} event.
30
+ */
31
+ testCallback: (text: string) => unknown;
32
+ /**
33
+ * Whether there is a match currently.
34
+ */
35
+ private _hasMatch;
36
+ /**
37
+ * Flag indicating whether the `TextWatcher` instance is enabled or disabled.
38
+ * A disabled TextWatcher will not evaluate text.
39
+ *
40
+ * To disable TextWatcher:
41
+ *
42
+ * ```ts
43
+ * const watcher = new TextWatcher( editor.model, testCallback );
44
+ *
45
+ * // After this a testCallback will not be called.
46
+ * watcher.isEnabled = false;
47
+ * ```
48
+ */
49
+ isEnabled: boolean;
50
+ /**
51
+ * Creates a text watcher instance.
52
+ *
53
+ * @param testCallback See {@link module:typing/textwatcher~TextWatcher#testCallback}.
54
+ */
55
+ constructor(model: Model, testCallback: (text: string) => unknown);
56
+ /**
57
+ * Flag indicating whether there is a match currently.
58
+ */
59
+ get hasMatch(): boolean;
60
+ /**
61
+ * Starts listening to the editor for typing and selection events.
62
+ */
63
+ private _startListening;
64
+ /**
65
+ * Checks the editor content for matched text.
66
+ *
67
+ * @fires matched:data
68
+ * @fires matched:selection
69
+ * @fires unmatched
70
+ *
71
+ * @param suffix A suffix used for generating the event name.
72
+ * @param data Data object for event.
73
+ */
74
+ private _evaluateTextBeforeSelection;
75
+ }
76
+ export type TextWatcherMatchedEvent<TCallbackResult extends Record<string, unknown> = Record<string, unknown>> = {
77
+ name: 'matched' | 'matched:data' | 'matched:selection';
78
+ args: [
79
+ {
80
+ text: string;
81
+ range: Range;
82
+ batch?: Batch;
83
+ } & TCallbackResult
84
+ ];
85
+ };
86
+ /**
87
+ * Fired whenever the text watcher found a match for data changes.
88
+ *
89
+ * @eventName ~TextWatcher#matched:data
90
+ * @param data Event data.
91
+ * @param data.testResult The additional data returned from the {@link module:typing/textwatcher~TextWatcher#testCallback}.
92
+ */
93
+ export type TextWatcherMatchedDataEvent<TCallbackResult extends Record<string, unknown>> = {
94
+ name: 'matched:data';
95
+ args: [data: TextWatcherMatchedDataEventData & TCallbackResult];
96
+ };
97
+ export interface TextWatcherMatchedDataEventData {
98
+ /**
99
+ * The full text before selection to which the regexp was applied.
100
+ */
101
+ text: string;
102
+ /**
103
+ * The range representing the position of the `data.text`.
104
+ */
105
+ range: Range;
106
+ batch: Batch;
107
+ }
108
+ /**
109
+ * Fired whenever the text watcher found a match for selection changes.
110
+ *
111
+ * @eventName ~TextWatcher#matched:selection
112
+ * @param data Event data.
113
+ * @param data.testResult The additional data returned from the {@link module:typing/textwatcher~TextWatcher#testCallback}.
114
+ */
115
+ export type TextWatcherMatchedSelectionEvent<TCallbackResult extends Record<string, unknown>> = {
116
+ name: 'matched:selection';
117
+ args: [data: TextWatcherMatchedSelectionEventData & TCallbackResult];
118
+ };
119
+ export interface TextWatcherMatchedSelectionEventData {
120
+ /**
121
+ * The full text before selection.
122
+ */
123
+ text: string;
124
+ /**
125
+ * The range representing the position of the `data.text`.
126
+ */
127
+ range: Range;
128
+ }
129
+ /**
130
+ * Fired whenever the text does not match anymore. Fired only when the text watcher found a match.
131
+ *
132
+ * @eventName ~TextWatcher#unmatched
133
+ */
134
+ export type TextWatcherUnmatchedEvent = {
135
+ name: 'unmatched';
136
+ args: [];
137
+ };
138
+ export {};
@@ -1,105 +1,105 @@
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/textwatcher
7
- */
8
- import { ObservableMixin } from '@ckeditor/ckeditor5-utils';
9
- import getLastTextLine from './utils/getlasttextline';
10
- /**
11
- * The text watcher feature.
12
- *
13
- * Fires the {@link module:typing/textwatcher~TextWatcher#event:matched:data `matched:data`},
14
- * {@link module:typing/textwatcher~TextWatcher#event:matched:selection `matched:selection`} and
15
- * {@link module:typing/textwatcher~TextWatcher#event:unmatched `unmatched`} events on typing or selection changes.
16
- */
17
- export default class TextWatcher extends ObservableMixin() {
18
- /**
19
- * Creates a text watcher instance.
20
- *
21
- * @param testCallback See {@link module:typing/textwatcher~TextWatcher#testCallback}.
22
- */
23
- constructor(model, testCallback) {
24
- super();
25
- this.model = model;
26
- this.testCallback = testCallback;
27
- this._hasMatch = false;
28
- this.set('isEnabled', true);
29
- // Toggle text watching on isEnabled state change.
30
- this.on('change:isEnabled', () => {
31
- if (this.isEnabled) {
32
- this._startListening();
33
- }
34
- else {
35
- this.stopListening(model.document.selection);
36
- this.stopListening(model.document);
37
- }
38
- });
39
- this._startListening();
40
- }
41
- /**
42
- * Flag indicating whether there is a match currently.
43
- */
44
- get hasMatch() {
45
- return this._hasMatch;
46
- }
47
- /**
48
- * Starts listening to the editor for typing and selection events.
49
- */
50
- _startListening() {
51
- const model = this.model;
52
- const document = model.document;
53
- this.listenTo(document.selection, 'change:range', (evt, { directChange }) => {
54
- // Indirect changes (i.e. when the user types or external changes are applied) are handled in the document's change event.
55
- if (!directChange) {
56
- return;
57
- }
58
- // Act only on collapsed selection.
59
- if (!document.selection.isCollapsed) {
60
- if (this.hasMatch) {
61
- this.fire('unmatched');
62
- this._hasMatch = false;
63
- }
64
- return;
65
- }
66
- this._evaluateTextBeforeSelection('selection');
67
- });
68
- this.listenTo(document, 'change:data', (evt, batch) => {
69
- if (batch.isUndo || !batch.isLocal) {
70
- return;
71
- }
72
- this._evaluateTextBeforeSelection('data', { batch });
73
- });
74
- }
75
- /**
76
- * Checks the editor content for matched text.
77
- *
78
- * @fires matched:data
79
- * @fires matched:selection
80
- * @fires unmatched
81
- *
82
- * @param suffix A suffix used for generating the event name.
83
- * @param data Data object for event.
84
- */
85
- _evaluateTextBeforeSelection(suffix, data = {}) {
86
- const model = this.model;
87
- const document = model.document;
88
- const selection = document.selection;
89
- const rangeBeforeSelection = model.createRange(model.createPositionAt(selection.focus.parent, 0), selection.focus);
90
- const { text, range } = getLastTextLine(rangeBeforeSelection, model);
91
- const testResult = this.testCallback(text);
92
- if (!testResult && this.hasMatch) {
93
- this.fire('unmatched');
94
- }
95
- this._hasMatch = !!testResult;
96
- if (testResult) {
97
- const eventData = Object.assign(data, { text, range });
98
- // If the test callback returns an object with additional data, assign the data as well.
99
- if (typeof testResult == 'object') {
100
- Object.assign(eventData, testResult);
101
- }
102
- this.fire(`matched:${suffix}`, eventData);
103
- }
104
- }
105
- }
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/textwatcher
7
+ */
8
+ import { ObservableMixin } from '@ckeditor/ckeditor5-utils';
9
+ import getLastTextLine from './utils/getlasttextline';
10
+ /**
11
+ * The text watcher feature.
12
+ *
13
+ * Fires the {@link module:typing/textwatcher~TextWatcher#event:matched:data `matched:data`},
14
+ * {@link module:typing/textwatcher~TextWatcher#event:matched:selection `matched:selection`} and
15
+ * {@link module:typing/textwatcher~TextWatcher#event:unmatched `unmatched`} events on typing or selection changes.
16
+ */
17
+ export default class TextWatcher extends ObservableMixin() {
18
+ /**
19
+ * Creates a text watcher instance.
20
+ *
21
+ * @param testCallback See {@link module:typing/textwatcher~TextWatcher#testCallback}.
22
+ */
23
+ constructor(model, testCallback) {
24
+ super();
25
+ this.model = model;
26
+ this.testCallback = testCallback;
27
+ this._hasMatch = false;
28
+ this.set('isEnabled', true);
29
+ // Toggle text watching on isEnabled state change.
30
+ this.on('change:isEnabled', () => {
31
+ if (this.isEnabled) {
32
+ this._startListening();
33
+ }
34
+ else {
35
+ this.stopListening(model.document.selection);
36
+ this.stopListening(model.document);
37
+ }
38
+ });
39
+ this._startListening();
40
+ }
41
+ /**
42
+ * Flag indicating whether there is a match currently.
43
+ */
44
+ get hasMatch() {
45
+ return this._hasMatch;
46
+ }
47
+ /**
48
+ * Starts listening to the editor for typing and selection events.
49
+ */
50
+ _startListening() {
51
+ const model = this.model;
52
+ const document = model.document;
53
+ this.listenTo(document.selection, 'change:range', (evt, { directChange }) => {
54
+ // Indirect changes (i.e. when the user types or external changes are applied) are handled in the document's change event.
55
+ if (!directChange) {
56
+ return;
57
+ }
58
+ // Act only on collapsed selection.
59
+ if (!document.selection.isCollapsed) {
60
+ if (this.hasMatch) {
61
+ this.fire('unmatched');
62
+ this._hasMatch = false;
63
+ }
64
+ return;
65
+ }
66
+ this._evaluateTextBeforeSelection('selection');
67
+ });
68
+ this.listenTo(document, 'change:data', (evt, batch) => {
69
+ if (batch.isUndo || !batch.isLocal) {
70
+ return;
71
+ }
72
+ this._evaluateTextBeforeSelection('data', { batch });
73
+ });
74
+ }
75
+ /**
76
+ * Checks the editor content for matched text.
77
+ *
78
+ * @fires matched:data
79
+ * @fires matched:selection
80
+ * @fires unmatched
81
+ *
82
+ * @param suffix A suffix used for generating the event name.
83
+ * @param data Data object for event.
84
+ */
85
+ _evaluateTextBeforeSelection(suffix, data = {}) {
86
+ const model = this.model;
87
+ const document = model.document;
88
+ const selection = document.selection;
89
+ const rangeBeforeSelection = model.createRange(model.createPositionAt(selection.focus.parent, 0), selection.focus);
90
+ const { text, range } = getLastTextLine(rangeBeforeSelection, model);
91
+ const testResult = this.testCallback(text);
92
+ if (!testResult && this.hasMatch) {
93
+ this.fire('unmatched');
94
+ }
95
+ this._hasMatch = !!testResult;
96
+ if (testResult) {
97
+ const eventData = Object.assign(data, { text, range });
98
+ // If the test callback returns an object with additional data, assign the data as well.
99
+ if (typeof testResult == 'object') {
100
+ Object.assign(eventData, testResult);
101
+ }
102
+ this.fire(`matched:${suffix}`, eventData);
103
+ }
104
+ }
105
+ }