@univerjs/sheets-find-replace 0.17.0 → 0.18.0-insiders.20260328-0842384

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.
Files changed (75) hide show
  1. package/lib/cjs/facade.js +1 -1
  2. package/lib/cjs/index.js +1 -1
  3. package/lib/cjs/locale/ca-ES.js +1 -1
  4. package/lib/cjs/locale/en-US.js +1 -1
  5. package/lib/cjs/locale/es-ES.js +1 -1
  6. package/lib/cjs/locale/fa-IR.js +1 -1
  7. package/lib/cjs/locale/fr-FR.js +1 -1
  8. package/lib/cjs/locale/ja-JP.js +1 -1
  9. package/lib/cjs/locale/ko-KR.js +1 -1
  10. package/lib/cjs/locale/ru-RU.js +1 -1
  11. package/lib/cjs/locale/sk-SK.js +1 -1
  12. package/lib/cjs/locale/vi-VN.js +1 -1
  13. package/lib/cjs/locale/zh-CN.js +1 -1
  14. package/lib/cjs/locale/zh-TW.js +1 -1
  15. package/lib/es/facade.js +1 -101
  16. package/lib/es/index.js +1 -633
  17. package/lib/es/locale/ca-ES.js +1 -11
  18. package/lib/es/locale/en-US.js +1 -11
  19. package/lib/es/locale/es-ES.js +1 -11
  20. package/lib/es/locale/fa-IR.js +1 -11
  21. package/lib/es/locale/fr-FR.js +1 -11
  22. package/lib/es/locale/ja-JP.js +1 -11
  23. package/lib/es/locale/ko-KR.js +1 -11
  24. package/lib/es/locale/ru-RU.js +1 -11
  25. package/lib/es/locale/sk-SK.js +1 -11
  26. package/lib/es/locale/vi-VN.js +1 -11
  27. package/lib/es/locale/zh-CN.js +1 -11
  28. package/lib/es/locale/zh-TW.js +1 -11
  29. package/lib/facade.js +1 -101
  30. package/lib/index.js +1 -633
  31. package/lib/locale/ca-ES.js +1 -11
  32. package/lib/locale/en-US.js +1 -11
  33. package/lib/locale/es-ES.js +1 -11
  34. package/lib/locale/fa-IR.js +1 -11
  35. package/lib/locale/fr-FR.js +1 -11
  36. package/lib/locale/ja-JP.js +1 -11
  37. package/lib/locale/ko-KR.js +1 -11
  38. package/lib/locale/ru-RU.js +1 -11
  39. package/lib/locale/sk-SK.js +1 -11
  40. package/lib/locale/vi-VN.js +1 -11
  41. package/lib/locale/zh-CN.js +1 -11
  42. package/lib/locale/zh-TW.js +1 -11
  43. package/lib/types/commands/commands/sheet-replace.command.d.ts +17 -2
  44. package/lib/types/controllers/sheet-find-replace.controller.d.ts +21 -4
  45. package/lib/types/controllers/utils.d.ts +17 -2
  46. package/lib/types/facade/f-text-finder.d.ts +19 -2
  47. package/lib/types/facade/f-univer.d.ts +15 -0
  48. package/lib/types/locale/ca-ES.d.ts +16 -1
  49. package/lib/types/locale/es-ES.d.ts +16 -1
  50. package/lib/types/locale/fa-IR.d.ts +16 -1
  51. package/lib/types/locale/fr-FR.d.ts +16 -1
  52. package/lib/types/locale/ja-JP.d.ts +16 -1
  53. package/lib/types/locale/ko-KR.d.ts +16 -1
  54. package/lib/types/locale/ru-RU.d.ts +16 -1
  55. package/lib/types/locale/sk-SK.d.ts +16 -1
  56. package/lib/types/locale/vi-VN.d.ts +16 -1
  57. package/lib/types/locale/zh-TW.d.ts +16 -1
  58. package/lib/types/plugin.d.ts +16 -1
  59. package/lib/types/views/shapes/find-replace-highlight.shape.d.ts +18 -2
  60. package/lib/umd/facade.js +1 -1
  61. package/lib/umd/index.js +1 -1
  62. package/lib/umd/locale/ca-ES.js +1 -1
  63. package/lib/umd/locale/en-US.js +1 -1
  64. package/lib/umd/locale/es-ES.js +1 -1
  65. package/lib/umd/locale/fa-IR.js +1 -1
  66. package/lib/umd/locale/fr-FR.js +1 -1
  67. package/lib/umd/locale/ja-JP.js +1 -1
  68. package/lib/umd/locale/ko-KR.js +1 -1
  69. package/lib/umd/locale/ru-RU.js +1 -1
  70. package/lib/umd/locale/sk-SK.js +1 -1
  71. package/lib/umd/locale/vi-VN.js +1 -1
  72. package/lib/umd/locale/zh-CN.js +1 -1
  73. package/lib/umd/locale/zh-TW.js +1 -1
  74. package/package.json +12 -11
  75. package/LICENSE +0 -176
package/lib/es/index.js CHANGED
@@ -1,633 +1 @@
1
- var fe = Object.defineProperty;
2
- var _e = (h, e, t) => e in h ? fe(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
3
- var f = (h, e, t) => _e(h, typeof e != "symbol" ? e + "" : e, t);
4
- import { CommandType as T, IUndoRedoService as me, ICommandService as A, Rectangle as Se, Inject as k, Injector as N, IContextService as q, IUniverInstanceService as ee, ThemeService as pe, Disposable as te, EDITOR_ACTIVATED as ve, UniverInstanceType as ie, fromCallback as W, ColorKit as Ie, rotate as j, groupBy as Ce, ObjectMatrix as Re, Tools as ke, replaceInDocumentBody as we, DependentOn as be, IConfigService as Me, Plugin as xe, merge as ye } from "@univerjs/core";
5
- import { SetRangeValuesCommand as ne, SheetsSelectionsService as Ue, SelectRangeCommand as Pe, SetWorksheetActiveOperation as Be, SetSelectionsOperation as Fe, SetWorksheetActivateCommand as We, UniverSheetsPlugin as V } from "@univerjs/sheets";
6
- import { Shape as He, Rect as Te, IRenderManagerService as se, RENDER_RAW_FORMULA_KEY as Ee } from "@univerjs/engine-render";
7
- import { FindReplaceController as Oe, IFindReplaceService as Ae, FindModel as Ne, FindBy as M, FindScope as R, FindDirection as b, UniverFindReplacePlugin as De } from "@univerjs/find-replace";
8
- import { SheetSkeletonManagerService as $e, getCoordByCell as G, getSheetObject as Le, ScrollToCellCommand as je } from "@univerjs/sheets-ui";
9
- import { filter as x, Subject as Y, throttleTime as Ve, merge as Ge, skip as Ye, debounceTime as Ke } from "rxjs";
10
- const re = {
11
- id: "sheet.command.replace",
12
- type: T.COMMAND,
13
- handler: async (h, e) => {
14
- const t = h.get(me), i = h.get(A), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(ne.id, {
15
- unitId: n,
16
- subUnitId: c.subUnitId,
17
- value: c.value
18
- })));
19
- return s.dispose(), Qe(o, r);
20
- }
21
- };
22
- function Qe(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 Xe extends He {
30
- constructor(t, i) {
31
- super(t, i);
32
- f(this, "_activated", !1);
33
- f(this, "_inHiddenRange", !1);
34
- f(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
- Te.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 K(h, e) {
56
- return h.startRow === e.startRow && h.startColumn === e.startColumn;
57
- }
58
- function Q(h, e) {
59
- return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
60
- }
61
- function X(h, e) {
62
- return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
63
- }
64
- function ze(h, e) {
65
- return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
66
- }
67
- function Je(h, e) {
68
- return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
69
- }
70
- function Ze(h, e) {
71
- const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
72
- return r ? Se.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
73
- }
74
- var qe = Object.getOwnPropertyDescriptor, D = (h, e, t, i) => {
75
- for (var n = i > 1 ? void 0 : i ? qe(e, t) : e, r = h.length - 1, s; r >= 0; r--)
76
- (s = h[r]) && (n = s(n) || n);
77
- return n;
78
- }, S = (h, e) => (t, i) => e(t, i, h);
79
- let y = class extends te {
80
- constructor(e, t, i, n, r) {
81
- super();
82
- f(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(O);
90
- this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(this._contextService.subscribeContextValue$(ve).pipe(x((t) => !!t)).subscribe(() => this._findReplaceController.closePanel()));
91
- }
92
- _initCommands() {
93
- [re].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
94
- }
95
- };
96
- y = D([
97
- S(0, k(N)),
98
- S(1, k(Oe)),
99
- S(2, q),
100
- S(3, Ae),
101
- S(4, A)
102
- ], y);
103
- const et = "sheets-find-replace-provider", tt = 1e4;
104
- let E = class extends Ne {
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
- f(this, "_matchesUpdate$", new Y());
109
- f(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
110
- f(this, "_activelyChangingMatch$", new Y());
111
- f(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
- f(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
114
- /** Hold all matches in the currently searching scope. */
115
- f(this, "_matches", []);
116
- /** Position of the current focused ISheetCellMatch, starting from 1. */
117
- f(this, "_matchesPosition", 0);
118
- f(this, "_activeHighlightIndex", -1);
119
- f(this, "_highlightShapes", []);
120
- f(this, "_currentHighlightShape", null);
121
- /** This properties holds the query params during this searching session. */
122
- f(this, "_query", null);
123
- f(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 === M.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
149
- case R.UNIT:
150
- this.findInWorkbook(e);
151
- break;
152
- case R.SUBUNIT:
153
- default:
154
- this.findInActiveWorksheet(e);
155
- break;
156
- }
157
- }
158
- focusSelection() {
159
- const e = this.currentMatch;
160
- e && this._commandService.executeCommand(Pe.id, {
161
- unitId: e.unitId,
162
- subUnit: e.range.subUnitId,
163
- range: e.range.range
164
- });
165
- }
166
- _toggleDisplayRawFormula(e) {
167
- this._contextService.setContextValue(Ee, 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
- W(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(x(([s, o]) => s.id === Be.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
- W(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
196
- x(
197
- ([s]) => s.type === T.MUTATION && s.params.unitId === this._workbook.getUnitId()
198
- ),
199
- Ve(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) => !Ze(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
- Ge(
236
- W(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
237
- x(([c]) => {
238
- if (c.type === T.MUTATION && c.params.unitId === this._workbook.getUnitId())
239
- return !0;
240
- if (c.id === Fe.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(Ye(1))
249
- ).pipe(Ke(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: _, value: m } = l;
256
- if (r != null && r(a, d) || !m || e.getRowFiltered(a))
257
- continue;
258
- const { hit: I, replaceable: C, isFormula: p } = it(e, a, d, t, m);
259
- if (I) {
260
- const P = {
261
- provider: et,
262
- unitId: n,
263
- replaceable: C,
264
- isFormula: p,
265
- range: {
266
- subUnitId: o,
267
- range: {
268
- startRow: a,
269
- startColumn: d,
270
- endColumn: d + (u != null ? u : 1) - 1,
271
- endRow: a + (_ != null ? _ : 1) - 1
272
- }
273
- }
274
- };
275
- s.push(P);
276
- }
277
- }
278
- return { results: s };
279
- }
280
- _findInSelections(e, t, i, n) {
281
- const { findDirection: r } = i, s = r === b.ROW ? Q : X, 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.getColorFromTheme("yellow.400"), o = new Ie(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, _) => {
312
- const { startColumn: m, startRow: I, endColumn: C, endRow: p } = u.range.range, P = G(I, m, n, e), he = G(p, C, n, e), { startX: $, startY: L } = P, { endX: ce, endY: ae } = he;
313
- let B = !0;
314
- for (let v = I; v <= p; v++)
315
- if (c.getRowRawVisible(v)) {
316
- B = !1;
317
- break;
318
- }
319
- let F = !0;
320
- for (let v = m; v <= C; v++)
321
- if (c.getColVisible(v)) {
322
- F = !1;
323
- break;
324
- }
325
- const le = B || F, de = F ? 2 : ce - $, ue = B ? 2 : ae - L, ge = {
326
- left: $,
327
- top: L,
328
- color: o,
329
- width: de,
330
- height: ue,
331
- evented: !1,
332
- inHiddenRange: le,
333
- zIndex: tt
334
- };
335
- return new Xe(`find-highlight-${_}`, ge);
336
- });
337
- n.addObjects(a), this._highlightShapes = a, n.makeDirty();
338
- }
339
- _updateCurrentHighlightShape(e) {
340
- var t;
341
- if ((t = this._currentHighlightShape) == null || t.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, e !== void 0) {
342
- const i = this._highlightShapes[e];
343
- if (!i)
344
- return;
345
- this._currentHighlightShape = i, i.setShapeProps({ activated: !0 });
346
- }
347
- }
348
- _getSheetObject() {
349
- return Le(this._univerInstanceService, this._renderManagerService);
350
- }
351
- _focusMatch(e) {
352
- var i;
353
- const t = e.range.subUnitId;
354
- t !== ((i = this._workbook.getActiveSheet()) == null ? void 0 : i.getSheetId()) && this._commandService.executeCommand(We.id, { unitId: this._workbook.getUnitId(), subUnitId: t }, { fromFindReplace: !0 }), this._commandService.executeCommand(
355
- je.id,
356
- { range: e.range.range },
357
- { fromFindReplace: !0 }
358
- );
359
- }
360
- _tryRestoreLastMatchesPosition(e, t) {
361
- if (!e) return 0;
362
- const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
363
- if (i !== o.range.subUnitId)
364
- return !1;
365
- const { startColumn: c, startRow: l } = o.range.range;
366
- return c === n && l === r;
367
- });
368
- return s > -1 ? s + 1 : 0;
369
- }
370
- moveToNextMatch(e) {
371
- var o, c, l, a, d;
372
- if (!this._matches.length)
373
- return null;
374
- 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);
375
- if (s) {
376
- const [u, _] = s;
377
- return this._matchesPosition = _ + 1, this._query.findScope === R.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(u.range.subUnitId).findIndex((m) => m === u) : this._activeHighlightIndex = _, n || this._focusMatch(u), ((d = this._workbook.getActiveSheet()) == null ? void 0 : d.getSheetId()) === u.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), u;
378
- }
379
- return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
380
- }
381
- moveToPreviousMatch(e) {
382
- var o, c, l, a, d;
383
- if (!this._matches.length)
384
- return null;
385
- 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);
386
- if (s) {
387
- const [u, _] = s;
388
- return this._matchesPosition = _ + 1, this._query.findScope === R.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(u.range.subUnitId).findIndex((m) => m === u) : this._activeHighlightIndex = _, n || this._focusMatch(u), ((d = this._workbook.getActiveSheet()) == null ? void 0 : d.getSheetId()) === u.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), u;
389
- }
390
- return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
391
- }
392
- _findPreviousMatch(e = !1, t = !1, i = !1) {
393
- var o;
394
- if (this.currentMatch) {
395
- const c = this._matches.findIndex((u) => u === this.currentMatch);
396
- if (t)
397
- return [this.currentMatch, c];
398
- const l = c - 1;
399
- if (!e && l < 0)
400
- return null;
401
- const a = this._matches.length, d = (l + a) % a;
402
- return [this._matches[d], d];
403
- }
404
- const n = this._workbookSelections.getCurrentLastSelection();
405
- if (i || !n) {
406
- const c = this._matches.length - 1;
407
- return [this._matches[c], c];
408
- }
409
- if (this._query.findScope !== R.UNIT)
410
- return this._findPreviousMatchByRange(this._matches, n.range);
411
- const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
412
- if (!r)
413
- return null;
414
- const s = this._findPreviousWorksheetThatHasAMatch(r, e);
415
- return s ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
416
- }
417
- _findNextMatch(e = !1, t = !1, i = !1) {
418
- var o;
419
- if (this.currentMatch) {
420
- const c = this._matches.findIndex((u) => u === this.currentMatch);
421
- if (t)
422
- return [this.currentMatch, c];
423
- const l = c + 1, a = this._matches.length;
424
- if (!e && l >= a)
425
- return null;
426
- const d = l % a;
427
- return [this._matches[d], d];
428
- }
429
- const n = this._workbookSelections.getCurrentLastSelection();
430
- if (i || !n)
431
- return [this._matches[0], 0];
432
- if (this._query.findScope !== R.UNIT)
433
- return this._findNextMatchByRange(this._matches, n.range, t);
434
- const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
435
- if (!r)
436
- return null;
437
- const s = this._findNextWorksheetThatHasAMatch(r, e);
438
- return s ? this._findNextMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
439
- }
440
- _findPreviousWorksheetThatHasAMatch(e, t = !1) {
441
- const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? j(i, n + 1) : i.slice(0, n + 1)).findLast((o) => this._matchesByWorksheet.has(o));
442
- return s != null ? s : null;
443
- }
444
- _findNextWorksheetThatHasAMatch(e, t = !1) {
445
- const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? j(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
446
- return s != null ? s : null;
447
- }
448
- _findNextMatchByRange(e, t, i = !1) {
449
- const n = this._query.findDirection === b.ROW;
450
- let r = e.findIndex((o) => {
451
- const c = o.range.range;
452
- if (!(n ? Q(t, c) : X(t, c)))
453
- return !1;
454
- const a = K(t, c);
455
- return i ? a : !a;
456
- });
457
- r === -1 && (r = e.length - 1);
458
- const s = e[r];
459
- return [s, this._matches.findIndex((o) => o === s)];
460
- }
461
- _findPreviousMatchByRange(e, t, i = !1) {
462
- const n = this._query.findDirection === b.ROW;
463
- let r = this._matches.findLastIndex((o) => {
464
- const c = o.range.range;
465
- if (!(n ? ze(t, c) : Je(t, c)))
466
- return !1;
467
- const a = K(t, c);
468
- return i ? a : !a;
469
- });
470
- r === -1 && (r = 0);
471
- const s = e[r];
472
- return [s, this._matches.findIndex((o) => o === s)];
473
- }
474
- async replace(e) {
475
- if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
476
- return !1;
477
- const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
478
- this.currentMatch,
479
- i,
480
- this._query.findBy === M.FORMULA,
481
- this._query.findString,
482
- e,
483
- this._query.caseSensitive ? "g" : "ig"
484
- ), r = {
485
- unitId: this.currentMatch.unitId,
486
- subUnitId: t.subUnitId,
487
- value: {
488
- [t.range.startRow]: {
489
- [t.range.startColumn]: n
490
- }
491
- }
492
- };
493
- return this._commandService.executeCommand(ne.id, r);
494
- }
495
- async replaceAll(e) {
496
- if (this._matchesCount === 0 || !this._query)
497
- return { success: 0, failure: 0 };
498
- const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === M.FORMULA, o = n ? "g" : "ig", c = [];
499
- return Ce(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
500
- const u = new Re(), _ = this._workbook.getSheetBySheetId(d);
501
- a.forEach((m) => {
502
- const { startColumn: I, startRow: C } = m.range.range, p = this._getReplacedCellData(m, _, s, i, e, o);
503
- p && u.setValue(C, I, p);
504
- }), c.push({
505
- count: a.length,
506
- subUnitId: d,
507
- value: u.getMatrix()
508
- });
509
- }), c ? this._commandService.executeCommand(re.id, {
510
- unitId: t,
511
- replacements: c
512
- }) : { success: 0, failure: 0 };
513
- }
514
- _getReplacedCellData(e, t, i, n, r, s) {
515
- var _;
516
- const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
517
- if (e.isFormula)
518
- return i ? { f: a.f.replace(new RegExp(z(n), s), r), v: null } : null;
519
- if (!!((_ = a.p) != null && _.body)) {
520
- const m = ke.deepClone(a.p);
521
- return we(m.body, n, r, this._query.caseSensitive), { p: m };
522
- }
523
- return { v: a.v.toString().replace(new RegExp(z(n), s), r) };
524
- }
525
- };
526
- E = D([
527
- S(2, ee),
528
- S(3, se),
529
- S(4, A),
530
- S(5, q),
531
- S(6, k(pe)),
532
- S(7, k(Ue))
533
- ], E);
534
- function z(h) {
535
- return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
536
- }
537
- let O = class extends te {
538
- constructor(e, t, i) {
539
- super();
540
- /**
541
- * Hold all find results in this kind of univer business instances (Workbooks).
542
- */
543
- f(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
544
- this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
545
- }
546
- async find(e) {
547
- this._terminate();
548
- const t = this._univerInstanceService.getCurrentUnitOfType(ie.UNIVER_SHEET);
549
- if (!t) return [];
550
- const i = this._preprocessQuery(e), n = this._renderManagerService.getRenderById(t.getUnitId()).with($e), r = this._injector.createInstance(E, t, n);
551
- return this._findModelsByUnitId.set(t.getUnitId(), r), r.start(i), [r];
552
- }
553
- terminate() {
554
- this._terminate();
555
- }
556
- _terminate() {
557
- this._findModelsByUnitId.forEach((e) => e.dispose()), this._findModelsByUnitId.clear();
558
- }
559
- /**
560
- * Parsed the query object before do actual searching in favor of performance.
561
- * @param query the raw query object
562
- * @returns the parsed query object
563
- */
564
- _preprocessQuery(e) {
565
- let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
566
- return t = t.trim(), {
567
- ...e,
568
- findString: t
569
- };
570
- }
571
- };
572
- O = D([
573
- S(0, ee),
574
- S(1, se),
575
- S(2, k(N))
576
- ], O);
577
- const g = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
578
- function it(h, e, t, i, n) {
579
- const { findBy: r } = i, s = r === M.FORMULA, o = h.getCellRaw(e, t);
580
- return g.rawData = o, !(o != null && o.f) ? (g.isFormula = !1, H(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 ? H({ v: o.f }, i) ? (g.hit = !0, g.replaceable = !0, g) : (g.hit = !1, g.replaceable = !1, g) : (g.replaceable = !1, H(n, i) ? g.hit = !0 : g.hit = !1, g));
581
- }
582
- function H(h, e) {
583
- let t = nt(h);
584
- return t ? e.matchesTheWholeCell ? (t = st(t), e.caseSensitive ? t === e.findString : t.toLowerCase() === e.findString) : e.caseSensitive ? t.indexOf(e.findString) > -1 : t.toLowerCase().indexOf(e.findString) > -1 : !1;
585
- }
586
- function nt(h) {
587
- var t, i, n;
588
- 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;
589
- return typeof e == "number" ? `${e}` : typeof e == "boolean" ? e ? "1" : "0" : e;
590
- }
591
- function st(h) {
592
- return h.replace(/^ +/g, "").replace(/ +$/g, "");
593
- }
594
- const rt = "@univerjs/sheets-find-replace", ot = "0.17.0", oe = {
595
- name: rt,
596
- version: ot
597
- }, ht = "sheets-find-replace.config", J = {};
598
- var ct = Object.defineProperty, at = Object.getOwnPropertyDescriptor, lt = (h, e, t) => e in h ? ct(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, dt = (h, e, t, i) => {
599
- for (var n = i > 1 ? void 0 : i ? at(e, t) : e, r = h.length - 1, s; r >= 0; r--)
600
- (s = h[r]) && (n = s(n) || n);
601
- return n;
602
- }, Z = (h, e) => (t, i) => e(t, i, h), U = (h, e, t) => lt(h, typeof e != "symbol" ? e + "" : e, t);
603
- let w = class extends xe {
604
- constructor(h = J, e, t) {
605
- super(), this._config = h, this._injector = e, this._configService = t;
606
- const { ...i } = ye(
607
- {},
608
- J,
609
- this._config
610
- );
611
- this._configService.setConfig(ht, i);
612
- }
613
- onStarting() {
614
- [[y]].forEach((h) => this._injector.add(h));
615
- }
616
- onSteady() {
617
- this._injector.get(y);
618
- }
619
- };
620
- U(w, "pluginName", "SHEET_FIND_REPLACE_PLUGIN");
621
- U(w, "packageName", oe.name);
622
- U(w, "version", oe.version);
623
- U(w, "type", ie.UNIVER_SHEET);
624
- w = dt([
625
- be(V, V, De),
626
- Z(1, k(N)),
627
- Z(2, Me)
628
- ], w);
629
- export {
630
- re as SheetReplaceCommand,
631
- y as SheetsFindReplaceController,
632
- w as UniverSheetsFindReplacePlugin
633
- };
1
+ import{ColorKit as e,CommandType as t,DependentOn as n,Disposable as r,EDITOR_ACTIVATED as i,ICommandService as a,IConfigService as o,IContextService as s,IUndoRedoService as c,IUniverInstanceService as l,Inject as u,Injector as d,ObjectMatrix as f,Plugin as p,Rectangle as m,ThemeService as h,Tools as g,UniverInstanceType as _,fromCallback as v,groupBy as y,merge as b,replaceInDocumentBody as ee,rotate as x}from"@univerjs/core";import{SelectRangeCommand as S,SetRangeValuesCommand as C,SetSelectionsOperation as te,SetWorksheetActivateCommand as ne,SetWorksheetActiveOperation as re,SheetsSelectionsService as ie,UniverSheetsPlugin as w}from"@univerjs/sheets";import{IRenderManagerService as T,RENDER_RAW_FORMULA_KEY as ae,Rect as oe,Shape as se}from"@univerjs/engine-render";import{FindBy as E,FindDirection as D,FindModel as ce,FindReplaceController as le,FindScope as O,IFindReplaceService as ue,UniverFindReplacePlugin as de}from"@univerjs/find-replace";import{ScrollToCellCommand as fe,SheetSkeletonManagerService as pe,getCoordByCell as k,getSheetObject as me}from"@univerjs/sheets-ui";import{Subject as A,debounceTime as he,filter as j,merge as M,skip as N,throttleTime as P}from"rxjs";const F={id:`sheet.command.replace`,type:t.COMMAND,handler:async(e,t)=>{let n=e.get(c),r=e.get(a),{unitId:i,replacements:o}=t,s=n.__tempBatchingUndoRedo(i),l=await Promise.all(o.map(e=>r.executeCommand(C.id,{unitId:i,subUnitId:e.subUnitId,value:e.value})));return s.dispose(),I(l,o)}};function I(e,t){let n=0,r=0;return e.forEach((e,i)=>{let a=t[i].count;e?n+=a:r+=a}),{success:n,failure:r}}function L(e){"@babel/helpers - typeof";return L=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},L(e)}function ge(e,t){if(L(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(L(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function _e(e){var t=ge(e,`string`);return L(t)==`symbol`?t:t+``}function R(e,t,n){return(t=_e(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var ve=class extends se{constructor(e,t){super(e,t),R(this,`_activated`,!1),R(this,`_inHiddenRange`,!1),R(this,`_color`,void 0),t&&this.setShapeProps(t)}setShapeProps(e){this._activated=!!e.activated,e.inHiddenRange!==void 0&&(this._inHiddenRange=e.inHiddenRange),e.color!==void 0&&(this._color=e.color),this.transformByState({width:e.width,height:e.height})}_draw(e){let t=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})`;oe.drawWith(e,{width:this.width,height:this.height,fill:n,stroke:t?r:void 0,strokeWidth:t?2:0,evented:!1})}};function z(e,t){return e.startRow===t.startRow&&e.startColumn===t.startColumn}function B(e,t){return e.startRow<t.startRow||e.startRow===t.startRow&&e.startColumn<=t.startColumn}function V(e,t){return e.startColumn<t.startColumn||e.startColumn===t.startColumn&&e.startRow<=t.startRow}function ye(e,t){return e.startRow>t.startRow||e.startRow===t.startRow&&e.startColumn>=t.startColumn}function be(e,t){return e.startColumn>t.startColumn||e.startColumn===t.startColumn&&e.startRow>=t.startRow}function H(e,t){let{range:n}=e,{startRow:r,startColumn:i}=n,a=t.getMergedCell(r,i);return a?m.equals(n,a):n.endRow===n.startRow&&n.endColumn===n.startColumn}function U(e,t){return function(n,r){t(n,r,e)}}function W(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let G=class extends r{constructor(e,t,n,r,i){super(),this._injector=e,this._findReplaceController=t,this._contextService=n,this._findReplaceService=r,this._commandService=i,R(this,`_provider`,void 0),this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){let e=this._injector.createInstance(J);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(i).pipe(j(e=>!!e)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[F].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};G=W([U(0,u(d)),U(1,u(le)),U(2,s),U(3,ue),U(4,a)],G);let K=class extends ce{get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}constructor(e,t,n,r,i,a,o,s){super(),this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=r,this._commandService=i,this._contextService=a,this._themeService=o,R(this,`_matchesUpdate$`,new A),R(this,`matchesUpdate$`,this._matchesUpdate$.asObservable()),R(this,`_activelyChangingMatch$`,new A),R(this,`activelyChangingMatch$`,this._activelyChangingMatch$.asObservable()),R(this,`_matchesByWorksheet`,new Map),R(this,`_matches`,[]),R(this,`_matchesPosition`,0),R(this,`_activeHighlightIndex`,-1),R(this,`_highlightShapes`,[]),R(this,`_currentHighlightShape`,null),R(this,`_query`,null),R(this,`_workbookSelections`,void 0),this._workbookSelections=s.getWorkbookSelections(this.unitId)}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===E.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case O.UNIT:this.findInWorkbook(e);break;case O.SUBUNIT:default:this.findInActiveWorksheet(e);break}}focusSelection(){let e=this.currentMatch;e&&this._commandService.executeCommand(S.id,{unitId:e.unitId,subUnit:e.range.subUnitId,range:e.range.range})}_toggleDisplayRawFormula(e){this._contextService.setContextValue(ae,e)}findInWorkbook(e){let n=this._workbook.getUnitId(),r,i=!0,a=()=>{this._matches=this._workbook.getSheets().filter(e=>!e.isSheetHidden()).map(t=>{let r=this._findInWorksheet(t,e,n),i=t.getSheetId(),{results:a}=r;return a.length?this._matchesByWorksheet.set(i,r.results):this._matchesByWorksheet.delete(i),r}).map(e=>e.results).flat(),this._updateFindHighlight(),i?(r={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e,t])=>e.id===re.id&&!(t!=null&&t.fromFindReplace))).subscribe(()=>{let e=this._workbook.getActiveSheet();if(!e)return;let t=e.getSheetId();this._matchesByWorksheet.has(t)&&this._findNextMatchOnActiveSheetChange(e)})),this.disposeWithMe(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e])=>e.type===t.MUTATION&&e.params.unitId===this._workbook.getUnitId()),P(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>a())),a(),r}_findNextMatchOnActiveSheetChange(e){let t,n,r=0,i=this._matchesByWorksheet.get(e.getSheetId()),a=this._workbookSelections.getCurrentSelections();a!=null&&a.length?([t,r]=this._findNextMatchByRange(i,a[0].range),n=i.findIndex(e=>e===t)):(t=i[0],n=0,r=this._matches.findIndex(e=>e===t)),this._matchesPosition=r+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){let n=this._workbook.getUnitId(),r=()=>{var e;let t=this._workbook.getActiveSheet();if(!t)return!1;let n=this._workbookSelections.getCurrentSelections();return(e=n==null?void 0:n.some(e=>!H(e,t)))==null?!1:e},i,a=!0,o=!1,s=()=>{let t=this._workbook.getActiveSheet();if(!t)return{results:[]};let s=this.currentMatch;o=r();let c=this._workbookSelections.getCurrentSelections(),l=o?this._findInSelections(t,c,e,n):this._findInWorksheet(t,e,n);return this._matches=l.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(s,this._matches),a?(i=l,a=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),l};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(M(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e])=>{if(e.type===t.MUTATION&&e.params.unitId===this._workbook.getUnitId())return!0;if(e.id===te.id&&e.params.unitId===n){let e=r();return e===!1&&o===!1?!1:(o=e,!0)}return!1})),this._workbook.activeSheet$.pipe(N(1))).pipe(he(200)).subscribe(()=>s())),s(),i}_findInRange(e,t,n,r,i){let a=[],o=e.getSheetId(),s=(t.findDirection===D.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(let n of s){let{row:s,col:c,colSpan:l,rowSpan:u,value:d}=n;if(i!=null&&i(s,c)||!d||e.getRowFiltered(s))continue;let{hit:f,replaceable:p,isFormula:m}=xe(e,s,c,t,d);if(f){let e={provider:`sheets-find-replace-provider`,unitId:r,replaceable:p,isFormula:m,range:{subUnitId:o,range:{startRow:s,startColumn:c,endColumn:c+(l==null?1:l)-1,endRow:s+(u==null?1:u)-1}}};a.push(e)}}return{results:a}}_findInSelections(e,t,n,r){let{findDirection:i}=n,a=i===D.ROW?B:V,o=new Set;return{results:t.map(t=>this._findInRange(e,n,t.range,r,(e,t)=>{let n=`${e}-${t}`;return o.has(n)?!0:(o.add(n),!1)}).results).flat().sort((e,t)=>a(e.range.range,t.range.range)?-1:1)}}_findInWorksheet(e,t,n){let r=e.getRowCount(),i=e.getColumnCount(),a={startRow:0,startColumn:0,endRow:r-1,endColumn:i-1};return this._findInRange(e,t,a,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(e=>{var t;(t=e.getScene())==null||t.makeDirty(),e.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var t;this._disposeHighlights();let n=(t=this._sheetSkeletonManagerService.getCurrent())==null?void 0:t.skeleton;if(!n)return;let r=this._workbook.getUnitId(),i=this._renderManagerService.getRenderById(r);if(i==null)return;let{scene:a}=i,o=this._matches,s=new e(this._themeService.getColorFromTheme(`yellow.400`)).toRgb(),c=this._workbook.getActiveSheet();if(!c)return;let l=c.getSheetId(),u=o.filter(e=>e.range.subUnitId===l).map((e,t)=>{let{startColumn:r,startRow:i,endColumn:o,endRow:l}=e.range.range,u=k(i,r,a,n),d=k(l,o,a,n),{startX:f,startY:p}=u,{endX:m,endY:h}=d,g=!0;for(let e=i;e<=l;e++)if(c.getRowRawVisible(e)){g=!1;break}let _=!0;for(let e=r;e<=o;e++)if(c.getColVisible(e)){_=!1;break}let v=g||_,y={left:f,top:p,color:s,width:_?2:m-f,height:g?2:h-p,evented:!1,inHiddenRange:v,zIndex:1e4};return new ve(`find-highlight-${t}`,y)});a.addObjects(u),this._highlightShapes=u,a.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){let t=this._highlightShapes[e];if(!t)return;this._currentHighlightShape=t,t.setShapeProps({activated:!0})}}_getSheetObject(){return me(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var t;let n=e.range.subUnitId;n!==((t=this._workbook.getActiveSheet())==null?void 0:t.getSheetId())&&this._commandService.executeCommand(ne.id,{unitId:this._workbook.getUnitId(),subUnitId:n},{fromFindReplace:!0}),this._commandService.executeCommand(fe.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;let{subUnitId:n}=e.range,{startColumn:r,startRow:i}=e.range.range,a=t.findIndex(e=>{if(n!==e.range.subUnitId)return!1;let{startColumn:t,startRow:a}=e.range.range;return t===r&&a===i});return a>-1?a+1:0}moveToNextMatch(e){var t,n,r,i;if(!this._matches.length)return null;let a=(t=e==null?void 0:e.loop)==null?!1:t,o=(n=e==null?void 0:e.stayIfOnMatch)==null?!1:n,s=(r=e==null?void 0:e.noFocus)==null?!1:r,c=(i=e==null?void 0:e.ignoreSelection)==null?!1:i,l=this._findNextMatch(a,o,c);if(l){var u;let[e,t]=l;return this._matchesPosition=t+1,this._query.findScope===O.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,s||this._focusMatch(e),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===e.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var t,n,r,i;if(!this._matches.length)return null;let a=(t=e==null?void 0:e.loop)==null?!1:t,o=(n=e==null?void 0:e.stayIfOnMatch)==null?!1:n,s=(r=e==null?void 0:e.noFocus)==null?!1:r,c=(i=e==null?void 0:e.ignoreSelection)==null?!1:i,l=this._findPreviousMatch(a,o,c);if(l){var u;let[e,t]=l;return this._matchesPosition=t+1,this._query.findScope===O.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,s||this._focusMatch(e),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===e.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1,n=!1){var r;if(this.currentMatch){let n=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,n];let r=n-1;if(!e&&r<0)return null;let i=this._matches.length,a=(r+i)%i;return[this._matches[a],a]}let i=this._workbookSelections.getCurrentLastSelection();if(n||!i){let e=this._matches.length-1;return[this._matches[e],e]}if(this._query.findScope!==O.UNIT)return this._findPreviousMatchByRange(this._matches,i.range);let a=(r=this._workbook.getActiveSheet())==null?void 0:r.getSheetId();if(!a)return null;let o=this._findPreviousWorksheetThatHasAMatch(a,e);return o?this._findPreviousMatchByRange(this._matchesByWorksheet.get(o),i.range):null}_findNextMatch(e=!1,t=!1,n=!1){var r;if(this.currentMatch){let n=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,n];let r=n+1,i=this._matches.length;if(!e&&r>=i)return null;let a=r%i;return[this._matches[a],a]}let i=this._workbookSelections.getCurrentLastSelection();if(n||!i)return[this._matches[0],0];if(this._query.findScope!==O.UNIT)return this._findNextMatchByRange(this._matches,i.range,t);let a=(r=this._workbook.getActiveSheet())==null?void 0:r.getSheetId();if(!a)return null;let o=this._findNextWorksheetThatHasAMatch(a,e);return o?this._findNextMatchByRange(this._matchesByWorksheet.get(o),i.range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){let n=this._workbook.getSheetOrders(),r=n.findIndex(t=>t===e),i=(t?x(n,r+1):n.slice(0,r+1)).findLast(e=>this._matchesByWorksheet.has(e));return i==null?null:i}_findNextWorksheetThatHasAMatch(e,t=!1){let n=this._workbook.getSheetOrders(),r=n.findIndex(t=>t===e),i=(t?x(n,r):n.slice(r)).find(e=>this._matchesByWorksheet.has(e));return i==null?null:i}_findNextMatchByRange(e,t,n=!1){let r=this._query.findDirection===D.ROW,i=e.findIndex(e=>{let i=e.range.range;if(!(r?B(t,i):V(t,i)))return!1;let a=z(t,i);return n?a:!a});i===-1&&(i=e.length-1);let a=e[i];return[a,this._matches.findIndex(e=>e===a)]}_findPreviousMatchByRange(e,t,n=!1){let r=this._query.findDirection===D.ROW,i=this._matches.findLastIndex(e=>{let i=e.range.range;if(!(r?ye(t,i):be(t,i)))return!1;let a=z(t,i);return n?a:!a});i===-1&&(i=0);let a=e[i];return[a,this._matches.findIndex(e=>e===a)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;let t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),r=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===E.FORMULA,this._query.findString,e,this._query.caseSensitive?`g`:`ig`),i={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:r}}};return this._commandService.executeCommand(C.id,i)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};let t=this._workbook.getUnitId(),{findString:n,caseSensitive:r,findBy:i}=this._query,a=i===E.FORMULA,o=r?`g`:`ig`,s=[];return y(this._matches.filter(e=>e.replaceable),e=>e.range.subUnitId).forEach((t,r)=>{let i=new f,c=this._workbook.getSheetBySheetId(r);t.forEach(t=>{let{startColumn:r,startRow:s}=t.range.range,l=this._getReplacedCellData(t,c,a,n,e,o);l&&i.setValue(s,r,l)}),s.push({count:t.length,subUnitId:r,value:i.getMatrix()})}),s?this._commandService.executeCommand(F.id,{unitId:t,replacements:s}):{success:0,failure:0}}_getReplacedCellData(e,t,n,r,i,a){var o;let{startRow:s,startColumn:c}=e.range.range,l=t.getCellRaw(s,c);if(e.isFormula)return n?{f:l.f.replace(new RegExp(q(r),a),i),v:null}:null;if((o=l.p)!=null&&o.body){let e=g.deepClone(l.p);return ee(e.body,r,i,this._query.caseSensitive),{p:e}}return{v:l.v.toString().replace(new RegExp(q(r),a),i)}}};K=W([U(2,l),U(3,T),U(4,a),U(5,s),U(6,u(h)),U(7,u(ie))],K);function q(e){return e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,`\\$&`)}let J=class extends r{constructor(e,t,n){super(),this._univerInstanceService=e,this._renderManagerService=t,this._injector=n,R(this,`_findModelsByUnitId`,new Map)}async find(e){this._terminate();let t=this._univerInstanceService.getCurrentUnitOfType(_.UNIVER_SHEET);if(!t)return[];let n=this._preprocessQuery(e),r=this._renderManagerService.getRenderById(t.getUnitId()).with(pe),i=this._injector.createInstance(K,t,r);return this._findModelsByUnitId.set(t.getUnitId(),i),i.start(n),[i]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};J=W([U(0,l),U(1,T),U(2,u(d))],J);const Y={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function xe(e,t,n,r,i){let{findBy:a}=r,o=a===E.FORMULA,s=e.getCellRaw(t,n);return Y.rawData=s,s!=null&&s.f?(Y.isFormula=!0,o?X({v:s.f},r)?(Y.hit=!0,Y.replaceable=!0,Y):(Y.hit=!1,Y.replaceable=!1,Y):(Y.replaceable=!1,X(i,r)?Y.hit=!0:Y.hit=!1,Y)):(Y.isFormula=!1,X(i,r)?s?(Y.hit=!0,Y.replaceable=!0):(Y.hit=!0,Y.replaceable=!1):(Y.hit=!1,Y.replaceable=!1),Y)}function X(e,t){let n=Se(e);return n?t.matchesTheWholeCell?(n=Ce(n),t.caseSensitive?n===t.findString:n.toLowerCase()===t.findString):t.caseSensitive?n.indexOf(t.findString)>-1:n.toLowerCase().indexOf(t.findString)>-1:!1}function Se(e){var t,n;let r=(t=e==null||(n=e.p)==null||(n=n.body)==null?void 0:n.dataStream)==null?e==null?void 0:e.v:t;return typeof r==`number`?`${r}`:typeof r==`boolean`?r?`1`:`0`:r}function Ce(e){return e.replace(/^ +/g,``).replace(/ +$/g,``)}var we=`@univerjs/sheets-find-replace`,Te=`0.18.0-insiders.20260328-0842384`;const Z=`sheets-find-replace.config`;Symbol(Z);const Q={};let $=class extends p{constructor(e=Q,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=b({},Q,this._config);this._configService.setConfig(Z,r)}onStarting(){[[G]].forEach(e=>this._injector.add(e))}onSteady(){this._injector.get(G)}};R($,`pluginName`,`SHEET_FIND_REPLACE_PLUGIN`),R($,`packageName`,we),R($,`version`,Te),R($,`type`,_.UNIVER_SHEET),$=W([n(w,w,de),U(1,u(d)),U(2,o)],$);export{F as SheetReplaceCommand,G as SheetsFindReplaceController,$ as UniverSheetsFindReplacePlugin};