@univerjs/sheets-ui
Version:
UniverSheet normal ui-plugin-sheets
1 lines • 885 kB
JavaScript
(function(T,q){typeof exports=="object"&&typeof module<"u"?q(exports,require("@univerjs/docs-ui"),require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-render"),require("rxjs"),require("@univerjs/ui"),require("@univerjs/docs"),require("react"),require("@univerjs/engine-formula"),require("react/jsx-runtime"),require("@univerjs/design"),require("rxjs/operators"),require("@univerjs/telemetry")):typeof define=="function"&&define.amd?define(["exports","@univerjs/docs-ui","@univerjs/core","@univerjs/sheets","@univerjs/engine-render","rxjs","@univerjs/ui","@univerjs/docs","react","@univerjs/engine-formula","react/jsx-runtime","@univerjs/design","rxjs/operators","@univerjs/telemetry"],q):(T=typeof globalThis<"u"?globalThis:T||self,q(T.UniverSheetsUi={},T.UniverDocsUi,T.UniverCore,T.UniverSheets,T.UniverEngineRender,T.rxjs,T.UniverUi,T.UniverDocs,T.React,T.UniverEngineFormula,T.React,T.UniverDesign,T.rxjs.operators,T.UniverTelemetry))})(this,(function(T,q,a,m,M,N,I,Ce,x,K,b,Y,ce,ud){"use strict";var wp=Object.defineProperty;var Ep=(T,q,a)=>q in T?wp(T,q,{enumerable:!0,configurable:!0,writable:!0,value:a}):T[q]=a;var y=(T,q,a)=>Ep(T,typeof q!="symbol"?q+"":q,a);function Ui(n,o){if(n==null)return;const e=o.getRenderById(n);if(e==null)return;const{mainComponent:t,scene:i,engine:r,components:s}=e,l=t,c=s.get(q.DOCS_VIEW_KEY.BACKGROUND);return{document:l,docBackground:c,scene:i,engine:r}}const Yo=[10,400],jo="RANGE_SELECTOR_COMPONENT_KEY",Bi="EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY";var ue=(n=>(n.MAIN="__SpreadsheetRender__",n.ROW="__SpreadsheetRowHeader__",n.COLUMN="__SpreadsheetColumnHeader__",n.LEFT_TOP="__SpreadsheetLeftTopPlaceholder__",n))(ue||{});const Ds=0,Fi=1,st=10,hd=11,Ns=12,Gn={id:"sheet.command.set-row-header-width",type:a.CommandType.COMMAND,handler:async(n,o)=>{if(!o)return!1;const e=n.get(M.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:i,size:r}=o,s=e.getRenderById(t);if(s){e.getRenderById(t).with(T.SheetSkeletonManagerService).setRowHeaderSize(s,i,r);const{components:c}=s,d=c.get(ue.ROW);d&&d.setCustomHeader({headerStyle:{size:r}},i)}return!0}},Kn={id:"sheet.command.set-col-header-height",type:a.CommandType.COMMAND,handler:async(n,o)=>{if(!o)return!1;const e=n.get(M.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:i,size:r}=o,s=e.getRenderById(t);if(s){e.getRenderById(t).with(T.SheetSkeletonManagerService).setColumnHeaderSize(s,i,r);const{components:c}=s,d=c.get(ue.COLUMN);d&&d.setCustomHeader({headerStyle:{size:r}},i)}return!0}},Zo=a.createInterceptorKey("rangeMovePermissionCheck"),Go=a.createInterceptorKey("rangeFillPermissionCheck");var Ko=(n=>(n[n.FORMULA_EDITOR_SHOW=100]="FORMULA_EDITOR_SHOW",n[n.MARK_SELECTION=1e4]="MARK_SELECTION",n))(Ko||{});function qe(n){const o=n.getColorFromTheme("primary.600"),e=n.getColorFromTheme("white"),t=new a.ColorKit(o).setAlpha(.07).toRgbString();return{strokeWidth:1,stroke:o,fill:t,widgets:{},widgetSize:6,widgetStrokeWidth:1,widgetStroke:e,autofillSize:6,autofillStrokeWidth:1,autofillStroke:e,rowHeaderFill:t,rowHeaderStroke:o,rowHeaderStrokeWidth:1,columnHeaderFill:t,columnHeaderStroke:o,columnHeaderStrokeWidth:1,expandCornerSize:40}}class md{constructor(){y(this,"_startColumn",-1);y(this,"_startRow",-1);y(this,"_endColumn",-1);y(this,"_endRow",-1);y(this,"_startX",0);y(this,"_startY",0);y(this,"_endX",0);y(this,"_endY",0);y(this,"_primary");y(this,"_rangeType",a.RANGE_TYPE.NORMAL)}get startColumn(){return this._startColumn}get startRow(){return this._startRow}get endColumn(){return this._endColumn}get endRow(){return this._endRow}get startX(){return this._startX}get startY(){return this._startY}get endX(){return this._endX}get endY(){return this._endY}get currentCell(){return this._primary}get rangeType(){return this._rangeType}isEqual(o){const{startColumn:e,startRow:t,endColumn:i,endRow:r}=this,{startColumn:s,startRow:l,endColumn:c,endRow:d}=o;return e===s&&t===l&&i===c&&r===d}highlightToSelection(){if(this._primary)return a.convertCellToRange(this._primary)}getRange(){return{startColumn:this._startColumn,startRow:this._startRow,endColumn:this._endColumn,endRow:this._endRow,startX:this._startX,startY:this._startY,endX:this._endX,endY:this._endY,rangeType:this.rangeType}}getCell(){return this._primary}getRangeType(){return this._rangeType}setRangeType(o){this._rangeType=o}getValue(){return{rangeWithCoord:this.getRange(),primaryWithCoord:this._primary}}setValue(o,e){const{startColumn:t,startRow:i,endColumn:r,endRow:s,startX:l,startY:c,endX:d,endY:u,rangeType:h}=o;this._startColumn=t,this._startRow=i,this._endColumn=r,this._endRow=s,this._startX=l,this._startY=c,this._endX=d,this._endY=u,h!==void 0&&(this._rangeType=h),e!==void 0&&this.setCurrentCell(e)}setCurrentCell(o){this._primary=o}clearCurrentCell(){this._primary=null}}function Jt(n,o){const{startRow:e,startColumn:t,endRow:i,endColumn:r,rangeType:s}=o,l=i<e?i:e,c=i<e?e:i,d=r<t?r:t,u=r<t?t:r,h=n.getNoMergeCellWithCoordByIndex(l,d),g=n.getNoMergeCellWithCoordByIndex(c,u);return{startRow:e,startColumn:t,endRow:i,endColumn:r,rangeType:s,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}function Re(n,o){const{range:e,primary:t,style:i}=n,r=Jt(o,e),s=t&&qn(o,t);return{rangeWithCoord:r,primaryWithCoord:s,style:i}}function qn(n,o){const{actualRow:e,actualColumn:t,isMerged:i,isMergedMainCell:r,startRow:s,startColumn:l,endRow:c,endColumn:d}=o,u=n.getNoMergeCellWithCoordByIndex(e,t),h=n.getNoMergeCellWithCoordByIndex(s,l),g=n.getNoMergeCellWithCoordByIndex(c,d);return{actualRow:e,actualColumn:t,isMerged:i,isMergedMainCell:r,startX:u.startX,startY:u.startY,endX:u.endX,endY:u.endY,mergeInfo:{startRow:s,startColumn:l,endRow:c,endColumn:d,startY:(h==null?void 0:h.startY)||0,endY:(g==null?void 0:g.endY)||0,startX:(h==null?void 0:h.startX)||0,endX:(g==null?void 0:g.endX)||0}}}const ks="__SpreadsheetHelperSelectionTempRect",gd=35;class Hs{constructor(o,e){y(this,"_startOffsetX",0);y(this,"_startOffsetY",0);y(this,"_relativeSelectionPositionRow",0);y(this,"_relativeSelectionPositionColumn",0);y(this,"_relativeSelectionRowLength",0);y(this,"_relativeSelectionColumnLength",0);y(this,"_scenePointerMoveSub");y(this,"_scenePointerUpSub");y(this,"_disabled",!1);y(this,"_helperSelection");y(this,"_scrollTimer");y(this,"_activeViewport");y(this,"_targetSelection",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});y(this,"_isInMergeState",!1);y(this,"_fillControlColors",[]);y(this,"_skeleton");y(this,"_scene");y(this,"_themeService");y(this,"_injector");y(this,"_selectionHooks");this._control=o,this._skeleton=e.skeleton,this._scene=e.scene,this._themeService=e.themeService,this._injector=e.injector,this._selectionHooks=e.selectionHooks,this._initialControl(),this._initialWidget(),this._initialAutoFill(),this._control.dispose$.subscribe(()=>{this.dispose()})}get isHelperSelection(){return this._control.isHelperSelection}dispose(){var o,e;(o=this._scrollTimer)==null||o.dispose(),this._fillControlColors=[],this._clearObserverEvent(),(e=this._helperSelection)==null||e.dispose()}setDisabled(o){this._disabled=o}_getFreeze(){var e,t;return(t=(e=M.withCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET,T.SheetSkeletonManagerService,this._injector.get(a.IUniverInstanceService),this._injector.get(M.IRenderManagerService)))==null?void 0:e.getCurrentParam())==null?void 0:t.skeleton.getWorksheetConfig().freeze}_isSelectionInViewport(o,e){const t=this._getFreeze()||{startRow:-1,startColumn:-1};switch(e.viewportKey){case M.SHEET_VIEWPORT_KEY.VIEW_MAIN:return o.endRow>=t.startRow&&o.endColumn>=t.startColumn;case M.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP:case M.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT:return o.endColumn>=t.startColumn&&o.startRow<t.startRow;case M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT:case M.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM:return o.endRow>=t.startRow&&o.startColumn<t.startColumn;case M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP:case M.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT:case M.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP:case M.SHEET_VIEWPORT_KEY.VIEW_LEFT_TOP:return o.startRow<t.startRow&&o.startColumn<t.startColumn}}_clearObserverEvent(){var o,e;(o=this._scenePointerMoveSub)==null||o.unsubscribe(),(e=this._scenePointerUpSub)==null||e.unsubscribe(),this._scenePointerMoveSub=null,this._scenePointerUpSub=null}_initialControl(){const{leftControl:o,rightControl:e,topControl:t,bottomControl:i}=this._control;[o,e,t,i].forEach(r=>{r.onPointerEnter$.subscribeEvent(()=>{var l;((l=this._injector.get(Ie,a.Quantity.OPTIONAL))==null?void 0:l.interceptor.fetchThroughInterceptors(Zo)(!1,null))!==!1&&r.setCursor(M.CURSOR_TYPE.MOVE)}),r.onPointerLeave$.subscribeEvent(()=>{r.resetCursor()}),r.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}_controlMoving(o,e){var D;const t=this._scene,i=t.getScrollXYInfoByViewport(M.Vector2.FromArray([o,e])),{scaleX:r,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(o,e,r,s,i),{row:c,column:d}=l,u=this._skeleton.getRowCount()-1,h=this._skeleton.getColumnCount()-1;let g=Math.max(0,c+this._relativeSelectionPositionRow),S=g+this._relativeSelectionRowLength;S>u&&(S=u,S-g<this._relativeSelectionRowLength&&(g=S-this._relativeSelectionRowLength));let f=Math.max(0,d+this._relativeSelectionPositionColumn),v=f+this._relativeSelectionColumnLength;v>h&&(v=h,v-f<this._relativeSelectionColumnLength&&(f=v-this._relativeSelectionColumnLength));const p=this._skeleton.worksheet.getCellInfoInMergeData(g,f),_=Re({range:{startRow:g,endRow:S,startColumn:f,endColumn:v},primary:p,style:null},this._skeleton),w=this._skeleton.getNoMergeCellWithCoordByIndex(g,f),E=this._skeleton.getNoMergeCellWithCoordByIndex(S,v),O=(w==null?void 0:w.startY)||0,R=(E==null?void 0:E.endY)||0,P=(w==null?void 0:w.startX)||0,A=(E==null?void 0:E.endX)||0;(D=this._helperSelection)==null||D.transformByState({left:P,top:O,width:A-P,height:R-O}),this._targetSelection={..._.rangeWithCoord},this._control.selectionMoving$.next(_.rangeWithCoord)}_controlPointerDownHandler(o){const{offsetX:e,offsetY:t}=o,i=this._scene,r=i.getCoordRelativeToViewport(M.Vector2.FromArray([e,t])),{x:s,y:l}=r,c=i.getScrollXYInfoByViewport(r),{scaleX:d,scaleY:u}=i.getAncestorScale(),h=this._skeleton.getCellIndexByOffset(s,l,d,u,c);this._startOffsetX=s,this._startOffsetY=l;const{row:g,column:S}=h,{startRow:f,startColumn:v,endRow:p,endColumn:C}=this._control.model;let _=0,w=0;g<f?_-=1:g>p&&(_+=1),S<v?w-=1:S>C&&(w+=1),this._relativeSelectionPositionRow=f-g+_,this._relativeSelectionPositionColumn=v-S+w,this._relativeSelectionRowLength=p-f,this._relativeSelectionColumnLength=C-v;const E=this._control.currentStyle,O=this._getScale();this.isHelperSelection&&(this._helperSelection=new M.Rect(ks,{stroke:E.stroke,strokeWidth:E.strokeWidth/O}),i.addObject(this._helperSelection));const R=i.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN),P=M.ScrollTimer.create(i);this._scrollTimer=P,P.startScroll(s,l,R),i.disableObjectsEvent(),this._scenePointerMoveSub=i.onPointerMove$.subscribeEvent(A=>{var L;if(this._disabled)return;const{offsetX:D,offsetY:k}=A;if(((L=this._injector.get(Ie,a.Quantity.OPTIONAL))==null?void 0:L.interceptor.fetchThroughInterceptors(Zo)(!1,null))===!1)return;const{x:V,y:W}=i.getCoordRelativeToViewport(M.Vector2.FromArray([D,k]));this._controlMoving(V,W),i.setCursor(M.CURSOR_TYPE.MOVE),P.scrolling(V,W,()=>{this._controlMoving(V,W)})}),this._scenePointerUpSub=i.onPointerUp$.subscribeEvent(()=>{var D,k,H,V;(D=this._helperSelection)==null||D.dispose();const A=this._scene;A.resetCursor(),this._clearObserverEvent(),A.enableObjectsEvent(),(k=this._scrollTimer)==null||k.dispose(),this._control.selectionMoveEnd$.next(this._targetSelection),(V=(H=this._selectionHooks).selectionMoveEnd)==null||V.call(H)})}_initialWidget(){const{topLeftWidget:o,topCenterWidget:e,topRightWidget:t,middleLeftWidget:i,middleRightWidget:r,bottomLeftWidget:s,bottomCenterWidget:l,bottomRightWidget:c}=this._control,d=[M.CURSOR_TYPE.NORTH_WEST_RESIZE,M.CURSOR_TYPE.NORTH_RESIZE,M.CURSOR_TYPE.NORTH_EAST_RESIZE,M.CURSOR_TYPE.WEST_RESIZE,M.CURSOR_TYPE.EAST_RESIZE,M.CURSOR_TYPE.SOUTH_WEST_RESIZE,M.CURSOR_TYPE.SOUTH_RESIZE,M.CURSOR_TYPE.SOUTH_EAST_RESIZE];[o,e,t,i,r,s,l,c].forEach((u,h)=>{u.onPointerEnter$.subscribeEvent(()=>{u.setCursor(d[h])}),u.onPointerLeave$.subscribeEvent(()=>{u.resetCursor()}),u.onPointerDown$.subscribeEvent(g=>{this._widgetPointerDownEvent(g,d[h])})})}_widgetPointerDownEvent(o,e){const t=this._scene,{offsetX:i,offsetY:r}=o,s=t.getCoordRelativeToViewport(M.Vector2.FromArray([i,r])),{x:l,y:c}=s;this._startOffsetX=i,this._startOffsetY=r;const{startRow:d,startColumn:u,endRow:h,endColumn:g}=this._control.model,S=Math.min(d,h),f=Math.min(u,g),v=Math.max(d,h),p=Math.max(u,g);this._relativeSelectionPositionRow=S,this._relativeSelectionPositionColumn=f,this._relativeSelectionRowLength=v-S,this._relativeSelectionColumnLength=p-f,e===M.CURSOR_TYPE.NORTH_WEST_RESIZE?(this._relativeSelectionPositionRow=v,this._relativeSelectionPositionColumn=p):e===M.CURSOR_TYPE.NORTH_RESIZE?this._relativeSelectionPositionRow=v:e===M.CURSOR_TYPE.NORTH_EAST_RESIZE?this._relativeSelectionPositionRow=v:e===M.CURSOR_TYPE.WEST_RESIZE?this._relativeSelectionPositionColumn=p:e===M.CURSOR_TYPE.SOUTH_WEST_RESIZE?this._relativeSelectionPositionColumn=p:e===M.CURSOR_TYPE.SOUTH_RESIZE&&(this._relativeSelectionPositionRow=S);const C=M.ScrollTimer.create(t),_=t.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN);C.startScroll(l,c,_),this._scrollTimer=C,t.disableObjectsEvent(),this._scenePointerMoveSub=t.onPointerMove$.subscribeEvent(w=>{const{offsetX:E,offsetY:O}=w,{x:R,y:P}=t.getCoordRelativeToViewport(M.Vector2.FromArray([E,O]));this._widgetMoving(R,P,e),t.setCursor(e),C.scrolling(R,P,()=>{this._widgetMoving(R,P,e)})}),this._scenePointerUpSub=t.onPointerUp$.subscribeEvent(()=>{var E,O,R;const w=this._scene;w.resetCursor(),this._clearObserverEvent(),w.enableObjectsEvent(),(E=this._scrollTimer)==null||E.dispose(),this._control.selectionScaled$.next(this._targetSelection),(R=(O=this._selectionHooks).selectionMoveEnd)==null||R.call(O)})}_widgetMoving(o,e,t){const i=this._scene,r=i.getScrollXYInfoByViewport(M.Vector2.FromArray([this._startOffsetX,this._startOffsetY])),{scaleX:s,scaleY:l}=i.getAncestorScale(),c=this._skeleton.getCellIndexByOffset(o,e,s,l,r),{row:d,column:u}=c;let h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=u;t===M.CURSOR_TYPE.NORTH_WEST_RESIZE?(h=d,g=u,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn):t===M.CURSOR_TYPE.NORTH_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength):t===M.CURSOR_TYPE.NORTH_EAST_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=u):t===M.CURSOR_TYPE.WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,f=this._relativeSelectionPositionColumn):t===M.CURSOR_TYPE.EAST_RESIZE?S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength:t===M.CURSOR_TYPE.SOUTH_WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=d,f=this._relativeSelectionPositionColumn):t===M.CURSOR_TYPE.SOUTH_RESIZE&&(h=this._relativeSelectionPositionRow,g=this._relativeSelectionPositionColumn,S=d,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength);const v=this._swapPositions(h,g,S,f),p=this._skeleton.getCellWithMergeInfoByIndex(h,g),_=Re({range:v,primary:p,style:null},this._skeleton);this._targetSelection={..._.rangeWithCoord},this._control.updateRangeBySelectionWithCoord(_),this._control.selectionScaling$.next(this._targetSelection)}_initialAutoFill(){const{fillControl:o}=this._control;o.onPointerEnter$.subscribeEvent(e=>{this._injector.get(Ie).interceptor.fetchThroughInterceptors(Go)(!1,{x:e.offsetX,y:e.offsetY,skeleton:this._skeleton,scene:this._scene})&&o.setCursor(M.CURSOR_TYPE.CROSSHAIR)}),o.onPointerLeave$.subscribeEvent(()=>{o.resetCursor()}),o.onPointerDown$.subscribeEvent(this._autoFillForPointerdown.bind(this))}_autoFillMoving(o,e){var k,H,V;const t=this._scene,i=t.getViewportScrollXY(this._activeViewport),{scaleX:r,scaleY:s}=t.getAncestorScale(),l=this._skeleton.getCellIndexByOffset(o,e,r,s,i),{row:c,column:d}=l,u=t.getCoordRelativeToViewport(M.Vector2.FromArray([o,e])),h=this._skeleton.getRowCount()-1,g=this._skeleton.getColumnCount()-1;let S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn,v=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,p=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength,C=!1,_=!0;if((d<f||d>p)&&c>=S&&c<=v){const W=this._fillRuler(d,f,p,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,p=W.endRowOrColumn,C=W.isLighten,_=!1}else if((c<S||c>v)&&d>=f&&d<=p){const W=this._fillRuler(c,S,v,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,v=W.endRowOrColumn,C=W.isLighten}else if(Math.abs(this._startOffsetX-u.x-i.x)/2>Math.abs(this._startOffsetY-u.y-i.y)){const W=this._fillRuler(d,f,p,this._relativeSelectionColumnLength,g);f=W.startRowOrColumn,p=W.endRowOrColumn,C=W.isLighten,_=!1}else{const W=this._fillRuler(c,S,v,this._relativeSelectionRowLength,h);S=W.startRowOrColumn,v=W.endRowOrColumn,C=W.isLighten}const w=this._skeleton.getNoMergeCellWithCoordByIndex(S,f),E=this._skeleton.getNoMergeCellWithCoordByIndex(v,p),O=(w==null?void 0:w.startY)||0,R=(E==null?void 0:E.endY)||0,P=(w==null?void 0:w.startX)||0,A=(E==null?void 0:E.endX)||0;C?this._controlHandler((W,L)=>{const B=new a.ColorKit(this._fillControlColors[L]).lighten(gd).toRgbString();W.setProps({fill:B})}):this._controlHandler((W,L)=>{W.setProps({fill:this._fillControlColors[L]})});const D=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/this._getScale();S===v&&_===!0||f===p&&_===!1?(k=this._helperSelection)==null||k.hide():((H=this._helperSelection)==null||H.transformByState({left:P-D/2,top:O-D/2,width:A-P,height:R-O}),(V=this._helperSelection)==null||V.show()),this._targetSelection={startY:O,endY:R,startX:P,endX:A,startRow:S,endRow:v,startColumn:f,endColumn:p},this._control.selectionFilling$.next(this._targetSelection)}_autoFillForPointerdown(o){const{offsetX:e,offsetY:t}=o,i=this._scene,r=i.getCoordRelativeToViewport(M.Vector2.FromArray([e,t])),{x:s,y:l}=r;this._startOffsetX=s,this._startOffsetY=l;const{startRow:c,startColumn:d,endRow:u,endColumn:h}=this._control.model;this._isInMergeState=this._hasMergeInRange(c,d,u,h),this._relativeSelectionPositionRow=c,this._relativeSelectionPositionColumn=d,this._relativeSelectionRowLength=u-c,this._relativeSelectionColumnLength=h-d;const g=this._control.currentStyle;let S=g==null?void 0:g.stroke,f=g==null?void 0:g.strokeWidth;const v=qe(this._themeService);S==null&&(S=v.stroke),f==null&&(f=v.strokeWidth);const p=this._getScale();f/=p;const C=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/p,_=new a.ColorKit(S).darken(2).toRgbString();this.isHelperSelection&&(this._helperSelection=new M.Rect(ks,{stroke:_,strokeWidth:f+C/2}),i.addObject(this._helperSelection)),this._activeViewport=i.getActiveViewportByCoord(M.Vector2.FromArray([e,t]));const w=i.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN),E=M.ScrollTimer.create(i,this._activeViewport.viewportKey===M.SHEET_VIEWPORT_KEY.VIEW_MAIN?M.ScrollTimerType.ALL:M.ScrollTimerType.NONE);E.startScroll(s,l,w),this._scrollTimer=E,i.disableObjectsEvent(),this._controlHandler(O=>{this._fillControlColors.push(O.fill)}),this._scenePointerMoveSub=i.onPointerMove$.subscribeEvent(O=>{var W;const{offsetX:R,offsetY:P}=O,A=i.getActiveViewportByCoord(M.Vector2.FromArray([R,P]));if(!this._injector.get(Ie).interceptor.fetchThroughInterceptors(Go)(!1,{x:o.offsetX,y:o.offsetY,skeleton:this._skeleton,scene:this._scene}))return;const{x:k,y:H}=i.getCoordRelativeToViewport(M.Vector2.FromArray([R,P]));this._autoFillMoving(k,H),i.setCursor(M.CURSOR_TYPE.CROSSHAIR);const V=this._targetSelection;if(w&&A&&((W=this._activeViewport)==null?void 0:W.viewportKey)!==(A==null?void 0:A.viewportKey)){let L;V.startRow!==c?(E.scrollTimerType=M.ScrollTimerType.Y,L={...V,endRow:V.startRow}):V.endRow!==u?(E.scrollTimerType=M.ScrollTimerType.Y,L={...V,startRow:V.endRow}):V.startColumn!==d?(E.scrollTimerType=M.ScrollTimerType.X,L={...V,endColumn:V.startColumn}):(E.scrollTimerType=M.ScrollTimerType.X,L={...V,startColumn:V.endColumn}),this._isSelectionInViewport(L,A)&&(this._viewportMainScrollBarNeedResetPosition(E.scrollTimerType,this._activeViewport.viewportKey,A.viewportKey)&&w.scrollToBarPos({x:E.scrollTimerType===M.ScrollTimerType.X?0:void 0,y:E.scrollTimerType===M.ScrollTimerType.Y?0:void 0}),this._activeViewport=A)}E.scrolling(k,H,()=>{this._autoFillMoving(k,H)})}),this._scenePointerUpSub=i.onPointerUp$.subscribeEvent(()=>{var R,P;(R=this._helperSelection)==null||R.dispose();const O=this._scene;O.resetCursor(),this._clearObserverEvent(),O.enableObjectsEvent(),(P=this._scrollTimer)==null||P.dispose(),this._control.refreshSelectionFilled(this._targetSelection),this._isInMergeState=!1,this._controlHandler((A,D)=>{A.setProps({fill:this._fillControlColors[D]})}),this._fillControlColors=[]})}_viewportMainScrollBarNeedResetPosition(o,e,t){return o===M.ScrollTimerType.X&&(e===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT||e===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP)&&(t===M.SHEET_VIEWPORT_KEY.VIEW_MAIN||t===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP)||o===M.ScrollTimerType.Y&&(e===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP||e===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP)&&(t===M.SHEET_VIEWPORT_KEY.VIEW_MAIN||t===M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)}_hasMergeInRange(o,e,t,i){return this._skeleton.worksheet.getMergedCellRange(o,e,t,i).length>0}_swapPositions(o,e,t,i){const r=Math.min(o,t),s=Math.min(e,i),l=Math.max(o,t),c=Math.max(e,i);return{startRow:r,startColumn:s,endRow:l,endColumn:c}}_controlHandler(o){const{leftControl:e,rightControl:t,topControl:i,bottomControl:r,backgroundControlTop:s,backgroundControlMiddleLeft:l,backgroundControlMiddleRight:c,backgroundControlBottom:d,fillControl:u}=this._control,h=[e,t,i,r,s,l,c,d,u];for(let g=0,S=h.length;g<S;g++){const f=h[g];o(f,g)}}_fillRuler(o,e,t,i,r){let s=!1;if(o<e)if(this._isInMergeState&&o<e){const l=e-o,c=i+1,d=Math.ceil(l/c);let u=e-d*c;u<0&&(u=e-(d-1)*c),e=u}else e=o;else if(o>=e&&o<=t)s=!0,this._isInMergeState||(t=o);else if(this._isInMergeState&&o>t){const l=o-t,c=i+1,d=Math.ceil(l/c);let u=t+d*c;u>r&&(u=t+(d-1)*c),t=u}else t=o;return{rowOrColumn:o,startRowOrColumn:e,endRowOrColumn:t,isLighten:s}}_getScale(){const{scaleX:o,scaleY:e}=this._scene.getAncestorScale();return Math.max(o,e)}}var qo=(n=>(n.Selection="__SpreadsheetSelectionShape__",n.top="__SpreadsheetSelectionTopControl__",n.bottom="__SpreadsheetSelectionBottomControl__",n.left="__SpreadsheetSelectionShapeLeftControl__",n.right="__SpreadsheetSelectionShapeRightControl__",n.backgroundTop="__SpreadsheetSelectionBackgroundControlTop__",n.backgroundMiddleLeft="__SpreadsheetSelectionBackgroundControlMiddleLeft__",n.backgroundMiddleRight="__SpreadsheetSelectionBackgroundControlMiddleRight__",n.backgroundBottom="__SpreadsheetSelectionBackgroundControlBottom__",n.fill="__SpreadsheetSelectionFillControl__",n.fillTopLeft="__SpreadsheetSelectionFillControlTopLeft__",n.fillBottomRight="__SpreadsheetSelectionFillControlBottomRight__",n.fillTopLeftInner="__SpreadsheetSelectionFillControlTopLeftInner__",n.fillBottomRightInner="__SpreadsheetSelectionFillControlBottomRightInner__",n.lineMain="__SpreadsheetDragLineMainControl__",n.lineContent="__SpreadsheetDragLineContentControl__",n.line="__SpreadsheetDragLineControl__",n.dash="__SpreadsheetDragDashControl__",n.rowHeaderBackground="__SpreadSheetSelectionRowHeaderBackground__",n.rowHeaderBorder="__SpreadSheetSelectionRowHeaderBorder__",n.rowHeaderGroup="__SpreadSheetSelectionRowHeaderGroup__",n.columnHeaderBackground="__SpreadSheetSelectionColumnHeaderBackground__",n.columnHeaderBorder="__SpreadSheetSelectionColumnHeaderBorder__",n.columnHeaderGroup="__SpreadSheetSelectionColumnHeaderGroup__",n.topLeftWidget="__SpreadSheetSelectionTopLeftWidget__",n.topCenterWidget="__SpreadSheetSelectionTopCenterWidget__",n.topRightWidget="__SpreadSheetSelectionTopRightWidget__",n.middleLeftWidget="__SpreadSheetSelectionMiddleLeftWidget__",n.middleRightWidget="__SpreadSheetSelectionMiddleRightWidget__",n.bottomLeftWidget="__SpreadSheetSelectionBottomLeftWidget__",n.bottomCenterWidget="__SpreadSheetSelectionBottomCenterWidget__",n.bottomRightWidget="__SpreadSheetSelectionBottomRightWidget__",n))(qo||{});const Vs=.3;class Qn extends a.Disposable{constructor(e,t,i,r){var s,l,c,d;super();y(this,"_isHelperSelection",!0);y(this,"_enableAutoFill",!0);y(this,"_showAutoFill",!1);y(this,"_highlightHeader",!0);y(this,"_selectionRenderModel");y(this,"_leftBorder");y(this,"_rightBorder");y(this,"_topBorder");y(this,"_bottomBorder");y(this,"_backgroundControlTop");y(this,"_backgroundControlBottom");y(this,"_backgroundControlMiddleLeft");y(this,"_backgroundControlMiddleRight");y(this,"_autoFillControl");y(this,"_selectionShapeGroup");y(this,"_rowHeaderBackground");y(this,"_rowHeaderBorder");y(this,"_rowHeaderGroup");y(this,"_columnHeaderBackground");y(this,"_columnHeaderBorder");y(this,"_columnHeaderGroup");y(this,"_dashedRect");y(this,"_topLeftWidget");y(this,"_topCenterWidget");y(this,"_topRightWidget");y(this,"_middleLeftWidget");y(this,"_middleRightWidget");y(this,"_bottomLeftWidget");y(this,"_bottomCenterWidget");y(this,"_bottomRightWidget");y(this,"_defaultStyle");y(this,"_currentStyle");y(this,"_rowHeaderWidth",0);y(this,"_columnHeaderHeight",0);y(this,"_widgetRects",[]);y(this,"_controlExtension");y(this,"_dispose$",new N.BehaviorSubject(this));y(this,"dispose$",this._dispose$.asObservable());y(this,"selectionMoving$",new N.Subject);y(this,"selectionMoveEnd$",new N.Subject);y(this,"selectionScaling$",new N.Subject);y(this,"selectionScaled$",new N.Subject);y(this,"selectionFilling$",new N.Subject);y(this,"_selectionFilled$",new N.Subject);y(this,"selectionFilled$",this._selectionFilled$.asObservable());y(this,"_antLineOffset",0);y(this,"_antRequestNewFrame",-1);this._scene=e,this._zIndex=t,this._themeService=i,this._enableAutoFill=(s=r==null?void 0:r.enableAutoFill)!=null?s:!0,this._highlightHeader=(l=r==null?void 0:r.highlightHeader)!=null?l:!0,this._rowHeaderWidth=(c=r==null?void 0:r.rowHeaderWidth)!=null?c:0,this._columnHeaderHeight=(d=r==null?void 0:r.columnHeaderHeight)!=null?d:0,this._initializeSheetBody(),this._initialHeader()}_initializeSheetBody(){this._defaultStyle=qe(this._themeService),this._currentStyle=qe(this._themeService),this._selectionRenderModel=new md;const e=this._zIndex;this._leftBorder=new M.Rect("__SpreadsheetSelectionShapeLeftControl__"+e,{zIndex:e}),this._rightBorder=new M.Rect("__SpreadsheetSelectionShapeRightControl__"+e,{zIndex:e}),this._topBorder=new M.Rect("__SpreadsheetSelectionTopControl__"+e,{zIndex:e}),this._bottomBorder=new M.Rect("__SpreadsheetSelectionBottomControl__"+e,{zIndex:e}),this._backgroundControlTop=new M.Rect("__SpreadsheetSelectionBackgroundControlTop__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlBottom=new M.Rect("__SpreadsheetSelectionBackgroundControlBottom__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleLeft=new M.Rect("__SpreadsheetSelectionBackgroundControlMiddleLeft__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleRight=new M.Rect("__SpreadsheetSelectionBackgroundControlMiddleRight__"+e,{zIndex:e-1,evented:!1}),this._autoFillControl=new M.Rect("__SpreadsheetSelectionFillControl__"+e,{zIndex:e+1}),this._autoFillControl.hide(),this._dashedRect=new M.DashedRect("__SpreadsheetDragDashControl__"+e,{zIndex:e+2,evented:!1,stroke:"#fff"});const t=[this._autoFillControl,this._leftBorder,this._rightBorder,this._topBorder,this._bottomBorder,this._backgroundControlTop,this._backgroundControlMiddleLeft,this._backgroundControlMiddleRight,this._backgroundControlBottom,this._dashedRect];this._widgetRects=this._initialWidget(),this._selectionShapeGroup=new M.Group("__SpreadsheetSelectionShape__"+e,...t,...this._widgetRects),this._selectionShapeGroup.hide(),this._selectionShapeGroup.evented=!1,this._selectionShapeGroup.zIndex=e;const i=this.getScene();i.addObject(this._selectionShapeGroup,Fi),this.disposeWithMe(a.toDisposable(i.onTransformChange$.subscribeEvent(r=>{r.type===M.TRANSFORM_CHANGE_OBSERVABLE_TYPE.scale&&(this._updateLayoutOfSelectionControl(this._currentStyle),this._updateControlCoord())})))}_initialHeader(){const e=this._zIndex;this._rowHeaderBackground=new M.Rect("__SpreadSheetSelectionRowHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderBorder=new M.Rect("__SpreadSheetSelectionRowHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderGroup=new M.Group("__SpreadSheetSelectionRowHeaderGroup__"+e,this._rowHeaderBackground,this._rowHeaderBorder),this._rowHeaderGroup.hide(),this._rowHeaderGroup.evented=!1,this._rowHeaderGroup.zIndex=e,this._columnHeaderBackground=new M.Rect("__SpreadSheetSelectionColumnHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderBorder=new M.Rect("__SpreadSheetSelectionColumnHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderGroup=new M.Group("__SpreadSheetSelectionColumnHeaderGroup__"+e,this._columnHeaderBackground,this._columnHeaderBorder),this._columnHeaderGroup.hide(),this._columnHeaderGroup.evented=!1,this._columnHeaderGroup.zIndex=e,this.getScene().addObjects([this._rowHeaderGroup,this._columnHeaderGroup],hd)}_initialWidget(){const e=this._zIndex;return this._topLeftWidget=new M.Rect("__SpreadSheetSelectionTopLeftWidget__"+e,{zIndex:e+1}),this._topCenterWidget=new M.Rect("__SpreadSheetSelectionTopCenterWidget__"+e,{zIndex:e+1}),this._topRightWidget=new M.Rect("__SpreadSheetSelectionTopRightWidget__"+e,{zIndex:e+1}),this._middleLeftWidget=new M.Rect("__SpreadSheetSelectionMiddleLeftWidget__"+e,{zIndex:e+1}),this._middleRightWidget=new M.Rect("__SpreadSheetSelectionMiddleRightWidget__"+e,{zIndex:e+1}),this._bottomLeftWidget=new M.Rect("__SpreadSheetSelectionBottomLeftWidget__"+e,{zIndex:e+1}),this._bottomCenterWidget=new M.Rect("__SpreadSheetSelectionBottomCenterWidget__"+e,{zIndex:e+1}),this._bottomRightWidget=new M.Rect("__SpreadSheetSelectionBottomRightWidget__"+e,{zIndex:e+1}),[this._topLeftWidget,this._topCenterWidget,this._topRightWidget,this._middleLeftWidget,this._middleRightWidget,this._bottomLeftWidget,this._bottomCenterWidget,this._bottomRightWidget]}get zIndex(){return this._zIndex}get leftControl(){return this._leftBorder}get rightControl(){return this._rightBorder}get topControl(){return this._topBorder}get bottomControl(){return this._bottomBorder}get fillControl(){return this._autoFillControl}get backgroundControlTop(){return this._backgroundControlTop}get backgroundControlBottom(){return this._backgroundControlBottom}get backgroundControlMiddleLeft(){return this._backgroundControlMiddleLeft}get backgroundControlMiddleRight(){return this._backgroundControlMiddleRight}get selectionShape(){return this._selectionShapeGroup}get columnHeaderGroup(){return this._columnHeaderGroup}get rowHeaderGroup(){return this._rowHeaderGroup}get selectionShapeGroup(){return this._selectionShapeGroup}get model(){return this._selectionRenderModel}get topLeftWidget(){return this._topLeftWidget}get topCenterWidget(){return this._topCenterWidget}get topRightWidget(){return this._topRightWidget}get middleLeftWidget(){return this._middleLeftWidget}get middleRightWidget(){return this._middleRightWidget}get bottomLeftWidget(){return this._bottomLeftWidget}get bottomCenterWidget(){return this._bottomCenterWidget}get bottomRightWidget(){return this._bottomRightWidget}get themeService(){return this._themeService}get selectionModel(){return this._selectionRenderModel}set selectionModel(e){this._selectionRenderModel=e}get currentStyle(){return this._currentStyle}set currentStyle(e){this._currentStyle=e}get dashedRect(){return this._dashedRect}get isHelperSelection(){return this._isHelperSelection}get rowHeaderWidth(){return this._rowHeaderWidth}set rowHeaderWidth(e){this._rowHeaderWidth=e}get columnHeaderHeight(){return this._columnHeaderHeight}set columnHeaderHeight(e){this._columnHeaderHeight=e}setControlExtension(e){this._controlExtension=new Hs(this,e)}setControlExtensionDisable(e){this._controlExtension&&this._controlExtension.setDisabled(e)}setEvent(e){this.leftControl.evented=e,this.rightControl.evented=e,this.topControl.evented=e,this.bottomControl.evented=e}refreshSelectionFilled(e){this._selectionFilled$.next(e)}_updateLayoutOfSelectionControl(e){e&&(this.currentStyle=Object.assign({},this._defaultStyle,e));const t=this.currentStyle,{stroke:i,widgets:r,autofillStroke:s,strokeDash:l,isAnimationDash:c}=t;let d=t.autofillSize,u=t.strokeWidth,h=t.autofillStrokeWidth;const g=this._getScale(),S=(u+m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH)/2/g;u/=g,d/=g,h/=g<1?1:g;const f=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/g,v=M.FIX_ONE_PIXEL_BLUR_OFFSET/g,{startX:p,startY:C,endX:_,endY:w}=this._selectionRenderModel;if(this.leftControl.transformByState({height:w-C,left:-S+v,width:u,strokeWidth:f,top:-f/2+v}),this.leftControl.setProps({fill:i,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.rightControl.transformByState({height:w-C,left:_-p-S+v,width:u,strokeWidth:f,top:-f/2+v}),this.rightControl.setProps({fill:i,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.topControl.transformByState({width:_-p+u,top:-S+v,left:-S+v,height:u,strokeWidth:f}),this.topControl.setProps({fill:i,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),this.bottomControl.transformByState({width:_-p+u,top:w-C-S+v,height:u,left:-S+v,strokeWidth:f}),this.bottomControl.setProps({fill:i,stroke:m.SELECTION_CONTROL_BORDER_BUFFER_COLOR}),l==null)this.dashedRect.hide(),this._stopAntLineAnimation();else{const E=t.strokeWidth*2/g;this.dashedRect.transformByState({height:w-C,width:_-p,strokeWidth:E,left:-E/2+v,top:-E/2+v}),this.dashedRect.setProps({strokeDashArray:[0,l/g]}),this._stopAntLineAnimation(),c!==!1&&this._startAntLineAnimation(),this.dashedRect.show()}if(this._showAutoFill=this._showAutoFill&&this._enableAutoFill,this._showAutoFill&&!this._hasWidgets(r)){const E={fill:i,stroke:s,strokeScaleEnabled:!1},O={width:d-h,height:d-h,left:_-p-d/2+h/2-v,top:w-C-d/2+h/2-v,strokeWidth:h};this._autoFillControl.setProps(E),this._autoFillControl.transformByState(O),this._autoFillControl.show()}else this._autoFillControl.hide();this._updateBackgroundControl(t),this._updateHeaderBackground(t),this._updateWidgets(t)}_updateControlCoord(){const{startX:e,startY:t}=this._selectionRenderModel;this.selectionShapeGroup.show(),this.selectionShapeGroup.translate(e,t),this.selectionShapeGroup.makeDirtyNoDebounce(!0)}updateStyle(e){this._updateLayoutOfSelectionControl(e),this._updateControlCoord()}updateRange(e,t){this._selectionRenderModel.setValue(e,t),this._showAutoFill=t!==null,this._updateLayoutOfSelectionControl(),this._updateControlCoord()}updateRangeBySelectionWithCoord(e,t){t&&(this._rowHeaderWidth=t.rowHeaderWidth,this._columnHeaderHeight=t.columnHeaderHeight),this._selectionRenderModel.setValue(e.rangeWithCoord,e.primaryWithCoord),this._showAutoFill=e.primaryWithCoord!==null,this._updateLayoutOfSelectionControl(e.style),this._updateControlCoord()}update(e,t=0,i=0,r,s){this._rowHeaderWidth=t,this._columnHeaderHeight=i,this.updateRangeBySelectionWithCoord({rangeWithCoord:e,primaryWithCoord:s,style:r}),r&&this.updateStyle(r)}updateCurrCell(e){this._showAutoFill=e!==null,this._selectionRenderModel.setCurrentCell(e)}clearHighlight(){this._showAutoFill=!1,this._selectionRenderModel.clearCurrentCell(),this._updateLayoutOfSelectionControl(this._currentStyle)}getScene(){return this._scene}dispose(){var e,t,i,r,s,l,c,d,u,h,g,S,f,v,p,C,_,w,E,O,R,P,A,D,k;(e=this._leftBorder)==null||e.dispose(),(t=this._rightBorder)==null||t.dispose(),(i=this._topBorder)==null||i.dispose(),(r=this._bottomBorder)==null||r.dispose(),(s=this._backgroundControlTop)==null||s.dispose(),(l=this._backgroundControlMiddleLeft)==null||l.dispose(),(c=this._backgroundControlMiddleRight)==null||c.dispose(),(d=this._backgroundControlBottom)==null||d.dispose(),this._autoFillControl.dispose(),(u=this._selectionShapeGroup)==null||u.dispose(),(h=this._rowHeaderBackground)==null||h.dispose(),(g=this._rowHeaderBorder)==null||g.dispose(),(S=this._rowHeaderGroup)==null||S.dispose(),(f=this._rowHeaderBackground)==null||f.dispose(),(v=this._columnHeaderBackground)==null||v.dispose(),(p=this._columnHeaderBorder)==null||p.dispose(),(C=this._columnHeaderGroup)==null||C.dispose(),(_=this._topLeftWidget)==null||_.dispose(),(w=this._topCenterWidget)==null||w.dispose(),(E=this._topRightWidget)==null||E.dispose(),(O=this._middleLeftWidget)==null||O.dispose(),(R=this._middleRightWidget)==null||R.dispose(),(P=this._bottomLeftWidget)==null||P.dispose(),(A=this._bottomCenterWidget)==null||A.dispose(),(D=this._bottomRightWidget)==null||D.dispose(),(k=this._controlExtension)==null||k.dispose(),super.dispose(),this._dispose$.next(this),this._dispose$.complete()}getCurrentCellInfo(){const e=this.model.currentCell;if(e){let t;if(e.isMerged){const i=e.mergeInfo;t={startRow:i.startRow,endRow:i.endRow,startColumn:i.startColumn,endColumn:i.endColumn,startX:i.startX,endX:i.endX,startY:i.startY,endY:i.endY}}else{const{actualRow:i,actualColumn:r,startX:s,endX:l,startY:c,endY:d}=e;t={startRow:i,endRow:i,startColumn:r,endColumn:r,startX:s,endX:l,startY:c,endY:d}}return t}}getValue(){return{...this._selectionRenderModel.getValue(),style:this._currentStyle}}getRange(){return this._selectionRenderModel.getValue().rangeWithCoord}enableHelperSelection(){this._isHelperSelection=!0}disableHelperSelection(){this._isHelperSelection=!1}_updateHeaderBackground(e){const{startX:t,startY:i,endX:r,endY:s,rangeType:l}=this._selectionRenderModel,c=this._currentStyle,d=this._getScale(),{stroke:u,rowHeaderFill:h=c.rowHeaderFill,rowHeaderStroke:g=c.rowHeaderStroke,columnHeaderFill:S=c.columnHeaderFill,columnHeaderStroke:f=c.columnHeaderStroke}=e;let{rowHeaderStrokeWidth:v=c.rowHeaderStrokeWidth,columnHeaderStrokeWidth:p=c.columnHeaderStrokeWidth}=e;v/=d,p/=d;const C=this._rowHeaderWidth,_=this._columnHeaderHeight;if(this._highlightHeader&&_>0){let w=S;l===a.RANGE_TYPE.COLUMN&&(w=new a.ColorKit(u).setAlpha(Vs).toString()),this._columnHeaderBackground.setProps({fill:w}),this._columnHeaderBackground.resize(r-t,_),this._columnHeaderBorder.setProps({fill:f}),this._columnHeaderBorder.transformByState({width:r-t,height:p,top:_-p+1/d}),this._columnHeaderGroup.show(),this._columnHeaderGroup.translate(t,0)}else this._columnHeaderGroup.hide();if(this._columnHeaderGroup.makeDirty(!0),this._highlightHeader&&C>0){let w=h;l===a.RANGE_TYPE.ROW&&(w=new a.ColorKit(u).setAlpha(Vs).toString()),this._rowHeaderBackground.setProps({fill:w}),this._rowHeaderBackground.resize(C,s-i),this._rowHeaderBorder.setProps({fill:g}),this._rowHeaderBorder.transformByState({width:v,height:s-i,left:C-v+1/d}),this._rowHeaderGroup.show(),this._rowHeaderGroup.translate(0,i)}else this._rowHeaderGroup.hide();this._rowHeaderGroup.makeDirty(!0)}_updateBackgroundControl(e){const{startX:t,startY:i,endX:r,endY:s}=this._selectionRenderModel,l=this._getScale(),{fill:c=e.fill}=e;let{strokeWidth:d}=e;d/=l;const u=this._selectionRenderModel.highlightToSelection();if(!u){this._backgroundControlTop.resize(r-t,s-i),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlBottom.resize(0,0),this._backgroundControlMiddleLeft.resize(0,0),this._backgroundControlMiddleRight.resize(0,0);return}const{startX:h,startY:g,endX:S,endY:f}=u,v=d/2,p={left:-v,top:-v,width:r-t+v*2,height:g-i+v};p.height<0&&(p.width=0,p.height=0),this._backgroundControlTop.transformByState(p);const C={left:-v,top:g-i,width:h-t+v,height:f-g};C.width<0&&(C.width=0,C.height=0),this._backgroundControlMiddleLeft.transformByState(C);const _={left:S-t-v,top:g-i,width:r-S+v*2,height:f-g};_.width<0&&(_.width=0,_.height=0),this._backgroundControlMiddleRight.transformByState(_);const w={left:-v,top:f-i,width:r-t+v*2,height:s-f+v};w.height<0&&(w.width=0,w.height=0),this._backgroundControlBottom.transformByState(w),this._backgroundControlTop.setProps({fill:c}),this._backgroundControlMiddleLeft.setProps({fill:c}),this._backgroundControlMiddleRight.setProps({fill:c}),this._backgroundControlBottom.setProps({fill:c})}_updateWidgets(e){const{startX:t,startY:i,endX:r,endY:s}=this._selectionRenderModel,{stroke:l=e.stroke,widgets:c=e.widgets,widgetStroke:d=e.widgetStroke}=e,u=this._getScale();let{widgetSize:h,widgetStrokeWidth:g}=e;h/=u,g/=u;const S={left:-h/2+g/2,center:(r-t)/2-h/2+g/2,right:r-t-h/2+g/2,top:-h/2,middle:(s-i)/2-h/2,bottom:s-i-h/2+g/2},f=h-g;this._widgetRects.forEach(v=>{v.setProps({fill:l,stroke:d})}),c.tl===!0?(this._topLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.top,strokeWidth:g}),this._topLeftWidget.show()):this._topLeftWidget.hide(),c.tc===!0?(this._topCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.top,strokeWidth:g}),this._topCenterWidget.show()):this._topCenterWidget.hide(),c.tr===!0?(this._topRightWidget.transformByState({height:f,width:f,left:S.right,top:S.top,strokeWidth:g}),this._topRightWidget.show()):this._topRightWidget.hide(),c.ml===!0?(this._middleLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.middle,strokeWidth:g}),this._middleLeftWidget.show()):this._middleLeftWidget.hide(),c.mr===!0?(this._middleRightWidget.transformByState({height:f,width:f,left:S.right,top:S.middle,strokeWidth:g}),this._middleRightWidget.show()):this._middleRightWidget.hide(),c.bl===!0?(this._bottomLeftWidget.transformByState({height:f,width:f,left:S.left,top:S.bottom,strokeWidth:g}),this._bottomLeftWidget.show()):this._bottomLeftWidget.hide(),c.bc===!0?(this._bottomCenterWidget.transformByState({height:f,width:f,left:S.center,top:S.bottom,strokeWidth:g}),this._bottomCenterWidget.show()):this._bottomCenterWidget.hide(),c.br===!0?(this._bottomRightWidget.transformByState({height:f,width:f,left:S.right,top:S.bottom,strokeWidth:g}),this._bottomRightWidget.show()):this._bottomRightWidget.hide()}_hasWidgets(e){if(e==null)return!1;const t=Object.keys(e);if(t.length===0)return!1;for(const i of t)if(e[i]===!0)return!0;return!1}_getScale(){const{scaleX:e,scaleY:t}=this._scene.getAncestorScale();return Math.max(e,t)}_stopAntLineAnimation(){this._antLineOffset=0,M.cancelRequestFrame(this._antRequestNewFrame)}_startAntLineAnimation(){const e=this._getScale();this._antLineOffset+=.6/e,this._antLineOffset>160/e&&(this._antLineOffset=0),this.dashedRect.setProps({strokeDashOffset:-this._antLineOffset}),this._antRequestNewFrame=M.requestNewFrame(()=>{this._startAntLineAnimation()})}}class Sd extends M.Layer{render(o,e=!1){const t=a.Tools.now();return super.render(o,e),this._afterRender(t),this}_afterRender(o){this.scene.getEngine().renderFrameTimeMetric$.next(["selectionLayer",a.Tools.now()-o])}}const Ie=a.createIdentifier("univer.sheet.selection-render-service");class Qo extends a.Disposable{constructor(e,t,i,r,s){super();y(this,"_downObserver");y(this,"_scenePointerMoveSub");y(this,"_scenePointerUpSub");y(this,"_controlFillConfig$",new N.BehaviorSubject(null));y(this,"controlFillConfig$",this._controlFillConfig$.asObservable());y(this,"_selectionControls",[]);y(this,"_startRangeWhenPointerDown",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});y(this,"_activeControlIndex",-1);y(this,"_startViewportPosX",0);y(this,"_startViewportPosY",0);y(this,"_scrollTimer");y(this,"_cancelDownSubscription");y(this,"_cancelUpSubscription");y(this,"_skeleton");y(this,"_scene");y(this,"_highlightHeader",!0);y(this,"_rangeType",a.RANGE_TYPE.NORMAL);y(this,"_selectionStyle");y(this,"_remainLastEnabled",!1);y(this,"_skipLastEnabled",!1);y(this,"_singleSelectionEnabled",!1);y(this,"_selectionMoveEnd$",new N.BehaviorSubject([]));y(this,"selectionMoveEnd$",this._selectionMoveEnd$.asObservable());y(this,"_selectionMoving$",new N.Subject);y(this,"selectionMoving$",this._selectionMoving$.asObservable());y(this,"_selectionMoveStart$",new N.Subject);y(this,"selectionMoveStart$",this._selectionMoveStart$.asObservable());y(this,"_selectionMoving",!1);y(this,"_selectionTheme");y(this,"_activeViewport");y(this,"interceptor",new a.InterceptorManager({RANGE_MOVE_PERMISSION_CHECK:Zo,RANGE_FILL_PERMISSION_CHECK:Go}));y(this,"_escapeShortcutDisposable",null);this._injector=e,this._themeService=t,this._shortcutService=i,this._sheetSkeletonManagerService=r,this.contextService=s,this._initSelectionThemeFromThemeService(),this._initMoving()}get selectionMoving(){return this._selectionMoving}get _shouldDetectMergedCells(){return this._rangeType===a.RANGE_TYPE.NORMAL}_initMoving(){this.disposeWithMe(this._selectionMoving$.subscribe(()=>{this._selectionMoving=!0})),this.disposeWithMe(this._selectionMoveEnd$.subscribe(()=>{this._selectionMoving=!1}))}_setSelectionStyle(e){this._selectionStyle=e}getViewPort(){return this._activeViewport}setSingleSelectionEnabled(e=!1){this._singleSelectionEnabled=e}newSelectionControl(e,t,i){const r=this.getSelectionControls().length,{rowHeaderWidth:s,columnHeaderHeight:l}=t,c=new Qn(e,r,this._selectionTheme,{highlightHeader:this._highlightHeader,rowHeaderWidth:s,columnHeaderHeight:l});this._selectionControls.push(c);const d=Re(i,t);return c.updateRangeBySelectionWithCoord(d,t),c.setControlExtension({skeleton:t,scene:e,themeService:this._selectionTheme,injector:this._injector,selectionHooks:{selectionMoveEnd:()=>{this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}}}),c}resetSelectionsByModelData(e){const t=this.getSelectionControls(),i=this._skeleton;if(!(!t||!i)){for(let r=0,s=e.length;r<s;r++){const l=e[r],c=Re(l,this._skeleton),d=t[r];d?d.updateRangeBySelectionWithCoord(c,i):this.isSelectionEnabled()&&this.newSelectionControl(this._scene,i,l)}e.length<t.length&&t.splice(e.length).forEach(s=>s.dispose())}}refreshSelectionMoveStart(){this._selectionMoveStart$.next(this.getSelectionDataWithStyle())}refreshSelectionMoveEnd(){this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}_initSelectionThemeFromThemeService(){const e=this._themeService.getCurrentTheme();this._selectionTheme=new a.ThemeService,this._selectionTheme.setTheme(e)}setSelectionTheme(e){this._selectionTheme.setTheme(e)}_changeRuntime(e,t,i){this._skeleton=e,this._scene=t,this._activeViewport=i||(t==null?void 0:t.getViewports()[0]),t.findLayerByZIndex(Fi)||t.addLayer(new Sd(t,[],Fi))}getSkeleton(){return this._skeleton}getSelectionDataWithStyle(){const e=this._selectionControls;if(this._skeleton.worksheet){const[t,i]=this._skeleton.getLocation();return e.map(r=>{const s=r.getValue();return s.rangeWithCoord.sheetId=i,s.rangeWithCoord.unitId=t,s})}return[]}getSelectionControls(){return this._selectionControls}_addSelectionControlByModelData(e){var l;const t=this._skeleton,i=(l=e.style)!=null?l:qe(this._selectionTheme),r=this._scene;return e.style=i,this.newSelectionControl(r,t,e)}_clearAllSelectionControls(){const e=this._selectionControls;for(const t of e)t.dispose();this._selectionControls.length=0}_getFreeze(){var t;return(t=this._sheetSkeletonManagerService.getCurrentParam())==null?void 0:t.skeleton.getWorksheetConfig().freeze}_getViewportByCell(e,t){if(e===void 0||t===void 0)return null;const i=this._getFreeze();if(!i||i.startRow<=0&&i.startColumn<=0)return this._scene.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e>i.startRow&&t>i.startColumn)return this._scene.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e<=i.startRow&&t<=i.startColumn)return this._scene.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP);if(e<=i.startRow&&t>i.startColumn)return this._scene.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);if(e>i.startRow&&t<=i.startColumn)return this._scene.getViewport(M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT)}getActiveRange(){const e=this.getSelectionControls(),t=e&&e[e.length-1].model;return t&&{startRow:t.startRow,startColumn:t.startColumn,endRow:t.endRow,endColumn:t.endColumn}}setActiveSelectionIndex(e){this._activeControlIndex=e}resetActiveSelectionIndex(){this._activeControlIndex=-1}getActiveSelectionControl(){const e=this.getSelectionControls();if(e)return this._activeControlIndex<0?e[e.length-1]:e[this._activeControlIndex]}endSelection(){var e;this._clearUpdatingListeners(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle()),(e=this._escapeShortcutDisposable)==null||e.dispose(),this._escapeShortcutDisposable=null}_reset(){var e;this._clearAllSelectionControls(),(e=this._downObserver)==null||e.unsubscribe(),this._downObserver=null}_setupPointerMoveListener(e,t,i,r=M.ScrollTimerType.ALL,s,l){var f,v;let c=0,d=0,u=s,h=l;this._scrollTimer=M.ScrollTimer.create(this._scene,r),this._scrollTimer.startScroll((f=e==null?void 0:e.left)!=null?f:0,(v=e==null?void 0:e.top)!=null?v:0,e);const g=this._scene,S=g.getActiveViewportByCoord(M.Vector2.FromArray([s,l]));this._scenePointerMoveSub=g.onPointerMove$.subscribeEvent(p=>{var V,W,L,B,U,F,z,X,j;const{offsetX:C,offsetY:_}=p,{x:w,y:E}=g.getCoordRelativeToViewport(M.Vector2.FromArray([C,_]));this._movingHandler(w,E,t,i);let O=w,R=E;const P=this.getActiveSelectionControl(),A=this._getFreeze(),D=P==null?void 0:P.model,k=(V=g.getActiveViewportByCoord(M.Vector2.FromArray([C,_])))!=null?V:this._getViewportByCell(D==null?void 0:D.endRow,D==null?void 0:D.endColumn);if((()=>{if(!S||!k||!e)return!1;const Q=[M.SHEET_VIEWPORT_KEY.VIEW_MAIN,M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP,M.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP,M.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT];return Q.includes(S.viewportKey)&&Q.includes(k.viewportKey)})()){if(!S||!k||!e)return!1;const Q=u<e.left&&w>e.left||u>e.left&&w<e.left,oe=h<e.top&&E>e.top||h>e.top&&E<e.top;Q&&(c+=1),oe&&(d+=1);const re=S.viewportKey,le=k.viewportKey;if(re===M.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP)_<e.top&&((W=D==null?void 0:D.endRow)!=null?W:0)<((L=A==null?void 0:A.startRow)!=null?L:0)?R=e.top:oe&&d