| 123456789101112131415161718192021222324252627282930313233 |
- "use strict";var vn=Object.defineProperty;var Cn=(e,t,n)=>t in e?vn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var Q=(e,t,n)=>Cn(e,typeof t!="symbol"?t+"":t,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),$=require("@univerjs/sheets-ui"),M=require("@univerjs/engine-formula"),q=require("rxjs"),te=require("@univerjs/docs-ui"),z=require("@univerjs/engine-render"),B=require("@univerjs/sheets"),Fe=require("@univerjs/sheets-formula"),I=require("@univerjs/ui"),f=require("react"),me=require("@univerjs/design"),It=require("@univerjs/docs"),Ke=require("rxjs/operators"),rt={id:"sheet.command.paste-formula",type:c.CommandType.COMMAND,handler:async e=>e.get(c.ICommandService).executeCommand($.SheetPasteCommand.id,{value:$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA})},Pe={id:"formula-ui.operation.select-editor-formula",type:c.CommandType.OPERATION,handler:(e,t)=>!0};var Rn=Object.defineProperty,_n=Object.getOwnPropertyDescriptor,In=(e,t,n,o)=>{for(var r=o>1?void 0:o?_n(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&Rn(t,n,r),r},En=(e,t)=>(n,o)=>t(n,o,e);const Ht="FORMULA_PROMPT_ACTIVATED",ze=c.createIdentifier("formula-ui.prompt-service");let vt=class{constructor(e){Q(this,"_search$",new q.Subject);Q(this,"_help$",new q.Subject);Q(this,"_navigate$",new q.Subject);Q(this,"_accept$",new q.Subject);Q(this,"_acceptFormulaName$",new q.Subject);Q(this,"search$",this._search$.asObservable());Q(this,"help$",this._help$.asObservable());Q(this,"navigate$",this._navigate$.asObservable());Q(this,"accept$",this._accept$.asObservable());Q(this,"acceptFormulaName$",this._acceptFormulaName$.asObservable());Q(this,"_searching",!1);Q(this,"_helping",!1);Q(this,"_sequenceNodes",[]);Q(this,"_isLockedOnSelectionChangeRefString",!1);Q(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(Ht,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 o=0,r=this._sequenceNodes.length;o<r;o++){const i=this._sequenceNodes[o];if(typeof i=="string")t++;else{const{endIndex:s}=i;t=s}if(e<=t)return typeof n=="string"&&e!==0?o+1:o}return this._sequenceNodes.length}updateSequenceRef(e,t){const n=this._sequenceNodes[e];if(typeof n=="string"||n.nodeType!==M.sequenceNodeType.REFERENCE)return;const o=t.length-n.token.length,r={...n};r.token=t,r.endIndex+=o,this._sequenceNodes[e]=r;for(let i=e+1,s=this._sequenceNodes.length;i<s;i++){const a=this._sequenceNodes[i];if(typeof a=="string")continue;const l={...a};l.startIndex+=o,l.endIndex+=o,this._sequenceNodes[i]=l}}insertSequenceRef(e,t){const n=t.length,o=this.getCurrentSequenceNodeIndex(e);this._sequenceNodes.splice(o,0,{token:t,startIndex:e,endIndex:e+n-1,nodeType:M.sequenceNodeType.REFERENCE});for(let r=o+1,i=this._sequenceNodes.length;r<i;r++){const s=this._sequenceNodes[r];if(typeof s=="string")continue;const a={...s};a.startIndex+=n,a.endIndex+=n,this._sequenceNodes[r]=a}}insertSequenceString(e,t){const n=this.getCurrentSequenceNodeIndex(e),o=t.split("");this._sequenceNodes.splice(n,0,...o);const r=o.length;for(let i=n+r,s=this._sequenceNodes.length;i<s;i++){const a=this._sequenceNodes[i];if(typeof a=="string")continue;const l={...a};l.startIndex+=r,l.endIndex+=r,this._sequenceNodes[i]=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}};vt=In([En(0,c.IContextService)],vt);const Ut={id:"formula-ui.operation.help-function",type:c.CommandType.OPERATION,handler:async(e,t)=>(e.get(ze).help(t),!0)},ot={id:"formula-ui.operation.insert-function",type:c.CommandType.OPERATION,handler:async(e,t)=>{var C,E;const n=e.get(B.SheetsSelectionsService),o=e.get(te.IEditorService),r=n.getCurrentSelections();if(!r||!r.length)return!1;const i=B.getSheetCommandTarget(e.get(c.IUniverInstanceService));if(!i)return!1;const{worksheet:s,unitId:a,subUnitId:l}=i,h=s.getCellMatrix(),{value:d}=t,g=e.get(c.ICommandService),m=e.get($.IEditorBridgeService),S=[];let u=null,p=0,R=0,b="";if(r.length===1&&(bn(r[0].range)||Mn(r[0].range))){const{range:T,primary:O}=r[0],D=(C=O==null?void 0:O.actualRow)!=null?C:T.startRow,A=(E=O==null?void 0:O.actualColumn)!=null?E:T.startColumn;u=T,p=D,R=A;const L=Ot(h,D,A);L&&(b=M.serializeRange(L))}else r.some(T=>{var w,y;const{range:O,primary:D}=T,A=(w=D==null?void 0:D.actualRow)!=null?w:O.startRow,L=(y=D==null?void 0:D.actualColumn)!=null?y:O.startColumn,k=Ot(h,A,L);if(!k)return u=O,p=A,R=L,!0;const x=M.serializeRange(k),N=`=${d}(${x})`;return S.push({range:O,primary:{row:A,column:L},formula:N}),!1});if(u){const T=B.getCellAtRowCol(p,R,s),O={range:c.Rectangle.clone(u),primary:{startRow:T.startRow,startColumn:T.startColumn,endRow:T.endRow,endColumn:T.endColumn,actualRow:p,actualColumn:R,isMerged:T.isMerged,isMergedMainCell:T.startRow===p&&T.startColumn===R}},D={unitId:a,subUnitId:l,selections:[O]};await g.executeCommand(B.SetSelectionsOperation.id,D);const A=o.getEditor(c.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),L=o.getEditor(c.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY);m.changeVisible({visible:!0,unitId:a,eventType:z.DeviceInputEventType.Dblclick});const k=`=${d}(${b}`;A==null||A.replaceText(k),L==null||L.replaceText(k,!1)}return S.length===0?!1:g.executeCommand(Fe.InsertFunctionCommand.id,{list:S})}};function Ot(e,t,n){const o=yn(e,t,n);if(o!==t)return{startRow:o,endRow:t-1,startColumn:n,endColumn:n};const r=Tn(e,t,n);return r!==n?{startRow:t,endRow:t,startColumn:r,endColumn:n-1}:null}function yn(e,t,n){let o=!1;if(t===0)return t;for(let r=t-1;r>=0;r--){const i=e.getValue(r,n);if(Ze(i)&&!o){if(r===0)return 0;o=!0}else{if(o&&!Ze(i))return r+1;if(o&&r===0)return 0}}return t}function Tn(e,t,n){let o=!1;if(n===0)return n;for(let r=n-1;r>=0;r--){const i=e.getValue(t,r);if(Ze(i)&&!o){if(r===0)return 0;o=!0}else{if(o&&!Ze(i))return r+1;if(o&&r===0)return 0}}return n}function Ze(e){if(e!=null&&e.p){const t=e==null?void 0:e.p.body;if(t==null)return!1;const n=t.dataStream,r=n.substring(n.length-2,n.length)===c.DEFAULT_EMPTY_DOCUMENT_VALUE?n.substring(0,n.length-2):n;return c.isRealNum(r)}return e&&(e.t===c.CellValueType.NUMBER||c.getCellValueType(e)===c.CellValueType.NUMBER)}function bn(e){return e.startRow===e.endRow&&e.startColumn===e.endColumn}function Mn(e){return e.startRow!==e.endRow&&e.startColumn!==e.endColumn}const Wt="SHEET_FORMULA_UI_PLUGIN",Bt=`${Wt}_MORE_FUNCTIONS_COMPONENT`,st={id:"formula-ui.operation.more-functions",type:c.CommandType.OPERATION,handler:async e=>(e.get(I.ISidebarService).open({header:{title:"formula.insert.tooltip"},children:{label:Bt}}),!0)},Et={id:"formula-ui.operation.change-ref-to-absolute",type:c.CommandType.OPERATION,handler:async e=>!0},Vt={id:"formula-ui.operation.search-function",type:c.CommandType.OPERATION,handler:async(e,t)=>(e.get(ze).search(t),!0)};var ft={exports:{}},He={};/**
- * @license React
- * react-jsx-runtime.production.min.js
- *
- * Copyright (c) Facebook, Inc. and its affiliates.
- *
- * This source code is licensed under the MIT license found in the
- * LICENSE file in the root directory of this source tree.
- */var Dt;function Nn(){if(Dt)return He;Dt=1;var e=f,t=Symbol.for("react.element"),n=Symbol.for("react.fragment"),o=Object.prototype.hasOwnProperty,r=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,i={key:!0,ref:!0,__self:!0,__source:!0};function s(a,l,h){var d,g={},m=null,S=null;h!==void 0&&(m=""+h),l.key!==void 0&&(m=""+l.key),l.ref!==void 0&&(S=l.ref);for(d in l)o.call(l,d)&&!i.hasOwnProperty(d)&&(g[d]=l[d]);if(a&&a.defaultProps)for(d in l=a.defaultProps,l)g[d]===void 0&&(g[d]=l[d]);return{$$typeof:t,type:a,key:m,ref:S,props:g,_owner:r.current}}return He.Fragment=n,He.jsx=s,He.jsxs=s,He}var At;function xn(){return At||(At=1,ft.exports=Nn()),ft.exports}var v=xn(),ve=function(){return ve=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++){t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},ve.apply(this,arguments)},Fn=function(e,t){var n={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(n[o]=e[o]);if(e!=null&&typeof Object.getOwnPropertySymbols=="function")for(var r=0,o=Object.getOwnPropertySymbols(e);r<o.length;r++)t.indexOf(o[r])<0&&Object.prototype.propertyIsEnumerable.call(e,o[r])&&(n[o[r]]=e[o[r]]);return n},Oe=f.forwardRef(function(e,t){var n=e.icon,o=e.id,r=e.className,i=e.extend,s=Fn(e,["icon","id","className","extend"]),a="univerjs-icon univerjs-icon-".concat(o," ").concat(r||"").trim(),l=f.useRef("_".concat(An()));return Kt(n,"".concat(o),{defIds:n.defIds,idSuffix:l.current},ve({ref:t,className:a},s),i)});function Kt(e,t,n,o,r){return f.createElement(e.tag,ve(ve({key:t},On(e,n,r)),o),(Dn(e,n).children||[]).map(function(i,s){return Kt(i,"".concat(t,"-").concat(e.tag,"-").concat(s),n,void 0,r)}))}function On(e,t,n){var o=ve({},e.attrs);n!=null&&n.colorChannel1&&o.fill==="colorChannel1"&&(o.fill=n.colorChannel1);var r=t.defIds;return!r||r.length===0||(e.tag==="use"&&o["xlink:href"]&&(o["xlink:href"]=o["xlink:href"]+t.idSuffix),Object.entries(o).forEach(function(i){var s=i[0],a=i[1];typeof a=="string"&&(o[s]=a.replace(/url\(#(.*)\)/,"url(#$1".concat(t.idSuffix,")")))})),o}function Dn(e,t){var n,o=t.defIds;return!o||o.length===0?e:e.tag==="defs"&&(!((n=e.children)===null||n===void 0)&&n.length)?ve(ve({},e),{children:e.children.map(function(r){return typeof r.attrs.id=="string"&&o&&o.indexOf(r.attrs.id)>-1?ve(ve({},r),{attrs:ve(ve({},r.attrs),{id:r.attrs.id+t.idSuffix})}):r})}):e}function An(){return Math.random().toString(36).substring(2,8)}Oe.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"}}]},qt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"check-mark-single",ref:t,icon:Pn}))});qt.displayName="CheckMarkSingle";var Ln={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"}}]},it=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"close-single",ref:t,icon:Ln}))});it.displayName="CloseSingle";var wn={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"}}]},Yt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"delete-single",ref:t,icon:wn}))});Yt.displayName="DeleteSingle";var kn={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"}}]},zt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"details-single",ref:t,icon:kn}))});zt.displayName="DetailsSingle";var $n={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"}}]},Gt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"increase-single",ref:t,icon:$n}))});Gt.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"}}]},yt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"more-single",ref:t,icon:jn}))});yt.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"}}]},Zt=f.forwardRef(function(e,t){return f.createElement(Oe,Object.assign({},e,{id:"select-range-single",ref:t,icon:Hn}))});Zt.displayName="SelectRangeSingle";function Xt(e){var t,n,o="";if(typeof e=="string"||typeof e=="number")o+=e;else if(typeof e=="object")if(Array.isArray(e)){var r=e.length;for(t=0;t<r;t++)e[t]&&(n=Xt(e[t]))&&(o&&(o+=" "),o+=n)}else for(n in e)e[n]&&(o&&(o+=" "),o+=n);return o}function Ct(){for(var e,t,n=0,o="",r=arguments.length;n<r;n++)(e=arguments[n])&&(t=Xt(e))&&(o&&(o+=" "),o+=t);return o}var Un=Object.defineProperty,Wn=Object.getOwnPropertyDescriptor,Bn=(e,t,n,o)=>{for(var r=o>1?void 0:o?Wn(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&Un(t,n,r),r},$e=(e,t)=>(n,o)=>t(n,o,e);exports.RefSelectionsRenderService=class extends $.BaseSelectionRenderService{constructor(n,o,r,i,s,a,l){super(o,r,i,s,a);Q(this,"_workbookSelections");Q(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(Vn(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(),c.toDisposable(()=>this._disableSelectionChanging())}_disableSelectionChanging(){var n;(n=this._eventDisposables)==null||n.dispose(),this._eventDisposables=null}disableSelectionChanging(){this._disableSelectionChanging()}_initCanvasEventListeners(){const n=this._getSheetObject(),{spreadsheetRowHeader:o,spreadsheetColumnHeader:r,spreadsheet:i,spreadsheetLeftTopPlaceholder:s}=n,{scene:a}=this._context,l=new c.DisposableCollection;return l.add(i==null?void 0:i.onPointerDown$.subscribeEvent((h,d)=>{this.inRefSelectionMode()&&(this._onPointerDown(h,i.zIndex+1,c.RANGE_TYPE.NORMAL,this._getActiveViewport(h)),h.button!==2&&d.stopPropagation())})),l.add(o==null?void 0:o.onPointerDown$.subscribeEvent((h,d)=>{if(!this.inRefSelectionMode())return;const g=this._sheetSkeletonManagerService.getCurrent().skeleton,{row:m}=$.getCoordByOffset(h.offsetX,h.offsetY,a,g);$.checkInHeaderRanges(this._workbookSelections.getCurrentSelections(),m,c.RANGE_TYPE.ROW)||(this._onPointerDown(h,(i.zIndex||1)+1,c.RANGE_TYPE.ROW,this._getActiveViewport(h),z.ScrollTimerType.Y),h.button!==2&&d.stopPropagation())})),l.add(r==null?void 0:r.onPointerDown$.subscribeEvent((h,d)=>{if(!this.inRefSelectionMode())return;const g=this._sheetSkeletonManagerService.getCurrent().skeleton,{column:m}=$.getCoordByOffset(h.offsetX,h.offsetY,a,g);$.checkInHeaderRanges(this._workbookSelections.getCurrentSelections(),m,c.RANGE_TYPE.COLUMN)||(this._onPointerDown(h,(i.zIndex||1)+1,c.RANGE_TYPE.COLUMN,this._getActiveViewport(h),z.ScrollTimerType.X),h.button!==2&&d.stopPropagation())})),l.add(s==null?void 0:s.onPointerDown$.subscribeEvent((h,d)=>{if(this._reset(),!this.inRefSelectionMode())return;const g=this._sheetSkeletonManagerService.getCurrent().skeleton,m=$.getAllSelection(g);this._addSelectionControlByModelData(m),this._selectionMoveStart$.next(this.getSelectionDataWithStyle());const S=a.onPointerUp$.subscribeEvent(()=>{S.unsubscribe(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())});h.button!==2&&d.stopPropagation()})),l}_addSelectionControlByModelData(n){var a;const o=this._skeleton,r=(a=n.style)!=null?a:$.genNormalSelectionStyle(this._themeService),i=this._scene;return n.style=r,this.newSelectionControl(i,o,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,B.SelectionMoveType.MOVE_START)})),this.disposeWithMe(this.selectionMoving$.subscribe(n=>{this._updateSelections(n,B.SelectionMoveType.MOVING)})),this.disposeWithMe(this.selectionMoveEnd$.subscribe(n=>{this._updateSelections(n,B.SelectionMoveType.MOVE_END)}))}_updateSelections(n,o){const i=this._context.unit.getActiveSheet().getSheetId();n.length!==0&&this._workbookSelections.setSelections(i,n.map(s=>B.convertSelectionDataToRange(s)),o)}_initSkeletonChangeListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(n=>{if(!n)return;const{skeleton:o}=n,{scene:r}=this._context,i=r.getViewport(z.SHEET_VIEWPORT_KEY.VIEW_MAIN);this._skeleton&&this._skeleton.worksheet.getSheetId()!==o.worksheet.getSheetId()&&this._reset(),this._changeRuntime(o,r,i);const s=this._workbookSelections.getCurrentSelections();this.resetSelectionsByModelData(s)}))}_getActiveViewport(n){const o=this._getSheetObject();return o==null?void 0:o.scene.getActiveViewportByCoord(z.Vector2.FromArray([n.offsetX,n.offsetY]))}_getSheetObject(){return $.getSheetObject(this._context.unit,this._context)}_onPointerDown(n,o=0,r=c.RANGE_TYPE.NORMAL,i,s=z.ScrollTimerType.ALL){var N;this._rangeType=r;const a=this._skeleton,l=this._scene;if(!l||!a)return;i&&(this._activeViewport=i);const{offsetX:h,offsetY:d}=n,g=l.getViewport(z.SHEET_VIEWPORT_KEY.VIEW_MAIN);if(!g)return;const m=l.getCoordRelativeToViewport(z.Vector2.FromArray([h,d])),{x:S,y:u}=m;this._startViewportPosX=S,this._startViewportPosY=u;const p=l.getScrollXYInfoByViewport(m),{scaleX:R,scaleY:b}=l.getAncestorScale(),C=this._skeleton.getCellByOffset(S,u,R,b,p);if(!C)return;switch(r){case c.RANGE_TYPE.NORMAL:break;case c.RANGE_TYPE.ROW:C.startColumn=0,C.endColumn=this._skeleton.getColumnCount()-1;break;case c.RANGE_TYPE.COLUMN:C.startRow=0,C.endRow=this._skeleton.getRowCount()-1;break;case c.RANGE_TYPE.ALL:C.startRow=0,C.startColumn=0,C.endRow=this._skeleton.getRowCount()-1,C.endColumn=this._skeleton.getColumnCount()-1}const E={range:C,primary:C,style:null};E.range.rangeType=r;const T=$.attachSelectionWithCoord(E,this._skeleton);this._startRangeWhenPointerDown={...T.rangeWithCoord};const O={...T.rangeWithCoord,rangeType:r};let D=this.getActiveSelectionControl();const A=this.getSelectionControls();for(const w of A){if(n.button===2&&w.model.isInclude(O)){D=w;return}if(w.model.isEqual(O)){D=w;break}}this._checkClearPreviousControls(n);const L=D==null?void 0:D.model.currentCell,k=n.shiftKey&&L,x=this._remainLastEnabled&&!n.ctrlKey&&!n.shiftKey&&!this._skipLastEnabled&&!this._singleSelectionEnabled;k&&L?this._makeSelectionByTwoCells(L,O,a,r,D):x&&D?D.updateRangeBySelectionWithCoord(T):D=this.newSelectionControl(l,a,E);for(let w=0;w<this.getSelectionControls().length-1;w++)this.getSelectionControls()[w].clearHighlight();this._selectionMoveStart$.next(this.getSelectionDataWithStyle()),l.disableObjectsEvent(),this._clearUpdatingListeners(),this._addEndingListeners(),(N=l.getTransformer())==null||N.clearSelectedObjects(),this._setupPointerMoveListener(g,D,r,s,S,u),this._escapeShortcutDisposable=this._shortcutService.forceEscape(),this._scenePointerUpSub=l.onPointerUp$.subscribeEvent(()=>{var w;this._clearUpdatingListeners(),this._selectionMoveEnd$.next(this.getSelectionDataWithStyle()),(w=this._escapeShortcutDisposable)==null||w.dispose(),this._escapeShortcutDisposable=null})}newSelectionControl(n,o,r){const i=this.getSelectionControls().length,{rowHeaderWidth:s,columnHeaderHeight:a}=o,l=new $.SelectionControl(n,i,this._themeService,{highlightHeader:this._highlightHeader,enableAutoFill:!1,rowHeaderWidth:s,columnHeaderHeight:a}),h=$.attachSelectionWithCoord(r,o);return l.updateRangeBySelectionWithCoord(h),this._selectionControls.push(l),l.setControlExtension({skeleton:o,scene:n,themeService:this._themeService,injector:this._injector,selectionHooks:{selectionMoveEnd:()=>{this._selectionMoveEnd$.next(this.getSelectionDataWithStyle())}}}),l}};exports.RefSelectionsRenderService=Bn([$e(1,c.Inject(c.Injector)),$e(2,c.Inject(c.ThemeService)),$e(3,I.IShortcutService),$e(4,c.Inject($.SheetSkeletonManagerService)),$e(5,c.IContextService),$e(6,B.IRefSelectionsService)],exports.RefSelectionsRenderService);function Vn(e){const t=$.genNormalSelectionStyle(e);return t.widgets={tl:!0,tc:!0,tr:!0,ml:!0,mr:!0,bl:!0,bc:!0,br:!0},t}const Kn=(e,t,n)=>{const r=c.useDependency(z.IRenderManagerService).getRenderById(e),i=r==null?void 0:r.with(exports.RefSelectionsRenderService);f.useEffect(()=>{if(n&&i){const s=n.input$.subscribe(a=>{a.content===M.matchToken.COMMA?i.setSkipLastEnabled(!0):i.setSkipLastEnabled(!1)});return()=>{s.unsubscribe()}}},[n,i]),f.useEffect(()=>{i&&(t.endsWith(M.matchToken.COMMA)||i.setSkipLastEnabled(!1))},[t,i])},ct=()=>{const e=c.useDependency(M.LexerTreeBuilder);return f.useCallback(n=>e.sequenceNodesBuilder(n)||[],[e])},qn=(e,t,n,o,r,i)=>{const s=ct(),a=f.useRef(!0);f.useEffect(()=>{var l,h;if(i){if(a.current){const d=s(e);if(d.length){const g=o(i,d);n&&r(g)}else{const g=i.getDocumentData(),S={dataStream:(h=(l=g.body)==null?void 0:l.dataStream)!=null?h:`${t}\r
- `,...g.body};i.setDocumentData({...g,body:S})}a.current=!1}else if(n){const d=s(e),g=o(i,d);r(g)}}},[i,n])},Jt=e=>I.useEvent(n=>{var o,r;if(e){e.focus();const i=[...e.getSelectionRanges()];if(c.Tools.isDefine(n))e.setSelectionRanges([{startOffset:n,endOffset:n}]);else if(!i.length&&!e.docSelectionRenderService.isOnPointerEvent){const s=(r=(o=e.getDocumentData().body)==null?void 0:o.dataStream)!=null?r:`\r
- `,a=Math.max(s.length-2,0);e.setSelectionRanges([{startOffset:a,endOffset:a}])}}});function Yn(e,t,n){const o=e.getCurrentTheme(),r=new c.ColorKit(t).setAlpha(.05).toRgbString();return{id:n,strokeWidth:1,stroke:t,fill:r,widgets:{tl:!0,tc:!0,tr:!0,ml:!0,mr:!0,bl:!0,bc:!0,br:!0},widgetSize:6,widgetStrokeWidth:1,widgetStroke:o.colorWhite}}function Qt(e){var b,C,E;const{unitId:t,subUnitId:n,refSelections:o,editor:r,refSelectionsService:i,refSelectionsRenderService:s,sheetSkeletonManagerService:a,themeService:l,univerInstanceService:h}=e,d=h.getUnit(t,c.UniverInstanceType.UNIVER_SHEET),g=d==null?void 0:d.getActiveSheet(),m=[];if(!d||!g){i.setSelections(m);return}const S=g.getSheetId(),u=T=>{var O;return(O=d==null?void 0:d.getSheetBySheetName(T))==null?void 0:O.getSheetId()};if(!((b=a==null?void 0:a.getWorksheetSkeleton(S))==null?void 0:b.skeleton))return;const R=[];for(let T=0,O=o.length;T<O;T++){const D=o[T],{themeColor:A,token:L,refIndex:k,endIndex:x}=D,N=M.deserializeRangeWithSheet(L),{unitId:w,sheetName:y,range:_}=N;if(w&&t!==w)continue;const F=u(y);if(F&&F!==S||!F&&S!==n)continue;const P=B.setEndForRange(_,g.getRowCount(),g.getColumnCount());m.push({range:P,primary:null,style:Yn(l,A,k.toString())}),R.push(x)}if(r){const T=(E=(C=r.getSelectionRanges())==null?void 0:C[0])==null?void 0:E.startOffset,O=R.findIndex(D=>D+2===T);O!==-1?s==null||s.setActiveSelectionIndex(O):s==null||s.resetActiveSelectionIndex()}return m}function Tt(e,t){const n=c.useDependency(c.IUniverInstanceService),o=c.useDependency(c.ThemeService),r=c.useDependency(B.IRefSelectionsService),s=c.useDependency(z.IRenderManagerService).getRenderById(e),a=s==null?void 0:s.with(exports.RefSelectionsRenderService),l=s==null?void 0:s.with($.SheetSkeletonManagerService),h=f.useCallback((d,g)=>{const m=Qt({unitId:e,subUnitId:t,refSelections:d,editor:g,refSelectionsService:r,refSelectionsRenderService:a,sheetSkeletonManagerService:l,themeService:o,univerInstanceService:n});if(!m)return;((a==null?void 0:a.getSelectionControls())||[]).length===m.length?a==null||a.resetSelectionsByModelData(m):r.setSelections(m)},[a,r,l,o,e,t,n]);return f.useEffect(()=>()=>{a==null||a.resetActiveSelectionIndex()},[a]),h}function en(e=""){const t=c.useDependency(Fe.IDescriptionService),n=tn(),o=c.useDependency(c.ICommandService),r=f.useMemo(()=>e.length,[e]);return f.useCallback((s,a,l=!0,h)=>{const d=s.getDocumentData(),g=s.getEditorId();if(!d)return[];const m=d.body;if(!m)return[];const S=m.dataStream.slice(0,m.dataStream.length-2),u={dataStream:"",...d.body};if(!S.startsWith(e))return[];if(a==null||a.length===0)return u.textRuns=[],o.syncExecuteCommand(te.ReplaceTextRunsCommand.id,{unitId:g,body:c.getBodySlice(u,0,u.dataStream.length-2)}),[];{const{textRuns:p,refSelections:R}=nn(t,n,a);r&&p.forEach(E=>{E.ed=E.ed+r,E.st=E.st+r}),u.textRuns=[{st:0,ed:1,ts:{fs:11}},...p];const b=a.reduce((E,T)=>typeof T=="string"?`${E}${T}`:`${E}${T.token}`,"");u.dataStream=`${e}${b}\r
- `;let C;if(l){C=s.getSelectionRanges();const E=u.dataStream.length-2+r;C.forEach(T=>{T.startOffset=Math.max(0,Math.min(T.startOffset,E)),T.endOffset=Math.max(0,Math.min(T.endOffset,E))})}return o.syncExecuteCommand(te.ReplaceTextRunsCommand.id,{unitId:g,body:c.getBodySlice(u,0,u.dataStream.length-2),textRanges:h!=null?h:C}),R}},[o,t,n,r,e])}function tn(){const t=c.useDependency(c.ThemeService).getCurrentTheme();return f.useMemo(()=>{const o=[t.loopColor1,t.loopColor2,t.loopColor3,t.loopColor4,t.loopColor5,t.loopColor6,t.loopColor7,t.loopColor8,t.loopColor9,t.loopColor10,t.loopColor11,t.loopColor12],r=t.hyacinth700,i=t.verdancy800,s=t.colorBlack;return{formulaRefColors:o,numberColor:r,stringColor:i,plainTextColor:s}},[t])}function nn(e,t,n){const{formulaRefColors:o,numberColor:r,stringColor:i,plainTextColor:s}=t,a=[],l=[],h=new Map;let d=0;for(let g=0,m=n.length;g<m;g++){const S=n[g];if(typeof S=="string"){const E=a[a.length-1],T=E?E.ed:0,O=T+S.length;a.push({st:T,ed:O,ts:{cl:{rgb:s},fs:11}});continue}if(e.hasDefinedNameDescription(S.token.trim())){a.push({st:S.startIndex,ed:S.endIndex+1,ts:{cl:{rgb:s},fs:11}});continue}const{startIndex:u,endIndex:p,nodeType:R,token:b}=S;let C="";if(R===M.sequenceNodeType.REFERENCE){if(h.has(b))C=h.get(b);else{const E=d%o.length;C=o[E],h.set(b,C),d++}l.push({refIndex:g,themeColor:C,token:b,startIndex:S.startIndex,endIndex:S.endIndex,index:l.length})}else R===M.sequenceNodeType.NUMBER?C=r:(R===M.sequenceNodeType.STRING||R===M.sequenceNodeType.ARRAY)&&(C=i);C&&C.length>0?a.push({st:u,ed:p+1,ts:{cl:{rgb:C},fs:11}}):a.push({st:u,ed:p+1,ts:{cl:{rgb:s},fs:11}})}return{textRuns:a,refSelections:l}}function zn(e){var r,i;const n=e.get(c.IUniverInstanceService).getCurrentUniverDocInstance();return n!=null&&n.getBody()?{dataStream:(i=(r=n.getBody())==null?void 0:r.dataStream)!=null?i:"",offset:0}:void 0}var Xe=(e=>(e[e.NOT_SELECT=0]="NOT_SELECT",e[e.NEED_ADD=1]="NEED_ADD",e[e.CAN_EDIT=2]="CAN_EDIT",e))(Xe||{});function Gn(e,t,n){var S;const r=c.useDependency(z.IRenderManagerService).getRenderById(e),i=r==null?void 0:r.with(te.DocSelectionRenderService),s=c.useDependency(It.DocSelectionManagerService),a=c.useDependency(c.Injector),[l,h]=f.useState(0),d=c.useDependency(M.LexerTreeBuilder),g=f.useRef(!0),m=f.useRef(l);return m.current=l,f.useEffect(()=>{const u=s.textSelection$.pipe(q.filter(p=>p.unitId===e),q.map(()=>{const p=i==null?void 0:i.getActiveTextRange();return p!=null&&p.collapsed?p.startOffset:-1})).subscribe(p=>{var L,k;const R=zn(a);if(!R)return;const b=(L=R==null?void 0:R.dataStream)==null?void 0:L.slice(0,-2),C=(k=d.sequenceNodesBuilder(b))!=null?k:[],E=b[p-1],T=b[p],O=C.findIndex(x=>typeof x=="object"&&x.nodeType===M.sequenceNodeType.REFERENCE&&p===x.endIndex+2),D=E&&M.matchRefDrawToken(E)&&(!T||M.isFormulaLexerToken(T)&&T!==M.matchToken.OPEN_BRACKET),A=O>-1;if((b==null?void 0:b.substring(0,1))==="="&&(D||A))if(A){if(g.current)return;h(2)}else g.current=!1,h(1);else h(0)});return()=>u.unsubscribe()},[s.textSelection$,i,e,a,d]),f.useEffect(()=>{t||(h(0),g.current=!0)},[t]),f.useEffect(()=>{var p;if(!n)return;const u=(p=r==null?void 0:r.mainComponent)==null?void 0:p.onPointerDown$.subscribeEvent(()=>{h(0),g.current=!0});return()=>u==null?void 0:u.unsubscribe()},[n,(S=r==null?void 0:r.mainComponent)==null?void 0:S.onPointerDown$]),{isSelecting:l}}const rn=(e,t,n,o)=>{const r=c.useDependency(c.ICommandService),i=c.useDependency(I.IShortcutService),s=f.useRef(t);s.current=t;const a=f.useRef(o);a.current=o,f.useEffect(()=>{if(!n||!e)return;const h=`sheet.formula-embedding-editor.${n.getEditorId()}`,d=new c.DisposableCollection,g=(u,p)=>{if(a.current){a.current(u,p);return}let R=c.Direction.LEFT;u===I.KeyCode.ARROW_DOWN?R=c.Direction.DOWN:u===I.KeyCode.ARROW_UP?R=c.Direction.UP:u===I.KeyCode.ARROW_RIGHT&&(R=c.Direction.RIGHT),p===I.MetaKeys.SHIFT?r.executeCommand(te.MoveSelectionOperation.id,{direction:R}):r.executeCommand(te.MoveCursorOperation.id,{direction:R})},m=(u,p)=>{let R=c.Direction.DOWN;u===I.KeyCode.ARROW_DOWN?R=c.Direction.DOWN:u===I.KeyCode.ARROW_UP?R=c.Direction.UP:u===I.KeyCode.ARROW_LEFT?R=c.Direction.LEFT:u===I.KeyCode.ARROW_RIGHT&&(R=c.Direction.RIGHT),s.current?p===I.MetaKeys.CTRL_COMMAND?r.executeCommand($.MoveSelectionCommand.id,{direction:R,jumpOver:$.JumpOver.moveGap,extra:"formula-editor",fromCurrentSelection:s.current===Xe.NEED_ADD}):p===I.MetaKeys.SHIFT?r.executeCommand($.ExpandSelectionCommand.id,{direction:R,extra:"formula-editor"}):p===(I.MetaKeys.CTRL_COMMAND|I.MetaKeys.SHIFT)?r.executeCommand($.ExpandSelectionCommand.id,{direction:R,jumpOver:$.JumpOver.moveGap,extra:"formula-editor"}):r.executeCommand($.MoveSelectionCommand.id,{direction:R,extra:"formula-editor",fromCurrentSelection:s.current===Xe.NEED_ADD}):g(u,p)};return d.add(r.registerCommand({id:h,type:c.CommandType.OPERATION,handler(u,p){const{keyCode:R,metaKey:b}=p;m(R,b)}})),[{keyCode:I.KeyCode.ARROW_DOWN},{keyCode:I.KeyCode.ARROW_LEFT},{keyCode:I.KeyCode.ARROW_RIGHT},{keyCode:I.KeyCode.ARROW_UP},{keyCode:I.KeyCode.ARROW_DOWN,metaKey:I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_LEFT,metaKey:I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_RIGHT,metaKey:I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_UP,metaKey:I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_DOWN,metaKey:I.MetaKeys.CTRL_COMMAND},{keyCode:I.KeyCode.ARROW_LEFT,metaKey:I.MetaKeys.CTRL_COMMAND},{keyCode:I.KeyCode.ARROW_RIGHT,metaKey:I.MetaKeys.CTRL_COMMAND},{keyCode:I.KeyCode.ARROW_UP,metaKey:I.MetaKeys.CTRL_COMMAND},{keyCode:I.KeyCode.ARROW_DOWN,metaKey:I.MetaKeys.CTRL_COMMAND|I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_LEFT,metaKey:I.MetaKeys.CTRL_COMMAND|I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_RIGHT,metaKey:I.MetaKeys.CTRL_COMMAND|I.MetaKeys.SHIFT},{keyCode:I.KeyCode.ARROW_UP,metaKey:I.MetaKeys.CTRL_COMMAND|I.MetaKeys.SHIFT}].map(({keyCode:u,metaKey:p})=>({id:h,binding:p?u|p:u,preconditions:()=>!0,priority:900,staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keyCode:u,metaKey:p}})).forEach(u=>{d.add(i.registerShortcut(u))}),()=>{d.dispose()}},[r,n,e,i])},on=(e,t)=>{const o=c.useDependency(z.IRenderManagerService).getRenderById(e),r=o==null?void 0:o.with(exports.RefSelectionsRenderService);f.useEffect(()=>{r&&(t?r==null||r.setRemainLastEnabled(!1):r==null||r.setRemainLastEnabled(!0))},[t,r])},bt=(e,t,n,o=!0)=>{const r=c.useDependency(z.IRenderManagerService),i=c.useDependency(c.IContextService),s=c.useDependency(I.IContextMenuService),a=c.useDependency(B.IRefSelectionsService),l=r.getRenderById(n),h=l==null?void 0:l.with(exports.RefSelectionsRenderService);f.useLayoutEffect(()=>{if(e)return i.setContextValue(c.EDITOR_ACTIVATED,!0),()=>{i.setContextValue(c.EDITOR_ACTIVATED,!1),a.clear()}},[i,e,a]),f.useLayoutEffect(()=>{if(e&&t){const d=h==null?void 0:h.enableSelectionChanging();return i.setContextValue(B.REF_SELECTIONS_ENABLED,!0),()=>{i.setContextValue(B.REF_SELECTIONS_ENABLED,!1),d==null||d.dispose()}}},[i,e,h,t]),f.useEffect(()=>{if(e)return i.setContextValue(c.EDITOR_ACTIVATED,!0),o&&s.disable(),()=>{i.setContextValue(c.EDITOR_ACTIVATED,!1),o&&s.enable()}},[s,i,e,o]),f.useEffect(()=>{e&&(h==null||h.setSkipLastEnabled(!1))},[e,h])},Zn=()=>{f.useEffect(()=>()=>{},[])},sn=(e,t,n)=>{const o=c.useDependency(c.IUniverInstanceService),r=c.useDependency(B.SheetsSelectionsService);return f.useCallback(()=>{if(e){const s=[...r.getWorkbookSelections(t).getSelectionsOfWorksheet(n)],a=o.getCurrentUnitForType(c.UniverInstanceType.UNIVER_SHEET),l=a==null?void 0:a.getActiveSheet();l&&l.getSheetId()===n&&r.setSelections(s)}},[e,r,n,t,o])},Rt=e=>typeof e=="string"?e===M.matchToken.COMMA:!1,Xn=e=>{if(typeof e=="object")return e.nodeType===M.sequenceNodeType.REFERENCE},Jn=e=>e.filter(t=>Rt(t)||Xn(t)),Mt=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},Ie=e=>e.map(t=>typeof t=="string"?t:t.token).join("");function Pt(e,t,n){var o,r;return((r=(o=e.getUnit(t))==null?void 0:o.getSheetBySheetId(n))==null?void 0:r.getName())||""}const _e=(e,t=!1,n="")=>t?e.map(o=>o.sheetName!==""&&o.sheetName!==n?M.serializeRangeWithSheet(o.sheetName,o.range):M.serializeRange(o.range)):e.map(o=>M.serializeRange(o.range)),cn=(e,t,n,o,r,i,s)=>{const a=c.useDependency(z.IRenderManagerService),l=c.useDependency(c.IUniverInstanceService),h=f.useRef(!1),d=a.getRenderById(t),g=d==null?void 0:d.with(exports.RefSelectionsRenderService),m=f.useRef([]),S=f.useMemo(()=>{const p=Jn(o),R=m.current;return p.length===R.length?(R.splice(0),R.push(...p),R):p},[o]);m.current=S;const u=f.useRef();f.useEffect(()=>{if(e&&g){let p=!0;const R=(E,T)=>{var y;if(p||h.current){p=!1;return}const O=[...E],D=l.getUnit(t),A=((y=D==null?void 0:D.getActiveSheet())==null?void 0:y.getName())||"",L=S.map((_,F)=>{if(typeof _=="string"){if(!S[F-1])return null;const j=S[F+1];return Rt(_)&&(Rt(j)||F===S.length-1)?null:_}else if(_.nodeType===M.sequenceNodeType.REFERENCE){const P=M.deserializeRangeWithSheet(_.token);P.unitId=P.unitId===""?t:P.unitId,P.sheetName=P.sheetName===""?A:P.sheetName;const{unitId:j,sheetName:Y}=P;if(i&&(j!==t||A!==Y))return null;if(j===t&&A===Y){const G=O.shift();if(G&&Pt(l,t,G.rangeWithCoord.sheetId||"")===Y){const K={..._};return Mt(G.rangeWithCoord),r?K.token=M.serializeRangeWithSheet(A,G.rangeWithCoord):K.token=M.serializeRange(G.rangeWithCoord),K}}return _}return null}).filter(_=>!!_),k=_e(O.map(_=>{var F,P,j;return{range:_.rangeWithCoord,unitId:(F=_.rangeWithCoord.unitId)!=null?F:"",sheetName:Pt(l,(P=_.rangeWithCoord.unitId)!=null?P:"",(j=_.rangeWithCoord.sheetId)!=null?j:"")}}),r).join(M.matchToken.COMMA),x=Ie(L),N=`${x}${x&&k?M.matchToken.COMMA:""}${k}`,w=h.current;s(N,w?-1:N.length,T)},b=g.selectionMoveEnd$.subscribe(E=>{if(R(E,!0),h.current=!1,u.current){const{result:T,offset:O}=u.current;s(T,O,!0),u.current=void 0}}),C=g.selectionMoving$.pipe(Ke.throttleTime(50)).subscribe(E=>{R(E,!1)});return()=>{b.unsubscribe(),C.unsubscribe()}}},[e,S,g,r,i,s,l,t]),f.useEffect(()=>{if(e&&g){const p=new c.DisposableCollection,R=(E,T)=>{var w;let O=0,D=0,A=!1;const L=l.getUnit(t),k=((w=L==null?void 0:L.getActiveSheet())==null?void 0:w.getName())||"",x=S.map(y=>{if(typeof y=="string")return A||(D+=y.length),y;if(y.nodeType===M.sequenceNodeType.REFERENCE){const _=M.deserializeRangeWithSheet(E);if(_.unitId=_.unitId===""?t:_.unitId,_.sheetName=_.sheetName===""?k:_.sheetName,O===T){A=!0;const F={...y,token:E};return r?F.token=M.serializeRangeWithSheet(_.sheetName,_.range):F.token=M.serializeRange(_.range),O++,D+=F.token.length,F}return O++,A||(D+=y.token.length),y}return y}),N=Ie(x);u.current={result:N,offset:D},s(N,-1,!1)};let b=0;const C=g.selectionMoveEnd$.subscribe(()=>{b=setTimeout(()=>{p.dispose(),g.getSelectionControls().forEach((T,O)=>{p.add(q.merge(T.selectionMoving$,T.selectionScaling$).pipe(q.map(D=>M.serializeRange(D)),q.distinctUntilChanged()).subscribe(D=>{h.current=!0,R(D,O)}))})},30)});return()=>{C.unsubscribe(),p.dispose(),clearTimeout(b)}}},[e,g,S,s,l,t,r])},Nt=(e,t,n,o,r,i)=>{const s=c.useDependency(c.ICommandService),a=c.useDependency(te.IEditorService),h=c.useDependency(z.IRenderManagerService).getRenderById(t),d=h==null?void 0:h.with(exports.RefSelectionsRenderService);f.useEffect(()=>{if(e&&d)if(n){const g=s.onCommandExecuted(m=>{if(m.id===B.SetWorksheetActiveOperation.id){const S=d.getSelectionControls().length;for(let u=1;u<=S;u++)d.clearLastSelection();setTimeout(()=>{i()},30)}});return()=>{g.dispose()}}else{const g=s.beforeCommandExecuted(m=>{if(m.id===B.SetWorksheetActiveOperation.id){o(!1),r(),i();const S=a.getEditor(c.DOCS_NORMAL_EDITOR_UNIT_ID_KEY);S==null||S.focus()}});return()=>{g.dispose()}}},[e,d])},an=e=>!e.some(n=>{if(typeof n=="string"){if(n!==M.matchToken.COMMA)return!0}else if(n.nodeType!==M.sequenceNodeType.REFERENCE)return!0;return!1}),Qn=(e,t,n)=>{const o=f.useRef(!0);f.useEffect(()=>{if(e){const r=setTimeout(()=>{o.current=!1},500);return()=>{clearTimeout(r)}}},[e]),f.useEffect(()=>{if(!o.current&&t){const r=an(n);t(r,Ie(n))}},[n,t])},er="univer-sheet-range-selector-text-wrap",tr="univer-sheet-range-selector-placeholder",nr="univer-sheet-range-selector-error",rr="univer-sheet-range-selector-error-wrap",or="univer-sheet-range-selector-text",sr="univer-sheet-range-selector-active",ir="univer-sheet-range-selector-icon",cr="univer-sheet-range-selector-dialog-item",ar="univer-sheet-range-selector-dialog-item-delete",Re={sheetRangeSelectorTextWrap:er,sheetRangeSelectorPlaceholder:tr,sheetRangeSelectorError:nr,sheetRangeSelectorErrorWrap:rr,sheetRangeSelectorText:or,sheetRangeSelectorActive:sr,sheetRangeSelectorIcon:ir,sheetRangeSelectorDialogItem:cr,sheetRangeSelectorDialogItemDelete:ar},Ue=()=>{};function ln(e){const{initValue:t,unitId:n,subUnitId:o,errorText:r,placeholder:i,actions:s,onChange:a=Ue,onVerify:l=Ue,onRangeSelectorDialogVisibleChange:h=Ue,onBlur:d=Ue,onFocus:g=Ue,isFocus:m=!0,isOnlyOneRange:S=!1,isSupportAcrossSheet:u=!1}=e,p=I.useEvent(a),R=c.useDependency(te.IEditorService),b=c.useDependency(c.LocaleService),C=c.useDependency(c.ICommandService),E=c.useDependency(M.LexerTreeBuilder),T=f.useRef(null),[O,D]=f.useState(!1),[A,L]=f.useState(m),k=f.useMemo(()=>c.createInternalEditorID(`${$.RANGE_SELECTOR_SYMBOLS}-${c.generateRandomId(4)}`),[]),x=f.useRef(),N=x.current,w=f.useRef(null),y=c.useDependency(c.IUniverInstanceService),_=f.useMemo(()=>!O&&A,[O,A]),[F,P]=f.useState(()=>typeof t=="string"?t:_e(t,u).join(M.matchToken.COMMA)),j=f.useMemo(()=>y.getCurrentTypeOfUnit$(c.UniverInstanceType.UNIVER_DOC),[y]),Y=c.useObservable(j),G=(Y==null?void 0:Y.getUnitId())===k,K=f.useRef([]),V=I.useEvent((U,H)=>{T.current&&!O&&!T.current.contains(U.target)&&H()});s&&(s.handleOutClick=V);const ne=f.useMemo(()=>F.split(M.matchToken.COMMA).filter(U=>!!U).map(U=>M.deserializeRangeWithSheet(U)),[F]),ie=f.useMemo(()=>r!==void 0,[r]),ee=sn(!O&&A,n,o);f.useMemo(()=>U=>{const H=E.sequenceNodesBuilder(U);if(H){if(an(H)){const X=H.map(oe=>{if(typeof oe=="string")return oe;if(oe.nodeType===M.sequenceNodeType.REFERENCE){const Le={...oe},Ee=M.deserializeRangeWithSheet(oe.token);return Ee.range=Mt(Ee.range),u||(Ee.sheetName="",Ee.unitId=""),Le.token=_e([Ee],u)[0],Le}return oe}),J=Ie(X);p(J)}}else P("")},[u]);const W=Jt(N),{checkScrollBar:ge}=te.useResize(N,!0,!0),de=ct(),fe=f.useMemo(()=>de(F),[F]),re=en(),Ce=Tt(n,o),ce=I.useEvent((U,H=!0,Z=!0)=>{if(!x.current)return;const X=de(U),J=re(x.current,X,H);K.current=J,Z&&Ce(J,x.current)});f.useEffect(()=>{const U=C.onCommandExecuted(H=>{var Z,X;if(H.id===It.RichTextEditingMutation.id){const J=H.params,{unitId:oe}=J;oe===k&&p(c.BuildTextUtils.transform.getPlainText((X=(Z=N==null?void 0:N.getDocumentData().body)==null?void 0:Z.dataStream)!=null?X:""))}});return()=>U.dispose()},[C,N,k,p]);const pe=I.useEvent((U,H,Z)=>{ce(U),P(U),Z&&(W(),H!==-1&&setTimeout(()=>{const X={startOffset:H,endOffset:H};N==null||N.setSelectionRanges([X]);const J=N==null?void 0:N.render.with(te.DocBackScrollRenderController);J==null||J.scrollToRange({...X,collapsed:!0})},50),ge())});cn(_,n,o,fe,u,S,pe),bt(_,_&&G,n),on(n,S),Kn(n,F,N),Qn(_,l,fe),rn(_,0,N),Zn(),Nt(_,n,u,L,d,()=>{_&&ce(F)}),f.useEffect(()=>{if(N){const U=N.input$.pipe(q.throttleTime(100)).subscribe(H=>{var X,J;const Z=((J=(X=H.data.body)==null?void 0:X.dataStream)!=null?J:"").replaceAll(/\n|\r/g,"").replaceAll(/,{2,}/g,",").replaceAll(/(^,)/g,"");ce(Z,!1),P(Z)});return()=>{U.unsubscribe()}}},[N]),f.useEffect(()=>{const U=C.onCommandExecuted(H=>{H.id===$.SetCellEditVisibleOperation.id&&(D(!1),h(!1),L(!1),d())});return()=>{U.dispose()}},[u]),f.useLayoutEffect(()=>{let U;if(w.current){U=R.register({autofocus:!0,editorUnitId:k,initialSnapshot:{id:k,body:{dataStream:`${F}\r
- `,textRuns:[]},documentStyle:{}}},w.current);const H=R.getEditor(k);x.current=H,ce(F,!1,!1)}return()=>{U==null||U.dispose()}},[]),f.useLayoutEffect(()=>{m?(L(m),W()):(N==null||N.blur(),ee(),L(m))},[m,W]),qn(F,"",A,re,Ce,N);const he=()=>{g(),W(),L(!0)},Se=U=>{const H=_e(U,u).join(M.matchToken.COMMA);H||N==null||N.setDocumentData({...N.getDocumentData(),body:{dataStream:`\r
- `,textRuns:[]}}),ce(H),P(H),D(!1),h(!1),setTimeout(()=>{L(!0),N==null||N.setSelectionRanges([{startOffset:H.length,endOffset:H.length}]),W(),ge()},30)},be=()=>{D(!1),h(!1),setTimeout(()=>{L(!0),W()},30)},Me=()=>{ie||(W(),setTimeout(()=>{D(!0),h(!0),L(!1)},30))};return v.jsxs("div",{className:Re.sheetRangeSelector,ref:T,children:[v.jsxs("div",{className:Ct(Re.sheetRangeSelectorTextWrap,{[Re.sheetRangeSelectorActive]:A&&!ie,[Re.sheetRangeSelectorError]:ie}),children:[v.jsx("div",{className:Re.sheetRangeSelectorText,ref:w,onMouseUp:he}),v.jsx(me.Tooltip,{title:b.t("rangeSelector.buttonTooltip"),placement:"bottom",children:v.jsx(Zt,{className:Re.sheetRangeSelectorIcon,onClick:Me})}),r!==void 0?v.jsx("div",{className:Re.sheetRangeSelectorErrorWrap,children:r}):null,i!==void 0&&!F?v.jsx("div",{className:Re.sheetRangeSelectorPlaceholder,children:i}):null]}),O&&v.jsx(lr,{editorId:k,handleConfirm:Se,handleClose:be,unitId:n,subUnitId:o,initValue:ne,visible:O,isOnlyOneRange:S,isSupportAcrossSheet:u})]})}function lr(e){const{handleConfirm:t,handleClose:n,visible:o,initValue:r,unitId:i,subUnitId:s,isOnlyOneRange:a,isSupportAcrossSheet:l}=e,h=c.useDependency(c.LocaleService),d=c.useDependency(Fe.IDescriptionService),g=c.useDependency(M.LexerTreeBuilder),S=c.useDependency(z.IRenderManagerService).getRenderById(i),u=S==null?void 0:S.with(exports.RefSelectionsRenderService),[p,R]=f.useState(()=>{if(a){const _=r[0];return _?_e([_],l):[""]}return _e(r,l)}),[b,C]=f.useState(()=>p.length-1),E=tn(),T=f.useMemo(()=>p.join(M.matchToken.COMMA),[p]),O=ct(),D=f.useMemo(()=>O(T),[T]),A=f.useMemo(()=>nn(d,E,D).refSelections,[D]),L=()=>{R([]),setTimeout(()=>{n()},30)},k=(_,F)=>{F?u==null||u.setSkipLastEnabled(!1):u==null||u.setSkipLastEnabled(!0),R(P=>{const j=[...P];return j[_]=F,j})},x=_=>{u==null||u.setSkipLastEnabled(!1),R(F=>{if(F.length===1)return F;const P=[];return F.forEach((j,Y)=>{_!==Y&&P.push(j)}),P})},N=()=>{u==null||u.setSkipLastEnabled(!0),R(_=>(_.push(""),C(_.length-1),[..._]))},w=I.useEvent(_=>{var P;u==null||u.setSkipLastEnabled(!1);const F=_.split(M.matchToken.COMMA).filter(j=>!!j);R(a?[(P=F[0])!=null?P:""]:F)}),y=Tt(i,s);return cn(b>=0,i,s,D,l,a,w),bt(b>=0,b>=0,i),on(i,a),Nt(b>=0,i,l,q.noop,q.noop,()=>y(A)),f.useEffect(()=>{y(A)},[A]),f.useEffect(()=>{(p.length===0||p.length===1&&!p[0])&&(u==null||u.setSkipLastEnabled(!0))},[p]),v.jsx(me.Dialog,{width:"328px",visible:o,title:h.t("rangeSelector.title"),draggable:!0,closeIcon:v.jsx(it,{}),footer:v.jsxs("footer",{children:[v.jsx(me.Button,{onClick:L,children:h.t("rangeSelector.cancel")}),v.jsx(me.Button,{style:{marginLeft:10},onClick:()=>t(p.filter(_=>{const F=g.sequenceNodesBuilder(_);return F&&F.length===1&&typeof F[0]!="string"&&F[0].nodeType===M.sequenceNodeType.REFERENCE}).map(_=>M.deserializeRangeWithSheet(_)).map(_=>({..._,range:Mt(_.range)}))),type:"primary",children:h.t("rangeSelector.confirm")})]}),onClose:L,children:v.jsxs("div",{className:Re.sheetRangeSelectorDialog,children:[p.map((_,F)=>v.jsxs("div",{className:Re.sheetRangeSelectorDialogItem,children:[v.jsx(me.Input,{affixWrapperStyle:{width:"100%"},placeholder:h.t("rangeSelector.placeHolder"),onFocus:()=>C(F),value:_,onChange:P=>k(F,P)},`input_${F}`),p.length>1&&!a&&v.jsx(Yt,{className:Re.sheetRangeSelectorDialogItemDelete,onClick:()=>x(F)})]},`${_}_${F}`)),!a&&v.jsx("div",{children:v.jsxs(me.Button,{type:"link",size:"small",onClick:N,children:[v.jsx(Gt,{}),v.jsx("span",{children:h.t("rangeSelector.addAnotherRange")})]})})]})})}function ur(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 xt(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 fr=(e,t=100)=>{f.useEffect(()=>{let n=null;const o=()=>{n===null&&(n=window.setTimeout(()=>{e(),n=null},t))};return window.addEventListener("scroll",o),window.addEventListener("resize",o),()=>{n!==null&&clearTimeout(n),window.removeEventListener("scroll",o),window.removeEventListener("resize",o)}},[e,t])};function un(e,t,n){const o=c.useDependency(te.IEditorService),r=f.useMemo(()=>new q.BehaviorSubject({left:-999,top:-999,right:-999,bottom:-999}),[]),i=c.useDependency(I.ISidebarService),s=c.useDependency(c.IUniverInstanceService),a=I.useEvent(()=>{const l=o.getEditor(e);if(!l)return;const h=l.getBoundingClientRect(),{left:d,top:g,right:m,bottom:S}=h,u=r.getValue();if(!(u.left===d&&u.top===g&&u.right===m&&u.bottom===S))return r.next({left:d-1,right:m+1,top:g-1,bottom:S+1}),h});return f.useEffect(()=>{t&&a()},[e,o,s.unitAdded$,a,t,...n!=null?n:[]]),fr(a),f.useEffect(()=>{const l=i.scrollEvent$.pipe(q.throttleTime(100)).subscribe(a);return()=>{l.unsubscribe()}},[]),[r,a]}const Ye=e=>{const t=f.useRef(e);return t.current=e,t},dr=(e,t,n)=>{const o=c.useDependency(Fe.IDescriptionService),r=c.useDependency(M.LexerTreeBuilder),[i,s]=f.useState(),[a,l]=f.useState(-1),[h,d]=f.useState(!0),g=Ye(h),m=f.useRef(t);m.current=t;const S=()=>{s(void 0),l(-1),d(!1)};return f.useEffect(()=>{if(n&&e){const u=n.selectionChange$.pipe(q.debounceTime(50)).subscribe(R=>{if(R.textRanges.length===1){const[b]=R.textRanges;if(b.collapsed&&g.current){const C=r.getFunctionAndParameter(`${m.current}A`,b.startOffset-1);if(C){const{functionName:E,paramIndex:T}=C,O=o.getFunctionInfo(E);s(O),l(T);return}}}s(void 0),l(-1)}),p=n.selectionChange$.pipe(q.filter(R=>R.isEditing),q.filter(R=>R.textRanges.length===1),q.map(R=>R.textRanges[0].startOffset),q.distinctUntilChanged()).subscribe(()=>{d(!0)});return()=>{u.unsubscribe(),p.unsubscribe()}}},[n,e]),f.useEffect(()=>{e||S()},[e]),{functionInfo:i,paramIndex:a,reset:S}},hr="univer-formula-help-function",mr="univer-formula-help-function-title",gr="univer-formula-help-function-title-icons",pr="univer-formula-help-function-title-icon",Sr="univer-formula-help-function-content",vr="univer-formula-help-function-content-inner",Cr="univer-formula-help-function-content-params",Rr="univer-formula-help-function-content-params-title",_r="univer-formula-help-function-content-params-detail",Ir="univer-formula-help-function-active",Er="univer-formula-help-decorator",yr="univer-formula-help-param",Tr="univer-formula-help-param-prefix",br="univer-formula-help-param-item",Mr="univer-formula-help-param-active",ue={formulaHelpFunction:hr,formulaHelpFunctionTitle:mr,formulaHelpFunctionTitleIcons:gr,formulaHelpFunctionTitleIcon:pr,formulaHelpFunctionContent:Sr,formulaHelpFunctionContentInner:vr,formulaHelpFunctionContentParams:Cr,formulaHelpFunctionContentParamsTitle:Rr,formulaHelpFunctionContentParamsDetail:_r,formulaHelpFunctionActive:Ir,formulaHelpDecorator:Er,formulaHelpParam:yr,formulaHelpParamPrefix:Tr,formulaHelpParamItem:br,formulaHelpParamActive:Mr},dt=({className:e,title:t,value:n})=>v.jsxs("div",{className:ue.formulaHelpFunctionContentParams,children:[v.jsx("div",{className:`
- ${ue.formulaHelpFunctionContentParamsTitle}
- ${e}
- `,children:t}),v.jsx("div",{className:ue.formulaHelpFunctionContentParamsDetail,children:n})]}),Nr=e=>{const{prefix:t,value:n,active:o,onClick:r}=e;return v.jsxs("div",{className:ue.formulaHelpParam,children:[v.jsxs("span",{className:ue.formulaHelpParamPrefix,children:[t,"("]}),n&&n.map((i,s)=>v.jsxs("span",{className:ue.formulaHelpParamItem,children:[v.jsx("span",{className:o===s?ue.formulaHelpFunctionActive:ue.formulaHelpParamActive,onClick:()=>r(s),children:xt(i)}),s===n.length-1?"":","]},i.name)),")"]})},Lt=()=>{};function xr(e){const{onParamsSwitch:t=Lt,onClose:n=Lt,isFocus:o,editor:r,formulaText:i}=e,{functionInfo:s,paramIndex:a,reset:l}=dr(o,i,r),h=f.useMemo(()=>!!s&&a>=0,[s,a]),[d,g]=f.useState(!0),m=c.useDependency(c.LocaleService),S=m.t("formula.prompt.required"),u=m.t("formula.prompt.optional"),p=r.getEditorId(),[R]=un(p,h,[s,a]);function b(E){t&&t(E)}const C=()=>{l(),n()};return h&&s?v.jsx(I.RectPopup,{portal:!0,onClickOutside:()=>l(),anchorRect$:R,direction:"vertical",children:v.jsxs("div",{className:ue.formulaHelpFunction,children:[v.jsxs("div",{className:ue.formulaHelpFunctionTitle,children:[v.jsx(Nr,{prefix:s.functionName,value:s.functionParameter,active:a,onClick:b}),v.jsxs("div",{className:ue.formulaHelpFunctionTitleIcons,children:[v.jsx("div",{className:ue.formulaHelpFunctionTitleIcon,style:{transform:d?"rotateZ(-90deg)":"rotateZ(90deg)"},onClick:()=>g(!d),children:v.jsx(yt,{})}),v.jsx("div",{className:ue.formulaHelpFunctionTitleIcon,onClick:C,children:v.jsx(it,{})})]})]}),v.jsx("div",{className:ue.formulaHelpFunctionContent,style:{height:d?"unset":0,padding:d?"revert-layer":0},children:v.jsxs("div",{className:ue.formulaHelpFunctionContentInner,children:[v.jsx(dt,{title:m.t("formula.prompt.helpExample"),value:`${s.functionName}(${s.functionParameter.map(E=>E.example).join(",")})`}),v.jsx(dt,{title:m.t("formula.prompt.helpAbstract"),value:s.description}),s&&s.functionParameter&&s.functionParameter.map((E,T)=>v.jsx(dt,{className:a===T?ue.formulaHelpFunctionActive:"",title:E.name,value:`${E.require?S:u} ${E.detail}`},T))]})})]})}):null}const fn=(e,t,n=!0)=>{let o=-1;return e.reduce((r,i,s)=>{if(r.isFinish)return r;const a=r.currentIndex;if(typeof i!="string")r.currentIndex+=i.token.length;else{const l=i.length;r.currentIndex+=l}return(n?r.currentIndex===t:t>a&&t<=r.currentIndex)&&(o=s,r.isFinish=!0),r},{currentIndex:0,isFinish:!1}),o},Ge=e=>e.reduce((t,n)=>typeof n=="string"?t+n.length:t+n.token.length,0),Fr=()=>{},Or=(e,t,n,o,r,i,s,a,l,h,d=Fr)=>{const g=c.useDependency(z.IRenderManagerService),m=c.useDependency(c.IUniverInstanceService),S=c.useDependency(c.ICommandService),u=Ye(i),p=c.useDependency(It.DocSelectionManagerService),R=c.useDependency(c.ThemeService),b=m.getUnit(o),C=I.useEvent(y=>{var _,F;return(F=(_=b==null?void 0:b.getSheetBySheetId(y))==null?void 0:_.getName())!=null?F:""}),E=f.useMemo(()=>C(r),[C,r]),T=c.useObservable(b==null?void 0:b.activeSheet$),O=Ye({activeSheet:T,sheetName:E}),D=g.getRenderById(o),A=D==null?void 0:D.with(exports.RefSelectionsRenderService),L=D==null?void 0:D.with($.SheetSkeletonManagerService),k=c.useDependency(B.IRefSelectionsService),x=f.useRef(!1),N=f.useRef(n);N.current=n;const w=f.useRef();f.useEffect(()=>{if(A&&e){let y=!0;const _=(P,j)=>{var ie,ee,W,ge,de,fe;if(y){y=!1;return}const Y=h==null?void 0:h.getSelectionRanges();if((Y==null?void 0:Y.length)!==1)return;const K=Y[0].startOffset-1,V=[...u.current],ne=fn(V,K,!1);if(N.current===Xe.NEED_ADD)if(K!==0){if(ne===-1&&V.length)return;const re=P[P.length-1],Ce=V.splice(ne+1),ce=(ie=re.rangeWithCoord.sheetId)!=null?ie:r,pe={range:re.rangeWithCoord,unitId:(ee=re.rangeWithCoord.unitId)!=null?ee:o,sheetName:C(ce)},Se=_e([pe],a&&ce!==r,E);V.push({token:Se[0],nodeType:M.sequenceNodeType.REFERENCE});const be=[...V,...Ce],Me=Ie(be);d(Me,Ge(V),j)}else{const re=P[P.length-1],Ce=(W=re.rangeWithCoord.sheetId)!=null?W:r,ce={range:re.rangeWithCoord,unitId:(ge=re.rangeWithCoord.unitId)!=null?ge:o,sheetName:C(Ce)},he=_e([ce],a&&Ce!==r);V.unshift({token:he[0],nodeType:M.sequenceNodeType.REFERENCE});const Se=Ie(V);d(Se,he[0].length,j)}else{let re=0;const Ce=V.map(U=>{var H,Z,X;if(typeof U=="string")return U;if(U.nodeType===M.sequenceNodeType.REFERENCE){const J=M.deserializeRangeWithSheet(U.token);if(J.sheetName||(J.sheetName=E),a&&((H=O.current.activeSheet)==null?void 0:H.getName())!==J.sheetName)return U.token;const oe=P[re];if(re++,!oe)return"";const Le=(Z=oe.rangeWithCoord.sheetId)!=null?Z:r,Ee={range:oe.rangeWithCoord,unitId:(X=oe.rangeWithCoord.unitId)!=null?X:o,sheetName:C(Le)};return _e([Ee],a,E)[0]}return U.token});let ce="",pe;Ce.forEach((U,H)=>{ce+=U,H===ne&&(pe=ce.length)});const he=[];for(let U=re;U<=P.length-1;U++){const H=P[U],Z=(de=H.rangeWithCoord.sheetId)!=null?de:r,X={range:H.rangeWithCoord,unitId:(fe=H.rangeWithCoord.unitId)!=null?fe:o,sheetName:C(Z)},oe=_e([X],a&&Z!==r,E);he.push(oe[0])}const Se=V[V.length-1],be=Se&&(typeof Se=="string"?!1:Se.nodeType===M.sequenceNodeType.REFERENCE),Me=`${ce}${he.length&&be?",":""}${he.join(",")}`;d(Me,!he.length&&pe?pe:Me.length,j)}},F=new c.DisposableCollection;return F.add(A.selectionMoving$.subscribe(P=>{x.current||_(P,!1)})),F.add(A.selectionMoveEnd$.subscribe(P=>{x.current||_(P,!0)})),()=>{F.dispose()}}},[A,h,a,e,u,r,o,C,E,d,O]),f.useEffect(()=>{if(t&&A&&h){const y=new c.DisposableCollection,_=(j,Y)=>{let G=0,K=0,V=!1;const{sheetName:ne}=O.current,ie=u.current.map(W=>{var ge;if(typeof W=="string")return V||(K+=W.length),W;if(W.nodeType===M.sequenceNodeType.REFERENCE){const de=M.deserializeRangeWithSheet(W.token);if(de.unitId||(de.unitId=o),de.sheetName||(de.sheetName=ne),a&&((ge=O.current.activeSheet)==null?void 0:ge.getName())!==de.sheetName)return V||(K+=W.token.length),W;if(G===Y){V=!0;const fe={...W,token:j};return a&&de.sheetName!==ne?fe.token=M.serializeRangeWithSheet(de.sheetName,M.deserializeRangeWithSheet(j).range):fe.token=j,K+=fe.token.length,G++,fe}return V||(K+=W.token.length),G++,W}return V||(K+=W.token.length),W}),ee=Ie(ie);d(ee,-1,!0),w.current={result:ee,offset:K}},F=()=>{y.dispose(),A.getSelectionControls().forEach((Y,G)=>{y.add(q.merge(Y.selectionMoving$,Y.selectionScaling$).pipe(Ke.map(K=>M.serializeRange(K)),Ke.distinctUntilChanged(),Ke.debounceTime(100)).subscribe(K=>{x.current=!0,_(K,G)}))}),y.add(A.selectionMoveEnd$.subscribe(Y=>{if(x.current=!1,w.current){const{result:G,offset:K}=w.current;d(G,K||-1,!0),w.current=void 0}}))},P=q.merge(h.input$,k.selectionSet$,A.selectionMoveEnd$).pipe(Ke.debounceTime(50)).subscribe(()=>{F()});return()=>{P.unsubscribe(),y.dispose()}}},[t,A,h,k.selectionSet$,O,u,d,a,o]),f.useEffect(()=>{if(l){const y=S.onCommandExecuted(_=>{if(_.id!==B.SetSelectionsOperation.id)return;const F=_.params;if(F.extra!=="formula-editor")return;const{selections:P}=F;if(P.length){const j=P[P.length-1];if(j){const Y=j.range,G=r,K={range:Y,unitId:F.unitId===o?"":F.unitId,sheetName:F.subUnitId===G?"":C(G)},V=[...u.current],ie=_e([K],a,E)[0];let ee=V[V.length-1];if(typeof ee=="object"&&ee.nodeType===M.sequenceNodeType.REFERENCE){ee={...ee},ee.token=ie,ee.endIndex=ee.startIndex+ie.length,V[V.length-1]=ee;const W=Ie(V);d(W,Ge(V),!0)}else{const W=Ge(V);V.push({nodeType:M.sequenceNodeType.REFERENCE,token:ie,startIndex:W,endIndex:W+ie.length});const ge=Ie(V);d(ge,Ge(V),!0)}}}});return()=>{y.dispose()}}},[S,C,d,a,l,u,E,r,o]),f.useEffect(()=>{if(!h)return;const y=p.textSelection$.subscribe(_=>{_.unitId===h.getEditorId()&&Qt({unitId:o,subUnitId:r,refSelections:s.current,editor:h,refSelectionsService:k,refSelectionsRenderService:A,sheetSkeletonManagerService:L,themeService:R,univerInstanceService:m})});return()=>y.unsubscribe()},[p.textSelection$,h,s,A,k,u,L,r,R,o,m])},Dr=(e,t,n)=>{const o=c.useDependency(M.LexerTreeBuilder),r=f.useRef(!0);f.useEffect(()=>{if(e){const i=setTimeout(()=>{r.current=!1},500);return()=>{clearTimeout(i)}}},[e]),f.useEffect(()=>{if(!r.current&&t){const i=o.checkIfAddBracket(n);t(i===0&&n.startsWith(M.operatorToken.EQUALS),`${n}`)}},[n,t])},Ar="univer-sheet-embedding-formula-editor-active",Pr="univer-sheet-embedding-formula-editor-wrap",Lr="univer-sheet-embedding-formula-editor-text",wr="univer-sheet-embedding-formula-editor-error",kr="univer-sheet-embedding-formula-editor",$r="univer-sheet-embedding-formula-editor-error-wrap",je={sheetEmbeddingFormulaEditorActive:Ar,sheetEmbeddingFormulaEditorWrap:Pr,sheetEmbeddingFormulaEditorText:Lr,sheetEmbeddingFormulaEditorError:wr,sheetEmbeddingFormulaEditor:kr,sheetEmbeddingFormulaEditorErrorWrap:$r},jr=(e,t=[],n)=>{const o=c.useDependency(Fe.IDescriptionService),[r,i]=f.useState([]),[s,a]=f.useState(""),l=f.useRef(-1),h=Ye({nodes:t}),d=()=>{i([]),a(""),l.current=-1};return f.useEffect(()=>{if(n&&e){const m=n.input$.pipe(q.debounceTime(300)).subscribe(()=>{const S=n.getSelectionRanges();if(S.length===1){const u=h.current.nodes,p=S[0];if(p.collapsed){const R=fn(u,p.startOffset-1,!1);l.current=R;const b=u[R];if(b&&typeof b!="string"&&b.nodeType===M.sequenceNodeType.FUNCTION){l.current=R;const C=b.token,E=o.getSearchListByNameFirstLetter(C);i(E),a(C);return}}}l.current=-1,a(""),i(u=>u!=null&&u.length?[]:u)});return()=>{m.unsubscribe()}}},[n,e]),f.useEffect(()=>{e||d()},[e]),{searchList:r,searchText:s,handlerFormulaReplace:m=>{const S=[...h.current.nodes];if(l.current!==-1){const u=S.splice(l.current+1),p=S.pop()||"";let R=(typeof p=="string"?p.length:p.token.length)-m.length;return S.push(m),u[0]!==M.matchToken.OPEN_BRACKET&&(S.push(M.matchToken.OPEN_BRACKET),R--),{text:Ie([...S,...u]),offset:R}}},reset:d}},Hr="univer-formula-search-function",Ur="univer-formula-search-function-item",Wr="univer-formula-search-function-item-name",Br="univer-formula-search-function-item-name-light",Vr="univer-formula-search-function-item-desc",Kr="univer-formula-search-function-item-active",Ne={formulaSearchFunction:Hr,formulaSearchFunctionItem:Ur,formulaSearchFunctionItemName:Wr,formulaSearchFunctionItemNameLight:Br,formulaSearchFunctionItemDesc:Vr,formulaSearchFunctionItemActive:Kr},qr=()=>{},Yr=f.forwardRef(zr);function zr(e,t){const{isFocus:n,sequenceNodes:o,onSelect:r,editor:i,onClose:s=qr}=e,a=i.getEditorId(),l=c.useDependency(I.IShortcutService),h=c.useDependency(c.ICommandService),{searchList:d,searchText:g,handlerFormulaReplace:m,reset:S}=jr(n,o,i),u=f.useMemo(()=>!!d.length,[d]),p=f.useRef(),[R,b]=f.useState(0),C=f.useRef(!1),[E]=un(a,u,[g,d]),T=Ye({searchList:d,active:R}),O=x=>{const N=m(x);N&&(S(),r(N))};function D(x){C.current&&b(x)}function A(){C.current&&b(-1)}f.useEffect(()=>{if(!d.length)return;const x=`sheet.formula-embedding-editor.search_function.${a}`,N=new c.DisposableCollection,w=y=>{const{searchList:_,active:F}=T.current;switch(y){case I.KeyCode.ARROW_UP:{b(P=>{const j=Math.max(0,P-1);return L(j),j});break}case I.KeyCode.ARROW_DOWN:{b(P=>{const j=Math.min(_.length-1,P+1);return L(j),j});break}case I.KeyCode.TAB:case I.KeyCode.ENTER:{const P=_[F];O(P.name);break}case I.KeyCode.ESC:{S(),s();break}}};return N.add(h.registerCommand({id:x,type:c.CommandType.OPERATION,handler(y,_){const{keyCode:F}=_;w(F)}})),[I.KeyCode.ARROW_UP,I.KeyCode.ARROW_DOWN,I.KeyCode.ENTER,I.KeyCode.ESC,I.KeyCode.TAB].map(y=>({id:x,binding:y,preconditions:()=>!0,priority:1e3,staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keyCode:y}})).forEach(y=>{N.add(l.registerShortcut(y))}),()=>{N.dispose()}},[d]);function L(x){var K;const N=(K=p.current)==null?void 0:K.querySelectorAll(`.${Ne.formulaSearchFunctionItem}`)[x];if(!N)return;const w=N.parentNode;if(!w)return;const _=w.getBoundingClientRect().top,F=w.offsetHeight,P=N.getBoundingClientRect(),j=P.top,Y=P.height;if(j>=0&&j>_&&j-_+Y<=F)return;const G=N.offsetTop-(F-Y)/2;w.scrollTo({top:G,behavior:"smooth"})}const k=f.useMemo(()=>{let x="";return()=>{clearTimeout(x),C.current=!0,x=setTimeout(()=>{C.current=!1},300)}},[]);return d.length>0&&u&&v.jsx(I.RectPopup,{portal:!0,anchorRect$:E,direction:"vertical",children:v.jsx("ul",{className:Ne.formulaSearchFunction,ref:x=>{p.current=x,t&&(t.current=x)},children:d.map((x,N)=>v.jsxs("li",{className:R===N?`
- ${Ne.formulaSearchFunctionItem}
- ${Ne.formulaSearchFunctionItemActive}
- `:Ne.formulaSearchFunctionItem,onMouseEnter:()=>D(N),onMouseLeave:A,onMouseMove:k,onClick:()=>{O(x.name),i&&i.focus()},children:[v.jsxs("span",{className:Ne.formulaSearchFunctionItemName,children:[v.jsx("span",{className:Ne.formulaSearchFunctionItemNameLight,children:x.name.substring(0,g.length)}),v.jsx("span",{children:x.name.slice(g.length)})]}),v.jsx("span",{className:Ne.formulaSearchFunctionItemDesc,children:x.desc})]},x.name))})})}const Gr=e=>e.startsWith(M.operatorToken.EQUALS)?e.slice(1):"",ht=()=>{};function dn(e){var lt,Ft;const{errorText:t,initValue:n,unitId:o,subUnitId:r,isFocus:i=!0,isSupportAcrossSheet:s=!1,onFocus:a=ht,onBlur:l=ht,onChange:h,onVerify:d,actions:g,className:m,editorId:S,moveCursor:u=!0,onFormulaSelectingChange:p,keyboradEventConfig:R,onMoveInEditor:b,resetSelectionOnBlur:C=!0,autoScrollbar:E=!0,isSingle:T=!0,disableSelectionOnClick:O=!1,disableContextMenu:D}=e,A=c.useDependency(te.IEditorService),L=f.useRef(null),k=I.useEvent(h);g&&(g.handleOutClick=(ae,le)=>{L.current&&!L.current.contains(ae.target)&&le()});const x=I.useEvent(p),N=f.useRef(null),w=f.useRef(),y=w.current,[_,F]=f.useState(i),P=f.useRef(null),j=f.useMemo(()=>S!=null?S:c.createInternalEditorID(`${$.EMBEDDING_FORMULA_EDITOR}-${c.generateRandomId(4)}`),[]),Y=f.useMemo(()=>t!==void 0,[t]),G=c.useDependency(c.IUniverInstanceService),K=G.getUnit(j);c.useObservable(K==null?void 0:K.change$);const V=ct(),ne=c.BuildTextUtils.transform.getPlainText((Ft=(lt=K==null?void 0:K.getBody())==null?void 0:lt.dataStream)!=null?Ft:""),ie=f.useMemo(()=>Gr(ne),[ne]),ee=f.useMemo(()=>V(ie),[ie,V]),{isSelecting:W}=Gn(j,_,O),ge=f.useRef(""),fe=c.useDependency(z.IRenderManagerService).getRenderById(j),re=fe==null?void 0:fe.with(te.DocSelectionRenderService),Ce=re==null?void 0:re.isFocusing,ce=f.useMemo(()=>G.getCurrentTypeOfUnit$(c.UniverInstanceType.UNIVER_DOC),[G]),pe=c.useObservable(ce),he=(pe==null?void 0:pe.getUnitId())===j,Se=f.useRef([]),be=W;I.useUpdateEffect(()=>{k(ne)},[ne,k]);const Me=en("="),U=Tt(o,r),H=I.useEvent((ae,le=!0,ye,we)=>{if(!w.current)return;ge.current=ae;const ut=V(ae[0]==="="?ae.slice(1):""),ke=Me(w.current,ut,le,we);Se.current=ke,ye&&U(_?ke:[],w.current)});f.useEffect(()=>{_&&H(ne,!1,!0)},[ne,_,H]),Dr(_,d,ne);const Z=Jt(y),X=sn(_,o,r);f.useEffect(()=>{x(W)},[x,W]),te.useKeyboardEvent(_,R,y),f.useLayoutEffect(()=>{let ae;if(P.current){ae=A.register({autofocus:!0,editorUnitId:j,initialSnapshot:{id:j,body:{dataStream:`${n}\r
- `,textRuns:[],customBlocks:[],customDecorations:[],customRanges:[]},documentStyle:{}}},P.current);const le=A.getEditor(j);w.current=le,H(n,!1,!0)}return()=>{ae==null||ae.dispose()}},[]),f.useLayoutEffect(()=>{i?(F(i),Z()):(C&&(y==null||y.blur(),X()),F(i))},[i,y,Z,X,C]);const{checkScrollBar:J}=te.useResize(y,T,E);bt(_,!!(W&&he),o,D),rn(_&&u,be,y,b);const oe=I.useEvent((ae,le,ye)=>{if(!Ce)return;const we=le!==-1?[{startOffset:le+1,endOffset:le+1,collapsed:!0}]:void 0;H(`=${ae}`,!0,ye,we),ye&&(Z(),le!==-1&&setTimeout(()=>{const ut={startOffset:le+1,endOffset:le+1},ke=y==null?void 0:y.render.with(te.DocBackScrollRenderController);ke==null||ke.scrollToRange({...ut,collapsed:!0})},50),J())});Or(_&&!!(W&&he),_,W,o,r,ee,Se,s,!!be,y,oe),Nt(_&&!!(W&&he),o,s,F,l,ht);const Le=ae=>{if(ae){const le=y==null?void 0:y.getSelectionRanges();if(le&&le.length===1){const ye=le[0];if(ye.collapsed){const we=ae.offset;setTimeout(()=>{y==null||y.setSelectionRanges([{startOffset:ye.startOffset-we,endOffset:ye.endOffset-we}])},30)}}Z(),H(`=${ae.text}`)}},Ee=()=>{F(!0),a(),Z()};return v.jsxs("div",{className:Ct(je.sheetEmbeddingFormulaEditor,m),children:[v.jsx("div",{className:Ct(je.sheetEmbeddingFormulaEditorWrap,{[je.sheetEmbeddingFormulaEditorActive]:_,[je.sheetEmbeddingFormulaEditorError]:Y}),ref:L,children:v.jsx("div",{className:je.sheetEmbeddingFormulaEditorText,ref:P,onMouseUp:Ee})}),t!==void 0?v.jsx("div",{className:je.sheetEmbeddingFormulaEditorErrorWrap,children:t}):null,y?v.jsx(xr,{editor:y,isFocus:_,formulaText:ne,onClose:()=>Z()}):null,y?v.jsx(Yr,{isFocus:_,sequenceNodes:ee,onSelect:Le,ref:N,editor:y}):null]})}const Zr="sheets-formula-ui.base.config",wt={};function Xr(e){return e.getContextValue(c.FOCUSING_DOC)&&e.getContextValue(c.FOCUSING_UNIVER_EDITOR)}function hn(e,t=!1){return!t&&!(c.isFormulaString(e==null?void 0:e.f)||c.isFormulaId(e==null?void 0:e.si))?null:typeof(e==null?void 0:e.v)=="string"&&M.ERROR_TYPE_SET.has(e.v)?e.v:null}var Jr=Object.defineProperty,Qr=Object.getOwnPropertyDescriptor,eo=(e,t,n,o)=>{for(var r=o>1?void 0:o?Qr(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&Jr(t,n,r),r},We=(e,t)=>(n,o)=>t(n,o,e);const mt="SHEET_FORMULA_ALERT",to={[M.ErrorType.DIV_BY_ZERO]:"divByZero",[M.ErrorType.NAME]:"name",[M.ErrorType.VALUE]:"value",[M.ErrorType.NUM]:"num",[M.ErrorType.NA]:"na",[M.ErrorType.CYCLE]:"cycle",[M.ErrorType.REF]:"ref",[M.ErrorType.SPILL]:"spill",[M.ErrorType.CALC]:"calc",[M.ErrorType.ERROR]:"error",[M.ErrorType.CONNECT]:"connect",[M.ErrorType.NULL]:"null"};let _t=class extends c.Disposable{constructor(e,t,n,o,r,i){super(),this._context=e,this._hoverManagerService=t,this._cellAlertManagerService=n,this._localeService=o,this._formulaDataModel=r,this._zenZoneService=i,this._init()}_init(){this._initCellAlertPopup(),this._initZenService()}_initCellAlertPopup(){this.disposeWithMe(this._hoverManagerService.currentCell$.pipe(q.debounceTime(100)).subscribe(e=>{var t,n,o,r,i;if(e){const a=this._context.unit.getActiveSheet();if(!a)return;const l=a.getCell(e.location.row,e.location.col),h=(r=(o=(n=(t=this._formulaDataModel.getArrayFormulaCellData())==null?void 0:t[e.location.unitId])==null?void 0:n[e.location.subUnitId])==null?void 0:o[e.location.row])==null?void 0:r[e.location.col];if(c.isICellData(l)){const d=hn(l,!!h);if(!d){this._hideAlert();return}const g=this._cellAlertManagerService.currentAlert.get(mt),m=(i=g==null?void 0:g.alert)==null?void 0:i.location;if(m&&m.row===e.location.row&&m.col===e.location.col&&m.subUnitId===e.location.subUnitId&&m.unitId===e.location.unitId)return;this._cellAlertManagerService.showAlert({type:$.CellAlertType.ERROR,title:this._localeService.t("formula.error.title"),message:this._localeService.t(`formula.error.${to[d]}`),location:e.location,width:200,height:74,key:mt});return}}this._hideAlert()}))}_initZenService(){this.disposeWithMe(this._zenZoneService.visible$.subscribe(e=>{e&&this._hideAlert()}))}_hideAlert(){this._cellAlertManagerService.removeAlert(mt)}};_t=eo([We(1,c.Inject($.HoverManagerService)),We(2,c.Inject($.CellAlertManagerService)),We(3,c.Inject(c.LocaleService)),We(4,c.Inject(M.FormulaDataModel)),We(5,I.IZenZoneService)],_t);var no=Object.defineProperty,ro=Object.getOwnPropertyDescriptor,oo=(e,t,n,o)=>{for(var r=o>1?void 0:o?ro(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&no(t,n,r),r},kt=(e,t)=>(n,o)=>t(n,o,e);let Je=class extends c.Disposable{constructor(e,t){super(),this._autoFillService=e,this._lexerTreeBuilder=t,this._registerAutoFill()}_registerAutoFill(){const e={type:$.DATA_TYPE.FORMULA,priority:1001,match:t=>c.isFormulaString(t==null?void 0:t.f)||c.isFormulaId(t==null?void 0:t.si),isContinue:(t,n)=>t.type===$.DATA_TYPE.FORMULA,applyFunctions:{[$.APPLY_TYPE.COPY]:(t,n,o,r)=>{const{data:i,index:s}=t;return this._fillCopyFormula(i,n,o,s,r)}}};this._autoFillService.registerRule(e)}_fillCopyFormula(e,t,n,o,r){var l,h;const i=io(r),s=[],a=new Map;for(let d=1;d<=t;d++){const g=(d-1)%e.length,m=c.Tools.deepClone(e[g]);if(m){const S=((l=e[g])==null?void 0:l.f)||"",u=((h=e[g])==null?void 0:h.si)||"",p=c.isFormulaString(S);if(c.isFormulaId(u))m.si=u,m.f=null,m.v=null,m.p=null,m.t=null,s.push(m);else if(p){let b=a.get(g);if(b)m.si=b,m.f=null,m.v=null,m.p=null,m.t=null;else{b=c.Tools.generateRandomId(6),a.set(g,b);const{offsetX:C,offsetY:E}=so(i,t,n),T=this._lexerTreeBuilder.moveFormulaRefOffset(S,C,E);m.si=b,m.f=T,m.v=null,m.p=null,m.t=null}s.push(m)}}}return s}};Je=oo([kt(0,$.IAutoFillService),kt(1,c.Inject(M.LexerTreeBuilder))],Je);function so(e,t,n){let o=0,r=0;switch(n){case c.Direction.UP:r=-e*t;break;case c.Direction.RIGHT:o=e;break;case c.Direction.DOWN:r=e;break;case c.Direction.LEFT:o=-e*t;break}return{offsetX:o,offsetY:r}}function io(e){let t=0;for(const n in e)e[n].forEach(o=>{t+=o.data.length});return t}var co=Object.defineProperty,ao=Object.getOwnPropertyDescriptor,lo=(e,t,n,o)=>{for(var r=o>1?void 0:o?ao(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&co(t,n,r),r},Be=(e,t)=>(n,o)=>t(n,o,e);const uo="default-paste-formula";let Qe=class extends c.Disposable{constructor(e,t,n,o,r){super(),this._currentUniverSheet=e,this._lexerTreeBuilder=t,this._sheetClipboardService=n,this._injector=o,this._formulaDataModel=r,this._initialize()}_initialize(){this._registerClipboardHook()}_registerClipboardHook(){this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteFormulaHook())),this.disposeWithMe(this._sheetClipboardService.addClipboardHook(this._pasteWithFormulaHook()))}_pasteFormulaHook(){return{id:$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA,priority:10,specialPasteInfo:{label:"specialPaste.formula"},onPasteCells:(e,t,n,o)=>this._onPasteCells(e,t,n,o,!0)}}_pasteWithFormulaHook(){return{id:uo,priority:10,onPasteCells:(e,t,n,o)=>this._onPasteCells(e,t,n,o,!1)}}_onPasteCells(e,t,n,o,r){var m;if([$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMAT,$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_COL_WIDTH].includes(o.pasteType))return{undos:[],redos:[]};const s=this._currentUniverSheet.getCurrentUnitForType(c.UniverInstanceType.UNIVER_SHEET),a=t.unitId||s.getUnitId(),l=t.subUnitId||((m=s.getActiveSheet())==null?void 0:m.getSheetId());if(!a||!l)return{undos:[],redos:[]};const h=t.range,d=n,g={copyType:o.copyType||$.COPY_TYPE.COPY,copyRange:e==null?void 0:e.range,pasteType:o.pasteType};return this._injector.invoke(S=>fo(a,l,h,d,S,g,this._lexerTreeBuilder,this._formulaDataModel,r,e))}};Qe=lo([Be(0,c.IUniverInstanceService),Be(1,c.Inject(M.LexerTreeBuilder)),Be(2,$.ISheetClipboardService),Be(3,c.Inject(c.Injector)),Be(4,c.Inject(M.FormulaDataModel))],Qe);function fo(e,t,n,o,r,i,s,a,l=!1,h){const d=[],g=[],m=ho(e,t,n,o,i,s,a,h),S={unitId:e,subUnitId:t,cellValue:m.getData()};d.push({id:B.SetRangeValuesMutation.id,params:S});const u=B.SetRangeValuesUndoMutationFactory(r,S);return g.push({id:B.SetRangeValuesMutation.id,params:u}),{undos:g,redos:d}}function ho(e,t,n,o,r,i,s,a){return a?r.pasteType===$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_VALUE?go(e,t,n,o,s,a):r.pasteType===$.PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA?po(e,t,n,o,i,s,a):So(e,t,n,o,i,s,a):mo(e,t,n,o,s)}function mo(e,t,n,o,r){const i=new c.ObjectMatrix,s=r.getSheetFormulaData(e,t);return o.forValue((a,l,h)=>{var S;const d=n.rows[a],g=n.cols[l],m={};c.isFormulaString(h.v)?(m.v=null,m.f=`${h.v}`,m.si=null,m.p=null,i.setValue(d,g,m)):(S=s==null?void 0:s[d])!=null&&S[g]&&(m.v=h.v,m.f=null,m.si=null,m.p=null,i.setValue(d,g,m))}),i}function go(e,t,n,o,r,i){var h,d;const s=new c.ObjectMatrix,a=(d=(h=r.getArrayFormulaCellData())==null?void 0:h[i.unitId])==null?void 0:d[i.subUnitId],l=r.getSheetFormulaData(e,t);return o.forValue((g,m,S)=>{var E,T;const u=i.range.rows[g%i.range.rows.length],p=i.range.cols[m%i.range.cols.length],R=n.rows[g],b=n.cols[m],C={};if(c.isFormulaString(S.f)||c.isFormulaId(S.si))C.v=S.v,C.f=null,C.si=null,C.p=null,s.setValue(R,b,C);else if((E=a==null?void 0:a[u])!=null&&E[p]){const O=a[u][p];C.v=O.v,C.f=null,C.si=null,C.p=null,s.setValue(R,b,C)}else if((T=l==null?void 0:l[R])!=null&&T[b]){if(C.v=S.v,C.f=null,C.si=null,C.p=null,S.p){const O=mn(S);O&&(C.v=O)}s.setValue(R,b,C)}}),s}function po(e,t,n,o,r,i,s){const a=new c.ObjectMatrix,l=new Map;return o.forValue((h,d,g)=>{const m=n.rows[h],S=n.cols[d],u={};if(c.isFormulaId(g.si)){if(s.unitId!==e||s.subUnitId!==t){const p=i.getFormulaStringByCell(s.range.rows[h%s.range.rows.length],s.range.cols[d%s.range.cols.length],s.subUnitId,s.unitId),R=n.cols[d]-s.range.cols[d%s.range.cols.length],b=n.rows[h]-s.range.rows[h%s.range.rows.length],C=r.moveFormulaRefOffset(p||"",R,b);u.si=null,u.f=C}else u.si=g.si,u.f=null;u.v=null,u.p=null,a.setValue(m,S,u)}else if(c.isFormulaString(g.f)){const p=`${h%s.range.rows.length}_${d%s.range.cols.length}`;let R=l.get(p);if(R)u.si=R,u.f=null;else{R=c.Tools.generateRandomId(6),l.set(p,R);const b=n.cols[d]-s.range.cols[d%s.range.cols.length],C=n.rows[h]-s.range.rows[h%s.range.rows.length],E=r.moveFormulaRefOffset(g.f||"",b,C);u.si=R,u.f=E}u.v=null,u.p=null,a.setValue(m,S,u)}else{if(u.v=g.v,u.f=null,u.si=null,u.p=null,g.p){const p=mn(g);p&&(u.v=p)}a.setValue(m,S,u)}}),a}function So(e,t,n,o,r,i,s){const a=new c.ObjectMatrix,l=new Map,h=i.getSheetFormulaData(e,t);return o.forValue((d,g,m)=>{var R;const S=n.rows[d],u=n.cols[g],p={};if(c.isFormulaId(m.si)){if(s.unitId!==e||s.subUnitId!==t){const b=i.getFormulaStringByCell(s.range.rows[d%s.range.rows.length],s.range.cols[g%s.range.cols.length],s.subUnitId,s.unitId),C=n.cols[g]-s.range.cols[g%s.range.cols.length],E=n.rows[d]-s.range.rows[d%s.range.rows.length],T=r.moveFormulaRefOffset(b||"",C,E);p.si=null,p.f=T}else p.si=m.si,p.f=null;p.v=null,p.p=null,a.setValue(S,u,p)}else if(c.isFormulaString(m.f)){const b=`${d%s.range.rows.length}_${g%s.range.cols.length}`;let C=l.get(b);if(C)p.si=C,p.f=null;else{C=c.Tools.generateRandomId(6),l.set(b,C);const E=n.cols[g]-s.range.cols[g%s.range.cols.length],T=n.rows[d]-s.range.rows[d%s.range.rows.length],O=r.moveFormulaRefOffset(m.f||"",E,T);p.si=C,p.f=O}p.v=null,p.p=null,a.setValue(S,u,p)}else(R=h==null?void 0:h[S])!=null&&R[u]&&(p.v=m.v,p.f=null,p.si=null,p.p=m.p,a.setValue(S,u,p))}),a}function mn(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)===c.DEFAULT_EMPTY_DOCUMENT_VALUE?n.substring(0,n.length-2):n}}var vo=Object.defineProperty,Co=Object.getOwnPropertyDescriptor,Ro=(e,t,n,o)=>{for(var r=o>1?void 0:o?Co(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&vo(t,n,r),r},De=(e,t)=>(n,o)=>t(n,o,e);let et=class extends c.Disposable{constructor(t,n,o,r,i,s,a,l){super();Q(this,"_previousShape");Q(this,"_skeleton");this._context=t,this._sheetInterceptorService=n,this._formulaDataModel=o,this._themeService=r,this._renderManagerService=i,this._sheetSkeletonManagerService=s,this._commandService=a,this._logService=l,this._initSkeletonChangeListener(),this._initInterceptorEditorStart(),this._commandExecutedListener()}_initSkeletonChangeListener(){this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(t=>{var n,o;if(t==null)this._logService.debug("[FormulaEditorShowController]: should not receive currentSkeleton$ as null!");else{const{skeleton:r}=t,i=(o=(n=this._skeleton)==null?void 0:n.worksheet)==null?void 0:o.getSheetId();if(this._changeRuntime(r),i!==r.worksheet.getSheetId())this._removeArrayFormulaRangeShape();else{const{unitId:s,sheetId:a}=t;this._updateArrayFormulaRangeShape(s,a)}}}))}_changeRuntime(t){this._skeleton=t}_initInterceptorEditorStart(){this.disposeWithMe(c.toDisposable(this._sheetInterceptorService.writeCellInterceptor.intercept(B.BEFORE_CELL_EDIT,{handler:(t,n,o)=>{var u,p,R,b;const{row:r,col:i,unitId:s,subUnitId:a,worksheet:l}=n,h=this._formulaDataModel.getArrayFormulaRange(),d=this._formulaDataModel.getArrayFormulaCellData();if(this._removeArrayFormulaRangeShape(),t==null)return o(t);let g=null;const m=this._formulaDataModel.getFormulaStringByCell(r,i,a,s);if(m!==null&&(g={f:m}),t.v!=null&&t.v!==""&&((R=(p=(u=d[s])==null?void 0:u[a])==null?void 0:p[r])==null?void 0:R[i])==null)return g?{...t,...g}:o(t);const S=(b=h==null?void 0:h[s])==null?void 0:b[a];return S!=null&&(g=this._displayArrayFormulaRangeShape(S,r,i,s,a,l,g)),g?{...t,...g}:o(t)}})))}_commandExecutedListener(){this.disposeWithMe(this._commandService.onCommandExecuted((t,n)=>{(t.id===M.SetFormulaCalculationResultMutation.id||t.id===M.SetArrayFormulaDataMutation.id&&n&&n.remove)&&this._removeArrayFormulaRangeShape()})),this.disposeWithMe(this._commandService.beforeCommandExecuted(t=>{B.SetWorksheetRowAutoHeightMutation.id===t.id&&requestIdleCallback(()=>{const n=t.params,{unitId:o,subUnitId:r,rowsAutoHeightInfo:i}=n;this._refreshArrayFormulaRangeShapeByRow(o,r,i)})}))}_displayArrayFormulaRangeShape(t,n,o,r,i,s,a){const l=this._formulaDataModel.getSheetFormulaData(r,i);return new c.ObjectMatrix(t).forValue((h,d,g)=>{var R;if(g==null)return!0;const{startRow:m,startColumn:S,endRow:u,endColumn:p}=g;if(h===n&&d===o)return this._createArrayFormulaRangeShape(g,r),!1;if(n>=m&&n<=u&&o>=S&&o<=p){const b=s.getCell(m,S);if((b==null?void 0:b.v)===M.ErrorType.SPILL)return;const C=(R=l==null?void 0:l[h])==null?void 0:R[d];return C==null||C.f==null?!0:(a==null&&(a={f:C.f,isInArrayFormulaRange:!0}),this._createArrayFormulaRangeShape(g,r),!1)}}),a}_createArrayFormulaRangeShape(t,n){const o=this._renderManagerService.getRenderById(n),r=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o||!r)return;const{scene:i}=o;if(!i)return;const s=this._themeService.getCurrentTheme(),a={range:t,primary:null,style:{strokeWidth:1,stroke:s.hyacinth700,fill:new c.ColorKit(s.colorWhite).setAlpha(0).toString(),widgets:{}}},l=$.attachSelectionWithCoord(a,r),{rowHeaderWidth:h,columnHeaderHeight:d}=r,g=new $.SelectionControl(i,$.SELECTION_SHAPE_DEPTH.FORMULA_EDITOR_SHOW,this._themeService,{highlightHeader:!1,rowHeaderWidth:h,columnHeaderHeight:d});g.updateRangeBySelectionWithCoord(l),g.setEvent(!1),this._previousShape=g}_removeArrayFormulaRangeShape(){this._previousShape!=null&&(this._previousShape.dispose(),this._previousShape=null)}_refreshArrayFormulaRangeShape(t,n){if(this._previousShape){const{startRow:o,endRow:r,startColumn:i,endColumn:s}=this._previousShape.getRange(),a={startRow:o,endRow:r,startColumn:i,endColumn:s};this._removeArrayFormulaRangeShape(),this._createArrayFormulaRangeShape(a,t)}}_checkCurrentSheet(t,n){const o=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!o)return!1;const r=o.worksheet;return r?r.unitId===t&&r.getSheetId()===n:!1}_updateArrayFormulaRangeShape(t,n){this._checkCurrentSheet(t,n)&&this._previousShape&&this._refreshArrayFormulaRangeShape(t)}_refreshArrayFormulaRangeShapeByRow(t,n,o){if(!this._checkCurrentSheet(t,n)||!this._previousShape)return;const{startRow:r,endRow:i,startColumn:s,endColumn:a}=this._previousShape.getRange();for(let l=0;l<o.length;l++){const{row:h}=o[l];if(r>=h){const d={startRow:r,endRow:i,startColumn:s,endColumn:a};this._refreshArrayFormulaRangeShape(t,d);break}}}};et=Ro([De(1,c.Inject(B.SheetInterceptorService)),De(2,c.Inject(M.FormulaDataModel)),De(3,c.Inject(c.ThemeService)),De(4,z.IRenderManagerService),De(5,c.Inject($.SheetSkeletonManagerService)),De(6,c.ICommandService),De(7,c.ILogService)],et);var _o=Object.defineProperty,Io=Object.getOwnPropertyDescriptor,Eo=(e,t,n,o)=>{for(var r=o>1?void 0:o?Io(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&_o(t,n,r),r},$t=(e,t)=>(n,o)=>t(n,o,e);const yo={tl:{size:6,color:"#409f11"}};let tt=class extends c.RxDisposable{constructor(e,t){super(),this._sheetInterceptorService=e,this._formulaDataModel=t,this.disposeWithMe(this._sheetInterceptorService.intercept(B.INTERCEPTOR_POINT.CELL_CONTENT,{effect:c.InterceptorEffectEnum.Style,handler:(n,o,r)=>{var a,l,h,d;const i=(d=(h=(l=(a=this._formulaDataModel.getArrayFormulaCellData())==null?void 0:a[o.unitId])==null?void 0:l[o.subUnitId])==null?void 0:h[o.row])==null?void 0:d[o.col];return hn(n,!!i)?r({...n,markers:{...n==null?void 0:n.markers,...yo}}):r(n)},priority:10}))}};tt=Eo([$t(0,c.Inject(B.SheetInterceptorService)),$t(1,c.Inject(M.FormulaDataModel))],tt);function To(){const e=c.useDependency(Fe.TriggerCalculationController),t=c.useDependency(c.ICommandService),n=c.useObservable(e.progress$),o=f.useCallback(()=>{t.executeCommand(M.SetFormulaCalculationStopMutation.id)},[t]),r=f.useCallback(()=>{e.clearProgress()},[e]);return v.jsx(I.ProgressBar,{progress:n,onTerminate:o,onClearProgress:r})}const bo=(e,t=100)=>{f.useEffect(()=>{let n=null;const o=()=>{n===null&&(n=window.setTimeout(()=>{e(),n=null},t))};return window.addEventListener("scroll",o),window.addEventListener("resize",o),()=>{n!==null&&clearTimeout(n),window.removeEventListener("scroll",o),window.removeEventListener("resize",o)}},[e,t])},Mo="univer-formula-help-function",No="univer-formula-help-function-title",xo="univer-formula-help-function-title-icons",Fo="univer-formula-help-function-title-icon",Oo="univer-formula-help-function-content",Do="univer-formula-help-function-content-inner",Ao="univer-formula-help-function-content-params",Po="univer-formula-help-function-content-params-title",Lo="univer-formula-help-function-content-params-detail",wo="univer-formula-help-function-active",ko="univer-formula-help-decorator",$o="univer-formula-help-param",jo="univer-formula-help-param-prefix",Ho="univer-formula-help-param-item",Uo="univer-formula-help-param-active",se={formulaHelpFunction:Mo,formulaHelpFunctionTitle:No,formulaHelpFunctionTitleIcons:xo,formulaHelpFunctionTitleIcon:Fo,formulaHelpFunctionContent:Oo,formulaHelpFunctionContentInner:Do,formulaHelpFunctionContentParams:Ao,formulaHelpFunctionContentParamsTitle:Po,formulaHelpFunctionContentParamsDetail:Lo,formulaHelpFunctionActive:wo,formulaHelpDecorator:ko,formulaHelpParam:$o,formulaHelpParamPrefix:jo,formulaHelpParamItem:Ho,formulaHelpParamActive:Uo};function Wo(){const[e,t]=f.useState(!1),[n,o]=f.useState(!0),[r,i]=f.useState(!0),[s,a]=f.useState(0),[l,h]=f.useState([0,0]),[d,g]=f.useState({left:0,top:0}),[m,S]=f.useState(null),u=c.useDependency(ze),p=c.useDependency(c.LocaleService),R=p.t("formula.prompt.required"),b=p.t("formula.prompt.optional"),C=c.useDependency(c.IUniverInstanceService),E=c.useDependency(te.IEditorService),T=c.useDependency(I.ISidebarService),O=c.useInjector();bo(D),f.useEffect(()=>{const x=u.help$.subscribe(w=>{const{visible:y,paramIndex:_,functionInfo:F}=w;if(!y){t(y);return}const P=A();if(P==null)return;const{left:j,top:Y,height:G}=P;F.description===""&&F.functionParameter.length===0||(h([j,Y+G]),a(_),S(F),g({left:j,top:Y}),t(y))}),N=T.scrollEvent$.pipe(q.throttleTime(100)).subscribe(D);return()=>{x==null||x.unsubscribe(),N.unsubscribe()}},[]);function D(){if(!r)return;const x=A();if(x==null)return;const{left:N,top:w,height:y}=x;h([N,w+y])}function A(){const x=C.getCurrentUniverDocInstance();if(!x)return;const N=x.getUnitId();if(!E.isEditor(N))return;const w=E.getEditor(N);return w==null?void 0:w.getBoundingClientRect()}function L(x){a(x)}function k(){i(!r),O.get(I.ILayoutService).focus()}return v.jsx(v.Fragment,{children:r?v.jsx(me.Popup,{visible:e,offset:l,children:m?v.jsxs("div",{className:se.formulaHelpFunction,children:[v.jsxs("div",{className:se.formulaHelpFunctionTitle,children:[v.jsx(Bo,{prefix:m.functionName,value:m.functionParameter,active:s,onClick:L}),v.jsxs("div",{className:se.formulaHelpFunctionTitleIcons,children:[v.jsx("div",{className:se.formulaHelpFunctionTitleIcon,style:{transform:n?"rotateZ(-90deg)":"rotateZ(90deg)"},onClick:()=>o(!n),children:v.jsx(yt,{})}),v.jsx("div",{className:se.formulaHelpFunctionTitleIcon,onClick:k,children:v.jsx(it,{})})]})]}),v.jsx("div",{className:se.formulaHelpFunctionContent,style:{height:n?"unset":0,padding:n?"revert-layer":0},children:v.jsxs("div",{className:se.formulaHelpFunctionContentInner,children:[v.jsx(gt,{title:p.t("formula.prompt.helpExample"),value:`${m.functionName}(${m.functionParameter.map(x=>x.example).join(",")})`}),v.jsx(gt,{title:p.t("formula.prompt.helpAbstract"),value:m.description}),m&&m.functionParameter&&m.functionParameter.map((x,N)=>v.jsx(gt,{className:s===N?se.formulaHelpFunctionActive:"",title:x.name,value:`${x.require?R:b} ${x.detail}`},N))]})})]}):v.jsx(v.Fragment,{})}):e?v.jsx("div",{className:se.formulaHelpDecorator,onClick:()=>i(!r),style:{left:d.left-24,top:d.top},children:v.jsx(zt,{})}):v.jsx(v.Fragment,{})})}const gt=e=>v.jsxs("div",{className:se.formulaHelpFunctionContentParams,children:[v.jsx("div",{className:`
- ${se.formulaHelpFunctionContentParamsTitle}
- ${e.className}
- `,children:e.title}),v.jsx("div",{className:se.formulaHelpFunctionContentParamsDetail,children:e.value})]}),Bo=e=>{const{prefix:t,value:n,active:o,onClick:r}=e;return v.jsxs("div",{className:se.formulaHelpParam,children:[v.jsxs("span",{className:se.formulaHelpParamPrefix,children:[t,"("]}),n&&n.map((i,s)=>v.jsxs("span",{className:se.formulaHelpParamItem,children:[v.jsx("span",{className:o===s?se.formulaHelpFunctionActive:se.formulaHelpParamActive,onClick:()=>r(s),children:xt(i)}),s===n.length-1?"":","]},s)),")"]})},Vo="univer-formula-search-function",Ko="univer-formula-search-function-item",qo="univer-formula-search-function-item-name",Yo="univer-formula-search-function-item-name-light",zo="univer-formula-search-function-item-desc",Go="univer-formula-search-function-item-active",xe={formulaSearchFunction:Vo,formulaSearchFunctionItem:Ko,formulaSearchFunctionItemName:qo,formulaSearchFunctionItemNameLight:Yo,formulaSearchFunctionItemDesc:zo,formulaSearchFunctionItemActive:Go};function Zo(){const[e,t]=f.useState(!1),[n,o]=f.useState(0),[r,i]=f.useState([0,0]),[s,a]=f.useState([]),[l,h]=f.useState(""),d=f.useRef(null),g=c.useDependency(ze),m=c.useDependency(c.IUniverInstanceService),S=c.useDependency(te.IEditorService);f.useEffect(()=>{let C=[],E=0;const T=g.search$.subscribe(A=>{const{visible:L,searchText:k,searchList:x}=A;if(!L){t(L);return}const N=u();if(N==null)return;const{left:w,top:y,height:_}=N;h(k),a(x),C=x,i([w,y+_]),t(L),o(0),E=0}),O=g.navigate$.subscribe(A=>{const{direction:L}=A;if(L===c.Direction.UP){let k=E-1;k=k<0?C.length-1:k,o(k),E=k}else if(L===c.Direction.DOWN){let k=E+1;k=k>=C.length?0:k,o(k),E=k}b(E)}),D=g.accept$.subscribe(A=>{const L=C[E].name;g.acceptFormulaName(L)});return()=>{T==null||T.unsubscribe(),O==null||O.unsubscribe(),D==null||D.unsubscribe()}},[]);function u(){const E=m.getCurrentUniverDocInstance().getUnitId();if(!S.isEditor(E))return;const T=S.getEditor(E);return T==null?void 0:T.getBoundingClientRect()}function p(C){o(C)}function R(){o(-1)}function b(C){var w;const E=(w=d.current)==null?void 0:w.querySelectorAll(`.${xe.formulaSearchFunctionItem}`)[C];if(!E)return;const T=E.parentNode;if(!T)return;const D=T.getBoundingClientRect().top,A=T.offsetHeight,L=E.getBoundingClientRect(),k=L.top,x=L.height;if(k>=0&&k>D&&k-D+x<=A)return;const N=E.offsetTop-(A-x)/2;T.scrollTo({top:N,behavior:"smooth"})}return s.length>0&&v.jsx(me.Popup,{visible:e,offset:r,children:v.jsx("ul",{className:xe.formulaSearchFunction,ref:d,children:s.map((C,E)=>v.jsxs("li",{className:n===E?`
- ${xe.formulaSearchFunctionItem}
- ${xe.formulaSearchFunctionItemActive}
- `:xe.formulaSearchFunctionItem,onMouseEnter:()=>p(E),onMouseLeave:R,onClick:()=>g.acceptFormulaName(C.name),children:[v.jsxs("span",{className:xe.formulaSearchFunctionItemName,children:[v.jsx("span",{className:xe.formulaSearchFunctionItemNameLight,children:C.name.substring(0,l.length)}),v.jsx("span",{children:C.name.slice(l.length)})]}),v.jsx("span",{className:xe.formulaSearchFunctionItemDesc,children:C.desc})]},E))})})}function Xo(){return v.jsxs(v.Fragment,{children:[v.jsx(Zo,{}),v.jsx(Wo,{})]})}const Jo="univer-formula-more-functions",Qo="univer-formula-more-functions-operation",jt={formulaMoreFunctions:Jo,formulaMoreFunctionsOperation:Qo};function gn(e){const{prefix:t,value:n}=e;return v.jsxs("div",{children:[v.jsxs("span",{children:[t,"("]}),n&&n.map((o,r)=>v.jsxs("span",{children:[v.jsx("span",{children:xt(o)}),r===n.length-1?"":","]},r)),")"]})}const es="univer-formula-function-params",ts="univer-formula-function-params-title",ns="univer-formula-function-params-detail",pt={formulaFunctionParams:es,formulaFunctionParamsTitle:ts,formulaFunctionParamsDetail:ns};function qe(e){const{className:t,value:n,title:o}=e;return v.jsxs("div",{className:pt.formulaFunctionParams,children:[v.jsx("div",{className:`
- ${pt.formulaFunctionParamsTitle}
- ${t}
- `,children:o}),v.jsx("div",{className:pt.formulaFunctionParamsDetail,children:n})]})}const rs="univer-formula-input-params-list",os="univer-formula-input-params-list-item-name",ss="univer-formula-input-params-list-item-selector",Ve={formulaInputParamsList:rs,formulaInputParamsListItemName:os,formulaInputParamsListItemSelector:ss};function is(e){const{functionInfo:t,onChange:n}=e;if(!t)return null;f.useState([]);const[o,r]=f.useState(t.functionParameter),[i,s]=f.useState(-1);return v.jsxs("div",{className:Ve.formulaInputParams,children:[v.jsx("div",{className:Ve.formulaInputParamsList,children:o.map((a,l)=>v.jsxs("div",{children:[v.jsx("div",{className:Ve.formulaInputParamsListItemName,children:a.name}),v.jsx("div",{className:Ve.formulaInputParamsListItemSelector})]},l))}),v.jsx("div",{className:Ve.formulaInputParamsInfo,children:v.jsx(qe,{title:i===-1?v.jsx(gn,{prefix:t.functionName,value:o}):o[i].name,value:i===-1?t.description:o[i].detail})})]})}const cs="univer-formula-select-function-select",as="univer-formula-select-function-result",ls="univer-formula-select-function-result-item",us="univer-formula-select-function-result-item-name-light",fs="univer-formula-select-function-result-item-selected",ds="univer-formula-select-function-result-item-active",hs="univer-formula-select-function-content",Te={formulaSelectFunctionSelect:cs,formulaSelectFunctionResult:as,formulaSelectFunctionResultItem:ls,formulaSelectFunctionResultItemNameLight:us,formulaSelectFunctionResultItemSelected:fs,formulaSelectFunctionResultItemActive:ds,formulaSelectFunctionContent:hs};function ms(e){const{onChange:t}=e,n="-1",[o,r]=f.useState(""),[i,s]=f.useState([]),[a,l]=f.useState(0),[h,d]=f.useState(n),[g,m]=f.useState(0),[S,u]=f.useState(null),p=c.useDependency(Fe.IDescriptionService),R=c.useDependency(c.LocaleService),b=c.useDependency(I.ISidebarService),C=c.useObservable(b.sidebarOptions$),E=ur(M.FunctionType,R);E.unshift({label:R.t("formula.moreFunctions.allFunctions"),value:n});const T=R.t("formula.prompt.required"),O=R.t("formula.prompt.optional");f.useEffect(()=>{L(n)},[]),f.useEffect(()=>{A(0)},[i]),f.useEffect(()=>{C!=null&&C.visible&&(r(""),s([]),l(0),d(n),m(0),u(null),L(n))},[C]);const D=y=>{if(o.trim()==="")return y;const _=new RegExp(`(${o.toLocaleUpperCase()})`);return y.split(_).filter(Boolean).map((P,j)=>P.match(_)?v.jsx("span",{className:Te.formulaSelectFunctionResultItemNameLight,children:P},j):P)},A=y=>{if(i.length===0){u(null);return}m(y);const _=p.getFunctionInfo(i[y].name);if(!_){u(null);return}u(_),t(_)};function L(y){d(y);const _=p.getSearchListByType(+y);s(_)}function k(y){r(y);const _=p.getSearchListByName(y);s(_)}function x(y){if(y.stopPropagation(),y.key==="ArrowDown"){const _=a+1;l(_===i.length?0:_)}else if(y.key==="ArrowUp"){const _=a-1;l(_===-1?i.length-1:_)}else y.key==="Enter"&&A(a)}const N=y=>{l(y)},w=()=>{l(-1)};return v.jsxs("div",{children:[v.jsxs("div",{className:Te.formulaSelectFunctionSelect,children:[v.jsx(me.Select,{value:h,options:E,onChange:L}),v.jsx(me.Input,{placeholder:R.t("formula.moreFunctions.searchFunctionPlaceholder"),onKeyDown:x,value:o,onChange:k,size:"large",allowClear:!0})]}),v.jsx("ul",{className:Te.formulaSelectFunctionResult,onKeyDown:x,tabIndex:-1,children:i.map(({name:y},_)=>v.jsxs("li",{className:a===_?`
- ${Te.formulaSelectFunctionResultItem}
- ${Te.formulaSelectFunctionResultItemActive}
- `:Te.formulaSelectFunctionResultItem,onMouseEnter:()=>N(_),onMouseLeave:w,onClick:()=>A(_),children:[g===_&&v.jsx(qt,{className:Te.formulaSelectFunctionResultItemSelected}),v.jsx("span",{className:Te.formulaSelectFunctionResultItemName,children:D(y)})]},_))}),S&&v.jsxs("div",{className:Te.formulaSelectFunctionContent,children:[v.jsx(qe,{title:S.functionName,value:S.description}),v.jsx(qe,{title:R.t("formula.moreFunctions.syntax"),value:v.jsx(gn,{prefix:S.functionName,value:S.functionParameter})}),v.jsx(qe,{title:R.t("formula.prompt.helpExample"),value:`${S.functionName}(${S.functionParameter.map(y=>y.example).join(",")})`}),S.functionParameter&&S.functionParameter.map((y,_)=>v.jsx(qe,{title:y.name,value:`${y.require?T:O} ${y.detail}`},_))]})]})}function gs(){const e=$.useActiveWorkbook(),[t,n]=f.useState(!0),[o,r]=f.useState(!1),[i,s]=f.useState(null),a=c.useDependency($.IEditorBridgeService),l=c.useDependency(c.LocaleService),h=c.useDependency(te.IEditorService),d=c.useDependency(c.IUniverInstanceService);function g(){n(!t),r(!o)}function m(){const S=B.getSheetCommandTarget(d);if(!S)return;a.changeVisible({visible:!0,unitId:S.unitId,eventType:z.DeviceInputEventType.Dblclick});const u=h.getEditor(c.DOCS_NORMAL_EDITOR_UNIT_ID_KEY),p=h.getEditor(c.DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY),R=`=${i==null?void 0:i.functionName}(`;u==null||u.replaceText(R),p==null||p.replaceText(R,!1)}return v.jsxs("div",{className:jt.formulaMoreFunctions,children:[t&&v.jsx(ms,{onChange:s}),o&&v.jsx(is,{functionInfo:i,onChange:()=>{}}),v.jsxs("div",{className:jt.formulaMoreFunctionsOperation,children:[o&&v.jsx(me.Button,{type:"primary",size:"small",onClick:g,children:l.t("formula.moreFunctions.next")}),o&&v.jsx(me.Button,{size:"small",onClick:g,children:l.t("formula.moreFunctions.prev")}),t&&!!e&&v.jsx(me.Button,{type:"primary",size:"small",onClick:m,children:l.t("formula.moreFunctions.confirm")})]})]})}function ps(e){return{id:ot.id,icon:"FunctionSingle",tooltip:"formula.insert.tooltip",type:I.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$:I.getMenuHiddenObservable(e,c.UniverInstanceType.UNIVER_SHEET),disabled$:$.getCurrentRangeDisable$(e,{workbookTypes:[B.WorkbookEditablePermission],worksheetTypes:[B.WorksheetEditPermission,B.WorksheetSetCellValuePermission],rangeTypes:[B.RangeProtectionPermissionEditPoint]})}}function Ss(e){return{id:st.id,title:"formula.insert.more",type:I.MenuItemType.BUTTON}}function vs(e){return e.get(c.IUniverInstanceService).getCurrentTypeOfUnit$(c.UniverInstanceType.UNIVER_SHEET).pipe(q.switchMap(o=>o&&e.get(I.IClipboardInterfaceService)?new q.Observable(i=>i.next(!e.get(I.IClipboardInterfaceService).supportClipboard)):q.of(!0)))}function Cs(e){return{id:rt.id,type:I.MenuItemType.BUTTON,title:"formula.operation.pasteFormula",disabled$:vs(e).pipe(q.combineLatestWith($.getCurrentRangeDisable$(e,{workbookTypes:[B.WorkbookEditablePermission],rangeTypes:[B.RangeProtectionPermissionEditPoint],worksheetTypes:[B.WorksheetSetCellValuePermission,B.WorksheetEditPermission]})),q.map(([t,n])=>t||n))}}const Rs={[I.RibbonStartGroup.FORMULAS_INSERT]:{[ot.id]:{order:1,menuItemFactory:ps,[st.id]:{order:1,menuItemFactory:Ss}}},[$.PASTE_SPECIAL_MENU_ID]:{[rt.id]:{order:4,menuItemFactory:Cs}}},_s="meta_key_ctrl_And_Shift",at=[I.KeyCode.ARROW_DOWN,I.KeyCode.ARROW_UP,I.KeyCode.ARROW_LEFT,I.KeyCode.ARROW_RIGHT],Is=[...at,I.KeyCode.ENTER,I.KeyCode.TAB,I.KeyCode.ESC];function Es(){const e=[];for(const t of Is)e.push({id:Pe.id,binding:t,preconditions:n=>$.whenFormulaEditorActivated(n),staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keycode:t}});return e}function ys(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|I.MetaKeys.SHIFT,preconditions:n=>$.whenFormulaEditorActivated(n),staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keycode:t,metaKey:I.MetaKeys.SHIFT}});return e}function Ts(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|I.MetaKeys.CTRL_COMMAND,preconditions:n=>$.whenFormulaEditorActivated(n),staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keycode:t,metaKey:I.MetaKeys.CTRL_COMMAND}});return e}function bs(){const e=[];for(const t of at)e.push({id:Pe.id,binding:t|I.MetaKeys.SHIFT|I.MetaKeys.CTRL_COMMAND,preconditions:n=>$.whenFormulaEditorActivated(n),staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keycode:t,metaKey:_s}});return e}const Ms={id:Et.id,binding:I.KeyCode.F4,preconditions:e=>$.whenFormulaEditorActivated(e)};function Ns(){const e=[];for(const t of[I.KeyCode.ENTER,I.KeyCode.TAB,I.KeyCode.ARROW_DOWN,I.KeyCode.ARROW_UP])e.push({id:Pe.id,binding:t,preconditions:n=>Xr(n),staticParameters:{eventType:z.DeviceInputEventType.Keyboard,keycode:t,isSingleEditor:!0}});return e}var xs=Object.defineProperty,Fs=Object.getOwnPropertyDescriptor,Os=(e,t,n,o)=>{for(var r=o>1?void 0:o?Fs(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&xs(t,n,r),r},Ae=(e,t)=>(n,o)=>t(n,o,e);let nt=class extends c.Disposable{constructor(e,t,n,o,r,i,s){super(),this._injector=e,this._menuManagerService=t,this._commandService=n,this._shortcutService=o,this._uiPartsService=r,this._renderManagerService=i,this._componentManager=s,this._initialize()}_initialize(){this._registerCommands(),this._registerMenus(),this._registerShortcuts(),this._registerComponents(),this._registerRenderModules()}_registerMenus(){this._menuManagerService.mergeMenu(Rs)}_registerCommands(){[rt,ot,st,Vt,Ut,Pe,Et].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}_registerShortcuts(){[...Es(),...ys(),...Ts(),...bs(),...Ns(),Ms].forEach(e=>{this.disposeWithMe(this._shortcutService.registerShortcut(e))})}_registerComponents(){this.disposeWithMe(this._uiPartsService.registerComponent(I.BuiltInUIPart.CONTENT,()=>c.connectInjector(Xo,this._injector))),this.disposeWithMe(this._uiPartsService.registerComponent($.SheetsUIPart.FORMULA_AUX,()=>c.connectInjector(To,this._injector))),this._componentManager.register(Bt,gs)}_registerRenderModules(){this.disposeWithMe(this._renderManagerService.registerRenderModule(c.UniverInstanceType.UNIVER_SHEET,[et]))}};nt=Os([Ae(0,c.Inject(c.Injector)),Ae(1,I.IMenuManagerService),Ae(2,c.ICommandService),Ae(3,I.IShortcutService),Ae(4,I.IUIPartsService),Ae(5,z.IRenderManagerService),Ae(6,c.Inject(I.ComponentManager))],nt);var pn=Object.defineProperty,Ds=Object.getOwnPropertyDescriptor,As=(e,t,n)=>t in e?pn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Ps=(e,t,n,o)=>{for(var r=o>1?void 0:o?Ds(t,n):t,i=e.length-1,s;i>=0;i--)(s=e[i])&&(r=(o?s(t,n,r):s(r))||r);return o&&r&&pn(t,n,r),r},St=(e,t)=>(n,o)=>t(n,o,e),Sn=(e,t,n)=>As(e,typeof t!="symbol"?t+"":t,n);exports.UniverSheetsFormulaUIPlugin=class extends c.Plugin{constructor(t=wt,n,o,r){super(),this._config=t,this._injector=n,this._renderManagerService=o,this._configService=r;const{menu:i,...s}=c.merge(wt,this._config);i&&this._configService.setConfig("menu",i,{merge:!0}),this._configService.setConfig(Zr,s)}onStarting(){const t=this._injector;[[ze,{useClass:vt}],[nt],[Je],[Qe],[et],[tt]].forEach(r=>t.add(r));const o=this._injector.get(I.ComponentManager);o.register($.RANGE_SELECTOR_COMPONENT_KEY,ln),o.register($.EMBEDDING_FORMULA_EDITOR_COMPONENT_KEY,dn)}onRendered(){[[exports.RefSelectionsRenderService],[_t]].forEach(t=>{this.disposeWithMe(this._renderManagerService.registerRenderModule(c.UniverInstanceType.UNIVER_SHEET,t))}),c.touchDependencies(this._injector,[[nt],[Qe],[tt]])}onSteady(){this._injector.get(Je)}};Sn(exports.UniverSheetsFormulaUIPlugin,"pluginName",Wt);Sn(exports.UniverSheetsFormulaUIPlugin,"type",c.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFormulaUIPlugin=Ps([c.DependentOn(M.UniverFormulaEnginePlugin,Fe.UniverSheetsFormulaPlugin),St(1,c.Inject(c.Injector)),St(2,z.IRenderManagerService),St(3,c.IConfigService)],exports.UniverSheetsFormulaUIPlugin);exports.FORMULA_PROMPT_ACTIVATED=Ht;exports.FormulaEditor=dn;exports.HelpFunctionOperation=Ut;exports.InsertFunctionOperation=ot;exports.MoreFunctionsOperation=st;exports.RangeSelector=ln;exports.ReferenceAbsoluteOperation=Et;exports.SearchFunctionOperation=Vt;exports.SelectEditorFormulaOperation=Pe;exports.SheetOnlyPasteFormulaCommand=rt;
|