@ckeditor/ckeditor5-find-and-replace 30.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 +17 -0
- package/README.md +20 -0
- package/build/find-and-replace.js +5 -0
- package/build/translations/de.js +1 -0
- package/build/translations/gl.js +1 -0
- package/build/translations/hu.js +1 -0
- package/build/translations/it.js +1 -0
- package/build/translations/nl.js +1 -0
- package/build/translations/no.js +1 -0
- package/build/translations/ru.js +1 -0
- package/build/translations/sr-latn.js +1 -0
- package/build/translations/sr.js +1 -0
- package/build/translations/zh-cn.js +1 -0
- package/ckeditor5-metadata.json +18 -0
- package/lang/contexts.json +15 -0
- package/lang/translations/de.po +69 -0
- package/lang/translations/en.po +69 -0
- package/lang/translations/gl.po +69 -0
- package/lang/translations/hu.po +69 -0
- package/lang/translations/it.po +69 -0
- package/lang/translations/nl.po +69 -0
- package/lang/translations/no.po +69 -0
- package/lang/translations/ru.po +69 -0
- package/lang/translations/sr-latn.po +69 -0
- package/lang/translations/sr.po +69 -0
- package/lang/translations/zh-cn.po +69 -0
- package/package.json +62 -0
- package/src/findandreplace.js +97 -0
- package/src/findandreplaceediting.js +254 -0
- package/src/findandreplacestate.js +131 -0
- package/src/findandreplaceui.js +202 -0
- package/src/findcommand.js +95 -0
- package/src/findnextcommand.js +67 -0
- package/src/findpreviouscommand.js +31 -0
- package/src/index.js +10 -0
- package/src/replaceallcommand.js +61 -0
- package/src/replacecommand.js +69 -0
- package/src/ui/findandreplaceformview.js +827 -0
- package/src/utils.js +166 -0
- package/theme/findandreplace.css +13 -0
- package/theme/findandreplaceform.css +17 -0
- package/theme/icons/find-replace.svg +1 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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/findnextcommand
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Command } from 'ckeditor5/src/core';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The find next command. Moves the highlight to the next search result.
|
|
14
|
+
*
|
|
15
|
+
* It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
16
|
+
*
|
|
17
|
+
* @extends module:core/command~Command
|
|
18
|
+
*/
|
|
19
|
+
export default class FindNextCommand extends Command {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new `FindNextCommand` instance.
|
|
22
|
+
*
|
|
23
|
+
* @param {module:core/editor/editor~Editor} editor The editor on which this command will be used.
|
|
24
|
+
* @param {module:find-and-replace/findandreplacestate~FindAndReplaceState} state An object to hold plugin state.
|
|
25
|
+
*/
|
|
26
|
+
constructor( editor, state ) {
|
|
27
|
+
super( editor );
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The find and replace state object used for command operations.
|
|
31
|
+
*
|
|
32
|
+
* @protected
|
|
33
|
+
* @member {module:find-and-replace/findandreplacestate~FindAndReplaceState} #_state
|
|
34
|
+
*/
|
|
35
|
+
this._state = state;
|
|
36
|
+
|
|
37
|
+
this.isEnabled = false;
|
|
38
|
+
|
|
39
|
+
this.listenTo( this._state.results, 'change', () => {
|
|
40
|
+
this.isEnabled = this._state.results.length > 1;
|
|
41
|
+
} );
|
|
42
|
+
|
|
43
|
+
// Do not block the command if the editor goes into the read-only mode as it does not impact the data. See #9975.
|
|
44
|
+
this.listenTo( editor, 'change:isReadOnly', () => {
|
|
45
|
+
this.clearForceDisabled( 'readOnlyMode' );
|
|
46
|
+
} );
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @inheritDoc
|
|
51
|
+
*/
|
|
52
|
+
refresh() {
|
|
53
|
+
this.isEnabled = this._state.results.length > 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
execute() {
|
|
60
|
+
const results = this._state.results;
|
|
61
|
+
const currentIndex = results.getIndex( this._state.highlightedResult );
|
|
62
|
+
const nextIndex = currentIndex + 1 >= results.length ?
|
|
63
|
+
0 : currentIndex + 1;
|
|
64
|
+
|
|
65
|
+
this._state.highlightedResult = this._state.results.get( nextIndex );
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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/findpreviouscommand
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import FindNextCommand from './findnextcommand';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The find previous command. Moves the highlight to the previous search result.
|
|
14
|
+
*
|
|
15
|
+
* 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
|
+
*/
|
|
19
|
+
export default class FindPreviousCommand extends FindNextCommand {
|
|
20
|
+
/**
|
|
21
|
+
* @inheritDoc
|
|
22
|
+
*/
|
|
23
|
+
execute() {
|
|
24
|
+
const results = this._state.results;
|
|
25
|
+
const currentIndex = results.getIndex( this._state.highlightedResult );
|
|
26
|
+
const previousIndex = currentIndex - 1 < 0 ?
|
|
27
|
+
this._state.results.length - 1 : currentIndex - 1;
|
|
28
|
+
|
|
29
|
+
this._state.highlightedResult = this._state.results.get( previousIndex );
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export { default as FindAndReplace } from './findandreplace';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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/replaceallcommand
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { updateFindResultFromRange, findByTextCallback } from './utils';
|
|
11
|
+
import { Collection } from 'ckeditor5/src/utils';
|
|
12
|
+
import ReplaceCommand from './replacecommand';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The replace all command. It is used by the {@link module:find-and-replace/findandreplace~FindAndReplace find and replace feature}.
|
|
16
|
+
*
|
|
17
|
+
* @extends module:find-and-replace/replacecommand~ReplaceCommand
|
|
18
|
+
*/
|
|
19
|
+
export default class ReplaceAllCommand extends ReplaceCommand {
|
|
20
|
+
/**
|
|
21
|
+
* Replaces all the occurrences of `textToReplace` with a given `newText` string.
|
|
22
|
+
*
|
|
23
|
+
* ```js
|
|
24
|
+
* replaceAllCommand.execute( 'replaceAll', 'new text replacement', 'text to replace' );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Alternatively you can call it from editor instance:
|
|
28
|
+
*
|
|
29
|
+
* ```js
|
|
30
|
+
* editor.execute( 'replaceAll', 'new text', 'old text' );
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @param {String} newText Text that will be inserted to the editor for each match.
|
|
34
|
+
* @param {String|module:utils/collection~Collection} textToReplace Text to be replaced or a collection of matches
|
|
35
|
+
* as returned by the find command.
|
|
36
|
+
*
|
|
37
|
+
* @fires module:core/command~Command#event:execute
|
|
38
|
+
*/
|
|
39
|
+
execute( newText, textToReplace ) {
|
|
40
|
+
const { editor } = this;
|
|
41
|
+
const { model } = editor;
|
|
42
|
+
|
|
43
|
+
const results = textToReplace instanceof Collection ?
|
|
44
|
+
textToReplace : model.document.getRootNames()
|
|
45
|
+
.reduce( ( ( currentResults, rootName ) => updateFindResultFromRange(
|
|
46
|
+
model.createRangeIn( model.document.getRoot( rootName ) ),
|
|
47
|
+
model,
|
|
48
|
+
findByTextCallback( textToReplace, this._state ),
|
|
49
|
+
currentResults
|
|
50
|
+
) ), null );
|
|
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
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. 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/replacecommand
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Command } from 'ckeditor5/src/core';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 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
|
+
*/
|
|
17
|
+
export default class ReplaceCommand extends Command {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new `ReplaceCommand` instance.
|
|
20
|
+
*
|
|
21
|
+
* @param {module:core/editor/editor~Editor} editor Editor on which this command will be used.
|
|
22
|
+
* @param {module:find-and-replace/findandreplacestate~FindAndReplaceState} state An object to hold plugin state.
|
|
23
|
+
*/
|
|
24
|
+
constructor( editor, state ) {
|
|
25
|
+
super( editor );
|
|
26
|
+
|
|
27
|
+
// The replace command is always enabled.
|
|
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
|
+
let textAttributes = {};
|
|
54
|
+
|
|
55
|
+
for ( const item of range.getItems() ) {
|
|
56
|
+
if ( item.is( '$text' ) || item.is( '$textProxy' ) ) {
|
|
57
|
+
textAttributes = item.getAttributes();
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
model.insertContent( writer.createText( replacementText, textAttributes ), range );
|
|
63
|
+
|
|
64
|
+
if ( this._state.results.has( result ) ) {
|
|
65
|
+
this._state.results.remove( result );
|
|
66
|
+
}
|
|
67
|
+
} );
|
|
68
|
+
}
|
|
69
|
+
}
|