@univerjs/sheets-find-replace 0.5.4 → 0.5.5-experimental.20250123-34738ff

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