@ckeditor/ckeditor5-find-and-replace 36.0.0 → 37.0.0-alpha.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/build/find-and-replace.js +1 -1
- package/package.json +28 -23
- package/src/findandreplace.d.ts +45 -0
- package/src/findandreplace.js +62 -75
- package/src/findandreplaceediting.d.ts +67 -0
- package/src/findandreplaceediting.js +178 -239
- package/src/findandreplacestate.d.ts +69 -0
- package/src/findandreplacestate.js +52 -123
- package/src/findandreplaceui.d.ts +60 -0
- package/src/findandreplaceui.js +114 -188
- package/src/findandreplaceutils.d.ts +72 -0
- package/src/findandreplaceutils.js +128 -164
- package/src/findcommand.d.ts +56 -0
- package/src/findcommand.js +53 -83
- package/src/findnextcommand.d.ts +40 -0
- package/src/findnextcommand.js +32 -50
- package/src/findpreviouscommand.d.ts +24 -0
- package/src/findpreviouscommand.js +10 -16
- package/src/index.d.ts +9 -0
- package/src/index.js +0 -2
- package/src/replaceallcommand.d.ts +40 -0
- package/src/replaceallcommand.js +35 -49
- package/src/replacecommand.d.ts +27 -0
- package/src/replacecommand.js +12 -68
- package/src/replacecommandbase.d.ts +31 -0
- package/src/replacecommandbase.js +50 -0
- package/src/ui/findandreplaceformview.d.ts +288 -0
- package/src/ui/findandreplaceformview.js +440 -817
|
@@ -0,0 +1,24 @@
|
|
|
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 find-and-replace/findpreviouscommand
|
|
7
|
+
*/
|
|
8
|
+
import FindNextCommand from './findnextcommand';
|
|
9
|
+
/**
|
|
10
|
+
* The find previous command. Moves the highlight to the previous search result.
|
|
11
|
+
*
|
|
12
|
+
* It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
13
|
+
*/
|
|
14
|
+
export default class FindPreviousCommand extends FindNextCommand {
|
|
15
|
+
/**
|
|
16
|
+
* @inheritDoc
|
|
17
|
+
*/
|
|
18
|
+
execute(): void;
|
|
19
|
+
}
|
|
20
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
21
|
+
interface CommandsMap {
|
|
22
|
+
'findPrevious': FindPreviousCommand;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -2,30 +2,24 @@
|
|
|
2
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
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module find-and-replace/findpreviouscommand
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import FindNextCommand from './findnextcommand';
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* The find previous command. Moves the highlight to the previous search result.
|
|
14
11
|
*
|
|
15
12
|
* It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
16
|
-
*
|
|
17
|
-
* @extends module:find-and-replace/findnextcommand~FindNextCommand
|
|
18
13
|
*/
|
|
19
14
|
export default class FindPreviousCommand extends FindNextCommand {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
15
|
+
/**
|
|
16
|
+
* @inheritDoc
|
|
17
|
+
*/
|
|
18
|
+
execute() {
|
|
19
|
+
const results = this._state.results;
|
|
20
|
+
const currentIndex = results.getIndex(this._state.highlightedResult);
|
|
21
|
+
const previousIndex = currentIndex - 1 < 0 ?
|
|
22
|
+
this._state.results.length - 1 : currentIndex - 1;
|
|
23
|
+
this._state.highlightedResult = this._state.results.get(previousIndex);
|
|
24
|
+
}
|
|
31
25
|
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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 find-and-replace
|
|
7
|
+
*/
|
|
8
|
+
export { default as FindAndReplace } from './findandreplace';
|
|
9
|
+
export { default as FindAndReplaceUtils } from './findandreplaceutils';
|
package/src/index.js
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
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
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module find-and-replace
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
export { default as FindAndReplace } from './findandreplace';
|
|
11
9
|
export { default as FindAndReplaceUtils } from './findandreplaceutils';
|
|
@@ -0,0 +1,40 @@
|
|
|
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 find-and-replace/replaceallcommand
|
|
7
|
+
*/
|
|
8
|
+
import { Collection } from 'ckeditor5/src/utils';
|
|
9
|
+
import type { ResultType } from './findandreplace';
|
|
10
|
+
import { ReplaceCommandBase } from './replacecommandbase';
|
|
11
|
+
/**
|
|
12
|
+
* The replace all command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
13
|
+
*/
|
|
14
|
+
export default class ReplaceAllCommand extends ReplaceCommandBase {
|
|
15
|
+
/**
|
|
16
|
+
* Replaces all the occurrences of `textToReplace` with a given `newText` string.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* replaceAllCommand.execute( 'replaceAll', 'new text replacement', 'text to replace' );
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Alternatively you can call it from editor instance:
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* editor.execute( 'replaceAll', 'new text', 'old text' );
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param newText Text that will be inserted to the editor for each match.
|
|
29
|
+
* @param textToReplace Text to be replaced or a collection of matches
|
|
30
|
+
* as returned by the find command.
|
|
31
|
+
*
|
|
32
|
+
* @fires module:core/command~Command#event:execute
|
|
33
|
+
*/
|
|
34
|
+
execute(newText: string, textToReplace: string | Collection<ResultType>): void;
|
|
35
|
+
}
|
|
36
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
37
|
+
interface CommandsMap {
|
|
38
|
+
'replaceAll': ReplaceAllCommand;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/replaceallcommand.js
CHANGED
|
@@ -2,60 +2,46 @@
|
|
|
2
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
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module find-and-replace/replaceallcommand
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import { Collection } from 'ckeditor5/src/utils';
|
|
11
|
-
import
|
|
12
|
-
|
|
9
|
+
import { ReplaceCommandBase } from './replacecommandbase';
|
|
13
10
|
/**
|
|
14
11
|
* The replace all command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
15
|
-
*
|
|
16
|
-
* @extends module:find-and-replace/replacecommand~ReplaceCommand
|
|
17
12
|
*/
|
|
18
|
-
export default class ReplaceAllCommand extends
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if ( results.length ) {
|
|
53
|
-
model.change( () => {
|
|
54
|
-
[ ...results ].forEach( searchResult => {
|
|
55
|
-
// Just reuse logic from the replace command to replace a single match.
|
|
56
|
-
super.execute( newText, searchResult );
|
|
57
|
-
} );
|
|
58
|
-
} );
|
|
59
|
-
}
|
|
60
|
-
}
|
|
13
|
+
export default class ReplaceAllCommand extends ReplaceCommandBase {
|
|
14
|
+
/**
|
|
15
|
+
* Replaces all the occurrences of `textToReplace` with a given `newText` string.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* replaceAllCommand.execute( 'replaceAll', 'new text replacement', 'text to replace' );
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Alternatively you can call it from editor instance:
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* editor.execute( 'replaceAll', 'new text', 'old text' );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param newText Text that will be inserted to the editor for each match.
|
|
28
|
+
* @param textToReplace Text to be replaced or a collection of matches
|
|
29
|
+
* as returned by the find command.
|
|
30
|
+
*
|
|
31
|
+
* @fires module:core/command~Command#event:execute
|
|
32
|
+
*/
|
|
33
|
+
execute(newText, textToReplace) {
|
|
34
|
+
const { editor } = this;
|
|
35
|
+
const { model } = editor;
|
|
36
|
+
const findAndReplaceUtils = editor.plugins.get('FindAndReplaceUtils');
|
|
37
|
+
const results = textToReplace instanceof Collection ?
|
|
38
|
+
textToReplace : model.document.getRootNames()
|
|
39
|
+
.reduce(((currentResults, rootName) => findAndReplaceUtils.updateFindResultFromRange(model.createRangeIn(model.document.getRoot(rootName)), model, findAndReplaceUtils.findByTextCallback(textToReplace, this._state), currentResults)), null);
|
|
40
|
+
if (results.length) {
|
|
41
|
+
[...results].forEach(searchResult => {
|
|
42
|
+
// Just reuse logic from the replace command to replace a single match.
|
|
43
|
+
this._replace(newText, searchResult);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
61
47
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 find-and-replace/replacecommand
|
|
7
|
+
*/
|
|
8
|
+
import type { ResultType } from './findandreplace';
|
|
9
|
+
import { ReplaceCommandBase } from './replacecommandbase';
|
|
10
|
+
/**
|
|
11
|
+
* The replace command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
12
|
+
*/
|
|
13
|
+
export default class ReplaceCommand extends ReplaceCommandBase {
|
|
14
|
+
/**
|
|
15
|
+
* Replace a given find result by a string or a callback.
|
|
16
|
+
*
|
|
17
|
+
* @param result A single result from the find command.
|
|
18
|
+
*
|
|
19
|
+
* @fires execute
|
|
20
|
+
*/
|
|
21
|
+
execute(replacementText: string, result: ResultType): void;
|
|
22
|
+
}
|
|
23
|
+
declare module '@ckeditor/ckeditor5-core' {
|
|
24
|
+
interface CommandsMap {
|
|
25
|
+
'replace': ReplaceCommand;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/replacecommand.js
CHANGED
|
@@ -2,75 +2,19 @@
|
|
|
2
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
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @module find-and-replace/replacecommand
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Command } from 'ckeditor5/src/core';
|
|
11
|
-
|
|
5
|
+
import { ReplaceCommandBase } from './replacecommandbase';
|
|
12
6
|
/**
|
|
13
7
|
* The replace command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
14
|
-
*
|
|
15
|
-
* @extends module:core/command~Command
|
|
16
8
|
*/
|
|
17
|
-
export default class ReplaceCommand extends
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.isEnabled = true;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The find and replace state object used for command operations.
|
|
32
|
-
*
|
|
33
|
-
* @protected
|
|
34
|
-
* @member {module:find-and-replace/findandreplacestate~FindAndReplaceState} #_state
|
|
35
|
-
*/
|
|
36
|
-
this._state = state;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Replace a given find result by a string or a callback.
|
|
41
|
-
*
|
|
42
|
-
* @param {String} replacementText
|
|
43
|
-
* @param {Object} result A single result from the find command.
|
|
44
|
-
*
|
|
45
|
-
* @fires module:core/command~Command#event:execute
|
|
46
|
-
*/
|
|
47
|
-
execute( replacementText, result ) {
|
|
48
|
-
const { model } = this.editor;
|
|
49
|
-
|
|
50
|
-
model.change( writer => {
|
|
51
|
-
const range = result.marker.getRange();
|
|
52
|
-
|
|
53
|
-
// Don't replace a result (marker) that found its way into the $graveyard (e.g. removed by collaborators).
|
|
54
|
-
if ( range.root.rootName === '$graveyard' ) {
|
|
55
|
-
this._state.results.remove( result );
|
|
56
|
-
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
let textAttributes = {};
|
|
61
|
-
|
|
62
|
-
for ( const item of range.getItems() ) {
|
|
63
|
-
if ( item.is( '$text' ) || item.is( '$textProxy' ) ) {
|
|
64
|
-
textAttributes = item.getAttributes();
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
model.insertContent( writer.createText( replacementText, textAttributes ), range );
|
|
70
|
-
|
|
71
|
-
if ( this._state.results.has( result ) ) {
|
|
72
|
-
this._state.results.remove( result );
|
|
73
|
-
}
|
|
74
|
-
} );
|
|
75
|
-
}
|
|
9
|
+
export default class ReplaceCommand extends ReplaceCommandBase {
|
|
10
|
+
/**
|
|
11
|
+
* Replace a given find result by a string or a callback.
|
|
12
|
+
*
|
|
13
|
+
* @param result A single result from the find command.
|
|
14
|
+
*
|
|
15
|
+
* @fires execute
|
|
16
|
+
*/
|
|
17
|
+
execute(replacementText, result) {
|
|
18
|
+
this._replace(replacementText, result);
|
|
19
|
+
}
|
|
76
20
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 find-and-replace/replacecommandbase
|
|
7
|
+
*/
|
|
8
|
+
import { Command, type Editor } from 'ckeditor5/src/core';
|
|
9
|
+
import type { ResultType } from './findandreplace';
|
|
10
|
+
import type FindAndReplaceState from './findandreplacestate';
|
|
11
|
+
export declare abstract class ReplaceCommandBase extends Command {
|
|
12
|
+
/**
|
|
13
|
+
* The find and replace state object used for command operations.
|
|
14
|
+
*/
|
|
15
|
+
protected _state: FindAndReplaceState;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new `ReplaceCommand` instance.
|
|
18
|
+
*
|
|
19
|
+
* @param editor Editor on which this command will be used.
|
|
20
|
+
* @param state An object to hold plugin state.
|
|
21
|
+
*/
|
|
22
|
+
constructor(editor: Editor, state: FindAndReplaceState);
|
|
23
|
+
abstract execute(...args: Array<unknown>): void;
|
|
24
|
+
/**
|
|
25
|
+
* Common logic for both `replace` commands.
|
|
26
|
+
* Replace a given find result by a string or a callback.
|
|
27
|
+
*
|
|
28
|
+
* @param result A single result from the find command.
|
|
29
|
+
*/
|
|
30
|
+
protected _replace(replacementText: string, result: ResultType): void;
|
|
31
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 find-and-replace/replacecommandbase
|
|
7
|
+
*/
|
|
8
|
+
import { Command } from 'ckeditor5/src/core';
|
|
9
|
+
export class ReplaceCommandBase extends Command {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new `ReplaceCommand` instance.
|
|
12
|
+
*
|
|
13
|
+
* @param editor Editor on which this command will be used.
|
|
14
|
+
* @param state An object to hold plugin state.
|
|
15
|
+
*/
|
|
16
|
+
constructor(editor, state) {
|
|
17
|
+
super(editor);
|
|
18
|
+
// The replace command is always enabled.
|
|
19
|
+
this.isEnabled = true;
|
|
20
|
+
this._state = state;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Common logic for both `replace` commands.
|
|
24
|
+
* Replace a given find result by a string or a callback.
|
|
25
|
+
*
|
|
26
|
+
* @param result A single result from the find command.
|
|
27
|
+
*/
|
|
28
|
+
_replace(replacementText, result) {
|
|
29
|
+
const { model } = this.editor;
|
|
30
|
+
model.change(writer => {
|
|
31
|
+
const range = result.marker.getRange();
|
|
32
|
+
// Don't replace a result (marker) that found its way into the $graveyard (e.g. removed by collaborators).
|
|
33
|
+
if (range.root.rootName === '$graveyard') {
|
|
34
|
+
this._state.results.remove(result);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
let textAttributes = {};
|
|
38
|
+
for (const item of range.getItems()) {
|
|
39
|
+
if (item.is('$text') || item.is('$textProxy')) {
|
|
40
|
+
textAttributes = item.getAttributes();
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
model.insertContent(writer.createText(replacementText, textAttributes), range);
|
|
45
|
+
if (this._state.results.has(result)) {
|
|
46
|
+
this._state.results.remove(result);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|