@univerjs_sheets-find-replace.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. import {
  2. Nu,
  3. me,
  4. of,
  5. pn
  6. } from "./chunk-T4NNB5UY.js";
  7. import {
  8. Ar,
  9. F,
  10. Wn,
  11. an,
  12. on,
  13. re as re2
  14. } from "./chunk-BW43Z7N3.js";
  15. import "./chunk-QRYBFJ3R.js";
  16. import "./chunk-MIOQ566M.js";
  17. import {
  18. Di,
  19. K,
  20. Y,
  21. _e,
  22. j,
  23. m,
  24. z
  25. } from "./chunk-NND5TVYZ.js";
  26. import "./chunk-POAJ2YNP.js";
  27. import "./chunk-GBYP4PB2.js";
  28. import "./chunk-PZD7N4BC.js";
  29. import {
  30. A1,
  31. L_,
  32. Yx,
  33. q3
  34. } from "./chunk-2MK3Q52E.js";
  35. import "./chunk-BSDDCPAW.js";
  36. import "./chunk-QG2HZMYH.js";
  37. import {
  38. D0,
  39. Fe,
  40. Gf,
  41. M0,
  42. NR,
  43. Nt,
  44. OR,
  45. Ot,
  46. Pt,
  47. Ra,
  48. SE,
  49. Subject,
  50. _n,
  51. ar,
  52. cn,
  53. debounceTime,
  54. en,
  55. filter,
  56. fs,
  57. kt,
  58. merge,
  59. ml,
  60. nt,
  61. nu,
  62. oE,
  63. ot,
  64. re,
  65. skip,
  66. throttleTime
  67. } from "./chunk-23V3HWTR.js";
  68. import "./chunk-GNR2UJZM.js";
  69. import "./chunk-2LSFTFF7.js";
  70. // node_modules/.pnpm/@univerjs+sheets-find-replace@0.5.5_@grpc+grpc-js@1.13.4_react-dom@18.3.1_react@18.3.1_rxjs@7.8.1_typescript@5.4.5/node_modules/@univerjs/sheets-find-replace/lib/es/index.js
  71. var fe = Object.defineProperty;
  72. var ge = (h, e, t) => e in h ? fe(h, e, { enumerable: true, configurable: true, writable: true, value: t }) : h[e] = t;
  73. var g = (h, e, t) => ge(h, typeof e != "symbol" ? e + "" : e, t);
  74. var Ye = "sheets-find-replace.config";
  75. var G = {};
  76. var ne = {
  77. id: "sheet.command.replace",
  78. type: ar.COMMAND,
  79. handler: async (h, e) => {
  80. const t = h.get(cn), i = h.get(Pt), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(on.id, {
  81. unitId: n,
  82. subUnitId: c.subUnitId,
  83. value: c.value
  84. })));
  85. return s.dispose(), Ke(o, r);
  86. }
  87. };
  88. function Ke(h, e) {
  89. let t = 0, i = 0;
  90. return h.forEach((n, r) => {
  91. const s = e[r].count;
  92. n ? t += s : i += s;
  93. }), { success: t, failure: i };
  94. }
  95. var Qe = class extends q3 {
  96. constructor(t, i) {
  97. super(t, i);
  98. g(this, "_activated", false);
  99. g(this, "_inHiddenRange", false);
  100. g(this, "_color");
  101. i && this.setShapeProps(i);
  102. }
  103. setShapeProps(t) {
  104. this._activated = !!t.activated, typeof t.inHiddenRange < "u" && (this._inHiddenRange = t.inHiddenRange), typeof t.color < "u" && (this._color = t.color), this.transformByState({
  105. width: t.width,
  106. height: t.height
  107. });
  108. }
  109. _draw(t) {
  110. 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})`;
  111. A1.drawWith(t, {
  112. width: this.width,
  113. height: this.height,
  114. fill: n,
  115. stroke: i ? r : void 0,
  116. strokeWidth: i ? 2 : 0,
  117. evented: false
  118. });
  119. }
  120. };
  121. function Y2(h, e) {
  122. return h.startRow === e.startRow && h.startColumn === e.startColumn;
  123. }
  124. function K2(h, e) {
  125. return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
  126. }
  127. function Q(h, e) {
  128. return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
  129. }
  130. function Xe(h, e) {
  131. return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
  132. }
  133. function ze(h, e) {
  134. return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
  135. }
  136. function Je(h, e) {
  137. const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
  138. return r ? en.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
  139. }
  140. var Ze = Object.defineProperty;
  141. var qe = Object.getOwnPropertyDescriptor;
  142. var E = (h, e, t, i) => {
  143. for (var n = i > 1 ? void 0 : i ? qe(e, t) : e, r = h.length - 1, s; r >= 0; r--)
  144. (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
  145. return i && n && Ze(e, t, n), n;
  146. };
  147. var m2 = (h, e) => (t, i) => e(t, i, h);
  148. var M = class extends nt {
  149. constructor(e, t, i, n, r) {
  150. super();
  151. g(this, "_provider");
  152. this._injector = e, this._findReplaceController = t, this._contextService = i, this._findReplaceService = n, this._commandService = r, this._init(), this._initCommands();
  153. }
  154. dispose() {
  155. super.dispose(), this._findReplaceController.closePanel(), this._provider.dispose();
  156. }
  157. _init() {
  158. const e = this._injector.createInstance(F2);
  159. this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(this._contextService.subscribeContextValue$(oE).pipe(filter((t) => !!t)).subscribe(() => this._findReplaceController.closePanel()));
  160. }
  161. _initCommands() {
  162. [ne].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
  163. }
  164. };
  165. M = E([
  166. m2(0, ot(Ot)),
  167. m2(1, ot(j)),
  168. m2(2, fs),
  169. m2(3, m),
  170. m2(4, Pt)
  171. ], M);
  172. var et = "sheets-find-replace-provider";
  173. var tt = 1e4;
  174. var W = class extends Di {
  175. constructor(e, t, i, n, r, s, o, c) {
  176. super();
  177. g(this, "_matchesUpdate$", new Subject());
  178. g(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
  179. g(this, "_activelyChangingMatch$", new Subject());
  180. g(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
  181. g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
  182. g(this, "_matches", []);
  183. g(this, "_matchesPosition", 0);
  184. g(this, "_activeHighlightIndex", -1);
  185. g(this, "_highlightShapes", []);
  186. g(this, "_currentHighlightShape", null);
  187. g(this, "_query", null);
  188. g(this, "_workbookSelections");
  189. 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);
  190. }
  191. get _matchesCount() {
  192. return this._matches.length;
  193. }
  194. get unitId() {
  195. return this._workbook.getUnitId();
  196. }
  197. get matchesCount() {
  198. return this._matchesCount;
  199. }
  200. get matchesPosition() {
  201. return this._matchesPosition;
  202. }
  203. get currentMatch() {
  204. return this._matchesPosition > 0 ? this._matches[this._matchesPosition - 1] : null;
  205. }
  206. dispose() {
  207. super.dispose(), this._disposeHighlights(), this._toggleDisplayRawFormula(false);
  208. }
  209. getMatches() {
  210. return this._matches;
  211. }
  212. start(e) {
  213. switch (this._query = e, e.findBy === z.FORMULA ? this._toggleDisplayRawFormula(true) : this._toggleDisplayRawFormula(false), e.findScope) {
  214. case Y.UNIT:
  215. this.findInWorkbook(e);
  216. break;
  217. case Y.SUBUNIT:
  218. default:
  219. this.findInActiveWorksheet(e);
  220. break;
  221. }
  222. }
  223. _toggleDisplayRawFormula(e) {
  224. this._contextService.setContextValue(L_, e);
  225. }
  226. /**
  227. * Find all matches in the current workbook no matter which worksheet is activated.
  228. * @param query the query object
  229. * @returns the query complete event
  230. */
  231. findInWorkbook(e) {
  232. const t = this._workbook.getUnitId();
  233. let i, n = true;
  234. const r = () => {
  235. const s = this._workbook.getSheets().filter((o) => !o.isSheetHidden()).map((o) => {
  236. const c = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: a } = c;
  237. return a.length ? this._matchesByWorksheet.set(l, c.results) : this._matchesByWorksheet.delete(l), c;
  238. });
  239. this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = false) : this._matchesUpdate$.next(this._matches);
  240. };
  241. return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
  242. this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
  243. })), this.disposeWithMe(
  244. Gf(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([s, o]) => s.id === an.id && !(o != null && o.fromFindReplace))).subscribe(() => {
  245. const s = this._workbook.getActiveSheet();
  246. if (!s)
  247. return;
  248. const o = s.getSheetId();
  249. this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
  250. })
  251. ), this.disposeWithMe(
  252. Gf(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
  253. filter(
  254. ([s]) => s.type === ar.MUTATION && s.params.unitId === this._workbook.getUnitId()
  255. ),
  256. throttleTime(600, void 0, { leading: false, trailing: true })
  257. ).subscribe(() => r())
  258. ), r(), i;
  259. }
  260. /**
  261. * This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
  262. * in the new worksheet.
  263. */
  264. _findNextMatchOnActiveSheetChange(e) {
  265. let t, i, n = 0;
  266. const r = this._matchesByWorksheet.get(e.getSheetId()), s = this._workbookSelections.getCurrentSelections();
  267. 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);
  268. }
  269. /**
  270. * Find all matches (only) in the currently activated worksheet.
  271. * @param query the query object
  272. * @returns the query complete event
  273. */
  274. findInActiveWorksheet(e) {
  275. const t = this._workbook.getUnitId(), i = () => {
  276. var d;
  277. const c = this._workbook.getActiveSheet();
  278. if (!c)
  279. return false;
  280. const l = this._workbookSelections.getCurrentSelections();
  281. return (d = l == null ? void 0 : l.some((u) => !Je(u, c))) != null ? d : false;
  282. };
  283. let n, r = true, s = false;
  284. const o = () => {
  285. const c = this._workbook.getActiveSheet();
  286. if (!c)
  287. return { results: [] };
  288. const l = this.currentMatch;
  289. s = i();
  290. const a = this._workbookSelections.getCurrentSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
  291. return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(l, this._matches), r ? (n = d, r = false) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), d;
  292. };
  293. return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
  294. merge(
  295. Gf(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
  296. filter(([c]) => {
  297. if (c.type === ar.MUTATION && c.params.unitId === this._workbook.getUnitId())
  298. return true;
  299. if (c.id === re2.id && c.params.unitId === t) {
  300. const l = i();
  301. return l === false && s === false ? false : (s = l, true);
  302. }
  303. return false;
  304. })
  305. ),
  306. // activeSheet$ is a BehaviorSubject, so we need to skip the first
  307. this._workbook.activeSheet$.pipe(skip(1))
  308. ).pipe(debounceTime(200)).subscribe(() => o())
  309. ), o(), n;
  310. }
  311. _findInRange(e, t, i, n, r) {
  312. const s = [], o = e.getSheetId(), c = (t.findDirection === K.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
  313. for (const l of c) {
  314. const { row: a, col: d, colSpan: u, rowSpan: _, value: S } = l;
  315. if (r != null && r(a, d) || !S || e.getRowFiltered(a))
  316. continue;
  317. const { hit: p, replaceable: R, isFormula: I } = it(e, a, d, t, S);
  318. if (p) {
  319. const y = {
  320. provider: et,
  321. unitId: n,
  322. replaceable: R,
  323. isFormula: I,
  324. range: {
  325. subUnitId: o,
  326. range: {
  327. startRow: a,
  328. startColumn: d,
  329. endColumn: d + (u != null ? u : 1) - 1,
  330. endRow: a + (_ != null ? _ : 1) - 1
  331. }
  332. }
  333. };
  334. s.push(y);
  335. }
  336. }
  337. return { results: s };
  338. }
  339. _findInSelections(e, t, i, n) {
  340. const { findDirection: r } = i, s = r === K.ROW ? K2 : Q, o = /* @__PURE__ */ new Set();
  341. return { results: t.map((l) => this._findInRange(
  342. e,
  343. i,
  344. l.range,
  345. n,
  346. (a, d) => {
  347. const u = `${a}-${d}`;
  348. return o.has(u) ? true : (o.add(u), false);
  349. }
  350. ).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
  351. }
  352. /** Find matches in a given worksheet. */
  353. _findInWorksheet(e, t, i) {
  354. const n = e.getRowCount(), r = e.getColumnCount(), s = { startRow: 0, startColumn: 0, endRow: n - 1, endColumn: r - 1 };
  355. return this._findInRange(e, t, s, i);
  356. }
  357. _disposeHighlights() {
  358. var e;
  359. this._highlightShapes.forEach((t) => {
  360. var i;
  361. (i = t.getScene()) == null || i.makeDirty(), t.dispose();
  362. }), this._highlightShapes = [], (e = this._currentHighlightShape) == null || e.dispose(), this._currentHighlightShape = null;
  363. }
  364. _updateFindHighlight() {
  365. var d;
  366. this._disposeHighlights();
  367. const e = (d = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : d.skeleton;
  368. if (!e)
  369. return;
  370. const t = this._workbook.getUnitId(), i = this._renderManagerService.getRenderById(t);
  371. if (i == null)
  372. return;
  373. const { scene: n } = i, r = this._matches, s = this._themeService.getCurrentTheme().gold400, o = new Nt(s).toRgb(), c = this._workbook.getActiveSheet();
  374. if (!c)
  375. return;
  376. const l = c.getSheetId(), a = r.filter((u) => u.range.subUnitId === l).map((u, _) => {
  377. const { startColumn: S, startRow: p, endColumn: R, endRow: I } = u.range.range, y = Nu(p, S, n, e), oe = Nu(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 = {
  378. left: O,
  379. top: A,
  380. color: o,
  381. width: le,
  382. height: de,
  383. evented: false,
  384. inHiddenRange: ae,
  385. zIndex: tt
  386. };
  387. return new Qe(`find-highlight-${_}`, ue);
  388. });
  389. n.addObjects(a), this._highlightShapes = a, n.makeDirty();
  390. }
  391. _updateCurrentHighlightShape(e) {
  392. var t;
  393. if ((t = this._currentHighlightShape) == null || t.setShapeProps({ activated: false }), this._currentHighlightShape = null, e !== void 0) {
  394. const i = this._highlightShapes[e];
  395. if (!i)
  396. return;
  397. this._currentHighlightShape = i, i.setShapeProps({ activated: true });
  398. }
  399. }
  400. _getSheetObject() {
  401. return pn(this._univerInstanceService, this._renderManagerService);
  402. }
  403. _focusMatch(e) {
  404. var i;
  405. const t = e.range.subUnitId;
  406. t !== ((i = this._workbook.getActiveSheet()) == null ? void 0 : i.getSheetId()) && this._commandService.executeCommand(
  407. Ar.id,
  408. { unitId: this._workbook.getUnitId(), subUnitId: t },
  409. { fromFindReplace: true }
  410. ), this._commandService.executeCommand(
  411. of.id,
  412. { range: e.range.range },
  413. { fromFindReplace: true }
  414. );
  415. }
  416. _tryRestoreLastMatchesPosition(e, t) {
  417. if (!e)
  418. return 0;
  419. const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
  420. if (i !== o.range.subUnitId)
  421. return false;
  422. const { startColumn: c, startRow: l } = o.range.range;
  423. return c === n && l === r;
  424. });
  425. return s > -1 ? s + 1 : 0;
  426. }
  427. moveToNextMatch(e) {
  428. var o, c, l, a, d;
  429. if (!this._matches.length)
  430. return null;
  431. const t = (o = e == null ? void 0 : e.loop) != null ? o : false, i = (c = e == null ? void 0 : e.stayIfOnMatch) != null ? c : false, n = (l = e == null ? void 0 : e.noFocus) != null ? l : false, r = (a = e == null ? void 0 : e.ignoreSelection) != null ? a : false, s = this._findNextMatch(t, i, r);
  432. if (s) {
  433. const [u, _] = s;
  434. return this._matchesPosition = _ + 1, this._query.findScope === Y.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;
  435. }
  436. return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
  437. }
  438. moveToPreviousMatch(e) {
  439. var o, c, l, a, d;
  440. if (!this._matches.length)
  441. return null;
  442. const t = (o = e == null ? void 0 : e.loop) != null ? o : false, i = (c = e == null ? void 0 : e.stayIfOnMatch) != null ? c : false, n = (l = e == null ? void 0 : e.noFocus) != null ? l : false, r = (a = e == null ? void 0 : e.ignoreSelection) != null ? a : false, s = this._findPreviousMatch(t, i, r);
  443. if (s) {
  444. const [u, _] = s;
  445. return this._matchesPosition = _ + 1, this._query.findScope === Y.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;
  446. }
  447. return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
  448. }
  449. _findPreviousMatch(e = false, t = false, i = false) {
  450. var o;
  451. if (this.currentMatch) {
  452. const c = this._matches.findIndex((u) => u === this.currentMatch);
  453. if (t)
  454. return [this.currentMatch, c];
  455. const l = c - 1;
  456. if (!e && l < 0)
  457. return null;
  458. const a = this._matches.length, d = (l + a) % a;
  459. return [this._matches[d], d];
  460. }
  461. const n = this._workbookSelections.getCurrentLastSelection();
  462. if (i || !n) {
  463. const c = this._matches.length - 1;
  464. return [this._matches[c], c];
  465. }
  466. if (this._query.findScope !== Y.UNIT)
  467. return this._findPreviousMatchByRange(this._matches, n.range);
  468. const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
  469. if (!r)
  470. return null;
  471. const s = this._findPreviousWorksheetThatHasAMatch(r, e);
  472. return s ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
  473. }
  474. _findNextMatch(e = false, t = false, i = false) {
  475. var o;
  476. if (this.currentMatch) {
  477. const c = this._matches.findIndex((u) => u === this.currentMatch);
  478. if (t)
  479. return [this.currentMatch, c];
  480. const l = c + 1, a = this._matches.length;
  481. if (!e && l >= a)
  482. return null;
  483. const d = l % a;
  484. return [this._matches[d], d];
  485. }
  486. const n = this._workbookSelections.getCurrentLastSelection();
  487. if (i || !n)
  488. return [this._matches[0], 0];
  489. if (this._query.findScope !== Y.UNIT)
  490. return this._findNextMatchByRange(this._matches, n.range, t);
  491. const r = (o = this._workbook.getActiveSheet()) == null ? void 0 : o.getSheetId();
  492. if (!r)
  493. return null;
  494. const s = this._findNextWorksheetThatHasAMatch(r, e);
  495. return s ? this._findNextMatchByRange(this._matchesByWorksheet.get(s), n.range) : null;
  496. }
  497. _findPreviousWorksheetThatHasAMatch(e, t = false) {
  498. const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? M0(i, n + 1) : i.slice(0, n + 1)).findLast((o) => this._matchesByWorksheet.has(o));
  499. return s != null ? s : null;
  500. }
  501. _findNextWorksheetThatHasAMatch(e, t = false) {
  502. const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? M0(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
  503. return s != null ? s : null;
  504. }
  505. _findNextMatchByRange(e, t, i = false) {
  506. const n = this._query.findDirection === K.ROW;
  507. let r = e.findIndex((o) => {
  508. const c = o.range.range;
  509. if (!(n ? K2(t, c) : Q(t, c)))
  510. return false;
  511. const a = Y2(t, c);
  512. return i ? a : !a;
  513. });
  514. r === -1 && (r = e.length - 1);
  515. const s = e[r];
  516. return [s, this._matches.findIndex((o) => o === s)];
  517. }
  518. _findPreviousMatchByRange(e, t, i = false) {
  519. const n = this._query.findDirection === K.ROW;
  520. let r = this._matches.findLastIndex((o) => {
  521. const c = o.range.range;
  522. if (!(n ? Xe(t, c) : ze(t, c)))
  523. return false;
  524. const a = Y2(t, c);
  525. return i ? a : !a;
  526. });
  527. r === -1 && (r = 0);
  528. const s = e[r];
  529. return [s, this._matches.findIndex((o) => o === s)];
  530. }
  531. async replace(e) {
  532. if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
  533. return false;
  534. const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
  535. this.currentMatch,
  536. i,
  537. this._query.findBy === z.FORMULA,
  538. this._query.findString,
  539. e,
  540. this._query.caseSensitive ? "g" : "ig"
  541. ), r = {
  542. unitId: this.currentMatch.unitId,
  543. subUnitId: t.subUnitId,
  544. value: {
  545. [t.range.startRow]: {
  546. [t.range.startColumn]: n
  547. }
  548. }
  549. };
  550. return this._commandService.executeCommand(on.id, r);
  551. }
  552. async replaceAll(e) {
  553. if (this._matchesCount === 0 || !this._query)
  554. return { success: 0, failure: 0 };
  555. const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === z.FORMULA, o = n ? "g" : "ig", c = [];
  556. return D0(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
  557. const u = new kt(), _ = this._workbook.getSheetBySheetId(d);
  558. a.forEach((S) => {
  559. const { startColumn: p, startRow: R } = S.range.range, I = this._getReplacedCellData(S, _, s, i, e, o);
  560. I && u.setValue(R, p, I);
  561. }), c.push({
  562. count: a.length,
  563. subUnitId: d,
  564. value: u.getMatrix()
  565. });
  566. }), c ? this._commandService.executeCommand(ne.id, {
  567. unitId: t,
  568. replacements: c
  569. }) : { success: 0, failure: 0 };
  570. }
  571. _getReplacedCellData(e, t, i, n, r, s) {
  572. var _;
  573. const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
  574. if (e.isFormula)
  575. return i ? { f: a.f.replace(new RegExp(X(n), s), r), v: null } : null;
  576. if (!!((_ = a.p) != null && _.body)) {
  577. const S = re.deepClone(a.p);
  578. return NR(S.body, n, r, this._query.caseSensitive), { p: S };
  579. }
  580. return { v: a.v.toString().replace(new RegExp(X(n), s), r) };
  581. }
  582. };
  583. W = E([
  584. m2(2, _n),
  585. m2(3, Yx),
  586. m2(4, Pt),
  587. m2(5, fs),
  588. m2(6, ot(ml)),
  589. m2(7, ot(F))
  590. ], W);
  591. function X(h) {
  592. return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  593. }
  594. var F2 = class extends nt {
  595. constructor(e, t, i) {
  596. super();
  597. g(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
  598. this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
  599. }
  600. async find(e) {
  601. this._terminate();
  602. const t = this._univerInstanceService.getAllUnitsForType(Fe.UNIVER_SHEET), i = this._preprocessQuery(e);
  603. return t.map((r) => {
  604. const s = this._renderManagerService.getRenderById(r.getUnitId()).with(me), o = this._injector.createInstance(W, r, s);
  605. return this._findModelsByUnitId.set(r.getUnitId(), o), o.start(i), o;
  606. });
  607. }
  608. terminate() {
  609. this._terminate();
  610. }
  611. _terminate() {
  612. this._findModelsByUnitId.forEach((e) => e.dispose()), this._findModelsByUnitId.clear();
  613. }
  614. /**
  615. * Parsed the query object before do actual searching in favor of performance.
  616. * @param query the raw query object
  617. * @returns the parsed query object
  618. */
  619. _preprocessQuery(e) {
  620. let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
  621. return t = t.trim(), {
  622. ...e,
  623. findString: t
  624. };
  625. }
  626. };
  627. F2 = E([
  628. m2(0, _n),
  629. m2(1, Yx),
  630. m2(2, ot(Ot))
  631. ], F2);
  632. var f = { hit: false, replaceable: false, isFormula: false, rawData: null };
  633. function it(h, e, t, i, n) {
  634. const { findBy: r } = i, s = r === z.FORMULA, o = h.getCellRaw(e, t);
  635. return f.rawData = o, !(o != null && o.f) ? (f.isFormula = false, U(n, i) ? o ? (f.hit = true, f.replaceable = true) : (f.hit = true, f.replaceable = false) : (f.hit = false, f.replaceable = false), f) : (f.isFormula = true, s ? U({ v: o.f }, i) ? (f.hit = true, f.replaceable = true, f) : (f.hit = false, f.replaceable = false, f) : (f.replaceable = false, U(n, i) ? f.hit = true : f.hit = false, f));
  636. }
  637. function U(h, e) {
  638. let t = nt2(h);
  639. 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 : false;
  640. }
  641. function nt2(h) {
  642. var t, i, n;
  643. 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;
  644. return typeof e == "number" ? `${e}` : typeof e == "boolean" ? e ? "1" : "0" : e;
  645. }
  646. function st(h) {
  647. return h.replace(/^ +/g, "").replace(/ +$/g, "");
  648. }
  649. var se = Object.defineProperty;
  650. var rt = Object.getOwnPropertyDescriptor;
  651. var ot2 = (h, e, t) => e in h ? se(h, e, { enumerable: true, configurable: true, writable: true, value: t }) : h[e] = t;
  652. var ht = (h, e, t, i) => {
  653. for (var n = i > 1 ? void 0 : i ? rt(e, t) : e, r = h.length - 1, s; r >= 0; r--)
  654. (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
  655. return i && n && se(e, t, n), n;
  656. };
  657. var z2 = (h, e) => (t, i) => e(t, i, h);
  658. var re3 = (h, e, t) => ot2(h, typeof e != "symbol" ? e + "" : e, t);
  659. var ct = "SHEET_FIND_REPLACE_PLUGIN";
  660. var x = class extends Ra {
  661. constructor(h = G, e, t) {
  662. super(), this._config = h, this._injector = e, this._configService = t;
  663. const { ...i } = nu(
  664. {},
  665. G,
  666. this._config
  667. );
  668. this._configService.setConfig(Ye, i);
  669. }
  670. onStarting() {
  671. [[M]].forEach((h) => this._injector.add(h));
  672. }
  673. onSteady() {
  674. this._injector.get(M);
  675. }
  676. };
  677. re3(x, "pluginName", ct);
  678. re3(x, "type", Fe.UNIVER_SHEET);
  679. x = ht([
  680. OR(Wn, Wn, _e),
  681. z2(1, ot(Ot)),
  682. z2(2, SE)
  683. ], x);
  684. export {
  685. ne as SheetReplaceCommand,
  686. M as SheetsFindReplaceController,
  687. x as UniverSheetsFindReplacePlugin
  688. };
  689. //# sourceMappingURL=@univerjs_sheets-find-replace.js.map