UNPKG

@univerjs/docs-ui

Version:

Editor UI layer for Univer Docs.

200 lines (191 loc) • 7.9 kB
import { DOC_RANGE_TYPE, ICommandService, IResourceManagerService, IUniverInstanceService, Inject, Injector, RedoCommand, UndoCommand, UniverInstanceType } from "@univerjs/core"; import { FUniver } from "@univerjs/core/facade"; import { DocSelectionRenderService, InsertCommand } from "@univerjs/docs-ui"; import { IRenderManagerService } from "@univerjs/engine-render"; //#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) { return typeof o; } : function(o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } //#endregion //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js function toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } //#endregion //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js function toPropertyKey(t) { var i = toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } //#endregion //#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js function _defineProperty(e, r, t) { return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } //#endregion //#region \0@oxc-project+runtime@0.133.0/helpers/esm/decorateParam.js function __decorateParam(paramIndex, decorator) { return function(target, key) { decorator(target, key, paramIndex); }; } //#endregion //#region \0@oxc-project+runtime@0.133.0/helpers/esm/decorate.js function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } //#endregion //#region src/facade/f-document.ts let FDocument = class FDocument { constructor(_documentDataModel, _injector, _univerInstanceService, _commandService, _resourceManagerService, _renderManagerService) { this._documentDataModel = _documentDataModel; this._injector = _injector; this._univerInstanceService = _univerInstanceService; this._commandService = _commandService; this._resourceManagerService = _resourceManagerService; this._renderManagerService = _renderManagerService; _defineProperty(this, "id", void 0); this.id = this._documentDataModel.getUnitId(); } getId() { return this._documentDataModel.getUnitId(); } getName() { return this.getSnapshot().title || ""; } getSnapshot() { const resources = this._resourceManagerService.getResourcesByType(this.id, UniverInstanceType.UNIVER_DOC); const snapshot = this._documentDataModel.getSnapshot(); snapshot.resources = resources; return snapshot; } undo() { this._univerInstanceService.focusUnit(this.id); return this._commandService.executeCommand(UndoCommand.id); } redo() { this._univerInstanceService.focusUnit(this.id); return this._commandService.executeCommand(RedoCommand.id); } /** * Adds the specified text to the end of this text region. * @param text - The text to be added to the end of this text region. */ appendText(text) { const { body } = this.getSnapshot(); if (!body) throw new Error("The document body is empty"); const lastPosition = body.dataStream.length - 2; return this.insertText(text, { startOffset: lastPosition, endOffset: lastPosition, segmentId: "" }); } /** * Inserts text at the provided document range. Defaults to appending before the final section break. * @param text - The text to insert. * @param options - Optional target range, segment id, and cursor offset. */ insertText(text, options = {}) { var _options$startOffset, _options$endOffset, _options$segmentId; const unitId = this.id; const { body } = this.getSnapshot(); if (!body) throw new Error("The document body is empty"); const startOffset = (_options$startOffset = options.startOffset) !== null && _options$startOffset !== void 0 ? _options$startOffset : Math.max(0, body.dataStream.length - 2); const endOffset = (_options$endOffset = options.endOffset) !== null && _options$endOffset !== void 0 ? _options$endOffset : startOffset; const segmentId = (_options$segmentId = options.segmentId) !== null && _options$segmentId !== void 0 ? _options$segmentId : ""; const activeRange = { startOffset, endOffset, collapsed: startOffset === endOffset, segmentId }; return this._commandService.executeCommand(InsertCommand.id, { unitId, body: { dataStream: text }, range: activeRange, segmentId, ...options.cursorOffset == null ? {} : { cursorOffset: options.cursorOffset } }); } /** * Inserts one or more plain-text paragraphs at the provided document range. * @param text - Paragraph text. Newlines are normalized to document paragraph separators. * @param options - Optional target range, segment id, and cursor offset. */ insertParagraph(text = "", options = {}) { var _options$cursorOffset; const dataStream = `${text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n").join("\r\n")}\r\n`; return this.insertText(dataStream, { ...options, cursorOffset: (_options$cursorOffset = options.cursorOffset) !== null && _options$cursorOffset !== void 0 ? _options$cursorOffset : dataStream.length }); } /** * Sets the selection to a specified text range in the document. * @param startOffset - The starting offset of the selection in the document. * @param endOffset - The ending offset of the selection in the document. * @example * ```typescript * document.setSelection(10, 20); * ``` */ setSelection(startOffset, endOffset) { var _this$_renderManagerS; const docSelectionRenderService = (_this$_renderManagerS = this._renderManagerService.getRenderById(this.getId())) === null || _this$_renderManagerS === void 0 ? void 0 : _this$_renderManagerS.with(DocSelectionRenderService); docSelectionRenderService === null || docSelectionRenderService === void 0 || docSelectionRenderService.removeAllRanges(); docSelectionRenderService === null || docSelectionRenderService === void 0 || docSelectionRenderService.addDocRanges([{ startOffset, endOffset, rangeType: DOC_RANGE_TYPE.TEXT }], true); } }; FDocument = __decorate([ __decorateParam(1, Inject(Injector)), __decorateParam(2, IUniverInstanceService), __decorateParam(3, ICommandService), __decorateParam(4, IResourceManagerService), __decorateParam(5, IRenderManagerService) ], FDocument); //#endregion //#region src/facade/f-univer.ts var FUniverDocsUIMixin = class extends FUniver { createUniverDoc(data) { const document = this._univerInstanceService.createUnit(UniverInstanceType.UNIVER_DOC, data); return this._injector.createInstance(FDocument, document); } getActiveDocument() { const document = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_DOC); if (!document) return null; return this._injector.createInstance(FDocument, document); } getUniverDoc(id) { const document = this._univerInstanceService.getUniverDocInstance(id); if (!document) return null; return this._injector.createInstance(FDocument, document); } }; FUniver.extend(FUniverDocsUIMixin); //#endregion export { FDocument };