@univerjs/sheets-find-replace 0.2.5 → 0.2.6

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/es/index.js CHANGED
@@ -1,130 +1,141 @@
1
- var de = Object.defineProperty;
2
- var ue = (h, e, t) => e in h ? de(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
3
- var g = (h, e, t) => ue(h, typeof e != "symbol" ? e + "" : e, t);
4
- import { CommandType as B, IUndoRedoService as fe, ICommandService as H, Rectangle as ge, OnLifecycle as _e, LifecycleStages as me, Inject as C, Injector as T, ThemeService as Se, Disposable as K, EDITOR_ACTIVATED as pe, IContextService as z, IUniverInstanceService as J, UniverInstanceType as Z, fromCallback as P, ColorKit as ve, rotate as $, groupBy as Ie, ObjectMatrix as Ce, Tools as Re, replaceInDocumentBody as we, DependentOn as be, Plugin as ke } from "@univerjs/core";
5
- import { FindReplaceController as Me, IFindReplaceService as xe, FindModel as ye, FindBy as b, FindScope as I, FindDirection as w, UniverFindReplacePlugin as Pe } from "@univerjs/find-replace";
6
- import { SetRangeValuesCommand as q, SheetsSelectionsService as Ue, SetWorksheetActiveOperation as Be, SetSelectionsOperation as We, SetWorksheetActivateCommand as Fe, UniverSheetsPlugin as L } from "@univerjs/sheets";
7
- import { Shape as He, Rect as Te, IRenderManagerService as ee, RENDER_RAW_FORMULA_KEY as Oe } from "@univerjs/engine-render";
8
- import { SheetSkeletonManagerService as Ee, getCoordByCell as j, getSheetObject as Ae, ScrollToCellCommand as Ne } from "@univerjs/sheets-ui";
9
- import { filter as k, Subject as V, throttleTime as De, merge as $e, skip as Le, debounceTime as je } from "rxjs";
10
- class Ve extends He {
11
- constructor(t, i) {
12
- super(t, i);
13
- g(this, "_activated", !1);
14
- g(this, "_inHiddenRange", !1);
15
- g(this, "_color");
16
- i && this.setShapeProps(i);
17
- }
18
- setShapeProps(t) {
19
- this._activated = !!t.activated, typeof t.inHiddenRange < "u" && (this._inHiddenRange = t.inHiddenRange), typeof t.color < "u" && (this._color = t.color), this.transformByState({
20
- width: t.width,
21
- height: t.height
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
5
+ import { CommandType, IUndoRedoService, ICommandService, Rectangle, OnLifecycle, LifecycleStages, Inject, Injector, ThemeService, Disposable, EDITOR_ACTIVATED, IContextService, IUniverInstanceService, UniverInstanceType, fromCallback, ColorKit, rotate, groupBy, ObjectMatrix, Tools, replaceInDocumentBody, DependentOn, Plugin } from "@univerjs/core";
6
+ import { FindReplaceController, IFindReplaceService, FindModel, FindBy, FindScope, FindDirection, UniverFindReplacePlugin } from "@univerjs/find-replace";
7
+ import { SetRangeValuesCommand, SheetsSelectionsService, SetWorksheetActiveOperation, SetSelectionsOperation, SetWorksheetActivateCommand, UniverSheetsPlugin } from "@univerjs/sheets";
8
+ import { Shape, Rect, IRenderManagerService, RENDER_RAW_FORMULA_KEY } from "@univerjs/engine-render";
9
+ import { SheetSkeletonManagerService, getCoordByCell, getSheetObject, ScrollToCellCommand } from "@univerjs/sheets-ui";
10
+ import { filter, Subject, throttleTime, merge, skip, debounceTime } from "rxjs";
11
+ const _SheetFindReplaceHighlightShape = class _SheetFindReplaceHighlightShape extends Shape {
12
+ constructor(key, props) {
13
+ super(key, props);
14
+ __publicField(this, "_activated", !1);
15
+ __publicField(this, "_inHiddenRange", !1);
16
+ __publicField(this, "_color");
17
+ props && this.setShapeProps(props);
18
+ }
19
+ setShapeProps(props) {
20
+ this._activated = !!props.activated, typeof props.inHiddenRange < "u" && (this._inHiddenRange = props.inHiddenRange), typeof props.color < "u" && (this._color = props.color), this.transformByState({
21
+ width: props.width,
22
+ height: props.height
22
23
  });
23
24
  }
24
- _draw(t) {
25
- 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})`;
26
- Te.drawWith(t, {
25
+ _draw(ctx) {
26
+ const activated = this._activated, color = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`, borderColor = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
27
+ Rect.drawWith(ctx, {
27
28
  width: this.width,
28
29
  height: this.height,
29
- fill: n,
30
- stroke: i ? r : void 0,
31
- strokeWidth: i ? 2 : 0,
30
+ fill: color,
31
+ stroke: activated ? borderColor : void 0,
32
+ strokeWidth: activated ? 2 : 0,
32
33
  evented: !1
33
34
  });
34
35
  }
35
- }
36
- const te = {
36
+ };
37
+ __name(_SheetFindReplaceHighlightShape, "SheetFindReplaceHighlightShape");
38
+ let SheetFindReplaceHighlightShape = _SheetFindReplaceHighlightShape;
39
+ const SheetReplaceCommand = {
37
40
  id: "sheet.command.replace",
38
- type: B.COMMAND,
39
- handler: async (h, e) => {
40
- const t = h.get(fe), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(q.id, {
41
- unitId: n,
42
- subUnitId: c.subUnitId,
43
- value: c.value
41
+ type: CommandType.COMMAND,
42
+ handler: /* @__PURE__ */ __name(async (accessor, params) => {
43
+ const undoRedoService = accessor.get(IUndoRedoService), commandService = accessor.get(ICommandService), { unitId, replacements } = params, disposeBatchingHandler = undoRedoService.__tempBatchingUndoRedo(unitId), results = await Promise.all(replacements.map((replacement) => commandService.executeCommand(SetRangeValuesCommand.id, {
44
+ unitId,
45
+ subUnitId: replacement.subUnitId,
46
+ value: replacement.value
44
47
  })));
45
- return s.dispose(), Qe(o, r);
46
- }
48
+ return disposeBatchingHandler.dispose(), getReplaceAllResult(results, replacements);
49
+ }, "handler")
47
50
  };
48
- function Qe(h, e) {
49
- let t = 0, i = 0;
50
- return h.forEach((n, r) => {
51
- const s = e[r].count;
52
- n ? t += s : i += s;
53
- }), { success: t, failure: i };
51
+ function getReplaceAllResult(results, replacements) {
52
+ let success = 0, failure = 0;
53
+ return results.forEach((r, index) => {
54
+ const count = replacements[index].count;
55
+ r ? success += count : failure += count;
56
+ }), { success, failure };
54
57
  }
55
- function Q(h, e) {
56
- return h.startRow === e.startRow && h.startColumn === e.startColumn;
58
+ __name(getReplaceAllResult, "getReplaceAllResult");
59
+ function isSamePosition(range1, range2) {
60
+ return range1.startRow === range2.startRow && range1.startColumn === range2.startColumn;
57
61
  }
58
- function X(h, e) {
59
- return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
62
+ __name(isSamePosition, "isSamePosition");
63
+ function isBehindPositionWithRowPriority(range1, range2) {
64
+ return range1.startRow < range2.startRow || range1.startRow === range2.startRow && range1.startColumn <= range2.startColumn;
60
65
  }
61
- function Y(h, e) {
62
- return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
66
+ __name(isBehindPositionWithRowPriority, "isBehindPositionWithRowPriority");
67
+ function isBehindPositionWithColumnPriority(range1, range2) {
68
+ return range1.startColumn < range2.startColumn || range1.startColumn === range2.startColumn && range1.startRow <= range2.startRow;
63
69
  }
64
- function Xe(h, e) {
65
- return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
70
+ __name(isBehindPositionWithColumnPriority, "isBehindPositionWithColumnPriority");
71
+ function isBeforePositionWithRowPriority(range1, range2) {
72
+ return range1.startRow > range2.startRow || range1.startRow === range2.startRow && range1.startColumn >= range2.startColumn;
66
73
  }
67
- function Ye(h, e) {
68
- return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
74
+ __name(isBeforePositionWithRowPriority, "isBeforePositionWithRowPriority");
75
+ function isBeforePositionWithColumnPriority(range1, range2) {
76
+ return range1.startColumn > range2.startColumn || range1.startColumn === range2.startColumn && range1.startRow >= range2.startRow;
69
77
  }
70
- function Ge(h, e) {
71
- const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
72
- return r ? ge.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
78
+ __name(isBeforePositionWithColumnPriority, "isBeforePositionWithColumnPriority");
79
+ function isSelectionSingleCell(selection, worksheet) {
80
+ const { range } = selection, { startRow, startColumn } = range, hasMergedCell = worksheet.getMergedCell(startRow, startColumn);
81
+ return hasMergedCell ? Rectangle.equals(range, hasMergedCell) : range.endRow === range.startRow && range.endColumn === range.startColumn;
73
82
  }
74
- var Ke = Object.defineProperty, ze = Object.getOwnPropertyDescriptor, O = (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 = (i ? s(e, t, n) : s(n)) || n);
77
- return i && n && Ke(e, t, n), n;
78
- }, S = (h, e) => (t, i) => e(t, i, h);
79
- let M = class extends K {
80
- constructor(e, t, i, n, r) {
83
+ __name(isSelectionSingleCell, "isSelectionSingleCell");
84
+ var __defProp$1 = Object.defineProperty, __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor, __decorateClass$1 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
85
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
86
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
87
+ return kind && result && __defProp$1(target, key, result), result;
88
+ }, "__decorateClass$1"), __decorateParam$1 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$1"), _a;
89
+ let SheetsFindReplaceController = (_a = class extends Disposable {
90
+ constructor(_injector, _findReplaceController, _contextService, _findReplaceService, _commandService) {
81
91
  super();
82
- g(this, "_provider");
83
- this._injector = e, this._findReplaceController = t, this._contextService = i, this._findReplaceService = n, this._commandService = r, this._init(), this._initCommands();
92
+ __publicField(this, "_provider");
93
+ this._injector = _injector, this._findReplaceController = _findReplaceController, this._contextService = _contextService, this._findReplaceService = _findReplaceService, this._commandService = _commandService, this._init(), this._initCommands();
84
94
  }
85
95
  dispose() {
86
96
  super.dispose(), this._findReplaceController.closePanel(), this._provider.dispose();
87
97
  }
88
98
  _init() {
89
- const e = this._injector.createInstance(F);
90
- this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(
91
- this._contextService.subscribeContextValue$(pe).pipe(k((t) => !!t)).subscribe(() => this._findReplaceController.closePanel())
99
+ const provider = this._injector.createInstance(SheetsFindReplaceProvider);
100
+ this._provider = provider, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(provider)), this.disposeWithMe(
101
+ this._contextService.subscribeContextValue$(EDITOR_ACTIVATED).pipe(filter((v) => !!v)).subscribe(() => this._findReplaceController.closePanel())
92
102
  );
93
103
  }
94
104
  _initCommands() {
95
- [te].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
96
- }
97
- };
98
- M = O([
99
- _e(me.Steady, M),
100
- S(0, C(T)),
101
- S(1, C(Me)),
102
- S(2, z),
103
- S(3, xe),
104
- S(4, H)
105
- ], M);
106
- const Je = "sheets-find-replace-provider", Ze = 1e4;
107
- let W = class extends ye {
108
- constructor(e, t, i, n, r, s, o, c) {
105
+ [SheetReplaceCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
106
+ }
107
+ }, __name(_a, "SheetsFindReplaceController"), _a);
108
+ SheetsFindReplaceController = __decorateClass$1([
109
+ OnLifecycle(LifecycleStages.Steady, SheetsFindReplaceController),
110
+ __decorateParam$1(0, Inject(Injector)),
111
+ __decorateParam$1(1, Inject(FindReplaceController)),
112
+ __decorateParam$1(2, IContextService),
113
+ __decorateParam$1(3, IFindReplaceService),
114
+ __decorateParam$1(4, ICommandService)
115
+ ], SheetsFindReplaceController);
116
+ const SHEETS_FIND_REPLACE_PROVIDER_NAME = "sheets-find-replace-provider", FIND_REPLACE_Z_INDEX = 1e4;
117
+ var _a2;
118
+ let SheetFindModel = (_a2 = class extends FindModel {
119
+ constructor(_workbook, _sheetSkeletonManagerService, _univerInstanceService, _renderManagerService, _commandService, _contextService, _themeService, _selectionManagerService) {
109
120
  super();
110
121
  // We can directly inject the `FindReplaceService` here, and call its methods instead of using the observables.
111
- g(this, "_matchesUpdate$", new V());
112
- g(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
113
- g(this, "_activelyChangingMatch$", new V());
114
- g(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
122
+ __publicField(this, "_matchesUpdate$", new Subject());
123
+ __publicField(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
124
+ __publicField(this, "_activelyChangingMatch$", new Subject());
125
+ __publicField(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
115
126
  /** Hold matches by the worksheet they are in. Make it easier to track the next (or previous) match when searching in the whole workbook. */
116
- g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
127
+ __publicField(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
117
128
  /** Hold all matches in the currently searching scope. */
118
- g(this, "_matches", []);
129
+ __publicField(this, "_matches", []);
119
130
  /** Position of the current focused ISheetCellMatch, starting from 1. */
120
- g(this, "_matchesPosition", 0);
121
- g(this, "_activeHighlightIndex", -1);
122
- g(this, "_highlightShapes", []);
123
- g(this, "_currentHighlightShape", null);
131
+ __publicField(this, "_matchesPosition", 0);
132
+ __publicField(this, "_activeHighlightIndex", -1);
133
+ __publicField(this, "_highlightShapes", []);
134
+ __publicField(this, "_currentHighlightShape", null);
124
135
  /** This properties holds the query params during this searching session. */
125
- g(this, "_query", null);
126
- g(this, "_workbookSelections");
127
- 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);
136
+ __publicField(this, "_query", null);
137
+ __publicField(this, "_workbookSelections");
138
+ this._workbook = _workbook, this._sheetSkeletonManagerService = _sheetSkeletonManagerService, this._univerInstanceService = _univerInstanceService, this._renderManagerService = _renderManagerService, this._commandService = _commandService, this._contextService = _contextService, this._themeService = _themeService, this._workbookSelections = _selectionManagerService.getWorkbookSelections(this.unitId);
128
139
  }
129
140
  get _matchesCount() {
130
141
  return this._matches.length;
@@ -147,466 +158,473 @@ let W = class extends ye {
147
158
  getMatches() {
148
159
  return this._matches;
149
160
  }
150
- start(e) {
151
- switch (this._query = e, e.findBy === b.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
152
- case I.UNIT:
153
- this.findInWorkbook(e);
161
+ start(query) {
162
+ switch (this._query = query, query.findBy === FindBy.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), query.findScope) {
163
+ case FindScope.UNIT:
164
+ this.findInWorkbook(query);
154
165
  break;
155
- case I.SUBUNIT:
166
+ case FindScope.SUBUNIT:
156
167
  default:
157
- this.findInActiveWorksheet(e);
168
+ this.findInActiveWorksheet(query);
158
169
  break;
159
170
  }
160
171
  }
161
- _toggleDisplayRawFormula(e) {
162
- this._contextService.setContextValue(Oe, e);
172
+ _toggleDisplayRawFormula(force) {
173
+ this._contextService.setContextValue(RENDER_RAW_FORMULA_KEY, force);
163
174
  }
164
175
  /**
165
176
  * Find all matches in the current workbook no matter which worksheet is activated.
166
177
  * @param query the query object
167
178
  * @returns the query complete event
168
179
  */
169
- findInWorkbook(e) {
170
- const t = this._workbook.getUnitId();
171
- let i, n = !0;
172
- const r = () => {
173
- const s = this._workbook.getSheets().filter((o) => !o.isSheetHidden()).map((o) => {
174
- const c = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: a } = c;
175
- return a.length ? this._matchesByWorksheet.set(l, c.results) : this._matchesByWorksheet.delete(l), c;
180
+ findInWorkbook(query) {
181
+ const unitId = this._workbook.getUnitId();
182
+ let complete, firstSearch = !0;
183
+ const findInWorkbook = /* @__PURE__ */ __name(() => {
184
+ const allCompletes = this._workbook.getSheets().filter((worksheet) => !worksheet.isSheetHidden()).map((worksheet) => {
185
+ const complete2 = this._findInWorksheet(worksheet, query, unitId), sheetId = worksheet.getSheetId(), { results } = complete2;
186
+ return results.length ? this._matchesByWorksheet.set(sheetId, complete2.results) : this._matchesByWorksheet.delete(sheetId), complete2;
176
187
  });
177
- this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
178
- };
188
+ this._matches = allCompletes.map((c) => c.results).flat(), this._updateFindHighlight(), firstSearch ? (complete = { results: this._matches }, firstSearch = !1) : this._matchesUpdate$.next(this._matches);
189
+ }, "findInWorkbook");
179
190
  return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
180
191
  this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
181
192
  })), this.disposeWithMe(
182
- P(this._commandService.onCommandExecuted.bind(this._commandService.onCommandExecuted)).pipe(
183
- k(([s, o]) => s.id === Be.id && !(o != null && o.fromFindReplace))
193
+ fromCallback(this._commandService.onCommandExecuted.bind(this._commandService.onCommandExecuted)).pipe(
194
+ filter(([command, options]) => command.id === SetWorksheetActiveOperation.id && !(options != null && options.fromFindReplace))
184
195
  ).subscribe(() => {
185
- const s = this._workbook.getActiveSheet();
186
- if (!s)
196
+ const activeSheet = this._workbook.getActiveSheet();
197
+ if (!activeSheet)
187
198
  return;
188
- const o = s.getSheetId();
189
- this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
199
+ const activeSheetId = activeSheet.getSheetId();
200
+ this._matchesByWorksheet.has(activeSheetId) && this._findNextMatchOnActiveSheetChange(activeSheet);
190
201
  })
191
202
  ), this.disposeWithMe(
192
- P(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
193
- k(
194
- ([s]) => s.type === B.MUTATION && s.params.unitId === this._workbook.getUnitId()
203
+ fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
204
+ filter(
205
+ ([command]) => command.type === CommandType.MUTATION && command.params.unitId === this._workbook.getUnitId()
195
206
  ),
196
- De(600, void 0, { leading: !1, trailing: !0 })
197
- ).subscribe(() => r())
198
- ), r(), i;
207
+ throttleTime(600, void 0, { leading: !1, trailing: !0 })
208
+ ).subscribe(() => findInWorkbook())
209
+ ), findInWorkbook(), complete;
199
210
  }
200
211
  /**
201
212
  * This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
202
213
  * in the new worksheet.
203
214
  */
204
- _findNextMatchOnActiveSheetChange(e) {
205
- let t, i, n = 0;
206
- const r = this._matchesByWorksheet.get(e.getSheetId()), s = this._workbookSelections.getCurrentSelections();
207
- 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);
215
+ _findNextMatchOnActiveSheetChange(activeSheet) {
216
+ let match, index, globalIndex = 0;
217
+ const matchesByWorksheet = this._matchesByWorksheet.get(activeSheet.getSheetId()), selections = this._workbookSelections.getCurrentSelections();
218
+ selections != null && selections.length ? ([match, globalIndex] = this._findNextMatchByRange(matchesByWorksheet, selections[0].range), index = matchesByWorksheet.findIndex((m) => m === match)) : (match = matchesByWorksheet[0], index = 0, globalIndex = this._matches.findIndex((m) => m === match)), this._matchesPosition = globalIndex + 1, this._activelyChangingMatch$.next(match), this._activeHighlightIndex = index, this._updateFindHighlight(), this._updateCurrentHighlightShape(index);
208
219
  }
209
220
  /**
210
221
  * Find all matches (only) in the currently activated worksheet.
211
222
  * @param query the query object
212
223
  * @returns the query complete event
213
224
  */
214
- findInActiveWorksheet(e) {
215
- const t = this._workbook.getUnitId(), i = () => {
216
- var d;
217
- const c = this._workbook.getActiveSheet();
218
- if (!c) return !1;
219
- const l = this._workbookSelections.getCurrentSelections();
220
- return (d = l == null ? void 0 : l.some((u) => !Ge(u, c))) != null ? d : !1;
221
- };
222
- let n, r = !0, s = !1;
223
- const o = () => {
224
- const c = this._workbook.getActiveSheet();
225
- if (!c) return { results: [] };
226
- const l = this.currentMatch;
227
- s = i();
228
- const a = this._workbookSelections.getCurrentSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
229
- 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;
230
- };
225
+ findInActiveWorksheet(query) {
226
+ const unitId = this._workbook.getUnitId(), checkShouldFindInSelections = /* @__PURE__ */ __name(() => {
227
+ var _a5;
228
+ const currentWorksheet = this._workbook.getActiveSheet();
229
+ if (!currentWorksheet) return !1;
230
+ const currentSelections = this._workbookSelections.getCurrentSelections();
231
+ return (_a5 = currentSelections == null ? void 0 : currentSelections.some((selection) => !isSelectionSingleCell(selection, currentWorksheet))) != null ? _a5 : !1;
232
+ }, "checkShouldFindInSelections");
233
+ let complete, firstSearch = !0, findBySelections = !1;
234
+ const performFindInWorksheet = /* @__PURE__ */ __name(() => {
235
+ const currentWorksheet = this._workbook.getActiveSheet();
236
+ if (!currentWorksheet) return { results: [] };
237
+ const lastMatch = this.currentMatch;
238
+ findBySelections = checkShouldFindInSelections();
239
+ const currentSelections = this._workbookSelections.getCurrentSelections(), newComplete = findBySelections ? this._findInSelections(currentWorksheet, currentSelections, query, unitId) : this._findInWorksheet(currentWorksheet, query, unitId);
240
+ return this._matches = newComplete.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(lastMatch, this._matches), firstSearch ? (complete = newComplete, firstSearch = !1) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), newComplete;
241
+ }, "performFindInWorksheet");
231
242
  return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
232
- $e(
233
- P(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
234
- k(([c]) => {
235
- if (c.type === B.MUTATION && c.params.unitId === this._workbook.getUnitId())
243
+ merge(
244
+ fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
245
+ filter(([command]) => {
246
+ if (command.type === CommandType.MUTATION && command.params.unitId === this._workbook.getUnitId())
236
247
  return !0;
237
- if (c.id === We.id && c.params.unitId === t) {
238
- const l = i();
239
- return l === !1 && s === !1 ? !1 : (s = l, !0);
248
+ if (command.id === SetSelectionsOperation.id && command.params.unitId === unitId) {
249
+ const shouldFindBySelections = checkShouldFindInSelections();
250
+ return shouldFindBySelections === !1 && findBySelections === !1 ? !1 : (findBySelections = shouldFindBySelections, !0);
240
251
  }
241
252
  return !1;
242
253
  })
243
254
  ),
244
255
  // activeSheet$ is a BehaviorSubject, so we need to skip the first
245
- this._workbook.activeSheet$.pipe(Le(1))
246
- ).pipe(je(200)).subscribe(() => o())
247
- ), o(), n;
248
- }
249
- _findInRange(e, t, i, n, r) {
250
- const s = [], o = e.getSheetId(), c = (t.findDirection === w.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
251
- for (const l of c) {
252
- const { row: a, col: d, colSpan: u, rowSpan: _, value: m } = l;
253
- if (r != null && r(a, d) || !m || e.getRowFiltered(a))
256
+ this._workbook.activeSheet$.pipe(skip(1))
257
+ ).pipe(debounceTime(200)).subscribe(() => performFindInWorksheet())
258
+ ), performFindInWorksheet(), complete;
259
+ }
260
+ _findInRange(worksheet, query, range, unitId, dedupeFn) {
261
+ const results = [], subUnitId = worksheet.getSheetId(), iter = (query.findDirection === FindDirection.COLUMN ? worksheet.iterateByColumn : worksheet.iterateByRow).bind(worksheet)(range);
262
+ for (const value of iter) {
263
+ const { row, col, colSpan, rowSpan, value: cellData } = value;
264
+ if (dedupeFn != null && dedupeFn(row, col) || !cellData || worksheet.getRowFiltered(row))
254
265
  continue;
255
- const { hit: p, replaceable: R, isFormula: v } = qe(e, a, d, t, m);
256
- if (p) {
257
- const y = {
258
- provider: Je,
259
- unitId: n,
260
- replaceable: R,
261
- isFormula: v,
266
+ const { hit, replaceable, isFormula } = hitCell(worksheet, row, col, query, cellData);
267
+ if (hit) {
268
+ const result = {
269
+ provider: SHEETS_FIND_REPLACE_PROVIDER_NAME,
270
+ unitId,
271
+ replaceable,
272
+ isFormula,
262
273
  range: {
263
- subUnitId: o,
274
+ subUnitId,
264
275
  range: {
265
- startRow: a,
266
- startColumn: d,
267
- endColumn: d + (u != null ? u : 1) - 1,
268
- endRow: a + (_ != null ? _ : 1) - 1
276
+ startRow: row,
277
+ startColumn: col,
278
+ endColumn: col + (colSpan != null ? colSpan : 1) - 1,
279
+ endRow: row + (rowSpan != null ? rowSpan : 1) - 1
269
280
  }
270
281
  }
271
282
  };
272
- s.push(y);
283
+ results.push(result);
273
284
  }
274
285
  }
275
- return { results: s };
276
- }
277
- _findInSelections(e, t, i, n) {
278
- const { findDirection: r } = i, s = r === w.ROW ? X : Y, o = /* @__PURE__ */ new Set();
279
- return { results: t.map((l) => this._findInRange(
280
- e,
281
- i,
282
- l.range,
283
- n,
284
- (a, d) => {
285
- const u = `${a}-${d}`;
286
- return o.has(u) ? !0 : (o.add(u), !1);
286
+ return { results };
287
+ }
288
+ _findInSelections(worksheet, selections, query, unitId) {
289
+ const { findDirection } = query, sortFn = findDirection === FindDirection.ROW ? isBehindPositionWithRowPriority : isBehindPositionWithColumnPriority, dedupeSet = /* @__PURE__ */ new Set();
290
+ return { results: selections.map((selection) => this._findInRange(
291
+ worksheet,
292
+ query,
293
+ selection.range,
294
+ unitId,
295
+ (row, col) => {
296
+ const key = `${row}-${col}`;
297
+ return dedupeSet.has(key) ? !0 : (dedupeSet.add(key), !1);
287
298
  }
288
- ).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
299
+ ).results).flat().sort((a, b) => sortFn(a.range.range, b.range.range) ? -1 : 1) };
289
300
  }
290
301
  /** Find matches in a given worksheet. */
291
- _findInWorksheet(e, t, i) {
292
- const n = e.getRowCount(), r = e.getColumnCount(), s = { startRow: 0, startColumn: 0, endRow: n - 1, endColumn: r - 1 };
293
- return this._findInRange(e, t, s, i);
302
+ _findInWorksheet(worksheet, query, unitId) {
303
+ const rowCount = worksheet.getRowCount(), colCount = worksheet.getColumnCount(), range = { startRow: 0, startColumn: 0, endRow: rowCount - 1, endColumn: colCount - 1 };
304
+ return this._findInRange(worksheet, query, range, unitId);
294
305
  }
295
306
  _disposeHighlights() {
296
- var e;
297
- this._highlightShapes.forEach((t) => {
298
- t.getScene().makeDirty(), t.dispose();
299
- }), this._highlightShapes = [], (e = this._currentHighlightShape) == null || e.dispose(), this._currentHighlightShape = null;
307
+ var _a5;
308
+ this._highlightShapes.forEach((shape) => {
309
+ shape.getScene().makeDirty(), shape.dispose();
310
+ }), this._highlightShapes = [], (_a5 = this._currentHighlightShape) == null || _a5.dispose(), this._currentHighlightShape = null;
300
311
  }
301
312
  _updateFindHighlight() {
302
- var d;
313
+ var _a5;
303
314
  this._disposeHighlights();
304
- const e = (d = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : d.skeleton;
305
- if (!e)
315
+ const skeleton = (_a5 = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : _a5.skeleton;
316
+ if (!skeleton)
306
317
  return;
307
- const t = this._workbook.getUnitId(), i = this._renderManagerService.getRenderById(t);
308
- if (i == null)
318
+ const unitId = this._workbook.getUnitId(), currentRender = this._renderManagerService.getRenderById(unitId);
319
+ if (currentRender == null)
309
320
  return;
310
- const { scene: n } = i, r = this._matches, s = this._themeService.getCurrentTheme().gold400, o = new ve(s).toRgb(), c = this._workbook.getActiveSheet();
311
- if (!c)
321
+ const { scene } = currentRender, matches = this._matches, searchBackgroundColor = this._themeService.getCurrentTheme().gold400, color = new ColorKit(searchBackgroundColor).toRgb(), worksheet = this._workbook.getActiveSheet();
322
+ if (!worksheet)
312
323
  return;
313
- const l = c.getSheetId(), a = r.filter((u) => u.range.subUnitId === l).map((u, _) => {
314
- const { startColumn: m, startRow: p, endColumn: R, endRow: v } = u.range.range, y = j(p, m, n, e), se = j(v, R, n, e), { startX: E, startY: A } = y, { endX: re, endY: oe } = se, N = !c.getRowRawVisible(p), D = !c.getColVisible(m), he = N || D, ce = D ? 2 : re - E, ae = N ? 2 : oe - A, le = {
315
- left: E,
316
- top: A,
317
- color: o,
318
- width: ce,
319
- height: ae,
324
+ const activeSheetId = worksheet.getSheetId(), highlightShapes = matches.filter((match) => match.range.subUnitId === activeSheetId).map((find, index) => {
325
+ const { startColumn, startRow, endColumn, endRow } = find.range.range, startPosition = getCoordByCell(startRow, startColumn, scene, skeleton), endPosition = getCoordByCell(endRow, endColumn, scene, skeleton), { startX, startY } = startPosition, { endX, endY } = endPosition, rowHidden = !worksheet.getRowRawVisible(startRow), columnHidden = !worksheet.getColVisible(startColumn), inHiddenRange = rowHidden || columnHidden, width = columnHidden ? 2 : endX - startX, height = rowHidden ? 2 : endY - startY, props = {
326
+ left: startX,
327
+ top: startY,
328
+ color,
329
+ width,
330
+ height,
320
331
  evented: !1,
321
- inHiddenRange: he,
322
- zIndex: Ze
332
+ inHiddenRange,
333
+ zIndex: FIND_REPLACE_Z_INDEX
323
334
  };
324
- return new Ve(`find-highlight-${_}`, le);
335
+ return new SheetFindReplaceHighlightShape(`find-highlight-${index}`, props);
325
336
  });
326
- n.addObjects(a), this._highlightShapes = a, n.makeDirty();
337
+ scene.addObjects(highlightShapes), this._highlightShapes = highlightShapes, scene.makeDirty();
327
338
  }
328
- _updateCurrentHighlightShape(e) {
329
- var t;
330
- if ((t = this._currentHighlightShape) == null || t.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, e !== void 0) {
331
- const i = this._highlightShapes[e];
332
- if (!i)
339
+ _updateCurrentHighlightShape(matchIndex) {
340
+ var _a5;
341
+ if ((_a5 = this._currentHighlightShape) == null || _a5.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, matchIndex !== void 0) {
342
+ const shape = this._highlightShapes[matchIndex];
343
+ if (!shape)
333
344
  return;
334
- this._currentHighlightShape = i, i.setShapeProps({ activated: !0 });
345
+ this._currentHighlightShape = shape, shape.setShapeProps({ activated: !0 });
335
346
  }
336
347
  }
337
348
  _getSheetObject() {
338
- return Ae(this._univerInstanceService, this._renderManagerService);
339
- }
340
- _focusMatch(e) {
341
- var i;
342
- const t = e.range.subUnitId;
343
- t !== ((i = this._workbook.getActiveSheet()) == null ? void 0 : i.getSheetId()) && this._commandService.executeCommand(
344
- Fe.id,
345
- { unitId: this._workbook.getUnitId(), subUnitId: t },
349
+ return getSheetObject(this._univerInstanceService, this._renderManagerService);
350
+ }
351
+ _focusMatch(match) {
352
+ var _a5;
353
+ const subUnitId = match.range.subUnitId;
354
+ subUnitId !== ((_a5 = this._workbook.getActiveSheet()) == null ? void 0 : _a5.getSheetId()) && this._commandService.executeCommand(
355
+ SetWorksheetActivateCommand.id,
356
+ { unitId: this._workbook.getUnitId(), subUnitId },
346
357
  { fromFindReplace: !0 }
347
358
  ), this._commandService.executeCommand(
348
- Ne.id,
349
- { range: e.range.range },
359
+ ScrollToCellCommand.id,
360
+ { range: match.range.range },
350
361
  { fromFindReplace: !0 }
351
362
  );
352
363
  }
353
- _tryRestoreLastMatchesPosition(e, t) {
354
- if (!e) return 0;
355
- const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
356
- if (i !== o.range.subUnitId)
364
+ _tryRestoreLastMatchesPosition(lastMatch, newMatches) {
365
+ if (!lastMatch) return 0;
366
+ const { subUnitId: lastSubUnitId } = lastMatch.range, { startColumn: lastStartColumn, startRow: lastStartRow } = lastMatch.range.range, index = newMatches.findIndex((match) => {
367
+ if (lastSubUnitId !== match.range.subUnitId)
357
368
  return !1;
358
- const { startColumn: c, startRow: l } = o.range.range;
359
- return c === n && l === r;
369
+ const { startColumn, startRow } = match.range.range;
370
+ return startColumn === lastStartColumn && startRow === lastStartRow;
360
371
  });
361
- return s > -1 ? s + 1 : 0;
372
+ return index > -1 ? index + 1 : 0;
362
373
  }
363
- moveToNextMatch(e) {
364
- var o, c, l, a, d;
374
+ moveToNextMatch(params) {
375
+ var _a5, _b, _c, _d, _e;
365
376
  if (!this._matches.length)
366
377
  return null;
367
- 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);
368
- if (s) {
369
- const [u, _] = s;
370
- return this._matchesPosition = _ + 1, this._query.findScope === I.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
+ const loop = (_a5 = params == null ? void 0 : params.loop) != null ? _a5 : !1, stayIfOnMatch = (_b = params == null ? void 0 : params.stayIfOnMatch) != null ? _b : !1, noFocus = (_c = params == null ? void 0 : params.noFocus) != null ? _c : !1, ignoreSelection = (_d = params == null ? void 0 : params.ignoreSelection) != null ? _d : !1, matchToMove = this._findNextMatch(loop, stayIfOnMatch, ignoreSelection);
379
+ if (matchToMove) {
380
+ const [match, index] = matchToMove;
381
+ return this._matchesPosition = index + 1, this._query.findScope === FindScope.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(match.range.subUnitId).findIndex((m) => m === match) : this._activeHighlightIndex = index, noFocus || this._focusMatch(match), ((_e = this._workbook.getActiveSheet()) == null ? void 0 : _e.getSheetId()) === match.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), match;
371
382
  }
372
383
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
373
384
  }
374
- moveToPreviousMatch(e) {
375
- var o, c, l, a, d;
385
+ moveToPreviousMatch(params) {
386
+ var _a5, _b, _c, _d, _e;
376
387
  if (!this._matches.length)
377
388
  return null;
378
- 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);
379
- if (s) {
380
- const [u, _] = s;
381
- return this._matchesPosition = _ + 1, this._query.findScope === I.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
+ const loop = (_a5 = params == null ? void 0 : params.loop) != null ? _a5 : !1, stayIfOnMatch = (_b = params == null ? void 0 : params.stayIfOnMatch) != null ? _b : !1, noFocus = (_c = params == null ? void 0 : params.noFocus) != null ? _c : !1, ignoreSelection = (_d = params == null ? void 0 : params.ignoreSelection) != null ? _d : !1, matchToMove = this._findPreviousMatch(loop, stayIfOnMatch, ignoreSelection);
390
+ if (matchToMove) {
391
+ const [match, index] = matchToMove;
392
+ return this._matchesPosition = index + 1, this._query.findScope === FindScope.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(match.range.subUnitId).findIndex((m) => m === match) : this._activeHighlightIndex = index, noFocus || this._focusMatch(match), ((_e = this._workbook.getActiveSheet()) == null ? void 0 : _e.getSheetId()) === match.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), match;
382
393
  }
383
394
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
384
395
  }
385
- _findPreviousMatch(e = !1, t = !1, i = !1) {
386
- var o;
396
+ _findPreviousMatch(loop = !1, stayIfOnMatch = !1, ignoreSelection = !1) {
397
+ var _a5;
387
398
  if (this.currentMatch) {
388
- const c = this._matches.findIndex((u) => u === this.currentMatch);
389
- if (t)
390
- return [this.currentMatch, c];
391
- const l = c - 1;
392
- if (!e && l < 0)
399
+ const currentMatchIndex = this._matches.findIndex((match) => match === this.currentMatch);
400
+ if (stayIfOnMatch)
401
+ return [this.currentMatch, currentMatchIndex];
402
+ const nextMatchIndex = currentMatchIndex - 1;
403
+ if (!loop && nextMatchIndex < 0)
393
404
  return null;
394
- const a = this._matches.length, d = (l + a) % a;
395
- return [this._matches[d], d];
405
+ const length = this._matches.length, modded = (nextMatchIndex + length) % length;
406
+ return [this._matches[modded], modded];
396
407
  }
397
- const n = this._workbookSelections.getCurrentLastSelection();
398
- if (i || !n) {
399
- const c = this._matches.length - 1;
400
- return [this._matches[c], c];
408
+ const lastSelection = this._workbookSelections.getCurrentLastSelection();
409
+ if (ignoreSelection || !lastSelection) {
410
+ const lastIndex = this._matches.length - 1;
411
+ return [this._matches[lastIndex], lastIndex];
401
412
  }
402
- if (this._query.findScope !== I.UNIT)
403
- return this._findPreviousMatchByRange(this._matches, n.range);
404
- const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
405
- if (!r)
413
+ if (this._query.findScope !== FindScope.UNIT)
414
+ return this._findPreviousMatchByRange(this._matches, lastSelection.range);
415
+ const currentSheetId = (_a5 = this._workbook.getActiveSheet()) == null ? void 0 : _a5.getSheetId();
416
+ if (!currentSheetId)
406
417
  return null;
407
- const s = this._findPreviousWorksheetThatHasAMatch(r, e);
408
- return s ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
418
+ const worksheetThatHasMatch = this._findPreviousWorksheetThatHasAMatch(currentSheetId, loop);
419
+ return worksheetThatHasMatch ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch), lastSelection.range) : null;
409
420
  }
410
- _findNextMatch(e = !1, t = !1, i = !1) {
411
- var o;
421
+ _findNextMatch(loop = !1, stayIfOnMatch = !1, ignoreSelection = !1) {
422
+ var _a5;
412
423
  if (this.currentMatch) {
413
- const c = this._matches.findIndex((u) => u === this.currentMatch);
414
- if (t)
415
- return [this.currentMatch, c];
416
- const l = c + 1, a = this._matches.length;
417
- if (!e && l >= a)
424
+ const currentMatchIndex = this._matches.findIndex((match) => match === this.currentMatch);
425
+ if (stayIfOnMatch)
426
+ return [this.currentMatch, currentMatchIndex];
427
+ const nextMatchIndex = currentMatchIndex + 1, length = this._matches.length;
428
+ if (!loop && nextMatchIndex >= length)
418
429
  return null;
419
- const d = l % a;
420
- return [this._matches[d], d];
430
+ const modded = nextMatchIndex % length;
431
+ return [this._matches[modded], modded];
421
432
  }
422
- const n = this._workbookSelections.getCurrentLastSelection();
423
- if (i || !n)
433
+ const last = this._workbookSelections.getCurrentLastSelection();
434
+ if (ignoreSelection || !last)
424
435
  return [this._matches[0], 0];
425
- if (this._query.findScope !== I.UNIT)
426
- return this._findNextMatchByRange(this._matches, n.range, t);
427
- const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
428
- if (!r)
436
+ if (this._query.findScope !== FindScope.UNIT)
437
+ return this._findNextMatchByRange(this._matches, last.range, stayIfOnMatch);
438
+ const currentSheetId = (_a5 = this._workbook.getActiveSheet()) == null ? void 0 : _a5.getSheetId();
439
+ if (!currentSheetId)
429
440
  return null;
430
- const s = this._findNextWorksheetThatHasAMatch(r, e);
431
- return s ? this._findNextMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
432
- }
433
- _findPreviousWorksheetThatHasAMatch(e, t = !1) {
434
- 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));
435
- return s != null ? s : null;
436
- }
437
- _findNextWorksheetThatHasAMatch(e, t = !1) {
438
- const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
439
- return s != null ? s : null;
440
- }
441
- _findNextMatchByRange(e, t, i = !1) {
442
- const n = this._query.findDirection === w.ROW;
443
- let r = e.findIndex((o) => {
444
- const c = o.range.range;
445
- if (!(n ? X(t, c) : Y(t, c)))
441
+ const worksheetThatHasMatch = this._findNextWorksheetThatHasAMatch(currentSheetId, loop);
442
+ return worksheetThatHasMatch ? this._findNextMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch), last.range) : null;
443
+ }
444
+ _findPreviousWorksheetThatHasAMatch(currentWorksheet, loop = !1) {
445
+ const rawWorksheetsInOrder = this._workbook.getSheetOrders(), currentSheetIndex = rawWorksheetsInOrder.findIndex((sheet) => sheet === currentWorksheet), first = (loop ? rotate(rawWorksheetsInOrder, currentSheetIndex + 1) : rawWorksheetsInOrder.slice(0, currentSheetIndex + 1)).findLast((worksheet) => this._matchesByWorksheet.has(worksheet));
446
+ return first != null ? first : null;
447
+ }
448
+ _findNextWorksheetThatHasAMatch(currentWorksheet, loop = !1) {
449
+ const rawWorksheetsInOrder = this._workbook.getSheetOrders(), currentSheetIndex = rawWorksheetsInOrder.findIndex((sheet) => sheet === currentWorksheet), first = (loop ? rotate(rawWorksheetsInOrder, currentSheetIndex) : rawWorksheetsInOrder.slice(currentSheetIndex)).find((worksheet) => this._matchesByWorksheet.has(worksheet));
450
+ return first != null ? first : null;
451
+ }
452
+ _findNextMatchByRange(matches, range, stayIfOnMatch = !1) {
453
+ const findByRow = this._query.findDirection === FindDirection.ROW;
454
+ let index = matches.findIndex((match2) => {
455
+ const matchRange = match2.range.range;
456
+ if (!(findByRow ? isBehindPositionWithRowPriority(range, matchRange) : isBehindPositionWithColumnPriority(range, matchRange)))
446
457
  return !1;
447
- const a = Q(t, c);
448
- return i ? a : !a;
458
+ const isSame = isSamePosition(range, matchRange);
459
+ return stayIfOnMatch ? isSame : !isSame;
449
460
  });
450
- r === -1 && (r = e.length - 1);
451
- const s = e[r];
452
- return [s, this._matches.findIndex((o) => o === s)];
453
- }
454
- _findPreviousMatchByRange(e, t, i = !1) {
455
- const n = this._query.findDirection === w.ROW;
456
- let r = this._matches.findLastIndex((o) => {
457
- const c = o.range.range;
458
- if (!(n ? Xe(t, c) : Ye(t, c)))
461
+ index === -1 && (index = matches.length - 1);
462
+ const match = matches[index];
463
+ return [match, this._matches.findIndex((m) => m === match)];
464
+ }
465
+ _findPreviousMatchByRange(matches, range, stayIfOnMatch = !1) {
466
+ const findByRow = this._query.findDirection === FindDirection.ROW;
467
+ let index = this._matches.findLastIndex((match2) => {
468
+ const matchRange = match2.range.range;
469
+ if (!(findByRow ? isBeforePositionWithRowPriority(range, matchRange) : isBeforePositionWithColumnPriority(range, matchRange)))
459
470
  return !1;
460
- const a = Q(t, c);
461
- return i ? a : !a;
471
+ const isSame = isSamePosition(range, matchRange);
472
+ return stayIfOnMatch ? isSame : !isSame;
462
473
  });
463
- r === -1 && (r = 0);
464
- const s = e[r];
465
- return [s, this._matches.findIndex((o) => o === s)];
474
+ index === -1 && (index = 0);
475
+ const match = matches[index];
476
+ return [match, this._matches.findIndex((m) => m === match)];
466
477
  }
467
- async replace(e) {
478
+ async replace(replaceString) {
468
479
  if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
469
480
  return !1;
470
- const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
481
+ const range = this.currentMatch.range, targetWorksheet = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), newContent = this._getReplacedCellData(
471
482
  this.currentMatch,
472
- i,
473
- this._query.findBy === b.FORMULA,
483
+ targetWorksheet,
484
+ this._query.findBy === FindBy.FORMULA,
474
485
  this._query.findString,
475
- e,
486
+ replaceString,
476
487
  this._query.caseSensitive ? "g" : "ig"
477
- ), r = {
488
+ ), params = {
478
489
  unitId: this.currentMatch.unitId,
479
- subUnitId: t.subUnitId,
490
+ subUnitId: range.subUnitId,
480
491
  value: {
481
- [t.range.startRow]: {
482
- [t.range.startColumn]: n
492
+ [range.range.startRow]: {
493
+ [range.range.startColumn]: newContent
483
494
  }
484
495
  }
485
496
  };
486
- return this._commandService.executeCommand(q.id, r);
497
+ return this._commandService.executeCommand(SetRangeValuesCommand.id, params);
487
498
  }
488
- async replaceAll(e) {
499
+ async replaceAll(replaceString) {
489
500
  if (this._matchesCount === 0 || !this._query)
490
501
  return { success: 0, failure: 0 };
491
- const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === b.FORMULA, o = n ? "g" : "ig", c = [];
492
- return Ie(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
493
- const u = new Ce(), _ = this._workbook.getSheetBySheetId(d);
494
- a.forEach((m) => {
495
- const { startColumn: p, startRow: R } = m.range.range, v = this._getReplacedCellData(m, _, s, i, e, o);
496
- v && u.setValue(R, p, v);
497
- }), c.push({
498
- count: a.length,
499
- subUnitId: d,
500
- value: u.getMatrix()
502
+ const unitId = this._workbook.getUnitId(), { findString, caseSensitive, findBy } = this._query, shouldReplaceFormula = findBy === FindBy.FORMULA, replaceFlag = caseSensitive ? "g" : "ig", replacements = [];
503
+ return groupBy(this._matches.filter((m) => m.replaceable), (match) => match.range.subUnitId).forEach((matches, subUnitId) => {
504
+ const matrix = new ObjectMatrix(), worksheet = this._workbook.getSheetBySheetId(subUnitId);
505
+ matches.forEach((match) => {
506
+ const { startColumn, startRow } = match.range.range, newCellData = this._getReplacedCellData(match, worksheet, shouldReplaceFormula, findString, replaceString, replaceFlag);
507
+ newCellData && matrix.setValue(startRow, startColumn, newCellData);
508
+ }), replacements.push({
509
+ count: matches.length,
510
+ subUnitId,
511
+ value: matrix.getMatrix()
501
512
  });
502
- }), c ? this._commandService.executeCommand(te.id, {
503
- unitId: t,
504
- replacements: c
513
+ }), replacements ? this._commandService.executeCommand(SheetReplaceCommand.id, {
514
+ unitId,
515
+ replacements
505
516
  }) : { success: 0, failure: 0 };
506
517
  }
507
- _getReplacedCellData(e, t, i, n, r, s) {
508
- var _;
509
- const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
510
- if (e.isFormula)
511
- return i ? { f: a.f.replace(new RegExp(G(n), s), r), v: null } : null;
512
- if (!!((_ = a.p) != null && _.body)) {
513
- const m = Re.deepClone(a.p);
514
- return we(m.body, n, r), { p: m };
518
+ _getReplacedCellData(match, worksheet, shouldReplaceFormula, findString, replaceString, replaceFlag) {
519
+ var _a5;
520
+ const range = match.range.range, { startRow, startColumn } = range, currentContent = worksheet.getCellRaw(startRow, startColumn);
521
+ if (match.isFormula)
522
+ return shouldReplaceFormula ? { f: currentContent.f.replace(new RegExp(escapeRegExp(findString), replaceFlag), replaceString), v: null } : null;
523
+ if (!!((_a5 = currentContent.p) != null && _a5.body)) {
524
+ const clonedRichText = Tools.deepClone(currentContent.p);
525
+ return replaceInDocumentBody(clonedRichText.body, findString, replaceString), { p: clonedRichText };
515
526
  }
516
- return { v: a.v.toString().replace(new RegExp(G(n), s), r) };
517
- }
518
- };
519
- W = O([
520
- S(2, J),
521
- S(3, ee),
522
- S(4, H),
523
- S(5, z),
524
- S(6, C(Se)),
525
- S(7, C(Ue))
526
- ], W);
527
- function G(h) {
528
- return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
527
+ return { v: currentContent.v.toString().replace(new RegExp(escapeRegExp(findString), replaceFlag), replaceString) };
528
+ }
529
+ }, __name(_a2, "SheetFindModel"), _a2);
530
+ SheetFindModel = __decorateClass$1([
531
+ __decorateParam$1(2, IUniverInstanceService),
532
+ __decorateParam$1(3, IRenderManagerService),
533
+ __decorateParam$1(4, ICommandService),
534
+ __decorateParam$1(5, IContextService),
535
+ __decorateParam$1(6, Inject(ThemeService)),
536
+ __decorateParam$1(7, Inject(SheetsSelectionsService))
537
+ ], SheetFindModel);
538
+ function escapeRegExp(text) {
539
+ return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
529
540
  }
530
- let F = class extends K {
531
- constructor(e, t, i) {
541
+ __name(escapeRegExp, "escapeRegExp");
542
+ var _a3;
543
+ let SheetsFindReplaceProvider = (_a3 = class extends Disposable {
544
+ constructor(_univerInstanceService, _renderManagerService, _injector) {
532
545
  super();
533
546
  /**
534
547
  * Hold all find results in this kind of univer business instances (Workbooks).
535
548
  */
536
- g(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
537
- this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
549
+ __publicField(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
550
+ this._univerInstanceService = _univerInstanceService, this._renderManagerService = _renderManagerService, this._injector = _injector;
538
551
  }
539
- async find(e) {
552
+ async find(query) {
540
553
  this._terminate();
541
- const t = this._univerInstanceService.getAllUnitsForType(Z.UNIVER_SHEET), i = this._preprocessQuery(e);
542
- return t.map((r) => {
543
- const s = this._renderManagerService.getRenderById(r.getUnitId()).with(Ee), o = this._injector.createInstance(W, r, s);
544
- return this._findModelsByUnitId.set(r.getUnitId(), o), o.start(i), o;
554
+ const allWorkbooks = this._univerInstanceService.getAllUnitsForType(UniverInstanceType.UNIVER_SHEET), parsedQuery = this._preprocessQuery(query);
555
+ return allWorkbooks.map((workbook) => {
556
+ const skeletonManagerService = this._renderManagerService.getRenderById(workbook.getUnitId()).with(SheetSkeletonManagerService), sheetFind = this._injector.createInstance(SheetFindModel, workbook, skeletonManagerService);
557
+ return this._findModelsByUnitId.set(workbook.getUnitId(), sheetFind), sheetFind.start(parsedQuery), sheetFind;
545
558
  });
546
559
  }
547
560
  terminate() {
548
561
  this._terminate();
549
562
  }
550
563
  _terminate() {
551
- this._findModelsByUnitId.forEach((e) => e.dispose()), this._findModelsByUnitId.clear();
564
+ this._findModelsByUnitId.forEach((model) => model.dispose()), this._findModelsByUnitId.clear();
552
565
  }
553
566
  /**
554
567
  * Parsed the query object before do actual searching in favor of performance.
555
568
  * @param query the raw query object
556
569
  * @returns the parsed query object
557
570
  */
558
- _preprocessQuery(e) {
559
- let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
560
- return t = t.trim(), {
561
- ...e,
562
- findString: t
571
+ _preprocessQuery(query) {
572
+ let findString = query.caseSensitive ? query.findString : query.findString.toLowerCase();
573
+ return findString = findString.trim(), {
574
+ ...query,
575
+ findString
563
576
  };
564
577
  }
565
- };
566
- F = O([
567
- S(0, J),
568
- S(1, ee),
569
- S(2, C(T))
570
- ], F);
571
- const f = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
572
- function qe(h, e, t, i, n) {
573
- const { findBy: r } = i, s = r === b.FORMULA, o = h.getCellRaw(e, t);
574
- return f.rawData = o, !(o != null && o.f) ? (f.isFormula = !1, U(n, i) ? o ? (f.hit = !0, f.replaceable = !0) : (f.hit = !0, f.replaceable = !1) : (f.hit = !1, f.replaceable = !1), f) : (f.isFormula = !0, s ? U({ v: o.f }, i) ? (f.hit = !0, f.replaceable = !0, f) : (f.hit = !1, f.replaceable = !1, f) : (f.replaceable = !1, U(n, i) ? f.hit = !0 : f.hit = !1, f));
578
+ }, __name(_a3, "SheetsFindReplaceProvider"), _a3);
579
+ SheetsFindReplaceProvider = __decorateClass$1([
580
+ __decorateParam$1(0, IUniverInstanceService),
581
+ __decorateParam$1(1, IRenderManagerService),
582
+ __decorateParam$1(2, Inject(Injector))
583
+ ], SheetsFindReplaceProvider);
584
+ const VALUE_PASSING_OBJECT = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
585
+ function hitCell(worksheet, row, col, query, cellData) {
586
+ const { findBy } = query, findByFormula = findBy === FindBy.FORMULA, rawData = worksheet.getCellRaw(row, col);
587
+ return VALUE_PASSING_OBJECT.rawData = rawData, !(rawData != null && rawData.f) ? (VALUE_PASSING_OBJECT.isFormula = !1, matchCellData(cellData, query) ? rawData ? (VALUE_PASSING_OBJECT.hit = !0, VALUE_PASSING_OBJECT.replaceable = !0) : (VALUE_PASSING_OBJECT.hit = !0, VALUE_PASSING_OBJECT.replaceable = !1) : (VALUE_PASSING_OBJECT.hit = !1, VALUE_PASSING_OBJECT.replaceable = !1), VALUE_PASSING_OBJECT) : (VALUE_PASSING_OBJECT.isFormula = !0, findByFormula ? matchCellData({ v: rawData.f }, query) ? (VALUE_PASSING_OBJECT.hit = !0, VALUE_PASSING_OBJECT.replaceable = !0, VALUE_PASSING_OBJECT) : (VALUE_PASSING_OBJECT.hit = !1, VALUE_PASSING_OBJECT.replaceable = !1, VALUE_PASSING_OBJECT) : (VALUE_PASSING_OBJECT.replaceable = !1, matchCellData(cellData, query) ? VALUE_PASSING_OBJECT.hit = !0 : VALUE_PASSING_OBJECT.hit = !1, VALUE_PASSING_OBJECT));
575
588
  }
576
- function U(h, e) {
577
- let t = et(h);
578
- return t ? e.matchesTheWholeCell ? (t = tt(t), e.caseSensitive ? t === e.findString : t.toLowerCase() === e.findString) : e.caseSensitive ? t.indexOf(e.findString) > -1 : t.toLowerCase().indexOf(e.findString) > -1 : !1;
589
+ __name(hitCell, "hitCell");
590
+ function matchCellData(cellData, query) {
591
+ let value = extractPureValue(cellData);
592
+ return value ? query.matchesTheWholeCell ? (value = trimLeadingTrailingWhitespace(value), query.caseSensitive ? value === query.findString : value.toLowerCase() === query.findString) : query.caseSensitive ? value.indexOf(query.findString) > -1 : value.toLowerCase().indexOf(query.findString) > -1 : !1;
579
593
  }
580
- function et(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;
594
+ __name(matchCellData, "matchCellData");
595
+ function extractPureValue(cell) {
596
+ var _a5, _b, _c;
597
+ const rawValue = (_c = (_b = (_a5 = cell == null ? void 0 : cell.p) == null ? void 0 : _a5.body) == null ? void 0 : _b.dataStream) != null ? _c : cell == null ? void 0 : cell.v;
598
+ return typeof rawValue == "number" ? `${rawValue}` : typeof rawValue == "boolean" ? rawValue ? "1" : "0" : rawValue;
584
599
  }
585
- function tt(h) {
586
- return h.replace(/^ +/g, "").replace(/ +$/g, "");
600
+ __name(extractPureValue, "extractPureValue");
601
+ function trimLeadingTrailingWhitespace(value) {
602
+ return value.replace(/^ +/g, "").replace(/ +$/g, "");
587
603
  }
588
- var ie = Object.defineProperty, it = Object.getOwnPropertyDescriptor, nt = (h, e, t) => e in h ? ie(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, st = (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 && ie(e, t, n), n;
592
- }, rt = (h, e) => (t, i) => e(t, i, h), ne = (h, e, t) => nt(h, typeof e != "symbol" ? e + "" : e, t);
593
- const ot = "SHEET_FIND_REPLACE_PLUGIN";
594
- let x = class extends ke {
595
- constructor(h, e) {
596
- super(), this._injector = e;
597
- }
598
- onStarting(h) {
599
- [[M]].forEach((e) => h.add(e));
600
- }
601
- };
602
- ne(x, "pluginName", ot);
603
- ne(x, "type", Z.UNIVER_SHEET);
604
- x = st([
605
- be(L, L, Pe),
606
- rt(1, C(T))
607
- ], x);
604
+ __name(trimLeadingTrailingWhitespace, "trimLeadingTrailingWhitespace");
605
+ var __defProp2 = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __defNormalProp2 = /* @__PURE__ */ __name((obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, "__defNormalProp"), __decorateClass = /* @__PURE__ */ __name((decorators, target, key, kind) => {
606
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
607
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
608
+ return kind && result && __defProp2(target, key, result), result;
609
+ }, "__decorateClass"), __decorateParam = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam"), __publicField2 = /* @__PURE__ */ __name((obj, key, value) => __defNormalProp2(obj, typeof key != "symbol" ? key + "" : key, value), "__publicField");
610
+ const NAME = "SHEET_FIND_REPLACE_PLUGIN";
611
+ var _a4;
612
+ let UniverSheetsFindReplacePlugin = (_a4 = class extends Plugin {
613
+ constructor(_config, _injector) {
614
+ super(), this._injector = _injector;
615
+ }
616
+ onStarting() {
617
+ [[SheetsFindReplaceController]].forEach((d) => this._injector.add(d));
618
+ }
619
+ }, __name(_a4, "UniverSheetsFindReplacePlugin"), _a4);
620
+ __publicField2(UniverSheetsFindReplacePlugin, "pluginName", NAME);
621
+ __publicField2(UniverSheetsFindReplacePlugin, "type", UniverInstanceType.UNIVER_SHEET);
622
+ UniverSheetsFindReplacePlugin = __decorateClass([
623
+ DependentOn(UniverSheetsPlugin, UniverSheetsPlugin, UniverFindReplacePlugin),
624
+ __decorateParam(1, Inject(Injector))
625
+ ], UniverSheetsFindReplacePlugin);
608
626
  export {
609
- te as SheetReplaceCommand,
610
- M as SheetsFindReplaceController,
611
- x as UniverSheetsFindReplacePlugin
627
+ SheetReplaceCommand,
628
+ SheetsFindReplaceController,
629
+ UniverSheetsFindReplacePlugin
612
630
  };