@univerjs/core
Version:
Core library for Univer.
1,467 lines (1,466 loc) • 39.4 kB
JavaScript
var F = Object.defineProperty;
var W = (i, e, t) => e in i ? F(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
var d = (i, e, t) => W(i, typeof e != "symbol" ? e + "" : e, t);
import { Disposable as I, Inject as p, Injector as _, AbsoluteRefType as q, UniverInstanceType as R, LifecycleStages as f, DataValidationType as G, DataValidationErrorStyle as J, DataValidationRenderMode as K, DataValidationOperator as Q, DataValidationStatus as X, CommandType as Y, BaselineOffset as Z, BooleanNumber as k, HorizontalAlign as ee, TextDecoration as te, TextDirection as re, VerticalAlign as ie, WrapStrategy as ne, BorderType as oe, BorderStyleTypes as se, AutoFillSeries as ce, ColorType as ae, CommonHideTypes as de, CopyPasteType as le, DeleteDirection as pe, DeveloperMetadataVisibility as he, Dimension as ue, Direction as ge, InterpolationPointType as ye, LocaleType as ve, MentionType as fe, ProtectionType as _e, RelativeDate as me, SheetTypes as Ee, ThemeColorType as Se, LifecycleService as M, toDisposable as l, ICommandService as g, UndoCommand as h, IUndoRedoService as E, RedoCommand as u, Registry as be, UserManagerService as Ce, Rectangle as De, numfmt as xe, Tools as Te, IUniverInstanceService as N, Univer as we, CanceledError as x, ThemeService as Re, LocaleService as z, ColorBuilder as je, RichTextBuilder as Oe, RichTextValue as Pe, ParagraphStyleBuilder as Be, ParagraphStyleValue as He, TextStyleBuilder as Ue, TextStyleValue as Ve, TextDecorationBuilder as Ie } from "@univerjs/core";
import { filter as S } from "rxjs";
class $ extends I {
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
}
const T = Symbol("initializers");
class Me extends I {
constructor(e) {
super(), this._injector = e;
const t = this, r = Object.getPrototypeOf(this)[T];
r && r.forEach(function(n) {
n.apply(t, [e]);
});
}
/**
* @ignore
*/
_initialize(e) {
}
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
if (t === "_initialize") {
let r = this.prototype[T];
r || (r = [], this.prototype[T] = r), r.push(e.prototype._initialize);
} else t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
}
var $e = Object.getOwnPropertyDescriptor, ze = (i, e, t, r) => {
for (var n = r > 1 ? void 0 : r ? $e(e, t) : e, o = i.length - 1, s; o >= 0; o--)
(s = i[o]) && (n = s(n) || n);
return n;
}, Ae = (i, e) => (t, r) => e(t, r, i);
let C = class extends $ {
constructor(i, e) {
super(), this._blob = i, this._injector = e;
}
/**
* Returns a copy of this blob.
* @returns a new blob by copying the current blob
* @example
* ```ts
* const blob = univerAPI.newBlob();
* const newBlob = blob.copyBlob();
* console.log(newBlob);
* ```
*/
copyBlob() {
return this._injector.createInstance(C, this._blob);
}
/**
* Return the data inside this object as a blob converted to the specified content type.
* @param contentType the content type refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types
* @returns a new blob by converting the current blob to the specified content type
* @example
* ```ts
* const blob = univerAPI.newBlob();
* const newBlob = blob.getAs('text/plain');
* console.log(newBlob);
* ```
*/
getAs(i) {
const e = this.copyBlob();
return e.setContentType(i), e;
}
getDataAsString(i) {
return this._blob === null ? Promise.resolve("") : i === void 0 ? this._blob.text() : new Promise((e, t) => {
this._blob.arrayBuffer().then((r) => {
const n = new TextDecoder(i).decode(r);
e(n);
}).catch((r) => {
t(new Error(`Failed to read Blob as ArrayBuffer: ${r.message}`));
});
});
}
/**
* Gets the data stored in this blob.
* @returns the blob content as a byte array
* @example
* ```ts
* const blob = univerAPI.newBlob();
* const bytes = await blob.getBytes();
* console.log(bytes);
* ```
*/
getBytes() {
return this._blob ? this._blob.arrayBuffer().then((i) => new Uint8Array(i)) : Promise.reject(new Error("Blob is undefined or null."));
}
/**
* Sets the data stored in this blob.
* @param bytes a byte array
* @returns the blob object
* @example
* ```ts
* const blob = univerAPI.newBlob();
* const bytes = new Uint8Array(10);
* blob.setBytes(bytes);
* ```
*/
setBytes(i) {
return this._blob = new Blob([i]), this;
}
setDataFromString(i, e) {
const t = e != null ? e : "text/plain", r = new Blob([i], { type: t });
return this._blob = r, this;
}
/**
* Gets the content type of the data stored in this blob.
* @returns the content type
* @example
* ```ts
* const blob = univerAPI.newBlob();
* const contentType = blob.getContentType();
* console.log(contentType);
* ```
*/
getContentType() {
var i;
return (i = this._blob) == null ? void 0 : i.type;
}
/**
* Sets the content type of the data stored in this blob.
* @param contentType the content type refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/MIME_types/Common_types
* @returns the blob object
* @example
* ```ts
* const blob = univerAPI.newBlob();
* blob.setContentType('text/plain');
* ```
*/
setContentType(i) {
var e;
return this._blob = (e = this._blob) == null ? void 0 : e.slice(0, this._blob.size, i), this;
}
};
C = ze([
Ae(1, p(_))
], C);
const y = class y {
static get() {
if (this._instance)
return this._instance;
const e = new y();
return this._instance = e, e;
}
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
constructor() {
for (const e in y.prototype)
this[e] = y.prototype[e];
}
/**
* Defines different types of absolute references
*
* @example
* ```ts
* console.log(univerAPI.Enum.AbsoluteRefType);
* ```
*/
get AbsoluteRefType() {
return q;
}
/**
* Defines different types of Univer instances
*
* @example
* ```ts
* console.log(univerAPI.Enum.UniverInstanceType.UNIVER_SHEET);
* ```
*/
get UniverInstanceType() {
return R;
}
/**
* Represents different stages in the lifecycle
*
* @example
* ```ts
* console.log(univerAPI.Enum.LifecycleStages.Rendered);
* ```
*/
get LifecycleStages() {
return f;
}
/**
* Different types of data validation
*
* @example
* ```ts
* console.log(univerAPI.Enum.DataValidationType.LIST);
* ```
*/
get DataValidationType() {
return G;
}
/**
* Different error display styles
*
* @example
* ```ts
* console.log(univerAPI.Enum.DataValidationErrorStyle.WARNING);
* ```
*/
get DataValidationErrorStyle() {
return J;
}
/**
* Different validation rendering modes
*
* @example
* ```ts
* console.log(univerAPI.Enum.DataValidationRenderMode.TEXT);
* ```
*/
get DataValidationRenderMode() {
return K;
}
/**
* Different validation operators
*
* @example
* ```ts
* console.log(univerAPI.Enum.DataValidationOperator.BETWEEN);
* ```
*/
get DataValidationOperator() {
return Q;
}
/**
* Different validation states
*
* @example
* ```ts
* console.log(univerAPI.Enum.DataValidationStatus.VALID);
* ```
*/
get DataValidationStatus() {
return X;
}
/**
* Different types of commands
*
* @example
* ```ts
* console.log(univerAPI.Enum.CommandType.COMMAND);
* ```
*/
get CommandType() {
return Y;
}
/**
* Different baseline offsets for text baseline positioning
*
* @example
* ```ts
* console.log(univerAPI.Enum.BaselineOffset.SUPERSCRIPT);
* ```
*/
get BaselineOffset() {
return Z;
}
/**
* Boolean number representations
*
* @example
* ```ts
* console.log(univerAPI.Enum.BooleanNumber.TRUE);
* ```
*/
get BooleanNumber() {
return k;
}
/**
* Different horizontal text alignment options
*
* @example
* ```ts
* console.log(univerAPI.Enum.HorizontalAlign.CENTER);
* ```
*/
get HorizontalAlign() {
return ee;
}
/**
* Different text decoration styles
*
* @example
* ```ts
* console.log(univerAPI.Enum.TextDecoration.DOUBLE);
* ```
*/
get TextDecoration() {
return te;
}
/**
* Different text direction options
*
* @example
* ```ts
* console.log(univerAPI.Enum.TextDirection.LEFT_TO_RIGHT);
* ```
*/
get TextDirection() {
return re;
}
/**
* Different vertical text alignment options
*
* @example
* ```ts
* console.log(univerAPI.Enum.VerticalAlign.MIDDLE);
* ```
*/
get VerticalAlign() {
return ie;
}
/**
* Different wrap strategy options
*
* @example
* ```ts
* console.log(univerAPI.Enum.WrapStrategy.WRAP);
* ```
*/
get WrapStrategy() {
return ne;
}
/**
* Different border types
*
* @example
* ```ts
* console.log(univerAPI.Enum.BorderType.OUTSIDE);
* ```
*/
get BorderType() {
return oe;
}
/**
* Different border style types
*
* @example
* ```ts
* console.log(univerAPI.Enum.BorderStyleTypes.NONE);
* ```
*/
get BorderStyleTypes() {
return se;
}
/**
* Auto fill series types
*
* @example
* ```ts
* console.log(univerAPI.Enum.AutoFillSeries.ALTERNATE_SERIES);
* ```
*/
get AutoFillSeries() {
return ce;
}
/**
* Color types
*
* @example
* ```ts
* console.log(univerAPI.Enum.ColorType.RGB);
* ```
*/
get ColorType() {
return ae;
}
/**
* Common hide types
*
* @example
* ```ts
* console.log(univerAPI.Enum.CommonHideTypes.ON);
* ```
*/
get CommonHideTypes() {
return de;
}
/**
* Copy paste types
*
* @example
* ```ts
* console.log(univerAPI.Enum.CopyPasteType.PASTE_VALUES);
* ```
*/
get CopyPasteType() {
return le;
}
/**
* Delete direction types
*
* @example
* ```ts
* console.log(univerAPI.Enum.DeleteDirection.LEFT);
* ```
*/
get DeleteDirection() {
return pe;
}
/**
* Developer metadata visibility types
*
* @example
* ```ts
* console.log(univerAPI.Enum.DeveloperMetadataVisibility.DOCUMENT);
* ```
*/
get DeveloperMetadataVisibility() {
return he;
}
/**
* Dimension types
*
* @example
* ```ts
* console.log(univerAPI.Enum.Dimension.ROWS);
* ```
*/
get Dimension() {
return ue;
}
/**
* Direction types
*
* @example
* ```ts
* console.log(univerAPI.Enum.Direction.UP);
* ```
*/
get Direction() {
return ge;
}
/**
* Interpolation point types
*
* @example
* ```ts
* console.log(univerAPI.Enum.InterpolationPointType.NUMBER);
* ```
*/
get InterpolationPointType() {
return ye;
}
/**
* Locale types
*
* @example
* ```ts
* console.log(univerAPI.Enum.LocaleType.EN_US);
* ```
*/
get LocaleType() {
return ve;
}
/**
* Mention types
*
* @example
* ```ts
* console.log(univerAPI.Enum.MentionType.PERSON);
* ```
*/
get MentionType() {
return fe;
}
/**
* Protection types
*
* @example
* ```ts
* console.log(univerAPI.Enum.ProtectionType.RANGE);
* ```
*/
get ProtectionType() {
return _e;
}
/**
* Relative date types
*
* @example
* ```ts
* console.log(univerAPI.Enum.RelativeDate.TODAY);
* ```
*/
get RelativeDate() {
return me;
}
/**
* Sheet types
*
* @example
* ```ts
* console.log(univerAPI.Enum.SheetTypes.GRID);
* ```
*/
get SheetTypes() {
return Ee;
}
/**
* Theme color types
*
* @example
* ```ts
* console.log(univerAPI.Enum.ThemeColorType.ACCENT1);
* ```
*/
get ThemeColorType() {
return Se;
}
};
/**
* @ignore
*/
d(y, "_instance");
let j = y;
const v = class v {
static get() {
if (this._instance)
return this._instance;
const e = new v();
return this._instance = e, e;
}
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
constructor() {
for (const e in v.prototype)
this[e] = v.prototype[e];
}
/**
* Event fired when a document is created
* @see {@link IDocCreatedParam}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.DocCreated, (params) => {
* const { unitId, type, doc, unit } = params;
* console.log('doc created', params);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get DocCreated() {
return "DocCreated";
}
/**
* Event fired when a document is disposed
* @see {@link IDocDisposedEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.DocDisposed, (params) => {
* const { unitId, unitType, snapshot } = params;
* console.log('doc disposed', params);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get DocDisposed() {
return "DocDisposed";
}
/**
* Event fired when life cycle is changed
* @see {@link ILifeCycleChangedEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, (params) => {
* const { stage } = params;
* console.log('life cycle changed', params);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get LifeCycleChanged() {
return "LifeCycleChanged";
}
/**
* Event fired when a redo command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.Redo, (event) => {
* const { params, id, type } = event;
* console.log('redo command executed', event);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get Redo() {
return "Redo";
}
/**
* Event fired when an undo command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.Undo, (event) => {
* const { params, id, type } = event;
* console.log('undo command executed', event);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get Undo() {
return "Undo";
}
/**
* Event fired before a redo command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.BeforeRedo, (event) => {
* const { params, id, type } = event;
* console.log('before redo command executed', event);
*
* // Cancel the redo operation
* event.cancel = true;
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get BeforeRedo() {
return "BeforeRedo";
}
/**
* Event fired before an undo command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.BeforeUndo, (event) => {
* const { params, id, type } = event;
* console.log('before undo command executed', event);
*
* // Cancel the undo operation
* event.cancel = true;
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get BeforeUndo() {
return "BeforeUndo";
}
/**
* Event fired when a command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.CommandExecuted, (event) => {
* const { params, id, type, options } = event;
* console.log('command executed', event);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get CommandExecuted() {
return "CommandExecuted";
}
/**
* Event fired before a command is executed
* @see {@link ICommandEvent}
* @example
* ```ts
* const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => {
* const { params, id, type, options } = event;
* console.log('before command executed', event);
*
* // Cancel the command execution
* event.cancel = true;
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
get BeforeCommandExecute() {
return "BeforeCommandExecute";
}
};
/**
* @ignore
*/
d(v, "_instance");
let O = v;
var Le = Object.getOwnPropertyDescriptor, Ne = (i, e, t, r) => {
for (var n = r > 1 ? void 0 : r ? Le(e, t) : e, o = i.length - 1, s; o >= 0; o--)
(s = i[o]) && (n = s(n) || n);
return n;
}, A = (i, e) => (t, r) => e(t, r, i);
let P = class extends $ {
constructor(i, e) {
super(), this._injector = i, this._lifecycleService = e;
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => {})` as instead
*/
onStarting(i) {
return l(this._lifecycleService.lifecycle$.pipe(S((e) => e === f.Starting)).subscribe(i));
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => {})` as instead
*/
onReady(i) {
return l(this._lifecycleService.lifecycle$.pipe(S((e) => e === f.Ready)).subscribe(i));
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => {})` as instead
*/
onRendered(i) {
return l(this._lifecycleService.lifecycle$.pipe(S((e) => e === f.Rendered)).subscribe(i));
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, ({ stage }) => {})` as instead
*/
onSteady(i) {
return l(this._lifecycleService.lifecycle$.pipe(S((e) => e === f.Steady)).subscribe(i));
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.BeforeUndo, (event) => {})` as instead
*/
onBeforeUndo(i) {
return this._injector.get(g).beforeCommandExecuted((t) => {
if (t.id === h.id) {
const n = this._injector.get(E).pitchTopUndoElement();
n && i(n);
}
});
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.Undo, (event) => {})` as instead
*/
onUndo(i) {
return this._injector.get(g).onCommandExecuted((t) => {
if (t.id === h.id) {
const n = this._injector.get(E).pitchTopUndoElement();
n && i(n);
}
});
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.BeforeRedo, (event) => {})` as instead
*/
onBeforeRedo(i) {
return this._injector.get(g).beforeCommandExecuted((t) => {
if (t.id === u.id) {
const n = this._injector.get(E).pitchTopRedoElement();
n && i(n);
}
});
}
/**
* @param callback
* @deprecated use `univerAPI.addEvent(univerAPI.Event.Redo, (event) => {})` as instead
*/
onRedo(i) {
return this._injector.get(g).onCommandExecuted((t) => {
if (t.id === u.id) {
const n = this._injector.get(E).pitchTopRedoElement();
n && i(n);
}
});
}
};
P = Ne([
A(0, p(_)),
A(1, p(M))
], P);
var Fe = Object.getOwnPropertyDescriptor, We = (i, e, t, r) => {
for (var n = r > 1 ? void 0 : r ? Fe(e, t) : e, o = i.length - 1, s; o >= 0; o--)
(s = i[o]) && (n = s(n) || n);
return n;
}, qe = (i, e) => (t, r) => e(t, r, i);
let B = class extends Me {
constructor(i, e) {
super(e), this.doc = i;
}
};
B = We([
qe(1, p(_))
], B);
class Ge {
constructor() {
d(this, "_eventRegistry", /* @__PURE__ */ new Map());
d(this, "_eventHandlerMap", /* @__PURE__ */ new Map());
d(this, "_eventHandlerRegisted", /* @__PURE__ */ new Map());
}
_ensureEventRegistry(e) {
return this._eventRegistry.has(e) || this._eventRegistry.set(e, new be()), this._eventRegistry.get(e);
}
registerEventHandler(e, t) {
const r = this._eventHandlerMap.get(e);
return r ? r.add(t) : this._eventHandlerMap.set(e, /* @__PURE__ */ new Set([t])), this._ensureEventRegistry(e).getData().length && this._initEventHandler(e), l(() => {
var n, o, s, c;
(n = this._eventHandlerMap.get(e)) == null || n.delete(t), (s = (o = this._eventHandlerRegisted.get(e)) == null ? void 0 : o.get(t)) == null || s.dispose(), (c = this._eventHandlerRegisted.get(e)) == null || c.delete(t);
});
}
removeEvent(e, t) {
const r = this._ensureEventRegistry(e);
if (r.delete(t), r.getData().length === 0) {
const n = this._eventHandlerRegisted.get(e);
n == null || n.forEach((o) => o.dispose()), this._eventHandlerRegisted.delete(e);
}
}
_initEventHandler(e) {
let t = this._eventHandlerRegisted.get(e);
const r = this._eventHandlerMap.get(e);
r && (t || (t = /* @__PURE__ */ new Map(), this._eventHandlerRegisted.set(e, t), r == null || r.forEach((n) => {
t == null || t.set(n, l(n()));
})));
}
/**
* Add an event listener
* @param {string} event key of event
* @param {(params: IEventParamConfig[typeof event]) => void} callback callback when event triggered
* @returns {Disposable} The Disposable instance, for remove the listener
* @example
* ```ts
* univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, (params) => {
* const { stage } = params;
* console.log('life cycle changed', params);
* });
* ```
*/
addEvent(e, t) {
return this._ensureEventRegistry(e).add(t), this._initEventHandler(e), l(() => this.removeEvent(e, t));
}
/**
* Fire an event, used in internal only.
* @param {string} event key of event
* @param {any} params params of event
* @returns {boolean} should cancel
* @example
* ```ts
* this.fireEvent(univerAPI.Event.LifeCycleChanged, params);
* ```
*/
fireEvent(e, t) {
var r;
return (r = this._eventRegistry.get(e)) == null || r.getData().forEach((n) => {
n(t);
}), t.cancel;
}
}
var Je = Object.getOwnPropertyDescriptor, Ke = (i, e, t, r) => {
for (var n = r > 1 ? void 0 : r ? Je(e, t) : e, o = i.length - 1, s; o >= 0; o--)
(s = i[o]) && (n = s(n) || n);
return n;
}, L = (i, e) => (t, r) => e(t, r, i);
let H = class extends $ {
constructor(i, e) {
super(), this._injector = i, this._userManagerService = e;
}
/**
* Get current user info.
* @returns {IUser} Current user info.
* @example
* ```typescript
* univerAPI.getUserManager().getCurrentUser();
* ```
*/
getCurrentUser() {
return this._userManagerService.getCurrentUser();
}
};
H = Ke([
L(0, p(_)),
L(1, p(Ce))
], H);
const D = class D {
static get() {
if (this._instance)
return this._instance;
const e = new D();
return this._instance = e, e;
}
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
/**
* Rectangle utils, including range operations likes merge, subtract, split
*
* @example
* ```ts
* const ranges = [
* { startRow: 0, startColumn: 0, endRow: 1, endColumn: 1 },
* { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 }
* ];
* const merged = univerAPI.Util.rectangle.mergeRanges(ranges);
* console.log(merged);
* ```
*/
get rectangle() {
return De;
}
/**
* Number format utils, including parse and strigify about date, price, etc
*
* @example
* ```ts
* const text = univerAPI.Util.numfmt.format('#,##0.00', 1234.567);
* console.log(text);
* ```
*/
get numfmt() {
return xe;
}
/**
* common tools
*
* @example
* ```ts
* const key = univerAPI.Util.tools.generateRandomId(6);
* console.log(key);
* ```
*/
get tools() {
return Te;
}
};
/**
* @ignore
*/
d(D, "_instance");
let U = D;
var Qe = Object.getOwnPropertyDescriptor, Xe = (i, e, t, r) => {
for (var n = r > 1 ? void 0 : r ? Qe(e, t) : e, o = i.length - 1, s; o >= 0; o--)
(s = i[o]) && (n = s(n) || n);
return n;
}, b = (i, e) => (t, r) => e(t, r, i);
const w = Symbol("initializers");
let V = class extends I {
constructor(e, t, r, n) {
super();
d(this, "_eventRegistry", new Ge());
d(this, "registerEventHandler", (e, t) => this._eventRegistry.registerEventHandler(e, t));
this._injector = e, this._commandService = t, this._univerInstanceService = r, this._lifecycleService = n, this.registerEventHandler(
this.Event.LifeCycleChanged,
() => l(
this._lifecycleService.lifecycle$.subscribe((s) => {
this.fireEvent(this.Event.LifeCycleChanged, { stage: s });
})
)
), this._initUnitEvent(this._injector), this._initBeforeCommandEvent(this._injector), this._initCommandEvent(this._injector), this._injector.onDispose(() => {
this.dispose();
});
const o = Object.getPrototypeOf(this)[w];
if (o) {
const s = this;
o.forEach(function(c) {
c.apply(s, [e]);
});
}
}
/**
* Create an FUniver instance, if the injector is not provided, it will create a new Univer instance.
* @static
* @param {Univer | Injector} wrapped - The Univer instance or injector instance.
* @returns {FUniver} - The FUniver instance.
*
* @example
* ```ts
* const univerAPI = FUniver.newAPI(univer);
* ```
*/
static newAPI(e) {
return (e instanceof we ? e.__getInjector() : e).createInstance(V);
}
/**
* @ignore
*/
_initialize(e) {
}
/**
* @ignore
*/
static extend(e) {
Object.getOwnPropertyNames(e.prototype).forEach((t) => {
if (t === "_initialize") {
let r = this.prototype[w];
r || (r = [], this.prototype[w] = r), r.push(e.prototype._initialize);
} else t !== "constructor" && (this.prototype[t] = e.prototype[t]);
}), Object.getOwnPropertyNames(e).forEach((t) => {
t !== "prototype" && t !== "name" && t !== "length" && (this[t] = e[t]);
});
}
_initCommandEvent(e) {
const t = e.get(g);
this.registerEventHandler(
this.Event.Redo,
() => t.onCommandExecuted((r) => {
const { id: n, type: o, params: s } = r;
if (r.id === u.id) {
const a = { id: n, type: o, params: s };
this.fireEvent(this.Event.Redo, a);
}
})
), this.registerEventHandler(
this.Event.Undo,
() => t.onCommandExecuted((r) => {
const { id: n, type: o, params: s } = r;
if (r.id === h.id) {
const a = { id: n, type: o, params: s };
this.fireEvent(this.Event.Undo, a);
}
})
), this.registerEventHandler(
this.Event.CommandExecuted,
() => t.onCommandExecuted((r, n) => {
const { id: o, type: s, params: c } = r;
if (r.id !== u.id && r.id !== h.id) {
const m = { id: o, type: s, params: c, options: n };
this.fireEvent(this.Event.CommandExecuted, m);
}
})
);
}
_initBeforeCommandEvent(e) {
const t = e.get(g);
this.registerEventHandler(
this.Event.BeforeRedo,
() => t.beforeCommandExecuted((r) => {
const { id: n, type: o, params: s } = r;
if (r.id === u.id) {
const a = { id: n, type: o, params: s };
if (this.fireEvent(this.Event.BeforeRedo, a), a.cancel)
throw new x();
}
})
), this.registerEventHandler(
this.Event.BeforeUndo,
() => t.beforeCommandExecuted((r) => {
const { id: n, type: o, params: s } = r;
if (r.id === h.id) {
const a = { id: n, type: o, params: s };
if (this.fireEvent(this.Event.BeforeUndo, a), a.cancel)
throw new x();
}
})
), this.registerEventHandler(
this.Event.BeforeCommandExecute,
() => t.beforeCommandExecuted((r, n) => {
const { id: o, type: s, params: c } = r;
if (r.id !== u.id && r.id !== h.id) {
const m = { id: o, type: s, params: c, options: n };
if (this.fireEvent(this.Event.BeforeCommandExecute, m), m.cancel)
throw new x();
}
})
);
}
_initUnitEvent(e) {
const t = e.get(N);
this.registerEventHandler(
this.Event.DocDisposed,
() => t.unitDisposed$.subscribe((r) => {
r.type === R.UNIVER_DOC && this.fireEvent(this.Event.DocDisposed, {
unitId: r.getUnitId(),
unitType: r.type,
snapshot: r.getSnapshot()
});
})
), this.registerEventHandler(
this.Event.DocCreated,
() => t.unitAdded$.subscribe((r) => {
if (r.type === R.UNIVER_DOC) {
const n = r, o = e.createInstance(B, n);
this.fireEvent(this.Event.DocCreated, {
unitId: r.getUnitId(),
type: r.type,
doc: o,
unit: o
});
}
})
);
}
/**
* Dispose the UniverSheet by the `unitId`. The UniverSheet would be unload from the application.
* @param unitId The unit id of the UniverSheet.
* @returns Whether the Univer instance is disposed successfully.
*
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const unitId = fWorkbook?.getId();
*
* if (unitId) {
* univerAPI.disposeUnit(unitId);
* }
* ```
*/
disposeUnit(e) {
return this._univerInstanceService.disposeUnit(e);
}
/**
* Get the current lifecycle stage.
* @returns {LifecycleStages} - The current lifecycle stage.
*
* @example
* ```ts
* const stage = univerAPI.getCurrentLifecycleStage();
* console.log(stage);
* ```
*/
getCurrentLifecycleStage() {
return this._injector.get(M).stage;
}
/**
* Undo an editing on the currently focused document.
* @returns {Promise<boolean>} undo result
*
* @example
* ```ts
* await univerAPI.undo();
* ```
*/
undo() {
return this._commandService.executeCommand(h.id);
}
/**
* Redo an editing on the currently focused document.
* @returns {Promise<boolean>} redo result
*
* @example
* ```ts
* await univerAPI.redo();
* ```
*/
redo() {
return this._commandService.executeCommand(u.id);
}
/**
* Toggle dark mode on or off.
* @param {boolean} isDarkMode - Whether the dark mode is enabled.
* @example
* ```ts
* univerAPI.toggleDarkMode(true);
* ```
*/
toggleDarkMode(e) {
this._injector.get(Re).setDarkMode(e);
}
/**
* Load locales for the given locale.
* @description This method is utilized to load locales, which can be either built-in or custom-defined.
* @param {string} locale - A unique locale identifier.
* @param {ILanguagePack} locales - The locales object containing the translations.
* @example
* ```ts
* univerAPI.loadLocales('esES', {
* 'Hello World': 'Hola Mundo',
* });
* ```
*/
loadLocales(e, t) {
this._injector.get(z).load({ [e]: t });
}
/**
* Set the current locale.
* @param {string} locale - A unique locale identifier.
* @example
* ```ts
* univerAPI.setLocale('esES');
* ```
*/
setLocale(e) {
this._injector.get(z).setLocale(e);
}
/**
* Register a callback that will be triggered before invoking a command.
* @deprecated use `univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => {})` instead.
* @param {CommandListener} callback The callback.
* @returns {IDisposable} The disposable instance.
*/
onBeforeCommandExecute(e) {
return this._commandService.beforeCommandExecuted((t, r) => {
e(t, r);
});
}
/**
* Register a callback that will be triggered when a command is invoked.
* @deprecated use `univerAPI.addEvent(univerAPI.Event.CommandExecuted, (event) => {})` instead.
* @param {CommandListener} callback The callback.
* @returns {IDisposable} The disposable instance.
*/
onCommandExecuted(e) {
return this._commandService.onCommandExecuted((t, r) => {
e(t, r);
});
}
/**
* Execute a command with the given id and parameters.
* @param id Identifier of the command.
* @param params Parameters of this execution.
* @param options Options of this execution.
* @returns The result of the execution. It is a boolean value by default which indicates the command is executed.
*
* @example
* ```ts
* univerAPI.executeCommand('sheet.command.set-range-values', {
* value: { v: "Hello, Univer!" },
* range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 0 }
* });
* ```
*/
executeCommand(e, t, r) {
return this._commandService.executeCommand(e, t, r);
}
/**
* Execute a command with the given id and parameters synchronously.
* @param id Identifier of the command.
* @param params Parameters of this execution.
* @param options Options of this execution.
* @returns The result of the execution. It is a boolean value by default which indicates the command is executed.
*
* @example
* ```ts
* univerAPI.syncExecuteCommand('sheet.command.set-range-values', {
* value: { v: "Hello, Univer!" },
* range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 0 }
* });
* ```
*/
syncExecuteCommand(e, t, r) {
return this._commandService.syncExecuteCommand(e, t, r);
}
/**
* Get hooks.
* @deprecated use `addEvent` instead.
* @returns {FHooks} FHooks instance
*/
getHooks() {
return this._injector.createInstance(P);
}
get Enum() {
return j.get();
}
get Event() {
return O.get();
}
get Util() {
return U.get();
}
/**
* Add an event listener
* @param {string} event key of event
* @param {(params: IEventParamConfig[typeof event]) => void} callback callback when event triggered
* @returns {Disposable} The Disposable instance, for remove the listener
* @example
* ```ts
* // Add life cycle changed event listener
* const disposable = univerAPI.addEvent(univerAPI.Event.LifeCycleChanged, (params) => {
* const { stage } = params;
* console.log('life cycle changed', params);
* });
*
* // Remove the event listener, use `disposable.dispose()`
* ```
*/
addEvent(e, t) {
if (!e || !t) throw new Error("Cannot add empty event");
return this._eventRegistry.addEvent(e, t);
}
/**
* Fire an event, used in internal only.
* @param {string} event key of event
* @param {any} params params of event
* @returns {boolean} should cancel
* @example
* ```ts
* this.fireEvent(univerAPI.Event.LifeCycleChanged, params);
* ```
*/
fireEvent(e, t) {
return this._eventRegistry.fireEvent(e, t);
}
getUserManager() {
return this._injector.createInstance(H);
}
/**
* Create a new blob.
* @returns {FBlob} The new blob instance
* @example
* ```ts
* const blob = univerAPI.newBlob();
* ```
*/
newBlob() {
return this._injector.createInstance(C);
}
/**
* Create a new color.
* @returns {ColorBuilder} The new color instance
* @example
* ```ts
* const color = univerAPI.newColor();
* ```
* @deprecated
*/
newColor() {
return new je();
}
/**
* Create a new rich text.
* @param {IDocumentData} data
* @returns {RichTextBuilder} The new rich text instance
* @example
* ```ts
* const richText = univerAPI.newRichText({ body: { dataStream: 'Hello World\r\n' } });
* const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1');
* range.setRichTextValueForCell(richText);
* ```
*/
newRichText(e) {
return Oe.create(e);
}
/**
* Create a new rich text value.
* @param {IDocumentData} data - The rich text data
* @returns {RichTextValue} The new rich text value instance
* @example
* ```ts
* const richTextValue = univerAPI.newRichTextValue({ body: { dataStream: 'Hello World\r\n' } });
* const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1');
* range.setRichTextValueForCell(richTextValue);
* ```
*/
newRichTextValue(e) {
return Pe.create(e);
}
/**
* Create a new paragraph style.
* @param {IParagraphStyle} style - The paragraph style
* @returns {ParagraphStyleBuilder} The new paragraph style instance
* @example
* ```ts
* const richText = univerAPI.newRichText({ body: { dataStream: 'Hello World\r\n' } });
* const paragraphStyle = univerAPI.newParagraphStyle({ textStyle: { ff: 'Arial', fs: 12, it: univerAPI.Enum.BooleanNumber.TRUE, bl: univerAPI.Enum.BooleanNumber.TRUE } });
* richText.insertParagraph(paragraphStyle);
* const range = univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1');
* range.setRichTextValueForCell(richText);
* ```
*/
newParagraphStyle(e) {
return Be.create(e);
}
/**
* Create a new paragraph style value.
* @param {IParagraphStyle} style - The paragraph style
* @returns {ParagraphStyleValue} The new paragraph style value instance
* @example
* ```ts
* const paragraphStyleValue = univerAPI.newParagraphStyleValue();
* ```
*/
newParagraphStyleValue(e) {
return He.create(e);
}
/**
* Create a new text style.
* @param {ITextStyle} style - The text style
* @returns {TextStyleBuilder} The new text style instance
* @example
* ```ts
* const textStyle = univerAPI.newTextStyle();
* ```
*/
newTextStyle(e) {
return Ue.create(e);
}
/**
* Create a new text style value.
* @param {ITextStyle} style - The text style
* @returns {TextStyleValue} The new text style value instance
* @example
* ```ts
* const textStyleValue = univerAPI.newTextStyleValue();
* ```
*/
newTextStyleValue(e) {
return Ve.create(e);
}
/**
* Create a new text decoration.
* @param {ITextDecoration} decoration - The text decoration
* @returns {TextDecorationBuilder} The new text decoration instance
* @example
* ```ts
* const decoration = univerAPI.newTextDecoration();
* ```
*/
newTextDecoration(e) {
return new Ie(e);
}
};
V = Xe([
b(0, p(_)),
b(1, g),
b(2, N),
b(3, p(M))
], V);
export {
$ as FBase,
Me as FBaseInitialable,
C as FBlob,
j as FEnum,
O as FEventName,
P as FHooks,
V as FUniver,
U as FUtil
};