@univerjs/sheets-ui
Version:
UniverSheet normal ui-plugin-sheets
1 lines • 872 kB
JavaScript
(function(y,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("react"),require("@univerjs/docs"),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","react","@univerjs/docs","@univerjs/engine-formula","react/jsx-runtime","@univerjs/design","rxjs/operators","@univerjs/telemetry"],q):(y=typeof globalThis<"u"?globalThis:y||self,q(y.UniverSheetsUi={},y.UniverDocsUi,y.UniverCore,y.UniverSheets,y.UniverEngineRender,y.rxjs,y.UniverUi,y.React,y.UniverDocs,y.UniverEngineFormula,y.React,y.UniverDesign,y.rxjs.operators,y.UniverTelemetry))})(this,function(y,q,a,m,R,D,_,L,Ce,Q,b,X,de,id){"use strict";var np=Object.defineProperty;var ip=(y,q,a)=>q in y?np(y,q,{enumerable:!0,configurable:!0,writable:!0,value:a}):y[q]=a;var T=(y,q,a)=>ip(y,typeof q!="symbol"?q+"":q,a);function Li(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 Bo=[10,400],Fo="RANGE_SELECTOR_COMPONENT_KEY",xi="EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY";var he=(n=>(n.MAIN="__SpreadsheetRender__",n.ROW="__SpreadsheetRowHeader__",n.COLUMN="__SpreadsheetColumnHeader__",n.LEFT_TOP="__SpreadsheetLeftTopPlaceholder__",n))(he||{});const ys=0,Ui=1,st=10,od=11,Rs=12,Fn={id:"sheet.command.set-row-header-width",type:a.CommandType.COMMAND,handler:async(n,o)=>{if(!o)return!1;const e=n.get(R.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:i,size:r}=o,s=e.getRenderById(t);if(s){e.getRenderById(t).with(y.SheetSkeletonManagerService).setRowHeaderSize(s,i,r);const{components:c}=s,d=c.get(he.ROW);d&&d.setCustomHeader({headerStyle:{size:r}},i)}return!0}},zn={id:"sheet.command.set-col-header-height",type:a.CommandType.COMMAND,handler:async(n,o)=>{if(!o)return!1;const e=n.get(R.IRenderManagerService);if(!e)return!1;const{unitId:t,subUnitId:i,size:r}=o,s=e.getRenderById(t);if(s){e.getRenderById(t).with(y.SheetSkeletonManagerService).setColumnHeaderSize(s,i,r);const{components:c}=s,d=c.get(he.COLUMN);d&&d.setCustomHeader({headerStyle:{size:r}},i)}return!0}},zo=a.createInterceptorKey("rangeMovePermissionCheck"),Xo=a.createInterceptorKey("rangeFillPermissionCheck");var Yo=(n=>(n[n.FORMULA_EDITOR_SHOW=100]="FORMULA_EDITOR_SHOW",n[n.MARK_SELECTION=1e4]="MARK_SELECTION",n))(Yo||{});function at(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 rd{constructor(){T(this,"_startColumn",-1);T(this,"_startRow",-1);T(this,"_endColumn",-1);T(this,"_endRow",-1);T(this,"_startX",0);T(this,"_startY",0);T(this,"_endX",0);T(this,"_endY",0);T(this,"_primary");T(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 Kt(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 ye(n,o){const{range:e,primary:t,style:i}=n,r=Kt(o,e),s=t&&Xn(o,t);return{rangeWithCoord:r,primaryWithCoord:s,style:i}}function Xn(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 Ms="__SpreadsheetHelperSelectionTempRect",sd=35;class Ps{constructor(o,e){T(this,"_startOffsetX",0);T(this,"_startOffsetY",0);T(this,"_relativeSelectionPositionRow",0);T(this,"_relativeSelectionPositionColumn",0);T(this,"_relativeSelectionRowLength",0);T(this,"_relativeSelectionColumnLength",0);T(this,"_scenePointerMoveSub");T(this,"_scenePointerUpSub");T(this,"_disabled",!1);T(this,"_helperSelection");T(this,"_scrollTimer");T(this,"_activeViewport");T(this,"_targetSelection",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});T(this,"_isInMergeState",!1);T(this,"_fillControlColors",[]);T(this,"_skeleton");T(this,"_scene");T(this,"_themeService");T(this,"_injector");T(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=R.withCurrentTypeOfRenderer(a.UniverInstanceType.UNIVER_SHEET,y.SheetSkeletonManagerService,this._injector.get(a.IUniverInstanceService),this._injector.get(R.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 R.SHEET_VIEWPORT_KEY.VIEW_MAIN:return o.endRow>=t.startRow&&o.endColumn>=t.startColumn;case R.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP:case R.SHEET_VIEWPORT_KEY.VIEW_COLUMN_RIGHT:return o.endColumn>=t.startColumn&&o.startRow<t.startRow;case R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT:case R.SHEET_VIEWPORT_KEY.VIEW_ROW_BOTTOM:return o.endRow>=t.startRow&&o.startColumn<t.startColumn;case R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP:case R.SHEET_VIEWPORT_KEY.VIEW_COLUMN_LEFT:case R.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP:case R.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(_e,a.Quantity.OPTIONAL))==null?void 0:l.interceptor.fetchThroughInterceptors(zo)(!1,null))!==!1&&r.setCursor(R.CURSOR_TYPE.MOVE)}),r.onPointerLeave$.subscribeEvent(()=>{r.resetCursor()}),r.onPointerDown$.subscribeEvent(this._controlPointerDownHandler.bind(this))})}_controlMoving(o,e){var k;const t=this._scene,i=t.getScrollXYInfoByViewport(R.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),I=ye({range:{startRow:g,endRow:S,startColumn:f,endColumn:v},primary:p,style:null},this._skeleton),E=this._skeleton.getNoMergeCellWithCoordByIndex(g,f),w=this._skeleton.getNoMergeCellWithCoordByIndex(S,v),O=(E==null?void 0:E.startY)||0,M=(w==null?void 0:w.endY)||0,P=(E==null?void 0:E.startX)||0,A=(w==null?void 0:w.endX)||0;(k=this._helperSelection)==null||k.transformByState({left:P,top:O,width:A-P,height:M-O}),this._targetSelection={...I.rangeWithCoord},this._control.selectionMoving$.next(I.rangeWithCoord)}_controlPointerDownHandler(o){const{offsetX:e,offsetY:t}=o,i=this._scene,r=i.getCoordRelativeToViewport(R.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 I=0,E=0;g<f?I-=1:g>p&&(I+=1),S<v?E-=1:S>C&&(E+=1),this._relativeSelectionPositionRow=f-g+I,this._relativeSelectionPositionColumn=v-S+E,this._relativeSelectionRowLength=p-f,this._relativeSelectionColumnLength=C-v;const w=this._control.currentStyle,O=this._getScale();this.isHelperSelection&&(this._helperSelection=new R.Rect(Ms,{stroke:w.stroke,strokeWidth:w.strokeWidth/O}),i.addObject(this._helperSelection));const M=i.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN),P=R.ScrollTimer.create(i);this._scrollTimer=P,P.startScroll(s,l,M),i.disableObjectsEvent(),this._scenePointerMoveSub=i.onPointerMove$.subscribeEvent(A=>{var V;if(this._disabled)return;const{offsetX:k,offsetY:N}=A;if(((V=this._injector.get(_e,a.Quantity.OPTIONAL))==null?void 0:V.interceptor.fetchThroughInterceptors(zo)(!1,null))===!1)return;const{x:W,y:x}=i.getCoordRelativeToViewport(R.Vector2.FromArray([k,N]));this._controlMoving(W,x),i.setCursor(R.CURSOR_TYPE.MOVE),P.scrolling(W,x,()=>{this._controlMoving(W,x)})}),this._scenePointerUpSub=i.onPointerUp$.subscribeEvent(()=>{var k,N,H,W;(k=this._helperSelection)==null||k.dispose();const A=this._scene;A.resetCursor(),this._clearObserverEvent(),A.enableObjectsEvent(),(N=this._scrollTimer)==null||N.dispose(),this._control.selectionMoveEnd$.next(this._targetSelection),(W=(H=this._selectionHooks).selectionMoveEnd)==null||W.call(H)})}_initialWidget(){const{topLeftWidget:o,topCenterWidget:e,topRightWidget:t,middleLeftWidget:i,middleRightWidget:r,bottomLeftWidget:s,bottomCenterWidget:l,bottomRightWidget:c}=this._control,d=[R.CURSOR_TYPE.NORTH_WEST_RESIZE,R.CURSOR_TYPE.NORTH_RESIZE,R.CURSOR_TYPE.NORTH_EAST_RESIZE,R.CURSOR_TYPE.WEST_RESIZE,R.CURSOR_TYPE.EAST_RESIZE,R.CURSOR_TYPE.SOUTH_WEST_RESIZE,R.CURSOR_TYPE.SOUTH_RESIZE,R.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(R.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===R.CURSOR_TYPE.NORTH_WEST_RESIZE?(this._relativeSelectionPositionRow=v,this._relativeSelectionPositionColumn=p):e===R.CURSOR_TYPE.NORTH_RESIZE?this._relativeSelectionPositionRow=v:e===R.CURSOR_TYPE.NORTH_EAST_RESIZE?this._relativeSelectionPositionRow=v:e===R.CURSOR_TYPE.WEST_RESIZE?this._relativeSelectionPositionColumn=p:e===R.CURSOR_TYPE.SOUTH_WEST_RESIZE?this._relativeSelectionPositionColumn=p:e===R.CURSOR_TYPE.SOUTH_RESIZE&&(this._relativeSelectionPositionRow=S);const C=R.ScrollTimer.create(t),I=t.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN);C.startScroll(l,c,I),this._scrollTimer=C,t.disableObjectsEvent(),this._scenePointerMoveSub=t.onPointerMove$.subscribeEvent(E=>{const{offsetX:w,offsetY:O}=E,{x:M,y:P}=t.getCoordRelativeToViewport(R.Vector2.FromArray([w,O]));this._widgetMoving(M,P,e),t.setCursor(e),C.scrolling(M,P,()=>{this._widgetMoving(M,P,e)})}),this._scenePointerUpSub=t.onPointerUp$.subscribeEvent(()=>{var w,O,M;const E=this._scene;E.resetCursor(),this._clearObserverEvent(),E.enableObjectsEvent(),(w=this._scrollTimer)==null||w.dispose(),this._control.selectionScaled$.next(this._targetSelection),(M=(O=this._selectionHooks).selectionMoveEnd)==null||M.call(O)})}_widgetMoving(o,e,t){const i=this._scene,r=i.getScrollXYInfoByViewport(R.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===R.CURSOR_TYPE.NORTH_WEST_RESIZE?(h=d,g=u,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn):t===R.CURSOR_TYPE.NORTH_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=this._relativeSelectionPositionColumn+this._relativeSelectionColumnLength):t===R.CURSOR_TYPE.NORTH_EAST_RESIZE?(h=d,g=this._relativeSelectionPositionColumn,S=this._relativeSelectionPositionRow,f=u):t===R.CURSOR_TYPE.WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength,f=this._relativeSelectionPositionColumn):t===R.CURSOR_TYPE.EAST_RESIZE?S=this._relativeSelectionPositionRow+this._relativeSelectionRowLength:t===R.CURSOR_TYPE.SOUTH_WEST_RESIZE?(h=this._relativeSelectionPositionRow,g=u,S=d,f=this._relativeSelectionPositionColumn):t===R.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),I=ye({range:v,primary:p,style:null},this._skeleton);this._targetSelection={...I.rangeWithCoord},this._control.updateRangeBySelectionWithCoord(I),this._control.selectionScaling$.next(this._targetSelection)}_initialAutoFill(){const{fillControl:o}=this._control;o.onPointerEnter$.subscribeEvent(e=>{this._injector.get(_e).interceptor.fetchThroughInterceptors(Xo)(!1,{x:e.offsetX,y:e.offsetY,skeleton:this._skeleton,scene:this._scene})&&o.setCursor(R.CURSOR_TYPE.CROSSHAIR)}),o.onPointerLeave$.subscribeEvent(()=>{o.resetCursor()}),o.onPointerDown$.subscribeEvent(this._autoFillForPointerdown.bind(this))}_autoFillMoving(o,e){var N,H,W;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(R.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,I=!0;if((d<f||d>p)&&c>=S&&c<=v){const x=this._fillRuler(d,f,p,this._relativeSelectionColumnLength,g);f=x.startRowOrColumn,p=x.endRowOrColumn,C=x.isLighten,I=!1}else if((c<S||c>v)&&d>=f&&d<=p){const x=this._fillRuler(c,S,v,this._relativeSelectionRowLength,h);S=x.startRowOrColumn,v=x.endRowOrColumn,C=x.isLighten}else if(Math.abs(this._startOffsetX-u.x-i.x)/2>Math.abs(this._startOffsetY-u.y-i.y)){const x=this._fillRuler(d,f,p,this._relativeSelectionColumnLength,g);f=x.startRowOrColumn,p=x.endRowOrColumn,C=x.isLighten,I=!1}else{const x=this._fillRuler(c,S,v,this._relativeSelectionRowLength,h);S=x.startRowOrColumn,v=x.endRowOrColumn,C=x.isLighten}const E=this._skeleton.getNoMergeCellWithCoordByIndex(S,f),w=this._skeleton.getNoMergeCellWithCoordByIndex(v,p),O=(E==null?void 0:E.startY)||0,M=(w==null?void 0:w.endY)||0,P=(E==null?void 0:E.startX)||0,A=(w==null?void 0:w.endX)||0;C?this._controlHandler((x,V)=>{const U=new a.ColorKit(this._fillControlColors[V]).lighten(sd).toRgbString();x.setProps({fill:U})}):this._controlHandler((x,V)=>{x.setProps({fill:this._fillControlColors[V]})});const k=m.SELECTION_CONTROL_BORDER_BUFFER_WIDTH/this._getScale();S===v&&I===!0||f===p&&I===!1?(N=this._helperSelection)==null||N.hide():((H=this._helperSelection)==null||H.transformByState({left:P-k/2,top:O-k/2,width:A-P,height:M-O}),(W=this._helperSelection)==null||W.show()),this._targetSelection={startY:O,endY:M,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(R.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=at(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,I=new a.ColorKit(S).darken(2).toRgbString();this.isHelperSelection&&(this._helperSelection=new R.Rect(Ms,{stroke:I,strokeWidth:f+C/2}),i.addObject(this._helperSelection)),this._activeViewport=i.getActiveViewportByCoord(R.Vector2.FromArray([e,t]));const E=i.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN),w=R.ScrollTimer.create(i,this._activeViewport.viewportKey===R.SHEET_VIEWPORT_KEY.VIEW_MAIN?R.ScrollTimerType.ALL:R.ScrollTimerType.NONE);w.startScroll(s,l,E),this._scrollTimer=w,i.disableObjectsEvent(),this._controlHandler(O=>{this._fillControlColors.push(O.fill)}),this._scenePointerMoveSub=i.onPointerMove$.subscribeEvent(O=>{var x;const{offsetX:M,offsetY:P}=O,A=i.getActiveViewportByCoord(R.Vector2.FromArray([M,P]));if(!this._injector.get(_e).interceptor.fetchThroughInterceptors(Xo)(!1,{x:o.offsetX,y:o.offsetY,skeleton:this._skeleton,scene:this._scene}))return;const{x:N,y:H}=i.getCoordRelativeToViewport(R.Vector2.FromArray([M,P]));this._autoFillMoving(N,H),i.setCursor(R.CURSOR_TYPE.CROSSHAIR);const W=this._targetSelection;if(E&&A&&((x=this._activeViewport)==null?void 0:x.viewportKey)!==(A==null?void 0:A.viewportKey)){let V;W.startRow!==c?(w.scrollTimerType=R.ScrollTimerType.Y,V={...W,endRow:W.startRow}):W.endRow!==u?(w.scrollTimerType=R.ScrollTimerType.Y,V={...W,startRow:W.endRow}):W.startColumn!==d?(w.scrollTimerType=R.ScrollTimerType.X,V={...W,endColumn:W.startColumn}):(w.scrollTimerType=R.ScrollTimerType.X,V={...W,startColumn:W.endColumn}),this._isSelectionInViewport(V,A)&&(this._viewportMainScrollBarNeedResetPosition(w.scrollTimerType,this._activeViewport.viewportKey,A.viewportKey)&&E.scrollToBarPos({x:w.scrollTimerType===R.ScrollTimerType.X?0:void 0,y:w.scrollTimerType===R.ScrollTimerType.Y?0:void 0}),this._activeViewport=A)}w.scrolling(N,H,()=>{this._autoFillMoving(N,H)})}),this._scenePointerUpSub=i.onPointerUp$.subscribeEvent(()=>{var M,P;(M=this._helperSelection)==null||M.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,k)=>{A.setProps({fill:this._fillControlColors[k]})}),this._fillControlColors=[]})}_viewportMainScrollBarNeedResetPosition(o,e,t){return o===R.ScrollTimerType.X&&(e===R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT||e===R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP)&&(t===R.SHEET_VIEWPORT_KEY.VIEW_MAIN||t===R.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP)||o===R.ScrollTimerType.Y&&(e===R.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP||e===R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP)&&(t===R.SHEET_VIEWPORT_KEY.VIEW_MAIN||t===R.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 jo=(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))(jo||{});const Os=.3;class Yn extends a.Disposable{constructor(e,t,i,r){var s,l,c,d;super();T(this,"_isHelperSelection",!0);T(this,"_enableAutoFill",!0);T(this,"_showAutoFill",!1);T(this,"_highlightHeader",!0);T(this,"_selectionRenderModel");T(this,"_leftBorder");T(this,"_rightBorder");T(this,"_topBorder");T(this,"_bottomBorder");T(this,"_backgroundControlTop");T(this,"_backgroundControlBottom");T(this,"_backgroundControlMiddleLeft");T(this,"_backgroundControlMiddleRight");T(this,"_autoFillControl");T(this,"_selectionShapeGroup");T(this,"_rowHeaderBackground");T(this,"_rowHeaderBorder");T(this,"_rowHeaderGroup");T(this,"_columnHeaderBackground");T(this,"_columnHeaderBorder");T(this,"_columnHeaderGroup");T(this,"_dashedRect");T(this,"_topLeftWidget");T(this,"_topCenterWidget");T(this,"_topRightWidget");T(this,"_middleLeftWidget");T(this,"_middleRightWidget");T(this,"_bottomLeftWidget");T(this,"_bottomCenterWidget");T(this,"_bottomRightWidget");T(this,"_defaultStyle");T(this,"_currentStyle");T(this,"_rowHeaderWidth",0);T(this,"_columnHeaderHeight",0);T(this,"_widgetRects",[]);T(this,"_controlExtension");T(this,"_dispose$",new D.BehaviorSubject(this));T(this,"dispose$",this._dispose$.asObservable());T(this,"selectionMoving$",new D.Subject);T(this,"selectionMoveEnd$",new D.Subject);T(this,"selectionScaling$",new D.Subject);T(this,"selectionScaled$",new D.Subject);T(this,"selectionFilling$",new D.Subject);T(this,"_selectionFilled$",new D.Subject);T(this,"selectionFilled$",this._selectionFilled$.asObservable());T(this,"_antLineOffset",0);T(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=at(this._themeService),this._currentStyle=at(this._themeService),this._selectionRenderModel=new rd;const e=this._zIndex;this._leftBorder=new R.Rect("__SpreadsheetSelectionShapeLeftControl__"+e,{zIndex:e}),this._rightBorder=new R.Rect("__SpreadsheetSelectionShapeRightControl__"+e,{zIndex:e}),this._topBorder=new R.Rect("__SpreadsheetSelectionTopControl__"+e,{zIndex:e}),this._bottomBorder=new R.Rect("__SpreadsheetSelectionBottomControl__"+e,{zIndex:e}),this._backgroundControlTop=new R.Rect("__SpreadsheetSelectionBackgroundControlTop__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlBottom=new R.Rect("__SpreadsheetSelectionBackgroundControlBottom__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleLeft=new R.Rect("__SpreadsheetSelectionBackgroundControlMiddleLeft__"+e,{zIndex:e-1,evented:!1}),this._backgroundControlMiddleRight=new R.Rect("__SpreadsheetSelectionBackgroundControlMiddleRight__"+e,{zIndex:e-1,evented:!1}),this._autoFillControl=new R.Rect("__SpreadsheetSelectionFillControl__"+e,{zIndex:e+1}),this._autoFillControl.hide(),this._dashedRect=new R.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 R.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,Ui),this.disposeWithMe(a.toDisposable(i.onTransformChange$.subscribeEvent(r=>{r.type===R.TRANSFORM_CHANGE_OBSERVABLE_TYPE.scale&&(this._updateLayoutOfSelectionControl(this._currentStyle),this._updateControlCoord())})))}_initialHeader(){const e=this._zIndex;this._rowHeaderBackground=new R.Rect("__SpreadSheetSelectionRowHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderBorder=new R.Rect("__SpreadSheetSelectionRowHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._rowHeaderGroup=new R.Group("__SpreadSheetSelectionRowHeaderGroup__"+e,this._rowHeaderBackground,this._rowHeaderBorder),this._rowHeaderGroup.hide(),this._rowHeaderGroup.evented=!1,this._rowHeaderGroup.zIndex=e,this._columnHeaderBackground=new R.Rect("__SpreadSheetSelectionColumnHeaderBackground__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderBorder=new R.Rect("__SpreadSheetSelectionColumnHeaderBorder__"+e,{zIndex:e-1,evented:!1}),this._columnHeaderGroup=new R.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],od)}_initialWidget(){const e=this._zIndex;return this._topLeftWidget=new R.Rect("__SpreadSheetSelectionTopLeftWidget__"+e,{zIndex:e+1}),this._topCenterWidget=new R.Rect("__SpreadSheetSelectionTopCenterWidget__"+e,{zIndex:e+1}),this._topRightWidget=new R.Rect("__SpreadSheetSelectionTopRightWidget__"+e,{zIndex:e+1}),this._middleLeftWidget=new R.Rect("__SpreadSheetSelectionMiddleLeftWidget__"+e,{zIndex:e+1}),this._middleRightWidget=new R.Rect("__SpreadSheetSelectionMiddleRightWidget__"+e,{zIndex:e+1}),this._bottomLeftWidget=new R.Rect("__SpreadSheetSelectionBottomLeftWidget__"+e,{zIndex:e+1}),this._bottomCenterWidget=new R.Rect("__SpreadSheetSelectionBottomCenterWidget__"+e,{zIndex:e+1}),this._bottomRightWidget=new R.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 Ps(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=R.FIX_ONE_PIXEL_BLUR_OFFSET/g,{startX:p,startY:C,endX:I,endY:E}=this._selectionRenderModel;if(this.leftControl.transformByState({height:E-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:E-C,left:I-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:I-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:I-p+u,top:E-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 w=t.strokeWidth*2/g;this.dashedRect.transformByState({height:E-C,width:I-p,strokeWidth:w,left:-w/2+v,top:-w/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 w={fill:i,stroke:s,strokeScaleEnabled:!1},O={width:d-h,height:d-h,left:I-p-d/2+h/2-v,top:E-C-d/2+h/2-v,strokeWidth:h};this._autoFillControl.setProps(w),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,I,E,w,O,M,P,A,k,N;(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(),(I=this._topLeftWidget)==null||I.dispose(),(E=this._topCenterWidget)==null||E.dispose(),(w=this._topRightWidget)==null||w.dispose(),(O=this._middleLeftWidget)==null||O.dispose(),(M=this._middleRightWidget)==null||M.dispose(),(P=this._bottomLeftWidget)==null||P.dispose(),(A=this._bottomCenterWidget)==null||A.dispose(),(k=this._bottomRightWidget)==null||k.dispose(),(N=this._controlExtension)==null||N.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,I=this._columnHeaderHeight;if(this._highlightHeader&&I>0){let E=S;l===a.RANGE_TYPE.COLUMN&&(E=new a.ColorKit(u).setAlpha(Os).toString()),this._columnHeaderBackground.setProps({fill:E}),this._columnHeaderBackground.resize(r-t,I),this._columnHeaderBorder.setProps({fill:f}),this._columnHeaderBorder.transformByState({width:r-t,height:p,top:I-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 E=h;l===a.RANGE_TYPE.ROW&&(E=new a.ColorKit(u).setAlpha(Os).toString()),this._rowHeaderBackground.setProps({fill:E}),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 I={left:S-t-v,top:g-i,width:r-S+v*2,height:f-g};I.width<0&&(I.width=0,I.height=0),this._backgroundControlMiddleRight.transformByState(I);const E={left:-v,top:f-i,width:r-t+v*2,height:s-f+v};E.height<0&&(E.width=0,E.height=0),this._backgroundControlBottom.transformByState(E),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,R.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=R.requestNewFrame(()=>{this._startAntLineAnimation()})}}class ad extends R.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 _e=a.createIdentifier("univer.sheet.selection-render-service");class Zo extends a.Disposable{constructor(e,t,i,r,s){super();T(this,"_downObserver");T(this,"_scenePointerMoveSub");T(this,"_scenePointerUpSub");T(this,"_controlFillConfig$",new D.BehaviorSubject(null));T(this,"controlFillConfig$",this._controlFillConfig$.asObservable());T(this,"_selectionControls",[]);T(this,"_startRangeWhenPointerDown",{startY:0,endY:0,startX:0,endX:0,startRow:-1,endRow:-1,startColumn:-1,endColumn:-1});T(this,"_activeControlIndex",-1);T(this,"_startViewportPosX",0);T(this,"_startViewportPosY",0);T(this,"_scrollTimer");T(this,"_cancelDownSubscription");T(this,"_cancelUpSubscription");T(this,"_skeleton");T(this,"_scene");T(this,"_highlightHeader",!0);T(this,"_rangeType",a.RANGE_TYPE.NORMAL);T(this,"_selectionStyle");T(this,"_remainLastEnabled",!1);T(this,"_skipLastEnabled",!1);T(this,"_singleSelectionEnabled",!1);T(this,"_selectionMoveEnd$",new D.BehaviorSubject([]));T(this,"selectionMoveEnd$",this._selectionMoveEnd$.asObservable());T(this,"_selectionMoving$",new D.Subject);T(this,"selectionMoving$",this._selectionMoving$.asObservable());T(this,"_selectionMoveStart$",new D.Subject);T(this,"selectionMoveStart$",this._selectionMoveStart$.asObservable());T(this,"_selectionMoving",!1);T(this,"_selectionTheme");T(this,"_activeViewport");T(this,"interceptor",new a.InterceptorManager({RANGE_MOVE_PERMISSION_CHECK:zo,RANGE_FILL_PERMISSION_CHECK:Xo}));T(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 Yn(e,r,this._selectionTheme,{highlightHeader:this._highlightHeader,rowHeaderWidth:s,columnHeaderHeight:l});this._selectionControls.push(c);const d=ye(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=ye(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(Ui)||t.addLayer(new ad(t,[],Ui))}getSkeleton(){return this._skeleton}getSelectionDataWithStyle(){const e=this._selectionControls,[t,i]=this._skeleton.getLocation();return e.map(r=>{const s=r.getValue();return s.rangeWithCoord.sheetId=i,s.rangeWithCoord.unitId=t,s})}getSelectionControls(){return this._selectionControls}_addSelectionControlByModelData(e){var l;const t=this._skeleton,i=(l=e.style)!=null?l:at(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(R.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e>i.startRow&&t>i.startColumn)return this._scene.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(e<=i.startRow&&t<=i.startColumn)return this._scene.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP);if(e<=i.startRow&&t>i.startColumn)return this._scene.getViewport(R.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP);if(e>i.startRow&&t<=i.startColumn)return this._scene.getViewport(R.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=R.ScrollTimerType.ALL,s,l){var f,v;let c=0,d=0,u=s,h=l;this._scrollTimer=R.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(R.Vector2.FromArray([s,l]));this._scenePointerMoveSub=g.onPointerMove$.subscribeEvent(p=>{var W,x,V,U,B,Y,F,z,j;const{offsetX:C,offsetY:I}=p,{x:E,y:w}=g.getCoordRelativeToViewport(R.Vector2.FromArray([C,I]));this._movingHandler(E,w,t,i);let O=E,M=w;const P=this.getActiveSelectionControl(),A=this._getFreeze(),k=P==null?void 0:P.model,N=(W=g.getActiveViewportByCoord(R.Vector2.FromArray([C,I])))!=null?W:this._getViewportByCell(k==null?void 0:k.endRow,k==null?void 0:k.endColumn);if((()=>{if(!S||!N||!e)return!1;const K=[R.SHEET_VIEWPORT_KEY.VIEW_MAIN,R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP,R.SHEET_VIEWPORT_KEY.VIEW_MAIN_TOP,R.SHEET_VIEWPORT_KEY.VIEW_MAIN_LEFT];return K.includes(S.viewportKey)&&K.includes(N.viewportKey)})()){if(!S||!N||!e)return!1;const K=u<e.left&&E>e.left||u>e.left&&E<e.left,ie=h<e.top&&w>e.top||h>e.top&&w<e.top;K&&(c+=1),ie&&(d+=1);const $=S.viewportKey,se=N.viewportKey;if($===R.SHEET_VIEWPORT_KEY.VIEW_ROW_TOP)I<e.top&&((x=k==null?void 0:k.endRow)!=null?x:0)<((V=A==null?void 0:A.startRow)!=null?V:0)?M=e.top:ie&&d%2===1&&e.scrollToViewportPos({viewportScrollY