@univerjs/docs-drawing-ui 1.0.0-beta.1 → 1.0.0-beta.2

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
@@ -163,6 +163,7 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
163
163
  this._docRefreshDrawingsService = _docRefreshDrawingsService;
164
164
  this._fileOpenerService = _fileOpenerService;
165
165
  this._updateOrderListener();
166
+ this._updateImageCropListener();
166
167
  this._groupDrawingListener();
167
168
  this._focusDrawingListener();
168
169
  this._transformDrawingListener();
@@ -174,10 +175,12 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
174
175
  }
175
176
  async insertDocImage() {
176
177
  const insertPosition = this._getCurrentImageInsertPosition();
178
+ const textRange = this._getCurrentImageInsertTextRange();
177
179
  const files = await this._fileOpenerService.openFile({
178
180
  multiple: true,
179
181
  accept: _univerjs_drawing.DRAWING_IMAGE_ALLOW_IMAGE_LIST.map((image) => `.${image.replace("image/", "")}`).join(",")
180
182
  });
183
+ if (this._disposed) return false;
181
184
  const fileLength = files.length;
182
185
  if (fileLength > _univerjs_drawing.DRAWING_IMAGE_COUNT_LIMIT) {
183
186
  this._messageService.show({
@@ -186,10 +189,9 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
186
189
  });
187
190
  return false;
188
191
  } else if (fileLength === 0) return false;
189
- await this._insertFloatImages(files, insertPosition);
190
- return true;
192
+ return await this._insertFloatImages(files, insertPosition, textRange);
191
193
  }
192
- async _insertFloatImages(files, insertPosition) {
194
+ async _insertFloatImages(files, insertPosition, textRange) {
193
195
  let imageParams = [];
194
196
  try {
195
197
  imageParams = await Promise.all(files.map((file) => this._imageIoService.saveImage(file)));
@@ -213,13 +215,14 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
213
215
  content
214
216
  });
215
217
  }
216
- if (imageParams.length === 0) return;
218
+ if (this._disposed || imageParams.length === 0) return false;
217
219
  const { unitId } = this._context;
218
220
  const docDrawingParams = [];
219
221
  for (const imageParam of imageParams) {
220
222
  if (imageParam == null) continue;
221
223
  const { imageId, imageSourceType, source, base64Cache } = imageParam;
222
224
  const { width, height, image } = await (0, _univerjs_drawing.getImageSize)(base64Cache || "");
225
+ if (this._disposed) return false;
223
226
  this._imageIoService.addImageSourceCache(imageId, imageSourceType, image);
224
227
  let scale = 1;
225
228
  if (width > _univerjs_drawing.DRAWING_IMAGE_WIDTH_LIMIT || height > _univerjs_drawing.DRAWING_IMAGE_HEIGHT_LIMIT) {
@@ -229,7 +232,7 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
229
232
  }
230
233
  const imagePosition = insertPosition ?? this._getCurrentImageInsertPosition();
231
234
  const docTransform = this._getImagePosition(width * scale, height * scale, imagePosition);
232
- if (docTransform == null) return;
235
+ if (docTransform == null) return false;
233
236
  const transform = (0, _univerjs_docs.docDrawingPositionToTransform)(docTransform);
234
237
  if (transform != null && imagePosition != null) transform.top = imagePosition.top;
235
238
  const docDrawingParam = {
@@ -257,9 +260,10 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
257
260
  }
258
261
  docDrawingParams.push(docDrawingParam);
259
262
  }
260
- this._commandService.executeCommand(_univerjs_docs_drawing.InsertDocDrawingCommand.id, {
263
+ return await this._commandService.executeCommand(_univerjs_docs_drawing.InsertDocDrawingCommand.id, {
261
264
  unitId,
262
- drawings: docDrawingParams
265
+ drawings: docDrawingParams,
266
+ textRange: textRange ?? void 0
263
267
  });
264
268
  }
265
269
  _isInsertInHeaderFooter() {
@@ -284,6 +288,9 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
284
288
  top: position.top
285
289
  };
286
290
  }
291
+ _getCurrentImageInsertTextRange() {
292
+ return this._docSelectionRenderService.getAllTextRanges().find((range) => range.isActive) ?? this._docSelectionManagerService.getActiveTextRange();
293
+ }
287
294
  _updateOrderListener() {
288
295
  this.disposeWithMe(this._drawingManagerService.featurePluginOrderUpdate$.subscribe((params) => {
289
296
  const { unitId, subUnitId, drawingIds, arrangeType } = params;
@@ -295,6 +302,73 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
295
302
  });
296
303
  }));
297
304
  }
305
+ _updateImageCropListener() {
306
+ this.disposeWithMe(this._drawingManagerService.featurePluginUpdate$.subscribe((params) => {
307
+ const drawings = params.flatMap((param) => this._getImageCropUpdates(param));
308
+ if (drawings.length > 0) {
309
+ const { unitId } = this._context;
310
+ this._commandService.executeCommand(_univerjs_docs_drawing.UpdateDrawingDocTransformCommand.id, {
311
+ unitId,
312
+ subUnitId: unitId,
313
+ drawings
314
+ });
315
+ }
316
+ }));
317
+ }
318
+ _getImageCropUpdates(param) {
319
+ const { unitId, subUnitId, drawingId, transform } = param;
320
+ if ([unitId, subUnitId].some((id) => id !== this._context.unitId)) return [];
321
+ if (transform == null || !Object.prototype.hasOwnProperty.call(param, "srcRect")) return [];
322
+ const docDrawing = this._getDocDrawing(drawingId);
323
+ const renderDrawing = this._drawingManagerService.getDrawingByParam({
324
+ unitId,
325
+ subUnitId,
326
+ drawingId
327
+ });
328
+ if ((docDrawing === null || docDrawing === void 0 ? void 0 : docDrawing.drawingType) !== _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE || (renderDrawing === null || renderDrawing === void 0 ? void 0 : renderDrawing.transform) == null) return [];
329
+ const nextTransform = {
330
+ ...renderDrawing.transform,
331
+ ...transform
332
+ };
333
+ const drawings = [{
334
+ drawingId,
335
+ key: "srcRect",
336
+ value: param.srcRect
337
+ }];
338
+ if (nextTransform.width != null && nextTransform.height != null) drawings.push({
339
+ drawingId,
340
+ key: "size",
341
+ value: {
342
+ width: nextTransform.width,
343
+ height: nextTransform.height
344
+ }
345
+ });
346
+ if (docDrawing.layoutType === _univerjs_core.PositionedObjectLayoutType.INLINE || renderDrawing.isMultiTransform === _univerjs_core.BooleanNumber.TRUE) return drawings;
347
+ const horizontalDelta = (nextTransform.left ?? 0) - (renderDrawing.transform.left ?? 0);
348
+ const verticalDelta = (nextTransform.top ?? 0) - (renderDrawing.transform.top ?? 0);
349
+ const { positionH, positionV } = docDrawing.docTransform;
350
+ if (horizontalDelta !== 0 && positionH.posOffset != null) drawings.push({
351
+ drawingId,
352
+ key: "positionH",
353
+ value: {
354
+ relativeFrom: positionH.relativeFrom,
355
+ posOffset: positionH.posOffset + horizontalDelta
356
+ }
357
+ });
358
+ if (verticalDelta !== 0 && positionV.posOffset != null) drawings.push({
359
+ drawingId,
360
+ key: "positionV",
361
+ value: {
362
+ relativeFrom: positionV.relativeFrom,
363
+ posOffset: positionV.posOffset + verticalDelta
364
+ }
365
+ });
366
+ return drawings;
367
+ }
368
+ _getDocDrawing(drawingId) {
369
+ var _this$_context$unit$g;
370
+ return (_this$_context$unit$g = this._context.unit.getDrawings()) === null || _this$_context$unit$g === void 0 ? void 0 : _this$_context$unit$g[drawingId];
371
+ }
298
372
  _groupDrawingListener() {
299
373
  this.disposeWithMe(this._drawingManagerService.featurePluginGroupUpdate$.subscribe((params) => {
300
374
  this._commandService.executeCommand(GroupDocDrawingCommand.id, params);
@@ -317,7 +391,7 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
317
391
  }
318
392
  _transformDrawingListener() {
319
393
  const res = this._getCurrentSceneAndTransformer();
320
- if (res && res.transformer) this.disposeWithMe(res.transformer.changeEnd$.pipe((0, rxjs.debounceTime)(30)).subscribe((params) => {
394
+ if (res && res.transformer) this.disposeWithMe(res.transformer.changeEnd$.pipe((0, rxjs.debounceTime)(30)).subscribe(() => {
321
395
  this._docSelectionManagerService.refreshSelection();
322
396
  }));
323
397
  else throw new Error("transformer is not init");
@@ -382,13 +456,13 @@ let DocDrawingUpdateRenderController = class DocDrawingUpdateRenderController ex
382
456
  scene.detachTransformerFrom(shape);
383
457
  try {
384
458
  shape.setOpacity(isDocInputFocusing ? .5 : 1);
385
- } catch (e) {}
459
+ } catch {}
386
460
  if (!isDocInputFocusing) continue;
387
461
  if (isEditBody && drawing.isMultiTransform !== _univerjs_core.BooleanNumber.TRUE || !isEditBody && drawing.isMultiTransform === _univerjs_core.BooleanNumber.TRUE) {
388
462
  if (drawing.allowTransform !== false) scene.attachTransformerTo(shape);
389
463
  try {
390
464
  shape.setOpacity(1);
391
- } catch (e) {}
465
+ } catch {}
392
466
  }
393
467
  }
394
468
  }
@@ -1192,7 +1266,7 @@ const menuSchema = {
1192
1266
  //#endregion
1193
1267
  //#region package.json
1194
1268
  var name = "@univerjs/docs-drawing-ui";
1195
- var version = "1.0.0-beta.1";
1269
+ var version = "1.0.0-beta.2";
1196
1270
 
1197
1271
  //#endregion
1198
1272
  //#region src/config/config.ts
@@ -3603,6 +3677,8 @@ let DocDrawingPopupMenuController = class DocDrawingPopupMenuController extends
3603
3677
  this._floatingToolbarAdapterService = _floatingToolbarAdapterService;
3604
3678
  this._commandService = _commandService;
3605
3679
  _defineProperty(this, "_initImagePopupMenu", /* @__PURE__ */ new Set());
3680
+ _defineProperty(this, "_embeddedRenderUnits", /* @__PURE__ */ new Set());
3681
+ _defineProperty(this, "_popupMenuListeners", /* @__PURE__ */ new Map());
3606
3682
  _defineProperty(this, "_disposePopups", []);
3607
3683
  _defineProperty(this, "_isDrawingPanelOpen", false);
3608
3684
  this._init();
@@ -3624,11 +3700,21 @@ let DocDrawingPopupMenuController = class DocDrawingPopupMenuController extends
3624
3700
  }));
3625
3701
  this.disposeWithMe(this._univerInstanceService.getCurrentTypeOfUnit$(_univerjs_core.UniverInstanceType.UNIVER_DOC).pipe((0, rxjs.takeUntil)(this.dispose$)).subscribe((documentDataModel) => this._create(documentDataModel)));
3626
3702
  this.disposeWithMe(this._univerInstanceService.getTypeOfUnitDisposed$(_univerjs_core.UniverInstanceType.UNIVER_DOC).pipe((0, rxjs.takeUntil)(this.dispose$)).subscribe((documentDataModel) => this._dispose(documentDataModel)));
3703
+ this.disposeWithMe(this._renderManagerService.created$.pipe((0, rxjs.takeUntil)(this.dispose$)).subscribe((render) => {
3704
+ if (render.type !== _univerjs_core.UniverInstanceType.UNIVER_DOC || render.isMainScene) return;
3705
+ this._embeddedRenderUnits.add(render.unitId);
3706
+ this._create(this._univerInstanceService.getUnit(render.unitId, _univerjs_core.UniverInstanceType.UNIVER_DOC));
3707
+ }));
3708
+ this.disposeWithMe(this._renderManagerService.disposed$.pipe((0, rxjs.takeUntil)(this.dispose$)).subscribe((unitId) => {
3709
+ if (!this._embeddedRenderUnits.delete(unitId)) return;
3710
+ this._disposePopupMenuListener(unitId);
3711
+ }));
3627
3712
  this._univerInstanceService.getAllUnitsForType(_univerjs_core.UniverInstanceType.UNIVER_DOC).forEach((documentDataModel) => this._create(documentDataModel));
3628
3713
  }
3629
3714
  _dispose(documentDataModel) {
3630
3715
  const unitId = documentDataModel.getUnitId();
3631
3716
  this._clearPopups();
3717
+ this._disposePopupMenuListener(unitId);
3632
3718
  this._renderManagerService.removeRender(unitId);
3633
3719
  }
3634
3720
  _clearPopups() {
@@ -3640,10 +3726,20 @@ let DocDrawingPopupMenuController = class DocDrawingPopupMenuController extends
3640
3726
  const unitId = documentDataModel.getUnitId();
3641
3727
  if ((0, _univerjs_core.isInternalEditorID)(unitId)) return;
3642
3728
  if (this._renderManagerService.has(unitId) && !this._initImagePopupMenu.has(unitId)) {
3643
- this._popupMenuListener(unitId);
3644
- this._initImagePopupMenu.add(unitId);
3729
+ const listener = this._popupMenuListener(unitId);
3730
+ if (listener) {
3731
+ this._popupMenuListeners.set(unitId, listener);
3732
+ this._initImagePopupMenu.add(unitId);
3733
+ }
3645
3734
  }
3646
3735
  }
3736
+ _disposePopupMenuListener(unitId) {
3737
+ var _this$_popupMenuListe;
3738
+ (_this$_popupMenuListe = this._popupMenuListeners.get(unitId)) === null || _this$_popupMenuListe === void 0 || _this$_popupMenuListe.dispose();
3739
+ this._popupMenuListeners.delete(unitId);
3740
+ this._initImagePopupMenu.delete(unitId);
3741
+ this._clearPopups();
3742
+ }
3647
3743
  _hasCropObject(scene) {
3648
3744
  const objects = scene.getAllObjects();
3649
3745
  for (const object of objects) if (object instanceof _univerjs_drawing_ui.ImageCropperObject) return true;
@@ -3656,55 +3752,57 @@ let DocDrawingPopupMenuController = class DocDrawingPopupMenuController extends
3656
3752
  const transformer = scene.getTransformerByCreate();
3657
3753
  if (!transformer) return;
3658
3754
  const disposePopups = this._disposePopups;
3659
- this.disposeWithMe(transformer.createControl$.subscribe(() => {
3660
- if (this._hasCropObject(scene)) return;
3661
- const selectedObjects = transformer.getSelectedObjectMap();
3662
- disposePopups.forEach((dispose) => dispose.dispose());
3663
- disposePopups.length = 0;
3664
- if (this._isDrawingPanelOpen) return;
3665
- if (selectedObjects.size > 1) return;
3666
- const object = selectedObjects.values().next().value;
3667
- if (!object) return;
3668
- const oKey = object.oKey;
3669
- const drawingParam = this._drawingManagerService.getDrawingOKey(oKey);
3670
- if (!drawingParam || drawingParam.drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_DOM || drawingParam.drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_SHAPE) return;
3671
- const { unitId, subUnitId, drawingId, drawingType } = drawingParam;
3672
- const isImage = drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE;
3673
- const isChart = drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_CHART;
3674
- const popup = this._canvasPopManagerService.attachPopupToObject(object, {
3675
- componentKey: _univerjs_drawing_ui.COMPONENT_IMAGE_POPUP_MENU,
3676
- direction: isImage || isChart ? "top-center" : "horizontal",
3677
- offset: isImage || isChart ? [0, 8] : [2, 0],
3678
- extraProps: {
3679
- menuItems: this._getDrawingPopupMenuItems(unitId, subUnitId, drawingId, drawingType),
3680
- variant: isImage ? "doc-floating-toolbar" : isChart ? "doc-chart-floating-toolbar" : void 0,
3755
+ const subscriptions = [
3756
+ transformer.createControl$.subscribe(() => {
3757
+ if (this._hasCropObject(scene)) return;
3758
+ const selectedObjects = transformer.getSelectedObjectMap();
3759
+ disposePopups.forEach((dispose) => dispose.dispose());
3760
+ disposePopups.length = 0;
3761
+ if (this._isDrawingPanelOpen || selectedObjects.size > 1) return;
3762
+ const object = selectedObjects.values().next().value;
3763
+ if (!object) return;
3764
+ const drawingParam = this._drawingManagerService.getDrawingOKey(object.oKey);
3765
+ if (!drawingParam || drawingParam.drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_DOM || drawingParam.drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_SHAPE) return;
3766
+ const { unitId, subUnitId, drawingId, drawingType } = drawingParam;
3767
+ const isImage = drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_IMAGE;
3768
+ const isChart = drawingType === _univerjs_core.DrawingTypeEnum.DRAWING_CHART;
3769
+ const popup = this._canvasPopManagerService.attachPopupToObject(object, {
3770
+ componentKey: _univerjs_drawing_ui.COMPONENT_IMAGE_POPUP_MENU,
3771
+ direction: isImage || isChart ? "top-center" : "horizontal",
3772
+ offset: isImage || isChart ? [0, 8] : [2, 0],
3773
+ extraProps: {
3774
+ menuItems: this._getDrawingPopupMenuItems(unitId, subUnitId, drawingId, drawingType),
3775
+ variant: isImage ? "doc-floating-toolbar" : isChart ? "doc-chart-floating-toolbar" : void 0,
3776
+ unitId,
3777
+ subUnitId,
3778
+ drawingId
3779
+ }
3780
+ }, unitId);
3781
+ disposePopups.push(this.disposeWithMe(popup));
3782
+ if (!this._drawingManagerService.getFocusDrawings().find((drawing) => drawing.unitId === unitId && drawing.subUnitId === subUnitId && drawing.drawingId === drawingId)) this._drawingManagerService.focusDrawing([{
3681
3783
  unitId,
3682
3784
  subUnitId,
3683
3785
  drawingId
3684
- }
3685
- }, unitId);
3686
- disposePopups.push(this.disposeWithMe(popup));
3687
- if (this._drawingManagerService.getFocusDrawings().find((drawing) => drawing.unitId === unitId && drawing.subUnitId === subUnitId && drawing.drawingId === drawingId)) return;
3688
- this._drawingManagerService.focusDrawing([{
3689
- unitId,
3690
- subUnitId,
3691
- drawingId
3692
- }]);
3693
- }));
3694
- this.disposeWithMe(transformer.clearControl$.subscribe(() => {
3695
- disposePopups.forEach((dispose) => dispose.dispose());
3696
- disposePopups.length = 0;
3697
- this._contextService.setContextValue(_univerjs_core.FOCUSING_COMMON_DRAWINGS, false);
3698
- this._drawingManagerService.focusDrawing(null);
3699
- }));
3700
- this.disposeWithMe(transformer.changing$.subscribe(() => {
3701
- disposePopups.forEach((dispose) => dispose.dispose());
3702
- disposePopups.length = 0;
3703
- }));
3704
- this.disposeWithMe(transformer.changeStart$.subscribe(() => {
3705
- disposePopups.forEach((dispose) => dispose.dispose());
3706
- disposePopups.length = 0;
3707
- }));
3786
+ }]);
3787
+ }),
3788
+ transformer.clearControl$.subscribe(() => {
3789
+ disposePopups.forEach((dispose) => dispose.dispose());
3790
+ disposePopups.length = 0;
3791
+ this._contextService.setContextValue(_univerjs_core.FOCUSING_COMMON_DRAWINGS, false);
3792
+ this._drawingManagerService.focusDrawing(null);
3793
+ }),
3794
+ transformer.changing$.subscribe(() => {
3795
+ disposePopups.forEach((dispose) => dispose.dispose());
3796
+ disposePopups.length = 0;
3797
+ }),
3798
+ transformer.changeStart$.subscribe(() => {
3799
+ disposePopups.forEach((dispose) => dispose.dispose());
3800
+ disposePopups.length = 0;
3801
+ })
3802
+ ];
3803
+ const disposable = (0, _univerjs_core.toDisposable)(() => subscriptions.forEach((subscription) => subscription.unsubscribe()));
3804
+ this.disposeWithMe(disposable);
3805
+ return disposable;
3708
3806
  }
3709
3807
  _getDrawingPopupMenuItems(unitId, subUnitId, drawingId, drawingType) {
3710
3808
  const drawing = this._drawingManagerService.getDrawingByParam({