facade.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. import { FUniver as ue, ICommandService as I, IUniverInstanceService as L, CanceledError as G, RichTextValue as q, DOCS_NORMAL_EDITOR_UNIT_ID_KEY as A, LifecycleService as Q, DisposableCollection as W, LifecycleStages as ee, UniverInstanceType as fe, ILogService as D, toDisposable as C, awaitTime as Ee, InterceptorEffectEnum as we, FEventName as ve, generateRandomId as _e } from "@univerjs/core";
  2. import { RichTextEditingMutation as ke } from "@univerjs/docs";
  3. import { IRenderManagerService as m, DeviceInputEventType as te, SHEET_VIEWPORT_KEY as Ie, sheetContentViewportKeys as Pe } from "@univerjs/engine-render";
  4. import { SheetsSelectionsService as Me, COMMAND_LISTENER_SKELETON_CHANGE as Re, getSkeletonChangedEffectedRange as ye, SheetInterceptorService as Ue, INTERCEPTOR_POINT as He, InterceptCellContentPriority as xe } from "@univerjs/sheets";
  5. import { SetCellEditVisibleOperation as P, IEditorBridgeService as $, SetZoomRatioCommand as re, HoverManagerService as _, DragManagerService as U, SheetScrollManagerService as T, SheetPasteShortKeyCommand as ne, ISheetClipboardService as Se, SHEET_VIEW_KEY as R, ISheetSelectionRenderService as H, SheetSkeletonManagerService as x, ChangeZoomRatioCommand as De, SheetsScrollRenderController as Te, SetWorksheetColAutoWidthCommand as Be, SheetCanvasPopManagerService as ie, CellAlertManagerService as Le, IMarkSelectionService as Ae } from "@univerjs/sheets-ui";
  6. import { FSheetHooks as j, FWorkbook as Ce, FWorksheet as be, FPermission as pe, FRange as me } from "@univerjs/sheets/facade";
  7. import { KeyCode as B, CutCommand as oe, CopyCommand as se, PasteCommand as ae, IClipboardInterfaceService as $e, PLAIN_TEXT_CLIPBOARD_MIME_TYPE as ce, HTML_CLIPBOARD_MIME_TYPE as de, supportClipboardAPI as he, ISidebarService as We, IDialogService as je, ComponentManager as le } from "@univerjs/ui";
  8. import { filter as v, combineLatest as Oe } from "rxjs";
  9. class Fe extends ue {
  10. // eslint-disable-next-line max-lines-per-function
  11. _initSheetUIEvent(e) {
  12. const r = e.get(I);
  13. this.disposeWithMe(r.beforeCommandExecuted((t) => {
  14. if (t.id === P.id) {
  15. if (!this._eventListend(this.Event.BeforeSheetEditStart) && !this._eventListend(this.Event.BeforeSheetEditEnd))
  16. return;
  17. const i = this.getCommandSheetTarget(t);
  18. if (!i)
  19. return;
  20. const { workbook: o, worksheet: s } = i, c = e.get($), g = e.get(L), b = t.params, { visible: l, keycode: h, eventType: S } = b, d = c.getEditLocation();
  21. if (l) {
  22. const f = {
  23. row: d.row,
  24. column: d.column,
  25. eventType: S,
  26. keycode: h,
  27. workbook: o,
  28. worksheet: s,
  29. isZenEditor: !1
  30. };
  31. if (this.fireEvent(this.Event.BeforeSheetEditStart, f), f.cancel)
  32. throw new G();
  33. } else {
  34. const f = {
  35. row: d.row,
  36. column: d.column,
  37. eventType: S,
  38. keycode: h,
  39. workbook: o,
  40. worksheet: s,
  41. isZenEditor: !1,
  42. value: q.create(g.getUnit(A).getSnapshot()),
  43. isConfirm: h !== B.ESC
  44. };
  45. if (this.fireEvent(this.Event.BeforeSheetEditEnd, f), f.cancel)
  46. throw new G();
  47. }
  48. }
  49. if (t.id === re.id) {
  50. if (!this._eventListend(this.Event.BeforeSheetZoomChange))
  51. return;
  52. const i = this.getCommandSheetTarget(t);
  53. if (!i)
  54. return;
  55. const { workbook: o, worksheet: s } = i;
  56. this.fireEvent(this.Event.BeforeSheetZoomChange, {
  57. zoom: t.params.zoomRatio,
  58. workbook: o,
  59. worksheet: s
  60. });
  61. }
  62. })), this.disposeWithMe(r.onCommandExecuted((t) => {
  63. if (t.id === P.id) {
  64. if (!this._eventListend(this.Event.SheetEditStarted) && !this._eventListend(this.Event.SheetEditEnded))
  65. return;
  66. const i = this.getCommandSheetTarget(t);
  67. if (!i)
  68. return;
  69. const { workbook: o, worksheet: s } = i, c = e.get($), g = t.params, { visible: b, keycode: l, eventType: h } = g, S = c.getEditLocation();
  70. if (b) {
  71. const d = {
  72. row: S.row,
  73. column: S.column,
  74. eventType: h,
  75. keycode: l,
  76. workbook: o,
  77. worksheet: s,
  78. isZenEditor: !1
  79. };
  80. this.fireEvent(this.Event.SheetEditStarted, d);
  81. } else {
  82. const d = {
  83. row: S.row,
  84. column: S.column,
  85. eventType: h,
  86. keycode: l,
  87. workbook: o,
  88. worksheet: s,
  89. isZenEditor: !1,
  90. isConfirm: l !== B.ESC
  91. };
  92. this.fireEvent(this.Event.SheetEditEnded, d);
  93. }
  94. }
  95. if (t.id === ke.id) {
  96. if (!this._eventListend(this.Event.SheetEditChanging))
  97. return;
  98. const i = this.getCommandSheetTarget(t);
  99. if (!i)
  100. return;
  101. const { workbook: o, worksheet: s } = i, c = e.get($), g = e.get(L), b = t.params;
  102. if (!c.isVisible().visible) return;
  103. const { unitId: l } = b;
  104. if (l === A) {
  105. const { row: h, column: S } = c.getEditLocation(), d = {
  106. workbook: o,
  107. worksheet: s,
  108. row: h,
  109. column: S,
  110. value: q.create(g.getUnit(A).getSnapshot()),
  111. isZenEditor: !1
  112. };
  113. this.fireEvent(this.Event.SheetEditChanging, d);
  114. }
  115. }
  116. if (t.id === re.id) {
  117. if (!this._eventListend(this.Event.SheetZoomChanged))
  118. return;
  119. const i = this.getCommandSheetTarget(t);
  120. if (!i)
  121. return;
  122. const { workbook: o, worksheet: s } = i;
  123. this.fireEvent(this.Event.SheetZoomChanged, {
  124. zoom: s.getZoom(),
  125. workbook: o,
  126. worksheet: s
  127. });
  128. }
  129. })), this._initObserverListener(e);
  130. }
  131. // eslint-disable-next-line max-lines-per-function
  132. _initObserverListener(e) {
  133. const t = e.get(L).getFocusedUnit(), i = t == null ? void 0 : t.getUnitId(), o = e.get(m);
  134. if (i) {
  135. const l = e.get(Q), h = new W();
  136. this.disposeWithMe(l.lifecycle$.subscribe((S) => {
  137. var k, M, u, p, E, O, F, N, V, Z, K, z, Y, X, J;
  138. if (S < ee.Rendered) return;
  139. h.dispose();
  140. const d = e.get(_), f = e.get(U);
  141. d && (h.add(
  142. (k = d.currentClickedCell$) == null ? void 0 : k.pipe(v((n) => !!n)).subscribe((n) => {
  143. if (!this._eventListend(this.Event.CellClicked)) return;
  144. const a = this.getSheetTarget(n.location.unitId, n.location.subUnitId);
  145. a && this.fireEvent(this.Event.CellClicked, {
  146. ...a,
  147. ...n,
  148. row: n.location.row,
  149. column: n.location.col
  150. });
  151. })
  152. ), h.add(
  153. (M = d.currentRichText$) == null ? void 0 : M.pipe(v((n) => !!n)).subscribe((n) => {
  154. if (!this._eventListend(this.Event.CellHover)) return;
  155. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  156. a && this.fireEvent(this.Event.CellHover, {
  157. ...a,
  158. ...n,
  159. row: n.row,
  160. column: n.col
  161. });
  162. })
  163. ), h.add(
  164. (u = d.currentPointerDownCell$) == null ? void 0 : u.pipe(v((n) => !!n)).subscribe((n) => {
  165. if (!this._eventListend(this.Event.CellPointerDown)) return;
  166. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  167. a && this.fireEvent(this.Event.CellPointerDown, {
  168. ...a,
  169. ...n,
  170. row: n.row,
  171. column: n.col
  172. });
  173. })
  174. ), h.add(
  175. (p = d.currentPointerUpCell$) == null ? void 0 : p.pipe(v((n) => !!n)).subscribe((n) => {
  176. if (!this._eventListend(this.Event.CellPointerUp)) return;
  177. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  178. a && this.fireEvent(this.Event.CellPointerUp, {
  179. ...a,
  180. ...n,
  181. row: n.row,
  182. column: n.col
  183. });
  184. })
  185. ), h.add(
  186. (E = d.currentCellPosWithEvent$) == null ? void 0 : E.pipe(v((n) => !!n)).subscribe((n) => {
  187. if (!this._eventListend(this.Event.CellPointerMove)) return;
  188. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  189. a && this.fireEvent(this.Event.CellPointerMove, {
  190. ...a,
  191. ...n,
  192. row: n.row,
  193. column: n.col
  194. });
  195. })
  196. ), h.add(
  197. (O = f.currentCell$) == null ? void 0 : O.pipe(v((n) => !!n)).subscribe((n) => {
  198. if (!this._eventListend(this.Event.DragOver)) return;
  199. const a = this.getSheetTarget(n.location.unitId, n.location.subUnitId);
  200. a && this.fireEvent(this.Event.DragOver, {
  201. ...a,
  202. ...n,
  203. row: n.location.row,
  204. column: n.location.col
  205. });
  206. })
  207. ), h.add(
  208. (F = f.endCell$) == null ? void 0 : F.pipe(v((n) => !!n)).subscribe((n) => {
  209. if (!this._eventListend(this.Event.Drop)) return;
  210. const a = this.getSheetTarget(n.location.unitId, n.location.subUnitId);
  211. a && this.fireEvent(this.Event.Drop, {
  212. ...a,
  213. ...n,
  214. row: n.location.row,
  215. column: n.location.col
  216. });
  217. })
  218. ), h.add(
  219. (N = d.currentRowHeaderClick$) == null ? void 0 : N.pipe(v((n) => !!n)).subscribe((n) => {
  220. if (!this._eventListend(this.Event.RowHeaderClick)) return;
  221. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  222. a && this.fireEvent(this.Event.RowHeaderClick, {
  223. ...a,
  224. row: n.index
  225. });
  226. })
  227. ), h.add(
  228. (V = d.currentRowHeaderPointerDown$) == null ? void 0 : V.pipe(v((n) => !!n)).subscribe((n) => {
  229. if (!this._eventListend(this.Event.RowHeaderPointerDown)) return;
  230. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  231. a && this.fireEvent(this.Event.RowHeaderPointerDown, {
  232. ...a,
  233. row: n.index
  234. });
  235. })
  236. ), h.add(
  237. (Z = d.currentRowHeaderPointerUp$) == null ? void 0 : Z.pipe(v((n) => !!n)).subscribe((n) => {
  238. if (!this._eventListend(this.Event.RowHeaderPointerUp)) return;
  239. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  240. a && this.fireEvent(this.Event.RowHeaderPointerUp, {
  241. ...a,
  242. row: n.index
  243. });
  244. })
  245. ), h.add(
  246. (K = d.currentHoveredRowHeader$) == null ? void 0 : K.pipe(v((n) => !!n)).subscribe((n) => {
  247. if (!this._eventListend(this.Event.RowHeaderHover)) return;
  248. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  249. a && this.fireEvent(this.Event.RowHeaderHover, {
  250. ...a,
  251. row: n.index
  252. });
  253. })
  254. ), h.add(
  255. (z = d.currentColHeaderClick$) == null ? void 0 : z.pipe(v((n) => !!n)).subscribe((n) => {
  256. if (!this._eventListend(this.Event.ColumnHeaderClick)) return;
  257. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  258. a && this.fireEvent(this.Event.ColumnHeaderClick, {
  259. ...a,
  260. column: n.index
  261. });
  262. })
  263. ), h.add(
  264. (Y = d.currentColHeaderPointerDown$) == null ? void 0 : Y.pipe(v((n) => !!n)).subscribe((n) => {
  265. if (!this._eventListend(this.Event.ColumnHeaderPointerDown)) return;
  266. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  267. a && this.fireEvent(this.Event.ColumnHeaderPointerDown, {
  268. ...a,
  269. column: n.index
  270. });
  271. })
  272. ), h.add(
  273. (X = d.currentColHeaderPointerUp$) == null ? void 0 : X.pipe(v((n) => !!n)).subscribe((n) => {
  274. if (!this._eventListend(this.Event.ColumnHeaderPointerUp)) return;
  275. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  276. a && this.fireEvent(this.Event.ColumnHeaderPointerUp, {
  277. ...a,
  278. column: n.index
  279. });
  280. })
  281. ), h.add(
  282. (J = d.currentHoveredColHeader$) == null ? void 0 : J.pipe(v((n) => !!n)).subscribe((n) => {
  283. if (!this._eventListend(this.Event.ColumnHeaderHover)) return;
  284. const a = this.getSheetTarget(n.unitId, n.subUnitId);
  285. a && this.fireEvent(this.Event.ColumnHeaderHover, {
  286. ...a,
  287. column: n.index
  288. });
  289. })
  290. ));
  291. })), this.disposeWithMe(h);
  292. }
  293. const s = /* @__PURE__ */ new Map();
  294. let c;
  295. const g = e.get(Q), b = Oe([
  296. o.created$,
  297. g.lifecycle$
  298. ]);
  299. this.disposeWithMe(b.subscribe(([l, h]) => {
  300. var M;
  301. if (l.type === fe.UNIVER_SHEET && (c = l), h <= ee.Rendered) return;
  302. const S = new W();
  303. if (!c) return;
  304. const d = this.getWorkbook(c.unitId);
  305. if (!d) return;
  306. s.get(c.unitId) && ((M = s.get(c.unitId)) == null || M.dispose()), s.set(c.unitId, S);
  307. const f = c.with(T);
  308. S.add(f.validViewportScrollInfo$.subscribe((u) => {
  309. u && this._eventListend(this.Event.Scroll) && this.fireEvent(this.Event.Scroll, {
  310. workbook: d,
  311. worksheet: d.getActiveSheet(),
  312. ...u
  313. });
  314. }));
  315. const k = c.with(Me);
  316. S.add(k.selectionMoveStart$.subscribe((u) => {
  317. var p;
  318. this._eventListend(this.Event.SelectionMoveStart) && this.fireEvent(this.Event.SelectionMoveStart, {
  319. workbook: d,
  320. worksheet: d.getActiveSheet(),
  321. selections: (p = u == null ? void 0 : u.map((E) => E.range)) != null ? p : []
  322. });
  323. })), S.add(k.selectionMoving$.subscribe((u) => {
  324. var p;
  325. this._eventListend(this.Event.SelectionMoving) && this.fireEvent(this.Event.SelectionMoving, {
  326. workbook: d,
  327. worksheet: d.getActiveSheet(),
  328. selections: (p = u == null ? void 0 : u.map((E) => E.range)) != null ? p : []
  329. });
  330. })), S.add(k.selectionMoveEnd$.subscribe((u) => {
  331. var p;
  332. this._eventListend(this.Event.SelectionMoveEnd) && this.fireEvent(this.Event.SelectionMoveEnd, {
  333. workbook: d,
  334. worksheet: d.getActiveSheet(),
  335. selections: (p = u == null ? void 0 : u.map((E) => E.range)) != null ? p : []
  336. });
  337. })), S.add(k.selectionChanged$.subscribe((u) => {
  338. var p;
  339. this._eventListend(this.Event.SelectionChanged) && this.fireEvent(this.Event.SelectionChanged, {
  340. workbook: d,
  341. worksheet: d.getActiveSheet(),
  342. selections: (p = u == null ? void 0 : u.map((E) => E.range)) != null ? p : []
  343. });
  344. })), c = null, this.disposeWithMe(S);
  345. })), this.disposeWithMe(o.disposed$.subscribe((l) => {
  346. var h;
  347. (h = s.get(l)) == null || h.dispose(), s.delete(l);
  348. })), this.disposeWithMe(() => {
  349. s.forEach((l) => {
  350. l.dispose();
  351. });
  352. });
  353. }
  354. _initialize(e) {
  355. this._initSheetUIEvent(e);
  356. const r = e.get(I);
  357. this.disposeWithMe(r.beforeCommandExecuted((t) => {
  358. switch (t.id) {
  359. case se.id:
  360. case oe.id:
  361. this._beforeClipboardChange();
  362. break;
  363. case ne.id:
  364. this._beforeClipboardPaste(t.params);
  365. break;
  366. }
  367. })), this.disposeWithMe(r.onCommandExecuted((t) => {
  368. if (Re.indexOf(t.id) > -1) {
  369. if (!this._eventListend(this.Event.SheetSkeletonChanged)) return;
  370. const i = this.getActiveSheet();
  371. if (!i) return;
  372. const o = ye(t).map((s) => {
  373. var c, g;
  374. return (g = (c = this.getWorkbook(s.unitId)) == null ? void 0 : c.getSheetBySheetId(s.subUnitId)) == null ? void 0 : g.getRange(s.range);
  375. }).filter(Boolean);
  376. if (!o.length) return;
  377. this.fireEvent(this.Event.SheetSkeletonChanged, {
  378. workbook: i.workbook,
  379. worksheet: i.worksheet,
  380. payload: t,
  381. skeleton: i.worksheet.getSkeleton(),
  382. effectedRanges: o
  383. });
  384. return;
  385. }
  386. switch (t.id) {
  387. case se.id:
  388. case oe.id:
  389. this._clipboardChanged();
  390. break;
  391. case ne.id:
  392. this._clipboardPaste();
  393. break;
  394. case ae.id:
  395. this._clipboardPasteAsync();
  396. break;
  397. }
  398. })), this.disposeWithMe(r.beforeCommandExecuted(async (t) => {
  399. switch (t.id) {
  400. case ae.id:
  401. await this._beforeClipboardPasteAsync();
  402. break;
  403. }
  404. }));
  405. }
  406. _generateClipboardCopyParam() {
  407. const e = this.getActiveWorkbook(), r = e == null ? void 0 : e.getActiveSheet(), t = e == null ? void 0 : e.getActiveRange();
  408. if (!e || !r || !t)
  409. return;
  410. const o = this._injector.get(Se).generateCopyContent(e.getId(), r.getSheetId(), t.getRange());
  411. if (!o)
  412. return;
  413. const { html: s, plain: c } = o;
  414. return {
  415. workbook: e,
  416. worksheet: r,
  417. text: c,
  418. html: s,
  419. fromSheet: r,
  420. fromRange: t
  421. };
  422. }
  423. _beforeClipboardChange() {
  424. if (!this.hasEventCallback(this.Event.BeforeClipboardChange))
  425. return;
  426. const e = this._generateClipboardCopyParam();
  427. if (e && (this.fireEvent(this.Event.BeforeClipboardChange, e), e.cancel))
  428. throw new Error("Before clipboard change is canceled");
  429. }
  430. _clipboardChanged() {
  431. if (!this.hasEventCallback(this.Event.ClipboardChanged))
  432. return;
  433. const e = this._generateClipboardCopyParam();
  434. if (e && (this.fireEvent(this.Event.ClipboardChanged, e), e.cancel))
  435. throw new Error("Clipboard changed is canceled");
  436. }
  437. _generateClipboardPasteParam(e) {
  438. if (!e)
  439. return;
  440. const { htmlContent: r, textContent: t } = e, i = this.getActiveWorkbook(), o = i == null ? void 0 : i.getActiveSheet();
  441. return !i || !o ? void 0 : {
  442. workbook: i,
  443. worksheet: o,
  444. text: t,
  445. html: r
  446. };
  447. }
  448. async _generateClipboardPasteParamAsync() {
  449. const e = this.getActiveWorkbook(), r = e == null ? void 0 : e.getActiveSheet();
  450. if (!e || !r)
  451. return;
  452. const o = (await this._injector.get($e).read())[0];
  453. let s;
  454. if (o) {
  455. const c = o.types, g = c.indexOf(ce) !== -1 ? await o.getType(ce).then((l) => l && l.text()) : "", b = c.indexOf(de) !== -1 ? await o.getType(de).then((l) => l && l.text()) : "";
  456. s = {
  457. workbook: e,
  458. worksheet: r,
  459. text: g,
  460. html: b
  461. };
  462. }
  463. return s;
  464. }
  465. _beforeClipboardPaste(e) {
  466. if (!this.hasEventCallback(this.Event.BeforeClipboardPaste))
  467. return;
  468. const r = this._generateClipboardPasteParam(e);
  469. if (r && (this.fireEvent(this.Event.BeforeClipboardPaste, r), r.cancel))
  470. throw new Error("Before clipboard paste is canceled");
  471. }
  472. _clipboardPaste(e) {
  473. if (!this.hasEventCallback(this.Event.BeforeClipboardPaste))
  474. return;
  475. const r = this._generateClipboardPasteParam(e);
  476. if (r && (this.fireEvent(this.Event.BeforeClipboardPaste, r), r.cancel))
  477. throw new Error("Clipboard pasted is canceled");
  478. }
  479. async _beforeClipboardPasteAsync() {
  480. if (!this.hasEventCallback(this.Event.BeforeClipboardPaste))
  481. return;
  482. if (!he()) {
  483. this._injector.get(D).warn("[Facade]: The navigator object only supports the browser environment");
  484. return;
  485. }
  486. const e = await this._generateClipboardPasteParamAsync();
  487. if (e && (this.fireEvent(this.Event.BeforeClipboardPaste, e), e.cancel))
  488. throw new Error("Before clipboard paste is canceled");
  489. }
  490. async _clipboardPasteAsync() {
  491. if (!this.hasEventCallback(this.Event.ClipboardPasted))
  492. return;
  493. if (!he()) {
  494. this._injector.get(D).warn("[Facade]: The navigator object only supports the browser environment");
  495. return;
  496. }
  497. const e = await this._generateClipboardPasteParamAsync();
  498. if (e && (this.fireEvent(this.Event.ClipboardPasted, e), e.cancel))
  499. throw new Error("Clipboard pasted is canceled");
  500. }
  501. customizeColumnHeader(e) {
  502. const r = this.getActiveWorkbook();
  503. if (!r) {
  504. console.error("WorkBook not exist");
  505. return;
  506. }
  507. const t = r == null ? void 0 : r.getId();
  508. this._getSheetRenderComponent(t, R.COLUMN).setCustomHeader(e);
  509. }
  510. customizeRowHeader(e) {
  511. const r = this.getActiveWorkbook();
  512. if (!r) {
  513. console.error("WorkBook not exist");
  514. return;
  515. }
  516. const t = r == null ? void 0 : r.getId();
  517. this._getSheetRenderComponent(t, R.ROW).setCustomHeader(e);
  518. }
  519. registerSheetRowHeaderExtension(e, ...r) {
  520. const t = this._getSheetRenderComponent(e, R.ROW), i = t.register(...r);
  521. return C(() => {
  522. i.dispose(), t.makeDirty(!0);
  523. });
  524. }
  525. registerSheetColumnHeaderExtension(e, ...r) {
  526. const t = this._getSheetRenderComponent(e, R.COLUMN), i = t.register(...r);
  527. return C(() => {
  528. i.dispose(), t.makeDirty(!0);
  529. });
  530. }
  531. registerSheetMainExtension(e, ...r) {
  532. const t = this._getSheetRenderComponent(e, R.MAIN), i = t.register(...r);
  533. return C(() => {
  534. i.dispose(), t.makeDirty(!0);
  535. });
  536. }
  537. /**
  538. * Get sheet render component from render by unitId and view key.
  539. * @private
  540. * @param {string} unitId The unit id of the spreadsheet.
  541. * @param {SHEET_VIEW_KEY} viewKey The view key of the spreadsheet.
  542. * @returns {Nullable<RenderComponentType>} The render component.
  543. */
  544. _getSheetRenderComponent(e, r) {
  545. const i = this._injector.get(m).getRenderById(e);
  546. if (!i)
  547. throw new Error(`Render Unit with unitId ${e} not found`);
  548. const { components: o } = i, s = o.get(r);
  549. if (!s)
  550. throw new Error("Render component not found");
  551. return s;
  552. }
  553. /**
  554. * Get sheet hooks.
  555. * @returns {FSheetHooks} FSheetHooks instance
  556. */
  557. getSheetHooks() {
  558. return this._injector.createInstance(j);
  559. }
  560. }
  561. ue.extend(Fe);
  562. class Ne extends Ce {
  563. openSiderbar(e) {
  564. return this._logDeprecation("openSiderbar"), this._injector.get(We).open(e);
  565. }
  566. openDialog(e) {
  567. this._logDeprecation("openDialog");
  568. const t = this._injector.get(je).open({
  569. ...e,
  570. onClose: () => {
  571. t.dispose();
  572. }
  573. });
  574. return t;
  575. }
  576. _logDeprecation(e) {
  577. this._injector.get(D).warn("[FWorkbook]", `${e} is deprecated. Please use the function of the same name on "FUniver".`);
  578. }
  579. generateCellParams(e) {
  580. const r = this.getActiveSheet();
  581. return {
  582. row: e.row,
  583. column: e.col,
  584. workbook: this,
  585. worksheet: r
  586. };
  587. }
  588. onCellClick(e) {
  589. const r = this._injector.get(_);
  590. return C(
  591. r.currentClickedCell$.pipe(v((t) => !!t)).subscribe((t) => {
  592. e(t);
  593. })
  594. );
  595. }
  596. onCellHover(e) {
  597. const r = this._injector.get(_);
  598. return C(
  599. r.currentRichText$.pipe(v((t) => !!t)).subscribe(e)
  600. );
  601. }
  602. onCellPointerDown(e) {
  603. const r = this._injector.get(_);
  604. return C(
  605. r.currentPointerDownCell$.subscribe(e)
  606. );
  607. }
  608. onCellPointerUp(e) {
  609. const r = this._injector.get(_);
  610. return C(
  611. r.currentPointerUpCell$.subscribe(e)
  612. );
  613. }
  614. onCellPointerMove(e) {
  615. const r = this._injector.get(_);
  616. return C(
  617. r.currentCellPosWithEvent$.pipe(v((t) => !!t)).subscribe((t) => {
  618. e(t, t.event);
  619. })
  620. );
  621. }
  622. onDragOver(e) {
  623. const r = this._injector.get(U);
  624. return C(
  625. r.currentCell$.pipe(v((t) => !!t)).subscribe((t) => {
  626. e(t);
  627. })
  628. );
  629. }
  630. onDrop(e) {
  631. const r = this._injector.get(U);
  632. return C(
  633. r.endCell$.pipe(v((t) => !!t)).subscribe((t) => {
  634. e(t);
  635. })
  636. );
  637. }
  638. startEditing() {
  639. return this._injector.get(I).syncExecuteCommand(P.id, {
  640. eventType: te.Dblclick,
  641. unitId: this._workbook.getUnitId(),
  642. visible: !0
  643. });
  644. }
  645. async endEditing(e) {
  646. return this._injector.get(I).syncExecuteCommand(P.id, {
  647. eventType: te.Keyboard,
  648. keycode: e ? B.ENTER : B.ESC,
  649. visible: !1,
  650. unitId: this._workbook.getUnitId()
  651. }), await Ee(0), !0;
  652. }
  653. endEditingAsync(e = !0) {
  654. return this.endEditing(e);
  655. }
  656. /**
  657. * Get scroll state of specified sheet.
  658. * @param {string} sheetId - sheet id
  659. * @returns {IScrollState} scroll state
  660. * @example
  661. * ``` ts
  662. * univerAPI.getActiveWorkbook().getScrollStateBySheetId($sheetId)
  663. * ```
  664. */
  665. getScrollStateBySheetId(e) {
  666. const r = this._workbook.getUnitId(), i = this._injector.get(m).getRenderById(r);
  667. return i ? i.with(T).getScrollStateByParam({ unitId: r, sheetId: e }) : null;
  668. }
  669. disableSelection() {
  670. const e = this._workbook.getUnitId(), t = this._injector.get(m).getRenderById(e);
  671. return t && t.with(H).disableSelection(), this;
  672. }
  673. enableSelection() {
  674. const e = this._workbook.getUnitId(), t = this._injector.get(m).getRenderById(e);
  675. return t && t.with(H).enableSelection(), this;
  676. }
  677. transparentSelection() {
  678. const e = this._workbook.getUnitId(), t = this._injector.get(m).getRenderById(e);
  679. return t && t.with(H).transparentSelection(), this;
  680. }
  681. showSelection() {
  682. const e = this._workbook.getUnitId(), t = this._injector.get(m).getRenderById(e);
  683. return t && t.with(H).showSelection(), this;
  684. }
  685. }
  686. Ce.extend(Ne);
  687. class Ve extends be {
  688. refreshCanvas() {
  689. const e = this._injector.get(m), r = this._fWorkbook.id, t = e.getRenderById(r);
  690. if (!t)
  691. throw new Error(`Render Unit with unitId ${r} not found`);
  692. t.with(x).reCalculate();
  693. const i = t.mainComponent;
  694. if (!i)
  695. throw new Error("Main component not found");
  696. return i.makeDirty(), this;
  697. }
  698. zoom(e) {
  699. return this._injector.get(I).syncExecuteCommand(De.id, {
  700. unitId: this._workbook.getUnitId(),
  701. subUnitId: this._worksheet.getSheetId(),
  702. zoomRatio: e
  703. }), this;
  704. }
  705. getZoom() {
  706. return this._worksheet.getZoomRatio();
  707. }
  708. getVisibleRange() {
  709. const e = this._workbook.getUnitId(), t = this._injector.get(m).getRenderById(e);
  710. let i = {
  711. startColumn: 0,
  712. startRow: 0,
  713. endColumn: 0,
  714. endRow: 0
  715. };
  716. if (!t) return i;
  717. const s = t.with(x).getCurrentSkeleton();
  718. if (!s) return i;
  719. const c = s == null ? void 0 : s.getVisibleRanges();
  720. if (!c) return i;
  721. i = s.getVisibleRangeByViewport(Ie.VIEW_MAIN);
  722. for (const [g, b] of c)
  723. Pe.indexOf(g) !== -1 && (i.startColumn = Math.min(i.startColumn, b.startColumn), i.startRow = Math.min(i.startRow, b.startRow), i.endColumn = Math.max(i.endColumn, b.endColumn), i.endRow = Math.max(i.endRow, b.endRow));
  724. return i;
  725. }
  726. scrollToCell(e, r) {
  727. const t = this._workbook.getUnitId(), o = this._injector.get(m).getRenderById(t);
  728. return o && (o == null ? void 0 : o.with(Te)).scrollToCell(e, r), this;
  729. }
  730. getScrollState() {
  731. const e = {
  732. offsetX: 0,
  733. offsetY: 0,
  734. sheetViewStartColumn: 0,
  735. sheetViewStartRow: 0
  736. }, r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(), o = this._injector.get(m).getRenderById(r);
  737. return o && o.with(T).getScrollStateByParam({ unitId: r, sheetId: t }) || e;
  738. }
  739. onScroll(e) {
  740. var o;
  741. const r = this._workbook.getUnitId(), i = (o = this._injector.get(m).getRenderById(r)) == null ? void 0 : o.with(T);
  742. if (i) {
  743. const s = i.validViewportScrollInfo$.subscribe((c) => {
  744. e(c);
  745. });
  746. return C(s);
  747. }
  748. return C(() => {
  749. });
  750. }
  751. getSkeleton() {
  752. var r, t;
  753. const e = (r = this._injector.get(m).getRenderById(this._workbook.getUnitId())) == null ? void 0 : r.with(x);
  754. return (t = e == null ? void 0 : e.getWorksheetSkeleton(this._worksheet.getSheetId())) == null ? void 0 : t.skeleton;
  755. }
  756. setColumnAutoWidth(e, r) {
  757. const t = this._workbook.getUnitId(), i = this._worksheet.getSheetId(), o = [
  758. {
  759. startColumn: e,
  760. endColumn: e + r - 1,
  761. startRow: 0,
  762. endRow: this._worksheet.getRowCount() - 1
  763. }
  764. ];
  765. return this._commandService.syncExecuteCommand(Be.id, {
  766. unitId: t,
  767. subUnitId: i,
  768. ranges: o
  769. }), this;
  770. }
  771. }
  772. be.extend(Ve);
  773. class Ze extends pe {
  774. setPermissionDialogVisible(e) {
  775. this._permissionService.setShowComponents(e);
  776. }
  777. }
  778. pe.extend(Ze);
  779. class Ke extends j {
  780. onCellPointerMove(e) {
  781. return C(this._injector.get(_).currentPosition$.subscribe(e));
  782. }
  783. onCellPointerOver(e) {
  784. return C(this._injector.get(_).currentCell$.subscribe(e));
  785. }
  786. onCellDragOver(e) {
  787. return C(this._injector.get(U).currentCell$.subscribe(e));
  788. }
  789. onCellDrop(e) {
  790. return C(this._injector.get(U).endCell$.subscribe(e));
  791. }
  792. onCellRender(e, r = we.Style, t = xe.DATA_VALIDATION) {
  793. return this._injector.get(Ue).intercept(He.CELL_CONTENT, {
  794. effect: r,
  795. handler: (i, o, s) => s({
  796. ...i,
  797. customRender: [
  798. ...(i == null ? void 0 : i.customRender) || [],
  799. ...e || []
  800. ]
  801. }),
  802. priority: t
  803. });
  804. }
  805. onBeforeCellEdit(e) {
  806. return this._injector.get(I).beforeCommandExecuted((r) => {
  807. const t = r.params;
  808. r.id === P.id && t.visible && e(t);
  809. });
  810. }
  811. onAfterCellEdit(e) {
  812. return this._injector.get(I).onCommandExecuted((r) => {
  813. const t = r.params;
  814. r.id === P.id && !t.visible && e(t);
  815. });
  816. }
  817. }
  818. j.extend(Ke);
  819. const y = {
  820. CellClicked: "CellClicked",
  821. CellPointerDown: "CellPointerDown",
  822. CellPointerUp: "CellPointerUp",
  823. CellPointerMove: "CellPointerMove",
  824. CellHover: "CellHover",
  825. DragOver: "DragOver",
  826. Drop: "Drop",
  827. Scroll: "Scroll",
  828. SelectionMoveStart: "SelectionMoveStart",
  829. SelectionMoving: "SelectionMoving",
  830. SelectionMoveEnd: "SelectionMoveEnd",
  831. SelectionChanged: "SelectionChanged"
  832. };
  833. class ze extends ve {
  834. get BeforeClipboardChange() {
  835. return "BeforeClipboardChange";
  836. }
  837. get ClipboardChanged() {
  838. return "ClipboardChanged";
  839. }
  840. get BeforeClipboardPaste() {
  841. return "BeforeClipboardPaste";
  842. }
  843. get ClipboardPasted() {
  844. return "ClipboardPasted";
  845. }
  846. get BeforeSheetEditStart() {
  847. return "BeforeSheetEditStart";
  848. }
  849. get SheetEditStarted() {
  850. return "SheetEditStarted";
  851. }
  852. get SheetEditChanging() {
  853. return "SheetEditChanging";
  854. }
  855. get BeforeSheetEditEnd() {
  856. return "BeforeSheetEditEnd";
  857. }
  858. get SheetEditEnded() {
  859. return "SheetEditEnded";
  860. }
  861. get CellClicked() {
  862. return y.CellClicked;
  863. }
  864. get CellHover() {
  865. return y.CellHover;
  866. }
  867. get CellPointerDown() {
  868. return y.CellPointerDown;
  869. }
  870. get CellPointerUp() {
  871. return y.CellPointerUp;
  872. }
  873. get CellPointerMove() {
  874. return y.CellPointerMove;
  875. }
  876. get DragOver() {
  877. return "DragOver";
  878. }
  879. get Drop() {
  880. return "Drop";
  881. }
  882. get Scroll() {
  883. return "Scroll";
  884. }
  885. get SelectionMoveStart() {
  886. return "SelectionMoveStart";
  887. }
  888. get SelectionChanged() {
  889. return "SelectionChanged";
  890. }
  891. get SelectionMoving() {
  892. return "SelectionMoving";
  893. }
  894. get SelectionMoveEnd() {
  895. return "SelectionMoveEnd";
  896. }
  897. get RowHeaderClick() {
  898. return "RowHeaderClick";
  899. }
  900. get RowHeaderPointerDown() {
  901. return "RowHeaderPointerDown";
  902. }
  903. get RowHeaderPointerUp() {
  904. return "RowHeaderPointerUp";
  905. }
  906. get RowHeaderHover() {
  907. return "RowHeaderHover";
  908. }
  909. get ColumnHeaderClick() {
  910. return "ColumnHeaderClick";
  911. }
  912. get ColumnHeaderPointerDown() {
  913. return "ColumnHeaderPointerDown";
  914. }
  915. get ColumnHeaderPointerUp() {
  916. return "ColumnHeaderPointerUp";
  917. }
  918. get ColumnHeaderHover() {
  919. return "ColumnHeaderHover";
  920. }
  921. get SheetSkeletonChanged() {
  922. return "SheetSkeletonChanged";
  923. }
  924. }
  925. ve.extend(ze);
  926. class Ye extends me {
  927. getCell() {
  928. var c;
  929. const e = this._injector.get(m), r = this._injector.get(D), t = this._workbook.getUnitId(), i = this._worksheet.getSheetId(), o = e.getRenderById(t), s = (c = o == null ? void 0 : o.with(x).getWorksheetSkeleton(i)) == null ? void 0 : c.skeleton;
  930. if (!s)
  931. throw r.error("[Facade]: `FRange.getCell` can only be called in current worksheet"), new Error("`FRange.getCell` can only be called in current worksheet");
  932. return s.getCellWithCoordByIndex(this._range.startRow, this._range.startColumn);
  933. }
  934. getCellRect() {
  935. const { startX: e, startY: r, endX: t, endY: i } = this.getCell(), o = { x: e, y: r, width: t - e, height: i - r, top: r, left: e, bottom: i, right: t };
  936. return { ...o, toJSON: () => JSON.stringify(o) };
  937. }
  938. generateHTML() {
  939. var r;
  940. const e = this._injector.get(Se).generateCopyContent(
  941. this._workbook.getUnitId(),
  942. this._worksheet.getSheetId(),
  943. this._range
  944. );
  945. return (r = e == null ? void 0 : e.html) != null ? r : "";
  946. }
  947. attachPopup(e) {
  948. var s, c, g;
  949. e.direction = (s = e.direction) != null ? s : "horizontal", e.extraProps = (c = e.extraProps) != null ? c : {}, e.offset = (g = e.offset) != null ? g : [0, 0];
  950. const { key: r, disposableCollection: t } = ge(e, this._injector.get(le)), o = this._injector.get(ie).attachPopupToCell(
  951. this._range.startRow,
  952. this._range.startColumn,
  953. { ...e, componentKey: r },
  954. this.getUnitId(),
  955. this._worksheet.getSheetId()
  956. );
  957. return o ? (t.add(o), t) : (t.dispose(), null);
  958. }
  959. attachAlertPopup(e) {
  960. const r = this._injector.get(Le), t = {
  961. workbook: this._workbook,
  962. worksheet: this._worksheet,
  963. row: this._range.startRow,
  964. col: this._range.startColumn,
  965. unitId: this.getUnitId(),
  966. subUnitId: this._worksheet.getSheetId()
  967. };
  968. return r.showAlert({
  969. ...e,
  970. location: t
  971. }), {
  972. dispose: () => {
  973. r.removeAlert(e.key);
  974. }
  975. };
  976. }
  977. /**
  978. * attachDOMPopup
  979. * @param popup
  980. * @returns {IDisposable} disposable
  981. let sheet = univerAPI.getActiveWorkbook().getActiveSheet();
  982. let range = sheet.getRange(2, 2, 3, 3);
  983. univerAPI.getActiveWorkbook().setActiveRange(range);
  984. let disposable = range.attachDOMPopup({
  985. componentKey: 'univer.sheet.single-dom-popup',
  986. extraProps: { alert: { type: 0, title: 'This is an Info', message: 'This is an info message' } },
  987. });
  988. */
  989. attachRangePopup(e) {
  990. var s, c, g;
  991. e.direction = (s = e.direction) != null ? s : "horizontal", e.extraProps = (c = e.extraProps) != null ? c : {}, e.offset = (g = e.offset) != null ? g : [0, 0];
  992. const { key: r, disposableCollection: t } = ge(e, this._injector.get(le)), o = this._injector.get(ie).attachRangePopup(
  993. this._range,
  994. { ...e, componentKey: r },
  995. this.getUnitId(),
  996. this._worksheet.getSheetId()
  997. );
  998. return o ? (t.add(o), t) : (t.dispose(), null);
  999. }
  1000. highlight(e, r) {
  1001. const t = this._injector.get(Ae), i = t.addShape({ range: this._range, style: e, primary: r });
  1002. if (!i)
  1003. throw new Error("Failed to highlight current range");
  1004. return C(() => {
  1005. t.removeShape(i);
  1006. });
  1007. }
  1008. }
  1009. me.extend(Ye);
  1010. function ge(w, e) {
  1011. const { componentKey: r, isVue3: t } = w;
  1012. let i;
  1013. const o = new W();
  1014. return typeof r == "string" ? i = r : (i = `External_${_e(6)}`, o.add(e.register(i, r, { framework: t ? "vue3" : "react" }))), {
  1015. key: i,
  1016. disposableCollection: o
  1017. };
  1018. }
  1019. export {
  1020. ge as transformComponentKey
  1021. };