@univerjs/sheets-find-replace 0.1.3

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