@ckeditor/ckeditor5-find-and-replace 41.2.0 → 41.3.0-alpha.1
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.
- package/dist/content-index.css +4 -0
- package/dist/editor-index.css +20 -0
- package/dist/index.css +38 -0
- package/dist/index.css.map +1 -0
- package/dist/index.js +1493 -0
- package/dist/index.js.map +1 -0
- package/dist/types/augmentation.d.ts +20 -0
- package/dist/types/findandreplace.d.ts +42 -0
- package/dist/types/findandreplaceconfig.d.ts +31 -0
- package/dist/types/findandreplaceediting.d.ts +63 -0
- package/dist/types/findandreplacestate.d.ts +95 -0
- package/dist/types/findandreplaceui.d.ts +67 -0
- package/dist/types/findandreplaceutils.d.ts +67 -0
- package/dist/types/findcommand.d.ts +48 -0
- package/dist/types/findnextcommand.d.ts +35 -0
- package/dist/types/findpreviouscommand.d.ts +19 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/replaceallcommand.d.ts +35 -0
- package/dist/types/replacecommand.d.ts +22 -0
- package/dist/types/replacecommandbase.d.ts +31 -0
- package/dist/types/ui/findandreplaceformview.d.ts +309 -0
- package/package.json +4 -3
- package/src/index.d.ts +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1493 @@
|
|
|
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 { icons, Plugin, Command } from '@ckeditor/ckeditor5-core/dist/index.js';
|
|
6
|
+
import { View, ViewCollection, FocusCycler, submitHandler, CollapsibleView, SwitchButtonView, ButtonView, LabeledFieldView, createLabeledInputText, Dialog, DropdownView, createDropdown, FormHeaderView, DialogViewPosition, CssTransitionDisablerMixin } from '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
7
|
+
import { FocusTracker, KeystrokeHandler, isVisible, Rect, Collection, ObservableMixin, uid, scrollViewportToShowTarget } from '@ckeditor/ckeditor5-utils/dist/index.js';
|
|
8
|
+
import '@ckeditor/ckeditor5-ui/dist/index.js';
|
|
9
|
+
import { escapeRegExp, debounce } from 'lodash-es';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
13
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @module find-and-replace/ui/findandreplaceformview
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The find and replace form view class.
|
|
20
|
+
*
|
|
21
|
+
* See {@link module:find-and-replace/ui/findandreplaceformview~FindAndReplaceFormView}.
|
|
22
|
+
*/
|
|
23
|
+
class FindAndReplaceFormView extends View {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a view of find and replace form.
|
|
26
|
+
*
|
|
27
|
+
* @param locale The localization services instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(locale) {
|
|
30
|
+
super(locale);
|
|
31
|
+
const t = locale.t;
|
|
32
|
+
this.children = this.createCollection();
|
|
33
|
+
this.set('matchCount', 0);
|
|
34
|
+
this.set('highlightOffset', 0);
|
|
35
|
+
this.set('isDirty', false);
|
|
36
|
+
this.set('_areCommandsEnabled', {});
|
|
37
|
+
this.set('_resultsCounterText', '');
|
|
38
|
+
this.set('_matchCase', false);
|
|
39
|
+
this.set('_wholeWordsOnly', false);
|
|
40
|
+
this.bind('_searchResultsFound').to(this, 'matchCount', this, 'isDirty', (matchCount, isDirty) => {
|
|
41
|
+
return matchCount > 0 && !isDirty;
|
|
42
|
+
});
|
|
43
|
+
this._findInputView = this._createInputField(t('Find in text…'));
|
|
44
|
+
this._findPrevButtonView = this._createButton({
|
|
45
|
+
label: t('Previous result'),
|
|
46
|
+
class: 'ck-button-prev',
|
|
47
|
+
icon: icons.previousArrow,
|
|
48
|
+
keystroke: 'Shift+F3',
|
|
49
|
+
tooltip: true
|
|
50
|
+
});
|
|
51
|
+
this._findNextButtonView = this._createButton({
|
|
52
|
+
label: t('Next result'),
|
|
53
|
+
class: 'ck-button-next',
|
|
54
|
+
icon: icons.previousArrow,
|
|
55
|
+
keystroke: 'F3',
|
|
56
|
+
tooltip: true
|
|
57
|
+
});
|
|
58
|
+
this._replaceInputView = this._createInputField(t('Replace with…'), 'ck-labeled-field-replace');
|
|
59
|
+
this._inputsDivView = this._createInputsDiv();
|
|
60
|
+
this._matchCaseSwitchView = this._createMatchCaseSwitch();
|
|
61
|
+
this._wholeWordsOnlySwitchView = this._createWholeWordsOnlySwitch();
|
|
62
|
+
this._advancedOptionsCollapsibleView = this._createAdvancedOptionsCollapsible();
|
|
63
|
+
this._replaceAllButtonView = this._createButton({
|
|
64
|
+
label: t('Replace all'),
|
|
65
|
+
class: 'ck-button-replaceall',
|
|
66
|
+
withText: true
|
|
67
|
+
});
|
|
68
|
+
this._replaceButtonView = this._createButton({
|
|
69
|
+
label: t('Replace'),
|
|
70
|
+
class: 'ck-button-replace',
|
|
71
|
+
withText: true
|
|
72
|
+
});
|
|
73
|
+
this._findButtonView = this._createButton({
|
|
74
|
+
label: t('Find'),
|
|
75
|
+
class: 'ck-button-find ck-button-action',
|
|
76
|
+
withText: true
|
|
77
|
+
});
|
|
78
|
+
this._actionButtonsDivView = this._createActionButtonsDiv();
|
|
79
|
+
this._focusTracker = new FocusTracker();
|
|
80
|
+
this._keystrokes = new KeystrokeHandler();
|
|
81
|
+
this._focusables = new ViewCollection();
|
|
82
|
+
this.focusCycler = new FocusCycler({
|
|
83
|
+
focusables: this._focusables,
|
|
84
|
+
focusTracker: this._focusTracker,
|
|
85
|
+
keystrokeHandler: this._keystrokes,
|
|
86
|
+
actions: {
|
|
87
|
+
// Navigate form fields backwards using the <kbd>Shift</kbd> + <kbd>Tab</kbd> keystroke.
|
|
88
|
+
focusPrevious: 'shift + tab',
|
|
89
|
+
// Navigate form fields forwards using the <kbd>Tab</kbd> key.
|
|
90
|
+
focusNext: 'tab'
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
this.children.addMany([
|
|
94
|
+
this._inputsDivView,
|
|
95
|
+
this._advancedOptionsCollapsibleView,
|
|
96
|
+
this._actionButtonsDivView
|
|
97
|
+
]);
|
|
98
|
+
this.setTemplate({
|
|
99
|
+
tag: 'form',
|
|
100
|
+
attributes: {
|
|
101
|
+
class: [
|
|
102
|
+
'ck',
|
|
103
|
+
'ck-find-and-replace-form'
|
|
104
|
+
],
|
|
105
|
+
tabindex: '-1'
|
|
106
|
+
},
|
|
107
|
+
children: this.children
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @inheritDoc
|
|
112
|
+
*/
|
|
113
|
+
render() {
|
|
114
|
+
super.render();
|
|
115
|
+
submitHandler({ view: this });
|
|
116
|
+
this._initFocusCycling();
|
|
117
|
+
this._initKeystrokeHandling();
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @inheritDoc
|
|
121
|
+
*/
|
|
122
|
+
destroy() {
|
|
123
|
+
super.destroy();
|
|
124
|
+
this._focusTracker.destroy();
|
|
125
|
+
this._keystrokes.destroy();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @inheritDoc
|
|
129
|
+
*/
|
|
130
|
+
focus(direction) {
|
|
131
|
+
if (direction === -1) {
|
|
132
|
+
this.focusCycler.focusLast();
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
this.focusCycler.focusFirst();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Resets the form before re-appearing.
|
|
140
|
+
*
|
|
141
|
+
* It clears error messages, hides the match counter and disables the replace feature
|
|
142
|
+
* until the next hit of the "Find" button.
|
|
143
|
+
*
|
|
144
|
+
* **Note**: It does not reset inputs and options, though. This way the form works better in editors with
|
|
145
|
+
* disappearing toolbar (e.g. BalloonEditor): hiding the toolbar by accident (together with the find and replace UI)
|
|
146
|
+
* does not require filling the entire form again.
|
|
147
|
+
*/
|
|
148
|
+
reset() {
|
|
149
|
+
this._findInputView.errorText = null;
|
|
150
|
+
this.isDirty = true;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Returns the value of the find input.
|
|
154
|
+
*/
|
|
155
|
+
get _textToFind() {
|
|
156
|
+
return this._findInputView.fieldView.element.value;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Returns the value of the replace input.
|
|
160
|
+
*/
|
|
161
|
+
get _textToReplace() {
|
|
162
|
+
return this._replaceInputView.fieldView.element.value;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Configures and returns the `<div>` aggregating all form inputs.
|
|
166
|
+
*/
|
|
167
|
+
_createInputsDiv() {
|
|
168
|
+
const locale = this.locale;
|
|
169
|
+
const t = locale.t;
|
|
170
|
+
const inputsDivView = new View(locale);
|
|
171
|
+
// Typing in the find field invalidates all previous results (the form is "dirty").
|
|
172
|
+
this._findInputView.fieldView.on('input', () => {
|
|
173
|
+
this.isDirty = true;
|
|
174
|
+
});
|
|
175
|
+
// Pressing prev/next buttons fires related event on the form.
|
|
176
|
+
this._findPrevButtonView.delegate('execute').to(this, 'findPrevious');
|
|
177
|
+
this._findNextButtonView.delegate('execute').to(this, 'findNext');
|
|
178
|
+
// Prev/next buttons will be disabled when related editor command gets disabled.
|
|
179
|
+
this._findPrevButtonView.bind('isEnabled').to(this, '_areCommandsEnabled', ({ findPrevious }) => findPrevious);
|
|
180
|
+
this._findNextButtonView.bind('isEnabled').to(this, '_areCommandsEnabled', ({ findNext }) => findNext);
|
|
181
|
+
this._injectFindResultsCounter();
|
|
182
|
+
this._replaceInputView.bind('isEnabled').to(this, '_areCommandsEnabled', this, '_searchResultsFound', ({ replace }, resultsFound) => replace && resultsFound);
|
|
183
|
+
this._replaceInputView.bind('infoText').to(this._replaceInputView, 'isEnabled', this._replaceInputView, 'isFocused', (isEnabled, isFocused) => {
|
|
184
|
+
if (isEnabled || !isFocused) {
|
|
185
|
+
return '';
|
|
186
|
+
}
|
|
187
|
+
return t('Tip: Find some text first in order to replace it.');
|
|
188
|
+
});
|
|
189
|
+
inputsDivView.setTemplate({
|
|
190
|
+
tag: 'div',
|
|
191
|
+
attributes: {
|
|
192
|
+
class: ['ck', 'ck-find-and-replace-form__inputs']
|
|
193
|
+
},
|
|
194
|
+
children: [
|
|
195
|
+
this._findInputView,
|
|
196
|
+
this._findPrevButtonView,
|
|
197
|
+
this._findNextButtonView,
|
|
198
|
+
this._replaceInputView
|
|
199
|
+
]
|
|
200
|
+
});
|
|
201
|
+
return inputsDivView;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The action performed when the {@link #_findButtonView} is pressed.
|
|
205
|
+
*/
|
|
206
|
+
_onFindButtonExecute() {
|
|
207
|
+
// When hitting "Find" in an empty input, an error should be displayed.
|
|
208
|
+
// Also, if the form was "dirty", it should remain so.
|
|
209
|
+
if (!this._textToFind) {
|
|
210
|
+
const t = this.t;
|
|
211
|
+
this._findInputView.errorText = t('Text to find must not be empty.');
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
// Hitting "Find" automatically clears the dirty state.
|
|
215
|
+
this.isDirty = false;
|
|
216
|
+
this.fire('findNext', {
|
|
217
|
+
searchText: this._textToFind,
|
|
218
|
+
matchCase: this._matchCase,
|
|
219
|
+
wholeWords: this._wholeWordsOnly
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Configures an injects the find results counter displaying a "N of M" label of the {@link #_findInputView}.
|
|
224
|
+
*/
|
|
225
|
+
_injectFindResultsCounter() {
|
|
226
|
+
const locale = this.locale;
|
|
227
|
+
const t = locale.t;
|
|
228
|
+
const bind = this.bindTemplate;
|
|
229
|
+
const resultsCounterView = new View(this.locale);
|
|
230
|
+
this.bind('_resultsCounterText').to(this, 'highlightOffset', this, 'matchCount', (highlightOffset, matchCount) => t('%0 of %1', [highlightOffset, matchCount]));
|
|
231
|
+
resultsCounterView.setTemplate({
|
|
232
|
+
tag: 'span',
|
|
233
|
+
attributes: {
|
|
234
|
+
class: [
|
|
235
|
+
'ck',
|
|
236
|
+
'ck-results-counter',
|
|
237
|
+
// The counter only makes sense when the field text corresponds to search results in the editing.
|
|
238
|
+
bind.if('isDirty', 'ck-hidden')
|
|
239
|
+
]
|
|
240
|
+
},
|
|
241
|
+
children: [
|
|
242
|
+
{
|
|
243
|
+
text: bind.to('_resultsCounterText')
|
|
244
|
+
}
|
|
245
|
+
]
|
|
246
|
+
});
|
|
247
|
+
// The whole idea is that when the text of the counter changes, its width also increases/decreases and
|
|
248
|
+
// it consumes more or less space over the input. The input, on the other hand, should adjust it's right
|
|
249
|
+
// padding so its *entire* text always remains visible and available to the user.
|
|
250
|
+
const updateFindInputPadding = () => {
|
|
251
|
+
const inputElement = this._findInputView.fieldView.element;
|
|
252
|
+
// Don't adjust the padding if the input (also: counter) were not rendered or not inserted into DOM yet.
|
|
253
|
+
if (!inputElement || !isVisible(inputElement)) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const counterWidth = new Rect(resultsCounterView.element).width;
|
|
257
|
+
const paddingPropertyName = locale.uiLanguageDirection === 'ltr' ? 'paddingRight' : 'paddingLeft';
|
|
258
|
+
if (!counterWidth) {
|
|
259
|
+
inputElement.style[paddingPropertyName] = '';
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
inputElement.style[paddingPropertyName] = `calc( 2 * var(--ck-spacing-standard) + ${counterWidth}px )`;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
// Adjust the input padding when the text of the counter changes, for instance "1 of 200" is narrower than "123 of 200".
|
|
266
|
+
// Using "low" priority to let the text be set by the template binding first.
|
|
267
|
+
this.on('change:_resultsCounterText', updateFindInputPadding, { priority: 'low' });
|
|
268
|
+
// Adjust the input padding when the counter shows or hides. When hidden, there should be no padding. When it shows, the
|
|
269
|
+
// padding should be set according to the text of the counter.
|
|
270
|
+
// Using "low" priority to let the text be set by the template binding first.
|
|
271
|
+
this.on('change:isDirty', updateFindInputPadding, { priority: 'low' });
|
|
272
|
+
// Put the counter element next to the <input> in the find field.
|
|
273
|
+
this._findInputView.template.children[0].children.push(resultsCounterView);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Creates the collapsible view aggregating the advanced search options.
|
|
277
|
+
*/
|
|
278
|
+
_createAdvancedOptionsCollapsible() {
|
|
279
|
+
const t = this.locale.t;
|
|
280
|
+
const collapsible = new CollapsibleView(this.locale, [
|
|
281
|
+
this._matchCaseSwitchView,
|
|
282
|
+
this._wholeWordsOnlySwitchView
|
|
283
|
+
]);
|
|
284
|
+
collapsible.set({
|
|
285
|
+
label: t('Advanced options'),
|
|
286
|
+
isCollapsed: true
|
|
287
|
+
});
|
|
288
|
+
return collapsible;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Configures and returns the `<div>` element aggregating all form action buttons.
|
|
292
|
+
*/
|
|
293
|
+
_createActionButtonsDiv() {
|
|
294
|
+
const actionsDivView = new View(this.locale);
|
|
295
|
+
this._replaceButtonView.bind('isEnabled').to(this, '_areCommandsEnabled', this, '_searchResultsFound', ({ replace }, resultsFound) => replace && resultsFound);
|
|
296
|
+
this._replaceAllButtonView.bind('isEnabled').to(this, '_areCommandsEnabled', this, '_searchResultsFound', ({ replaceAll }, resultsFound) => replaceAll && resultsFound);
|
|
297
|
+
this._replaceButtonView.on('execute', () => {
|
|
298
|
+
this.fire('replace', {
|
|
299
|
+
searchText: this._textToFind,
|
|
300
|
+
replaceText: this._textToReplace
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
this._replaceAllButtonView.on('execute', () => {
|
|
304
|
+
this.fire('replaceAll', {
|
|
305
|
+
searchText: this._textToFind,
|
|
306
|
+
replaceText: this._textToReplace
|
|
307
|
+
});
|
|
308
|
+
this.focus();
|
|
309
|
+
});
|
|
310
|
+
this._findButtonView.on('execute', this._onFindButtonExecute.bind(this));
|
|
311
|
+
actionsDivView.setTemplate({
|
|
312
|
+
tag: 'div',
|
|
313
|
+
attributes: {
|
|
314
|
+
class: ['ck', 'ck-find-and-replace-form__actions']
|
|
315
|
+
},
|
|
316
|
+
children: [
|
|
317
|
+
this._replaceAllButtonView,
|
|
318
|
+
this._replaceButtonView,
|
|
319
|
+
this._findButtonView
|
|
320
|
+
]
|
|
321
|
+
});
|
|
322
|
+
return actionsDivView;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Creates, configures and returns and instance of a dropdown allowing users to narrow
|
|
326
|
+
* the search criteria down. The dropdown has a list with switch buttons for each option.
|
|
327
|
+
*/
|
|
328
|
+
_createMatchCaseSwitch() {
|
|
329
|
+
const t = this.locale.t;
|
|
330
|
+
const matchCaseSwitchButton = new SwitchButtonView(this.locale);
|
|
331
|
+
matchCaseSwitchButton.set({
|
|
332
|
+
label: t('Match case'),
|
|
333
|
+
withText: true
|
|
334
|
+
});
|
|
335
|
+
// Let the switch be controlled by form's observable property.
|
|
336
|
+
matchCaseSwitchButton.bind('isOn').to(this, '_matchCase');
|
|
337
|
+
// // Update the state of the form when a switch is toggled.
|
|
338
|
+
matchCaseSwitchButton.on('execute', () => {
|
|
339
|
+
this._matchCase = !this._matchCase;
|
|
340
|
+
// Toggling a switch makes the form dirty because this changes search criteria
|
|
341
|
+
// just like typing text of the find input.
|
|
342
|
+
this.isDirty = true;
|
|
343
|
+
});
|
|
344
|
+
return matchCaseSwitchButton;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Creates, configures and returns and instance of a dropdown allowing users to narrow
|
|
348
|
+
* the search criteria down. The dropdown has a list with switch buttons for each option.
|
|
349
|
+
*/
|
|
350
|
+
_createWholeWordsOnlySwitch() {
|
|
351
|
+
const t = this.locale.t;
|
|
352
|
+
const wholeWordsOnlySwitchButton = new SwitchButtonView(this.locale);
|
|
353
|
+
wholeWordsOnlySwitchButton.set({
|
|
354
|
+
label: t('Whole words only'),
|
|
355
|
+
withText: true
|
|
356
|
+
});
|
|
357
|
+
// Let the switch be controlled by form's observable property.
|
|
358
|
+
wholeWordsOnlySwitchButton.bind('isOn').to(this, '_wholeWordsOnly');
|
|
359
|
+
// // Update the state of the form when a switch is toggled.
|
|
360
|
+
wholeWordsOnlySwitchButton.on('execute', () => {
|
|
361
|
+
this._wholeWordsOnly = !this._wholeWordsOnly;
|
|
362
|
+
// Toggling a switch makes the form dirty because this changes search criteria
|
|
363
|
+
// just like typing text of the find input.
|
|
364
|
+
this.isDirty = true;
|
|
365
|
+
});
|
|
366
|
+
return wholeWordsOnlySwitchButton;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Initializes the {@link #_focusables} and {@link #_focusTracker} to allow navigation
|
|
370
|
+
* using <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> keystrokes in the right order.
|
|
371
|
+
*/
|
|
372
|
+
_initFocusCycling() {
|
|
373
|
+
const childViews = [
|
|
374
|
+
this._findInputView,
|
|
375
|
+
this._findPrevButtonView,
|
|
376
|
+
this._findNextButtonView,
|
|
377
|
+
this._replaceInputView,
|
|
378
|
+
this._advancedOptionsCollapsibleView.buttonView,
|
|
379
|
+
this._matchCaseSwitchView,
|
|
380
|
+
this._wholeWordsOnlySwitchView,
|
|
381
|
+
this._replaceAllButtonView,
|
|
382
|
+
this._replaceButtonView,
|
|
383
|
+
this._findButtonView
|
|
384
|
+
];
|
|
385
|
+
childViews.forEach(v => {
|
|
386
|
+
// Register the view as focusable.
|
|
387
|
+
this._focusables.add(v);
|
|
388
|
+
// Register the view in the focus tracker.
|
|
389
|
+
this._focusTracker.add(v.element);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Initializes the keystroke handling in the form.
|
|
394
|
+
*/
|
|
395
|
+
_initKeystrokeHandling() {
|
|
396
|
+
const stopPropagation = (data) => data.stopPropagation();
|
|
397
|
+
const stopPropagationAndPreventDefault = (data) => {
|
|
398
|
+
data.stopPropagation();
|
|
399
|
+
data.preventDefault();
|
|
400
|
+
};
|
|
401
|
+
// Start listening for the keystrokes coming from #element.
|
|
402
|
+
this._keystrokes.listenTo(this.element);
|
|
403
|
+
// Find the next result upon F3.
|
|
404
|
+
this._keystrokes.set('f3', event => {
|
|
405
|
+
stopPropagationAndPreventDefault(event);
|
|
406
|
+
this._findNextButtonView.fire('execute');
|
|
407
|
+
});
|
|
408
|
+
// Find the previous result upon F3.
|
|
409
|
+
this._keystrokes.set('shift+f3', event => {
|
|
410
|
+
stopPropagationAndPreventDefault(event);
|
|
411
|
+
this._findPrevButtonView.fire('execute');
|
|
412
|
+
});
|
|
413
|
+
// Find or replace upon pressing Enter in the find and replace fields.
|
|
414
|
+
this._keystrokes.set('enter', event => {
|
|
415
|
+
const target = event.target;
|
|
416
|
+
if (target === this._findInputView.fieldView.element) {
|
|
417
|
+
if (this._areCommandsEnabled.findNext) {
|
|
418
|
+
this._findNextButtonView.fire('execute');
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
this._findButtonView.fire('execute');
|
|
422
|
+
}
|
|
423
|
+
stopPropagationAndPreventDefault(event);
|
|
424
|
+
}
|
|
425
|
+
else if (target === this._replaceInputView.fieldView.element && !this.isDirty) {
|
|
426
|
+
this._replaceButtonView.fire('execute');
|
|
427
|
+
stopPropagationAndPreventDefault(event);
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
// Find previous upon pressing Shift+Enter in the find field.
|
|
431
|
+
this._keystrokes.set('shift+enter', event => {
|
|
432
|
+
const target = event.target;
|
|
433
|
+
if (target !== this._findInputView.fieldView.element) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (this._areCommandsEnabled.findPrevious) {
|
|
437
|
+
this._findPrevButtonView.fire('execute');
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
this._findButtonView.fire('execute');
|
|
441
|
+
}
|
|
442
|
+
stopPropagationAndPreventDefault(event);
|
|
443
|
+
});
|
|
444
|
+
// Since the form is in the dropdown panel which is a child of the toolbar, the toolbar's
|
|
445
|
+
// keystroke handler would take over the key management in the URL input.
|
|
446
|
+
// We need to prevent this ASAP. Otherwise, the basic caret movement using the arrow keys will be impossible.
|
|
447
|
+
this._keystrokes.set('arrowright', stopPropagation);
|
|
448
|
+
this._keystrokes.set('arrowleft', stopPropagation);
|
|
449
|
+
this._keystrokes.set('arrowup', stopPropagation);
|
|
450
|
+
this._keystrokes.set('arrowdown', stopPropagation);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Creates a button view.
|
|
454
|
+
*
|
|
455
|
+
* @param options The properties of the `ButtonView`.
|
|
456
|
+
* @returns The button view instance.
|
|
457
|
+
*/
|
|
458
|
+
_createButton(options) {
|
|
459
|
+
const button = new ButtonView(this.locale);
|
|
460
|
+
button.set(options);
|
|
461
|
+
return button;
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Creates a labeled input view.
|
|
465
|
+
*
|
|
466
|
+
* @param label The input label.
|
|
467
|
+
* @returns The labeled input view instance.
|
|
468
|
+
*/
|
|
469
|
+
_createInputField(label, className) {
|
|
470
|
+
const labeledInput = new LabeledFieldView(this.locale, createLabeledInputText);
|
|
471
|
+
labeledInput.label = label;
|
|
472
|
+
labeledInput.class = className;
|
|
473
|
+
return labeledInput;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
var loupeIcon = "<svg viewBox=\"0 0 20 20\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m12.87 13.786 1.532-1.286 3.857 4.596a1 1 0 1 1-1.532 1.286l-3.857-4.596z\"/><path d=\"M16.004 8.5a6.5 6.5 0 0 1-9.216 5.905c-1.154-.53-.863-1.415-.663-1.615.194-.194.564-.592 1.635-.141a4.5 4.5 0 0 0 5.89-5.904l-.104-.227 1.332-1.331c.045-.046.196-.041.224.007a6.47 6.47 0 0 1 .902 3.306zm-3.4-5.715c.562.305.742 1.106.354 1.494-.388.388-.995.414-1.476.178a4.5 4.5 0 0 0-6.086 5.882l.114.236-1.348 1.349c-.038.037-.17.022-.198-.023a6.5 6.5 0 0 1 5.54-9.9 6.469 6.469 0 0 1 3.1.784z\"/><path d=\"M4.001 11.93.948 8.877a.2.2 0 0 1 .141-.341h6.106a.2.2 0 0 1 .141.341L4.283 11.93a.2.2 0 0 1-.282 0zm11.083-6.789 3.053 3.053a.2.2 0 0 1-.14.342H11.89a.2.2 0 0 1-.14-.342l3.052-3.053a.2.2 0 0 1 .282 0z\"/></svg>";
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
481
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
482
|
+
*/
|
|
483
|
+
/**
|
|
484
|
+
* @module find-and-replace/findandreplaceui
|
|
485
|
+
*/
|
|
486
|
+
/**
|
|
487
|
+
* The default find and replace UI.
|
|
488
|
+
*
|
|
489
|
+
* It registers the `'findAndReplace'` UI button in the editor's {@link module:ui/componentfactory~ComponentFactory component factory}.
|
|
490
|
+
* that uses the {@link module:find-and-replace/findandreplace~FindAndReplace FindAndReplace} plugin API.
|
|
491
|
+
*/
|
|
492
|
+
class FindAndReplaceUI extends Plugin {
|
|
493
|
+
/**
|
|
494
|
+
* @inheritDoc
|
|
495
|
+
*/
|
|
496
|
+
static get requires() {
|
|
497
|
+
return [Dialog];
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* @inheritDoc
|
|
501
|
+
*/
|
|
502
|
+
static get pluginName() {
|
|
503
|
+
return 'FindAndReplaceUI';
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* @inheritDoc
|
|
507
|
+
*/
|
|
508
|
+
constructor(editor) {
|
|
509
|
+
super(editor);
|
|
510
|
+
editor.config.define('findAndReplace.uiType', 'dialog');
|
|
511
|
+
this.formView = null;
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* @inheritDoc
|
|
515
|
+
*/
|
|
516
|
+
init() {
|
|
517
|
+
const editor = this.editor;
|
|
518
|
+
const isUiUsingDropdown = editor.config.get('findAndReplace.uiType') === 'dropdown';
|
|
519
|
+
const findCommand = editor.commands.get('find');
|
|
520
|
+
const t = this.editor.t;
|
|
521
|
+
// Register the toolbar component: dropdown or button (that opens a dialog).
|
|
522
|
+
editor.ui.componentFactory.add('findAndReplace', () => {
|
|
523
|
+
let view;
|
|
524
|
+
if (isUiUsingDropdown) {
|
|
525
|
+
view = this._createDropdown();
|
|
526
|
+
// Button should be disabled when in source editing mode. See #10001.
|
|
527
|
+
view.bind('isEnabled').to(findCommand);
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
view = this._createDialogButton();
|
|
531
|
+
// Button should be disabled when in source editing mode. See #10001.
|
|
532
|
+
view.bind('isEnabled').to(findCommand);
|
|
533
|
+
}
|
|
534
|
+
editor.keystrokes.set('Ctrl+F', (data, cancelEvent) => {
|
|
535
|
+
if (!findCommand.isEnabled) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (view instanceof DropdownView) {
|
|
539
|
+
const dropdownButtonView = view.buttonView;
|
|
540
|
+
if (!dropdownButtonView.isOn) {
|
|
541
|
+
dropdownButtonView.fire('execute');
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
if (view.isOn) {
|
|
546
|
+
// If the dialog is open, do not close it. Instead focus it.
|
|
547
|
+
// Unfortunately we can't simply use:
|
|
548
|
+
// this.formView!.focus();
|
|
549
|
+
// because it would always move focus to the first input field, which we don't want.
|
|
550
|
+
editor.plugins.get('Dialog').view.focus();
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
view.fire('execute');
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
cancelEvent();
|
|
557
|
+
});
|
|
558
|
+
return view;
|
|
559
|
+
});
|
|
560
|
+
// Add the information about the keystroke to the accessibility database.
|
|
561
|
+
editor.accessibility.addKeystrokeInfos({
|
|
562
|
+
keystrokes: [
|
|
563
|
+
{
|
|
564
|
+
label: t('Find in the document'),
|
|
565
|
+
keystroke: 'CTRL+F'
|
|
566
|
+
}
|
|
567
|
+
]
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Creates a dropdown containing the find and replace form.
|
|
572
|
+
*/
|
|
573
|
+
_createDropdown() {
|
|
574
|
+
const editor = this.editor;
|
|
575
|
+
const t = editor.locale.t;
|
|
576
|
+
const dropdownView = createDropdown(editor.locale);
|
|
577
|
+
dropdownView.once('change:isOpen', () => {
|
|
578
|
+
this.formView = this._createFormView();
|
|
579
|
+
this.formView.children.add(new FormHeaderView(editor.locale, {
|
|
580
|
+
label: t('Find and replace')
|
|
581
|
+
}), 0);
|
|
582
|
+
dropdownView.panelView.children.add(this.formView);
|
|
583
|
+
});
|
|
584
|
+
// Every time a dropdown is opened, the search text field should get focused and selected for better UX.
|
|
585
|
+
// Note: Using the low priority here to make sure the following listener starts working after
|
|
586
|
+
// the default action of the drop-down is executed (i.e. the panel showed up). Otherwise,
|
|
587
|
+
// the invisible form/input cannot be focused/selected.
|
|
588
|
+
//
|
|
589
|
+
// Each time a dropdown is closed, move the focus back to the find and replace toolbar button
|
|
590
|
+
// and let the find and replace editing feature know that all search results can be invalidated
|
|
591
|
+
// and no longer should be marked in the content.
|
|
592
|
+
dropdownView.on('change:isOpen', (event, name, isOpen) => {
|
|
593
|
+
if (isOpen) {
|
|
594
|
+
this._setupFormView();
|
|
595
|
+
}
|
|
596
|
+
else {
|
|
597
|
+
this.fire('searchReseted');
|
|
598
|
+
}
|
|
599
|
+
}, { priority: 'low' });
|
|
600
|
+
dropdownView.buttonView.set({
|
|
601
|
+
icon: loupeIcon,
|
|
602
|
+
label: t('Find and replace'),
|
|
603
|
+
keystroke: 'CTRL+F',
|
|
604
|
+
tooltip: true
|
|
605
|
+
});
|
|
606
|
+
return dropdownView;
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Creates a button that opens a dialog with the find and replace form.
|
|
610
|
+
*/
|
|
611
|
+
_createDialogButton() {
|
|
612
|
+
const editor = this.editor;
|
|
613
|
+
const buttonView = new ButtonView(editor.locale);
|
|
614
|
+
const dialog = editor.plugins.get('Dialog');
|
|
615
|
+
const t = editor.locale.t;
|
|
616
|
+
buttonView.set({
|
|
617
|
+
icon: loupeIcon,
|
|
618
|
+
label: t('Find and replace'),
|
|
619
|
+
keystroke: 'CTRL+F',
|
|
620
|
+
tooltip: true
|
|
621
|
+
});
|
|
622
|
+
// Button should be on when the find and replace dialog is opened.
|
|
623
|
+
buttonView.bind('isOn').to(dialog, 'id', id => id === 'findAndReplace');
|
|
624
|
+
// Every time a dialog is opened, the search text field should get focused and selected for better UX.
|
|
625
|
+
// Each time a dialog is closed, move the focus back to the find and replace toolbar button
|
|
626
|
+
// and let the find and replace editing feature know that all search results can be invalidated
|
|
627
|
+
// and no longer should be marked in the content.
|
|
628
|
+
buttonView.on('execute', () => {
|
|
629
|
+
if (!this.formView) {
|
|
630
|
+
this.formView = this._createFormView();
|
|
631
|
+
}
|
|
632
|
+
if (buttonView.isOn) {
|
|
633
|
+
dialog.hide();
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
dialog.show({
|
|
637
|
+
id: 'findAndReplace',
|
|
638
|
+
title: t('Find and replace'),
|
|
639
|
+
content: this.formView,
|
|
640
|
+
position: DialogViewPosition.EDITOR_TOP_SIDE,
|
|
641
|
+
onShow: () => {
|
|
642
|
+
this._setupFormView();
|
|
643
|
+
},
|
|
644
|
+
onHide: () => {
|
|
645
|
+
this.fire('searchReseted');
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
return buttonView;
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Sets up the form view for the find and replace.
|
|
654
|
+
*
|
|
655
|
+
* @param formView A related form view.
|
|
656
|
+
*/
|
|
657
|
+
_createFormView() {
|
|
658
|
+
const editor = this.editor;
|
|
659
|
+
const formView = new (CssTransitionDisablerMixin(FindAndReplaceFormView))(editor.locale);
|
|
660
|
+
const commands = editor.commands;
|
|
661
|
+
const findAndReplaceEditing = this.editor.plugins.get('FindAndReplaceEditing');
|
|
662
|
+
const editingState = findAndReplaceEditing.state;
|
|
663
|
+
formView.bind('highlightOffset').to(editingState, 'highlightedOffset');
|
|
664
|
+
// Let the form know how many results were found in total.
|
|
665
|
+
formView.listenTo(editingState.results, 'change', () => {
|
|
666
|
+
formView.matchCount = editingState.results.length;
|
|
667
|
+
});
|
|
668
|
+
// Command states are used to enable/disable individual form controls.
|
|
669
|
+
// To keep things simple, instead of binding 4 individual observables, there's only one that combines every
|
|
670
|
+
// commands' isEnabled state. Yes, it will change more often but this simplifies the structure of the form.
|
|
671
|
+
const findNextCommand = commands.get('findNext');
|
|
672
|
+
const findPreviousCommand = commands.get('findPrevious');
|
|
673
|
+
const replaceCommand = commands.get('replace');
|
|
674
|
+
const replaceAllCommand = commands.get('replaceAll');
|
|
675
|
+
formView.bind('_areCommandsEnabled').to(findNextCommand, 'isEnabled', findPreviousCommand, 'isEnabled', replaceCommand, 'isEnabled', replaceAllCommand, 'isEnabled', (findNext, findPrevious, replace, replaceAll) => ({ findNext, findPrevious, replace, replaceAll }));
|
|
676
|
+
// The UI plugin works as an interface between the form and the editing part of the feature.
|
|
677
|
+
formView.delegate('findNext', 'findPrevious', 'replace', 'replaceAll').to(this);
|
|
678
|
+
// Let the feature know that search results are no longer relevant because the user changed the searched phrase
|
|
679
|
+
// (or options) but didn't hit the "Find" button yet (e.g. still typing).
|
|
680
|
+
formView.on('change:isDirty', (evt, data, isDirty) => {
|
|
681
|
+
if (isDirty) {
|
|
682
|
+
this.fire('searchReseted');
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
return formView;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Clears the find and replace form and focuses the search text field.
|
|
689
|
+
*/
|
|
690
|
+
_setupFormView() {
|
|
691
|
+
this.formView.disableCssTransitions();
|
|
692
|
+
this.formView.reset();
|
|
693
|
+
this.formView._findInputView.fieldView.select();
|
|
694
|
+
this.formView.enableCssTransitions();
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
700
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
701
|
+
*/
|
|
702
|
+
/**
|
|
703
|
+
* @module find-and-replace/findcommand
|
|
704
|
+
*/
|
|
705
|
+
/**
|
|
706
|
+
* The find command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
707
|
+
*/
|
|
708
|
+
class FindCommand extends Command {
|
|
709
|
+
/**
|
|
710
|
+
* Creates a new `FindCommand` instance.
|
|
711
|
+
*
|
|
712
|
+
* @param editor The editor on which this command will be used.
|
|
713
|
+
* @param state An object to hold plugin state.
|
|
714
|
+
*/
|
|
715
|
+
constructor(editor, state) {
|
|
716
|
+
super(editor);
|
|
717
|
+
// The find command is always enabled.
|
|
718
|
+
this.isEnabled = true;
|
|
719
|
+
// It does not affect data so should be enabled in read-only mode.
|
|
720
|
+
this.affectsData = false;
|
|
721
|
+
this._state = state;
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Executes the command.
|
|
725
|
+
*
|
|
726
|
+
* @param callbackOrText
|
|
727
|
+
* @param options Options object.
|
|
728
|
+
* @param options.matchCase If set to `true`, the letter case will be matched.
|
|
729
|
+
* @param options.wholeWords If set to `true`, only whole words that match `callbackOrText` will be matched.
|
|
730
|
+
*
|
|
731
|
+
* @fires execute
|
|
732
|
+
*/
|
|
733
|
+
execute(callbackOrText, { matchCase, wholeWords } = {}) {
|
|
734
|
+
const { editor } = this;
|
|
735
|
+
const { model } = editor;
|
|
736
|
+
const findAndReplaceUtils = editor.plugins.get('FindAndReplaceUtils');
|
|
737
|
+
let findCallback;
|
|
738
|
+
// Allow to execute `find()` on a plugin with a keyword only.
|
|
739
|
+
if (typeof callbackOrText === 'string') {
|
|
740
|
+
findCallback = findAndReplaceUtils.findByTextCallback(callbackOrText, { matchCase, wholeWords });
|
|
741
|
+
this._state.searchText = callbackOrText;
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
findCallback = callbackOrText;
|
|
745
|
+
}
|
|
746
|
+
// Initial search is done on all nodes in all roots inside the content.
|
|
747
|
+
const results = model.document.getRootNames()
|
|
748
|
+
.reduce(((currentResults, rootName) => findAndReplaceUtils.updateFindResultFromRange(model.createRangeIn(model.document.getRoot(rootName)), model, findCallback, currentResults)), null);
|
|
749
|
+
this._state.clear(model);
|
|
750
|
+
this._state.results.addMany(results);
|
|
751
|
+
this._state.highlightedResult = results.get(0);
|
|
752
|
+
if (typeof callbackOrText === 'string') {
|
|
753
|
+
this._state.searchText = callbackOrText;
|
|
754
|
+
}
|
|
755
|
+
if (findCallback) {
|
|
756
|
+
this._state.lastSearchCallback = findCallback;
|
|
757
|
+
}
|
|
758
|
+
this._state.matchCase = !!matchCase;
|
|
759
|
+
this._state.matchWholeWords = !!wholeWords;
|
|
760
|
+
return {
|
|
761
|
+
results,
|
|
762
|
+
findCallback
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
769
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
770
|
+
*/
|
|
771
|
+
/**
|
|
772
|
+
* @module find-and-replace/replacecommandbase
|
|
773
|
+
*/
|
|
774
|
+
class ReplaceCommandBase extends Command {
|
|
775
|
+
/**
|
|
776
|
+
* Creates a new `ReplaceCommand` instance.
|
|
777
|
+
*
|
|
778
|
+
* @param editor Editor on which this command will be used.
|
|
779
|
+
* @param state An object to hold plugin state.
|
|
780
|
+
*/
|
|
781
|
+
constructor(editor, state) {
|
|
782
|
+
super(editor);
|
|
783
|
+
// The replace command is always enabled.
|
|
784
|
+
this.isEnabled = true;
|
|
785
|
+
this._state = state;
|
|
786
|
+
// Since this command executes on particular result independent of selection, it should be checked directly in execute block.
|
|
787
|
+
this._isEnabledBasedOnSelection = false;
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Common logic for both `replace` commands.
|
|
791
|
+
* Replace a given find result by a string or a callback.
|
|
792
|
+
*
|
|
793
|
+
* @param result A single result from the find command.
|
|
794
|
+
*/
|
|
795
|
+
_replace(replacementText, result) {
|
|
796
|
+
const { model } = this.editor;
|
|
797
|
+
const range = result.marker.getRange();
|
|
798
|
+
// Don't replace a result that is in non-editable place.
|
|
799
|
+
if (!model.canEditAt(range)) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
model.change(writer => {
|
|
803
|
+
// Don't replace a result (marker) that found its way into the $graveyard (e.g. removed by collaborators).
|
|
804
|
+
if (range.root.rootName === '$graveyard') {
|
|
805
|
+
this._state.results.remove(result);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
let textAttributes = {};
|
|
809
|
+
for (const item of range.getItems()) {
|
|
810
|
+
if (item.is('$text') || item.is('$textProxy')) {
|
|
811
|
+
textAttributes = item.getAttributes();
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
model.insertContent(writer.createText(replacementText, textAttributes), range);
|
|
816
|
+
if (this._state.results.has(result)) {
|
|
817
|
+
this._state.results.remove(result);
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
825
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
826
|
+
*/
|
|
827
|
+
/**
|
|
828
|
+
* The replace command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
829
|
+
*/
|
|
830
|
+
class ReplaceCommand extends ReplaceCommandBase {
|
|
831
|
+
/**
|
|
832
|
+
* Replace a given find result by a string or a callback.
|
|
833
|
+
*
|
|
834
|
+
* @param result A single result from the find command.
|
|
835
|
+
*
|
|
836
|
+
* @fires execute
|
|
837
|
+
*/
|
|
838
|
+
execute(replacementText, result) {
|
|
839
|
+
this._replace(replacementText, result);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/**
|
|
844
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
845
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
846
|
+
*/
|
|
847
|
+
/**
|
|
848
|
+
* @module find-and-replace/replaceallcommand
|
|
849
|
+
*/
|
|
850
|
+
/**
|
|
851
|
+
* The replace all command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
852
|
+
*/
|
|
853
|
+
class ReplaceAllCommand extends ReplaceCommandBase {
|
|
854
|
+
/**
|
|
855
|
+
* Replaces all the occurrences of `textToReplace` with a given `newText` string.
|
|
856
|
+
*
|
|
857
|
+
* ```ts
|
|
858
|
+
* replaceAllCommand.execute( 'replaceAll', 'new text replacement', 'text to replace' );
|
|
859
|
+
* ```
|
|
860
|
+
*
|
|
861
|
+
* Alternatively you can call it from editor instance:
|
|
862
|
+
*
|
|
863
|
+
* ```ts
|
|
864
|
+
* editor.execute( 'replaceAll', 'new text', 'old text' );
|
|
865
|
+
* ```
|
|
866
|
+
*
|
|
867
|
+
* @param newText Text that will be inserted to the editor for each match.
|
|
868
|
+
* @param textToReplace Text to be replaced or a collection of matches
|
|
869
|
+
* as returned by the find command.
|
|
870
|
+
*
|
|
871
|
+
* @fires module:core/command~Command#event:execute
|
|
872
|
+
*/
|
|
873
|
+
execute(newText, textToReplace) {
|
|
874
|
+
const { editor } = this;
|
|
875
|
+
const { model } = editor;
|
|
876
|
+
const findAndReplaceUtils = editor.plugins.get('FindAndReplaceUtils');
|
|
877
|
+
const results = textToReplace instanceof Collection ?
|
|
878
|
+
textToReplace : model.document.getRootNames()
|
|
879
|
+
.reduce(((currentResults, rootName) => findAndReplaceUtils.updateFindResultFromRange(model.createRangeIn(model.document.getRoot(rootName)), model, findAndReplaceUtils.findByTextCallback(textToReplace, this._state), currentResults)), null);
|
|
880
|
+
if (results.length) {
|
|
881
|
+
// Wrapped in single change will batch it into one transaction.
|
|
882
|
+
model.change(() => {
|
|
883
|
+
[...results].forEach(searchResult => {
|
|
884
|
+
// Just reuse logic from the replace command to replace a single match.
|
|
885
|
+
this._replace(newText, searchResult);
|
|
886
|
+
});
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
894
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
895
|
+
*/
|
|
896
|
+
/**
|
|
897
|
+
* @module find-and-replace/findnextcommand
|
|
898
|
+
*/
|
|
899
|
+
/**
|
|
900
|
+
* The find next command. Moves the highlight to the next search result.
|
|
901
|
+
*
|
|
902
|
+
* It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
903
|
+
*/
|
|
904
|
+
class FindNextCommand extends Command {
|
|
905
|
+
/**
|
|
906
|
+
* Creates a new `FindNextCommand` instance.
|
|
907
|
+
*
|
|
908
|
+
* @param editor The editor on which this command will be used.
|
|
909
|
+
* @param state An object to hold plugin state.
|
|
910
|
+
*/
|
|
911
|
+
constructor(editor, state) {
|
|
912
|
+
super(editor);
|
|
913
|
+
// It does not affect data so should be enabled in read-only mode.
|
|
914
|
+
this.affectsData = false;
|
|
915
|
+
this._state = state;
|
|
916
|
+
this.isEnabled = false;
|
|
917
|
+
this.listenTo(this._state.results, 'change', () => {
|
|
918
|
+
this.isEnabled = this._state.results.length > 1;
|
|
919
|
+
});
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* @inheritDoc
|
|
923
|
+
*/
|
|
924
|
+
refresh() {
|
|
925
|
+
this.isEnabled = this._state.results.length > 1;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* @inheritDoc
|
|
929
|
+
*/
|
|
930
|
+
execute() {
|
|
931
|
+
const results = this._state.results;
|
|
932
|
+
const currentIndex = results.getIndex(this._state.highlightedResult);
|
|
933
|
+
const nextIndex = currentIndex + 1 >= results.length ?
|
|
934
|
+
0 : currentIndex + 1;
|
|
935
|
+
this._state.highlightedResult = this._state.results.get(nextIndex);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
941
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
942
|
+
*/
|
|
943
|
+
/**
|
|
944
|
+
* @module find-and-replace/findpreviouscommand
|
|
945
|
+
*/
|
|
946
|
+
/**
|
|
947
|
+
* The find previous command. Moves the highlight to the previous search result.
|
|
948
|
+
*
|
|
949
|
+
* It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
950
|
+
*/
|
|
951
|
+
class FindPreviousCommand extends FindNextCommand {
|
|
952
|
+
/**
|
|
953
|
+
* @inheritDoc
|
|
954
|
+
*/
|
|
955
|
+
execute() {
|
|
956
|
+
const results = this._state.results;
|
|
957
|
+
const currentIndex = results.getIndex(this._state.highlightedResult);
|
|
958
|
+
const previousIndex = currentIndex - 1 < 0 ?
|
|
959
|
+
this._state.results.length - 1 : currentIndex - 1;
|
|
960
|
+
this._state.highlightedResult = this._state.results.get(previousIndex);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
966
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
967
|
+
*/
|
|
968
|
+
/**
|
|
969
|
+
* The object storing find and replace plugin state for a given editor instance.
|
|
970
|
+
*/
|
|
971
|
+
class FindAndReplaceState extends ObservableMixin() {
|
|
972
|
+
/**
|
|
973
|
+
* Creates an instance of the state.
|
|
974
|
+
*/
|
|
975
|
+
constructor(model) {
|
|
976
|
+
super();
|
|
977
|
+
this.set('results', new Collection());
|
|
978
|
+
this.set('highlightedResult', null);
|
|
979
|
+
this.set('highlightedOffset', 0);
|
|
980
|
+
this.set('searchText', '');
|
|
981
|
+
this.set('replaceText', '');
|
|
982
|
+
this.set('lastSearchCallback', null);
|
|
983
|
+
this.set('matchCase', false);
|
|
984
|
+
this.set('matchWholeWords', false);
|
|
985
|
+
this.results.on('change', (eventInfo, { removed, index }) => {
|
|
986
|
+
if (Array.from(removed).length) {
|
|
987
|
+
let highlightedResultRemoved = false;
|
|
988
|
+
model.change(writer => {
|
|
989
|
+
for (const removedResult of removed) {
|
|
990
|
+
if (this.highlightedResult === removedResult) {
|
|
991
|
+
highlightedResultRemoved = true;
|
|
992
|
+
}
|
|
993
|
+
if (model.markers.has(removedResult.marker.name)) {
|
|
994
|
+
writer.removeMarker(removedResult.marker);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
});
|
|
998
|
+
if (highlightedResultRemoved) {
|
|
999
|
+
const nextHighlightedIndex = index >= this.results.length ? 0 : index;
|
|
1000
|
+
this.highlightedResult = this.results.get(nextHighlightedIndex);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
this.on('change:highlightedResult', () => {
|
|
1005
|
+
this.refreshHighlightOffset();
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Cleans the state up and removes markers from the model.
|
|
1010
|
+
*/
|
|
1011
|
+
clear(model) {
|
|
1012
|
+
this.searchText = '';
|
|
1013
|
+
model.change(writer => {
|
|
1014
|
+
if (this.highlightedResult) {
|
|
1015
|
+
const oldMatchId = this.highlightedResult.marker.name.split(':')[1];
|
|
1016
|
+
const oldMarker = model.markers.get(`findResultHighlighted:${oldMatchId}`);
|
|
1017
|
+
if (oldMarker) {
|
|
1018
|
+
writer.removeMarker(oldMarker);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
[...this.results].forEach(({ marker }) => {
|
|
1022
|
+
writer.removeMarker(marker);
|
|
1023
|
+
});
|
|
1024
|
+
});
|
|
1025
|
+
this.results.clear();
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Refreshes the highlight result offset based on it's index within the result list.
|
|
1029
|
+
*/
|
|
1030
|
+
refreshHighlightOffset() {
|
|
1031
|
+
const { highlightedResult, results } = this;
|
|
1032
|
+
const sortMapping = { before: -1, same: 0, after: 1, different: 1 };
|
|
1033
|
+
if (highlightedResult) {
|
|
1034
|
+
this.highlightedOffset = Array.from(results)
|
|
1035
|
+
.sort((a, b) => sortMapping[a.marker.getStart().compareWith(b.marker.getStart())])
|
|
1036
|
+
.indexOf(highlightedResult) + 1;
|
|
1037
|
+
}
|
|
1038
|
+
else {
|
|
1039
|
+
this.highlightedOffset = 0;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
1046
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
1047
|
+
*/
|
|
1048
|
+
/**
|
|
1049
|
+
* A set of helpers related to find and replace.
|
|
1050
|
+
*/
|
|
1051
|
+
class FindAndReplaceUtils extends Plugin {
|
|
1052
|
+
/**
|
|
1053
|
+
* @inheritDoc
|
|
1054
|
+
*/
|
|
1055
|
+
static get pluginName() {
|
|
1056
|
+
return 'FindAndReplaceUtils';
|
|
1057
|
+
}
|
|
1058
|
+
/**
|
|
1059
|
+
* Executes findCallback and updates search results list.
|
|
1060
|
+
*
|
|
1061
|
+
* @param range The model range to scan for matches.
|
|
1062
|
+
* @param model The model.
|
|
1063
|
+
* @param findCallback The callback that should return `true` if provided text matches the search term.
|
|
1064
|
+
* @param startResults An optional collection of find matches that the function should
|
|
1065
|
+
* start with. This would be a collection returned by a previous `updateFindResultFromRange()` call.
|
|
1066
|
+
* @returns A collection of objects describing find match.
|
|
1067
|
+
*
|
|
1068
|
+
* An example structure:
|
|
1069
|
+
*
|
|
1070
|
+
* ```js
|
|
1071
|
+
* {
|
|
1072
|
+
* id: resultId,
|
|
1073
|
+
* label: foundItem.label,
|
|
1074
|
+
* marker
|
|
1075
|
+
* }
|
|
1076
|
+
* ```
|
|
1077
|
+
*/
|
|
1078
|
+
updateFindResultFromRange(range, model, findCallback, startResults) {
|
|
1079
|
+
const results = startResults || new Collection();
|
|
1080
|
+
const checkIfResultAlreadyOnList = (marker) => results.find(markerItem => {
|
|
1081
|
+
const { marker: resultsMarker } = markerItem;
|
|
1082
|
+
const resultRange = resultsMarker.getRange();
|
|
1083
|
+
const markerRange = marker.getRange();
|
|
1084
|
+
return resultRange.isEqual(markerRange);
|
|
1085
|
+
});
|
|
1086
|
+
model.change(writer => {
|
|
1087
|
+
[...range].forEach(({ type, item }) => {
|
|
1088
|
+
if (type === 'elementStart') {
|
|
1089
|
+
if (model.schema.checkChild(item, '$text')) {
|
|
1090
|
+
const foundItems = findCallback({
|
|
1091
|
+
item,
|
|
1092
|
+
text: this.rangeToText(model.createRangeIn(item))
|
|
1093
|
+
});
|
|
1094
|
+
if (!foundItems) {
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
foundItems.forEach(foundItem => {
|
|
1098
|
+
const resultId = `findResult:${uid()}`;
|
|
1099
|
+
const marker = writer.addMarker(resultId, {
|
|
1100
|
+
usingOperation: false,
|
|
1101
|
+
affectsData: false,
|
|
1102
|
+
range: writer.createRange(writer.createPositionAt(item, foundItem.start), writer.createPositionAt(item, foundItem.end))
|
|
1103
|
+
});
|
|
1104
|
+
const index = findInsertIndex(results, marker);
|
|
1105
|
+
if (!checkIfResultAlreadyOnList(marker)) {
|
|
1106
|
+
results.add({
|
|
1107
|
+
id: resultId,
|
|
1108
|
+
label: foundItem.label,
|
|
1109
|
+
marker
|
|
1110
|
+
}, index);
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
});
|
|
1117
|
+
return results;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Returns text representation of a range. The returned text length should be the same as range length.
|
|
1121
|
+
* In order to achieve this, this function will replace inline elements (text-line) as new line character ("\n").
|
|
1122
|
+
*
|
|
1123
|
+
* @param range The model range.
|
|
1124
|
+
* @returns The text content of the provided range.
|
|
1125
|
+
*/
|
|
1126
|
+
rangeToText(range) {
|
|
1127
|
+
return Array.from(range.getItems()).reduce((rangeText, node) => {
|
|
1128
|
+
// Trim text to a last occurrence of an inline element and update range start.
|
|
1129
|
+
if (!(node.is('$text') || node.is('$textProxy'))) {
|
|
1130
|
+
// Editor has only one inline element defined in schema: `<softBreak>` which is treated as new line character in blocks.
|
|
1131
|
+
// Special handling might be needed for other inline elements (inline widgets).
|
|
1132
|
+
return `${rangeText}\n`;
|
|
1133
|
+
}
|
|
1134
|
+
return rangeText + node.data;
|
|
1135
|
+
}, '');
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* Creates a text matching callback for a specified search term and matching options.
|
|
1139
|
+
*
|
|
1140
|
+
* @param searchTerm The search term.
|
|
1141
|
+
* @param options Matching options.
|
|
1142
|
+
* - options.matchCase=false If set to `true` letter casing will be ignored.
|
|
1143
|
+
* - options.wholeWords=false If set to `true` only whole words that match `callbackOrText` will be matched.
|
|
1144
|
+
*/
|
|
1145
|
+
findByTextCallback(searchTerm, options) {
|
|
1146
|
+
let flags = 'gu';
|
|
1147
|
+
if (!options.matchCase) {
|
|
1148
|
+
flags += 'i';
|
|
1149
|
+
}
|
|
1150
|
+
let regExpQuery = `(${escapeRegExp(searchTerm)})`;
|
|
1151
|
+
if (options.wholeWords) {
|
|
1152
|
+
const nonLetterGroup = '[^a-zA-Z\u00C0-\u024F\u1E00-\u1EFF]';
|
|
1153
|
+
if (!new RegExp('^' + nonLetterGroup).test(searchTerm)) {
|
|
1154
|
+
regExpQuery = `(^|${nonLetterGroup}|_)${regExpQuery}`;
|
|
1155
|
+
}
|
|
1156
|
+
if (!new RegExp(nonLetterGroup + '$').test(searchTerm)) {
|
|
1157
|
+
regExpQuery = `${regExpQuery}(?=_|${nonLetterGroup}|$)`;
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
const regExp = new RegExp(regExpQuery, flags);
|
|
1161
|
+
function findCallback({ text }) {
|
|
1162
|
+
const matches = [...text.matchAll(regExp)];
|
|
1163
|
+
return matches.map(regexpMatchToFindResult);
|
|
1164
|
+
}
|
|
1165
|
+
return findCallback;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
// Finds the appropriate index in the resultsList Collection.
|
|
1169
|
+
function findInsertIndex(resultsList, markerToInsert) {
|
|
1170
|
+
const result = resultsList.find(({ marker }) => {
|
|
1171
|
+
return markerToInsert.getStart().isBefore(marker.getStart());
|
|
1172
|
+
});
|
|
1173
|
+
return result ? resultsList.getIndex(result) : resultsList.length;
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Maps RegExp match result to find result.
|
|
1177
|
+
*/
|
|
1178
|
+
function regexpMatchToFindResult(matchResult) {
|
|
1179
|
+
const lastGroupIndex = matchResult.length - 1;
|
|
1180
|
+
let startOffset = matchResult.index;
|
|
1181
|
+
// Searches with match all flag have an extra matching group with empty string or white space matched before the word.
|
|
1182
|
+
// If the search term starts with the space already, there is no extra group even with match all flag on.
|
|
1183
|
+
if (matchResult.length === 3) {
|
|
1184
|
+
startOffset += matchResult[1].length;
|
|
1185
|
+
}
|
|
1186
|
+
return {
|
|
1187
|
+
label: matchResult[lastGroupIndex],
|
|
1188
|
+
start: startOffset,
|
|
1189
|
+
end: startOffset + matchResult[lastGroupIndex].length
|
|
1190
|
+
};
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
1195
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
1196
|
+
*/
|
|
1197
|
+
/**
|
|
1198
|
+
* @module find-and-replace/findandreplaceediting
|
|
1199
|
+
*/
|
|
1200
|
+
const HIGHLIGHT_CLASS = 'ck-find-result_selected';
|
|
1201
|
+
/**
|
|
1202
|
+
* Implements the editing part for find and replace plugin. For example conversion, commands etc.
|
|
1203
|
+
*/
|
|
1204
|
+
class FindAndReplaceEditing extends Plugin {
|
|
1205
|
+
constructor() {
|
|
1206
|
+
super(...arguments);
|
|
1207
|
+
/**
|
|
1208
|
+
* Reacts to document changes in order to update search list.
|
|
1209
|
+
*/
|
|
1210
|
+
this._onDocumentChange = () => {
|
|
1211
|
+
const changedNodes = new Set();
|
|
1212
|
+
const removedMarkers = new Set();
|
|
1213
|
+
const model = this.editor.model;
|
|
1214
|
+
const { results } = this.state;
|
|
1215
|
+
const changes = model.document.differ.getChanges();
|
|
1216
|
+
const changedMarkers = model.document.differ.getChangedMarkers();
|
|
1217
|
+
// Get nodes in which changes happened to re-run a search callback on them.
|
|
1218
|
+
changes.forEach(change => {
|
|
1219
|
+
if (!change.position) {
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
if (change.name === '$text' || (change.position.nodeAfter && model.schema.isInline(change.position.nodeAfter))) {
|
|
1223
|
+
changedNodes.add(change.position.parent);
|
|
1224
|
+
[...model.markers.getMarkersAtPosition(change.position)].forEach(markerAtChange => {
|
|
1225
|
+
removedMarkers.add(markerAtChange.name);
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
else if (change.type === 'insert' && change.position.nodeAfter) {
|
|
1229
|
+
changedNodes.add(change.position.nodeAfter);
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
// Get markers from removed nodes also.
|
|
1233
|
+
changedMarkers.forEach(({ name, data: { newRange } }) => {
|
|
1234
|
+
if (newRange && newRange.start.root.rootName === '$graveyard') {
|
|
1235
|
+
removedMarkers.add(name);
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
// Get markers from the updated nodes and remove all (search will be re-run on these nodes).
|
|
1239
|
+
changedNodes.forEach(node => {
|
|
1240
|
+
const markersInNode = [...model.markers.getMarkersIntersectingRange(model.createRangeIn(node))];
|
|
1241
|
+
markersInNode.forEach(marker => removedMarkers.add(marker.name));
|
|
1242
|
+
});
|
|
1243
|
+
// Remove results from the changed part of content.
|
|
1244
|
+
removedMarkers.forEach(markerName => {
|
|
1245
|
+
if (!results.has(markerName)) {
|
|
1246
|
+
return;
|
|
1247
|
+
}
|
|
1248
|
+
if (results.get(markerName) === this.state.highlightedResult) {
|
|
1249
|
+
this.state.highlightedResult = null;
|
|
1250
|
+
}
|
|
1251
|
+
results.remove(markerName);
|
|
1252
|
+
});
|
|
1253
|
+
// Run search callback again on updated nodes.
|
|
1254
|
+
const changedSearchResults = [];
|
|
1255
|
+
const findAndReplaceUtils = this.editor.plugins.get('FindAndReplaceUtils');
|
|
1256
|
+
changedNodes.forEach(nodeToCheck => {
|
|
1257
|
+
const changedNodeSearchResults = findAndReplaceUtils.updateFindResultFromRange(model.createRangeOn(nodeToCheck), model, this.state.lastSearchCallback, results);
|
|
1258
|
+
changedSearchResults.push(...changedNodeSearchResults);
|
|
1259
|
+
});
|
|
1260
|
+
changedMarkers.forEach(markerToCheck => {
|
|
1261
|
+
// Handle search result highlight update when T&C plugin is active.
|
|
1262
|
+
// Lookup is performed only on newly inserted markers.
|
|
1263
|
+
if (markerToCheck.data.newRange) {
|
|
1264
|
+
const changedNodeSearchResults = findAndReplaceUtils.updateFindResultFromRange(markerToCheck.data.newRange, model, this.state.lastSearchCallback, results);
|
|
1265
|
+
changedSearchResults.push(...changedNodeSearchResults);
|
|
1266
|
+
}
|
|
1267
|
+
});
|
|
1268
|
+
if (!this.state.highlightedResult && changedSearchResults.length) {
|
|
1269
|
+
// If there are found phrases but none is selected, select the first one.
|
|
1270
|
+
this.state.highlightedResult = changedSearchResults[0];
|
|
1271
|
+
}
|
|
1272
|
+
else {
|
|
1273
|
+
// If there is already highlight item then refresh highlight offset after appending new items.
|
|
1274
|
+
this.state.refreshHighlightOffset();
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
}
|
|
1278
|
+
/**
|
|
1279
|
+
* @inheritDoc
|
|
1280
|
+
*/
|
|
1281
|
+
static get requires() {
|
|
1282
|
+
return [FindAndReplaceUtils];
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* @inheritDoc
|
|
1286
|
+
*/
|
|
1287
|
+
static get pluginName() {
|
|
1288
|
+
return 'FindAndReplaceEditing';
|
|
1289
|
+
}
|
|
1290
|
+
/**
|
|
1291
|
+
* @inheritDoc
|
|
1292
|
+
*/
|
|
1293
|
+
init() {
|
|
1294
|
+
this.state = new FindAndReplaceState(this.editor.model);
|
|
1295
|
+
this.set('_isSearchActive', false);
|
|
1296
|
+
this._defineConverters();
|
|
1297
|
+
this._defineCommands();
|
|
1298
|
+
this.listenTo(this.state, 'change:highlightedResult', (eventInfo, name, newValue, oldValue) => {
|
|
1299
|
+
const { model } = this.editor;
|
|
1300
|
+
model.change(writer => {
|
|
1301
|
+
if (oldValue) {
|
|
1302
|
+
const oldMatchId = oldValue.marker.name.split(':')[1];
|
|
1303
|
+
const oldMarker = model.markers.get(`findResultHighlighted:${oldMatchId}`);
|
|
1304
|
+
if (oldMarker) {
|
|
1305
|
+
writer.removeMarker(oldMarker);
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
if (newValue) {
|
|
1309
|
+
const newMatchId = newValue.marker.name.split(':')[1];
|
|
1310
|
+
writer.addMarker(`findResultHighlighted:${newMatchId}`, {
|
|
1311
|
+
usingOperation: false,
|
|
1312
|
+
affectsData: false,
|
|
1313
|
+
range: newValue.marker.getRange()
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
});
|
|
1317
|
+
});
|
|
1318
|
+
/* istanbul ignore next -- @preserve */
|
|
1319
|
+
const scrollToHighlightedResult = (eventInfo, name, newValue) => {
|
|
1320
|
+
if (newValue) {
|
|
1321
|
+
const domConverter = this.editor.editing.view.domConverter;
|
|
1322
|
+
const viewRange = this.editor.editing.mapper.toViewRange(newValue.marker.getRange());
|
|
1323
|
+
scrollViewportToShowTarget({
|
|
1324
|
+
target: domConverter.viewRangeToDom(viewRange),
|
|
1325
|
+
viewportOffset: 40
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
};
|
|
1329
|
+
const debouncedScrollListener = debounce(scrollToHighlightedResult.bind(this), 32);
|
|
1330
|
+
// Debounce scroll as highlight might be changed very frequently, e.g. when there's a replace all command.
|
|
1331
|
+
this.listenTo(this.state, 'change:highlightedResult', debouncedScrollListener, { priority: 'low' });
|
|
1332
|
+
// It's possible that the editor will get destroyed before debounced call kicks in.
|
|
1333
|
+
// This would result with accessing a view three that is no longer in DOM.
|
|
1334
|
+
this.listenTo(this.editor, 'destroy', debouncedScrollListener.cancel);
|
|
1335
|
+
this.on('change:_isSearchActive', (evt, name, isSearchActive) => {
|
|
1336
|
+
if (isSearchActive) {
|
|
1337
|
+
this.listenTo(this.editor.model.document, 'change:data', this._onDocumentChange);
|
|
1338
|
+
}
|
|
1339
|
+
else {
|
|
1340
|
+
this.stopListening(this.editor.model.document, 'change:data', this._onDocumentChange);
|
|
1341
|
+
}
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Initiate a search.
|
|
1346
|
+
*/
|
|
1347
|
+
find(callbackOrText, findAttributes) {
|
|
1348
|
+
this._isSearchActive = true;
|
|
1349
|
+
this.editor.execute('find', callbackOrText, findAttributes);
|
|
1350
|
+
return this.state.results;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Stops active results from updating, and clears out the results.
|
|
1354
|
+
*/
|
|
1355
|
+
stop() {
|
|
1356
|
+
this.state.clear(this.editor.model);
|
|
1357
|
+
this._isSearchActive = false;
|
|
1358
|
+
}
|
|
1359
|
+
/**
|
|
1360
|
+
* Sets up the commands.
|
|
1361
|
+
*/
|
|
1362
|
+
_defineCommands() {
|
|
1363
|
+
this.editor.commands.add('find', new FindCommand(this.editor, this.state));
|
|
1364
|
+
this.editor.commands.add('findNext', new FindNextCommand(this.editor, this.state));
|
|
1365
|
+
this.editor.commands.add('findPrevious', new FindPreviousCommand(this.editor, this.state));
|
|
1366
|
+
this.editor.commands.add('replace', new ReplaceCommand(this.editor, this.state));
|
|
1367
|
+
this.editor.commands.add('replaceAll', new ReplaceAllCommand(this.editor, this.state));
|
|
1368
|
+
}
|
|
1369
|
+
/**
|
|
1370
|
+
* Sets up the marker downcast converters for search results highlighting.
|
|
1371
|
+
*/
|
|
1372
|
+
_defineConverters() {
|
|
1373
|
+
const { editor } = this;
|
|
1374
|
+
// Setup the marker highlighting conversion.
|
|
1375
|
+
editor.conversion.for('editingDowncast').markerToHighlight({
|
|
1376
|
+
model: 'findResult',
|
|
1377
|
+
view: ({ markerName }) => {
|
|
1378
|
+
const [, id] = markerName.split(':');
|
|
1379
|
+
// Marker removal from the view has a bug: https://github.com/ckeditor/ckeditor5/issues/7499
|
|
1380
|
+
// A minimal option is to return a new object for each converted marker...
|
|
1381
|
+
return {
|
|
1382
|
+
name: 'span',
|
|
1383
|
+
classes: ['ck-find-result'],
|
|
1384
|
+
attributes: {
|
|
1385
|
+
// ...however, adding a unique attribute should be future-proof..
|
|
1386
|
+
'data-find-result': id
|
|
1387
|
+
}
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
1390
|
+
});
|
|
1391
|
+
editor.conversion.for('editingDowncast').markerToHighlight({
|
|
1392
|
+
model: 'findResultHighlighted',
|
|
1393
|
+
view: ({ markerName }) => {
|
|
1394
|
+
const [, id] = markerName.split(':');
|
|
1395
|
+
// Marker removal from the view has a bug: https://github.com/ckeditor/ckeditor5/issues/7499
|
|
1396
|
+
// A minimal option is to return a new object for each converted marker...
|
|
1397
|
+
return {
|
|
1398
|
+
name: 'span',
|
|
1399
|
+
classes: [HIGHLIGHT_CLASS],
|
|
1400
|
+
attributes: {
|
|
1401
|
+
// ...however, adding a unique attribute should be future-proof..
|
|
1402
|
+
'data-find-result': id
|
|
1403
|
+
}
|
|
1404
|
+
};
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
1412
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
1413
|
+
*/
|
|
1414
|
+
/**
|
|
1415
|
+
* @module find-and-replace/findandreplace
|
|
1416
|
+
*/
|
|
1417
|
+
/**
|
|
1418
|
+
* The find and replace plugin.
|
|
1419
|
+
*
|
|
1420
|
+
* For a detailed overview, check the {@glink features/find-and-replace Find and replace feature documentation}.
|
|
1421
|
+
*
|
|
1422
|
+
* This is a "glue" plugin which loads the following plugins:
|
|
1423
|
+
*
|
|
1424
|
+
* * The {@link module:find-and-replace/findandreplaceediting~FindAndReplaceEditing find and replace editing feature},
|
|
1425
|
+
* * The {@link module:find-and-replace/findandreplaceui~FindAndReplaceUI find and replace UI feature}
|
|
1426
|
+
*/
|
|
1427
|
+
class FindAndReplace extends Plugin {
|
|
1428
|
+
/**
|
|
1429
|
+
* @inheritDoc
|
|
1430
|
+
*/
|
|
1431
|
+
static get requires() {
|
|
1432
|
+
return [FindAndReplaceEditing, FindAndReplaceUI];
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* @inheritDoc
|
|
1436
|
+
*/
|
|
1437
|
+
static get pluginName() {
|
|
1438
|
+
return 'FindAndReplace';
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* @inheritDoc
|
|
1442
|
+
*/
|
|
1443
|
+
init() {
|
|
1444
|
+
const ui = this.editor.plugins.get('FindAndReplaceUI');
|
|
1445
|
+
const findAndReplaceEditing = this.editor.plugins.get('FindAndReplaceEditing');
|
|
1446
|
+
const state = findAndReplaceEditing.state;
|
|
1447
|
+
ui.on('findNext', (event, data) => {
|
|
1448
|
+
// Data is contained only for the "find" button.
|
|
1449
|
+
if (data) {
|
|
1450
|
+
state.searchText = data.searchText;
|
|
1451
|
+
findAndReplaceEditing.find(data.searchText, data);
|
|
1452
|
+
}
|
|
1453
|
+
else {
|
|
1454
|
+
// Find next arrow button press.
|
|
1455
|
+
this.editor.execute('findNext');
|
|
1456
|
+
}
|
|
1457
|
+
});
|
|
1458
|
+
ui.on('findPrevious', (event, data) => {
|
|
1459
|
+
if (data && state.searchText !== data.searchText) {
|
|
1460
|
+
findAndReplaceEditing.find(data.searchText);
|
|
1461
|
+
}
|
|
1462
|
+
else {
|
|
1463
|
+
// Subsequent calls.
|
|
1464
|
+
this.editor.execute('findPrevious');
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1467
|
+
ui.on('replace', (event, data) => {
|
|
1468
|
+
if (state.searchText !== data.searchText) {
|
|
1469
|
+
findAndReplaceEditing.find(data.searchText);
|
|
1470
|
+
}
|
|
1471
|
+
const highlightedResult = state.highlightedResult;
|
|
1472
|
+
if (highlightedResult) {
|
|
1473
|
+
this.editor.execute('replace', data.replaceText, highlightedResult);
|
|
1474
|
+
}
|
|
1475
|
+
});
|
|
1476
|
+
ui.on('replaceAll', (event, data) => {
|
|
1477
|
+
// The state hadn't been yet built for this search text.
|
|
1478
|
+
if (state.searchText !== data.searchText) {
|
|
1479
|
+
findAndReplaceEditing.find(data.searchText);
|
|
1480
|
+
}
|
|
1481
|
+
this.editor.execute('replaceAll', data.replaceText, state.results);
|
|
1482
|
+
});
|
|
1483
|
+
// Reset the state when the user invalidated last search results, for instance,
|
|
1484
|
+
// by starting typing another search query or changing options.
|
|
1485
|
+
ui.on('searchReseted', () => {
|
|
1486
|
+
state.clear(this.editor.model);
|
|
1487
|
+
findAndReplaceEditing.stop();
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
export { FindAndReplace, FindAndReplaceEditing, FindAndReplaceUI, FindAndReplaceUtils, FindCommand, FindNextCommand, FindPreviousCommand, ReplaceAllCommand, ReplaceCommand };
|
|
1493
|
+
//# sourceMappingURL=index.js.map
|