@univerjs/docs 1.0.0-alpha.0 → 1.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/facade.js +834 -1405
- package/lib/cjs/index.js +30 -1
- package/lib/es/facade.js +831 -1400
- package/lib/es/index.js +30 -2
- package/lib/facade.js +831 -1400
- package/lib/index.js +30 -2
- package/lib/types/facade/f-document-block-range.d.ts +132 -0
- package/lib/types/facade/f-document-body.d.ts +276 -0
- package/lib/types/facade/f-document-custom-block.d.ts +57 -0
- package/lib/types/facade/f-document-element.d.ts +193 -0
- package/lib/types/facade/f-document-paragraph.d.ts +231 -0
- package/lib/types/facade/f-document-table.d.ts +57 -0
- package/lib/types/facade/f-document.d.ts +12 -94
- package/lib/types/facade/index.d.ts +6 -8
- package/lib/types/facade/utils.d.ts +2 -4
- package/lib/types/index.d.ts +2 -0
- package/lib/types/services/doc-block-move-validator.service.d.ts +46 -0
- package/lib/umd/facade.js +1 -5
- package/lib/umd/index.js +1 -1
- package/package.json +4 -4
- package/lib/types/facade/doc-element-registry.d.ts +0 -84
- package/lib/types/facade/f-doc-block-range.d.ts +0 -139
- package/lib/types/facade/f-doc-body.d.ts +0 -535
- package/lib/types/facade/f-doc-custom-block.d.ts +0 -94
- package/lib/types/facade/f-doc-element.d.ts +0 -177
- package/lib/types/facade/f-doc-paragraph.d.ts +0 -194
- package/lib/types/facade/f-doc-table.d.ts +0 -94
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { IDisposable, IDocumentData } from '@univerjs/core';
|
|
17
|
+
import { Disposable } from '@univerjs/core';
|
|
18
|
+
export interface IDocBlockMoveValidationContext {
|
|
19
|
+
unitId: string;
|
|
20
|
+
sourceRange: {
|
|
21
|
+
startOffset: number;
|
|
22
|
+
endOffset: number;
|
|
23
|
+
};
|
|
24
|
+
targetOffset: number;
|
|
25
|
+
}
|
|
26
|
+
export type DocBlockMoveValidator = (context: IDocBlockMoveValidationContext) => boolean;
|
|
27
|
+
export interface IDocBlockMoveResult {
|
|
28
|
+
nextDocumentData: IDocumentData;
|
|
29
|
+
movedRange: {
|
|
30
|
+
startOffset: number;
|
|
31
|
+
endOffset: number;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface IDocBlockMoveTransformContext extends IDocBlockMoveValidationContext {
|
|
35
|
+
previousDocumentData: IDocumentData;
|
|
36
|
+
result: IDocBlockMoveResult;
|
|
37
|
+
}
|
|
38
|
+
export type DocBlockMoveTransformer = (context: IDocBlockMoveTransformContext) => IDocBlockMoveResult;
|
|
39
|
+
export declare class DocBlockMoveValidatorService extends Disposable {
|
|
40
|
+
private readonly _validators;
|
|
41
|
+
private readonly _transformers;
|
|
42
|
+
registerValidator(validator: DocBlockMoveValidator): IDisposable;
|
|
43
|
+
registerTransformer(transformer: DocBlockMoveTransformer): IDisposable;
|
|
44
|
+
canMoveBlock(context: IDocBlockMoveValidationContext): boolean;
|
|
45
|
+
transformMoveResult(context: IDocBlockMoveTransformContext): IDocBlockMoveResult;
|
|
46
|
+
}
|
package/lib/umd/facade.js
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/core/facade"),require("@univerjs/docs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/core/facade`,`@univerjs/docs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsFacade={},e.UniverCore,e.UniverCoreFacade,e.UniverDocs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i=`DocElementRegistry no longer tracks paragraph identity; use paragraphId.`;var a=class extends Error{constructor(e=`Doc element is stale`){super(e),this.name=`DocElementStaleError`}},o=class{getParagraphKey(e,t,n){throw Error(i)}resolveParagraphStartIndex(e,t){throw Error(i)}syncParagraph(e,t,n){throw Error(i)}markStale(e,t){throw Error(i)}beforeTextEdit(e,t,n,r){throw Error(i)}};function s(e){return e==null?e:JSON.parse(JSON.stringify(e))}function c(e){return e.replace(/\r\n/g,`\r`).replace(/\n/g,`\r`)}function l(e,t){let n=c(e);return t&&n.length>1&&n.startsWith(`\r`)?1:0}function u(e,t,n){let r=c(e.slice(0,t)).length;return Math.max(0,r-l(e,n))}function d(e,t){var n,r;let i=(n=e.paragraphs)==null?[]:n,a=(r=i.find(e=>e.startIndex>=t))==null?i[i.length-1]:r;return a==null?void 0:a.paragraphStyle}function f(e,n={}){let r=c(e).slice(l(e,n.removeLeadingParagraphBreak)),i={dataStream:r,customDecorations:[],customRanges:[],textRuns:[]},a=[],o=new Set;for(let e=0;e<r.length;e++)r[e]===`\r`&&a.push({startIndex:e,paragraphId:(0,t.createParagraphId)(o),...n.paragraphStyle==null?{}:{paragraphStyle:s(n.paragraphStyle)}});return a.length>0&&(i.paragraphs=a),i}var p=class{constructor(e,t){this._body=e,this._key=t}getType(){return`blockRange`}getKey(){return this._key}getParent(){return this._body}removeFromParent(){return this._body.removeBlockRange(this._key)}getBlockType(){return this._body.getBlockRange(this._key).blockType}getText(){return this._body.getBlockRangeText(this._key)}setText(e){return this._body.setBlockRangeText(this._key,e)}unwrap(){return this.removeFromParent()}},m=class{constructor(e,t){this._body=e,this._key=t}getType(){return`customBlock`}getKey(){return this._key}getParent(){return this._body}removeFromParent(){return this._body.removeCustomBlock(this._key)}getBlockId(){return this._body.getCustomBlock(this._key).blockId}},h=class{constructor(e,t){this._body=e,this._key=t}getType(){return`paragraph`}getKey(){return this._key}getId(){return this._key}getParent(){return this._body}removeFromParent(){return this._body.removeParagraph(this._key)}getText(){return this._body.getParagraphText(this._key)}setText(e){return this._body.setParagraphText(this._key,e)}appendText(e){return this._body.appendParagraphText(this._key,e)}getRange(){return this._body.getParagraphRange(this._key)}isListItem(){return this._body.isListParagraph(this._key)}isTask(){return this._body.isTaskParagraph(this._key)}setTaskChecked(e){return this._body.setTaskChecked(this._key,e)}},g=class{constructor(e,t){this._body=e,this._key=t}getType(){return`table`}getKey(){return this._key}getParent(){return this._body}removeFromParent(){return this._body.removeTable(this._key)}getTableId(){return this._body.getTable(this._key).tableId}},_=class{constructor(e,t,n){this._body=e,this._type=t,this._key=n}getType(){return this._type}getKey(){return this._key}getParent(){return this._body}getNextSibling(){return this._body.createSibling(this._type,this._key,1)}getPreviousSibling(){return this._body.createSibling(this._type,this._key,-1)}removeFromParent(){return this._type===`paragraph`?this._body.removeParagraph(this._key):this._type===`blockRange`?this._body.removeBlockRange(this._key):this._type===`table`?this._body.removeTable(this._key):this._body.removeCustomBlock(this._key)}asParagraph(){return this._assertType(`paragraph`),new h(this._body,this._key)}asTable(){return this._assertType(`table`),new g(this._body,this._key)}asBlockRange(){return this._assertType(`blockRange`),new p(this._body,this._key)}asCustomBlock(){return this._assertType(`customBlock`),new m(this._body,this._key)}_assertType(e){if(this._type!==e)throw TypeError(`Cannot cast ${this._type} to ${e}.`)}};function v(e){return typeof e==`object`&&!!e&&`getData`in e&&typeof e.getData==`function`}let y=`doc-facade`;var b=class{constructor(e,t,n,r=``){this._documentDataModel=e,this._commandService=t,this._segmentId=r}getNumChildren(){return this._getChildren().length}getChild(e){let t=this._getChildren()[e];if(!t)throw RangeError(`Child index ${e} is out of range.`);return this._createElement(t.type,t.key)}getChildIndex(e){let t=this.resolveElement(e.getType(),e.getKey()),n=this._getChildren().findIndex(e=>e.type===t.type&&e.key===t.key);if(n<0)throw Error(`Doc element is stale`);return n}insertText(e,t){return this._replaceBodyRange({startOffset:e,endOffset:e},f(t))}insertParagraph(e,t=``){var n;let r=this._getParagraphInsertOffset(e);if(!this._replaceBodyRange({startOffset:r,endOffset:r},f(`${t}\r`)))throw Error(`Failed to insert paragraph.`);let i=this._normalizeInsertedParagraphIndex(e),a=(n=this._getBody().paragraphs)==null?void 0:n[i];return new h(this,this._getParagraphId(a,i))}appendParagraph(e=``){var t,n;return this.insertParagraph((t=(n=this._getBody().paragraphs)==null?void 0:n.length)==null?0:t,e)}deleteRange(e){return this._replaceBodyRange(e,{dataStream:``})}replaceRange(e,t){let n;if(typeof t==`string`)n=f(t);else if(v(t)){var r;n=(r=t.getData().body)==null?{dataStream:``}:r}else{var i;n=(i=t.body)==null?{dataStream:``}:i}return this._replaceBodyRange(e,n)}setTextStyle(e,n){let r={dataStream:``,textRuns:[{st:0,ed:e.endOffset-e.startOffset,ts:n}]};return this._retainBodyRange(e,r,t.UpdateDocsAttributeType.COVER)}setParagraphStyle(e,n){let r=e instanceof _||e instanceof h?this.resolveParagraph(e.getKey()):this._findParagraphByRange(e),i={dataStream:``,paragraphs:[{...r.paragraph,startIndex:0,paragraphStyle:{...r.paragraph.paragraphStyle,...n}}]};return this._preserveExplicitParagraphIds(i),this._retainBodyRange({startOffset:r.endOffset,endOffset:r.endOffset+1},i,t.UpdateDocsAttributeType.REPLACE)}getParagraphText(e){let t=this.resolveParagraph(e);return this._getBody().dataStream.slice(t.startOffset,t.endOffset)}setParagraphText(e,t){let n=this.resolveParagraph(e);return this.replaceRange({startOffset:n.startOffset,endOffset:n.endOffset},t)}appendParagraphText(e,t){let n=this.resolveParagraph(e);return this.insertText(n.endOffset,t)}removeParagraph(e){let t=this.resolveParagraph(e);return this.deleteRange({startOffset:t.startOffset,endOffset:t.endOffset+1})}getParagraphRange(e){let t=this.resolveParagraph(e);return{startOffset:t.startOffset,endOffset:t.endOffset,segmentId:this._segmentId}}isListParagraph(e){return!!this.resolveParagraph(e).paragraph.bullet}isTaskParagraph(e){var n;let r=(n=this.resolveParagraph(e).paragraph.bullet)==null?void 0:n.listType;return r===t.PresetListType.CHECK_LIST||r===t.PresetListType.CHECK_LIST_CHECKED}setTaskChecked(e,n){let r=this.resolveParagraph(e),i=r.paragraph.bullet;if(!i||!this.isTaskParagraph(e))return!1;let a={dataStream:``,paragraphs:[{...r.paragraph,startIndex:0,bullet:{...i,listType:n?t.PresetListType.CHECK_LIST_CHECKED:t.PresetListType.CHECK_LIST}}]};return this._preserveExplicitParagraphIds(a),this._retainBodyRange({startOffset:r.endOffset,endOffset:r.endOffset+1},a,t.UpdateDocsAttributeType.REPLACE)}resolveParagraph(e){var t;let n=this._getBody(),r=((t=n.paragraphs)==null?[]:t).map((e,t)=>({paragraph:e,paragraphIndex:t})).filter(({paragraph:t})=>t.paragraphId===e);if(r.length!==1)throw new a(r.length>1?`Doc paragraph id "${e}" is duplicated.`:`Doc paragraph id "${e}" is stale.`);let{paragraph:i,paragraphIndex:o}=r[0];return{paragraph:i,paragraphIndex:o,startOffset:o>0?n.paragraphs[o-1].startIndex+1:0,endOffset:i.startIndex}}resolveElement(e,t){if(e===`paragraph`)return{type:e,key:t,position:this.resolveParagraph(t).startOffset,priority:3};let n=this._getChildren().find(n=>n.type===e&&n.key===t);if(!n)throw Error(`Doc element is stale`);return n}getBlockRange(e){var t;let n=(t=this._getBody().blockRanges)==null?void 0:t.find(t=>t.blockId===e);if(!n)throw Error(`Doc element is stale`);return n}getBlockRangeText(e){let t=this.getBlockRange(e);return this._getBody().dataStream.slice(t.startIndex,t.endIndex)}setBlockRangeText(e,t){let n=this.getBlockRange(e),r=f(`${t}\r`);return r.blockRanges=[{...n,startIndex:0,endIndex:t.length}],this.replaceRange({startOffset:n.startIndex,endOffset:n.endIndex+1},{body:r})}removeBlockRange(e){let t=this.getBlockRange(e);return this.deleteRange({startOffset:t.startIndex,endOffset:t.endIndex+1})}getTable(e){var t;let n=(t=this._getBody().tables)==null?void 0:t.find(t=>t.tableId===e);if(!n)throw Error(`Doc element is stale`);return n}removeTable(e){let t=this.getTable(e);return this.deleteRange({startOffset:t.startIndex,endOffset:t.endIndex+1})}getCustomBlock(e){var t;let n=(t=this._getBody().customBlocks)==null?void 0:t.find(t=>t.blockId===e);if(!n)throw Error(`Doc element is stale`);return n}removeCustomBlock(e){let t=this.getCustomBlock(e);return this.deleteRange({startOffset:t.startIndex,endOffset:t.startIndex+1})}createSibling(e,t,n){let r=this.getChildIndex(this._createElement(e,t)),i=this._getChildren()[r+n];return i?this._createElement(i.type,i.key):null}_getChildren(){var e,t,n,r,i;let a=this._getBody(),o=[];for(let n=0;n<((e=(t=a.paragraphs)==null?void 0:t.length)==null?0:e);n++){let e=a.paragraphs[n];o.push({type:`paragraph`,key:this._getParagraphId(e,n),position:n>0?a.paragraphs[n-1].startIndex+1:0,priority:3})}return(n=a.blockRanges)==null||n.forEach(e=>o.push({type:`blockRange`,key:e.blockId,position:e.startIndex,priority:0})),(r=a.tables)==null||r.forEach(e=>o.push({type:`table`,key:e.tableId,position:e.startIndex,priority:1})),(i=a.customBlocks)==null||i.forEach(e=>o.push({type:`customBlock`,key:e.blockId,position:e.startIndex,priority:2})),o.sort((e,t)=>e.position-t.position||e.priority-t.priority)}_createElement(e,t){return new _(this,e,t)}_getBody(){let e=this._documentDataModel.getSelfOrHeaderFooterModel(this._segmentId).getBody();if(!e)throw Error(`The document body is empty`);return e}_getParagraphInsertOffset(e){var t;let n=this._getBody();if(e<=0)return 0;let r=(t=n.paragraphs)==null?[]:t;return r.length===0?Math.max(0,n.dataStream.length-1):e>=r.length?r[r.length-1].startIndex+1:r[e-1].startIndex+1}_normalizeInsertedParagraphIndex(e){var t;let n=(t=this._getBody().paragraphs)==null?[]:t;return Math.max(0,Math.min(e,n.length-1))}_getParagraphId(e,t){if(!e)throw RangeError(`Paragraph index ${t} is out of range.`);if(!e.paragraphId)throw new a(`Paragraph at index ${t} is missing paragraphId.`);return e.paragraphId}_preserveExplicitParagraphIds(e){e.__textXRestoreParagraphIds=!0}_findParagraphByRange(e){var t;let n=(t=this._getBody().paragraphs)==null?[]:t,r=n.findIndex((t,r)=>(r>0?n[r-1].startIndex+1:0)<=e.startOffset&&e.endOffset<=t.startIndex);if(r<0)throw RangeError(`Range does not resolve to a paragraph.`);let i=n[r];return{paragraph:i,paragraphIndex:r,startOffset:r>0?n[r-1].startIndex+1:0,endOffset:i.startIndex}}_replaceBodyRange(e,n){let r=e.startOffset,i=e.endOffset,a=new t.TextX;return r>0&&a.push({t:t.TextXActionType.RETAIN,len:r}),i>r&&a.push({t:t.TextXActionType.DELETE,len:i-r}),n.dataStream.length>0&&a.push({t:t.TextXActionType.INSERT,body:n,len:n.dataStream.length}),this._executeTextX(a)}_retainBodyRange(e,n,r){var i;(i=n.textRuns)!=null&&i.length&&this._getBody().textRuns==null&&this._ensureTextRuns();let a=new t.TextX;return e.startOffset>0&&a.push({t:t.TextXActionType.RETAIN,len:e.startOffset}),a.push({t:t.TextXActionType.RETAIN,body:n,coverType:r,len:e.endOffset-e.startOffset}),this._executeTextX(a)}_ensureTextRuns(){if(this._getBody().textRuns!=null)return;let e=t.JSONX.getInstance().replaceOp([...(0,t.getRichTextEditPath)(this._documentDataModel,this._segmentId),`textRuns`],void 0,[]);this._commandService.syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:this._documentDataModel.getUnitId(),segmentId:this._segmentId,actions:e,textRanges:[],trigger:y,isEditing:!1})}_executeTextX(e){let n=t.JSONX.getInstance().editOp(e.serialize(),(0,t.getRichTextEditPath)(this._documentDataModel,this._segmentId));return!!this._commandService.syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:this._documentDataModel.getUnitId(),segmentId:this._segmentId,actions:n,textRanges:[],trigger:y,isEditing:!1})}};function x(e){"@babel/helpers - typeof";return x=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},x(e)}function S(e,t){if(x(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(x(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function C(e){var t=S(e,`string`);return x(t)==`symbol`?t:t+``}function w(e,t,n){return(t=C(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function T(e,t){return function(n,r){t(n,r,e)}}function E(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let D=class extends n.FBaseInitialable{constructor(e,t,n,r,i){super(t),this._documentDataModel=e,this._injector=t,this._univerInstanceService=n,this._resourceLoaderService=r,this._commandService=i,w(this,`id`,void 0),w(this,`_docElementRegistry`,new o),this.id=this._documentDataModel.getUnitId()}getDocumentDataModel(){return this._documentDataModel}getBody(){return new b(this._documentDataModel,this._commandService,this._docElementRegistry)}dispose(){super.dispose()}getId(){return this.id}getName(){return this._documentDataModel.getTitle()||``}save(){return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId())}undo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.UndoCommand.id)}redo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.RedoCommand.id)}appendText(e){let{body:t}=this.save();if(!t)throw Error(`The document body is empty`);let n=t.dataStream.length-2;return this.insertText(e,{startOffset:n,endOffset:n,segmentId:``})}insertText(e,t={}){var n,i,a;let o=this.id,{body:s}=this.save();if(!s)throw Error(`The document body is empty`);let c=(n=t.startOffset)==null?Math.max(0,s.dataStream.length-2):n,l=(i=t.endOffset)==null?c:i,p=(a=t.segmentId)==null?``:a,m={startOffset:c,endOffset:l,collapsed:c===l,segmentId:p},h=c===0,g=f(e,{paragraphStyle:d(s,c),removeLeadingParagraphBreak:h}),_=t.cursorOffset==null?void 0:u(e,t.cursorOffset,h);return this._commandService.syncExecuteCommand(r.InsertTextCommand.id,{unitId:o,body:g,range:m,segmentId:p,..._==null?{}:{cursorOffset:_}})}insertParagraph(e=``,t={}){var n;let r=`${e.replace(/\r\n/g,`
|
|
2
|
-
`).replace(/\r/g,`
|
|
3
|
-
`).split(`
|
|
4
|
-
`).join(`\r
|
|
5
|
-
`)}\r\n`;return this.insertText(r,{...t,cursorOffset:(n=t.cursorOffset)==null?r.length:n})}};D=E([T(1,(0,t.Inject)(t.Injector)),T(2,t.IUniverInstanceService),T(3,(0,t.Inject)(t.IResourceLoaderService)),T(4,t.ICommandService)],D);var O=class extends n.FUniver{createDocument(e){let n=this._injector.get(t.IUniverInstanceService).createUnit(t.UniverInstanceType.UNIVER_DOC,e);return this._injector.createInstance(D,n)}getActiveDocument(){let e=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC);return e?this._injector.createInstance(D,e):null}getDocument(e){let n=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_DOC);return n?this._injector.createInstance(D,n):null}};n.FUniver.extend(O),e.DocElementRegistry=o,e.DocElementStaleError=a,e.FDocBlockRange=p,e.FDocBody=b,e.FDocCustomBlock=m,e.FDocElement=_,e.FDocParagraph=h,e.FDocTable=g,Object.defineProperty(e,"FDocument",{enumerable:!0,get:function(){return D}})});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/core/facade"),require("@univerjs/docs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/core/facade`,`@univerjs/docs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsFacade={},e.UniverCore,e.UniverCoreFacade,e.UniverDocs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var i=class extends n.FBase{constructor(e,t,n,r){super(),this._body=e,this._bodyEdit=t,this._info=n,this._injector=r}getType(){return this._info.type}isParagraph(){return this._info.type===t.DocumentBlockType.PARAGRAPH}isTable(){return this._info.type===t.DocumentBlockType.TABLE}isBlockRange(){return this._info.type===t.DocumentBlockType.BLOCK_RANGE}isCustomBlock(){return this._info.type===t.DocumentBlockType.CUSTOM_BLOCK}getKey(){return this._info.key}getParent(){return this._body}getResolvedInfo(){return this._info}getNextSibling(){return this._createSibling(1)}getPreviousSibling(){return this._createSibling(-1)}getSibling(e){return this._createSibling(e)}remove(){return this.isParagraph()?this._body.removeParagraph(this.asParagraph()):this.isBlockRange()?this._body.removeBlockRange(this.asBlockRange()):this.isTable()?this._body.removeTable(this.asTable()):this._body.removeCustomBlock(this.asCustomBlock())}_createSibling(e){if(e===0)throw Error(`Offset cannot be zero.`);let t=this._body.getElementIndex(this);return this._body.getElement(t+e)}};function a(e){return e==null?e:JSON.parse(JSON.stringify(e))}function o(e){return e.replace(/\r\n/g,`\r`).replace(/\n/g,`\r`)}function s(e,t){let n=o(e);return t&&n.length>1&&n.startsWith(`\r`)?1:0}function c(e,n={}){let r=o(e).slice(s(e,n.removeLeadingParagraphBreak)),i={dataStream:r,customDecorations:[],customRanges:[],textRuns:[]},c=[],l=new Set;for(let e=0;e<r.length;e++)r[e]===`\r`&&c.push({startIndex:e,paragraphId:(0,t.createParagraphId)(l),...n.paragraphStyle==null?{}:{paragraphStyle:a(n.paragraphStyle)}});return c.length>0&&(i.paragraphs=c),i}var l=class extends i{constructor(e,n,r,i){if(super(e,n,r,i),this.body=e,this.bodyEdit=n,this.info=r,this.injector=i,this.getType()!==t.DocumentBlockType.PARAGRAPH)throw Error(`Element type is not a paragraph: ${this.getType()}`)}getParagraphId(){return this.getKey()}getResolvedParagraphInfo(){let{paragraphs:e=[]}=this._body.getBody(),t=e.map((e,t)=>({paragraph:e,paragraphIndex:t})).filter(({paragraph:e})=>e.paragraphId===this.getKey());if(t.length===0)throw Error(`Document paragraph with id ${this.getKey()} not found`);if(t.length>1)throw Error(`Multiple document paragraphs with id ${this.getKey()} found`);let{paragraph:n,paragraphIndex:r}=t[0];return{paragraph:n,paragraphIndex:r,startOffset:r>0?e[r-1].startIndex+1:0,endOffset:n.startIndex}}getRange(){let{startOffset:e,endOffset:t}=this.getResolvedParagraphInfo();return{startOffset:e,endOffset:t,segmentId:this._body.getSegmentId()}}getText(){let{dataStream:e}=this._body.getBody(),{startOffset:t,endOffset:n}=this.getResolvedParagraphInfo();return e.slice(t,n)}setText(e){let{startOffset:t,endOffset:n}=this.getResolvedParagraphInfo();return this._bodyEdit.replaceRange({startOffset:t,endOffset:n},c(e))}appendText(e){let{endOffset:t}=this.getResolvedParagraphInfo();return this._body.insertText(t,e)}setStyle(e){let{paragraph:n,endOffset:r}=this.getResolvedParagraphInfo(),i={dataStream:``,paragraphs:[{...n,startIndex:0,paragraphStyle:{...n.paragraphStyle,...e}}]};return this._preserveExplicitParagraphIds(i),this._bodyEdit.retainRange({startOffset:r,endOffset:r+1},i,t.UpdateDocsAttributeType.REPLACE)}isListItem(){let{paragraph:e}=this.getResolvedParagraphInfo();return!!e.bullet}isTask(){var e;let{paragraph:n}=this.getResolvedParagraphInfo(),r=(e=n.bullet)==null?void 0:e.listType;return r===t.PresetListType.CHECK_LIST||r===t.PresetListType.CHECK_LIST_CHECKED}setTaskChecked(e){if(!this.isTask())return!1;let{paragraph:n,endOffset:r}=this.getResolvedParagraphInfo(),i=n.bullet,a={dataStream:``,paragraphs:[{...n,startIndex:0,bullet:{...i,listType:e?t.PresetListType.CHECK_LIST_CHECKED:t.PresetListType.CHECK_LIST}}]};return this._preserveExplicitParagraphIds(a),this._bodyEdit.retainRange({startOffset:r,endOffset:r+1},a,t.UpdateDocsAttributeType.REPLACE)}_preserveExplicitParagraphIds(e){e[t.RESTORE_INSERTED_PARAGRAPH_IDS]=!0}},u=class extends i{asParagraph(){if(this.getType()!==t.DocumentBlockType.PARAGRAPH)throw Error(`Element type is not a paragraph: ${this.getType()}`);return this._injector.createInstance(l,this._body,this._bodyEdit,this.getResolvedInfo(),this._injector)}};i.extend(u);function d(e){"@babel/helpers - typeof";return d=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},d(e)}function f(e,t){if(d(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(d(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function p(e){var t=f(e,`string`);return d(t)==`symbol`?t:t+``}function m(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var h=class{constructor(e,t,n=``){this._documentDataModel=e,this._injector=t,this._segmentId=n,m(this,`_bodyEdit`,void 0),this._bodyEdit={replaceRange:this._replaceBodyRange.bind(this),retainRange:this._retainBodyRange.bind(this)}}getSegmentId(){return this._segmentId}getBody(){let e=this._documentDataModel.getSelfOrHeaderFooterModel(this._segmentId).getBody();if(!e)throw Error(`The document body is empty`);return e}getElements(){return this._getChildren().map(e=>this._injector.createInstance(i,this,this._bodyEdit,e,this._injector))}getElement(e){var t;return(t=this.getElements()[e])==null?null:t}getElementIndex(e){let{type:t,key:n}=e.getResolvedInfo(),r=this._getChildren().findIndex(e=>e.type===t&&e.key===n);if(r<0)throw Error(`Doc element is stale`);return r}insertText(e,t){return this._replaceBodyRange({startOffset:e,endOffset:e},c(t))}setTextStyle(e,n){let r={dataStream:``,textRuns:[{st:0,ed:e.endOffset-e.startOffset,ts:n}]};return this._retainBodyRange(e,r,t.UpdateDocsAttributeType.COVER)}insertParagraph(e,t=``){var n;let r=this._getParagraphInsertOffset(e);if(!this._replaceBodyRange({startOffset:r,endOffset:r},c(`${t}\r`)))throw Error(`Failed to insert paragraph.`);let{paragraphs:i=[]}=this.getBody(),a=i[e];if(!a)throw Error(`Failed to insert paragraph.`);let o=this._resolveParagraphInfo(a,e,(n=i[e-1])==null?void 0:n.startIndex);return this._injector.createInstance(l,this,this._bodyEdit,o,this._injector)}appendParagraph(e=``){let{paragraphs:t=[]}=this.getBody();return this.insertParagraph(t.length,e)}deleteRange(e){return this._replaceBodyRange(e,{dataStream:``})}removeParagraph(e){let{startOffset:t,endOffset:n}=e.getResolvedParagraphInfo();return this.deleteRange({startOffset:t,endOffset:n+1})}removeBlockRange(e){let{startIndex:t,endIndex:n}=e.getBlockRange();return this.deleteRange({startOffset:t,endOffset:n+1})}removeTable(e){let{startIndex:t,endIndex:n}=e.getTable();return this.deleteRange({startOffset:t,endOffset:n+1})}removeCustomBlock(e){let{startIndex:t}=e.getCustomBlock();return this.deleteRange({startOffset:t,endOffset:t+1})}resolveElement(e){let{type:t,key:n}=e.getResolvedInfo(),r=this._getChildren().find(e=>e.type===t&&e.key===n);if(!r)throw Error(`Doc element is stale`);return r}_getChildren(){let{paragraphs:e,blockRanges:n,tables:r,customBlocks:i}=this.getBody(),a=[];if(e)for(let t=0;t<e.length;t++){var o;let n=e[t],r=this._resolveParagraphInfo(n,t,(o=e[t-1])==null?void 0:o.startIndex);a.push(r)}if(n)for(let e=0;e<n.length;e++){let r=n[e];a.push({type:t.DocumentBlockType.BLOCK_RANGE,key:r.blockId,position:r.startIndex,priority:0})}if(r)for(let e=0;e<r.length;e++){let n=r[e];a.push({type:t.DocumentBlockType.TABLE,key:n.tableId,position:n.startIndex,priority:1})}if(i)for(let e=0;e<i.length;e++){let n=i[e];a.push({type:t.DocumentBlockType.CUSTOM_BLOCK,key:n.blockId,position:n.startIndex,priority:2})}return a.sort((e,t)=>e.position-t.position||e.priority-t.priority)}_resolveParagraphInfo(e,n,r){return{type:t.DocumentBlockType.PARAGRAPH,key:this._getParagraphId(e,n),position:n>0?r+1:0,priority:3}}_getParagraphId(e,t){if(!e)throw Error(`Paragraph index ${t} is out of range.`);if(!e.paragraphId)throw Error(`Paragraph at index ${t} is missing paragraphId.`);return e.paragraphId}_getParagraphInsertOffset(e){if(e<=0)return 0;let{dataStream:t,paragraphs:n=[]}=this.getBody();return n.length===0?Math.max(0,t.length-1):e>=n.length?n[n.length-1].startIndex+1:n[e-1].startIndex+1}_replaceBodyRange(e,n){let{startOffset:r,endOffset:i}=e,a=new t.TextX;return r>0&&a.push({t:t.TextXActionType.RETAIN,len:r}),i>r&&a.push({t:t.TextXActionType.DELETE,len:i-r}),n.dataStream.length>0&&a.push({t:t.TextXActionType.INSERT,body:n,len:n.dataStream.length}),this._executeTextX(a)}_retainBodyRange(e,n,r){var i;(i=n.textRuns)!=null&&i.length&&this.getBody().textRuns==null&&this._ensureTextRuns();let a=new t.TextX;return e.startOffset>0&&a.push({t:t.TextXActionType.RETAIN,len:e.startOffset}),a.push({t:t.TextXActionType.RETAIN,body:n,coverType:r,len:e.endOffset-e.startOffset}),this._executeTextX(a)}_ensureTextRuns(){let e=t.JSONX.getInstance().replaceOp([...(0,t.getRichTextEditPath)(this._documentDataModel,this._segmentId),`textRuns`],void 0,[]);this._injector.get(t.ICommandService).syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:this._documentDataModel.getUnitId(),segmentId:this._segmentId,actions:e,textRanges:[],isEditing:!1})}_executeTextX(e){let n=t.JSONX.getInstance().editOp(e.serialize(),(0,t.getRichTextEditPath)(this._documentDataModel,this._segmentId));return this._injector.get(t.ICommandService).syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:this._documentDataModel.getUnitId(),segmentId:this._segmentId,actions:n,textRanges:[],isEditing:!1})!==!1}};function g(e,t){return function(n,r){t(n,r,e)}}function _(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let v=class extends n.FBaseInitialable{constructor(e,t,n,r,i){super(t),this._documentDataModel=e,this._injector=t,this._univerInstanceService=n,this._resourceLoaderService=r,this._commandService=i,m(this,`id`,void 0),this.id=this._documentDataModel.getUnitId()}getDocumentDataModel(){return this._documentDataModel}getBody(){return this._injector.createInstance(h,this._documentDataModel,this._injector)}dispose(){super.dispose()}getId(){return this.id}getName(){return this._documentDataModel.getTitle()||``}save(){return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId())}undo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.UndoCommand.id)}redo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.RedoCommand.id)}};v=_([g(1,(0,t.Inject)(t.Injector)),g(2,t.IUniverInstanceService),g(3,(0,t.Inject)(t.IResourceLoaderService)),g(4,t.ICommandService)],v);var y=class extends n.FUniver{createDocument(e){let n=this._injector.get(t.IUniverInstanceService).createUnit(t.UniverInstanceType.UNIVER_DOC,e);return this._injector.createInstance(v,n)}getActiveDocument(){let e=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC);return e?this._injector.createInstance(v,e):null}getDocument(e){let n=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_DOC);return n?this._injector.createInstance(v,n):null}};n.FUniver.extend(y);var b=class extends i{constructor(e,n,r,i){if(super(e,n,r,i),this.body=e,this.bodyEdit=n,this.info=r,this.injector=i,this.getType()!==t.DocumentBlockType.BLOCK_RANGE)throw Error(`Element type is not a block range: ${this.getType()}`)}getBlockRange(){let{blockRanges:e=[]}=this._body.getBody(),t=e.find(e=>e.blockId===this.getKey());if(!t)throw Error(`Block range not found: ${this.getKey()}`);return t}getBlockType(){return this.getBlockRange().blockType}getText(){let{dataStream:e}=this._body.getBody(),{startIndex:t,endIndex:n}=this.getBlockRange();return e.slice(t,n)}setText(e){let t=this.getBlockRange(),{startIndex:n,endIndex:r}=t,i=c(`${e}\r`);return i.blockRanges=[{...t,startIndex:0,endIndex:e.length}],this._bodyEdit.replaceRange({startOffset:n,endOffset:r+1},i)}unwrap(){return this.remove()}},x=class extends i{asBlockRange(){if(this.getType()!==t.DocumentBlockType.BLOCK_RANGE)throw Error(`Element type is not a block range: ${this.getType()}`);return this._injector.createInstance(b,this._body,this._bodyEdit,this.getResolvedInfo(),this._injector)}};i.extend(x);var S=class extends i{constructor(e,n,r,i){if(super(e,n,r,i),this.body=e,this.bodyEdit=n,this.info=r,this.injector=i,this.getType()!==t.DocumentBlockType.CUSTOM_BLOCK)throw Error(`Element type is not a custom block: ${this.getType()}`)}getCustomBlock(){let{customBlocks:e=[]}=this._body.getBody(),t=e.find(e=>e.blockId===this.getKey());if(!t)throw Error(`Doc custom block is stale`);return t}},C=class extends i{asCustomBlock(){if(this.getType()!==t.DocumentBlockType.CUSTOM_BLOCK)throw Error(`Element type is not a custom block: ${this.getType()}`);return this._injector.createInstance(S,this._body,this._bodyEdit,this.getResolvedInfo(),this._injector)}};i.extend(C);var w=class extends i{constructor(e,n,r,i){if(super(e,n,r,i),this.body=e,this.bodyEdit=n,this.info=r,this.injector=i,this.getType()!==t.DocumentBlockType.TABLE)throw Error(`Element type is not a table: ${this.getType()}`)}getTable(){let{tables:e=[]}=this._body.getBody(),t=e.find(e=>e.tableId===this.getKey());if(!t)throw Error(`Doc table is stale`);return t}},T=class extends i{asTable(){if(this.getType()!==t.DocumentBlockType.TABLE)throw Error(`Element type is not a table: ${this.getType()}`);return this._injector.createInstance(w,this._body,this._bodyEdit,this.getResolvedInfo(),this._injector)}};i.extend(T),Object.defineProperty(e,"FDocument",{enumerable:!0,get:function(){return v}}),e.FDocumentBlockRange=b,e.FDocumentBody=h,e.FDocumentCustomBlock=S,e.FDocumentElement=i,e.FDocumentParagraph=l,e.FDocumentTable=w});
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/engine-render"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/engine-render`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocs={},e.UniverCore,e.UniverEngineRender,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i={id:`doc.operation.set-selections`,type:t.CommandType.OPERATION,handler:()=>!0};function a(e){"@babel/helpers - typeof";return a=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},a(e)}function o(e,t){if(a(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(a(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function s(e){var t=o(e,`string`);return a(t)==`symbol`?t:t+``}function c(e,t,n){return(t=s(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){return function(n,r){t(n,r,e)}}function u(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let d=class extends t.RxDisposable{constructor(e,t){super(),this._commandService=e,this._univerInstanceService=t,c(this,`_currentSelection`,null),c(this,`_textSelectionInfo`,new Map),c(this,`_textSelection$`,new r.Subject),c(this,`textSelection$`,this._textSelection$.asObservable()),c(this,`_refreshSelection$`,new r.BehaviorSubject(null)),c(this,`refreshSelection$`,this._refreshSelection$.asObservable()),this._listenCurrentUnit()}_listenCurrentUnit(){this._univerInstanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_DOC).pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{if(e==null)return;let t=e.getUnitId();this._setCurrentSelectionNotRefresh({unitId:t,subUnitId:t})})}__getCurrentSelection(){return this._currentSelection}getSelectionInfo(e=this._currentSelection){return this._getTextRanges(e)}refreshSelection(e=this._currentSelection){e!=null&&this._refresh(e)}__TEST_ONLY_setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}getTextRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.textRanges}getRectRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.rectRanges}getDocRanges(e=this._currentSelection){var t,n;let r=(t=this.getTextRanges(e))==null?[]:t,i=(n=this.getRectRanges(e))==null?[]:n;return[...r,...i].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0)}getActiveTextRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{textRanges:t}=e;return t.find(e=>e.isActive)}getActiveRectRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{rectRanges:t}=e;return t.find(e=>e.isActive)}__TEST_ONLY_add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,rectRanges:[],segmentId:``,segmentPage:-1,isEditing:t,style:n.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0,n){return this.replaceDocRanges(e,this._currentSelection,t,n)}replaceDocRanges(e,t=this._currentSelection,n=!0,r){if(t==null)return;let{unitId:i,subUnitId:a}=t;this._refreshSelection$.next({unitId:i,subUnitId:a,docRanges:e,isEditing:n,options:r})}__replaceTextRangesWithNoRefresh(e,t){if(this._currentSelection==null)return;let n={...e,...t};this._replaceByParam(n),this._textSelection$.next(n);let{unitId:r,subUnitId:a,segmentId:o,style:s,textRanges:c,rectRanges:l,isEditing:u}=n,d=[...c,...l].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0);this._commandService.executeCommand(i.id,{unitId:r,subUnitId:a,segmentId:o,style:s,isEditing:u,ranges:d})}dispose(){this._textSelection$.complete(),this._refreshSelection$.complete()}_setCurrentSelectionNotRefresh(e){this._currentSelection=e}_getTextRanges(e){var t;if(e==null)return;let{unitId:n,subUnitId:r=``}=e;return(t=this._textSelectionInfo.get(n))==null?void 0:t.get(r)}_refresh(e){let t=this._getTextRanges(e);if(t==null)return;let{textRanges:n,rectRanges:r}=t,i=[...n,...r],{unitId:a,subUnitId:o}=e;this._refreshSelection$.next({unitId:a,subUnitId:o,docRanges:i,isEditing:!1})}_replaceByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,{...r})}_addByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);let i=this._textSelectionInfo.get(t);i.has(n)?i.get(n).textRanges.push(...e.textRanges):i.set(n,{...r})}};d=u([l(0,t.ICommandService),l(1,t.IUniverInstanceService)],d);let f=class extends t.RxDisposable{constructor(e,n,i){super(),this._context=e,this._localeService=n,this._univerInstanceService=i,c(this,`_skeleton`,void 0),c(this,`_docViewModel`,void 0),c(this,`_currentSkeleton$`,new r.BehaviorSubject(null)),c(this,`currentSkeleton$`,this._currentSkeleton$.asObservable()),c(this,`_currentSkeletonBefore$`,new r.BehaviorSubject(null)),c(this,`currentSkeletonBefore$`,this._currentSkeletonBefore$.asObservable()),c(this,`_currentViewModel$`,new r.BehaviorSubject(null)),c(this,`currentViewModel$`,this._currentViewModel$.asObservable()),this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_DOC).pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{e&&e.getUnitId()===this._context.unitId&&this._update(e)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){let e=this._context.unit;this._update(e)}_update(e){let n=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&(0,t.isInternalEditorID)(n)?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));let r=this._skeleton;r.calculate(),this._currentSkeletonBefore$.next(r),this._currentSkeleton$.next(r),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return n.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new n.DocumentViewModel(e)}};f=u([l(1,(0,t.Inject)(t.LocaleService)),l(2,t.IUniverInstanceService)],f);var p=class extends t.RxDisposable{constructor(){super(),c(this,`_docStateChangeParams$`,new r.BehaviorSubject(null)),c(this,`docStateChangeParams$`,this._docStateChangeParams$.asObservable())}emitStateChangeInfo(e){this._docStateChangeParams$.next(e)}dispose(){super.dispose(),this._docStateChangeParams$.complete()}};let m=`doc.mutation.rich-text-editing`,h={id:m,type:t.CommandType.MUTATION,handler:(e,r,i)=>{var a,o;let{unitId:s,segmentId:c=``,actions:l,textRanges:u,prevTextRanges:h,trigger:g,noHistory:_,isCompositionEnd:v,noNeedSetTextRange:y,debounce:b,isEditing:x=!0,isSync:S,syncer:C}=r,w=S||(i==null?void 0:i.fromCollab)||(i==null?void 0:i.fromChangeset),T=e.get(t.IUniverInstanceService),E=e.get(n.IRenderManagerService),D=e.get(p),O=T.getUnit(s,t.UniverInstanceType.UNIVER_DOC),k=(a=E.getRenderUnitById(s))==null?void 0:a.with(f).getViewModel();if(O==null)throw Error(`DocumentDataModel not found for unitId: ${s}`);let A=e.get(d),j=(o=A.getDocRanges())==null?[]:o,M=!!O.getSnapshot().disabled;if(t.JSONX.isNoop(l)||l&&l.length===0||M)return{unitId:s,actions:[],textRanges:j};let N=t.JSONX.invertWithDoc(l,O.getSnapshot());O.apply(l),k==null||k.reset(O),!y&&u&&g!=null&&!w&&queueMicrotask(()=>{A.replaceDocRanges(u,{unitId:s,subUnitId:s},x,r.options)});let P={commandId:m,unitId:s,segmentId:c,trigger:g,noHistory:_,debounce:b,redoState:{actions:l,textRanges:u},undoState:{actions:N,textRanges:h==null?j:h},isCompositionEnd:v,isSync:w,syncer:C};return D.emitStateChangeInfo(P),{unitId:s,actions:N,textRanges:j}}},g={id:`doc.command.insert-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{var r;let i=e.get(t.ICommandService),{range:a,segmentId:o,body:s,unitId:c,cursorOffset:l}=n,u=e.get(d),f=e.get(t.IUniverInstanceService).getUnit(c,t.UniverInstanceType.UNIVER_DOC);if(f==null)return!1;let p=u.getActiveTextRange(),m=f.getSelfOrHeaderFooterModel((r=p==null?void 0:p.segmentId)==null?``:r).getBody();if(m==null)return!1;let{startOffset:g,collapsed:_}=a,v=l==null?s.dataStream.length:l,y=[{startOffset:g+v,endOffset:g+v,style:p==null?void 0:p.style,collapsed:_}],b={id:h.id,params:{unitId:c,actions:[],textRanges:y,debounce:!0}},x=new t.TextX,S=t.JSONX.getInstance();if(_)g>0&&x.push({t:t.TextXActionType.RETAIN,len:g}),x.push({t:t.TextXActionType.INSERT,body:s,len:s.dataStream.length});else{let e=t.BuildTextUtils.selection.delete([a],m,0,s);x.push(...e)}b.params.textRanges=[{startOffset:g+v,endOffset:g+v,collapsed:_}];let C=(0,t.getRichTextEditPath)(f,o);return b.params.actions=S.editOp(x.serialize(),C),!!i.syncExecuteCommand(b.id,b.params)}},_={id:`doc.command.delete-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{var r;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),{range:o,segmentId:s,unitId:c,direction:l,len:u=1}=n,d=a.getUnit(c,t.UniverInstanceType.UNIVER_DOC),f=d==null?void 0:d.getSelfOrHeaderFooterModel(s).getBody();if(d==null||f==null)return!1;let{startOffset:p}=o,m=l===t.DeleteDirection.LEFT?p-u:p,g=l===t.DeleteDirection.LEFT?p-1:p+u-1,_=(r=f.customRanges)==null?void 0:r.find(e=>e.startIndex<=m&&e.endIndex>=g);_!=null&&_.wholeEntity&&(m=_.startIndex,g=Math.max(g,_.endIndex));let v={id:h.id,params:{unitId:c,actions:[],textRanges:[{startOffset:m,endOffset:m,collapsed:!0}],debounce:!0}},y=new t.TextX,b=t.JSONX.getInstance();y.push({t:t.TextXActionType.RETAIN,len:m-0}),y.push({t:t.TextXActionType.DELETE,len:g-m+1});let x=(0,t.getRichTextEditPath)(d,s);return v.params.actions=b.editOp(y.serialize(),x),!!i.syncExecuteCommand(v.id,v.params)}},v={id:`doc.command.update-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{let{range:r,segmentId:i,updateBody:a,coverType:o,unitId:s,textRanges:c}=n,l=e.get(t.ICommandService),u=e.get(t.IUniverInstanceService).getCurrentUniverDocInstance();if(u==null)return!1;let d={id:h.id,params:{unitId:s,actions:[],textRanges:c}},f=new t.TextX,p=t.JSONX.getInstance(),{startOffset:m,endOffset:g}=r;f.push({t:t.TextXActionType.RETAIN,len:m}),f.push({t:t.TextXActionType.RETAIN,body:a,len:g-m,coverType:o});let _=(0,t.getRichTextEditPath)(u,i);return d.params.actions=p.editOp(f.serialize(),_),!!l.syncExecuteCommand(d.id,d.params)}};var y=`@univerjs/docs`,b=`1.0.0-alpha.0`;let x={id:`doc.mutation.rename-doc`,type:t.CommandType.MUTATION,handler:(e,n)=>{let r=e.get(t.IUniverInstanceService).getUnit(n.unitId,t.UniverInstanceType.UNIVER_DOC);return r?(r.setName(n.name),!0):!1}},S={},C=class extends t.Disposable{constructor(e,t,n){super(),this._commandService=e,this._textSelectionManagerService=t,this._univerInstanceService=n,this._initSelectionChange()}_transformCustomRange(e,n){var r;let{startOffset:i,endOffset:a,collapsed:o}=n,s=(r=e.getCustomRanges())==null?void 0:r.filter(e=>!e.wholeEntity||i<=e.startIndex&&a>e.endIndex?!1:o?e.startIndex<i&&e.endIndex>=a:t.BuildTextUtils.range.isIntersects(i,a-1,e.startIndex,e.endIndex));if(s!=null&&s.length){let e=i,t=a;return s.forEach(n=>{e=Math.min(n.startIndex,e),t=Math.max(n.endIndex+1,t)}),{...n,startOffset:e,endOffset:t,collapsed:e===t}}return n}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.id){let{unitId:t,ranges:n,isEditing:r}=e.params,i=this._univerInstanceService.getUnit(t);if(!i)return;let a=n.map(e=>this._transformCustomRange(i,e));a.some((e,t)=>n[t]!==e)&&this._textSelectionManagerService.replaceTextRanges(a,r)}}))}};C=u([l(0,t.ICommandService),l(1,(0,t.Inject)(d)),l(2,t.IUniverInstanceService)],C);var w=class extends t.Disposable{constructor(...e){super(...e),c(this,`_range`,null)}setInsertRange(e){this._range=e}consumeInsertRange(e){if(!this._range||e&&this._range.unitId!==e)return null;let t=this._range;return this._range=null,t}clearInsertRange(){this._range=null}};let T=(0,t.createIdentifier)(`doc.state-change-interceptor-service`),E=class extends t.RxDisposable{constructor(e,t,n,i,a){super(),this._undoRedoService=e,this._commandService=t,this._univerInstanceService=n,this._docStateEmitService=i,this._docStateChangeInterceptorService=a,c(this,`_docStateChange$`,new r.BehaviorSubject(null)),c(this,`docStateChange$`,this._docStateChange$.asObservable()),c(this,`_historyStateCache`,new Map),c(this,`_changeStateCache`,new Map),c(this,`_historyTimer`,null),c(this,`_changeStateCacheTimer`,null),this._initialize(),this._listenDocStateChange()}getStateCache(e){var t,n;return{history:(t=this._historyStateCache.get(e))==null?[]:t,collaboration:(n=this._changeStateCache.get(e))==null?[]:n}}setStateCache(e,t){this._historyStateCache.set(e,t.history),this._changeStateCache.set(e,t.collaboration)}_setChangeState(e){this._cacheChangeState(e,`history`),this._cacheChangeState(e,`collaboration`)}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===t.UndoCommandId||e.id===t.RedoCommandId){let e=this._univerInstanceService.getCurrentUniverDocInstance();if(e==null)return;let t=e.getUnitId();this._pushHistory(t),this._emitChangeState(t)}}))}_listenDocStateChange(){this._docStateEmitService.docStateChangeParams$.pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{var t,n;if(e==null)return;let r=(t=(n=this._docStateChangeInterceptorService)==null?void 0:n.transformChangeStateInfo(e))==null?e:t;if(r==null||r.isSync)return;let{isCompositionEnd:i,isSync:a,syncer:o,...s}=r;this._setChangeState(s)})}_cacheChangeState(e,n=`history`){let{trigger:r,unitId:i,noHistory:a,debounce:o=!1}=e;if(a||n===`history`&&r==null||n===`history`&&(r===t.RedoCommandId||r===t.UndoCommandId))return;let s=n===`history`?this._historyStateCache:this._changeStateCache,c=n===`history`?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(s.has(i)){let t=s.get(i);t==null||t.push(e)}else s.set(i,[e]);o?n===`history`?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{c(i)},300)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{c(i)},300)):c(i)}_pushHistory(e){let n=this._undoRedoService,r=this._historyStateCache.get(e);if(n==null||!Array.isArray(r)||r.length===0)return;let i=r.length,a=r[0].commandId,o=r[0],s=r[i-1],c={unitId:e,actions:r.reduce((e,n)=>t.JSONX.compose(e,n.redoState.actions),null),textRanges:s.redoState.textRanges},l={unitId:e,actions:r.reverse().reduce((e,n)=>t.JSONX.compose(e,n.undoState.actions),null),textRanges:o.undoState.textRanges};n.pushUndoRedo({unitID:e,undoMutations:[{id:a,params:l}],redoMutations:[{id:a,params:c}]}),r.length=0}_emitChangeState(e){let n=this._changeStateCache.get(e);if(!Array.isArray(n)||n.length===0)return;let r=n.length,{commandId:i,trigger:a,segmentId:o,noHistory:s,debounce:c}=n[0],l=n[0],u=n[r-1],d={commandId:i,unitId:e,trigger:a,redoState:{unitId:e,actions:n.reduce((e,n)=>t.JSONX.compose(e,n.redoState.actions),null),textRanges:u.redoState.textRanges},undoState:{unitId:e,actions:n.reverse().reduce((e,n)=>t.JSONX.compose(e,n.undoState.actions),null),textRanges:l.undoState.textRanges},segmentId:o,noHistory:s,debounce:c};n.length=0,this._docStateChange$.next(d)}};E=u([l(0,(0,t.Optional)(t.IUndoRedoService)),l(1,t.ICommandService),l(2,t.IUniverInstanceService),l(3,(0,t.Inject)(p)),l(4,(0,t.Optional)(T))],E);let D=class extends t.Plugin{constructor(e=S,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},S,this._config);this._configService.setConfig(`docs.config`,i)}onStarting(){this._initializeDependencies(),this._initializeCommands()}_initializeCommands(){[g,_,v,h,x,i].forEach(e=>{this._injector.get(t.ICommandService).registerCommand(e)})}_initializeDependencies(){[[d],[p],[E],[w],[C]].forEach(e=>this._injector.add(e))}onReady(){this._injector.get(E),this._injector.get(C)}};c(D,`pluginName`,`DOCS_PLUGIN`),c(D,`packageName`,y),c(D,`version`,b),D=u([l(1,(0,t.Inject)(t.Injector)),l(2,t.IConfigService)],D);let O={CUSTOM_RANGE:(0,t.createInterceptorKey)(`CUSTOM_RANGE`),CUSTOM_DECORATION:(0,t.createInterceptorKey)(`CUSTOM_DECORATION`)},k=class extends t.Disposable{constructor(e,n){super(),this._context=e,this._docSkeletonManagerService=n,c(this,`_interceptorsByName`,new Map);let r=this._docSkeletonManagerService.getViewModel(),i=r.getDataModel().getUnitId();if(i===t.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||i===t.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.disposeWithMe(this.interceptDocumentViewModel(r)),this.disposeWithMe(this.intercept(O.CUSTOM_RANGE,{priority:-1,handler:(e,t,n)=>n(e)}));let a=new t.DisposableCollection;r.segmentViewModels$.subscribe(e=>{a.dispose(),a=new t.DisposableCollection,e.forEach(e=>{a.add(this.interceptDocumentViewModel(e))})}),this.disposeWithMe(a)}intercept(e,n){let r=e;this._interceptorsByName.has(r)||this._interceptorsByName.set(r,[]);let i=this._interceptorsByName.get(r);return i.push(n),this._interceptorsByName.set(r,i.sort((e,t)=>{var n,r;return((n=t.priority)==null?0:n)-((r=e.priority)==null?0:r)})),this.disposeWithMe((0,t.toDisposable)(()=>(0,t.remove)(this._interceptorsByName.get(r),n)))}fetchThroughInterceptors(e){let n=e;return(0,t.composeInterceptors)(this._interceptorsByName.get(n)||[])}interceptDocumentViewModel(e){let n=new t.DisposableCollection;return n.add(e.registerCustomRangeInterceptor({getCustomRange:t=>{var n;return this.fetchThroughInterceptors(O.CUSTOM_RANGE)(e.getCustomRangeRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customRanges:(n=e.getDataModel().getCustomRanges())==null?[]:n})},getCustomDecoration:t=>{var n;return this.fetchThroughInterceptors(O.CUSTOM_DECORATION)(e.getCustomDecorationRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customDecorations:(n=e.getDataModel().getCustomDecorations())==null?[]:n})}})),n}};k=u([l(1,(0,t.Inject)(f))],k);function A(e,n,r){let{unitId:i,segmentId:a}=n,o=e.get(t.IUniverInstanceService).getUnit(i);if(!o)return!1;let s={id:h.id,params:{unitId:n.unitId,actions:[],textRanges:void 0}},c=t.JSONX.getInstance(),l=t.BuildTextUtils.customRange.add({...n,body:r});if(!l)return!1;let u=(0,t.getRichTextEditPath)(o,a);return s.params.actions=c.editOp(l.serialize(),u),s}function j(e,n){var r;let{rangeId:i,rangeType:a,wholeEntity:o,properties:s,unitId:c,selections:l}=n,u=e.get(d),f=e.get(t.IUniverInstanceService),p=l==null?u.getTextRanges({unitId:c,subUnitId:c}):l,m=p==null||(r=p[0])==null?void 0:r.segmentId;if(!(p!=null&&p.length))return!1;let g=f.getUnit(c,t.UniverInstanceType.UNIVER_DOC);if(!g)return!1;let _=g.getSelfOrHeaderFooterModel(m).getBody();if(!_)return!1;let v=t.BuildTextUtils.customRange.add({ranges:p,rangeId:i,rangeType:a,segmentId:m,wholeEntity:o,properties:s,body:_});if(!v)return!1;let y=t.JSONX.getInstance(),b={id:h.id,params:{unitId:c,actions:[],textRanges:v.selections,segmentId:m},textX:v},x=(0,t.getRichTextEditPath)(g,m);return b.params.actions=y.editOp(v.serialize(),x),b}function M(e,n){let{unitId:r,segmentId:i,insert:a}=n,o=e.get(t.IUniverInstanceService).getUnit(r);if(!o)return!1;let s={id:h.id,params:{unitId:n.unitId,actions:[],textRanges:void 0,segmentId:i}},c=t.JSONX.getInstance(),l=t.BuildTextUtils.customRange.delete({documentDataModel:o,rangeId:n.rangeId,insert:a,segmentId:i});if(!l)return!1;let u=(0,t.getRichTextEditPath)(o,i);return s.params.actions=c.editOp(l.serialize(),u),s.params.textRanges=l.selections,s}function N(e,n){var r,i,a,o;let{unitId:s,body:c,doc:l}=n,u=l;if(u||(u=e.get(t.IUniverInstanceService).getUnit(s)),!u)return!1;let f=(r=n.selection)==null?void 0:r.segmentId,p=(i=u.getSelfOrHeaderFooterModel(f))==null?void 0:i.getBody();if(!p)return!1;let m=e.get(d),g=(a=n.selection)==null?m.getActiveTextRange():a;if(!g||!p)return!1;let _=(o=n.textRanges)==null?[{startOffset:g.startOffset+c.dataStream.length,endOffset:g.startOffset+c.dataStream.length,collapsed:!0,segmentId:f}]:o,v=t.BuildTextUtils.selection.replace({selection:g,body:c,doc:u});if(!v)return!1;let y={id:h.id,params:{unitId:s,actions:[],textRanges:_,debounce:!0,segmentId:f},textX:v},b=t.JSONX.getInstance();return y.params.actions=b.editOp(v.serialize()),y}e.DOC_INTERCEPTOR_POINT=O,e.DeleteTextCommand=_,e.DocContentInsertService=w,Object.defineProperty(e,"DocInterceptorService",{enumerable:!0,get:function(){return k}}),Object.defineProperty(e,"DocSelectionManagerService",{enumerable:!0,get:function(){return d}}),Object.defineProperty(e,"DocSkeletonManagerService",{enumerable:!0,get:function(){return f}}),Object.defineProperty(e,"DocStateChangeManagerService",{enumerable:!0,get:function(){return E}}),e.DocStateEmitService=p,e.IDocStateChangeInterceptorService=T,e.InsertTextCommand=g,e.RichTextEditingMutation=h,e.SetTextSelectionsOperation=i,Object.defineProperty(e,"UniverDocsPlugin",{enumerable:!0,get:function(){return D}}),e.UpdateTextCommand=v,e.addCustomRangeBySelectionFactory=j,e.addCustomRangeFactory=A,e.deleteCustomRangeFactory=M,e.replaceSelectionFactory=N});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/engine-render"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/engine-render`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocs={},e.UniverCore,e.UniverEngineRender,e.rxjs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let i={id:`doc.operation.set-selections`,type:t.CommandType.OPERATION,handler:()=>!0};function a(e){"@babel/helpers - typeof";return a=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},a(e)}function o(e,t){if(a(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(a(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function s(e){var t=o(e,`string`);return a(t)==`symbol`?t:t+``}function c(e,t,n){return(t=s(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){return function(n,r){t(n,r,e)}}function u(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let d=class extends t.RxDisposable{constructor(e,t){super(),this._commandService=e,this._univerInstanceService=t,c(this,`_currentSelection`,null),c(this,`_textSelectionInfo`,new Map),c(this,`_textSelection$`,new r.Subject),c(this,`textSelection$`,this._textSelection$.asObservable()),c(this,`_refreshSelection$`,new r.BehaviorSubject(null)),c(this,`refreshSelection$`,this._refreshSelection$.asObservable()),this._listenCurrentUnit()}_listenCurrentUnit(){this._univerInstanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_DOC).pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{if(e==null)return;let t=e.getUnitId();this._setCurrentSelectionNotRefresh({unitId:t,subUnitId:t})})}__getCurrentSelection(){return this._currentSelection}getSelectionInfo(e=this._currentSelection){return this._getTextRanges(e)}refreshSelection(e=this._currentSelection){e!=null&&this._refresh(e)}__TEST_ONLY_setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}getTextRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.textRanges}getRectRanges(e=this._currentSelection){var t;return(t=this._getTextRanges(e))==null?void 0:t.rectRanges}getDocRanges(e=this._currentSelection){var t,n;let r=(t=this.getTextRanges(e))==null?[]:t,i=(n=this.getRectRanges(e))==null?[]:n;return[...r,...i].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0)}getActiveTextRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{textRanges:t}=e;return t.find(e=>e.isActive)}getActiveRectRange(){let e=this._getTextRanges(this._currentSelection);if(e==null)return;let{rectRanges:t}=e;return t.find(e=>e.isActive)}__TEST_ONLY_add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,rectRanges:[],segmentId:``,segmentPage:-1,isEditing:t,style:n.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0,n){return this.replaceDocRanges(e,this._currentSelection,t,n)}replaceDocRanges(e,t=this._currentSelection,n=!0,r){if(t==null)return;let{unitId:i,subUnitId:a}=t;this._refreshSelection$.next({unitId:i,subUnitId:a,docRanges:e,isEditing:n,options:r})}__replaceTextRangesWithNoRefresh(e,t){if(this._currentSelection==null)return;let n={...e,...t};this._replaceByParam(n),this._textSelection$.next(n);let{unitId:r,subUnitId:a,segmentId:o,style:s,textRanges:c,rectRanges:l,isEditing:u}=n,d=[...c,...l].filter(e=>e.startOffset!=null&&e.endOffset!=null).sort((e,t)=>e.startOffset>t.startOffset?1:e.startOffset<t.startOffset?-1:0);this._commandService.executeCommand(i.id,{unitId:r,subUnitId:a,segmentId:o,style:s,isEditing:u,ranges:d})}dispose(){this._textSelection$.complete(),this._refreshSelection$.complete()}_setCurrentSelectionNotRefresh(e){this._currentSelection=e}_getTextRanges(e){var t;if(e==null)return;let{unitId:n,subUnitId:r=``}=e;return(t=this._textSelectionInfo.get(n))==null?void 0:t.get(r)}_refresh(e){let t=this._getTextRanges(e);if(t==null)return;let{textRanges:n,rectRanges:r}=t,i=[...n,...r],{unitId:a,subUnitId:o}=e;this._refreshSelection$.next({unitId:a,subUnitId:o,docRanges:i,isEditing:!1})}_replaceByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(n,{...r})}_addByParam(e){let{unitId:t,subUnitId:n,...r}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);let i=this._textSelectionInfo.get(t);i.has(n)?i.get(n).textRanges.push(...e.textRanges):i.set(n,{...r})}};d=u([l(0,t.ICommandService),l(1,t.IUniverInstanceService)],d);let f=class extends t.RxDisposable{constructor(e,n,i){super(),this._context=e,this._localeService=n,this._univerInstanceService=i,c(this,`_skeleton`,void 0),c(this,`_docViewModel`,void 0),c(this,`_currentSkeleton$`,new r.BehaviorSubject(null)),c(this,`currentSkeleton$`,this._currentSkeleton$.asObservable()),c(this,`_currentSkeletonBefore$`,new r.BehaviorSubject(null)),c(this,`currentSkeletonBefore$`,this._currentSkeletonBefore$.asObservable()),c(this,`_currentViewModel$`,new r.BehaviorSubject(null)),c(this,`currentViewModel$`,this._currentViewModel$.asObservable()),this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_DOC).pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{e&&e.getUnitId()===this._context.unitId&&this._update(e)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_init(){let e=this._context.unit;this._update(e)}_update(e){let n=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&(0,t.isInternalEditorID)(n)?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));let r=this._skeleton;r.calculate(),this._currentSkeletonBefore$.next(r),this._currentSkeleton$.next(r),this._currentViewModel$.next(this._docViewModel)}_buildSkeleton(e){return n.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new n.DocumentViewModel(e)}};f=u([l(1,(0,t.Inject)(t.LocaleService)),l(2,t.IUniverInstanceService)],f);var p=class extends t.RxDisposable{constructor(){super(),c(this,`_docStateChangeParams$`,new r.BehaviorSubject(null)),c(this,`docStateChangeParams$`,this._docStateChangeParams$.asObservable())}emitStateChangeInfo(e){this._docStateChangeParams$.next(e)}dispose(){super.dispose(),this._docStateChangeParams$.complete()}};let m=`doc.mutation.rich-text-editing`,h={id:m,type:t.CommandType.MUTATION,handler:(e,r,i)=>{var a,o;let{unitId:s,segmentId:c=``,actions:l,textRanges:u,prevTextRanges:h,trigger:g,noHistory:_,isCompositionEnd:v,noNeedSetTextRange:y,debounce:b,isEditing:x=!0,isSync:S,syncer:C}=r,w=S||(i==null?void 0:i.fromCollab)||(i==null?void 0:i.fromChangeset),T=e.get(t.IUniverInstanceService),E=e.get(n.IRenderManagerService),D=e.get(p),O=T.getUnit(s,t.UniverInstanceType.UNIVER_DOC),k=(a=E.getRenderUnitById(s))==null?void 0:a.with(f).getViewModel();if(O==null)throw Error(`DocumentDataModel not found for unitId: ${s}`);let A=e.get(d),j=(o=A.getDocRanges())==null?[]:o,M=!!O.getSnapshot().disabled;if(t.JSONX.isNoop(l)||l&&l.length===0||M)return{unitId:s,actions:[],textRanges:j};let N=t.JSONX.invertWithDoc(l,O.getSnapshot());O.apply(l),k==null||k.reset(O),!y&&u&&g!=null&&!w&&queueMicrotask(()=>{A.replaceDocRanges(u,{unitId:s,subUnitId:s},x,r.options)});let P={commandId:m,unitId:s,segmentId:c,trigger:g,noHistory:_,debounce:b,redoState:{actions:l,textRanges:u},undoState:{actions:N,textRanges:h==null?j:h},isCompositionEnd:v,isSync:w,syncer:C};return D.emitStateChangeInfo(P),{unitId:s,actions:N,textRanges:j}}},g={id:`doc.command.insert-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{var r;let i=e.get(t.ICommandService),{range:a,segmentId:o,body:s,unitId:c,cursorOffset:l}=n,u=e.get(d),f=e.get(t.IUniverInstanceService).getUnit(c,t.UniverInstanceType.UNIVER_DOC);if(f==null)return!1;let p=u.getActiveTextRange(),m=f.getSelfOrHeaderFooterModel((r=p==null?void 0:p.segmentId)==null?``:r).getBody();if(m==null)return!1;let{startOffset:g,collapsed:_}=a,v=l==null?s.dataStream.length:l,y=[{startOffset:g+v,endOffset:g+v,style:p==null?void 0:p.style,collapsed:_}],b={id:h.id,params:{unitId:c,actions:[],textRanges:y,debounce:!0}},x=new t.TextX,S=t.JSONX.getInstance();if(_)g>0&&x.push({t:t.TextXActionType.RETAIN,len:g}),x.push({t:t.TextXActionType.INSERT,body:s,len:s.dataStream.length});else{let e=t.BuildTextUtils.selection.delete([a],m,0,s);x.push(...e)}b.params.textRanges=[{startOffset:g+v,endOffset:g+v,collapsed:_}];let C=(0,t.getRichTextEditPath)(f,o);return b.params.actions=S.editOp(x.serialize(),C),!!i.syncExecuteCommand(b.id,b.params)}},_={id:`doc.command.delete-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{var r;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),{range:o,segmentId:s,unitId:c,direction:l,len:u=1}=n,d=a.getUnit(c,t.UniverInstanceType.UNIVER_DOC),f=d==null?void 0:d.getSelfOrHeaderFooterModel(s).getBody();if(d==null||f==null)return!1;let{startOffset:p}=o,m=l===t.DeleteDirection.LEFT?p-u:p,g=l===t.DeleteDirection.LEFT?p-1:p+u-1,_=(r=f.customRanges)==null?void 0:r.find(e=>e.startIndex<=m&&e.endIndex>=g);_!=null&&_.wholeEntity&&(m=_.startIndex,g=Math.max(g,_.endIndex));let v={id:h.id,params:{unitId:c,actions:[],textRanges:[{startOffset:m,endOffset:m,collapsed:!0}],debounce:!0}},y=new t.TextX,b=t.JSONX.getInstance();y.push({t:t.TextXActionType.RETAIN,len:m-0}),y.push({t:t.TextXActionType.DELETE,len:g-m+1});let x=(0,t.getRichTextEditPath)(d,s);return v.params.actions=b.editOp(y.serialize(),x),!!i.syncExecuteCommand(v.id,v.params)}},v={id:`doc.command.update-text`,type:t.CommandType.COMMAND,handler:(e,n)=>{let{range:r,segmentId:i,updateBody:a,coverType:o,unitId:s,textRanges:c}=n,l=e.get(t.ICommandService),u=e.get(t.IUniverInstanceService).getCurrentUniverDocInstance();if(u==null)return!1;let d={id:h.id,params:{unitId:s,actions:[],textRanges:c}},f=new t.TextX,p=t.JSONX.getInstance(),{startOffset:m,endOffset:g}=r;f.push({t:t.TextXActionType.RETAIN,len:m}),f.push({t:t.TextXActionType.RETAIN,body:a,len:g-m,coverType:o});let _=(0,t.getRichTextEditPath)(u,i);return d.params.actions=p.editOp(f.serialize(),_),!!l.syncExecuteCommand(d.id,d.params)}};var y=`@univerjs/docs`,b=`1.0.0-alpha.1`;let x={id:`doc.mutation.rename-doc`,type:t.CommandType.MUTATION,handler:(e,n)=>{let r=e.get(t.IUniverInstanceService).getUnit(n.unitId,t.UniverInstanceType.UNIVER_DOC);return r?(r.setName(n.name),!0):!1}},S={},C=class extends t.Disposable{constructor(e,t,n){super(),this._commandService=e,this._textSelectionManagerService=t,this._univerInstanceService=n,this._initSelectionChange()}_transformCustomRange(e,n){var r;let{startOffset:i,endOffset:a,collapsed:o}=n,s=(r=e.getCustomRanges())==null?void 0:r.filter(e=>!e.wholeEntity||i<=e.startIndex&&a>e.endIndex?!1:o?e.startIndex<i&&e.endIndex>=a:t.BuildTextUtils.range.isIntersects(i,a-1,e.startIndex,e.endIndex));if(s!=null&&s.length){let e=i,t=a;return s.forEach(n=>{e=Math.min(n.startIndex,e),t=Math.max(n.endIndex+1,t)}),{...n,startOffset:e,endOffset:t,collapsed:e===t}}return n}_initSelectionChange(){this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===i.id){let{unitId:t,ranges:n,isEditing:r}=e.params,i=this._univerInstanceService.getUnit(t);if(!i)return;let a=n.map(e=>this._transformCustomRange(i,e));a.some((e,t)=>n[t]!==e)&&this._textSelectionManagerService.replaceTextRanges(a,r)}}))}};C=u([l(0,t.ICommandService),l(1,(0,t.Inject)(d)),l(2,t.IUniverInstanceService)],C);var w=class extends t.Disposable{constructor(...e){super(...e),c(this,`_validators`,[]),c(this,`_transformers`,[])}registerValidator(e){return this._validators.push(e),this.disposeWithMe((0,t.toDisposable)(()=>(0,t.remove)(this._validators,e)))}registerTransformer(e){return this._transformers.push(e),this.disposeWithMe((0,t.toDisposable)(()=>(0,t.remove)(this._transformers,e)))}canMoveBlock(e){return this._validators.every(t=>t(e))}transformMoveResult(e){return this._transformers.reduce((t,n)=>n({...e,result:t}),e.result)}},T=class extends t.Disposable{constructor(...e){super(...e),c(this,`_range`,null)}setInsertRange(e){this._range=e}consumeInsertRange(e){if(!this._range||e&&this._range.unitId!==e)return null;let t=this._range;return this._range=null,t}clearInsertRange(){this._range=null}};let E=(0,t.createIdentifier)(`doc.state-change-interceptor-service`),D=class extends t.RxDisposable{constructor(e,t,n,i,a){super(),this._undoRedoService=e,this._commandService=t,this._univerInstanceService=n,this._docStateEmitService=i,this._docStateChangeInterceptorService=a,c(this,`_docStateChange$`,new r.BehaviorSubject(null)),c(this,`docStateChange$`,this._docStateChange$.asObservable()),c(this,`_historyStateCache`,new Map),c(this,`_changeStateCache`,new Map),c(this,`_historyTimer`,null),c(this,`_changeStateCacheTimer`,null),this._initialize(),this._listenDocStateChange()}getStateCache(e){var t,n;return{history:(t=this._historyStateCache.get(e))==null?[]:t,collaboration:(n=this._changeStateCache.get(e))==null?[]:n}}setStateCache(e,t){this._historyStateCache.set(e,t.history),this._changeStateCache.set(e,t.collaboration)}_setChangeState(e){this._cacheChangeState(e,`history`),this._cacheChangeState(e,`collaboration`)}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===t.UndoCommandId||e.id===t.RedoCommandId){let e=this._univerInstanceService.getCurrentUniverDocInstance();if(e==null)return;let t=e.getUnitId();this._pushHistory(t),this._emitChangeState(t)}}))}_listenDocStateChange(){this._docStateEmitService.docStateChangeParams$.pipe((0,r.takeUntil)(this.dispose$)).subscribe(e=>{var t,n;if(e==null)return;let r=(t=(n=this._docStateChangeInterceptorService)==null?void 0:n.transformChangeStateInfo(e))==null?e:t;if(r==null||r.isSync)return;let{isCompositionEnd:i,isSync:a,syncer:o,...s}=r;this._setChangeState(s)})}_cacheChangeState(e,n=`history`){let{trigger:r,unitId:i,noHistory:a,debounce:o=!1}=e;if(a||n===`history`&&r==null||n===`history`&&(r===t.RedoCommandId||r===t.UndoCommandId))return;let s=n===`history`?this._historyStateCache:this._changeStateCache,c=n===`history`?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(s.has(i)){let t=s.get(i);t==null||t.push(e)}else s.set(i,[e]);o?n===`history`?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{c(i)},300)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{c(i)},300)):c(i)}_pushHistory(e){let n=this._undoRedoService,r=this._historyStateCache.get(e);if(n==null||!Array.isArray(r)||r.length===0)return;let i=r.length,a=r[0].commandId,o=r[0],s=r[i-1],c={unitId:e,actions:r.reduce((e,n)=>t.JSONX.compose(e,n.redoState.actions),null),textRanges:s.redoState.textRanges},l={unitId:e,actions:r.reverse().reduce((e,n)=>t.JSONX.compose(e,n.undoState.actions),null),textRanges:o.undoState.textRanges};n.pushUndoRedo({unitID:e,undoMutations:[{id:a,params:l}],redoMutations:[{id:a,params:c}]}),r.length=0}_emitChangeState(e){let n=this._changeStateCache.get(e);if(!Array.isArray(n)||n.length===0)return;let r=n.length,{commandId:i,trigger:a,segmentId:o,noHistory:s,debounce:c}=n[0],l=n[0],u=n[r-1],d={commandId:i,unitId:e,trigger:a,redoState:{unitId:e,actions:n.reduce((e,n)=>t.JSONX.compose(e,n.redoState.actions),null),textRanges:u.redoState.textRanges},undoState:{unitId:e,actions:n.reverse().reduce((e,n)=>t.JSONX.compose(e,n.undoState.actions),null),textRanges:l.undoState.textRanges},segmentId:o,noHistory:s,debounce:c};n.length=0,this._docStateChange$.next(d)}};D=u([l(0,(0,t.Optional)(t.IUndoRedoService)),l(1,t.ICommandService),l(2,t.IUniverInstanceService),l(3,(0,t.Inject)(p)),l(4,(0,t.Optional)(E))],D);let O=class extends t.Plugin{constructor(e=S,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},S,this._config);this._configService.setConfig(`docs.config`,i)}onStarting(){this._initializeDependencies(),this._initializeCommands()}_initializeCommands(){[g,_,v,h,x,i].forEach(e=>{this._injector.get(t.ICommandService).registerCommand(e)})}_initializeDependencies(){[[d],[p],[D],[w],[T],[C]].forEach(e=>this._injector.add(e))}onReady(){this._injector.get(D),this._injector.get(C)}};c(O,`pluginName`,`DOCS_PLUGIN`),c(O,`packageName`,y),c(O,`version`,b),O=u([l(1,(0,t.Inject)(t.Injector)),l(2,t.IConfigService)],O);let k={CUSTOM_RANGE:(0,t.createInterceptorKey)(`CUSTOM_RANGE`),CUSTOM_DECORATION:(0,t.createInterceptorKey)(`CUSTOM_DECORATION`)},A=class extends t.Disposable{constructor(e,n){super(),this._context=e,this._docSkeletonManagerService=n,c(this,`_interceptorsByName`,new Map);let r=this._docSkeletonManagerService.getViewModel(),i=r.getDataModel().getUnitId();if(i===t.DOCS_NORMAL_EDITOR_UNIT_ID_KEY||i===t.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY)return;this.disposeWithMe(this.interceptDocumentViewModel(r)),this.disposeWithMe(this.intercept(k.CUSTOM_RANGE,{priority:-1,handler:(e,t,n)=>n(e)}));let a=new t.DisposableCollection;r.segmentViewModels$.subscribe(e=>{a.dispose(),a=new t.DisposableCollection,e.forEach(e=>{a.add(this.interceptDocumentViewModel(e))})}),this.disposeWithMe(a)}intercept(e,n){let r=e;this._interceptorsByName.has(r)||this._interceptorsByName.set(r,[]);let i=this._interceptorsByName.get(r);return i.push(n),this._interceptorsByName.set(r,i.sort((e,t)=>{var n,r;return((n=t.priority)==null?0:n)-((r=e.priority)==null?0:r)})),this.disposeWithMe((0,t.toDisposable)(()=>(0,t.remove)(this._interceptorsByName.get(r),n)))}fetchThroughInterceptors(e){let n=e;return(0,t.composeInterceptors)(this._interceptorsByName.get(n)||[])}interceptDocumentViewModel(e){let n=new t.DisposableCollection;return n.add(e.registerCustomRangeInterceptor({getCustomRange:t=>{var n;return this.fetchThroughInterceptors(k.CUSTOM_RANGE)(e.getCustomRangeRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customRanges:(n=e.getDataModel().getCustomRanges())==null?[]:n})},getCustomDecoration:t=>{var n;return this.fetchThroughInterceptors(k.CUSTOM_DECORATION)(e.getCustomDecorationRaw(t),{index:t,unitId:e.getDataModel().getUnitId(),customDecorations:(n=e.getDataModel().getCustomDecorations())==null?[]:n})}})),n}};A=u([l(1,(0,t.Inject)(f))],A);function j(e,n,r){let{unitId:i,segmentId:a}=n,o=e.get(t.IUniverInstanceService).getUnit(i);if(!o)return!1;let s={id:h.id,params:{unitId:n.unitId,actions:[],textRanges:void 0}},c=t.JSONX.getInstance(),l=t.BuildTextUtils.customRange.add({...n,body:r});if(!l)return!1;let u=(0,t.getRichTextEditPath)(o,a);return s.params.actions=c.editOp(l.serialize(),u),s}function M(e,n){var r;let{rangeId:i,rangeType:a,wholeEntity:o,properties:s,unitId:c,selections:l}=n,u=e.get(d),f=e.get(t.IUniverInstanceService),p=l==null?u.getTextRanges({unitId:c,subUnitId:c}):l,m=p==null||(r=p[0])==null?void 0:r.segmentId;if(!(p!=null&&p.length))return!1;let g=f.getUnit(c,t.UniverInstanceType.UNIVER_DOC);if(!g)return!1;let _=g.getSelfOrHeaderFooterModel(m).getBody();if(!_)return!1;let v=t.BuildTextUtils.customRange.add({ranges:p,rangeId:i,rangeType:a,segmentId:m,wholeEntity:o,properties:s,body:_});if(!v)return!1;let y=t.JSONX.getInstance(),b={id:h.id,params:{unitId:c,actions:[],textRanges:v.selections,segmentId:m},textX:v},x=(0,t.getRichTextEditPath)(g,m);return b.params.actions=y.editOp(v.serialize(),x),b}function N(e,n){let{unitId:r,segmentId:i,insert:a}=n,o=e.get(t.IUniverInstanceService).getUnit(r);if(!o)return!1;let s={id:h.id,params:{unitId:n.unitId,actions:[],textRanges:void 0,segmentId:i}},c=t.JSONX.getInstance(),l=t.BuildTextUtils.customRange.delete({documentDataModel:o,rangeId:n.rangeId,insert:a,segmentId:i});if(!l)return!1;let u=(0,t.getRichTextEditPath)(o,i);return s.params.actions=c.editOp(l.serialize(),u),s.params.textRanges=l.selections,s}function P(e,n){var r,i,a,o;let{unitId:s,body:c,doc:l}=n,u=l;if(u||(u=e.get(t.IUniverInstanceService).getUnit(s)),!u)return!1;let f=(r=n.selection)==null?void 0:r.segmentId,p=(i=u.getSelfOrHeaderFooterModel(f))==null?void 0:i.getBody();if(!p)return!1;let m=e.get(d),g=(a=n.selection)==null?m.getActiveTextRange():a;if(!g||!p)return!1;let _=(o=n.textRanges)==null?[{startOffset:g.startOffset+c.dataStream.length,endOffset:g.startOffset+c.dataStream.length,collapsed:!0,segmentId:f}]:o,v=t.BuildTextUtils.selection.replace({selection:g,body:c,doc:u});if(!v)return!1;let y={id:h.id,params:{unitId:s,actions:[],textRanges:_,debounce:!0,segmentId:f},textX:v},b=t.JSONX.getInstance();return y.params.actions=b.editOp(v.serialize()),y}e.DOC_INTERCEPTOR_POINT=k,e.DeleteTextCommand=_,e.DocBlockMoveValidatorService=w,e.DocContentInsertService=T,Object.defineProperty(e,"DocInterceptorService",{enumerable:!0,get:function(){return A}}),Object.defineProperty(e,"DocSelectionManagerService",{enumerable:!0,get:function(){return d}}),Object.defineProperty(e,"DocSkeletonManagerService",{enumerable:!0,get:function(){return f}}),Object.defineProperty(e,"DocStateChangeManagerService",{enumerable:!0,get:function(){return D}}),e.DocStateEmitService=p,e.IDocStateChangeInterceptorService=E,e.InsertTextCommand=g,e.RichTextEditingMutation=h,e.SetTextSelectionsOperation=i,Object.defineProperty(e,"UniverDocsPlugin",{enumerable:!0,get:function(){return O}}),e.UpdateTextCommand=v,e.addCustomRangeBySelectionFactory=M,e.addCustomRangeFactory=j,e.deleteCustomRangeFactory=N,e.replaceSelectionFactory=P});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/docs",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Core document model and rich-text operations for Univer Docs.",
|
|
6
6
|
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
@@ -62,14 +62,14 @@
|
|
|
62
62
|
"rxjs": ">=7.0.0"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@univerjs/core": "1.0.0-alpha.
|
|
66
|
-
"@univerjs/engine-render": "1.0.0-alpha.
|
|
65
|
+
"@univerjs/core": "1.0.0-alpha.1",
|
|
66
|
+
"@univerjs/engine-render": "1.0.0-alpha.1"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"rxjs": "^7.8.2",
|
|
70
70
|
"typescript": "^6.0.3",
|
|
71
71
|
"vitest": "^4.1.9",
|
|
72
|
-
"@univerjs-infra/shared": "1.0.0-alpha.
|
|
72
|
+
"@univerjs-infra/shared": "1.0.0-alpha.1"
|
|
73
73
|
},
|
|
74
74
|
"scripts": {
|
|
75
75
|
"test": "vitest run",
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2023-present DreamNum Co., Ltd.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import type { IDocumentBody } from '@univerjs/core';
|
|
17
|
-
/**
|
|
18
|
-
* The top-level document body element types supported by the doc facade.
|
|
19
|
-
*/
|
|
20
|
-
export type FDocElementType = 'paragraph' | 'table' | 'blockRange' | 'customBlock';
|
|
21
|
-
/**
|
|
22
|
-
* Error thrown when a facade element key can no longer be resolved uniquely.
|
|
23
|
-
*
|
|
24
|
-
* A stale paragraph usually means the paragraph was deleted, or the document no
|
|
25
|
-
* longer contains exactly one paragraph with the requested `paragraphId`.
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* const doc = univerAPI.getActiveDocument();
|
|
30
|
-
* if (!doc) throw new Error('No active document');
|
|
31
|
-
*
|
|
32
|
-
* const paragraph = doc.getBody().getChild(0).asParagraph();
|
|
33
|
-
* paragraph.removeFromParent();
|
|
34
|
-
*
|
|
35
|
-
* try {
|
|
36
|
-
* paragraph.getText();
|
|
37
|
-
* } catch (error) {
|
|
38
|
-
* if (error instanceof DocElementStaleError) {
|
|
39
|
-
* console.log('The paragraph handle is stale.');
|
|
40
|
-
* }
|
|
41
|
-
* }
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
export declare class DocElementStaleError extends Error {
|
|
45
|
-
/**
|
|
46
|
-
* Create a stale element error.
|
|
47
|
-
* @param {string} message The error message.
|
|
48
|
-
*/
|
|
49
|
-
constructor(message?: string);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @deprecated Paragraph facade identity is now resolved directly from persisted
|
|
53
|
-
* `paragraphId` values. This class remains as a compatibility shell for callers
|
|
54
|
-
* that constructed document facade internals directly.
|
|
55
|
-
*
|
|
56
|
-
* @hideconstructor
|
|
57
|
-
*/
|
|
58
|
-
export declare class DocElementRegistry {
|
|
59
|
-
/**
|
|
60
|
-
* @deprecated Paragraph facade identity is now the persisted `paragraphId`.
|
|
61
|
-
* @throws {Error} Always throws; use `paragraph.paragraphId` instead.
|
|
62
|
-
*/
|
|
63
|
-
getParagraphKey(_segmentId: string, _body: IDocumentBody, _paragraphIndex: number): string;
|
|
64
|
-
/**
|
|
65
|
-
* @deprecated Paragraph facade identity is now the persisted `paragraphId`.
|
|
66
|
-
* @throws {Error} Always throws; resolve paragraph handles by `paragraphId` instead.
|
|
67
|
-
*/
|
|
68
|
-
resolveParagraphStartIndex(_segmentId: string, _key: string): number;
|
|
69
|
-
/**
|
|
70
|
-
* @deprecated Paragraph facade identity is now the persisted `paragraphId`.
|
|
71
|
-
* @throws {Error} Always throws; resolve paragraph handles by `paragraphId` instead.
|
|
72
|
-
*/
|
|
73
|
-
syncParagraph(_segmentId: string, _key: string, _body: IDocumentBody): number;
|
|
74
|
-
/**
|
|
75
|
-
* @deprecated Paragraph facade identity is now the persisted `paragraphId`.
|
|
76
|
-
* @throws {Error} Always throws; stale state is detected from live `paragraphId` lookups.
|
|
77
|
-
*/
|
|
78
|
-
markStale(_segmentId: string, _key: string): void;
|
|
79
|
-
/**
|
|
80
|
-
* @deprecated Paragraph facade identity is now the persisted `paragraphId`.
|
|
81
|
-
* @throws {Error} Always throws; text edits no longer need registry offset tracking.
|
|
82
|
-
*/
|
|
83
|
-
beforeTextEdit(_segmentId: string, _startOffset: number, _endOffset: number, _insertLength: number): void;
|
|
84
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2023-present DreamNum Co., Ltd.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import type { DocumentBlockRangeType } from '@univerjs/core';
|
|
17
|
-
import type { FDocBody } from './f-doc-body';
|
|
18
|
-
/**
|
|
19
|
-
* A facade wrapper for document block ranges, such as callout, quote, and code blocks.
|
|
20
|
-
*
|
|
21
|
-
* Block range identity is backed by the persisted `IDocumentBlockRange.blockId`,
|
|
22
|
-
* so wrappers can be re-resolved after text is inserted before the block range.
|
|
23
|
-
*
|
|
24
|
-
* @hideconstructor
|
|
25
|
-
*/
|
|
26
|
-
export declare class FDocBlockRange {
|
|
27
|
-
protected readonly _body: FDocBody;
|
|
28
|
-
protected readonly _key: string;
|
|
29
|
-
constructor(_body: FDocBody, _key: string);
|
|
30
|
-
/**
|
|
31
|
-
* Get the document element type.
|
|
32
|
-
* @returns {'blockRange'} The literal block range element type.
|
|
33
|
-
* @example
|
|
34
|
-
* ```ts
|
|
35
|
-
* const doc = univerAPI.getActiveDocument();
|
|
36
|
-
* if (!doc) throw new Error('No active document');
|
|
37
|
-
*
|
|
38
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
39
|
-
* console.log(blockRange.getType());
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
getType(): 'blockRange';
|
|
43
|
-
/**
|
|
44
|
-
* Get the block range key.
|
|
45
|
-
* @returns {string} The persisted `blockId` for this block range.
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* const doc = univerAPI.getActiveDocument();
|
|
49
|
-
* if (!doc) throw new Error('No active document');
|
|
50
|
-
*
|
|
51
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
52
|
-
* console.log(blockRange.getKey());
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
getKey(): string;
|
|
56
|
-
/**
|
|
57
|
-
* Get the parent body facade that owns this block range.
|
|
58
|
-
* @returns {FDocBody} The document body facade.
|
|
59
|
-
* @example
|
|
60
|
-
* ```ts
|
|
61
|
-
* const doc = univerAPI.getActiveDocument();
|
|
62
|
-
* if (!doc) throw new Error('No active document');
|
|
63
|
-
*
|
|
64
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
65
|
-
* console.log(blockRange.getParent().getChildIndex(blockRange));
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
getParent(): FDocBody;
|
|
69
|
-
/**
|
|
70
|
-
* Remove this block range and its content from the parent body.
|
|
71
|
-
* @returns {boolean} `true` if the block range content was removed.
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* const doc = univerAPI.getActiveDocument();
|
|
75
|
-
* if (!doc) throw new Error('No active document');
|
|
76
|
-
*
|
|
77
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
78
|
-
* blockRange.removeFromParent();
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
removeFromParent(): boolean;
|
|
82
|
-
/**
|
|
83
|
-
* Get the block range type.
|
|
84
|
-
* @returns {DocumentBlockRangeType} The block type, such as callout, quote, or code.
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* const doc = univerAPI.getActiveDocument();
|
|
88
|
-
* if (!doc) throw new Error('No active document');
|
|
89
|
-
*
|
|
90
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
91
|
-
* console.log(blockRange.getBlockType());
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
getBlockType(): DocumentBlockRangeType;
|
|
95
|
-
/**
|
|
96
|
-
* Get the plain text inside this block range.
|
|
97
|
-
* @returns {string} The block range text.
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* const doc = univerAPI.getActiveDocument();
|
|
101
|
-
* if (!doc) throw new Error('No active document');
|
|
102
|
-
*
|
|
103
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
104
|
-
* console.log(blockRange.getText());
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
getText(): string;
|
|
108
|
-
/**
|
|
109
|
-
* Replace the plain text inside this block range.
|
|
110
|
-
* @param {string} text The replacement text.
|
|
111
|
-
* @returns {boolean} `true` if the block range text was replaced.
|
|
112
|
-
* @example
|
|
113
|
-
* ```ts
|
|
114
|
-
* const doc = univerAPI.getActiveDocument();
|
|
115
|
-
* if (!doc) throw new Error('No active document');
|
|
116
|
-
*
|
|
117
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
118
|
-
* blockRange.setText('Updated block text');
|
|
119
|
-
* ```
|
|
120
|
-
*/
|
|
121
|
-
setText(text: string): boolean;
|
|
122
|
-
/**
|
|
123
|
-
* Remove this block range wrapper from the body.
|
|
124
|
-
*
|
|
125
|
-
* This currently removes the block range and its content, matching
|
|
126
|
-
* `removeFromParent()`.
|
|
127
|
-
*
|
|
128
|
-
* @returns {boolean} `true` if the block range content was removed.
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* const doc = univerAPI.getActiveDocument();
|
|
132
|
-
* if (!doc) throw new Error('No active document');
|
|
133
|
-
*
|
|
134
|
-
* const blockRange = doc.getBody().getChild(0).asBlockRange();
|
|
135
|
-
* blockRange.unwrap();
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
unwrap(): boolean;
|
|
139
|
-
}
|