@ckeditor/ckeditor5-find-and-replace 35.3.2 → 36.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/build/find-and-replace.js +2 -2
- package/build/translations/ug.js +1 -0
- package/lang/translations/ug.po +69 -0
- package/package.json +20 -20
- package/src/findandreplace.js +1 -1
- package/src/findandreplaceediting.js +14 -5
- package/src/findandreplacestate.js +1 -1
- package/src/findandreplaceui.js +17 -10
- package/src/findandreplaceutils.js +179 -0
- package/src/findcommand.js +4 -4
- package/src/findnextcommand.js +1 -1
- package/src/findpreviouscommand.js +1 -1
- package/src/index.js +2 -1
- package/src/replaceallcommand.js +4 -4
- package/src/replacecommand.js +1 -1
- package/src/ui/findandreplaceformview.js +1 -1
- package/theme/findandreplace.css +1 -1
- package/theme/findandreplaceform.css +1 -1
- package/build/find-and-replace.js.map +0 -1
- package/src/utils.js +0 -166
|
@@ -0,0 +1,179 @@
|
|
|
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
|
+
/**
|
|
7
|
+
* @module find-and-replace/findandreplaceutils
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Plugin } from 'ckeditor5/src/core';
|
|
11
|
+
import { Collection, uid } from 'ckeditor5/src/utils';
|
|
12
|
+
import { escapeRegExp } from 'lodash-es';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A set of helpers related to find and replace.
|
|
16
|
+
*/
|
|
17
|
+
export default class FindAndReplaceUtils extends Plugin {
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
static get pluginName() {
|
|
22
|
+
return 'FindAndReplaceUtils';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Executes findCallback and updates search results list.
|
|
27
|
+
*
|
|
28
|
+
* @param {module:engine/model/range~Range} range The model range to scan for matches.
|
|
29
|
+
* @param {module:engine/model/model~Model} model The model.
|
|
30
|
+
* @param {Function} findCallback The callback that should return `true` if provided text matches the search term.
|
|
31
|
+
* @param {module:utils/collection~Collection} [startResults] An optional collection of find matches that the function should
|
|
32
|
+
* start with. This would be a collection returned by a previous `updateFindResultFromRange()` call.
|
|
33
|
+
* @returns {module:utils/collection~Collection} A collection of objects describing find match.
|
|
34
|
+
*
|
|
35
|
+
* An example structure:
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* {
|
|
39
|
+
* id: resultId,
|
|
40
|
+
* label: foundItem.label,
|
|
41
|
+
* marker
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
updateFindResultFromRange( range, model, findCallback, startResults ) {
|
|
46
|
+
const results = startResults || new Collection();
|
|
47
|
+
|
|
48
|
+
model.change( writer => {
|
|
49
|
+
[ ...range ].forEach( ( { type, item } ) => {
|
|
50
|
+
if ( type === 'elementStart' ) {
|
|
51
|
+
if ( model.schema.checkChild( item, '$text' ) ) {
|
|
52
|
+
const foundItems = findCallback( {
|
|
53
|
+
item,
|
|
54
|
+
text: this.rangeToText( model.createRangeIn( item ) )
|
|
55
|
+
} );
|
|
56
|
+
|
|
57
|
+
if ( !foundItems ) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
foundItems.forEach( foundItem => {
|
|
62
|
+
const resultId = `findResult:${ uid() }`;
|
|
63
|
+
const marker = writer.addMarker( resultId, {
|
|
64
|
+
usingOperation: false,
|
|
65
|
+
affectsData: false,
|
|
66
|
+
range: writer.createRange(
|
|
67
|
+
writer.createPositionAt( item, foundItem.start ),
|
|
68
|
+
writer.createPositionAt( item, foundItem.end )
|
|
69
|
+
)
|
|
70
|
+
} );
|
|
71
|
+
|
|
72
|
+
const index = findInsertIndex( results, marker );
|
|
73
|
+
|
|
74
|
+
results.add(
|
|
75
|
+
{
|
|
76
|
+
id: resultId,
|
|
77
|
+
label: foundItem.label,
|
|
78
|
+
marker
|
|
79
|
+
},
|
|
80
|
+
index
|
|
81
|
+
);
|
|
82
|
+
} );
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} );
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
return results;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Returns text representation of a range. The returned text length should be the same as range length.
|
|
93
|
+
* In order to achieve this, this function will replace inline elements (text-line) as new line character ("\n").
|
|
94
|
+
*
|
|
95
|
+
* @param {module:engine/model/range~Range} range The model range.
|
|
96
|
+
* @returns {String} The text content of the provided range.
|
|
97
|
+
*/
|
|
98
|
+
rangeToText( range ) {
|
|
99
|
+
return Array.from( range.getItems() ).reduce( ( rangeText, node ) => {
|
|
100
|
+
// Trim text to a last occurrence of an inline element and update range start.
|
|
101
|
+
if ( !( node.is( 'text' ) || node.is( 'textProxy' ) ) ) {
|
|
102
|
+
// Editor has only one inline element defined in schema: `<softBreak>` which is treated as new line character in blocks.
|
|
103
|
+
// Special handling might be needed for other inline elements (inline widgets).
|
|
104
|
+
return `${ rangeText }\n`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return rangeText + node.data;
|
|
108
|
+
}, '' );
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Creates a text matching callback for a specified search term and matching options.
|
|
113
|
+
*
|
|
114
|
+
* @param {String} searchTerm The search term.
|
|
115
|
+
* @param {Object} [options] Matching options.
|
|
116
|
+
* @param {Boolean} [options.matchCase=false] If set to `true` letter casing will be ignored.
|
|
117
|
+
* @param {Boolean} [options.wholeWords=false] If set to `true` only whole words that match `callbackOrText` will be matched.
|
|
118
|
+
* @returns {Function}
|
|
119
|
+
*/
|
|
120
|
+
findByTextCallback( searchTerm, options ) {
|
|
121
|
+
let flags = 'gu';
|
|
122
|
+
|
|
123
|
+
if ( !options.matchCase ) {
|
|
124
|
+
flags += 'i';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
let regExpQuery = `(${ escapeRegExp( searchTerm ) })`;
|
|
128
|
+
|
|
129
|
+
if ( options.wholeWords ) {
|
|
130
|
+
const nonLetterGroup = '[^a-zA-Z\u00C0-\u024F\u1E00-\u1EFF]';
|
|
131
|
+
|
|
132
|
+
if ( !new RegExp( '^' + nonLetterGroup ).test( searchTerm ) ) {
|
|
133
|
+
regExpQuery = `(^|${ nonLetterGroup }|_)${ regExpQuery }`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if ( !new RegExp( nonLetterGroup + '$' ).test( searchTerm ) ) {
|
|
137
|
+
regExpQuery = `${ regExpQuery }(?=_|${ nonLetterGroup }|$)`;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const regExp = new RegExp( regExpQuery, flags );
|
|
142
|
+
|
|
143
|
+
function findCallback( { text } ) {
|
|
144
|
+
const matches = [ ...text.matchAll( regExp ) ];
|
|
145
|
+
|
|
146
|
+
return matches.map( regexpMatchToFindResult );
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return findCallback;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Finds the appropriate index in the resultsList Collection.
|
|
154
|
+
function findInsertIndex( resultsList, markerToInsert ) {
|
|
155
|
+
const result = resultsList.find( ( { marker } ) => {
|
|
156
|
+
return markerToInsert.getStart().isBefore( marker.getStart() );
|
|
157
|
+
} );
|
|
158
|
+
|
|
159
|
+
return result ? resultsList.getIndex( result ) : resultsList.length;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Maps RegExp match result to find result.
|
|
163
|
+
function regexpMatchToFindResult( matchResult ) {
|
|
164
|
+
const lastGroupIndex = matchResult.length - 1;
|
|
165
|
+
|
|
166
|
+
let startOffset = matchResult.index;
|
|
167
|
+
|
|
168
|
+
// Searches with match all flag have an extra matching group with empty string or white space matched before the word.
|
|
169
|
+
// If the search term starts with the space already, there is no extra group even with match all flag on.
|
|
170
|
+
if ( matchResult.length === 3 ) {
|
|
171
|
+
startOffset += matchResult[ 1 ].length;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
label: matchResult[ lastGroupIndex ],
|
|
176
|
+
start: startOffset,
|
|
177
|
+
end: startOffset + matchResult[ lastGroupIndex ].length
|
|
178
|
+
};
|
|
179
|
+
}
|
package/src/findcommand.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { Command } from 'ckeditor5/src/core';
|
|
11
|
-
import { updateFindResultFromRange, findByTextCallback } from './utils';
|
|
12
11
|
|
|
13
12
|
/**
|
|
14
13
|
* The find command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
@@ -53,12 +52,13 @@ export default class FindCommand extends Command {
|
|
|
53
52
|
execute( callbackOrText, { matchCase, wholeWords } = {} ) {
|
|
54
53
|
const { editor } = this;
|
|
55
54
|
const { model } = editor;
|
|
55
|
+
const findAndReplaceUtils = editor.plugins.get( 'FindAndReplaceUtils' );
|
|
56
56
|
|
|
57
57
|
let findCallback;
|
|
58
58
|
|
|
59
59
|
// Allow to execute `find()` on a plugin with a keyword only.
|
|
60
60
|
if ( typeof callbackOrText === 'string' ) {
|
|
61
|
-
findCallback = findByTextCallback( callbackOrText, { matchCase, wholeWords } );
|
|
61
|
+
findCallback = findAndReplaceUtils.findByTextCallback( callbackOrText, { matchCase, wholeWords } );
|
|
62
62
|
|
|
63
63
|
this._state.searchText = callbackOrText;
|
|
64
64
|
} else {
|
|
@@ -67,7 +67,7 @@ export default class FindCommand extends Command {
|
|
|
67
67
|
|
|
68
68
|
// Initial search is done on all nodes in all roots inside the content.
|
|
69
69
|
const results = model.document.getRootNames()
|
|
70
|
-
.reduce( ( ( currentResults, rootName ) => updateFindResultFromRange(
|
|
70
|
+
.reduce( ( ( currentResults, rootName ) => findAndReplaceUtils.updateFindResultFromRange(
|
|
71
71
|
model.createRangeIn( model.document.getRoot( rootName ) ),
|
|
72
72
|
model,
|
|
73
73
|
findCallback,
|
package/src/findnextcommand.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -8,3 +8,4 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
export { default as FindAndReplace } from './findandreplace';
|
|
11
|
+
export { default as FindAndReplaceUtils } from './findandreplaceutils';
|
package/src/replaceallcommand.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @license Copyright (c) 2003-
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
* @module find-and-replace/replaceallcommand
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import { updateFindResultFromRange, findByTextCallback } from './utils';
|
|
11
10
|
import { Collection } from 'ckeditor5/src/utils';
|
|
12
11
|
import ReplaceCommand from './replacecommand';
|
|
13
12
|
|
|
@@ -39,13 +38,14 @@ export default class ReplaceAllCommand extends ReplaceCommand {
|
|
|
39
38
|
execute( newText, textToReplace ) {
|
|
40
39
|
const { editor } = this;
|
|
41
40
|
const { model } = editor;
|
|
41
|
+
const findAndReplaceUtils = editor.plugins.get( 'FindAndReplaceUtils' );
|
|
42
42
|
|
|
43
43
|
const results = textToReplace instanceof Collection ?
|
|
44
44
|
textToReplace : model.document.getRootNames()
|
|
45
|
-
.reduce( ( ( currentResults, rootName ) => updateFindResultFromRange(
|
|
45
|
+
.reduce( ( ( currentResults, rootName ) => findAndReplaceUtils.updateFindResultFromRange(
|
|
46
46
|
model.createRangeIn( model.document.getRoot( rootName ) ),
|
|
47
47
|
model,
|
|
48
|
-
findByTextCallback( textToReplace, this._state ),
|
|
48
|
+
findAndReplaceUtils.findByTextCallback( textToReplace, this._state ),
|
|
49
49
|
currentResults
|
|
50
50
|
) ), null );
|
|
51
51
|
|
package/src/replacecommand.js
CHANGED
package/theme/findandreplace.css
CHANGED