@univerjs/docs 1.1.0 → 1.3.0

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/index.js CHANGED
@@ -646,9 +646,11 @@ let DocLayoutExecutorService = _DocLayoutExecutorService = class DocLayoutExecut
646
646
  });
647
647
  }
648
648
  _enqueueCommittedMutation(params) {
649
+ var _params$actions;
649
650
  const executor = this._executor;
650
651
  const dataModel = this._getEligibleModel(params.unitId);
651
652
  if (executor == null || dataModel == null) return;
653
+ if (_univerjs_core.JSONX.isNoop(params.actions) || ((_params$actions = params.actions) === null || _params$actions === void 0 ? void 0 : _params$actions.length) === 0) return;
652
654
  const session = this._sessions.get(params.unitId);
653
655
  if (session == null) {
654
656
  this._replaceSession(dataModel).catch((error) => {
@@ -656,7 +658,6 @@ let DocLayoutExecutorService = _DocLayoutExecutorService = class DocLayoutExecut
656
658
  });
657
659
  return;
658
660
  }
659
- if (_univerjs_core.JSONX.isNoop(params.actions)) return;
660
661
  const sourceMutationRevision = dataModel.getMutationRevision();
661
662
  if (sourceMutationRevision !== session.sourceMutationRevision + 1) {
662
663
  session.needsResnapshot = true;
@@ -2125,6 +2126,339 @@ function getTopLevelSectionBreaks(body) {
2125
2126
  return result;
2126
2127
  }
2127
2128
 
2129
+ //#endregion
2130
+ //#region src/services/permission/document-permission-resolver.ts
2131
+ const HEADER_FOOTER_REFERENCE_KEYS = [
2132
+ "defaultHeaderId",
2133
+ "defaultFooterId",
2134
+ "firstPageHeaderId",
2135
+ "firstPageFooterId",
2136
+ "evenPageHeaderId",
2137
+ "evenPageFooterId"
2138
+ ];
2139
+ const documentPermissionResolverIndexCache = /* @__PURE__ */ new WeakMap();
2140
+ function getDocumentPermissionResolverIndex(documentDataModel) {
2141
+ var _getMutationRevision, _snapshot$body;
2142
+ const mutationRevision = ((_getMutationRevision = documentDataModel.getMutationRevision) === null || _getMutationRevision === void 0 ? void 0 : _getMutationRevision.call(documentDataModel)) ?? NaN;
2143
+ const cached = documentPermissionResolverIndexCache.get(documentDataModel);
2144
+ if ((cached === null || cached === void 0 ? void 0 : cached.mutationRevision) === mutationRevision) return cached;
2145
+ const snapshot = documentDataModel.getSnapshot();
2146
+ const topLevelSections = snapshot.body ? getTopLevelSectionBreaks(snapshot.body) : [];
2147
+ const drawingSegmentIds = /* @__PURE__ */ new Map();
2148
+ const addDrawingSegments = (body, segmentId) => {
2149
+ for (const block of (body === null || body === void 0 ? void 0 : body.customBlocks) ?? []) if (!drawingSegmentIds.has(block.blockId)) drawingSegmentIds.set(block.blockId, segmentId);
2150
+ };
2151
+ addDrawingSegments(snapshot.body, "");
2152
+ Object.entries(snapshot.headers ?? {}).forEach(([segmentId, header]) => addDrawingSegments(header.body, segmentId));
2153
+ Object.entries(snapshot.footers ?? {}).forEach(([segmentId, footer]) => addDrawingSegments(footer.body, segmentId));
2154
+ Object.entries(snapshot.notes ?? {}).forEach(([segmentId, footnote]) => addDrawingSegments(footnote.body, segmentId));
2155
+ const footnoteReferenceRanges = /* @__PURE__ */ new Map();
2156
+ for (const reference of ((_snapshot$body = snapshot.body) === null || _snapshot$body === void 0 ? void 0 : _snapshot$body.customRanges) ?? []) {
2157
+ var _reference$properties;
2158
+ if ((reference.rangeType === _univerjs_core.CustomRangeType.FOOTNOTE || reference.rangeType === _univerjs_core.CustomRangeType.ENDNOTE) && typeof ((_reference$properties = reference.properties) === null || _reference$properties === void 0 ? void 0 : _reference$properties.noteId) === "string") footnoteReferenceRanges.set(reference.properties.noteId, {
2159
+ startOffset: reference.startIndex,
2160
+ endOffset: reference.endIndex + 1
2161
+ });
2162
+ }
2163
+ const index = {
2164
+ drawingSegmentIds,
2165
+ footnoteReferenceRanges,
2166
+ mutationRevision,
2167
+ segments: /* @__PURE__ */ new Map(),
2168
+ topLevelSections
2169
+ };
2170
+ documentPermissionResolverIndexCache.set(documentDataModel, index);
2171
+ return index;
2172
+ }
2173
+ function getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex = getDocumentPermissionResolverIndex(documentDataModel)) {
2174
+ var _documentDataModel$ge, _snapshot$notes;
2175
+ const cached = resolverIndex.segments.get(segmentId);
2176
+ if (cached) return cached;
2177
+ const body = ((_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody()) ?? null;
2178
+ const snapshot = documentDataModel.getSnapshot();
2179
+ const note = (_snapshot$notes = snapshot.notes) === null || _snapshot$notes === void 0 ? void 0 : _snapshot$notes[segmentId];
2180
+ const drawings = (note ? note.drawings : snapshot.drawings) ?? {};
2181
+ const entities = body == null ? [] : [
2182
+ ...(body.tables ?? []).map((item) => ({
2183
+ type: "table",
2184
+ id: item.tableId,
2185
+ startOffset: item.startIndex,
2186
+ endOffset: item.endIndex
2187
+ })),
2188
+ ...(body.customBlocks ?? []).map((item) => ({
2189
+ type: drawings[item.blockId] ? "drawing" : "custom-block",
2190
+ id: item.blockId,
2191
+ startOffset: item.startIndex,
2192
+ endOffset: item.startIndex + 1
2193
+ })),
2194
+ ...(body.blockRanges ?? []).map((item) => ({
2195
+ type: "block-range",
2196
+ id: item.blockId,
2197
+ startOffset: item.startIndex,
2198
+ endOffset: item.endIndex + 1
2199
+ })),
2200
+ ...(body.customRanges ?? []).map((item) => ({
2201
+ type: "custom-range",
2202
+ id: item.rangeId,
2203
+ startOffset: item.startIndex,
2204
+ endOffset: item.endIndex + 1
2205
+ })),
2206
+ ...(body.columnGroups ?? []).map((item) => ({
2207
+ type: "column-group",
2208
+ id: item.columnGroupId,
2209
+ startOffset: item.startIndex,
2210
+ endOffset: item.endIndex + 1
2211
+ }))
2212
+ ];
2213
+ const entityRanges = new Map(entities.map((entity) => [`${entity.type}:${entity.id}`, {
2214
+ startOffset: entity.startOffset,
2215
+ endOffset: entity.endOffset
2216
+ }]));
2217
+ for (const block of (body === null || body === void 0 ? void 0 : body.customBlocks) ?? []) {
2218
+ const range = {
2219
+ startOffset: block.startIndex,
2220
+ endOffset: block.startIndex + 1
2221
+ };
2222
+ entityRanges.set(`custom-block:${block.blockId}`, range);
2223
+ entityRanges.set(`drawing:${block.blockId}`, range);
2224
+ }
2225
+ const segmentIndex = {
2226
+ body,
2227
+ entities,
2228
+ entityRanges,
2229
+ paragraphContentStartOffsets: body == null ? /* @__PURE__ */ new Map() : (0, _univerjs_core.getParagraphContentStartOffsets)(body),
2230
+ paragraphsById: new Map(((body === null || body === void 0 ? void 0 : body.paragraphs) ?? []).filter((paragraph) => paragraph.paragraphId != null).map((paragraph) => [paragraph.paragraphId, paragraph]))
2231
+ };
2232
+ resolverIndex.segments.set(segmentId, segmentIndex);
2233
+ return segmentIndex;
2234
+ }
2235
+ function getDocumentDrawingSegmentId(documentDataModel, drawingId) {
2236
+ return getDocumentPermissionResolverIndex(documentDataModel).drawingSegmentIds.get(drawingId) ?? "";
2237
+ }
2238
+ function getDocumentEditTargetObjectIds(documentDataModel, segmentId, range) {
2239
+ const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
2240
+ const segmentIndex = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex);
2241
+ const { body } = segmentIndex;
2242
+ if (!body) return [];
2243
+ return [
2244
+ ...getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex),
2245
+ ...getParagraphPermissionObjectIds(body, segmentId, range, segmentIndex),
2246
+ ...getEntityPermissionObjectIds(segmentId, range, segmentIndex)
2247
+ ];
2248
+ }
2249
+ function getDocumentEditTargetObjectIdsFromActions(documentDataModel, segmentId, actions) {
2250
+ const result = /* @__PURE__ */ new Set();
2251
+ if (actions == null) return [];
2252
+ const cursor = _univerjs_core.JSON1.type.readCursor(actions);
2253
+ cursor.traverse(null, () => {
2254
+ const path = cursor.getPath();
2255
+ const edit = cursor.getComponent();
2256
+ const scopedSegment = [
2257
+ "headers",
2258
+ "footers",
2259
+ "notes"
2260
+ ].includes(String(path[0])) && typeof path[1] === "string" ? path[1] : segmentId;
2261
+ if (path[0] === "notes" && typeof path[1] === "string") getSectionPermissionObjectIds(documentDataModel, path[1], {
2262
+ startOffset: 0,
2263
+ endOffset: 0
2264
+ }).forEach((objectId) => result.add(objectId));
2265
+ if (path[0] === "noteSettings") getDocumentPermissionResolverIndex(documentDataModel).topLevelSections.forEach((section) => {
2266
+ result.add(getDocumentSectionPermissionObjectId("", section.sectionId));
2267
+ });
2268
+ if (path[0] === "body" && path[1] === "sectionBreaks" && typeof path[2] === "number" && (path[3] === "noteProperties" || path[3] === "suppressEndnotes")) {
2269
+ var _documentDataModel$ge2;
2270
+ const sectionId = (_documentDataModel$ge2 = documentDataModel.getBody()) === null || _documentDataModel$ge2 === void 0 || (_documentDataModel$ge2 = _documentDataModel$ge2.sectionBreaks) === null || _documentDataModel$ge2 === void 0 || (_documentDataModel$ge2 = _documentDataModel$ge2[path[2]]) === null || _documentDataModel$ge2 === void 0 ? void 0 : _documentDataModel$ge2.sectionId;
2271
+ if (sectionId) result.add(getDocumentSectionPermissionObjectId("", sectionId));
2272
+ }
2273
+ if (path[path.length - 1] !== "body" || (edit === null || edit === void 0 ? void 0 : edit.et) !== _univerjs_core.TextX.id || !Array.isArray(edit.e) || !edit.e.every(isTextXAction)) return;
2274
+ const textActions = edit.e;
2275
+ let offset = 0;
2276
+ const addRange = (startOffset, endOffset) => {
2277
+ getDocumentEditTargetObjectIds(documentDataModel, scopedSegment, {
2278
+ startOffset,
2279
+ endOffset
2280
+ }).forEach((objectId) => result.add(objectId));
2281
+ };
2282
+ textActions.forEach((action) => {
2283
+ if (action.t === _univerjs_core.TextXActionType.INSERT) {
2284
+ addRange(offset, offset);
2285
+ return;
2286
+ }
2287
+ if (action.t === _univerjs_core.TextXActionType.DELETE) {
2288
+ addRange(offset, offset + action.len);
2289
+ offset += action.len;
2290
+ return;
2291
+ }
2292
+ if (action.body !== void 0 || action.oldBody !== void 0 || action.coverType !== void 0) addRange(offset, offset + action.len);
2293
+ offset += action.len;
2294
+ });
2295
+ });
2296
+ getDrawingIdsFromActions(actions).forEach((drawingId) => {
2297
+ var _documentDataModel$ge3, _body$customBlocks;
2298
+ result.add(getDocumentEntityPermissionObjectId(segmentId, "drawing", drawingId));
2299
+ const body = (_documentDataModel$ge3 = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge3 === void 0 ? void 0 : _documentDataModel$ge3.getBody();
2300
+ const block = body === null || body === void 0 || (_body$customBlocks = body.customBlocks) === null || _body$customBlocks === void 0 ? void 0 : _body$customBlocks.find((item) => item.blockId === drawingId);
2301
+ if (block) getSectionPermissionObjectIds(documentDataModel, segmentId, {
2302
+ startOffset: block.startIndex,
2303
+ endOffset: block.startIndex + 1
2304
+ }).forEach((objectId) => result.add(objectId));
2305
+ });
2306
+ return [...result];
2307
+ }
2308
+ function getDrawingIdsFromActions(actions) {
2309
+ const result = /* @__PURE__ */ new Set();
2310
+ const visit = (value) => {
2311
+ if (Array.isArray(value)) {
2312
+ value.forEach((item, index) => {
2313
+ if (item === "drawings" && typeof value[index + 1] === "string") result.add(value[index + 1]);
2314
+ visit(item);
2315
+ });
2316
+ return;
2317
+ }
2318
+ if (!isRecord$2(value)) return;
2319
+ Object.values(value).forEach(visit);
2320
+ };
2321
+ visit(actions);
2322
+ return [...result];
2323
+ }
2324
+ function isTextXAction(value) {
2325
+ if (!isRecord$2(value) || typeof value.len !== "number") return false;
2326
+ if (value.t === _univerjs_core.TextXActionType.INSERT) return isRecord$2(value.body);
2327
+ return value.t === _univerjs_core.TextXActionType.RETAIN || value.t === _univerjs_core.TextXActionType.DELETE;
2328
+ }
2329
+ function isRecord$2(value) {
2330
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2331
+ }
2332
+ function getDocumentEntityParentPermissionObjectIds(documentDataModel, segmentId, entityType, entityId) {
2333
+ const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
2334
+ const range = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex).entityRanges.get(`${entityType}:${entityId}`) ?? null;
2335
+ return range ? getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex) : [];
2336
+ }
2337
+ function getDocumentParagraphParentPermissionObjectIds(documentDataModel, segmentId, paragraphId) {
2338
+ const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
2339
+ const segmentIndex = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex);
2340
+ const { body } = segmentIndex;
2341
+ const paragraph = segmentIndex.paragraphsById.get(paragraphId);
2342
+ if (!body || !paragraph) return [];
2343
+ return getSectionPermissionObjectIds(documentDataModel, segmentId, {
2344
+ startOffset: segmentIndex.paragraphContentStartOffsets.get(paragraph.startIndex) ?? (0, _univerjs_core.getParagraphContentStartOffset)(body, paragraph),
2345
+ endOffset: paragraph.startIndex + 1
2346
+ }, resolverIndex);
2347
+ }
2348
+ function getDocumentSectionPermissionObjectIdsByIds(sectionIds) {
2349
+ return Array.from(sectionIds, (sectionId) => getDocumentSectionPermissionObjectId("", sectionId));
2350
+ }
2351
+ function getDocumentSectionIdsAtOffset(body, offset) {
2352
+ return getSectionsIntersectingRange(getTopLevelSectionBreaks(body), {
2353
+ startOffset: offset,
2354
+ endOffset: offset
2355
+ }).map((section) => section.sectionId);
2356
+ }
2357
+ function getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex = getDocumentPermissionResolverIndex(documentDataModel)) {
2358
+ if (segmentId) {
2359
+ const referenceRange = resolverIndex.footnoteReferenceRanges.get(segmentId);
2360
+ if (referenceRange) return getDocumentEditTargetObjectIds(documentDataModel, "", referenceRange);
2361
+ return getHeaderFooterOwnerSectionIds(documentDataModel, segmentId, resolverIndex.topLevelSections).map((sectionId) => getDocumentSectionPermissionObjectId("", sectionId));
2362
+ }
2363
+ return getSectionsIntersectingRange(resolverIndex.topLevelSections, range).map((section) => getDocumentSectionPermissionObjectId("", section.sectionId));
2364
+ }
2365
+ function getParagraphPermissionObjectIds(body, segmentId, range, segmentIndex) {
2366
+ const startOffset = Math.min(range.startOffset, range.endOffset);
2367
+ const endOffset = Math.max(range.startOffset, range.endOffset);
2368
+ return (body.paragraphs ?? []).filter((paragraph) => intersectsRange(segmentIndex.paragraphContentStartOffsets.get(paragraph.startIndex) ?? (0, _univerjs_core.getParagraphContentStartOffset)(body, paragraph), paragraph.startIndex + 1, startOffset, endOffset)).flatMap((paragraph) => paragraph.paragraphId ? [getDocumentParagraphPermissionObjectId(segmentId, paragraph.paragraphId)] : []);
2369
+ }
2370
+ function getEntityPermissionObjectIds(segmentId, range, segmentIndex) {
2371
+ const startOffset = Math.min(range.startOffset, range.endOffset);
2372
+ const endOffset = Math.max(range.startOffset, range.endOffset);
2373
+ return segmentIndex.entities.filter((item) => intersectsRange(item.startOffset, item.endOffset, startOffset, endOffset)).map((item) => getDocumentEntityPermissionObjectId(segmentId, item.type, item.id));
2374
+ }
2375
+ function getSectionsIntersectingRange(sections, range) {
2376
+ const startOffset = Math.min(range.startOffset, range.endOffset);
2377
+ const endOffset = Math.max(range.startOffset, range.endOffset);
2378
+ return sections.filter((section, index) => intersectsRange(index === 0 ? 0 : sections[index - 1].startIndex + 1, section.startIndex + 1, startOffset, endOffset));
2379
+ }
2380
+ function intersectsRange(targetStart, targetEnd, rangeStart, rangeEnd) {
2381
+ if (rangeStart === rangeEnd) return rangeStart >= targetStart && rangeStart < targetEnd;
2382
+ return rangeStart < targetEnd && rangeEnd > targetStart;
2383
+ }
2384
+ function getHeaderFooterOwnerSectionIds(documentDataModel, segmentId, sections = getDocumentPermissionResolverIndex(documentDataModel).topLevelSections) {
2385
+ const snapshot = documentDataModel.getSnapshot();
2386
+ const result = /* @__PURE__ */ new Set();
2387
+ sections.forEach((section, sectionIndex) => {
2388
+ if (HEADER_FOOTER_REFERENCE_KEYS.some((key) => (0, _univerjs_core.resolveSectionHeaderFooterReference)(snapshot.documentStyle, sections, sectionIndex, key).segmentId === segmentId)) result.add(section.sectionId);
2389
+ });
2390
+ return [...result];
2391
+ }
2392
+
2393
+ //#endregion
2394
+ //#region src/commands/commands/set-heading-collapsed.command.ts
2395
+ const SetHeadingCollapsedCommand = {
2396
+ id: "doc.command.set-heading-collapsed",
2397
+ type: _univerjs_core.CommandType.COMMAND,
2398
+ handler: (accessor, params) => {
2399
+ var _body$paragraphs;
2400
+ if (!params || typeof params.collapsed !== "boolean") return false;
2401
+ const doc = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(params.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
2402
+ if (!doc || doc.getDisabled() || doc.getSnapshot().documentStyle.documentFlavor !== _univerjs_core.DocumentFlavor.MODERN) return false;
2403
+ const body = doc.getBody();
2404
+ const matches = (body === null || body === void 0 || (_body$paragraphs = body.paragraphs) === null || _body$paragraphs === void 0 ? void 0 : _body$paragraphs.filter((paragraph) => paragraph.paragraphId === params.paragraphId)) ?? [];
2405
+ const paragraph = matches[0];
2406
+ if (!body || matches.length !== 1 || paragraph.headingCollapsed === true === params.collapsed) return false;
2407
+ const viewModel = new _univerjs_engine_render.DocumentViewModel(doc);
2408
+ try {
2409
+ var _selectionService$get;
2410
+ const section = viewModel.getHeadingSections().find((section) => section.paragraphId === params.paragraphId);
2411
+ if (!section || params.collapsed && section.startOffset >= section.endOffset || viewModel.getCollapsedHeadingAtOffset(paragraph.startIndex)) return false;
2412
+ const targets = getDocumentEditTargetObjectIds(doc, "", {
2413
+ startOffset: (0, _univerjs_core.getParagraphContentStartOffset)(body, paragraph),
2414
+ endOffset: paragraph.startIndex + 1
2415
+ });
2416
+ if (!canEditDocumentTargets(accessor.get(_univerjs_core.IPermissionService), params.unitId, targets)) return false;
2417
+ const textX = new _univerjs_core.TextX();
2418
+ if (paragraph.startIndex > 0) textX.push({
2419
+ t: _univerjs_core.TextXActionType.RETAIN,
2420
+ len: paragraph.startIndex
2421
+ });
2422
+ textX.push({
2423
+ t: _univerjs_core.TextXActionType.RETAIN,
2424
+ len: 1,
2425
+ coverType: _univerjs_core.UpdateDocsAttributeType.COVER,
2426
+ body: {
2427
+ dataStream: "",
2428
+ paragraphs: [{
2429
+ paragraphId: paragraph.paragraphId,
2430
+ startIndex: 0,
2431
+ headingCollapsed: params.collapsed
2432
+ }]
2433
+ }
2434
+ });
2435
+ const actions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(doc));
2436
+ const selectionService = accessor.get(DocSelectionManagerService);
2437
+ const selectionTarget = {
2438
+ unitId: params.unitId,
2439
+ subUnitId: params.unitId
2440
+ };
2441
+ const ranges = selectionService.getDocRanges(selectionTarget) ?? [];
2442
+ const hidesSelection = params.collapsed && !((_selectionService$get = selectionService.getSelectionInfo(selectionTarget)) === null || _selectionService$get === void 0 ? void 0 : _selectionService$get.segmentId) && ranges.some((range) => !range.segmentId && range.startOffset != null && range.endOffset != null && range.endOffset >= section.startOffset && range.startOffset < section.endOffset);
2443
+ return Boolean(accessor.get(_univerjs_core.ICommandService).syncExecuteCommand(RichTextEditingMutation.id, {
2444
+ unitId: params.unitId,
2445
+ actions,
2446
+ textRanges: hidesSelection ? [{
2447
+ startOffset: paragraph.startIndex,
2448
+ endOffset: paragraph.startIndex,
2449
+ collapsed: true
2450
+ }] : null,
2451
+ prevTextRanges: ranges,
2452
+ noNeedSetTextRange: !hidesSelection,
2453
+ isEditing: false,
2454
+ trigger: SetHeadingCollapsedCommand.id
2455
+ }));
2456
+ } finally {
2457
+ viewModel.dispose();
2458
+ }
2459
+ }
2460
+ };
2461
+
2128
2462
  //#endregion
2129
2463
  //#region src/commands/commands/set-section-header-footer-link.command.ts
2130
2464
  const SetSectionHeaderFooterLinkCommand = {
@@ -2425,725 +2759,461 @@ const UpdateDocumentSectionCommand = {
2425
2759
  body: {
2426
2760
  dataStream: "",
2427
2761
  sectionBreaks: [{
2428
- ..._univerjs_core.Tools.deepClone(section),
2429
- ..._univerjs_core.Tools.deepClone(updates.get(section.sectionId)),
2430
- sectionId: section.sectionId,
2431
- startIndex: 0
2432
- }]
2433
- }
2434
- });
2435
- cursor.moveCursorTo(section.startIndex + 1);
2436
- }
2437
- const jsonX = _univerjs_core.JSONX.getInstance();
2438
- const mutation = {
2439
- id: RichTextEditingMutation.id,
2440
- params: {
2441
- unitId: params.unitId,
2442
- actions: jsonX.editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(documentDataModel)),
2443
- textRanges: null,
2444
- noNeedSetTextRange: true,
2445
- debounce: true,
2446
- isEditing: false,
2447
- trigger: UpdateDocumentSectionCommand.id
2448
- }
2449
- };
2450
- return Boolean(commandService.syncExecuteCommand(mutation.id, mutation.params));
2451
- }
2452
- };
2453
- const InsertDocumentSectionBreakCommand = {
2454
- id: "doc.command.insert-section-break",
2455
- type: _univerjs_core.CommandType.COMMAND,
2456
- handler: (accessor, params) => {
2457
- var _body$sectionBreaks;
2458
- if (!params) return false;
2459
- const context = getTraditionalDocumentContext(accessor, params.unitId);
2460
- if (!context || !params.sectionId || !Number.isInteger(params.offset) || params.nextSectionType != null && !isValidEnumValue(_univerjs_core.SectionType, params.nextSectionType)) return false;
2461
- const { body, documentDataModel, commandService } = context;
2462
- const insertedSection = {
2463
- ..._univerjs_core.Tools.deepClone(params.config ?? {}),
2464
- sectionId: params.sectionId,
2465
- startIndex: params.offset
2466
- };
2467
- if (!isValidTopLevelInsertionOffset(body, params.offset) || ((_body$sectionBreaks = body.sectionBreaks) === null || _body$sectionBreaks === void 0 ? void 0 : _body$sectionBreaks.some((section) => section.sectionId === params.sectionId)) || !isValidSectionConfig(insertedSection, documentDataModel.getDocumentStyle())) return false;
2468
- const nextSection = params.nextSectionType == null ? void 0 : getTopLevelSectionBreaks(body).find((section) => section.startIndex >= params.offset);
2469
- if (params.nextSectionType != null && !nextSection) return false;
2470
- const textX = new _univerjs_core.TextX();
2471
- textX.retain(params.offset);
2472
- textX.insert(1, {
2473
- dataStream: _univerjs_core.DataStreamTreeTokenType.SECTION_BREAK,
2474
- sectionBreaks: [{
2475
- ...insertedSection,
2476
- startIndex: 0
2477
- }]
2478
- });
2479
- if (nextSection && params.nextSectionType != null) {
2480
- textX.retain(nextSection.startIndex - params.offset);
2481
- textX.push({
2482
- t: _univerjs_core.TextXActionType.RETAIN,
2483
- len: 1,
2484
- coverType: _univerjs_core.UpdateDocsAttributeType.REPLACE,
2485
- body: {
2486
- dataStream: "",
2487
- sectionBreaks: [{
2488
- ..._univerjs_core.Tools.deepClone(nextSection),
2489
- sectionType: params.nextSectionType,
2762
+ ..._univerjs_core.Tools.deepClone(section),
2763
+ ..._univerjs_core.Tools.deepClone(updates.get(section.sectionId)),
2764
+ sectionId: section.sectionId,
2490
2765
  startIndex: 0
2491
2766
  }]
2492
2767
  }
2493
2768
  });
2769
+ cursor.moveCursorTo(section.startIndex + 1);
2494
2770
  }
2495
- return executeSectionTextX(commandService, documentDataModel, textX, InsertDocumentSectionBreakCommand.id);
2496
- }
2497
- };
2498
- const InsertDocumentColumnBreakCommand = {
2499
- id: "doc.command.insert-column-break",
2500
- type: _univerjs_core.CommandType.COMMAND,
2501
- handler: (accessor, params) => {
2502
- if (!params) return false;
2503
- const context = getTraditionalDocumentContext(accessor, params.unitId);
2504
- if (!context || !isValidTopLevelInsertionOffset(context.body, params.offset)) return false;
2505
- const textX = new _univerjs_core.TextX();
2506
- textX.retain(params.offset);
2507
- textX.insert(1, {
2508
- dataStream: _univerjs_core.DataStreamTreeTokenType.COLUMN_BREAK,
2509
- customRanges: [{
2510
- startIndex: 0,
2511
- endIndex: 0,
2512
- rangeId: `docx-break-${(0, _univerjs_core.generateRandomId)()}`,
2513
- rangeType: _univerjs_core.CustomRangeType.CUSTOM,
2514
- wholeEntity: true,
2515
- properties: { breakType: _univerjs_core.DocxBreakType.COLUMN }
2516
- }]
2517
- });
2518
- return executeSectionTextX(context.commandService, context.documentDataModel, textX, InsertDocumentColumnBreakCommand.id);
2771
+ const jsonX = _univerjs_core.JSONX.getInstance();
2772
+ const mutation = {
2773
+ id: RichTextEditingMutation.id,
2774
+ params: {
2775
+ unitId: params.unitId,
2776
+ actions: jsonX.editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(documentDataModel)),
2777
+ textRanges: null,
2778
+ noNeedSetTextRange: true,
2779
+ debounce: true,
2780
+ isEditing: false,
2781
+ trigger: UpdateDocumentSectionCommand.id
2782
+ }
2783
+ };
2784
+ return Boolean(commandService.syncExecuteCommand(mutation.id, mutation.params));
2519
2785
  }
2520
2786
  };
2521
- const DeleteDocumentSectionBreakCommand = {
2522
- id: "doc.command.delete-section-break",
2787
+ const InsertDocumentSectionBreakCommand = {
2788
+ id: "doc.command.insert-section-break",
2523
2789
  type: _univerjs_core.CommandType.COMMAND,
2524
2790
  handler: (accessor, params) => {
2791
+ var _body$sectionBreaks;
2525
2792
  if (!params) return false;
2526
2793
  const context = getTraditionalDocumentContext(accessor, params.unitId);
2527
- if (!context || !params.sectionId) return false;
2528
- const sections = getTopLevelSectionBreaks(context.body);
2529
- if (sections.length <= 1) return false;
2530
- const section = sections.find((item) => item.sectionId === params.sectionId);
2531
- if (!section || section === sections[sections.length - 1]) return false;
2532
- const textX = new _univerjs_core.TextX();
2533
- textX.retain(section.startIndex);
2534
- textX.delete(1);
2535
- return executeSectionTextX(context.commandService, context.documentDataModel, textX, DeleteDocumentSectionBreakCommand.id);
2536
- }
2537
- };
2538
- function getTraditionalDocumentContext(accessor, unitId) {
2539
- if (!unitId) return null;
2540
- const documentDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
2541
- const body = documentDataModel === null || documentDataModel === void 0 ? void 0 : documentDataModel.getBody();
2542
- if (!documentDataModel || !body || documentDataModel.getDocumentStyle().documentFlavor !== _univerjs_core.DocumentFlavor.TRADITIONAL) return null;
2543
- return {
2544
- body,
2545
- documentDataModel,
2546
- commandService: accessor.get(_univerjs_core.ICommandService)
2547
- };
2548
- }
2549
- function isValidTopLevelInsertionOffset(body, offset) {
2550
- var _body$tables, _body$columnGroups, _body$blockRanges;
2551
- return Number.isInteger(offset) && offset >= 0 && offset <= body.dataStream.length && !((_body$tables = body.tables) === null || _body$tables === void 0 ? void 0 : _body$tables.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getTableRangeInterval)(range), offset))) && !((_body$columnGroups = body.columnGroups) === null || _body$columnGroups === void 0 ? void 0 : _body$columnGroups.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getColumnGroupRangeInterval)(range), offset))) && !((_body$blockRanges = body.blockRanges) === null || _body$blockRanges === void 0 ? void 0 : _body$blockRanges.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getBlockRangeInterval)(range), offset)));
2552
- }
2553
- function isValidSectionConfig(section, documentStyle) {
2554
- if (section.sectionType != null && !isValidEnumValue(_univerjs_core.SectionType, section.sectionType)) return false;
2555
- if (section.pageOrient != null && !isValidEnumValue(_univerjs_core.PageOrientType, section.pageOrient)) return false;
2556
- if (section.columnSeparatorType != null && !isValidEnumValue(_univerjs_core.ColumnSeparatorType, section.columnSeparatorType)) return false;
2557
- if (section.pageNumberStart != null && (!Number.isInteger(section.pageNumberStart) || section.pageNumberStart < 1)) return false;
2558
- if (section.pageSize && (!isPositiveFinite(section.pageSize.width) || !isPositiveFinite(section.pageSize.height))) return false;
2559
- if ([
2560
- section.marginTop,
2561
- section.marginBottom,
2562
- section.marginLeft,
2563
- section.marginRight
2564
- ].some((margin) => margin != null && (!Number.isFinite(margin) || margin < 0))) return false;
2565
- const pageSize = section.pageSize ?? documentStyle.pageSize;
2566
- const marginTop = section.marginTop ?? documentStyle.marginTop ?? 0;
2567
- const marginBottom = section.marginBottom ?? documentStyle.marginBottom ?? 0;
2568
- const marginLeft = section.marginLeft ?? documentStyle.marginLeft ?? 0;
2569
- const marginRight = section.marginRight ?? documentStyle.marginRight ?? 0;
2570
- if ((pageSize === null || pageSize === void 0 ? void 0 : pageSize.width) != null && marginLeft + marginRight >= pageSize.width || (pageSize === null || pageSize === void 0 ? void 0 : pageSize.height) != null && marginTop + marginBottom >= pageSize.height) return false;
2571
- const columns = section.columnProperties ?? [];
2572
- if (columns.some(({ width, paddingEnd }) => !Number.isFinite(width) || !Number.isFinite(paddingEnd) || width < 0 || paddingEnd < 0)) return false;
2573
- return columns.reduce((sum, { width, paddingEnd }) => sum + width + paddingEnd, 0) <= getSectionContentWidth(documentStyle, section);
2574
- }
2575
- function isPositiveFinite(value) {
2576
- return value != null && Number.isFinite(value) && value > 0;
2577
- }
2578
- function isValidEnumValue(enumObject, value) {
2579
- return Object.values(enumObject).includes(value);
2580
- }
2581
- function executeSectionTextX(commandService, documentDataModel, textX, trigger) {
2582
- const actions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(documentDataModel));
2583
- return Boolean(commandService.syncExecuteCommand(RichTextEditingMutation.id, {
2584
- unitId: documentDataModel.getUnitId(),
2585
- actions,
2586
- textRanges: null,
2587
- noNeedSetTextRange: true,
2588
- debounce: true,
2589
- isEditing: false,
2590
- trigger
2591
- }));
2592
- }
2593
-
2594
- //#endregion
2595
- //#region src/services/permission/document-permission-rule.model.ts
2596
- /**
2597
- * Copyright 2023-present DreamNum Co., Ltd.
2598
- *
2599
- * Licensed under the Apache License, Version 2.0 (the "License");
2600
- * you may not use this file except in compliance with the License.
2601
- * You may obtain a copy of the License at
2602
- *
2603
- * http://www.apache.org/licenses/LICENSE-2.0
2604
- *
2605
- * Unless required by applicable law or agreed to in writing, software
2606
- * distributed under the License is distributed on an "AS IS" BASIS,
2607
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2608
- * See the License for the specific language governing permissions and
2609
- * limitations under the License.
2610
- */
2611
- let DocumentPermissionRuleModel = class DocumentPermissionRuleModel extends _univerjs_core.ObjectPermissionRuleModel {
2612
- constructor(resources) {
2613
- super(resources, "DOC_OBJECT_PERMISSION_PLUGIN", _univerjs_core.UniverInstanceType.UNIVER_DOC, [
2614
- _univerjs_protocol.UnitObject.DocumentSection,
2615
- _univerjs_protocol.UnitObject.DocumentParagraph,
2616
- _univerjs_protocol.UnitObject.DocumentEntity
2617
- ]);
2618
- }
2619
- };
2620
- DocumentPermissionRuleModel = __decorate([__decorateParam(0, _univerjs_core.IResourceManagerService)], DocumentPermissionRuleModel);
2621
-
2622
- //#endregion
2623
- //#region src/commands/mutations/set-document-permission-rule.mutation.ts
2624
- const SetDocumentPermissionRuleMutation = {
2625
- id: "doc.mutation.set-permission-rule",
2626
- type: _univerjs_core.CommandType.MUTATION,
2627
- handler: (accessor, params) => {
2628
- if (!params || !accessor.get(_univerjs_core.IUniverInstanceService).getUnit(params.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC)) return false;
2629
- return accessor.get(DocumentPermissionRuleModel).setRule(params.unitId, params.objectType, params.objectId, params.rule);
2630
- }
2631
- };
2632
-
2633
- //#endregion
2634
- //#region src/commands/mutations/set-document-permission-rules.mutation.ts
2635
- const SetDocumentPermissionRulesMutation = {
2636
- id: "doc.mutation.set-permission-rules",
2637
- type: _univerjs_core.CommandType.MUTATION,
2638
- handler: (accessor, params) => {
2639
- if (!params || !accessor.get(_univerjs_core.IUniverInstanceService).getUnit(params.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC)) return false;
2640
- return accessor.get(DocumentPermissionRuleModel).setRules(params.unitId, params.rules);
2641
- }
2642
- };
2643
-
2644
- //#endregion
2645
- //#region src/embed-host-anchor.ts
2646
- const EMBED_DOCS_CUSTOM_BLOCK_DEFAULT_COMPONENT_KEY = "UniverEmbedDocsCustomBlock";
2647
- const DEFAULT_CUSTOM_BLOCK_SIZE = {
2648
- width: 720,
2649
- height: 360
2650
- };
2651
- const SHEET_LIKE_CUSTOM_BLOCK_SIZE = {
2652
- width: 960,
2653
- height: 480
2654
- };
2655
- const SLIDE_CUSTOM_BLOCK_SIZE = {
2656
- width: 720,
2657
- height: 405
2658
- };
2659
- function createDocsCustomBlockInsertMutation(params) {
2660
- return createRichTextMutation(params.unitId, params.segmentId, createInsertCustomBlockActions(params));
2661
- }
2662
- function createDocsCustomBlockRemoveMutation(params) {
2663
- return createRichTextMutation(params.unitId, params.segmentId, createRemoveCustomBlockActions(params));
2664
- }
2665
- function createInsertCustomBlockActions(params) {
2666
- const textX = new _univerjs_core.TextX();
2667
- if (params.startIndex > 0) textX.push({
2668
- t: _univerjs_core.TextXActionType.RETAIN,
2669
- len: params.startIndex
2670
- });
2671
- textX.push({
2672
- t: _univerjs_core.TextXActionType.INSERT,
2673
- body: {
2674
- dataStream: "\b",
2675
- customBlocks: [{
2676
- startIndex: 0,
2677
- blockId: params.blockId
2678
- }]
2679
- },
2680
- len: 1
2681
- });
2682
- return composeActions([toBodyEditActions(textX, params.segmentId), createDrawingInsertActions(params)]);
2683
- }
2684
- function createRemoveCustomBlockActions(params) {
2685
- const textX = new _univerjs_core.TextX();
2686
- if (params.startIndex > 0) textX.push({
2687
- t: _univerjs_core.TextXActionType.RETAIN,
2688
- len: params.startIndex
2689
- });
2690
- textX.push({
2691
- t: _univerjs_core.TextXActionType.DELETE,
2692
- len: 1
2693
- });
2694
- return composeActions([toBodyEditActions(textX, params.segmentId), createDrawingRemoveActions(params)]);
2695
- }
2696
- function createDocsCustomBlockDrawing(params) {
2697
- const size = resolveDocsCustomBlockSize(params.childType);
2698
- const isInline = params.interactionMode === "inline";
2699
- return {
2700
- unitId: params.unitId,
2701
- subUnitId: params.unitId,
2702
- drawingId: params.blockId,
2703
- drawingType: _univerjs_core.DrawingTypeEnum.DRAWING_DOM,
2704
- componentKey: params.componentKey ?? "UniverEmbedDocsCustomBlock",
2705
- data: createEmbedDocsCustomBlockData(params),
2706
- title: params.blockId,
2707
- description: "Univer embedded unit custom block",
2708
- layoutType: isInline ? _univerjs_core.PositionedObjectLayoutType.INLINE : _univerjs_core.PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM,
2709
- allowTransform: false,
2710
- docTransform: {
2711
- size: {
2712
- width: size.width,
2713
- height: size.height
2714
- },
2715
- positionH: {
2716
- relativeFrom: isInline ? _univerjs_core.ObjectRelativeFromH.PAGE : _univerjs_core.ObjectRelativeFromH.COLUMN,
2717
- ...isInline ? { posOffset: 0 } : { align: _univerjs_core.AlignTypeH.LEFT }
2718
- },
2719
- positionV: {
2720
- relativeFrom: isInline ? _univerjs_core.ObjectRelativeFromV.PAGE : _univerjs_core.ObjectRelativeFromV.PARAGRAPH,
2721
- posOffset: 0
2722
- },
2723
- angle: 0
2724
- },
2725
- transform: {
2726
- left: 0,
2727
- top: 0,
2728
- width: size.width,
2729
- height: size.height
2794
+ if (!context || !params.sectionId || !Number.isInteger(params.offset) || params.nextSectionType != null && !isValidEnumValue(_univerjs_core.SectionType, params.nextSectionType)) return false;
2795
+ const { body, documentDataModel, commandService } = context;
2796
+ const insertedSection = {
2797
+ ..._univerjs_core.Tools.deepClone(params.config ?? {}),
2798
+ sectionId: params.sectionId,
2799
+ startIndex: params.offset
2800
+ };
2801
+ if (!isValidTopLevelInsertionOffset(body, params.offset) || ((_body$sectionBreaks = body.sectionBreaks) === null || _body$sectionBreaks === void 0 ? void 0 : _body$sectionBreaks.some((section) => section.sectionId === params.sectionId)) || !isValidSectionConfig(insertedSection, documentDataModel.getDocumentStyle())) return false;
2802
+ const nextSection = params.nextSectionType == null ? void 0 : getTopLevelSectionBreaks(body).find((section) => section.startIndex >= params.offset);
2803
+ if (params.nextSectionType != null && !nextSection) return false;
2804
+ const textX = new _univerjs_core.TextX();
2805
+ textX.retain(params.offset);
2806
+ textX.insert(1, {
2807
+ dataStream: _univerjs_core.DataStreamTreeTokenType.SECTION_BREAK,
2808
+ sectionBreaks: [{
2809
+ ...insertedSection,
2810
+ startIndex: 0
2811
+ }]
2812
+ });
2813
+ if (nextSection && params.nextSectionType != null) {
2814
+ textX.retain(nextSection.startIndex - params.offset);
2815
+ textX.push({
2816
+ t: _univerjs_core.TextXActionType.RETAIN,
2817
+ len: 1,
2818
+ coverType: _univerjs_core.UpdateDocsAttributeType.REPLACE,
2819
+ body: {
2820
+ dataStream: "",
2821
+ sectionBreaks: [{
2822
+ ..._univerjs_core.Tools.deepClone(nextSection),
2823
+ sectionType: params.nextSectionType,
2824
+ startIndex: 0
2825
+ }]
2826
+ }
2827
+ });
2730
2828
  }
2731
- };
2732
- }
2733
- function resolveDocsCustomBlockSize(childType) {
2734
- if (childType === _univerjs_core.UniverInstanceType.UNIVER_SHEET || childType === _univerjs_core.UniverInstanceType.UNIVER_BASE) return SHEET_LIKE_CUSTOM_BLOCK_SIZE;
2735
- if (childType === _univerjs_core.UniverInstanceType.UNIVER_SLIDE) return SLIDE_CUSTOM_BLOCK_SIZE;
2736
- return DEFAULT_CUSTOM_BLOCK_SIZE;
2737
- }
2738
- function isSheetLikeDocsCustomBlockChildType(childType) {
2739
- return childType === _univerjs_core.UniverInstanceType.UNIVER_SHEET || childType === _univerjs_core.UniverInstanceType.UNIVER_BASE;
2740
- }
2741
- function createEmbedDocsCustomBlockData(params) {
2742
- return {
2743
- version: 1,
2744
- embedId: params.embedId ?? params.blockId,
2745
- hostUnitId: params.unitId,
2746
- hostAnchorId: params.blockId,
2747
- childUnitId: params.childUnitId,
2748
- childType: params.childType,
2749
- interactionMode: params.interactionMode ?? "block"
2750
- };
2751
- }
2752
- function isEmbedDocsCustomBlockData(data) {
2753
- if (!data || typeof data !== "object") return false;
2754
- const candidate = data;
2755
- return candidate.version === 1 && typeof candidate.embedId === "string" && typeof candidate.hostAnchorId === "string";
2756
- }
2757
- function shouldUseInlineTextSelectionForDocsCustomBlockDrawing(drawing) {
2758
- const data = drawing && typeof drawing === "object" ? drawing.data : void 0;
2759
- if (!isEmbedDocsCustomBlockData(data)) return true;
2760
- return data.interactionMode === "inline";
2761
- }
2762
- function createRichTextMutation(unitId, segmentId, actions) {
2829
+ return executeSectionTextX(commandService, documentDataModel, textX, InsertDocumentSectionBreakCommand.id);
2830
+ }
2831
+ };
2832
+ const InsertDocumentColumnBreakCommand = {
2833
+ id: "doc.command.insert-column-break",
2834
+ type: _univerjs_core.CommandType.COMMAND,
2835
+ handler: (accessor, params) => {
2836
+ if (!params) return false;
2837
+ const context = getTraditionalDocumentContext(accessor, params.unitId);
2838
+ if (!context || !isValidTopLevelInsertionOffset(context.body, params.offset)) return false;
2839
+ const textX = new _univerjs_core.TextX();
2840
+ textX.retain(params.offset);
2841
+ textX.insert(1, {
2842
+ dataStream: _univerjs_core.DataStreamTreeTokenType.COLUMN_BREAK,
2843
+ customRanges: [{
2844
+ startIndex: 0,
2845
+ endIndex: 0,
2846
+ rangeId: `docx-break-${(0, _univerjs_core.generateRandomId)()}`,
2847
+ rangeType: _univerjs_core.CustomRangeType.CUSTOM,
2848
+ wholeEntity: true,
2849
+ properties: { breakType: _univerjs_core.DocxBreakType.COLUMN }
2850
+ }]
2851
+ });
2852
+ return executeSectionTextX(context.commandService, context.documentDataModel, textX, InsertDocumentColumnBreakCommand.id);
2853
+ }
2854
+ };
2855
+ const DeleteDocumentSectionBreakCommand = {
2856
+ id: "doc.command.delete-section-break",
2857
+ type: _univerjs_core.CommandType.COMMAND,
2858
+ handler: (accessor, params) => {
2859
+ if (!params) return false;
2860
+ const context = getTraditionalDocumentContext(accessor, params.unitId);
2861
+ if (!context || !params.sectionId) return false;
2862
+ const sections = getTopLevelSectionBreaks(context.body);
2863
+ if (sections.length <= 1) return false;
2864
+ const section = sections.find((item) => item.sectionId === params.sectionId);
2865
+ if (!section || section === sections[sections.length - 1]) return false;
2866
+ const textX = new _univerjs_core.TextX();
2867
+ textX.retain(section.startIndex);
2868
+ textX.delete(1);
2869
+ return executeSectionTextX(context.commandService, context.documentDataModel, textX, DeleteDocumentSectionBreakCommand.id);
2870
+ }
2871
+ };
2872
+ function getTraditionalDocumentContext(accessor, unitId) {
2873
+ if (!unitId) return null;
2874
+ const documentDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
2875
+ const body = documentDataModel === null || documentDataModel === void 0 ? void 0 : documentDataModel.getBody();
2876
+ if (!documentDataModel || !body || documentDataModel.getDocumentStyle().documentFlavor !== _univerjs_core.DocumentFlavor.TRADITIONAL) return null;
2763
2877
  return {
2764
- id: RichTextEditingMutation.id,
2765
- params: {
2766
- unitId,
2767
- segmentId,
2768
- actions,
2769
- textRanges: [],
2770
- isEditing: false,
2771
- noNeedSetTextRange: true
2772
- }
2878
+ body,
2879
+ documentDataModel,
2880
+ commandService: accessor.get(_univerjs_core.ICommandService)
2773
2881
  };
2774
2882
  }
2775
- function toBodyEditActions(textX, segmentId) {
2776
- return _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), segmentId ? [
2777
- "headers",
2778
- segmentId,
2779
- "body"
2780
- ] : ["body"]) ?? [];
2883
+ function isValidTopLevelInsertionOffset(body, offset) {
2884
+ var _body$tables, _body$columnGroups, _body$blockRanges;
2885
+ return Number.isInteger(offset) && offset >= 0 && offset <= body.dataStream.length && !((_body$tables = body.tables) === null || _body$tables === void 0 ? void 0 : _body$tables.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getTableRangeInterval)(range), offset))) && !((_body$columnGroups = body.columnGroups) === null || _body$columnGroups === void 0 ? void 0 : _body$columnGroups.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getColumnGroupRangeInterval)(range), offset))) && !((_body$blockRanges = body.blockRanges) === null || _body$blockRanges === void 0 ? void 0 : _body$blockRanges.some((range) => (0, _univerjs_core.containsInteriorInsertionOffset)((0, _univerjs_core.getBlockRangeInterval)(range), offset)));
2781
2886
  }
2782
- function createDrawingInsertActions(params) {
2783
- if (params.segmentId) return [];
2784
- const jsonX = _univerjs_core.JSONX.getInstance();
2785
- const drawing = createDocsCustomBlockDrawing(params);
2786
- return composeActions([jsonX.insertOp(["drawings", params.blockId], drawing) ?? [], jsonX.insertOp(["drawingsOrder", params.drawingOrderIndex ?? 0], params.blockId) ?? []]);
2887
+ function isValidSectionConfig(section, documentStyle) {
2888
+ if (section.sectionType != null && !isValidEnumValue(_univerjs_core.SectionType, section.sectionType)) return false;
2889
+ if (section.pageOrient != null && !isValidEnumValue(_univerjs_core.PageOrientType, section.pageOrient)) return false;
2890
+ if (section.columnSeparatorType != null && !isValidEnumValue(_univerjs_core.ColumnSeparatorType, section.columnSeparatorType)) return false;
2891
+ if (section.pageNumberStart != null && (!Number.isInteger(section.pageNumberStart) || section.pageNumberStart < 1)) return false;
2892
+ if (section.pageSize && (!isPositiveFinite(section.pageSize.width) || !isPositiveFinite(section.pageSize.height))) return false;
2893
+ if ([
2894
+ section.marginTop,
2895
+ section.marginBottom,
2896
+ section.marginLeft,
2897
+ section.marginRight
2898
+ ].some((margin) => margin != null && (!Number.isFinite(margin) || margin < 0))) return false;
2899
+ const pageSize = section.pageSize ?? documentStyle.pageSize;
2900
+ const marginTop = section.marginTop ?? documentStyle.marginTop ?? 0;
2901
+ const marginBottom = section.marginBottom ?? documentStyle.marginBottom ?? 0;
2902
+ const marginLeft = section.marginLeft ?? documentStyle.marginLeft ?? 0;
2903
+ const marginRight = section.marginRight ?? documentStyle.marginRight ?? 0;
2904
+ if ((pageSize === null || pageSize === void 0 ? void 0 : pageSize.width) != null && marginLeft + marginRight >= pageSize.width || (pageSize === null || pageSize === void 0 ? void 0 : pageSize.height) != null && marginTop + marginBottom >= pageSize.height) return false;
2905
+ const columns = section.columnProperties ?? [];
2906
+ if (columns.some(({ width, paddingEnd }) => !Number.isFinite(width) || !Number.isFinite(paddingEnd) || width < 0 || paddingEnd < 0)) return false;
2907
+ return columns.reduce((sum, { width, paddingEnd }) => sum + width + paddingEnd, 0) <= getSectionContentWidth(documentStyle, section);
2787
2908
  }
2788
- function createDrawingRemoveActions(params) {
2789
- if (params.segmentId) return [];
2790
- const jsonX = _univerjs_core.JSONX.getInstance();
2791
- const drawing = createDocsCustomBlockDrawing(params);
2792
- return composeActions([jsonX.removeOp(["drawings", params.blockId], drawing) ?? [], jsonX.removeOp(["drawingsOrder", params.drawingOrderIndex ?? 0], params.blockId) ?? []]);
2909
+ function isPositiveFinite(value) {
2910
+ return value != null && Number.isFinite(value) && value > 0;
2793
2911
  }
2794
- function composeActions(actions) {
2795
- return actions.reduce((composed, action) => {
2796
- if (!action || _univerjs_core.JSONX.isNoop(action) || action.length === 0) return composed;
2797
- if (!composed || _univerjs_core.JSONX.isNoop(composed) || composed.length === 0) return action;
2798
- return _univerjs_core.JSONX.compose(composed, action) ?? [];
2799
- }, []);
2912
+ function isValidEnumValue(enumObject, value) {
2913
+ return Object.values(enumObject).includes(value);
2800
2914
  }
2801
-
2802
- //#endregion
2803
- //#region package.json
2804
- var name = "@univerjs/docs";
2805
- var version = "1.1.0";
2806
-
2807
- //#endregion
2808
- //#region src/config/config.ts
2809
- /**
2810
- * Copyright 2023-present DreamNum Co., Ltd.
2811
- *
2812
- * Licensed under the Apache License, Version 2.0 (the "License");
2813
- * you may not use this file except in compliance with the License.
2814
- * You may obtain a copy of the License at
2815
- *
2816
- * http://www.apache.org/licenses/LICENSE-2.0
2817
- *
2818
- * Unless required by applicable law or agreed to in writing, software
2819
- * distributed under the License is distributed on an "AS IS" BASIS,
2820
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2821
- * See the License for the specific language governing permissions and
2822
- * limitations under the License.
2823
- */
2824
- const DOCS_PLUGIN_CONFIG_KEY = "docs.config";
2825
- const configSymbol$1 = Symbol(DOCS_PLUGIN_CONFIG_KEY);
2826
- const defaultPluginConfig = {};
2827
-
2828
- //#endregion
2829
- //#region src/controllers/custom-range.controller.ts
2830
- let DocCustomRangeController = class DocCustomRangeController extends _univerjs_core.Disposable {
2831
- constructor(_commandService, _textSelectionManagerService, _univerInstanceService) {
2832
- super();
2833
- this._commandService = _commandService;
2834
- this._textSelectionManagerService = _textSelectionManagerService;
2835
- this._univerInstanceService = _univerInstanceService;
2836
- this._initSelectionChange();
2837
- }
2838
- _transformCustomRange(doc, selection, segmentId) {
2839
- var _doc$getSelfOrHeaderF;
2840
- const { startOffset, endOffset, collapsed } = selection;
2841
- const customRanges = (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(selection.segmentId ?? segmentId)) === null || _doc$getSelfOrHeaderF === void 0 || (_doc$getSelfOrHeaderF = _doc$getSelfOrHeaderF.getBody()) === null || _doc$getSelfOrHeaderF === void 0 || (_doc$getSelfOrHeaderF = _doc$getSelfOrHeaderF.customRanges) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.filter((range) => {
2842
- const selectsFieldResult = range.rangeType === _univerjs_core.CustomRangeType.FIELD && !collapsed && startOffset <= range.startIndex + 1 && endOffset >= range.endIndex;
2843
- if (!range.wholeEntity && !selectsFieldResult) return false;
2844
- if (startOffset <= range.startIndex && endOffset > range.endIndex) return false;
2845
- if (collapsed) return range.startIndex < startOffset && range.endIndex >= endOffset;
2846
- return _univerjs_core.BuildTextUtils.range.isIntersects(startOffset, endOffset - 1, range.startIndex, range.endIndex);
2847
- });
2848
- if (customRanges === null || customRanges === void 0 ? void 0 : customRanges.length) {
2849
- let start = startOffset;
2850
- let end = endOffset;
2851
- customRanges.forEach((range) => {
2852
- start = Math.min(range.startIndex, start);
2853
- end = Math.max(range.endIndex + 1, end);
2854
- });
2855
- return {
2856
- ...selection,
2857
- startOffset: start,
2858
- endOffset: end,
2859
- collapsed: start === end
2860
- };
2861
- }
2862
- return selection;
2863
- }
2864
- _initSelectionChange() {
2865
- this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
2866
- if (commandInfo.id === SetTextSelectionsOperation.id) {
2867
- const params = commandInfo.params;
2868
- const { unitId, ranges, isEditing } = params;
2869
- const doc = this._univerInstanceService.getUnit(unitId);
2870
- if (!doc) return;
2871
- const transformedRanges = ranges.map((range) => this._transformCustomRange(doc, range, params.segmentId));
2872
- if (transformedRanges.some((range, i) => ranges[i] !== range)) this._textSelectionManagerService.replaceDocRanges(transformedRanges, void 0, isEditing);
2873
- }
2874
- }));
2915
+ function executeSectionTextX(commandService, documentDataModel, textX, trigger) {
2916
+ const actions = _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), (0, _univerjs_core.getRichTextEditPath)(documentDataModel));
2917
+ return Boolean(commandService.syncExecuteCommand(RichTextEditingMutation.id, {
2918
+ unitId: documentDataModel.getUnitId(),
2919
+ actions,
2920
+ textRanges: null,
2921
+ noNeedSetTextRange: true,
2922
+ debounce: true,
2923
+ isEditing: false,
2924
+ trigger
2925
+ }));
2926
+ }
2927
+
2928
+ //#endregion
2929
+ //#region src/services/permission/document-permission-rule.model.ts
2930
+ /**
2931
+ * Copyright 2023-present DreamNum Co., Ltd.
2932
+ *
2933
+ * Licensed under the Apache License, Version 2.0 (the "License");
2934
+ * you may not use this file except in compliance with the License.
2935
+ * You may obtain a copy of the License at
2936
+ *
2937
+ * http://www.apache.org/licenses/LICENSE-2.0
2938
+ *
2939
+ * Unless required by applicable law or agreed to in writing, software
2940
+ * distributed under the License is distributed on an "AS IS" BASIS,
2941
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2942
+ * See the License for the specific language governing permissions and
2943
+ * limitations under the License.
2944
+ */
2945
+ let DocumentPermissionRuleModel = class DocumentPermissionRuleModel extends _univerjs_core.ObjectPermissionRuleModel {
2946
+ constructor(resources) {
2947
+ super(resources, "DOC_OBJECT_PERMISSION_PLUGIN", _univerjs_core.UniverInstanceType.UNIVER_DOC, [
2948
+ _univerjs_protocol.UnitObject.DocumentSection,
2949
+ _univerjs_protocol.UnitObject.DocumentParagraph,
2950
+ _univerjs_protocol.UnitObject.DocumentEntity
2951
+ ]);
2875
2952
  }
2876
2953
  };
2877
- DocCustomRangeController = __decorate([
2878
- __decorateParam(0, _univerjs_core.ICommandService),
2879
- __decorateParam(1, (0, _univerjs_core.Inject)(DocSelectionManagerService)),
2880
- __decorateParam(2, _univerjs_core.IUniverInstanceService)
2881
- ], DocCustomRangeController);
2954
+ DocumentPermissionRuleModel = __decorate([__decorateParam(0, _univerjs_core.IResourceManagerService)], DocumentPermissionRuleModel);
2882
2955
 
2883
2956
  //#endregion
2884
- //#region src/services/permission/document-permission-resolver.ts
2885
- const HEADER_FOOTER_REFERENCE_KEYS = [
2886
- "defaultHeaderId",
2887
- "defaultFooterId",
2888
- "firstPageHeaderId",
2889
- "firstPageFooterId",
2890
- "evenPageHeaderId",
2891
- "evenPageFooterId"
2892
- ];
2893
- const documentPermissionResolverIndexCache = /* @__PURE__ */ new WeakMap();
2894
- function getDocumentPermissionResolverIndex(documentDataModel) {
2895
- var _getMutationRevision, _snapshot$body;
2896
- const mutationRevision = ((_getMutationRevision = documentDataModel.getMutationRevision) === null || _getMutationRevision === void 0 ? void 0 : _getMutationRevision.call(documentDataModel)) ?? NaN;
2897
- const cached = documentPermissionResolverIndexCache.get(documentDataModel);
2898
- if ((cached === null || cached === void 0 ? void 0 : cached.mutationRevision) === mutationRevision) return cached;
2899
- const snapshot = documentDataModel.getSnapshot();
2900
- const topLevelSections = snapshot.body ? getTopLevelSectionBreaks(snapshot.body) : [];
2901
- const drawingSegmentIds = /* @__PURE__ */ new Map();
2902
- const addDrawingSegments = (body, segmentId) => {
2903
- for (const block of (body === null || body === void 0 ? void 0 : body.customBlocks) ?? []) if (!drawingSegmentIds.has(block.blockId)) drawingSegmentIds.set(block.blockId, segmentId);
2904
- };
2905
- addDrawingSegments(snapshot.body, "");
2906
- Object.entries(snapshot.headers ?? {}).forEach(([segmentId, header]) => addDrawingSegments(header.body, segmentId));
2907
- Object.entries(snapshot.footers ?? {}).forEach(([segmentId, footer]) => addDrawingSegments(footer.body, segmentId));
2908
- Object.entries(snapshot.notes ?? {}).forEach(([segmentId, footnote]) => addDrawingSegments(footnote.body, segmentId));
2909
- const footnoteReferenceRanges = /* @__PURE__ */ new Map();
2910
- for (const reference of ((_snapshot$body = snapshot.body) === null || _snapshot$body === void 0 ? void 0 : _snapshot$body.customRanges) ?? []) {
2911
- var _reference$properties;
2912
- if ((reference.rangeType === _univerjs_core.CustomRangeType.FOOTNOTE || reference.rangeType === _univerjs_core.CustomRangeType.ENDNOTE) && typeof ((_reference$properties = reference.properties) === null || _reference$properties === void 0 ? void 0 : _reference$properties.noteId) === "string") footnoteReferenceRanges.set(reference.properties.noteId, {
2913
- startOffset: reference.startIndex,
2914
- endOffset: reference.endIndex + 1
2915
- });
2957
+ //#region src/commands/mutations/set-document-permission-rule.mutation.ts
2958
+ const SetDocumentPermissionRuleMutation = {
2959
+ id: "doc.mutation.set-permission-rule",
2960
+ type: _univerjs_core.CommandType.MUTATION,
2961
+ handler: (accessor, params) => {
2962
+ if (!params || !accessor.get(_univerjs_core.IUniverInstanceService).getUnit(params.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC)) return false;
2963
+ return accessor.get(DocumentPermissionRuleModel).setRule(params.unitId, params.objectType, params.objectId, params.rule);
2916
2964
  }
2917
- const index = {
2918
- drawingSegmentIds,
2919
- footnoteReferenceRanges,
2920
- mutationRevision,
2921
- segments: /* @__PURE__ */ new Map(),
2922
- topLevelSections
2923
- };
2924
- documentPermissionResolverIndexCache.set(documentDataModel, index);
2925
- return index;
2926
- }
2927
- function getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex = getDocumentPermissionResolverIndex(documentDataModel)) {
2928
- var _documentDataModel$ge, _snapshot$notes;
2929
- const cached = resolverIndex.segments.get(segmentId);
2930
- if (cached) return cached;
2931
- const body = ((_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody()) ?? null;
2932
- const snapshot = documentDataModel.getSnapshot();
2933
- const note = (_snapshot$notes = snapshot.notes) === null || _snapshot$notes === void 0 ? void 0 : _snapshot$notes[segmentId];
2934
- const drawings = (note ? note.drawings : snapshot.drawings) ?? {};
2935
- const entities = body == null ? [] : [
2936
- ...(body.tables ?? []).map((item) => ({
2937
- type: "table",
2938
- id: item.tableId,
2939
- startOffset: item.startIndex,
2940
- endOffset: item.endIndex
2941
- })),
2942
- ...(body.customBlocks ?? []).map((item) => ({
2943
- type: drawings[item.blockId] ? "drawing" : "custom-block",
2944
- id: item.blockId,
2945
- startOffset: item.startIndex,
2946
- endOffset: item.startIndex + 1
2947
- })),
2948
- ...(body.blockRanges ?? []).map((item) => ({
2949
- type: "block-range",
2950
- id: item.blockId,
2951
- startOffset: item.startIndex,
2952
- endOffset: item.endIndex + 1
2953
- })),
2954
- ...(body.customRanges ?? []).map((item) => ({
2955
- type: "custom-range",
2956
- id: item.rangeId,
2957
- startOffset: item.startIndex,
2958
- endOffset: item.endIndex + 1
2959
- })),
2960
- ...(body.columnGroups ?? []).map((item) => ({
2961
- type: "column-group",
2962
- id: item.columnGroupId,
2963
- startOffset: item.startIndex,
2964
- endOffset: item.endIndex + 1
2965
- }))
2966
- ];
2967
- const entityRanges = new Map(entities.map((entity) => [`${entity.type}:${entity.id}`, {
2968
- startOffset: entity.startOffset,
2969
- endOffset: entity.endOffset
2970
- }]));
2971
- for (const block of (body === null || body === void 0 ? void 0 : body.customBlocks) ?? []) {
2972
- const range = {
2973
- startOffset: block.startIndex,
2974
- endOffset: block.startIndex + 1
2975
- };
2976
- entityRanges.set(`custom-block:${block.blockId}`, range);
2977
- entityRanges.set(`drawing:${block.blockId}`, range);
2965
+ };
2966
+
2967
+ //#endregion
2968
+ //#region src/commands/mutations/set-document-permission-rules.mutation.ts
2969
+ const SetDocumentPermissionRulesMutation = {
2970
+ id: "doc.mutation.set-permission-rules",
2971
+ type: _univerjs_core.CommandType.MUTATION,
2972
+ handler: (accessor, params) => {
2973
+ if (!params || !accessor.get(_univerjs_core.IUniverInstanceService).getUnit(params.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC)) return false;
2974
+ return accessor.get(DocumentPermissionRuleModel).setRules(params.unitId, params.rules);
2978
2975
  }
2979
- const segmentIndex = {
2980
- body,
2981
- entities,
2982
- entityRanges,
2983
- paragraphContentStartOffsets: body == null ? /* @__PURE__ */ new Map() : (0, _univerjs_core.getParagraphContentStartOffsets)(body),
2984
- paragraphsById: new Map(((body === null || body === void 0 ? void 0 : body.paragraphs) ?? []).filter((paragraph) => paragraph.paragraphId != null).map((paragraph) => [paragraph.paragraphId, paragraph]))
2985
- };
2986
- resolverIndex.segments.set(segmentId, segmentIndex);
2987
- return segmentIndex;
2988
- }
2989
- function getDocumentDrawingSegmentId(documentDataModel, drawingId) {
2990
- return getDocumentPermissionResolverIndex(documentDataModel).drawingSegmentIds.get(drawingId) ?? "";
2976
+ };
2977
+
2978
+ //#endregion
2979
+ //#region src/embed-host-anchor.ts
2980
+ const EMBED_DOCS_CUSTOM_BLOCK_DEFAULT_COMPONENT_KEY = "UniverEmbedDocsCustomBlock";
2981
+ const DEFAULT_CUSTOM_BLOCK_SIZE = {
2982
+ width: 720,
2983
+ height: 360
2984
+ };
2985
+ const SHEET_LIKE_CUSTOM_BLOCK_SIZE = {
2986
+ width: 960,
2987
+ height: 480
2988
+ };
2989
+ const SLIDE_CUSTOM_BLOCK_SIZE = {
2990
+ width: 720,
2991
+ height: 405
2992
+ };
2993
+ function createDocsCustomBlockInsertMutation(params) {
2994
+ return createRichTextMutation(params.unitId, params.segmentId, createInsertCustomBlockActions(params));
2991
2995
  }
2992
- function getDocumentEditTargetObjectIds(documentDataModel, segmentId, range) {
2993
- const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
2994
- const segmentIndex = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex);
2995
- const { body } = segmentIndex;
2996
- if (!body) return [];
2997
- return [
2998
- ...getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex),
2999
- ...getParagraphPermissionObjectIds(body, segmentId, range, segmentIndex),
3000
- ...getEntityPermissionObjectIds(segmentId, range, segmentIndex)
3001
- ];
2996
+ function createDocsCustomBlockRemoveMutation(params) {
2997
+ return createRichTextMutation(params.unitId, params.segmentId, createRemoveCustomBlockActions(params));
3002
2998
  }
3003
- function getDocumentEditTargetObjectIdsFromActions(documentDataModel, segmentId, actions) {
3004
- const result = /* @__PURE__ */ new Set();
3005
- if (actions == null) return [];
3006
- const cursor = _univerjs_core.JSON1.type.readCursor(actions);
3007
- cursor.traverse(null, () => {
3008
- const path = cursor.getPath();
3009
- const edit = cursor.getComponent();
3010
- const scopedSegment = [
3011
- "headers",
3012
- "footers",
3013
- "notes"
3014
- ].includes(String(path[0])) && typeof path[1] === "string" ? path[1] : segmentId;
3015
- if (path[0] === "notes" && typeof path[1] === "string") getSectionPermissionObjectIds(documentDataModel, path[1], {
3016
- startOffset: 0,
3017
- endOffset: 0
3018
- }).forEach((objectId) => result.add(objectId));
3019
- if (path[0] === "noteSettings") getDocumentPermissionResolverIndex(documentDataModel).topLevelSections.forEach((section) => {
3020
- result.add(getDocumentSectionPermissionObjectId("", section.sectionId));
3021
- });
3022
- if (path[0] === "body" && path[1] === "sectionBreaks" && typeof path[2] === "number" && (path[3] === "noteProperties" || path[3] === "suppressEndnotes")) {
3023
- var _documentDataModel$ge2;
3024
- const sectionId = (_documentDataModel$ge2 = documentDataModel.getBody()) === null || _documentDataModel$ge2 === void 0 || (_documentDataModel$ge2 = _documentDataModel$ge2.sectionBreaks) === null || _documentDataModel$ge2 === void 0 || (_documentDataModel$ge2 = _documentDataModel$ge2[path[2]]) === null || _documentDataModel$ge2 === void 0 ? void 0 : _documentDataModel$ge2.sectionId;
3025
- if (sectionId) result.add(getDocumentSectionPermissionObjectId("", sectionId));
3026
- }
3027
- if (path[path.length - 1] !== "body" || (edit === null || edit === void 0 ? void 0 : edit.et) !== _univerjs_core.TextX.id || !Array.isArray(edit.e) || !edit.e.every(isTextXAction)) return;
3028
- const textActions = edit.e;
3029
- let offset = 0;
3030
- const addRange = (startOffset, endOffset) => {
3031
- getDocumentEditTargetObjectIds(documentDataModel, scopedSegment, {
3032
- startOffset,
3033
- endOffset
3034
- }).forEach((objectId) => result.add(objectId));
3035
- };
3036
- textActions.forEach((action) => {
3037
- if (action.t === _univerjs_core.TextXActionType.INSERT) {
3038
- addRange(offset, offset);
3039
- return;
3040
- }
3041
- if (action.t === _univerjs_core.TextXActionType.DELETE) {
3042
- addRange(offset, offset + action.len);
3043
- offset += action.len;
3044
- return;
3045
- }
3046
- if (action.body !== void 0 || action.oldBody !== void 0 || action.coverType !== void 0) addRange(offset, offset + action.len);
3047
- offset += action.len;
3048
- });
2999
+ function createInsertCustomBlockActions(params) {
3000
+ const textX = new _univerjs_core.TextX();
3001
+ if (params.startIndex > 0) textX.push({
3002
+ t: _univerjs_core.TextXActionType.RETAIN,
3003
+ len: params.startIndex
3049
3004
  });
3050
- getDrawingIdsFromActions(actions).forEach((drawingId) => {
3051
- var _documentDataModel$ge3, _body$customBlocks;
3052
- result.add(getDocumentEntityPermissionObjectId(segmentId, "drawing", drawingId));
3053
- const body = (_documentDataModel$ge3 = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge3 === void 0 ? void 0 : _documentDataModel$ge3.getBody();
3054
- const block = body === null || body === void 0 || (_body$customBlocks = body.customBlocks) === null || _body$customBlocks === void 0 ? void 0 : _body$customBlocks.find((item) => item.blockId === drawingId);
3055
- if (block) getSectionPermissionObjectIds(documentDataModel, segmentId, {
3056
- startOffset: block.startIndex,
3057
- endOffset: block.startIndex + 1
3058
- }).forEach((objectId) => result.add(objectId));
3005
+ textX.push({
3006
+ t: _univerjs_core.TextXActionType.INSERT,
3007
+ body: {
3008
+ dataStream: "\b",
3009
+ customBlocks: [{
3010
+ startIndex: 0,
3011
+ blockId: params.blockId
3012
+ }]
3013
+ },
3014
+ len: 1
3015
+ });
3016
+ return composeActions([toBodyEditActions(textX, params.segmentId), createDrawingInsertActions(params)]);
3017
+ }
3018
+ function createRemoveCustomBlockActions(params) {
3019
+ const textX = new _univerjs_core.TextX();
3020
+ if (params.startIndex > 0) textX.push({
3021
+ t: _univerjs_core.TextXActionType.RETAIN,
3022
+ len: params.startIndex
3059
3023
  });
3060
- return [...result];
3024
+ textX.push({
3025
+ t: _univerjs_core.TextXActionType.DELETE,
3026
+ len: 1
3027
+ });
3028
+ return composeActions([toBodyEditActions(textX, params.segmentId), createDrawingRemoveActions(params)]);
3061
3029
  }
3062
- function getDrawingIdsFromActions(actions) {
3063
- const result = /* @__PURE__ */ new Set();
3064
- const visit = (value) => {
3065
- if (Array.isArray(value)) {
3066
- value.forEach((item, index) => {
3067
- if (item === "drawings" && typeof value[index + 1] === "string") result.add(value[index + 1]);
3068
- visit(item);
3069
- });
3070
- return;
3030
+ function createDocsCustomBlockDrawing(params) {
3031
+ const size = resolveDocsCustomBlockSize(params.childType);
3032
+ const isInline = params.interactionMode === "inline";
3033
+ return {
3034
+ unitId: params.unitId,
3035
+ subUnitId: params.unitId,
3036
+ drawingId: params.blockId,
3037
+ drawingType: _univerjs_core.DrawingTypeEnum.DRAWING_DOM,
3038
+ componentKey: params.componentKey ?? "UniverEmbedDocsCustomBlock",
3039
+ data: createEmbedDocsCustomBlockData(params),
3040
+ title: params.blockId,
3041
+ description: "Univer embedded unit custom block",
3042
+ layoutType: isInline ? _univerjs_core.PositionedObjectLayoutType.INLINE : _univerjs_core.PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM,
3043
+ allowTransform: false,
3044
+ docTransform: {
3045
+ size: {
3046
+ width: size.width,
3047
+ height: size.height
3048
+ },
3049
+ positionH: {
3050
+ relativeFrom: isInline ? _univerjs_core.ObjectRelativeFromH.PAGE : _univerjs_core.ObjectRelativeFromH.COLUMN,
3051
+ ...isInline ? { posOffset: 0 } : { align: _univerjs_core.AlignTypeH.LEFT }
3052
+ },
3053
+ positionV: {
3054
+ relativeFrom: isInline ? _univerjs_core.ObjectRelativeFromV.PAGE : _univerjs_core.ObjectRelativeFromV.PARAGRAPH,
3055
+ posOffset: 0
3056
+ },
3057
+ angle: 0
3058
+ },
3059
+ transform: {
3060
+ left: 0,
3061
+ top: 0,
3062
+ width: size.width,
3063
+ height: size.height
3071
3064
  }
3072
- if (!isRecord$2(value)) return;
3073
- Object.values(value).forEach(visit);
3074
3065
  };
3075
- visit(actions);
3076
- return [...result];
3077
- }
3078
- function isTextXAction(value) {
3079
- if (!isRecord$2(value) || typeof value.len !== "number") return false;
3080
- if (value.t === _univerjs_core.TextXActionType.INSERT) return isRecord$2(value.body);
3081
- return value.t === _univerjs_core.TextXActionType.RETAIN || value.t === _univerjs_core.TextXActionType.DELETE;
3082
- }
3083
- function isRecord$2(value) {
3084
- return typeof value === "object" && value !== null && !Array.isArray(value);
3085
3066
  }
3086
- function getDocumentEntityParentPermissionObjectIds(documentDataModel, segmentId, entityType, entityId) {
3087
- const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
3088
- const range = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex).entityRanges.get(`${entityType}:${entityId}`) ?? null;
3089
- return range ? getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex) : [];
3067
+ function resolveDocsCustomBlockSize(childType) {
3068
+ if (childType === _univerjs_core.UniverInstanceType.UNIVER_SHEET || childType === _univerjs_core.UniverInstanceType.UNIVER_BASE) return SHEET_LIKE_CUSTOM_BLOCK_SIZE;
3069
+ if (childType === _univerjs_core.UniverInstanceType.UNIVER_SLIDE) return SLIDE_CUSTOM_BLOCK_SIZE;
3070
+ return DEFAULT_CUSTOM_BLOCK_SIZE;
3090
3071
  }
3091
- function getDocumentParagraphParentPermissionObjectIds(documentDataModel, segmentId, paragraphId) {
3092
- const resolverIndex = getDocumentPermissionResolverIndex(documentDataModel);
3093
- const segmentIndex = getDocumentPermissionSegmentIndex(documentDataModel, segmentId, resolverIndex);
3094
- const { body } = segmentIndex;
3095
- const paragraph = segmentIndex.paragraphsById.get(paragraphId);
3096
- if (!body || !paragraph) return [];
3097
- return getSectionPermissionObjectIds(documentDataModel, segmentId, {
3098
- startOffset: segmentIndex.paragraphContentStartOffsets.get(paragraph.startIndex) ?? (0, _univerjs_core.getParagraphContentStartOffset)(body, paragraph),
3099
- endOffset: paragraph.startIndex + 1
3100
- }, resolverIndex);
3072
+ function isSheetLikeDocsCustomBlockChildType(childType) {
3073
+ return childType === _univerjs_core.UniverInstanceType.UNIVER_SHEET || childType === _univerjs_core.UniverInstanceType.UNIVER_BASE;
3101
3074
  }
3102
- function getDocumentSectionPermissionObjectIdsByIds(sectionIds) {
3103
- return Array.from(sectionIds, (sectionId) => getDocumentSectionPermissionObjectId("", sectionId));
3075
+ function createEmbedDocsCustomBlockData(params) {
3076
+ return {
3077
+ version: 1,
3078
+ embedId: params.embedId ?? params.blockId,
3079
+ hostUnitId: params.unitId,
3080
+ hostAnchorId: params.blockId,
3081
+ childUnitId: params.childUnitId,
3082
+ childType: params.childType,
3083
+ interactionMode: params.interactionMode ?? "block"
3084
+ };
3104
3085
  }
3105
- function getDocumentSectionIdsAtOffset(body, offset) {
3106
- return getSectionsIntersectingRange(getTopLevelSectionBreaks(body), {
3107
- startOffset: offset,
3108
- endOffset: offset
3109
- }).map((section) => section.sectionId);
3086
+ function isEmbedDocsCustomBlockData(data) {
3087
+ if (!data || typeof data !== "object") return false;
3088
+ const candidate = data;
3089
+ return candidate.version === 1 && typeof candidate.embedId === "string" && typeof candidate.hostAnchorId === "string";
3110
3090
  }
3111
- function getSectionPermissionObjectIds(documentDataModel, segmentId, range, resolverIndex = getDocumentPermissionResolverIndex(documentDataModel)) {
3112
- if (segmentId) {
3113
- const referenceRange = resolverIndex.footnoteReferenceRanges.get(segmentId);
3114
- if (referenceRange) return getDocumentEditTargetObjectIds(documentDataModel, "", referenceRange);
3115
- return getHeaderFooterOwnerSectionIds(documentDataModel, segmentId, resolverIndex.topLevelSections).map((sectionId) => getDocumentSectionPermissionObjectId("", sectionId));
3116
- }
3117
- return getSectionsIntersectingRange(resolverIndex.topLevelSections, range).map((section) => getDocumentSectionPermissionObjectId("", section.sectionId));
3091
+ function shouldUseInlineTextSelectionForDocsCustomBlockDrawing(drawing) {
3092
+ const data = drawing && typeof drawing === "object" ? drawing.data : void 0;
3093
+ if (!isEmbedDocsCustomBlockData(data)) return true;
3094
+ return data.interactionMode === "inline";
3118
3095
  }
3119
- function getParagraphPermissionObjectIds(body, segmentId, range, segmentIndex) {
3120
- const startOffset = Math.min(range.startOffset, range.endOffset);
3121
- const endOffset = Math.max(range.startOffset, range.endOffset);
3122
- return (body.paragraphs ?? []).filter((paragraph) => intersectsRange(segmentIndex.paragraphContentStartOffsets.get(paragraph.startIndex) ?? (0, _univerjs_core.getParagraphContentStartOffset)(body, paragraph), paragraph.startIndex + 1, startOffset, endOffset)).flatMap((paragraph) => paragraph.paragraphId ? [getDocumentParagraphPermissionObjectId(segmentId, paragraph.paragraphId)] : []);
3096
+ function createRichTextMutation(unitId, segmentId, actions) {
3097
+ return {
3098
+ id: RichTextEditingMutation.id,
3099
+ params: {
3100
+ unitId,
3101
+ segmentId,
3102
+ actions,
3103
+ textRanges: [],
3104
+ isEditing: false,
3105
+ noNeedSetTextRange: true
3106
+ }
3107
+ };
3123
3108
  }
3124
- function getEntityPermissionObjectIds(segmentId, range, segmentIndex) {
3125
- const startOffset = Math.min(range.startOffset, range.endOffset);
3126
- const endOffset = Math.max(range.startOffset, range.endOffset);
3127
- return segmentIndex.entities.filter((item) => intersectsRange(item.startOffset, item.endOffset, startOffset, endOffset)).map((item) => getDocumentEntityPermissionObjectId(segmentId, item.type, item.id));
3109
+ function toBodyEditActions(textX, segmentId) {
3110
+ return _univerjs_core.JSONX.getInstance().editOp(textX.serialize(), segmentId ? [
3111
+ "headers",
3112
+ segmentId,
3113
+ "body"
3114
+ ] : ["body"]) ?? [];
3128
3115
  }
3129
- function getSectionsIntersectingRange(sections, range) {
3130
- const startOffset = Math.min(range.startOffset, range.endOffset);
3131
- const endOffset = Math.max(range.startOffset, range.endOffset);
3132
- return sections.filter((section, index) => intersectsRange(index === 0 ? 0 : sections[index - 1].startIndex + 1, section.startIndex + 1, startOffset, endOffset));
3116
+ function createDrawingInsertActions(params) {
3117
+ if (params.segmentId) return [];
3118
+ const jsonX = _univerjs_core.JSONX.getInstance();
3119
+ const drawing = createDocsCustomBlockDrawing(params);
3120
+ return composeActions([jsonX.insertOp(["drawings", params.blockId], drawing) ?? [], jsonX.insertOp(["drawingsOrder", params.drawingOrderIndex ?? 0], params.blockId) ?? []]);
3133
3121
  }
3134
- function intersectsRange(targetStart, targetEnd, rangeStart, rangeEnd) {
3135
- if (rangeStart === rangeEnd) return rangeStart >= targetStart && rangeStart < targetEnd;
3136
- return rangeStart < targetEnd && rangeEnd > targetStart;
3122
+ function createDrawingRemoveActions(params) {
3123
+ if (params.segmentId) return [];
3124
+ const jsonX = _univerjs_core.JSONX.getInstance();
3125
+ const drawing = createDocsCustomBlockDrawing(params);
3126
+ return composeActions([jsonX.removeOp(["drawings", params.blockId], drawing) ?? [], jsonX.removeOp(["drawingsOrder", params.drawingOrderIndex ?? 0], params.blockId) ?? []]);
3137
3127
  }
3138
- function getHeaderFooterOwnerSectionIds(documentDataModel, segmentId, sections = getDocumentPermissionResolverIndex(documentDataModel).topLevelSections) {
3139
- const snapshot = documentDataModel.getSnapshot();
3140
- const result = /* @__PURE__ */ new Set();
3141
- sections.forEach((section, sectionIndex) => {
3142
- if (HEADER_FOOTER_REFERENCE_KEYS.some((key) => (0, _univerjs_core.resolveSectionHeaderFooterReference)(snapshot.documentStyle, sections, sectionIndex, key).segmentId === segmentId)) result.add(section.sectionId);
3143
- });
3144
- return [...result];
3128
+ function composeActions(actions) {
3129
+ return actions.reduce((composed, action) => {
3130
+ if (!action || _univerjs_core.JSONX.isNoop(action) || action.length === 0) return composed;
3131
+ if (!composed || _univerjs_core.JSONX.isNoop(composed) || composed.length === 0) return action;
3132
+ return _univerjs_core.JSONX.compose(composed, action) ?? [];
3133
+ }, []);
3145
3134
  }
3146
3135
 
3136
+ //#endregion
3137
+ //#region package.json
3138
+ var name = "@univerjs/docs";
3139
+ var version = "1.3.0";
3140
+
3141
+ //#endregion
3142
+ //#region src/config/config.ts
3143
+ /**
3144
+ * Copyright 2023-present DreamNum Co., Ltd.
3145
+ *
3146
+ * Licensed under the Apache License, Version 2.0 (the "License");
3147
+ * you may not use this file except in compliance with the License.
3148
+ * You may obtain a copy of the License at
3149
+ *
3150
+ * http://www.apache.org/licenses/LICENSE-2.0
3151
+ *
3152
+ * Unless required by applicable law or agreed to in writing, software
3153
+ * distributed under the License is distributed on an "AS IS" BASIS,
3154
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3155
+ * See the License for the specific language governing permissions and
3156
+ * limitations under the License.
3157
+ */
3158
+ const DOCS_PLUGIN_CONFIG_KEY = "docs.config";
3159
+ const configSymbol$1 = Symbol(DOCS_PLUGIN_CONFIG_KEY);
3160
+ const defaultPluginConfig = {};
3161
+
3162
+ //#endregion
3163
+ //#region src/controllers/custom-range.controller.ts
3164
+ let DocCustomRangeController = class DocCustomRangeController extends _univerjs_core.Disposable {
3165
+ constructor(_commandService, _textSelectionManagerService, _univerInstanceService) {
3166
+ super();
3167
+ this._commandService = _commandService;
3168
+ this._textSelectionManagerService = _textSelectionManagerService;
3169
+ this._univerInstanceService = _univerInstanceService;
3170
+ this._initSelectionChange();
3171
+ }
3172
+ _transformCustomRange(doc, selection, segmentId) {
3173
+ var _doc$getSelfOrHeaderF;
3174
+ const { startOffset, endOffset, collapsed } = selection;
3175
+ const customRanges = (_doc$getSelfOrHeaderF = doc.getSelfOrHeaderFooterModel(selection.segmentId ?? segmentId)) === null || _doc$getSelfOrHeaderF === void 0 || (_doc$getSelfOrHeaderF = _doc$getSelfOrHeaderF.getBody()) === null || _doc$getSelfOrHeaderF === void 0 || (_doc$getSelfOrHeaderF = _doc$getSelfOrHeaderF.customRanges) === null || _doc$getSelfOrHeaderF === void 0 ? void 0 : _doc$getSelfOrHeaderF.filter((range) => {
3176
+ const selectsFieldResult = range.rangeType === _univerjs_core.CustomRangeType.FIELD && !collapsed && startOffset <= range.startIndex + 1 && endOffset >= range.endIndex;
3177
+ if (!range.wholeEntity && !selectsFieldResult) return false;
3178
+ if (startOffset <= range.startIndex && endOffset > range.endIndex) return false;
3179
+ if (collapsed) return range.startIndex < startOffset && range.endIndex >= endOffset;
3180
+ return _univerjs_core.BuildTextUtils.range.isIntersects(startOffset, endOffset - 1, range.startIndex, range.endIndex);
3181
+ });
3182
+ if (customRanges === null || customRanges === void 0 ? void 0 : customRanges.length) {
3183
+ let start = startOffset;
3184
+ let end = endOffset;
3185
+ customRanges.forEach((range) => {
3186
+ start = Math.min(range.startIndex, start);
3187
+ end = Math.max(range.endIndex + 1, end);
3188
+ });
3189
+ return {
3190
+ ...selection,
3191
+ startOffset: start,
3192
+ endOffset: end,
3193
+ collapsed: start === end
3194
+ };
3195
+ }
3196
+ return selection;
3197
+ }
3198
+ _initSelectionChange() {
3199
+ this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
3200
+ if (commandInfo.id === SetTextSelectionsOperation.id) {
3201
+ const params = commandInfo.params;
3202
+ const { unitId, ranges, isEditing } = params;
3203
+ const doc = this._univerInstanceService.getUnit(unitId);
3204
+ if (!doc) return;
3205
+ const transformedRanges = ranges.map((range) => this._transformCustomRange(doc, range, params.segmentId));
3206
+ if (transformedRanges.some((range, i) => ranges[i] !== range)) this._textSelectionManagerService.replaceDocRanges(transformedRanges, void 0, isEditing);
3207
+ }
3208
+ }));
3209
+ }
3210
+ };
3211
+ DocCustomRangeController = __decorate([
3212
+ __decorateParam(0, _univerjs_core.ICommandService),
3213
+ __decorateParam(1, (0, _univerjs_core.Inject)(DocSelectionManagerService)),
3214
+ __decorateParam(2, _univerjs_core.IUniverInstanceService)
3215
+ ], DocCustomRangeController);
3216
+
3147
3217
  //#endregion
3148
3218
  //#region src/controllers/doc-permission.controller.ts
3149
3219
  const NON_EDIT_DOCUMENT_COMMAND_IDS = /* @__PURE__ */ new Set([
@@ -3720,6 +3790,7 @@ let UniverDocsPlugin = class UniverDocsPlugin extends _univerjs_core.Plugin {
3720
3790
  SetDocumentPermissionsCommand,
3721
3791
  SetDocumentDefaultParagraphStyleCommand,
3722
3792
  SetDocumentNameCommand,
3793
+ SetHeadingCollapsedCommand,
3723
3794
  SetSectionHeaderFooterLinkCommand,
3724
3795
  UpdateDocumentParagraphStyleCommand,
3725
3796
  UpdateDocumentSectionCommand,
@@ -4868,6 +4939,7 @@ exports.SetDocumentPermissionCommand = SetDocumentPermissionCommand;
4868
4939
  exports.SetDocumentPermissionRuleMutation = SetDocumentPermissionRuleMutation;
4869
4940
  exports.SetDocumentPermissionRulesMutation = SetDocumentPermissionRulesMutation;
4870
4941
  exports.SetDocumentPermissionsCommand = SetDocumentPermissionsCommand;
4942
+ exports.SetHeadingCollapsedCommand = SetHeadingCollapsedCommand;
4871
4943
  exports.SetSectionHeaderFooterLinkCommand = SetSectionHeaderFooterLinkCommand;
4872
4944
  exports.SetTextSelectionsOperation = SetTextSelectionsOperation;
4873
4945
  Object.defineProperty(exports, "UniverDocsLayoutWorkerPlugin", {