@univerjs/docs 1.0.0-alpha.6 → 1.0.0-alpha.8
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/lib/cjs/facade.js +486 -169
- package/lib/cjs/index.js +363 -48
- package/lib/es/facade.js +476 -169
- package/lib/es/index.js +359 -50
- package/lib/facade.js +476 -169
- package/lib/index.js +359 -50
- package/lib/types/{facade/f-types.d.ts → commands/commands/update-document-paragraph-style.command.d.ts} +10 -1
- package/lib/types/commands/commands/update-document-section.command.d.ts +7 -0
- package/lib/types/facade/f-document-paragraph.d.ts +115 -20
- package/lib/types/facade/f-document-section.d.ts +131 -28
- package/lib/types/facade/f-document-text-range.d.ts +7 -14
- package/lib/types/facade/f-document.d.ts +130 -20
- package/lib/types/facade/f-enum.d.ts +4 -1
- package/lib/types/facade/index.d.ts +2 -3
- package/lib/types/index.d.ts +8 -4
- package/lib/types/services/doc-selection-manager.service.d.ts +5 -11
- package/lib/types/services/doc-text-resolver.service.d.ts +53 -0
- package/lib/types/utils/section-columns.d.ts +23 -1
- package/lib/umd/facade.js +2 -2
- package/lib/umd/index.js +2 -2
- package/package.json +4 -4
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import type { DocumentDataModel, IDocumentBody, IDocumentData, IParagraphBorder, ISectionBreak } from '@univerjs/core';
|
|
16
|
+
import type { DocumentDataModel, IDocumentBody, IDocumentData, IParagraphBorder, ISectionBreak, SectionType } from '@univerjs/core';
|
|
17
17
|
import type { IHeaderFooterProps } from '@univerjs/docs';
|
|
18
18
|
import type { IFDocumentTextRange } from './utils';
|
|
19
|
-
import { ICommandService, Injector, IResourceLoaderService, IUniverInstanceService } from '@univerjs/core';
|
|
19
|
+
import { DocumentFlavor, ICommandService, Injector, IResourceLoaderService, IUniverInstanceService } from '@univerjs/core';
|
|
20
20
|
import { FBaseInitialable } from '@univerjs/core/facade';
|
|
21
21
|
import { FDocumentParagraph } from './f-document-paragraph';
|
|
22
22
|
import { FDocumentSection } from './f-document-section';
|
|
@@ -26,6 +26,21 @@ export interface IFDocumentParagraphQuery {
|
|
|
26
26
|
paragraphId?: string;
|
|
27
27
|
segmentId?: string;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Options for inserting a section break in a traditional document.
|
|
31
|
+
*
|
|
32
|
+
* Section properties such as margins and page size describe the section created
|
|
33
|
+
* before the inserted break. `nextSectionType` controls how the existing section
|
|
34
|
+
* after the break begins relative to that newly created section.
|
|
35
|
+
*/
|
|
36
|
+
export type IFDocumentInsertSectionBreakOptions = Partial<Omit<ISectionBreak, 'sectionId' | 'startIndex'>> & {
|
|
37
|
+
/**
|
|
38
|
+
* How the existing section after the inserted boundary begins relative to
|
|
39
|
+
* the newly created section. Prefer this atomic option to inserting a break
|
|
40
|
+
* and then resolving and updating the following section separately.
|
|
41
|
+
*/
|
|
42
|
+
nextSectionType?: SectionType;
|
|
43
|
+
};
|
|
29
44
|
/**
|
|
30
45
|
* Facade API object bounded to a document. It provides a set of methods to interact with the document.
|
|
31
46
|
* @hideconstructor
|
|
@@ -90,15 +105,65 @@ export declare class FDocument extends FBaseInitialable {
|
|
|
90
105
|
*/
|
|
91
106
|
getName(): string;
|
|
92
107
|
/**
|
|
93
|
-
*
|
|
94
|
-
*
|
|
108
|
+
* Returns the document's explicit layout flavor.
|
|
109
|
+
*
|
|
110
|
+
* Use this method when all three states matter. Do not infer a Traditional
|
|
111
|
+
* document from `!isModern()`: that expression is also true for
|
|
112
|
+
* `DocumentFlavor.UNSPECIFIED`.
|
|
113
|
+
*
|
|
114
|
+
* @returns {DocumentFlavor} `TRADITIONAL`, `MODERN`, or `UNSPECIFIED`.
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const document = univerAPI.getActiveDocument();
|
|
118
|
+
* if (!document) {
|
|
119
|
+
* throw new Error('No active document');
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* switch (document.getDocumentFlavor()) {
|
|
123
|
+
* case univerAPI.Enum.DocumentFlavor.TRADITIONAL:
|
|
124
|
+
* console.log('Word-compatible physical pagination is available');
|
|
125
|
+
* break;
|
|
126
|
+
* case univerAPI.Enum.DocumentFlavor.MODERN:
|
|
127
|
+
* console.log('Use Modern Doc layout APIs such as ColumnGroup');
|
|
128
|
+
* break;
|
|
129
|
+
* default:
|
|
130
|
+
* console.log('Resolve the unspecified flavor before using flavor-specific APIs');
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
getDocumentFlavor(): DocumentFlavor;
|
|
135
|
+
/**
|
|
136
|
+
* Whether this is a Traditional document with Word-compatible physical pagination.
|
|
137
|
+
*
|
|
138
|
+
* Prefer this positive guard before calling section, column-break, page-setup,
|
|
139
|
+
* or paragraph-pagination APIs.
|
|
140
|
+
*
|
|
141
|
+
* @returns {boolean} `true` only for `DocumentFlavor.TRADITIONAL`.
|
|
142
|
+
* @example
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const document = univerAPI.getActiveDocument();
|
|
145
|
+
* if (document?.isTraditional()) {
|
|
146
|
+
* console.log(document.getSection(0)?.getEffectivePageSetup());
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
isTraditional(): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Whether this is a Modern document.
|
|
153
|
+
*
|
|
154
|
+
* A `false` result can mean either Traditional or Unspecified. Use
|
|
155
|
+
* `isTraditional()` before Traditional-only APIs, or `getDocumentFlavor()`
|
|
156
|
+
* when all three states matter.
|
|
157
|
+
*
|
|
158
|
+
* @returns {boolean} `true` only for `DocumentFlavor.MODERN`.
|
|
95
159
|
* @example
|
|
96
160
|
* ```typescript
|
|
97
161
|
* const fDocument = univerAPI.getActiveDocument();
|
|
98
|
-
* console.log(fDocument
|
|
162
|
+
* console.log(fDocument?.isModern());
|
|
99
163
|
* ```
|
|
100
164
|
*/
|
|
101
165
|
isModern(): boolean;
|
|
166
|
+
private _resolveDocumentFlavor;
|
|
102
167
|
/**
|
|
103
168
|
* Save the document snapshot data, including the document content and resource data, etc.
|
|
104
169
|
* @returns {IDocumentData} The document snapshot data.
|
|
@@ -173,7 +238,7 @@ export declare class FDocument extends FBaseInitialable {
|
|
|
173
238
|
*/
|
|
174
239
|
insertText(index: number, text: string, segmentId?: string): boolean;
|
|
175
240
|
/**
|
|
176
|
-
* Returns document-level header/footer switches and margins. Margin values
|
|
241
|
+
* Returns document-level header/footer switches and margins. Margin values use 96-DPI layout pixels.
|
|
177
242
|
* @example
|
|
178
243
|
* ```ts
|
|
179
244
|
* const fDocument = univerAPI.getActiveDocument();
|
|
@@ -182,12 +247,18 @@ export declare class FDocument extends FBaseInitialable {
|
|
|
182
247
|
*/
|
|
183
248
|
getHeaderFooterOptions(): IHeaderFooterProps;
|
|
184
249
|
/**
|
|
185
|
-
* Updates document-level header/footer switches and margins
|
|
186
|
-
*
|
|
250
|
+
* Updates document-level header/footer switches and margins.
|
|
251
|
+
*
|
|
252
|
+
* Traditional and Unspecified documents keep the legacy header/footer
|
|
253
|
+
* behavior. Modern documents reject this API. `marginHeader` and
|
|
254
|
+
* `marginFooter` use 96-DPI layout pixels.
|
|
187
255
|
* @example
|
|
188
256
|
* ```ts
|
|
189
257
|
* const fDocument = univerAPI.getActiveDocument();
|
|
190
|
-
* if (
|
|
258
|
+
* if (
|
|
259
|
+
* fDocument &&
|
|
260
|
+
* fDocument.getDocumentFlavor() !== univerAPI.Enum.DocumentFlavor.MODERN
|
|
261
|
+
* ) {
|
|
191
262
|
* fDocument.setHeaderFooterOptions({ marginHeader: 36, marginFooter: 36 });
|
|
192
263
|
* }
|
|
193
264
|
* ```
|
|
@@ -243,27 +314,66 @@ export declare class FDocument extends FBaseInitialable {
|
|
|
243
314
|
getSectionAt(offset: number): FDocumentSection | null;
|
|
244
315
|
/**
|
|
245
316
|
* Inserts a traditional document section break and returns its stable facade.
|
|
246
|
-
*
|
|
247
|
-
*
|
|
317
|
+
*
|
|
318
|
+
* `options` configures the section created before the inserted break.
|
|
319
|
+
* Set `options.nextSectionType` to control how the existing section after the
|
|
320
|
+
* break begins. For example, use `SectionType.NEXT_PAGE` to start a chapter on
|
|
321
|
+
* a new physical page. Both changes are executed by one command and are
|
|
322
|
+
* undone or redone together.
|
|
323
|
+
*
|
|
324
|
+
* The offset must be a top-level document position. To insert a break before
|
|
325
|
+
* a table or block such as a callout, use that object's start offset instead
|
|
326
|
+
* of an offset inside the object.
|
|
327
|
+
*
|
|
328
|
+
* Modern documents must use ColumnGroup. Unspecified documents must resolve
|
|
329
|
+
* their flavor first. Both throw `DocsSectionUnsupportedDocumentFlavorError`.
|
|
330
|
+
* Numeric layout values in `options` are in 96-DPI layout pixels.
|
|
331
|
+
*
|
|
332
|
+
* @param {number} offset Top-level data-stream offset where the section break is inserted.
|
|
333
|
+
* @param {IFDocumentInsertSectionBreakOptions} [options] Section properties and the optional type of the following section.
|
|
334
|
+
* @returns {FDocumentSection | null} The section created before the break, or `null` when the command rejects the insertion.
|
|
248
335
|
* @example
|
|
249
336
|
* ```ts
|
|
250
|
-
* const
|
|
251
|
-
* if (
|
|
252
|
-
*
|
|
253
|
-
* const offset = paragraph?.getInfo().startOffset;
|
|
254
|
-
* const section = offset == null ? null : fDocument.insertSectionBreak(offset);
|
|
255
|
-
* console.log(section?.getId());
|
|
337
|
+
* const document = univerAPI.getActiveDocument();
|
|
338
|
+
* if (!document) {
|
|
339
|
+
* throw new Error('No active document');
|
|
256
340
|
* }
|
|
341
|
+
* if (!document.isTraditional()) {
|
|
342
|
+
* throw new Error('Traditional document sections are required');
|
|
343
|
+
* }
|
|
344
|
+
*
|
|
345
|
+
* const chapter = document.findParagraphByText('Chapter 2');
|
|
346
|
+
* if (!chapter) {
|
|
347
|
+
* throw new Error('Chapter heading not found');
|
|
348
|
+
* }
|
|
349
|
+
*
|
|
350
|
+
* // Insert the boundary immediately before the chapter heading. The command
|
|
351
|
+
* // also marks the following section as NEXT_PAGE, so the two model changes
|
|
352
|
+
* // share one undo/redo step.
|
|
353
|
+
* const sectionBeforeChapter = document.insertSectionBreak(
|
|
354
|
+
* chapter.getInfo().startOffset,
|
|
355
|
+
* { nextSectionType: univerAPI.Enum.SectionType.NEXT_PAGE }
|
|
356
|
+
* );
|
|
357
|
+
* if (!sectionBeforeChapter) {
|
|
358
|
+
* throw new Error('The chapter heading is not at a valid top-level offset');
|
|
359
|
+
* }
|
|
360
|
+
*
|
|
361
|
+
* console.log({
|
|
362
|
+
* insertedSection: sectionBeforeChapter.describe(),
|
|
363
|
+
* chapterSection: document.getSectionAt(chapter.getInfo().startOffset)?.describe(),
|
|
364
|
+
* });
|
|
257
365
|
* ```
|
|
258
366
|
*/
|
|
259
|
-
insertSectionBreak(offset: number,
|
|
367
|
+
insertSectionBreak(offset: number, options?: IFDocumentInsertSectionBreakOptions): FDocumentSection | null;
|
|
260
368
|
/**
|
|
261
369
|
* Inserts a column-break token in a traditional document.
|
|
262
|
-
*
|
|
370
|
+
* In a single-column section, the traditional renderer advances to the next physical page.
|
|
371
|
+
* Modern documents must use ColumnGroup. Unspecified documents must resolve
|
|
372
|
+
* their flavor first. Both throw `DocsSectionUnsupportedDocumentFlavorError`.
|
|
263
373
|
* @example
|
|
264
374
|
* ```ts
|
|
265
375
|
* const fDocument = univerAPI.getActiveDocument();
|
|
266
|
-
* if (fDocument
|
|
376
|
+
* if (fDocument?.isTraditional()) {
|
|
267
377
|
* const paragraph = fDocument.findParagraphByText('Continue in next column');
|
|
268
378
|
* const offset = paragraph?.getInfo().startOffset;
|
|
269
379
|
* if (offset != null) {
|
|
@@ -13,16 +13,19 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { ColumnSeparatorType, SectionType } from '@univerjs/core';
|
|
16
|
+
import { ColumnSeparatorType, DocumentFlavor, SectionType } from '@univerjs/core';
|
|
17
17
|
import { FEnum } from '@univerjs/core/facade';
|
|
18
18
|
/** @ignore */
|
|
19
19
|
export interface IFDocsEnumMixin {
|
|
20
|
+
/** Document layout flavors. */
|
|
21
|
+
DocumentFlavor: typeof DocumentFlavor;
|
|
20
22
|
/** OOXML-compatible section start types. */
|
|
21
23
|
SectionType: typeof SectionType;
|
|
22
24
|
/** Section column separator types. */
|
|
23
25
|
ColumnSeparatorType: typeof ColumnSeparatorType;
|
|
24
26
|
}
|
|
25
27
|
export declare class FDocsEnumMixin extends FEnum implements IFDocsEnumMixin {
|
|
28
|
+
get DocumentFlavor(): typeof DocumentFlavor;
|
|
26
29
|
get SectionType(): typeof SectionType;
|
|
27
30
|
get ColumnSeparatorType(): typeof ColumnSeparatorType;
|
|
28
31
|
}
|
|
@@ -17,12 +17,11 @@ import './f-univer';
|
|
|
17
17
|
import './f-enum';
|
|
18
18
|
export { FDocument } from './f-document';
|
|
19
19
|
export { FDocumentParagraph, isParagraphFacade } from './f-document-paragraph';
|
|
20
|
-
export type { IFDocumentParagraphInfo } from './f-document-paragraph';
|
|
20
|
+
export type { IFDocumentFindTextOptions, IFDocumentParagraphInfo } from './f-document-paragraph';
|
|
21
21
|
export { DocsSectionUnsupportedDocumentFlavorError, FDocumentSection } from './f-document-section';
|
|
22
|
-
export type { IFDocumentSectionColumnOptions, IFDocumentSectionDescription } from './f-document-section';
|
|
22
|
+
export type { FDocumentSectionPageSetup, IFDocumentSectionColumnOptions, IFDocumentSectionDescription } from './f-document-section';
|
|
23
23
|
export { FDocumentTextRange } from './f-document-text-range';
|
|
24
24
|
export type { IFDocumentTextRangeDescription, IFDocumentTextStyleRun } from './f-document-text-range';
|
|
25
25
|
export * from './f-enum';
|
|
26
|
-
export type { FDocEmbedUnitFacadeMapAugmentation } from './f-types';
|
|
27
26
|
export type { IFDocumentTextRange } from './utils';
|
|
28
27
|
export { stripBlockTokens } from './utils';
|
package/lib/types/index.d.ts
CHANGED
|
@@ -21,8 +21,9 @@ export { SetDocumentDefaultParagraphStyleCommand } from './commands/commands/set
|
|
|
21
21
|
export type { IDocumentDefaultParagraphStylePatch, ISetDocumentDefaultParagraphStyleCommandParams, } from './commands/commands/set-document-default-paragraph-style.command';
|
|
22
22
|
export { SetSectionHeaderFooterLinkCommand } from './commands/commands/set-section-header-footer-link.command';
|
|
23
23
|
export type { ISetSectionHeaderFooterLinkCommandParams } from './commands/commands/set-section-header-footer-link.command';
|
|
24
|
-
export {
|
|
25
|
-
export
|
|
24
|
+
export { UpdateDocumentParagraphStyleCommand } from './commands/commands/update-document-paragraph-style.command';
|
|
25
|
+
export { DeleteDocumentSectionBreakCommand, InsertDocumentColumnBreakCommand, InsertDocumentSectionBreakCommand, UpdateDocumentSectionCommand } from './commands/commands/update-document-section.command';
|
|
26
|
+
export type { IDeleteDocumentSectionBreakCommandParams, IDocumentSectionConfig, IDocumentSectionUpdate, IInsertDocumentColumnBreakCommandParams, IInsertDocumentSectionBreakCommandParams, IUpdateDocumentSectionCommandParams } from './commands/commands/update-document-section.command';
|
|
26
27
|
export { RichTextEditingMutation } from './commands/mutations/core-editing.mutation';
|
|
27
28
|
export type { IRichTextEditingMutationParams } from './commands/mutations/core-editing.mutation';
|
|
28
29
|
export { SetTextSelectionsOperation } from './commands/operations/text-selection.operation';
|
|
@@ -37,15 +38,18 @@ export { DocContentInsertService } from './services/doc-content-insert.service';
|
|
|
37
38
|
export type { IDocContentInsertRange } from './services/doc-content-insert.service';
|
|
38
39
|
export { DocInterceptorService } from './services/doc-interceptor/doc-interceptor.service';
|
|
39
40
|
export { DOC_INTERCEPTOR_POINT } from './services/doc-interceptor/interceptor-const';
|
|
40
|
-
export { DocSelectionManagerService } from './services/doc-selection-manager.service';
|
|
41
|
+
export { DOC_SELECTION_OPTION_PRESERVE_CARET, DocSelectionManagerService, } from './services/doc-selection-manager.service';
|
|
41
42
|
export { DocSkeletonManagerService } from './services/doc-skeleton-manager.service';
|
|
42
43
|
export { DocStateChangeManagerService, IDocStateChangeInterceptorService, } from './services/doc-state-change-manager.service';
|
|
43
44
|
export type { IDocStateChangeInfo, IDocStateChangeParams } from './services/doc-state-emit.service';
|
|
44
45
|
export { DocStateEmitService } from './services/doc-state-emit.service';
|
|
46
|
+
export { DocTextResolverService } from './services/doc-text-resolver.service';
|
|
47
|
+
export type { IDocTextReplacement, IDocTextResolver, IResolvedDocText, IResolvedDocTextCharacter, } from './services/doc-text-resolver.service';
|
|
45
48
|
export { addCustomRangeBySelectionFactory, addCustomRangeFactory, deleteCustomRangeFactory, } from './utils/custom-range-factory';
|
|
46
49
|
export { generateParagraphs } from './utils/paragraphs';
|
|
47
50
|
export { replaceSelectionFactory } from './utils/replace-selection-factory';
|
|
48
|
-
export { createSectionColumnProperties } from './utils/section-columns';
|
|
51
|
+
export { createSectionColumnProperties, getEffectiveSectionPageSetup, getSectionContentWidth } from './utils/section-columns';
|
|
52
|
+
export type { IEffectiveSectionPageSetup } from './utils/section-columns';
|
|
49
53
|
export { getTopLevelSectionBreaks } from './utils/sections';
|
|
50
54
|
export { buildDocTransform, docDrawingPositionToTransform, transformToDocDrawingPosition } from './utils/transform-position';
|
|
51
55
|
export { consumeContentInsertRange, getContentInsertRange, isHeaderFooterSelection, normalizeTextRange } from './utils/util';
|
|
@@ -20,6 +20,11 @@ interface IDocSelectionManagerSearchParam {
|
|
|
20
20
|
unitId: string;
|
|
21
21
|
subUnitId: string;
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Keeps a programmatically restored collapsed caret from being promoted back
|
|
25
|
+
* into a neighboring whole-entity selection by feature-specific UI.
|
|
26
|
+
*/
|
|
27
|
+
export declare const DOC_SELECTION_OPTION_PRESERVE_CARET = "preserveCaret";
|
|
23
28
|
export interface IRefreshSelectionParam extends IDocSelectionManagerSearchParam {
|
|
24
29
|
docRanges: ISuccinctDocRangeParam[];
|
|
25
30
|
isEditing: boolean;
|
|
@@ -51,18 +56,7 @@ export declare class DocSelectionManagerService extends RxDisposable {
|
|
|
51
56
|
getRectRanges(params?: Nullable<IDocSelectionManagerSearchParam>): Readonly<Nullable<IRectRangeWithStyle[]>>;
|
|
52
57
|
getDocRanges(params?: Nullable<IDocSelectionManagerSearchParam>): ITextRangeWithStyle[];
|
|
53
58
|
getActiveTextRange(): Nullable<ITextRangeWithStyle>;
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
56
|
-
* @deprecated
|
|
57
|
-
*/
|
|
58
|
-
getActiveRectRange(): Nullable<ITextRangeWithStyle>;
|
|
59
59
|
__TEST_ONLY_add(textRanges: ITextRangeWithStyle[], isEditing?: boolean): void;
|
|
60
|
-
/**
|
|
61
|
-
* @deprecated pls use replaceDocRanges.
|
|
62
|
-
*/
|
|
63
|
-
replaceTextRanges(docRanges: ISuccinctDocRangeParam[], isEditing?: boolean, options?: {
|
|
64
|
-
[key: string]: boolean;
|
|
65
|
-
}): void;
|
|
66
60
|
replaceDocRanges(docRanges: ISuccinctDocRangeParam[], params?: Nullable<IDocSelectionManagerSearchParam>, isEditing?: boolean, options?: {
|
|
67
61
|
[key: string]: boolean;
|
|
68
62
|
}): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import type { IDisposable, IDocumentBody } from '@univerjs/core';
|
|
17
|
+
import { Disposable } from '@univerjs/core';
|
|
18
|
+
export interface IDocTextReplacement {
|
|
19
|
+
endOffset: number;
|
|
20
|
+
replaceable?: boolean;
|
|
21
|
+
startOffset: number;
|
|
22
|
+
text: string;
|
|
23
|
+
}
|
|
24
|
+
export interface IDocTextResolver {
|
|
25
|
+
resolve(unitId: string, body: IDocumentBody): readonly IDocTextReplacement[];
|
|
26
|
+
}
|
|
27
|
+
export interface IResolvedDocTextCharacter {
|
|
28
|
+
endOffset: number;
|
|
29
|
+
replaceable: boolean;
|
|
30
|
+
startOffset: number;
|
|
31
|
+
}
|
|
32
|
+
export interface IResolvedDocText {
|
|
33
|
+
characters: readonly IResolvedDocTextCharacter[];
|
|
34
|
+
text: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Builds a consumer-facing text projection while retaining a mapping back to
|
|
38
|
+
* the native document offsets.
|
|
39
|
+
*
|
|
40
|
+
* Consumers such as find/replace can search the projected text and then use
|
|
41
|
+
* `characters` to focus the native object that supplied a matching character.
|
|
42
|
+
* Resolvers must return half-open, non-overlapping replacements.
|
|
43
|
+
*/
|
|
44
|
+
export declare class DocTextResolverService extends Disposable {
|
|
45
|
+
private readonly _resolvers;
|
|
46
|
+
private readonly _textChanged$;
|
|
47
|
+
readonly textChanged$: import("rxjs").Observable<string>;
|
|
48
|
+
register(resolver: IDocTextResolver): IDisposable;
|
|
49
|
+
notifyTextChanged(unitId: string): void;
|
|
50
|
+
resolve(unitId: string, body: IDocumentBody): IResolvedDocText;
|
|
51
|
+
private _collectReplacements;
|
|
52
|
+
dispose(): void;
|
|
53
|
+
}
|
|
@@ -13,6 +13,28 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import type { IDocumentStyle, ISectionBreak, ISectionColumnProperties } from '@univerjs/core';
|
|
16
|
+
import type { IDocumentStyle, ISectionBreak, ISectionColumnProperties, PageOrientType } from '@univerjs/core';
|
|
17
|
+
export interface IEffectiveSectionPageSetup {
|
|
18
|
+
pageSize: {
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
};
|
|
22
|
+
pageOrient: PageOrientType;
|
|
23
|
+
margins: {
|
|
24
|
+
top: number;
|
|
25
|
+
bottom: number;
|
|
26
|
+
left: number;
|
|
27
|
+
right: number;
|
|
28
|
+
};
|
|
29
|
+
contentSize: {
|
|
30
|
+
width: number;
|
|
31
|
+
height: number;
|
|
32
|
+
};
|
|
33
|
+
pageNumberStart?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Resolves nominal traditional page geometry without requiring a renderer. */
|
|
36
|
+
export declare function getEffectiveSectionPageSetup(documentStyle: IDocumentStyle | undefined, section: ISectionBreak | undefined): IEffectiveSectionPageSetup;
|
|
37
|
+
/** Returns the usable horizontal layout width for a traditional section. */
|
|
38
|
+
export declare function getSectionContentWidth(documentStyle: IDocumentStyle | undefined, section: ISectionBreak | undefined): number;
|
|
17
39
|
/** Creates explicit OOXML section columns from a count, gap, and optional widths. */
|
|
18
40
|
export declare function createSectionColumnProperties(documentStyle: IDocumentStyle | undefined, section: ISectionBreak | undefined, columnCount: number, gap: number, widths?: number[]): ISectionColumnProperties[];
|
package/lib/umd/facade.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/core/facade"),require("@univerjs/docs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/core/facade`,`@univerjs/docs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsFacade={},e.UniverCore,e.UniverCoreFacade,e.UniverDocs))})(this,function(e,t,n,r){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});function i(e){return e==null?e:JSON.parse(JSON.stringify(e))}function a(e){return e.replace(/\r\n/g,`\r`).replace(/\n/g,`\r`)}function o(e,t){let n=a(e);return t&&n.length>1&&n.startsWith(`\r`)?1:0}function s(e,n={}){let r=a(e).slice(o(e,n.removeLeadingParagraphBreak)),s={dataStream:r,customDecorations:[],customRanges:[],textRuns:[]},c=[],l=new Set;for(let e=0;e<r.length;e++)r[e]===`\r`&&c.push({startIndex:e,paragraphId:(0,t.createParagraphId)(l),...n.paragraphStyle==null?{}:{paragraphStyle:i(n.paragraphStyle)}});return c.length>0&&(s.paragraphs=c),s}function c(e,n,i,a){let{startOffset:o,endOffset:s,segmentId:c}=e,l=new t.TextX;o>0&&l.push({t:t.TextXActionType.RETAIN,len:o}),s>o&&l.push({t:t.TextXActionType.DELETE,len:s-o}),n.dataStream.length>0&&l.push({t:t.TextXActionType.INSERT,body:n,len:n.dataStream.length});let u=t.JSONX.getInstance().editOp(l.serialize(),(0,t.getRichTextEditPath)(i,c)),d=a.get(t.ICommandService).syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:i.getUnitId(),segmentId:c,actions:u,textRanges:[],isEditing:!1});return!!(d!=null&&d.actions&&d.actions.length>0)}function l(e,n,i,a,o){var s,c;let{startOffset:l,endOffset:u,segmentId:d}=e,f=o.get(t.ICommandService);if((s=n.textRuns)!=null&&s.length&&((c=a.getSelfOrHeaderFooterModel(d))==null||(c=c.getBody())==null?void 0:c.textRuns)==null){let e=t.JSONX.getInstance().replaceOp([...(0,t.getRichTextEditPath)(a,d),`textRuns`],void 0,[]);f.syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:a.getUnitId(),segmentId:d,actions:e,textRanges:[],isEditing:!1})}let p=new t.TextX;l>0&&p.push({t:t.TextXActionType.RETAIN,len:l}),p.push({t:t.TextXActionType.RETAIN,body:n,coverType:i,len:u-l});let m=t.JSONX.getInstance().editOp(p.serialize(),(0,t.getRichTextEditPath)(a,d)),h=f.syncExecuteCommand(r.RichTextEditingMutation.id,{unitId:a.getUnitId(),segmentId:d,actions:m,textRanges:[],isEditing:!1});return!!(h!=null&&h.actions&&h.actions.length>0)}function u(e){return Array.from(e).map(e=>e===t.DataStreamTreeTokenType.PARAGRAPH?`
|
|
2
|
-
`:e).filter(e=>e!==t.DataStreamTreeTokenType.BLOCK_START&&e!==t.DataStreamTreeTokenType.BLOCK_END&&e!==t.DataStreamTreeTokenType.SECTION_BREAK).join(``).replace(/\n$/,``)}var d=class{constructor(e,t,n,r,i){this._document=e,this._startOffset=t,this._endOffset=n,this._segmentId=r,this._injector=i,this._validateRange()}getRange(){return{startOffset:this._startOffset,endOffset:this._endOffset,segmentId:this._segmentId}}getText(){return this._document.getBody(this._segmentId).dataStream.slice(this._startOffset,this._endOffset)}getExplicitTextStyleRuns(){let{textRuns:e=[]}=this._document.getBody(this._segmentId);return e.filter(e=>e.st<this._endOffset&&e.ed>this._startOffset).map(e=>{var n;return{startOffset:Math.max(e.st,this._startOffset),endOffset:Math.min(e.ed,this._endOffset),textStyle:t.Tools.deepClone((n=e.ts)==null?{}:n)}})}getTextStyleRuns(){return this.getExplicitTextStyleRuns()}getCommonExplicitTextStyle(){if(this._startOffset===this._endOffset)return{};let[e,...n]=this._getStyleSegments(),r=t.Tools.deepClone(e.textStyle);for(let e of Object.keys(r))n.some(t=>!f(t.textStyle[e],r[e]))&&delete r[e];return r}getCommonTextStyle(){return this.getCommonExplicitTextStyle()}describe(){let e=this.getExplicitTextStyleRuns(),t=this.getCommonExplicitTextStyle();return{...this.getRange(),text:this.getText(),length:this._endOffset-this._startOffset,explicitTextStyleRuns:e,commonExplicitTextStyle:t,textStyleRuns:e,commonTextStyle:t}}setTextStyle(e){return this._startOffset===this._endOffset?!1:l(this.getRange(),{dataStream:``,textRuns:[{st:0,ed:this._endOffset-this._startOffset,ts:t.Tools.deepClone(e)}]},t.UpdateDocsAttributeType.COVER,this._document.getDocumentDataModel(),this._injector)}setText(e){return c(this.getRange(),s(e),this._document.getDocumentDataModel(),this._injector)}_validateRange(){let e=this._document.getBody(this._segmentId).dataStream.length;if(!Number.isInteger(this._startOffset)||!Number.isInteger(this._endOffset)||this._startOffset<0||this._endOffset<this._startOffset||this._endOffset>e)throw RangeError(`Invalid document text range [${this._startOffset}, ${this._endOffset}) for body length ${e}.`)}_getStyleSegments(){let e=this.getExplicitTextStyleRuns(),t=[],n=this._startOffset;for(let r of e)n<r.startOffset&&t.push({startOffset:n,endOffset:r.startOffset,textStyle:{}}),t.push(r),n=r.endOffset;return n<this._endOffset&&t.push({startOffset:n,endOffset:this._endOffset,textStyle:{}}),t}};function f(e,t){return JSON.stringify(e)===JSON.stringify(t)}var p=class{constructor(e,t,n=``,r){this._document=e,this._paragraphId=t,this._segmentId=n,this._injector=r}getId(){return this._paragraphId}getSegmentId(){return this._segmentId}getInfo(){let e=this._document.getBody(this._segmentId),{paragraphs:n=[]}=e,r=n.map((e,t)=>({paragraph:e,paragraphIndex:t})).filter(({paragraph:e})=>e.paragraphId===this._paragraphId);if(r.length===0)throw Error(`Document paragraph with id ${this._paragraphId} not found`);if(r.length>1)throw Error(`Multiple document paragraphs with id ${this._paragraphId} found`);let{paragraph:i,paragraphIndex:a}=r[0];return{paragraph:i,paragraphIndex:a,startOffset:(0,t.getParagraphContentStartOffset)(e,i),endOffset:i.startIndex}}getRange(){let{startOffset:e,endOffset:t}=this.getInfo();return{startOffset:e,endOffset:t,segmentId:this._segmentId}}getTextRange(){let{startOffset:e,endOffset:t}=this.getInfo();return this._injector.createInstance(d,this._document,e,t,this._segmentId,this._injector)}getText(){let{dataStream:e}=this._document.getBody(this._segmentId),{startOffset:t,endOffset:n}=this.getInfo();return e.slice(t,n)}setText(e){let{startOffset:t,endOffset:n}=this.getInfo();return c({startOffset:t,endOffset:n,segmentId:this._segmentId},s(e),this._document.getDocumentDataModel(),this._injector)}appendText(e){let{endOffset:t}=this.getInfo();return this._document.insertText(t,e,this._segmentId)}setStyle(e){let{paragraph:n,startOffset:r,endOffset:i}=this.getInfo(),a=!0;e.textStyle&&r<i&&(a=l({startOffset:r,endOffset:i,segmentId:this._segmentId},{dataStream:``,textRuns:[{st:0,ed:i-r,ts:e.textStyle}]},t.UpdateDocsAttributeType.COVER,this._document.getDocumentDataModel(),this._injector));let o={dataStream:``,paragraphs:[{...n,startIndex:0,paragraphStyle:{...n.paragraphStyle,...e}}]};return this._preserveExplicitParagraphIds(o),l({startOffset:i,endOffset:i+1,segmentId:this._segmentId},o,t.UpdateDocsAttributeType.REPLACE,this._document.getDocumentDataModel(),this._injector)&&a}isListItem(){let{paragraph:e}=this.getInfo();return!!e.bullet}isTask(){var e;let{paragraph:n}=this.getInfo(),r=(e=n.bullet)==null?void 0:e.listType;return r===t.PresetListType.CHECK_LIST||r===t.PresetListType.CHECK_LIST_CHECKED}setTaskChecked(e){if(!this.isTask())return!1;let{paragraph:n,endOffset:r}=this.getInfo(),i=n.bullet,a={dataStream:``,paragraphs:[{...n,startIndex:0,bullet:{...i,listType:e?t.PresetListType.CHECK_LIST_CHECKED:t.PresetListType.CHECK_LIST}}]};return this._preserveExplicitParagraphIds(a),l({startOffset:r,endOffset:r+1,segmentId:this._segmentId},a,t.UpdateDocsAttributeType.REPLACE,this._document.getDocumentDataModel(),this._injector)}remove(){let{startOffset:e,endOffset:t}=this.getInfo();return this._document.deleteRange({startOffset:e,endOffset:t+1,segmentId:this._segmentId})}_preserveExplicitParagraphIds(e){e[t.RESTORE_INSERTED_PARAGRAPH_IDS]=!0}};function m(e){return typeof e!=`object`||!e?!1:typeof e.getId==`function`&&typeof e.getSegmentId==`function`&&typeof e.getInfo==`function`&&typeof e.getRange==`function`}var h=class extends Error{constructor(){super(`Section column APIs are supported only in traditional documents. Use ColumnGroup APIs for modern documents.`),this.name=`DocsSectionUnsupportedDocumentFlavorError`}},g=class{constructor(e,t,n){this._document=e,this._sectionId=t,this._injector=n}getId(){return this._sectionId}getIndex(){return this._resolve().index}getConfig(){let{sectionBreak:e}=this._resolve();return t.Tools.deepClone(e)}getRange(){let e=(0,r.getTopLevelSectionBreaks)(this._document.getBody()),{index:t,sectionBreak:n}=this._resolve();return{startOffset:t===0?0:e[t-1].startIndex+1,endOffset:n.startIndex,segmentId:``}}getColumns(){var e;return t.Tools.deepClone((e=this.getConfig().columnProperties)==null?[]:e)}describe(){var e,n,r;let i=this.getConfig(),a=(e=i.columnProperties)==null?[]:e,o={defaultHeader:this._describeHeaderFooterReference(`header`,`default`),defaultFooter:this._describeHeaderFooterReference(`footer`,`default`),firstHeader:this._describeHeaderFooterReference(`header`,`first`),firstFooter:this._describeHeaderFooterReference(`footer`,`first`),evenHeader:this._describeHeaderFooterReference(`header`,`even`),evenFooter:this._describeHeaderFooterReference(`footer`,`even`)};return{sectionId:this._sectionId,index:this.getIndex(),range:this.getRange(),columnCount:a.length||1,columns:t.Tools.deepClone(a),columnSeparatorType:(n=i.columnSeparatorType)==null?t.ColumnSeparatorType.NONE:n,sectionType:(r=i.sectionType)==null?t.SectionType.SECTION_TYPE_UNSPECIFIED:r,headerFooter:o,config:i}}setColumns(e,n={}){var i,a;if(this._assertTraditionalDocument(),!Number.isInteger(e)||e<1)throw RangeError(`Section column count must be a positive integer.`);if(n.widths&&n.widths.length!==e)throw RangeError(`Section column widths must match the column count.`);let o=Math.max(0,(i=n.gap)==null?18:i),s=this.getConfig(),c=(0,r.createSectionColumnProperties)(this._document.getDocumentDataModel().getSnapshot().documentStyle,s,e,o,n.widths),l=typeof n.separator==`boolean`?n.separator?t.ColumnSeparatorType.BETWEEN_EACH_COLUMN:t.ColumnSeparatorType.NONE:(a=n.separator)==null?t.ColumnSeparatorType.NONE:a;return this._update({columnProperties:c,columnSeparatorType:l,...n.sectionType==null?{}:{sectionType:n.sectionType}})}setColumnProperties(e,n=t.ColumnSeparatorType.NONE){if(this._assertTraditionalDocument(),e.some(({width:e,paddingEnd:t})=>e<0||t<0))throw RangeError(`Section column widths and padding must be non-negative.`);return this._update({columnProperties:t.Tools.deepClone(e),columnSeparatorType:n})}setSectionType(e){return this._assertTraditionalDocument(),this._update({sectionType:e})}ensureHeader(e=`default`){return this._ensureHeaderFooter(`header`,e)}ensureFooter(e=`default`){return this._ensureHeaderFooter(`footer`,e)}getHeaderId(e=`default`){var t;return(t=this._getHeaderFooterReference(`header`,e).segmentId)==null?null:t}getFooterId(e=`default`){var t;return(t=this._getHeaderFooterReference(`footer`,e).segmentId)==null?null:t}isHeaderLinkedToPrevious(e=`default`){return this._getHeaderFooterReference(`header`,e).linkedToPrevious}isFooterLinkedToPrevious(e=`default`){return this._getHeaderFooterReference(`footer`,e).linkedToPrevious}setHeaderLinkedToPrevious(e,t=`default`){return this._setHeaderFooterLinkedToPrevious(`header`,t,e)}setFooterLinkedToPrevious(e,t=`default`){return this._setHeaderFooterLinkedToPrevious(`footer`,t,e)}setHeaderFooterOptions(e){return this._assertTraditionalDocument(),this._update(e)}remove(){return this._assertTraditionalDocument(),this._injector.get(t.ICommandService).syncExecuteCommand(r.DeleteDocumentSectionBreakCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId})}_update(e){let{sectionId:n,startIndex:i,...a}=e;return this._injector.get(t.ICommandService).syncExecuteCommand(r.UpdateDocumentSectionCommand.id,{unitId:this._document.getId(),updates:[{sectionId:this._sectionId,config:a}]})}_ensureHeaderFooter(e,n){this._assertTraditionalDocument();let{index:i}=this._resolve(),a=this.getConfig()[(0,t.getSectionHeaderFooterReferenceKey)(e,n)];if(typeof a==`string`&&a)return a;if(i>0){let i=(0,t.generateRandomId)(6);if(!this._injector.get(t.ICommandService).syncExecuteCommand(r.SetSectionHeaderFooterLinkCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId,kind:e,variant:n,linkedToPrevious:!1,segmentId:i}))throw Error(`Failed to create section ${e}.`);return i}let o={default:e===`header`?r.HeaderFooterType.DEFAULT_HEADER:r.HeaderFooterType.DEFAULT_FOOTER,first:e===`header`?r.HeaderFooterType.FIRST_PAGE_HEADER:r.HeaderFooterType.FIRST_PAGE_FOOTER,even:e===`header`?r.HeaderFooterType.EVEN_PAGE_HEADER:r.HeaderFooterType.EVEN_PAGE_FOOTER},s=(0,t.generateRandomId)(6);if(!this._injector.get(t.ICommandService).syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this._document.getId(),segmentId:s,createType:o[n],sectionId:this._sectionId}))throw Error(`Failed to create section ${e}.`);return s}_getHeaderFooterReference(e,n){let{index:i}=this._resolve();return(0,t.resolveSectionHeaderFooterReference)(this._document.getDocumentDataModel().getSnapshot().documentStyle,(0,r.getTopLevelSectionBreaks)(this._document.getBody()),i,(0,t.getSectionHeaderFooterReferenceKey)(e,n))}_describeHeaderFooterReference(e,t){var n;let r=this._getHeaderFooterReference(e,t);return{segmentId:(n=r.segmentId)==null?null:n,linkedToPrevious:r.linkedToPrevious}}_setHeaderFooterLinkedToPrevious(e,n,i){return this._assertTraditionalDocument(),this._injector.get(t.ICommandService).syncExecuteCommand(r.SetSectionHeaderFooterLinkCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId,kind:e,variant:n,linkedToPrevious:i,...i?{}:{segmentId:(0,t.generateRandomId)(6)}})}_assertTraditionalDocument(){if(this._document.getDocumentDataModel().getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new h}_resolve(){this._assertTraditionalDocument();let e=(0,r.getTopLevelSectionBreaks)(this._document.getBody()),t=e.findIndex(e=>e.sectionId===this._sectionId);if(t<0)throw Error(`Document section with id ${this._sectionId} not found.`);return{index:t,sectionBreak:e[t]}}};function _(e){"@babel/helpers - typeof";return _=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},_(e)}function v(e,t){if(_(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(_(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function y(e){var t=v(e,`string`);return _(t)==`symbol`?t:t+``}function b(e,t,n){return(t=y(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function x(e,t){return function(n,r){t(n,r,e)}}function S(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let C=class extends n.FBaseInitialable{constructor(e,t,n,r,i){super(t),this._documentDataModel=e,this._injector=t,this._univerInstanceService=n,this._resourceLoaderService=r,this._commandService=i,b(this,`id`,void 0),this.id=this._documentDataModel.getUnitId()}getDocumentDataModel(e=``){let t=this._documentDataModel.getSelfOrHeaderFooterModel(e);if(!t)throw Error(e===``?`Document data model is not found.`:`Document data model is not found in the segment: ${e}`);return t}getBody(e=``){var t;let n=(t=this._documentDataModel.getSelfOrHeaderFooterModel(e))==null?void 0:t.getBody();if(!n)throw Error(e===``?`Body is not found in the document.`:`Body is not found in the segment: ${e}`);return n}dispose(){super.dispose()}getId(){return this.id}getName(){return this._documentDataModel.getTitle()||``}isModern(){return this._documentDataModel.getSnapshot().documentStyle.documentFlavor===t.DocumentFlavor.MODERN}save(){return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId())}undo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.UndoCommand.id)}redo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.RedoCommand.id)}ensurePageHeader(e=0){return this._ensureHeaderFooter(`header`,e)}ensurePageFooter(e=0){return this._ensureHeaderFooter(`footer`,e)}insertText(e,t,n=``){return c({startOffset:e,endOffset:e,segmentId:n},s(t),this._documentDataModel,this._injector)}getHeaderFooterOptions(){let e=this._documentDataModel.getSnapshot().documentStyle;return{marginHeader:e.marginHeader,marginFooter:e.marginFooter,useFirstPageHeaderFooter:e.useFirstPageHeaderFooter,evenAndOddHeaders:e.evenAndOddHeaders}}setHeaderFooterOptions(e){if(this.isModern())throw Error(`The document is a modern document, header/footer is not supported.`);return this._commandService.syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this.getId(),headerFooterProps:e})}getTextRange(e,t,n=``){return this._injector.createInstance(d,this,e,t,n,this._injector)}getSections(){return this._documentDataModel.getSnapshot().documentStyle.documentFlavor===t.DocumentFlavor.TRADITIONAL?(0,r.getTopLevelSectionBreaks)(this.getBody()).map(e=>this._injector.createInstance(g,this,e.sectionId,this._injector)):[]}getSection(e){var t;return(t=this.getSections()[e])==null?null:t}getSectionAt(e){var t;return(t=this.getSections().find(t=>{let n=t.getRange();return e>=n.startOffset&&e<=n.endOffset}))==null?null:t}insertSectionBreak(e,n={}){var i;if(this._documentDataModel.getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new h;let a=(0,t.createSectionId)(new Set(((i=this.getBody().sectionBreaks)==null?[]:i).map(e=>e.sectionId)));return this._commandService.syncExecuteCommand(r.InsertDocumentSectionBreakCommand.id,{unitId:this.getId(),offset:e,sectionId:a,config:n})?this._injector.createInstance(g,this,a,this._injector):null}insertColumnBreak(e){if(this._documentDataModel.getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new h;return this.insertText(e,t.DataStreamTreeTokenType.COLUMN_BREAK)}insertHorizontalRule(e,n={padding:5,color:{rgb:`#CDD0D8`},width:1,dashStyle:t.DashStyleType.SOLID},i=``){var a;let o=this.getBody(i),s=(0,r.generateParagraphs)(t.DataStreamTreeTokenType.PARAGRAPH,void 0,n,(a=o.paragraphs)==null?void 0:a.map(e=>e.paragraphId)),l=s[0].paragraphId;return c({startOffset:e,endOffset:e,segmentId:i},{dataStream:t.DataStreamTreeTokenType.PARAGRAPH,paragraphs:s},this._documentDataModel,this._injector)?this.getParagraph(l,i):null}getParagraphs(e=``){let{paragraphs:t=[]}=this.getBody(e);return t.map(t=>this._createFDocumentParagraph(t.paragraphId,e))}getParagraph(e,t=``){let{paragraphs:n=[]}=this.getBody(t);return n.find(t=>t.paragraphId===e)?this._createFDocumentParagraph(e,t):null}findParagraphByText(e,t=``){return this.findParagraphs({text:e,segmentId:t})[0]||null}findParagraphs(e){let{text:t,paragraphId:n,segmentId:r=``}=typeof e==`string`?{text:e}:e;return this.getParagraphs(r).filter(e=>!(n&&e.getId()!==n||t&&!e.getText().includes(t)))}insertParagraph(e,t=``,n=``){let r=this._getParagraphInsertOffset(e,n);if(!c({startOffset:r,endOffset:r,segmentId:n},s(`${t}\r`),this._documentDataModel,this._injector))throw Error(`Failed to insert paragraph.`);let{paragraphs:i=[]}=this.getBody(n),a=i[e];if(!a)throw Error(`Failed to insert paragraph.`);return this._createFDocumentParagraph(a.paragraphId,n)}appendParagraph(e=``,t=``){let{paragraphs:n=[]}=this.getBody(t);return this.insertParagraph(n.length,e,t)}deleteRange(e){let t=this._normalizeDeleteRange(e);return t.startOffset>=t.endOffset?!1:c(t,{dataStream:``},this._documentDataModel,this._injector)}_createFDocumentParagraph(e,t=``){return this._injector.createInstance(p,this,e,t,this._injector)}_normalizeDeleteRange(e){let t=this.getBody(e.segmentId),n=t.dataStream.endsWith(`\r
|
|
3
|
-
`)?Math.max(0,t.dataStream.length-2):t.dataStream.length,r=Math.min(Math.max(e.endOffset,0),n);return{...e,startOffset:Math.min(Math.max(e.startOffset,0),r),endOffset:r}}_getParagraphInsertOffset(e,n=``){if(e<=0)return 0;let r=this.getBody(n),{dataStream:i,paragraphs:a=[]}=r;return a.length===0?Math.max(0,i.length-1):e>=a.length?a[a.length-1].startIndex+1:(0,t.getParagraphContentStartOffset)(r,a[e])}_ensureHeaderFooter(e,n){if(this.isModern())throw Error(`The document is a modern document, header/footer is not supported.`);let{createType:i,segmentId:a}=this._getHeaderFooterCreateInfo(e,n);if(a)return a;let o=(0,t.generateRandomId)(6);if(!this._commandService.syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this.getId(),segmentId:o,createType:i}))throw Error(`Failed to create page ${e}.`);return o}_getHeaderFooterCreateInfo(e,n){var i,a;let{documentStyle:o}=this._documentDataModel.getSnapshot(),s=n===0,c=(n+1)%2==0;if(s&&o.useFirstPageHeaderFooter===t.BooleanNumber.TRUE){var l,u;return e===`header`?{createType:r.HeaderFooterType.FIRST_PAGE_HEADER,segmentId:(l=o.firstPageHeaderId)==null?``:l}:{createType:r.HeaderFooterType.FIRST_PAGE_FOOTER,segmentId:(u=o.firstPageFooterId)==null?``:u}}if(c&&o.evenAndOddHeaders===t.BooleanNumber.TRUE){var d,f;return e===`header`?{createType:r.HeaderFooterType.EVEN_PAGE_HEADER,segmentId:(d=o.evenPageHeaderId)==null?``:d}:{createType:r.HeaderFooterType.EVEN_PAGE_FOOTER,segmentId:(f=o.evenPageFooterId)==null?``:f}}return e===`header`?{createType:r.HeaderFooterType.DEFAULT_HEADER,segmentId:(i=o.defaultHeaderId)==null?``:i}:{createType:r.HeaderFooterType.DEFAULT_FOOTER,segmentId:(a=o.defaultFooterId)==null?``:a}}};
|
|
2
|
+
`:e).filter(e=>e!==t.DataStreamTreeTokenType.BLOCK_START&&e!==t.DataStreamTreeTokenType.BLOCK_END&&e!==t.DataStreamTreeTokenType.SECTION_BREAK).join(``).replace(/\n$/,``)}var d=class extends n.FBaseInitialable{constructor(e,t,n,r,i){super(i),this._document=e,this._startOffset=t,this._endOffset=n,this._segmentId=r,this._injector=i,this._validateRange()}getRange(){return{startOffset:this._startOffset,endOffset:this._endOffset,segmentId:this._segmentId}}getText(){return this._document.getBody(this._segmentId).dataStream.slice(this._startOffset,this._endOffset)}getExplicitTextStyleRuns(){let{textRuns:e=[]}=this._document.getBody(this._segmentId);return e.filter(e=>e.st<this._endOffset&&e.ed>this._startOffset).map(e=>{var n;return{startOffset:Math.max(e.st,this._startOffset),endOffset:Math.min(e.ed,this._endOffset),textStyle:t.Tools.deepClone((n=e.ts)==null?{}:n)}})}getCommonExplicitTextStyle(){if(this._startOffset===this._endOffset)return{};let[e,...n]=this._getStyleSegments(),r=t.Tools.deepClone(e.textStyle);for(let e of Object.keys(r))n.some(t=>!f(t.textStyle[e],r[e]))&&delete r[e];return r}describe(){let e=this.getExplicitTextStyleRuns(),t=this.getCommonExplicitTextStyle();return{...this.getRange(),text:this.getText(),length:this._endOffset-this._startOffset,explicitTextStyleRuns:e,commonExplicitTextStyle:t}}setTextStyle(e){return this._startOffset!==this._endOffset&&l(this.getRange(),{dataStream:``,textRuns:[{st:0,ed:this._endOffset-this._startOffset,ts:t.Tools.deepClone(e)}]},t.UpdateDocsAttributeType.COVER,this._document.getDocumentDataModel(),this._injector)}setText(e){return c(this.getRange(),s(e),this._document.getDocumentDataModel(),this._injector)}_validateRange(){let e=this._document.getBody(this._segmentId).dataStream.length;if(!Number.isInteger(this._startOffset)||!Number.isInteger(this._endOffset)||this._startOffset<0||this._endOffset<this._startOffset||this._endOffset>e)throw RangeError(`Invalid document text range [${this._startOffset}, ${this._endOffset}) for body length ${e}.`)}_getStyleSegments(){let e=this.getExplicitTextStyleRuns(),t=[],n=this._startOffset;for(let r of e)n<r.startOffset&&t.push({startOffset:n,endOffset:r.startOffset,textStyle:{}}),t.push(r),n=r.endOffset;return n<this._endOffset&&t.push({startOffset:n,endOffset:this._endOffset,textStyle:{}}),t}};function f(e,t){return JSON.stringify(e)===JSON.stringify(t)}function p(e,t){return function(n,r){t(n,r,e)}}function m(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let h=class extends n.FBaseInitialable{constructor(e,t,n=``,r,i){super(r),this._document=e,this._paragraphId=t,this._segmentId=n,this._injector=r,this._commandService=i}getId(){return this._paragraphId}getSegmentId(){return this._segmentId}getInfo(){let e=this._document.getBody(this._segmentId),{paragraphs:n=[]}=e,r=n.map((e,t)=>({paragraph:e,paragraphIndex:t})).filter(({paragraph:e})=>e.paragraphId===this._paragraphId);if(r.length===0)throw Error(`Document paragraph with id ${this._paragraphId} not found`);if(r.length>1)throw Error(`Multiple document paragraphs with id ${this._paragraphId} found`);let{paragraph:i,paragraphIndex:a}=r[0];return{paragraph:i,paragraphIndex:a,startOffset:(0,t.getParagraphContentStartOffset)(e,i),endOffset:i.startIndex}}getRange(){let{startOffset:e,endOffset:t}=this.getInfo();return{startOffset:e,endOffset:t,segmentId:this._segmentId}}getTextRange(){let{startOffset:e,endOffset:t}=this.getInfo();return this._injector.createInstance(d,this._document,e,t,this._segmentId,this._injector)}findText(e,t={}){var n,r;let i=(n=t.occurrence)==null?0:n;if(!Number.isInteger(i)||i<0)throw RangeError(`Text occurrence must be a non-negative integer.`);return(r=this.findAllText(e,t)[i])==null?null:r}findAllText(e,n={}){var r;if(e.length===0)throw TypeError(`Text to find must not be empty.`);let i=(r=n.matchCase)==null||r,a=this.getText(),o=t.regexp.createLiteralRegExp(e,i?`gu`:`giu`),{startOffset:s}=this.getInfo(),c=[];for(let e of a.matchAll(o)){let t=s+e.index;c.push(this._injector.createInstance(d,this._document,t,t+e[0].length,this._segmentId,this._injector))}return c}getText(){let{dataStream:e}=this._document.getBody(this._segmentId),{startOffset:t,endOffset:n}=this.getInfo();return e.slice(t,n)}setText(e){let{startOffset:t,endOffset:n}=this.getInfo();return c({startOffset:t,endOffset:n,segmentId:this._segmentId},s(e),this._document.getDocumentDataModel(),this._injector)}appendText(e){let{endOffset:t}=this.getInfo();return this._document.insertText(t,e,this._segmentId)}setStyle(e){let{startOffset:t,endOffset:n}=this.getInfo();return this._commandService.syncExecuteCommand(r.UpdateDocumentParagraphStyleCommand.id,{unitId:this._document.getId(),segmentId:this._segmentId,paragraphId:this._paragraphId,startOffset:t,endOffset:n,style:e})}isListItem(){let{paragraph:e}=this.getInfo();return!!e.bullet}isTask(){var e;let{paragraph:n}=this.getInfo(),r=(e=n.bullet)==null?void 0:e.listType;return r===t.PresetListType.CHECK_LIST||r===t.PresetListType.CHECK_LIST_CHECKED}setTaskChecked(e){if(!this.isTask())return!1;let{paragraph:n,endOffset:r}=this.getInfo(),i=n.bullet,a={dataStream:``,paragraphs:[{...n,startIndex:0,bullet:{...i,listType:e?t.PresetListType.CHECK_LIST_CHECKED:t.PresetListType.CHECK_LIST}}]};return this._preserveExplicitParagraphIds(a),l({startOffset:r,endOffset:r+1,segmentId:this._segmentId},a,t.UpdateDocsAttributeType.REPLACE,this._document.getDocumentDataModel(),this._injector)}remove(){let{startOffset:e,endOffset:t}=this.getInfo();return this._document.deleteRange({startOffset:e,endOffset:t+1,segmentId:this._segmentId})}_preserveExplicitParagraphIds(e){e[t.RESTORE_INSERTED_PARAGRAPH_IDS]=!0}};h=m([p(4,t.ICommandService)],h);function g(e){return typeof e!=`object`||!e?!1:typeof e.getId==`function`&&typeof e.getSegmentId==`function`&&typeof e.getInfo==`function`&&typeof e.getRange==`function`}function _(e){let{pageNumberStart:n,pageSize:r,pageOrient:i,marginTop:a,marginBottom:o,marginLeft:s,marginRight:c}=e;if(n!=null&&(!Number.isInteger(n)||n<1))throw RangeError(`Section page number start must be a positive integer.`);if(r&&[r.width,r.height].some(e=>e!=null&&(!Number.isFinite(e)||e<=0)))throw RangeError(`Section page size must be finite and positive.`);if(i!=null&&!Object.values(t.PageOrientType).includes(i))throw RangeError(`Invalid section page orientation.`);if([a,o,s,c].some(e=>e!=null&&(!Number.isFinite(e)||e<0)))throw RangeError(`Section page margins must be finite and non-negative.`)}var v=class extends Error{constructor(){super(`Section column APIs are supported only in traditional documents. Use ColumnGroup APIs for modern documents, or resolve an unspecified document flavor first.`),this.name=`DocsSectionUnsupportedDocumentFlavorError`}};let y=class{constructor(e,t,n){this._document=e,this._sectionId=t,this._commandService=n}getId(){return this._sectionId}getIndex(){return this._resolve().index}getConfig(){return this._getConfigSnapshot()}getRange(){return this._getRange(this._resolve().index)}getColumns(){var e;return t.Tools.deepClone((e=this._getConfigSnapshot().columnProperties)==null?[]:e)}describe(){var e,n,r;let{index:i}=this._resolve(),a=this._getConfigSnapshot(),o=(e=a.columnProperties)==null?[]:e,s={defaultHeader:this._describeHeaderFooterReference(`header`,`default`),defaultFooter:this._describeHeaderFooterReference(`footer`,`default`),firstHeader:this._describeHeaderFooterReference(`header`,`first`),firstFooter:this._describeHeaderFooterReference(`footer`,`first`),evenHeader:this._describeHeaderFooterReference(`header`,`even`),evenFooter:this._describeHeaderFooterReference(`footer`,`even`)};return{sectionId:this._sectionId,index:i,range:this._getRange(i),columnCount:o.length||1,columns:t.Tools.deepClone(o),columnSeparatorType:(n=a.columnSeparatorType)==null?t.ColumnSeparatorType.NONE:n,sectionType:(r=a.sectionType)==null?t.SectionType.SECTION_TYPE_UNSPECIFIED:r,headerFooter:s,config:a}}setColumns(e,n={}){var i,a;if(this._assertTraditionalDocument(),!Number.isInteger(e)||e<1)throw RangeError(`Section column count must be a positive integer.`);if(n.widths&&n.widths.length!==e)throw RangeError(`Section column widths must match the column count.`);if(n.gap!=null&&(!Number.isFinite(n.gap)||n.gap<0))throw RangeError(`Section column gap must be finite and non-negative.`);let o=Math.max(0,(i=n.gap)==null?18:i),s=this._getConfigSnapshot(),c=(0,r.createSectionColumnProperties)(this._document.getDocumentDataModel().getSnapshot().documentStyle,s,e,o,n.widths),l=typeof n.separator==`boolean`?n.separator?t.ColumnSeparatorType.BETWEEN_EACH_COLUMN:t.ColumnSeparatorType.NONE:(a=n.separator)==null?t.ColumnSeparatorType.NONE:a;if(!Object.values(t.ColumnSeparatorType).includes(l))throw RangeError(`Invalid section column separator type.`);return this._update({columnProperties:c,columnSeparatorType:l})}setColumnProperties(e,n=t.ColumnSeparatorType.NONE){if(this._assertTraditionalDocument(),!Object.values(t.ColumnSeparatorType).includes(n))throw RangeError(`Invalid section column separator type.`);if(e.some(({width:e,paddingEnd:t})=>!Number.isFinite(e)||!Number.isFinite(t)||e<0||t<0))throw RangeError(`Section column widths and padding must be finite and non-negative.`);let i=(0,r.getSectionContentWidth)(this._document.getDocumentDataModel().getSnapshot().documentStyle,this._getConfigSnapshot());if(e.reduce((e,{width:t,paddingEnd:n})=>e+t+n,0)>i)throw RangeError(`Section columns exceed the available page content width.`);return this._update({columnProperties:t.Tools.deepClone(e),columnSeparatorType:n})}setSectionType(e){if(this._assertTraditionalDocument(),!Object.values(t.SectionType).includes(e))throw RangeError(`Invalid section type.`);return this._update({sectionType:e})}getPageSetup(){let{pageNumberStart:e,pageSize:n,pageOrient:r,marginTop:i,marginBottom:a,marginLeft:o,marginRight:s}=this._getConfigSnapshot();return t.Tools.deepClone({pageNumberStart:e,pageSize:n,pageOrient:r,marginTop:i,marginBottom:a,marginLeft:o,marginRight:s})}getEffectivePageSetup(){this._assertTraditionalDocument();let e=this._document.getDocumentDataModel().getSnapshot().documentStyle;return t.Tools.deepClone((0,r.getEffectiveSectionPageSetup)(e,this._getConfigSnapshot()))}setPageSetup(e){this._assertTraditionalDocument(),_(e);let n=t.Tools.deepClone(e);t.Tools.removeNull(n);let i=this._document.getDocumentDataModel().getSnapshot().documentStyle;return(0,r.getEffectiveSectionPageSetup)(i,{...this._getConfigSnapshot(),...n}),this._update(n)}ensureHeader(e=`default`){return this._ensureHeaderFooter(`header`,e)}ensureFooter(e=`default`){return this._ensureHeaderFooter(`footer`,e)}getHeaderId(e=`default`){var t;return(t=this._getHeaderFooterReference(`header`,e).segmentId)==null?null:t}getFooterId(e=`default`){var t;return(t=this._getHeaderFooterReference(`footer`,e).segmentId)==null?null:t}isHeaderLinkedToPrevious(e=`default`){return this._getHeaderFooterReference(`header`,e).linkedToPrevious}isFooterLinkedToPrevious(e=`default`){return this._getHeaderFooterReference(`footer`,e).linkedToPrevious}setHeaderLinkedToPrevious(e,t=`default`){return this._setHeaderFooterLinkedToPrevious(`header`,t,e)}setFooterLinkedToPrevious(e,t=`default`){return this._setHeaderFooterLinkedToPrevious(`footer`,t,e)}setHeaderFooterOptions(e){return this._assertTraditionalDocument(),this._update(e)}remove(){return this._assertTraditionalDocument(),this._commandService.syncExecuteCommand(r.DeleteDocumentSectionBreakCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId})}_update(e){let{sectionId:t,startIndex:n,...i}=e;return this._commandService.syncExecuteCommand(r.UpdateDocumentSectionCommand.id,{unitId:this._document.getId(),updates:[{sectionId:this._sectionId,config:i}]})}_ensureHeaderFooter(e,n){this._assertTraditionalDocument();let{index:i}=this._resolve(),a=this._getConfigSnapshot()[(0,t.getSectionHeaderFooterReferenceKey)(e,n)];if(typeof a==`string`&&a)return a;if(i>0){let i=(0,t.generateRandomId)(6);if(!this._commandService.syncExecuteCommand(r.SetSectionHeaderFooterLinkCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId,kind:e,variant:n,linkedToPrevious:!1,segmentId:i}))throw Error(`Failed to create section ${e}.`);return i}let o={default:e===`header`?r.HeaderFooterType.DEFAULT_HEADER:r.HeaderFooterType.DEFAULT_FOOTER,first:e===`header`?r.HeaderFooterType.FIRST_PAGE_HEADER:r.HeaderFooterType.FIRST_PAGE_FOOTER,even:e===`header`?r.HeaderFooterType.EVEN_PAGE_HEADER:r.HeaderFooterType.EVEN_PAGE_FOOTER},s=(0,t.generateRandomId)(6);if(!this._commandService.syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this._document.getId(),segmentId:s,createType:o[n],sectionId:this._sectionId}))throw Error(`Failed to create section ${e}.`);return s}_getHeaderFooterReference(e,n){let{index:i}=this._resolve();return(0,t.resolveSectionHeaderFooterReference)(this._document.getDocumentDataModel().getSnapshot().documentStyle,(0,r.getTopLevelSectionBreaks)(this._document.getBody()),i,(0,t.getSectionHeaderFooterReferenceKey)(e,n))}_describeHeaderFooterReference(e,t){var n;let r=this._getHeaderFooterReference(e,t);return{segmentId:(n=r.segmentId)==null?null:n,linkedToPrevious:r.linkedToPrevious}}_setHeaderFooterLinkedToPrevious(e,n,i){return this._assertTraditionalDocument(),this._commandService.syncExecuteCommand(r.SetSectionHeaderFooterLinkCommand.id,{unitId:this._document.getId(),sectionId:this._sectionId,kind:e,variant:n,linkedToPrevious:i,...i?{}:{segmentId:(0,t.generateRandomId)(6)}})}_assertTraditionalDocument(){if(this._document.getDocumentDataModel().getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new v}_getConfigSnapshot(){return t.Tools.deepClone(this._resolve().sectionBreak)}_getRange(e){let t=(0,r.getTopLevelSectionBreaks)(this._document.getBody());return{startOffset:e===0?0:t[e-1].startIndex+1,endOffset:t[e].startIndex,segmentId:``}}_resolve(){this._assertTraditionalDocument();let e=(0,r.getTopLevelSectionBreaks)(this._document.getBody()),t=e.findIndex(e=>e.sectionId===this._sectionId);if(t<0)throw Error(`Document section with id ${this._sectionId} not found.`);return{index:t,sectionBreak:e[t]}}};y=m([p(2,t.ICommandService)],y);function b(e){"@babel/helpers - typeof";return b=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},b(e)}function x(e,t){if(b(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(b(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function S(e){var t=x(e,`string`);return b(t)==`symbol`?t:t+``}function C(e,t,n){return(t=S(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}let w=class extends n.FBaseInitialable{constructor(e,t,n,r,i){super(t),this._documentDataModel=e,this._injector=t,this._univerInstanceService=n,this._resourceLoaderService=r,this._commandService=i,C(this,`id`,void 0),this.id=this._documentDataModel.getUnitId()}getDocumentDataModel(e=``){let t=this._documentDataModel.getSelfOrHeaderFooterModel(e);if(!t)throw Error(e===``?`Document data model is not found.`:`Document data model is not found in the segment: ${e}`);return t}getBody(e=``){var t;let n=(t=this._documentDataModel.getSelfOrHeaderFooterModel(e))==null?void 0:t.getBody();if(!n)throw Error(e===``?`Body is not found in the document.`:`Body is not found in the segment: ${e}`);return n}dispose(){super.dispose()}getId(){return this.id}getName(){return this._documentDataModel.getTitle()||``}getDocumentFlavor(){return this._resolveDocumentFlavor()}isTraditional(){return this._resolveDocumentFlavor()===t.DocumentFlavor.TRADITIONAL}isModern(){return this._resolveDocumentFlavor()===t.DocumentFlavor.MODERN}_resolveDocumentFlavor(){var e;return(e=this._documentDataModel.getSnapshot().documentStyle.documentFlavor)==null?t.DocumentFlavor.UNSPECIFIED:e}save(){return this._resourceLoaderService.saveUnit(this._documentDataModel.getUnitId())}undo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.UndoCommand.id)}redo(){return this._univerInstanceService.focusUnit(this.id),this._commandService.syncExecuteCommand(t.RedoCommand.id)}ensurePageHeader(e=0){return this._ensureHeaderFooter(`header`,e)}ensurePageFooter(e=0){return this._ensureHeaderFooter(`footer`,e)}insertText(e,t,n=``){return c({startOffset:e,endOffset:e,segmentId:n},s(t),this._documentDataModel,this._injector)}getHeaderFooterOptions(){let e=this._documentDataModel.getSnapshot().documentStyle;return{marginHeader:e.marginHeader,marginFooter:e.marginFooter,useFirstPageHeaderFooter:e.useFirstPageHeaderFooter,evenAndOddHeaders:e.evenAndOddHeaders}}setHeaderFooterOptions(e){if(this.isModern())throw Error(`The document is a modern document, header/footer is not supported.`);return this._commandService.syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this.getId(),headerFooterProps:e})}getTextRange(e,t,n=``){return this._injector.createInstance(d,this,e,t,n,this._injector)}getSections(){return this._documentDataModel.getSnapshot().documentStyle.documentFlavor===t.DocumentFlavor.TRADITIONAL?(0,r.getTopLevelSectionBreaks)(this.getBody()).map(e=>this._injector.createInstance(y,this,e.sectionId)):[]}getSection(e){var t;return(t=this.getSections()[e])==null?null:t}getSectionAt(e){var t;return(t=this.getSections().find(t=>{let n=t.getRange();return e>=n.startOffset&&e<n.endOffset}))==null?null:t}insertSectionBreak(e,n={}){var i;if(this._documentDataModel.getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new v;let{nextSectionType:a,...o}=n,s=(0,t.createSectionId)(new Set(((i=this.getBody().sectionBreaks)==null?[]:i).map(e=>e.sectionId)));return this._commandService.syncExecuteCommand(r.InsertDocumentSectionBreakCommand.id,{unitId:this.getId(),offset:e,sectionId:s,config:o,nextSectionType:a})?this._injector.createInstance(y,this,s):null}insertColumnBreak(e){if(this._documentDataModel.getSnapshot().documentStyle.documentFlavor!==t.DocumentFlavor.TRADITIONAL)throw new v;return this._commandService.syncExecuteCommand(r.InsertDocumentColumnBreakCommand.id,{unitId:this.getId(),offset:e})}insertHorizontalRule(e,n={padding:5,color:{rgb:`#CDD0D8`},width:1,dashStyle:t.DashStyleType.SOLID},i=``){var a;let o=this.getBody(i),s=(0,r.generateParagraphs)(t.DataStreamTreeTokenType.PARAGRAPH,void 0,n,(a=o.paragraphs)==null?void 0:a.map(e=>e.paragraphId)),l=s[0].paragraphId;return c({startOffset:e,endOffset:e,segmentId:i},{dataStream:t.DataStreamTreeTokenType.PARAGRAPH,paragraphs:s},this._documentDataModel,this._injector)?this.getParagraph(l,i):null}getParagraphs(e=``){let{paragraphs:t=[]}=this.getBody(e);return t.map(t=>this._createFDocumentParagraph(t.paragraphId,e))}getParagraph(e,t=``){let{paragraphs:n=[]}=this.getBody(t);return n.find(t=>t.paragraphId===e)?this._createFDocumentParagraph(e,t):null}findParagraphByText(e,t=``){return this.findParagraphs({text:e,segmentId:t})[0]||null}findParagraphs(e){let{text:t,paragraphId:n,segmentId:r=``}=typeof e==`string`?{text:e}:e;return this.getParagraphs(r).filter(e=>!(n&&e.getId()!==n||t&&!e.getText().includes(t)))}insertParagraph(e,t=``,n=``){let r=this._getParagraphInsertOffset(e,n);if(!c({startOffset:r,endOffset:r,segmentId:n},s(`${t}\r`),this._documentDataModel,this._injector))throw Error(`Failed to insert paragraph.`);let{paragraphs:i=[]}=this.getBody(n),a=i[e];if(!a)throw Error(`Failed to insert paragraph.`);return this._createFDocumentParagraph(a.paragraphId,n)}appendParagraph(e=``,t=``){let{paragraphs:n=[]}=this.getBody(t);return this.insertParagraph(n.length,e,t)}deleteRange(e){let t=this._normalizeDeleteRange(e);return t.startOffset>=t.endOffset?!1:c(t,{dataStream:``},this._documentDataModel,this._injector)}_createFDocumentParagraph(e,t=``){return this._injector.createInstance(h,this,e,t,this._injector)}_normalizeDeleteRange(e){let t=this.getBody(e.segmentId),n=t.dataStream.endsWith(`\r
|
|
3
|
+
`)?Math.max(0,t.dataStream.length-2):t.dataStream.length,r=Math.min(Math.max(e.endOffset,0),n);return{...e,startOffset:Math.min(Math.max(e.startOffset,0),r),endOffset:r}}_getParagraphInsertOffset(e,n=``){if(e<=0)return 0;let r=this.getBody(n),{dataStream:i,paragraphs:a=[]}=r;return a.length===0?Math.max(0,i.length-1):e>=a.length?a[a.length-1].startIndex+1:(0,t.getParagraphContentStartOffset)(r,a[e])}_ensureHeaderFooter(e,n){if(this.isModern())throw Error(`The document is a modern document, header/footer is not supported.`);let{createType:i,segmentId:a}=this._getHeaderFooterCreateInfo(e,n);if(a)return a;let o=(0,t.generateRandomId)(6);if(!this._commandService.syncExecuteCommand(r.CreateHeaderFooterCommand.id,{unitId:this.getId(),segmentId:o,createType:i}))throw Error(`Failed to create page ${e}.`);return o}_getHeaderFooterCreateInfo(e,n){var i,a;let{documentStyle:o}=this._documentDataModel.getSnapshot(),s=n===0,c=(n+1)%2==0;if(s&&o.useFirstPageHeaderFooter===t.BooleanNumber.TRUE){var l,u;return e===`header`?{createType:r.HeaderFooterType.FIRST_PAGE_HEADER,segmentId:(l=o.firstPageHeaderId)==null?``:l}:{createType:r.HeaderFooterType.FIRST_PAGE_FOOTER,segmentId:(u=o.firstPageFooterId)==null?``:u}}if(c&&o.evenAndOddHeaders===t.BooleanNumber.TRUE){var d,f;return e===`header`?{createType:r.HeaderFooterType.EVEN_PAGE_HEADER,segmentId:(d=o.evenPageHeaderId)==null?``:d}:{createType:r.HeaderFooterType.EVEN_PAGE_FOOTER,segmentId:(f=o.evenPageFooterId)==null?``:f}}return e===`header`?{createType:r.HeaderFooterType.DEFAULT_HEADER,segmentId:(i=o.defaultHeaderId)==null?``:i}:{createType:r.HeaderFooterType.DEFAULT_FOOTER,segmentId:(a=o.defaultFooterId)==null?``:a}}};w=m([p(1,(0,t.Inject)(t.Injector)),p(2,t.IUniverInstanceService),p(3,(0,t.Inject)(t.IResourceLoaderService)),p(4,t.ICommandService)],w);var T=class extends n.FUniver{createDocument(e){let n=this._injector.get(t.IUniverInstanceService).createUnit(t.UniverInstanceType.UNIVER_DOC,e);return this._injector.createInstance(w,n)}getActiveDocument(){let e=this._univerInstanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC);return e?this._injector.createInstance(w,e):null}getDocument(e){let n=this._univerInstanceService.getUnit(e,t.UniverInstanceType.UNIVER_DOC);return n?this._injector.createInstance(w,n):null}};n.FUniver.extend(T);var E=class extends n.FEnum{get DocumentFlavor(){return t.DocumentFlavor}get SectionType(){return t.SectionType}get ColumnSeparatorType(){return t.ColumnSeparatorType}};n.FEnum.extend(E),e.DocsSectionUnsupportedDocumentFlavorError=v,e.FDocsEnumMixin=E,Object.defineProperty(e,"FDocument",{enumerable:!0,get:function(){return w}}),Object.defineProperty(e,"FDocumentParagraph",{enumerable:!0,get:function(){return h}}),Object.defineProperty(e,"FDocumentSection",{enumerable:!0,get:function(){return y}}),e.FDocumentTextRange=d,e.isParagraphFacade=g,e.stripBlockTokens=u});
|