@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.
@@ -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
- * @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
- }
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
+ }
@@ -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 ReplaceCommand from './replacecommand';
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 ReplaceCommand {
19
- /**
20
- * Replaces all the occurrences of `textToReplace` with a given `newText` string.
21
- *
22
- * ```js
23
- * replaceAllCommand.execute( 'replaceAll', 'new text replacement', 'text to replace' );
24
- * ```
25
- *
26
- * Alternatively you can call it from editor instance:
27
- *
28
- * ```js
29
- * editor.execute( 'replaceAll', 'new text', 'old text' );
30
- * ```
31
- *
32
- * @param {String} newText Text that will be inserted to the editor for each match.
33
- * @param {String|module:utils/collection~Collection} textToReplace Text to be replaced or a collection of matches
34
- * as returned by the find command.
35
- *
36
- * @fires module:core/command~Command#event:execute
37
- */
38
- execute( newText, textToReplace ) {
39
- const { editor } = this;
40
- const { model } = editor;
41
- const findAndReplaceUtils = editor.plugins.get( 'FindAndReplaceUtils' );
42
-
43
- const results = textToReplace instanceof Collection ?
44
- textToReplace : model.document.getRootNames()
45
- .reduce( ( ( currentResults, rootName ) => findAndReplaceUtils.updateFindResultFromRange(
46
- model.createRangeIn( model.document.getRoot( rootName ) ),
47
- model,
48
- findAndReplaceUtils.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
- }
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
+ }
@@ -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 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
- // 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
+ }