@univerjs/drawing 0.20.1 → 0.21.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/es/index.js CHANGED
@@ -1 +1,1059 @@
1
- import{CommandType as e,Disposable as t,DrawingTypeEnum as n,ICommandService as r,IConfigService as i,IImageIoService as a,IURLImageService as o,ImageSourceType as s,ImageUploadStatusType as c,Inject as l,Injector as u,Plugin as d,createIdentifier as f,generateRandomId as p,merge as m,mergeOverrideWithDependencies as h,sortRules as g,sortRulesByDesc as _,toDisposable as v}from"@univerjs/core";import*as y from"ot-json1";import{Subject as b}from"rxjs";const x=500,S=500,C=10,w=5*1024*1024,T=[`image/png`,`image/jpeg`,`image/jpg`,`image/gif`,`image/bmp`],E=f(`univer.drawing-manager.service`),D={id:`drawing.operation.set-drawing-selected`,type:e.OPERATION,handler:(e,t)=>{let n=e.get(E);return t==null?!1:(n.focusDrawing(t),!0)}};var O=`@univerjs/drawing`,k=`0.20.1`;const A=`drawing.config`;Symbol(A);const j={};function M(e){"@babel/helpers - typeof";return M=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},M(e)}function N(e,t){if(M(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(M(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function P(e){var t=N(e,`string`);return M(t)==`symbol`?t:t+``}function F(e,t,n){return(t=P(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var I=function(e){return e.data=`data`,e.order=`order`,e}(I||{}),L=class{constructor(){F(this,`drawingManagerData`,{}),F(this,`_oldDrawingManagerData`,{}),F(this,`_focusDrawings`,[]),F(this,`_remove$`,new b),F(this,`remove$`,this._remove$.asObservable()),F(this,`_add$`,new b),F(this,`add$`,this._add$.asObservable()),F(this,`_update$`,new b),F(this,`update$`,this._update$.asObservable()),F(this,`_order$`,new b),F(this,`order$`,this._order$.asObservable()),F(this,`_group$`,new b),F(this,`group$`,this._group$.asObservable()),F(this,`_ungroup$`,new b),F(this,`ungroup$`,this._ungroup$.asObservable()),F(this,`_refreshTransform$`,new b),F(this,`refreshTransform$`,this._refreshTransform$.asObservable()),F(this,`_visible$`,new b),F(this,`visible$`,this._visible$.asObservable()),F(this,`_focus$`,new b),F(this,`focus$`,this._focus$.asObservable()),F(this,`_featurePluginUpdate$`,new b),F(this,`featurePluginUpdate$`,this._featurePluginUpdate$.asObservable()),F(this,`_featurePluginAdd$`,new b),F(this,`featurePluginAdd$`,this._featurePluginAdd$.asObservable()),F(this,`_featurePluginRemove$`,new b),F(this,`featurePluginRemove$`,this._featurePluginRemove$.asObservable()),F(this,`_featurePluginOrderUpdate$`,new b),F(this,`featurePluginOrderUpdate$`,this._featurePluginOrderUpdate$.asObservable()),F(this,`_featurePluginGroupUpdate$`,new b),F(this,`featurePluginGroupUpdate$`,this._featurePluginGroupUpdate$.asObservable()),F(this,`_featurePluginUngroupUpdate$`,new b),F(this,`featurePluginUngroupUpdate$`,this._featurePluginUngroupUpdate$.asObservable()),F(this,`_visible`,!0),F(this,`_editable`,!0)}dispose(){this._remove$.complete(),this._add$.complete(),this._update$.complete(),this._order$.complete(),this._focus$.complete(),this._featurePluginUpdate$.complete(),this._featurePluginAdd$.complete(),this._featurePluginRemove$.complete(),this._featurePluginOrderUpdate$.complete(),this.drawingManagerData={},this._oldDrawingManagerData={}}visibleNotification(e){this._visible$.next(e)}refreshTransform(e){e.forEach(e=>{let t=this._getCurrentBySearch(e);t!=null&&(t.transform=e.transform,t.transforms=e.transforms,t.isMultiTransform=e.isMultiTransform)}),this.refreshTransformNotification(e)}getDrawingDataForUnit(e){return this.drawingManagerData[e]||{}}removeDrawingDataForUnit(e){let t=this.drawingManagerData[e];if(t==null)return;delete this.drawingManagerData[e];let n=[];Object.keys(t).forEach(r=>{let i=t[r];(i==null?void 0:i.data)!=null&&Object.keys(i.data).forEach(t=>{n.push({unitId:e,subUnitId:r,drawingId:t})})}),n.length>0&&this.removeNotification(n)}registerDrawingData(e,t){this.drawingManagerData[e]=t}initializeNotification(e){let t=[],n=this.drawingManagerData[e];n!=null&&(Object.keys(n).forEach(r=>{this._establishDrawingMap(e,r);let i=n[r];Object.keys(i.data).forEach(n=>{let a=i.data[n];a.unitId=e,a.subUnitId=r,t.push(a)})}),t.length>0&&this.addNotification(t))}getDrawingData(e,t){return this._getDrawingData(e,t)}setDrawingData(e,t,n){this.drawingManagerData[e][t].data=n}getBatchAddOp(e){let t=[],n=[],r=[];e.forEach(e=>{let{op:i,invertOp:a}=this._addByParam(e);t.push({unitId:e.unitId,subUnitId:e.subUnitId,drawingId:e.drawingId}),n.push(i),r.push(a)});let i=n.reduce(y.type.compose,null),a=r.reduce(y.type.compose,null),{unitId:o,subUnitId:s}=e[0];return{undo:a,redo:i,unitId:o,subUnitId:s,objects:t}}getBatchRemoveOpInOder(e){if(e.length===0)return{undo:null,redo:null,unitId:``,subUnitId:``,objects:[]};let t=this.getDrawingOrder(e[0].unitId,e[0].subUnitId),n=new Map;return t.forEach((e,t)=>{n.set(e,t)}),e.sort((e,t)=>{var r,i;return((r=n.get(e.drawingId))==null?-1/0:r)-((i=n.get(t.drawingId))==null?-1/0:i)}),this.getBatchRemoveOp(e)}getBatchRemoveOp(e){var t;let r=new Set,i=[];e.forEach(e=>{let t=this.getDrawingByParam(e);if((t==null?void 0:t.drawingType)===n.DRAWING_GROUP){let t=this.getDrawingsByGroupNested(e);if(t){let{flatChildren:e,groups:n}=t;[...e==null?[]:e,...n].forEach(e=>{r.has(e.drawingId)||(r.add(e.drawingId),i.push({unitId:e.unitId,subUnitId:e.subUnitId,drawingId:e.drawingId}))})}else r.has(e.drawingId)||(r.add(e.drawingId),i.push(e))}else r.has(e.drawingId)||(r.add(e.drawingId),i.push(e))});let{unitId:a,subUnitId:o}=(t=i[0])==null?e[0]:t,s=this._getDrawingOrder(a,o),c=new Map;s.forEach((e,t)=>c.set(e,t)),i.sort((e,t)=>{var n,r;return((n=c.get(e.drawingId))==null?-1/0:n)-((r=c.get(t.drawingId))==null?-1/0:r)});let l=[],u=[];i.forEach(e=>{let{op:t,invertOp:n}=this._removeByParam(e);l.unshift(t),u.push(n)});let d=l.reduce(y.type.compose,null);return{undo:u.reduce(y.type.compose,null),redo:d,unitId:a,subUnitId:o,objects:i}}getBatchUpdateOp(e){let t=[],n=[],r=[];e.forEach(e=>{let{op:i,invertOp:a}=this._updateByParam(e);t.push({unitId:e.unitId,subUnitId:e.subUnitId,drawingId:e.drawingId}),n.push(i),r.push(a)});let i=n.reduce(y.type.compose,null),a=r.reduce(y.type.compose,null),{unitId:o,subUnitId:s}=e[0];return{undo:a,redo:i,unitId:o,subUnitId:s,objects:t}}removeNotification(e){this._remove$.next(e)}addNotification(e){this._add$.next(e)}updateNotification(e){this._update$.next(e)}orderNotification(e){this._order$.next(e)}groupUpdateNotification(e){this._group$.next(e)}ungroupUpdateNotification(e){this._ungroup$.next(e)}refreshTransformNotification(e){this._refreshTransform$.next(e)}getGroupDrawingOp(e){let t=[],{unitId:n,subUnitId:r}=e[0].parent;e.forEach(e=>{t.push(this._getGroupDrawingOp(e))});let i=t.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(i,this.drawingManagerData),redo:i,unitId:n,subUnitId:r,objects:e}}getUngroupDrawingOp(e){let t=[],{unitId:n,subUnitId:r}=e[0].parent;e.forEach(e=>{t.push(this._getUngroupDrawingOp(e))});let i=t.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(i,this.drawingManagerData),redo:i,unitId:n,subUnitId:r,objects:e}}getDrawingsByGroup(e){let{unitId:t,subUnitId:n,drawingId:r}=e;if(this.getDrawingByParam({unitId:t,subUnitId:n,drawingId:r})==null)return[];let i=this._getDrawingData(t,n),a=[];return Object.keys(i).forEach(e=>{let t=i[e];t.groupId===r&&a.push(t)}),a}getDrawingsByGroupNested(e){let{unitId:t,subUnitId:r}=e,i=this.getDrawingByParam(e);if(!i)return null;let a=this._getDrawingData(t,r),o=new Map;Object.values(a).forEach(e=>{e.groupId!=null&&(o.has(e.groupId)||o.set(e.groupId,[]),o.get(e.groupId).push(e.drawingId))});let s=[],c=[],l={},u=e=>{var t;let{drawingId:r}=e,i=(t=o.get(r))==null?[]:t;l[r]={drawingId:r,children:i},i.forEach(e=>{let t=a[e];t&&(t.drawingType===n.DRAWING_GROUP?(u(t),c.push(t)):s.push(t))})};return u(i),c.push(i),{nestedIdRecord:l,flatChildren:s,groups:c}}_getGroupDrawingOp(e){let{parent:t,children:n}=e,{unitId:r,subUnitId:i,drawingId:a}=t,o=[];o.push(y.insertOp([r,i,I.data,a],t));let s=-1/0;return n.forEach(e=>{let{unitId:t,subUnitId:n,drawingId:r}=e,i=this._hasDrawingOrder({unitId:t,subUnitId:n,drawingId:r});s=Math.max(s,i),o.push(...this._getUpdateParamCompareOp(e,this.getDrawingByParam({unitId:t,subUnitId:n,drawingId:r})))}),s===-1/0&&(s=this._getDrawingOrder(r,i).length),o.push(y.insertOp([r,i,I.order,s],a)),o.reduce(y.type.compose,null)}_getUngroupDrawingOp(e){let{parent:t,children:n}=e,{unitId:r,subUnitId:i,drawingId:a}=t,o=[];return n.forEach(e=>{let{unitId:t,subUnitId:n,drawingId:r}=e;o.push(...this._getUpdateParamCompareOp(e,this.getDrawingByParam({unitId:t,subUnitId:n,drawingId:r})))}),o.push(y.removeOp([r,i,I.data,a],!0)),o.push(y.removeOp([r,i,I.order,this._getDrawingOrder(r,i).indexOf(a)],!0)),o.reduce(y.type.compose,null)}applyJson1(e,t,n){this._establishDrawingMap(e,t),this._oldDrawingManagerData={...this.drawingManagerData},this.drawingManagerData=y.type.apply(this.drawingManagerData,n)}featurePluginUpdateNotification(e){this._featurePluginUpdate$.next(e)}featurePluginOrderUpdateNotification(e){this._featurePluginOrderUpdate$.next(e)}featurePluginAddNotification(e){this._featurePluginAdd$.next(e)}featurePluginRemoveNotification(e){this._featurePluginRemove$.next(e)}featurePluginGroupUpdateNotification(e){this._featurePluginGroupUpdate$.next(e)}featurePluginUngroupUpdateNotification(e){this._featurePluginUngroupUpdate$.next(e)}getDrawingByParam(e){return this._getCurrentBySearch(e)}getOldDrawingByParam(e){return this._getOldBySearch(e)}getDrawingOKey(e){let[t,n,r]=e.split(`#-#`);return this._getCurrentBySearch({unitId:t,subUnitId:n,drawingId:r})}focusDrawing(e){if(e==null||e.length===0){this._focusDrawings=[],this._focus$.next([]);return}let t=[];e.forEach(e=>{var n;let{unitId:r,subUnitId:i,drawingId:a}=e,o=(n=this._getDrawingData(r,i))==null?void 0:n[a];o!=null&&t.push(o)}),t.length>0&&(this._focusDrawings=t,this._focus$.next(t))}getFocusDrawings(){let e=[];return this._focusDrawings.forEach(t=>{var n;let{unitId:r,subUnitId:i,drawingId:a}=t,o=(n=this._getDrawingData(r,i))==null?void 0:n[a];o!=null&&e.push(o)}),e}getDrawingOrder(e,t){return this._getDrawingOrder(e,t)}setDrawingOrder(e,t,n){this.drawingManagerData[e][t].order=n}orderUpdateNotification(e){this._order$.next(e)}getForwardDrawingsOp(e){let{unitId:t,subUnitId:n,drawingIds:r}=e,i=[],a=this.getDrawingOrder(t,n),o=[...r];r.forEach(e=>{let r=this._hasDrawingOrder({unitId:t,subUnitId:n,drawingId:e});if(r===-1||r===a.length-1)return;let s=y.moveOp([t,n,I.order,r],[t,n,I.order,r+1]);i.push(s),o.includes(a[r+1])||o.push(a[r+1])});let s=i.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(s,this.drawingManagerData),redo:s,unitId:t,subUnitId:n,objects:{...e,drawingIds:o}}}getBackwardDrawingOp(e){let{unitId:t,subUnitId:n,drawingIds:r}=e,i=[],a=this.getDrawingOrder(t,n),o=[...r];r.forEach(e=>{let r=this._hasDrawingOrder({unitId:t,subUnitId:n,drawingId:e});if(r===-1||r===0)return;let s=y.moveOp([t,n,I.order,r],[t,n,I.order,r-1]);i.push(s),o.includes(a[r-1])||o.push(a[r-1])});let s=i.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(s,this.drawingManagerData),redo:s,unitId:t,subUnitId:n,objects:{...e,drawingIds:o}}}getFrontDrawingsOp(e){let{unitId:t,subUnitId:n,drawingIds:r}=e,i=this._getOrderFromSearchParams(t,n,r),a=[...r],o=this.getDrawingOrder(t,n),s=[];i.forEach(e=>{let{drawingId:r}=e,i=this._getDrawingCount(t,n)-1,c=y.moveOp([t,n,I.order,this._getDrawingOrder(t,n).indexOf(r)],[t,n,I.order,i]);s.push(c),a.includes(o[i])||a.push(o[i])});let c=s.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(c,this.drawingManagerData),redo:c,unitId:t,subUnitId:n,objects:{...e,drawingIds:a}}}getBackDrawingsOp(e){let{unitId:t,subUnitId:n,drawingIds:r}=e,i=this._getOrderFromSearchParams(t,n,r,!0),a=[...r],o=this.getDrawingOrder(t,n),s=[];i.forEach(e=>{let{drawingId:r}=e,i=y.moveOp([t,n,I.order,this._getDrawingOrder(t,n).indexOf(r)],[t,n,I.order,0]);s.push(i),a.includes(o[0])||a.push(o[0])});let c=s.reduce(y.type.compose,null);return{undo:y.type.invertWithDoc(c,this.drawingManagerData),redo:c,unitId:t,subUnitId:n,objects:{...e,drawingIds:a}}}_getDrawingCount(e,t){return this.getDrawingOrder(e,t).length||0}_getOrderFromSearchParams(e,t,n,r=!1){return n.map(n=>({drawingId:n,zIndex:this._hasDrawingOrder({unitId:e,subUnitId:t,drawingId:n})})).sort(r===!1?g:_)}_hasDrawingOrder(e){if(e==null)return-1;let{unitId:t,subUnitId:n,drawingId:r}=e;return this._establishDrawingMap(t,n),this._getDrawingOrder(t,n).indexOf(r)}_getCurrentBySearch(e){var t;if(e==null)return;let{unitId:n,subUnitId:r,drawingId:i}=e;return(t=this.drawingManagerData[n])==null||(t=t[r])==null||(t=t.data)==null?void 0:t[i]}_getOldBySearch(e){var t;if(e==null)return;let{unitId:n,subUnitId:r,drawingId:i}=e;return(t=this._oldDrawingManagerData[n])==null||(t=t[r])==null||(t=t.data)==null?void 0:t[i]}_establishDrawingMap(e,t,n){var r;return this.drawingManagerData[e]||(this.drawingManagerData[e]={}),this.drawingManagerData[e][t]||(this.drawingManagerData[e][t]={data:{},order:[]}),n==null?null:(r=this.drawingManagerData[e][t].data)==null?void 0:r[n]}_addByParam(e){let{unitId:t,subUnitId:n,drawingId:r}=e;this._establishDrawingMap(t,n,r);let i=[y.insertOp([t,n,I.data,r],e),y.insertOp([t,n,I.order,this._getDrawingOrder(t,n).length],r)].reduce(y.type.compose,null);return{op:i,invertOp:y.type.invertWithDoc(i,this.drawingManagerData)}}_removeByParam(e){if(e==null)return{op:[],invertOp:[]};let{unitId:t,subUnitId:n,drawingId:r}=e;if(this._establishDrawingMap(t,n,r)==null)return{op:[],invertOp:[]};let i=[y.removeOp([t,n,I.data,r],!0),y.removeOp([t,n,I.order,this._getDrawingOrder(t,n).indexOf(r)],!0)].reduce(y.type.compose,null);return{op:i,invertOp:y.type.invertWithDoc(i,this.drawingManagerData)}}_updateByParam(e){let{unitId:t,subUnitId:n,drawingId:r}=e,i=this._establishDrawingMap(t,n,r);if(i==null)return{op:[],invertOp:[]};let a=this._getUpdateParamCompareOp(e,i).reduce(y.type.compose,null);return{op:a,invertOp:y.type.invertWithDoc(a,this.drawingManagerData)}}_getUpdateParamCompareOp(e,t){let{unitId:n,subUnitId:r,drawingId:i}=e,a=[];return Object.keys(e).forEach(o=>{let s=e[o],c=t[o];c!==s&&a.push(y.replaceOp([n,r,I.data,i,o],c,s))}),a}_getDrawingData(e,t){var n;return((n=this.drawingManagerData[e])==null||(n=n[t])==null?void 0:n.data)||{}}_getDrawingOrder(e,t){var n;return((n=this.drawingManagerData[e])==null||(n=n[t])==null?void 0:n.order)||[]}getDrawingVisible(){return this._visible}getDrawingEditable(){return this._editable}setDrawingVisible(e){this._visible=e}setDrawingEditable(e){this._editable=e}},R=class extends L{},z=class{constructor(){F(this,`_waitCount`,0),F(this,`_change$`,new b),F(this,`change$`,this._change$),F(this,`_imageSourceCache`,new Map)}setWaitCount(e){this._waitCount=e,this._change$.next(e)}getImageSourceCache(e,t){if(t===s.BASE64){let t=new Image;return t.src=e,t}return this._imageSourceCache.get(e)}addImageSourceCache(e,t,n){t===s.BASE64||n==null||this._imageSourceCache.set(e,n)}async getImage(e){return Promise.resolve(e)}async saveImage(e){return new Promise((t,n)=>{if(!T.includes(e.type)){n(Error(c.ERROR_IMAGE_TYPE)),this._decreaseWaiting();return}if(e.size>5242880){n(Error(c.ERROR_EXCEED_SIZE)),this._decreaseWaiting();return}let r=new FileReader;r.readAsDataURL(e),r.onload=e=>{var r;let i=(r=e.target)==null?void 0:r.result;if(i==null){n(Error(c.ERROR_IMAGE)),this._decreaseWaiting();return}t({imageId:p(6),imageSourceType:s.BASE64,source:i,base64Cache:i,status:c.SUCCUSS}),this._decreaseWaiting()}})}_decreaseWaiting(){--this._waitCount,this._change$.next(this._waitCount)}},B=class extends t{constructor(...e){super(...e),F(this,`_urlImageDownloader`,null)}registerURLImageDownloader(e){return this._urlImageDownloader=e,v(()=>{this._urlImageDownloader=null})}async getImage(e){if(this._urlImageDownloader)try{return await this._urlImageDownloader(e)}catch(t){console.error(`Custom downloader failed for ${e}, falling back to default behavior:`,t)}return e}async downloadImage(e){if(this._urlImageDownloader)try{let t=await this._urlImageDownloader(e);return await(await fetch(t)).blob()}catch(t){console.error(`Custom downloader failed for ${e}, falling back to default fetch:`,t)}return await(await fetch(e)).blob()}};function V(e,t){return function(n,r){t(n,r,e)}}function H(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let U=class extends d{constructor(e=j,t,n,r){super(),this._config=e,this._injector=t,this._configService=n,this._commandService=r;let{...i}=m({},j,this._config);this._configService.setConfig(A,i)}onStarting(){this._initCommands(),this._initDependencies()}_initDependencies(){var e;h([[a,{useClass:z}],[o,{useClass:B}],[E,{useClass:R}]],(e=this._config)==null?void 0:e.override).forEach(e=>this._injector.add(e))}_initCommands(){[D].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};F(U,`pluginName`,`UNIVER_DRAWING_PLUGIN`),F(U,`packageName`,O),F(U,`version`,k),U=H([V(1,l(u)),V(2,i),V(3,r)],U);function W({unitId:e,subUnitId:t,drawingId:n},r){return typeof r==`number`?`${e}#-#${t}#-#${n}#-#${r}`:`${e}#-#${t}#-#${n}`}const G=async e=>new Promise((t,n)=>{let r=new Image;r.src=e,r.onload=()=>{t({width:r.width,height:r.height,image:r})},r.onerror=e=>{n(e)}});export{T as DRAWING_IMAGE_ALLOW_IMAGE_LIST,w as DRAWING_IMAGE_ALLOW_SIZE,C as DRAWING_IMAGE_COUNT_LIMIT,S as DRAWING_IMAGE_HEIGHT_LIMIT,x as DRAWING_IMAGE_WIDTH_LIMIT,R as DrawingManagerService,E as IDrawingManagerService,a as IImageIoService,z as ImageIoService,s as ImageSourceType,c as ImageUploadStatusType,D as SetDrawingSelectedOperation,B as URLImageService,L as UnitDrawingService,U as UniverDrawingPlugin,W as getDrawingShapeKeyByDrawingSearch,G as getImageSize};
1
+ import { CommandType, Disposable, DrawingTypeEnum, ICommandService, IConfigService, IImageIoService, IURLImageService, ImageSourceType, ImageUploadStatusType, Inject, Injector, Plugin, createIdentifier, generateRandomId, merge, mergeOverrideWithDependencies, sortRules, sortRulesByDesc, toDisposable } from "@univerjs/core";
2
+ import * as json1 from "ot-json1";
3
+ import { Subject } from "rxjs";
4
+
5
+ //#region src/basics/config.ts
6
+ /**
7
+ * Copyright 2023-present DreamNum Co., Ltd.
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+ const DRAWING_IMAGE_WIDTH_LIMIT = 500;
22
+ const DRAWING_IMAGE_HEIGHT_LIMIT = 500;
23
+ const DRAWING_IMAGE_COUNT_LIMIT = 10;
24
+ const DRAWING_IMAGE_ALLOW_SIZE = 5 * 1024 * 1024;
25
+ const DRAWING_IMAGE_ALLOW_IMAGE_LIST = [
26
+ "image/png",
27
+ "image/jpeg",
28
+ "image/jpg",
29
+ "image/gif",
30
+ "image/bmp"
31
+ ];
32
+
33
+ //#endregion
34
+ //#region src/services/drawing-manager.service.ts
35
+ const IDrawingManagerService = createIdentifier("univer.drawing-manager.service");
36
+
37
+ //#endregion
38
+ //#region src/commands/operations/set-drawing-selected.operation.ts
39
+ const SetDrawingSelectedOperation = {
40
+ id: "drawing.operation.set-drawing-selected",
41
+ type: CommandType.OPERATION,
42
+ handler: (accessor, params) => {
43
+ const drawingManagerService = accessor.get(IDrawingManagerService);
44
+ if (params == null) return false;
45
+ drawingManagerService.focusDrawing(params);
46
+ return true;
47
+ }
48
+ };
49
+
50
+ //#endregion
51
+ //#region package.json
52
+ var name = "@univerjs/drawing";
53
+ var version = "0.21.0";
54
+
55
+ //#endregion
56
+ //#region src/config/config.ts
57
+ const DRAWING_PLUGIN_CONFIG_KEY = "drawing.config";
58
+ const configSymbol = Symbol(DRAWING_PLUGIN_CONFIG_KEY);
59
+ const defaultPluginConfig = {};
60
+
61
+ //#endregion
62
+ //#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
63
+ function _typeof(o) {
64
+ "@babel/helpers - typeof";
65
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
66
+ return typeof o;
67
+ } : function(o) {
68
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
69
+ }, _typeof(o);
70
+ }
71
+
72
+ //#endregion
73
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
74
+ function toPrimitive(t, r) {
75
+ if ("object" != _typeof(t) || !t) return t;
76
+ var e = t[Symbol.toPrimitive];
77
+ if (void 0 !== e) {
78
+ var i = e.call(t, r || "default");
79
+ if ("object" != _typeof(i)) return i;
80
+ throw new TypeError("@@toPrimitive must return a primitive value.");
81
+ }
82
+ return ("string" === r ? String : Number)(t);
83
+ }
84
+
85
+ //#endregion
86
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
87
+ function toPropertyKey(t) {
88
+ var i = toPrimitive(t, "string");
89
+ return "symbol" == _typeof(i) ? i : i + "";
90
+ }
91
+
92
+ //#endregion
93
+ //#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
94
+ function _defineProperty(e, r, t) {
95
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
96
+ value: t,
97
+ enumerable: !0,
98
+ configurable: !0,
99
+ writable: !0
100
+ }) : e[r] = t, e;
101
+ }
102
+
103
+ //#endregion
104
+ //#region src/services/drawing-manager-impl.service.ts
105
+ var DrawingMapItemType = /* @__PURE__ */ function(DrawingMapItemType) {
106
+ DrawingMapItemType["data"] = "data";
107
+ DrawingMapItemType["order"] = "order";
108
+ return DrawingMapItemType;
109
+ }(DrawingMapItemType || {});
110
+ /**
111
+ * unitId -> subUnitId -> drawingId -> drawingParam
112
+ */
113
+ var UnitDrawingService = class {
114
+ constructor() {
115
+ _defineProperty(this, "drawingManagerData", {});
116
+ _defineProperty(this, "_oldDrawingManagerData", {});
117
+ _defineProperty(this, "_focusDrawings", []);
118
+ _defineProperty(this, "_remove$", new Subject());
119
+ _defineProperty(this, "remove$", this._remove$.asObservable());
120
+ _defineProperty(this, "_add$", new Subject());
121
+ _defineProperty(this, "add$", this._add$.asObservable());
122
+ _defineProperty(this, "_update$", new Subject());
123
+ _defineProperty(this, "update$", this._update$.asObservable());
124
+ _defineProperty(this, "_order$", new Subject());
125
+ _defineProperty(this, "order$", this._order$.asObservable());
126
+ _defineProperty(this, "_group$", new Subject());
127
+ _defineProperty(this, "group$", this._group$.asObservable());
128
+ _defineProperty(this, "_ungroup$", new Subject());
129
+ _defineProperty(this, "ungroup$", this._ungroup$.asObservable());
130
+ _defineProperty(this, "_refreshTransform$", new Subject());
131
+ _defineProperty(this, "refreshTransform$", this._refreshTransform$.asObservable());
132
+ _defineProperty(this, "_visible$", new Subject());
133
+ _defineProperty(this, "visible$", this._visible$.asObservable());
134
+ _defineProperty(this, "_focus$", new Subject());
135
+ _defineProperty(this, "focus$", this._focus$.asObservable());
136
+ _defineProperty(this, "_featurePluginUpdate$", new Subject());
137
+ _defineProperty(this, "featurePluginUpdate$", this._featurePluginUpdate$.asObservable());
138
+ _defineProperty(this, "_featurePluginAdd$", new Subject());
139
+ _defineProperty(this, "featurePluginAdd$", this._featurePluginAdd$.asObservable());
140
+ _defineProperty(this, "_featurePluginRemove$", new Subject());
141
+ _defineProperty(this, "featurePluginRemove$", this._featurePluginRemove$.asObservable());
142
+ _defineProperty(this, "_featurePluginOrderUpdate$", new Subject());
143
+ _defineProperty(this, "featurePluginOrderUpdate$", this._featurePluginOrderUpdate$.asObservable());
144
+ _defineProperty(this, "_featurePluginGroupUpdate$", new Subject());
145
+ _defineProperty(this, "featurePluginGroupUpdate$", this._featurePluginGroupUpdate$.asObservable());
146
+ _defineProperty(this, "_featurePluginUngroupUpdate$", new Subject());
147
+ _defineProperty(this, "featurePluginUngroupUpdate$", this._featurePluginUngroupUpdate$.asObservable());
148
+ _defineProperty(this, "_visible", true);
149
+ _defineProperty(this, "_editable", true);
150
+ }
151
+ dispose() {
152
+ this._remove$.complete();
153
+ this._add$.complete();
154
+ this._update$.complete();
155
+ this._order$.complete();
156
+ this._focus$.complete();
157
+ this._featurePluginUpdate$.complete();
158
+ this._featurePluginAdd$.complete();
159
+ this._featurePluginRemove$.complete();
160
+ this._featurePluginOrderUpdate$.complete();
161
+ this.drawingManagerData = {};
162
+ this._oldDrawingManagerData = {};
163
+ }
164
+ visibleNotification(visibleParams) {
165
+ this._visible$.next(visibleParams);
166
+ }
167
+ refreshTransform(updateParams) {
168
+ updateParams.forEach((updateParam) => {
169
+ const param = this._getCurrentBySearch(updateParam);
170
+ if (param == null) return;
171
+ param.transform = updateParam.transform;
172
+ param.transforms = updateParam.transforms;
173
+ param.isMultiTransform = updateParam.isMultiTransform;
174
+ });
175
+ this.refreshTransformNotification(updateParams);
176
+ }
177
+ getDrawingDataForUnit(unitId) {
178
+ return this.drawingManagerData[unitId] || {};
179
+ }
180
+ removeDrawingDataForUnit(unitId) {
181
+ const subUnits = this.drawingManagerData[unitId];
182
+ if (subUnits == null) return;
183
+ delete this.drawingManagerData[unitId];
184
+ const drawings = [];
185
+ Object.keys(subUnits).forEach((subUnitId) => {
186
+ const subUnit = subUnits[subUnitId];
187
+ if ((subUnit === null || subUnit === void 0 ? void 0 : subUnit.data) == null) return;
188
+ Object.keys(subUnit.data).forEach((drawingId) => {
189
+ drawings.push({
190
+ unitId,
191
+ subUnitId,
192
+ drawingId
193
+ });
194
+ });
195
+ });
196
+ if (drawings.length > 0) this.removeNotification(drawings);
197
+ }
198
+ registerDrawingData(unitId, data) {
199
+ this.drawingManagerData[unitId] = data;
200
+ }
201
+ initializeNotification(unitId) {
202
+ const drawings = [];
203
+ const data = this.drawingManagerData[unitId];
204
+ if (data == null) return;
205
+ Object.keys(data).forEach((subUnitId) => {
206
+ this._establishDrawingMap(unitId, subUnitId);
207
+ const subUnitData = data[subUnitId];
208
+ Object.keys(subUnitData.data).forEach((drawingId) => {
209
+ const drawing = subUnitData.data[drawingId];
210
+ drawing.unitId = unitId;
211
+ drawing.subUnitId = subUnitId;
212
+ drawings.push(drawing);
213
+ });
214
+ });
215
+ if (drawings.length > 0) this.addNotification(drawings);
216
+ }
217
+ getDrawingData(unitId, subUnitId) {
218
+ return this._getDrawingData(unitId, subUnitId);
219
+ }
220
+ setDrawingData(unitId, subUnitId, data) {
221
+ this.drawingManagerData[unitId][subUnitId].data = data;
222
+ }
223
+ getBatchAddOp(insertParams) {
224
+ const objects = [];
225
+ const ops = [];
226
+ const invertOps = [];
227
+ insertParams.forEach((insertParam) => {
228
+ const { op, invertOp } = this._addByParam(insertParam);
229
+ objects.push({
230
+ unitId: insertParam.unitId,
231
+ subUnitId: insertParam.subUnitId,
232
+ drawingId: insertParam.drawingId
233
+ });
234
+ ops.push(op);
235
+ invertOps.push(invertOp);
236
+ });
237
+ const op = ops.reduce(json1.type.compose, null);
238
+ const invertOp = invertOps.reduce(json1.type.compose, null);
239
+ const { unitId, subUnitId } = insertParams[0];
240
+ return {
241
+ undo: invertOp,
242
+ redo: op,
243
+ unitId,
244
+ subUnitId,
245
+ objects
246
+ };
247
+ }
248
+ getBatchRemoveOpInOder(removeParams) {
249
+ if (removeParams.length === 0) return {
250
+ undo: null,
251
+ redo: null,
252
+ unitId: "",
253
+ subUnitId: "",
254
+ objects: []
255
+ };
256
+ const orders = this.getDrawingOrder(removeParams[0].unitId, removeParams[0].subUnitId);
257
+ const orderIndexMap = /* @__PURE__ */ new Map();
258
+ orders.forEach((drawingId, index) => {
259
+ orderIndexMap.set(drawingId, index);
260
+ });
261
+ removeParams.sort((a, b) => {
262
+ var _orderIndexMap$get, _orderIndexMap$get2;
263
+ return ((_orderIndexMap$get = orderIndexMap.get(a.drawingId)) !== null && _orderIndexMap$get !== void 0 ? _orderIndexMap$get : Number.NEGATIVE_INFINITY) - ((_orderIndexMap$get2 = orderIndexMap.get(b.drawingId)) !== null && _orderIndexMap$get2 !== void 0 ? _orderIndexMap$get2 : Number.NEGATIVE_INFINITY);
264
+ });
265
+ return this.getBatchRemoveOp(removeParams);
266
+ }
267
+ getBatchRemoveOp(removeParams) {
268
+ var _allToRemove$;
269
+ const seenIds = /* @__PURE__ */ new Set();
270
+ const allToRemove = [];
271
+ removeParams.forEach((removeParam) => {
272
+ const drawing = this.getDrawingByParam(removeParam);
273
+ if ((drawing === null || drawing === void 0 ? void 0 : drawing.drawingType) === DrawingTypeEnum.DRAWING_GROUP) {
274
+ const nested = this.getDrawingsByGroupNested(removeParam);
275
+ if (nested) {
276
+ const { flatChildren, groups } = nested;
277
+ [...flatChildren !== null && flatChildren !== void 0 ? flatChildren : [], ...groups].forEach((d) => {
278
+ if (!seenIds.has(d.drawingId)) {
279
+ seenIds.add(d.drawingId);
280
+ allToRemove.push({
281
+ unitId: d.unitId,
282
+ subUnitId: d.subUnitId,
283
+ drawingId: d.drawingId
284
+ });
285
+ }
286
+ });
287
+ } else if (!seenIds.has(removeParam.drawingId)) {
288
+ seenIds.add(removeParam.drawingId);
289
+ allToRemove.push(removeParam);
290
+ }
291
+ } else if (!seenIds.has(removeParam.drawingId)) {
292
+ seenIds.add(removeParam.drawingId);
293
+ allToRemove.push(removeParam);
294
+ }
295
+ });
296
+ const { unitId, subUnitId } = (_allToRemove$ = allToRemove[0]) !== null && _allToRemove$ !== void 0 ? _allToRemove$ : removeParams[0];
297
+ const orderArr = this._getDrawingOrder(unitId, subUnitId);
298
+ const orderIndexMap = /* @__PURE__ */ new Map();
299
+ orderArr.forEach((id, idx) => orderIndexMap.set(id, idx));
300
+ allToRemove.sort((a, b) => {
301
+ var _orderIndexMap$get3, _orderIndexMap$get4;
302
+ return ((_orderIndexMap$get3 = orderIndexMap.get(a.drawingId)) !== null && _orderIndexMap$get3 !== void 0 ? _orderIndexMap$get3 : Number.NEGATIVE_INFINITY) - ((_orderIndexMap$get4 = orderIndexMap.get(b.drawingId)) !== null && _orderIndexMap$get4 !== void 0 ? _orderIndexMap$get4 : Number.NEGATIVE_INFINITY);
303
+ });
304
+ const ops = [];
305
+ const invertOps = [];
306
+ allToRemove.forEach((removeParam) => {
307
+ const { op, invertOp } = this._removeByParam(removeParam);
308
+ /**
309
+ * ot-json compose case
310
+ * two remove ops to does composition
311
+ * ops: [[unit, sheetUnit, order, 0, { r: true }], [unit, sheetUnit, order, 1, { r: true }]]
312
+ * We expected them to composed as [unit, sheetUnit, order, [0, { r: true }], [1, { r: true }]]
313
+ * But extremely confusing to get [unit, sheetUnit, order, 0, { r: true }, 2, { r: true }]
314
+ * And We apply this composed op to data, it's no item with index 2 can be removed.
315
+ * So use unshift api instead of push here.
316
+ */
317
+ ops.unshift(op);
318
+ invertOps.push(invertOp);
319
+ });
320
+ const op = ops.reduce(json1.type.compose, null);
321
+ return {
322
+ undo: invertOps.reduce(json1.type.compose, null),
323
+ redo: op,
324
+ unitId,
325
+ subUnitId,
326
+ objects: allToRemove
327
+ };
328
+ }
329
+ getBatchUpdateOp(updateParams) {
330
+ const objects = [];
331
+ const ops = [];
332
+ const invertOps = [];
333
+ updateParams.forEach((updateParam) => {
334
+ const { op, invertOp } = this._updateByParam(updateParam);
335
+ objects.push({
336
+ unitId: updateParam.unitId,
337
+ subUnitId: updateParam.subUnitId,
338
+ drawingId: updateParam.drawingId
339
+ });
340
+ ops.push(op);
341
+ invertOps.push(invertOp);
342
+ });
343
+ const op = ops.reduce(json1.type.compose, null);
344
+ const invertOp = invertOps.reduce(json1.type.compose, null);
345
+ const { unitId, subUnitId } = updateParams[0];
346
+ return {
347
+ undo: invertOp,
348
+ redo: op,
349
+ unitId,
350
+ subUnitId,
351
+ objects
352
+ };
353
+ }
354
+ removeNotification(removeParams) {
355
+ this._remove$.next(removeParams);
356
+ }
357
+ addNotification(insertParams) {
358
+ this._add$.next(insertParams);
359
+ }
360
+ updateNotification(updateParams) {
361
+ this._update$.next(updateParams);
362
+ }
363
+ orderNotification(orderParams) {
364
+ this._order$.next(orderParams);
365
+ }
366
+ groupUpdateNotification(groupParams) {
367
+ this._group$.next(groupParams);
368
+ }
369
+ ungroupUpdateNotification(groupParams) {
370
+ this._ungroup$.next(groupParams);
371
+ }
372
+ refreshTransformNotification(refreshParams) {
373
+ this._refreshTransform$.next(refreshParams);
374
+ }
375
+ getGroupDrawingOp(groupParams) {
376
+ const ops = [];
377
+ const { unitId, subUnitId } = groupParams[0].parent;
378
+ groupParams.forEach((groupParam) => {
379
+ ops.push(this._getGroupDrawingOp(groupParam));
380
+ });
381
+ const op = ops.reduce(json1.type.compose, null);
382
+ return {
383
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
384
+ redo: op,
385
+ unitId,
386
+ subUnitId,
387
+ objects: groupParams
388
+ };
389
+ }
390
+ getUngroupDrawingOp(groupParams) {
391
+ const ops = [];
392
+ const { unitId, subUnitId } = groupParams[0].parent;
393
+ groupParams.forEach((groupParam) => {
394
+ ops.push(this._getUngroupDrawingOp(groupParam));
395
+ });
396
+ const op = ops.reduce(json1.type.compose, null);
397
+ return {
398
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
399
+ redo: op,
400
+ unitId,
401
+ subUnitId,
402
+ objects: groupParams
403
+ };
404
+ }
405
+ getDrawingsByGroup(groupParam) {
406
+ const { unitId, subUnitId, drawingId } = groupParam;
407
+ if (this.getDrawingByParam({
408
+ unitId,
409
+ subUnitId,
410
+ drawingId
411
+ }) == null) return [];
412
+ const drawings = this._getDrawingData(unitId, subUnitId);
413
+ const children = [];
414
+ Object.keys(drawings).forEach((key) => {
415
+ const drawing = drawings[key];
416
+ if (drawing.groupId === drawingId) children.push(drawing);
417
+ });
418
+ return children;
419
+ }
420
+ getDrawingsByGroupNested(groupSearch) {
421
+ const { unitId, subUnitId } = groupSearch;
422
+ const rootParam = this.getDrawingByParam(groupSearch);
423
+ if (!rootParam) return null;
424
+ const allDrawings = this._getDrawingData(unitId, subUnitId);
425
+ const groupDerivedDrawingsIdMap = /* @__PURE__ */ new Map();
426
+ Object.values(allDrawings).forEach((drawing) => {
427
+ if (drawing.groupId != null) {
428
+ if (!groupDerivedDrawingsIdMap.has(drawing.groupId)) groupDerivedDrawingsIdMap.set(drawing.groupId, []);
429
+ groupDerivedDrawingsIdMap.get(drawing.groupId).push(drawing.drawingId);
430
+ }
431
+ });
432
+ const flatChildren = [];
433
+ const groups = [];
434
+ const nestedIdRecord = {};
435
+ const dfs = (param) => {
436
+ var _groupDerivedDrawings;
437
+ const { drawingId } = param;
438
+ const childrenIds = (_groupDerivedDrawings = groupDerivedDrawingsIdMap.get(drawingId)) !== null && _groupDerivedDrawings !== void 0 ? _groupDerivedDrawings : [];
439
+ nestedIdRecord[drawingId] = {
440
+ drawingId,
441
+ children: childrenIds
442
+ };
443
+ childrenIds.forEach((childId) => {
444
+ const childParam = allDrawings[childId];
445
+ if (!childParam) return;
446
+ if (childParam.drawingType === DrawingTypeEnum.DRAWING_GROUP) {
447
+ dfs(childParam);
448
+ groups.push(childParam);
449
+ } else flatChildren.push(childParam);
450
+ });
451
+ };
452
+ dfs(rootParam);
453
+ groups.push(rootParam);
454
+ return {
455
+ nestedIdRecord,
456
+ flatChildren,
457
+ groups
458
+ };
459
+ }
460
+ _getGroupDrawingOp(groupParam) {
461
+ const { parent, children } = groupParam;
462
+ const { unitId: groupUnitId, subUnitId: groupSubUnitId, drawingId: groupDrawingId } = parent;
463
+ const ops = [];
464
+ ops.push(json1.insertOp([
465
+ groupUnitId,
466
+ groupSubUnitId,
467
+ DrawingMapItemType.data,
468
+ groupDrawingId
469
+ ], parent));
470
+ let maxChildIndex = Number.NEGATIVE_INFINITY;
471
+ children.forEach((child) => {
472
+ const { unitId, subUnitId, drawingId } = child;
473
+ const index = this._hasDrawingOrder({
474
+ unitId,
475
+ subUnitId,
476
+ drawingId
477
+ });
478
+ maxChildIndex = Math.max(maxChildIndex, index);
479
+ ops.push(...this._getUpdateParamCompareOp(child, this.getDrawingByParam({
480
+ unitId,
481
+ subUnitId,
482
+ drawingId
483
+ })));
484
+ });
485
+ if (maxChildIndex === Number.NEGATIVE_INFINITY) maxChildIndex = this._getDrawingOrder(groupUnitId, groupSubUnitId).length;
486
+ ops.push(json1.insertOp([
487
+ groupUnitId,
488
+ groupSubUnitId,
489
+ DrawingMapItemType.order,
490
+ maxChildIndex
491
+ ], groupDrawingId));
492
+ return ops.reduce(json1.type.compose, null);
493
+ }
494
+ _getUngroupDrawingOp(groupParam) {
495
+ const { parent, children } = groupParam;
496
+ const { unitId: groupUnitId, subUnitId: groupSubUnitId, drawingId: groupDrawingId } = parent;
497
+ const ops = [];
498
+ children.forEach((child) => {
499
+ const { unitId, subUnitId, drawingId } = child;
500
+ ops.push(...this._getUpdateParamCompareOp(child, this.getDrawingByParam({
501
+ unitId,
502
+ subUnitId,
503
+ drawingId
504
+ })));
505
+ });
506
+ ops.push(json1.removeOp([
507
+ groupUnitId,
508
+ groupSubUnitId,
509
+ DrawingMapItemType.data,
510
+ groupDrawingId
511
+ ], true));
512
+ ops.push(json1.removeOp([
513
+ groupUnitId,
514
+ groupSubUnitId,
515
+ DrawingMapItemType.order,
516
+ this._getDrawingOrder(groupUnitId, groupSubUnitId).indexOf(groupDrawingId)
517
+ ], true));
518
+ return ops.reduce(json1.type.compose, null);
519
+ }
520
+ applyJson1(unitId, subUnitId, jsonOp) {
521
+ this._establishDrawingMap(unitId, subUnitId);
522
+ this._oldDrawingManagerData = { ...this.drawingManagerData };
523
+ this.drawingManagerData = json1.type.apply(this.drawingManagerData, jsonOp);
524
+ }
525
+ featurePluginUpdateNotification(updateParams) {
526
+ this._featurePluginUpdate$.next(updateParams);
527
+ }
528
+ featurePluginOrderUpdateNotification(drawingOrderUpdateParam) {
529
+ this._featurePluginOrderUpdate$.next(drawingOrderUpdateParam);
530
+ }
531
+ featurePluginAddNotification(insertParams) {
532
+ this._featurePluginAdd$.next(insertParams);
533
+ }
534
+ featurePluginRemoveNotification(removeParams) {
535
+ this._featurePluginRemove$.next(removeParams);
536
+ }
537
+ featurePluginGroupUpdateNotification(groupParams) {
538
+ this._featurePluginGroupUpdate$.next(groupParams);
539
+ }
540
+ featurePluginUngroupUpdateNotification(groupParams) {
541
+ this._featurePluginUngroupUpdate$.next(groupParams);
542
+ }
543
+ getDrawingByParam(param) {
544
+ return this._getCurrentBySearch(param);
545
+ }
546
+ getOldDrawingByParam(param) {
547
+ return this._getOldBySearch(param);
548
+ }
549
+ getDrawingOKey(oKey) {
550
+ const [unitId, subUnitId, drawingId] = oKey.split("#-#");
551
+ return this._getCurrentBySearch({
552
+ unitId,
553
+ subUnitId,
554
+ drawingId
555
+ });
556
+ }
557
+ focusDrawing(params) {
558
+ if (params == null || params.length === 0) {
559
+ this._focusDrawings = [];
560
+ this._focus$.next([]);
561
+ return;
562
+ }
563
+ const drawingParams = [];
564
+ params.forEach((param) => {
565
+ var _this$_getDrawingData;
566
+ const { unitId, subUnitId, drawingId } = param;
567
+ const item = (_this$_getDrawingData = this._getDrawingData(unitId, subUnitId)) === null || _this$_getDrawingData === void 0 ? void 0 : _this$_getDrawingData[drawingId];
568
+ if (item != null) drawingParams.push(item);
569
+ });
570
+ if (drawingParams.length > 0) {
571
+ this._focusDrawings = drawingParams;
572
+ this._focus$.next(drawingParams);
573
+ }
574
+ }
575
+ getFocusDrawings() {
576
+ const drawingParams = [];
577
+ this._focusDrawings.forEach((param) => {
578
+ var _this$_getDrawingData2;
579
+ const { unitId, subUnitId, drawingId } = param;
580
+ const item = (_this$_getDrawingData2 = this._getDrawingData(unitId, subUnitId)) === null || _this$_getDrawingData2 === void 0 ? void 0 : _this$_getDrawingData2[drawingId];
581
+ if (item != null) drawingParams.push(item);
582
+ });
583
+ return drawingParams;
584
+ }
585
+ getDrawingOrder(unitId, subUnitId) {
586
+ return this._getDrawingOrder(unitId, subUnitId);
587
+ }
588
+ setDrawingOrder(unitId, subUnitId, order) {
589
+ this.drawingManagerData[unitId][subUnitId].order = order;
590
+ }
591
+ orderUpdateNotification(orderParams) {
592
+ this._order$.next(orderParams);
593
+ }
594
+ getForwardDrawingsOp(orderParams) {
595
+ const { unitId, subUnitId, drawingIds } = orderParams;
596
+ const ops = [];
597
+ const orders = this.getDrawingOrder(unitId, subUnitId);
598
+ const newIds = [...drawingIds];
599
+ drawingIds.forEach((drawingId) => {
600
+ const index = this._hasDrawingOrder({
601
+ unitId,
602
+ subUnitId,
603
+ drawingId
604
+ });
605
+ if (index === -1 || index === orders.length - 1) return;
606
+ const op = json1.moveOp([
607
+ unitId,
608
+ subUnitId,
609
+ DrawingMapItemType.order,
610
+ index
611
+ ], [
612
+ unitId,
613
+ subUnitId,
614
+ DrawingMapItemType.order,
615
+ index + 1
616
+ ]);
617
+ ops.push(op);
618
+ if (!newIds.includes(orders[index + 1])) newIds.push(orders[index + 1]);
619
+ });
620
+ const op = ops.reduce(json1.type.compose, null);
621
+ return {
622
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
623
+ redo: op,
624
+ unitId,
625
+ subUnitId,
626
+ objects: {
627
+ ...orderParams,
628
+ drawingIds: newIds
629
+ }
630
+ };
631
+ }
632
+ getBackwardDrawingOp(orderParams) {
633
+ const { unitId, subUnitId, drawingIds } = orderParams;
634
+ const ops = [];
635
+ const orders = this.getDrawingOrder(unitId, subUnitId);
636
+ const newIds = [...drawingIds];
637
+ drawingIds.forEach((drawingId) => {
638
+ const index = this._hasDrawingOrder({
639
+ unitId,
640
+ subUnitId,
641
+ drawingId
642
+ });
643
+ if (index === -1 || index === 0) return;
644
+ const op = json1.moveOp([
645
+ unitId,
646
+ subUnitId,
647
+ DrawingMapItemType.order,
648
+ index
649
+ ], [
650
+ unitId,
651
+ subUnitId,
652
+ DrawingMapItemType.order,
653
+ index - 1
654
+ ]);
655
+ ops.push(op);
656
+ if (!newIds.includes(orders[index - 1])) newIds.push(orders[index - 1]);
657
+ });
658
+ const op = ops.reduce(json1.type.compose, null);
659
+ return {
660
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
661
+ redo: op,
662
+ unitId,
663
+ subUnitId,
664
+ objects: {
665
+ ...orderParams,
666
+ drawingIds: newIds
667
+ }
668
+ };
669
+ }
670
+ getFrontDrawingsOp(orderParams) {
671
+ const { unitId, subUnitId, drawingIds } = orderParams;
672
+ const orderDrawingIds = this._getOrderFromSearchParams(unitId, subUnitId, drawingIds);
673
+ const newIds = [...drawingIds];
674
+ const orders = this.getDrawingOrder(unitId, subUnitId);
675
+ const ops = [];
676
+ orderDrawingIds.forEach((orderDrawingId) => {
677
+ const { drawingId } = orderDrawingId;
678
+ const index = this._getDrawingCount(unitId, subUnitId) - 1;
679
+ const op = json1.moveOp([
680
+ unitId,
681
+ subUnitId,
682
+ DrawingMapItemType.order,
683
+ this._getDrawingOrder(unitId, subUnitId).indexOf(drawingId)
684
+ ], [
685
+ unitId,
686
+ subUnitId,
687
+ DrawingMapItemType.order,
688
+ index
689
+ ]);
690
+ ops.push(op);
691
+ if (!newIds.includes(orders[index])) newIds.push(orders[index]);
692
+ });
693
+ const op = ops.reduce(json1.type.compose, null);
694
+ return {
695
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
696
+ redo: op,
697
+ unitId,
698
+ subUnitId,
699
+ objects: {
700
+ ...orderParams,
701
+ drawingIds: newIds
702
+ }
703
+ };
704
+ }
705
+ getBackDrawingsOp(orderParams) {
706
+ const { unitId, subUnitId, drawingIds } = orderParams;
707
+ const orderSearchParams = this._getOrderFromSearchParams(unitId, subUnitId, drawingIds, true);
708
+ const newIds = [...drawingIds];
709
+ const orders = this.getDrawingOrder(unitId, subUnitId);
710
+ const ops = [];
711
+ orderSearchParams.forEach((orderSearchParam) => {
712
+ const { drawingId } = orderSearchParam;
713
+ const op = json1.moveOp([
714
+ unitId,
715
+ subUnitId,
716
+ DrawingMapItemType.order,
717
+ this._getDrawingOrder(unitId, subUnitId).indexOf(drawingId)
718
+ ], [
719
+ unitId,
720
+ subUnitId,
721
+ DrawingMapItemType.order,
722
+ 0
723
+ ]);
724
+ ops.push(op);
725
+ if (!newIds.includes(orders[0])) newIds.push(orders[0]);
726
+ });
727
+ const op = ops.reduce(json1.type.compose, null);
728
+ return {
729
+ undo: json1.type.invertWithDoc(op, this.drawingManagerData),
730
+ redo: op,
731
+ unitId,
732
+ subUnitId,
733
+ objects: {
734
+ ...orderParams,
735
+ drawingIds: newIds
736
+ }
737
+ };
738
+ }
739
+ _getDrawingCount(unitId, subUnitId) {
740
+ return this.getDrawingOrder(unitId, subUnitId).length || 0;
741
+ }
742
+ _getOrderFromSearchParams(unitId, subUnitId, drawingIds, isDesc = false) {
743
+ return drawingIds.map((drawingId) => {
744
+ return {
745
+ drawingId,
746
+ zIndex: this._hasDrawingOrder({
747
+ unitId,
748
+ subUnitId,
749
+ drawingId
750
+ })
751
+ };
752
+ }).sort(isDesc === false ? sortRules : sortRulesByDesc);
753
+ }
754
+ _hasDrawingOrder(searchParam) {
755
+ if (searchParam == null) return -1;
756
+ const { unitId, subUnitId, drawingId } = searchParam;
757
+ this._establishDrawingMap(unitId, subUnitId);
758
+ return this._getDrawingOrder(unitId, subUnitId).indexOf(drawingId);
759
+ }
760
+ _getCurrentBySearch(searchParam) {
761
+ var _this$drawingManagerD;
762
+ if (searchParam == null) return;
763
+ const { unitId, subUnitId, drawingId } = searchParam;
764
+ return (_this$drawingManagerD = this.drawingManagerData[unitId]) === null || _this$drawingManagerD === void 0 || (_this$drawingManagerD = _this$drawingManagerD[subUnitId]) === null || _this$drawingManagerD === void 0 || (_this$drawingManagerD = _this$drawingManagerD.data) === null || _this$drawingManagerD === void 0 ? void 0 : _this$drawingManagerD[drawingId];
765
+ }
766
+ _getOldBySearch(searchParam) {
767
+ var _this$_oldDrawingMana;
768
+ if (searchParam == null) return;
769
+ const { unitId, subUnitId, drawingId } = searchParam;
770
+ return (_this$_oldDrawingMana = this._oldDrawingManagerData[unitId]) === null || _this$_oldDrawingMana === void 0 || (_this$_oldDrawingMana = _this$_oldDrawingMana[subUnitId]) === null || _this$_oldDrawingMana === void 0 || (_this$_oldDrawingMana = _this$_oldDrawingMana.data) === null || _this$_oldDrawingMana === void 0 ? void 0 : _this$_oldDrawingMana[drawingId];
771
+ }
772
+ _establishDrawingMap(unitId, subUnitId, drawingId) {
773
+ var _this$drawingManagerD2;
774
+ if (!this.drawingManagerData[unitId]) this.drawingManagerData[unitId] = {};
775
+ if (!this.drawingManagerData[unitId][subUnitId]) this.drawingManagerData[unitId][subUnitId] = {
776
+ data: {},
777
+ order: []
778
+ };
779
+ if (drawingId == null) return null;
780
+ return (_this$drawingManagerD2 = this.drawingManagerData[unitId][subUnitId].data) === null || _this$drawingManagerD2 === void 0 ? void 0 : _this$drawingManagerD2[drawingId];
781
+ }
782
+ _addByParam(insertParam) {
783
+ const { unitId, subUnitId, drawingId } = insertParam;
784
+ this._establishDrawingMap(unitId, subUnitId, drawingId);
785
+ const op = [json1.insertOp([
786
+ unitId,
787
+ subUnitId,
788
+ DrawingMapItemType.data,
789
+ drawingId
790
+ ], insertParam), json1.insertOp([
791
+ unitId,
792
+ subUnitId,
793
+ DrawingMapItemType.order,
794
+ this._getDrawingOrder(unitId, subUnitId).length
795
+ ], drawingId)].reduce(json1.type.compose, null);
796
+ return {
797
+ op,
798
+ invertOp: json1.type.invertWithDoc(op, this.drawingManagerData)
799
+ };
800
+ }
801
+ _removeByParam(searchParam) {
802
+ if (searchParam == null) return {
803
+ op: [],
804
+ invertOp: []
805
+ };
806
+ const { unitId, subUnitId, drawingId } = searchParam;
807
+ if (this._establishDrawingMap(unitId, subUnitId, drawingId) == null) return {
808
+ op: [],
809
+ invertOp: []
810
+ };
811
+ const op = [json1.removeOp([
812
+ unitId,
813
+ subUnitId,
814
+ DrawingMapItemType.data,
815
+ drawingId
816
+ ], true), json1.removeOp([
817
+ unitId,
818
+ subUnitId,
819
+ DrawingMapItemType.order,
820
+ this._getDrawingOrder(unitId, subUnitId).indexOf(drawingId)
821
+ ], true)].reduce(json1.type.compose, null);
822
+ return {
823
+ op,
824
+ invertOp: json1.type.invertWithDoc(op, this.drawingManagerData)
825
+ };
826
+ }
827
+ _updateByParam(updateParam) {
828
+ const { unitId, subUnitId, drawingId } = updateParam;
829
+ const object = this._establishDrawingMap(unitId, subUnitId, drawingId);
830
+ if (object == null) return {
831
+ op: [],
832
+ invertOp: []
833
+ };
834
+ const op = this._getUpdateParamCompareOp(updateParam, object).reduce(json1.type.compose, null);
835
+ return {
836
+ op,
837
+ invertOp: json1.type.invertWithDoc(op, this.drawingManagerData)
838
+ };
839
+ }
840
+ _getUpdateParamCompareOp(newParam, oldParam) {
841
+ const { unitId, subUnitId, drawingId } = newParam;
842
+ const ops = [];
843
+ Object.keys(newParam).forEach((key) => {
844
+ const newVal = newParam[key];
845
+ const oldVal = oldParam[key];
846
+ if (oldVal === newVal) return;
847
+ ops.push(json1.replaceOp([
848
+ unitId,
849
+ subUnitId,
850
+ DrawingMapItemType.data,
851
+ drawingId,
852
+ key
853
+ ], oldVal, newVal));
854
+ });
855
+ return ops;
856
+ }
857
+ _getDrawingData(unitId, subUnitId) {
858
+ var _this$drawingManagerD3;
859
+ return ((_this$drawingManagerD3 = this.drawingManagerData[unitId]) === null || _this$drawingManagerD3 === void 0 || (_this$drawingManagerD3 = _this$drawingManagerD3[subUnitId]) === null || _this$drawingManagerD3 === void 0 ? void 0 : _this$drawingManagerD3.data) || {};
860
+ }
861
+ _getDrawingOrder(unitId, subUnitId) {
862
+ var _this$drawingManagerD4;
863
+ return ((_this$drawingManagerD4 = this.drawingManagerData[unitId]) === null || _this$drawingManagerD4 === void 0 || (_this$drawingManagerD4 = _this$drawingManagerD4[subUnitId]) === null || _this$drawingManagerD4 === void 0 ? void 0 : _this$drawingManagerD4.order) || [];
864
+ }
865
+ getDrawingVisible() {
866
+ return this._visible;
867
+ }
868
+ getDrawingEditable() {
869
+ return this._editable;
870
+ }
871
+ setDrawingVisible(visible) {
872
+ this._visible = visible;
873
+ }
874
+ setDrawingEditable(editable) {
875
+ this._editable = editable;
876
+ }
877
+ };
878
+ var DrawingManagerService = class extends UnitDrawingService {};
879
+
880
+ //#endregion
881
+ //#region src/services/image-io-impl.service.ts
882
+ var ImageIoService = class {
883
+ constructor() {
884
+ _defineProperty(this, "_waitCount", 0);
885
+ _defineProperty(this, "_change$", new Subject());
886
+ _defineProperty(this, "change$", this._change$);
887
+ _defineProperty(this, "_imageSourceCache", /* @__PURE__ */ new Map());
888
+ }
889
+ setWaitCount(count) {
890
+ this._waitCount = count;
891
+ this._change$.next(count);
892
+ }
893
+ getImageSourceCache(source, imageSourceType) {
894
+ if (imageSourceType === ImageSourceType.BASE64) {
895
+ const image = new Image();
896
+ image.src = source;
897
+ return image;
898
+ }
899
+ return this._imageSourceCache.get(source);
900
+ }
901
+ addImageSourceCache(source, imageSourceType, imageSource) {
902
+ if (imageSourceType === ImageSourceType.BASE64 || imageSource == null) return;
903
+ this._imageSourceCache.set(source, imageSource);
904
+ }
905
+ async getImage(imageId) {
906
+ return Promise.resolve(imageId);
907
+ }
908
+ async saveImage(imageFile) {
909
+ return new Promise((resolve, reject) => {
910
+ if (!DRAWING_IMAGE_ALLOW_IMAGE_LIST.includes(imageFile.type)) {
911
+ reject(new Error(ImageUploadStatusType.ERROR_IMAGE_TYPE));
912
+ this._decreaseWaiting();
913
+ return;
914
+ }
915
+ if (imageFile.size > 5242880) {
916
+ reject(new Error(ImageUploadStatusType.ERROR_EXCEED_SIZE));
917
+ this._decreaseWaiting();
918
+ return;
919
+ }
920
+ const reader = new FileReader();
921
+ reader.readAsDataURL(imageFile);
922
+ reader.onload = (evt) => {
923
+ var _evt$target;
924
+ const replaceSrc = (_evt$target = evt.target) === null || _evt$target === void 0 ? void 0 : _evt$target.result;
925
+ if (replaceSrc == null) {
926
+ reject(new Error(ImageUploadStatusType.ERROR_IMAGE));
927
+ this._decreaseWaiting();
928
+ return;
929
+ }
930
+ resolve({
931
+ imageId: generateRandomId(6),
932
+ imageSourceType: ImageSourceType.BASE64,
933
+ source: replaceSrc,
934
+ base64Cache: replaceSrc,
935
+ status: ImageUploadStatusType.SUCCUSS
936
+ });
937
+ this._decreaseWaiting();
938
+ };
939
+ });
940
+ }
941
+ _decreaseWaiting() {
942
+ this._waitCount -= 1;
943
+ this._change$.next(this._waitCount);
944
+ }
945
+ };
946
+
947
+ //#endregion
948
+ //#region src/services/url-image.service.ts
949
+ var URLImageService = class extends Disposable {
950
+ constructor(..._args) {
951
+ super(..._args);
952
+ _defineProperty(this, "_urlImageDownloader", null);
953
+ }
954
+ registerURLImageDownloader(downloader) {
955
+ this._urlImageDownloader = downloader;
956
+ return toDisposable(() => {
957
+ this._urlImageDownloader = null;
958
+ });
959
+ }
960
+ async getImage(url) {
961
+ if (this._urlImageDownloader) try {
962
+ return await this._urlImageDownloader(url);
963
+ } catch (error) {
964
+ console.error(`Custom downloader failed for ${url}, falling back to default behavior:`, error);
965
+ }
966
+ return url;
967
+ }
968
+ async downloadImage(url) {
969
+ if (this._urlImageDownloader) try {
970
+ const base64 = await this._urlImageDownloader(url);
971
+ return await (await fetch(base64)).blob();
972
+ } catch (error) {
973
+ console.error(`Custom downloader failed for ${url}, falling back to default fetch:`, error);
974
+ }
975
+ return await (await fetch(url)).blob();
976
+ }
977
+ };
978
+
979
+ //#endregion
980
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorateParam.js
981
+ function __decorateParam(paramIndex, decorator) {
982
+ return function(target, key) {
983
+ decorator(target, key, paramIndex);
984
+ };
985
+ }
986
+
987
+ //#endregion
988
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
989
+ function __decorate(decorators, target, key, desc) {
990
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
991
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
992
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
993
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
994
+ }
995
+
996
+ //#endregion
997
+ //#region src/plugin.ts
998
+ let UniverDrawingPlugin = class UniverDrawingPlugin extends Plugin {
999
+ constructor(_config = defaultPluginConfig, _injector, _configService, _commandService) {
1000
+ super();
1001
+ this._config = _config;
1002
+ this._injector = _injector;
1003
+ this._configService = _configService;
1004
+ this._commandService = _commandService;
1005
+ const { ...rest } = merge({}, defaultPluginConfig, this._config);
1006
+ this._configService.setConfig(DRAWING_PLUGIN_CONFIG_KEY, rest);
1007
+ }
1008
+ onStarting() {
1009
+ this._initCommands();
1010
+ this._initDependencies();
1011
+ }
1012
+ _initDependencies() {
1013
+ var _this$_config;
1014
+ mergeOverrideWithDependencies([
1015
+ [IImageIoService, { useClass: ImageIoService }],
1016
+ [IURLImageService, { useClass: URLImageService }],
1017
+ [IDrawingManagerService, { useClass: DrawingManagerService }]
1018
+ ], (_this$_config = this._config) === null || _this$_config === void 0 ? void 0 : _this$_config.override).forEach((d) => this._injector.add(d));
1019
+ }
1020
+ _initCommands() {
1021
+ [SetDrawingSelectedOperation].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
1022
+ }
1023
+ };
1024
+ _defineProperty(UniverDrawingPlugin, "pluginName", "UNIVER_DRAWING_PLUGIN");
1025
+ _defineProperty(UniverDrawingPlugin, "packageName", name);
1026
+ _defineProperty(UniverDrawingPlugin, "version", version);
1027
+ UniverDrawingPlugin = __decorate([
1028
+ __decorateParam(1, Inject(Injector)),
1029
+ __decorateParam(2, IConfigService),
1030
+ __decorateParam(3, ICommandService)
1031
+ ], UniverDrawingPlugin);
1032
+
1033
+ //#endregion
1034
+ //#region src/utils/get-image-shape-key.ts
1035
+ function getDrawingShapeKeyByDrawingSearch({ unitId, subUnitId, drawingId }, index) {
1036
+ return typeof index === "number" ? `${unitId}#-#${subUnitId}#-#${drawingId}#-#${index}` : `${unitId}#-#${subUnitId}#-#${drawingId}`;
1037
+ }
1038
+
1039
+ //#endregion
1040
+ //#region src/utils/get-image-size.ts
1041
+ const getImageSize = async (src) => {
1042
+ return new Promise((resolve, reject) => {
1043
+ const image = new Image();
1044
+ image.src = src;
1045
+ image.onload = () => {
1046
+ resolve({
1047
+ width: image.width,
1048
+ height: image.height,
1049
+ image
1050
+ });
1051
+ };
1052
+ image.onerror = (error) => {
1053
+ reject(error);
1054
+ };
1055
+ });
1056
+ };
1057
+
1058
+ //#endregion
1059
+ export { DRAWING_IMAGE_ALLOW_IMAGE_LIST, DRAWING_IMAGE_ALLOW_SIZE, DRAWING_IMAGE_COUNT_LIMIT, DRAWING_IMAGE_HEIGHT_LIMIT, DRAWING_IMAGE_WIDTH_LIMIT, DrawingManagerService, IDrawingManagerService, IImageIoService, ImageIoService, ImageSourceType, ImageUploadStatusType, SetDrawingSelectedOperation, URLImageService, UnitDrawingService, UniverDrawingPlugin, getDrawingShapeKeyByDrawingSearch, getImageSize };