@univerjs/sheets-find-replace 0.6.7 → 0.6.9

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/lib/facade.js ADDED
@@ -0,0 +1,101 @@
1
+ var u = Object.defineProperty;
2
+ var p = (a, e, t) => e in a ? u(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
+ var c = (a, e, t) => p(a, typeof e != "symbol" ? e + "" : e, t);
4
+ import { Inject as m, Injector as _, IUniverInstanceService as f, Disposable as v } from "@univerjs/core";
5
+ import { IFindReplaceService as g, FindReplaceModel as y, createInitFindReplaceState as S, FindReplaceState as b, FindBy as h } from "@univerjs/find-replace";
6
+ import { FRange as F } from "@univerjs/sheets/facade";
7
+ import { FUniver as d } from "@univerjs/core/facade";
8
+ var w = Object.getOwnPropertyDescriptor, C = (a, e, t, s) => {
9
+ for (var n = s > 1 ? void 0 : s ? w(e, t) : e, i = a.length - 1, r; i >= 0; i--)
10
+ (r = a[i]) && (n = r(n) || n);
11
+ return n;
12
+ }, o = (a, e) => (t, s) => e(t, s, a);
13
+ let l = class extends v {
14
+ constructor(e, t, s, n) {
15
+ super();
16
+ c(this, "_state", new b());
17
+ c(this, "_model");
18
+ c(this, "_complete");
19
+ this._injector = t, this._univerInstanceService = s, this._findReplaceService = n;
20
+ const i = this._findReplaceService.getProviders();
21
+ this._model = this._injector.createInstance(y, this._state, i);
22
+ const r = {
23
+ ...S(),
24
+ ...e
25
+ };
26
+ this._state.changeState(r);
27
+ }
28
+ findAll() {
29
+ return !this._state.findCompleted || !this._complete ? [] : this._complete.results.map((e) => this._findMatchToFRange(e));
30
+ }
31
+ findNext() {
32
+ var t;
33
+ if (!this._state.findCompleted || !this._complete)
34
+ return null;
35
+ const e = (t = this._model) == null ? void 0 : t.moveToNextMatch();
36
+ return e ? this._findMatchToFRange(e) : null;
37
+ }
38
+ findPrevious() {
39
+ var t;
40
+ const e = (t = this._model) == null ? void 0 : t.moveToPreviousMatch();
41
+ return e ? this._findMatchToFRange(e) : null;
42
+ }
43
+ getCurrentMatch() {
44
+ var t;
45
+ if (!this._state.findCompleted || !this._complete)
46
+ throw new Error("Find operation is not completed.");
47
+ const e = (t = this._model) == null ? void 0 : t.currentMatch$.value;
48
+ return e ? this._findMatchToFRange(e) : null;
49
+ }
50
+ async matchCaseAsync(e) {
51
+ return this._state.changeState({ caseSensitive: e, findCompleted: !1 }), new Promise((t) => {
52
+ const s = this._state.stateUpdates$.subscribe(async (n) => {
53
+ n.findCompleted === !0 && (s.unsubscribe(), await this.ensureCompleteAsync(), t(this));
54
+ });
55
+ });
56
+ }
57
+ async matchEntireCellAsync(e) {
58
+ return this._state.changeState({ matchesTheWholeCell: e, findCompleted: !1 }), new Promise((t) => {
59
+ const s = this._state.stateUpdates$.subscribe(async (n) => {
60
+ n.findCompleted === !0 && (s.unsubscribe(), await this.ensureCompleteAsync(), t(this));
61
+ });
62
+ });
63
+ }
64
+ async matchFormulaTextAsync(e) {
65
+ return this._state.changeState({ findBy: e ? h.FORMULA : h.VALUE, findCompleted: !1 }), new Promise((t) => {
66
+ const s = this._state.stateUpdates$.subscribe(async (n) => {
67
+ n.findCompleted === !0 && (s.unsubscribe(), await this.ensureCompleteAsync(), t(this));
68
+ });
69
+ });
70
+ }
71
+ async replaceAllWithAsync(e) {
72
+ var s, n, i;
73
+ await this._state.changeState({ replaceRevealed: !0, replaceString: e });
74
+ const t = (i = (n = await ((s = this._model) == null ? void 0 : s.replaceAll())) == null ? void 0 : n.success) != null ? i : 0;
75
+ return this._state.changeState({ replaceRevealed: !1 }), t;
76
+ }
77
+ async replaceWithAsync(e) {
78
+ var t;
79
+ return await this._state.changeState({ replaceRevealed: !0, replaceString: e }), await ((t = this._model) == null ? void 0 : t.replace()), this._state.changeState({ replaceRevealed: !1 }), !0;
80
+ }
81
+ async ensureCompleteAsync() {
82
+ var e;
83
+ this._complete = await ((e = this._model) == null ? void 0 : e.start());
84
+ }
85
+ _findMatchToFRange(e) {
86
+ const { unitId: t } = e, { subUnitId: s, range: n } = e.range, i = this._univerInstanceService.getUnit(t), r = i.getSheetBySheetId(s);
87
+ return this._injector.createInstance(F, i, r, n);
88
+ }
89
+ };
90
+ l = C([
91
+ o(1, m(_)),
92
+ o(2, f),
93
+ o(3, g)
94
+ ], l);
95
+ class R extends d {
96
+ async createTextFinderAsync(e) {
97
+ const t = { findString: e }, s = this._injector.createInstance(l, t);
98
+ return await s.ensureCompleteAsync(), s;
99
+ }
100
+ }
101
+ d.extend(R);
package/lib/index.js ADDED
@@ -0,0 +1,616 @@
1
+ var ue = Object.defineProperty;
2
+ var ge = (h, e, t) => e in h ? ue(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
3
+ var _ = (h, e, t) => ge(h, typeof e != "symbol" ? e + "" : e, t);
4
+ import { CommandType as B, IUndoRedoService as _e, ICommandService as H, Rectangle as fe, Inject as C, Injector as T, IContextService as J, IUniverInstanceService as Z, ThemeService as Se, Disposable as q, EDITOR_ACTIVATED as me, UniverInstanceType as ee, fromCallback as U, ColorKit as pe, rotate as $, groupBy as ve, ObjectMatrix as Ie, Tools as Ce, replaceInDocumentBody as Re, DependentOn as be, IConfigService as ke, Plugin as we, merge as Me } from "@univerjs/core";
5
+ import { FindReplaceController as xe, IFindReplaceService as ye, FindModel as Ue, FindBy as k, FindScope as I, FindDirection as b, UniverFindReplacePlugin as Pe } from "@univerjs/find-replace";
6
+ import { SetRangeValuesCommand as te, SheetsSelectionsService as Be, SelectRangeCommand as We, SetWorksheetActiveOperation as Fe, SetSelectionsOperation as He, SetWorksheetActivateCommand as Te, UniverSheetsPlugin as L } from "@univerjs/sheets";
7
+ import { Shape as Ee, Rect as Oe, IRenderManagerService as ie, RENDER_RAW_FORMULA_KEY as Ae } from "@univerjs/engine-render";
8
+ import { SheetSkeletonManagerService as Ne, getCoordByCell as j, getSheetObject as De, ScrollToCellCommand as $e } from "@univerjs/sheets-ui";
9
+ import { filter as w, Subject as V, throttleTime as Le, merge as je, skip as Ve, debounceTime as Ge } from "rxjs";
10
+ const Ye = "sheets-find-replace.config", G = {}, ne = {
11
+ id: "sheet.command.replace",
12
+ type: B.COMMAND,
13
+ handler: async (h, e) => {
14
+ const t = h.get(_e), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(te.id, {
15
+ unitId: n,
16
+ subUnitId: c.subUnitId,
17
+ value: c.value
18
+ })));
19
+ return s.dispose(), Ke(o, r);
20
+ }
21
+ };
22
+ function Ke(h, e) {
23
+ let t = 0, i = 0;
24
+ return h.forEach((n, r) => {
25
+ const s = e[r].count;
26
+ n ? t += s : i += s;
27
+ }), { success: t, failure: i };
28
+ }
29
+ class Qe extends Ee {
30
+ constructor(t, i) {
31
+ super(t, i);
32
+ _(this, "_activated", !1);
33
+ _(this, "_inHiddenRange", !1);
34
+ _(this, "_color");
35
+ i && this.setShapeProps(i);
36
+ }
37
+ setShapeProps(t) {
38
+ this._activated = !!t.activated, typeof t.inHiddenRange < "u" && (this._inHiddenRange = t.inHiddenRange), typeof t.color < "u" && (this._color = t.color), this.transformByState({
39
+ width: t.width,
40
+ height: t.height
41
+ });
42
+ }
43
+ _draw(t) {
44
+ const i = this._activated, n = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`, r = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
45
+ Oe.drawWith(t, {
46
+ width: this.width,
47
+ height: this.height,
48
+ fill: n,
49
+ stroke: i ? r : void 0,
50
+ strokeWidth: i ? 2 : 0,
51
+ evented: !1
52
+ });
53
+ }
54
+ }
55
+ function Y(h, e) {
56
+ return h.startRow === e.startRow && h.startColumn === e.startColumn;
57
+ }
58
+ function K(h, e) {
59
+ return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
60
+ }
61
+ function Q(h, e) {
62
+ return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
63
+ }
64
+ function Xe(h, e) {
65
+ return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
66
+ }
67
+ function ze(h, e) {
68
+ return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
69
+ }
70
+ function Je(h, e) {
71
+ const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
72
+ return r ? fe.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
73
+ }
74
+ var Ze = Object.getOwnPropertyDescriptor, E = (h, e, t, i) => {
75
+ for (var n = i > 1 ? void 0 : i ? Ze(e, t) : e, r = h.length - 1, s; r >= 0; r--)
76
+ (s = h[r]) && (n = s(n) || n);
77
+ return n;
78
+ }, m = (h, e) => (t, i) => e(t, i, h);
79
+ let M = class extends q {
80
+ constructor(e, t, i, n, r) {
81
+ super();
82
+ _(this, "_provider");
83
+ this._injector = e, this._findReplaceController = t, this._contextService = i, this._findReplaceService = n, this._commandService = r, this._init(), this._initCommands();
84
+ }
85
+ dispose() {
86
+ super.dispose(), this._findReplaceController.closePanel(), this._provider.dispose();
87
+ }
88
+ _init() {
89
+ const e = this._injector.createInstance(F);
90
+ this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(this._contextService.subscribeContextValue$(me).pipe(w((t) => !!t)).subscribe(() => this._findReplaceController.closePanel()));
91
+ }
92
+ _initCommands() {
93
+ [ne].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
94
+ }
95
+ };
96
+ M = E([
97
+ m(0, C(T)),
98
+ m(1, C(xe)),
99
+ m(2, J),
100
+ m(3, ye),
101
+ m(4, H)
102
+ ], M);
103
+ const qe = "sheets-find-replace-provider", et = 1e4;
104
+ let W = class extends Ue {
105
+ constructor(e, t, i, n, r, s, o, c) {
106
+ super();
107
+ // We can directly inject the `FindReplaceService` here, and call its methods instead of using the observables.
108
+ _(this, "_matchesUpdate$", new V());
109
+ _(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
110
+ _(this, "_activelyChangingMatch$", new V());
111
+ _(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
112
+ /** Hold matches by the worksheet they are in. Make it easier to track the next (or previous) match when searching in the whole workbook. */
113
+ _(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
114
+ /** Hold all matches in the currently searching scope. */
115
+ _(this, "_matches", []);
116
+ /** Position of the current focused ISheetCellMatch, starting from 1. */
117
+ _(this, "_matchesPosition", 0);
118
+ _(this, "_activeHighlightIndex", -1);
119
+ _(this, "_highlightShapes", []);
120
+ _(this, "_currentHighlightShape", null);
121
+ /** This properties holds the query params during this searching session. */
122
+ _(this, "_query", null);
123
+ _(this, "_workbookSelections");
124
+ this._workbook = e, this._sheetSkeletonManagerService = t, this._univerInstanceService = i, this._renderManagerService = n, this._commandService = r, this._contextService = s, this._themeService = o, this._workbookSelections = c.getWorkbookSelections(this.unitId);
125
+ }
126
+ get _matchesCount() {
127
+ return this._matches.length;
128
+ }
129
+ get unitId() {
130
+ return this._workbook.getUnitId();
131
+ }
132
+ get matchesCount() {
133
+ return this._matchesCount;
134
+ }
135
+ get matchesPosition() {
136
+ return this._matchesPosition;
137
+ }
138
+ get currentMatch() {
139
+ return this._matchesPosition > 0 ? this._matches[this._matchesPosition - 1] : null;
140
+ }
141
+ dispose() {
142
+ super.dispose(), this._disposeHighlights(), this._toggleDisplayRawFormula(!1);
143
+ }
144
+ getMatches() {
145
+ return this._matches;
146
+ }
147
+ start(e) {
148
+ switch (this._query = e, e.findBy === k.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
149
+ case I.UNIT:
150
+ this.findInWorkbook(e);
151
+ break;
152
+ case I.SUBUNIT:
153
+ default:
154
+ this.findInActiveWorksheet(e);
155
+ break;
156
+ }
157
+ }
158
+ focusSelection() {
159
+ const e = this.currentMatch;
160
+ e && this._commandService.executeCommand(We.id, {
161
+ unitId: e.unitId,
162
+ subUnit: e.range.subUnitId,
163
+ range: e.range.range
164
+ });
165
+ }
166
+ _toggleDisplayRawFormula(e) {
167
+ this._contextService.setContextValue(Ae, e);
168
+ }
169
+ /**
170
+ * Find all matches in the current workbook no matter which worksheet is activated.
171
+ * @param query the query object
172
+ * @returns the query complete event
173
+ */
174
+ findInWorkbook(e) {
175
+ const t = this._workbook.getUnitId();
176
+ let i, n = !0;
177
+ const r = () => {
178
+ const s = this._workbook.getSheets().filter((o) => !o.isSheetHidden()).map((o) => {
179
+ const c = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: a } = c;
180
+ return a.length ? this._matchesByWorksheet.set(l, c.results) : this._matchesByWorksheet.delete(l), c;
181
+ });
182
+ this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
183
+ };
184
+ return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
185
+ this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
186
+ })), this.disposeWithMe(
187
+ U(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(w(([s, o]) => s.id === Fe.id && !(o != null && o.fromFindReplace))).subscribe(() => {
188
+ const s = this._workbook.getActiveSheet();
189
+ if (!s)
190
+ return;
191
+ const o = s.getSheetId();
192
+ this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
193
+ })
194
+ ), this.disposeWithMe(
195
+ U(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
196
+ w(
197
+ ([s]) => s.type === B.MUTATION && s.params.unitId === this._workbook.getUnitId()
198
+ ),
199
+ Le(600, void 0, { leading: !1, trailing: !0 })
200
+ ).subscribe(() => r())
201
+ ), r(), i;
202
+ }
203
+ /**
204
+ * This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
205
+ * in the new worksheet.
206
+ */
207
+ _findNextMatchOnActiveSheetChange(e) {
208
+ let t, i, n = 0;
209
+ const r = this._matchesByWorksheet.get(e.getSheetId()), s = this._workbookSelections.getCurrentSelections();
210
+ s != null && s.length ? ([t, n] = this._findNextMatchByRange(r, s[0].range), i = r.findIndex((o) => o === t)) : (t = r[0], i = 0, n = this._matches.findIndex((o) => o === t)), this._matchesPosition = n + 1, this._activelyChangingMatch$.next(t), this._activeHighlightIndex = i, this._updateFindHighlight(), this._updateCurrentHighlightShape(i);
211
+ }
212
+ /**
213
+ * Find all matches (only) in the currently activated worksheet.
214
+ * @param query the query object
215
+ * @returns the query complete event
216
+ */
217
+ findInActiveWorksheet(e) {
218
+ const t = this._workbook.getUnitId(), i = () => {
219
+ var d;
220
+ const c = this._workbook.getActiveSheet();
221
+ if (!c) return !1;
222
+ const l = this._workbookSelections.getCurrentSelections();
223
+ return (d = l == null ? void 0 : l.some((u) => !Je(u, c))) != null ? d : !1;
224
+ };
225
+ let n, r = !0, s = !1;
226
+ const o = () => {
227
+ const c = this._workbook.getActiveSheet();
228
+ if (!c) return { results: [] };
229
+ const l = this.currentMatch;
230
+ s = i();
231
+ const a = this._workbookSelections.getCurrentSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
232
+ return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(l, this._matches), r ? (n = d, r = !1) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), d;
233
+ };
234
+ return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
235
+ je(
236
+ U(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
237
+ w(([c]) => {
238
+ if (c.type === B.MUTATION && c.params.unitId === this._workbook.getUnitId())
239
+ return !0;
240
+ if (c.id === He.id && c.params.unitId === t) {
241
+ const l = i();
242
+ return l === !1 && s === !1 ? !1 : (s = l, !0);
243
+ }
244
+ return !1;
245
+ })
246
+ ),
247
+ // activeSheet$ is a BehaviorSubject, so we need to skip the first
248
+ this._workbook.activeSheet$.pipe(Ve(1))
249
+ ).pipe(Ge(200)).subscribe(() => o())
250
+ ), o(), n;
251
+ }
252
+ _findInRange(e, t, i, n, r) {
253
+ const s = [], o = e.getSheetId(), c = (t.findDirection === b.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
254
+ for (const l of c) {
255
+ const { row: a, col: d, colSpan: u, rowSpan: f, value: S } = l;
256
+ if (r != null && r(a, d) || !S || e.getRowFiltered(a))
257
+ continue;
258
+ const { hit: p, replaceable: R, isFormula: v } = tt(e, a, d, t, S);
259
+ if (p) {
260
+ const y = {
261
+ provider: qe,
262
+ unitId: n,
263
+ replaceable: R,
264
+ isFormula: v,
265
+ range: {
266
+ subUnitId: o,
267
+ range: {
268
+ startRow: a,
269
+ startColumn: d,
270
+ endColumn: d + (u != null ? u : 1) - 1,
271
+ endRow: a + (f != null ? f : 1) - 1
272
+ }
273
+ }
274
+ };
275
+ s.push(y);
276
+ }
277
+ }
278
+ return { results: s };
279
+ }
280
+ _findInSelections(e, t, i, n) {
281
+ const { findDirection: r } = i, s = r === b.ROW ? K : Q, o = /* @__PURE__ */ new Set();
282
+ return { results: t.map((l) => this._findInRange(e, i, l.range, n, (a, d) => {
283
+ const u = `${a}-${d}`;
284
+ return o.has(u) ? !0 : (o.add(u), !1);
285
+ }).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
286
+ }
287
+ /** Find matches in a given worksheet. */
288
+ _findInWorksheet(e, t, i) {
289
+ const n = e.getRowCount(), r = e.getColumnCount(), s = { startRow: 0, startColumn: 0, endRow: n - 1, endColumn: r - 1 };
290
+ return this._findInRange(e, t, s, i);
291
+ }
292
+ _disposeHighlights() {
293
+ var e;
294
+ this._highlightShapes.forEach((t) => {
295
+ var i;
296
+ (i = t.getScene()) == null || i.makeDirty(), t.dispose();
297
+ }), this._highlightShapes = [], (e = this._currentHighlightShape) == null || e.dispose(), this._currentHighlightShape = null;
298
+ }
299
+ _updateFindHighlight() {
300
+ var d;
301
+ this._disposeHighlights();
302
+ const e = (d = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : d.skeleton;
303
+ if (!e)
304
+ return;
305
+ const t = this._workbook.getUnitId(), i = this._renderManagerService.getRenderById(t);
306
+ if (i == null)
307
+ return;
308
+ const { scene: n } = i, r = this._matches, s = this._themeService.getCurrentTheme().gold400, o = new pe(s).toRgb(), c = this._workbook.getActiveSheet();
309
+ if (!c)
310
+ return;
311
+ const l = c.getSheetId(), a = r.filter((u) => u.range.subUnitId === l).map((u, f) => {
312
+ const { startColumn: S, startRow: p, endColumn: R, endRow: v } = u.range.range, y = j(p, S, n, e), re = j(v, R, n, e), { startX: O, startY: A } = y, { endX: oe, endY: he } = re, N = !c.getRowRawVisible(p), D = !c.getColVisible(S), ce = N || D, ae = D ? 2 : oe - O, le = N ? 2 : he - A, de = {
313
+ left: O,
314
+ top: A,
315
+ color: o,
316
+ width: ae,
317
+ height: le,
318
+ evented: !1,
319
+ inHiddenRange: ce,
320
+ zIndex: et
321
+ };
322
+ return new Qe(`find-highlight-${f}`, de);
323
+ });
324
+ n.addObjects(a), this._highlightShapes = a, n.makeDirty();
325
+ }
326
+ _updateCurrentHighlightShape(e) {
327
+ var t;
328
+ if ((t = this._currentHighlightShape) == null || t.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, e !== void 0) {
329
+ const i = this._highlightShapes[e];
330
+ if (!i)
331
+ return;
332
+ this._currentHighlightShape = i, i.setShapeProps({ activated: !0 });
333
+ }
334
+ }
335
+ _getSheetObject() {
336
+ return De(this._univerInstanceService, this._renderManagerService);
337
+ }
338
+ _focusMatch(e) {
339
+ var i;
340
+ const t = e.range.subUnitId;
341
+ t !== ((i = this._workbook.getActiveSheet()) == null ? void 0 : i.getSheetId()) && this._commandService.executeCommand(Te.id, { unitId: this._workbook.getUnitId(), subUnitId: t }, { fromFindReplace: !0 }), this._commandService.executeCommand(
342
+ $e.id,
343
+ { range: e.range.range },
344
+ { fromFindReplace: !0 }
345
+ );
346
+ }
347
+ _tryRestoreLastMatchesPosition(e, t) {
348
+ if (!e) return 0;
349
+ const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
350
+ if (i !== o.range.subUnitId)
351
+ return !1;
352
+ const { startColumn: c, startRow: l } = o.range.range;
353
+ return c === n && l === r;
354
+ });
355
+ return s > -1 ? s + 1 : 0;
356
+ }
357
+ moveToNextMatch(e) {
358
+ var o, c, l, a, d;
359
+ if (!this._matches.length)
360
+ return null;
361
+ const t = (o = e == null ? void 0 : e.loop) != null ? o : !1, i = (c = e == null ? void 0 : e.stayIfOnMatch) != null ? c : !1, n = (l = e == null ? void 0 : e.noFocus) != null ? l : !1, r = (a = e == null ? void 0 : e.ignoreSelection) != null ? a : !1, s = this._findNextMatch(t, i, r);
362
+ if (s) {
363
+ const [u, f] = s;
364
+ return this._matchesPosition = f + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(u.range.subUnitId).findIndex((S) => S === u) : this._activeHighlightIndex = f, n || this._focusMatch(u), ((d = this._workbook.getActiveSheet()) == null ? void 0 : d.getSheetId()) === u.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), u;
365
+ }
366
+ return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
367
+ }
368
+ moveToPreviousMatch(e) {
369
+ var o, c, l, a, d;
370
+ if (!this._matches.length)
371
+ return null;
372
+ const t = (o = e == null ? void 0 : e.loop) != null ? o : !1, i = (c = e == null ? void 0 : e.stayIfOnMatch) != null ? c : !1, n = (l = e == null ? void 0 : e.noFocus) != null ? l : !1, r = (a = e == null ? void 0 : e.ignoreSelection) != null ? a : !1, s = this._findPreviousMatch(t, i, r);
373
+ if (s) {
374
+ const [u, f] = s;
375
+ return this._matchesPosition = f + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(u.range.subUnitId).findIndex((S) => S === u) : this._activeHighlightIndex = f, n || this._focusMatch(u), ((d = this._workbook.getActiveSheet()) == null ? void 0 : d.getSheetId()) === u.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), u;
376
+ }
377
+ return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
378
+ }
379
+ _findPreviousMatch(e = !1, t = !1, i = !1) {
380
+ var o;
381
+ if (this.currentMatch) {
382
+ const c = this._matches.findIndex((u) => u === this.currentMatch);
383
+ if (t)
384
+ return [this.currentMatch, c];
385
+ const l = c - 1;
386
+ if (!e && l < 0)
387
+ return null;
388
+ const a = this._matches.length, d = (l + a) % a;
389
+ return [this._matches[d], d];
390
+ }
391
+ const n = this._workbookSelections.getCurrentLastSelection();
392
+ if (i || !n) {
393
+ const c = this._matches.length - 1;
394
+ return [this._matches[c], c];
395
+ }
396
+ if (this._query.findScope !== I.UNIT)
397
+ return this._findPreviousMatchByRange(this._matches, n.range);
398
+ const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
399
+ if (!r)
400
+ return null;
401
+ const s = this._findPreviousWorksheetThatHasAMatch(r, e);
402
+ return s ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
403
+ }
404
+ _findNextMatch(e = !1, t = !1, i = !1) {
405
+ var o;
406
+ if (this.currentMatch) {
407
+ const c = this._matches.findIndex((u) => u === this.currentMatch);
408
+ if (t)
409
+ return [this.currentMatch, c];
410
+ const l = c + 1, a = this._matches.length;
411
+ if (!e && l >= a)
412
+ return null;
413
+ const d = l % a;
414
+ return [this._matches[d], d];
415
+ }
416
+ const n = this._workbookSelections.getCurrentLastSelection();
417
+ if (i || !n)
418
+ return [this._matches[0], 0];
419
+ if (this._query.findScope !== I.UNIT)
420
+ return this._findNextMatchByRange(this._matches, n.range, t);
421
+ const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
422
+ if (!r)
423
+ return null;
424
+ const s = this._findNextWorksheetThatHasAMatch(r, e);
425
+ return s ? this._findNextMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
426
+ }
427
+ _findPreviousWorksheetThatHasAMatch(e, t = !1) {
428
+ const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n + 1) : i.slice(0, n + 1)).findLast((o) => this._matchesByWorksheet.has(o));
429
+ return s != null ? s : null;
430
+ }
431
+ _findNextWorksheetThatHasAMatch(e, t = !1) {
432
+ const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
433
+ return s != null ? s : null;
434
+ }
435
+ _findNextMatchByRange(e, t, i = !1) {
436
+ const n = this._query.findDirection === b.ROW;
437
+ let r = e.findIndex((o) => {
438
+ const c = o.range.range;
439
+ if (!(n ? K(t, c) : Q(t, c)))
440
+ return !1;
441
+ const a = Y(t, c);
442
+ return i ? a : !a;
443
+ });
444
+ r === -1 && (r = e.length - 1);
445
+ const s = e[r];
446
+ return [s, this._matches.findIndex((o) => o === s)];
447
+ }
448
+ _findPreviousMatchByRange(e, t, i = !1) {
449
+ const n = this._query.findDirection === b.ROW;
450
+ let r = this._matches.findLastIndex((o) => {
451
+ const c = o.range.range;
452
+ if (!(n ? Xe(t, c) : ze(t, c)))
453
+ return !1;
454
+ const a = Y(t, c);
455
+ return i ? a : !a;
456
+ });
457
+ r === -1 && (r = 0);
458
+ const s = e[r];
459
+ return [s, this._matches.findIndex((o) => o === s)];
460
+ }
461
+ async replace(e) {
462
+ if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
463
+ return !1;
464
+ const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
465
+ this.currentMatch,
466
+ i,
467
+ this._query.findBy === k.FORMULA,
468
+ this._query.findString,
469
+ e,
470
+ this._query.caseSensitive ? "g" : "ig"
471
+ ), r = {
472
+ unitId: this.currentMatch.unitId,
473
+ subUnitId: t.subUnitId,
474
+ value: {
475
+ [t.range.startRow]: {
476
+ [t.range.startColumn]: n
477
+ }
478
+ }
479
+ };
480
+ return this._commandService.executeCommand(te.id, r);
481
+ }
482
+ async replaceAll(e) {
483
+ if (this._matchesCount === 0 || !this._query)
484
+ return { success: 0, failure: 0 };
485
+ const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === k.FORMULA, o = n ? "g" : "ig", c = [];
486
+ return ve(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
487
+ const u = new Ie(), f = this._workbook.getSheetBySheetId(d);
488
+ a.forEach((S) => {
489
+ const { startColumn: p, startRow: R } = S.range.range, v = this._getReplacedCellData(S, f, s, i, e, o);
490
+ v && u.setValue(R, p, v);
491
+ }), c.push({
492
+ count: a.length,
493
+ subUnitId: d,
494
+ value: u.getMatrix()
495
+ });
496
+ }), c ? this._commandService.executeCommand(ne.id, {
497
+ unitId: t,
498
+ replacements: c
499
+ }) : { success: 0, failure: 0 };
500
+ }
501
+ _getReplacedCellData(e, t, i, n, r, s) {
502
+ var f;
503
+ const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
504
+ if (e.isFormula)
505
+ return i ? { f: a.f.replace(new RegExp(X(n), s), r), v: null } : null;
506
+ if (!!((f = a.p) != null && f.body)) {
507
+ const S = Ce.deepClone(a.p);
508
+ return Re(S.body, n, r, this._query.caseSensitive), { p: S };
509
+ }
510
+ return { v: a.v.toString().replace(new RegExp(X(n), s), r) };
511
+ }
512
+ };
513
+ W = E([
514
+ m(2, Z),
515
+ m(3, ie),
516
+ m(4, H),
517
+ m(5, J),
518
+ m(6, C(Se)),
519
+ m(7, C(Be))
520
+ ], W);
521
+ function X(h) {
522
+ return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
523
+ }
524
+ let F = class extends q {
525
+ constructor(e, t, i) {
526
+ super();
527
+ /**
528
+ * Hold all find results in this kind of univer business instances (Workbooks).
529
+ */
530
+ _(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
531
+ this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
532
+ }
533
+ async find(e) {
534
+ this._terminate();
535
+ const t = this._univerInstanceService.getAllUnitsForType(ee.UNIVER_SHEET), i = this._preprocessQuery(e);
536
+ return t.map((r) => {
537
+ const s = this._renderManagerService.getRenderById(r.getUnitId()).with(Ne), o = this._injector.createInstance(W, r, s);
538
+ return this._findModelsByUnitId.set(r.getUnitId(), o), o.start(i), o;
539
+ });
540
+ }
541
+ terminate() {
542
+ this._terminate();
543
+ }
544
+ _terminate() {
545
+ this._findModelsByUnitId.forEach((e) => e.dispose()), this._findModelsByUnitId.clear();
546
+ }
547
+ /**
548
+ * Parsed the query object before do actual searching in favor of performance.
549
+ * @param query the raw query object
550
+ * @returns the parsed query object
551
+ */
552
+ _preprocessQuery(e) {
553
+ let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
554
+ return t = t.trim(), {
555
+ ...e,
556
+ findString: t
557
+ };
558
+ }
559
+ };
560
+ F = E([
561
+ m(0, Z),
562
+ m(1, ie),
563
+ m(2, C(T))
564
+ ], F);
565
+ const g = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
566
+ function tt(h, e, t, i, n) {
567
+ const { findBy: r } = i, s = r === k.FORMULA, o = h.getCellRaw(e, t);
568
+ return g.rawData = o, !(o != null && o.f) ? (g.isFormula = !1, P(n, i) ? o ? (g.hit = !0, g.replaceable = !0) : (g.hit = !0, g.replaceable = !1) : (g.hit = !1, g.replaceable = !1), g) : (g.isFormula = !0, s ? P({ v: o.f }, i) ? (g.hit = !0, g.replaceable = !0, g) : (g.hit = !1, g.replaceable = !1, g) : (g.replaceable = !1, P(n, i) ? g.hit = !0 : g.hit = !1, g));
569
+ }
570
+ function P(h, e) {
571
+ let t = it(h);
572
+ return t ? e.matchesTheWholeCell ? (t = nt(t), e.caseSensitive ? t === e.findString : t.toLowerCase() === e.findString) : e.caseSensitive ? t.indexOf(e.findString) > -1 : t.toLowerCase().indexOf(e.findString) > -1 : !1;
573
+ }
574
+ function it(h) {
575
+ var t, i, n;
576
+ const e = (n = (i = (t = h == null ? void 0 : h.p) == null ? void 0 : t.body) == null ? void 0 : i.dataStream) != null ? n : h == null ? void 0 : h.v;
577
+ return typeof e == "number" ? `${e}` : typeof e == "boolean" ? e ? "1" : "0" : e;
578
+ }
579
+ function nt(h) {
580
+ return h.replace(/^ +/g, "").replace(/ +$/g, "");
581
+ }
582
+ var st = Object.defineProperty, rt = Object.getOwnPropertyDescriptor, ot = (h, e, t) => e in h ? st(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, ht = (h, e, t, i) => {
583
+ for (var n = i > 1 ? void 0 : i ? rt(e, t) : e, r = h.length - 1, s; r >= 0; r--)
584
+ (s = h[r]) && (n = s(n) || n);
585
+ return n;
586
+ }, z = (h, e) => (t, i) => e(t, i, h), se = (h, e, t) => ot(h, typeof e != "symbol" ? e + "" : e, t);
587
+ const ct = "SHEET_FIND_REPLACE_PLUGIN";
588
+ let x = class extends we {
589
+ constructor(h = G, e, t) {
590
+ super(), this._config = h, this._injector = e, this._configService = t;
591
+ const { ...i } = Me(
592
+ {},
593
+ G,
594
+ this._config
595
+ );
596
+ this._configService.setConfig(Ye, i);
597
+ }
598
+ onStarting() {
599
+ [[M]].forEach((h) => this._injector.add(h));
600
+ }
601
+ onSteady() {
602
+ this._injector.get(M);
603
+ }
604
+ };
605
+ se(x, "pluginName", ct);
606
+ se(x, "type", ee.UNIVER_SHEET);
607
+ x = ht([
608
+ be(L, L, Pe),
609
+ z(1, C(T)),
610
+ z(2, ke)
611
+ ], x);
612
+ export {
613
+ ne as SheetReplaceCommand,
614
+ M as SheetsFindReplaceController,
615
+ x as UniverSheetsFindReplacePlugin
616
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "Failed to replace some cells",
5
+ failure: "Failed to replace all cells"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "جایگزینی برخی سلول‌ها ناموفق بود",
5
+ failure: "جایگزینی همه سلول‌ها ناموفق بود"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "Échec du remplacement de certaines cellules",
5
+ failure: "Échec du remplacement de toutes les cellules"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "Не удалось заменить некоторые ячейки",
5
+ failure: "Не удалось заменить все ячейки"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const t = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "Thay thế một phần ô thất bại",
5
+ failure: "Thay thế thất bại"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ t as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "部分单元格替换失败",
5
+ failure: "替换失败"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,11 @@
1
+ const e = {
2
+ "sheet-find-replace": {
3
+ replace: {
4
+ "partial-failure": "部分單元格替換失敗",
5
+ failure: "替換失敗"
6
+ }
7
+ }
8
+ };
9
+ export {
10
+ e as default
11
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-find-replace",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "private": false,
5
5
  "description": "UniverSheet find replace plugin",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -58,18 +58,18 @@
58
58
  "rxjs": ">=7.0.0"
59
59
  },
60
60
  "dependencies": {
61
- "@univerjs/core": "0.6.7",
62
- "@univerjs/sheets": "0.6.7",
63
- "@univerjs/engine-render": "0.6.7",
64
- "@univerjs/sheets-ui": "0.6.7",
65
- "@univerjs/find-replace": "0.6.7"
61
+ "@univerjs/core": "0.6.9",
62
+ "@univerjs/find-replace": "0.6.9",
63
+ "@univerjs/sheets": "0.6.9",
64
+ "@univerjs/engine-render": "0.6.9",
65
+ "@univerjs/sheets-ui": "0.6.9"
66
66
  },
67
67
  "devDependencies": {
68
68
  "rxjs": "^7.8.1",
69
69
  "typescript": "^5.8.2",
70
70
  "vite": "^6.2.3",
71
71
  "vitest": "^3.0.9",
72
- "@univerjs-infra/shared": "0.6.7"
72
+ "@univerjs-infra/shared": "0.6.9"
73
73
  },
74
74
  "scripts": {
75
75
  "test": "vitest run",