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