@univerjs/docs 0.1.16 → 0.1.17

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.
@@ -1,5 +1,5 @@
1
- import { IUniverInstanceService, Nullable } from '@univerjs/core';
2
- import { DocBackground, Documents, Engine, IRenderManagerService, Scene } from '@univerjs/engine-render';
1
+ import { DocumentDataModel, IUniverInstanceService, Nullable } from '@univerjs/core';
2
+ import { DocBackground, Documents, Engine, IRenderContext, IRenderManagerService, Scene } from '@univerjs/engine-render';
3
3
 
4
4
  export interface IDocObjectParam {
5
5
  document: Documents;
@@ -7,5 +7,12 @@ export interface IDocObjectParam {
7
7
  scene: Scene;
8
8
  engine: Engine;
9
9
  }
10
+ export declare function neoGetDocObject(renderContext: IRenderContext<DocumentDataModel>): {
11
+ document: Documents;
12
+ docBackground: DocBackground;
13
+ scene: Scene;
14
+ engine: Engine;
15
+ };
16
+ /** @deprecated After migrating to `RenderUnit`, use `neoGetDocObject` instead. */
10
17
  export declare function getDocObject(univerInstanceService: IUniverInstanceService, renderManagerService: IRenderManagerService): Nullable<IDocObjectParam>;
11
18
  export declare function getDocObjectById(unitId: string, renderManagerService: IRenderManagerService): Nullable<IDocObjectParam>;
@@ -1,15 +1,36 @@
1
- import { IDocumentData, Univer } from '@univerjs/core';
2
- import { Dependency } from '@wendellhu/redi';
1
+ import { DocumentDataModel, IDocumentData, Nullable, IUniverInstanceService, RxDisposable, Univer } from '@univerjs/core';
2
+ import { Dependency, DependencyIdentifier, Injector } from '@wendellhu/redi';
3
+ import { DocumentSkeleton, IRender, IRenderContext, IRenderModule, DocumentViewModel, IRenderManagerService } from '@univerjs/engine-render';
3
4
 
4
5
  export declare function createCommandTestBed(workbookData?: IDocumentData, dependencies?: Dependency[]): {
5
6
  univer: Univer;
6
7
  get: {
7
- <T>(id: import('@wendellhu/redi').DependencyIdentifier<T>, lookUp?: import('@wendellhu/redi').LookUp | undefined): T;
8
- <T_1>(id: import('@wendellhu/redi').DependencyIdentifier<T_1>, quantity: import("@wendellhu/redi").Quantity.MANY, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_1[];
9
- <T_2>(id: import('@wendellhu/redi').DependencyIdentifier<T_2>, quantity: import("@wendellhu/redi").Quantity.OPTIONAL, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_2 | null;
10
- <T_3>(id: import('@wendellhu/redi').DependencyIdentifier<T_3>, quantity: import("@wendellhu/redi").Quantity.REQUIRED, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_3;
11
- <T_4>(id: import('@wendellhu/redi').DependencyIdentifier<T_4>, quantity?: import('@wendellhu/redi').Quantity | undefined, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_4 | T_4[] | null;
12
- <T_5>(id: import('@wendellhu/redi').DependencyIdentifier<T_5>, quantityOrLookup?: import('@wendellhu/redi').Quantity | import('@wendellhu/redi').LookUp | undefined, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_5 | T_5[] | null;
8
+ <T>(id: DependencyIdentifier<T>, lookUp?: import('@wendellhu/redi').LookUp | undefined): T;
9
+ <T_1>(id: DependencyIdentifier<T_1>, quantity: import("@wendellhu/redi").Quantity.MANY, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_1[];
10
+ <T_2>(id: DependencyIdentifier<T_2>, quantity: import("@wendellhu/redi").Quantity.OPTIONAL, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_2 | null;
11
+ <T_3>(id: DependencyIdentifier<T_3>, quantity: import("@wendellhu/redi").Quantity.REQUIRED, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_3;
12
+ <T_4>(id: DependencyIdentifier<T_4>, quantity?: import('@wendellhu/redi').Quantity | undefined, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_4 | T_4[] | null;
13
+ <T_5>(id: DependencyIdentifier<T_5>, quantityOrLookup?: import('@wendellhu/redi').Quantity | import('@wendellhu/redi').LookUp | undefined, lookUp?: import('@wendellhu/redi').LookUp | undefined): T_5 | T_5[] | null;
13
14
  };
14
- doc: import('@univerjs/core').DocumentDataModel;
15
+ doc: DocumentDataModel;
15
16
  };
17
+ export declare class MockRenderManagerService implements Pick<IRenderManagerService, 'getRenderById'> {
18
+ private readonly _injector;
19
+ constructor(_injector: Injector);
20
+ getRenderById(_unitId: string): Nullable<IRender>;
21
+ }
22
+ export declare class MockDocSkeletonManagerService extends RxDisposable implements IRenderModule {
23
+ private readonly _context;
24
+ private readonly _univerInstanceService;
25
+ private _docViewModel;
26
+ private readonly _currentSkeleton$;
27
+ readonly currentSkeleton$: import('rxjs').Observable<Nullable<DocumentSkeleton>>;
28
+ private readonly _currentSkeletonBefore$;
29
+ readonly currentSkeletonBefore$: import('rxjs').Observable<Nullable<DocumentSkeleton>>;
30
+ constructor(_context: IRenderContext<DocumentDataModel>, _univerInstanceService: IUniverInstanceService);
31
+ dispose(): void;
32
+ private _update;
33
+ getSkeleton(): DocumentSkeleton;
34
+ getViewModel(): DocumentViewModel;
35
+ private _buildDocViewModel;
36
+ }
@@ -0,0 +1,9 @@
1
+ import { IAccessor } from '@wendellhu/redi';
2
+ import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
3
+
4
+ /**
5
+ * Get the skeleton of the command's target.
6
+ * @param accessor The injection accessor.
7
+ * @param unitId Unit ID.
8
+ */
9
+ export declare function getCommandSkeleton(accessor: IAccessor, unitId: string): DocSkeletonManagerService | undefined;
@@ -1,11 +1,10 @@
1
1
  import { Disposable, ICommandService, IUniverInstanceService } from '@univerjs/core';
2
- import { ITextSelectionRenderManager } from '@univerjs/engine-render';
3
- import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
2
+ import { IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/engine-render';
4
3
  import { IMEInputManagerService } from '../services/ime-input-manager.service';
5
4
 
6
5
  export declare class IMEInputController extends Disposable {
7
- private readonly _docSkeletonManagerService;
8
6
  private readonly _univerInstanceService;
7
+ private readonly _renderManagerSrv;
9
8
  private readonly _textSelectionRenderManager;
10
9
  private readonly _imeInputManagerService;
11
10
  private readonly _commandService;
@@ -14,7 +13,7 @@ export declare class IMEInputController extends Disposable {
14
13
  private _onStartSubscription;
15
14
  private _onUpdateSubscription;
16
15
  private _onEndSubscription;
17
- constructor(_docSkeletonManagerService: DocSkeletonManagerService, _univerInstanceService: IUniverInstanceService, _textSelectionRenderManager: ITextSelectionRenderManager, _imeInputManagerService: IMEInputManagerService, _commandService: ICommandService);
16
+ constructor(_univerInstanceService: IUniverInstanceService, _renderManagerSrv: IRenderManagerService, _textSelectionRenderManager: ITextSelectionRenderManager, _imeInputManagerService: IMEInputManagerService, _commandService: ICommandService);
18
17
  dispose(): void;
19
18
  private _initialize;
20
19
  private _initialOnCompositionstart;
@@ -1,16 +1,14 @@
1
1
  import { Disposable, ICommandService, IUniverInstanceService } from '@univerjs/core';
2
2
  import { IRenderManagerService } from '@univerjs/engine-render';
3
- import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
4
3
  import { TextSelectionManagerService } from '../services/text-selection-manager.service';
5
4
 
6
5
  export declare class MoveCursorController extends Disposable {
7
- private readonly _docSkeletonManagerService;
8
6
  private readonly _univerInstanceService;
9
7
  private readonly _renderManagerService;
10
8
  private readonly _textSelectionManagerService;
11
9
  private readonly _commandService;
12
10
  private _onInputSubscription;
13
- constructor(_docSkeletonManagerService: DocSkeletonManagerService, _univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _textSelectionManagerService: TextSelectionManagerService, _commandService: ICommandService);
11
+ constructor(_univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _textSelectionManagerService: TextSelectionManagerService, _commandService: ICommandService);
14
12
  dispose(): void;
15
13
  private _commandExecutedListener;
16
14
  private _handleShiftMoveSelection;
@@ -1,15 +1,13 @@
1
1
  import { Disposable, ICommandService, IUniverInstanceService } from '@univerjs/core';
2
2
  import { IRenderManagerService, ITextSelectionRenderManager } from '@univerjs/engine-render';
3
- import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
4
3
 
5
4
  export declare class NormalInputController extends Disposable {
6
- private readonly _docSkeletonManagerService;
7
5
  private readonly _univerInstanceService;
8
6
  private readonly _renderManagerService;
9
7
  private readonly _textSelectionRenderManager;
10
8
  private readonly _commandService;
11
9
  private _onInputSubscription;
12
- constructor(_docSkeletonManagerService: DocSkeletonManagerService, _univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _textSelectionRenderManager: ITextSelectionRenderManager, _commandService: ICommandService);
10
+ constructor(_univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _textSelectionRenderManager: ITextSelectionRenderManager, _commandService: ICommandService);
13
11
  dispose(): void;
14
12
  private _init;
15
13
  private _initialNormalInput;
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  export type { IDocObjectParam } from './basics/component-tools';
17
- export { getDocObject, getDocObjectById } from './basics/component-tools';
17
+ export { getDocObject, neoGetDocObject, getDocObjectById } from './basics/component-tools';
18
18
  export * from './basics/docs-view-key';
19
19
  export { BreakLineCommand } from './commands/commands/break-line.command';
20
20
  export { DeleteCommand, type ICoverCommandParams, type IDeleteCommandParams, type IInsertCommandParams, InsertCommand, type IUpdateCommandParams, UpdateCommand, EditorInsertTextCommandId, } from './commands/commands/core-editing.command';
@@ -28,8 +28,7 @@ export { type IRichTextEditingMutationParams, RichTextEditingMutation, } from '.
28
28
  export { MoveCursorOperation, MoveSelectionOperation } from './commands/operations/cursor.operation';
29
29
  export { type ISetTextSelectionsOperationParams, SetTextSelectionsOperation, } from './commands/operations/text-selection.operation';
30
30
  export { type IUniverDocsConfig, UniverDocsPlugin } from './doc-plugin';
31
- export { DocSkeletonManagerService, type IDocSkeletonManagerParam } from './services/doc-skeleton-manager.service';
32
- export { DocViewModelManagerService } from './services/doc-view-model-manager.service';
31
+ export { DocSkeletonManagerService } from './services/doc-skeleton-manager.service';
33
32
  export { TextSelectionManagerService, serializeTextRange } from './services/text-selection-manager.service';
34
33
  export { DocStateChangeManagerService, type IDocStateChangeParams } from './services/doc-state-change-manager.service';
35
34
  export { IMEInputManagerService } from './services/ime-input-manager.service';
@@ -1,33 +1,26 @@
1
- import { Nullable, IUniverInstanceService, LocaleService, RxDisposable } from '@univerjs/core';
2
- import { DocumentSkeleton } from '@univerjs/engine-render';
3
- import { DocViewModelManagerService } from './doc-view-model-manager.service';
1
+ import { DocumentDataModel, Nullable, IUniverInstanceService, LocaleService, RxDisposable } from '@univerjs/core';
2
+ import { IRenderContext, IRenderModule, DocumentSkeleton, DocumentViewModel } from '@univerjs/engine-render';
4
3
 
5
- export interface IDocSkeletonManagerParam {
6
- unitId: string;
7
- skeleton: DocumentSkeleton;
8
- dirty: boolean;
9
- }
10
4
  /**
11
- * This service is for document build and manage doc skeletons.
5
+ * This service is for document build and manage doc skeletons. It also manages
6
+ * DocumentViewModels.
12
7
  */
13
- export declare class DocSkeletonManagerService extends RxDisposable {
8
+ export declare class DocSkeletonManagerService extends RxDisposable implements IRenderModule {
9
+ private readonly _context;
14
10
  private readonly _localeService;
15
- private readonly _docViewModelManagerService;
16
11
  private readonly _univerInstanceService;
17
- private _currentSkeletonUnitId;
18
- private _docSkeletonMap;
12
+ private _skeleton;
13
+ private _docViewModel;
19
14
  private readonly _currentSkeleton$;
20
- readonly currentSkeleton$: import('rxjs').Observable<Nullable<IDocSkeletonManagerParam>>;
15
+ readonly currentSkeleton$: import('rxjs').Observable<Nullable<DocumentSkeleton>>;
21
16
  private readonly _currentSkeletonBefore$;
22
- readonly currentSkeletonBefore$: import('rxjs').Observable<Nullable<IDocSkeletonManagerParam>>;
23
- constructor(_localeService: LocaleService, _docViewModelManagerService: DocViewModelManagerService, _univerInstanceService: IUniverInstanceService);
24
- private _initialize;
17
+ readonly currentSkeletonBefore$: import('rxjs').Observable<Nullable<DocumentSkeleton>>;
18
+ constructor(_context: IRenderContext<DocumentDataModel>, _localeService: LocaleService, _univerInstanceService: IUniverInstanceService);
25
19
  dispose(): void;
26
20
  private _init;
27
- getCurrent(): Nullable<IDocSkeletonManagerParam>;
28
- getAllSkeleton(): Map<string, IDocSkeletonManagerParam>;
29
- makeDirty(unitId: string, state?: boolean): void;
30
- getSkeletonByUnitId(unitId: string): Nullable<IDocSkeletonManagerParam>;
31
- private _setCurrent;
21
+ private _update;
22
+ getSkeleton(): DocumentSkeleton;
23
+ getViewModel(): DocumentViewModel;
32
24
  private _buildSkeleton;
25
+ private _buildDocViewModel;
33
26
  }
@@ -1,19 +1,21 @@
1
- import { Nullable, IUniverInstanceService, RxDisposable } from '@univerjs/core';
2
- import { DocumentViewModel } from '@univerjs/engine-render';
1
+ import { DocumentDataModel, Nullable, IUniverInstanceService, RxDisposable } from '@univerjs/core';
2
+ import { IRenderContext, IRenderModule, DocumentViewModel } from '@univerjs/engine-render';
3
3
 
4
4
  export interface IDocumentViewModelManagerParam {
5
5
  unitId: string;
6
6
  docViewModel: DocumentViewModel;
7
7
  }
8
8
  /**
9
- * The view model manager is used to manage Doc view model. has a one-to-one correspondence with the doc skeleton.
9
+ * The view model manager is used to manage Doc view model. Each view model has a one-to-one correspondence
10
+ * with the doc skeleton.
10
11
  */
11
- export declare class DocViewModelManagerService extends RxDisposable {
12
+ export declare class DocViewModelManagerService extends RxDisposable implements IRenderModule {
13
+ private readonly _context;
12
14
  private readonly _univerInstanceService;
13
15
  private _docViewModelMap;
14
16
  private readonly _currentDocViewModel$;
15
17
  readonly currentDocViewModel$: import('rxjs').Observable<Nullable<IDocumentViewModelManagerParam>>;
16
- constructor(_univerInstanceService: IUniverInstanceService);
18
+ constructor(_context: IRenderContext<DocumentDataModel>, _univerInstanceService: IUniverInstanceService);
17
19
  private _initialize;
18
20
  dispose(): void;
19
21
  private _init;
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(l,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("@univerjs/core"),require("@univerjs/engine-render"),require("rxjs"),require("@wendellhu/redi")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/engine-render","rxjs","@wendellhu/redi"],n):(l=typeof globalThis<"u"?globalThis:l||self,n(l.UniverDocs={},l.UniverCore,l.UniverEngineRender,l.rxjs,l["@wendellhu/redi"]))})(this,function(l,n,O,w,B){"use strict";var Kt=Object.defineProperty;var Qt=(l,n,O)=>n in l?Kt(l,n,{enumerable:!0,configurable:!0,writable:!0,value:O}):l[n]=O;var T=(l,n,O)=>Qt(l,typeof n!="symbol"?n+"":n,O);var Se;var Z=(o=>(o.MAIN="__Document_Render_Main__",o.BACKGROUND="__Document_Render_Background__",o))(Z||{}),Ee=(o=>(o.VIEW_MAIN="viewMain",o.VIEW_TOP="viewTop",o.VIEW_LEFT="viewLeft",o.VIEW_LEFT_TOP="viewLeftTop",o))(Ee||{});const dt=0,ut=2,mt=4,gt=10,St="normalTextSelectionPluginName";function Ae(o,i){const e=o.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!e)return null;const t=e.getUnitId(),a=i.getRenderById(t);if(a==null)return;const{mainComponent:s,scene:r,engine:c,components:d}=a,u=s,m=d.get(Z.BACKGROUND);return{document:u,docBackground:m,scene:r,engine:c}}function ht(o,i){const e=i.getRenderById(o);if(e==null)return;const{mainComponent:t,scene:a,engine:s,components:r}=e,c=t,d=r.get(Z.BACKGROUND);return{document:c,docBackground:d,scene:a,engine:s}}const fe={id:"doc.operation.set-selections",type:n.CommandType.OPERATION,handler:(o,i)=>!0};var ft=Object.defineProperty,pt=Object.getOwnPropertyDescriptor,vt=(o,i,e,t)=>{for(var a=t>1?void 0:t?pt(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&ft(i,e,a),a},Ne=(o,i)=>(e,t)=>i(e,t,o);function $(o){const{startOffset:i,endOffset:e,collapsed:t}=o,a={startOffset:i,endOffset:e,collapsed:t};return typeof o.isActive=="function"&&(a.isActive=o.isActive()),a}l.TextSelectionManagerService=class extends n.RxDisposable{constructor(e,t){super();T(this,"_currentSelection",null);T(this,"_textSelectionInfo",new Map);T(this,"_textSelection$",new w.BehaviorSubject(null));T(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=e,this._commandService=t,this._syncSelectionFromRenderService()}getCurrentSelection(){return this._currentSelection}getCurrentSelectionInfo(){return this._getTextRanges(this._currentSelection)}dispose(){this._textSelection$.complete()}refreshSelection(){this._currentSelection!=null&&this._refresh(this._currentSelection)}setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}setCurrentSelectionNotRefresh(e){this._currentSelection=e}getSelections(){var e;return(e=this._getTextRanges(this._currentSelection))==null?void 0:e.textRanges}getActiveTextRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t}=e;return t.find(a=>a.isActive())}getActiveRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t,segmentId:a,style:s}=e,r=t.find(h=>h.isActive());if(r==null)return null;const{startOffset:c,endOffset:d,collapsed:u,startNodePosition:m,endNodePosition:g,direction:S}=r;return c==null||d==null?null:{startOffset:c,endOffset:d,collapsed:u,startNodePosition:m,endNodePosition:g,direction:S,segmentId:a,style:s}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",isEditing:t,style:O.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e,t))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe(w.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:a,subUnitId:s,segmentId:r,style:c,textRanges:d,isEditing:u}=t;this._commandService.executeCommand(fe.id,{unitId:a,subUnitId:s,segmentId:r,style:c,isEditing:u,ranges:d.map($)})}_getTextRanges(e){var s;if(e==null)return;const{unitId:t,subUnitId:a=""}=e;return(s=this._textSelectionInfo.get(t))==null?void 0:s.get(a)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map($))}_replaceByParam(e){const{unitId:t,subUnitId:a,style:s,segmentId:r,textRanges:c,isEditing:d}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(a,{textRanges:c,style:s,segmentId:r,isEditing:d})}_addByParam(e){const{unitId:t,subUnitId:a,textRanges:s,style:r,segmentId:c,isEditing:d}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const u=this._textSelectionInfo.get(t);u.has(a)?u.get(a).textRanges.push(...s):u.set(a,{textRanges:s,style:r,segmentId:c,isEditing:d})}},l.TextSelectionManagerService=vt([Ne(0,O.ITextSelectionRenderManager),Ne(1,n.ICommandService)],l.TextSelectionManagerService);function pe(o,i="",e=0){const{startOffset:t,endOffset:a}=o,s=[],r=t-e,c=a-e;return r>0&&s.push({t:n.TextXActionType.RETAIN,len:r,segmentId:i}),s.push({t:n.TextXActionType.DELETE,len:c-r,line:0,segmentId:i}),s}var It=Object.defineProperty,Ct=Object.getOwnPropertyDescriptor,_t=(o,i,e,t)=>{for(var a=t>1?void 0:t?Ct(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&It(i,e,a),a},Mt=(o,i)=>(e,t)=>i(e,t,o);l.DocViewModelManagerService=class extends n.RxDisposable{constructor(e){super();T(this,"_docViewModelMap",new Map);T(this,"_currentDocViewModel$",new w.BehaviorSubject(null));T(this,"currentDocViewModel$",this._currentDocViewModel$.asObservable());this._univerInstanceService=e,this._initialize()}_initialize(){this._init()}dispose(){this._currentDocViewModel$.complete(),this._docViewModelMap.clear()}_init(){this._univerInstanceService.getCurrentTypeOfUnit$(n.UniverInstanceType.UNIVER_DOC).pipe(w.takeUntil(this.dispose$)).subscribe(e=>{this._create(e)}),this._univerInstanceService.getAllUnitsForType(n.UniverInstanceType.UNIVER_DOC).forEach(e=>{this._create(e)}),this._univerInstanceService.getTypeOfUnitDisposed$(n.UniverInstanceType.UNIVER_DOC).pipe(w.takeUntil(this.dispose$)).subscribe(e=>{this._docViewModelMap.delete(e.getUnitId())})}_create(e){if(e==null)return;const t=e.getUnitId();this._setCurrent(t)}getAllModel(){return this._docViewModelMap}getViewModel(e){var t;return(t=this._docViewModelMap.get(e))==null?void 0:t.docViewModel}_setCurrent(e){var a;const t=this._univerInstanceService.getUniverDocInstance(e);if(t==null)throw new Error(`Document data model with id ${e} not found when build view model.`);if(t.getBody()!=null){if(!this._docViewModelMap.has(e)){const s=this._buildDocViewModel(t);this._docViewModelMap.set(e,{unitId:e,docViewModel:s})}if(e===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY){const s=(a=this._docViewModelMap.get(e))==null?void 0:a.docViewModel;if(s==null)return;s.reset(t)}this._currentDocViewModel$.next(this._docViewModelMap.get(e))}}_buildDocViewModel(e){return new O.DocumentViewModel(e)}},l.DocViewModelManagerService=_t([Mt(0,n.IUniverInstanceService)],l.DocViewModelManagerService);var Tt=Object.defineProperty,yt=Object.getOwnPropertyDescriptor,Ot=(o,i,e,t)=>{for(var a=t>1?void 0:t?yt(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Tt(i,e,a),a},ve=(o,i)=>(e,t)=>i(e,t,o);const be=300;l.DocStateChangeManagerService=class extends n.RxDisposable{constructor(e,t,a){super();T(this,"_docStateChange$",new w.BehaviorSubject(null));T(this,"docStateChange$",this._docStateChange$.asObservable());T(this,"_historyStateCache",new Map);T(this,"_changeStateCache",new Map);T(this,"_historyTimer",null);T(this,"_changeStateCacheTimer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=a,this._initialize()}setChangeState(e){this._cacheChangeState(e,"history"),this._cacheChangeState(e,"collaboration")}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===n.UndoCommandId||e.id===n.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const a=t.getUnitId();this._pushHistory(a),this._emitChangeState(a)}}))}_cacheChangeState(e,t="history"){const{trigger:a,unitId:s,noHistory:r,debounce:c=!1}=e;if(r||t==="history"&&(a===n.RedoCommandId||a===n.UndoCommandId))return;const d=t==="history"?this._historyStateCache:this._changeStateCache,u=t==="history"?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(d.has(s)){const m=d.get(s);m==null||m.push(e)}else d.set(s,[e]);c?t==="history"?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{u(s)},be)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{u(s)},be)):u(s)}_pushHistory(e){const t=this._undoRedoService,a=this._historyStateCache.get(e);if(!Array.isArray(a)||a.length===0)return;const s=a.length,r=a[0].commandId,c=a[0],d=a[s-1],u={unitId:e,actions:a.reduce((g,S)=>n.JSONX.compose(g,S.redoState.actions),null),textRanges:d.redoState.textRanges},m={unitId:e,actions:a.reverse().reduce((g,S)=>n.JSONX.compose(g,S.undoState.actions),null),textRanges:c.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:r,params:m}],redoMutations:[{id:r,params:u}]}),a.length=0}_emitChangeState(e){const t=this._changeStateCache.get(e);if(!Array.isArray(t)||t.length===0)return;const a=t.length,{commandId:s,trigger:r,segmentId:c,noHistory:d,debounce:u}=t[0],m=t[0],g=t[a-1],S={unitId:e,actions:t.reduce((p,f)=>n.JSONX.compose(p,f.redoState.actions),null),textRanges:g.redoState.textRanges},h={unitId:e,actions:t.reverse().reduce((p,f)=>n.JSONX.compose(p,f.undoState.actions),null),textRanges:m.undoState.textRanges},I={commandId:s,unitId:e,trigger:r,redoState:S,undoState:h,segmentId:c,noHistory:d,debounce:u};t.length=0,this._docStateChange$.next(I)}},l.DocStateChangeManagerService=Ot([ve(0,B.Inject(n.IUndoRedoService)),ve(1,n.ICommandService),ve(2,n.IUniverInstanceService)],l.DocStateChangeManagerService);class G{constructor(){T(this,"_previousActiveRange",null);T(this,"_undoMutationParamsCache",[]);T(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:i=[],redoCache:e=[]}){this._undoMutationParamsCache=i,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(i){this._previousActiveRange=i}pushUndoRedoMutationParams(i,e){this._undoMutationParamsCache.push(i),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:i}=this._undoMutationParamsCache[0],e={unitId:i,actions:this._undoMutationParamsCache.reverse().reduce((a,s)=>n.JSONX.compose(a,s.actions),null),textRanges:[]};return{redoMutationParams:{unitId:i,actions:this._redoMutationParamsCache.reduce((a,s)=>n.JSONX.compose(a,s.actions),null),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}const Ue="doc.mutation.rich-text-editing",N={id:Ue,type:n.CommandType.MUTATION,handler:(o,i)=>{var R;const{unitId:e,segmentId:t="",actions:a,textRanges:s,prevTextRanges:r,trigger:c,noHistory:d,isCompositionEnd:u,noNeedSetTextRange:m,debounce:g}=i,h=o.get(n.IUniverInstanceService).getUniverDocInstance(e),p=o.get(l.DocViewModelManagerService).getViewModel(e),f=o.get(l.TextSelectionManagerService),v=((R=f.getSelections())!=null?R:[]).map($),y=o.get(l.DocStateChangeManagerService),_=o.get(G);if(h==null||p==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const M=!!h.getSnapshot().disabled;if(n.JSONX.isNoop(a)||a&&a.length===0||M)return{unitId:e,actions:[],textRanges:v};const D=n.JSONX.invertWithDoc(a,h.getSnapshot());h.apply(a);const x=h.getSelfOrHeaderFooterModel(t);p.getSelfOrHeaderFooterViewModel(t).reset(x),!m&&s&&c!=null&&queueMicrotask(()=>{f.replaceTextRanges(s)});const A={commandId:Ue,unitId:e,segmentId:t,trigger:c,noHistory:d,debounce:g,redoState:{actions:a,textRanges:s},undoState:{actions:D,textRanges:r!=null?r:v}};if(u){const E=_.fetchComposedUndoRedoMutationParams();if(E==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:U,redoMutationParams:k,previousActiveRange:he}=E;A.redoState.actions=k.actions,A.undoState.actions=U.actions,A.undoState.textRanges=[he]}return y.setChangeState(A),{unitId:e,actions:D,textRanges:v}}},Pe="doc.command.insert-text",q={id:Pe,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),{range:t,segmentId:a,body:s,unitId:r,textRanges:c}=i,{startOffset:d,collapsed:u}=t,m={id:N.id,params:{unitId:r,actions:[],textRanges:c,debounce:!0}},g=new n.TextX,S=n.JSONX.getInstance();return u?d>0&&g.push({t:n.TextXActionType.RETAIN,len:d,segmentId:a}):g.push(...pe(t,a)),g.push({t:n.TextXActionType.INSERT,body:s,len:s.dataStream.length,line:0,segmentId:a}),m.params.actions=S.editOp(g.serialize()),!!e.syncExecuteCommand(m.id,m.params)}};var X=(o=>(o[o.LEFT=0]="LEFT",o[o.RIGHT=1]="RIGHT",o))(X||{});const K={id:"doc.command.delete-text",type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),{range:t,segmentId:a,unitId:s,direction:r,textRanges:c,len:d=1}=i,{startOffset:u}=t,m={id:N.id,params:{unitId:s,actions:[],textRanges:c,debounce:!0}},g=new n.TextX,S=n.JSONX.getInstance();return u>0&&g.push({t:n.TextXActionType.RETAIN,len:r===0?u-d:u,segmentId:a}),g.push({t:n.TextXActionType.DELETE,len:d,line:0,segmentId:a}),m.params.actions=S.editOp(g.serialize()),!!e.syncExecuteCommand(m.id,m.params)}},Ie={id:"doc.command.update-text",type:n.CommandType.COMMAND,handler:async(o,i)=>{const{range:e,segmentId:t,updateBody:a,coverType:s,unitId:r,textRanges:c}=i,d=o.get(n.ICommandService),u={id:N.id,params:{unitId:r,actions:[],textRanges:c}},m=new n.TextX,g=n.JSONX.getInstance(),{startOffset:S,endOffset:h}=e;return m.push({t:n.TextXActionType.RETAIN,len:S,segmentId:t}),m.push({t:n.TextXActionType.RETAIN,body:a,len:h-S,segmentId:t,coverType:s}),u.params.actions=g.editOp(m.serialize()),!!d.syncExecuteCommand(u.id,u.params)}};function Rt(o,i){const e=[];for(let t=0,a=o.length;t<a;t++)o[t]===n.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(i)for(const t of e)i.bullet&&(t.bullet=n.Tools.deepClone(i.bullet)),i.paragraphStyle&&(t.paragraphStyle=n.Tools.deepClone(i.paragraphStyle));return e}const we={id:"doc.command.break-line",type:n.CommandType.COMMAND,handler:async o=>{var I,p;const i=o.get(l.TextSelectionManagerService),e=o.get(n.IUniverInstanceService),t=o.get(n.ICommandService),a=i.getActiveRange();if(a==null)return!1;const s=e.getCurrentUniverDocInstance();if(!s)return!1;const r=s.getUnitId(),{startOffset:c,segmentId:d,style:u}=a,m=[{startOffset:c+1,endOffset:c+1,style:u}],S=((p=(I=s.getBody())==null?void 0:I.paragraphs)!=null?p:[]).find(f=>f.startIndex>=c);return await t.executeCommand(q.id,{unitId:r,body:{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:Rt(n.DataStreamTreeTokenType.PARAGRAPH,S)},range:a,textRanges:m,segmentId:d})}};var xt=Object.defineProperty,Dt=Object.getOwnPropertyDescriptor,Et=(o,i,e,t)=>{for(var a=t>1?void 0:t?Dt(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&xt(i,e,a),a},Ce=(o,i)=>(e,t)=>i(e,t,o);l.DocSkeletonManagerService=class extends n.RxDisposable{constructor(e,t,a){super();T(this,"_currentSkeletonUnitId","");T(this,"_docSkeletonMap",new Map);T(this,"_currentSkeleton$",new w.BehaviorSubject(null));T(this,"currentSkeleton$",this._currentSkeleton$.asObservable());T(this,"_currentSkeletonBefore$",new w.BehaviorSubject(null));T(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._localeService=e,this._docViewModelManagerService=t,this._univerInstanceService=a,this._initialize()}_initialize(){this._init()}dispose(){this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete(),this._docSkeletonMap.clear()}_init(){this._docViewModelManagerService.currentDocViewModel$.pipe(w.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._setCurrent(e)}),this._docViewModelManagerService.getAllModel().forEach(e=>{e!=null&&this._setCurrent(e)}),this._univerInstanceService.getTypeOfUnitDisposed$(n.UniverInstanceType.UNIVER_DOC).pipe(w.takeUntil(this.dispose$)).subscribe(e=>{var t,a;this._docSkeletonMap.delete(e.getUnitId()),this._currentSkeletonUnitId=(a=(t=this._univerInstanceService.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC))==null?void 0:t.getUnitId())!=null?a:""})}getCurrent(){return this.getSkeletonByUnitId(this._currentSkeletonUnitId)}getAllSkeleton(){return this._docSkeletonMap}makeDirty(e,t=!0){const a=this.getSkeletonByUnitId(e);a!=null&&(a.dirty=t)}getSkeletonByUnitId(e){return this._docSkeletonMap.get(e)}_setCurrent(e){const{unitId:t}=e;if(this._docSkeletonMap.has(t)){const a=this.getSkeletonByUnitId(t);a.skeleton.calculate(),a.dirty=!0}else{const a=this._buildSkeleton(e.docViewModel);a.calculate(),this._docSkeletonMap.set(t,{unitId:t,skeleton:a,dirty:!1})}return this._currentSkeletonUnitId=t,this._currentSkeletonBefore$.next(this.getCurrent()),this._currentSkeleton$.next(this.getCurrent()),this.getCurrent()}_buildSkeleton(e){return O.DocumentSkeleton.create(e,this._localeService)}},l.DocSkeletonManagerService=Et([Ce(0,B.Inject(n.LocaleService)),Ce(1,B.Inject(l.DocViewModelManagerService)),Ce(2,n.IUniverInstanceService)],l.DocSkeletonManagerService);const At={id:"doc.command.inner-paste",type:n.CommandType.COMMAND,handler:async(o,i)=>{const{segmentId:e,body:t,textRanges:a}=i,s=o.get(n.ICommandService),r=o.get(l.TextSelectionManagerService),c=o.get(n.IUniverInstanceService),d=r.getSelections();if(!Array.isArray(d)||d.length===0)return!1;const u=c.getCurrentUniverDocInstance();if(!u)return!1;const m=u.getUnitId(),g={id:N.id,params:{unitId:m,actions:[],textRanges:a}},S=new n.MemoryCursor;S.reset();const h=new n.TextX,I=n.JSONX.getInstance();for(const f of d){const{startOffset:C,endOffset:v,collapsed:y}=f,_=C-S.cursor;y?h.push({t:n.TextXActionType.RETAIN,len:_,segmentId:e}):h.push(...pe(f,e,S.cursor)),h.push({t:n.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),S.reset(),S.moveCursor(v)}return g.params.actions=I.editOp(h.serialize()),!!s.syncExecuteCommand(g.id,g.params)}},_e={id:"doc.command.inner-cut",type:n.CommandType.COMMAND,handler:async(o,i)=>{var f;const{segmentId:e,textRanges:t}=i,a=o.get(n.ICommandService),s=o.get(l.TextSelectionManagerService),r=o.get(n.IUniverInstanceService),c=s.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const d=(f=r.getCurrentUniverDocInstance())==null?void 0:f.getUnitId();if(!d)return!1;const u=r.getUniverDocInstance(d),m=n.getDocsUpdateBody(u.getSnapshot(),e);if(m==null)return!1;const g={id:N.id,params:{unitId:d,actions:[],textRanges:t}},S=new n.MemoryCursor;S.reset();const h=new n.TextX,I=n.JSONX.getInstance();for(const C of c){const{startOffset:v,endOffset:y,collapsed:_}=C,M=v-S.cursor;_?h.push({t:n.TextXActionType.RETAIN,len:M,segmentId:e}):h.push(...Nt(C,m,e,S.cursor)),S.reset(),S.moveCursor(y)}return g.params.actions=I.editOp(h.serialize()),!!a.syncExecuteCommand(g.id,g.params)}};function Nt(o,i,e="",t=0){const{startOffset:a,endOffset:s}=o,r=[],{paragraphs:c=[]}=i,d=a-t,u=s-t,m=c==null?void 0:c.find(g=>g.startIndex-t>=d&&g.startIndex-t<=u);if(d>0&&r.push({t:n.TextXActionType.RETAIN,len:d,segmentId:e}),m&&m.startIndex-t>d){const g=m.startIndex-t;r.push({t:n.TextXActionType.DELETE,len:g-d,line:0,segmentId:e}),r.push({t:n.TextXActionType.RETAIN,len:1,segmentId:e}),u>g+1&&r.push({t:n.TextXActionType.DELETE,len:u-g-1,line:0,segmentId:e})}else r.push({t:n.TextXActionType.DELETE,len:u-d,line:0,segmentId:e});return r}const Be={id:"doc.command.delete-left",type:n.CommandType.COMMAND,handler:async o=>{var _;const i=o.get(l.TextSelectionManagerService),e=o.get(l.DocSkeletonManagerService),t=o.get(n.IUniverInstanceService),a=o.get(n.ICommandService),s=i.getActiveRange(),r=i.getSelections(),c=(_=e.getCurrent())==null?void 0:_.skeleton;let d=!0;if(s==null||c==null||r==null)return!1;const u=t.getCurrentUniverDocInstance();if(!u)return!1;const{startOffset:m,collapsed:g,segmentId:S,style:h}=s,I=c.findNodeByCharIndex(m),p=O.hasListGlyph(I),f=O.isIndentByGlyph(I,u.getBody());let C=m;const v=c.findNodeByCharIndex(m-1);if(O.isFirstGlyph(I)&&v!==I&&(p===!0||f===!0)&&g){const M=O.getParagraphByGlyph(I,u.getBody());if(M==null)return!1;const D=M==null?void 0:M.startIndex,x={startIndex:0},b=M.paragraphStyle;if(p===!0){const R=M.paragraphStyle;if(R){x.paragraphStyle=R;const{hanging:E}=R;E&&(x.paragraphStyle.indentStart=E,x.paragraphStyle.hanging=void 0)}}else if(f===!0){const R=M.bullet;R&&(x.bullet=R),b!=null&&(x.paragraphStyle={...b},delete x.paragraphStyle.hanging,delete x.paragraphStyle.indentStart)}const A=[{startOffset:C,endOffset:C,style:h}];d=await a.executeCommand(Ie.id,{unitId:u.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...x}]},range:{startOffset:D,endOffset:D+1},textRanges:A,coverType:n.UpdateDocsAttributeType.REPLACE,segmentId:S})}else if(g===!0){if(v==null)return!0;if(v.content==="\r")d=await a.executeCommand(Me.id,{direction:X.LEFT,range:s});else{C-=v.count;const M=[{startOffset:C,endOffset:C,style:h}];d=await a.executeCommand(K.id,{unitId:u.getUnitId(),range:s,segmentId:S,direction:X.LEFT,len:v.count,textRanges:M})}}else{const M=Xe(s,r);d=await a.executeCommand(_e.id,{segmentId:S,textRanges:M})}return d}},Le={id:"doc.command.delete-right",type:n.CommandType.COMMAND,handler:async o=>{var I;const i=o.get(l.TextSelectionManagerService),e=o.get(l.DocSkeletonManagerService),t=o.get(n.IUniverInstanceService),a=o.get(n.ICommandService),s=i.getActiveRange(),r=i.getSelections(),c=(I=e.getCurrent())==null?void 0:I.skeleton;let d;if(s==null||c==null||r==null)return!1;const u=t.getCurrentUniverDocInstance();if(!u)return!1;const{startOffset:m,collapsed:g,segmentId:S,style:h}=s;if(m===u.getBody().dataStream.length-2&&g)return!0;if(g===!0){const p=c.findNodeByCharIndex(m);if(p.content==="\r")d=await a.executeCommand(Me.id,{direction:X.RIGHT,range:s});else{const f=[{startOffset:m,endOffset:m,style:h}];d=await a.executeCommand(K.id,{unitId:u.getUnitId(),range:s,segmentId:S,direction:X.RIGHT,textRanges:f,len:p.count})}}else{const p=Xe(s,r);d=await a.executeCommand(_e.id,{segmentId:S,textRanges:p})}return d}},Me={id:"doc.command.merge-two-paragraph",type:n.CommandType.COMMAND,handler:async(o,i)=>{var b,A,R;const e=o.get(l.TextSelectionManagerService),t=o.get(n.IUniverInstanceService),a=o.get(n.ICommandService),{direction:s,range:r}=i,c=e.getActiveRange(),d=e.getSelections();if(c==null||d==null)return!1;const u=t.getCurrentUniverDocInstance();if(!u)return!1;const{startOffset:m,collapsed:g,segmentId:S,style:h}=c;if(!g)return!1;const I=s===X.LEFT?m:m+1,p=(R=(A=(b=u.getBody())==null?void 0:b.paragraphs)==null?void 0:A.find(E=>E.startIndex>=I))==null?void 0:R.startIndex,f=bt(u.getBody(),I,p),C=s===X.LEFT?m-1:m,v=u.getUnitId(),y=[{startOffset:C,endOffset:C,style:h}],_={id:N.id,params:{unitId:v,actions:[],textRanges:y,prevTextRanges:[r]}},M=new n.TextX,D=n.JSONX.getInstance();return M.push({t:n.TextXActionType.RETAIN,len:s===X.LEFT?m-1:m,segmentId:S}),f.dataStream.length&&M.push({t:n.TextXActionType.INSERT,body:f,len:f.dataStream.length,line:0,segmentId:S}),M.push({t:n.TextXActionType.RETAIN,len:1,segmentId:S}),M.push({t:n.TextXActionType.DELETE,len:p+1-I,line:0,segmentId:S}),_.params.actions=D.editOp(M.serialize()),!!a.syncExecuteCommand(_.id,_.params)}};function bt(o,i,e){const{textRuns:t}=o,a=o.dataStream.substring(i,e);if(t==null)return{dataStream:a};const s=[];for(const r of t){const{st:c,ed:d}=r;d<=i||c>=e||(c<i?s.push({...r,st:0,ed:d-i}):d>e?s.push({...r,st:c-i,ed:e-i}):s.push({...r,st:c-i,ed:d-i}))}return{dataStream:a,textRuns:s}}function Xe(o,i){let e=o.endOffset;for(const a of i){const{startOffset:s,endOffset:r}=a;s==null||r==null||r<=o.endOffset&&(e-=r-s)}return[{startOffset:e,endOffset:e,style:o.style}]}const Te={id:"doc.command.ime-input",type:n.CommandType.COMMAND,handler:async(o,i)=>{const{unitId:e,newText:t,oldTextLen:a,isCompositionEnd:s,isCompositionStart:r}=i,c=o.get(n.ICommandService),d=o.get(G),u=d.getActiveRange();if(u==null)return!1;const{startOffset:m,style:g,segmentId:S}=u,h=t.length,I=[{startOffset:m+h,endOffset:m+h,collapsed:!0,style:g}],p={id:N.id,params:{unitId:e,actions:[],textRanges:I}},f=new n.TextX,C=n.JSONX.getInstance();!u.collapsed&&r?f.push(...pe(u,S)):f.push({t:n.TextXActionType.RETAIN,len:m,segmentId:S}),a>0&&f.push({t:n.TextXActionType.DELETE,len:a,line:0,segmentId:S}),f.push({t:n.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:S}),p.params.actions=C.editOp(f.serialize()),p.params.noHistory=!s,p.params.isCompositionEnd=s;const v=c.syncExecuteCommand(p.id,p.params);return d.pushUndoRedoMutationParams(v,p.params),!!v}};function P(o,i,e,t){var s;const{segmentId:a}=(s=e.getActiveRange())!=null?s:{};return a==null?!1:t.executeCommand(ye.id,{segmentId:a,preCommandId:o,...i!=null?i:{}})}const Fe="doc.command.set-inline-format-bold",Q={id:Fe,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(Fe,i,t,e)}},$e="doc.command.set-inline-format-italic",ee={id:$e,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P($e,i,t,e)}},ke="doc.command.set-inline-format-underline",te={id:ke,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(ke,i,t,e)}},Ve="doc.command.set-inline-format-strikethrough",ne={id:Ve,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(Ve,i,t,e)}},je="doc.command.set-inline-format-subscript",j={id:je,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(je,i,t,e)}},ze="doc.command.set-inline-format-superscript",ie={id:ze,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(ze,i,t,e)}},Ge="doc.command.set-inline-format-fontsize",ae={id:Ge,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(Ge,i,t,e)}},He="doc.command.set-inline-format-font-family",oe={id:He,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(He,i,t,e)}},Je="doc.command.set-inline-format-text-color",se={id:Je,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(Je,i,t,e)}},Ye="doc.command.set-inline-format-text-background-color",re={id:Ye,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(Ye,i,t,e)}},We="doc.command.reset-inline-format-text-background-color",ce={id:We,type:n.CommandType.COMMAND,handler:async(o,i)=>{const e=o.get(n.ICommandService),t=o.get(l.TextSelectionManagerService);return P(We,i,t,e)}},Ze={[Q.id]:"bl",[ee.id]:"it",[te.id]:"ul",[ne.id]:"st",[ae.id]:"fs",[oe.id]:"ff",[se.id]:"cl",[re.id]:"bg",[ce.id]:"bg",[j.id]:"va",[ie.id]:"va"},ye={id:"doc.command.set-inline-format",type:n.CommandType.COMMAND,handler:async(o,i)=>{const{segmentId:e,value:t,preCommandId:a}=i,s=o.get(n.ICommandService),r=o.get(l.TextSelectionManagerService),c=o.get(n.IUniverInstanceService),d=r.getSelections();if(!Array.isArray(d)||d.length===0)return!1;const u=c.getCurrentUniverDocInstance();if(!u)return!1;const m=u.getUnitId();let g;switch(a){case Q.id:case ee.id:case te.id:case ne.id:case j.id:case ie.id:{g=Pt(u.getBody().textRuns,a,d);break}case ae.id:case oe.id:{g=t;break}case se.id:case re.id:{g={rgb:t};break}case ce.id:{g={rgb:null};break}default:throw new Error(`Unknown command: ${a} in handleInlineFormat`)}const S={id:N.id,params:{unitId:m,actions:[],textRanges:d.map($)}},h=new n.TextX,I=n.JSONX.getInstance(),p=new n.MemoryCursor;p.reset();for(const C of d){const{startOffset:v,endOffset:y}=C,_={dataStream:"",textRuns:[{st:0,ed:y-v,ts:{[Ze[a]]:g}}]},M=v-p.cursor;M!==0&&h.push({t:n.TextXActionType.RETAIN,len:M,segmentId:e}),h.push({t:n.TextXActionType.RETAIN,body:_,len:y-v,segmentId:e}),p.reset(),p.moveCursor(y)}return S.params.actions=I.editOp(h.serialize()),!!s.syncExecuteCommand(S.id,S.params)}};function Ut(o){return o!==null&&typeof o=="object"}function Pt(o,i,e){let t=0,a=0;const s=Ze[i];for(;t!==o.length&&a!==e.length;){const{startOffset:r,endOffset:c}=e[a],{st:d,ed:u,ts:m}=o[t];if(c<=d)a++;else if(u<=r)t++;else{if(/bl|it/.test(s))return(m==null?void 0:m[s])===n.BooleanNumber.TRUE?n.BooleanNumber.FALSE:n.BooleanNumber.TRUE;if(/ul|st/.test(s))return Ut(m==null?void 0:m[s])&&(m==null?void 0:m[s]).s===n.BooleanNumber.TRUE?{s:n.BooleanNumber.FALSE}:{s:n.BooleanNumber.TRUE};if(/va/.test(s))return i===j.id?(m==null?void 0:m[s])===n.BaselineOffset.SUBSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUBSCRIPT:(m==null?void 0:m[s])===n.BaselineOffset.SUPERSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(s)?n.BooleanNumber.TRUE:/ul|st/.test(s)?{s:n.BooleanNumber.TRUE}:i===j.id?n.BaselineOffset.SUBSCRIPT:n.BaselineOffset.SUPERSCRIPT}const Oe={id:"doc.command.list-operation",type:n.CommandType.COMMAND,handler:(o,i)=>{var E,U,k,he;const e=o.get(l.TextSelectionManagerService),t=o.get(n.IUniverInstanceService),a=o.get(n.ICommandService),{listType:s}=i,r=t.getCurrentUniverDocInstance();if(!r)return!1;const c=e.getActiveRange(),d=(E=e.getSelections())!=null?E:[],u=(U=r.getBody())==null?void 0:U.paragraphs,m=d.map($);if(c==null||u==null)return!1;const g=Qe(c,u),{segmentId:S}=c,h=r.getUnitId(),I=g.every(V=>{var L;return((L=V.bullet)==null?void 0:L.listType)===s});let f=n.Tools.generateRandomId(6);if(g.length===1){const V=u.indexOf(g[0]),L=u[V-1],F=u[V+1];L&&L.bullet&&L.bullet.listType===s?f=L.bullet.listId:F&&F.bullet&&F.bullet.listType===s&&(f=F.bullet.listId)}const C={id:N.id,params:{unitId:h,actions:[],textRanges:m}},v=new n.MemoryCursor;v.reset();const y=new n.TextX,_=n.JSONX.getInstance(),M=(k=r.getSnapshot().lists)!=null?k:{},D={...n.PRESET_LIST_TYPE,...M},{charSpace:x,defaultTabStop:b=36,gridType:A}=r.getSnapshot().documentStyle;for(const V of g){const{startIndex:L,paragraphStyle:F={}}=V,{indentFirstLine:Zt=0,snapToGrid:qt,indentStart:Re=0}=F,{hanging:xe,indentStart:lt}=D[s].nestingLevel[0],De=O.getCharSpaceApply(x,b,A,qt);y.push({t:n.TextXActionType.RETAIN,len:L-v.cursor,segmentId:S}),y.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[I?{paragraphStyle:{...F,hanging:void 0,indentStart:Re?Math.max(0,O.getNumberUnitValue(Re,De)+xe-lt):void 0},startIndex:0}:{startIndex:0,paragraphStyle:{...F,indentFirstLine:void 0,hanging:xe,indentStart:lt-xe+O.getNumberUnitValue(Zt,De)+O.getNumberUnitValue(Re,De)},bullet:{...(he=V.bullet)!=null?he:{nestingLevel:0,textStyle:{fs:20}},listType:s,listId:f}}]},segmentId:S,coverType:n.UpdateDocsAttributeType.REPLACE}),v.moveCursorTo(L+1)}return C.params.actions=_.editOp(y.serialize()),!!a.syncExecuteCommand(C.id,C.params)}},qe={id:"doc.command.bullet-list",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(Oe.id,{listType:n.PresetListType.BULLET_LIST})},Ke={id:"doc.command.order-list",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(Oe.id,{listType:n.PresetListType.ORDER_LIST})};function Qe(o,i){const{startOffset:e,endOffset:t}=o,a=[];let s=-1;for(const r of i){const{startIndex:c}=r;(e>s&&e<=c||t>s&&t<=c||c>=e&&c<=t)&&a.push(r),s=c}return a}const z={id:"doc.command.align-operation",type:n.CommandType.COMMAND,handler:(o,i)=>{var _,M;const e=o.get(l.TextSelectionManagerService),t=o.get(n.IUniverInstanceService),a=o.get(n.ICommandService),{alignType:s}=i,r=t.getCurrentUniverDocInstance();if(!r)return!1;const c=e.getActiveRange(),d=(_=e.getSelections())!=null?_:[],u=(M=r.getBody())==null?void 0:M.paragraphs,m=d.map($);if(c==null||u==null)return!1;const g=Qe(c,u),{segmentId:S}=c,h=r.getUnitId(),I=g.every(D=>{var x;return((x=D.paragraphStyle)==null?void 0:x.horizontalAlign)===s}),p={id:N.id,params:{unitId:h,actions:[],textRanges:m}},f=new n.MemoryCursor;f.reset();const C=new n.TextX,v=n.JSONX.getInstance();for(const D of g){const{startIndex:x}=D;C.push({t:n.TextXActionType.RETAIN,len:x-f.cursor,segmentId:S});const b={...D.paragraphStyle,horizontalAlign:I?n.HorizontalAlign.UNSPECIFIED:s};C.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...D,paragraphStyle:b,startIndex:0}]},segmentId:S,coverType:n.UpdateDocsAttributeType.REPLACE}),f.moveCursorTo(x+1)}return p.params.actions=v.editOp(C.serialize()),!!a.syncExecuteCommand(p.id,p.params)}},et={id:"doc.command.align-left",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.LEFT})},tt={id:"doc.command.align-center",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.CENTER})},nt={id:"doc.command.align-right",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.RIGHT})},it={id:"doc.command.align-justify",type:n.CommandType.COMMAND,handler:o=>o.get(n.ICommandService).syncExecuteCommand(z.id,{alignType:n.HorizontalAlign.JUSTIFIED})},at={id:"doc.command-replace-content",type:n.CommandType.COMMAND,handler:async(o,i)=>{var h;const{unitId:e,body:t,textRanges:a,segmentId:s=""}=i,r=o.get(n.IUniverInstanceService),c=o.get(n.ICommandService),d=o.get(l.TextSelectionManagerService),u=(h=r.getUniverDocInstance(e))==null?void 0:h.getSnapshot().body,m=d.getSelections();if(u==null||!Array.isArray(m)||m.length===0)return!1;const g=st(e,s,u,t);return g.params.textRanges=a,!!c.syncExecuteCommand(g.id,g.params)}},ot={id:"doc.command-cover-content",type:n.CommandType.COMMAND,handler:async(o,i)=>{var m;const{unitId:e,body:t,segmentId:a=""}=i,s=o.get(n.IUniverInstanceService),r=o.get(n.ICommandService),c=o.get(n.IUndoRedoService),d=(m=s.getUniverDocInstance(e))==null?void 0:m.getSnapshot().body;if(d==null)return!1;const u=st(e,a,d,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,r.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function st(o,i,e,t){const a={id:N.id,params:{unitId:o,actions:[],textRanges:[]}},s=new n.TextX,r=n.JSONX.getInstance(),c=(e==null?void 0:e.dataStream.length)-2;return c>0&&s.push({t:n.TextXActionType.DELETE,len:c,line:0,segmentId:i}),t.dataStream.length>0&&s.push({t:n.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:i}),a.params.actions=r.editOp(s.serialize()),a}const le={id:"doc.operation.move-cursor",type:n.CommandType.OPERATION,handler:(o,i)=>!!i},de={id:"doc.operation.move-selection",type:n.CommandType.OPERATION,handler:(o,i)=>!!i},wt=(o,i)=>{const e=o.get(n.IUniverInstanceService).getUniverDocInstance(i.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...n.Tools.deepClone(i),zoomRatio:t}},H={id:"doc.operation.set-zoom-ratio",type:n.CommandType.OPERATION,handler:(o,i)=>{const e=o.get(n.IUniverInstanceService).getUniverDocInstance(i.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:i.zoomRatio}:t.settings.zoomRatio=i.zoomRatio,!0}},rt={type:n.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(o,i)=>{var g,S,h;const e=o.get(n.ICommandService),t=o.get(n.IUndoRedoService),a=o.get(n.IUniverInstanceService);let s=(g=a.getCurrentUniverDocInstance())==null?void 0:g.getUnitId();if(!s)return!1;let r=1;if(i&&(s=(S=i.documentId)!=null?S:s,r=(h=i.zoomRatio)!=null?h:r),!a.getUniverDocInstance(s))return!1;const d={zoomRatio:r,unitId:s},u=wt(o,d);return e.syncExecuteCommand(H.id,d)?(t.pushUndoRedo({unitID:s,undoMutations:[{id:H.id,params:u}],redoMutations:[{id:H.id,params:d}]}),!0):!1}},ct={id:"doc.operation.select-all",type:n.CommandType.COMMAND,handler:async o=>{const i=o.get(n.IUniverInstanceService),e=o.get(l.TextSelectionManagerService),t=i.getCurrentUniverDocInstance();if(!t)return!1;const a=t.getSnapshot().body;if(a==null)return!1;const s=[{startOffset:0,endOffset:a.dataStream.length-2}];return e.replaceTextRanges(s,!1),!0}};var Bt=Object.defineProperty,Lt=Object.getOwnPropertyDescriptor,Xt=(o,i,e,t)=>{for(var a=t>1?void 0:t?Lt(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Bt(i,e,a),a},J=(o,i)=>(e,t)=>i(e,t,o);let ue=class extends n.Disposable{constructor(i,e,t,a,s){super();T(this,"_previousIMEContent","");T(this,"_isCompositionStart",!0);T(this,"_onStartSubscription");T(this,"_onUpdateSubscription");T(this,"_onEndSubscription");this._docSkeletonManagerService=i,this._univerInstanceService=e,this._textSelectionRenderManager=t,this._imeInputManagerService=a,this._commandService=s,this._initialize()}dispose(){var i,e,t;(i=this._onStartSubscription)==null||i.unsubscribe(),(e=this._onUpdateSubscription)==null||e.unsubscribe(),(t=this._onEndSubscription)==null||t.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(i=>{if(i==null)return;this._resetIME();const{activeRange:e}=i;e!=null&&this._imeInputManagerService.setActiveRange(n.Tools.deepClone(e))})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async i=>{this._updateContent(i,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(i=>{this._updateContent(i,!1)})}async _updateContent(i,e){var u;const t=(u=this._docSkeletonManagerService.getCurrent())==null?void 0:u.skeleton;if(i==null||t==null)return;const a=this._univerInstanceService.getCurrentUniverDocInstance();if(!a)return;const{event:s,activeRange:r}=i;if(t==null||r==null)return;const d=s.data;d===this._previousIMEContent&&e||(await this._commandService.executeCommand(Te.id,{unitId:a.getUnitId(),newText:d,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=d):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};ue=Xt([n.OnLifecycle(n.LifecycleStages.Rendered,ue),J(0,B.Inject(l.DocSkeletonManagerService)),J(1,n.IUniverInstanceService),J(2,O.ITextSelectionRenderManager),J(3,B.Inject(G)),J(4,n.ICommandService)],ue);var Ft=Object.defineProperty,$t=Object.getOwnPropertyDescriptor,kt=(o,i,e,t)=>{for(var a=t>1?void 0:t?$t(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Ft(i,e,a),a},Y=(o,i)=>(e,t)=>i(e,t,o);let me=class extends n.Disposable{constructor(i,e,t,a,s){super();T(this,"_onInputSubscription");this._docSkeletonManagerService=i,this._univerInstanceService=e,this._renderManagerService=t,this._textSelectionManagerService=a,this._commandService=s,this._commandExecutedListener()}dispose(){var i;super.dispose(),(i=this._onInputSubscription)==null||i.unsubscribe()}_commandExecutedListener(){const i=[le.id,de.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!i.includes(e.id))return;const t=e.params;switch(e.id){case le.id:return this._handleMoveCursor(t.direction);case de.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(i){var p,f,C;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),a=this._univerInstanceService.getCurrentUniverDocInstance();if(!a)return;const s=(p=this._docSkeletonManagerService.getCurrent())==null?void 0:p.skeleton,r=this._getDocObject();if(e==null||s==null||r==null)return;const{startOffset:c,endOffset:d,style:u,collapsed:m,direction:g}=e;if(t.length>1){let v=Number.POSITIVE_INFINITY,y=Number.NEGATIVE_INFINITY;for(const _ of t)v=Math.min(v,_.startOffset),y=Math.max(y,_.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:i===n.Direction.LEFT||i===n.Direction.UP?y:v,endOffset:i===n.Direction.LEFT||i===n.Direction.UP?v:y,style:u}],!1);return}const S=m||g===O.RANGE_DIRECTION.FORWARD?c:d;let h=m||g===O.RANGE_DIRECTION.FORWARD?d:c;const I=(f=a.getBody().dataStream.length)!=null?f:Number.POSITIVE_INFINITY;if(i===n.Direction.LEFT||i===n.Direction.RIGHT){const v=s.findNodeByCharIndex(h-1),y=s.findNodeByCharIndex(h);h=i===n.Direction.RIGHT?h+y.count:h-((C=v==null?void 0:v.count)!=null?C:0),h=Math.min(I-2,Math.max(0,h)),this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:h,style:u}],!1)}else{const v=s.findNodeByCharIndex(h),y=r.document.getOffsetConfig(),_=this._getTopOrBottomPosition(s,v,i===n.Direction.DOWN);if(_==null){const D=i===n.Direction.UP?0:I-2;if(D===h)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:D,style:u}],!1);return}const M=new O.NodePositionConvertToCursor(y,s).getRangePointData(_,_).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:M.endOffset,style:u}],!1)}}_handleMoveCursor(i){var S,h,I;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),a=this._univerInstanceService.getCurrentUniverDocInstance();if(!a)return!1;const s=(S=this._docSkeletonManagerService.getCurrent())==null?void 0:S.skeleton,r=this._getDocObject();if(e==null||s==null||r==null||t==null)return;const{startOffset:c,endOffset:d,style:u,collapsed:m}=e,g=(h=a.getBody().dataStream.length)!=null?h:Number.POSITIVE_INFINITY;if(i===n.Direction.LEFT||i===n.Direction.RIGHT){let p;if(!e.collapsed||t.length>1){let f=Number.POSITIVE_INFINITY,C=Number.NEGATIVE_INFINITY;for(const v of t)f=Math.min(f,v.startOffset),C=Math.max(C,v.endOffset);p=i===n.Direction.LEFT?f:C}else{const f=s.findNodeByCharIndex(c-1),C=s.findNodeByCharIndex(c);i===n.Direction.LEFT?p=Math.max(0,c-((I=f==null?void 0:f.count)!=null?I:0)):p=Math.min(g-2,d+C.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:p,endOffset:p,style:u}],!1)}else{const p=s.findNodeByCharIndex(c),f=s.findNodeByCharIndex(d),C=r.document.getOffsetConfig(),v=this._getTopOrBottomPosition(s,i===n.Direction.UP?p:f,i===n.Direction.DOWN);if(v==null){let _;m?_=i===n.Direction.UP?0:g-2:_=i===n.Direction.UP?c:d,this._textSelectionManagerService.replaceTextRanges([{startOffset:_,endOffset:_,style:u}],!1);return}const y=new O.NodePositionConvertToCursor(C,s).getRangePointData(v,v).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{...y,style:u}],!1)}}_getTopOrBottomPosition(i,e,t){if(e==null)return;const a=this._getSpanLeftOffsetInLine(e),s=this._getNextOrPrevLine(e,t);if(s==null)return;const r=this._matchPositionByLeftOffset(i,s,a);if(r!=null)return{...r,isBack:!0}}_getSpanLeftOffsetInLine(i){const e=i.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:a}=i;return t+a}_matchPositionByLeftOffset(i,e,t){const a={distance:Number.POSITIVE_INFINITY};for(const s of e.divides){const r=s.left;for(const c of s.glyphGroup){const{left:d}=c,u=r+d,m=Math.abs(t-u);m<a.distance&&(a.glyph=c,a.distance=m)}}if(a.glyph!=null)return i.findPositionByGlyph(a.glyph)}_getNextOrPrevLine(i,e){var I,p,f,C,v,y,_,M,D,x,b,A;const t=i.parent;if(t==null)return;const a=t.parent;if(a==null)return;const s=a.parent;if(s==null)return;const r=s.lines.indexOf(a);if(r===-1)return;let c;if(e===!0?c=s.lines[r+1]:c=s.lines[r-1],c!=null)return c;const d=s.parent;if(d==null)return;const u=d.columns.indexOf(s);if(u===-1)return;if(e===!0)c=(I=d.columns[u+1])==null?void 0:I.lines[0];else{const R=(f=(p=d.columns)==null?void 0:p[u-1])==null?void 0:f.lines;c=R==null?void 0:R[R.length-1]}if(c!=null)return c;const m=d.parent;if(m==null)return;const g=m.sections.indexOf(d);if(g===-1)return;if(e===!0)c=(v=(C=m.sections[g-1])==null?void 0:C.columns[0])==null?void 0:v.lines[0];else{const R=(_=(y=m.sections)==null?void 0:y[g-1])==null?void 0:_.columns,E=R==null?void 0:R[R.length-1],U=E==null?void 0:E.lines;c=U==null?void 0:U[U.length-1]}if(c!=null)return c;const S=m.parent;if(S==null)return;const h=S.pages.indexOf(m);if(h!==-1){if(e===!0)c=(x=(D=(M=S.pages[h+1])==null?void 0:M.sections[0])==null?void 0:D.columns[0])==null?void 0:x.lines[0];else{const R=(b=S.pages[h-1])==null?void 0:b.sections;if(R==null)return;const E=(A=R[R.length-1])==null?void 0:A.columns,U=E[E.length-1],k=U==null?void 0:U.lines;c=k[k.length-1]}if(c!=null)return c}}_getDocObject(){return Ae(this._univerInstanceService,this._renderManagerService)}};me=kt([n.OnLifecycle(n.LifecycleStages.Rendered,me),Y(0,B.Inject(l.DocSkeletonManagerService)),Y(1,n.IUniverInstanceService),Y(2,O.IRenderManagerService),Y(3,B.Inject(l.TextSelectionManagerService)),Y(4,n.ICommandService)],me);var Vt=Object.defineProperty,jt=Object.getOwnPropertyDescriptor,zt=(o,i,e,t)=>{for(var a=t>1?void 0:t?jt(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Vt(i,e,a),a},W=(o,i)=>(e,t)=>i(e,t,o);let ge=class extends n.Disposable{constructor(i,e,t,a,s){super();T(this,"_onInputSubscription");this._docSkeletonManagerService=i,this._univerInstanceService=e,this._renderManagerService=t,this._textSelectionRenderManager=a,this._commandService=s,this._init()}dispose(){var i;super.dispose(),(i=this._onInputSubscription)==null||i.unsubscribe()}_init(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async i=>{var I;if(i==null)return;const e=this._univerInstanceService.getCurrentUniverDocInstance();if(!e)return;const t=e.getUnitId(),{event:a,content:s="",activeRange:r}=i,c=a,d=(I=this._docSkeletonManagerService.getCurrent())==null?void 0:I.skeleton;if(c.data==null||d==null||!d||!r)return;const{startOffset:u,segmentId:m,style:g}=r,S=s.length,h=[{startOffset:u+S,endOffset:u+S,style:g}];await this._commandService.executeCommand(q.id,{unitId:t,body:{dataStream:s},range:r,textRanges:h,segmentId:m})})}};ge=zt([n.OnLifecycle(n.LifecycleStages.Rendered,ge),W(0,B.Inject(l.DocSkeletonManagerService)),W(1,n.IUniverInstanceService),W(2,O.IRenderManagerService),W(3,O.ITextSelectionRenderManager),W(4,n.ICommandService)],ge);var Gt=Object.defineProperty,Ht=Object.getOwnPropertyDescriptor,Jt=(o,i,e,t)=>{for(var a=t>1?void 0:t?Ht(i,e):i,s=o.length-1,r;s>=0;s--)(r=o[s])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Gt(i,e,a),a},Yt=(o,i)=>(e,t)=>i(e,t,o);const Wt="docs";l.UniverDocsPlugin=(Se=class extends n.Plugin{constructor(i={},e){super(),this._injector=e,this._initializeDependencies(e),this._initializeCommands()}_initializeCommands(){[le,de,Be,Le,Q,ee,te,ne,j,ie,ae,oe,se,ce,re,ye,we,q,K,Ie,Te,Me,N,at,ot,rt,H,fe,ct,Ke,qe,Oe,et,tt,nt,z,it].forEach(i=>{this._injector.get(n.ICommandService).registerCommand(i)})}_initializeDependencies(i){[[l.DocSkeletonManagerService],[l.DocViewModelManagerService],[l.DocStateChangeManagerService],[G],[O.ITextSelectionRenderManager,{useClass:O.TextSelectionRenderManager}],[l.TextSelectionManagerService],[ge],[ue],[me]].forEach(e=>i.add(e))}},T(Se,"pluginName",Wt),T(Se,"type",n.UniverInstanceType.UNIVER_DOC),Se),l.UniverDocsPlugin=Jt([Yt(1,B.Inject(B.Injector))],l.UniverDocsPlugin),l.AlignCenterCommand=tt,l.AlignJustifyCommand=it,l.AlignLeftCommand=et,l.AlignOperationCommand=z,l.AlignRightCommand=nt,l.BreakLineCommand=we,l.BulletListCommand=qe,l.CoverContentCommand=ot,l.CutContentCommand=_e,l.DOCS_COMPONENT_BACKGROUND_LAYER_INDEX=dt,l.DOCS_COMPONENT_DEFAULT_Z_INDEX=gt,l.DOCS_COMPONENT_HEADER_LAYER_INDEX=mt,l.DOCS_COMPONENT_MAIN_LAYER_INDEX=ut,l.DOCS_VIEW_KEY=Z,l.DeleteCommand=K,l.DeleteLeftCommand=Be,l.DeleteRightCommand=Le,l.EditorInsertTextCommandId=Pe,l.IMEInputCommand=Te,l.IMEInputManagerService=G,l.InnerPasteCommand=At,l.InsertCommand=q,l.MoveCursorOperation=le,l.MoveSelectionOperation=de,l.NORMAL_TEXT_SELECTION_PLUGIN_NAME=St,l.OrderListCommand=Ke,l.ReplaceContentCommand=at,l.ResetInlineFormatTextBackgroundColorCommand=ce,l.RichTextEditingMutation=N,l.SelectAllOperation=ct,l.SetDocZoomRatioCommand=rt,l.SetDocZoomRatioOperation=H,l.SetInlineFormatBoldCommand=Q,l.SetInlineFormatCommand=ye,l.SetInlineFormatFontFamilyCommand=oe,l.SetInlineFormatFontSizeCommand=ae,l.SetInlineFormatItalicCommand=ee,l.SetInlineFormatStrikethroughCommand=ne,l.SetInlineFormatSubscriptCommand=j,l.SetInlineFormatSuperscriptCommand=ie,l.SetInlineFormatTextBackgroundColorCommand=re,l.SetInlineFormatTextColorCommand=se,l.SetInlineFormatUnderlineCommand=te,l.SetTextSelectionsOperation=fe,l.UpdateCommand=Ie,l.VIEWPORT_KEY=Ee,l.getDocObject=Ae,l.getDocObjectById=ht,l.serializeTextRange=$,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(d,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("@univerjs/core"),require("@univerjs/engine-render"),require("rxjs"),require("@wendellhu/redi")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/engine-render","rxjs","@wendellhu/redi"],n):(d=typeof globalThis<"u"?globalThis:d||self,n(d.UniverDocs={},d.UniverCore,d.UniverEngineRender,d.rxjs,d["@wendellhu/redi"]))})(this,function(d,n,M,$,z){"use strict";var Zt=Object.defineProperty;var Kt=(d,n,M)=>n in d?Zt(d,n,{enumerable:!0,configurable:!0,writable:!0,value:M}):d[n]=M;var O=(d,n,M)=>Kt(d,typeof n!="symbol"?n+"":n,M);var Se;var V=(s=>(s.MAIN="__Document_Render_Main__",s.BACKGROUND="__Document_Render_Background__",s))(V||{}),De=(s=>(s.VIEW_MAIN="viewMain",s.VIEW_TOP="viewTop",s.VIEW_LEFT="viewLeft",s.VIEW_LEFT_TOP="viewLeftTop",s))(De||{});const ut=0,mt=2,gt=4,St=10,ft="normalTextSelectionPluginName";function ht(s){const{mainComponent:i,scene:e,engine:t,components:a}=s,o=i,r=a.get(V.BACKGROUND);return{document:o,docBackground:r,scene:e,engine:t}}function Ee(s,i){const e=s.getCurrentUnitForType(n.UniverInstanceType.UNIVER_DOC);if(!e)return null;const t=e.getUnitId(),a=i.getRenderById(t);if(a==null)return;const{mainComponent:o,scene:r,engine:c,components:l}=a,u=o,m=l.get(V.BACKGROUND);return{document:u,docBackground:m,scene:r,engine:c}}function pt(s,i){const e=i.getRenderById(s);if(e==null)return;const{mainComponent:t,scene:a,engine:o,components:r}=e,c=t,l=r.get(V.BACKGROUND);return{document:c,docBackground:l,scene:a,engine:o}}const he={id:"doc.operation.set-selections",type:n.CommandType.OPERATION,handler:(s,i)=>!0};var It=Object.defineProperty,vt=Object.getOwnPropertyDescriptor,Ct=(s,i,e,t)=>{for(var a=t>1?void 0:t?vt(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&It(i,e,a),a},Ae=(s,i)=>(e,t)=>i(e,t,s);function F(s){const{startOffset:i,endOffset:e,collapsed:t}=s,a={startOffset:i,endOffset:e,collapsed:t};return typeof s.isActive=="function"&&(a.isActive=s.isActive()),a}d.TextSelectionManagerService=class extends n.RxDisposable{constructor(e,t){super();O(this,"_currentSelection",null);O(this,"_textSelectionInfo",new Map);O(this,"_textSelection$",new $.BehaviorSubject(null));O(this,"textSelection$",this._textSelection$.asObservable());this._textSelectionRenderManager=e,this._commandService=t,this._syncSelectionFromRenderService()}getCurrentSelection(){return this._currentSelection}getCurrentSelectionInfo(){return this._getTextRanges(this._currentSelection)}dispose(){this._textSelection$.complete()}refreshSelection(){this._currentSelection!=null&&this._refresh(this._currentSelection)}setCurrentSelection(e){this._currentSelection=e,this._refresh(e)}setCurrentSelectionNotRefresh(e){this._currentSelection=e}getSelections(){var e;return(e=this._getTextRanges(this._currentSelection))==null?void 0:e.textRanges}getActiveTextRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t}=e;return t.find(a=>a.isActive())}getActiveRange(){const e=this._getTextRanges(this._currentSelection);if(e==null)return;const{textRanges:t,segmentId:a,style:o}=e,r=t.find(f=>f.isActive());if(r==null)return null;const{startOffset:c,endOffset:l,collapsed:u,startNodePosition:m,endNodePosition:g,direction:S}=r;return c==null||l==null?null:{startOffset:c,endOffset:l,collapsed:u,startNodePosition:m,endNodePosition:g,direction:S,segmentId:a,style:o}}add(e,t=!0){this._currentSelection!=null&&this._addByParam({...this._currentSelection,textRanges:e,segmentId:"",isEditing:t,style:M.NORMAL_TEXT_SELECTION_PLUGIN_STYLE})}replaceTextRanges(e,t=!0){this._currentSelection!=null&&(this._textSelectionRenderManager.removeAllTextRanges(),this._textSelectionRenderManager.addTextRanges(e,t))}_syncSelectionFromRenderService(){this._textSelectionRenderManager.textSelectionInner$.pipe($.takeUntil(this.dispose$)).subscribe(e=>{e!=null&&this._replaceTextRangesWithNoRefresh(e)})}_replaceTextRangesWithNoRefresh(e){if(this._currentSelection==null)return;const t={...this._currentSelection,...e};this._replaceByParam(t),this._textSelection$.next(t);const{unitId:a,subUnitId:o,segmentId:r,style:c,textRanges:l,isEditing:u}=t;this._commandService.executeCommand(he.id,{unitId:a,subUnitId:o,segmentId:r,style:c,isEditing:u,ranges:l.map(F)})}_getTextRanges(e){var o;if(e==null)return;const{unitId:t,subUnitId:a=""}=e;return(o=this._textSelectionInfo.get(t))==null?void 0:o.get(a)}_refresh(e){const t=this._getTextRanges(e);this._textSelectionRenderManager.removeAllTextRanges(),t&&Array.isArray(t.textRanges)&&t.textRanges.length&&this._textSelectionRenderManager.addTextRanges(t.textRanges.map(F))}_replaceByParam(e){const{unitId:t,subUnitId:a,style:o,segmentId:r,textRanges:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map),this._textSelectionInfo.get(t).set(a,{textRanges:c,style:o,segmentId:r,isEditing:l})}_addByParam(e){const{unitId:t,subUnitId:a,textRanges:o,style:r,segmentId:c,isEditing:l}=e;this._textSelectionInfo.has(t)||this._textSelectionInfo.set(t,new Map);const u=this._textSelectionInfo.get(t);u.has(a)?u.get(a).textRanges.push(...o):u.set(a,{textRanges:o,style:r,segmentId:c,isEditing:l})}},d.TextSelectionManagerService=Ct([Ae(0,M.ITextSelectionRenderManager),Ae(1,n.ICommandService)],d.TextSelectionManagerService);function pe(s,i="",e=0){const{startOffset:t,endOffset:a}=s,o=[],r=t-e,c=a-e;return r>0&&o.push({t:n.TextXActionType.RETAIN,len:r,segmentId:i}),o.push({t:n.TextXActionType.DELETE,len:c-r,line:0,segmentId:i}),o}var _t=Object.defineProperty,Tt=Object.getOwnPropertyDescriptor,yt=(s,i,e,t)=>{for(var a=t>1?void 0:t?Tt(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&_t(i,e,a),a},Ie=(s,i)=>(e,t)=>i(e,t,s);const Ne=300;d.DocStateChangeManagerService=class extends n.RxDisposable{constructor(e,t,a){super();O(this,"_docStateChange$",new $.BehaviorSubject(null));O(this,"docStateChange$",this._docStateChange$.asObservable());O(this,"_historyStateCache",new Map);O(this,"_changeStateCache",new Map);O(this,"_historyTimer",null);O(this,"_changeStateCacheTimer",null);this._undoRedoService=e,this._commandService=t,this._univerInstanceService=a,this._initialize()}setChangeState(e){this._cacheChangeState(e,"history"),this._cacheChangeState(e,"collaboration")}_initialize(){this.disposeWithMe(this._commandService.beforeCommandExecuted(e=>{if(e.id===n.UndoCommandId||e.id===n.RedoCommandId){const t=this._univerInstanceService.getCurrentUniverDocInstance();if(t==null)return;const a=t.getUnitId();this._pushHistory(a),this._emitChangeState(a)}}))}_cacheChangeState(e,t="history"){const{trigger:a,unitId:o,noHistory:r,debounce:c=!1}=e;if(r||t==="history"&&(a===n.RedoCommandId||a===n.UndoCommandId))return;const l=t==="history"?this._historyStateCache:this._changeStateCache,u=t==="history"?this._pushHistory.bind(this):this._emitChangeState.bind(this);if(l.has(o)){const m=l.get(o);m==null||m.push(e)}else l.set(o,[e]);c?t==="history"?(this._historyTimer&&clearTimeout(this._historyTimer),this._historyTimer=setTimeout(()=>{u(o)},Ne)):(this._changeStateCacheTimer&&clearTimeout(this._changeStateCacheTimer),this._changeStateCacheTimer=setTimeout(()=>{u(o)},Ne)):u(o)}_pushHistory(e){const t=this._undoRedoService,a=this._historyStateCache.get(e);if(!Array.isArray(a)||a.length===0)return;const o=a.length,r=a[0].commandId,c=a[0],l=a[o-1],u={unitId:e,actions:a.reduce((g,S)=>n.JSONX.compose(g,S.redoState.actions),null),textRanges:l.redoState.textRanges},m={unitId:e,actions:a.reverse().reduce((g,S)=>n.JSONX.compose(g,S.undoState.actions),null),textRanges:c.undoState.textRanges};t.pushUndoRedo({unitID:e,undoMutations:[{id:r,params:m}],redoMutations:[{id:r,params:u}]}),a.length=0}_emitChangeState(e){const t=this._changeStateCache.get(e);if(!Array.isArray(t)||t.length===0)return;const a=t.length,{commandId:o,trigger:r,segmentId:c,noHistory:l,debounce:u}=t[0],m=t[0],g=t[a-1],S={unitId:e,actions:t.reduce((h,p)=>n.JSONX.compose(h,p.redoState.actions),null),textRanges:g.redoState.textRanges},f={unitId:e,actions:t.reverse().reduce((h,p)=>n.JSONX.compose(h,p.undoState.actions),null),textRanges:m.undoState.textRanges},v={commandId:o,unitId:e,trigger:r,redoState:S,undoState:f,segmentId:c,noHistory:l,debounce:u};t.length=0,this._docStateChange$.next(v)}},d.DocStateChangeManagerService=yt([Ie(0,z.Inject(n.IUndoRedoService)),Ie(1,n.ICommandService),Ie(2,n.IUniverInstanceService)],d.DocStateChangeManagerService);class H{constructor(){O(this,"_previousActiveRange",null);O(this,"_undoMutationParamsCache",[]);O(this,"_redoMutationParamsCache",[])}clearUndoRedoMutationParamsCache(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[]}getUndoRedoMutationParamsCache(){return{undoCache:this._undoMutationParamsCache,redoCache:this._redoMutationParamsCache}}setUndoRedoMutationParamsCache({undoCache:i=[],redoCache:e=[]}){this._undoMutationParamsCache=i,this._redoMutationParamsCache=e}getActiveRange(){return this._previousActiveRange}setActiveRange(i){this._previousActiveRange=i}pushUndoRedoMutationParams(i,e){this._undoMutationParamsCache.push(i),this._redoMutationParamsCache.push(e)}fetchComposedUndoRedoMutationParams(){if(this._undoMutationParamsCache.length===0||this._previousActiveRange==null||this._redoMutationParamsCache.length===0)return null;const{unitId:i}=this._undoMutationParamsCache[0],e={unitId:i,actions:this._undoMutationParamsCache.reverse().reduce((a,o)=>n.JSONX.compose(a,o.actions),null),textRanges:[]};return{redoMutationParams:{unitId:i,actions:this._redoMutationParamsCache.reduce((a,o)=>n.JSONX.compose(a,o.actions),null),textRanges:[]},undoMutationParams:e,previousActiveRange:this._previousActiveRange}}dispose(){this._undoMutationParamsCache=[],this._redoMutationParamsCache=[],this._previousActiveRange=null}}var Mt=Object.defineProperty,Ot=Object.getOwnPropertyDescriptor,Rt=(s,i,e,t)=>{for(var a=t>1?void 0:t?Ot(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Mt(i,e,a),a},be=(s,i)=>(e,t)=>i(e,t,s);d.DocSkeletonManagerService=class extends n.RxDisposable{constructor(e,t,a){super();O(this,"_skeleton");O(this,"_docViewModel");O(this,"_currentSkeleton$",new $.BehaviorSubject(null));O(this,"currentSkeleton$",this._currentSkeleton$.asObservable());O(this,"_currentSkeletonBefore$",new $.BehaviorSubject(null));O(this,"currentSkeletonBefore$",this._currentSkeletonBefore$.asObservable());this._context=e,this._localeService=t,this._univerInstanceService=a,this._init(),this._univerInstanceService.getCurrentTypeOfUnit$(n.UniverInstanceType.UNIVER_DOC).pipe($.takeUntil(this.dispose$)).subscribe(o=>{(o==null?void 0:o.getUnitId())===this._context.unitId&&this._update(o)})}dispose(){super.dispose(),this._currentSkeletonBefore$.complete(),this._currentSkeleton$.complete()}_init(){const e=this._context.unit;this._update(e)}_update(e){const t=this._context.unitId;if(e.getBody()==null)return;this._docViewModel&&t===n.DOCS_NORMAL_EDITOR_UNIT_ID_KEY?(this._docViewModel.reset(e),this._context.unit=e):this._docViewModel||(this._docViewModel=this._buildDocViewModel(e)),this._skeleton||(this._skeleton=this._buildSkeleton(this._docViewModel));const a=this._skeleton;a.calculate(),this._currentSkeletonBefore$.next(a),this._currentSkeleton$.next(a)}getSkeleton(){return this._skeleton}getViewModel(){return this._docViewModel}_buildSkeleton(e){return M.DocumentSkeleton.create(e,this._localeService)}_buildDocViewModel(e){return new M.DocumentViewModel(e)}},d.DocSkeletonManagerService=Rt([be(1,z.Inject(n.LocaleService)),be(2,n.IUniverInstanceService)],d.DocSkeletonManagerService);const Pe="doc.mutation.rich-text-editing",b={id:Pe,type:n.CommandType.MUTATION,handler:(s,i)=>{var R,E;const{unitId:e,segmentId:t="",actions:a,textRanges:o,prevTextRanges:r,trigger:c,noHistory:l,isCompositionEnd:u,noNeedSetTextRange:m,debounce:g}=i,S=s.get(n.IUniverInstanceService),f=s.get(M.IRenderManagerService),v=S.getUniverDocInstance(e),h=(R=f.getRenderById(e))==null?void 0:R.with(d.DocSkeletonManagerService).getViewModel();if(v==null||h==null)throw new Error(`DocumentDataModel or documentViewModel not found for unitId: ${e}`);const p=s.get(d.TextSelectionManagerService),I=((E=p.getSelections())!=null?E:[]).map(F),_=s.get(d.DocStateChangeManagerService),y=s.get(H),T=!!v.getSnapshot().disabled;if(n.JSONX.isNoop(a)||a&&a.length===0||T)return{unitId:e,actions:[],textRanges:I};const D=n.JSONX.invertWithDoc(a,v.getSnapshot());v.apply(a);const x=v.getSelfOrHeaderFooterModel(t);h.getSelfOrHeaderFooterViewModel(t).reset(x),!m&&o&&c!=null&&queueMicrotask(()=>{p.replaceTextRanges(o)});const A={commandId:Pe,unitId:e,segmentId:t,trigger:c,noHistory:l,debounce:g,redoState:{actions:a,textRanges:o},undoState:{actions:D,textRanges:r!=null?r:I}};if(u){const N=y.fetchComposedUndoRedoMutationParams();if(N==null)throw new Error("historyParams is null in RichTextEditingMutation");const{undoMutationParams:j,redoMutationParams:fe,previousActiveRange:L}=N;A.redoState.actions=fe.actions,A.undoState.actions=j.actions,A.undoState.textRanges=[L]}return _.setChangeState(A),{unitId:e,actions:D,textRanges:I}}},Ue="doc.command.insert-text",W={id:Ue,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),{range:t,segmentId:a,body:o,unitId:r,textRanges:c}=i,{startOffset:l,collapsed:u}=t,m={id:b.id,params:{unitId:r,actions:[],textRanges:c,debounce:!0}},g=new n.TextX,S=n.JSONX.getInstance();return u?l>0&&g.push({t:n.TextXActionType.RETAIN,len:l,segmentId:a}):g.push(...pe(t,a)),g.push({t:n.TextXActionType.INSERT,body:o,len:o.dataStream.length,line:0,segmentId:a}),m.params.actions=S.editOp(g.serialize()),!!e.syncExecuteCommand(m.id,m.params)}};var w=(s=>(s[s.LEFT=0]="LEFT",s[s.RIGHT=1]="RIGHT",s))(w||{});const Z={id:"doc.command.delete-text",type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),{range:t,segmentId:a,unitId:o,direction:r,textRanges:c,len:l=1}=i,{startOffset:u}=t,m={id:b.id,params:{unitId:o,actions:[],textRanges:c,debounce:!0}},g=new n.TextX,S=n.JSONX.getInstance();return u>0&&g.push({t:n.TextXActionType.RETAIN,len:r===0?u-l:u,segmentId:a}),g.push({t:n.TextXActionType.DELETE,len:l,line:0,segmentId:a}),m.params.actions=S.editOp(g.serialize()),!!e.syncExecuteCommand(m.id,m.params)}},ve={id:"doc.command.update-text",type:n.CommandType.COMMAND,handler:async(s,i)=>{const{range:e,segmentId:t,updateBody:a,coverType:o,unitId:r,textRanges:c}=i,l=s.get(n.ICommandService),u={id:b.id,params:{unitId:r,actions:[],textRanges:c}},m=new n.TextX,g=n.JSONX.getInstance(),{startOffset:S,endOffset:f}=e;return m.push({t:n.TextXActionType.RETAIN,len:S,segmentId:t}),m.push({t:n.TextXActionType.RETAIN,body:a,len:f-S,segmentId:t,coverType:o}),u.params.actions=g.editOp(m.serialize()),!!l.syncExecuteCommand(u.id,u.params)}};function xt(s,i){const e=[];for(let t=0,a=s.length;t<a;t++)s[t]===n.DataStreamTreeTokenType.PARAGRAPH&&e.push({startIndex:t});if(i)for(const t of e)i.bullet&&(t.bullet=n.Tools.deepClone(i.bullet)),i.paragraphStyle&&(t.paragraphStyle=n.Tools.deepClone(i.paragraphStyle));return e}const Be={id:"doc.command.break-line",type:n.CommandType.COMMAND,handler:async s=>{var v,h;const i=s.get(d.TextSelectionManagerService),e=s.get(n.IUniverInstanceService),t=s.get(n.ICommandService),a=i.getActiveRange();if(a==null)return!1;const o=e.getCurrentUniverDocInstance();if(!o)return!1;const r=o.getUnitId(),{startOffset:c,segmentId:l,style:u}=a,m=[{startOffset:c+1,endOffset:c+1,style:u}],S=((h=(v=o.getBody())==null?void 0:v.paragraphs)!=null?h:[]).find(p=>p.startIndex>=c);return await t.executeCommand(W.id,{unitId:r,body:{dataStream:n.DataStreamTreeTokenType.PARAGRAPH,paragraphs:xt(n.DataStreamTreeTokenType.PARAGRAPH,S)},range:a,textRanges:m,segmentId:l})}};function Le(s,i){var t;return(t=s.get(M.IRenderManagerService).getRenderById(i))==null?void 0:t.with(d.DocSkeletonManagerService)}const Dt={id:"doc.command.inner-paste",type:n.CommandType.COMMAND,handler:async(s,i)=>{const{segmentId:e,body:t,textRanges:a}=i,o=s.get(n.ICommandService),r=s.get(d.TextSelectionManagerService),c=s.get(n.IUniverInstanceService),l=r.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance();if(!u)return!1;const m=u.getUnitId(),g={id:b.id,params:{unitId:m,actions:[],textRanges:a}},S=new n.MemoryCursor;S.reset();const f=new n.TextX,v=n.JSONX.getInstance();for(const p of l){const{startOffset:C,endOffset:I,collapsed:_}=p,y=C-S.cursor;_?f.push({t:n.TextXActionType.RETAIN,len:y,segmentId:e}):f.push(...pe(p,e,S.cursor)),f.push({t:n.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:e}),S.reset(),S.moveCursor(I)}return g.params.actions=v.editOp(f.serialize()),!!o.syncExecuteCommand(g.id,g.params)}},Ce={id:"doc.command.inner-cut",type:n.CommandType.COMMAND,handler:async(s,i)=>{var p;const{segmentId:e,textRanges:t}=i,a=s.get(n.ICommandService),o=s.get(d.TextSelectionManagerService),r=s.get(n.IUniverInstanceService),c=o.getSelections();if(!Array.isArray(c)||c.length===0)return!1;const l=(p=r.getCurrentUniverDocInstance())==null?void 0:p.getUnitId();if(!l)return!1;const u=r.getUniverDocInstance(l),m=n.getDocsUpdateBody(u.getSnapshot(),e);if(m==null)return!1;const g={id:b.id,params:{unitId:l,actions:[],textRanges:t}},S=new n.MemoryCursor;S.reset();const f=new n.TextX,v=n.JSONX.getInstance();for(const C of c){const{startOffset:I,endOffset:_,collapsed:y}=C,T=I-S.cursor;y?f.push({t:n.TextXActionType.RETAIN,len:T,segmentId:e}):f.push(...Et(C,m,e,S.cursor)),S.reset(),S.moveCursor(_)}return g.params.actions=v.editOp(f.serialize()),!!a.syncExecuteCommand(g.id,g.params)}};function Et(s,i,e="",t=0){const{startOffset:a,endOffset:o}=s,r=[],{paragraphs:c=[]}=i,l=a-t,u=o-t,m=c==null?void 0:c.find(g=>g.startIndex-t>=l&&g.startIndex-t<=u);if(l>0&&r.push({t:n.TextXActionType.RETAIN,len:l,segmentId:e}),m&&m.startIndex-t>l){const g=m.startIndex-t;r.push({t:n.TextXActionType.DELETE,len:g-l,line:0,segmentId:e}),r.push({t:n.TextXActionType.RETAIN,len:1,segmentId:e}),u>g+1&&r.push({t:n.TextXActionType.DELETE,len:u-g-1,line:0,segmentId:e})}else r.push({t:n.TextXActionType.DELETE,len:u-l,line:0,segmentId:e});return r}const we={id:"doc.command.delete-left",type:n.CommandType.COMMAND,handler:async s=>{const i=s.get(d.TextSelectionManagerService),e=s.get(n.IUniverInstanceService),t=s.get(n.ICommandService);let a=!0;const o=e.getCurrentUniverDocInstance();if(!o)return!1;const r=o.getUnitId(),c=Le(s,r),l=i.getActiveRange(),u=i.getSelections(),m=c==null?void 0:c.getSkeleton();if(l==null||m==null||u==null)return!1;const{startOffset:g,collapsed:S,segmentId:f,style:v}=l,h=m.findNodeByCharIndex(g),p=M.hasListGlyph(h),C=M.isIndentByGlyph(h,o.getBody());let I=g;const _=m.findNodeByCharIndex(g-1);if(M.isFirstGlyph(h)&&_!==h&&(p===!0||C===!0)&&S){const T=M.getParagraphByGlyph(h,o.getBody());if(T==null)return!1;const D=T==null?void 0:T.startIndex,x={startIndex:0},P=T.paragraphStyle;if(p===!0){const R=T.paragraphStyle;if(R){x.paragraphStyle=R;const{hanging:E}=R;E&&(x.paragraphStyle.indentStart=E,x.paragraphStyle.hanging=void 0)}}else if(C===!0){const R=T.bullet;R&&(x.bullet=R),P!=null&&(x.paragraphStyle={...P},delete x.paragraphStyle.hanging,delete x.paragraphStyle.indentStart)}const A=[{startOffset:I,endOffset:I,style:v}];a=await t.executeCommand(ve.id,{unitId:o.getUnitId(),updateBody:{dataStream:"",paragraphs:[{...x}]},range:{startOffset:D,endOffset:D+1},textRanges:A,coverType:n.UpdateDocsAttributeType.REPLACE,segmentId:f})}else if(S===!0){if(_==null)return!0;if(_.content==="\r")a=await t.executeCommand(_e.id,{direction:w.LEFT,range:l});else{I-=_.count;const T=[{startOffset:I,endOffset:I,style:v}];a=await t.executeCommand(Z.id,{unitId:o.getUnitId(),range:l,segmentId:f,direction:w.LEFT,len:_.count,textRanges:T})}}else{const T=Fe(l,u);a=await t.executeCommand(Ce.id,{segmentId:f,textRanges:T})}return a}},Xe={id:"doc.command.delete-right",type:n.CommandType.COMMAND,handler:async s=>{const i=s.get(d.TextSelectionManagerService),t=s.get(n.IUniverInstanceService).getCurrentUniverDocInstance();if(!t)return!1;const a=Le(s,t.getUnitId()),o=s.get(n.ICommandService),r=i.getActiveRange(),c=i.getSelections(),l=a==null?void 0:a.getSkeleton();if(r==null||l==null||c==null)return!1;const{startOffset:u,collapsed:m,segmentId:g,style:S}=r;if(u===t.getBody().dataStream.length-2&&m)return!0;let f=!1;if(m===!0){const v=l.findNodeByCharIndex(u);if(v.content==="\r")f=await o.executeCommand(_e.id,{direction:w.RIGHT,range:r});else{const h=[{startOffset:u,endOffset:u,style:S}];f=await o.executeCommand(Z.id,{unitId:t.getUnitId(),range:r,segmentId:g,direction:w.RIGHT,textRanges:h,len:v.count})}}else{const v=Fe(r,c);f=await o.executeCommand(Ce.id,{segmentId:g,textRanges:v})}return f}},_e={id:"doc.command.merge-two-paragraph",type:n.CommandType.COMMAND,handler:async(s,i)=>{var P,A,R;const e=s.get(d.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),a=s.get(n.ICommandService),{direction:o,range:r}=i,c=e.getActiveRange(),l=e.getSelections();if(c==null||l==null)return!1;const u=t.getCurrentUniverDocInstance();if(!u)return!1;const{startOffset:m,collapsed:g,segmentId:S,style:f}=c;if(!g)return!1;const v=o===w.LEFT?m:m+1,h=(R=(A=(P=u.getBody())==null?void 0:P.paragraphs)==null?void 0:A.find(E=>E.startIndex>=v))==null?void 0:R.startIndex,p=At(u.getBody(),v,h),C=o===w.LEFT?m-1:m,I=u.getUnitId(),_=[{startOffset:C,endOffset:C,style:f}],y={id:b.id,params:{unitId:I,actions:[],textRanges:_,prevTextRanges:[r]}},T=new n.TextX,D=n.JSONX.getInstance();return T.push({t:n.TextXActionType.RETAIN,len:o===w.LEFT?m-1:m,segmentId:S}),p.dataStream.length&&T.push({t:n.TextXActionType.INSERT,body:p,len:p.dataStream.length,line:0,segmentId:S}),T.push({t:n.TextXActionType.RETAIN,len:1,segmentId:S}),T.push({t:n.TextXActionType.DELETE,len:h+1-v,line:0,segmentId:S}),y.params.actions=D.editOp(T.serialize()),!!a.syncExecuteCommand(y.id,y.params)}};function At(s,i,e){const{textRuns:t}=s,a=s.dataStream.substring(i,e);if(t==null)return{dataStream:a};const o=[];for(const r of t){const{st:c,ed:l}=r;l<=i||c>=e||(c<i?o.push({...r,st:0,ed:l-i}):l>e?o.push({...r,st:c-i,ed:e-i}):o.push({...r,st:c-i,ed:l-i}))}return{dataStream:a,textRuns:o}}function Fe(s,i){let e=s.endOffset;for(const a of i){const{startOffset:o,endOffset:r}=a;o==null||r==null||r<=s.endOffset&&(e-=r-o)}return[{startOffset:e,endOffset:e,style:s.style}]}const Te={id:"doc.command.ime-input",type:n.CommandType.COMMAND,handler:async(s,i)=>{const{unitId:e,newText:t,oldTextLen:a,isCompositionEnd:o,isCompositionStart:r}=i,c=s.get(n.ICommandService),l=s.get(H),u=l.getActiveRange();if(u==null)return!1;const{startOffset:m,style:g,segmentId:S}=u,f=t.length,v=[{startOffset:m+f,endOffset:m+f,collapsed:!0,style:g}],h={id:b.id,params:{unitId:e,actions:[],textRanges:v}},p=new n.TextX,C=n.JSONX.getInstance();!u.collapsed&&r?p.push(...pe(u,S)):p.push({t:n.TextXActionType.RETAIN,len:m,segmentId:S}),a>0&&p.push({t:n.TextXActionType.DELETE,len:a,line:0,segmentId:S}),p.push({t:n.TextXActionType.INSERT,body:{dataStream:t},len:t.length,line:0,segmentId:S}),h.params.actions=C.editOp(p.serialize()),h.params.noHistory=!o,h.params.isCompositionEnd=o;const I=c.syncExecuteCommand(h.id,h.params);return l.pushUndoRedoMutationParams(I,h.params),!!I}};function U(s,i,e,t){var o;const{segmentId:a}=(o=e.getActiveRange())!=null?o:{};return a==null?!1:t.executeCommand(ye.id,{segmentId:a,preCommandId:s,...i!=null?i:{}})}const je="doc.command.set-inline-format-bold",K={id:je,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(je,i,t,e)}},$e="doc.command.set-inline-format-italic",q={id:$e,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U($e,i,t,e)}},ze="doc.command.set-inline-format-underline",Q={id:ze,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(ze,i,t,e)}},ke="doc.command.set-inline-format-strikethrough",ee={id:ke,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(ke,i,t,e)}},Ge="doc.command.set-inline-format-subscript",k={id:Ge,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(Ge,i,t,e)}},Ve="doc.command.set-inline-format-superscript",te={id:Ve,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(Ve,i,t,e)}},He="doc.command.set-inline-format-fontsize",ne={id:He,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(He,i,t,e)}},Je="doc.command.set-inline-format-font-family",ie={id:Je,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(Je,i,t,e)}},Ye="doc.command.set-inline-format-text-color",ae={id:Ye,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(Ye,i,t,e)}},We="doc.command.set-inline-format-text-background-color",se={id:We,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(We,i,t,e)}},Ze="doc.command.reset-inline-format-text-background-color",oe={id:Ze,type:n.CommandType.COMMAND,handler:async(s,i)=>{const e=s.get(n.ICommandService),t=s.get(d.TextSelectionManagerService);return U(Ze,i,t,e)}},Ke={[K.id]:"bl",[q.id]:"it",[Q.id]:"ul",[ee.id]:"st",[ne.id]:"fs",[ie.id]:"ff",[ae.id]:"cl",[se.id]:"bg",[oe.id]:"bg",[k.id]:"va",[te.id]:"va"},ye={id:"doc.command.set-inline-format",type:n.CommandType.COMMAND,handler:async(s,i)=>{const{segmentId:e,value:t,preCommandId:a}=i,o=s.get(n.ICommandService),r=s.get(d.TextSelectionManagerService),c=s.get(n.IUniverInstanceService),l=r.getSelections();if(!Array.isArray(l)||l.length===0)return!1;const u=c.getCurrentUniverDocInstance();if(!u)return!1;const m=u.getUnitId();let g;switch(a){case K.id:case q.id:case Q.id:case ee.id:case k.id:case te.id:{g=bt(u.getBody().textRuns,a,l);break}case ne.id:case ie.id:{g=t;break}case ae.id:case se.id:{g={rgb:t};break}case oe.id:{g={rgb:null};break}default:throw new Error(`Unknown command: ${a} in handleInlineFormat`)}const S={id:b.id,params:{unitId:m,actions:[],textRanges:l.map(F)}},f=new n.TextX,v=n.JSONX.getInstance(),h=new n.MemoryCursor;h.reset();for(const C of l){const{startOffset:I,endOffset:_}=C,y={dataStream:"",textRuns:[{st:0,ed:_-I,ts:{[Ke[a]]:g}}]},T=I-h.cursor;T!==0&&f.push({t:n.TextXActionType.RETAIN,len:T,segmentId:e}),f.push({t:n.TextXActionType.RETAIN,body:y,len:_-I,segmentId:e}),h.reset(),h.moveCursor(_)}return S.params.actions=v.editOp(f.serialize()),!!o.syncExecuteCommand(S.id,S.params)}};function Nt(s){return s!==null&&typeof s=="object"}function bt(s,i,e){let t=0,a=0;const o=Ke[i];for(;t!==s.length&&a!==e.length;){const{startOffset:r,endOffset:c}=e[a],{st:l,ed:u,ts:m}=s[t];if(c<=l)a++;else if(u<=r)t++;else{if(/bl|it/.test(o))return(m==null?void 0:m[o])===n.BooleanNumber.TRUE?n.BooleanNumber.FALSE:n.BooleanNumber.TRUE;if(/ul|st/.test(o))return Nt(m==null?void 0:m[o])&&(m==null?void 0:m[o]).s===n.BooleanNumber.TRUE?{s:n.BooleanNumber.FALSE}:{s:n.BooleanNumber.TRUE};if(/va/.test(o))return i===k.id?(m==null?void 0:m[o])===n.BaselineOffset.SUBSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUBSCRIPT:(m==null?void 0:m[o])===n.BaselineOffset.SUPERSCRIPT?n.BaselineOffset.NORMAL:n.BaselineOffset.SUPERSCRIPT;t++}}return/bl|it/.test(o)?n.BooleanNumber.TRUE:/ul|st/.test(o)?{s:n.BooleanNumber.TRUE}:i===k.id?n.BaselineOffset.SUBSCRIPT:n.BaselineOffset.SUPERSCRIPT}const Me={id:"doc.command.list-operation",type:n.CommandType.COMMAND,handler:(s,i)=>{var E,N,j,fe;const e=s.get(d.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),a=s.get(n.ICommandService),{listType:o}=i,r=t.getCurrentUniverDocInstance();if(!r)return!1;const c=e.getActiveRange(),l=(E=e.getSelections())!=null?E:[],u=(N=r.getBody())==null?void 0:N.paragraphs,m=l.map(F);if(c==null||u==null)return!1;const g=et(c,u),{segmentId:S}=c,f=r.getUnitId(),v=g.every(L=>{var B;return((B=L.bullet)==null?void 0:B.listType)===o});let p=n.Tools.generateRandomId(6);if(g.length===1){const L=u.indexOf(g[0]),B=u[L-1],X=u[L+1];B&&B.bullet&&B.bullet.listType===o?p=B.bullet.listId:X&&X.bullet&&X.bullet.listType===o&&(p=X.bullet.listId)}const C={id:b.id,params:{unitId:f,actions:[],textRanges:m}},I=new n.MemoryCursor;I.reset();const _=new n.TextX,y=n.JSONX.getInstance(),T=(j=r.getSnapshot().lists)!=null?j:{},D={...n.PRESET_LIST_TYPE,...T},{charSpace:x,defaultTabStop:P=36,gridType:A}=r.getSnapshot().documentStyle;for(const L of g){const{startIndex:B,paragraphStyle:X={}}=L,{indentFirstLine:Yt=0,snapToGrid:Wt,indentStart:Oe=0}=X,{hanging:Re,indentStart:dt}=D[o].nestingLevel[0],xe=M.getCharSpaceApply(x,P,A,Wt);_.push({t:n.TextXActionType.RETAIN,len:B-I.cursor,segmentId:S}),_.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[v?{paragraphStyle:{...X,hanging:void 0,indentStart:Oe?Math.max(0,M.getNumberUnitValue(Oe,xe)+Re-dt):void 0},startIndex:0}:{startIndex:0,paragraphStyle:{...X,indentFirstLine:void 0,hanging:Re,indentStart:dt-Re+M.getNumberUnitValue(Yt,xe)+M.getNumberUnitValue(Oe,xe)},bullet:{...(fe=L.bullet)!=null?fe:{nestingLevel:0,textStyle:{fs:20}},listType:o,listId:p}}]},segmentId:S,coverType:n.UpdateDocsAttributeType.REPLACE}),I.moveCursorTo(B+1)}return C.params.actions=y.editOp(_.serialize()),!!a.syncExecuteCommand(C.id,C.params)}},qe={id:"doc.command.bullet-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(Me.id,{listType:n.PresetListType.BULLET_LIST})},Qe={id:"doc.command.order-list",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(Me.id,{listType:n.PresetListType.ORDER_LIST})};function et(s,i){const{startOffset:e,endOffset:t}=s,a=[];let o=-1;for(const r of i){const{startIndex:c}=r;(e>o&&e<=c||t>o&&t<=c||c>=e&&c<=t)&&a.push(r),o=c}return a}const G={id:"doc.command.align-operation",type:n.CommandType.COMMAND,handler:(s,i)=>{var y,T;const e=s.get(d.TextSelectionManagerService),t=s.get(n.IUniverInstanceService),a=s.get(n.ICommandService),{alignType:o}=i,r=t.getCurrentUniverDocInstance();if(!r)return!1;const c=e.getActiveRange(),l=(y=e.getSelections())!=null?y:[],u=(T=r.getBody())==null?void 0:T.paragraphs,m=l.map(F);if(c==null||u==null)return!1;const g=et(c,u),{segmentId:S}=c,f=r.getUnitId(),v=g.every(D=>{var x;return((x=D.paragraphStyle)==null?void 0:x.horizontalAlign)===o}),h={id:b.id,params:{unitId:f,actions:[],textRanges:m}},p=new n.MemoryCursor;p.reset();const C=new n.TextX,I=n.JSONX.getInstance();for(const D of g){const{startIndex:x}=D;C.push({t:n.TextXActionType.RETAIN,len:x-p.cursor,segmentId:S});const P={...D.paragraphStyle,horizontalAlign:v?n.HorizontalAlign.UNSPECIFIED:o};C.push({t:n.TextXActionType.RETAIN,len:1,body:{dataStream:"",paragraphs:[{...D,paragraphStyle:P,startIndex:0}]},segmentId:S,coverType:n.UpdateDocsAttributeType.REPLACE}),p.moveCursorTo(x+1)}return h.params.actions=I.editOp(C.serialize()),!!a.syncExecuteCommand(h.id,h.params)}},tt={id:"doc.command.align-left",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(G.id,{alignType:n.HorizontalAlign.LEFT})},nt={id:"doc.command.align-center",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(G.id,{alignType:n.HorizontalAlign.CENTER})},it={id:"doc.command.align-right",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(G.id,{alignType:n.HorizontalAlign.RIGHT})},at={id:"doc.command.align-justify",type:n.CommandType.COMMAND,handler:s=>s.get(n.ICommandService).syncExecuteCommand(G.id,{alignType:n.HorizontalAlign.JUSTIFIED})},st={id:"doc.command-replace-content",type:n.CommandType.COMMAND,handler:async(s,i)=>{var f;const{unitId:e,body:t,textRanges:a,segmentId:o=""}=i,r=s.get(n.IUniverInstanceService),c=s.get(n.ICommandService),l=s.get(d.TextSelectionManagerService),u=(f=r.getUniverDocInstance(e))==null?void 0:f.getSnapshot().body,m=l.getSelections();if(u==null||!Array.isArray(m)||m.length===0)return!1;const g=rt(e,o,u,t);return g.params.textRanges=a,!!c.syncExecuteCommand(g.id,g.params)}},ot={id:"doc.command-cover-content",type:n.CommandType.COMMAND,handler:async(s,i)=>{var m;const{unitId:e,body:t,segmentId:a=""}=i,o=s.get(n.IUniverInstanceService),r=s.get(n.ICommandService),c=s.get(n.IUndoRedoService),l=(m=o.getUniverDocInstance(e))==null?void 0:m.getSnapshot().body;if(l==null)return!1;const u=rt(e,a,l,t);return u.params.noNeedSetTextRange=!0,u.params.noHistory=!0,r.syncExecuteCommand(u.id,u.params),c.clearUndoRedo(e),!0}};function rt(s,i,e,t){const a={id:b.id,params:{unitId:s,actions:[],textRanges:[]}},o=new n.TextX,r=n.JSONX.getInstance(),c=(e==null?void 0:e.dataStream.length)-2;return c>0&&o.push({t:n.TextXActionType.DELETE,len:c,line:0,segmentId:i}),t.dataStream.length>0&&o.push({t:n.TextXActionType.INSERT,body:t,len:t.dataStream.length,line:0,segmentId:i}),a.params.actions=r.editOp(o.serialize()),a}const re={id:"doc.operation.move-cursor",type:n.CommandType.OPERATION,handler:(s,i)=>!!i},ce={id:"doc.operation.move-selection",type:n.CommandType.OPERATION,handler:(s,i)=>!!i},Pt=(s,i)=>{const e=s.get(n.IUniverInstanceService).getUniverDocInstance(i.unitId),t=(e==null?void 0:e.zoomRatio)||1;return{...n.Tools.deepClone(i),zoomRatio:t}},J={id:"doc.operation.set-zoom-ratio",type:n.CommandType.OPERATION,handler:(s,i)=>{const e=s.get(n.IUniverInstanceService).getUniverDocInstance(i.unitId);if(!e)return!1;const t=e.getSnapshot();return t.settings==null?t.settings={zoomRatio:i.zoomRatio}:t.settings.zoomRatio=i.zoomRatio,!0}},ct={type:n.CommandType.COMMAND,id:"doc.command.set-zoom-ratio",handler:async(s,i)=>{var g,S,f;const e=s.get(n.ICommandService),t=s.get(n.IUndoRedoService),a=s.get(n.IUniverInstanceService);let o=(g=a.getCurrentUniverDocInstance())==null?void 0:g.getUnitId();if(!o)return!1;let r=1;if(i&&(o=(S=i.documentId)!=null?S:o,r=(f=i.zoomRatio)!=null?f:r),!a.getUniverDocInstance(o))return!1;const l={zoomRatio:r,unitId:o},u=Pt(s,l);return e.syncExecuteCommand(J.id,l)?(t.pushUndoRedo({unitID:o,undoMutations:[{id:J.id,params:u}],redoMutations:[{id:J.id,params:l}]}),!0):!1}},lt={id:"doc.operation.select-all",type:n.CommandType.COMMAND,handler:async s=>{const i=s.get(n.IUniverInstanceService),e=s.get(d.TextSelectionManagerService),t=i.getCurrentUniverDocInstance();if(!t)return!1;const a=t.getSnapshot().body;if(a==null)return!1;const o=[{startOffset:0,endOffset:a.dataStream.length-2}];return e.replaceTextRanges(o,!1),!0}};var Ut=Object.defineProperty,Bt=Object.getOwnPropertyDescriptor,Lt=(s,i,e,t)=>{for(var a=t>1?void 0:t?Bt(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&Ut(i,e,a),a},Y=(s,i)=>(e,t)=>i(e,t,s);let le=class extends n.Disposable{constructor(i,e,t,a,o){super();O(this,"_previousIMEContent","");O(this,"_isCompositionStart",!0);O(this,"_onStartSubscription");O(this,"_onUpdateSubscription");O(this,"_onEndSubscription");this._univerInstanceService=i,this._renderManagerSrv=e,this._textSelectionRenderManager=t,this._imeInputManagerService=a,this._commandService=o,this._initialize()}dispose(){var i,e,t;(i=this._onStartSubscription)==null||i.unsubscribe(),(e=this._onUpdateSubscription)==null||e.unsubscribe(),(t=this._onEndSubscription)==null||t.unsubscribe()}_initialize(){this._initialOnCompositionstart(),this._initialOnCompositionUpdate(),this._initialOnCompositionend()}_initialOnCompositionstart(){this._onStartSubscription=this._textSelectionRenderManager.onCompositionstart$.subscribe(i=>{if(i==null)return;this._resetIME();const{activeRange:e}=i;e!=null&&this._imeInputManagerService.setActiveRange(n.Tools.deepClone(e))})}_initialOnCompositionUpdate(){this._onUpdateSubscription=this._textSelectionRenderManager.onCompositionupdate$.subscribe(async i=>{this._updateContent(i,!0)})}_initialOnCompositionend(){this._onEndSubscription=this._textSelectionRenderManager.onCompositionend$.subscribe(i=>{this._updateContent(i,!1)})}async _updateContent(i,e){var u;if(i==null)return;const t=this._univerInstanceService.getCurrentUniverDocInstance();if(!t)return;const a=(u=this._renderManagerSrv.getRenderById(t.getUnitId()))==null?void 0:u.with(d.DocSkeletonManagerService).getSkeleton(),{event:o,activeRange:r}=i;if(a==null||r==null)return;const l=o.data;l===this._previousIMEContent&&e||(await this._commandService.executeCommand(Te.id,{unitId:t.getUnitId(),newText:l,oldTextLen:this._previousIMEContent.length,isCompositionStart:this._isCompositionStart,isCompositionEnd:!e}),e?(this._isCompositionStart&&(this._isCompositionStart=!1),this._previousIMEContent=l):this._resetIME())}_resetIME(){this._previousIMEContent="",this._isCompositionStart=!0,this._imeInputManagerService.clearUndoRedoMutationParamsCache(),this._imeInputManagerService.setActiveRange(null)}};le=Lt([n.OnLifecycle(n.LifecycleStages.Rendered,le),Y(0,n.IUniverInstanceService),Y(1,M.IRenderManagerService),Y(2,M.ITextSelectionRenderManager),Y(3,z.Inject(H)),Y(4,n.ICommandService)],le);var wt=Object.defineProperty,Xt=Object.getOwnPropertyDescriptor,Ft=(s,i,e,t)=>{for(var a=t>1?void 0:t?Xt(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&wt(i,e,a),a},de=(s,i)=>(e,t)=>i(e,t,s);let ue=class extends n.Disposable{constructor(i,e,t,a){super();O(this,"_onInputSubscription");this._univerInstanceService=i,this._renderManagerService=e,this._textSelectionManagerService=t,this._commandService=a,this._commandExecutedListener()}dispose(){var i;super.dispose(),(i=this._onInputSubscription)==null||i.unsubscribe()}_commandExecutedListener(){const i=[re.id,ce.id];this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(!i.includes(e.id))return;const t=e.params;switch(e.id){case re.id:return this._handleMoveCursor(t.direction);case ce.id:return this._handleShiftMoveSelection(t.direction);default:throw new Error("Unknown command")}}))}_handleShiftMoveSelection(i){var h,p,C;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),a=this._univerInstanceService.getCurrentUniverDocInstance();if(!a)return;const o=(h=this._renderManagerService.getRenderById(a.getUnitId()))==null?void 0:h.with(d.DocSkeletonManagerService).getSkeleton(),r=this._getDocObject();if(e==null||o==null||r==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:m,direction:g}=e;if(t.length>1){let I=Number.POSITIVE_INFINITY,_=Number.NEGATIVE_INFINITY;for(const y of t)I=Math.min(I,y.startOffset),_=Math.max(_,y.endOffset);this._textSelectionManagerService.replaceTextRanges([{startOffset:i===n.Direction.LEFT||i===n.Direction.UP?_:I,endOffset:i===n.Direction.LEFT||i===n.Direction.UP?I:_,style:u}],!1);return}const S=m||g===M.RANGE_DIRECTION.FORWARD?c:l;let f=m||g===M.RANGE_DIRECTION.FORWARD?l:c;const v=(p=a.getBody().dataStream.length)!=null?p:Number.POSITIVE_INFINITY;if(i===n.Direction.LEFT||i===n.Direction.RIGHT){const I=o.findNodeByCharIndex(f-1),_=o.findNodeByCharIndex(f);f=i===n.Direction.RIGHT?f+_.count:f-((C=I==null?void 0:I.count)!=null?C:0),f=Math.min(v-2,Math.max(0,f)),this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:f,style:u}],!1)}else{const I=o.findNodeByCharIndex(f),_=r.document.getOffsetConfig(),y=this._getTopOrBottomPosition(o,I,i===n.Direction.DOWN);if(y==null){const D=i===n.Direction.UP?0:v-2;if(D===f)return;this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:D,style:u}],!1);return}const T=new M.NodePositionConvertToCursor(_,o).getRangePointData(y,y).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{startOffset:S,endOffset:T.endOffset,style:u}],!1)}}_handleMoveCursor(i){var S,f,v;const e=this._textSelectionManagerService.getActiveRange(),t=this._textSelectionManagerService.getSelections(),a=this._univerInstanceService.getCurrentUniverDocInstance();if(!a)return!1;const o=(S=this._renderManagerService.getRenderById(a.getUnitId()))==null?void 0:S.with(d.DocSkeletonManagerService).getSkeleton(),r=this._getDocObject();if(e==null||o==null||r==null||t==null)return;const{startOffset:c,endOffset:l,style:u,collapsed:m}=e,g=(f=a.getBody().dataStream.length)!=null?f:Number.POSITIVE_INFINITY;if(i===n.Direction.LEFT||i===n.Direction.RIGHT){let h;if(!e.collapsed||t.length>1){let p=Number.POSITIVE_INFINITY,C=Number.NEGATIVE_INFINITY;for(const I of t)p=Math.min(p,I.startOffset),C=Math.max(C,I.endOffset);h=i===n.Direction.LEFT?p:C}else{const p=o.findNodeByCharIndex(c-1),C=o.findNodeByCharIndex(c);i===n.Direction.LEFT?h=Math.max(0,c-((v=p==null?void 0:p.count)!=null?v:0)):h=Math.min(g-2,l+C.count)}this._textSelectionManagerService.replaceTextRanges([{startOffset:h,endOffset:h,style:u}],!1)}else{const h=o.findNodeByCharIndex(c),p=o.findNodeByCharIndex(l),C=r.document.getOffsetConfig(),I=this._getTopOrBottomPosition(o,i===n.Direction.UP?h:p,i===n.Direction.DOWN);if(I==null){let y;m?y=i===n.Direction.UP?0:g-2:y=i===n.Direction.UP?c:l,this._textSelectionManagerService.replaceTextRanges([{startOffset:y,endOffset:y,style:u}],!1);return}const _=new M.NodePositionConvertToCursor(C,o).getRangePointData(I,I).cursorList[0];this._textSelectionManagerService.replaceTextRanges([{..._,style:u}],!1)}}_getTopOrBottomPosition(i,e,t){if(e==null)return;const a=this._getSpanLeftOffsetInLine(e),o=this._getNextOrPrevLine(e,t);if(o==null)return;const r=this._matchPositionByLeftOffset(i,o,a);if(r!=null)return{...r,isBack:!0}}_getSpanLeftOffsetInLine(i){const e=i.parent;if(e==null)return Number.NEGATIVE_INFINITY;const t=e.left,{left:a}=i;return t+a}_matchPositionByLeftOffset(i,e,t){const a={distance:Number.POSITIVE_INFINITY};for(const o of e.divides){const r=o.left;for(const c of o.glyphGroup){const{left:l}=c,u=r+l,m=Math.abs(t-u);m<a.distance&&(a.glyph=c,a.distance=m)}}if(a.glyph!=null)return i.findPositionByGlyph(a.glyph)}_getNextOrPrevLine(i,e){var v,h,p,C,I,_,y,T,D,x,P,A;const t=i.parent;if(t==null)return;const a=t.parent;if(a==null)return;const o=a.parent;if(o==null)return;const r=o.lines.indexOf(a);if(r===-1)return;let c;if(e===!0?c=o.lines[r+1]:c=o.lines[r-1],c!=null)return c;const l=o.parent;if(l==null)return;const u=l.columns.indexOf(o);if(u===-1)return;if(e===!0)c=(v=l.columns[u+1])==null?void 0:v.lines[0];else{const R=(p=(h=l.columns)==null?void 0:h[u-1])==null?void 0:p.lines;c=R==null?void 0:R[R.length-1]}if(c!=null)return c;const m=l.parent;if(m==null)return;const g=m.sections.indexOf(l);if(g===-1)return;if(e===!0)c=(I=(C=m.sections[g-1])==null?void 0:C.columns[0])==null?void 0:I.lines[0];else{const R=(y=(_=m.sections)==null?void 0:_[g-1])==null?void 0:y.columns,E=R==null?void 0:R[R.length-1],N=E==null?void 0:E.lines;c=N==null?void 0:N[N.length-1]}if(c!=null)return c;const S=m.parent;if(S==null)return;const f=S.pages.indexOf(m);if(f!==-1){if(e===!0)c=(x=(D=(T=S.pages[f+1])==null?void 0:T.sections[0])==null?void 0:D.columns[0])==null?void 0:x.lines[0];else{const R=(P=S.pages[f-1])==null?void 0:P.sections;if(R==null)return;const E=(A=R[R.length-1])==null?void 0:A.columns,N=E[E.length-1],j=N==null?void 0:N.lines;c=j[j.length-1]}if(c!=null)return c}}_getDocObject(){return Ee(this._univerInstanceService,this._renderManagerService)}};ue=Ft([n.OnLifecycle(n.LifecycleStages.Rendered,ue),de(0,n.IUniverInstanceService),de(1,M.IRenderManagerService),de(2,z.Inject(d.TextSelectionManagerService)),de(3,n.ICommandService)],ue);var jt=Object.defineProperty,$t=Object.getOwnPropertyDescriptor,zt=(s,i,e,t)=>{for(var a=t>1?void 0:t?$t(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&jt(i,e,a),a},me=(s,i)=>(e,t)=>i(e,t,s);let ge=class extends n.Disposable{constructor(i,e,t,a){super();O(this,"_onInputSubscription");this._univerInstanceService=i,this._renderManagerService=e,this._textSelectionRenderManager=t,this._commandService=a,this._init()}dispose(){var i;super.dispose(),(i=this._onInputSubscription)==null||i.unsubscribe()}_init(){this._initialNormalInput()}_initialNormalInput(){this._onInputSubscription=this._textSelectionRenderManager.onInput$.subscribe(async i=>{var v;if(i==null)return;const e=this._univerInstanceService.getCurrentUniverDocInstance();if(!e)return;const t=e.getUnitId(),{event:a,content:o="",activeRange:r}=i,c=a,l=(v=this._renderManagerService.getRenderById(e.getUnitId()))==null?void 0:v.with(d.DocSkeletonManagerService).getSkeleton();if(c.data==null||l==null||!l||!r)return;const{startOffset:u,segmentId:m,style:g}=r,S=o.length,f=[{startOffset:u+S,endOffset:u+S,style:g}];await this._commandService.executeCommand(W.id,{unitId:t,body:{dataStream:o},range:r,textRanges:f,segmentId:m})})}};ge=zt([n.OnLifecycle(n.LifecycleStages.Rendered,ge),me(0,n.IUniverInstanceService),me(1,M.IRenderManagerService),me(2,M.ITextSelectionRenderManager),me(3,n.ICommandService)],ge);var kt=Object.defineProperty,Gt=Object.getOwnPropertyDescriptor,Vt=(s,i,e,t)=>{for(var a=t>1?void 0:t?Gt(i,e):i,o=s.length-1,r;o>=0;o--)(r=s[o])&&(a=(t?r(i,e,a):r(a))||a);return t&&a&&kt(i,e,a),a},Ht=(s,i)=>(e,t)=>i(e,t,s);const Jt="docs";d.UniverDocsPlugin=(Se=class extends n.Plugin{constructor(i={},e){super(),this._injector=e,this._initializeDependencies(e),this._initializeCommands()}_initializeCommands(){[re,ce,we,Xe,K,q,Q,ee,k,te,ne,ie,ae,oe,se,ye,Be,W,Z,ve,Te,_e,b,st,ot,ct,J,he,lt,Qe,qe,Me,tt,nt,it,G,at].forEach(i=>{this._injector.get(n.ICommandService).registerCommand(i)})}_initializeDependencies(i){[[d.DocStateChangeManagerService],[H],[M.ITextSelectionRenderManager,{useClass:M.TextSelectionRenderManager}],[d.TextSelectionManagerService],[ge],[le],[ue]].forEach(e=>i.add(e))}},O(Se,"pluginName",Jt),O(Se,"type",n.UniverInstanceType.UNIVER_DOC),Se),d.UniverDocsPlugin=Vt([Ht(1,z.Inject(z.Injector))],d.UniverDocsPlugin),d.AlignCenterCommand=nt,d.AlignJustifyCommand=at,d.AlignLeftCommand=tt,d.AlignOperationCommand=G,d.AlignRightCommand=it,d.BreakLineCommand=Be,d.BulletListCommand=qe,d.CoverContentCommand=ot,d.CutContentCommand=Ce,d.DOCS_COMPONENT_BACKGROUND_LAYER_INDEX=ut,d.DOCS_COMPONENT_DEFAULT_Z_INDEX=St,d.DOCS_COMPONENT_HEADER_LAYER_INDEX=gt,d.DOCS_COMPONENT_MAIN_LAYER_INDEX=mt,d.DOCS_VIEW_KEY=V,d.DeleteCommand=Z,d.DeleteLeftCommand=we,d.DeleteRightCommand=Xe,d.EditorInsertTextCommandId=Ue,d.IMEInputCommand=Te,d.IMEInputManagerService=H,d.InnerPasteCommand=Dt,d.InsertCommand=W,d.MoveCursorOperation=re,d.MoveSelectionOperation=ce,d.NORMAL_TEXT_SELECTION_PLUGIN_NAME=ft,d.OrderListCommand=Qe,d.ReplaceContentCommand=st,d.ResetInlineFormatTextBackgroundColorCommand=oe,d.RichTextEditingMutation=b,d.SelectAllOperation=lt,d.SetDocZoomRatioCommand=ct,d.SetDocZoomRatioOperation=J,d.SetInlineFormatBoldCommand=K,d.SetInlineFormatCommand=ye,d.SetInlineFormatFontFamilyCommand=ie,d.SetInlineFormatFontSizeCommand=ne,d.SetInlineFormatItalicCommand=q,d.SetInlineFormatStrikethroughCommand=ee,d.SetInlineFormatSubscriptCommand=k,d.SetInlineFormatSuperscriptCommand=te,d.SetInlineFormatTextBackgroundColorCommand=se,d.SetInlineFormatTextColorCommand=ae,d.SetInlineFormatUnderlineCommand=Q,d.SetTextSelectionsOperation=he,d.UpdateCommand=ve,d.VIEWPORT_KEY=De,d.getDocObject=Ee,d.getDocObjectById=pt,d.neoGetDocObject=ht,d.serializeTextRange=F,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/docs",
3
- "version": "0.1.16",
3
+ "version": "0.1.17",
4
4
  "private": false,
5
5
  "description": "UniverSheet normal base-docs",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -48,8 +48,8 @@
48
48
  "peerDependencies": {
49
49
  "@wendellhu/redi": "0.15.4",
50
50
  "rxjs": ">=7.0.0",
51
- "@univerjs/core": "0.1.16",
52
- "@univerjs/engine-render": "0.1.16"
51
+ "@univerjs/core": "0.1.17",
52
+ "@univerjs/engine-render": "0.1.17"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@wendellhu/redi": "0.15.4",
@@ -57,9 +57,9 @@
57
57
  "typescript": "^5.4.5",
58
58
  "vite": "^5.3.1",
59
59
  "vitest": "^1.6.0",
60
- "@univerjs/core": "0.1.16",
61
- "@univerjs/shared": "0.1.16",
62
- "@univerjs/engine-render": "0.1.16"
60
+ "@univerjs/core": "0.1.17",
61
+ "@univerjs/engine-render": "0.1.17",
62
+ "@univerjs/shared": "0.1.17"
63
63
  },
64
64
  "univerSpace": {
65
65
  ".": {