UNPKG

@univerjs/sheets-ui

Version:

UniverSheet normal ui-plugin-sheets

1,241 lines (1,240 loc) • 41.8 kB
import { ICommandService as R, CanceledError as b, IUniverInstanceService as O, RichTextValue as F, DOCS_NORMAL_EDITOR_UNIT_ID_KEY as D, LifecycleService as ae, DisposableCollection as j, LifecycleStages as z, UniverInstanceType as ce, ILogService as U, toDisposable as m, IPermissionService as de, awaitTime as he, InterceptorEffectEnum as ge, Rectangle as le, generateRandomId as ue } from "@univerjs/core"; import { FUniver as Q, FEventName as ve, FEnum as Se } from "@univerjs/core/facade"; import { RichTextEditingMutation as me } from "@univerjs/docs"; import { IRenderManagerService as v, DeviceInputEventType as L, SHEET_VIEWPORT_KEY as ee } from "@univerjs/engine-render"; import { SheetsSelectionsService as Ce, COMMAND_LISTENER_SKELETON_CHANGE as we, getSkeletonChangedEffectedRange as Ee, SetWorksheetRowIsAutoHeightCommand as be, InterceptCellContentPriority as pe, SheetInterceptorService as fe, INTERCEPTOR_POINT as ke } from "@univerjs/sheets"; import { SetCellEditVisibleOperation as I, IEditorBridgeService as f, SetZoomRatioCommand as $, HoverManagerService as k, DragManagerService as M, SheetScrollManagerService as x, SheetPasteShortKeyCommand as T, ISheetClipboardService as te, SheetSkeletonManagerService as p, SHEET_VIEW_KEY as E, SheetPermissionRenderManagerService as A, ISheetSelectionRenderService as y, IMarkSelectionService as re, SheetsScrollRenderController as Ie, SetWorksheetColAutoWidthCommand as _e, SetColumnHeaderHeightCommand as Re, SetRowHeaderWidthCommand as Pe, SheetCanvasPopManagerService as N, CellAlertManagerService as He, ISheetCellDropdownManagerService as Me, AutoFillCommand as ye } from "@univerjs/sheets-ui"; import { FSheetHooks as W, FWorkbook as ne, FWorksheet as ie, FPermission as oe, FRange as se } from "@univerjs/sheets/facade"; import { KeyCode as B, CutCommand as V, CopyCommand as Z, PasteCommand as K, IClipboardInterfaceService as Ue, PLAIN_TEXT_CLIPBOARD_MIME_TYPE as Y, HTML_CLIPBOARD_MIME_TYPE as X, supportClipboardAPI as J, ISidebarService as xe, IDialogService as Be, ComponentManager as G } from "@univerjs/ui"; import { filter as u, combineLatest as De } from "rxjs"; class Te extends Q { // eslint-disable-next-line max-lines-per-function _initSheetUIEvent(e) { const r = e.get(R); this.registerEventHandler( this.Event.BeforeSheetEditStart, () => r.beforeCommandExecuted((t) => { if (t.id !== I.id) return; const i = this.getActiveSheet(); if (!i) return; const { workbook: o, worksheet: s } = i, d = e.get(f), h = t.params, { visible: g, keycode: l, eventType: a } = h, n = d.getEditLocation(); if (g) { const c = { row: n.row, column: n.column, eventType: a, keycode: l, workbook: o, worksheet: s, isZenEditor: !1 }; if (this.fireEvent(this.Event.BeforeSheetEditStart, c), c.cancel) throw new b(); } }) ), this.registerEventHandler( this.Event.BeforeSheetEditEnd, () => r.beforeCommandExecuted((t) => { if (t.id !== I.id) return; const i = this.getActiveSheet(); if (!i) return; const { workbook: o, worksheet: s } = i, d = e.get(f), h = e.get(O), g = t.params, { visible: l, keycode: a, eventType: n } = g, c = d.getEditLocation(); if (!l) { const P = { row: c.row, column: c.column, eventType: n, keycode: a, workbook: o, worksheet: s, isZenEditor: !1, value: F.create(h.getUnit(D).getSnapshot()), isConfirm: a !== B.ESC }; if (this.fireEvent(this.Event.BeforeSheetEditEnd, P), P.cancel) throw new b(); } }) ), this.registerEventHandler( this.Event.SheetEditStarted, () => r.onCommandExecuted((t) => { if (t.id !== I.id) return; const i = this.getCommandSheetTarget(t); if (!i) return; const { workbook: o, worksheet: s } = i, d = e.get(f), h = t.params, { visible: g, keycode: l, eventType: a } = h, n = d.getEditLocation(); if (g) { const c = { row: n.row, column: n.column, eventType: a, keycode: l, workbook: o, worksheet: s, isZenEditor: !1 }; this.fireEvent(this.Event.SheetEditStarted, c); } }) ), this.registerEventHandler( this.Event.SheetEditEnded, () => r.onCommandExecuted((t) => { if (t.id !== I.id) return; const i = this.getCommandSheetTarget(t); if (!i) return; const { workbook: o, worksheet: s } = i, d = e.get(f), h = t.params, { visible: g, keycode: l, eventType: a } = h, n = d.getEditLocation(); if (!g) { const c = { row: n.row, column: n.column, eventType: a, keycode: l, workbook: o, worksheet: s, isZenEditor: !1, isConfirm: l !== B.ESC }; this.fireEvent(this.Event.SheetEditEnded, c); } }) ), this.registerEventHandler( this.Event.SheetEditChanging, () => r.onCommandExecuted((t) => { if (t.id !== me.id) return; const i = this.getActiveSheet(); if (!i) return; const { workbook: o, worksheet: s } = i, d = e.get(f), h = e.get(O), g = t.params; if (!d.isVisible().visible) return; const { unitId: l } = g; if (l === D) { const { row: a, column: n } = d.getEditLocation(), c = { workbook: o, worksheet: s, row: a, column: n, value: F.create(h.getUnit(D).getSnapshot()), isZenEditor: !1 }; this.fireEvent(this.Event.SheetEditChanging, c); } }) ), this.registerEventHandler( this.Event.BeforeSheetZoomChange, () => r.beforeCommandExecuted((t) => { if (t.id !== $.id) return; const i = this.getCommandSheetTarget(t); if (!i) return; const { workbook: o, worksheet: s } = i, d = { zoom: t.params.zoomRatio, workbook: o, worksheet: s }; if (this.fireEvent(this.Event.BeforeSheetZoomChange, d), d.cancel) throw new b(); }) ), this.registerEventHandler( this.Event.SheetZoomChanged, () => r.onCommandExecuted((t) => { if (t.id !== $.id) return; const i = this.getCommandSheetTarget(t); if (!i) return; const { workbook: o, worksheet: s } = i; this.fireEvent(this.Event.SheetZoomChanged, { zoom: s.getZoom(), workbook: o, worksheet: s }); }) ); } // eslint-disable-next-line max-lines-per-function _initObserverListener(e) { const r = e.get(v), t = e.get(ae), i = new j(); this.disposeWithMe(t.lifecycle$.subscribe((h) => { if (h !== z.Rendered) return; i.dispose(); const g = e.get(k), l = e.get(M); g && (this.registerEventHandler( this.Event.CellClicked, () => { var a; return (a = g.currentClickedCell$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.location.unitId, n.location.subUnitId); c && this.fireEvent(this.Event.CellClicked, { ...c, ...n, row: n.location.row, column: n.location.col }); }); } ), this.registerEventHandler( this.Event.CellHover, () => { var a; return (a = g.currentRichText$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.CellHover, { ...c, ...n, row: n.row, column: n.col }); }); } ), this.registerEventHandler( this.Event.CellPointerDown, () => { var a; return (a = g.currentPointerDownCell$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.CellPointerDown, { ...c, ...n, row: n.row, column: n.col }); }); } ), this.registerEventHandler( this.Event.CellPointerUp, () => { var a; return (a = g.currentPointerUpCell$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.CellPointerUp, { ...c, ...n, row: n.row, column: n.col }); }); } ), this.registerEventHandler( this.Event.CellPointerMove, () => { var a; return (a = g.currentCellPosWithEvent$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.CellPointerMove, { ...c, ...n, row: n.row, column: n.col }); }); } ), this.registerEventHandler( this.Event.DragOver, () => { var a; return (a = l.currentCell$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.location.unitId, n.location.subUnitId); c && this.fireEvent(this.Event.DragOver, { ...c, ...n, row: n.location.row, column: n.location.col }); }); } ), this.registerEventHandler( this.Event.Drop, () => { var a; return (a = l.endCell$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.location.unitId, n.location.subUnitId); c && this.fireEvent(this.Event.Drop, { ...c, ...n, row: n.location.row, column: n.location.col }); }); } ), this.registerEventHandler( this.Event.RowHeaderClick, () => { var a; return (a = g.currentRowHeaderClick$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.RowHeaderClick, { ...c, row: n.index }); }); } ), this.registerEventHandler( this.Event.RowHeaderPointerDown, () => { var a; return (a = g.currentRowHeaderPointerDown$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.RowHeaderPointerDown, { ...c, row: n.index }); }); } ), this.registerEventHandler( this.Event.RowHeaderPointerUp, () => { var a; return (a = g.currentRowHeaderPointerUp$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.RowHeaderPointerUp, { ...c, row: n.index }); }); } ), this.registerEventHandler( this.Event.RowHeaderHover, () => { var a; return (a = g.currentHoveredRowHeader$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.RowHeaderHover, { ...c, row: n.index }); }); } ), this.registerEventHandler( this.Event.ColumnHeaderClick, () => { var a; return (a = g.currentColHeaderClick$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.ColumnHeaderClick, { ...c, column: n.index }); }); } ), this.registerEventHandler( this.Event.ColumnHeaderPointerDown, () => { var a; return (a = g.currentColHeaderPointerDown$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.ColumnHeaderPointerDown, { ...c, column: n.index }); }); } ), this.registerEventHandler( this.Event.ColumnHeaderPointerUp, () => { var a; return (a = g.currentColHeaderPointerUp$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.ColumnHeaderPointerUp, { ...c, column: n.index }); }); } ), this.registerEventHandler( this.Event.ColumnHeaderHover, () => { var a; return (a = g.currentHoveredColHeader$) == null ? void 0 : a.pipe(u((n) => !!n)).subscribe((n) => { const c = this.getSheetTarget(n.unitId, n.subUnitId); c && this.fireEvent(this.Event.ColumnHeaderHover, { ...c, column: n.index }); }); } )); })), this.disposeWithMe(i); const o = /* @__PURE__ */ new Map(); let s; const d = De([ r.created$, t.lifecycle$ ]); this.disposeWithMe(d.subscribe(([h, g]) => { var P; if (h.type === ce.UNIVER_SHEET && (s = h), g <= z.Rendered || !s) return; const l = new j(), a = this.getWorkbook(s.unitId); if (!a) return; o.get(s.unitId) && ((P = o.get(s.unitId)) == null || P.dispose()), o.set(s.unitId, l); const n = s.with(x), c = s.with(Ce); l.add(this.registerEventHandler( this.Event.Scroll, () => n.validViewportScrollInfo$.subscribe((S) => { S && this.fireEvent(this.Event.Scroll, { workbook: a, worksheet: a.getActiveSheet(), ...S }); }) )), l.add(this.registerEventHandler( this.Event.SelectionMoveStart, () => c.selectionMoveStart$.subscribe((S) => { var C; this.fireEvent(this.Event.SelectionMoveStart, { workbook: a, worksheet: a.getActiveSheet(), selections: (C = S == null ? void 0 : S.map((_) => _.range)) != null ? C : [] }); }) )), l.add(this.registerEventHandler( this.Event.SelectionMoving, () => c.selectionMoving$.subscribe((S) => { var C; this.fireEvent(this.Event.SelectionMoving, { workbook: a, worksheet: a.getActiveSheet(), selections: (C = S == null ? void 0 : S.map((_) => _.range)) != null ? C : [] }); }) )), l.add(this.registerEventHandler( this.Event.SelectionMoveEnd, () => c.selectionMoveEnd$.subscribe((S) => { var C; this.fireEvent(this.Event.SelectionMoveEnd, { workbook: a, worksheet: a.getActiveSheet(), selections: (C = S == null ? void 0 : S.map((_) => _.range)) != null ? C : [] }); }) )), l.add(this.registerEventHandler( this.Event.SelectionChanged, () => c.selectionChanged$.subscribe((S) => { var C; this.fireEvent(this.Event.SelectionChanged, { workbook: a, worksheet: a.getActiveSheet(), selections: (C = S == null ? void 0 : S.map((_) => _.range)) != null ? C : [] }); }) )), s = null, this.disposeWithMe(l); })), this.disposeWithMe(r.disposed$.subscribe((h) => { var g; (g = o.get(h)) == null || g.dispose(), o.delete(h); })), this.disposeWithMe(() => { o.forEach((h) => { h.dispose(); }); }); } /** * @ignore */ _initialize(e) { this._initSheetUIEvent(e), this._initObserverListener(e); const r = e.get(R); this.registerEventHandler( this.Event.BeforeClipboardChange, () => r.beforeCommandExecuted((t) => { switch (t.id) { case Z.id: case V.id: this._beforeClipboardChange(); break; } }) ), this.registerEventHandler( this.Event.ClipboardChanged, () => r.onCommandExecuted((t) => { switch (t.id) { case Z.id: case V.id: this._clipboardChanged(); break; } }) ), this.registerEventHandler( this.Event.BeforeClipboardPaste, () => r.beforeCommandExecuted((t) => { switch (t.id) { case T.id: this._beforeClipboardPaste(t.params); break; case K.id: this._beforeClipboardPasteAsync(); break; } }) ), this.registerEventHandler( this.Event.ClipboardPasted, () => r.onCommandExecuted((t) => { switch (t.id) { case T.id: this._clipboardPaste(t.params); break; case K.id: this._clipboardPasteAsync(); break; } }) ), this.registerEventHandler( this.Event.SheetSkeletonChanged, () => r.onCommandExecuted((t) => { if (we.indexOf(t.id) > -1) { const i = this.getActiveSheet(); if (!i) return; const o = Ee(t, i.worksheet.getMaxColumns()).map((s) => { var d, h; return (h = (d = this.getWorkbook(s.unitId)) == null ? void 0 : d.getSheetBySheetId(s.subUnitId)) == null ? void 0 : h.getRange(s.range); }).filter(Boolean); if (!o.length) return; this.fireEvent(this.Event.SheetSkeletonChanged, { workbook: i.workbook, worksheet: i.worksheet, payload: t, skeleton: i.worksheet.getSkeleton(), effectedRanges: o }); } }) ); } _generateClipboardCopyParam() { const e = this.getActiveWorkbook(), r = e == null ? void 0 : e.getActiveSheet(), t = e == null ? void 0 : e.getActiveRange(); if (!e || !r || !t) return; const o = this._injector.get(te).generateCopyContent(e.getId(), r.getSheetId(), t.getRange()); if (!o) return; const { html: s, plain: d } = o; return { workbook: e, worksheet: r, text: d, html: s, fromSheet: r, fromRange: t }; } _beforeClipboardChange() { const e = this._generateClipboardCopyParam(); if (e && (this.fireEvent(this.Event.BeforeClipboardChange, e), e.cancel)) throw new b(); } _clipboardChanged() { const e = this._generateClipboardCopyParam(); e && this.fireEvent(this.Event.ClipboardChanged, e); } _generateClipboardPasteParam(e) { if (!e) return; const { htmlContent: r, textContent: t } = e, i = this.getActiveWorkbook(), o = i == null ? void 0 : i.getActiveSheet(); return !i || !o ? void 0 : { workbook: i, worksheet: o, text: t, html: r }; } async _generateClipboardPasteParamAsync() { const e = this.getActiveWorkbook(), r = e == null ? void 0 : e.getActiveSheet(); if (!e || !r) return; const o = (await this._injector.get(Ue).read())[0]; let s; if (o) { const d = o.types, h = d.indexOf(Y) !== -1 ? await o.getType(Y).then((l) => l && l.text()) : "", g = d.indexOf(X) !== -1 ? await o.getType(X).then((l) => l && l.text()) : ""; s = { workbook: e, worksheet: r, text: h, html: g }; } return s; } _beforeClipboardPaste(e) { const r = this._generateClipboardPasteParam(e); if (r && (this.fireEvent(this.Event.BeforeClipboardPaste, r), r.cancel)) throw new b(); } _clipboardPaste(e) { const r = this._generateClipboardPasteParam(e); if (r && (this.fireEvent(this.Event.ClipboardPasted, r), r.cancel)) throw new b(); } async _beforeClipboardPasteAsync() { if (!J()) { this._injector.get(U).warn("[Facade]: The navigator object only supports the browser environment"); return; } const e = await this._generateClipboardPasteParamAsync(); if (e && (this.fireEvent(this.Event.BeforeClipboardPaste, e), e.cancel)) throw new b(); } async _clipboardPasteAsync() { if (!J()) { this._injector.get(U).warn("[Facade]: The navigator object only supports the browser environment"); return; } const e = await this._generateClipboardPasteParamAsync(); if (e && (this.fireEvent(this.Event.ClipboardPasted, e), e.cancel)) throw new b(); } customizeColumnHeader(e) { var g, l; const r = this.getActiveWorkbook(); if (!r) { console.error("WorkBook not exist"); return; } const t = r == null ? void 0 : r.getId(), i = this._injector.get(v), o = r.getActiveSheet(), s = o.getSheetId(), d = i.getRenderById(t); d && ((g = e.headerStyle) != null && g.size) && (d.with(p).setColumnHeaderSize(d, s, (l = e.headerStyle) == null ? void 0 : l.size), o == null || o.refreshCanvas()), this._getSheetRenderComponent(t, E.COLUMN).setCustomHeader(e), o == null || o.refreshCanvas(); } customizeRowHeader(e) { const r = this.getActiveWorkbook(); if (!r) { console.error("WorkBook not exist"); return; } const t = r == null ? void 0 : r.getId(); this._getSheetRenderComponent(t, E.ROW).setCustomHeader(e); } registerSheetRowHeaderExtension(e, ...r) { const t = this._getSheetRenderComponent(e, E.ROW), i = t.register(...r); return m(() => { i.dispose(), t.makeDirty(!0); }); } registerSheetColumnHeaderExtension(e, ...r) { const t = this._getSheetRenderComponent(e, E.COLUMN), i = t.register(...r); return m(() => { i.dispose(), t.makeDirty(!0); }); } registerSheetMainExtension(e, ...r) { const t = this._getSheetRenderComponent(e, E.MAIN), i = t.register(...r); return m(() => { i.dispose(), t.makeDirty(!0); }); } /** * Get sheet render component from render by unitId and view key. * @private * @param {string} unitId The unit id of the spreadsheet. * @param {SHEET_VIEW_KEY} viewKey The view key of the spreadsheet. * @returns {Nullable<RenderComponentType>} The render component. */ _getSheetRenderComponent(e, r) { const i = this._injector.get(v).getRenderById(e); if (!i) throw new Error(`Render Unit with unitId ${e} not found`); const { components: o } = i, s = o.get(r); if (!s) throw new Error("Render component not found"); return s; } /** * Get sheet hooks. * @returns {FSheetHooks} FSheetHooks instance */ getSheetHooks() { return this._injector.createInstance(W); } pasteIntoSheet(e, r, t) { return this._commandService.executeCommand(T.id, { htmlContent: e, textContent: r, files: t }); } setProtectedRangeShadowStrategy(e) { this._injector.get(A).setProtectedRangeShadowStrategy(e); } getProtectedRangeShadowStrategy() { return this._injector.get(A).getProtectedRangeShadowStrategy(); } getProtectedRangeShadowStrategy$() { return this._injector.get(A).getProtectedRangeShadowStrategy$(); } setPermissionDialogVisible(e) { this._injector.get(de).setShowComponents(e); } } Q.extend(Te); class Ae extends ne { openSiderbar(e) { return this._logDeprecation("openSiderbar"), this._injector.get(xe).open(e); } openDialog(e) { this._logDeprecation("openDialog"); const t = this._injector.get(Be).open({ ...e, onClose: () => { t.dispose(); } }); return t; } customizeColumnHeader(e) { const r = this._workbook.getUnitId(); this._getSheetRenderComponent(r, E.COLUMN).setCustomHeader(e); } customizeRowHeader(e) { const r = this._workbook.getUnitId(); this._getSheetRenderComponent(r, E.ROW).setCustomHeader(e); } /** * Get sheet render component from render by unitId and view key. * @private * @param {string} unitId The unit id of the spreadsheet. * @param {SHEET_VIEW_KEY} viewKey The view key of the spreadsheet. * @returns {Nullable<RenderComponentType>} The render component. */ _getSheetRenderComponent(e, r) { const i = this._injector.get(v).getRenderById(e); if (!i) throw new Error(`Render Unit with unitId ${e} not found`); const { components: o } = i, s = o.get(r); if (!s) throw new Error("Render component not found"); return s; } _logDeprecation(e) { this._injector.get(U).warn("[FWorkbook]", `${e} is deprecated. Please use the function of the same name on "FUniver".`); } generateCellParams(e) { const r = this.getActiveSheet(); return { row: e.row, column: e.col, workbook: this, worksheet: r }; } onCellClick(e) { const r = this._injector.get(k); return m( r.currentClickedCell$.pipe(u((t) => !!t)).subscribe((t) => { e(t); }) ); } onCellHover(e) { const r = this._injector.get(k); return m( r.currentRichText$.pipe(u((t) => !!t)).subscribe(e) ); } onCellPointerDown(e) { const r = this._injector.get(k); return m( r.currentPointerDownCell$.subscribe(e) ); } onCellPointerUp(e) { const r = this._injector.get(k); return m( r.currentPointerUpCell$.subscribe(e) ); } onCellPointerMove(e) { const r = this._injector.get(k); return m( r.currentCellPosWithEvent$.pipe(u((t) => !!t)).subscribe((t) => { e(t, t.event); }) ); } onDragOver(e) { const r = this._injector.get(M); return m( r.currentCell$.pipe(u((t) => !!t)).subscribe((t) => { e(t); }) ); } onDrop(e) { const r = this._injector.get(M); return m( r.endCell$.pipe(u((t) => !!t)).subscribe((t) => { e(t); }) ); } startEditing() { const e = this._injector.get(R); return this._injector.get(f).isVisible().visible ? !0 : e.syncExecuteCommand(I.id, { eventType: L.Dblclick, unitId: this._workbook.getUnitId(), visible: !0 }); } async endEditing(e) { const r = this._injector.get(R); return this._injector.get(f).isVisible().visible && r.syncExecuteCommand(I.id, { eventType: L.Keyboard, keycode: e ? B.ENTER : B.ESC, visible: !1, unitId: this._workbook.getUnitId() }), await he(0), !0; } endEditingAsync(e = !0) { return this.endEditing(e); } abortEditingAsync() { return this.endEditingAsync(!1); } isCellEditing() { return this._injector.get(f).isVisible().visible; } /** * Get scroll state of specified sheet. * @param {string} sheetId - sheet id * @returns {IScrollState} scroll state * @example * ``` ts * univerAPI.getActiveWorkbook().getScrollStateBySheetId($sheetId) * ``` */ getScrollStateBySheetId(e) { const r = this._workbook.getUnitId(), i = this._injector.get(v).getRenderById(r); return i ? i.with(x).getScrollStateByParam({ unitId: r, sheetId: e }) : null; } disableSelection() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); return t && t.with(y).disableSelection(), this; } enableSelection() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); return t && t.with(y).enableSelection(), this; } transparentSelection() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); return t && t.with(y).transparentSelection(), this; } showSelection() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); return t && t.with(y).showSelection(), this; } } ne.extend(Ae); class je extends ie { refreshCanvas() { const e = this._injector.get(v), r = this._fWorkbook.id, t = e.getRenderById(r); if (!t) throw new Error(`Render Unit with unitId ${r} not found`); t.with(p).reCalculate(); const i = t.mainComponent; if (!i) throw new Error("Main component not found"); return i.makeDirty(), this; } highlightRanges(e, r, t) { const i = this._injector.get(re), o = []; for (const s of e) { const d = s.getRange(), h = i.addShapeWithNoFresh({ range: d, style: r, primary: t }); h && o.push(h); } if (i.refreshShapes(), o.length === 0) throw new Error("Failed to highlight current range"); return m(() => { o.forEach((s) => { i.removeShape(s); }); }); } zoom(e) { const r = this._injector.get(R), t = Math.min(Math.max(e, 0.1), 4); return r.executeCommand($.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), zoomRatio: t }), this; } getZoom() { return this._worksheet.getZoomRatio(); } getVisibleRange() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); if (!t) return null; const o = t.with(p).getCurrentSkeleton(); return o ? o.getVisibleRangeByViewport(ee.VIEW_MAIN) : null; } getVisibleRangesOfAllViewports() { const e = this._workbook.getUnitId(), t = this._injector.get(v).getRenderById(e); if (!t) return null; const o = t.with(p).getCurrentSkeleton(); return o ? o.getVisibleRanges() : null; } scrollToCell(e, r, t) { const i = this._workbook.getUnitId(), s = this._injector.get(v).getRenderById(i); return s && (s == null ? void 0 : s.with(Ie)).scrollToCell(e, r, t), this; } getScrollState() { const e = { offsetX: 0, offsetY: 0, sheetViewStartColumn: 0, sheetViewStartRow: 0 }, r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(), o = this._injector.get(v).getRenderById(r); return o && o.with(x).getScrollStateByParam({ unitId: r, sheetId: t }) || e; } onScroll(e) { var o; const r = this._workbook.getUnitId(), i = (o = this._injector.get(v).getRenderById(r)) == null ? void 0 : o.with(x); if (i) { const s = i.validViewportScrollInfo$.subscribe((d) => { e(d); }); return m(s); } return m(() => { }); } getSkeleton() { var r, t; const e = (r = this._injector.get(v).getRenderById(this._workbook.getUnitId())) == null ? void 0 : r.with(p); return (t = e == null ? void 0 : e.getWorksheetSkeleton(this._worksheet.getSheetId())) == null ? void 0 : t.skeleton; } autoResizeColumn(e) { return this.autoResizeColumns(e, 1); } autoResizeColumns(e, r) { const t = this._workbook.getUnitId(), i = this._worksheet.getSheetId(), o = [ { startColumn: e, endColumn: e + r - 1, startRow: 0, endRow: this._worksheet.getRowCount() - 1 } ]; return this._commandService.syncExecuteCommand(_e.id, { unitId: t, subUnitId: i, ranges: o }), this; } setColumnAutoWidth(e, r) { return this.autoResizeColumns(e, r); } autoResizeRows(e, r) { const t = this._workbook.getUnitId(), i = this._worksheet.getSheetId(), o = [ { startRow: e, endRow: e + r - 1, startColumn: 0, endColumn: this._worksheet.getColumnCount() - 1 } ]; return this._commandService.syncExecuteCommand(be.id, { unitId: t, subUnitId: i, ranges: o }), this; } customizeColumnHeader(e) { var d, h; const r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(), o = this._injector.get(v).getRenderById(r); o && ((d = e.headerStyle) != null && d.size) && o.with(p).setColumnHeaderSize(o, t, (h = e.headerStyle) == null ? void 0 : h.size), this._getSheetRenderComponent(r, E.COLUMN).setCustomHeader(e, t); } customizeRowHeader(e) { var d, h; const r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(), o = this._injector.get(v).getRenderById(r); o && ((d = e.headerStyle) != null && d.size) && o.with(p).setRowHeaderSize(o, t, (h = e.headerStyle) == null ? void 0 : h.size), this._getSheetRenderComponent(r, E.ROW).setCustomHeader(e, t); } setColumnHeaderHeight(e) { const r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(); return this._commandService.executeCommand(Re.id, { unitId: r, subUnitId: t, size: e }), this; } setRowHeaderWidth(e) { const r = this._workbook.getUnitId(), t = this._worksheet.getSheetId(); return this._commandService.executeCommand(Pe.id, { unitId: r, subUnitId: t, size: e }), this; } /** * Get sheet render component from render by unitId and view key. * @private * @param {string} unitId The unit id of the spreadsheet. * @param {SHEET_VIEW_KEY} viewKey The view key of the spreadsheet. * @returns {Nullable<RenderComponentType>} The render component. */ _getSheetRenderComponent(e, r) { const i = this._injector.get(v).getRenderById(e); if (!i) throw new Error(`Render Unit with unitId ${e} not found`); const { components: o } = i, s = o.get(r); if (!s) throw new Error("Render component not found"); return s; } } ie.extend(je); class $e extends oe { setPermissionDialogVisible(e) { this._permissionService.setShowComponents(e); } } oe.extend($e); class We extends W { onCellPointerMove(e) { return m(this._injector.get(k).currentPosition$.subscribe(e)); } onCellPointerOver(e) { return m(this._injector.get(k).currentCell$.subscribe(e)); } onCellDragOver(e) { return m(this._injector.get(M).currentCell$.subscribe(e)); } onCellDrop(e) { return m(this._injector.get(M).endCell$.subscribe(e)); } onCellRender(e, r = ge.Style, t = pe.DATA_VALIDATION) { return this._injector.get(fe).intercept(ke.CELL_CONTENT, { effect: r, handler: (i, o, s) => (i && !i.customRender && e && (i.customRender = [...e]), s(i)), priority: t }); } onBeforeCellEdit(e) { return this._injector.get(R).beforeCommandExecuted((r) => { const t = r.params; r.id === I.id && t.visible && e(t); }); } onAfterCellEdit(e) { return this._injector.get(R).onCommandExecuted((r) => { const t = r.params; r.id === I.id && !t.visible && e(t); }); } } W.extend(We); const H = { CellClicked: "CellClicked", CellPointerDown: "CellPointerDown", CellPointerUp: "CellPointerUp", CellPointerMove: "CellPointerMove", CellHover: "CellHover" }; class Oe { get BeforeClipboardChange() { return "BeforeClipboardChange"; } get ClipboardChanged() { return "ClipboardChanged"; } get BeforeClipboardPaste() { return "BeforeClipboardPaste"; } get ClipboardPasted() { return "ClipboardPasted"; } get BeforeSheetEditStart() { return "BeforeSheetEditStart"; } get SheetEditStarted() { return "SheetEditStarted"; } get SheetEditChanging() { return "SheetEditChanging"; } get BeforeSheetEditEnd() { return "BeforeSheetEditEnd"; } get SheetEditEnded() { return "SheetEditEnded"; } get CellClicked() { return H.CellClicked; } get CellHover() { return H.CellHover; } get CellPointerDown() { return H.CellPointerDown; } get CellPointerUp() { return H.CellPointerUp; } get CellPointerMove() { return H.CellPointerMove; } get DragOver() { return "DragOver"; } get Drop() { return "Drop"; } get Scroll() { return "Scroll"; } get SelectionMoveStart() { return "SelectionMoveStart"; } get SelectionChanged() { return "SelectionChanged"; } get SelectionMoving() { return "SelectionMoving"; } get SelectionMoveEnd() { return "SelectionMoveEnd"; } get RowHeaderClick() { return "RowHeaderClick"; } get RowHeaderPointerDown() { return "RowHeaderPointerDown"; } get RowHeaderPointerUp() { return "RowHeaderPointerUp"; } get RowHeaderHover() { return "RowHeaderHover"; } get ColumnHeaderClick() { return "ColumnHeaderClick"; } get ColumnHeaderPointerDown() { return "ColumnHeaderPointerDown"; } get ColumnHeaderPointerUp() { return "ColumnHeaderPointerUp"; } get ColumnHeaderHover() { return "ColumnHeaderHover"; } get SheetSkeletonChanged() { return "SheetSkeletonChanged"; } get BeforeSheetZoomChange() { return "BeforeSheetZoomChange"; } get SheetZoomChanged() { return "SheetZoomChanged"; } } ve.extend(Oe); class Fe { get SHEET_VIEWPORT_KEY() { return ee; } } Se.extend(Fe); class ze extends se { getCell() { var d; const e = this._injector.get(v), r = this._injector.get(U), t = this._workbook.getUnitId(), i = this._worksheet.getSheetId(), o = e.getRenderById(t), s = (d = o == null ? void 0 : o.with(p).getSkeletonParam(i)) == null ? void 0 : d.skeleton; if (!s) throw r.error("[Facade]: `FRange.getCell` can only be called in current worksheet"), new Error("`FRange.getCell` can only be called in current worksheet"); return s.getCellWithCoordByIndex(this._range.startRow, this._range.startColumn); } getCellRect() { 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 }; return { ...o, toJSON: () => JSON.stringify(o) }; } generateHTML() { var t; const r = this._injector.get(te).generateCopyContent( this._workbook.getUnitId(), this._worksheet.getSheetId(), this._range ); return (t = r == null ? void 0 : r.html) != null ? t : ""; } attachPopup(e) { var s, d, h; e.direction = (s = e.direction) != null ? s : "horizontal", e.extraProps = (d = e.extraProps) != null ? d : {}, e.offset = (h = e.offset) != null ? h : [0, 0]; const { key: r, disposableCollection: t } = q(e, this._injector.get(G)), o = this._injector.get(N).attachPopupToCell( this._range.startRow, this._range.startColumn, { ...e, componentKey: r }, this.getUnitId(), this._worksheet.getSheetId() ); return o ? (t.add(o), t) : (t.dispose(), null); } attachAlertPopup(e) { const r = this._injector.get(He), t = { workbook: this._workbook, worksheet: this._worksheet, row: this._range.startRow, col: this._range.startColumn, unitId: this.getUnitId(), subUnitId: this._worksheet.getSheetId() }; return r.showAlert({ ...e, location: t }), { dispose: () => { r.removeAlert(e.key); } }; } /** * attachDOMPopup * @param popup * @returns {IDisposable} disposable let sheet = univerAPI.getActiveWorkbook().getActiveSheet(); let range = sheet.getRange(2, 2, 3, 3); univerAPI.getActiveWorkbook().setActiveRange(range); let disposable = range.attachDOMPopup({ componentKey: 'univer.sheet.single-dom-popup', extraProps: { alert: { type: 0, title: 'This is an Info', message: 'This is an info message' } }, }); */ attachRangePopup(e) { var s, d, h; e.direction = (s = e.direction) != null ? s : "top-center", e.extraProps = (d = e.extraProps) != null ? d : {}, e.offset = (h = e.offset) != null ? h : [0, 0]; const { key: r, disposableCollection: t } = q(e, this._injector.get(G)), o = this._injector.get(N).attachRangePopup( this._range, { ...e, componentKey: r }, this.getUnitId(), this._worksheet.getSheetId() ); return o ? (t.add(o), t) : (t.dispose(), null); } highlight(e, r) { const t = this._injector.get(re), i = t.addShape({ range: this._range, style: e, primary: r }); if (!i) throw new Error("Failed to highlight current range"); return m(() => { t.removeShape(i); }); } showDropdown(e) { return this._injector.get(Me).showDropdown(e); } async autoFill(e, r) { const t = this.getRange(), i = e.getRange(); if (!le.contains(i, t)) throw new Error("AutoFill target range must contain source range"); const { startRow: o, startColumn: s, endRow: d, endColumn: h } = t, { startRow: g, startColumn: l, endRow: a, endColumn: n } = i; if (d - o !== a - g && h - s !== n - l) throw new Error("AutoFill can only fill in one direction"); if (d - o === a - g && s !== l && h !== n) throw new Error("AutoFill can only fill in one direction"); if (h - s === n - l && o !== g && d !== a) throw new Error("AutoFill can only fill in one direction"); return this._commandService.executeCommand(ye.id, { sourceRange: t, targetRange: i, unitId: this.getUnitId(), subUnitId: this.getSheetId(), applyType: r }); } } se.extend(ze); function q(w, e) { const { componentKey: r, isVue3: t } = w; let i; const o = new j(); return typeof r == "string" ? i = r : (i = `External_${ue(6)}`, o.add(e.register(i, r, { framework: t ? "vue3" : "react" }))), { key: i, disposableCollection: o }; } export { Fe as FSheetsUIEnum, q as transformComponentKey };