@univerjs/core
Version:
Core library for Univer.
2 lines (1 loc) • 14.5 kB
JavaScript
"use strict";var w=Object.defineProperty;var B=(s,t,e)=>t in s?w(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var p=(s,t,e)=>B(s,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("@univerjs/core"),g=require("rxjs");class S extends r.Disposable{static extend(t){Object.getOwnPropertyNames(t.prototype).forEach(e=>{e!=="constructor"&&(this.prototype[e]=t.prototype[e])}),Object.getOwnPropertyNames(t).forEach(e=>{e!=="prototype"&&e!=="name"&&e!=="length"&&(this[e]=t[e])})}}const b=Symbol("initializers");class x extends r.Disposable{constructor(t){super(),this._injector=t;const e=this,n=Object.getPrototypeOf(this)[b];n&&n.forEach(function(i){i.apply(e,[t])})}_initialize(t){}static extend(t){Object.getOwnPropertyNames(t.prototype).forEach(e=>{if(e==="_initialize"){let n=this.prototype[b];n||(n=[],this.prototype[b]=n),n.push(t.prototype._initialize)}else e!=="constructor"&&(this.prototype[e]=t.prototype[e])}),Object.getOwnPropertyNames(t).forEach(e=>{e!=="prototype"&&e!=="name"&&e!=="length"&&(this[e]=t[e])})}}var O=Object.getOwnPropertyDescriptor,P=(s,t,e,n)=>{for(var i=n>1?void 0:n?O(t,e):t,o=s.length-1,c;o>=0;o--)(c=s[o])&&(i=c(i)||i);return i},U=(s,t)=>(e,n)=>t(e,n,s);exports.FBlob=class extends S{constructor(t,e){super(),this._blob=t,this._injector=e}copyBlob(){return this._injector.createInstance(exports.FBlob,this._blob)}getAs(t){const e=this.copyBlob();return e.setContentType(t),e}getDataAsString(t){return this._blob===null?Promise.resolve(""):t===void 0?this._blob.text():new Promise((e,n)=>{this._blob.arrayBuffer().then(i=>{const o=new TextDecoder(t).decode(i);e(o)}).catch(i=>{n(new Error(`Failed to read Blob as ArrayBuffer: ${i.message}`))})})}getBytes(){return this._blob?this._blob.arrayBuffer().then(t=>new Uint8Array(t)):Promise.reject(new Error("Blob is undefined or null."))}setBytes(t){return this._blob=new Blob([t]),this}setDataFromString(t,e){const n=e!=null?e:"text/plain",i=new Blob([t],{type:n});return this._blob=i,this}getContentType(){var t;return(t=this._blob)==null?void 0:t.type}setContentType(t){var e;return this._blob=(e=this._blob)==null?void 0:e.slice(0,this._blob.size,t),this}};exports.FBlob=P([U(1,r.Inject(r.Injector))],exports.FBlob);const h=class h{static get(){if(this._instance)return this._instance;const t=new h;return this._instance=t,t}static extend(t){Object.getOwnPropertyNames(t.prototype).forEach(e=>{e!=="constructor"&&(this.prototype[e]=t.prototype[e])}),Object.getOwnPropertyNames(t).forEach(e=>{e!=="prototype"&&e!=="name"&&e!=="length"&&(this[e]=t[e])})}constructor(){for(const t in h.prototype)this[t]=h.prototype[t]}get AbsoluteRefType(){return r.AbsoluteRefType}get UniverInstanceType(){return r.UniverInstanceType}get LifecycleStages(){return r.LifecycleStages}get DataValidationType(){return r.DataValidationType}get DataValidationErrorStyle(){return r.DataValidationErrorStyle}get DataValidationRenderMode(){return r.DataValidationRenderMode}get DataValidationOperator(){return r.DataValidationOperator}get DataValidationStatus(){return r.DataValidationStatus}get CommandType(){return r.CommandType}get BaselineOffset(){return r.BaselineOffset}get BooleanNumber(){return r.BooleanNumber}get HorizontalAlign(){return r.HorizontalAlign}get TextDecoration(){return r.TextDecoration}get TextDirection(){return r.TextDirection}get VerticalAlign(){return r.VerticalAlign}get WrapStrategy(){return r.WrapStrategy}get BorderType(){return r.BorderType}get BorderStyleTypes(){return r.BorderStyleTypes}get AutoFillSeries(){return r.AutoFillSeries}get ColorType(){return r.ColorType}get CommonHideTypes(){return r.CommonHideTypes}get CopyPasteType(){return r.CopyPasteType}get DeleteDirection(){return r.DeleteDirection}get DeveloperMetadataVisibility(){return r.DeveloperMetadataVisibility}get Dimension(){return r.Dimension}get Direction(){return r.Direction}get InterpolationPointType(){return r.InterpolationPointType}get LocaleType(){return r.LocaleType}get MentionType(){return r.MentionType}get ProtectionType(){return r.ProtectionType}get RelativeDate(){return r.RelativeDate}get SheetTypes(){return r.SheetTypes}get ThemeColorType(){return r.ThemeColorType}};p(h,"_instance");let f=h;const u=class u{static get(){if(this._instance)return this._instance;const t=new u;return this._instance=t,t}static extend(t){Object.getOwnPropertyNames(t.prototype).forEach(e=>{e!=="constructor"&&(this.prototype[e]=t.prototype[e])}),Object.getOwnPropertyNames(t).forEach(e=>{e!=="prototype"&&e!=="name"&&e!=="length"&&(this[e]=t[e])})}constructor(){for(const t in u.prototype)this[t]=u.prototype[t]}get DocCreated(){return"DocCreated"}get DocDisposed(){return"DocDisposed"}get LifeCycleChanged(){return"LifeCycleChanged"}get Redo(){return"Redo"}get Undo(){return"Undo"}get BeforeRedo(){return"BeforeRedo"}get BeforeUndo(){return"BeforeUndo"}get CommandExecuted(){return"CommandExecuted"}get BeforeCommandExecute(){return"BeforeCommandExecute"}};p(u,"_instance");let m=u;var I=Object.getOwnPropertyDescriptor,H=(s,t,e,n)=>{for(var i=n>1?void 0:n?I(t,e):t,o=s.length-1,c;o>=0;o--)(c=s[o])&&(i=c(i)||i);return i},j=(s,t)=>(e,n)=>t(e,n,s);exports.FHooks=class extends S{constructor(t,e){super(),this._injector=t,this._lifecycleService=e}onStarting(t){return r.toDisposable(this._lifecycleService.lifecycle$.pipe(g.filter(e=>e===r.LifecycleStages.Starting)).subscribe(t))}onReady(t){return r.toDisposable(this._lifecycleService.lifecycle$.pipe(g.filter(e=>e===r.LifecycleStages.Ready)).subscribe(t))}onRendered(t){return r.toDisposable(this._lifecycleService.lifecycle$.pipe(g.filter(e=>e===r.LifecycleStages.Rendered)).subscribe(t))}onSteady(t){return r.toDisposable(this._lifecycleService.lifecycle$.pipe(g.filter(e=>e===r.LifecycleStages.Steady)).subscribe(t))}onBeforeUndo(t){return this._injector.get(r.ICommandService).beforeCommandExecuted(n=>{if(n.id===r.UndoCommand.id){const o=this._injector.get(r.IUndoRedoService).pitchTopUndoElement();o&&t(o)}})}onUndo(t){return this._injector.get(r.ICommandService).onCommandExecuted(n=>{if(n.id===r.UndoCommand.id){const o=this._injector.get(r.IUndoRedoService).pitchTopUndoElement();o&&t(o)}})}onBeforeRedo(t){return this._injector.get(r.ICommandService).beforeCommandExecuted(n=>{if(n.id===r.RedoCommand.id){const o=this._injector.get(r.IUndoRedoService).pitchTopRedoElement();o&&t(o)}})}onRedo(t){return this._injector.get(r.ICommandService).onCommandExecuted(n=>{if(n.id===r.RedoCommand.id){const o=this._injector.get(r.IUndoRedoService).pitchTopRedoElement();o&&t(o)}})}};exports.FHooks=H([j(0,r.Inject(r.Injector)),j(1,r.Inject(r.LifecycleService))],exports.FHooks);var F=Object.getOwnPropertyDescriptor,M=(s,t,e,n)=>{for(var i=n>1?void 0:n?F(t,e):t,o=s.length-1,c;o>=0;o--)(c=s[o])&&(i=c(i)||i);return i},V=(s,t)=>(e,n)=>t(e,n,s);let D=class extends x{constructor(s,t){super(t),this.doc=s}};D=M([V(1,r.Inject(r.Injector))],D);class L{constructor(){p(this,"_eventRegistry",new Map);p(this,"_eventHandlerMap",new Map);p(this,"_eventHandlerRegisted",new Map)}_ensureEventRegistry(t){return this._eventRegistry.has(t)||this._eventRegistry.set(t,new r.Registry),this._eventRegistry.get(t)}registerEventHandler(t,e){const n=this._eventHandlerMap.get(t);return n?n.add(e):this._eventHandlerMap.set(t,new Set([e])),this._ensureEventRegistry(t).getData().length&&this._initEventHandler(t),r.toDisposable(()=>{var i,o,c,a;(i=this._eventHandlerMap.get(t))==null||i.delete(e),(c=(o=this._eventHandlerRegisted.get(t))==null?void 0:o.get(e))==null||c.dispose(),(a=this._eventHandlerRegisted.get(t))==null||a.delete(e)})}removeEvent(t,e){const n=this._ensureEventRegistry(t);if(n.delete(e),n.getData().length===0){const i=this._eventHandlerRegisted.get(t);i==null||i.forEach(o=>o.dispose()),this._eventHandlerRegisted.delete(t)}}_initEventHandler(t){let e=this._eventHandlerRegisted.get(t);const n=this._eventHandlerMap.get(t);n&&(e||(e=new Map,this._eventHandlerRegisted.set(t,e),n==null||n.forEach(i=>{e==null||e.set(i,r.toDisposable(i()))})))}addEvent(t,e){return this._ensureEventRegistry(t).add(e),this._initEventHandler(t),r.toDisposable(()=>this.removeEvent(t,e))}fireEvent(t,e){var n;return(n=this._eventRegistry.get(t))==null||n.getData().forEach(i=>{i(e)}),e.cancel}}var $=Object.getOwnPropertyDescriptor,z=(s,t,e,n)=>{for(var i=n>1?void 0:n?$(t,e):t,o=s.length-1,c;o>=0;o--)(c=s[o])&&(i=c(i)||i);return i},R=(s,t)=>(e,n)=>t(e,n,s);let T=class extends S{constructor(s,t){super(),this._injector=s,this._userManagerService=t}getCurrentUser(){return this._userManagerService.getCurrentUser()}};T=z([R(0,r.Inject(r.Injector)),R(1,r.Inject(r.UserManagerService))],T);const E=class E{static get(){if(this._instance)return this._instance;const t=new E;return this._instance=t,t}static extend(t){Object.getOwnPropertyNames(t.prototype).forEach(e=>{e!=="constructor"&&(this.prototype[e]=t.prototype[e])}),Object.getOwnPropertyNames(t).forEach(e=>{e!=="prototype"&&e!=="name"&&e!=="length"&&(this[e]=t[e])})}get rectangle(){return r.Rectangle}get numfmt(){return r.numfmt}get tools(){return r.Tools}};p(E,"_instance");let _=E;var N=Object.getOwnPropertyDescriptor,A=(s,t,e,n)=>{for(var i=n>1?void 0:n?N(t,e):t,o=s.length-1,c;o>=0;o--)(c=s[o])&&(i=c(i)||i);return i},v=(s,t)=>(e,n)=>t(e,n,s);const C=Symbol("initializers");exports.FUniver=class extends r.Disposable{constructor(e,n,i,o){super();p(this,"_eventRegistry",new L);p(this,"registerEventHandler",(e,n)=>this._eventRegistry.registerEventHandler(e,n));this._injector=e,this._commandService=n,this._univerInstanceService=i,this._lifecycleService=o,this.registerEventHandler(this.Event.LifeCycleChanged,()=>r.toDisposable(this._lifecycleService.lifecycle$.subscribe(a=>{this.fireEvent(this.Event.LifeCycleChanged,{stage:a})}))),this._initUnitEvent(this._injector),this._initBeforeCommandEvent(this._injector),this._initCommandEvent(this._injector),this._injector.onDispose(()=>{this.dispose()});const c=Object.getPrototypeOf(this)[C];if(c){const a=this;c.forEach(function(l){l.apply(a,[e])})}}static newAPI(e){return(e instanceof r.Univer?e.__getInjector():e).createInstance(exports.FUniver)}_initialize(e){}static extend(e){Object.getOwnPropertyNames(e.prototype).forEach(n=>{if(n==="_initialize"){let i=this.prototype[C];i||(i=[],this.prototype[C]=i),i.push(e.prototype._initialize)}else n!=="constructor"&&(this.prototype[n]=e.prototype[n])}),Object.getOwnPropertyNames(e).forEach(n=>{n!=="prototype"&&n!=="name"&&n!=="length"&&(this[n]=e[n])})}_initCommandEvent(e){const n=e.get(r.ICommandService);this.registerEventHandler(this.Event.Redo,()=>n.onCommandExecuted(i=>{const{id:o,type:c,params:a}=i;if(i.id===r.RedoCommand.id){const d={id:o,type:c,params:a};this.fireEvent(this.Event.Redo,d)}})),this.registerEventHandler(this.Event.Undo,()=>n.onCommandExecuted(i=>{const{id:o,type:c,params:a}=i;if(i.id===r.UndoCommand.id){const d={id:o,type:c,params:a};this.fireEvent(this.Event.Undo,d)}})),this.registerEventHandler(this.Event.CommandExecuted,()=>n.onCommandExecuted((i,o)=>{const{id:c,type:a,params:l}=i;if(i.id!==r.RedoCommand.id&&i.id!==r.UndoCommand.id){const y={id:c,type:a,params:l,options:o};this.fireEvent(this.Event.CommandExecuted,y)}}))}_initBeforeCommandEvent(e){const n=e.get(r.ICommandService);this.registerEventHandler(this.Event.BeforeRedo,()=>n.beforeCommandExecuted(i=>{const{id:o,type:c,params:a}=i;if(i.id===r.RedoCommand.id){const d={id:o,type:c,params:a};if(this.fireEvent(this.Event.BeforeRedo,d),d.cancel)throw new r.CanceledError}})),this.registerEventHandler(this.Event.BeforeUndo,()=>n.beforeCommandExecuted(i=>{const{id:o,type:c,params:a}=i;if(i.id===r.UndoCommand.id){const d={id:o,type:c,params:a};if(this.fireEvent(this.Event.BeforeUndo,d),d.cancel)throw new r.CanceledError}})),this.registerEventHandler(this.Event.BeforeCommandExecute,()=>n.beforeCommandExecuted((i,o)=>{const{id:c,type:a,params:l}=i;if(i.id!==r.RedoCommand.id&&i.id!==r.UndoCommand.id){const y={id:c,type:a,params:l,options:o};if(this.fireEvent(this.Event.BeforeCommandExecute,y),y.cancel)throw new r.CanceledError}}))}_initUnitEvent(e){const n=e.get(r.IUniverInstanceService);this.registerEventHandler(this.Event.DocDisposed,()=>n.unitDisposed$.subscribe(i=>{i.type===r.UniverInstanceType.UNIVER_DOC&&this.fireEvent(this.Event.DocDisposed,{unitId:i.getUnitId(),unitType:i.type,snapshot:i.getSnapshot()})})),this.registerEventHandler(this.Event.DocCreated,()=>n.unitAdded$.subscribe(i=>{if(i.type===r.UniverInstanceType.UNIVER_DOC){const o=i,c=e.createInstance(D,o);this.fireEvent(this.Event.DocCreated,{unitId:i.getUnitId(),type:i.type,doc:c,unit:c})}}))}disposeUnit(e){return this._univerInstanceService.disposeUnit(e)}getCurrentLifecycleStage(){return this._injector.get(r.LifecycleService).stage}undo(){return this._commandService.executeCommand(r.UndoCommand.id)}redo(){return this._commandService.executeCommand(r.RedoCommand.id)}toggleDarkMode(e){this._injector.get(r.ThemeService).setDarkMode(e)}loadLocales(e,n){this._injector.get(r.LocaleService).load({[e]:n})}setLocale(e){this._injector.get(r.LocaleService).setLocale(e)}onBeforeCommandExecute(e){return this._commandService.beforeCommandExecuted((n,i)=>{e(n,i)})}onCommandExecuted(e){return this._commandService.onCommandExecuted((n,i)=>{e(n,i)})}executeCommand(e,n,i){return this._commandService.executeCommand(e,n,i)}syncExecuteCommand(e,n,i){return this._commandService.syncExecuteCommand(e,n,i)}getHooks(){return this._injector.createInstance(exports.FHooks)}get Enum(){return f.get()}get Event(){return m.get()}get Util(){return _.get()}addEvent(e,n){if(!e||!n)throw new Error("Cannot add empty event");return this._eventRegistry.addEvent(e,n)}fireEvent(e,n){return this._eventRegistry.fireEvent(e,n)}getUserManager(){return this._injector.createInstance(T)}newBlob(){return this._injector.createInstance(exports.FBlob)}newColor(){return new r.ColorBuilder}newRichText(e){return r.RichTextBuilder.create(e)}newRichTextValue(e){return r.RichTextValue.create(e)}newParagraphStyle(e){return r.ParagraphStyleBuilder.create(e)}newParagraphStyleValue(e){return r.ParagraphStyleValue.create(e)}newTextStyle(e){return r.TextStyleBuilder.create(e)}newTextStyleValue(e){return r.TextStyleValue.create(e)}newTextDecoration(e){return new r.TextDecorationBuilder(e)}};exports.FUniver=A([v(0,r.Inject(r.Injector)),v(1,r.ICommandService),v(2,r.IUniverInstanceService),v(3,r.Inject(r.LifecycleService))],exports.FUniver);exports.FBase=S;exports.FBaseInitialable=x;exports.FEnum=f;exports.FEventName=m;exports.FUtil=_;