@univerjs/find-replace 0.10.12-nightly.202510221114 → 0.10.12-nightly.202510251119
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.js +1 -1
- package/lib/es/index.js +384 -366
- package/lib/index.js +384 -366
- package/lib/umd/index.js +1 -1
- package/package.json +6 -6
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Fe=Object.defineProperty;var xe=(t,e,n)=>e in t?Fe(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var u=(t,e,n)=>xe(t,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("@univerjs/core"),l=require("@univerjs/ui"),f=require("@univerjs/design"),De=require("@univerjs/engine-render"),_=require("rxjs"),d=require("react/jsx-runtime"),h=require("react"),q="FIND_REPLACE_INPUT_FOCUS",Z="FIND_REPLACE_DIALOG_FOCUS",z="FIND_REPLACE_REPLACE_REVEALED";var Te=Object.getOwnPropertyDescriptor,Y=(t,e,n,i)=>{for(var s=i>1?void 0:i?Te(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},T=(t,e)=>(n,i)=>e(n,i,t);class Oe extends c.Disposable{}const S=c.createIdentifier("find-replace.service");function Pe(t){return typeof t.findString<"u"||typeof t.inputtingFindString<"u"||typeof t.findDirection<"u"||typeof t.matchesTheWholeCell<"u"||typeof t.caseSensitive<"u"||typeof t.findScope<"u"||typeof t.findBy<"u"}exports.FindReplaceModel=class extends c.Disposable{constructor(n,i,s,r){super();u(this,"currentMatch$",new _.BehaviorSubject(null));u(this,"replaceables$",new _.BehaviorSubject([]));u(this,"_findModels",[]);u(this,"_matchingModel",null);u(this,"_matches",[]);u(this,"_currentSearchingDisposables",null);this._state=n,this._providers=i,this._univerInstanceService=s,this._commandService=r,this.disposeWithMe(this._state.stateUpdates$.pipe(_.throttleTime(200,void 0,{leading:!0,trailing:!0})).subscribe(async a=>{const o=this._state.state;Pe(a)&&(o.findString!==""&&!o.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):a.replaceRevealed!==!0&&this._stopSearching())}))}get searched(){return this._findModels.length>0}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...N(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};const n=await this._startSearching();return this._state.changeState({findCompleted:!0}),n}focusSelection(){var n;(n=this._matchingModel)==null||n.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};const n=Array.from(this._providers),i=this._findModels=(await Promise.all(n.map(r=>r.find({findString:this._state.findString,findDirection:this._state.findDirection,findScope:this._state.findScope,findBy:this._state.findBy,replaceRevealed:this._state.replaceRevealed,caseSensitive:this._state.caseSensitive,matchesTheWholeCell:this._state.matchesTheWholeCell})))).flat();this._subscribeToModelsChanges(i);const s=this._matches=i.map(r=>r.getMatches()).flat();return this.replaceables$.next(s.filter(r=>r.replaceable)),s.length?(this._moveToInitialMatch(i),this._state.changeState({matchesCount:s.length}),{results:s}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var n;this._providers.forEach(i=>i.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(n=this._currentSearchingDisposables)==null||n.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(n){const i=this._currentSearchingDisposables=new c.DisposableCollection,s=_.combineLatest(n.map(r=>r.matchesUpdate$)).pipe(_.debounceTime(220)).subscribe(([...r])=>{const a=this._matches=r.flat();a.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:a.length}),this.replaceables$.next(a.filter(o=>o.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});n.forEach(r=>i.add(c.toDisposable(r.activelyChangingMatch$.subscribe(a=>{const o=this._matches.findIndex(p=>p===a);this._state.changeState({matchesPosition:o+1})})))),i.add(c.toDisposable(s))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){const n=await Promise.all(this._findModels.map(i=>i.replaceAll(this._state.replaceString))).then(i=>i.reduce((s,r)=>(s.success+=r.success,s.failure+=r.failure,s),{success:0,failure:0}));return n.failure===0&&this._stopSearching(),n}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(n){const i=this._matches.findIndex(s=>s===n);this.currentMatch$.next(n),this._state.changeState({matchesPosition:i+1})}moveToNextMatch(){if(!this._matchingModel)return;const n=this._findModels.length===1,i=this._matchingModel.moveToNextMatch({loop:n});if(i)return this._markMatch(i),i;{const s=this._findModels.findIndex(r=>r===this._matchingModel);return this._moveToNextUnitMatch(s)}}_moveToNextUnitMatch(n){const i=this._findModels.length;for(let s=(n+1)%i;s!==n;){const r=this._findModels[s],a=r.moveToNextMatch({ignoreSelection:!0});if(a)return this._matchingModel=r,this._markMatch(a),a;s=(s+1)%i}if(this._matchingModel){const s=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return s&&this._markMatch(s),s}}moveToPreviousMatch(){if(!this._matchingModel)return;const n=this._findModels.length===1,i=this._matchingModel.moveToPreviousMatch({loop:n});if(i){const s=this._matches.findIndex(r=>r===i);return this.currentMatch$.next(i),this._state.changeState({matchesPosition:s+1}),i}else{const s=this._findModels.length,r=this._findModels.findIndex(o=>o===this._matchingModel);for(let o=(r-1+s)%s;o!==r;){const p=this._findModels[o],m=p.moveToPreviousMatch({ignoreSelection:!0});if(m)return this._matchingModel=p,this._markMatch(m),m;o=(o-1)%s}const a=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return a&&this._markMatch(a),a}}_moveToInitialMatch(n,i=!1){var a;const s=(a=this._univerInstanceService.getFocusedUnit())==null?void 0:a.getUnitId();if(!s)return-1;const r=n.findIndex(o=>o.unitId===s);if(r!==-1){this._matchingModel=n[r];const o=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:i});if(o)return this._markMatch(o),r}return this._moveToNextUnitMatch(r),0}};exports.FindReplaceModel=Y([T(2,c.IUniverInstanceService),T(3,c.ICommandService)],exports.FindReplaceModel);var O=(t=>(t.ROW="row",t.COLUMN="column",t))(O||{}),P=(t=>(t.VALUE="value",t.FORMULA="formula",t))(P||{}),E=(t=>(t.SUBUNIT="subunit",t.UNIT="unit",t))(E||{});function N(){return{caseSensitive:!1,findBy:"value",findCompleted:!1,findDirection:"row",findScope:"subunit",findString:"",inputtingFindString:"",matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:"",revealed:!0}}class J{constructor(){u(this,"_stateUpdates$",new _.Subject);u(this,"stateUpdates$",this._stateUpdates$.asObservable());u(this,"_state$",new _.BehaviorSubject(N()));u(this,"state$",this._state$.asObservable());u(this,"_findString","");u(this,"_inputtingFindString","");u(this,"_replaceString","");u(this,"_revealed",!1);u(this,"_replaceRevealed",!1);u(this,"_matchesPosition",0);u(this,"_matchesCount",0);u(this,"_caseSensitive",!0);u(this,"_matchesTheWholeCell",!1);u(this,"_findDirection","row");u(this,"_findScope","subunit");u(this,"_findBy","value");u(this,"_findCompleted",!1)}get state(){return this._state$.getValue()}get inputtingFindString(){return this._inputtingFindString}get findString(){return this._findString}get revealed(){return this._revealed}get replaceRevealed(){return this._replaceRevealed}get matchesPosition(){return this._matchesPosition}get matchesCount(){return this._matchesCount}get replaceString(){return this._replaceString}get caseSensitive(){return this._caseSensitive}get matchesTheWholeCell(){return this._matchesTheWholeCell}get findDirection(){return this._findDirection}get findScope(){return this._findScope}get findBy(){return this._findBy}get findCompleted(){return this._findCompleted}changeState(e){let n=!1;const i={};typeof e.findString<"u"&&e.findString!==this._findString&&(this._findString=e.findString,i.findString=this._findString,n=!0),typeof e.revealed<"u"&&e.revealed!==this._revealed&&(this._revealed=e.revealed,i.revealed=e.revealed,n=!0),typeof e.replaceRevealed<"u"&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,i.replaceRevealed=e.replaceRevealed,n=!0),typeof e.replaceString<"u"&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,i.replaceString=e.replaceString,n=!0),typeof e.matchesCount<"u"&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,i.matchesCount=e.matchesCount,n=!0),typeof e.matchesPosition<"u"&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,i.matchesPosition=e.matchesPosition,n=!0),typeof e.findBy<"u"&&e.findBy!==this._findBy&&(this._findBy=e.findBy,i.findBy=e.findBy,n=!0),typeof e.findScope<"u"&&e.findScope!==this._findScope&&(this._findScope=e.findScope,i.findScope=e.findScope,n=!0),typeof e.findDirection<"u"&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,i.findDirection=e.findDirection,n=!0),typeof e.caseSensitive<"u"&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,i.caseSensitive=e.caseSensitive,n=!0),typeof e.matchesTheWholeCell<"u"&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,i.matchesTheWholeCell=e.matchesTheWholeCell,n=!0),typeof e.inputtingFindString<"u"&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,i.inputtingFindString=e.inputtingFindString,n=!0),typeof e.findCompleted<"u"&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,i.findCompleted=e.findCompleted,n=!0),n&&(this._state$.next({caseSensitive:this._caseSensitive,findBy:this._findBy,findCompleted:this._findCompleted,findDirection:this._findDirection,findScope:this._findScope,findString:this._findString,inputtingFindString:this._inputtingFindString,matchesCount:this._matchesCount,matchesPosition:this._matchesPosition,matchesTheWholeCell:this._matchesTheWholeCell,replaceRevealed:this._replaceRevealed,revealed:this._revealed}),this._stateUpdates$.next(i))}}let j=class extends c.Disposable{constructor(e,n){super();u(this,"_providers",new Set);u(this,"_state",new J);u(this,"_model");u(this,"_currentMatch$",new _.BehaviorSubject(null));u(this,"currentMatch$",this._currentMatch$.asObservable());u(this,"_replaceables$",new _.BehaviorSubject([]));u(this,"replaceables$",this._replaceables$.asObservable());u(this,"_focusSignal$",new _.Subject);u(this,"focusSignal$",this._focusSignal$.asObservable());this._injector=e,this._contextService=n}get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}dispose(){super.dispose(),this._currentMatch$.next(null),this._currentMatch$.complete(),this._replaceables$.next([]),this._replaceables$.complete(),this._focusSignal$.complete()}getProviders(){return this._providers}getCurrentMatch(){var e;return(e=this._model)==null?void 0:e.getCurrentMatch()}getFindString(){return this._state.findString}changeFindString(e){this._state.changeState({findString:e})}focusFindInput(){this._focusSignal$.next()}changeInputtingFindString(e){e?this._state.changeState({inputtingFindString:e}):this._state.changeState({inputtingFindString:"",findString:""})}changeReplaceString(e){this._state.changeState({replaceString:e})}changeMatchesTheWholeCell(e){this._state.changeState({matchesTheWholeCell:e})}changeCaseSensitive(e){this._state.changeState({caseSensitive:e})}changeFindBy(e){this._state.changeState({findBy:e}),this._toggleDisplayRawFormula(e==="formula")}changeFindScope(e){this._state.changeState({findScope:e})}changeFindDirection(e){this._state.changeState({findDirection:e})}moveToNextMatch(){this._model&&(this._state.replaceRevealed&&!this._model.searched?(this._state.changeState({findString:this._state.inputtingFindString}),this._model.start()):this._model.moveToNextMatch(),this._focusSignal$.next())}moveToPreviousMatch(){this._model&&(this._state.replaceRevealed&&!this._model.searched?(this._state.changeState({findString:this._state.inputtingFindString}),this._model.start()):this._model.moveToPreviousMatch(),this._focusSignal$.next())}async replace(){return this._model?this._model.replace():!1}async replaceAll(){if(!this._model)throw new Error("[FindReplaceService] replaceAll: model is not initialized!");return this._model.replaceAll()}revealReplace(){this._state.changeState({replaceRevealed:!0,inputtingFindString:this._state.findString}),this._toggleRevealReplace(!0)}focusSelection(){var e;(e=this._model)==null||e.focusSelection()}start(e=!1){if(this._providers.size===0)return!1;this._model=this._injector.createInstance(exports.FindReplaceModel,this._state,this._providers),this._model.currentMatch$.subscribe(i=>this._currentMatch$.next(i)),this._model.replaceables$.subscribe(i=>this._replaceables$.next(i));const n=N();return e&&(n.replaceRevealed=!0),this._state.changeState(n),this._toggleRevealReplace(e),!0}find(){var e;(e=this._model)==null||e.start()}terminate(){var e;(e=this._model)==null||e.dispose(),this._model=null,this._toggleDisplayRawFormula(!1),this._toggleRevealReplace(!1)}registerFindReplaceProvider(e){return this._providers.add(e),c.toDisposable(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(z,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(De.RENDER_RAW_FORMULA_KEY,e)}};j=Y([T(0,c.Inject(c.Injector)),T(1,c.IContextService)],j);const w={id:"ui.command.replace-current-match",type:c.CommandType.COMMAND,handler:t=>t.get(S).replace()},Ee="CONFIRM_REPLACE_ALL",U={id:"ui.command.replace-all-matches",type:c.CommandType.COMMAND,handler:async t=>{const e=t.get(l.IConfirmService),n=t.get(c.LocaleService),i=t.get(l.IMessageService);if(!await e.confirm({id:Ee,title:{title:n.t("find-replace.replace.confirm.title")},cancelText:n.t("button.cancel"),confirmText:n.t("button.confirm")}))return!1;const r=await t.get(S).replaceAll(),{success:a,failure:o}=r;return o>0?(a===0?i.show({type:f.MessageType.Error,content:n.t("find-replace.replace.all-failure")}):i.show({type:f.MessageType.Warning,content:n.t("find-replace.replace.partial-success",`${a}`,`${o}`)}),!1):(i.show({type:f.MessageType.Success,content:n.t("find-replace.replace.all-success",`${a}`)}),!0)}},F={id:"ui.operation.open-find-dialog",type:c.CommandType.OPERATION,handler:t=>{const e=t.get(S);return e.revealed?e.focusFindInput():e.start(),!0}},L={id:"ui.operation.open-replace-dialog",type:c.CommandType.OPERATION,handler:t=>{const e=t.get(S);return e.revealed?e.replaceRevealed?e.focusFindInput():e.revealReplace():e.start(!0),!0}},k={type:c.CommandType.OPERATION,id:"ui.operation.go-to-next-match",handler:t=>(t.get(S).moveToNextMatch(),!0)},B={type:c.CommandType.OPERATION,id:"ui.operation.go-to-previous-match",handler:t=>(t.get(S).moveToPreviousMatch(),!0)},Q={type:c.CommandType.OPERATION,id:"ui.operation.focus-selection",handler:t=>(t.get(S).focusSelection(),!0)};var C=function(){return C=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++){e=arguments[n];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&(t[s]=e[s])}return t},C.apply(this,arguments)},Ne=function(t,e){var n={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(n[i]=t[i]);if(t!=null&&typeof Object.getOwnPropertySymbols=="function")for(var s=0,i=Object.getOwnPropertySymbols(t);s<i.length;s++)e.indexOf(i[s])<0&&Object.prototype.propertyIsEnumerable.call(t,i[s])&&(n[i[s]]=t[i[s]]);return n},X=h.forwardRef(function(t,e){var n=t.icon,i=t.id,s=t.className,r=t.extend,a=Ne(t,["icon","id","className","extend"]),o="univerjs-icon univerjs-icon-".concat(i," ").concat(s||"").trim(),p=h.useRef("_".concat(Ae()));return ee(n,"".concat(i),{defIds:n.defIds,idSuffix:p.current},C({ref:e,className:o},a),r)});function ee(t,e,n,i,s){return h.createElement(t.tag,C(C({key:e},Le(t,n,s)),i),($e(t,n).children||[]).map(function(r,a){return ee(r,"".concat(e,"-").concat(t.tag,"-").concat(a),n,void 0,s)}))}function Le(t,e,n){var i=C({},t.attrs);n!=null&&n.colorChannel1&&i.fill==="colorChannel1"&&(i.fill=n.colorChannel1),t.tag==="mask"&&i.id&&(i.id=i.id+e.idSuffix),Object.entries(i).forEach(function(r){var a=r[0],o=r[1];a==="mask"&&typeof o=="string"&&(i[a]=o.replace(/url\(#(.*)\)/,"url(#$1".concat(e.idSuffix,")")))});var s=e.defIds;return!s||s.length===0||(t.tag==="use"&&i["xlink:href"]&&(i["xlink:href"]=i["xlink:href"]+e.idSuffix),Object.entries(i).forEach(function(r){var a=r[0],o=r[1];typeof o=="string"&&(i[a]=o.replace(/url\(#(.*)\)/,"url(#$1".concat(e.idSuffix,")")))})),i}function $e(t,e){var n,i=e.defIds;return!i||i.length===0?t:t.tag==="defs"&&(!((n=t.children)===null||n===void 0)&&n.length)?C(C({},t),{children:t.children.map(function(s){return typeof s.attrs.id=="string"&&i&&i.includes(s.attrs.id)?C(C({},s),{attrs:C(C({},s.attrs),{id:s.attrs.id+e.idSuffix})}):s})}):t}function Ae(){return Math.random().toString(36).substring(2,8)}X.displayName="UniverIcon";var je={tag:"svg",attrs:{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M7.71899 1.36938C4.37604 1.36938 1.66602 4.07941 1.66602 7.42236C1.66602 10.7653 4.37604 13.4753 7.71899 13.4753C9.16744 13.4753 10.4971 12.9666 11.5389 12.118L13.6598 14.4109C13.8848 14.6542 14.2644 14.669 14.5077 14.444C14.7509 14.219 14.7657 13.8393 14.5407 13.5961L12.3906 11.2716C13.2536 10.2254 13.772 8.88442 13.772 7.42236C13.772 4.07941 11.0619 1.36938 7.71899 1.36938ZM2.86602 7.42236C2.86602 4.74215 5.03878 2.56938 7.71899 2.56938C10.3992 2.56938 12.572 4.74215 12.572 7.42236C12.572 10.1026 10.3992 12.2753 7.71899 12.2753C5.03878 12.2753 2.86602 10.1026 2.86602 7.42236Z",fillRule:"evenodd",clipRule:"evenodd"}}]},te=h.forwardRef(function(t,e){return h.createElement(X,Object.assign({},t,{id:"search-icon",ref:e,icon:je}))});te.displayName="SearchIcon";function ie(t){const{findCompleted:e,localeService:n,matchesCount:i,matchesPosition:s,initialFindString:r,findReplaceService:a,onChange:o,...p}=t,[m,v]=h.useState(r),I=e&&i===0?n.t("find-replace.dialog.no-result"):i===0?" ":void 0;function M(b){s===i&&b===1?a.moveToNextMatch():s===1&&b===i||b<s?a.moveToPreviousMatch():a.moveToNextMatch()}return d.jsx("div",{className:"univer-relative univer-flex univer-items-center univer-gap-2",onDrag:b=>b.stopPropagation(),children:d.jsx(f.Input,{"data-u-comp":"search-input",autoFocus:!0,placeholder:n.t("find-replace.dialog.find-placeholder"),value:m,onChange:b=>{v(b),o==null||o(b)},slot:d.jsx(f.Pager,{loop:!0,text:I,value:s,total:i,onChange:M}),...p})})}function ne(t,e){const n=h.useCallback(()=>{var s;(s=document.querySelector(".univer-find-input input"))==null||s.focus()},[]),i=h.useCallback(()=>{const s=document.querySelectorAll("[data-u-comp=find-replace-dialog] [data-u-comp=search-input]");return Array.from(s).some(r=>r===document.activeElement)},[]);return h.useImperativeHandle(e,()=>({focus:n,selectHasFocus:i})),h.useEffect(()=>{const s=t.focusSignal$.subscribe(()=>n());return()=>s.unsubscribe()},[t,n]),{focus:n,selectHasFocus:i}}const we=h.forwardRef(function(e,n){const i=l.useDependency(c.LocaleService),s=l.useDependency(S),r=l.useDependency(c.ICommandService),a=l.useObservable(s.state$,void 0,!0),{findCompleted:o,findString:p,matchesCount:m,matchesPosition:v}=a,y=h.useCallback(()=>{r.executeCommand(L.id)},[r]),I=l.useDebounceFn(M=>s.changeFindString(M),500);return ne(s,n),d.jsxs(d.Fragment,{children:[d.jsx(ie,{findCompleted:o,matchesCount:m,matchesPosition:v,findReplaceService:s,localeService:i,initialFindString:p,onChange:I}),d.jsx("div",{className:"univer-mt-4 univer-text-center",children:d.jsx("a",{className:"hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors",onClick:y,children:i.t("find-replace.dialog.advanced-finding")})})]})}),Ue=h.forwardRef(function(e,n){const i=l.useDependency(S),s=l.useDependency(c.LocaleService),r=l.useDependency(c.ICommandService),a=l.useDependency(l.IMessageService),o=l.useObservable(i.currentMatch$,void 0,!0),p=l.useObservable(i.replaceables$,void 0,!0),m=l.useObservable(i.state$,void 0,!0),{matchesCount:v,matchesPosition:y,findString:I,inputtingFindString:M,replaceString:b,caseSensitive:ae,matchesTheWholeCell:oe,findDirection:ce,findScope:le,findBy:de,findCompleted:$}=m,ue=M.length===0,he=v===0||!(o!=null&&o.replaceable),pe=p.length===0,fe=h.useCallback(g=>i.changeInputtingFindString(g),[i]),ge=h.useCallback(g=>i.changeReplaceString(g),[i]),{focus:_e}=ne(i,n),me=h.useCallback(()=>{I===M?i.moveToNextMatch():(i.changeFindString(M),i.find())},[I,M,i]),ve=h.useCallback(()=>r.executeCommand(w.id),[r]),Se=h.useCallback(async()=>{await r.executeCommand(U.id),_e()},[r]),Ce=h.useCallback(g=>{i.changeFindDirection(g)},[i]),be=h.useCallback(g=>{i.changeFindScope(g)},[i]),Me=h.useCallback(g=>{i.changeFindBy(g)},[i]),Re=Be(s),ye=We(s),Ie=Ge(s);return h.useEffect(()=>{$&&v===0&&a.show({content:s.t("find-replace.dialog.no-match"),type:f.MessageType.Warning,duration:5e3})},[$,v,a,s]),d.jsxs("div",{children:[d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find"),children:d.jsx(ie,{findCompleted:$,className:"univer-find-input",matchesCount:v,matchesPosition:y,findReplaceService:i,localeService:s,initialFindString:M,onChange:fe})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.replace"),children:d.jsx(f.Input,{placeholder:s.t("find-replace.dialog.replace-placeholder"),value:b,onChange:g=>ge(g)})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-direction.title"),children:d.jsx(f.Select,{value:ce,options:ye,onChange:Ce})}),d.jsx(f.FormDualColumnLayout,{children:d.jsxs(d.Fragment,{children:[d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-scope.title"),children:d.jsx(f.Select,{value:le,options:Re,onChange:be})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-by.title"),children:d.jsx(f.Select,{value:de,options:Ie,onChange:Me})})]})}),d.jsx(f.FormDualColumnLayout,{children:d.jsxs(d.Fragment,{children:[d.jsx(f.FormLayout,{children:d.jsx(f.Checkbox,{checked:ae,onChange:g=>{i.changeCaseSensitive(g)},children:s.t("find-replace.dialog.case-sensitive")})}),d.jsx(f.FormLayout,{children:d.jsx(f.Checkbox,{checked:oe,onChange:g=>{i.changeMatchesTheWholeCell(g)},children:s.t("find-replace.dialog.match-the-whole-cell")})})]})}),d.jsxs("div",{className:"univer-mt-6 univer-flex univer-justify-between",children:[d.jsx(f.Button,{variant:"primary",onClick:me,disabled:ue,children:s.t("find-replace.dialog.find")}),d.jsxs("span",{className:"univer-inline-flex univer-gap-2",children:[d.jsx(f.Button,{disabled:he,onClick:ve,children:s.t("find-replace.dialog.replace")}),d.jsx(f.Button,{disabled:pe,onClick:Se,children:s.t("find-replace.dialog.replace-all")})]})]})]})});function ke(){const t=l.useDependency(S),e=l.useDependency(l.ILayoutService),n=l.useDependency(c.IContextService),i=l.useObservable(t.state$,void 0,!0),s=h.useRef(null);h.useEffect(()=>{let p;return s.current&&(p=e.registerContainerElement(s.current)),()=>p==null?void 0:p.dispose()},[e]);const r=h.useRef(null),a=h.useCallback(p=>n.setContextValue(Z,p),[n]),o=h.useCallback(p=>n.setContextValue(q,p),[n]);return h.useEffect(()=>{var m;const p=_.fromEvent(document,"focusin").subscribe(v=>{var y;v.target&&((y=s.current)!=null&&y.contains(v.target))?a(!0):a(!1),!r.current||!r.current.selectHasFocus()?o(!1):o(!0)});return(m=r.current)==null||m.focus(),a(!0),o(!0),()=>{p.unsubscribe(),a(!1)}},[a,o]),d.jsx("div",{ref:s,"data-u-comp":"find-replace-dialog",children:i.replaceRevealed?d.jsx(Ue,{ref:r}):d.jsx(we,{ref:r})})}function Be(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-scope.current-sheet"),value:E.SUBUNIT},{label:t.t("find-replace.dialog.find-scope.workbook"),value:E.UNIT}],[e])}function We(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-direction.row"),value:O.ROW},{label:t.t("find-replace.dialog.find-direction.column"),value:O.COLUMN}],[e])}function Ge(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-by.value"),value:P.VALUE},{label:t.t("find-replace.dialog.find-by.formula"),value:P.FORMULA}],[e])}function x(t){return t.getContextValue(Z)}function Ve(t){return t.getContextValue(z)}function se(t){return t.getContextValue(q)}const D="7_find-replace-shortcuts";function W(t){return t.getContextValue(c.FOCUSING_SHEET)}function G(t){return!t.getContextValue(c.EDITOR_ACTIVATED)}const Ke={id:F.id,description:"find-replace.shortcut.open-find-dialog",binding:l.KeyCode.F|l.MetaKeys.CTRL_COMMAND,group:D,preconditions(t){return!x(t)&&W(t)&&G(t)}},He={id:F.id,description:"find-replace.shortcut.open-find-dialog",binding:l.KeyCode.F|l.MetaKeys.CTRL_COMMAND,mac:l.KeyCode.F|l.MetaKeys.MAC_CTRL,preconditions(t){return!x(t)&&W(t)&&G(t)}},qe={id:L.id,description:"find-replace.shortcut.open-replace-dialog",binding:l.KeyCode.H|l.MetaKeys.CTRL_COMMAND,mac:l.KeyCode.H|l.MetaKeys.MAC_CTRL,group:D,preconditions(t){return W(t)&&G(t)&&(!x(t)||!Ve(t))}},Ze={id:k.id,description:"find-replace.shortcut.go-to-next-match",binding:l.KeyCode.ENTER,group:D,priority:1e3,preconditions(t){return se(t)&&x(t)}},ze={id:B.id,description:"find-replace.shortcut.go-to-previous-match",binding:l.KeyCode.ENTER|l.MetaKeys.SHIFT,group:D,priority:1e3,preconditions(t){return se(t)&&x(t)}},Ye={id:Q.id,description:"find-replace.shortcut.focus-selection",binding:l.KeyCode.ESC,group:D,priority:1e3,preconditions(t){return x(t)}};function Je(t){const e=t.get(c.IContextService);return{id:F.id,icon:"SearchIcon",tooltip:"find-replace.toolbar",type:l.MenuItemType.BUTTON,hidden$:l.getMenuHiddenObservable(t,c.UniverInstanceType.UNIVER_SHEET),disabled$:_.combineLatest([e.subscribeContextValue$(c.EDITOR_ACTIVATED),e.subscribeContextValue$(c.FOCUSING_SHEET)]).pipe(_.map(([n,i])=>n||!i))}}const Qe={[l.RibbonDataGroup.ORGANIZATION]:{[F.id]:{order:2,menuItemFactory:Je}}};var Xe=Object.getOwnPropertyDescriptor,et=(t,e,n,i)=>{for(var s=i>1?void 0:i?Xe(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},R=(t,e)=>(n,i)=>e(n,i,t);const V="DESKTOP_FIND_REPLACE_DIALOG",re=350,tt=20,it=64;exports.FindReplaceController=class extends c.RxDisposable{constructor(n,i,s,r,a,o,p,m,v){super();u(this,"_closingListenerDisposable");this._univerInstanceService=n,this._menuManagerService=i,this._shortcutService=s,this._commandService=r,this._findReplaceService=a,this._dialogService=o,this._layoutService=p,this._localeService=m,this._componentManager=v,this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var n;super.dispose(),(n=this._closingListenerDisposable)==null||n.dispose(),this._closingListenerDisposable=null}_initCommands(){[F,L,k,B,U,w,Q].forEach(n=>{this.disposeWithMe(this._commandService.registerCommand(n))})}_initShortcuts(){[qe,Ke,He,ze,Ze,Ye].forEach(n=>this.disposeWithMe(this._shortcutService.registerShortcut(n)))}_initUI(){[["FindReplaceDialog",ke],["SearchIcon",te]].forEach(([n,i])=>{this.disposeWithMe(this._componentManager.register(n,i))}),this._menuManagerService.mergeMenu(Qe),this._findReplaceService.stateUpdates$.pipe(_.takeUntil(this.dispose$)).subscribe(n=>{n.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:V,draggable:!0,width:re,title:{title:this._localeService.t("find-replace.dialog.title")},children:{label:"FindReplaceDialog"},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:nt(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=c.toDisposable(this._univerInstanceService.focused$.pipe(_.takeUntil(this.dispose$)).subscribe(n=>{(!n||!this._univerInstanceService.getUniverSheetInstance(n))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(V),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};exports.FindReplaceController=et([R(0,c.IUniverInstanceService),R(1,l.IMenuManagerService),R(2,l.IShortcutService),R(3,c.ICommandService),R(4,S),R(5,l.IDialogService),R(6,l.ILayoutService),R(7,c.Inject(c.LocaleService)),R(8,c.Inject(l.ComponentManager))],exports.FindReplaceController);function nt(){const{innerWidth:t}=window;return{x:t-re-tt,y:it}}const st="find-replace.config",K={};var rt=Object.getOwnPropertyDescriptor,at=(t,e,n,i)=>{for(var s=i>1?void 0:i?rt(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},H=(t,e)=>(n,i)=>e(n,i,t);const ot="UNIVER_FIND_REPLACE_PLUGIN";var A;exports.UniverFindReplacePlugin=(A=class extends c.Plugin{constructor(e=K,n,i){super(),this._config=e,this._injector=n,this._configService=i;const{...s}=c.merge({},K,this._config);this._configService.setConfig(st,s)}onStarting(){[[exports.FindReplaceController],[S,{useClass:j}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(exports.FindReplaceController)}},u(A,"pluginName",ot),A);exports.UniverFindReplacePlugin=at([H(1,c.Inject(c.Injector)),H(2,c.IConfigService)],exports.UniverFindReplacePlugin);exports.FindBy=P;exports.FindDirection=O;exports.FindModel=Oe;exports.FindReplaceState=J;exports.FindScope=E;exports.GoToNextMatchOperation=k;exports.GoToPreviousMatchOperation=B;exports.IFindReplaceService=S;exports.OpenFindDialogOperation=F;exports.OpenReplaceDialogOperation=L;exports.ReplaceAllMatchesCommand=U;exports.ReplaceCurrentMatchCommand=w;exports.createInitFindReplaceState=N;
|
|
1
|
+
"use strict";var Fe=Object.defineProperty;var Ie=(t,e,n)=>e in t?Fe(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var u=(t,e,n)=>Ie(t,typeof e!="symbol"?e+"":e,n);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@univerjs/core"),c=require("@univerjs/ui"),f=require("@univerjs/design"),De=require("@univerjs/engine-render"),_=require("rxjs"),h=require("react"),d=require("react/jsx-runtime"),H="FIND_REPLACE_INPUT_FOCUS",q="FIND_REPLACE_DIALOG_FOCUS",Z="FIND_REPLACE_REPLACE_REVEALED";var xe=Object.getOwnPropertyDescriptor,z=(t,e,n,i)=>{for(var s=i>1?void 0:i?xe(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},x=(t,e)=>(n,i)=>e(n,i,t);class Te extends o.Disposable{}const v=o.createIdentifier("find-replace.service");function Pe(t){return typeof t.findString<"u"||typeof t.inputtingFindString<"u"||typeof t.findDirection<"u"||typeof t.matchesTheWholeCell<"u"||typeof t.caseSensitive<"u"||typeof t.findScope<"u"||typeof t.findBy<"u"}exports.FindReplaceModel=class extends o.Disposable{constructor(n,i,s,r){super();u(this,"currentMatch$",new _.BehaviorSubject(null));u(this,"replaceables$",new _.BehaviorSubject([]));u(this,"_findModels",[]);u(this,"_matchingModel",null);u(this,"_matches",[]);u(this,"_currentSearchingDisposables",null);this._state=n,this._providers=i,this._univerInstanceService=s,this._commandService=r,this.disposeWithMe(this._state.stateUpdates$.pipe(_.throttleTime(200,void 0,{leading:!0,trailing:!0})).subscribe(async a=>{const l=this._state.state;Pe(a)&&(l.findString!==""&&!l.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):a.replaceRevealed!==!0&&this._stopSearching())}))}get searched(){return this._findModels.length>0}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...O(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};const n=await this._startSearching();return this._state.changeState({findCompleted:!0}),n}focusSelection(){var n;(n=this._matchingModel)==null||n.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};const n=Array.from(this._providers),i=this._findModels=(await Promise.all(n.map(r=>r.find({findString:this._state.findString,findDirection:this._state.findDirection,findScope:this._state.findScope,findBy:this._state.findBy,replaceRevealed:this._state.replaceRevealed,caseSensitive:this._state.caseSensitive,matchesTheWholeCell:this._state.matchesTheWholeCell})))).flat();this._subscribeToModelsChanges(i);const s=this._matches=i.map(r=>r.getMatches()).flat();return this.replaceables$.next(s.filter(r=>r.replaceable)),s.length?(this._moveToInitialMatch(i),this._state.changeState({matchesCount:s.length}),{results:s}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var n;this._providers.forEach(i=>i.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(n=this._currentSearchingDisposables)==null||n.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(n){const i=this._currentSearchingDisposables=new o.DisposableCollection,s=_.combineLatest(n.map(r=>r.matchesUpdate$)).pipe(_.debounceTime(220)).subscribe(([...r])=>{const a=this._matches=r.flat();a.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:a.length}),this.replaceables$.next(a.filter(l=>l.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});n.forEach(r=>i.add(o.toDisposable(r.activelyChangingMatch$.subscribe(a=>{const l=this._matches.findIndex(p=>p===a);this._state.changeState({matchesPosition:l+1})})))),i.add(o.toDisposable(s))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){const n=await Promise.all(this._findModels.map(i=>i.replaceAll(this._state.replaceString))).then(i=>i.reduce((s,r)=>(s.success+=r.success,s.failure+=r.failure,s),{success:0,failure:0}));return n.failure===0&&this._stopSearching(),n}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(n){const i=this._matches.findIndex(s=>s===n);this.currentMatch$.next(n),this._state.changeState({matchesPosition:i+1})}moveToNextMatch(){if(!this._matchingModel)return;const n=this._findModels.length===1,i=this._matchingModel.moveToNextMatch({loop:n});if(i)return this._markMatch(i),i;{const s=this._findModels.findIndex(r=>r===this._matchingModel);return this._moveToNextUnitMatch(s)}}_moveToNextUnitMatch(n){const i=this._findModels.length;for(let s=(n+1)%i;s!==n;){const r=this._findModels[s],a=r.moveToNextMatch({ignoreSelection:!0});if(a)return this._matchingModel=r,this._markMatch(a),a;s=(s+1)%i}if(this._matchingModel){const s=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return s&&this._markMatch(s),s}}moveToPreviousMatch(){if(!this._matchingModel)return;const n=this._findModels.length===1,i=this._matchingModel.moveToPreviousMatch({loop:n});if(i){const s=this._matches.findIndex(r=>r===i);return this.currentMatch$.next(i),this._state.changeState({matchesPosition:s+1}),i}else{const s=this._findModels.length,r=this._findModels.findIndex(l=>l===this._matchingModel);for(let l=(r-1+s)%s;l!==r;){const p=this._findModels[l],m=p.moveToPreviousMatch({ignoreSelection:!0});if(m)return this._matchingModel=p,this._markMatch(m),m;l=(l-1)%s}const a=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return a&&this._markMatch(a),a}}_moveToInitialMatch(n,i=!1){var a;const s=(a=this._univerInstanceService.getFocusedUnit())==null?void 0:a.getUnitId();if(!s)return-1;const r=n.findIndex(l=>l.unitId===s);if(r!==-1){this._matchingModel=n[r];const l=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:i});if(l)return this._markMatch(l),r}return this._moveToNextUnitMatch(r),0}};exports.FindReplaceModel=z([x(2,o.IUniverInstanceService),x(3,o.ICommandService)],exports.FindReplaceModel);var T=(t=>(t.ROW="row",t.COLUMN="column",t))(T||{}),P=(t=>(t.VALUE="value",t.FORMULA="formula",t))(P||{}),E=(t=>(t.SUBUNIT="subunit",t.UNIT="unit",t))(E||{});function O(){return{caseSensitive:!1,findBy:"value",findCompleted:!1,findDirection:"row",findScope:"subunit",findString:"",inputtingFindString:"",matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:"",revealed:!0}}class Y{constructor(){u(this,"_stateUpdates$",new _.Subject);u(this,"stateUpdates$",this._stateUpdates$.asObservable());u(this,"_state$",new _.BehaviorSubject(O()));u(this,"state$",this._state$.asObservable());u(this,"_findString","");u(this,"_inputtingFindString","");u(this,"_replaceString","");u(this,"_revealed",!1);u(this,"_replaceRevealed",!1);u(this,"_matchesPosition",0);u(this,"_matchesCount",0);u(this,"_caseSensitive",!0);u(this,"_matchesTheWholeCell",!1);u(this,"_findDirection","row");u(this,"_findScope","subunit");u(this,"_findBy","value");u(this,"_findCompleted",!1)}get state(){return this._state$.getValue()}get inputtingFindString(){return this._inputtingFindString}get findString(){return this._findString}get revealed(){return this._revealed}get replaceRevealed(){return this._replaceRevealed}get matchesPosition(){return this._matchesPosition}get matchesCount(){return this._matchesCount}get replaceString(){return this._replaceString}get caseSensitive(){return this._caseSensitive}get matchesTheWholeCell(){return this._matchesTheWholeCell}get findDirection(){return this._findDirection}get findScope(){return this._findScope}get findBy(){return this._findBy}get findCompleted(){return this._findCompleted}changeState(e){let n=!1;const i={};typeof e.findString<"u"&&e.findString!==this._findString&&(this._findString=e.findString,i.findString=this._findString,n=!0),typeof e.revealed<"u"&&e.revealed!==this._revealed&&(this._revealed=e.revealed,i.revealed=e.revealed,n=!0),typeof e.replaceRevealed<"u"&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,i.replaceRevealed=e.replaceRevealed,n=!0),typeof e.replaceString<"u"&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,i.replaceString=e.replaceString,n=!0),typeof e.matchesCount<"u"&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,i.matchesCount=e.matchesCount,n=!0),typeof e.matchesPosition<"u"&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,i.matchesPosition=e.matchesPosition,n=!0),typeof e.findBy<"u"&&e.findBy!==this._findBy&&(this._findBy=e.findBy,i.findBy=e.findBy,n=!0),typeof e.findScope<"u"&&e.findScope!==this._findScope&&(this._findScope=e.findScope,i.findScope=e.findScope,n=!0),typeof e.findDirection<"u"&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,i.findDirection=e.findDirection,n=!0),typeof e.caseSensitive<"u"&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,i.caseSensitive=e.caseSensitive,n=!0),typeof e.matchesTheWholeCell<"u"&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,i.matchesTheWholeCell=e.matchesTheWholeCell,n=!0),typeof e.inputtingFindString<"u"&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,i.inputtingFindString=e.inputtingFindString,n=!0),typeof e.findCompleted<"u"&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,i.findCompleted=e.findCompleted,n=!0),n&&(this._state$.next({caseSensitive:this._caseSensitive,findBy:this._findBy,findCompleted:this._findCompleted,findDirection:this._findDirection,findScope:this._findScope,findString:this._findString,inputtingFindString:this._inputtingFindString,matchesCount:this._matchesCount,matchesPosition:this._matchesPosition,matchesTheWholeCell:this._matchesTheWholeCell,replaceRevealed:this._replaceRevealed,revealed:this._revealed}),this._stateUpdates$.next(i))}}let A=class extends o.Disposable{constructor(e,n){super();u(this,"_providers",new Set);u(this,"_state",new Y);u(this,"_model");u(this,"_currentMatch$",new _.BehaviorSubject(null));u(this,"currentMatch$",this._currentMatch$.asObservable());u(this,"_replaceables$",new _.BehaviorSubject([]));u(this,"replaceables$",this._replaceables$.asObservable());u(this,"_focusSignal$",new _.Subject);u(this,"focusSignal$",this._focusSignal$.asObservable());this._injector=e,this._contextService=n}get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}dispose(){super.dispose(),this._currentMatch$.next(null),this._currentMatch$.complete(),this._replaceables$.next([]),this._replaceables$.complete(),this._focusSignal$.complete()}getProviders(){return this._providers}getCurrentMatch(){var e;return(e=this._model)==null?void 0:e.getCurrentMatch()}getFindString(){return this._state.findString}changeFindString(e){this._state.changeState({findString:e})}focusFindInput(){this._focusSignal$.next()}changeInputtingFindString(e){e?this._state.changeState({inputtingFindString:e}):this._state.changeState({inputtingFindString:"",findString:""})}changeReplaceString(e){this._state.changeState({replaceString:e})}changeMatchesTheWholeCell(e){this._state.changeState({matchesTheWholeCell:e})}changeCaseSensitive(e){this._state.changeState({caseSensitive:e})}changeFindBy(e){this._state.changeState({findBy:e}),this._toggleDisplayRawFormula(e==="formula")}changeFindScope(e){this._state.changeState({findScope:e})}changeFindDirection(e){this._state.changeState({findDirection:e})}moveToNextMatch(){this._model&&(this._state.replaceRevealed&&!this._model.searched?(this._state.changeState({findString:this._state.inputtingFindString}),this._model.start()):this._model.moveToNextMatch(),this._focusSignal$.next())}moveToPreviousMatch(){this._model&&(this._state.replaceRevealed&&!this._model.searched?(this._state.changeState({findString:this._state.inputtingFindString}),this._model.start()):this._model.moveToPreviousMatch(),this._focusSignal$.next())}async replace(){return this._model?this._model.replace():!1}async replaceAll(){if(!this._model)throw new Error("[FindReplaceService] replaceAll: model is not initialized!");return this._model.replaceAll()}revealReplace(){this._state.changeState({replaceRevealed:!0,inputtingFindString:this._state.findString}),this._toggleRevealReplace(!0)}focusSelection(){var e;(e=this._model)==null||e.focusSelection()}start(e=!1){if(this._providers.size===0)return!1;this._model=this._injector.createInstance(exports.FindReplaceModel,this._state,this._providers),this._model.currentMatch$.subscribe(i=>this._currentMatch$.next(i)),this._model.replaceables$.subscribe(i=>this._replaceables$.next(i));const n=O();return e&&(n.replaceRevealed=!0),this._state.changeState(n),this._toggleRevealReplace(e),!0}find(){var e;(e=this._model)==null||e.start()}terminate(){var e;(e=this._model)==null||e.dispose(),this._model=null,this._toggleDisplayRawFormula(!1),this._toggleRevealReplace(!1)}registerFindReplaceProvider(e){return this._providers.add(e),o.toDisposable(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(Z,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(De.RENDER_RAW_FORMULA_KEY,e)}};A=z([x(0,o.Inject(o.Injector)),x(1,o.IContextService)],A);const j={id:"ui.command.replace-current-match",type:o.CommandType.COMMAND,handler:t=>t.get(v).replace()},Ee="CONFIRM_REPLACE_ALL",w={id:"ui.command.replace-all-matches",type:o.CommandType.COMMAND,handler:async t=>{const e=t.get(c.IConfirmService),n=t.get(o.LocaleService),i=t.get(c.IMessageService);if(!await e.confirm({id:Ee,title:{title:n.t("find-replace.replace.confirm.title")},cancelText:n.t("button.cancel"),confirmText:n.t("button.confirm")}))return!1;const r=await t.get(v).replaceAll(),{success:a,failure:l}=r;return l>0?(a===0?i.show({type:f.MessageType.Error,content:n.t("find-replace.replace.all-failure")}):i.show({type:f.MessageType.Warning,content:n.t("find-replace.replace.partial-success",`${a}`,`${l}`)}),!1):(i.show({type:f.MessageType.Success,content:n.t("find-replace.replace.all-success",`${a}`)}),!0)}},F={id:"ui.operation.open-find-dialog",type:o.CommandType.OPERATION,handler:t=>{const e=t.get(v);return e.revealed?e.focusFindInput():e.start(),!0}},N={id:"ui.operation.open-replace-dialog",type:o.CommandType.OPERATION,handler:t=>{const e=t.get(v);return e.revealed?e.replaceRevealed?e.focusFindInput():e.revealReplace():e.start(!0),!0}},U={type:o.CommandType.OPERATION,id:"ui.operation.go-to-next-match",handler:t=>(t.get(v).moveToNextMatch(),!0)},k={type:o.CommandType.OPERATION,id:"ui.operation.go-to-previous-match",handler:t=>(t.get(v).moveToPreviousMatch(),!0)},J={type:o.CommandType.OPERATION,id:"ui.operation.focus-selection",handler:t=>(t.get(v).focusSelection(),!0)};function Q({ref:t,...e}){const{icon:n,id:i,className:s,extend:r,...a}=e,l=`univerjs-icon univerjs-icon-${i} ${s||""}`.trim(),p=h.useRef(`_${$e()}`);return X(n,`${i}`,{defIds:n.defIds,idSuffix:p.current},{ref:t,className:l,...a},r)}function X(t,e,n,i,s){return h.createElement(t.tag,{key:e,...Oe(t,n,s),...i},(Ne(t,n).children||[]).map((r,a)=>X(r,`${e}-${t.tag}-${a}`,n,void 0,s)))}function Oe(t,e,n){const i={...t.attrs};n!=null&&n.colorChannel1&&i.fill==="colorChannel1"&&(i.fill=n.colorChannel1),t.tag==="mask"&&i.id&&(i.id=i.id+e.idSuffix),Object.entries(i).forEach(([r,a])=>{r==="mask"&&typeof a=="string"&&(i[r]=a.replace(/url\(#(.*)\)/,`url(#$1${e.idSuffix})`))});const{defIds:s}=e;return!s||s.length===0||(t.tag==="use"&&i["xlink:href"]&&(i["xlink:href"]=i["xlink:href"]+e.idSuffix),Object.entries(i).forEach(([r,a])=>{typeof a=="string"&&(i[r]=a.replace(/url\(#(.*)\)/,`url(#$1${e.idSuffix})`))})),i}function Ne(t,e){var i;const{defIds:n}=e;return!n||n.length===0?t:t.tag==="defs"&&((i=t.children)!=null&&i.length)?{...t,children:t.children.map(s=>typeof s.attrs.id=="string"&&n&&n.includes(s.attrs.id)?{...s,attrs:{...s.attrs,id:s.attrs.id+e.idSuffix}}:s)}:t}function $e(){return Math.random().toString(36).substring(2,8)}Q.displayName="UniverIcon";const Le={tag:"svg",attrs:{xmlns:"http://www.w3.org/2000/svg",fill:"none",viewBox:"0 0 16 16",width:"1em",height:"1em"},children:[{tag:"path",attrs:{fill:"currentColor",d:"M7.71899 1.36938C4.37604 1.36938 1.66602 4.07941 1.66602 7.42236C1.66602 10.7653 4.37604 13.4753 7.71899 13.4753C9.16744 13.4753 10.4971 12.9666 11.5389 12.118L13.6598 14.4109C13.8848 14.6542 14.2644 14.669 14.5077 14.444C14.7509 14.219 14.7657 13.8393 14.5407 13.5961L12.3906 11.2716C13.2536 10.2254 13.772 8.88442 13.772 7.42236C13.772 4.07941 11.0619 1.36938 7.71899 1.36938ZM2.86602 7.42236C2.86602 4.74215 5.03878 2.56938 7.71899 2.56938C10.3992 2.56938 12.572 4.74215 12.572 7.42236C12.572 10.1026 10.3992 12.2753 7.71899 12.2753C5.03878 12.2753 2.86602 10.1026 2.86602 7.42236Z",fillRule:"evenodd",clipRule:"evenodd"}}]},ee=h.forwardRef(function(e,n){return h.createElement(Q,Object.assign({},e,{id:"search-icon",ref:n,icon:Le}))});ee.displayName="SearchIcon";function te(t){const{findCompleted:e,localeService:n,matchesCount:i,matchesPosition:s,initialFindString:r,findReplaceService:a,onChange:l,...p}=t,[m,S]=h.useState(r),y=e&&i===0?n.t("find-replace.dialog.no-result"):i===0?" ":void 0;function M(C){s===i&&C===1?a.moveToNextMatch():s===1&&C===i||C<s?a.moveToPreviousMatch():a.moveToNextMatch()}return d.jsx("div",{className:"univer-relative univer-flex univer-items-center univer-gap-2",onDrag:C=>C.stopPropagation(),children:d.jsx(f.Input,{"data-u-comp":"search-input",autoFocus:!0,placeholder:n.t("find-replace.dialog.find-placeholder"),value:m,onChange:C=>{S(C),l==null||l(C)},slot:d.jsx(f.Pager,{loop:!0,text:y,value:s,total:i,onChange:M}),...p})})}function ie(t,e){const n=h.useCallback(()=>{var s;(s=document.querySelector(".univer-find-input input"))==null||s.focus()},[]),i=h.useCallback(()=>{const s=document.querySelectorAll("[data-u-comp=find-replace-dialog] [data-u-comp=search-input]");return Array.from(s).some(r=>r===document.activeElement)},[]);return h.useImperativeHandle(e,()=>({focus:n,selectHasFocus:i})),h.useEffect(()=>{const s=t.focusSignal$.subscribe(()=>n());return()=>s.unsubscribe()},[t,n]),{focus:n,selectHasFocus:i}}const Ae=h.forwardRef(function(e,n){const i=c.useDependency(o.LocaleService),s=c.useDependency(v),r=c.useDependency(o.ICommandService),a=c.useObservable(s.state$,void 0,!0),{findCompleted:l,findString:p,matchesCount:m,matchesPosition:S}=a,b=h.useCallback(()=>{r.executeCommand(N.id)},[r]),y=c.useDebounceFn(M=>s.changeFindString(M),500);return ie(s,n),d.jsxs(d.Fragment,{children:[d.jsx(te,{findCompleted:l,matchesCount:m,matchesPosition:S,findReplaceService:s,localeService:i,initialFindString:p,onChange:y}),d.jsx("div",{className:"univer-mt-4 univer-text-center",children:d.jsx("a",{className:"hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors",onClick:b,children:i.t("find-replace.dialog.advanced-finding")})})]})}),je=h.forwardRef(function(e,n){const i=c.useDependency(v),s=c.useDependency(o.LocaleService),r=c.useDependency(o.ICommandService),a=c.useDependency(c.IMessageService),l=c.useObservable(i.currentMatch$,void 0,!0),p=c.useObservable(i.replaceables$,void 0,!0),m=c.useObservable(i.state$,void 0,!0),{matchesCount:S,matchesPosition:b,findString:y,inputtingFindString:M,replaceString:C,caseSensitive:re,matchesTheWholeCell:ae,findDirection:oe,findScope:ce,findBy:le,findCompleted:$}=m,de=M.length===0,ue=S===0||!(l!=null&&l.replaceable),he=p.length===0,pe=h.useCallback(g=>i.changeInputtingFindString(g),[i]),fe=h.useCallback(g=>i.changeReplaceString(g),[i]),{focus:ge}=ie(i,n),_e=h.useCallback(()=>{y===M?i.moveToNextMatch():(i.changeFindString(M),i.find())},[y,M,i]),me=h.useCallback(()=>r.executeCommand(j.id),[r]),Se=h.useCallback(async()=>{await r.executeCommand(w.id),ge()},[r]),ve=h.useCallback(g=>{i.changeFindDirection(g)},[i]),Ce=h.useCallback(g=>{i.changeFindScope(g)},[i]),Me=h.useCallback(g=>{i.changeFindBy(g)},[i]),Re=Ue(s),be=ke(s),ye=Be(s);return h.useEffect(()=>{$&&S===0&&a.show({content:s.t("find-replace.dialog.no-match"),type:f.MessageType.Warning,duration:5e3})},[$,S,a,s]),d.jsxs("div",{children:[d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find"),children:d.jsx(te,{findCompleted:$,className:"univer-find-input",matchesCount:S,matchesPosition:b,findReplaceService:i,localeService:s,initialFindString:M,onChange:pe})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.replace"),children:d.jsx(f.Input,{placeholder:s.t("find-replace.dialog.replace-placeholder"),value:C,onChange:g=>fe(g)})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-direction.title"),children:d.jsx(f.Select,{value:oe,options:be,onChange:ve})}),d.jsx(f.FormDualColumnLayout,{children:d.jsxs(d.Fragment,{children:[d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-scope.title"),children:d.jsx(f.Select,{value:ce,options:Re,onChange:Ce})}),d.jsx(f.FormLayout,{label:s.t("find-replace.dialog.find-by.title"),children:d.jsx(f.Select,{value:le,options:ye,onChange:Me})})]})}),d.jsx(f.FormDualColumnLayout,{children:d.jsxs(d.Fragment,{children:[d.jsx(f.FormLayout,{children:d.jsx(f.Checkbox,{checked:re,onChange:g=>{i.changeCaseSensitive(g)},children:s.t("find-replace.dialog.case-sensitive")})}),d.jsx(f.FormLayout,{children:d.jsx(f.Checkbox,{checked:ae,onChange:g=>{i.changeMatchesTheWholeCell(g)},children:s.t("find-replace.dialog.match-the-whole-cell")})})]})}),d.jsxs("div",{className:"univer-mt-6 univer-flex univer-justify-between",children:[d.jsx(f.Button,{variant:"primary",onClick:_e,disabled:de,children:s.t("find-replace.dialog.find")}),d.jsxs("span",{className:"univer-inline-flex univer-gap-2",children:[d.jsx(f.Button,{disabled:ue,onClick:me,children:s.t("find-replace.dialog.replace")}),d.jsx(f.Button,{disabled:he,onClick:Se,children:s.t("find-replace.dialog.replace-all")})]})]})]})});function we(){const t=c.useDependency(v),e=c.useDependency(c.ILayoutService),n=c.useDependency(o.IContextService),i=c.useObservable(t.state$,void 0,!0),s=h.useRef(null);h.useEffect(()=>{let p;return s.current&&(p=e.registerContainerElement(s.current)),()=>p==null?void 0:p.dispose()},[e]);const r=h.useRef(null),a=h.useCallback(p=>n.setContextValue(q,p),[n]),l=h.useCallback(p=>n.setContextValue(H,p),[n]);return h.useEffect(()=>{var m;const p=_.fromEvent(document,"focusin").subscribe(S=>{var b;S.target&&((b=s.current)!=null&&b.contains(S.target))?a(!0):a(!1),!r.current||!r.current.selectHasFocus()?l(!1):l(!0)});return(m=r.current)==null||m.focus(),a(!0),l(!0),()=>{p.unsubscribe(),a(!1)}},[a,l]),d.jsx("div",{ref:s,"data-u-comp":"find-replace-dialog",children:i.replaceRevealed?d.jsx(je,{ref:r}):d.jsx(Ae,{ref:r})})}function Ue(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-scope.current-sheet"),value:E.SUBUNIT},{label:t.t("find-replace.dialog.find-scope.workbook"),value:E.UNIT}],[e])}function ke(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-direction.row"),value:T.ROW},{label:t.t("find-replace.dialog.find-direction.column"),value:T.COLUMN}],[e])}function Be(t){const e=t.getCurrentLocale();return h.useMemo(()=>[{label:t.t("find-replace.dialog.find-by.value"),value:P.VALUE},{label:t.t("find-replace.dialog.find-by.formula"),value:P.FORMULA}],[e])}function I(t){return t.getContextValue(q)}function We(t){return t.getContextValue(Z)}function ne(t){return t.getContextValue(H)}const D="7_find-replace-shortcuts";function B(t){return t.getContextValue(o.FOCUSING_SHEET)}function W(t){return!t.getContextValue(o.EDITOR_ACTIVATED)}const Ge={id:F.id,description:"find-replace.shortcut.open-find-dialog",binding:c.KeyCode.F|c.MetaKeys.CTRL_COMMAND,group:D,preconditions(t){return!I(t)&&B(t)&&W(t)}},Ve={id:F.id,description:"find-replace.shortcut.open-find-dialog",binding:c.KeyCode.F|c.MetaKeys.CTRL_COMMAND,mac:c.KeyCode.F|c.MetaKeys.MAC_CTRL,preconditions(t){return!I(t)&&B(t)&&W(t)}},Ke={id:N.id,description:"find-replace.shortcut.open-replace-dialog",binding:c.KeyCode.H|c.MetaKeys.CTRL_COMMAND,mac:c.KeyCode.H|c.MetaKeys.MAC_CTRL,group:D,preconditions(t){return B(t)&&W(t)&&(!I(t)||!We(t))}},He={id:U.id,description:"find-replace.shortcut.go-to-next-match",binding:c.KeyCode.ENTER,group:D,priority:1e3,preconditions(t){return ne(t)&&I(t)}},qe={id:k.id,description:"find-replace.shortcut.go-to-previous-match",binding:c.KeyCode.ENTER|c.MetaKeys.SHIFT,group:D,priority:1e3,preconditions(t){return ne(t)&&I(t)}},Ze={id:J.id,description:"find-replace.shortcut.focus-selection",binding:c.KeyCode.ESC,group:D,priority:1e3,preconditions(t){return I(t)}};function ze(t){const e=t.get(o.IContextService);return{id:F.id,icon:"SearchIcon",tooltip:"find-replace.toolbar",type:c.MenuItemType.BUTTON,hidden$:c.getMenuHiddenObservable(t,o.UniverInstanceType.UNIVER_SHEET),disabled$:_.combineLatest([e.subscribeContextValue$(o.EDITOR_ACTIVATED),e.subscribeContextValue$(o.FOCUSING_SHEET)]).pipe(_.map(([n,i])=>n||!i))}}const Ye={[c.RibbonDataGroup.ORGANIZATION]:{[F.id]:{order:2,menuItemFactory:ze}}};var Je=Object.getOwnPropertyDescriptor,Qe=(t,e,n,i)=>{for(var s=i>1?void 0:i?Je(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},R=(t,e)=>(n,i)=>e(n,i,t);const G="DESKTOP_FIND_REPLACE_DIALOG",se=350,Xe=20,et=64;exports.FindReplaceController=class extends o.RxDisposable{constructor(n,i,s,r,a,l,p,m,S){super();u(this,"_closingListenerDisposable");this._univerInstanceService=n,this._menuManagerService=i,this._shortcutService=s,this._commandService=r,this._findReplaceService=a,this._dialogService=l,this._layoutService=p,this._localeService=m,this._componentManager=S,this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var n;super.dispose(),(n=this._closingListenerDisposable)==null||n.dispose(),this._closingListenerDisposable=null}_initCommands(){[F,N,U,k,w,j,J].forEach(n=>{this.disposeWithMe(this._commandService.registerCommand(n))})}_initShortcuts(){[Ke,Ge,Ve,qe,He,Ze].forEach(n=>this.disposeWithMe(this._shortcutService.registerShortcut(n)))}_initUI(){[["FindReplaceDialog",we],["SearchIcon",ee]].forEach(([n,i])=>{this.disposeWithMe(this._componentManager.register(n,i))}),this._menuManagerService.mergeMenu(Ye),this._findReplaceService.stateUpdates$.pipe(_.takeUntil(this.dispose$)).subscribe(n=>{n.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:G,draggable:!0,width:se,title:{title:this._localeService.t("find-replace.dialog.title")},children:{label:"FindReplaceDialog"},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:tt(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=o.toDisposable(this._univerInstanceService.focused$.pipe(_.takeUntil(this.dispose$)).subscribe(n=>{(!n||!this._univerInstanceService.getUniverSheetInstance(n))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(G),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};exports.FindReplaceController=Qe([R(0,o.IUniverInstanceService),R(1,c.IMenuManagerService),R(2,c.IShortcutService),R(3,o.ICommandService),R(4,v),R(5,c.IDialogService),R(6,c.ILayoutService),R(7,o.Inject(o.LocaleService)),R(8,o.Inject(c.ComponentManager))],exports.FindReplaceController);function tt(){const{innerWidth:t}=window;return{x:t-se-Xe,y:et}}const it="find-replace.config",V={};var nt=Object.getOwnPropertyDescriptor,st=(t,e,n,i)=>{for(var s=i>1?void 0:i?nt(e,n):e,r=t.length-1,a;r>=0;r--)(a=t[r])&&(s=a(s)||s);return s},K=(t,e)=>(n,i)=>e(n,i,t);const rt="UNIVER_FIND_REPLACE_PLUGIN";var L;exports.UniverFindReplacePlugin=(L=class extends o.Plugin{constructor(e=V,n,i){super(),this._config=e,this._injector=n,this._configService=i;const{...s}=o.merge({},V,this._config);this._configService.setConfig(it,s)}onStarting(){[[exports.FindReplaceController],[v,{useClass:A}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(exports.FindReplaceController)}},u(L,"pluginName",rt),L);exports.UniverFindReplacePlugin=st([K(1,o.Inject(o.Injector)),K(2,o.IConfigService)],exports.UniverFindReplacePlugin);exports.FindBy=P;exports.FindDirection=T;exports.FindModel=Te;exports.FindReplaceState=Y;exports.FindScope=E;exports.GoToNextMatchOperation=U;exports.GoToPreviousMatchOperation=k;exports.IFindReplaceService=v;exports.OpenFindDialogOperation=F;exports.OpenReplaceDialogOperation=N;exports.ReplaceAllMatchesCommand=w;exports.ReplaceCurrentMatchCommand=j;exports.createInitFindReplaceState=O;
|