@univerjs/docs 0.24.0 → 0.25.0-insiders.20260608-e4336f7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  | Package | UMD global | CSS | Locales | Facade entry |
12
12
  | --- | --- | :---: | :---: | :---: |
13
- | `@univerjs/docs` | `UniverDocs` | No | No | No |
13
+ | `@univerjs/docs` | `UniverDocs` | No | No | Yes |
14
14
 
15
15
  ## Installation
16
16
 
@@ -0,0 +1,351 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _univerjs_core = require("@univerjs/core");
3
+ let _univerjs_core_facade = require("@univerjs/core/facade");
4
+ let _univerjs_docs = require("@univerjs/docs");
5
+
6
+ //#region src/facade/utils.ts
7
+ function cloneParagraphStyle(paragraphStyle) {
8
+ return paragraphStyle == null ? paragraphStyle : JSON.parse(JSON.stringify(paragraphStyle));
9
+ }
10
+ function normalizePlainTextDataStream(dataStream) {
11
+ return dataStream.replace(/\r\n/g, "\r").replace(/\n/g, "\r");
12
+ }
13
+ function getRemovedLeadingParagraphBreakLength(dataStream, removeLeadingParagraphBreak) {
14
+ const normalized = normalizePlainTextDataStream(dataStream);
15
+ if (removeLeadingParagraphBreak && normalized.length > 1 && normalized.startsWith("\r")) return 1;
16
+ return 0;
17
+ }
18
+ function getNormalizedPlainTextCursorOffset(dataStream, cursorOffset, removeLeadingParagraphBreak) {
19
+ const normalizedPrefixLength = normalizePlainTextDataStream(dataStream.slice(0, cursorOffset)).length;
20
+ return Math.max(0, normalizedPrefixLength - getRemovedLeadingParagraphBreakLength(dataStream, removeLeadingParagraphBreak));
21
+ }
22
+ function getParagraphStyleAtOffset(body, offset) {
23
+ var _body$paragraphs, _paragraphs$find;
24
+ const paragraphs = (_body$paragraphs = body.paragraphs) !== null && _body$paragraphs !== void 0 ? _body$paragraphs : [];
25
+ const paragraph = (_paragraphs$find = paragraphs.find((item) => item.startIndex >= offset)) !== null && _paragraphs$find !== void 0 ? _paragraphs$find : paragraphs[paragraphs.length - 1];
26
+ return paragraph === null || paragraph === void 0 ? void 0 : paragraph.paragraphStyle;
27
+ }
28
+ function buildPlainTextInsertBody(dataStream, options = {}) {
29
+ const normalizedDataStream = normalizePlainTextDataStream(dataStream).slice(getRemovedLeadingParagraphBreakLength(dataStream, options.removeLeadingParagraphBreak));
30
+ const body = {
31
+ dataStream: normalizedDataStream,
32
+ customDecorations: [],
33
+ customRanges: [],
34
+ textRuns: []
35
+ };
36
+ const paragraphs = [];
37
+ for (let index = 0; index < normalizedDataStream.length; index++) if (normalizedDataStream[index] === "\r") paragraphs.push({
38
+ startIndex: index,
39
+ ...options.paragraphStyle == null ? {} : { paragraphStyle: cloneParagraphStyle(options.paragraphStyle) }
40
+ });
41
+ if (paragraphs.length > 0) body.paragraphs = paragraphs;
42
+ return body;
43
+ }
44
+
45
+ //#endregion
46
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/typeof.js
47
+ function _typeof(o) {
48
+ "@babel/helpers - typeof";
49
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
50
+ return typeof o;
51
+ } : function(o) {
52
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
53
+ }, _typeof(o);
54
+ }
55
+
56
+ //#endregion
57
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/toPrimitive.js
58
+ function toPrimitive(t, r) {
59
+ if ("object" != _typeof(t) || !t) return t;
60
+ var e = t[Symbol.toPrimitive];
61
+ if (void 0 !== e) {
62
+ var i = e.call(t, r || "default");
63
+ if ("object" != _typeof(i)) return i;
64
+ throw new TypeError("@@toPrimitive must return a primitive value.");
65
+ }
66
+ return ("string" === r ? String : Number)(t);
67
+ }
68
+
69
+ //#endregion
70
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/toPropertyKey.js
71
+ function toPropertyKey(t) {
72
+ var i = toPrimitive(t, "string");
73
+ return "symbol" == _typeof(i) ? i : i + "";
74
+ }
75
+
76
+ //#endregion
77
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/defineProperty.js
78
+ function _defineProperty(e, r, t) {
79
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
80
+ value: t,
81
+ enumerable: !0,
82
+ configurable: !0,
83
+ writable: !0
84
+ }) : e[r] = t, e;
85
+ }
86
+
87
+ //#endregion
88
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/decorateParam.js
89
+ function __decorateParam(paramIndex, decorator) {
90
+ return function(target, key) {
91
+ decorator(target, key, paramIndex);
92
+ };
93
+ }
94
+
95
+ //#endregion
96
+ //#region \0@oxc-project+runtime@0.134.0/helpers/esm/decorate.js
97
+ function __decorate(decorators, target, key, desc) {
98
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
99
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
100
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
101
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
102
+ }
103
+
104
+ //#endregion
105
+ //#region src/facade/f-document.ts
106
+ let FDocument = class FDocument extends _univerjs_core_facade.FBaseInitialable {
107
+ constructor(_documentDataModel, _injector, _univerInstanceService, _resourceLoaderService, _commandService) {
108
+ super(_injector);
109
+ this._documentDataModel = _documentDataModel;
110
+ this._injector = _injector;
111
+ this._univerInstanceService = _univerInstanceService;
112
+ this._resourceLoaderService = _resourceLoaderService;
113
+ this._commandService = _commandService;
114
+ _defineProperty(this, "id", void 0);
115
+ this.id = this._documentDataModel.getUnitId();
116
+ }
117
+ /**
118
+ * Get the document data model of the document.
119
+ * @returns {DocumentDataModel} The document data model.
120
+ * @example
121
+ * ```typescript
122
+ * const fDocument = univerAPI.getActiveDocument();
123
+ * const documentDataModel = fDocument.getDocumentDataModel();
124
+ * console.log(documentDataModel);
125
+ * ```
126
+ */
127
+ getDocumentDataModel() {
128
+ return this._documentDataModel;
129
+ }
130
+ dispose() {
131
+ super.dispose();
132
+ }
133
+ /**
134
+ * Get the document id.
135
+ * @returns {string} The document id.
136
+ * @example
137
+ * ```typescript
138
+ * const fDocument = univerAPI.getActiveDocument();
139
+ * const unitId = fDocument.getId();
140
+ * console.log(unitId);
141
+ * ```
142
+ */
143
+ getId() {
144
+ return this.id;
145
+ }
146
+ /**
147
+ * Get the document name.
148
+ * @returns {string} The document name.
149
+ * @example
150
+ * ```typescript
151
+ * const fDocument = univerAPI.getActiveDocument();
152
+ * const name = fDocument.getName();
153
+ * console.log(name);
154
+ * ```
155
+ */
156
+ getName() {
157
+ return this._documentDataModel.getTitle() || "";
158
+ }
159
+ /**
160
+ * Save the document snapshot data, including the document content and resource data, etc.
161
+ * @returns {IDocumentData} The document snapshot data.
162
+ * @example
163
+ * ```typescript
164
+ * const fDocument = univerAPI.getActiveDocument();
165
+ * const snapshot = fDocument.save();
166
+ * console.log(snapshot);
167
+ * ```
168
+ */
169
+ save() {
170
+ return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId());
171
+ }
172
+ /**
173
+ * Undo the last operation in the document.
174
+ * @returns {Promise<boolean>} A promise that resolves to true if the undo operation was successful, or false if it failed.
175
+ * @example
176
+ * ```typescript
177
+ * const fDocument = univerAPI.getActiveDocument();
178
+ * await fDocument.undo();
179
+ * ```
180
+ */
181
+ undo() {
182
+ this._univerInstanceService.focusUnit(this.id);
183
+ return this._commandService.executeCommand(_univerjs_core.UndoCommand.id);
184
+ }
185
+ /**
186
+ * Redo the last undone operation in the document.
187
+ * @returns {Promise<boolean>} A promise that resolves to true if the redo operation was successful, or false if it failed.
188
+ * @example
189
+ * ```typescript
190
+ * const fDocument = univerAPI.getActiveDocument();
191
+ * await fDocument.redo();
192
+ * ```
193
+ */
194
+ redo() {
195
+ this._univerInstanceService.focusUnit(this.id);
196
+ return this._commandService.executeCommand(_univerjs_core.RedoCommand.id);
197
+ }
198
+ /**
199
+ * Adds the specified text to the end of this text region.
200
+ * @param {string} text - The text to be added to the end of this text region.
201
+ * @return {Promise<boolean>} A promise that resolves to true if the text was successfully appended, or false if it failed.
202
+ * @example
203
+ * ```typescript
204
+ * const fDocument = univerAPI.getActiveDocument();
205
+ * await fDocument.appendText('Hello, world!');
206
+ * ```
207
+ */
208
+ appendText(text) {
209
+ const { body } = this.save();
210
+ if (!body) throw new Error("The document body is empty");
211
+ const lastPosition = body.dataStream.length - 2;
212
+ return this.insertText(text, {
213
+ startOffset: lastPosition,
214
+ endOffset: lastPosition,
215
+ segmentId: ""
216
+ });
217
+ }
218
+ /**
219
+ * Inserts text at the provided document range. Defaults to appending before the final section break.
220
+ * @param {string} text - The text to insert.
221
+ * @param {IDocumentInsertTextFacadeOptions} options - Optional target range, segment id, and cursor offset.
222
+ * @returns {Promise<boolean>} A promise that resolves to true if the text was successfully inserted, or false if it failed.
223
+ * @example
224
+ *
225
+ * // Insert text at a specific range in the document body
226
+ * ```typescript
227
+ * const fDocument = univerAPI.getActiveDocument();
228
+ * await fDocument.insertText('Hello, world!', {
229
+ * startOffset: 5,
230
+ * endOffset: 5,
231
+ * segmentId: '',
232
+ * cursorOffset: 13,
233
+ * });
234
+ * ```
235
+ *
236
+ * // Insert text at the beginning of a header or footer segment
237
+ * ```typescript
238
+ * const fDocument = univerAPI.getActiveDocument();
239
+ * const snapshot = fDocument.save();
240
+ * const { headers, footers } = snapshot;
241
+ *
242
+ * if (headers) {
243
+ * for (const headerId in headers) {
244
+ * if (headerId === 'target-header-id') {
245
+ * await fDocument.insertText('Hello, header!', {
246
+ * startOffset: 0,
247
+ * endOffset: 0,
248
+ * segmentId: headerId,
249
+ * });
250
+ * }
251
+ * }
252
+ * }
253
+ *
254
+ * if (footers) {
255
+ * for (const footerId in footers) {
256
+ * if (footerId === 'target-footer-id') {
257
+ * await fDocument.insertText('Hello, footer!', {
258
+ * startOffset: 0,
259
+ * endOffset: 0,
260
+ * segmentId: footerId,
261
+ * });
262
+ * }
263
+ * }
264
+ * }
265
+ * ```
266
+ */
267
+ insertText(text, options = {}) {
268
+ var _options$startOffset, _options$endOffset, _options$segmentId;
269
+ const unitId = this.id;
270
+ const { body } = this.save();
271
+ if (!body) throw new Error("The document body is empty");
272
+ const startOffset = (_options$startOffset = options.startOffset) !== null && _options$startOffset !== void 0 ? _options$startOffset : Math.max(0, body.dataStream.length - 2);
273
+ const endOffset = (_options$endOffset = options.endOffset) !== null && _options$endOffset !== void 0 ? _options$endOffset : startOffset;
274
+ const segmentId = (_options$segmentId = options.segmentId) !== null && _options$segmentId !== void 0 ? _options$segmentId : "";
275
+ const activeRange = {
276
+ startOffset,
277
+ endOffset,
278
+ collapsed: startOffset === endOffset,
279
+ segmentId
280
+ };
281
+ const removeLeadingParagraphBreak = startOffset === 0;
282
+ const insertBody = buildPlainTextInsertBody(text, {
283
+ paragraphStyle: getParagraphStyleAtOffset(body, startOffset),
284
+ removeLeadingParagraphBreak
285
+ });
286
+ const cursorOffset = options.cursorOffset == null ? void 0 : getNormalizedPlainTextCursorOffset(text, options.cursorOffset, removeLeadingParagraphBreak);
287
+ return this._commandService.executeCommand(_univerjs_docs.InsertTextCommand.id, {
288
+ unitId,
289
+ body: insertBody,
290
+ range: activeRange,
291
+ segmentId,
292
+ ...cursorOffset == null ? {} : { cursorOffset }
293
+ });
294
+ }
295
+ /**
296
+ * Inserts one or more plain-text paragraphs at the provided document range.
297
+ * @param {string} text - The paragraph text to insert. Newlines are normalized to document paragraph separators.
298
+ * @param {IDocumentInsertTextFacadeOptions} options - Optional target range, segment id, and cursor offset.
299
+ * @returns {Promise<boolean>} A promise that resolves to true if the paragraphs were successfully inserted, or false if it failed.
300
+ * @example
301
+ * ```typescript
302
+ * const fDocument = univerAPI.getActiveDocument();
303
+ * await fDocument.insertParagraph('Hello, world! This is a new paragraph.', {
304
+ * startOffset: 5,
305
+ * endOffset: 5,
306
+ * });
307
+ * ```
308
+ */
309
+ insertParagraph(text = "", options = {}) {
310
+ var _options$cursorOffset;
311
+ const dataStream = `${text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n").join("\r\n")}\r\n`;
312
+ return this.insertText(dataStream, {
313
+ ...options,
314
+ cursorOffset: (_options$cursorOffset = options.cursorOffset) !== null && _options$cursorOffset !== void 0 ? _options$cursorOffset : dataStream.length
315
+ });
316
+ }
317
+ };
318
+ FDocument = __decorate([
319
+ __decorateParam(1, (0, _univerjs_core.Inject)(_univerjs_core.Injector)),
320
+ __decorateParam(2, _univerjs_core.IUniverInstanceService),
321
+ __decorateParam(3, (0, _univerjs_core.Inject)(_univerjs_core.IResourceLoaderService)),
322
+ __decorateParam(4, _univerjs_core.ICommandService)
323
+ ], FDocument);
324
+
325
+ //#endregion
326
+ //#region src/facade/f-univer.ts
327
+ var FUniverDocsMixin = class extends _univerjs_core_facade.FUniver {
328
+ createDocument(data) {
329
+ const document = this._injector.get(_univerjs_core.IUniverInstanceService).createUnit(_univerjs_core.UniverInstanceType.UNIVER_DOC, data);
330
+ return this._injector.createInstance(FDocument, document);
331
+ }
332
+ getActiveDocument() {
333
+ const document = this._univerInstanceService.getCurrentUnitOfType(_univerjs_core.UniverInstanceType.UNIVER_DOC);
334
+ if (!document) return null;
335
+ return this._injector.createInstance(FDocument, document);
336
+ }
337
+ getDocument(id) {
338
+ const document = this._univerInstanceService.getUnit(id, _univerjs_core.UniverInstanceType.UNIVER_DOC);
339
+ if (!document) return null;
340
+ return this._injector.createInstance(FDocument, document);
341
+ }
342
+ };
343
+ _univerjs_core_facade.FUniver.extend(FUniverDocsMixin);
344
+
345
+ //#endregion
346
+ Object.defineProperty(exports, 'FDocument', {
347
+ enumerable: true,
348
+ get: function () {
349
+ return FDocument;
350
+ }
351
+ });