@univerjs/docs-drawing 1.0.0-alpha.2 → 1.0.0-alpha.4

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/index.js CHANGED
@@ -1,10 +1,488 @@
1
- import { BooleanNumber, Disposable, IConfigService, IResourceManagerService, IUniverInstanceService, Inject, Injector, Plugin, PositionedObjectLayoutType, UniverInstanceType, createIdentifier, merge, touchDependencies } from "@univerjs/core";
1
+ import { ArrangeTypeEnum, BooleanNumber, BuildTextUtils, CommandType, Disposable, ICommandService, IConfigService, IResourceManagerService, IUndoRedoService, IUniverInstanceService, Inject, Injector, JSONX, MemoryCursor, Plugin, PositionedObjectLayoutType, TextX, TextXActionType, Tools, UniverInstanceType, createIdentifier, getCustomBlockIdsInSelections, getRichTextEditPath, merge, toDisposable, touchDependencies } from "@univerjs/core";
2
+ import { DocSelectionManagerService, RichTextEditingMutation, getContentInsertRange, normalizeTextRange } from "@univerjs/docs";
2
3
  import { IDrawingManagerService, UnitDrawingService } from "@univerjs/drawing";
3
4
 
5
+ //#region src/commands/commands/insert-doc-drawing.command.ts
6
+ /**
7
+ * The command to insert new drawings
8
+ */
9
+ const InsertDocDrawingCommand = {
10
+ id: "doc.command.insert-doc-image",
11
+ type: CommandType.COMMAND,
12
+ handler: (accessor, params) => {
13
+ var _documentDataModel$ge, _snapshot$headers, _snapshot$footers, _snapshot$drawingsOrd, _snapshot$drawingsOrd2;
14
+ if (!params) return false;
15
+ const commandService = accessor.get(ICommandService);
16
+ const univerInstanceService = accessor.get(IUniverInstanceService);
17
+ const { unitId, drawings, textRange } = params;
18
+ const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
19
+ if (!documentDataModel) return false;
20
+ const targetTextRange = resolveDocDrawingInsertTextRange(accessor, unitId, textRange);
21
+ if (!targetTextRange) return false;
22
+ const { collapsed, startOffset, segmentId = "" } = targetTextRange;
23
+ const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
24
+ if (!body) return false;
25
+ const textX = new TextX();
26
+ const jsonX = JSONX.getInstance();
27
+ const rawActions = [];
28
+ const snapshot = documentDataModel.getSnapshot();
29
+ const targetDrawings = !!((_snapshot$headers = snapshot.headers) === null || _snapshot$headers === void 0 ? void 0 : _snapshot$headers[segmentId]) || !!((_snapshot$footers = snapshot.footers) === null || _snapshot$footers === void 0 ? void 0 : _snapshot$footers[segmentId]) ? drawings.map((drawing) => {
30
+ var _drawing$transforms;
31
+ return {
32
+ ...drawing,
33
+ isMultiTransform: BooleanNumber.TRUE,
34
+ transforms: (_drawing$transforms = drawing.transforms) !== null && _drawing$transforms !== void 0 ? _drawing$transforms : drawing.transform ? [drawing.transform] : null
35
+ };
36
+ }) : drawings;
37
+ const drawingOrderLength = (_snapshot$drawingsOrd = (_snapshot$drawingsOrd2 = snapshot.drawingsOrder) === null || _snapshot$drawingsOrd2 === void 0 ? void 0 : _snapshot$drawingsOrd2.length) !== null && _snapshot$drawingsOrd !== void 0 ? _snapshot$drawingsOrd : 0;
38
+ let removeDrawingLen = 0;
39
+ if (collapsed) {
40
+ if (startOffset > 0) textX.push({
41
+ t: TextXActionType.RETAIN,
42
+ len: startOffset
43
+ });
44
+ } else {
45
+ var _documentDataModel$ge2, _documentDataModel$ge3;
46
+ const dos = BuildTextUtils.selection.delete([targetTextRange], body, 0, null, false);
47
+ textX.push(...dos);
48
+ const removedCustomBlockIds = getCustomBlockIdsInSelections(body, [targetTextRange]);
49
+ const drawings = (_documentDataModel$ge2 = documentDataModel.getDrawings()) !== null && _documentDataModel$ge2 !== void 0 ? _documentDataModel$ge2 : {};
50
+ const drawingOrder = (_documentDataModel$ge3 = documentDataModel.getDrawingsOrder()) !== null && _documentDataModel$ge3 !== void 0 ? _documentDataModel$ge3 : [];
51
+ const sortedRemovedCustomBlockIds = removedCustomBlockIds.sort((a, b) => {
52
+ if (drawingOrder.indexOf(a) > drawingOrder.indexOf(b)) return -1;
53
+ else if (drawingOrder.indexOf(a) < drawingOrder.indexOf(b)) return 1;
54
+ return 0;
55
+ });
56
+ if (sortedRemovedCustomBlockIds.length > 0) for (const blockId of sortedRemovedCustomBlockIds) {
57
+ const drawing = drawings[blockId];
58
+ const drawingIndex = drawingOrder.indexOf(blockId);
59
+ if (drawing == null || drawingIndex < 0) continue;
60
+ const removeDrawingAction = jsonX.removeOp(["drawings", blockId], drawing);
61
+ const removeDrawingOrderAction = jsonX.removeOp(["drawingsOrder", drawingIndex], blockId);
62
+ rawActions.push(removeDrawingAction);
63
+ rawActions.push(removeDrawingOrderAction);
64
+ removeDrawingLen++;
65
+ }
66
+ }
67
+ textX.push({
68
+ t: TextXActionType.INSERT,
69
+ body: {
70
+ dataStream: "\b".repeat(targetDrawings.length),
71
+ customBlocks: targetDrawings.map((drawing, i) => ({
72
+ startIndex: i,
73
+ blockId: drawing.drawingId
74
+ }))
75
+ },
76
+ len: targetDrawings.length
77
+ });
78
+ const path = getRichTextEditPath(documentDataModel, segmentId);
79
+ const placeHolderAction = jsonX.editOp(textX.serialize(), path);
80
+ rawActions.push(placeHolderAction);
81
+ for (const drawing of targetDrawings) {
82
+ const { drawingId } = drawing;
83
+ const addDrawingAction = jsonX.insertOp(["drawings", drawingId], drawing);
84
+ const addDrawingOrderAction = jsonX.insertOp(["drawingsOrder", drawingOrderLength - removeDrawingLen], drawingId);
85
+ rawActions.push(addDrawingAction);
86
+ rawActions.push(addDrawingOrderAction);
87
+ }
88
+ const doMutation = {
89
+ id: RichTextEditingMutation.id,
90
+ params: {
91
+ unitId,
92
+ actions: [],
93
+ textRanges: []
94
+ }
95
+ };
96
+ doMutation.params.actions = rawActions.reduce((acc, cur) => {
97
+ return JSONX.compose(acc, cur);
98
+ }, null);
99
+ const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
100
+ return Boolean(result);
101
+ }
102
+ };
103
+ function resolveDocDrawingInsertTextRange(accessor, unitId, textRange) {
104
+ var _ref, _contentInsertRange$s;
105
+ const activeTextRange = accessor.get(DocSelectionManagerService).getActiveTextRange();
106
+ const explicitTextRange = textRange ? normalizeTextRange(textRange) : null;
107
+ const contentInsertRange = explicitTextRange !== null && explicitTextRange !== void 0 ? explicitTextRange : getContentInsertRange(accessor, unitId);
108
+ if (!contentInsertRange) return activeTextRange !== null && activeTextRange !== void 0 ? activeTextRange : null;
109
+ return {
110
+ ...activeTextRange,
111
+ startOffset: contentInsertRange.startOffset,
112
+ endOffset: contentInsertRange.endOffset,
113
+ collapsed: contentInsertRange.startOffset === contentInsertRange.endOffset,
114
+ segmentId: (_ref = (_contentInsertRange$s = contentInsertRange.segmentId) !== null && _contentInsertRange$s !== void 0 ? _contentInsertRange$s : activeTextRange === null || activeTextRange === void 0 ? void 0 : activeTextRange.segmentId) !== null && _ref !== void 0 ? _ref : ""
115
+ };
116
+ }
117
+
118
+ //#endregion
119
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
120
+ function _typeof(o) {
121
+ "@babel/helpers - typeof";
122
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
123
+ return typeof o;
124
+ } : function(o) {
125
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
126
+ }, _typeof(o);
127
+ }
128
+
129
+ //#endregion
130
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
131
+ function toPrimitive(t, r) {
132
+ if ("object" != _typeof(t) || !t) return t;
133
+ var e = t[Symbol.toPrimitive];
134
+ if (void 0 !== e) {
135
+ var i = e.call(t, r || "default");
136
+ if ("object" != _typeof(i)) return i;
137
+ throw new TypeError("@@toPrimitive must return a primitive value.");
138
+ }
139
+ return ("string" === r ? String : Number)(t);
140
+ }
141
+
142
+ //#endregion
143
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
144
+ function toPropertyKey(t) {
145
+ var i = toPrimitive(t, "string");
146
+ return "symbol" == _typeof(i) ? i : i + "";
147
+ }
148
+
149
+ //#endregion
150
+ //#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
151
+ function _defineProperty(e, r, t) {
152
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
153
+ value: t,
154
+ enumerable: !0,
155
+ configurable: !0,
156
+ writable: !0
157
+ }) : e[r] = t, e;
158
+ }
159
+
160
+ //#endregion
161
+ //#region src/services/doc-drawing-adapter.service.ts
162
+ const IDocDrawingAdapterService = createIdentifier("doc.drawing-adapter.service");
163
+ var DocDrawingAdapterService = class {
164
+ constructor() {
165
+ _defineProperty(this, "_adapters", []);
166
+ }
167
+ registerAdapter(adapter) {
168
+ this._adapters.push(adapter);
169
+ return toDisposable(() => {
170
+ const index = this._adapters.indexOf(adapter);
171
+ if (index >= 0) this._adapters.splice(index, 1);
172
+ });
173
+ }
174
+ getRemoveDrawingMutationInfos(params) {
175
+ const mutationInfos = {
176
+ redoMutations: [],
177
+ undoMutations: []
178
+ };
179
+ for (const adapter of this._adapters) {
180
+ var _adapter$getRemoveDra;
181
+ const nextMutationInfos = (_adapter$getRemoveDra = adapter.getRemoveDrawingMutationInfos) === null || _adapter$getRemoveDra === void 0 ? void 0 : _adapter$getRemoveDra.call(adapter, params);
182
+ if (!nextMutationInfos) continue;
183
+ mutationInfos.redoMutations.push(...nextMutationInfos.redoMutations);
184
+ mutationInfos.undoMutations.push(...nextMutationInfos.undoMutations);
185
+ }
186
+ return mutationInfos;
187
+ }
188
+ getEditDrawingCommandInfo(params) {
189
+ for (const adapter of this._adapters) {
190
+ var _adapter$getEditDrawi;
191
+ const commandInfo = (_adapter$getEditDrawi = adapter.getEditDrawingCommandInfo) === null || _adapter$getEditDrawi === void 0 ? void 0 : _adapter$getEditDrawi.call(adapter, params);
192
+ if (commandInfo) return commandInfo;
193
+ }
194
+ return null;
195
+ }
196
+ };
197
+
198
+ //#endregion
199
+ //#region src/commands/commands/remove-doc-drawing.command.ts
200
+ const RemoveDocDrawingCommand = {
201
+ id: "doc.command.remove-doc-image",
202
+ type: CommandType.COMMAND,
203
+ handler: (accessor, params) => {
204
+ var _ref, _contentInsertRange$s, _documentDataModel$ge, _documentDataModel$ge2, _documentDataModel$ge3;
205
+ if (!params) return false;
206
+ const commandService = accessor.get(ICommandService);
207
+ const undoRedoService = accessor.get(IUndoRedoService);
208
+ const drawingAdapterService = accessor.get(IDocDrawingAdapterService);
209
+ const univerInstanceService = accessor.get(IUniverInstanceService);
210
+ const docSelectionManagerService = accessor.get(DocSelectionManagerService);
211
+ const { unitId, drawings: removeDrawings, textRange } = params;
212
+ const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
213
+ if (!documentDataModel || removeDrawings.length === 0) return false;
214
+ const activeTextRange = docSelectionManagerService.getActiveTextRange();
215
+ const explicitTextRange = !textRange ? null : normalizeTextRange(textRange);
216
+ const contentInsertRange = explicitTextRange !== null && explicitTextRange !== void 0 ? explicitTextRange : getContentInsertRange(accessor, unitId);
217
+ const segmentId = (_ref = (_contentInsertRange$s = contentInsertRange === null || contentInsertRange === void 0 ? void 0 : contentInsertRange.segmentId) !== null && _contentInsertRange$s !== void 0 ? _contentInsertRange$s : activeTextRange === null || activeTextRange === void 0 ? void 0 : activeTextRange.segmentId) !== null && _ref !== void 0 ? _ref : "";
218
+ const textX = new TextX();
219
+ const jsonX = JSONX.getInstance();
220
+ const customBlocks = (_documentDataModel$ge = (_documentDataModel$ge2 = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge2 === void 0 || (_documentDataModel$ge2 = _documentDataModel$ge2.getBody()) === null || _documentDataModel$ge2 === void 0 ? void 0 : _documentDataModel$ge2.customBlocks) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : [];
221
+ const removeCustomBlocks = removeDrawings.map((drawing) => customBlocks.find((customBlock) => customBlock.blockId === drawing.drawingId)).filter((block) => !!block).sort((a, b) => a.startIndex > b.startIndex ? 1 : -1);
222
+ if (removeCustomBlocks.length === 0) return false;
223
+ const drawings = (_documentDataModel$ge3 = documentDataModel.getDrawings()) !== null && _documentDataModel$ge3 !== void 0 ? _documentDataModel$ge3 : {};
224
+ const removeDrawingParamById = new Map(removeDrawings.map((drawing) => [drawing.drawingId, drawing]));
225
+ const removeDrawingSnapshots = removeCustomBlocks.map((block) => drawings[block.blockId]).filter((drawing) => drawing != null);
226
+ const resourceRedoMutations = [];
227
+ const resourceUndoMutations = [];
228
+ const resourceMutationGroups = [];
229
+ for (const block of removeCustomBlocks) {
230
+ var _removeDrawingParam$s;
231
+ const { blockId } = block;
232
+ const drawing = drawings[blockId];
233
+ if (drawing == null) continue;
234
+ const removeDrawingParam = removeDrawingParamById.get(blockId);
235
+ const mutationInfos = drawingAdapterService.getRemoveDrawingMutationInfos({
236
+ unitId,
237
+ subUnitId: (_removeDrawingParam$s = removeDrawingParam === null || removeDrawingParam === void 0 ? void 0 : removeDrawingParam.subUnitId) !== null && _removeDrawingParam$s !== void 0 ? _removeDrawingParam$s : unitId,
238
+ drawing,
239
+ removeDrawings: removeDrawingSnapshots
240
+ });
241
+ if (mutationInfos.redoMutations.length === 0 && mutationInfos.undoMutations.length === 0) continue;
242
+ resourceRedoMutations.push(...mutationInfos.redoMutations);
243
+ resourceUndoMutations.push(...mutationInfos.undoMutations);
244
+ resourceMutationGroups.push(mutationInfos);
245
+ }
246
+ if (!executeResourceMutationGroups(resourceMutationGroups, commandService)) return false;
247
+ const historyId = `doc-drawing-remove-resource:${unitId}:${removeCustomBlocks.map((block) => block.blockId).join(",")}`;
248
+ let batchingDisposable = null;
249
+ if (resourceRedoMutations.length > 0 || resourceUndoMutations.length > 0) {
250
+ batchingDisposable = undoRedoService.__tempBatchingUndoRedo(unitId);
251
+ undoRedoService.pushUndoRedo({
252
+ unitID: unitId,
253
+ redoMutations: resourceRedoMutations,
254
+ undoMutations: resourceUndoMutations,
255
+ id: historyId
256
+ });
257
+ }
258
+ const memoryCursor = new MemoryCursor();
259
+ const cursorIndex = removeCustomBlocks[0].startIndex;
260
+ const textRanges = [{
261
+ startOffset: cursorIndex,
262
+ endOffset: cursorIndex
263
+ }];
264
+ const doMutation = {
265
+ id: RichTextEditingMutation.id,
266
+ params: {
267
+ unitId,
268
+ actions: [],
269
+ textRanges
270
+ }
271
+ };
272
+ const rawActions = [];
273
+ for (const block of removeCustomBlocks) {
274
+ const { startIndex } = block;
275
+ if (startIndex > memoryCursor.cursor) textX.push({
276
+ t: TextXActionType.RETAIN,
277
+ len: startIndex - memoryCursor.cursor
278
+ });
279
+ textX.push({
280
+ t: TextXActionType.DELETE,
281
+ len: 1
282
+ });
283
+ memoryCursor.moveCursorTo(startIndex + 1);
284
+ }
285
+ rawActions.push(jsonX.editOp(textX.serialize(), getRichTextEditPath(documentDataModel, segmentId)));
286
+ for (const block of removeCustomBlocks) {
287
+ const { blockId } = block;
288
+ const drawingIndex = documentDataModel.getDrawingsOrder().indexOf(blockId);
289
+ rawActions.push(jsonX.removeOp(["drawings", blockId], drawings[blockId]));
290
+ rawActions.push(jsonX.removeOp(["drawingsOrder", drawingIndex], blockId));
291
+ }
292
+ doMutation.params.actions = rawActions.reduce((acc, cur) => JSONX.compose(acc, cur), null);
293
+ const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
294
+ if (!result && batchingDisposable != null) {
295
+ batchingDisposable.dispose();
296
+ undoRedoService.rollback(historyId, unitId);
297
+ return false;
298
+ }
299
+ batchingDisposable === null || batchingDisposable === void 0 || batchingDisposable.dispose();
300
+ return Boolean(result);
301
+ }
302
+ };
303
+ function executeResourceMutationGroups(mutationGroups, commandService) {
304
+ const executedUndoGroups = [];
305
+ for (const mutationGroup of mutationGroups) {
306
+ if (!executeMutations(mutationGroup.redoMutations, commandService)) {
307
+ executeMutationGroups([...executedUndoGroups].reverse(), commandService);
308
+ return false;
309
+ }
310
+ executedUndoGroups.push(mutationGroup.undoMutations);
311
+ }
312
+ return true;
313
+ }
314
+ function executeMutationGroups(mutationGroups, commandService) {
315
+ mutationGroups.forEach((mutations) => executeMutations(mutations, commandService));
316
+ }
317
+ function executeMutations(mutations, commandService) {
318
+ for (const mutation of mutations) if (!commandService.syncExecuteCommand(mutation.id, mutation.params)) return false;
319
+ return true;
320
+ }
321
+
322
+ //#endregion
4
323
  //#region src/services/doc-drawing.service.ts
5
324
  var DocDrawingService = class extends UnitDrawingService {};
6
325
  const IDocDrawingService = createIdentifier("univer.doc.plugin.doc-drawing.service");
7
326
 
327
+ //#endregion
328
+ //#region src/commands/commands/set-drawing-arrange.command.ts
329
+ /**
330
+ * The command to arrange drawings.
331
+ */
332
+ const SetDocDrawingArrangeCommand = {
333
+ id: "doc.command.set-drawing-arrange",
334
+ type: CommandType.COMMAND,
335
+ handler: (accessor, params) => {
336
+ const commandService = accessor.get(ICommandService);
337
+ const docDrawingService = accessor.get(IDocDrawingService);
338
+ if (params == null) return false;
339
+ const { unitId, subUnitId, drawingIds, arrangeType } = params;
340
+ const drawingOrderMapParam = {
341
+ unitId,
342
+ subUnitId,
343
+ drawingIds
344
+ };
345
+ let jsonOp;
346
+ if (arrangeType === ArrangeTypeEnum.forward) jsonOp = docDrawingService.getForwardDrawingsOp(drawingOrderMapParam);
347
+ else if (arrangeType === ArrangeTypeEnum.backward) jsonOp = docDrawingService.getBackwardDrawingOp(drawingOrderMapParam);
348
+ else if (arrangeType === ArrangeTypeEnum.front) jsonOp = docDrawingService.getFrontDrawingsOp(drawingOrderMapParam);
349
+ else if (arrangeType === ArrangeTypeEnum.back) jsonOp = docDrawingService.getBackDrawingsOp(drawingOrderMapParam);
350
+ if (jsonOp == null) return false;
351
+ const { redo } = jsonOp;
352
+ if (redo == null) return false;
353
+ const rawActions = [];
354
+ let redoCopy = Tools.deepClone(redo);
355
+ redoCopy = redoCopy.slice(3);
356
+ redoCopy.unshift("drawingsOrder");
357
+ rawActions.push(redoCopy);
358
+ const doMutation = {
359
+ id: RichTextEditingMutation.id,
360
+ params: {
361
+ unitId,
362
+ actions: [],
363
+ textRanges: null
364
+ }
365
+ };
366
+ doMutation.params.actions = rawActions.reduce((acc, cur) => {
367
+ return JSONX.compose(acc, cur);
368
+ }, null);
369
+ const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
370
+ return Boolean(result);
371
+ }
372
+ };
373
+
374
+ //#endregion
375
+ //#region src/commands/commands/update-doc-drawing-transform.command.ts
376
+ const UpdateDrawingDocTransformCommand = {
377
+ id: "doc.command.update-drawing-doc-transform",
378
+ type: CommandType.COMMAND,
379
+ handler: (accessor, params) => {
380
+ var _documentDataModel$ge;
381
+ if (!params) return false;
382
+ const commandService = accessor.get(ICommandService);
383
+ const univerInstanceService = accessor.get(IUniverInstanceService);
384
+ const { unitId, drawings } = params;
385
+ const documentDataModel = univerInstanceService.getUnit(unitId, UniverInstanceType.UNIVER_DOC);
386
+ if (!documentDataModel) return false;
387
+ const oldDrawings = (_documentDataModel$ge = documentDataModel.getSnapshot().drawings) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
388
+ const jsonX = JSONX.getInstance();
389
+ const actions = [];
390
+ for (const { drawingId, key, value } of drawings) {
391
+ var _oldDrawings$drawingI;
392
+ const oldValue = (_oldDrawings$drawingI = oldDrawings[drawingId]) === null || _oldDrawings$drawingI === void 0 || (_oldDrawings$drawingI = _oldDrawings$drawingI.docTransform) === null || _oldDrawings$drawingI === void 0 ? void 0 : _oldDrawings$drawingI[key];
393
+ if (!Tools.diffValue(oldValue, value)) {
394
+ const path = [
395
+ "drawings",
396
+ drawingId,
397
+ "docTransform",
398
+ key
399
+ ];
400
+ actions.push(oldValue === void 0 ? jsonX.insertOp(path, value) : jsonX.replaceOp(path, oldValue, value));
401
+ }
402
+ }
403
+ return Boolean(commandService.syncExecuteCommand(RichTextEditingMutation.id, {
404
+ unitId,
405
+ actions: actions.reduce((acc, action) => JSONX.compose(acc, action), null),
406
+ textRanges: null,
407
+ debounce: true
408
+ }));
409
+ }
410
+ };
411
+
412
+ //#endregion
413
+ //#region src/commands/commands/update-doc-drawing-wrapping-style.command.ts
414
+ let TextWrappingStyle = /* @__PURE__ */ function(TextWrappingStyle) {
415
+ TextWrappingStyle["INLINE"] = "inline";
416
+ TextWrappingStyle["BEHIND_TEXT"] = "behindText";
417
+ TextWrappingStyle["IN_FRONT_OF_TEXT"] = "inFrontOfText";
418
+ TextWrappingStyle["WRAP_SQUARE"] = "wrapSquare";
419
+ TextWrappingStyle["WRAP_TOP_AND_BOTTOM"] = "wrapTopAndBottom";
420
+ return TextWrappingStyle;
421
+ }({});
422
+ const WRAPPING_STYLE_TO_LAYOUT_TYPE = {
423
+ ["inline"]: PositionedObjectLayoutType.INLINE,
424
+ ["wrapSquare"]: PositionedObjectLayoutType.WRAP_SQUARE,
425
+ ["wrapTopAndBottom"]: PositionedObjectLayoutType.WRAP_TOP_AND_BOTTOM,
426
+ ["inFrontOfText"]: PositionedObjectLayoutType.WRAP_NONE,
427
+ ["behindText"]: PositionedObjectLayoutType.WRAP_NONE
428
+ };
429
+ /**
430
+ * Updates document drawing wrapping styles and optional persisted positions.
431
+ */
432
+ const UpdateDocDrawingWrappingStyleCommand = {
433
+ id: "doc.command.update-doc-drawing-wrapping-style",
434
+ type: CommandType.COMMAND,
435
+ handler: (accessor, params) => {
436
+ var _documentDataModel$ge;
437
+ if (!params) return false;
438
+ const { drawings, wrappingStyle, unitId } = params;
439
+ const commandService = accessor.get(ICommandService);
440
+ const documentDataModel = accessor.get(IUniverInstanceService).getUnit(unitId, UniverInstanceType.UNIVER_DOC);
441
+ if (!documentDataModel) return false;
442
+ const oldDrawings = (_documentDataModel$ge = documentDataModel.getDrawings()) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
443
+ const jsonX = JSONX.getInstance();
444
+ const rawActions = [];
445
+ for (const drawing of drawings) {
446
+ const oldDrawing = oldDrawings[drawing.drawingId];
447
+ if (!oldDrawing) continue;
448
+ const layoutType = WRAPPING_STYLE_TO_LAYOUT_TYPE[wrappingStyle];
449
+ if (oldDrawing.layoutType !== layoutType) rawActions.push(jsonX.replaceOp([
450
+ "drawings",
451
+ drawing.drawingId,
452
+ "layoutType"
453
+ ], oldDrawing.layoutType, layoutType));
454
+ if (wrappingStyle === "behindText" || wrappingStyle === "inFrontOfText") {
455
+ const behindDoc = wrappingStyle === "behindText" ? BooleanNumber.TRUE : BooleanNumber.FALSE;
456
+ if (oldDrawing.behindDoc !== behindDoc) rawActions.push(jsonX.replaceOp([
457
+ "drawings",
458
+ drawing.drawingId,
459
+ "behindDoc"
460
+ ], oldDrawing.behindDoc, behindDoc));
461
+ }
462
+ if (wrappingStyle !== "inline") for (const key of ["positionH", "positionV"]) {
463
+ var _drawing$docTransform;
464
+ const value = (_drawing$docTransform = drawing.docTransform) === null || _drawing$docTransform === void 0 ? void 0 : _drawing$docTransform[key];
465
+ const oldValue = oldDrawing.docTransform[key];
466
+ if (value && !Tools.diffValue(oldValue, value)) rawActions.push(jsonX.replaceOp([
467
+ "drawings",
468
+ drawing.drawingId,
469
+ "docTransform",
470
+ key
471
+ ], oldValue, value));
472
+ }
473
+ }
474
+ const mutation = {
475
+ id: RichTextEditingMutation.id,
476
+ params: {
477
+ unitId,
478
+ actions: rawActions.reduce((actions, action) => JSONX.compose(actions, action), null),
479
+ textRanges: null
480
+ }
481
+ };
482
+ return Boolean(commandService.syncExecuteCommand(mutation.id, mutation.params));
483
+ }
484
+ };
485
+
8
486
  //#endregion
9
487
  //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
10
488
  function __decorateParam(paramIndex, decorator) {
@@ -39,16 +517,18 @@ function isDocDrawingBehindText(drawing) {
39
517
  return (drawing === null || drawing === void 0 ? void 0 : drawing.layoutType) === PositionedObjectLayoutType.WRAP_NONE && drawing.behindDoc === BooleanNumber.TRUE;
40
518
  }
41
519
  let DocDrawingController = class DocDrawingController extends Disposable {
42
- constructor(_docDrawingService, _drawingManagerService, _resourceManagerService, _univerInstanceService) {
520
+ constructor(_docDrawingService, _drawingManagerService, _resourceManagerService, _univerInstanceService, _commandService) {
43
521
  super();
44
522
  this._docDrawingService = _docDrawingService;
45
523
  this._drawingManagerService = _drawingManagerService;
46
524
  this._resourceManagerService = _resourceManagerService;
47
525
  this._univerInstanceService = _univerInstanceService;
526
+ this._commandService = _commandService;
48
527
  this._init();
49
528
  }
50
529
  _init() {
51
530
  this._initSnapshot();
531
+ this._initCommands();
52
532
  }
53
533
  _initSnapshot() {
54
534
  const toJson = (unitId) => {
@@ -124,18 +604,28 @@ let DocDrawingController = class DocDrawingController extends Disposable {
124
604
  this._drawingManagerService.registerDrawingData(unitId, renderSubDrawings);
125
605
  return true;
126
606
  }
607
+ _initCommands() {
608
+ [
609
+ InsertDocDrawingCommand,
610
+ RemoveDocDrawingCommand,
611
+ UpdateDrawingDocTransformCommand,
612
+ UpdateDocDrawingWrappingStyleCommand,
613
+ SetDocDrawingArrangeCommand
614
+ ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
615
+ }
127
616
  };
128
617
  DocDrawingController = __decorate([
129
618
  __decorateParam(0, IDocDrawingService),
130
619
  __decorateParam(1, IDrawingManagerService),
131
620
  __decorateParam(2, IResourceManagerService),
132
- __decorateParam(3, IUniverInstanceService)
621
+ __decorateParam(3, IUniverInstanceService),
622
+ __decorateParam(4, ICommandService)
133
623
  ], DocDrawingController);
134
624
 
135
625
  //#endregion
136
626
  //#region package.json
137
627
  var name = "@univerjs/docs-drawing";
138
- var version = "1.0.0-alpha.2";
628
+ var version = "1.0.0-alpha.4";
139
629
 
140
630
  //#endregion
141
631
  //#region src/config/config.ts
@@ -158,48 +648,6 @@ const DOCS_DRAWING_PLUGIN_CONFIG_KEY = "docs-drawing.config";
158
648
  const configSymbol = Symbol(DOCS_DRAWING_PLUGIN_CONFIG_KEY);
159
649
  const defaultPluginConfig = {};
160
650
 
161
- //#endregion
162
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
163
- function _typeof(o) {
164
- "@babel/helpers - typeof";
165
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
166
- return typeof o;
167
- } : function(o) {
168
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
169
- }, _typeof(o);
170
- }
171
-
172
- //#endregion
173
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
174
- function toPrimitive(t, r) {
175
- if ("object" != _typeof(t) || !t) return t;
176
- var e = t[Symbol.toPrimitive];
177
- if (void 0 !== e) {
178
- var i = e.call(t, r || "default");
179
- if ("object" != _typeof(i)) return i;
180
- throw new TypeError("@@toPrimitive must return a primitive value.");
181
- }
182
- return ("string" === r ? String : Number)(t);
183
- }
184
-
185
- //#endregion
186
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
187
- function toPropertyKey(t) {
188
- var i = toPrimitive(t, "string");
189
- return "symbol" == _typeof(i) ? i : i + "";
190
- }
191
-
192
- //#endregion
193
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
194
- function _defineProperty(e, r, t) {
195
- return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
196
- value: t,
197
- enumerable: !0,
198
- configurable: !0,
199
- writable: !0
200
- }) : e[r] = t, e;
201
- }
202
-
203
651
  //#endregion
204
652
  //#region src/plugin.ts
205
653
  let UniverDocsDrawingPlugin = class UniverDocsDrawingPlugin extends Plugin {
@@ -214,6 +662,8 @@ let UniverDocsDrawingPlugin = class UniverDocsDrawingPlugin extends Plugin {
214
662
  onStarting() {
215
663
  [
216
664
  [DocDrawingController],
665
+ [DocDrawingAdapterService],
666
+ [IDocDrawingAdapterService, { useClass: DocDrawingAdapterService }],
217
667
  [DocDrawingService],
218
668
  [IDocDrawingService, { useClass: DocDrawingService }]
219
669
  ].forEach((dependency) => this._injector.add(dependency));
@@ -227,4 +677,4 @@ _defineProperty(UniverDocsDrawingPlugin, "type", UniverInstanceType.UNIVER_DOC);
227
677
  UniverDocsDrawingPlugin = __decorate([__decorateParam(1, Inject(Injector)), __decorateParam(2, IConfigService)], UniverDocsDrawingPlugin);
228
678
 
229
679
  //#endregion
230
- export { DOCS_DRAWING_PLUGIN, DocDrawingController, DocDrawingService, IDocDrawingService, UniverDocsDrawingPlugin, getDocDrawingRenderOrder };
680
+ export { DOCS_DRAWING_PLUGIN, DocDrawingAdapterService, DocDrawingController, DocDrawingService, IDocDrawingAdapterService, IDocDrawingService, InsertDocDrawingCommand, RemoveDocDrawingCommand, SetDocDrawingArrangeCommand, TextWrappingStyle, UniverDocsDrawingPlugin, UpdateDocDrawingWrappingStyleCommand, UpdateDrawingDocTransformCommand, WRAPPING_STYLE_TO_LAYOUT_TYPE, getDocDrawingRenderOrder };
@@ -0,0 +1,26 @@
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 { ICommand, ITextRangeParam } from '@univerjs/core';
17
+ import type { IDocDrawing } from '../../services/doc-drawing.service';
18
+ export interface IInsertDocDrawingCommandParams {
19
+ unitId: string;
20
+ drawings: IDocDrawing[];
21
+ textRange?: ITextRangeParam;
22
+ }
23
+ /**
24
+ * The command to insert new drawings
25
+ */
26
+ export declare const InsertDocDrawingCommand: ICommand;
@@ -0,0 +1,28 @@
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 { DrawingTypeEnum, ICommand, ITextRangeParam } from '@univerjs/core';
17
+ export interface IRemoveDocDrawingCommandParam {
18
+ unitId: string;
19
+ subUnitId: string;
20
+ drawingId: string;
21
+ drawingType: DrawingTypeEnum;
22
+ }
23
+ export interface IRemoveDocDrawingCommandParams {
24
+ unitId: string;
25
+ drawings: IRemoveDocDrawingCommandParam[];
26
+ textRange?: ITextRangeParam;
27
+ }
28
+ export declare const RemoveDocDrawingCommand: ICommand;
@@ -0,0 +1,25 @@
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 { ICommand } from '@univerjs/core';
17
+ import type { IDrawingOrderMapParam } from '@univerjs/drawing';
18
+ import { ArrangeTypeEnum } from '@univerjs/core';
19
+ export interface ISetDocDrawingArrangeCommandParams extends IDrawingOrderMapParam {
20
+ arrangeType: ArrangeTypeEnum;
21
+ }
22
+ /**
23
+ * The command to arrange drawings.
24
+ */
25
+ export declare const SetDocDrawingArrangeCommand: ICommand;