@univerjs/docs-drawing 1.0.0-alpha.7 → 1.0.0-beta.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/facade.js CHANGED
@@ -97,8 +97,8 @@ var FDocumentImage = class {
97
97
  * ```
98
98
  */
99
99
  getSize() {
100
- var _this$getImageData$do, _this$getImageData3;
101
- return (_this$getImageData$do = (_this$getImageData3 = this.getImageData()) === null || _this$getImageData3 === void 0 ? void 0 : _this$getImageData3.docTransform.size) !== null && _this$getImageData$do !== void 0 ? _this$getImageData$do : null;
100
+ var _this$getImageData3;
101
+ return ((_this$getImageData3 = this.getImageData()) === null || _this$getImageData3 === void 0 ? void 0 : _this$getImageData3.docTransform.size) ?? null;
102
102
  }
103
103
  /**
104
104
  * Gets the current clockwise rotation angle.
@@ -131,8 +131,8 @@ var FDocumentImage = class {
131
131
  * ```
132
132
  */
133
133
  getPositionH() {
134
- var _this$getImageData$do2, _this$getImageData5;
135
- return (_this$getImageData$do2 = (_this$getImageData5 = this.getImageData()) === null || _this$getImageData5 === void 0 ? void 0 : _this$getImageData5.docTransform.positionH) !== null && _this$getImageData$do2 !== void 0 ? _this$getImageData$do2 : null;
134
+ var _this$getImageData5;
135
+ return ((_this$getImageData5 = this.getImageData()) === null || _this$getImageData5 === void 0 ? void 0 : _this$getImageData5.docTransform.positionH) ?? null;
136
136
  }
137
137
  /**
138
138
  * Gets the current vertical position.
@@ -148,8 +148,8 @@ var FDocumentImage = class {
148
148
  * ```
149
149
  */
150
150
  getPositionV() {
151
- var _this$getImageData$do3, _this$getImageData6;
152
- return (_this$getImageData$do3 = (_this$getImageData6 = this.getImageData()) === null || _this$getImageData6 === void 0 ? void 0 : _this$getImageData6.docTransform.positionV) !== null && _this$getImageData$do3 !== void 0 ? _this$getImageData$do3 : null;
151
+ var _this$getImageData6;
152
+ return ((_this$getImageData6 = this.getImageData()) === null || _this$getImageData6 === void 0 ? void 0 : _this$getImageData6.docTransform.positionV) ?? null;
153
153
  }
154
154
  /**
155
155
  * Gets the current raw document image data.
@@ -258,6 +258,11 @@ var FDocumentImage = class {
258
258
  }
259
259
  /**
260
260
  * Sets the image wrapping style.
261
+ *
262
+ * Use `INLINE` to place the image in the text flow, `WRAP_SQUARE` to flow text beside it, or
263
+ * `WRAP_TOP_AND_BOTTOM` to keep text above and below it. `BEHIND_TEXT` and `IN_FRONT_OF_TEXT` do not reserve space
264
+ * in the text layout and can overlap text.
265
+ *
261
266
  * When switching from inline to a floating style in a UI environment, the current visual position is preserved.
262
267
  * @param {TextWrappingStyle} wrappingStyle The wrapping style to apply.
263
268
  * @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
@@ -267,7 +272,8 @@ var FDocumentImage = class {
267
272
  * const image = fDocument.getImages()[0];
268
273
  *
269
274
  * if (image) {
270
- * const success = image.setWrappingStyle(univerAPI.Enum.DocsImageWrappingStyle.WRAP_SQUARE);
275
+ * // Float the image beside body text without covering it.
276
+ * const success = image.setWrappingStyle(univerAPI.Enum.TextWrappingStyle.WRAP_SQUARE);
271
277
  * console.log(success);
272
278
  * }
273
279
  * ```
@@ -432,17 +438,19 @@ var FDocumentImage = class {
432
438
  //#region src/facade/f-document.ts
433
439
  var FDocumentImageMixin = class extends _univerjs_docs_facade.FDocument {
434
440
  async insertImage(options) {
435
- var _options$wrappingStyl, _options$angle, _options$positionH, _options$positionV;
436
441
  const unitId = this.getId();
437
442
  const imageId = (0, _univerjs_core.generateRandomId)(6);
438
- const size = resolveImageSize(await this._getIntrinsicSize(options.source, options.imageSourceType), options);
443
+ const size = options.width != null && options.height != null ? {
444
+ width: options.width,
445
+ height: options.height
446
+ } : resolveImageSize(await this._getIntrinsicSize(options.source, options.imageSourceType), options);
439
447
  const defaultTransform = (0, _univerjs_docs.buildDocTransform)(size.width, size.height);
440
- const wrappingStyle = (_options$wrappingStyl = options.wrappingStyle) !== null && _options$wrappingStyl !== void 0 ? _options$wrappingStyl : _univerjs_docs_drawing.TextWrappingStyle.INLINE;
448
+ const wrappingStyle = options.wrappingStyle ?? _univerjs_docs_drawing.TextWrappingStyle.INLINE;
441
449
  const docTransform = {
442
450
  ...defaultTransform,
443
- angle: (_options$angle = options.angle) !== null && _options$angle !== void 0 ? _options$angle : defaultTransform.angle,
444
- positionH: (_options$positionH = options.positionH) !== null && _options$positionH !== void 0 ? _options$positionH : defaultTransform.positionH,
445
- positionV: (_options$positionV = options.positionV) !== null && _options$positionV !== void 0 ? _options$positionV : defaultTransform.positionV
451
+ angle: options.angle ?? defaultTransform.angle,
452
+ positionH: options.positionH ?? defaultTransform.positionH,
453
+ positionV: options.positionV ?? defaultTransform.positionV
446
454
  };
447
455
  const transform = (0, _univerjs_docs.docDrawingPositionToTransform)(docTransform);
448
456
  const drawing = {
@@ -478,10 +486,9 @@ var FDocumentImageMixin = class extends _univerjs_docs_facade.FDocument {
478
486
  return this._injector.createInstance(FDocumentImage, this, imageId, this._injector);
479
487
  }
480
488
  getImages() {
481
- var _documentDataModel$ge, _documentDataModel$ge2;
482
489
  const documentDataModel = this.getDocumentDataModel();
483
- const drawings = (_documentDataModel$ge = documentDataModel.getDrawings()) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
484
- return ((_documentDataModel$ge2 = documentDataModel.getDrawingsOrder()) !== null && _documentDataModel$ge2 !== void 0 ? _documentDataModel$ge2 : Object.keys(drawings)).filter((drawingId) => {
490
+ const drawings = documentDataModel.getDrawings() ?? {};
491
+ return (documentDataModel.getDrawingsOrder() ?? Object.keys(drawings)).filter((drawingId) => {
485
492
  var _drawings$drawingId;
486
493
  return ((_drawings$drawingId = drawings[drawingId]) === null || _drawings$drawingId === void 0 ? void 0 : _drawings$drawingId.drawingType) === _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE;
487
494
  }).map((drawingId) => this._injector.createInstance(FDocumentImage, this, drawingId, this._injector));
@@ -543,7 +550,7 @@ _univerjs_docs_facade.FDocument.extend(FDocumentImageMixin);
543
550
  * limitations under the License.
544
551
  */
545
552
  var FDocumentImageEnumMixin = class extends _univerjs_core_facade.FEnum {
546
- get DocsImageWrappingStyle() {
553
+ get TextWrappingStyle() {
547
554
  return _univerjs_docs_drawing.TextWrappingStyle;
548
555
  }
549
556
  get DocsImageRelativeFromH() {
package/lib/cjs/index.js CHANGED
@@ -11,81 +11,29 @@ const InsertDocDrawingCommand = {
11
11
  id: "doc.command.insert-doc-image",
12
12
  type: _univerjs_core.CommandType.COMMAND,
13
13
  handler: (accessor, params) => {
14
- var _documentDataModel$ge, _snapshot$headers, _snapshot$footers, _snapshot$drawingsOrd, _snapshot$drawingsOrd2;
14
+ var _documentDataModel$ge, _snapshot$headers, _snapshot$footers;
15
15
  if (!params) return false;
16
16
  const commandService = accessor.get(_univerjs_core.ICommandService);
17
17
  const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
18
18
  const { unitId, drawings, textRange } = params;
19
19
  const documentDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
20
20
  if (!documentDataModel) return false;
21
- const targetTextRange = resolveDocDrawingInsertTextRange(accessor, unitId, textRange);
22
- if (!targetTextRange) return false;
23
- const { collapsed, startOffset, segmentId = "" } = targetTextRange;
24
- const body = (_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody();
25
- if (!body) return false;
26
- const textX = new _univerjs_core.TextX();
27
- const jsonX = _univerjs_core.JSONX.getInstance();
28
- const rawActions = [];
21
+ const resolvedTextRange = resolveDocDrawingInsertTextRange(accessor, unitId, textRange);
22
+ if (!resolvedTextRange) return false;
23
+ const { segmentId = "" } = resolvedTextRange;
24
+ if (!((_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.getBody())) return false;
29
25
  const snapshot = documentDataModel.getSnapshot();
30
- 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) => {
31
- var _drawing$transforms;
32
- return {
33
- ...drawing,
34
- isMultiTransform: _univerjs_core.BooleanNumber.TRUE,
35
- transforms: (_drawing$transforms = drawing.transforms) !== null && _drawing$transforms !== void 0 ? _drawing$transforms : drawing.transform ? [drawing.transform] : null
36
- };
37
- }) : drawings;
38
- 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;
39
- let removeDrawingLen = 0;
40
- if (collapsed) {
41
- if (startOffset > 0) textX.push({
42
- t: _univerjs_core.TextXActionType.RETAIN,
43
- len: startOffset
44
- });
45
- } else {
46
- var _documentDataModel$ge2, _documentDataModel$ge3;
47
- const dos = _univerjs_core.BuildTextUtils.selection.delete([targetTextRange], body, 0, null, false);
48
- textX.push(...dos);
49
- const removedCustomBlockIds = (0, _univerjs_core.getCustomBlockIdsInSelections)(body, [targetTextRange]);
50
- const drawings = (_documentDataModel$ge2 = documentDataModel.getDrawings()) !== null && _documentDataModel$ge2 !== void 0 ? _documentDataModel$ge2 : {};
51
- const drawingOrder = (_documentDataModel$ge3 = documentDataModel.getDrawingsOrder()) !== null && _documentDataModel$ge3 !== void 0 ? _documentDataModel$ge3 : [];
52
- const sortedRemovedCustomBlockIds = removedCustomBlockIds.sort((a, b) => {
53
- if (drawingOrder.indexOf(a) > drawingOrder.indexOf(b)) return -1;
54
- else if (drawingOrder.indexOf(a) < drawingOrder.indexOf(b)) return 1;
55
- return 0;
56
- });
57
- if (sortedRemovedCustomBlockIds.length > 0) for (const blockId of sortedRemovedCustomBlockIds) {
58
- const drawing = drawings[blockId];
59
- const drawingIndex = drawingOrder.indexOf(blockId);
60
- if (drawing == null || drawingIndex < 0) continue;
61
- const removeDrawingAction = jsonX.removeOp(["drawings", blockId], drawing);
62
- const removeDrawingOrderAction = jsonX.removeOp(["drawingsOrder", drawingIndex], blockId);
63
- rawActions.push(removeDrawingAction);
64
- rawActions.push(removeDrawingOrderAction);
65
- removeDrawingLen++;
66
- }
67
- }
68
- textX.push({
69
- t: _univerjs_core.TextXActionType.INSERT,
70
- body: {
71
- dataStream: "\b".repeat(targetDrawings.length),
72
- customBlocks: targetDrawings.map((drawing, i) => ({
73
- startIndex: i,
74
- blockId: drawing.drawingId
75
- }))
76
- },
77
- len: targetDrawings.length
26
+ 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) => ({
27
+ ...drawing,
28
+ isMultiTransform: _univerjs_core.BooleanNumber.TRUE,
29
+ transforms: drawing.transforms ?? (drawing.transform ? [drawing.transform] : null)
30
+ })) : drawings;
31
+ const actions = _univerjs_core.BuildTextUtils.drawing.add({
32
+ selection: resolvedTextRange,
33
+ documentDataModel,
34
+ drawings: targetDrawings
78
35
  });
79
- const path = (0, _univerjs_core.getRichTextEditPath)(documentDataModel, segmentId);
80
- const placeHolderAction = jsonX.editOp(textX.serialize(), path);
81
- rawActions.push(placeHolderAction);
82
- for (const drawing of targetDrawings) {
83
- const { drawingId } = drawing;
84
- const addDrawingAction = jsonX.insertOp(["drawings", drawingId], drawing);
85
- const addDrawingOrderAction = jsonX.insertOp(["drawingsOrder", drawingOrderLength - removeDrawingLen], drawingId);
86
- rawActions.push(addDrawingAction);
87
- rawActions.push(addDrawingOrderAction);
88
- }
36
+ if (!actions) return false;
89
37
  const doMutation = {
90
38
  id: _univerjs_docs.RichTextEditingMutation.id,
91
39
  params: {
@@ -94,30 +42,26 @@ const InsertDocDrawingCommand = {
94
42
  textRanges: []
95
43
  }
96
44
  };
97
- doMutation.params.actions = rawActions.reduce((acc, cur) => {
98
- return _univerjs_core.JSONX.compose(acc, cur);
99
- }, null);
45
+ doMutation.params.actions = actions;
100
46
  const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
101
47
  return Boolean(result);
102
48
  }
103
49
  };
104
50
  function resolveDocDrawingInsertTextRange(accessor, unitId, textRange) {
105
- var _ref, _contentInsertRange$s;
106
51
  const activeTextRange = accessor.get(_univerjs_docs.DocSelectionManagerService).getActiveTextRange();
107
- const explicitTextRange = textRange ? (0, _univerjs_docs.normalizeTextRange)(textRange) : null;
108
- const contentInsertRange = explicitTextRange !== null && explicitTextRange !== void 0 ? explicitTextRange : (0, _univerjs_docs.getContentInsertRange)(accessor, unitId);
109
- if (!contentInsertRange) return activeTextRange !== null && activeTextRange !== void 0 ? activeTextRange : null;
52
+ const contentInsertRange = (textRange ? (0, _univerjs_docs.normalizeTextRange)(textRange) : null) ?? (0, _univerjs_docs.getContentInsertRange)(accessor, unitId);
53
+ if (!contentInsertRange) return activeTextRange ?? null;
110
54
  return {
111
55
  ...activeTextRange,
112
56
  startOffset: contentInsertRange.startOffset,
113
57
  endOffset: contentInsertRange.endOffset,
114
58
  collapsed: contentInsertRange.startOffset === contentInsertRange.endOffset,
115
- 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 : ""
59
+ segmentId: contentInsertRange.segmentId ?? (activeTextRange === null || activeTextRange === void 0 ? void 0 : activeTextRange.segmentId) ?? ""
116
60
  };
117
61
  }
118
62
 
119
63
  //#endregion
120
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/typeof.js
64
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/typeof.js
121
65
  function _typeof(o) {
122
66
  "@babel/helpers - typeof";
123
67
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -128,7 +72,7 @@ function _typeof(o) {
128
72
  }
129
73
 
130
74
  //#endregion
131
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPrimitive.js
75
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPrimitive.js
132
76
  function toPrimitive(t, r) {
133
77
  if ("object" != _typeof(t) || !t) return t;
134
78
  var e = t[Symbol.toPrimitive];
@@ -141,14 +85,14 @@ function toPrimitive(t, r) {
141
85
  }
142
86
 
143
87
  //#endregion
144
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/toPropertyKey.js
88
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/toPropertyKey.js
145
89
  function toPropertyKey(t) {
146
90
  var i = toPrimitive(t, "string");
147
91
  return "symbol" == _typeof(i) ? i : i + "";
148
92
  }
149
93
 
150
94
  //#endregion
151
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/defineProperty.js
95
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/defineProperty.js
152
96
  function _defineProperty(e, r, t) {
153
97
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
154
98
  value: t,
@@ -202,7 +146,7 @@ const RemoveDocDrawingCommand = {
202
146
  id: "doc.command.remove-doc-image",
203
147
  type: _univerjs_core.CommandType.COMMAND,
204
148
  handler: (accessor, params) => {
205
- var _ref, _contentInsertRange$s, _documentDataModel$ge, _documentDataModel$ge2, _documentDataModel$ge3;
149
+ var _documentDataModel$ge;
206
150
  if (!params) return false;
207
151
  const commandService = accessor.get(_univerjs_core.ICommandService);
208
152
  const undoRedoService = accessor.get(_univerjs_core.IUndoRedoService);
@@ -213,29 +157,27 @@ const RemoveDocDrawingCommand = {
213
157
  const documentDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
214
158
  if (!documentDataModel || removeDrawings.length === 0) return false;
215
159
  const activeTextRange = docSelectionManagerService.getActiveTextRange();
216
- const explicitTextRange = !textRange ? null : (0, _univerjs_docs.normalizeTextRange)(textRange);
217
- const contentInsertRange = explicitTextRange !== null && explicitTextRange !== void 0 ? explicitTextRange : (0, _univerjs_docs.getContentInsertRange)(accessor, unitId);
218
- 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 : "";
160
+ const contentInsertRange = (!textRange ? null : (0, _univerjs_docs.normalizeTextRange)(textRange)) ?? (0, _univerjs_docs.getContentInsertRange)(accessor, unitId);
161
+ const segmentId = (contentInsertRange === null || contentInsertRange === void 0 ? void 0 : contentInsertRange.segmentId) ?? (activeTextRange === null || activeTextRange === void 0 ? void 0 : activeTextRange.segmentId) ?? "";
219
162
  const textX = new _univerjs_core.TextX();
220
163
  const jsonX = _univerjs_core.JSONX.getInstance();
221
- 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 : [];
164
+ const customBlocks = ((_documentDataModel$ge = documentDataModel.getSelfOrHeaderFooterModel(segmentId)) === null || _documentDataModel$ge === void 0 || (_documentDataModel$ge = _documentDataModel$ge.getBody()) === null || _documentDataModel$ge === void 0 ? void 0 : _documentDataModel$ge.customBlocks) ?? [];
222
165
  const removeCustomBlocks = removeDrawings.map((drawing) => customBlocks.find((customBlock) => customBlock.blockId === drawing.drawingId)).filter((block) => !!block).sort((a, b) => a.startIndex > b.startIndex ? 1 : -1);
223
166
  if (removeCustomBlocks.length === 0) return false;
224
- const drawings = (_documentDataModel$ge3 = documentDataModel.getDrawings()) !== null && _documentDataModel$ge3 !== void 0 ? _documentDataModel$ge3 : {};
167
+ const drawings = documentDataModel.getDrawings() ?? {};
225
168
  const removeDrawingParamById = new Map(removeDrawings.map((drawing) => [drawing.drawingId, drawing]));
226
169
  const removeDrawingSnapshots = removeCustomBlocks.map((block) => drawings[block.blockId]).filter((drawing) => drawing != null);
227
170
  const resourceRedoMutations = [];
228
171
  const resourceUndoMutations = [];
229
172
  const resourceMutationGroups = [];
230
173
  for (const block of removeCustomBlocks) {
231
- var _removeDrawingParam$s;
232
174
  const { blockId } = block;
233
175
  const drawing = drawings[blockId];
234
176
  if (drawing == null) continue;
235
177
  const removeDrawingParam = removeDrawingParamById.get(blockId);
236
178
  const mutationInfos = drawingAdapterService.getRemoveDrawingMutationInfos({
237
179
  unitId,
238
- subUnitId: (_removeDrawingParam$s = removeDrawingParam === null || removeDrawingParam === void 0 ? void 0 : removeDrawingParam.subUnitId) !== null && _removeDrawingParam$s !== void 0 ? _removeDrawingParam$s : unitId,
180
+ subUnitId: (removeDrawingParam === null || removeDrawingParam === void 0 ? void 0 : removeDrawingParam.subUnitId) ?? unitId,
239
181
  drawing,
240
182
  removeDrawings: removeDrawingSnapshots
241
183
  });
@@ -320,13 +262,47 @@ function executeMutations(mutations, commandService) {
320
262
  return true;
321
263
  }
322
264
 
323
- //#endregion
324
- //#region src/services/doc-drawing.service.ts
325
- var DocDrawingService = class extends _univerjs_drawing.UnitDrawingService {};
326
- const IDocDrawingService = (0, _univerjs_core.createIdentifier)("univer.doc.plugin.doc-drawing.service");
327
-
328
265
  //#endregion
329
266
  //#region src/commands/commands/set-drawing-arrange.command.ts
267
+ const DRAWINGS_ORDER_KEY = "drawingsOrder";
268
+ /**
269
+ * Calculates the target model order. Adjacent moves treat selected drawings as a group,
270
+ * so selected drawings keep their relative order instead of crossing each other.
271
+ */
272
+ function getArrangedDrawingOrder(drawingOrder, drawingIds, arrangeType) {
273
+ const selectedDrawingIds = new Set(drawingIds.filter((drawingId) => drawingOrder.includes(drawingId)));
274
+ const arrangedDrawingOrder = [...drawingOrder];
275
+ if (selectedDrawingIds.size === 0) return arrangedDrawingOrder;
276
+ if (arrangeType === _univerjs_core.ArrangeTypeEnum.forward) {
277
+ for (let index = arrangedDrawingOrder.length - 2; index >= 0; index--) if (selectedDrawingIds.has(arrangedDrawingOrder[index]) && !selectedDrawingIds.has(arrangedDrawingOrder[index + 1])) [arrangedDrawingOrder[index], arrangedDrawingOrder[index + 1]] = [arrangedDrawingOrder[index + 1], arrangedDrawingOrder[index]];
278
+ } else if (arrangeType === _univerjs_core.ArrangeTypeEnum.backward) {
279
+ for (let index = 1; index < arrangedDrawingOrder.length; index++) if (selectedDrawingIds.has(arrangedDrawingOrder[index]) && !selectedDrawingIds.has(arrangedDrawingOrder[index - 1])) [arrangedDrawingOrder[index], arrangedDrawingOrder[index - 1]] = [arrangedDrawingOrder[index - 1], arrangedDrawingOrder[index]];
280
+ } else if (arrangeType === _univerjs_core.ArrangeTypeEnum.front || arrangeType === _univerjs_core.ArrangeTypeEnum.back) {
281
+ const selected = arrangedDrawingOrder.filter((drawingId) => selectedDrawingIds.has(drawingId));
282
+ const unselected = arrangedDrawingOrder.filter((drawingId) => !selectedDrawingIds.has(drawingId));
283
+ return arrangeType === _univerjs_core.ArrangeTypeEnum.front ? [...unselected, ...selected] : [...selected, ...unselected];
284
+ }
285
+ return arrangedDrawingOrder;
286
+ }
287
+ /**
288
+ * Converts the target order into granular JSONX move operations. This avoids replacing
289
+ * the entire drawingsOrder array and keeps collaboration conflicts scoped to moved items.
290
+ */
291
+ function createDrawingOrderActions(drawingOrder, arrangedDrawingOrder) {
292
+ const jsonX = _univerjs_core.JSONX.getInstance();
293
+ const workingDrawingOrder = [...drawingOrder];
294
+ const rawActions = [];
295
+ for (let targetIndex = 0; targetIndex < arrangedDrawingOrder.length; targetIndex++) {
296
+ const drawingId = arrangedDrawingOrder[targetIndex];
297
+ const currentIndex = workingDrawingOrder.indexOf(drawingId);
298
+ if (currentIndex < 0 || currentIndex === targetIndex) continue;
299
+ const action = jsonX.moveOp([DRAWINGS_ORDER_KEY, currentIndex], [DRAWINGS_ORDER_KEY, targetIndex]);
300
+ if (action) rawActions.push(action);
301
+ workingDrawingOrder.splice(currentIndex, 1);
302
+ workingDrawingOrder.splice(targetIndex, 0, drawingId);
303
+ }
304
+ return rawActions.reduce((actions, action) => _univerjs_core.JSONX.compose(actions, action), null);
305
+ }
330
306
  /**
331
307
  * The command to arrange drawings.
332
308
  */
@@ -335,39 +311,18 @@ const SetDocDrawingArrangeCommand = {
335
311
  type: _univerjs_core.CommandType.COMMAND,
336
312
  handler: (accessor, params) => {
337
313
  const commandService = accessor.get(_univerjs_core.ICommandService);
338
- const docDrawingService = accessor.get(IDocDrawingService);
339
314
  if (params == null) return false;
340
- const { unitId, subUnitId, drawingIds, arrangeType } = params;
341
- const drawingOrderMapParam = {
315
+ const { unitId, drawingIds, arrangeType } = params;
316
+ const documentDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
317
+ const drawingOrder = documentDataModel === null || documentDataModel === void 0 ? void 0 : documentDataModel.getDrawingsOrder();
318
+ if (!drawingOrder) return false;
319
+ const actions = createDrawingOrderActions(drawingOrder, getArrangedDrawingOrder(drawingOrder, drawingIds, arrangeType));
320
+ if (_univerjs_core.JSONX.isNoop(actions)) return false;
321
+ const result = commandService.syncExecuteCommand(_univerjs_docs.RichTextEditingMutation.id, {
342
322
  unitId,
343
- subUnitId,
344
- drawingIds
345
- };
346
- let jsonOp;
347
- if (arrangeType === _univerjs_core.ArrangeTypeEnum.forward) jsonOp = docDrawingService.getForwardDrawingsOp(drawingOrderMapParam);
348
- else if (arrangeType === _univerjs_core.ArrangeTypeEnum.backward) jsonOp = docDrawingService.getBackwardDrawingOp(drawingOrderMapParam);
349
- else if (arrangeType === _univerjs_core.ArrangeTypeEnum.front) jsonOp = docDrawingService.getFrontDrawingsOp(drawingOrderMapParam);
350
- else if (arrangeType === _univerjs_core.ArrangeTypeEnum.back) jsonOp = docDrawingService.getBackDrawingsOp(drawingOrderMapParam);
351
- if (jsonOp == null) return false;
352
- const { redo } = jsonOp;
353
- if (redo == null) return false;
354
- const rawActions = [];
355
- let redoCopy = _univerjs_core.Tools.deepClone(redo);
356
- redoCopy = redoCopy.slice(3);
357
- redoCopy.unshift("drawingsOrder");
358
- rawActions.push(redoCopy);
359
- const doMutation = {
360
- id: _univerjs_docs.RichTextEditingMutation.id,
361
- params: {
362
- unitId,
363
- actions: [],
364
- textRanges: null
365
- }
366
- };
367
- doMutation.params.actions = rawActions.reduce((acc, cur) => {
368
- return _univerjs_core.JSONX.compose(acc, cur);
369
- }, null);
370
- const result = commandService.syncExecuteCommand(doMutation.id, doMutation.params);
323
+ actions,
324
+ textRanges: null
325
+ });
371
326
  return Boolean(result);
372
327
  }
373
328
  };
@@ -378,14 +333,13 @@ const UpdateDrawingDocTransformCommand = {
378
333
  id: "doc.command.update-drawing-doc-transform",
379
334
  type: _univerjs_core.CommandType.COMMAND,
380
335
  handler: (accessor, params) => {
381
- var _documentDataModel$ge;
382
336
  if (!params) return false;
383
337
  const commandService = accessor.get(_univerjs_core.ICommandService);
384
338
  const univerInstanceService = accessor.get(_univerjs_core.IUniverInstanceService);
385
339
  const { unitId, drawings } = params;
386
340
  const documentDataModel = univerInstanceService.getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
387
341
  if (!documentDataModel) return false;
388
- const oldDrawings = (_documentDataModel$ge = documentDataModel.getSnapshot().drawings) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
342
+ const oldDrawings = documentDataModel.getSnapshot().drawings ?? {};
389
343
  const jsonX = _univerjs_core.JSONX.getInstance();
390
344
  const actions = [];
391
345
  for (const { drawingId, key, value } of drawings) {
@@ -412,11 +366,43 @@ const UpdateDrawingDocTransformCommand = {
412
366
 
413
367
  //#endregion
414
368
  //#region src/commands/commands/update-doc-drawing-wrapping-style.command.ts
369
+ /**
370
+ * Controls how a document drawing participates in text layout.
371
+ *
372
+ * `INLINE`, `WRAP_SQUARE`, and `WRAP_TOP_AND_BOTTOM` participate in text layout and can cause text to reflow.
373
+ * `BEHIND_TEXT` and `IN_FRONT_OF_TEXT` are overlay styles and do not cause text to reflow.
374
+ */
415
375
  let TextWrappingStyle = /* @__PURE__ */ function(TextWrappingStyle) {
376
+ /**
377
+ * Places the drawing in the text flow like a character.
378
+ *
379
+ * The drawing occupies layout space and does not overlap surrounding text. Its position is determined by its
380
+ * document range, so floating position settings have no visible effect.
381
+ */
416
382
  TextWrappingStyle["INLINE"] = "inline";
383
+ /**
384
+ * Floats the drawing behind document text without causing the text to reflow.
385
+ *
386
+ * Use this style for backgrounds or watermarks. Text can overlap the drawing, so ensure that it remains readable.
387
+ */
417
388
  TextWrappingStyle["BEHIND_TEXT"] = "behindText";
389
+ /**
390
+ * Floats the drawing in front of document text without causing the text to reflow.
391
+ *
392
+ * The drawing can cover overlapping text. Use this style only when an overlay is intentional.
393
+ */
418
394
  TextWrappingStyle["IN_FRONT_OF_TEXT"] = "inFrontOfText";
395
+ /**
396
+ * Floats the drawing and wraps text around its rectangular bounds.
397
+ *
398
+ * Use this style for images that should appear beside body text, such as portraits or illustrations.
399
+ */
419
400
  TextWrappingStyle["WRAP_SQUARE"] = "wrapSquare";
401
+ /**
402
+ * Floats the drawing in a horizontal band, with text only above and below it.
403
+ *
404
+ * Use this style for wide figures or standalone illustrations that should not have text beside them.
405
+ */
420
406
  TextWrappingStyle["WRAP_TOP_AND_BOTTOM"] = "wrapTopAndBottom";
421
407
  return TextWrappingStyle;
422
408
  }({});
@@ -434,13 +420,12 @@ const UpdateDocDrawingWrappingStyleCommand = {
434
420
  id: "doc.command.update-doc-drawing-wrapping-style",
435
421
  type: _univerjs_core.CommandType.COMMAND,
436
422
  handler: (accessor, params) => {
437
- var _documentDataModel$ge;
438
423
  if (!params) return false;
439
424
  const { drawings, wrappingStyle, unitId } = params;
440
425
  const commandService = accessor.get(_univerjs_core.ICommandService);
441
426
  const documentDataModel = accessor.get(_univerjs_core.IUniverInstanceService).getUnit(unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC);
442
427
  if (!documentDataModel) return false;
443
- const oldDrawings = (_documentDataModel$ge = documentDataModel.getDrawings()) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
428
+ const oldDrawings = documentDataModel.getDrawings() ?? {};
444
429
  const jsonX = _univerjs_core.JSONX.getInstance();
445
430
  const rawActions = [];
446
431
  for (const drawing of drawings) {
@@ -485,7 +470,12 @@ const UpdateDocDrawingWrappingStyleCommand = {
485
470
  };
486
471
 
487
472
  //#endregion
488
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorateParam.js
473
+ //#region src/services/doc-drawing.service.ts
474
+ var DocDrawingService = class extends _univerjs_drawing.UnitDrawingService {};
475
+ const IDocDrawingService = (0, _univerjs_core.createIdentifier)("univer.doc.plugin.doc-drawing.service");
476
+
477
+ //#endregion
478
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorateParam.js
489
479
  function __decorateParam(paramIndex, decorator) {
490
480
  return function(target, key) {
491
481
  decorator(target, key, paramIndex);
@@ -493,7 +483,7 @@ function __decorateParam(paramIndex, decorator) {
493
483
  }
494
484
 
495
485
  //#endregion
496
- //#region \0@oxc-project+runtime@0.137.0/helpers/esm/decorate.js
486
+ //#region \0@oxc-project+runtime@0.140.0/helpers/esm/decorate.js
497
487
  function __decorate(decorators, target, key, desc) {
498
488
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
499
489
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -538,8 +528,8 @@ let DocDrawingController = class DocDrawingController extends _univerjs_core.Dis
538
528
  const drawings = doc.getSnapshot().drawings;
539
529
  const drawingOrder = doc.getSnapshot().drawingsOrder;
540
530
  return JSON.stringify({
541
- data: drawings !== null && drawings !== void 0 ? drawings : {},
542
- order: drawingOrder !== null && drawingOrder !== void 0 ? drawingOrder : []
531
+ data: drawings ?? {},
532
+ order: drawingOrder ?? []
543
533
  });
544
534
  }
545
535
  return "";
@@ -570,10 +560,9 @@ let DocDrawingController = class DocDrawingController extends _univerjs_core.Dis
570
560
  });
571
561
  },
572
562
  onLoad: (unitId, value) => {
573
- var _value$data, _value$order;
574
563
  this._setDrawingDataForUnit(unitId, {
575
- data: (_value$data = value.data) !== null && _value$data !== void 0 ? _value$data : {},
576
- order: (_value$order = value.order) !== null && _value$order !== void 0 ? _value$order : []
564
+ data: value.data ?? {},
565
+ order: value.order ?? []
577
566
  });
578
567
  }
579
568
  }));
@@ -626,7 +615,7 @@ DocDrawingController = __decorate([
626
615
  //#endregion
627
616
  //#region package.json
628
617
  var name = "@univerjs/docs-drawing";
629
- var version = "1.0.0-alpha.7";
618
+ var version = "1.0.0-beta.0";
630
619
 
631
620
  //#endregion
632
621
  //#region src/config/config.ts
@@ -675,7 +664,11 @@ _defineProperty(UniverDocsDrawingPlugin, "pluginName", DOCS_DRAWING_PLUGIN);
675
664
  _defineProperty(UniverDocsDrawingPlugin, "packageName", name);
676
665
  _defineProperty(UniverDocsDrawingPlugin, "version", version);
677
666
  _defineProperty(UniverDocsDrawingPlugin, "type", _univerjs_core.UniverInstanceType.UNIVER_DOC);
678
- UniverDocsDrawingPlugin = __decorate([__decorateParam(1, (0, _univerjs_core.Inject)(_univerjs_core.Injector)), __decorateParam(2, _univerjs_core.IConfigService)], UniverDocsDrawingPlugin);
667
+ UniverDocsDrawingPlugin = __decorate([
668
+ (0, _univerjs_core.DependentOn)(_univerjs_docs.UniverDocsPlugin, _univerjs_drawing.UniverDrawingPlugin),
669
+ __decorateParam(1, (0, _univerjs_core.Inject)(_univerjs_core.Injector)),
670
+ __decorateParam(2, _univerjs_core.IConfigService)
671
+ ], UniverDocsDrawingPlugin);
679
672
 
680
673
  //#endregion
681
674
  exports.DOCS_DRAWING_PLUGIN = DOCS_DRAWING_PLUGIN;
package/lib/es/facade.js CHANGED
@@ -96,8 +96,8 @@ var FDocumentImage = class {
96
96
  * ```
97
97
  */
98
98
  getSize() {
99
- var _this$getImageData$do, _this$getImageData3;
100
- return (_this$getImageData$do = (_this$getImageData3 = this.getImageData()) === null || _this$getImageData3 === void 0 ? void 0 : _this$getImageData3.docTransform.size) !== null && _this$getImageData$do !== void 0 ? _this$getImageData$do : null;
99
+ var _this$getImageData3;
100
+ return ((_this$getImageData3 = this.getImageData()) === null || _this$getImageData3 === void 0 ? void 0 : _this$getImageData3.docTransform.size) ?? null;
101
101
  }
102
102
  /**
103
103
  * Gets the current clockwise rotation angle.
@@ -130,8 +130,8 @@ var FDocumentImage = class {
130
130
  * ```
131
131
  */
132
132
  getPositionH() {
133
- var _this$getImageData$do2, _this$getImageData5;
134
- return (_this$getImageData$do2 = (_this$getImageData5 = this.getImageData()) === null || _this$getImageData5 === void 0 ? void 0 : _this$getImageData5.docTransform.positionH) !== null && _this$getImageData$do2 !== void 0 ? _this$getImageData$do2 : null;
133
+ var _this$getImageData5;
134
+ return ((_this$getImageData5 = this.getImageData()) === null || _this$getImageData5 === void 0 ? void 0 : _this$getImageData5.docTransform.positionH) ?? null;
135
135
  }
136
136
  /**
137
137
  * Gets the current vertical position.
@@ -147,8 +147,8 @@ var FDocumentImage = class {
147
147
  * ```
148
148
  */
149
149
  getPositionV() {
150
- var _this$getImageData$do3, _this$getImageData6;
151
- return (_this$getImageData$do3 = (_this$getImageData6 = this.getImageData()) === null || _this$getImageData6 === void 0 ? void 0 : _this$getImageData6.docTransform.positionV) !== null && _this$getImageData$do3 !== void 0 ? _this$getImageData$do3 : null;
150
+ var _this$getImageData6;
151
+ return ((_this$getImageData6 = this.getImageData()) === null || _this$getImageData6 === void 0 ? void 0 : _this$getImageData6.docTransform.positionV) ?? null;
152
152
  }
153
153
  /**
154
154
  * Gets the current raw document image data.
@@ -257,6 +257,11 @@ var FDocumentImage = class {
257
257
  }
258
258
  /**
259
259
  * Sets the image wrapping style.
260
+ *
261
+ * Use `INLINE` to place the image in the text flow, `WRAP_SQUARE` to flow text beside it, or
262
+ * `WRAP_TOP_AND_BOTTOM` to keep text above and below it. `BEHIND_TEXT` and `IN_FRONT_OF_TEXT` do not reserve space
263
+ * in the text layout and can overlap text.
264
+ *
260
265
  * When switching from inline to a floating style in a UI environment, the current visual position is preserved.
261
266
  * @param {TextWrappingStyle} wrappingStyle The wrapping style to apply.
262
267
  * @returns {boolean} `true` when the update command succeeds; otherwise, `false`.
@@ -266,7 +271,8 @@ var FDocumentImage = class {
266
271
  * const image = fDocument.getImages()[0];
267
272
  *
268
273
  * if (image) {
269
- * const success = image.setWrappingStyle(univerAPI.Enum.DocsImageWrappingStyle.WRAP_SQUARE);
274
+ * // Float the image beside body text without covering it.
275
+ * const success = image.setWrappingStyle(univerAPI.Enum.TextWrappingStyle.WRAP_SQUARE);
270
276
  * console.log(success);
271
277
  * }
272
278
  * ```
@@ -431,17 +437,19 @@ var FDocumentImage = class {
431
437
  //#region src/facade/f-document.ts
432
438
  var FDocumentImageMixin = class extends FDocument {
433
439
  async insertImage(options) {
434
- var _options$wrappingStyl, _options$angle, _options$positionH, _options$positionV;
435
440
  const unitId = this.getId();
436
441
  const imageId = generateRandomId(6);
437
- const size = resolveImageSize(await this._getIntrinsicSize(options.source, options.imageSourceType), options);
442
+ const size = options.width != null && options.height != null ? {
443
+ width: options.width,
444
+ height: options.height
445
+ } : resolveImageSize(await this._getIntrinsicSize(options.source, options.imageSourceType), options);
438
446
  const defaultTransform = buildDocTransform(size.width, size.height);
439
- const wrappingStyle = (_options$wrappingStyl = options.wrappingStyle) !== null && _options$wrappingStyl !== void 0 ? _options$wrappingStyl : TextWrappingStyle.INLINE;
447
+ const wrappingStyle = options.wrappingStyle ?? TextWrappingStyle.INLINE;
440
448
  const docTransform = {
441
449
  ...defaultTransform,
442
- angle: (_options$angle = options.angle) !== null && _options$angle !== void 0 ? _options$angle : defaultTransform.angle,
443
- positionH: (_options$positionH = options.positionH) !== null && _options$positionH !== void 0 ? _options$positionH : defaultTransform.positionH,
444
- positionV: (_options$positionV = options.positionV) !== null && _options$positionV !== void 0 ? _options$positionV : defaultTransform.positionV
450
+ angle: options.angle ?? defaultTransform.angle,
451
+ positionH: options.positionH ?? defaultTransform.positionH,
452
+ positionV: options.positionV ?? defaultTransform.positionV
445
453
  };
446
454
  const transform = docDrawingPositionToTransform(docTransform);
447
455
  const drawing = {
@@ -477,10 +485,9 @@ var FDocumentImageMixin = class extends FDocument {
477
485
  return this._injector.createInstance(FDocumentImage, this, imageId, this._injector);
478
486
  }
479
487
  getImages() {
480
- var _documentDataModel$ge, _documentDataModel$ge2;
481
488
  const documentDataModel = this.getDocumentDataModel();
482
- const drawings = (_documentDataModel$ge = documentDataModel.getDrawings()) !== null && _documentDataModel$ge !== void 0 ? _documentDataModel$ge : {};
483
- return ((_documentDataModel$ge2 = documentDataModel.getDrawingsOrder()) !== null && _documentDataModel$ge2 !== void 0 ? _documentDataModel$ge2 : Object.keys(drawings)).filter((drawingId) => {
489
+ const drawings = documentDataModel.getDrawings() ?? {};
490
+ return (documentDataModel.getDrawingsOrder() ?? Object.keys(drawings)).filter((drawingId) => {
484
491
  var _drawings$drawingId;
485
492
  return ((_drawings$drawingId = drawings[drawingId]) === null || _drawings$drawingId === void 0 ? void 0 : _drawings$drawingId.drawingType) === DrawingTypeEnum.DRAWING_IMAGE;
486
493
  }).map((drawingId) => this._injector.createInstance(FDocumentImage, this, drawingId, this._injector));
@@ -542,7 +549,7 @@ FDocument.extend(FDocumentImageMixin);
542
549
  * limitations under the License.
543
550
  */
544
551
  var FDocumentImageEnumMixin = class extends FEnum {
545
- get DocsImageWrappingStyle() {
552
+ get TextWrappingStyle() {
546
553
  return TextWrappingStyle;
547
554
  }
548
555
  get DocsImageRelativeFromH() {