index.js 105 KB

123456789101112131415161718192021222324252627282930313233
  1. (function(W,r){typeof exports=="object"&&typeof module<"u"?r(exports,require("@univerjs/core"),require("@univerjs/sheets-ui"),require("@univerjs/engine-formula"),require("rxjs"),require("@univerjs/docs-ui"),require("@univerjs/engine-render"),require("@univerjs/sheets"),require("@univerjs/sheets-formula"),require("@univerjs/ui"),require("react"),require("@univerjs/design"),require("@univerjs/docs"),require("rxjs/operators")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@univerjs/sheets-ui","@univerjs/engine-formula","rxjs","@univerjs/docs-ui","@univerjs/engine-render","@univerjs/sheets","@univerjs/sheets-formula","@univerjs/ui","react","@univerjs/design","@univerjs/docs","rxjs/operators"],r):(W=typeof globalThis<"u"?globalThis:W||self,r(W.UniverSheetsFormulaUi={},W.UniverCore,W.UniverSheetsUi,W.UniverEngineFormula,W.rxjs,W.UniverDocsUi,W.UniverEngineRender,W.UniverSheets,W.UniverSheetsFormula,W.UniverUi,W.React,W.UniverDesign,W.UniverDocs,W.rxjs.operators))})(this,function(W,r,w,N,G,te,X,K,Ne,E,f,pe,ut,Be){"use strict";var Ss=Object.defineProperty;var gs=(W,r,w)=>r in W?Ss(W,r,{enumerable:!0,configurable:!0,writable:!0,value:w}):W[r]=w;var ee=(W,r,w)=>gs(W,typeof r!="symbol"?r+"":r,w);const Je={id:"sheet.command.paste-formula",type:r.CommandType.COMMAND,handler:async e=>e.get(r.ICommandService).executeCommand(w.SheetPasteCommand.id,{value:w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA})},Pe={id:"formula-ui.operation.select-editor-formula",type:r.CommandType.OPERATION,handler:(e,t)=>!0};var Cn=Object.defineProperty,In=Object.getOwnPropertyDescriptor,Rn=(e,t,n,s)=>{for(var o=s>1?void 0:s?In(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&Cn(t,n,o),o},En=(e,t)=>(n,s)=>t(n,s,e);const xt="FORMULA_PROMPT_ACTIVATED",Ve=r.createIdentifier("formula-ui.prompt-service");let ft=class{constructor(e){ee(this,"_search$",new G.Subject);ee(this,"_help$",new G.Subject);ee(this,"_navigate$",new G.Subject);ee(this,"_accept$",new G.Subject);ee(this,"_acceptFormulaName$",new G.Subject);ee(this,"search$",this._search$.asObservable());ee(this,"help$",this._help$.asObservable());ee(this,"navigate$",this._navigate$.asObservable());ee(this,"accept$",this._accept$.asObservable());ee(this,"acceptFormulaName$",this._acceptFormulaName$.asObservable());ee(this,"_searching",!1);ee(this,"_helping",!1);ee(this,"_sequenceNodes",[]);ee(this,"_isLockedOnSelectionChangeRefString",!1);ee(this,"_isLockedOnSelectionInsertRefString",!1);this._contextService=e}dispose(){this._search$.complete(),this._help$.complete(),this._navigate$.complete(),this._accept$.complete(),this._acceptFormulaName$.complete(),this._sequenceNodes=[]}search(e){this._contextService.setContextValue(xt,e.visible),this._searching=e.visible,this._search$.next(e)}isSearching(){return this._searching}help(e){this._helping=e.visible,this._help$.next(e)}isHelping(){return this._helping}navigate(e){this._navigate$.next(e)}accept(e){this._accept$.next(e)}acceptFormulaName(e){this._acceptFormulaName$.next(e)}getSequenceNodes(){return[...this._sequenceNodes]}setSequenceNodes(e){this._sequenceNodes=e}clearSequenceNodes(){this._sequenceNodes=[]}getCurrentSequenceNode(e){return this._sequenceNodes[this.getCurrentSequenceNodeIndex(e)]}getCurrentSequenceNodeByIndex(e){return this._sequenceNodes[e]}getCurrentSequenceNodeIndex(e){let t=0;const n=this._sequenceNodes[0];for(let s=0,o=this._sequenceNodes.length;s<o;s++){const c=this._sequenceNodes[s];if(typeof c=="string")t++;else{const{endIndex:i}=c;t=i}if(e<=t)return typeof n=="string"&&e!==0?s+1:s}return this._sequenceNodes.length}updateSequenceRef(e,t){const n=this._sequenceNodes[e];if(typeof n=="string"||n.nodeType!==N.sequenceNodeType.REFERENCE)return;const s=t.length-n.token.length,o={...n};o.token=t,o.endIndex+=s,this._sequenceNodes[e]=o;for(let c=e+1,i=this._sequenceNodes.length;c<i;c++){const a=this._sequenceNodes[c];if(typeof a=="string")continue;const l={...a};l.startIndex+=s,l.endIndex+=s,this._sequenceNodes[c]=l}}insertSequenceRef(e,t){const n=t.length,s=this.getCurrentSequenceNodeIndex(e);this._sequenceNodes.splice(s,0,{token:t,startIndex:e,endIndex:e+n-1,nodeType:N.sequenceNodeType.REFERENCE});for(let o=s+1,c=this._sequenceNodes.length;o<c;o++){const i=this._sequenceNodes[o];if(typeof i=="string")continue;const a={...i};a.startIndex+=n,a.endIndex+=n,this._sequenceNodes[o]=a}}insertSequenceString(e,t){const n=this.getCurrentSequenceNodeIndex(e),s=t.split("");this._sequenceNodes.splice(n,0,...s);const o=s.length;for(let c=n+o,i=this._sequenceNodes.length;c<i;c++){const a=this._sequenceNodes[c];if(typeof a=="string")continue;const l={...a};l.startIndex+=o,l.endIndex+=o,this._sequenceNodes[c]=l}}enableLockedSelectionChange(){this._isLockedOnSelectionChangeRefString=!0}disableLockedSelectionChange(){this._isLockedOnSelectionChangeRefString=!1}isLockedSelectionChange(){return this._isLockedOnSelectionChangeRefString}enableLockedSelectionInsert(){this._isLockedOnSelectionInsertRefString=!0}disableLockedSelectionInsert(){this._isLockedOnSelectionInsertRefString=!1}isLockedSelectionInsert(){return this._isLockedOnSelectionInsertRefString}};ft=Rn([En(0,r.IContextService)],ft);const Ft={id:"formula-ui.operation.help-function",type:r.CommandType.OPERATION,handler:async(e,t)=>(e.get(Ve).help(t),!0)},Qe={id:"formula-ui.operation.insert-function",type:r.CommandType.OPERATION,handler:async(e,t)=>{var C,_;const n=e.get(K.SheetsSelectionsService),s=e.get(te.IEditorService),o=n.getCurrentSelections();if(!o||!o.length)return!1;const c=K.getSheetCommandTarget(e.get(r.IUniverInstanceService));if(!c)return!1;const{worksheet:i,unitId:a,subUnitId:l}=c,m=i.getCellMatrix(),{value:d}=t,p=e.get(r.ICommandService),h=e.get(w.IEditorBridgeService),g=[];let u=null,S=0,I=0,b="";if(o.length===1&&(Tn(o[0].range)||bn(o[0].range))){const{range:T,primary:x}=o[0],F=(C=x==null?void 0:x.actualRow)!=null?C:T.startRow,P=(_=x==null?void 0:x.actualColumn)!=null?_:T.startColumn;u=T,S=F,I=P;const L=Pt(m,F,P);L&&(b=N.serializeRange(L))}else o.some(T=>{var k,y;const{range:x,primary:F}=T,P=(k=F==null?void 0:F.actualRow)!=null?k:x.startRow,L=(y=F==null?void 0:F.actualColumn)!=null?y:x.startColumn,j=Pt(m,P,L);if(!j)return u=x,S=P,I=L,!0;const O=N.serializeRange(j),M=`=${d}(${O})`;return g.push({range:x,primary:{row:P,column:L},formula:M}),!1});if(u){const T=K.getCellAtRowCol(S,I,i),x={range:r.Rectangle.clone(u),primary:{startRow:T.startRow,startColumn:T.startColumn,endRow:T.endRow,endColumn:T.endColumn,actualRow:S,actualColumn:I,isMerged:T.isMerged,isMergedMainCell:T.startRow===S&&T.startColumn===I}},F={unitId:a,subUnitId:l,selections:[x]};await p.executeCommand(K.SetSelectionsOperation.id,F);const P=s.getEditor(r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),L=s.getEditor(r.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);h.changeVisible({visible:!0,unitId:a,eventType:X.DeviceInputEventType.Dblclick});const j=`=${d}(${b}`;P==null||P.replaceText(j),L==null||L.replaceText(j,!1)}return g.length===0?!1:p.executeCommand(Ne.InsertFunctionCommand.id,{list:g})}};function Pt(e,t,n){const s=_n(e,t,n);if(s!==t)return{startRow:s,endRow:t-1,startColumn:n,endColumn:n};const o=yn(e,t,n);return o!==n?{startRow:t,endRow:t,startColumn:o,endColumn:n-1}:null}function _n(e,t,n){let s=!1;if(t===0)return t;for(let o=t-1;o>=0;o--){const c=e.getValue(o,n);if(qe(c)&&!s){if(o===0)return 0;s=!0}else{if(s&&!qe(c))return o+1;if(s&&o===0)return 0}}return t}function yn(e,t,n){let s=!1;if(n===0)return n;for(let o=n-1;o>=0;o--){const c=e.getValue(t,o);if(qe(c)&&!s){if(o===0)return 0;s=!0}else{if(s&&!qe(c))return o+1;if(s&&o===0)return 0}}return n}function qe(e){if(e!=null&&e.p){const t=e==null?void 0:e.p.body;if(t==null)return!1;const n=t.dataStream,o=n.substring(n.length-2,n.length)===r.DEFAULT_EMPTY_DOCUMENT_VALUE?n.substring(0,n.length-2):n;return r.isRealNum(o)}return e&&(e.t===r.CellValueType.NUMBER||r.getCellValueType(e)===r.CellValueType.NUMBER)}function Tn(e){return e.startRow===e.endRow&&e.startColumn===e.endColumn}function bn(e){return e.startRow!==e.endRow&&e.startColumn!==e.endColumn}const At="SHEET_FORMULA_UI_PLUGIN",Lt=`${At}_MORE_FUNCTIONS_COMPONENT`,$e={id:"formula-ui.operation.more-functions",type:r.CommandType.OPERATION,handler:async e=>(e.get(E.ISidebarService).open({header:{title:"formula.insert.tooltip"},children:{label:Lt}}),!0)},dt={id:"formula-ui.operation.change-ref-to-absolute",type:r.CommandType.OPERATION,handler:async e=>!0},wt={id:"formula-ui.operation.search-function",type:r.CommandType.OPERATION,handler:async(e,t)=>(e.get(Ve).search(t),!0)};var mt={exports:{}},Ke={};/**
  2. * @license React
  3. * react-jsx-runtime.production.min.js
  4. *
  5. * Copyright (c) Facebook, Inc. and its affiliates.
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE file in the root directory of this source tree.
  9. */var kt;function Nn(){if(kt)return Ke;kt=1;var e=f,t=Symbol.for("react.element"),n=Symbol.for("react.fragment"),s=Object.prototype.hasOwnProperty,o=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,c={key:!0,ref:!0,__self:!0,__source:!0};function i(a,l,m){var d,p={},h=null,g=null;m!==void 0&&(h=""+m),l.key!==void 0&&(h=""+l.key),l.ref!==void 0&&(g=l.ref);for(d in l)s.call(l,d)&&!c.hasOwnProperty(d)&&(p[d]=l[d]);if(a&&a.defaultProps)for(d in l=a.defaultProps,l)p[d]===void 0&&(p[d]=l[d]);return{$$typeof:t,type:a,key:h,ref:g,props:p,_owner:o.current}}return Ke.Fragment=n,Ke.jsx=i,Ke.jsxs=i,Ke}var jt;function Mn(){return jt||(jt=1,mt.exports=Nn()),mt.exports}var v=Mn(),Se=function(){return Se=Object.assign||function(e){for(var t,n=1,s=arguments.length;n<s;n++){t=arguments[n];for(var o in t)Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o])}return e},Se.apply(this,arguments)},On=function(e,t){var n={};for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(n[s]=e[s]);if(e!=null&&typeof Object.getOwnPropertySymbols=="function")for(var o=0,s=Object.getOwnPropertySymbols(e);o<s.length;o++)t.indexOf(s[o])<0&&Object.prototype.propertyIsEnumerable.call(e,s[o])&&(n[s[o]]=e[s[o]]);return n},Me=f.forwardRef(function(e,t){var n=e.icon,s=e.id,o=e.className,c=e.extend,i=On(e,["icon","id","className","extend"]),a="univerjs-icon univerjs-icon-".concat(s," ").concat(o||"").trim(),l=f.useRef("_".concat(Fn()));return Ht(n,"".concat(s),{defIds:n.defIds,idSuffix:l.current},Se({ref:t,className:a},i),c)});function Ht(e,t,n,s,o){return f.createElement(e.tag,Se(Se({key:t},Dn(e,n,o)),s),(xn(e,n).children||[]).map(function(c,i){return Ht(c,"".concat(t,"-").concat(e.tag,"-").concat(i),n,void 0,o)}))}function Dn(e,t,n){var s=Se({},e.attrs);n!=null&&n.colorChannel1&&s.fill==="colorChannel1"&&(s.fill=n.colorChannel1);var o=t.defIds;return!o||o.length===0||(e.tag==="use"&&s["xlink:href"]&&(s["xlink:href"]=s["xlink:href"]+t.idSuffix),Object.entries(s).forEach(function(c){var i=c[0],a=c[1];typeof a=="string"&&(s[i]=a.replace(/url\(#(.*)\)/,"url(#$1".concat(t.idSuffix,")")))})),s}function xn(e,t){var n,s=t.defIds;return!s||s.length===0?e:e.tag==="defs"&&(!((n=e.children)===null||n===void 0)&&n.length)?Se(Se({},e),{children:e.children.map(function(o){return typeof o.attrs.id=="string"&&s&&s.indexOf(o.attrs.id)>-1?Se(Se({},o),{attrs:Se(Se({},o.attrs),{id:o.attrs.id+t.idSuffix})}):o})}):e}function Fn(){return Math.random().toString(36).substring(2,8)}Me.displayName="UniverIcon";var Pn={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M14.1544 3.75557C14.3887 3.98988 14.3887 4.36978 14.1544 4.6041L6.51409 12.2444C6.40157 12.3569 6.24896 12.4201 6.08983 12.4201C5.9307 12.4201 5.77808 12.3569 5.66556 12.2444L1.84541 8.42425C1.6111 8.18993 1.6111 7.81003 1.84541 7.57572C2.07973 7.34141 2.45963 7.34141 2.69394 7.57572L6.08983 10.9716L13.3059 3.75557C13.5402 3.52126 13.9201 3.52126 14.1544 3.75557Z",fillRule:"evenodd",clipRule:"evenodd"}}]},Wt=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"check-mark-single",ref:t,icon:Pn}))});Wt.displayName="CheckMarkSingle";var An={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M3.71274 2.86421C3.47843 2.6299 3.09853 2.6299 2.86421 2.86421C2.6299 3.09853 2.6299 3.47843 2.86421 3.71274L7.15154 8.00007L2.86421 12.2874C2.6299 12.5217 2.6299 12.9016 2.86421 13.1359C3.09853 13.3702 3.47843 13.3702 3.71274 13.1359L8.00007 8.84859L12.2874 13.1359C12.5217 13.3702 12.9016 13.3702 13.1359 13.1359C13.3702 12.9016 13.3702 12.5217 13.1359 12.2874L8.84859 8.00007L13.1359 3.71274C13.3702 3.47843 13.3702 3.09853 13.1359 2.86421C12.9016 2.6299 12.5217 2.6299 12.2874 2.86421L8.00007 7.15154L3.71274 2.86421Z"}}]},et=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"close-single",ref:t,icon:An}))});et.displayName="CloseSingle";var Ln={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M5.3313 1.4667C5.3313 1.13533 5.59993.866699 5.9313.866699H10.069C10.4004.866699 10.669 1.13533 10.669 1.4667 10.669 1.79807 10.4004 2.0667 10.069 2.0667H5.9313C5.59993 2.0667 5.3313 1.79807 5.3313 1.4667zM1.09985 3.64443C1.09985 3.31306 1.36848 3.04443 1.69985 3.04443H14.2999C14.6312 3.04443 14.8999 3.31306 14.8999 3.64443 14.8999 3.9758 14.6312 4.24443 14.2999 4.24443H1.69985C1.36848 4.24443 1.09985 3.9758 1.09985 3.64443zM6.12398 8.30171C6.35829 8.0674 6.73819 8.0674 6.97251 8.30171L8.00007 9.32928 9.02764 8.30171C9.26195 8.0674 9.64185 8.0674 9.87617 8.30171 10.1105 8.53603 10.1105 8.91593 9.87617 9.15024L8.8486 10.1778 9.87617 11.2054C10.1105 11.4397 10.1105 11.8196 9.87617 12.0539 9.64185 12.2882 9.26195 12.2882 9.02764 12.0539L8.00007 11.0263 6.97251 12.0539C6.73819 12.2882 6.35829 12.2882 6.12398 12.0539 5.88966 11.8196 5.88966 11.4397 6.12398 11.2054L7.15154 10.1778 6.12398 9.15024C5.88966 8.91593 5.88966 8.53603 6.12398 8.30171z"}},{tag:"path",attrs:{fill:"currentColor",d:"M4.75332 5.22217C3.86966 5.22217 3.15332 5.93851 3.15332 6.82217V12.5331C3.15332 13.9691 4.31738 15.1332 5.75332 15.1332H10.2465C11.6825 15.1332 12.8465 13.9691 12.8465 12.5331V6.82217C12.8465 5.93851 12.1302 5.22217 11.2465 5.22217H4.75332ZM4.35332 6.82217C4.35332 6.60125 4.53241 6.42217 4.75332 6.42217H11.2465C11.4674 6.42217 11.6465 6.60125 11.6465 6.82217V12.5331C11.6465 13.3063 11.0197 13.9332 10.2465 13.9332H5.75332C4.98012 13.9332 4.35332 13.3063 4.35332 12.5331V6.82217Z",fillRule:"evenodd",clipRule:"evenodd"}}]},Bt=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"delete-single",ref:t,icon:Ln}))});Bt.displayName="DeleteSingle";var wn={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M8.75 5.32495C8.75 5.73916 8.41421 6.07495 8 6.07495 7.58579 6.07495 7.25 5.73916 7.25 5.32495 7.25 4.91074 7.58579 4.57495 8 4.57495 8.41421 4.57495 8.75 4.91074 8.75 5.32495zM8.5999 7.52505C8.5999 7.19368 8.33127 6.92505 7.9999 6.92505 7.66853 6.92505 7.3999 7.19368 7.3999 7.52505V11.425C7.3999 11.7564 7.66853 12.025 7.9999 12.025 8.33127 12.025 8.5999 11.7564 8.5999 11.425V7.52505z"}},{tag:"path",attrs:{fill:"currentColor",d:"M0.899902 8.00002C0.899902 4.0788 4.07868 0.900024 7.9999 0.900024C11.9211 0.900024 15.0999 4.0788 15.0999 8.00002C15.0999 11.9212 11.9211 15.1 7.9999 15.1C4.07868 15.1 0.899902 11.9212 0.899902 8.00002ZM7.9999 2.10002C4.74142 2.10002 2.0999 4.74154 2.0999 8.00002C2.0999 11.2585 4.74142 13.9 7.9999 13.9C11.2584 13.9 13.8999 11.2585 13.8999 8.00002C13.8999 4.74154 11.2584 2.10002 7.9999 2.10002Z",fillRule:"evenodd",clipRule:"evenodd"}}]},Vt=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"details-single",ref:t,icon:wn}))});Vt.displayName="DetailsSingle";var kn={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M8.6 1.99991C8.60001 1.66854 8.33138 1.39991 8.00001 1.3999C7.66864 1.3999 7.40001 1.66853 7.4 1.9999L7.39996 7.3999H1.9999C1.66853 7.3999 1.3999 7.66853 1.3999 7.9999C1.3999 8.33127 1.66853 8.5999 1.9999 8.5999H7.39995L7.3999 13.9999C7.3999 14.3313 7.66853 14.5999 7.9999 14.5999C8.33127 14.5999 8.5999 14.3313 8.5999 13.9999L8.59995 8.5999H13.9999C14.3313 8.5999 14.5999 8.33127 14.5999 7.9999C14.5999 7.66853 14.3313 7.3999 13.9999 7.3999H8.59996L8.6 1.99991Z"}}]},Kt=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"increase-single",ref:t,icon:kn}))});Kt.displayName="IncreaseSingle";var jn={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M5.90913 3.57564C6.14345 3.34132 6.52335 3.34132 6.75766 3.57564L10.7577 7.57564C10.992 7.80995 10.992 8.18985 10.7577 8.42417L6.75766 12.4242C6.52335 12.6585 6.14345 12.6585 5.90913 12.4242C5.67482 12.1899 5.67482 11.81 5.90913 11.5756L9.48487 7.9999L5.90913 4.42417C5.67482 4.18985 5.67482 3.80995 5.90913 3.57564Z",fillRule:"evenodd",clipRule:"evenodd"}}]},ht=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"more-single",ref:t,icon:jn}))});ht.displayName="MoreSingle";var Hn={tag:"svg",attrs:{fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M12.6185 12.4423C12.5907 12.2749 12.7773 12.15 12.9343 12.2308L15.4242 13.5126C15.6102 13.6084 15.5544 13.8745 15.3439 13.8955L14.2456 14.184L13.4521 15.1286C13.3495 15.2939 13.085 15.2463 13.0534 15.0568L12.6185 12.4423Z"}},{tag:"path",attrs:{fill:"currentColor",d:"M1 3.6C1 2.16406 2.16406 1 3.6 1H12.3C13.7359 1 14.9 2.16406 14.9 3.6V5.81156C14.9003 5.81881 14.9004 5.82609 14.9004 5.8334C14.9004 5.84071 14.9003 5.84799 14.9 5.85524V10.045C14.9003 10.0522 14.9004 10.0595 14.9004 10.0668C14.9004 10.3982 14.6318 10.6668 14.3004 10.6668H11.1668C10.8907 10.6668 10.6668 10.8907 10.6668 11.1668V14.3C10.6668 14.6314 10.3982 14.9 10.0668 14.9L10.05 14.8998L3.6 14.9C2.16406 14.9 1 13.7359 1 12.3V3.6ZM13.2 5.2334C13.4761 5.2334 13.7 5.00954 13.7 4.7334V3.6C13.7 2.8268 13.0732 2.2 12.3 2.2H11.1668C10.8907 2.2 10.6668 2.42386 10.6668 2.7V4.7334C10.6668 5.00954 10.8907 5.2334 11.1668 5.2334H13.2ZM10.6668 6.9334C10.6668 6.65726 10.8907 6.4334 11.1668 6.4334H13.2C13.4761 6.4334 13.7 6.65726 13.7 6.9334V8.9668C13.7 9.24294 13.4761 9.4668 13.2 9.4668H11.1668C10.8907 9.4668 10.6668 9.24294 10.6668 8.9668V6.9334ZM8.9668 5.2334C9.24294 5.2334 9.4668 5.00954 9.4668 4.7334V2.7C9.4668 2.42386 9.24294 2.2 8.9668 2.2H6.9334C6.65726 2.2 6.4334 2.42386 6.4334 2.7V4.7334C6.4334 5.00954 6.65726 5.2334 6.9334 5.2334L8.9668 5.2334ZM6.4334 6.9334C6.4334 6.65726 6.65726 6.4334 6.9334 6.4334L8.9668 6.4334C9.24294 6.4334 9.4668 6.65726 9.4668 6.9334V8.9668C9.4668 9.24294 9.24294 9.4668 8.9668 9.4668L6.9334 9.4668C6.65726 9.4668 6.4334 9.24294 6.4334 8.9668V6.9334ZM4.7334 5.2334C5.00954 5.2334 5.2334 5.00954 5.2334 4.7334V2.7C5.2334 2.42386 5.00954 2.2 4.7334 2.2H3.6C2.8268 2.2 2.2 2.8268 2.2 3.6V4.7334C2.2 5.00954 2.42386 5.2334 2.7 5.2334H4.7334ZM2.2 6.9334C2.2 6.65726 2.42386 6.4334 2.7 6.4334H4.7334C5.00954 6.4334 5.2334 6.65725 5.2334 6.9334V8.9668C5.2334 9.24294 5.00954 9.4668 4.7334 9.4668H2.7C2.42386 9.4668 2.2 9.24294 2.2 8.9668V6.9334ZM5.2334 11.1668C5.2334 10.8907 5.00954 10.6668 4.7334 10.6668H2.7C2.42386 10.6668 2.2 10.8907 2.2 11.1668V12.3C2.2 13.0732 2.8268 13.7 3.6 13.7H4.7334C5.00954 13.7 5.2334 13.4761 5.2334 13.2V11.1668ZM9.4668 11.1668C9.4668 10.8907 9.24294 10.6668 8.9668 10.6668H6.9334C6.65726 10.6668 6.4334 10.8907 6.4334 11.1668V13.2C6.4334 13.4761 6.65726 13.7 6.9334 13.7H8.9668C9.24294 13.7 9.4668 13.4761 9.4668 13.2V11.1668Z",fillRule:"evenodd",clipRule:"evenodd"}}]},Ut=f.forwardRef(function(e,t){return f.createElement(Me,Object.assign({},e,{id:"select-range-single",ref:t,icon:Hn}))});Ut.displayName="SelectRangeSingle";function Yt(e){var t,n,s="";if(typeof e=="string"||typeof e=="number")s+=e;else if(typeof e=="object")if(Array.isArray(e)){var o=e.length;for(t=0;t<o;t++)e[t]&&(n=Yt(e[t]))&&(s&&(s+=" "),s+=n)}else for(n in e)e[n]&&(s&&(s+=" "),s+=n);return s}function pt(){for(var e,t,n=0,s="",o=arguments.length;n<o;n++)(e=arguments[n])&&(t=Yt(e))&&(s&&(s+=" "),s+=t);return s}var Wn=Object.defineProperty,Bn=Object.getOwnPropertyDescriptor,Vn=(e,t,n,s)=>{for(var o=s>1?void 0:s?Bn(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&Wn(t,n,o),o},we=(e,t)=>(n,s)=>t(n,s,e);W.RefSelectionsRenderService=class extends w.BaseSelectionRenderService{constructor(n,s,o,c,i,a,l){super(s,o,c,i,a);ee(this,"_workbookSelections");ee(this,"_eventDisposables");this._context=n,this._contextService=a,this._refSelectionsService=l,this._workbookSelections=this._refSelectionsService.getWorkbookSelections(this._context.unitId),this._initSelectionChangeListener(),this._initSkeletonChangeListener(),this._initUserActionSyncListener(),this._setSelectionStyle(Kn(this._themeService)),this._remainLastEnabled=!0,this._highlightHeader=!1}getLocation(){return this._skeleton.getLocation()}setRemainLastEnabled(n){this._remainLastEnabled=n}setSkipLastEnabled(n){this._skipLastEnabled=n}clearLastSelection(){const n=this._selectionControls[this._selectionControls.length-1];n&&(n.dispose(),this._selectionControls.pop())}enableSelectionChanging(){return this._disableSelectionChanging(),this._eventDisposables=this._initCanvasEventListeners(),r.toDisposable(()=>this._disableSelectionChanging())}_disableSelectionChanging(){var n;(n=this._eventDisposables)==null||n.dispose(),this._eventDisposables=null}disableSelectionChanging(){this._disableSelectionChanging()}_initCanvasEventListeners(){const n=this._getSheetObject(),{spreadsheetRowHeader:s,spreadsheetColumnHeader:o,spreadsheet:c,spreadsheetLeftTopPlaceholder:i}=n,{scene:a}=this._context,l=new r.DisposableCollection;return l.add(c==null?void 0:c.onPointerDown$.subscribeEvent((m,d)=>{this.inRefSelectionMode()&&(this._onPointerDown(m,c.zIndex+1,r.RANGE_TYPE.NORMAL,this._getActiveViewport(m)),m.button!==2&&d.stopPropagation())})),l.add(s==null?void 0:s.onPointerDown$.subscribeEvent((m,d)=>{if(!this.inRefSelectionMode())return;const p=this._sheetSkeletonManagerService.getCurrent().skeleton,{row:h}=w.getCoordByOffset(m.offsetX,m.offsetY,a,p);w.checkInHeaderRanges(this._workbookSelections.getCurrentSelections(),h,r.RANGE_TYPE.ROW)||(this._onPointerDown(m,(c.zIndex||1)+1,r.RANGE_TYPE.ROW,this._getActiveViewport(m),X.ScrollTimerType.Y),m.button!==2&&d.stopPropagation())})),l.add(o==null?void 0:o.onPointerDown$.subscribeEvent((m,d)=>{if(!this.inRefSelectionMode())return;const p=this._sheetSkeletonManagerService.getCurrent().skeleton,{column:h}=w.getCoordByOffset(m.offsetX,m.offsetY,a,p);w.checkInHeaderRanges(this._workbookSelections.getCurrentSelections(),h,r.RANGE_TYPE.COLUMN)||(this._onPointerDown(m,(c.zIndex||1)+1,r.RANGE_TYPE.COLUMN,this._getActiveViewport(m),X.ScrollTimerType.X),m.button!==2&&d.stopPropagation())})),l.add(i==null?void 0:i.onPointerDown$.subscribeEvent((m,d)=>{if(this._reset(),!this.inRefSelectionMode())return;const p=this._sheetSkeletonManagerService.getCurrent().skeleton,h=w.getAllSelection(p);this._addSelectionControlByModelData(h),this._selectionMoveStart$.next(this.getSelectionDataWithStyle());const g=a.onPointerUp$.subscribeEvent(()=>{g.unsubscribe(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())});m.button!==2&&d.stopPropagation()})),l}_addSelectionControlByModelData(n){var a;const s=this._skeleton,o=(a=n.style)!=null?a:w.genNormalSelectionStyle(this._themeService),c=this._scene;return n.style=o,this.newSelectionControl(c,s,n)}_initSelectionChangeListener(){this.disposeWithMe(this._refSelectionsService.selectionSet$.subscribe(n=>{this._reset(),this._skeleton&&this.resetSelectionsByModelData(n||[])}))}_initUserActionSyncListener(){this.disposeWithMe(this.selectionMoveStart$.subscribe(n=>{this._updateSelections(n,K.SelectionMoveType.MOVE_START)})),this.disposeWithMe(this.selectionMoving$.subscribe(n=>{this._updateSelections(n,K.SelectionMoveType.MOVING)})),this.disposeWithMe(this.selectionMoveEnd$.subscribe(n=>{this._updateSelections(n,K.SelectionMoveType.MOVE_END)}))}_updateSelections(n,s){const c=this._context.unit.getActiveSheet().getSheetId();n.length!==0&&this._workbookSelections.setSelections(c,n.map(i=>K.convertSelectionDataToRange(i)),s)}_initSkeletonChangeListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(n=>{if(!n)return;const{skeleton:s}=n,{scene:o}=this._context,c=o.getViewport(X.SHEET_VIEWPORT_KEY.VIEW_MAIN);this._skeleton&&this._skeleton.worksheet.getSheetId()!==s.worksheet.getSheetId()&&this._reset(),this._changeRuntime(s,o,c);const i=this._workbookSelections.getCurrentSelections();this.resetSelectionsByModelData(i)}))}_getActiveViewport(n){const s=this._getSheetObject();return s==null?void 0:s.scene.getActiveViewportByCoord(X.Vector2.FromArray([n.offsetX,n.offsetY]))}_getSheetObject(){return w.getSheetObject(this._context.unit,this._context)}_onPointerDown(n,s=0,o=r.RANGE_TYPE.NORMAL,c,i=X.ScrollTimerType.ALL){var M;this._rangeType=o;const a=this._skeleton,l=this._scene;if(!l||!a)return;c&&(this._activeViewport=c);const{offsetX:m,offsetY:d}=n,p=l.getViewport(X.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(!p)return;const h=l.getCoordRelativeToViewport(X.Vector2.FromArray([m,d])),{x:g,y:u}=h;this._startViewportPosX=g,this._startViewportPosY=u;const S=l.getScrollXYInfoByViewport(h),{scaleX:I,scaleY:b}=l.getAncestorScale(),C=this._skeleton.getCellByOffset(g,u,I,b,S);if(!C)return;switch(o){case r.RANGE_TYPE.NORMAL:break;case r.RANGE_TYPE.ROW:C.startColumn=0,C.endColumn=this._skeleton.getColumnCount()-1;break;case r.RANGE_TYPE.COLUMN:C.startRow=0,C.endRow=this._skeleton.getRowCount()-1;break;case r.RANGE_TYPE.ALL:C.startRow=0,C.startColumn=0,C.endRow=this._skeleton.getRowCount()-1,C.endColumn=this._skeleton.getColumnCount()-1}const _={range:C,primary:C,style:null};_.range.rangeType=o;const T=w.attachSelectionWithCoord(_,this._skeleton);this._startRangeWhenPointerDown={...T.rangeWithCoord};const x={...T.rangeWithCoord,rangeType:o};let F=this.getActiveSelectionControl();const P=this.getSelectionControls();for(const k of P){if(n.button===2&&k.model.isInclude(x)){F=k;return}if(k.model.isEqual(x)){F=k;break}}this._checkClearPreviousControls(n);const L=F==null?void 0:F.model.currentCell,j=n.shiftKey&&L,O=this._remainLastEnabled&&!n.ctrlKey&&!n.shiftKey&&!this._skipLastEnabled&&!this._singleSelectionEnabled;j&&L?this._makeSelectionByTwoCells(L,x,a,o,F):O&&F?F.updateRangeBySelectionWithCoord(T):F=this.newSelectionControl(l,a,_);for(let k=0;k<this.getSelectionControls().length-1;k++)this.getSelectionControls()[k].clearHighlight();this._selectionMoveStart$.next(this.getSelectionDataWithStyle()),l.disableObjectsEvent(),this._clearUpdatingListeners(),this._addEndingListeners(),(M=l.getTransformer())==null||M.clearSelectedObjects(),this._setupPointerMoveListener(p,F,o,i,g,u),this._escapeShortcutDisposable=this._shortcutService.forceEscape(),this._scenePointerUpSub=l.onPointerUp$.subscribeEvent(()=>{var k;this._clearUpdatingListeners(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle()),(k=this._escapeShortcutDisposable)==null||k.dispose(),this._escapeShortcutDisposable=null})}newSelectionControl(n,s,o){const c=this.getSelectionControls().length,{rowHeaderWidth:i,columnHeaderHeight:a}=s,l=new w.SelectionControl(n,c,this._themeService,{highlightHeader:this._highlightHeader,enableAutoFill:!1,rowHeaderWidth:i,columnHeaderHeight:a}),m=w.attachSelectionWithCoord(o,s);return l.updateRangeBySelectionWithCoord(m),this._selectionControls.push(l),l.setControlExtension({skeleton:s,scene:n,themeService:this._themeService,injector:this._injector,selectionHooks:{selectionMoveEnd:()=>{this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}}}),l}},W.RefSelectionsRenderService=Vn([we(1,r.Inject(r.Injector)),we(2,r.Inject(r.ThemeService)),we(3,E.IShortcutService),we(4,r.Inject(w.SheetSkeletonManagerService)),we(5,r.IContextService),we(6,K.IRefSelectionsService)],W.RefSelectionsRenderService);function Kn(e){const t=w.genNormalSelectionStyle(e);return t.widgets={tl:!0,tc:!0,tr:!0,ml:!0,mr:!0,bl:!0,bc:!0,br:!0},t}const Un=(e,t,n)=>{const o=r.useDependency(X.IRenderManagerService).getRenderById(e),c=o==null?void 0:o.with(W.RefSelectionsRenderService);f.useEffect(()=>{if(n&&c){const i=n.input$.subscribe(a=>{a.content===N.matchToken.COMMA?c.setSkipLastEnabled(!0):c.setSkipLastEnabled(!1)});return()=>{i.unsubscribe()}}},[n,c]),f.useEffect(()=>{c&&(t.endsWith(N.matchToken.COMMA)||c.setSkipLastEnabled(!1))},[t,c])},tt=()=>{const e=r.useDependency(N.LexerTreeBuilder);return f.useCallback(n=>e.sequenceNodesBuilder(n)||[],[e])},Yn=(e,t,n,s,o,c)=>{const i=tt(),a=f.useRef(!0);f.useEffect(()=>{var l,m;if(c){if(a.current){const d=i(e);if(d.length){const p=s(c,d);n&&o(p)}else{const p=c.getDocumentData(),g={dataStream:(m=(l=p.body)==null?void 0:l.dataStream)!=null?m:`${t}\r
  10. `,...p.body};c.setDocumentData({...p,body:g})}a.current=!1}else if(n){const d=i(e),p=s(c,d);o(p)}}},[c,n])},zt=e=>E.useEvent(n=>{var s,o;if(e){e.focus();const c=[...e.getSelectionRanges()];if(r.Tools.isDefine(n))e.setSelectionRanges([{startOffset:n,endOffset:n}]);else if(!c.length&&!e.docSelectionRenderService.isOnPointerEvent){const i=(o=(s=e.getDocumentData().body)==null?void 0:s.dataStream)!=null?o:`\r
  11. `,a=Math.max(i.length-2,0);e.setSelectionRanges([{startOffset:a,endOffset:a}])}}});function zn(e,t,n){const s=e.getCurrentTheme(),o=new r.ColorKit(t).setAlpha(.05).toRgbString();return{id:n,strokeWidth:1,stroke:t,fill:o,widgets:{tl:!0,tc:!0,tr:!0,ml:!0,mr:!0,bl:!0,bc:!0,br:!0},widgetSize:6,widgetStrokeWidth:1,widgetStroke:s.colorWhite}}function Gt(e){var b,C,_;const{unitId:t,subUnitId:n,refSelections:s,editor:o,refSelectionsService:c,refSelectionsRenderService:i,sheetSkeletonManagerService:a,themeService:l,univerInstanceService:m}=e,d=m.getUnit(t,r.UniverInstanceType.UNIVER_SHEET),p=d==null?void 0:d.getActiveSheet(),h=[];if(!d||!p){c.setSelections(h);return}const g=p.getSheetId(),u=T=>{var x;return(x=d==null?void 0:d.getSheetBySheetName(T))==null?void 0:x.getSheetId()};if(!((b=a==null?void 0:a.getWorksheetSkeleton(g))==null?void 0:b.skeleton))return;const I=[];for(let T=0,x=s.length;T<x;T++){const F=s[T],{themeColor:P,token:L,refIndex:j,endIndex:O}=F,M=N.deserializeRangeWithSheet(L),{unitId:k,sheetName:y,range:R}=M;if(k&&t!==k)continue;const D=u(y);if(D&&D!==g||!D&&g!==n)continue;const A=K.setEndForRange(R,p.getRowCount(),p.getColumnCount());h.push({range:A,primary:null,style:zn(l,P,j.toString())}),I.push(O)}if(o){const T=(_=(C=o.getSelectionRanges())==null?void 0:C[0])==null?void 0:_.startOffset,x=I.findIndex(F=>F+2===T);x!==-1?i==null||i.setActiveSelectionIndex(x):i==null||i.resetActiveSelectionIndex()}return h}function St(e,t){const n=r.useDependency(r.IUniverInstanceService),s=r.useDependency(r.ThemeService),o=r.useDependency(K.IRefSelectionsService),i=r.useDependency(X.IRenderManagerService).getRenderById(e),a=i==null?void 0:i.with(W.RefSelectionsRenderService),l=i==null?void 0:i.with(w.SheetSkeletonManagerService),m=f.useCallback((d,p)=>{const h=Gt({unitId:e,subUnitId:t,refSelections:d,editor:p,refSelectionsService:o,refSelectionsRenderService:a,sheetSkeletonManagerService:l,themeService:s,univerInstanceService:n});if(!h)return;((a==null?void 0:a.getSelectionControls())||[]).length===h.length?a==null||a.resetSelectionsByModelData(h):o.setSelections(h)},[a,o,l,s,e,t,n]);return f.useEffect(()=>()=>{a==null||a.resetActiveSelectionIndex()},[a]),m}function Zt(e=""){const t=r.useDependency(Ne.IDescriptionService),n=Xt(),s=r.useDependency(r.ICommandService),o=f.useMemo(()=>e.length,[e]);return f.useCallback((i,a,l=!0,m)=>{const d=i.getDocumentData(),p=i.getEditorId();if(!d)return[];const h=d.body;if(!h)return[];const g=h.dataStream.slice(0,h.dataStream.length-2),u={dataStream:"",...d.body};if(!g.startsWith(e))return[];if(a==null||a.length===0)return u.textRuns=[],s.syncExecuteCommand(te.ReplaceTextRunsCommand.id,{unitId:p,body:r.getBodySlice(u,0,u.dataStream.length-2)}),[];{const{textRuns:S,refSelections:I}=Jt(t,n,a);o&&S.forEach(_=>{_.ed=_.ed+o,_.st=_.st+o}),u.textRuns=[{st:0,ed:1,ts:{fs:11}},...S];const b=a.reduce((_,T)=>typeof T=="string"?`${_}${T}`:`${_}${T.token}`,"");u.dataStream=`${e}${b}\r
  12. `;let C;if(l){C=i.getSelectionRanges();const _=u.dataStream.length-2+o;C.forEach(T=>{T.startOffset=Math.max(0,Math.min(T.startOffset,_)),T.endOffset=Math.max(0,Math.min(T.endOffset,_))})}return s.syncExecuteCommand(te.ReplaceTextRunsCommand.id,{unitId:p,body:r.getBodySlice(u,0,u.dataStream.length-2),textRanges:m!=null?m:C}),I}},[s,t,n,o,e])}function Xt(){const t=r.useDependency(r.ThemeService).getCurrentTheme();return f.useMemo(()=>{const s=[t.loopColor1,t.loopColor2,t.loopColor3,t.loopColor4,t.loopColor5,t.loopColor6,t.loopColor7,t.loopColor8,t.loopColor9,t.loopColor10,t.loopColor11,t.loopColor12],o=t.hyacinth700,c=t.verdancy800,i=t.colorBlack;return{formulaRefColors:s,numberColor:o,stringColor:c,plainTextColor:i}},[t])}function Jt(e,t,n){const{formulaRefColors:s,numberColor:o,stringColor:c,plainTextColor:i}=t,a=[],l=[],m=new Map;let d=0;for(let p=0,h=n.length;p<h;p++){const g=n[p];if(typeof g=="string"){const _=a[a.length-1],T=_?_.ed:0,x=T+g.length;a.push({st:T,ed:x,ts:{cl:{rgb:i},fs:11}});continue}if(e.hasDefinedNameDescription(g.token.trim())){a.push({st:g.startIndex,ed:g.endIndex+1,ts:{cl:{rgb:i},fs:11}});continue}const{startIndex:u,endIndex:S,nodeType:I,token:b}=g;let C="";if(I===N.sequenceNodeType.REFERENCE){if(m.has(b))C=m.get(b);else{const _=d%s.length;C=s[_],m.set(b,C),d++}l.push({refIndex:p,themeColor:C,token:b,startIndex:g.startIndex,endIndex:g.endIndex,index:l.length})}else I===N.sequenceNodeType.NUMBER?C=o:(I===N.sequenceNodeType.STRING||I===N.sequenceNodeType.ARRAY)&&(C=c);C&&C.length>0?a.push({st:u,ed:S+1,ts:{cl:{rgb:C},fs:11}}):a.push({st:u,ed:S+1,ts:{cl:{rgb:i},fs:11}})}return{textRuns:a,refSelections:l}}function Gn(e){var o,c;const n=e.get(r.IUniverInstanceService).getCurrentUniverDocInstance();return n!=null&&n.getBody()?{dataStream:(c=(o=n.getBody())==null?void 0:o.dataStream)!=null?c:"",offset:0}:void 0}var nt=(e=>(e[e.NOT_SELECT=0]="NOT_SELECT",e[e.NEED_ADD=1]="NEED_ADD",e[e.CAN_EDIT=2]="CAN_EDIT",e))(nt||{});function Zn(e,t,n){var g;const o=r.useDependency(X.IRenderManagerService).getRenderById(e),c=o==null?void 0:o.with(te.DocSelectionRenderService),i=r.useDependency(ut.DocSelectionManagerService),a=r.useDependency(r.Injector),[l,m]=f.useState(0),d=r.useDependency(N.LexerTreeBuilder),p=f.useRef(!0),h=f.useRef(l);return h.current=l,f.useEffect(()=>{const u=i.textSelection$.pipe(G.filter(S=>S.unitId===e),G.map(()=>{const S=c==null?void 0:c.getActiveTextRange();return S!=null&&S.collapsed?S.startOffset:-1})).subscribe(S=>{var L,j;const I=Gn(a);if(!I)return;const b=(L=I==null?void 0:I.dataStream)==null?void 0:L.slice(0,-2),C=(j=d.sequenceNodesBuilder(b))!=null?j:[],_=b[S-1],T=b[S],x=C.findIndex(O=>typeof O=="object"&&O.nodeType===N.sequenceNodeType.REFERENCE&&S===O.endIndex+2),F=_&&N.matchRefDrawToken(_)&&(!T||N.isFormulaLexerToken(T)&&T!==N.matchToken.OPEN_BRACKET),P=x>-1;if((b==null?void 0:b.substring(0,1))==="="&&(F||P))if(P){if(p.current)return;m(2)}else p.current=!1,m(1);else m(0)});return()=>u.unsubscribe()},[i.textSelection$,c,e,a,d]),f.useEffect(()=>{t||(m(0),p.current=!0)},[t]),f.useEffect(()=>{var S;if(!n)return;const u=(S=o==null?void 0:o.mainComponent)==null?void 0:S.onPointerDown$.subscribeEvent(()=>{m(0),p.current=!0});return()=>u==null?void 0:u.unsubscribe()},[n,(g=o==null?void 0:o.mainComponent)==null?void 0:g.onPointerDown$]),{isSelecting:l}}const Qt=(e,t,n,s)=>{const o=r.useDependency(r.ICommandService),c=r.useDependency(E.IShortcutService),i=f.useRef(t);i.current=t;const a=f.useRef(s);a.current=s,f.useEffect(()=>{if(!n||!e)return;const m=`sheet.formula-embedding-editor.${n.getEditorId()}`,d=new r.DisposableCollection,p=(u,S)=>{if(a.current){a.current(u,S);return}let I=r.Direction.LEFT;u===E.KeyCode.ARROW_DOWN?I=r.Direction.DOWN:u===E.KeyCode.ARROW_UP?I=r.Direction.UP:u===E.KeyCode.ARROW_RIGHT&&(I=r.Direction.RIGHT),S===E.MetaKeys.SHIFT?o.executeCommand(te.MoveSelectionOperation.id,{direction:I}):o.executeCommand(te.MoveCursorOperation.id,{direction:I})},h=(u,S)=>{let I=r.Direction.DOWN;u===E.KeyCode.ARROW_DOWN?I=r.Direction.DOWN:u===E.KeyCode.ARROW_UP?I=r.Direction.UP:u===E.KeyCode.ARROW_LEFT?I=r.Direction.LEFT:u===E.KeyCode.ARROW_RIGHT&&(I=r.Direction.RIGHT),i.current?S===E.MetaKeys.CTRL_COMMAND?o.executeCommand(w.MoveSelectionCommand.id,{direction:I,jumpOver:w.JumpOver.moveGap,extra:"formula-editor",fromCurrentSelection:i.current===nt.NEED_ADD}):S===E.MetaKeys.SHIFT?o.executeCommand(w.ExpandSelectionCommand.id,{direction:I,extra:"formula-editor"}):S===(E.MetaKeys.CTRL_COMMAND|E.MetaKeys.SHIFT)?o.executeCommand(w.ExpandSelectionCommand.id,{direction:I,jumpOver:w.JumpOver.moveGap,extra:"formula-editor"}):o.executeCommand(w.MoveSelectionCommand.id,{direction:I,extra:"formula-editor",fromCurrentSelection:i.current===nt.NEED_ADD}):p(u,S)};return d.add(o.registerCommand({id:m,type:r.CommandType.OPERATION,handler(u,S){const{keyCode:I,metaKey:b}=S;h(I,b)}})),[{keyCode:E.KeyCode.ARROW_DOWN},{keyCode:E.KeyCode.ARROW_LEFT},{keyCode:E.KeyCode.ARROW_RIGHT},{keyCode:E.KeyCode.ARROW_UP},{keyCode:E.KeyCode.ARROW_DOWN,metaKey:E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_LEFT,metaKey:E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_RIGHT,metaKey:E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_UP,metaKey:E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_DOWN,metaKey:E.MetaKeys.CTRL_COMMAND},{keyCode:E.KeyCode.ARROW_LEFT,metaKey:E.MetaKeys.CTRL_COMMAND},{keyCode:E.KeyCode.ARROW_RIGHT,metaKey:E.MetaKeys.CTRL_COMMAND},{keyCode:E.KeyCode.ARROW_UP,metaKey:E.MetaKeys.CTRL_COMMAND},{keyCode:E.KeyCode.ARROW_DOWN,metaKey:E.MetaKeys.CTRL_COMMAND|E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_LEFT,metaKey:E.MetaKeys.CTRL_COMMAND|E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_RIGHT,metaKey:E.MetaKeys.CTRL_COMMAND|E.MetaKeys.SHIFT},{keyCode:E.KeyCode.ARROW_UP,metaKey:E.MetaKeys.CTRL_COMMAND|E.MetaKeys.SHIFT}].map(({keyCode:u,metaKey:S})=>({id:m,binding:S?u|S:u,preconditions:()=>!0,priority:900,staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keyCode:u,metaKey:S}})).forEach(u=>{d.add(c.registerShortcut(u))}),()=>{d.dispose()}},[o,n,e,c])},qt=(e,t)=>{const s=r.useDependency(X.IRenderManagerService).getRenderById(e),o=s==null?void 0:s.with(W.RefSelectionsRenderService);f.useEffect(()=>{o&&(t?o==null||o.setRemainLastEnabled(!1):o==null||o.setRemainLastEnabled(!0))},[t,o])},gt=(e,t,n,s=!0)=>{const o=r.useDependency(X.IRenderManagerService),c=r.useDependency(r.IContextService),i=r.useDependency(E.IContextMenuService),a=r.useDependency(K.IRefSelectionsService),l=o.getRenderById(n),m=l==null?void 0:l.with(W.RefSelectionsRenderService);f.useLayoutEffect(()=>{if(e)return c.setContextValue(r.EDITOR_ACTIVATED,!0),()=>{c.setContextValue(r.EDITOR_ACTIVATED,!1),a.clear()}},[c,e,a]),f.useLayoutEffect(()=>{if(e&&t){const d=m==null?void 0:m.enableSelectionChanging();return c.setContextValue(K.REF_SELECTIONS_ENABLED,!0),()=>{c.setContextValue(K.REF_SELECTIONS_ENABLED,!1),d==null||d.dispose()}}},[c,e,m,t]),f.useEffect(()=>{if(e)return c.setContextValue(r.EDITOR_ACTIVATED,!0),s&&i.disable(),()=>{c.setContextValue(r.EDITOR_ACTIVATED,!1),s&&i.enable()}},[i,c,e,s]),f.useEffect(()=>{e&&(m==null||m.setSkipLastEnabled(!1))},[e,m])},Xn=()=>{f.useEffect(()=>()=>{},[])},$t=(e,t,n)=>{const s=r.useDependency(r.IUniverInstanceService),o=r.useDependency(K.SheetsSelectionsService);return f.useCallback(()=>{if(e){const i=[...o.getWorkbookSelections(t).getSelectionsOfWorksheet(n)],a=s.getCurrentUnitForType(r.UniverInstanceType.UNIVER_SHEET),l=a==null?void 0:a.getActiveSheet();l&&l.getSheetId()===n&&o.setSelections(i)}},[e,o,n,t,s])},vt=e=>typeof e=="string"?e===N.matchToken.COMMA:!1,Jn=e=>{if(typeof e=="object")return e.nodeType===N.sequenceNodeType.REFERENCE},Qn=e=>e.filter(t=>vt(t)||Jn(t)),Ct=e=>{if(e.endColumn<e.startColumn){const t=e.endColumn;e.endColumn=e.startColumn,e.startColumn=t}if(e.endRow<e.startRow){const t=e.endRow;e.endRow=e.startRow,e.startRow=t}return e},_e=e=>e.map(t=>typeof t=="string"?t:t.token).join("");function en(e,t,n){var s,o;return((o=(s=e.getUnit(t))==null?void 0:s.getSheetBySheetId(n))==null?void 0:o.getName())||""}const Ie=(e,t=!1,n="")=>t?e.map(s=>s.sheetName!==""&&s.sheetName!==n?N.serializeRangeWithSheet(s.sheetName,s.range):N.serializeRange(s.range)):e.map(s=>N.serializeRange(s.range)),tn=(e,t,n,s,o,c,i)=>{const a=r.useDependency(X.IRenderManagerService),l=r.useDependency(r.IUniverInstanceService),m=f.useRef(!1),d=a.getRenderById(t),p=d==null?void 0:d.with(W.RefSelectionsRenderService),h=f.useRef([]),g=f.useMemo(()=>{const S=Qn(s),I=h.current;return S.length===I.length?(I.splice(0),I.push(...S),I):S},[s]);h.current=g;const u=f.useRef();f.useEffect(()=>{if(e&&p){let S=!0;const I=(_,T)=>{var y;if(S||m.current){S=!1;return}const x=[..._],F=l.getUnit(t),P=((y=F==null?void 0:F.getActiveSheet())==null?void 0:y.getName())||"",L=g.map((R,D)=>{if(typeof R=="string"){if(!g[D-1])return null;const H=g[D+1];return vt(R)&&(vt(H)||D===g.length-1)?null:R}else if(R.nodeType===N.sequenceNodeType.REFERENCE){const A=N.deserializeRangeWithSheet(R.token);A.unitId=A.unitId===""?t:A.unitId,A.sheetName=A.sheetName===""?P:A.sheetName;const{unitId:H,sheetName:Z}=A;if(c&&(H!==t||P!==Z))return null;if(H===t&&P===Z){const J=x.shift();if(J&&en(l,t,J.rangeWithCoord.sheetId||"")===Z){const z={...R};return Ct(J.rangeWithCoord),o?z.token=N.serializeRangeWithSheet(P,J.rangeWithCoord):z.token=N.serializeRange(J.rangeWithCoord),z}}return R}return null}).filter(R=>!!R),j=Ie(x.map(R=>{var D,A,H;return{range:R.rangeWithCoord,unitId:(D=R.rangeWithCoord.unitId)!=null?D:"",sheetName:en(l,(A=R.rangeWithCoord.unitId)!=null?A:"",(H=R.rangeWithCoord.sheetId)!=null?H:"")}}),o).join(N.matchToken.COMMA),O=_e(L),M=`${O}${O&&j?N.matchToken.COMMA:""}${j}`,k=m.current;i(M,k?-1:M.length,T)},b=p.selectionMoveEnd$.subscribe(_=>{if(I(_,!0),m.current=!1,u.current){const{result:T,offset:x}=u.current;i(T,x,!0),u.current=void 0}}),C=p.selectionMoving$.pipe(Be.throttleTime(50)).subscribe(_=>{I(_,!1)});return()=>{b.unsubscribe(),C.unsubscribe()}}},[e,g,p,o,c,i,l,t]),f.useEffect(()=>{if(e&&p){const S=new r.DisposableCollection,I=(_,T)=>{var k;let x=0,F=0,P=!1;const L=l.getUnit(t),j=((k=L==null?void 0:L.getActiveSheet())==null?void 0:k.getName())||"",O=g.map(y=>{if(typeof y=="string")return P||(F+=y.length),y;if(y.nodeType===N.sequenceNodeType.REFERENCE){const R=N.deserializeRangeWithSheet(_);if(R.unitId=R.unitId===""?t:R.unitId,R.sheetName=R.sheetName===""?j:R.sheetName,x===T){P=!0;const D={...y,token:_};return o?D.token=N.serializeRangeWithSheet(R.sheetName,R.range):D.token=N.serializeRange(R.range),x++,F+=D.token.length,D}return x++,P||(F+=y.token.length),y}return y}),M=_e(O);u.current={result:M,offset:F},i(M,-1,!1)};let b=0;const C=p.selectionMoveEnd$.subscribe(()=>{b=setTimeout(()=>{S.dispose(),p.getSelectionControls().forEach((T,x)=>{S.add(G.merge(T.selectionMoving$,T.selectionScaling$).pipe(G.map(F=>N.serializeRange(F)),G.distinctUntilChanged()).subscribe(F=>{m.current=!0,I(F,x)}))})},30)});return()=>{C.unsubscribe(),S.dispose(),clearTimeout(b)}}},[e,p,g,i,l,t,o])},It=(e,t,n,s,o,c)=>{const i=r.useDependency(r.ICommandService),a=r.useDependency(te.IEditorService),m=r.useDependency(X.IRenderManagerService).getRenderById(t),d=m==null?void 0:m.with(W.RefSelectionsRenderService);f.useEffect(()=>{if(e&&d)if(n){const p=i.onCommandExecuted(h=>{if(h.id===K.SetWorksheetActiveOperation.id){const g=d.getSelectionControls().length;for(let u=1;u<=g;u++)d.clearLastSelection();setTimeout(()=>{c()},30)}});return()=>{p.dispose()}}else{const p=i.beforeCommandExecuted(h=>{if(h.id===K.SetWorksheetActiveOperation.id){s(!1),o(),c();const g=a.getEditor(r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);g==null||g.focus()}});return()=>{p.dispose()}}},[e,d])},nn=e=>!e.some(n=>{if(typeof n=="string"){if(n!==N.matchToken.COMMA)return!0}else if(n.nodeType!==N.sequenceNodeType.REFERENCE)return!0;return!1}),qn=(e,t,n)=>{const s=f.useRef(!0);f.useEffect(()=>{if(e){const o=setTimeout(()=>{s.current=!1},500);return()=>{clearTimeout(o)}}},[e]),f.useEffect(()=>{if(!s.current&&t){const o=nn(n);t(o,_e(n))}},[n,t])},Re={sheetRangeSelectorTextWrap:"univer-sheet-range-selector-text-wrap",sheetRangeSelectorPlaceholder:"univer-sheet-range-selector-placeholder",sheetRangeSelectorError:"univer-sheet-range-selector-error",sheetRangeSelectorErrorWrap:"univer-sheet-range-selector-error-wrap",sheetRangeSelectorText:"univer-sheet-range-selector-text",sheetRangeSelectorActive:"univer-sheet-range-selector-active",sheetRangeSelectorIcon:"univer-sheet-range-selector-icon",sheetRangeSelectorDialogItem:"univer-sheet-range-selector-dialog-item",sheetRangeSelectorDialogItemDelete:"univer-sheet-range-selector-dialog-item-delete"},Ue=()=>{};function on(e){const{initValue:t,unitId:n,subUnitId:s,errorText:o,placeholder:c,actions:i,onChange:a=Ue,onVerify:l=Ue,onRangeSelectorDialogVisibleChange:m=Ue,onBlur:d=Ue,onFocus:p=Ue,isFocus:h=!0,isOnlyOneRange:g=!1,isSupportAcrossSheet:u=!1}=e,S=E.useEvent(a),I=r.useDependency(te.IEditorService),b=r.useDependency(r.LocaleService),C=r.useDependency(r.ICommandService),_=r.useDependency(N.LexerTreeBuilder),T=f.useRef(null),[x,F]=f.useState(!1),[P,L]=f.useState(h),j=f.useMemo(()=>r.createInternalEditorID(`${w.RANGE_SELECTOR_SYMBOLS}-${r.generateRandomId(4)}`),[]),O=f.useRef(),M=O.current,k=f.useRef(null),y=r.useDependency(r.IUniverInstanceService),R=f.useMemo(()=>!x&&P,[x,P]),[D,A]=f.useState(()=>typeof t=="string"?t:Ie(t,u).join(N.matchToken.COMMA)),H=f.useMemo(()=>y.getCurrentTypeOfUnit$(r.UniverInstanceType.UNIVER_DOC),[y]),Z=r.useObservable(H),J=(Z==null?void 0:Z.getUnitId())===j,z=f.useRef([]),Y=E.useEvent((V,B)=>{T.current&&!x&&!T.current.contains(V.target)&&B()});i&&(i.handleOutClick=Y);const oe=f.useMemo(()=>D.split(N.matchToken.COMMA).filter(V=>!!V).map(V=>N.deserializeRangeWithSheet(V)),[D]),ae=f.useMemo(()=>o!==void 0,[o]),ne=$t(!x&&P,n,s);f.useMemo(()=>V=>{const B=_.sequenceNodesBuilder(V);if(B){if(nn(B)){const q=B.map(ie=>{if(typeof ie=="string")return ie;if(ie.nodeType===N.sequenceNodeType.REFERENCE){const je={...ie},Te=N.deserializeRangeWithSheet(ie.token);return Te.range=Ct(Te.range),u||(Te.sheetName="",Te.unitId=""),je.token=Ie([Te],u)[0],je}return ie}),$=_e(q);S($)}}else A("")},[u]);const U=zt(M),{checkScrollBar:ge}=te.useResize(M,!0,!0),me=tt(),de=f.useMemo(()=>me(D),[D]),se=Zt(),Ee=St(n,s),le=E.useEvent((V,B=!0,Q=!0)=>{if(!O.current)return;const q=me(V),$=se(O.current,q,B);z.current=$,Q&&Ee($,O.current)});f.useEffect(()=>{const V=C.onCommandExecuted(B=>{var Q,q;if(B.id===ut.RichTextEditingMutation.id){const $=B.params,{unitId:ie}=$;ie===j&&S(r.BuildTextUtils.transform.getPlainText((q=(Q=M==null?void 0:M.getDocumentData().body)==null?void 0:Q.dataStream)!=null?q:""))}});return()=>V.dispose()},[C,M,j,S]);const ve=E.useEvent((V,B,Q)=>{le(V),A(V),Q&&(U(),B!==-1&&setTimeout(()=>{const q={startOffset:B,endOffset:B};M==null||M.setSelectionRanges([q]);const $=M==null?void 0:M.render.with(te.DocBackScrollRenderController);$==null||$.scrollToRange({...q,collapsed:!0})},50),ge())});tn(R,n,s,de,u,g,ve),gt(R,R&&J,n),qt(n,g),Un(n,D,M),qn(R,l,de),Qt(R,0,M),Xn(),It(R,n,u,L,d,()=>{R&&le(D)}),f.useEffect(()=>{if(M){const V=M.input$.pipe(G.throttleTime(100)).subscribe(B=>{var q,$;const Q=(($=(q=B.data.body)==null?void 0:q.dataStream)!=null?$:"").replaceAll(/\n|\r/g,"").replaceAll(/,{2,}/g,",").replaceAll(/(^,)/g,"");le(Q,!1),A(Q)});return()=>{V.unsubscribe()}}},[M]),f.useEffect(()=>{const V=C.onCommandExecuted(B=>{B.id===w.SetCellEditVisibleOperation.id&&(F(!1),m(!1),L(!1),d())});return()=>{V.dispose()}},[u]),f.useLayoutEffect(()=>{let V;if(k.current){V=I.register({autofocus:!0,editorUnitId:j,initialSnapshot:{id:j,body:{dataStream:`${D}\r
  13. `,textRuns:[]},documentStyle:{}}},k.current);const B=I.getEditor(j);O.current=B,le(D,!1,!1)}return()=>{V==null||V.dispose()}},[]),f.useLayoutEffect(()=>{h?(L(h),U()):(M==null||M.blur(),ne(),L(h))},[h,U]),Yn(D,"",P,se,Ee,M);const he=()=>{p(),U(),L(!0)},Ce=V=>{const B=Ie(V,u).join(N.matchToken.COMMA);B||M==null||M.setDocumentData({...M.getDocumentData(),body:{dataStream:`\r
  14. `,textRuns:[]}}),le(B),A(B),F(!1),m(!1),setTimeout(()=>{L(!0),M==null||M.setSelectionRanges([{startOffset:B.length,endOffset:B.length}]),U(),ge()},30)},xe=()=>{F(!1),m(!1),setTimeout(()=>{L(!0),U()},30)},Fe=()=>{ae||(U(),setTimeout(()=>{F(!0),m(!0),L(!1)},30))};return v.jsxs("div",{className:Re.sheetRangeSelector,ref:T,children:[v.jsxs("div",{className:pt(Re.sheetRangeSelectorTextWrap,{[Re.sheetRangeSelectorActive]:P&&!ae,[Re.sheetRangeSelectorError]:ae}),children:[v.jsx("div",{className:Re.sheetRangeSelectorText,ref:k,onMouseUp:he}),v.jsx(pe.Tooltip,{title:b.t("rangeSelector.buttonTooltip"),placement:"bottom",children:v.jsx(Ut,{className:Re.sheetRangeSelectorIcon,onClick:Fe})}),o!==void 0?v.jsx("div",{className:Re.sheetRangeSelectorErrorWrap,children:o}):null,c!==void 0&&!D?v.jsx("div",{className:Re.sheetRangeSelectorPlaceholder,children:c}):null]}),x&&v.jsx($n,{editorId:j,handleConfirm:Ce,handleClose:xe,unitId:n,subUnitId:s,initValue:oe,visible:x,isOnlyOneRange:g,isSupportAcrossSheet:u})]})}function $n(e){const{handleConfirm:t,handleClose:n,visible:s,initValue:o,unitId:c,subUnitId:i,isOnlyOneRange:a,isSupportAcrossSheet:l}=e,m=r.useDependency(r.LocaleService),d=r.useDependency(Ne.IDescriptionService),p=r.useDependency(N.LexerTreeBuilder),g=r.useDependency(X.IRenderManagerService).getRenderById(c),u=g==null?void 0:g.with(W.RefSelectionsRenderService),[S,I]=f.useState(()=>{if(a){const R=o[0];return R?Ie([R],l):[""]}return Ie(o,l)}),[b,C]=f.useState(()=>S.length-1),_=Xt(),T=f.useMemo(()=>S.join(N.matchToken.COMMA),[S]),x=tt(),F=f.useMemo(()=>x(T),[T]),P=f.useMemo(()=>Jt(d,_,F).refSelections,[F]),L=()=>{I([]),setTimeout(()=>{n()},30)},j=(R,D)=>{D?u==null||u.setSkipLastEnabled(!1):u==null||u.setSkipLastEnabled(!0),I(A=>{const H=[...A];return H[R]=D,H})},O=R=>{u==null||u.setSkipLastEnabled(!1),I(D=>{if(D.length===1)return D;const A=[];return D.forEach((H,Z)=>{R!==Z&&A.push(H)}),A})},M=()=>{u==null||u.setSkipLastEnabled(!0),I(R=>(R.push(""),C(R.length-1),[...R]))},k=E.useEvent(R=>{var A;u==null||u.setSkipLastEnabled(!1);const D=R.split(N.matchToken.COMMA).filter(H=>!!H);I(a?[(A=D[0])!=null?A:""]:D)}),y=St(c,i);return tn(b>=0,c,i,F,l,a,k),gt(b>=0,b>=0,c),qt(c,a),It(b>=0,c,l,G.noop,G.noop,()=>y(P)),f.useEffect(()=>{y(P)},[P]),f.useEffect(()=>{(S.length===0||S.length===1&&!S[0])&&(u==null||u.setSkipLastEnabled(!0))},[S]),v.jsx(pe.Dialog,{width:"328px",visible:s,title:m.t("rangeSelector.title"),draggable:!0,closeIcon:v.jsx(et,{}),footer:v.jsxs("footer",{children:[v.jsx(pe.Button,{onClick:L,children:m.t("rangeSelector.cancel")}),v.jsx(pe.Button,{style:{marginLeft:10},onClick:()=>t(S.filter(R=>{const D=p.sequenceNodesBuilder(R);return D&&D.length===1&&typeof D[0]!="string"&&D[0].nodeType===N.sequenceNodeType.REFERENCE}).map(R=>N.deserializeRangeWithSheet(R)).map(R=>({...R,range:Ct(R.range)}))),type:"primary",children:m.t("rangeSelector.confirm")})]}),onClose:L,children:v.jsxs("div",{className:Re.sheetRangeSelectorDialog,children:[S.map((R,D)=>v.jsxs("div",{className:Re.sheetRangeSelectorDialogItem,children:[v.jsx(pe.Input,{affixWrapperStyle:{width:"100%"},placeholder:m.t("rangeSelector.placeHolder"),onFocus:()=>C(D),value:R,onChange:A=>j(D,A)},`input_${D}`),S.length>1&&!a&&v.jsx(Bt,{className:Re.sheetRangeSelectorDialogItemDelete,onClick:()=>O(D)})]},`${R}_${D}`)),!a&&v.jsx("div",{children:v.jsxs(pe.Button,{type:"link",size:"small",onClick:M,children:[v.jsx(Kt,{}),v.jsx("span",{children:m.t("rangeSelector.addAnotherRange")})]})})]})})}function eo(e,t){return Object.keys(e).filter(n=>isNaN(Number(n))&&n!=="DefinedName").map(n=>({label:t.t(`formula.functionType.${n.toLocaleLowerCase()}`),value:`${e[n]}`}))}function Rt(e){if(!e.require&&!e.repeat)return`[${e.name}]`;if(e.require&&!e.repeat)return e.name;if(!e.require&&e.repeat)return`[${e.name},...]`;if(e.require&&e.repeat)return`${e.name},...`}const to=(e,t=100)=>{f.useEffect(()=>{let n=null;const s=()=>{n===null&&(n=window.setTimeout(()=>{e(),n=null},t))};return window.addEventListener("scroll",s),window.addEventListener("resize",s),()=>{n!==null&&clearTimeout(n),window.removeEventListener("scroll",s),window.removeEventListener("resize",s)}},[e,t])};function sn(e,t,n){const s=r.useDependency(te.IEditorService),o=f.useMemo(()=>new G.BehaviorSubject({left:-999,top:-999,right:-999,bottom:-999}),[]),c=r.useDependency(E.ISidebarService),i=r.useDependency(r.IUniverInstanceService),a=E.useEvent(()=>{const l=s.getEditor(e);if(!l)return;const m=l.getBoundingClientRect(),{left:d,top:p,right:h,bottom:g}=m,u=o.getValue();if(!(u.left===d&&u.top===p&&u.right===h&&u.bottom===g))return o.next({left:d-1,right:h+1,top:p-1,bottom:g+1}),m});return f.useEffect(()=>{t&&a()},[e,s,i.unitAdded$,a,t,...n!=null?n:[]]),to(a),f.useEffect(()=>{const l=c.scrollEvent$.pipe(G.throttleTime(100)).subscribe(a);return()=>{l.unsubscribe()}},[]),[o,a]}const Ye=e=>{const t=f.useRef(e);return t.current=e,t},no=(e,t,n)=>{const s=r.useDependency(Ne.IDescriptionService),o=r.useDependency(N.LexerTreeBuilder),[c,i]=f.useState(),[a,l]=f.useState(-1),[m,d]=f.useState(!0),p=Ye(m),h=f.useRef(t);h.current=t;const g=()=>{i(void 0),l(-1),d(!1)};return f.useEffect(()=>{if(n&&e){const u=n.selectionChange$.pipe(G.debounceTime(50)).subscribe(I=>{if(I.textRanges.length===1){const[b]=I.textRanges;if(b.collapsed&&p.current){const C=o.getFunctionAndParameter(`${h.current}A`,b.startOffset-1);if(C){const{functionName:_,paramIndex:T}=C,x=s.getFunctionInfo(_);i(x),l(T);return}}}i(void 0),l(-1)}),S=n.selectionChange$.pipe(G.filter(I=>I.isEditing),G.filter(I=>I.textRanges.length===1),G.map(I=>I.textRanges[0].startOffset),G.distinctUntilChanged()).subscribe(()=>{d(!0)});return()=>{u.unsubscribe(),S.unsubscribe()}}},[n,e]),f.useEffect(()=>{e||g()},[e]),{functionInfo:c,paramIndex:a,reset:g}},ce={formulaHelpFunction:"univer-formula-help-function",formulaHelpFunctionTitle:"univer-formula-help-function-title",formulaHelpFunctionTitleIcons:"univer-formula-help-function-title-icons",formulaHelpFunctionTitleIcon:"univer-formula-help-function-title-icon",formulaHelpFunctionContent:"univer-formula-help-function-content",formulaHelpFunctionContentInner:"univer-formula-help-function-content-inner",formulaHelpFunctionContentParams:"univer-formula-help-function-content-params",formulaHelpFunctionContentParamsTitle:"univer-formula-help-function-content-params-title",formulaHelpFunctionContentParamsDetail:"univer-formula-help-function-content-params-detail",formulaHelpFunctionActive:"univer-formula-help-function-active",formulaHelpDecorator:"univer-formula-help-decorator",formulaHelpParam:"univer-formula-help-param",formulaHelpParamPrefix:"univer-formula-help-param-prefix",formulaHelpParamItem:"univer-formula-help-param-item",formulaHelpParamActive:"univer-formula-help-param-active"},Et=({className:e,title:t,value:n})=>v.jsxs("div",{className:ce.formulaHelpFunctionContentParams,children:[v.jsx("div",{className:`
  15. ${ce.formulaHelpFunctionContentParamsTitle}
  16. ${e}
  17. `,children:t}),v.jsx("div",{className:ce.formulaHelpFunctionContentParamsDetail,children:n})]}),oo=e=>{const{prefix:t,value:n,active:s,onClick:o}=e;return v.jsxs("div",{className:ce.formulaHelpParam,children:[v.jsxs("span",{className:ce.formulaHelpParamPrefix,children:[t,"("]}),n&&n.map((c,i)=>v.jsxs("span",{className:ce.formulaHelpParamItem,children:[v.jsx("span",{className:s===i?ce.formulaHelpFunctionActive:ce.formulaHelpParamActive,onClick:()=>o(i),children:Rt(c)}),i===n.length-1?"":","]},c.name)),")"]})},rn=()=>{};function so(e){const{onParamsSwitch:t=rn,onClose:n=rn,isFocus:s,editor:o,formulaText:c}=e,{functionInfo:i,paramIndex:a,reset:l}=no(s,c,o),m=f.useMemo(()=>!!i&&a>=0,[i,a]),[d,p]=f.useState(!0),h=r.useDependency(r.LocaleService),g=h.t("formula.prompt.required"),u=h.t("formula.prompt.optional"),S=o.getEditorId(),[I]=sn(S,m,[i,a]);function b(_){t&&t(_)}const C=()=>{l(),n()};return m&&i?v.jsx(E.RectPopup,{portal:!0,onClickOutside:()=>l(),anchorRect$:I,direction:"vertical",children:v.jsxs("div",{className:ce.formulaHelpFunction,children:[v.jsxs("div",{className:ce.formulaHelpFunctionTitle,children:[v.jsx(oo,{prefix:i.functionName,value:i.functionParameter,active:a,onClick:b}),v.jsxs("div",{className:ce.formulaHelpFunctionTitleIcons,children:[v.jsx("div",{className:ce.formulaHelpFunctionTitleIcon,style:{transform:d?"rotateZ(-90deg)":"rotateZ(90deg)"},onClick:()=>p(!d),children:v.jsx(ht,{})}),v.jsx("div",{className:ce.formulaHelpFunctionTitleIcon,onClick:C,children:v.jsx(et,{})})]})]}),v.jsx("div",{className:ce.formulaHelpFunctionContent,style:{height:d?"unset":0,padding:d?"revert-layer":0},children:v.jsxs("div",{className:ce.formulaHelpFunctionContentInner,children:[v.jsx(Et,{title:h.t("formula.prompt.helpExample"),value:`${i.functionName}(${i.functionParameter.map(_=>_.example).join(",")})`}),v.jsx(Et,{title:h.t("formula.prompt.helpAbstract"),value:i.description}),i&&i.functionParameter&&i.functionParameter.map((_,T)=>v.jsx(Et,{className:a===T?ce.formulaHelpFunctionActive:"",title:_.name,value:`${_.require?g:u} ${_.detail}`},T))]})})]})}):null}const cn=(e,t,n=!0)=>{let s=-1;return e.reduce((o,c,i)=>{if(o.isFinish)return o;const a=o.currentIndex;if(typeof c!="string")o.currentIndex+=c.token.length;else{const l=c.length;o.currentIndex+=l}return(n?o.currentIndex===t:t>a&&t<=o.currentIndex)&&(s=i,o.isFinish=!0),o},{currentIndex:0,isFinish:!1}),s},ot=e=>e.reduce((t,n)=>typeof n=="string"?t+n.length:t+n.token.length,0),ro=()=>{},io=(e,t,n,s,o,c,i,a,l,m,d=ro)=>{const p=r.useDependency(X.IRenderManagerService),h=r.useDependency(r.IUniverInstanceService),g=r.useDependency(r.ICommandService),u=Ye(c),S=r.useDependency(ut.DocSelectionManagerService),I=r.useDependency(r.ThemeService),b=h.getUnit(s),C=E.useEvent(y=>{var R,D;return(D=(R=b==null?void 0:b.getSheetBySheetId(y))==null?void 0:R.getName())!=null?D:""}),_=f.useMemo(()=>C(o),[C,o]),T=r.useObservable(b==null?void 0:b.activeSheet$),x=Ye({activeSheet:T,sheetName:_}),F=p.getRenderById(s),P=F==null?void 0:F.with(W.RefSelectionsRenderService),L=F==null?void 0:F.with(w.SheetSkeletonManagerService),j=r.useDependency(K.IRefSelectionsService),O=f.useRef(!1),M=f.useRef(n);M.current=n;const k=f.useRef();f.useEffect(()=>{if(P&&e){let y=!0;const R=(A,H)=>{var ae,ne,U,ge,me,de;if(y){y=!1;return}const Z=m==null?void 0:m.getSelectionRanges();if((Z==null?void 0:Z.length)!==1)return;const z=Z[0].startOffset-1,Y=[...u.current],oe=cn(Y,z,!1);if(M.current===nt.NEED_ADD)if(z!==0){if(oe===-1&&Y.length)return;const se=A[A.length-1],Ee=Y.splice(oe+1),le=(ae=se.rangeWithCoord.sheetId)!=null?ae:o,ve={range:se.rangeWithCoord,unitId:(ne=se.rangeWithCoord.unitId)!=null?ne:s,sheetName:C(le)},Ce=Ie([ve],a&&le!==o,_);Y.push({token:Ce[0],nodeType:N.sequenceNodeType.REFERENCE});const xe=[...Y,...Ee],Fe=_e(xe);d(Fe,ot(Y),H)}else{const se=A[A.length-1],Ee=(U=se.rangeWithCoord.sheetId)!=null?U:o,le={range:se.rangeWithCoord,unitId:(ge=se.rangeWithCoord.unitId)!=null?ge:s,sheetName:C(Ee)},he=Ie([le],a&&Ee!==o);Y.unshift({token:he[0],nodeType:N.sequenceNodeType.REFERENCE});const Ce=_e(Y);d(Ce,he[0].length,H)}else{let se=0;const Ee=Y.map(V=>{var B,Q,q;if(typeof V=="string")return V;if(V.nodeType===N.sequenceNodeType.REFERENCE){const $=N.deserializeRangeWithSheet(V.token);if($.sheetName||($.sheetName=_),a&&((B=x.current.activeSheet)==null?void 0:B.getName())!==$.sheetName)return V.token;const ie=A[se];if(se++,!ie)return"";const je=(Q=ie.rangeWithCoord.sheetId)!=null?Q:o,Te={range:ie.rangeWithCoord,unitId:(q=ie.rangeWithCoord.unitId)!=null?q:s,sheetName:C(je)};return Ie([Te],a,_)[0]}return V.token});let le="",ve;Ee.forEach((V,B)=>{le+=V,B===oe&&(ve=le.length)});const he=[];for(let V=se;V<=A.length-1;V++){const B=A[V],Q=(me=B.rangeWithCoord.sheetId)!=null?me:o,q={range:B.rangeWithCoord,unitId:(de=B.rangeWithCoord.unitId)!=null?de:s,sheetName:C(Q)},ie=Ie([q],a&&Q!==o,_);he.push(ie[0])}const Ce=Y[Y.length-1],xe=Ce&&(typeof Ce=="string"?!1:Ce.nodeType===N.sequenceNodeType.REFERENCE),Fe=`${le}${he.length&&xe?",":""}${he.join(",")}`;d(Fe,!he.length&&ve?ve:Fe.length,H)}},D=new r.DisposableCollection;return D.add(P.selectionMoving$.subscribe(A=>{O.current||R(A,!1)})),D.add(P.selectionMoveEnd$.subscribe(A=>{O.current||R(A,!0)})),()=>{D.dispose()}}},[P,m,a,e,u,o,s,C,_,d,x]),f.useEffect(()=>{if(t&&P&&m){const y=new r.DisposableCollection,R=(H,Z)=>{let J=0,z=0,Y=!1;const{sheetName:oe}=x.current,ae=u.current.map(U=>{var ge;if(typeof U=="string")return Y||(z+=U.length),U;if(U.nodeType===N.sequenceNodeType.REFERENCE){const me=N.deserializeRangeWithSheet(U.token);if(me.unitId||(me.unitId=s),me.sheetName||(me.sheetName=oe),a&&((ge=x.current.activeSheet)==null?void 0:ge.getName())!==me.sheetName)return Y||(z+=U.token.length),U;if(J===Z){Y=!0;const de={...U,token:H};return a&&me.sheetName!==oe?de.token=N.serializeRangeWithSheet(me.sheetName,N.deserializeRangeWithSheet(H).range):de.token=H,z+=de.token.length,J++,de}return Y||(z+=U.token.length),J++,U}return Y||(z+=U.token.length),U}),ne=_e(ae);d(ne,-1,!0),k.current={result:ne,offset:z}},D=()=>{y.dispose(),P.getSelectionControls().forEach((Z,J)=>{y.add(G.merge(Z.selectionMoving$,Z.selectionScaling$).pipe(Be.map(z=>N.serializeRange(z)),Be.distinctUntilChanged(),Be.debounceTime(100)).subscribe(z=>{O.current=!0,R(z,J)}))}),y.add(P.selectionMoveEnd$.subscribe(Z=>{if(O.current=!1,k.current){const{result:J,offset:z}=k.current;d(J,z||-1,!0),k.current=void 0}}))},A=G.merge(m.input$,j.selectionSet$,P.selectionMoveEnd$).pipe(Be.debounceTime(50)).subscribe(()=>{D()});return()=>{A.unsubscribe(),y.dispose()}}},[t,P,m,j.selectionSet$,x,u,d,a,s]),f.useEffect(()=>{if(l){const y=g.onCommandExecuted(R=>{if(R.id!==K.SetSelectionsOperation.id)return;const D=R.params;if(D.extra!=="formula-editor")return;const{selections:A}=D;if(A.length){const H=A[A.length-1];if(H){const Z=H.range,J=o,z={range:Z,unitId:D.unitId===s?"":D.unitId,sheetName:D.subUnitId===J?"":C(J)},Y=[...u.current],ae=Ie([z],a,_)[0];let ne=Y[Y.length-1];if(typeof ne=="object"&&ne.nodeType===N.sequenceNodeType.REFERENCE){ne={...ne},ne.token=ae,ne.endIndex=ne.startIndex+ae.length,Y[Y.length-1]=ne;const U=_e(Y);d(U,ot(Y),!0)}else{const U=ot(Y);Y.push({nodeType:N.sequenceNodeType.REFERENCE,token:ae,startIndex:U,endIndex:U+ae.length});const ge=_e(Y);d(ge,ot(Y),!0)}}}});return()=>{y.dispose()}}},[g,C,d,a,l,u,_,o,s]),f.useEffect(()=>{if(!m)return;const y=S.textSelection$.subscribe(R=>{R.unitId===m.getEditorId()&&Gt({unitId:s,subUnitId:o,refSelections:i.current,editor:m,refSelectionsService:j,refSelectionsRenderService:P,sheetSkeletonManagerService:L,themeService:I,univerInstanceService:h})});return()=>y.unsubscribe()},[S.textSelection$,m,i,P,j,u,L,o,I,s,h])},co=(e,t,n)=>{const s=r.useDependency(N.LexerTreeBuilder),o=f.useRef(!0);f.useEffect(()=>{if(e){const c=setTimeout(()=>{o.current=!1},500);return()=>{clearTimeout(c)}}},[e]),f.useEffect(()=>{if(!o.current&&t){const c=s.checkIfAddBracket(n);t(c===0&&n.startsWith(N.operatorToken.EQUALS),`${n}`)}},[n,t])},ke={sheetEmbeddingFormulaEditorActive:"univer-sheet-embedding-formula-editor-active",sheetEmbeddingFormulaEditorWrap:"univer-sheet-embedding-formula-editor-wrap",sheetEmbeddingFormulaEditorText:"univer-sheet-embedding-formula-editor-text",sheetEmbeddingFormulaEditorError:"univer-sheet-embedding-formula-editor-error",sheetEmbeddingFormulaEditor:"univer-sheet-embedding-formula-editor",sheetEmbeddingFormulaEditorErrorWrap:"univer-sheet-embedding-formula-editor-error-wrap"},ao=(e,t=[],n)=>{const s=r.useDependency(Ne.IDescriptionService),[o,c]=f.useState([]),[i,a]=f.useState(""),l=f.useRef(-1),m=Ye({nodes:t}),d=()=>{c([]),a(""),l.current=-1};return f.useEffect(()=>{if(n&&e){const h=n.input$.pipe(G.debounceTime(300)).subscribe(()=>{const g=n.getSelectionRanges();if(g.length===1){const u=m.current.nodes,S=g[0];if(S.collapsed){const I=cn(u,S.startOffset-1,!1);l.current=I;const b=u[I];if(b&&typeof b!="string"&&b.nodeType===N.sequenceNodeType.FUNCTION){l.current=I;const C=b.token,_=s.getSearchListByNameFirstLetter(C);c(_),a(C);return}}}l.current=-1,a(""),c(u=>u!=null&&u.length?[]:u)});return()=>{h.unsubscribe()}}},[n,e]),f.useEffect(()=>{e||d()},[e]),{searchList:o,searchText:i,handlerFormulaReplace:h=>{const g=[...m.current.nodes];if(l.current!==-1){const u=g.splice(l.current+1),S=g.pop()||"";let I=(typeof S=="string"?S.length:S.token.length)-h.length;return g.push(h),u[0]!==N.matchToken.OPEN_BRACKET&&(g.push(N.matchToken.OPEN_BRACKET),I--),{text:_e([...g,...u]),offset:I}}},reset:d}},Oe={formulaSearchFunction:"univer-formula-search-function",formulaSearchFunctionItem:"univer-formula-search-function-item",formulaSearchFunctionItemName:"univer-formula-search-function-item-name",formulaSearchFunctionItemNameLight:"univer-formula-search-function-item-name-light",formulaSearchFunctionItemDesc:"univer-formula-search-function-item-desc",formulaSearchFunctionItemActive:"univer-formula-search-function-item-active"},lo=()=>{},uo=f.forwardRef(fo);function fo(e,t){const{isFocus:n,sequenceNodes:s,onSelect:o,editor:c,onClose:i=lo}=e,a=c.getEditorId(),l=r.useDependency(E.IShortcutService),m=r.useDependency(r.ICommandService),{searchList:d,searchText:p,handlerFormulaReplace:h,reset:g}=ao(n,s,c),u=f.useMemo(()=>!!d.length,[d]),S=f.useRef(),[I,b]=f.useState(0),C=f.useRef(!1),[_]=sn(a,u,[p,d]),T=Ye({searchList:d,active:I}),x=O=>{const M=h(O);M&&(g(),o(M))};function F(O){C.current&&b(O)}function P(){C.current&&b(-1)}f.useEffect(()=>{if(!d.length)return;const O=`sheet.formula-embedding-editor.search_function.${a}`,M=new r.DisposableCollection,k=y=>{const{searchList:R,active:D}=T.current;switch(y){case E.KeyCode.ARROW_UP:{b(A=>{const H=Math.max(0,A-1);return L(H),H});break}case E.KeyCode.ARROW_DOWN:{b(A=>{const H=Math.min(R.length-1,A+1);return L(H),H});break}case E.KeyCode.TAB:case E.KeyCode.ENTER:{const A=R[D];x(A.name);break}case E.KeyCode.ESC:{g(),i();break}}};return M.add(m.registerCommand({id:O,type:r.CommandType.OPERATION,handler(y,R){const{keyCode:D}=R;k(D)}})),[E.KeyCode.ARROW_UP,E.KeyCode.ARROW_DOWN,E.KeyCode.ENTER,E.KeyCode.ESC,E.KeyCode.TAB].map(y=>({id:O,binding:y,preconditions:()=>!0,priority:1e3,staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keyCode:y}})).forEach(y=>{M.add(l.registerShortcut(y))}),()=>{M.dispose()}},[d]);function L(O){var z;const M=(z=S.current)==null?void 0:z.querySelectorAll(`.${Oe.formulaSearchFunctionItem}`)[O];if(!M)return;const k=M.parentNode;if(!k)return;const R=k.getBoundingClientRect().top,D=k.offsetHeight,A=M.getBoundingClientRect(),H=A.top,Z=A.height;if(H>=0&&H>R&&H-R+Z<=D)return;const J=M.offsetTop-(D-Z)/2;k.scrollTo({top:J,behavior:"smooth"})}const j=f.useMemo(()=>{let O="";return()=>{clearTimeout(O),C.current=!0,O=setTimeout(()=>{C.current=!1},300)}},[]);return d.length>0&&u&&v.jsx(E.RectPopup,{portal:!0,anchorRect$:_,direction:"vertical",children:v.jsx("ul",{className:Oe.formulaSearchFunction,ref:O=>{S.current=O,t&&(t.current=O)},children:d.map((O,M)=>v.jsxs("li",{className:I===M?`
  18. ${Oe.formulaSearchFunctionItem}
  19. ${Oe.formulaSearchFunctionItemActive}
  20. `:Oe.formulaSearchFunctionItem,onMouseEnter:()=>F(M),onMouseLeave:P,onMouseMove:j,onClick:()=>{x(O.name),c&&c.focus()},children:[v.jsxs("span",{className:Oe.formulaSearchFunctionItemName,children:[v.jsx("span",{className:Oe.formulaSearchFunctionItemNameLight,children:O.name.substring(0,p.length)}),v.jsx("span",{children:O.name.slice(p.length)})]}),v.jsx("span",{className:Oe.formulaSearchFunctionItemDesc,children:O.desc})]},O.name))})})}const mo=e=>e.startsWith(N.operatorToken.EQUALS)?e.slice(1):"",_t=()=>{};function an(e){var Ot,vn;const{errorText:t,initValue:n,unitId:s,subUnitId:o,isFocus:c=!0,isSupportAcrossSheet:i=!1,onFocus:a=_t,onBlur:l=_t,onChange:m,onVerify:d,actions:p,className:h,editorId:g,moveCursor:u=!0,onFormulaSelectingChange:S,keyboradEventConfig:I,onMoveInEditor:b,resetSelectionOnBlur:C=!0,autoScrollbar:_=!0,isSingle:T=!0,disableSelectionOnClick:x=!1,disableContextMenu:F}=e,P=r.useDependency(te.IEditorService),L=f.useRef(null),j=E.useEvent(m);p&&(p.handleOutClick=(ue,fe)=>{L.current&&!L.current.contains(ue.target)&&fe()});const O=E.useEvent(S),M=f.useRef(null),k=f.useRef(),y=k.current,[R,D]=f.useState(c),A=f.useRef(null),H=f.useMemo(()=>g!=null?g:r.createInternalEditorID(`${w.EMBEDDING_FORMULA_EDITOR}-${r.generateRandomId(4)}`),[]),Z=f.useMemo(()=>t!==void 0,[t]),J=r.useDependency(r.IUniverInstanceService),z=J.getUnit(H);r.useObservable(z==null?void 0:z.change$);const Y=tt(),oe=r.BuildTextUtils.transform.getPlainText((vn=(Ot=z==null?void 0:z.getBody())==null?void 0:Ot.dataStream)!=null?vn:""),ae=f.useMemo(()=>mo(oe),[oe]),ne=f.useMemo(()=>Y(ae),[ae,Y]),{isSelecting:U}=Zn(H,R,x),ge=f.useRef(""),de=r.useDependency(X.IRenderManagerService).getRenderById(H),se=de==null?void 0:de.with(te.DocSelectionRenderService),Ee=se==null?void 0:se.isFocusing,le=f.useMemo(()=>J.getCurrentTypeOfUnit$(r.UniverInstanceType.UNIVER_DOC),[J]),ve=r.useObservable(le),he=(ve==null?void 0:ve.getUnitId())===H,Ce=f.useRef([]),xe=U;E.useUpdateEffect(()=>{j(oe)},[oe,j]);const Fe=Zt("="),V=St(s,o),B=E.useEvent((ue,fe=!0,be,He)=>{if(!k.current)return;ge.current=ue;const Dt=Y(ue[0]==="="?ue.slice(1):""),We=Fe(k.current,Dt,fe,He);Ce.current=We,be&&V(R?We:[],k.current)});f.useEffect(()=>{R&&B(oe,!1,!0)},[oe,R,B]),co(R,d,oe);const Q=zt(y),q=$t(R,s,o);f.useEffect(()=>{O(U)},[O,U]),te.useKeyboardEvent(R,I,y),f.useLayoutEffect(()=>{let ue;if(A.current){ue=P.register({autofocus:!0,editorUnitId:H,initialSnapshot:{id:H,body:{dataStream:`${n}\r
  21. `,textRuns:[],customBlocks:[],customDecorations:[],customRanges:[]},documentStyle:{}}},A.current);const fe=P.getEditor(H);k.current=fe,B(n,!1,!0)}return()=>{ue==null||ue.dispose()}},[]),f.useLayoutEffect(()=>{c?(D(c),Q()):(C&&(y==null||y.blur(),q()),D(c))},[c,y,Q,q,C]);const{checkScrollBar:$}=te.useResize(y,T,_);gt(R,!!(U&&he),s,F),Qt(R&&u,xe,y,b);const ie=E.useEvent((ue,fe,be)=>{if(!Ee)return;const He=fe!==-1?[{startOffset:fe+1,endOffset:fe+1,collapsed:!0}]:void 0;B(`=${ue}`,!0,be,He),be&&(Q(),fe!==-1&&setTimeout(()=>{const Dt={startOffset:fe+1,endOffset:fe+1},We=y==null?void 0:y.render.with(te.DocBackScrollRenderController);We==null||We.scrollToRange({...Dt,collapsed:!0})},50),$())});io(R&&!!(U&&he),R,U,s,o,ne,Ce,i,!!xe,y,ie),It(R&&!!(U&&he),s,i,D,l,_t);const je=ue=>{if(ue){const fe=y==null?void 0:y.getSelectionRanges();if(fe&&fe.length===1){const be=fe[0];if(be.collapsed){const He=ue.offset;setTimeout(()=>{y==null||y.setSelectionRanges([{startOffset:be.startOffset-He,endOffset:be.endOffset-He}])},30)}}Q(),B(`=${ue.text}`)}},Te=()=>{D(!0),a(),Q()};return v.jsxs("div",{className:pt(ke.sheetEmbeddingFormulaEditor,h),children:[v.jsx("div",{className:pt(ke.sheetEmbeddingFormulaEditorWrap,{[ke.sheetEmbeddingFormulaEditorActive]:R,[ke.sheetEmbeddingFormulaEditorError]:Z}),ref:L,children:v.jsx("div",{className:ke.sheetEmbeddingFormulaEditorText,ref:A,onMouseUp:Te})}),t!==void 0?v.jsx("div",{className:ke.sheetEmbeddingFormulaEditorErrorWrap,children:t}):null,y?v.jsx(so,{editor:y,isFocus:R,formulaText:oe,onClose:()=>Q()}):null,y?v.jsx(uo,{isFocus:R,sequenceNodes:ne,onSelect:je,ref:M,editor:y}):null]})}const ho="sheets-formula-ui.base.config",ln={};function po(e){return e.getContextValue(r.FOCUSING_DOC)&&e.getContextValue(r.FOCUSING_UNIVER_EDITOR)}function un(e,t=!1){return!t&&!(r.isFormulaString(e==null?void 0:e.f)||r.isFormulaId(e==null?void 0:e.si))?null:typeof(e==null?void 0:e.v)=="string"&&N.ERROR_TYPE_SET.has(e.v)?e.v:null}var So=Object.defineProperty,go=Object.getOwnPropertyDescriptor,vo=(e,t,n,s)=>{for(var o=s>1?void 0:s?go(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&So(t,n,o),o},ze=(e,t)=>(n,s)=>t(n,s,e);const yt="SHEET_FORMULA_ALERT",Co={[N.ErrorType.DIV_BY_ZERO]:"divByZero",[N.ErrorType.NAME]:"name",[N.ErrorType.VALUE]:"value",[N.ErrorType.NUM]:"num",[N.ErrorType.NA]:"na",[N.ErrorType.CYCLE]:"cycle",[N.ErrorType.REF]:"ref",[N.ErrorType.SPILL]:"spill",[N.ErrorType.CALC]:"calc",[N.ErrorType.ERROR]:"error",[N.ErrorType.CONNECT]:"connect",[N.ErrorType.NULL]:"null"};let Tt=class extends r.Disposable{constructor(e,t,n,s,o,c){super(),this._context=e,this._hoverManagerService=t,this._cellAlertManagerService=n,this._localeService=s,this._formulaDataModel=o,this._zenZoneService=c,this._init()}_init(){this._initCellAlertPopup(),this._initZenService()}_initCellAlertPopup(){this.disposeWithMe(this._hoverManagerService.currentCell$.pipe(G.debounceTime(100)).subscribe(e=>{var t,n,s,o,c;if(e){const a=this._context.unit.getActiveSheet();if(!a)return;const l=a.getCell(e.location.row,e.location.col),m=(o=(s=(n=(t=this._formulaDataModel.getArrayFormulaCellData())==null?void 0:t[e.location.unitId])==null?void 0:n[e.location.subUnitId])==null?void 0:s[e.location.row])==null?void 0:o[e.location.col];if(r.isICellData(l)){const d=un(l,!!m);if(!d){this._hideAlert();return}const p=this._cellAlertManagerService.currentAlert.get(yt),h=(c=p==null?void 0:p.alert)==null?void 0:c.location;if(h&&h.row===e.location.row&&h.col===e.location.col&&h.subUnitId===e.location.subUnitId&&h.unitId===e.location.unitId)return;this._cellAlertManagerService.showAlert({type:w.CellAlertType.ERROR,title:this._localeService.t("formula.error.title"),message:this._localeService.t(`formula.error.${Co[d]}`),location:e.location,width:200,height:74,key:yt});return}}this._hideAlert()}))}_initZenService(){this.disposeWithMe(this._zenZoneService.visible$.subscribe(e=>{e&&this._hideAlert()}))}_hideAlert(){this._cellAlertManagerService.removeAlert(yt)}};Tt=vo([ze(1,r.Inject(w.HoverManagerService)),ze(2,r.Inject(w.CellAlertManagerService)),ze(3,r.Inject(r.LocaleService)),ze(4,r.Inject(N.FormulaDataModel)),ze(5,E.IZenZoneService)],Tt);var Io=Object.defineProperty,Ro=Object.getOwnPropertyDescriptor,Eo=(e,t,n,s)=>{for(var o=s>1?void 0:s?Ro(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&Io(t,n,o),o},fn=(e,t)=>(n,s)=>t(n,s,e);let st=class extends r.Disposable{constructor(e,t){super(),this._autoFillService=e,this._lexerTreeBuilder=t,this._registerAutoFill()}_registerAutoFill(){const e={type:w.DATA_TYPE.FORMULA,priority:1001,match:t=>r.isFormulaString(t==null?void 0:t.f)||r.isFormulaId(t==null?void 0:t.si),isContinue:(t,n)=>t.type===w.DATA_TYPE.FORMULA,applyFunctions:{[w.APPLY_TYPE.COPY]:(t,n,s,o)=>{const{data:c,index:i}=t;return this._fillCopyFormula(c,n,s,i,o)}}};this._autoFillService.registerRule(e)}_fillCopyFormula(e,t,n,s,o){var l,m;const c=yo(o),i=[],a=new Map;for(let d=1;d<=t;d++){const p=(d-1)%e.length,h=r.Tools.deepClone(e[p]);if(h){const g=((l=e[p])==null?void 0:l.f)||"",u=((m=e[p])==null?void 0:m.si)||"",S=r.isFormulaString(g);if(r.isFormulaId(u))h.si=u,h.f=null,h.v=null,h.p=null,h.t=null,i.push(h);else if(S){let b=a.get(p);if(b)h.si=b,h.f=null,h.v=null,h.p=null,h.t=null;else{b=r.Tools.generateRandomId(6),a.set(p,b);const{offsetX:C,offsetY:_}=_o(c,t,n),T=this._lexerTreeBuilder.moveFormulaRefOffset(g,C,_);h.si=b,h.f=T,h.v=null,h.p=null,h.t=null}i.push(h)}}}return i}};st=Eo([fn(0,w.IAutoFillService),fn(1,r.Inject(N.LexerTreeBuilder))],st);function _o(e,t,n){let s=0,o=0;switch(n){case r.Direction.UP:o=-e*t;break;case r.Direction.RIGHT:s=e;break;case r.Direction.DOWN:o=e;break;case r.Direction.LEFT:s=-e*t;break}return{offsetX:s,offsetY:o}}function yo(e){let t=0;for(const n in e)e[n].forEach(s=>{t+=s.data.length});return t}var To=Object.defineProperty,bo=Object.getOwnPropertyDescriptor,No=(e,t,n,s)=>{for(var o=s>1?void 0:s?bo(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&To(t,n,o),o},Ge=(e,t)=>(n,s)=>t(n,s,e);const Mo="default-paste-formula";let rt=class extends r.Disposable{constructor(e,t,n,s,o){super(),this._currentUniverSheet=e,this._lexerTreeBuilder=t,this._sheetClipboardService=n,this._injector=s,this._formulaDataModel=o,this._initialize()}_initialize(){this._registerClipboardHook()}_registerClipboardHook(){this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteFormulaHook())),this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteWithFormulaHook()))}_pasteFormulaHook(){return{id:w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,priority:10,specialPasteInfo:{label:"specialPaste.formula"},onPasteCells:(e,t,n,s)=>this._onPasteCells(e,t,n,s,!0)}}_pasteWithFormulaHook(){return{id:Mo,priority:10,onPasteCells:(e,t,n,s)=>this._onPasteCells(e,t,n,s,!1)}}_onPasteCells(e,t,n,s,o){var h;if([w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH].includes(s.pasteType))return{undos:[],redos:[]};const i=this._currentUniverSheet.getCurrentUnitForType(r.UniverInstanceType.UNIVER_SHEET),a=t.unitId||i.getUnitId(),l=t.subUnitId||((h=i.getActiveSheet())==null?void 0:h.getSheetId());if(!a||!l)return{undos:[],redos:[]};const m=t.range,d=n,p={copyType:s.copyType||w.COPY_TYPE.COPY,copyRange:e==null?void 0:e.range,pasteType:s.pasteType};return this._injector.invoke(g=>Oo(a,l,m,d,g,p,this._lexerTreeBuilder,this._formulaDataModel,o,e))}};rt=No([Ge(0,r.IUniverInstanceService),Ge(1,r.Inject(N.LexerTreeBuilder)),Ge(2,w.ISheetClipboardService),Ge(3,r.Inject(r.Injector)),Ge(4,r.Inject(N.FormulaDataModel))],rt);function Oo(e,t,n,s,o,c,i,a,l=!1,m){const d=[],p=[],h=Do(e,t,n,s,c,i,a,m),g={unitId:e,subUnitId:t,cellValue:h.getData()};d.push({id:K.SetRangeValuesMutation.id,params:g});const u=K.SetRangeValuesUndoMutationFactory(o,g);return p.push({id:K.SetRangeValuesMutation.id,params:u}),{undos:p,redos:d}}function Do(e,t,n,s,o,c,i,a){return a?o.pasteType===w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE?Fo(e,t,n,s,i,a):o.pasteType===w.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA?Po(e,t,n,s,c,i,a):Ao(e,t,n,s,c,i,a):xo(e,t,n,s,i)}function xo(e,t,n,s,o){const c=new r.ObjectMatrix,i=o.getSheetFormulaData(e,t);return s.forValue((a,l,m)=>{var g;const d=n.rows[a],p=n.cols[l],h={};r.isFormulaString(m.v)?(h.v=null,h.f=`${m.v}`,h.si=null,h.p=null,c.setValue(d,p,h)):(g=i==null?void 0:i[d])!=null&&g[p]&&(h.v=m.v,h.f=null,h.si=null,h.p=null,c.setValue(d,p,h))}),c}function Fo(e,t,n,s,o,c){var m,d;const i=new r.ObjectMatrix,a=(d=(m=o.getArrayFormulaCellData())==null?void 0:m[c.unitId])==null?void 0:d[c.subUnitId],l=o.getSheetFormulaData(e,t);return s.forValue((p,h,g)=>{var _,T;const u=c.range.rows[p%c.range.rows.length],S=c.range.cols[h%c.range.cols.length],I=n.rows[p],b=n.cols[h],C={};if(r.isFormulaString(g.f)||r.isFormulaId(g.si))C.v=g.v,C.f=null,C.si=null,C.p=null,i.setValue(I,b,C);else if((_=a==null?void 0:a[u])!=null&&_[S]){const x=a[u][S];C.v=x.v,C.f=null,C.si=null,C.p=null,i.setValue(I,b,C)}else if((T=l==null?void 0:l[I])!=null&&T[b]){if(C.v=g.v,C.f=null,C.si=null,C.p=null,g.p){const x=dn(g);x&&(C.v=x)}i.setValue(I,b,C)}}),i}function Po(e,t,n,s,o,c,i){const a=new r.ObjectMatrix,l=new Map;return s.forValue((m,d,p)=>{const h=n.rows[m],g=n.cols[d],u={};if(r.isFormulaId(p.si)){if(i.unitId!==e||i.subUnitId!==t){const S=c.getFormulaStringByCell(i.range.rows[m%i.range.rows.length],i.range.cols[d%i.range.cols.length],i.subUnitId,i.unitId),I=n.cols[d]-i.range.cols[d%i.range.cols.length],b=n.rows[m]-i.range.rows[m%i.range.rows.length],C=o.moveFormulaRefOffset(S||"",I,b);u.si=null,u.f=C}else u.si=p.si,u.f=null;u.v=null,u.p=null,a.setValue(h,g,u)}else if(r.isFormulaString(p.f)){const S=`${m%i.range.rows.length}_${d%i.range.cols.length}`;let I=l.get(S);if(I)u.si=I,u.f=null;else{I=r.Tools.generateRandomId(6),l.set(S,I);const b=n.cols[d]-i.range.cols[d%i.range.cols.length],C=n.rows[m]-i.range.rows[m%i.range.rows.length],_=o.moveFormulaRefOffset(p.f||"",b,C);u.si=I,u.f=_}u.v=null,u.p=null,a.setValue(h,g,u)}else{if(u.v=p.v,u.f=null,u.si=null,u.p=null,p.p){const S=dn(p);S&&(u.v=S)}a.setValue(h,g,u)}}),a}function Ao(e,t,n,s,o,c,i){const a=new r.ObjectMatrix,l=new Map,m=c.getSheetFormulaData(e,t);return s.forValue((d,p,h)=>{var I;const g=n.rows[d],u=n.cols[p],S={};if(r.isFormulaId(h.si)){if(i.unitId!==e||i.subUnitId!==t){const b=c.getFormulaStringByCell(i.range.rows[d%i.range.rows.length],i.range.cols[p%i.range.cols.length],i.subUnitId,i.unitId),C=n.cols[p]-i.range.cols[p%i.range.cols.length],_=n.rows[d]-i.range.rows[d%i.range.rows.length],T=o.moveFormulaRefOffset(b||"",C,_);S.si=null,S.f=T}else S.si=h.si,S.f=null;S.v=null,S.p=null,a.setValue(g,u,S)}else if(r.isFormulaString(h.f)){const b=`${d%i.range.rows.length}_${p%i.range.cols.length}`;let C=l.get(b);if(C)S.si=C,S.f=null;else{C=r.Tools.generateRandomId(6),l.set(b,C);const _=n.cols[p]-i.range.cols[p%i.range.cols.length],T=n.rows[d]-i.range.rows[d%i.range.rows.length],x=o.moveFormulaRefOffset(h.f||"",_,T);S.si=C,S.f=x}S.v=null,S.p=null,a.setValue(g,u,S)}else(I=m==null?void 0:m[g])!=null&&I[u]&&(S.v=h.v,S.f=null,S.si=null,S.p=h.p,a.setValue(g,u,S))}),a}function dn(e){if(e!=null&&e.p){const t=e==null?void 0:e.p.body;if(t==null)return;const n=t.dataStream;return n.substring(n.length-2,n.length)===r.DEFAULT_EMPTY_DOCUMENT_VALUE?n.substring(0,n.length-2):n}}var Lo=Object.defineProperty,wo=Object.getOwnPropertyDescriptor,ko=(e,t,n,s)=>{for(var o=s>1?void 0:s?wo(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&Lo(t,n,o),o},Ae=(e,t)=>(n,s)=>t(n,s,e);let it=class extends r.Disposable{constructor(t,n,s,o,c,i,a,l){super();ee(this,"_previousShape");ee(this,"_skeleton");this._context=t,this._sheetInterceptorService=n,this._formulaDataModel=s,this._themeService=o,this._renderManagerService=c,this._sheetSkeletonManagerService=i,this._commandService=a,this._logService=l,this._initSkeletonChangeListener(),this._initInterceptorEditorStart(),this._commandExecutedListener()}_initSkeletonChangeListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(t=>{var n,s;if(t==null)this._logService.debug("[FormulaEditorShowController]: should not receive currentSkeleton$ as null!");else{const{skeleton:o}=t,c=(s=(n=this._skeleton)==null?void 0:n.worksheet)==null?void 0:s.getSheetId();if(this._changeRuntime(o),c!==o.worksheet.getSheetId())this._removeArrayFormulaRangeShape();else{const{unitId:i,sheetId:a}=t;this._updateArrayFormulaRangeShape(i,a)}}}))}_changeRuntime(t){this._skeleton=t}_initInterceptorEditorStart(){this.disposeWithMe(r.toDisposable(this._sheetInterceptorService.writeCellInterceptor.intercept(K.BEFORE_CELL_EDIT,{handler:(t,n,s)=>{var u,S,I,b;const{row:o,col:c,unitId:i,subUnitId:a,worksheet:l}=n,m=this._formulaDataModel.getArrayFormulaRange(),d=this._formulaDataModel.getArrayFormulaCellData();if(this._removeArrayFormulaRangeShape(),t==null)return s(t);let p=null;const h=this._formulaDataModel.getFormulaStringByCell(o,c,a,i);if(h!==null&&(p={f:h}),t.v!=null&&t.v!==""&&((I=(S=(u=d[i])==null?void 0:u[a])==null?void 0:S[o])==null?void 0:I[c])==null)return p?{...t,...p}:s(t);const g=(b=m==null?void 0:m[i])==null?void 0:b[a];return g!=null&&(p=this._displayArrayFormulaRangeShape(g,o,c,i,a,l,p)),p?{...t,...p}:s(t)}})))}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{(t.id===N.SetFormulaCalculationResultMutation.id||t.id===N.SetArrayFormulaDataMutation.id&&n&&n.remove)&&this._removeArrayFormulaRangeShape()})),this.disposeWithMe(this._commandService.beforeCommandExecuted(t=>{K.SetWorksheetRowAutoHeightMutation.id===t.id&&requestIdleCallback(()=>{const n=t.params,{unitId:s,subUnitId:o,rowsAutoHeightInfo:c}=n;this._refreshArrayFormulaRangeShapeByRow(s,o,c)})}))}_displayArrayFormulaRangeShape(t,n,s,o,c,i,a){const l=this._formulaDataModel.getSheetFormulaData(o,c);return new r.ObjectMatrix(t).forValue((m,d,p)=>{var I;if(p==null)return!0;const{startRow:h,startColumn:g,endRow:u,endColumn:S}=p;if(m===n&&d===s)return this._createArrayFormulaRangeShape(p,o),!1;if(n>=h&&n<=u&&s>=g&&s<=S){const b=i.getCell(h,g);if((b==null?void 0:b.v)===N.ErrorType.SPILL)return;const C=(I=l==null?void 0:l[m])==null?void 0:I[d];return C==null||C.f==null?!0:(a==null&&(a={f:C.f,isInArrayFormulaRange:!0}),this._createArrayFormulaRangeShape(p,o),!1)}}),a}_createArrayFormulaRangeShape(t,n){const s=this._renderManagerService.getRenderById(n),o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!s||!o)return;const{scene:c}=s;if(!c)return;const i=this._themeService.getCurrentTheme(),a={range:t,primary:null,style:{strokeWidth:1,stroke:i.hyacinth700,fill:new r.ColorKit(i.colorWhite).setAlpha(0).toString(),widgets:{}}},l=w.attachSelectionWithCoord(a,o),{rowHeaderWidth:m,columnHeaderHeight:d}=o,p=new w.SelectionControl(c,w.SELECTION_SHAPE_DEPTH.FORMULA_EDITOR_SHOW,this._themeService,{highlightHeader:!1,rowHeaderWidth:m,columnHeaderHeight:d});p.updateRangeBySelectionWithCoord(l),p.setEvent(!1),this._previousShape=p}_removeArrayFormulaRangeShape(){this._previousShape!=null&&(this._previousShape.dispose(),this._previousShape=null)}_refreshArrayFormulaRangeShape(t,n){if(this._previousShape){const{startRow:s,endRow:o,startColumn:c,endColumn:i}=this._previousShape.getRange(),a={startRow:s,endRow:o,startColumn:c,endColumn:i};this._removeArrayFormulaRangeShape(),this._createArrayFormulaRangeShape(a,t)}}_checkCurrentSheet(t,n){const s=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!s)return!1;const o=s.worksheet;return o?o.unitId===t&&o.getSheetId()===n:!1}_updateArrayFormulaRangeShape(t,n){this._checkCurrentSheet(t,n)&&this._previousShape&&this._refreshArrayFormulaRangeShape(t)}_refreshArrayFormulaRangeShapeByRow(t,n,s){if(!this._checkCurrentSheet(t,n)||!this._previousShape)return;const{startRow:o,endRow:c,startColumn:i,endColumn:a}=this._previousShape.getRange();for(let l=0;l<s.length;l++){const{row:m}=s[l];if(o>=m){const d={startRow:o,endRow:c,startColumn:i,endColumn:a};this._refreshArrayFormulaRangeShape(t,d);break}}}};it=ko([Ae(1,r.Inject(K.SheetInterceptorService)),Ae(2,r.Inject(N.FormulaDataModel)),Ae(3,r.Inject(r.ThemeService)),Ae(4,X.IRenderManagerService),Ae(5,r.Inject(w.SheetSkeletonManagerService)),Ae(6,r.ICommandService),Ae(7,r.ILogService)],it);var jo=Object.defineProperty,Ho=Object.getOwnPropertyDescriptor,Wo=(e,t,n,s)=>{for(var o=s>1?void 0:s?Ho(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&jo(t,n,o),o},mn=(e,t)=>(n,s)=>t(n,s,e);const Bo={tl:{size:6,color:"#409f11"}};let ct=class extends r.RxDisposable{constructor(e,t){super(),this._sheetInterceptorService=e,this._formulaDataModel=t,this.disposeWithMe(this._sheetInterceptorService.intercept(K.INTERCEPTOR_POINT.CELL_CONTENT,{effect:r.InterceptorEffectEnum.Style,handler:(n,s,o)=>{var a,l,m,d;const c=(d=(m=(l=(a=this._formulaDataModel.getArrayFormulaCellData())==null?void 0:a[s.unitId])==null?void 0:l[s.subUnitId])==null?void 0:m[s.row])==null?void 0:d[s.col];return un(n,!!c)?o({...n,markers:{...n==null?void 0:n.markers,...Bo}}):o(n)},priority:10}))}};ct=Wo([mn(0,r.Inject(K.SheetInterceptorService)),mn(1,r.Inject(N.FormulaDataModel))],ct);function Vo(){const e=r.useDependency(Ne.TriggerCalculationController),t=r.useDependency(r.ICommandService),n=r.useObservable(e.progress$),s=f.useCallback(()=>{t.executeCommand(N.SetFormulaCalculationStopMutation.id)},[t]),o=f.useCallback(()=>{e.clearProgress()},[e]);return v.jsx(E.ProgressBar,{progress:n,onTerminate:s,onClearProgress:o})}const Ko=(e,t=100)=>{f.useEffect(()=>{let n=null;const s=()=>{n===null&&(n=window.setTimeout(()=>{e(),n=null},t))};return window.addEventListener("scroll",s),window.addEventListener("resize",s),()=>{n!==null&&clearTimeout(n),window.removeEventListener("scroll",s),window.removeEventListener("resize",s)}},[e,t])},re={formulaHelpFunction:"univer-formula-help-function",formulaHelpFunctionTitle:"univer-formula-help-function-title",formulaHelpFunctionTitleIcons:"univer-formula-help-function-title-icons",formulaHelpFunctionTitleIcon:"univer-formula-help-function-title-icon",formulaHelpFunctionContent:"univer-formula-help-function-content",formulaHelpFunctionContentInner:"univer-formula-help-function-content-inner",formulaHelpFunctionContentParams:"univer-formula-help-function-content-params",formulaHelpFunctionContentParamsTitle:"univer-formula-help-function-content-params-title",formulaHelpFunctionContentParamsDetail:"univer-formula-help-function-content-params-detail",formulaHelpFunctionActive:"univer-formula-help-function-active",formulaHelpDecorator:"univer-formula-help-decorator",formulaHelpParam:"univer-formula-help-param",formulaHelpParamPrefix:"univer-formula-help-param-prefix",formulaHelpParamItem:"univer-formula-help-param-item",formulaHelpParamActive:"univer-formula-help-param-active"};function Uo(){const[e,t]=f.useState(!1),[n,s]=f.useState(!0),[o,c]=f.useState(!0),[i,a]=f.useState(0),[l,m]=f.useState([0,0]),[d,p]=f.useState({left:0,top:0}),[h,g]=f.useState(null),u=r.useDependency(Ve),S=r.useDependency(r.LocaleService),I=S.t("formula.prompt.required"),b=S.t("formula.prompt.optional"),C=r.useDependency(r.IUniverInstanceService),_=r.useDependency(te.IEditorService),T=r.useDependency(E.ISidebarService),x=r.useInjector();Ko(F),f.useEffect(()=>{const O=u.help$.subscribe(k=>{const{visible:y,paramIndex:R,functionInfo:D}=k;if(!y){t(y);return}const A=P();if(A==null)return;const{left:H,top:Z,height:J}=A;D.description===""&&D.functionParameter.length===0||(m([H,Z+J]),a(R),g(D),p({left:H,top:Z}),t(y))}),M=T.scrollEvent$.pipe(G.throttleTime(100)).subscribe(F);return()=>{O==null||O.unsubscribe(),M.unsubscribe()}},[]);function F(){if(!o)return;const O=P();if(O==null)return;const{left:M,top:k,height:y}=O;m([M,k+y])}function P(){const O=C.getCurrentUniverDocInstance();if(!O)return;const M=O.getUnitId();if(!_.isEditor(M))return;const k=_.getEditor(M);return k==null?void 0:k.getBoundingClientRect()}function L(O){a(O)}function j(){c(!o),x.get(E.ILayoutService).focus()}return v.jsx(v.Fragment,{children:o?v.jsx(pe.Popup,{visible:e,offset:l,children:h?v.jsxs("div",{className:re.formulaHelpFunction,children:[v.jsxs("div",{className:re.formulaHelpFunctionTitle,children:[v.jsx(Yo,{prefix:h.functionName,value:h.functionParameter,active:i,onClick:L}),v.jsxs("div",{className:re.formulaHelpFunctionTitleIcons,children:[v.jsx("div",{className:re.formulaHelpFunctionTitleIcon,style:{transform:n?"rotateZ(-90deg)":"rotateZ(90deg)"},onClick:()=>s(!n),children:v.jsx(ht,{})}),v.jsx("div",{className:re.formulaHelpFunctionTitleIcon,onClick:j,children:v.jsx(et,{})})]})]}),v.jsx("div",{className:re.formulaHelpFunctionContent,style:{height:n?"unset":0,padding:n?"revert-layer":0},children:v.jsxs("div",{className:re.formulaHelpFunctionContentInner,children:[v.jsx(bt,{title:S.t("formula.prompt.helpExample"),value:`${h.functionName}(${h.functionParameter.map(O=>O.example).join(",")})`}),v.jsx(bt,{title:S.t("formula.prompt.helpAbstract"),value:h.description}),h&&h.functionParameter&&h.functionParameter.map((O,M)=>v.jsx(bt,{className:i===M?re.formulaHelpFunctionActive:"",title:O.name,value:`${O.require?I:b} ${O.detail}`},M))]})})]}):v.jsx(v.Fragment,{})}):e?v.jsx("div",{className:re.formulaHelpDecorator,onClick:()=>c(!o),style:{left:d.left-24,top:d.top},children:v.jsx(Vt,{})}):v.jsx(v.Fragment,{})})}const bt=e=>v.jsxs("div",{className:re.formulaHelpFunctionContentParams,children:[v.jsx("div",{className:`
  22. ${re.formulaHelpFunctionContentParamsTitle}
  23. ${e.className}
  24. `,children:e.title}),v.jsx("div",{className:re.formulaHelpFunctionContentParamsDetail,children:e.value})]}),Yo=e=>{const{prefix:t,value:n,active:s,onClick:o}=e;return v.jsxs("div",{className:re.formulaHelpParam,children:[v.jsxs("span",{className:re.formulaHelpParamPrefix,children:[t,"("]}),n&&n.map((c,i)=>v.jsxs("span",{className:re.formulaHelpParamItem,children:[v.jsx("span",{className:s===i?re.formulaHelpFunctionActive:re.formulaHelpParamActive,onClick:()=>o(i),children:Rt(c)}),i===n.length-1?"":","]},i)),")"]})},De={formulaSearchFunction:"univer-formula-search-function",formulaSearchFunctionItem:"univer-formula-search-function-item",formulaSearchFunctionItemName:"univer-formula-search-function-item-name",formulaSearchFunctionItemNameLight:"univer-formula-search-function-item-name-light",formulaSearchFunctionItemDesc:"univer-formula-search-function-item-desc",formulaSearchFunctionItemActive:"univer-formula-search-function-item-active"};function zo(){const[e,t]=f.useState(!1),[n,s]=f.useState(0),[o,c]=f.useState([0,0]),[i,a]=f.useState([]),[l,m]=f.useState(""),d=f.useRef(null),p=r.useDependency(Ve),h=r.useDependency(r.IUniverInstanceService),g=r.useDependency(te.IEditorService);f.useEffect(()=>{let C=[],_=0;const T=p.search$.subscribe(P=>{const{visible:L,searchText:j,searchList:O}=P;if(!L){t(L);return}const M=u();if(M==null)return;const{left:k,top:y,height:R}=M;m(j),a(O),C=O,c([k,y+R]),t(L),s(0),_=0}),x=p.navigate$.subscribe(P=>{const{direction:L}=P;if(L===r.Direction.UP){let j=_-1;j=j<0?C.length-1:j,s(j),_=j}else if(L===r.Direction.DOWN){let j=_+1;j=j>=C.length?0:j,s(j),_=j}b(_)}),F=p.accept$.subscribe(P=>{const L=C[_].name;p.acceptFormulaName(L)});return()=>{T==null||T.unsubscribe(),x==null||x.unsubscribe(),F==null||F.unsubscribe()}},[]);function u(){const _=h.getCurrentUniverDocInstance().getUnitId();if(!g.isEditor(_))return;const T=g.getEditor(_);return T==null?void 0:T.getBoundingClientRect()}function S(C){s(C)}function I(){s(-1)}function b(C){var k;const _=(k=d.current)==null?void 0:k.querySelectorAll(`.${De.formulaSearchFunctionItem}`)[C];if(!_)return;const T=_.parentNode;if(!T)return;const F=T.getBoundingClientRect().top,P=T.offsetHeight,L=_.getBoundingClientRect(),j=L.top,O=L.height;if(j>=0&&j>F&&j-F+O<=P)return;const M=_.offsetTop-(P-O)/2;T.scrollTo({top:M,behavior:"smooth"})}return i.length>0&&v.jsx(pe.Popup,{visible:e,offset:o,children:v.jsx("ul",{className:De.formulaSearchFunction,ref:d,children:i.map((C,_)=>v.jsxs("li",{className:n===_?`
  25. ${De.formulaSearchFunctionItem}
  26. ${De.formulaSearchFunctionItemActive}
  27. `:De.formulaSearchFunctionItem,onMouseEnter:()=>S(_),onMouseLeave:I,onClick:()=>p.acceptFormulaName(C.name),children:[v.jsxs("span",{className:De.formulaSearchFunctionItemName,children:[v.jsx("span",{className:De.formulaSearchFunctionItemNameLight,children:C.name.substring(0,l.length)}),v.jsx("span",{children:C.name.slice(l.length)})]}),v.jsx("span",{className:De.formulaSearchFunctionItemDesc,children:C.desc})]},_))})})}function Go(){return v.jsxs(v.Fragment,{children:[v.jsx(zo,{}),v.jsx(Uo,{})]})}const hn={formulaMoreFunctions:"univer-formula-more-functions",formulaMoreFunctionsOperation:"univer-formula-more-functions-operation"};function pn(e){const{prefix:t,value:n}=e;return v.jsxs("div",{children:[v.jsxs("span",{children:[t,"("]}),n&&n.map((s,o)=>v.jsxs("span",{children:[v.jsx("span",{children:Rt(s)}),o===n.length-1?"":","]},o)),")"]})}const Nt={formulaFunctionParams:"univer-formula-function-params",formulaFunctionParamsTitle:"univer-formula-function-params-title",formulaFunctionParamsDetail:"univer-formula-function-params-detail"};function Ze(e){const{className:t,value:n,title:s}=e;return v.jsxs("div",{className:Nt.formulaFunctionParams,children:[v.jsx("div",{className:`
  28. ${Nt.formulaFunctionParamsTitle}
  29. ${t}
  30. `,children:s}),v.jsx("div",{className:Nt.formulaFunctionParamsDetail,children:n})]})}const Xe={formulaInputParamsList:"univer-formula-input-params-list",formulaInputParamsListItemName:"univer-formula-input-params-list-item-name",formulaInputParamsListItemSelector:"univer-formula-input-params-list-item-selector"};function Zo(e){const{functionInfo:t,onChange:n}=e;if(!t)return null;f.useState([]);const[s,o]=f.useState(t.functionParameter),[c,i]=f.useState(-1);return v.jsxs("div",{className:Xe.formulaInputParams,children:[v.jsx("div",{className:Xe.formulaInputParamsList,children:s.map((a,l)=>v.jsxs("div",{children:[v.jsx("div",{className:Xe.formulaInputParamsListItemName,children:a.name}),v.jsx("div",{className:Xe.formulaInputParamsListItemSelector})]},l))}),v.jsx("div",{className:Xe.formulaInputParamsInfo,children:v.jsx(Ze,{title:c===-1?v.jsx(pn,{prefix:t.functionName,value:s}):s[c].name,value:c===-1?t.description:s[c].detail})})]})}const ye={formulaSelectFunctionSelect:"univer-formula-select-function-select",formulaSelectFunctionResult:"univer-formula-select-function-result",formulaSelectFunctionResultItem:"univer-formula-select-function-result-item",formulaSelectFunctionResultItemNameLight:"univer-formula-select-function-result-item-name-light",formulaSelectFunctionResultItemSelected:"univer-formula-select-function-result-item-selected",formulaSelectFunctionResultItemActive:"univer-formula-select-function-result-item-active",formulaSelectFunctionContent:"univer-formula-select-function-content"};function Xo(e){const{onChange:t}=e,n="-1",[s,o]=f.useState(""),[c,i]=f.useState([]),[a,l]=f.useState(0),[m,d]=f.useState(n),[p,h]=f.useState(0),[g,u]=f.useState(null),S=r.useDependency(Ne.IDescriptionService),I=r.useDependency(r.LocaleService),b=r.useDependency(E.ISidebarService),C=r.useObservable(b.sidebarOptions$),_=eo(N.FunctionType,I);_.unshift({label:I.t("formula.moreFunctions.allFunctions"),value:n});const T=I.t("formula.prompt.required"),x=I.t("formula.prompt.optional");f.useEffect(()=>{L(n)},[]),f.useEffect(()=>{P(0)},[c]),f.useEffect(()=>{C!=null&&C.visible&&(o(""),i([]),l(0),d(n),h(0),u(null),L(n))},[C]);const F=y=>{if(s.trim()==="")return y;const R=new RegExp(`(${s.toLocaleUpperCase()})`);return y.split(R).filter(Boolean).map((A,H)=>A.match(R)?v.jsx("span",{className:ye.formulaSelectFunctionResultItemNameLight,children:A},H):A)},P=y=>{if(c.length===0){u(null);return}h(y);const R=S.getFunctionInfo(c[y].name);if(!R){u(null);return}u(R),t(R)};function L(y){d(y);const R=S.getSearchListByType(+y);i(R)}function j(y){o(y);const R=S.getSearchListByName(y);i(R)}function O(y){if(y.stopPropagation(),y.key==="ArrowDown"){const R=a+1;l(R===c.length?0:R)}else if(y.key==="ArrowUp"){const R=a-1;l(R===-1?c.length-1:R)}else y.key==="Enter"&&P(a)}const M=y=>{l(y)},k=()=>{l(-1)};return v.jsxs("div",{children:[v.jsxs("div",{className:ye.formulaSelectFunctionSelect,children:[v.jsx(pe.Select,{value:m,options:_,onChange:L}),v.jsx(pe.Input,{placeholder:I.t("formula.moreFunctions.searchFunctionPlaceholder"),onKeyDown:O,value:s,onChange:j,size:"large",allowClear:!0})]}),v.jsx("ul",{className:ye.formulaSelectFunctionResult,onKeyDown:O,tabIndex:-1,children:c.map(({name:y},R)=>v.jsxs("li",{className:a===R?`
  31. ${ye.formulaSelectFunctionResultItem}
  32. ${ye.formulaSelectFunctionResultItemActive}
  33. `:ye.formulaSelectFunctionResultItem,onMouseEnter:()=>M(R),onMouseLeave:k,onClick:()=>P(R),children:[p===R&&v.jsx(Wt,{className:ye.formulaSelectFunctionResultItemSelected}),v.jsx("span",{className:ye.formulaSelectFunctionResultItemName,children:F(y)})]},R))}),g&&v.jsxs("div",{className:ye.formulaSelectFunctionContent,children:[v.jsx(Ze,{title:g.functionName,value:g.description}),v.jsx(Ze,{title:I.t("formula.moreFunctions.syntax"),value:v.jsx(pn,{prefix:g.functionName,value:g.functionParameter})}),v.jsx(Ze,{title:I.t("formula.prompt.helpExample"),value:`${g.functionName}(${g.functionParameter.map(y=>y.example).join(",")})`}),g.functionParameter&&g.functionParameter.map((y,R)=>v.jsx(Ze,{title:y.name,value:`${y.require?T:x} ${y.detail}`},R))]})]})}function Jo(){const e=w.useActiveWorkbook(),[t,n]=f.useState(!0),[s,o]=f.useState(!1),[c,i]=f.useState(null),a=r.useDependency(w.IEditorBridgeService),l=r.useDependency(r.LocaleService),m=r.useDependency(te.IEditorService),d=r.useDependency(r.IUniverInstanceService);function p(){n(!t),o(!s)}function h(){const g=K.getSheetCommandTarget(d);if(!g)return;a.changeVisible({visible:!0,unitId:g.unitId,eventType:X.DeviceInputEventType.Dblclick});const u=m.getEditor(r.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),S=m.getEditor(r.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY),I=`=${c==null?void 0:c.functionName}(`;u==null||u.replaceText(I),S==null||S.replaceText(I,!1)}return v.jsxs("div",{className:hn.formulaMoreFunctions,children:[t&&v.jsx(Xo,{onChange:i}),s&&v.jsx(Zo,{functionInfo:c,onChange:()=>{}}),v.jsxs("div",{className:hn.formulaMoreFunctionsOperation,children:[s&&v.jsx(pe.Button,{type:"primary",size:"small",onClick:p,children:l.t("formula.moreFunctions.next")}),s&&v.jsx(pe.Button,{size:"small",onClick:p,children:l.t("formula.moreFunctions.prev")}),t&&!!e&&v.jsx(pe.Button,{type:"primary",size:"small",onClick:h,children:l.t("formula.moreFunctions.confirm")})]})]})}function Qo(e){return{id:Qe.id,icon:"FunctionSingle",tooltip:"formula.insert.tooltip",type:E.MenuItemType.SELECTOR,selections:[{label:"SUM",value:"SUM",icon:"SumSingle"},{label:"AVERAGE",value:"AVERAGE",icon:"AvgSingle"},{label:"COUNT",value:"COUNT",icon:"CntSingle"},{label:"MAX",value:"MAX",icon:"MaxSingle"},{label:"MIN",value:"MIN",icon:"MinSingle"}],hidden$:E.getMenuHiddenObservable(e,r.UniverInstanceType.UNIVER_SHEET),disabled$:w.getCurrentRangeDisable$(e,{workbookTypes:[K.WorkbookEditablePermission],worksheetTypes:[K.WorksheetEditPermission,K.WorksheetSetCellValuePermission],rangeTypes:[K.RangeProtectionPermissionEditPoint]})}}function qo(e){return{id:$e.id,title:"formula.insert.more",type:E.MenuItemType.BUTTON}}function $o(e){return e.get(r.IUniverInstanceService).getCurrentTypeOfUnit$(r.UniverInstanceType.UNIVER_SHEET).pipe(G.switchMap(s=>s&&e.get(E.IClipboardInterfaceService)?new G.Observable(c=>c.next(!e.get(E.IClipboardInterfaceService).supportClipboard)):G.of(!0)))}function es(e){return{id:Je.id,type:E.MenuItemType.BUTTON,title:"formula.operation.pasteFormula",disabled$:$o(e).pipe(G.combineLatestWith(w.getCurrentRangeDisable$(e,{workbookTypes:[K.WorkbookEditablePermission],rangeTypes:[K.RangeProtectionPermissionEditPoint],worksheetTypes:[K.WorksheetSetCellValuePermission,K.WorksheetEditPermission]})),G.map(([t,n])=>t||n))}}const ts={[E.RibbonStartGroup.FORMULAS_INSERT]:{[Qe.id]:{order:1,menuItemFactory:Qo,[$e.id]:{order:1,menuItemFactory:qo}}},[w.PASTE_SPECIAL_MENU_ID]:{[Je.id]:{order:4,menuItemFactory:es}}},ns="meta_key_ctrl_And_Shift",at=[E.KeyCode.ARROW_DOWN,E.KeyCode.ARROW_UP,E.KeyCode.ARROW_LEFT,E.KeyCode.ARROW_RIGHT],os=[...at,E.KeyCode.ENTER,E.KeyCode.TAB,E.KeyCode.ESC];function ss(){const e=[];for(const t of os)e.push({id:Pe.id,binding:t,preconditions:n=>w.whenFormulaEditorActivated(n),staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keycode:t}});return e}function rs(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|E.MetaKeys.SHIFT,preconditions:n=>w.whenFormulaEditorActivated(n),staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keycode:t,metaKey:E.MetaKeys.SHIFT}});return e}function is(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|E.MetaKeys.CTRL_COMMAND,preconditions:n=>w.whenFormulaEditorActivated(n),staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keycode:t,metaKey:E.MetaKeys.CTRL_COMMAND}});return e}function cs(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|E.MetaKeys.SHIFT|E.MetaKeys.CTRL_COMMAND,preconditions:n=>w.whenFormulaEditorActivated(n),staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keycode:t,metaKey:ns}});return e}const as={id:dt.id,binding:E.KeyCode.F4,preconditions:e=>w.whenFormulaEditorActivated(e)};function ls(){const e=[];for(const t of[E.KeyCode.ENTER,E.KeyCode.TAB,E.KeyCode.ARROW_DOWN,E.KeyCode.ARROW_UP])e.push({id:Pe.id,binding:t,preconditions:n=>po(n),staticParameters:{eventType:X.DeviceInputEventType.Keyboard,keycode:t,isSingleEditor:!0}});return e}var us=Object.defineProperty,fs=Object.getOwnPropertyDescriptor,ds=(e,t,n,s)=>{for(var o=s>1?void 0:s?fs(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&us(t,n,o),o},Le=(e,t)=>(n,s)=>t(n,s,e);let lt=class extends r.Disposable{constructor(e,t,n,s,o,c,i){super(),this._injector=e,this._menuManagerService=t,this._commandService=n,this._shortcutService=s,this._uiPartsService=o,this._renderManagerService=c,this._componentManager=i,this._initialize()}_initialize(){this._registerCommands(),this._registerMenus(),this._registerShortcuts(),this._registerComponents(),this._registerRenderModules()}_registerMenus(){this._menuManagerService.mergeMenu(ts)}_registerCommands(){[Je,Qe,$e,wt,Ft,Pe,dt].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerShortcuts(){[...ss(),...rs(),...is(),...cs(),...ls(),as].forEach(e=>{this.disposeWithMe(this._shortcutService.registerShortcut(e))})}_registerComponents(){this.disposeWithMe(this._uiPartsService.registerComponent(E.BuiltInUIPart.CONTENT,()=>r.connectInjector(Go,this._injector))),this.disposeWithMe(this._uiPartsService.registerComponent(w.SheetsUIPart.FORMULA_AUX,()=>r.connectInjector(Vo,this._injector))),this._componentManager.register(Lt,Jo)}_registerRenderModules(){this.disposeWithMe(this._renderManagerService.registerRenderModule(r.UniverInstanceType.UNIVER_SHEET,[it]))}};lt=ds([Le(0,r.Inject(r.Injector)),Le(1,E.IMenuManagerService),Le(2,r.ICommandService),Le(3,E.IShortcutService),Le(4,E.IUIPartsService),Le(5,X.IRenderManagerService),Le(6,r.Inject(E.ComponentManager))],lt);var Sn=Object.defineProperty,ms=Object.getOwnPropertyDescriptor,hs=(e,t,n)=>t in e?Sn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,ps=(e,t,n,s)=>{for(var o=s>1?void 0:s?ms(t,n):t,c=e.length-1,i;c>=0;c--)(i=e[c])&&(o=(s?i(t,n,o):i(o))||o);return s&&o&&Sn(t,n,o),o},Mt=(e,t)=>(n,s)=>t(n,s,e),gn=(e,t,n)=>hs(e,typeof t!="symbol"?t+"":t,n);W.UniverSheetsFormulaUIPlugin=class extends r.Plugin{constructor(t=ln,n,s,o){super(),this._config=t,this._injector=n,this._renderManagerService=s,this._configService=o;const{menu:c,...i}=r.merge(ln,this._config);c&&this._configService.setConfig("menu",c,{merge:!0}),this._configService.setConfig(ho,i)}onStarting(){const t=this._injector;[[Ve,{useClass:ft}],[lt],[st],[rt],[it],[ct]].forEach(o=>t.add(o));const s=this._injector.get(E.ComponentManager);s.register(w.RANGE_SELECTOR_COMPONENT_KEY,on),s.register(w.EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY,an)}onRendered(){[[W.RefSelectionsRenderService],[Tt]].forEach(t=>{this.disposeWithMe(this._renderManagerService.registerRenderModule(r.UniverInstanceType.UNIVER_SHEET,t))}),r.touchDependencies(this._injector,[[lt],[rt],[ct]])}onSteady(){this._injector.get(st)}},gn(W.UniverSheetsFormulaUIPlugin,"pluginName",At),gn(W.UniverSheetsFormulaUIPlugin,"type",r.UniverInstanceType.UNIVER_SHEET),W.UniverSheetsFormulaUIPlugin=ps([r.DependentOn(N.UniverFormulaEnginePlugin,Ne.UniverSheetsFormulaPlugin),Mt(1,r.Inject(r.Injector)),Mt(2,X.IRenderManagerService),Mt(3,r.IConfigService)],W.UniverSheetsFormulaUIPlugin),W.FORMULA_PROMPT_ACTIVATED=xt,W.FormulaEditor=an,W.HelpFunctionOperation=Ft,W.InsertFunctionOperation=Qe,W.MoreFunctionsOperation=$e,W.RangeSelector=on,W.ReferenceAbsoluteOperation=dt,W.SearchFunctionOperation=wt,W.SelectEditorFormulaOperation=Pe,W.SheetOnlyPasteFormulaCommand=Je,Object.defineProperty(W,Symbol.toStringTag,{value:"Module"})});