index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 = {
  11. id: "sheet.command.replace",
  12. type: B.COMMAND,
  13. handler: async (h, e) => {
  14. const t = h.get(_e), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(te.id, {
  15. unitId: n,
  16. subUnitId: c.subUnitId,
  17. value: c.value
  18. })));
  19. return s.dispose(), Ke(o, r);
  20. }
  21. };
  22. function Ke(h, e) {
  23. let t = 0, i = 0;
  24. return h.forEach((n, r) => {
  25. const s = e[r].count;
  26. n ? t += s : i += s;
  27. }), { success: t, failure: i };
  28. }
  29. class Qe extends Ee {
  30. constructor(t, i) {
  31. super(t, i);
  32. 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
  41. });
  42. }
  43. _draw(t) {
  44. const i = this._activated, n = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`, r = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
  45. Oe.drawWith(t, {
  46. width: this.width,
  47. height: this.height,
  48. fill: n,
  49. stroke: i ? r : void 0,
  50. strokeWidth: i ? 2 : 0,
  51. evented: !1
  52. });
  53. }
  54. }
  55. function Y(h, e) {
  56. return h.startRow === e.startRow && h.startColumn === e.startColumn;
  57. }
  58. function K(h, e) {
  59. return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
  60. }
  61. function Q(h, e) {
  62. return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
  63. }
  64. function Xe(h, e) {
  65. return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
  66. }
  67. function ze(h, e) {
  68. return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
  69. }
  70. function Je(h, e) {
  71. const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
  72. return r ? Se.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
  73. }
  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) {
  81. 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();
  84. }
  85. dispose() {
  86. super.dispose(), this._findReplaceController.closePanel(), this._provider.dispose();
  87. }
  88. _init() {
  89. const e = this._injector.createInstance(F);
  90. this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(this._contextService.subscribeContextValue$(pe).pipe(k((t) => !!t)).subscribe(() => this._findReplaceController.closePanel()));
  91. }
  92. _initCommands() {
  93. [ne].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
  94. }
  95. };
  96. M = E([
  97. m(0, C(T)),
  98. m(1, C(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) {
  106. super();
  107. // 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());
  112. /** Hold matches by the worksheet they are in. Make it easier to track the next (or previous) match when searching in the whole workbook. */
  113. g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
  114. /** Hold all matches in the currently searching scope. */
  115. g(this, "_matches", []);
  116. /** 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);
  121. /** 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);
  125. }
  126. get _matchesCount() {
  127. return this._matches.length;
  128. }
  129. get unitId() {
  130. return this._workbook.getUnitId();
  131. }
  132. get matchesCount() {
  133. return this._matchesCount;
  134. }
  135. get matchesPosition() {
  136. return this._matchesPosition;
  137. }
  138. get currentMatch() {
  139. return this._matchesPosition > 0 ? this._matches[this._matchesPosition - 1] : null;
  140. }
  141. dispose() {
  142. super.dispose(), this._disposeHighlights(), this._toggleDisplayRawFormula(!1);
  143. }
  144. getMatches() {
  145. return this._matches;
  146. }
  147. start(e) {
  148. switch (this._query = e, e.findBy === b.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
  149. case v.UNIT:
  150. this.findInWorkbook(e);
  151. break;
  152. case v.SUBUNIT:
  153. default:
  154. this.findInActiveWorksheet(e);
  155. break;
  156. }
  157. }
  158. _toggleDisplayRawFormula(e) {
  159. this._contextService.setContextValue(Ae, e);
  160. }
  161. /**
  162. * Find all matches in the current workbook no matter which worksheet is activated.
  163. * @param query the query object
  164. * @returns the query complete event
  165. */
  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;
  173. });
  174. this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
  175. };
  176. return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
  177. this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
  178. })), 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)
  182. return;
  183. const o = s.getSheetId();
  184. this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
  185. })
  186. ), 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()
  190. ),
  191. Le(600, void 0, { leading: !1, trailing: !0 })
  192. ).subscribe(() => r())
  193. ), r(), i;
  194. }
  195. /**
  196. * This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
  197. * in the new worksheet.
  198. */
  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);
  203. }
  204. /**
  205. * Find all matches (only) in the currently activated worksheet.
  206. * @param query the query object
  207. * @returns the query complete event
  208. */
  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. };
  226. 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())
  231. 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);
  235. }
  236. return !1;
  237. })
  238. ),
  239. // 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))
  249. 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,
  257. range: {
  258. subUnitId: o,
  259. range: {
  260. startRow: a,
  261. startColumn: d,
  262. endColumn: d + (u != null ? u : 1) - 1,
  263. endRow: a + (_ != null ? _ : 1) - 1
  264. }
  265. }
  266. };
  267. s.push(y);
  268. }
  269. }
  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);
  282. }
  283. ).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
  284. }
  285. /** 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);
  289. }
  290. _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;
  296. }
  297. _updateFindHighlight() {
  298. var d;
  299. this._disposeHighlights();
  300. const e = (d = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : d.skeleton;
  301. if (!e)
  302. return;
  303. const t = this._workbook.getUnitId(), i = this._renderManagerService.getRenderById(t);
  304. if (i == null)
  305. 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)
  308. 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,
  316. evented: !1,
  317. inHiddenRange: ae,
  318. zIndex: tt
  319. };
  320. return new Qe(`find-highlight-${_}`, ue);
  321. });
  322. n.addObjects(a), this._highlightShapes = a, n.makeDirty();
  323. }
  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)
  329. return;
  330. this._currentHighlightShape = i, i.setShapeProps({ activated: !0 });
  331. }
  332. }
  333. _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 },
  342. { fromFindReplace: !0 }
  343. ), this._commandService.executeCommand(
  344. $e.id,
  345. { range: e.range.range },
  346. { fromFindReplace: !0 }
  347. );
  348. }
  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)
  353. return !1;
  354. const { startColumn: c, startRow: l } = o.range.range;
  355. return c === n && l === r;
  356. });
  357. return s > -1 ? s + 1 : 0;
  358. }
  359. moveToNextMatch(e) {
  360. var o, c, l, a, d;
  361. if (!this._matches.length)
  362. 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;
  367. }
  368. return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
  369. }
  370. moveToPreviousMatch(e) {
  371. var o, c, l, a, d;
  372. if (!this._matches.length)
  373. return null;
  374. const t = (o = e == null ? void 0 : e.loop) != null ? o : !1, i = (c = e == null ? void 0 : e.stayIfOnMatch) != null ? c : !1, n = (l = e == null ? void 0 : e.noFocus) != null ? l : !1, r = (a = e == null ? void 0 : e.ignoreSelection) != null ? a : !1, s = this._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;
  378. }
  379. return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
  380. }
  381. _findPreviousMatch(e = !1, t = !1, i = !1) {
  382. var o;
  383. 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)
  389. return null;
  390. const a = this._matches.length, d = (l + a) % a;
  391. return [this._matches[d], d];
  392. }
  393. const n = this._workbookSelections.getCurrentLastSelection();
  394. if (i || !n) {
  395. const c = this._matches.length - 1;
  396. return [this._matches[c], c];
  397. }
  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)
  402. return null;
  403. const s = this._findPreviousWorksheetThatHasAMatch(r, e);
  404. return s ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
  405. }
  406. _findNextMatch(e = !1, t = !1, i = !1) {
  407. var o;
  408. 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)
  414. return null;
  415. const d = l % a;
  416. return [this._matches[d], d];
  417. }
  418. const n = this._workbookSelections.getCurrentLastSelection();
  419. if (i || !n)
  420. 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)
  425. 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)))
  442. return !1;
  443. const a = Y(t, c);
  444. return i ? a : !a;
  445. });
  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)))
  455. return !1;
  456. const a = Y(t, c);
  457. return i ? a : !a;
  458. });
  459. r === -1 && (r = 0);
  460. const s = e[r];
  461. return [s, this._matches.findIndex((o) => o === s)];
  462. }
  463. async replace(e) {
  464. if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
  465. return !1;
  466. const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
  467. this.currentMatch,
  468. i,
  469. this._query.findBy === b.FORMULA,
  470. this._query.findString,
  471. e,
  472. this._query.caseSensitive ? "g" : "ig"
  473. ), r = {
  474. unitId: this.currentMatch.unitId,
  475. subUnitId: t.subUnitId,
  476. value: {
  477. [t.range.startRow]: {
  478. [t.range.startColumn]: n
  479. }
  480. }
  481. };
  482. return this._commandService.executeCommand(te.id, r);
  483. }
  484. async replaceAll(e) {
  485. if (this._matchesCount === 0 || !this._query)
  486. 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()
  497. });
  498. }), c ? this._commandService.executeCommand(ne.id, {
  499. unitId: t,
  500. replacements: c
  501. }) : { success: 0, failure: 0 };
  502. }
  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 };
  511. }
  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, "\\$&");
  525. }
  526. let F = class extends J {
  527. constructor(e, t, i) {
  528. super();
  529. /**
  530. * Hold all find results in this kind of univer business instances (Workbooks).
  531. */
  532. g(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
  533. this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
  534. }
  535. async find(e) {
  536. 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;
  541. });
  542. }
  543. terminate() {
  544. this._terminate();
  545. }
  546. _terminate() {
  547. this._findModelsByUnitId.forEach((e) => e.dispose()), this._findModelsByUnitId.clear();
  548. }
  549. /**
  550. * Parsed the query object before do actual searching in favor of performance.
  551. * @param query the raw query object
  552. * @returns the parsed query object
  553. */
  554. _preprocessQuery(e) {
  555. let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
  556. return t = t.trim(), {
  557. ...e,
  558. findString: t
  559. };
  560. }
  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));
  571. }
  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;
  575. }
  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;
  580. }
  581. function st(h) {
  582. return h.replace(/^ +/g, "").replace(/ +$/g, "");
  583. }
  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(
  594. {},
  595. G,
  596. this._config
  597. );
  598. this._configService.setConfig(Ye, i);
  599. }
  600. onStarting() {
  601. [[M]].forEach((h) => this._injector.add(h));
  602. }
  603. 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);
  614. export {
  615. ne as SheetReplaceCommand,
  616. M as SheetsFindReplaceController,
  617. x as UniverSheetsFindReplacePlugin
  618. };