UNPKG

@univerjs/core

Version:

Core library for Univer.

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