-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
findandreplacestate.ts
169 lines (144 loc) · 4.27 KB
/
findandreplacestate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module find-and-replace/findandreplacestate
*/
import type { Model, Item } from 'ckeditor5/src/engine.js';
import { ObservableMixin, Collection, type CollectionChangeEvent, type ObservableChangeEvent } from 'ckeditor5/src/utils.js';
import type { ResultType } from './findandreplace.js';
/**
* The object storing find and replace plugin state for a given editor instance.
*/
export default class FindAndReplaceState extends ObservableMixin() {
/**
* A collection of find matches.
*
* @observable
*/
declare public results: Collection<ResultType>;
/**
* Currently highlighted search result in {@link #results matched results}.
*
* @readonly
* @observable
*/
declare public highlightedResult: ResultType | null;
/**
* Currently highlighted search result offset in {@link #results matched results}.
*
* @readonly
* @observable
*/
declare public highlightedOffset: number;
/**
* Searched text value.
*
* @readonly
* @observable
*/
declare public searchText: string;
/**
* The most recent search callback used by the feature to find matches.
* It is used to re-run the search when user modifies the editor content.
*
* @readonly
* @observable
*/
declare public lastSearchCallback: FindCallback | null;
/**
* Replace text value.
*
* @readonly
* @observable
*/
declare public replaceText: string;
/**
* Indicates whether the matchCase checkbox has been checked.
*
* @readonly
* @observable
*/
declare public matchCase: boolean;
/**
* Indicates whether the matchWholeWords checkbox has been checked.
*
* @readonly
* @observable
*/
declare public matchWholeWords: boolean;
/**
* Creates an instance of the state.
*/
public constructor( model: Model ) {
super();
this.set( 'results', new Collection() );
this.set( 'highlightedResult', null );
this.set( 'highlightedOffset', 0 );
this.set( 'searchText', '' );
this.set( 'replaceText', '' );
this.set( 'lastSearchCallback', null );
this.set( 'matchCase', false );
this.set( 'matchWholeWords', false );
this.results.on<CollectionChangeEvent<ResultType>>( 'change', ( eventInfo, { removed, index } ) => {
if ( Array.from( removed ).length ) {
let highlightedResultRemoved = false;
model.change( writer => {
for ( const removedResult of removed ) {
if ( this.highlightedResult === removedResult ) {
highlightedResultRemoved = true;
}
if ( model.markers.has( removedResult.marker!.name ) ) {
writer.removeMarker( removedResult.marker! );
}
}
} );
if ( highlightedResultRemoved ) {
const nextHighlightedIndex = index >= this.results.length ? 0 : index;
this.highlightedResult = this.results.get( nextHighlightedIndex );
}
}
} );
this.on<ObservableChangeEvent<ResultType | null>>( 'change:highlightedResult', ( ) => {
this.refreshHighlightOffset();
} );
}
/**
* Cleans the state up and removes markers from the model.
*/
public clear( model: Model ): void {
this.searchText = '';
model.change( writer => {
if ( this.highlightedResult ) {
const oldMatchId = this.highlightedResult.marker!.name.split( ':' )[ 1 ];
const oldMarker = model.markers.get( `findResultHighlighted:${ oldMatchId }` );
if ( oldMarker ) {
writer.removeMarker( oldMarker );
}
}
[ ...this.results ].forEach( ( { marker } ) => {
writer.removeMarker( marker! );
} );
} );
this.results.clear();
}
/**
* Refreshes the highlight result offset based on it's index within the result list.
*/
public refreshHighlightOffset(): void {
const { highlightedResult, results } = this;
const sortMapping = { before: -1, same: 0, after: 1, different: 1 };
if ( highlightedResult ) {
this.highlightedOffset = Array.from( results )
.sort( ( a, b ) => sortMapping[ a.marker!.getStart().compareWith( b.marker!.getStart() ) ] )
.indexOf( highlightedResult ) + 1;
} else {
this.highlightedOffset = 0;
}
}
}
/**
* The callback function used to find matches in the document.
*/
export type FindCallback = ( ( { item, text }: { item: Item; text: string } ) => Array<ResultType> );