@univerjs/find-replace 0.20.0 → 0.20.1

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 CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@univerjs/core`),t=require(`@univerjs/design`),n=require(`@univerjs/ui`),r=require(`@univerjs/engine-render`),i=require(`rxjs`),a=require(`@univerjs/icons`),o=require(`react`),s=require(`react/jsx-runtime`);const c=`FIND_REPLACE_INPUT_FOCUS`,l=`FIND_REPLACE_DIALOG_FOCUS`,u=`FIND_REPLACE_REPLACE_REVEALED`;function d(e){"@babel/helpers - typeof";return d=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},d(e)}function f(e,t){if(d(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(d(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function p(e){var t=f(e,`string`);return d(t)==`symbol`?t:t+``}function m(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function h(e,t){return function(n,r){t(n,r,e)}}function g(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var _=class extends e.Disposable{};const v=(0,e.createIdentifier)(`find-replace.service`);function y(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let b=class extends e.Disposable{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,m(this,`currentMatch$`,new i.BehaviorSubject(null)),m(this,`replaceables$`,new i.BehaviorSubject([])),m(this,`_findModels`,[]),m(this,`_matchingModel`,null),m(this,`_matches`,[]),m(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe((0,i.throttleTime)(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;y(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...w(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(t){let n=this._currentSearchingDisposables=new e.DisposableCollection,r=(0,i.combineLatest)(t.map(e=>e.matchesUpdate$)).pipe((0,i.debounceTime)(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});t.forEach(t=>n.add((0,e.toDisposable)(t.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),n.add((0,e.toDisposable)(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};b=g([h(2,e.IUniverInstanceService),h(3,e.ICommandService)],b);let x=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),S=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),C=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function w(){return{caseSensitive:!1,findBy:S.VALUE,findCompleted:!1,findDirection:x.ROW,findScope:C.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var T=class{constructor(){m(this,`_stateUpdates$`,new i.Subject),m(this,`stateUpdates$`,this._stateUpdates$.asObservable()),m(this,`_state$`,new i.BehaviorSubject(w())),m(this,`state$`,this._state$.asObservable()),m(this,`_findString`,``),m(this,`_inputtingFindString`,``),m(this,`_replaceString`,``),m(this,`_revealed`,!1),m(this,`_replaceRevealed`,!1),m(this,`_matchesPosition`,0),m(this,`_matchesCount`,0),m(this,`_caseSensitive`,!0),m(this,`_matchesTheWholeCell`,!1),m(this,`_findDirection`,x.ROW),m(this,`_findScope`,C.SUBUNIT),m(this,`_findBy`,S.VALUE),m(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let E=class extends e.Disposable{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,m(this,`_providers`,new Set),m(this,`_state`,new T),m(this,`_model`,void 0),m(this,`_currentMatch$`,new i.BehaviorSubject(null)),m(this,`currentMatch$`,this._currentMatch$.asObservable()),m(this,`_replaceables$`,new i.BehaviorSubject([])),m(this,`replaceables$`,this._replaceables$.asObservable()),m(this,`_focusSignal$`,new i.Subject),m(this,`focusSignal$`,this._focusSignal$.asObservable())}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===S.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 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(b,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=w();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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(t){return this._providers.add(t),(0,e.toDisposable)(()=>this._providers.delete(t))}_toggleRevealReplace(e){this._contextService.setContextValue(u,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(r.RENDER_RAW_FORMULA_KEY,e)}};E=g([h(0,(0,e.Inject)(e.Injector)),h(1,e.IContextService)],E);const D={id:`ui.command.replace-current-match`,type:e.CommandType.COMMAND,handler:e=>e.get(v).replace()},O={id:`ui.command.replace-all-matches`,type:e.CommandType.COMMAND,handler:async r=>{let i=r.get(e.IConfirmService),a=r.get(e.LocaleService),o=r.get(n.IMessageService);if(!await i.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:a.t(`find-replace.replace.confirm.title`)},cancelText:a.t(`button.cancel`),confirmText:a.t(`button.confirm`)}))return!1;let{success:s,failure:c}=await r.get(v).replaceAll();return c>0?(s===0?o.show({type:t.MessageType.Error,content:a.t(`find-replace.replace.all-failure`)}):o.show({type:t.MessageType.Warning,content:a.t(`find-replace.replace.partial-success`,`${s}`,`${c}`)}),!1):(o.show({type:t.MessageType.Success,content:a.t(`find-replace.replace.all-success`,`${s}`)}),!0)}},k={id:`ui.operation.open-find-dialog`,type:e.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.focusFindInput():t.start(),!0}},A={id:`ui.operation.open-replace-dialog`,type:e.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},j={type:e.CommandType.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(v).moveToNextMatch(),!0)},M={type:e.CommandType.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(v).moveToPreviousMatch(),!0)},N={type:e.CommandType.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(v).focusSelection(),!0)};function P(t){let r=t.get(e.IContextService);return{id:k.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:n.MenuItemType.BUTTON,hidden$:(0,n.getMenuHiddenObservable)(t,e.UniverInstanceType.UNIVER_SHEET),disabled$:(0,i.combineLatest)([r.subscribeContextValue$(e.EDITOR_ACTIVATED),r.subscribeContextValue$(e.FOCUSING_SHEET)]).pipe((0,i.map)(([e,t])=>e||!t))}}const F={[n.RibbonDataGroup.ORGANIZATION]:{[k.id]:{order:2,menuItemFactory:P}}};function I(e){let{findCompleted:n,localeService:r,matchesCount:i,matchesPosition:a,initialFindString:c,findReplaceService:l,onChange:u,...d}=e,[f,p]=(0,o.useState)(c),m=n&&i===0?r.t(`find-replace.dialog.no-result`):i===0?` `:void 0;function h(e){a===i&&e===1?l.moveToNextMatch():a===1&&e===i||e<a?l.moveToPreviousMatch():l.moveToNextMatch()}return(0,s.jsx)(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:(0,s.jsx)(t.Input,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:r.t(`find-replace.dialog.find-placeholder`),value:f,onChange:e=>{p(e),u==null||u(e)},slot:(0,s.jsx)(t.Pager,{loop:!0,text:m,value:a,total:i,onChange:h}),...d})})}function L(e,t){let n=(0,o.useCallback)(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=(0,o.useCallback)(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return(0,o.useImperativeHandle)(t,()=>({focus:n,selectHasFocus:r})),(0,o.useEffect)(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const R=(0,o.forwardRef)(function(t,r){let i=(0,n.useDependency)(e.LocaleService),a=(0,n.useDependency)(v),c=(0,n.useDependency)(e.ICommandService),{findCompleted:l,findString:u,matchesCount:d,matchesPosition:f}=(0,n.useObservable)(a.state$,void 0,!0),p=(0,o.useCallback)(()=>{c.executeCommand(A.id)},[c]),m=(0,n.useDebounceFn)(e=>a.changeFindString(e),500);return L(a,r),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(I,{findCompleted:l,matchesCount:d,matchesPosition:f,findReplaceService:a,localeService:i,initialFindString:u,onChange:m}),(0,s.jsx)(`div`,{className:`univer-mt-4 univer-text-center`,children:(0,s.jsx)(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:p,children:i.t(`find-replace.dialog.advanced-finding`)})})]})}),z=(0,o.forwardRef)(function(r,i){let a=(0,n.useDependency)(v),c=(0,n.useDependency)(e.LocaleService),l=(0,n.useDependency)(e.ICommandService),u=(0,n.useDependency)(n.IMessageService),d=(0,n.useObservable)(a.currentMatch$,void 0,!0),f=(0,n.useObservable)(a.replaceables$,void 0,!0),{matchesCount:p,matchesPosition:m,findString:h,inputtingFindString:g,replaceString:_,caseSensitive:y,matchesTheWholeCell:b,findDirection:x,findScope:S,findBy:C,findCompleted:w}=(0,n.useObservable)(a.state$,void 0,!0),T=g.length===0,E=p===0||!(d!=null&&d.replaceable),k=f.length===0,A=(0,o.useCallback)(e=>a.changeInputtingFindString(e),[a]),j=(0,o.useCallback)(e=>a.changeReplaceString(e),[a]),{focus:M}=L(a,i),N=(0,o.useCallback)(()=>{h===g?a.moveToNextMatch():(a.changeFindString(g),a.find())},[h,g,a]),P=(0,o.useCallback)(()=>l.executeCommand(D.id),[l]),F=(0,o.useCallback)(async()=>{await l.executeCommand(O.id),M()},[l]),R=(0,o.useCallback)(e=>{a.changeFindDirection(e)},[a]),z=(0,o.useCallback)(e=>{a.changeFindScope(e)},[a]),B=(0,o.useCallback)(e=>{a.changeFindBy(e)},[a]),U=V(c),W=H(c),G=ee(c);return(0,o.useEffect)(()=>{w&&p===0&&u.show({content:c.t(`find-replace.dialog.no-match`),type:t.MessageType.Warning,duration:5e3})},[w,p,u,c]),(0,s.jsxs)(`div`,{children:[(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find`),children:(0,s.jsx)(I,{findCompleted:w,className:`univer-find-input`,matchesCount:p,matchesPosition:m,findReplaceService:a,localeService:c,initialFindString:g,onChange:A})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.replace`),children:(0,s.jsx)(t.Input,{placeholder:c.t(`find-replace.dialog.replace-placeholder`),value:_,onChange:e=>j(e)})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-direction.title`),children:(0,s.jsx)(t.Select,{value:x,options:W,onChange:R})}),(0,s.jsx)(t.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-scope.title`),children:(0,s.jsx)(t.Select,{value:S,options:U,onChange:z})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-by.title`),children:(0,s.jsx)(t.Select,{value:C,options:G,onChange:B})})]})}),(0,s.jsx)(t.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.FormLayout,{children:(0,s.jsx)(t.Checkbox,{checked:y,onChange:e=>{a.changeCaseSensitive(e)},children:c.t(`find-replace.dialog.case-sensitive`)})}),(0,s.jsx)(t.FormLayout,{children:(0,s.jsx)(t.Checkbox,{checked:b,onChange:e=>{a.changeMatchesTheWholeCell(e)},children:c.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),(0,s.jsxs)(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[(0,s.jsx)(t.Button,{variant:`primary`,onClick:N,disabled:T,children:c.t(`find-replace.dialog.find`)}),(0,s.jsxs)(`span`,{className:`univer-inline-flex univer-gap-2`,children:[(0,s.jsx)(t.Button,{disabled:E,onClick:P,children:c.t(`find-replace.dialog.replace`)}),(0,s.jsx)(t.Button,{disabled:k,onClick:F,children:c.t(`find-replace.dialog.replace-all`)})]})]})]})});function B(){let t=(0,n.useDependency)(v),r=(0,n.useDependency)(n.ILayoutService),a=(0,n.useDependency)(e.IContextService),u=(0,n.useObservable)(t.state$,void 0,!0),d=(0,o.useRef)(null);(0,o.useEffect)(()=>{let e;return d.current&&(e=r.registerContainerElement(d.current)),()=>e==null?void 0:e.dispose()},[r]);let f=(0,o.useRef)(null),p=(0,o.useCallback)(e=>a.setContextValue(l,e),[a]),m=(0,o.useCallback)(e=>a.setContextValue(c,e),[a]);return(0,o.useEffect)(()=>{var e;let t=(0,i.fromEvent)(document,`focusin`).subscribe(e=>{var t;e.target&&(t=d.current)!=null&&t.contains(e.target)?p(!0):p(!1),!f.current||!f.current.selectHasFocus()?m(!1):m(!0)});return(e=f.current)==null||e.focus(),p(!0),m(!0),()=>{t.unsubscribe(),p(!1)}},[p,m]),(0,s.jsx)(`div`,{ref:d,"data-u-comp":`find-replace-dialog`,children:u.replaceRevealed?(0,s.jsx)(z,{ref:f}):(0,s.jsx)(R,{ref:f})})}function V(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:C.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:C.UNIT}],[e.getCurrentLocale()])}function H(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:x.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:x.COLUMN}],[e.getCurrentLocale()])}function ee(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:S.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:S.FORMULA}],[e.getCurrentLocale()])}function U(e){return e.getContextValue(l)}function W(e){return e.getContextValue(u)}function G(e){return e.getContextValue(c)}const K=`7_find-replace-shortcuts`;function q(t){return t.getContextValue(e.FOCUSING_SHEET)}function J(t){return!t.getContextValue(e.EDITOR_ACTIVATED)}const te={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:n.KeyCode.F|n.MetaKeys.CTRL_COMMAND,group:K,preconditions(e){return!U(e)&&q(e)&&J(e)}},ne={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:n.KeyCode.F|n.MetaKeys.CTRL_COMMAND,mac:n.KeyCode.F|n.MetaKeys.MAC_CTRL,preconditions(e){return!U(e)&&q(e)&&J(e)}},re={id:A.id,description:`find-replace.shortcut.open-replace-dialog`,binding:n.KeyCode.H|n.MetaKeys.CTRL_COMMAND,mac:n.KeyCode.H|n.MetaKeys.MAC_CTRL,group:K,preconditions(e){return q(e)&&J(e)&&(!U(e)||!W(e))}},ie={id:j.id,description:`find-replace.shortcut.go-to-next-match`,binding:n.KeyCode.ENTER,group:K,priority:1e3,preconditions(e){return G(e)&&U(e)}},ae={id:M.id,description:`find-replace.shortcut.go-to-previous-match`,binding:n.KeyCode.ENTER|n.MetaKeys.SHIFT,group:K,priority:1e3,preconditions(e){return G(e)&&U(e)}},oe={id:N.id,description:`find-replace.shortcut.focus-selection`,binding:n.KeyCode.ESC,group:K,priority:1e3,preconditions(e){return U(e)}},Y=`DESKTOP_FIND_REPLACE_DIALOG`;let X=class extends e.RxDisposable{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,m(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[k,A,j,M,O,D,N].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[re,te,ne,ae,ie,oe].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,B],[`SearchIcon`,a.SearchIcon]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(F),this._findReplaceService.stateUpdates$.pipe((0,i.takeUntil)(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:Y,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:se(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=(0,e.toDisposable)(this._univerInstanceService.focused$.pipe((0,i.takeUntil)(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(Y),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};X=g([h(0,e.IUniverInstanceService),h(1,n.IMenuManagerService),h(2,n.IShortcutService),h(3,e.ICommandService),h(4,v),h(5,n.IDialogService),h(6,n.ILayoutService),h(7,(0,e.Inject)(e.LocaleService)),h(8,(0,e.Inject)(n.ComponentManager))],X);function se(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var ce=`@univerjs/find-replace`,le=`0.20.0`;const Z=`find-replace.config`;Symbol(Z);const Q={};let $=class extends e.Plugin{constructor(t=Q,n,r){super(),this._config=t,this._injector=n,this._configService=r;let{...i}=(0,e.merge)({},Q,this._config);this._configService.setConfig(Z,i)}onStarting(){[[X],[v,{useClass:E}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(X)}};m($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),m($,`packageName`,ce),m($,`version`,le),$=g([h(1,(0,e.Inject)(e.Injector)),h(2,e.IConfigService)],$),exports.FindBy=S,exports.FindDirection=x,exports.FindModel=_,Object.defineProperty(exports,`FindReplaceController`,{enumerable:!0,get:function(){return X}}),Object.defineProperty(exports,`FindReplaceModel`,{enumerable:!0,get:function(){return b}}),exports.FindReplaceState=T,exports.FindScope=C,exports.GoToNextMatchOperation=j,exports.GoToPreviousMatchOperation=M,exports.IFindReplaceService=v,exports.OpenFindDialogOperation=k,exports.OpenReplaceDialogOperation=A,exports.ReplaceAllMatchesCommand=O,exports.ReplaceCurrentMatchCommand=D,Object.defineProperty(exports,`UniverFindReplacePlugin`,{enumerable:!0,get:function(){return $}}),exports.createInitFindReplaceState=w;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@univerjs/core`),t=require(`@univerjs/design`),n=require(`@univerjs/ui`),r=require(`@univerjs/engine-render`),i=require(`rxjs`),a=require(`@univerjs/icons`),o=require(`react`),s=require(`react/jsx-runtime`);const c=`FIND_REPLACE_INPUT_FOCUS`,l=`FIND_REPLACE_DIALOG_FOCUS`,u=`FIND_REPLACE_REPLACE_REVEALED`;function d(e){"@babel/helpers - typeof";return d=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},d(e)}function f(e,t){if(d(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(d(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function p(e){var t=f(e,`string`);return d(t)==`symbol`?t:t+``}function m(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function h(e,t){return function(n,r){t(n,r,e)}}function g(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var _=class extends e.Disposable{};const v=(0,e.createIdentifier)(`find-replace.service`);function y(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let b=class extends e.Disposable{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,m(this,`currentMatch$`,new i.BehaviorSubject(null)),m(this,`replaceables$`,new i.BehaviorSubject([])),m(this,`_findModels`,[]),m(this,`_matchingModel`,null),m(this,`_matches`,[]),m(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe((0,i.throttleTime)(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;y(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...w(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(t){let n=this._currentSearchingDisposables=new e.DisposableCollection,r=(0,i.combineLatest)(t.map(e=>e.matchesUpdate$)).pipe((0,i.debounceTime)(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});t.forEach(t=>n.add((0,e.toDisposable)(t.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),n.add((0,e.toDisposable)(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};b=g([h(2,e.IUniverInstanceService),h(3,e.ICommandService)],b);let x=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),S=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),C=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function w(){return{caseSensitive:!1,findBy:S.VALUE,findCompleted:!1,findDirection:x.ROW,findScope:C.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var T=class{constructor(){m(this,`_stateUpdates$`,new i.Subject),m(this,`stateUpdates$`,this._stateUpdates$.asObservable()),m(this,`_state$`,new i.BehaviorSubject(w())),m(this,`state$`,this._state$.asObservable()),m(this,`_findString`,``),m(this,`_inputtingFindString`,``),m(this,`_replaceString`,``),m(this,`_revealed`,!1),m(this,`_replaceRevealed`,!1),m(this,`_matchesPosition`,0),m(this,`_matchesCount`,0),m(this,`_caseSensitive`,!0),m(this,`_matchesTheWholeCell`,!1),m(this,`_findDirection`,x.ROW),m(this,`_findScope`,C.SUBUNIT),m(this,`_findBy`,S.VALUE),m(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let E=class extends e.Disposable{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,m(this,`_providers`,new Set),m(this,`_state`,new T),m(this,`_model`,void 0),m(this,`_currentMatch$`,new i.BehaviorSubject(null)),m(this,`currentMatch$`,this._currentMatch$.asObservable()),m(this,`_replaceables$`,new i.BehaviorSubject([])),m(this,`replaceables$`,this._replaceables$.asObservable()),m(this,`_focusSignal$`,new i.Subject),m(this,`focusSignal$`,this._focusSignal$.asObservable())}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===S.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 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(b,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=w();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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(t){return this._providers.add(t),(0,e.toDisposable)(()=>this._providers.delete(t))}_toggleRevealReplace(e){this._contextService.setContextValue(u,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(r.RENDER_RAW_FORMULA_KEY,e)}};E=g([h(0,(0,e.Inject)(e.Injector)),h(1,e.IContextService)],E);const D={id:`ui.command.replace-current-match`,type:e.CommandType.COMMAND,handler:e=>e.get(v).replace()},O={id:`ui.command.replace-all-matches`,type:e.CommandType.COMMAND,handler:async r=>{let i=r.get(e.IConfirmService),a=r.get(e.LocaleService),o=r.get(n.IMessageService);if(!await i.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:a.t(`find-replace.replace.confirm.title`)},cancelText:a.t(`button.cancel`),confirmText:a.t(`button.confirm`)}))return!1;let{success:s,failure:c}=await r.get(v).replaceAll();return c>0?(s===0?o.show({type:t.MessageType.Error,content:a.t(`find-replace.replace.all-failure`)}):o.show({type:t.MessageType.Warning,content:a.t(`find-replace.replace.partial-success`,`${s}`,`${c}`)}),!1):(o.show({type:t.MessageType.Success,content:a.t(`find-replace.replace.all-success`,`${s}`)}),!0)}},k={id:`ui.operation.open-find-dialog`,type:e.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.focusFindInput():t.start(),!0}},A={id:`ui.operation.open-replace-dialog`,type:e.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},j={type:e.CommandType.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(v).moveToNextMatch(),!0)},M={type:e.CommandType.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(v).moveToPreviousMatch(),!0)},N={type:e.CommandType.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(v).focusSelection(),!0)};function P(t){let r=t.get(e.IContextService);return{id:k.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:n.MenuItemType.BUTTON,hidden$:(0,n.getMenuHiddenObservable)(t,e.UniverInstanceType.UNIVER_SHEET),disabled$:(0,i.combineLatest)([r.subscribeContextValue$(e.EDITOR_ACTIVATED),r.subscribeContextValue$(e.FOCUSING_SHEET)]).pipe((0,i.map)(([e,t])=>e||!t))}}const F={[n.RibbonDataGroup.ORGANIZATION]:{[k.id]:{order:2,menuItemFactory:P}}};function I(e){let{findCompleted:n,localeService:r,matchesCount:i,matchesPosition:a,initialFindString:c,findReplaceService:l,onChange:u,...d}=e,[f,p]=(0,o.useState)(c),m=n&&i===0?r.t(`find-replace.dialog.no-result`):i===0?` `:void 0;function h(e){a===i&&e===1?l.moveToNextMatch():a===1&&e===i||e<a?l.moveToPreviousMatch():l.moveToNextMatch()}return(0,s.jsx)(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:(0,s.jsx)(t.Input,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:r.t(`find-replace.dialog.find-placeholder`),value:f,onChange:e=>{p(e),u==null||u(e)},slot:(0,s.jsx)(t.Pager,{loop:!0,text:m,value:a,total:i,onChange:h}),...d})})}function L(e,t){let n=(0,o.useCallback)(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=(0,o.useCallback)(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return(0,o.useImperativeHandle)(t,()=>({focus:n,selectHasFocus:r})),(0,o.useEffect)(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const R=(0,o.forwardRef)(function(t,r){let i=(0,n.useDependency)(e.LocaleService),a=(0,n.useDependency)(v),c=(0,n.useDependency)(e.ICommandService),{findCompleted:l,findString:u,matchesCount:d,matchesPosition:f}=(0,n.useObservable)(a.state$,void 0,!0),p=(0,o.useCallback)(()=>{c.executeCommand(A.id)},[c]),m=(0,n.useDebounceFn)(e=>a.changeFindString(e),500);return L(a,r),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(I,{findCompleted:l,matchesCount:d,matchesPosition:f,findReplaceService:a,localeService:i,initialFindString:u,onChange:m}),(0,s.jsx)(`div`,{className:`univer-mt-4 univer-text-center`,children:(0,s.jsx)(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:p,children:i.t(`find-replace.dialog.advanced-finding`)})})]})}),z=(0,o.forwardRef)(function(r,i){let a=(0,n.useDependency)(v),c=(0,n.useDependency)(e.LocaleService),l=(0,n.useDependency)(e.ICommandService),u=(0,n.useDependency)(n.IMessageService),d=(0,n.useObservable)(a.currentMatch$,void 0,!0),f=(0,n.useObservable)(a.replaceables$,void 0,!0),{matchesCount:p,matchesPosition:m,findString:h,inputtingFindString:g,replaceString:_,caseSensitive:y,matchesTheWholeCell:b,findDirection:x,findScope:S,findBy:C,findCompleted:w}=(0,n.useObservable)(a.state$,void 0,!0),T=g.length===0,E=p===0||!(d!=null&&d.replaceable),k=f.length===0,A=(0,o.useCallback)(e=>a.changeInputtingFindString(e),[a]),j=(0,o.useCallback)(e=>a.changeReplaceString(e),[a]),{focus:M}=L(a,i),N=(0,o.useCallback)(()=>{h===g?a.moveToNextMatch():(a.changeFindString(g),a.find())},[h,g,a]),P=(0,o.useCallback)(()=>l.executeCommand(D.id),[l]),F=(0,o.useCallback)(async()=>{await l.executeCommand(O.id),M()},[l]),R=(0,o.useCallback)(e=>{a.changeFindDirection(e)},[a]),z=(0,o.useCallback)(e=>{a.changeFindScope(e)},[a]),B=(0,o.useCallback)(e=>{a.changeFindBy(e)},[a]),U=V(c),W=H(c),G=ee(c);return(0,o.useEffect)(()=>{w&&p===0&&u.show({content:c.t(`find-replace.dialog.no-match`),type:t.MessageType.Warning,duration:5e3})},[w,p,u,c]),(0,s.jsxs)(`div`,{children:[(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find`),children:(0,s.jsx)(I,{findCompleted:w,className:`univer-find-input`,matchesCount:p,matchesPosition:m,findReplaceService:a,localeService:c,initialFindString:g,onChange:A})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.replace`),children:(0,s.jsx)(t.Input,{placeholder:c.t(`find-replace.dialog.replace-placeholder`),value:_,onChange:e=>j(e)})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-direction.title`),children:(0,s.jsx)(t.Select,{value:x,options:W,onChange:R})}),(0,s.jsx)(t.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-scope.title`),children:(0,s.jsx)(t.Select,{value:S,options:U,onChange:z})}),(0,s.jsx)(t.FormLayout,{label:c.t(`find-replace.dialog.find-by.title`),children:(0,s.jsx)(t.Select,{value:C,options:G,onChange:B})})]})}),(0,s.jsx)(t.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.FormLayout,{children:(0,s.jsx)(t.Checkbox,{checked:y,onChange:e=>{a.changeCaseSensitive(e)},children:c.t(`find-replace.dialog.case-sensitive`)})}),(0,s.jsx)(t.FormLayout,{children:(0,s.jsx)(t.Checkbox,{checked:b,onChange:e=>{a.changeMatchesTheWholeCell(e)},children:c.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),(0,s.jsxs)(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[(0,s.jsx)(t.Button,{variant:`primary`,onClick:N,disabled:T,children:c.t(`find-replace.dialog.find`)}),(0,s.jsxs)(`span`,{className:`univer-inline-flex univer-gap-2`,children:[(0,s.jsx)(t.Button,{disabled:E,onClick:P,children:c.t(`find-replace.dialog.replace`)}),(0,s.jsx)(t.Button,{disabled:k,onClick:F,children:c.t(`find-replace.dialog.replace-all`)})]})]})]})});function B(){let t=(0,n.useDependency)(v),r=(0,n.useDependency)(n.ILayoutService),a=(0,n.useDependency)(e.IContextService),u=(0,n.useObservable)(t.state$,void 0,!0),d=(0,o.useRef)(null);(0,o.useEffect)(()=>{let e;return d.current&&(e=r.registerContainerElement(d.current)),()=>e==null?void 0:e.dispose()},[r]);let f=(0,o.useRef)(null),p=(0,o.useCallback)(e=>a.setContextValue(l,e),[a]),m=(0,o.useCallback)(e=>a.setContextValue(c,e),[a]);return(0,o.useEffect)(()=>{var e;let t=(0,i.fromEvent)(document,`focusin`).subscribe(e=>{var t;e.target&&(t=d.current)!=null&&t.contains(e.target)?p(!0):p(!1),!f.current||!f.current.selectHasFocus()?m(!1):m(!0)});return(e=f.current)==null||e.focus(),p(!0),m(!0),()=>{t.unsubscribe(),p(!1)}},[p,m]),(0,s.jsx)(`div`,{ref:d,"data-u-comp":`find-replace-dialog`,children:u.replaceRevealed?(0,s.jsx)(z,{ref:f}):(0,s.jsx)(R,{ref:f})})}function V(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:C.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:C.UNIT}],[e.getCurrentLocale()])}function H(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:x.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:x.COLUMN}],[e.getCurrentLocale()])}function ee(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:S.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:S.FORMULA}],[e.getCurrentLocale()])}function U(e){return e.getContextValue(l)}function W(e){return e.getContextValue(u)}function G(e){return e.getContextValue(c)}const K=`7_find-replace-shortcuts`;function q(t){return t.getContextValue(e.FOCUSING_SHEET)}function J(t){return!t.getContextValue(e.EDITOR_ACTIVATED)}const te={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:n.KeyCode.F|n.MetaKeys.CTRL_COMMAND,group:K,preconditions(e){return!U(e)&&q(e)&&J(e)}},ne={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:n.KeyCode.F|n.MetaKeys.CTRL_COMMAND,mac:n.KeyCode.F|n.MetaKeys.MAC_CTRL,preconditions(e){return!U(e)&&q(e)&&J(e)}},re={id:A.id,description:`find-replace.shortcut.open-replace-dialog`,binding:n.KeyCode.H|n.MetaKeys.CTRL_COMMAND,mac:n.KeyCode.H|n.MetaKeys.MAC_CTRL,group:K,preconditions(e){return q(e)&&J(e)&&(!U(e)||!W(e))}},ie={id:j.id,description:`find-replace.shortcut.go-to-next-match`,binding:n.KeyCode.ENTER,group:K,priority:1e3,preconditions(e){return G(e)&&U(e)}},ae={id:M.id,description:`find-replace.shortcut.go-to-previous-match`,binding:n.KeyCode.ENTER|n.MetaKeys.SHIFT,group:K,priority:1e3,preconditions(e){return G(e)&&U(e)}},oe={id:N.id,description:`find-replace.shortcut.focus-selection`,binding:n.KeyCode.ESC,group:K,priority:1e3,preconditions(e){return U(e)}},Y=`DESKTOP_FIND_REPLACE_DIALOG`;let X=class extends e.RxDisposable{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,m(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[k,A,j,M,O,D,N].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[re,te,ne,ae,ie,oe].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,B],[`SearchIcon`,a.SearchIcon]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(F),this._findReplaceService.stateUpdates$.pipe((0,i.takeUntil)(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:Y,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:se(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=(0,e.toDisposable)(this._univerInstanceService.focused$.pipe((0,i.takeUntil)(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(Y),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};X=g([h(0,e.IUniverInstanceService),h(1,n.IMenuManagerService),h(2,n.IShortcutService),h(3,e.ICommandService),h(4,v),h(5,n.IDialogService),h(6,n.ILayoutService),h(7,(0,e.Inject)(e.LocaleService)),h(8,(0,e.Inject)(n.ComponentManager))],X);function se(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var ce=`@univerjs/find-replace`,le=`0.20.1`;const Z=`find-replace.config`;Symbol(Z);const Q={};let $=class extends e.Plugin{constructor(t=Q,n,r){super(),this._config=t,this._injector=n,this._configService=r;let{...i}=(0,e.merge)({},Q,this._config);this._configService.setConfig(Z,i)}onStarting(){[[X],[v,{useClass:E}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(X)}};m($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),m($,`packageName`,ce),m($,`version`,le),$=g([h(1,(0,e.Inject)(e.Injector)),h(2,e.IConfigService)],$),exports.FindBy=S,exports.FindDirection=x,exports.FindModel=_,Object.defineProperty(exports,`FindReplaceController`,{enumerable:!0,get:function(){return X}}),Object.defineProperty(exports,`FindReplaceModel`,{enumerable:!0,get:function(){return b}}),exports.FindReplaceState=T,exports.FindScope=C,exports.GoToNextMatchOperation=j,exports.GoToPreviousMatchOperation=M,exports.IFindReplaceService=v,exports.OpenFindDialogOperation=k,exports.OpenReplaceDialogOperation=A,exports.ReplaceAllMatchesCommand=O,exports.ReplaceCurrentMatchCommand=D,Object.defineProperty(exports,`UniverFindReplacePlugin`,{enumerable:!0,get:function(){return $}}),exports.createInitFindReplaceState=w;
package/lib/es/index.js CHANGED
@@ -1 +1 @@
1
- import{CommandType as e,Disposable as t,DisposableCollection as n,EDITOR_ACTIVATED as r,FOCUSING_SHEET as i,ICommandService as a,IConfigService as o,IConfirmService as s,IContextService as c,IUniverInstanceService as l,Inject as u,Injector as d,LocaleService as f,Plugin as p,RxDisposable as ee,UniverInstanceType as te,createIdentifier as ne,merge as re,toDisposable as m}from"@univerjs/core";import{Button as ie,Checkbox as ae,FormDualColumnLayout as oe,FormLayout as h,Input as se,MessageType as g,Pager as ce,Select as _}from"@univerjs/design";import{ComponentManager as v,IDialogService as le,ILayoutService as y,IMenuManagerService as ue,IMessageService as de,IShortcutService as fe,KeyCode as b,MenuItemType as pe,MetaKeys as x,RibbonDataGroup as me,getMenuHiddenObservable as he,useDebounceFn as ge,useDependency as S,useObservable as C}from"@univerjs/ui";import{RENDER_RAW_FORMULA_KEY as _e}from"@univerjs/engine-render";import{BehaviorSubject as w,Subject as T,combineLatest as ve,debounceTime as ye,fromEvent as be,map as xe,takeUntil as Se,throttleTime as Ce}from"rxjs";import{SearchIcon as we}from"@univerjs/icons";import{forwardRef as Te,useCallback as E,useEffect as D,useImperativeHandle as Ee,useMemo as O,useRef as De,useState as Oe}from"react";import{Fragment as k,jsx as A,jsxs as j}from"react/jsx-runtime";const ke=`FIND_REPLACE_INPUT_FOCUS`,Ae=`FIND_REPLACE_DIALOG_FOCUS`,je=`FIND_REPLACE_REPLACE_REVEALED`;function M(e){"@babel/helpers - typeof";return M=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},M(e)}function Me(e,t){if(M(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(M(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function Ne(e){var t=Me(e,`string`);return M(t)==`symbol`?t:t+``}function N(e,t,n){return(t=Ne(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(n,r){t(n,r,e)}}function F(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var Pe=class extends t{};const I=ne(`find-replace.service`);function Fe(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let L=class extends t{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,N(this,`currentMatch$`,new w(null)),N(this,`replaceables$`,new w([])),N(this,`_findModels`,[]),N(this,`_matchingModel`,null),N(this,`_matches`,[]),N(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe(Ce(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;Fe(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...V(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let t=this._currentSearchingDisposables=new n,r=ve(e.map(e=>e.matchesUpdate$)).pipe(ye(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>t.add(m(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),t.add(m(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};L=F([P(2,l),P(3,a)],L);let R=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),z=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),B=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function V(){return{caseSensitive:!1,findBy:z.VALUE,findCompleted:!1,findDirection:R.ROW,findScope:B.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var Ie=class{constructor(){N(this,`_stateUpdates$`,new T),N(this,`stateUpdates$`,this._stateUpdates$.asObservable()),N(this,`_state$`,new w(V())),N(this,`state$`,this._state$.asObservable()),N(this,`_findString`,``),N(this,`_inputtingFindString`,``),N(this,`_replaceString`,``),N(this,`_revealed`,!1),N(this,`_replaceRevealed`,!1),N(this,`_matchesPosition`,0),N(this,`_matchesCount`,0),N(this,`_caseSensitive`,!0),N(this,`_matchesTheWholeCell`,!1),N(this,`_findDirection`,R.ROW),N(this,`_findScope`,B.SUBUNIT),N(this,`_findBy`,z.VALUE),N(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let H=class extends t{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,N(this,`_providers`,new Set),N(this,`_state`,new Ie),N(this,`_model`,void 0),N(this,`_currentMatch$`,new w(null)),N(this,`currentMatch$`,this._currentMatch$.asObservable()),N(this,`_replaceables$`,new w([])),N(this,`replaceables$`,this._replaceables$.asObservable()),N(this,`_focusSignal$`,new T),N(this,`focusSignal$`,this._focusSignal$.asObservable())}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===z.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 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(L,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=V();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),m(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(je,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(_e,e)}};H=F([P(0,u(d)),P(1,c)],H);const U={id:`ui.command.replace-current-match`,type:e.COMMAND,handler:e=>e.get(I).replace()},W={id:`ui.command.replace-all-matches`,type:e.COMMAND,handler:async e=>{let t=e.get(s),n=e.get(f),r=e.get(de);if(!await t.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:n.t(`find-replace.replace.confirm.title`)},cancelText:n.t(`button.cancel`),confirmText:n.t(`button.confirm`)}))return!1;let{success:i,failure:a}=await e.get(I).replaceAll();return a>0?(i===0?r.show({type:g.Error,content:n.t(`find-replace.replace.all-failure`)}):r.show({type:g.Warning,content:n.t(`find-replace.replace.partial-success`,`${i}`,`${a}`)}),!1):(r.show({type:g.Success,content:n.t(`find-replace.replace.all-success`,`${i}`)}),!0)}},G={id:`ui.operation.open-find-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.focusFindInput():t.start(),!0}},K={id:`ui.operation.open-replace-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},q={type:e.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(I).moveToNextMatch(),!0)},J={type:e.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(I).moveToPreviousMatch(),!0)},Le={type:e.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(I).focusSelection(),!0)};function Re(e){let t=e.get(c);return{id:G.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:pe.BUTTON,hidden$:he(e,te.UNIVER_SHEET),disabled$:ve([t.subscribeContextValue$(r),t.subscribeContextValue$(i)]).pipe(xe(([e,t])=>e||!t))}}const ze={[me.ORGANIZATION]:{[G.id]:{order:2,menuItemFactory:Re}}};function Be(e){let{findCompleted:t,localeService:n,matchesCount:r,matchesPosition:i,initialFindString:a,findReplaceService:o,onChange:s,...c}=e,[l,u]=Oe(a),d=t&&r===0?n.t(`find-replace.dialog.no-result`):r===0?` `:void 0;function f(e){i===r&&e===1?o.moveToNextMatch():i===1&&e===r||e<i?o.moveToPreviousMatch():o.moveToNextMatch()}return A(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:A(se,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:n.t(`find-replace.dialog.find-placeholder`),value:l,onChange:e=>{u(e),s==null||s(e)},slot:A(ce,{loop:!0,text:d,value:i,total:r,onChange:f}),...c})})}function Ve(e,t){let n=E(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=E(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return Ee(t,()=>({focus:n,selectHasFocus:r})),D(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const He=Te(function(e,t){let n=S(f),r=S(I),i=S(a),{findCompleted:o,findString:s,matchesCount:c,matchesPosition:l}=C(r.state$,void 0,!0),u=E(()=>{i.executeCommand(K.id)},[i]),d=ge(e=>r.changeFindString(e),500);return Ve(r,t),j(k,{children:[A(Be,{findCompleted:o,matchesCount:c,matchesPosition:l,findReplaceService:r,localeService:n,initialFindString:s,onChange:d}),A(`div`,{className:`univer-mt-4 univer-text-center`,children:A(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:u,children:n.t(`find-replace.dialog.advanced-finding`)})})]})}),Ue=Te(function(e,t){let n=S(I),r=S(f),i=S(a),o=S(de),s=C(n.currentMatch$,void 0,!0),c=C(n.replaceables$,void 0,!0),{matchesCount:l,matchesPosition:u,findString:d,inputtingFindString:p,replaceString:ee,caseSensitive:te,matchesTheWholeCell:ne,findDirection:re,findScope:m,findBy:ce,findCompleted:v}=C(n.state$,void 0,!0),le=p.length===0,y=l===0||!(s!=null&&s.replaceable),ue=c.length===0,fe=E(e=>n.changeInputtingFindString(e),[n]),b=E(e=>n.changeReplaceString(e),[n]),{focus:pe}=Ve(n,t),x=E(()=>{d===p?n.moveToNextMatch():(n.changeFindString(p),n.find())},[d,p,n]),me=E(()=>i.executeCommand(U.id),[i]),he=E(async()=>{await i.executeCommand(W.id),pe()},[i]),ge=E(e=>{n.changeFindDirection(e)},[n]),_e=E(e=>{n.changeFindScope(e)},[n]),w=E(e=>{n.changeFindBy(e)},[n]),T=Ge(r),ve=Ke(r),ye=qe(r);return D(()=>{v&&l===0&&o.show({content:r.t(`find-replace.dialog.no-match`),type:g.Warning,duration:5e3})},[v,l,o,r]),j(`div`,{children:[A(h,{label:r.t(`find-replace.dialog.find`),children:A(Be,{findCompleted:v,className:`univer-find-input`,matchesCount:l,matchesPosition:u,findReplaceService:n,localeService:r,initialFindString:p,onChange:fe})}),A(h,{label:r.t(`find-replace.dialog.replace`),children:A(se,{placeholder:r.t(`find-replace.dialog.replace-placeholder`),value:ee,onChange:e=>b(e)})}),A(h,{label:r.t(`find-replace.dialog.find-direction.title`),children:A(_,{value:re,options:ve,onChange:ge})}),A(oe,{children:j(k,{children:[A(h,{label:r.t(`find-replace.dialog.find-scope.title`),children:A(_,{value:m,options:T,onChange:_e})}),A(h,{label:r.t(`find-replace.dialog.find-by.title`),children:A(_,{value:ce,options:ye,onChange:w})})]})}),A(oe,{children:j(k,{children:[A(h,{children:A(ae,{checked:te,onChange:e=>{n.changeCaseSensitive(e)},children:r.t(`find-replace.dialog.case-sensitive`)})}),A(h,{children:A(ae,{checked:ne,onChange:e=>{n.changeMatchesTheWholeCell(e)},children:r.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),j(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[A(ie,{variant:`primary`,onClick:x,disabled:le,children:r.t(`find-replace.dialog.find`)}),j(`span`,{className:`univer-inline-flex univer-gap-2`,children:[A(ie,{disabled:y,onClick:me,children:r.t(`find-replace.dialog.replace`)}),A(ie,{disabled:ue,onClick:he,children:r.t(`find-replace.dialog.replace-all`)})]})]})]})});function We(){let e=S(I),t=S(y),n=S(c),r=C(e.state$,void 0,!0),i=De(null);D(()=>{let e;return i.current&&(e=t.registerContainerElement(i.current)),()=>e==null?void 0:e.dispose()},[t]);let a=De(null),o=E(e=>n.setContextValue(Ae,e),[n]),s=E(e=>n.setContextValue(ke,e),[n]);return D(()=>{var e;let t=be(document,`focusin`).subscribe(e=>{var t;e.target&&(t=i.current)!=null&&t.contains(e.target)?o(!0):o(!1),!a.current||!a.current.selectHasFocus()?s(!1):s(!0)});return(e=a.current)==null||e.focus(),o(!0),s(!0),()=>{t.unsubscribe(),o(!1)}},[o,s]),A(`div`,{ref:i,"data-u-comp":`find-replace-dialog`,children:r.replaceRevealed?A(Ue,{ref:a}):A(He,{ref:a})})}function Ge(e){return O(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:B.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:B.UNIT}],[e.getCurrentLocale()])}function Ke(e){return O(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:R.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:R.COLUMN}],[e.getCurrentLocale()])}function qe(e){return O(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:z.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:z.FORMULA}],[e.getCurrentLocale()])}function Y(e){return e.getContextValue(Ae)}function Je(e){return e.getContextValue(je)}function Ye(e){return e.getContextValue(ke)}const X=`7_find-replace-shortcuts`;function Z(e){return e.getContextValue(i)}function Xe(e){return!e.getContextValue(r)}const Ze={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,group:X,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},Qe={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,mac:b.F|x.MAC_CTRL,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},$e={id:K.id,description:`find-replace.shortcut.open-replace-dialog`,binding:b.H|x.CTRL_COMMAND,mac:b.H|x.MAC_CTRL,group:X,preconditions(e){return Z(e)&&Xe(e)&&(!Y(e)||!Je(e))}},et={id:q.id,description:`find-replace.shortcut.go-to-next-match`,binding:b.ENTER,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},tt={id:J.id,description:`find-replace.shortcut.go-to-previous-match`,binding:b.ENTER|x.SHIFT,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},nt={id:Le.id,description:`find-replace.shortcut.focus-selection`,binding:b.ESC,group:X,priority:1e3,preconditions(e){return Y(e)}},rt=`DESKTOP_FIND_REPLACE_DIALOG`;let Q=class extends ee{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,N(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[G,K,q,J,W,U,Le].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[$e,Ze,Qe,tt,et,nt].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,We],[`SearchIcon`,we]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ze),this._findReplaceService.stateUpdates$.pipe(Se(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:rt,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:it(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=m(this._univerInstanceService.focused$.pipe(Se(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(rt),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};Q=F([P(0,l),P(1,ue),P(2,fe),P(3,a),P(4,I),P(5,le),P(6,y),P(7,u(f)),P(8,u(v))],Q);function it(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var at=`@univerjs/find-replace`,ot=`0.20.0`;const st=`find-replace.config`;Symbol(st);const ct={};let $=class extends p{constructor(e=ct,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=re({},ct,this._config);this._configService.setConfig(st,r)}onStarting(){[[Q],[I,{useClass:H}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(Q)}};N($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),N($,`packageName`,at),N($,`version`,ot),$=F([P(1,u(d)),P(2,o)],$);export{z as FindBy,R as FindDirection,Pe as FindModel,Q as FindReplaceController,L as FindReplaceModel,Ie as FindReplaceState,B as FindScope,q as GoToNextMatchOperation,J as GoToPreviousMatchOperation,I as IFindReplaceService,G as OpenFindDialogOperation,K as OpenReplaceDialogOperation,W as ReplaceAllMatchesCommand,U as ReplaceCurrentMatchCommand,$ as UniverFindReplacePlugin,V as createInitFindReplaceState};
1
+ import{CommandType as e,Disposable as t,DisposableCollection as n,EDITOR_ACTIVATED as r,FOCUSING_SHEET as i,ICommandService as a,IConfigService as o,IConfirmService as s,IContextService as c,IUniverInstanceService as l,Inject as u,Injector as d,LocaleService as f,Plugin as p,RxDisposable as ee,UniverInstanceType as te,createIdentifier as ne,merge as re,toDisposable as m}from"@univerjs/core";import{Button as ie,Checkbox as ae,FormDualColumnLayout as oe,FormLayout as h,Input as se,MessageType as g,Pager as ce,Select as _}from"@univerjs/design";import{ComponentManager as v,IDialogService as le,ILayoutService as y,IMenuManagerService as ue,IMessageService as de,IShortcutService as fe,KeyCode as b,MenuItemType as pe,MetaKeys as x,RibbonDataGroup as me,getMenuHiddenObservable as he,useDebounceFn as ge,useDependency as S,useObservable as C}from"@univerjs/ui";import{RENDER_RAW_FORMULA_KEY as _e}from"@univerjs/engine-render";import{BehaviorSubject as w,Subject as T,combineLatest as ve,debounceTime as ye,fromEvent as be,map as xe,takeUntil as Se,throttleTime as Ce}from"rxjs";import{SearchIcon as we}from"@univerjs/icons";import{forwardRef as Te,useCallback as E,useEffect as D,useImperativeHandle as Ee,useMemo as O,useRef as De,useState as Oe}from"react";import{Fragment as k,jsx as A,jsxs as j}from"react/jsx-runtime";const ke=`FIND_REPLACE_INPUT_FOCUS`,Ae=`FIND_REPLACE_DIALOG_FOCUS`,je=`FIND_REPLACE_REPLACE_REVEALED`;function M(e){"@babel/helpers - typeof";return M=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},M(e)}function Me(e,t){if(M(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(M(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function Ne(e){var t=Me(e,`string`);return M(t)==`symbol`?t:t+``}function N(e,t,n){return(t=Ne(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(n,r){t(n,r,e)}}function F(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var Pe=class extends t{};const I=ne(`find-replace.service`);function Fe(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let L=class extends t{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,N(this,`currentMatch$`,new w(null)),N(this,`replaceables$`,new w([])),N(this,`_findModels`,[]),N(this,`_matchingModel`,null),N(this,`_matches`,[]),N(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe(Ce(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;Fe(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...V(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let t=this._currentSearchingDisposables=new n,r=ve(e.map(e=>e.matchesUpdate$)).pipe(ye(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>t.add(m(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),t.add(m(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};L=F([P(2,l),P(3,a)],L);let R=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),z=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),B=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function V(){return{caseSensitive:!1,findBy:z.VALUE,findCompleted:!1,findDirection:R.ROW,findScope:B.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var Ie=class{constructor(){N(this,`_stateUpdates$`,new T),N(this,`stateUpdates$`,this._stateUpdates$.asObservable()),N(this,`_state$`,new w(V())),N(this,`state$`,this._state$.asObservable()),N(this,`_findString`,``),N(this,`_inputtingFindString`,``),N(this,`_replaceString`,``),N(this,`_revealed`,!1),N(this,`_replaceRevealed`,!1),N(this,`_matchesPosition`,0),N(this,`_matchesCount`,0),N(this,`_caseSensitive`,!0),N(this,`_matchesTheWholeCell`,!1),N(this,`_findDirection`,R.ROW),N(this,`_findScope`,B.SUBUNIT),N(this,`_findBy`,z.VALUE),N(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let H=class extends t{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,N(this,`_providers`,new Set),N(this,`_state`,new Ie),N(this,`_model`,void 0),N(this,`_currentMatch$`,new w(null)),N(this,`currentMatch$`,this._currentMatch$.asObservable()),N(this,`_replaceables$`,new w([])),N(this,`replaceables$`,this._replaceables$.asObservable()),N(this,`_focusSignal$`,new T),N(this,`focusSignal$`,this._focusSignal$.asObservable())}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===z.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 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(L,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=V();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),m(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(je,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(_e,e)}};H=F([P(0,u(d)),P(1,c)],H);const U={id:`ui.command.replace-current-match`,type:e.COMMAND,handler:e=>e.get(I).replace()},W={id:`ui.command.replace-all-matches`,type:e.COMMAND,handler:async e=>{let t=e.get(s),n=e.get(f),r=e.get(de);if(!await t.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:n.t(`find-replace.replace.confirm.title`)},cancelText:n.t(`button.cancel`),confirmText:n.t(`button.confirm`)}))return!1;let{success:i,failure:a}=await e.get(I).replaceAll();return a>0?(i===0?r.show({type:g.Error,content:n.t(`find-replace.replace.all-failure`)}):r.show({type:g.Warning,content:n.t(`find-replace.replace.partial-success`,`${i}`,`${a}`)}),!1):(r.show({type:g.Success,content:n.t(`find-replace.replace.all-success`,`${i}`)}),!0)}},G={id:`ui.operation.open-find-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.focusFindInput():t.start(),!0}},K={id:`ui.operation.open-replace-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},q={type:e.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(I).moveToNextMatch(),!0)},J={type:e.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(I).moveToPreviousMatch(),!0)},Le={type:e.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(I).focusSelection(),!0)};function Re(e){let t=e.get(c);return{id:G.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:pe.BUTTON,hidden$:he(e,te.UNIVER_SHEET),disabled$:ve([t.subscribeContextValue$(r),t.subscribeContextValue$(i)]).pipe(xe(([e,t])=>e||!t))}}const ze={[me.ORGANIZATION]:{[G.id]:{order:2,menuItemFactory:Re}}};function Be(e){let{findCompleted:t,localeService:n,matchesCount:r,matchesPosition:i,initialFindString:a,findReplaceService:o,onChange:s,...c}=e,[l,u]=Oe(a),d=t&&r===0?n.t(`find-replace.dialog.no-result`):r===0?` `:void 0;function f(e){i===r&&e===1?o.moveToNextMatch():i===1&&e===r||e<i?o.moveToPreviousMatch():o.moveToNextMatch()}return A(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:A(se,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:n.t(`find-replace.dialog.find-placeholder`),value:l,onChange:e=>{u(e),s==null||s(e)},slot:A(ce,{loop:!0,text:d,value:i,total:r,onChange:f}),...c})})}function Ve(e,t){let n=E(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=E(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return Ee(t,()=>({focus:n,selectHasFocus:r})),D(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const He=Te(function(e,t){let n=S(f),r=S(I),i=S(a),{findCompleted:o,findString:s,matchesCount:c,matchesPosition:l}=C(r.state$,void 0,!0),u=E(()=>{i.executeCommand(K.id)},[i]),d=ge(e=>r.changeFindString(e),500);return Ve(r,t),j(k,{children:[A(Be,{findCompleted:o,matchesCount:c,matchesPosition:l,findReplaceService:r,localeService:n,initialFindString:s,onChange:d}),A(`div`,{className:`univer-mt-4 univer-text-center`,children:A(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:u,children:n.t(`find-replace.dialog.advanced-finding`)})})]})}),Ue=Te(function(e,t){let n=S(I),r=S(f),i=S(a),o=S(de),s=C(n.currentMatch$,void 0,!0),c=C(n.replaceables$,void 0,!0),{matchesCount:l,matchesPosition:u,findString:d,inputtingFindString:p,replaceString:ee,caseSensitive:te,matchesTheWholeCell:ne,findDirection:re,findScope:m,findBy:ce,findCompleted:v}=C(n.state$,void 0,!0),le=p.length===0,y=l===0||!(s!=null&&s.replaceable),ue=c.length===0,fe=E(e=>n.changeInputtingFindString(e),[n]),b=E(e=>n.changeReplaceString(e),[n]),{focus:pe}=Ve(n,t),x=E(()=>{d===p?n.moveToNextMatch():(n.changeFindString(p),n.find())},[d,p,n]),me=E(()=>i.executeCommand(U.id),[i]),he=E(async()=>{await i.executeCommand(W.id),pe()},[i]),ge=E(e=>{n.changeFindDirection(e)},[n]),_e=E(e=>{n.changeFindScope(e)},[n]),w=E(e=>{n.changeFindBy(e)},[n]),T=Ge(r),ve=Ke(r),ye=qe(r);return D(()=>{v&&l===0&&o.show({content:r.t(`find-replace.dialog.no-match`),type:g.Warning,duration:5e3})},[v,l,o,r]),j(`div`,{children:[A(h,{label:r.t(`find-replace.dialog.find`),children:A(Be,{findCompleted:v,className:`univer-find-input`,matchesCount:l,matchesPosition:u,findReplaceService:n,localeService:r,initialFindString:p,onChange:fe})}),A(h,{label:r.t(`find-replace.dialog.replace`),children:A(se,{placeholder:r.t(`find-replace.dialog.replace-placeholder`),value:ee,onChange:e=>b(e)})}),A(h,{label:r.t(`find-replace.dialog.find-direction.title`),children:A(_,{value:re,options:ve,onChange:ge})}),A(oe,{children:j(k,{children:[A(h,{label:r.t(`find-replace.dialog.find-scope.title`),children:A(_,{value:m,options:T,onChange:_e})}),A(h,{label:r.t(`find-replace.dialog.find-by.title`),children:A(_,{value:ce,options:ye,onChange:w})})]})}),A(oe,{children:j(k,{children:[A(h,{children:A(ae,{checked:te,onChange:e=>{n.changeCaseSensitive(e)},children:r.t(`find-replace.dialog.case-sensitive`)})}),A(h,{children:A(ae,{checked:ne,onChange:e=>{n.changeMatchesTheWholeCell(e)},children:r.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),j(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[A(ie,{variant:`primary`,onClick:x,disabled:le,children:r.t(`find-replace.dialog.find`)}),j(`span`,{className:`univer-inline-flex univer-gap-2`,children:[A(ie,{disabled:y,onClick:me,children:r.t(`find-replace.dialog.replace`)}),A(ie,{disabled:ue,onClick:he,children:r.t(`find-replace.dialog.replace-all`)})]})]})]})});function We(){let e=S(I),t=S(y),n=S(c),r=C(e.state$,void 0,!0),i=De(null);D(()=>{let e;return i.current&&(e=t.registerContainerElement(i.current)),()=>e==null?void 0:e.dispose()},[t]);let a=De(null),o=E(e=>n.setContextValue(Ae,e),[n]),s=E(e=>n.setContextValue(ke,e),[n]);return D(()=>{var e;let t=be(document,`focusin`).subscribe(e=>{var t;e.target&&(t=i.current)!=null&&t.contains(e.target)?o(!0):o(!1),!a.current||!a.current.selectHasFocus()?s(!1):s(!0)});return(e=a.current)==null||e.focus(),o(!0),s(!0),()=>{t.unsubscribe(),o(!1)}},[o,s]),A(`div`,{ref:i,"data-u-comp":`find-replace-dialog`,children:r.replaceRevealed?A(Ue,{ref:a}):A(He,{ref:a})})}function Ge(e){return O(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:B.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:B.UNIT}],[e.getCurrentLocale()])}function Ke(e){return O(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:R.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:R.COLUMN}],[e.getCurrentLocale()])}function qe(e){return O(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:z.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:z.FORMULA}],[e.getCurrentLocale()])}function Y(e){return e.getContextValue(Ae)}function Je(e){return e.getContextValue(je)}function Ye(e){return e.getContextValue(ke)}const X=`7_find-replace-shortcuts`;function Z(e){return e.getContextValue(i)}function Xe(e){return!e.getContextValue(r)}const Ze={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,group:X,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},Qe={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,mac:b.F|x.MAC_CTRL,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},$e={id:K.id,description:`find-replace.shortcut.open-replace-dialog`,binding:b.H|x.CTRL_COMMAND,mac:b.H|x.MAC_CTRL,group:X,preconditions(e){return Z(e)&&Xe(e)&&(!Y(e)||!Je(e))}},et={id:q.id,description:`find-replace.shortcut.go-to-next-match`,binding:b.ENTER,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},tt={id:J.id,description:`find-replace.shortcut.go-to-previous-match`,binding:b.ENTER|x.SHIFT,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},nt={id:Le.id,description:`find-replace.shortcut.focus-selection`,binding:b.ESC,group:X,priority:1e3,preconditions(e){return Y(e)}},rt=`DESKTOP_FIND_REPLACE_DIALOG`;let Q=class extends ee{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,N(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[G,K,q,J,W,U,Le].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[$e,Ze,Qe,tt,et,nt].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,We],[`SearchIcon`,we]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ze),this._findReplaceService.stateUpdates$.pipe(Se(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:rt,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:it(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=m(this._univerInstanceService.focused$.pipe(Se(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(rt),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};Q=F([P(0,l),P(1,ue),P(2,fe),P(3,a),P(4,I),P(5,le),P(6,y),P(7,u(f)),P(8,u(v))],Q);function it(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var at=`@univerjs/find-replace`,ot=`0.20.1`;const st=`find-replace.config`;Symbol(st);const ct={};let $=class extends p{constructor(e=ct,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=re({},ct,this._config);this._configService.setConfig(st,r)}onStarting(){[[Q],[I,{useClass:H}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(Q)}};N($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),N($,`packageName`,at),N($,`version`,ot),$=F([P(1,u(d)),P(2,o)],$);export{z as FindBy,R as FindDirection,Pe as FindModel,Q as FindReplaceController,L as FindReplaceModel,Ie as FindReplaceState,B as FindScope,q as GoToNextMatchOperation,J as GoToPreviousMatchOperation,I as IFindReplaceService,G as OpenFindDialogOperation,K as OpenReplaceDialogOperation,W as ReplaceAllMatchesCommand,U as ReplaceCurrentMatchCommand,$ as UniverFindReplacePlugin,V as createInitFindReplaceState};
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- import{CommandType as e,Disposable as t,DisposableCollection as n,EDITOR_ACTIVATED as r,FOCUSING_SHEET as i,ICommandService as a,IConfigService as o,IConfirmService as s,IContextService as c,IUniverInstanceService as l,Inject as u,Injector as d,LocaleService as f,Plugin as p,RxDisposable as ee,UniverInstanceType as te,createIdentifier as ne,merge as re,toDisposable as m}from"@univerjs/core";import{Button as ie,Checkbox as ae,FormDualColumnLayout as oe,FormLayout as h,Input as se,MessageType as g,Pager as ce,Select as _}from"@univerjs/design";import{ComponentManager as v,IDialogService as le,ILayoutService as y,IMenuManagerService as ue,IMessageService as de,IShortcutService as fe,KeyCode as b,MenuItemType as pe,MetaKeys as x,RibbonDataGroup as me,getMenuHiddenObservable as he,useDebounceFn as ge,useDependency as S,useObservable as C}from"@univerjs/ui";import{RENDER_RAW_FORMULA_KEY as _e}from"@univerjs/engine-render";import{BehaviorSubject as w,Subject as T,combineLatest as ve,debounceTime as ye,fromEvent as be,map as xe,takeUntil as Se,throttleTime as Ce}from"rxjs";import{SearchIcon as we}from"@univerjs/icons";import{forwardRef as Te,useCallback as E,useEffect as D,useImperativeHandle as Ee,useMemo as O,useRef as De,useState as Oe}from"react";import{Fragment as k,jsx as A,jsxs as j}from"react/jsx-runtime";const ke=`FIND_REPLACE_INPUT_FOCUS`,Ae=`FIND_REPLACE_DIALOG_FOCUS`,je=`FIND_REPLACE_REPLACE_REVEALED`;function M(e){"@babel/helpers - typeof";return M=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},M(e)}function Me(e,t){if(M(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(M(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function Ne(e){var t=Me(e,`string`);return M(t)==`symbol`?t:t+``}function N(e,t,n){return(t=Ne(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(n,r){t(n,r,e)}}function F(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var Pe=class extends t{};const I=ne(`find-replace.service`);function Fe(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let L=class extends t{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,N(this,`currentMatch$`,new w(null)),N(this,`replaceables$`,new w([])),N(this,`_findModels`,[]),N(this,`_matchingModel`,null),N(this,`_matches`,[]),N(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe(Ce(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;Fe(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...V(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let t=this._currentSearchingDisposables=new n,r=ve(e.map(e=>e.matchesUpdate$)).pipe(ye(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>t.add(m(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),t.add(m(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};L=F([P(2,l),P(3,a)],L);let R=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),z=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),B=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function V(){return{caseSensitive:!1,findBy:z.VALUE,findCompleted:!1,findDirection:R.ROW,findScope:B.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var Ie=class{constructor(){N(this,`_stateUpdates$`,new T),N(this,`stateUpdates$`,this._stateUpdates$.asObservable()),N(this,`_state$`,new w(V())),N(this,`state$`,this._state$.asObservable()),N(this,`_findString`,``),N(this,`_inputtingFindString`,``),N(this,`_replaceString`,``),N(this,`_revealed`,!1),N(this,`_replaceRevealed`,!1),N(this,`_matchesPosition`,0),N(this,`_matchesCount`,0),N(this,`_caseSensitive`,!0),N(this,`_matchesTheWholeCell`,!1),N(this,`_findDirection`,R.ROW),N(this,`_findScope`,B.SUBUNIT),N(this,`_findBy`,z.VALUE),N(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let H=class extends t{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,N(this,`_providers`,new Set),N(this,`_state`,new Ie),N(this,`_model`,void 0),N(this,`_currentMatch$`,new w(null)),N(this,`currentMatch$`,this._currentMatch$.asObservable()),N(this,`_replaceables$`,new w([])),N(this,`replaceables$`,this._replaceables$.asObservable()),N(this,`_focusSignal$`,new T),N(this,`focusSignal$`,this._focusSignal$.asObservable())}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===z.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 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(L,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=V();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),m(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(je,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(_e,e)}};H=F([P(0,u(d)),P(1,c)],H);const U={id:`ui.command.replace-current-match`,type:e.COMMAND,handler:e=>e.get(I).replace()},W={id:`ui.command.replace-all-matches`,type:e.COMMAND,handler:async e=>{let t=e.get(s),n=e.get(f),r=e.get(de);if(!await t.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:n.t(`find-replace.replace.confirm.title`)},cancelText:n.t(`button.cancel`),confirmText:n.t(`button.confirm`)}))return!1;let{success:i,failure:a}=await e.get(I).replaceAll();return a>0?(i===0?r.show({type:g.Error,content:n.t(`find-replace.replace.all-failure`)}):r.show({type:g.Warning,content:n.t(`find-replace.replace.partial-success`,`${i}`,`${a}`)}),!1):(r.show({type:g.Success,content:n.t(`find-replace.replace.all-success`,`${i}`)}),!0)}},G={id:`ui.operation.open-find-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.focusFindInput():t.start(),!0}},K={id:`ui.operation.open-replace-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},q={type:e.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(I).moveToNextMatch(),!0)},J={type:e.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(I).moveToPreviousMatch(),!0)},Le={type:e.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(I).focusSelection(),!0)};function Re(e){let t=e.get(c);return{id:G.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:pe.BUTTON,hidden$:he(e,te.UNIVER_SHEET),disabled$:ve([t.subscribeContextValue$(r),t.subscribeContextValue$(i)]).pipe(xe(([e,t])=>e||!t))}}const ze={[me.ORGANIZATION]:{[G.id]:{order:2,menuItemFactory:Re}}};function Be(e){let{findCompleted:t,localeService:n,matchesCount:r,matchesPosition:i,initialFindString:a,findReplaceService:o,onChange:s,...c}=e,[l,u]=Oe(a),d=t&&r===0?n.t(`find-replace.dialog.no-result`):r===0?` `:void 0;function f(e){i===r&&e===1?o.moveToNextMatch():i===1&&e===r||e<i?o.moveToPreviousMatch():o.moveToNextMatch()}return A(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:A(se,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:n.t(`find-replace.dialog.find-placeholder`),value:l,onChange:e=>{u(e),s==null||s(e)},slot:A(ce,{loop:!0,text:d,value:i,total:r,onChange:f}),...c})})}function Ve(e,t){let n=E(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=E(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return Ee(t,()=>({focus:n,selectHasFocus:r})),D(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const He=Te(function(e,t){let n=S(f),r=S(I),i=S(a),{findCompleted:o,findString:s,matchesCount:c,matchesPosition:l}=C(r.state$,void 0,!0),u=E(()=>{i.executeCommand(K.id)},[i]),d=ge(e=>r.changeFindString(e),500);return Ve(r,t),j(k,{children:[A(Be,{findCompleted:o,matchesCount:c,matchesPosition:l,findReplaceService:r,localeService:n,initialFindString:s,onChange:d}),A(`div`,{className:`univer-mt-4 univer-text-center`,children:A(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:u,children:n.t(`find-replace.dialog.advanced-finding`)})})]})}),Ue=Te(function(e,t){let n=S(I),r=S(f),i=S(a),o=S(de),s=C(n.currentMatch$,void 0,!0),c=C(n.replaceables$,void 0,!0),{matchesCount:l,matchesPosition:u,findString:d,inputtingFindString:p,replaceString:ee,caseSensitive:te,matchesTheWholeCell:ne,findDirection:re,findScope:m,findBy:ce,findCompleted:v}=C(n.state$,void 0,!0),le=p.length===0,y=l===0||!(s!=null&&s.replaceable),ue=c.length===0,fe=E(e=>n.changeInputtingFindString(e),[n]),b=E(e=>n.changeReplaceString(e),[n]),{focus:pe}=Ve(n,t),x=E(()=>{d===p?n.moveToNextMatch():(n.changeFindString(p),n.find())},[d,p,n]),me=E(()=>i.executeCommand(U.id),[i]),he=E(async()=>{await i.executeCommand(W.id),pe()},[i]),ge=E(e=>{n.changeFindDirection(e)},[n]),_e=E(e=>{n.changeFindScope(e)},[n]),w=E(e=>{n.changeFindBy(e)},[n]),T=Ge(r),ve=Ke(r),ye=qe(r);return D(()=>{v&&l===0&&o.show({content:r.t(`find-replace.dialog.no-match`),type:g.Warning,duration:5e3})},[v,l,o,r]),j(`div`,{children:[A(h,{label:r.t(`find-replace.dialog.find`),children:A(Be,{findCompleted:v,className:`univer-find-input`,matchesCount:l,matchesPosition:u,findReplaceService:n,localeService:r,initialFindString:p,onChange:fe})}),A(h,{label:r.t(`find-replace.dialog.replace`),children:A(se,{placeholder:r.t(`find-replace.dialog.replace-placeholder`),value:ee,onChange:e=>b(e)})}),A(h,{label:r.t(`find-replace.dialog.find-direction.title`),children:A(_,{value:re,options:ve,onChange:ge})}),A(oe,{children:j(k,{children:[A(h,{label:r.t(`find-replace.dialog.find-scope.title`),children:A(_,{value:m,options:T,onChange:_e})}),A(h,{label:r.t(`find-replace.dialog.find-by.title`),children:A(_,{value:ce,options:ye,onChange:w})})]})}),A(oe,{children:j(k,{children:[A(h,{children:A(ae,{checked:te,onChange:e=>{n.changeCaseSensitive(e)},children:r.t(`find-replace.dialog.case-sensitive`)})}),A(h,{children:A(ae,{checked:ne,onChange:e=>{n.changeMatchesTheWholeCell(e)},children:r.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),j(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[A(ie,{variant:`primary`,onClick:x,disabled:le,children:r.t(`find-replace.dialog.find`)}),j(`span`,{className:`univer-inline-flex univer-gap-2`,children:[A(ie,{disabled:y,onClick:me,children:r.t(`find-replace.dialog.replace`)}),A(ie,{disabled:ue,onClick:he,children:r.t(`find-replace.dialog.replace-all`)})]})]})]})});function We(){let e=S(I),t=S(y),n=S(c),r=C(e.state$,void 0,!0),i=De(null);D(()=>{let e;return i.current&&(e=t.registerContainerElement(i.current)),()=>e==null?void 0:e.dispose()},[t]);let a=De(null),o=E(e=>n.setContextValue(Ae,e),[n]),s=E(e=>n.setContextValue(ke,e),[n]);return D(()=>{var e;let t=be(document,`focusin`).subscribe(e=>{var t;e.target&&(t=i.current)!=null&&t.contains(e.target)?o(!0):o(!1),!a.current||!a.current.selectHasFocus()?s(!1):s(!0)});return(e=a.current)==null||e.focus(),o(!0),s(!0),()=>{t.unsubscribe(),o(!1)}},[o,s]),A(`div`,{ref:i,"data-u-comp":`find-replace-dialog`,children:r.replaceRevealed?A(Ue,{ref:a}):A(He,{ref:a})})}function Ge(e){return O(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:B.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:B.UNIT}],[e.getCurrentLocale()])}function Ke(e){return O(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:R.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:R.COLUMN}],[e.getCurrentLocale()])}function qe(e){return O(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:z.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:z.FORMULA}],[e.getCurrentLocale()])}function Y(e){return e.getContextValue(Ae)}function Je(e){return e.getContextValue(je)}function Ye(e){return e.getContextValue(ke)}const X=`7_find-replace-shortcuts`;function Z(e){return e.getContextValue(i)}function Xe(e){return!e.getContextValue(r)}const Ze={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,group:X,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},Qe={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,mac:b.F|x.MAC_CTRL,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},$e={id:K.id,description:`find-replace.shortcut.open-replace-dialog`,binding:b.H|x.CTRL_COMMAND,mac:b.H|x.MAC_CTRL,group:X,preconditions(e){return Z(e)&&Xe(e)&&(!Y(e)||!Je(e))}},et={id:q.id,description:`find-replace.shortcut.go-to-next-match`,binding:b.ENTER,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},tt={id:J.id,description:`find-replace.shortcut.go-to-previous-match`,binding:b.ENTER|x.SHIFT,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},nt={id:Le.id,description:`find-replace.shortcut.focus-selection`,binding:b.ESC,group:X,priority:1e3,preconditions(e){return Y(e)}},rt=`DESKTOP_FIND_REPLACE_DIALOG`;let Q=class extends ee{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,N(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[G,K,q,J,W,U,Le].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[$e,Ze,Qe,tt,et,nt].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,We],[`SearchIcon`,we]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ze),this._findReplaceService.stateUpdates$.pipe(Se(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:rt,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:it(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=m(this._univerInstanceService.focused$.pipe(Se(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(rt),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};Q=F([P(0,l),P(1,ue),P(2,fe),P(3,a),P(4,I),P(5,le),P(6,y),P(7,u(f)),P(8,u(v))],Q);function it(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var at=`@univerjs/find-replace`,ot=`0.20.0`;const st=`find-replace.config`;Symbol(st);const ct={};let $=class extends p{constructor(e=ct,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=re({},ct,this._config);this._configService.setConfig(st,r)}onStarting(){[[Q],[I,{useClass:H}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(Q)}};N($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),N($,`packageName`,at),N($,`version`,ot),$=F([P(1,u(d)),P(2,o)],$);export{z as FindBy,R as FindDirection,Pe as FindModel,Q as FindReplaceController,L as FindReplaceModel,Ie as FindReplaceState,B as FindScope,q as GoToNextMatchOperation,J as GoToPreviousMatchOperation,I as IFindReplaceService,G as OpenFindDialogOperation,K as OpenReplaceDialogOperation,W as ReplaceAllMatchesCommand,U as ReplaceCurrentMatchCommand,$ as UniverFindReplacePlugin,V as createInitFindReplaceState};
1
+ import{CommandType as e,Disposable as t,DisposableCollection as n,EDITOR_ACTIVATED as r,FOCUSING_SHEET as i,ICommandService as a,IConfigService as o,IConfirmService as s,IContextService as c,IUniverInstanceService as l,Inject as u,Injector as d,LocaleService as f,Plugin as p,RxDisposable as ee,UniverInstanceType as te,createIdentifier as ne,merge as re,toDisposable as m}from"@univerjs/core";import{Button as ie,Checkbox as ae,FormDualColumnLayout as oe,FormLayout as h,Input as se,MessageType as g,Pager as ce,Select as _}from"@univerjs/design";import{ComponentManager as v,IDialogService as le,ILayoutService as y,IMenuManagerService as ue,IMessageService as de,IShortcutService as fe,KeyCode as b,MenuItemType as pe,MetaKeys as x,RibbonDataGroup as me,getMenuHiddenObservable as he,useDebounceFn as ge,useDependency as S,useObservable as C}from"@univerjs/ui";import{RENDER_RAW_FORMULA_KEY as _e}from"@univerjs/engine-render";import{BehaviorSubject as w,Subject as T,combineLatest as ve,debounceTime as ye,fromEvent as be,map as xe,takeUntil as Se,throttleTime as Ce}from"rxjs";import{SearchIcon as we}from"@univerjs/icons";import{forwardRef as Te,useCallback as E,useEffect as D,useImperativeHandle as Ee,useMemo as O,useRef as De,useState as Oe}from"react";import{Fragment as k,jsx as A,jsxs as j}from"react/jsx-runtime";const ke=`FIND_REPLACE_INPUT_FOCUS`,Ae=`FIND_REPLACE_DIALOG_FOCUS`,je=`FIND_REPLACE_REPLACE_REVEALED`;function M(e){"@babel/helpers - typeof";return M=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},M(e)}function Me(e,t){if(M(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(M(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function Ne(e){var t=Me(e,`string`);return M(t)==`symbol`?t:t+``}function N(e,t,n){return(t=Ne(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function P(e,t){return function(n,r){t(n,r,e)}}function F(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var Pe=class extends t{};const I=ne(`find-replace.service`);function Fe(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let L=class extends t{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,N(this,`currentMatch$`,new w(null)),N(this,`replaceables$`,new w([])),N(this,`_findModels`,[]),N(this,`_matchingModel`,null),N(this,`_matches`,[]),N(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe(Ce(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;Fe(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...V(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let t=this._currentSearchingDisposables=new n,r=ve(e.map(e=>e.matchesUpdate$)).pipe(ye(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>t.add(m(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),t.add(m(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};L=F([P(2,l),P(3,a)],L);let R=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),z=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),B=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function V(){return{caseSensitive:!1,findBy:z.VALUE,findCompleted:!1,findDirection:R.ROW,findScope:B.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var Ie=class{constructor(){N(this,`_stateUpdates$`,new T),N(this,`stateUpdates$`,this._stateUpdates$.asObservable()),N(this,`_state$`,new w(V())),N(this,`state$`,this._state$.asObservable()),N(this,`_findString`,``),N(this,`_inputtingFindString`,``),N(this,`_replaceString`,``),N(this,`_revealed`,!1),N(this,`_replaceRevealed`,!1),N(this,`_matchesPosition`,0),N(this,`_matchesCount`,0),N(this,`_caseSensitive`,!0),N(this,`_matchesTheWholeCell`,!1),N(this,`_findDirection`,R.ROW),N(this,`_findScope`,B.SUBUNIT),N(this,`_findBy`,z.VALUE),N(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let H=class extends t{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,N(this,`_providers`,new Set),N(this,`_state`,new Ie),N(this,`_model`,void 0),N(this,`_currentMatch$`,new w(null)),N(this,`currentMatch$`,this._currentMatch$.asObservable()),N(this,`_replaceables$`,new w([])),N(this,`replaceables$`,this._replaceables$.asObservable()),N(this,`_focusSignal$`,new T),N(this,`focusSignal$`,this._focusSignal$.asObservable())}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===z.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 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(L,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=V();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),m(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(je,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(_e,e)}};H=F([P(0,u(d)),P(1,c)],H);const U={id:`ui.command.replace-current-match`,type:e.COMMAND,handler:e=>e.get(I).replace()},W={id:`ui.command.replace-all-matches`,type:e.COMMAND,handler:async e=>{let t=e.get(s),n=e.get(f),r=e.get(de);if(!await t.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:n.t(`find-replace.replace.confirm.title`)},cancelText:n.t(`button.cancel`),confirmText:n.t(`button.confirm`)}))return!1;let{success:i,failure:a}=await e.get(I).replaceAll();return a>0?(i===0?r.show({type:g.Error,content:n.t(`find-replace.replace.all-failure`)}):r.show({type:g.Warning,content:n.t(`find-replace.replace.partial-success`,`${i}`,`${a}`)}),!1):(r.show({type:g.Success,content:n.t(`find-replace.replace.all-success`,`${i}`)}),!0)}},G={id:`ui.operation.open-find-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.focusFindInput():t.start(),!0}},K={id:`ui.operation.open-replace-dialog`,type:e.OPERATION,handler:e=>{let t=e.get(I);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},q={type:e.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(I).moveToNextMatch(),!0)},J={type:e.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(I).moveToPreviousMatch(),!0)},Le={type:e.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(I).focusSelection(),!0)};function Re(e){let t=e.get(c);return{id:G.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:pe.BUTTON,hidden$:he(e,te.UNIVER_SHEET),disabled$:ve([t.subscribeContextValue$(r),t.subscribeContextValue$(i)]).pipe(xe(([e,t])=>e||!t))}}const ze={[me.ORGANIZATION]:{[G.id]:{order:2,menuItemFactory:Re}}};function Be(e){let{findCompleted:t,localeService:n,matchesCount:r,matchesPosition:i,initialFindString:a,findReplaceService:o,onChange:s,...c}=e,[l,u]=Oe(a),d=t&&r===0?n.t(`find-replace.dialog.no-result`):r===0?` `:void 0;function f(e){i===r&&e===1?o.moveToNextMatch():i===1&&e===r||e<i?o.moveToPreviousMatch():o.moveToNextMatch()}return A(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:A(se,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:n.t(`find-replace.dialog.find-placeholder`),value:l,onChange:e=>{u(e),s==null||s(e)},slot:A(ce,{loop:!0,text:d,value:i,total:r,onChange:f}),...c})})}function Ve(e,t){let n=E(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=E(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return Ee(t,()=>({focus:n,selectHasFocus:r})),D(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}const He=Te(function(e,t){let n=S(f),r=S(I),i=S(a),{findCompleted:o,findString:s,matchesCount:c,matchesPosition:l}=C(r.state$,void 0,!0),u=E(()=>{i.executeCommand(K.id)},[i]),d=ge(e=>r.changeFindString(e),500);return Ve(r,t),j(k,{children:[A(Be,{findCompleted:o,matchesCount:c,matchesPosition:l,findReplaceService:r,localeService:n,initialFindString:s,onChange:d}),A(`div`,{className:`univer-mt-4 univer-text-center`,children:A(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:u,children:n.t(`find-replace.dialog.advanced-finding`)})})]})}),Ue=Te(function(e,t){let n=S(I),r=S(f),i=S(a),o=S(de),s=C(n.currentMatch$,void 0,!0),c=C(n.replaceables$,void 0,!0),{matchesCount:l,matchesPosition:u,findString:d,inputtingFindString:p,replaceString:ee,caseSensitive:te,matchesTheWholeCell:ne,findDirection:re,findScope:m,findBy:ce,findCompleted:v}=C(n.state$,void 0,!0),le=p.length===0,y=l===0||!(s!=null&&s.replaceable),ue=c.length===0,fe=E(e=>n.changeInputtingFindString(e),[n]),b=E(e=>n.changeReplaceString(e),[n]),{focus:pe}=Ve(n,t),x=E(()=>{d===p?n.moveToNextMatch():(n.changeFindString(p),n.find())},[d,p,n]),me=E(()=>i.executeCommand(U.id),[i]),he=E(async()=>{await i.executeCommand(W.id),pe()},[i]),ge=E(e=>{n.changeFindDirection(e)},[n]),_e=E(e=>{n.changeFindScope(e)},[n]),w=E(e=>{n.changeFindBy(e)},[n]),T=Ge(r),ve=Ke(r),ye=qe(r);return D(()=>{v&&l===0&&o.show({content:r.t(`find-replace.dialog.no-match`),type:g.Warning,duration:5e3})},[v,l,o,r]),j(`div`,{children:[A(h,{label:r.t(`find-replace.dialog.find`),children:A(Be,{findCompleted:v,className:`univer-find-input`,matchesCount:l,matchesPosition:u,findReplaceService:n,localeService:r,initialFindString:p,onChange:fe})}),A(h,{label:r.t(`find-replace.dialog.replace`),children:A(se,{placeholder:r.t(`find-replace.dialog.replace-placeholder`),value:ee,onChange:e=>b(e)})}),A(h,{label:r.t(`find-replace.dialog.find-direction.title`),children:A(_,{value:re,options:ve,onChange:ge})}),A(oe,{children:j(k,{children:[A(h,{label:r.t(`find-replace.dialog.find-scope.title`),children:A(_,{value:m,options:T,onChange:_e})}),A(h,{label:r.t(`find-replace.dialog.find-by.title`),children:A(_,{value:ce,options:ye,onChange:w})})]})}),A(oe,{children:j(k,{children:[A(h,{children:A(ae,{checked:te,onChange:e=>{n.changeCaseSensitive(e)},children:r.t(`find-replace.dialog.case-sensitive`)})}),A(h,{children:A(ae,{checked:ne,onChange:e=>{n.changeMatchesTheWholeCell(e)},children:r.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),j(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[A(ie,{variant:`primary`,onClick:x,disabled:le,children:r.t(`find-replace.dialog.find`)}),j(`span`,{className:`univer-inline-flex univer-gap-2`,children:[A(ie,{disabled:y,onClick:me,children:r.t(`find-replace.dialog.replace`)}),A(ie,{disabled:ue,onClick:he,children:r.t(`find-replace.dialog.replace-all`)})]})]})]})});function We(){let e=S(I),t=S(y),n=S(c),r=C(e.state$,void 0,!0),i=De(null);D(()=>{let e;return i.current&&(e=t.registerContainerElement(i.current)),()=>e==null?void 0:e.dispose()},[t]);let a=De(null),o=E(e=>n.setContextValue(Ae,e),[n]),s=E(e=>n.setContextValue(ke,e),[n]);return D(()=>{var e;let t=be(document,`focusin`).subscribe(e=>{var t;e.target&&(t=i.current)!=null&&t.contains(e.target)?o(!0):o(!1),!a.current||!a.current.selectHasFocus()?s(!1):s(!0)});return(e=a.current)==null||e.focus(),o(!0),s(!0),()=>{t.unsubscribe(),o(!1)}},[o,s]),A(`div`,{ref:i,"data-u-comp":`find-replace-dialog`,children:r.replaceRevealed?A(Ue,{ref:a}):A(He,{ref:a})})}function Ge(e){return O(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:B.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:B.UNIT}],[e.getCurrentLocale()])}function Ke(e){return O(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:R.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:R.COLUMN}],[e.getCurrentLocale()])}function qe(e){return O(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:z.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:z.FORMULA}],[e.getCurrentLocale()])}function Y(e){return e.getContextValue(Ae)}function Je(e){return e.getContextValue(je)}function Ye(e){return e.getContextValue(ke)}const X=`7_find-replace-shortcuts`;function Z(e){return e.getContextValue(i)}function Xe(e){return!e.getContextValue(r)}const Ze={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,group:X,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},Qe={id:G.id,description:`find-replace.shortcut.open-find-dialog`,binding:b.F|x.CTRL_COMMAND,mac:b.F|x.MAC_CTRL,preconditions(e){return!Y(e)&&Z(e)&&Xe(e)}},$e={id:K.id,description:`find-replace.shortcut.open-replace-dialog`,binding:b.H|x.CTRL_COMMAND,mac:b.H|x.MAC_CTRL,group:X,preconditions(e){return Z(e)&&Xe(e)&&(!Y(e)||!Je(e))}},et={id:q.id,description:`find-replace.shortcut.go-to-next-match`,binding:b.ENTER,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},tt={id:J.id,description:`find-replace.shortcut.go-to-previous-match`,binding:b.ENTER|x.SHIFT,group:X,priority:1e3,preconditions(e){return Ye(e)&&Y(e)}},nt={id:Le.id,description:`find-replace.shortcut.focus-selection`,binding:b.ESC,group:X,priority:1e3,preconditions(e){return Y(e)}},rt=`DESKTOP_FIND_REPLACE_DIALOG`;let Q=class extends ee{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,N(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[G,K,q,J,W,U,Le].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[$e,Ze,Qe,tt,et,nt].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,We],[`SearchIcon`,we]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ze),this._findReplaceService.stateUpdates$.pipe(Se(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:rt,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:it(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=m(this._univerInstanceService.focused$.pipe(Se(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(rt),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};Q=F([P(0,l),P(1,ue),P(2,fe),P(3,a),P(4,I),P(5,le),P(6,y),P(7,u(f)),P(8,u(v))],Q);function it(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var at=`@univerjs/find-replace`,ot=`0.20.1`;const st=`find-replace.config`;Symbol(st);const ct={};let $=class extends p{constructor(e=ct,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=re({},ct,this._config);this._configService.setConfig(st,r)}onStarting(){[[Q],[I,{useClass:H}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(Q)}};N($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),N($,`packageName`,at),N($,`version`,ot),$=F([P(1,u(d)),P(2,o)],$);export{z as FindBy,R as FindDirection,Pe as FindModel,Q as FindReplaceController,L as FindReplaceModel,Ie as FindReplaceState,B as FindScope,q as GoToNextMatchOperation,J as GoToPreviousMatchOperation,I as IFindReplaceService,G as OpenFindDialogOperation,K as OpenReplaceDialogOperation,W as ReplaceAllMatchesCommand,U as ReplaceCurrentMatchCommand,$ as UniverFindReplacePlugin,V as createInitFindReplaceState};
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`@univerjs/core`),require(`@univerjs/design`),require(`@univerjs/ui`),require(`@univerjs/engine-render`),require(`rxjs`),require(`react`),require(`react/jsx-runtime`)):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/design`,`@univerjs/ui`,`@univerjs/engine-render`,`rxjs`,`react`,`react/jsx-runtime`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverFindReplace={},e.UniverCore,e.UniverDesign,e.UniverUi,e.UniverEngineRender,e.rxjs,e.React,e.React))})(this,function(e,t,n,r,i,a,o,s){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let c=`FIND_REPLACE_INPUT_FOCUS`,l=`FIND_REPLACE_DIALOG_FOCUS`,u=`FIND_REPLACE_REPLACE_REVEALED`;function d(e){"@babel/helpers - typeof";return d=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},d(e)}function f(e,t){if(d(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(d(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function p(e){var t=f(e,`string`);return d(t)==`symbol`?t:t+``}function m(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function h(e,t){return function(n,r){t(n,r,e)}}function g(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var _=class extends t.Disposable{};let v=(0,t.createIdentifier)(`find-replace.service`);function y(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let b=class extends t.Disposable{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,m(this,`currentMatch$`,new a.BehaviorSubject(null)),m(this,`replaceables$`,new a.BehaviorSubject([])),m(this,`_findModels`,[]),m(this,`_matchingModel`,null),m(this,`_matches`,[]),m(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe((0,a.throttleTime)(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;y(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...w(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let n=this._currentSearchingDisposables=new t.DisposableCollection,r=(0,a.combineLatest)(e.map(e=>e.matchesUpdate$)).pipe((0,a.debounceTime)(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>n.add((0,t.toDisposable)(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),n.add((0,t.toDisposable)(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};b=g([h(2,t.IUniverInstanceService),h(3,t.ICommandService)],b);let x=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),S=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),C=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function w(){return{caseSensitive:!1,findBy:S.VALUE,findCompleted:!1,findDirection:x.ROW,findScope:C.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var T=class{constructor(){m(this,`_stateUpdates$`,new a.Subject),m(this,`stateUpdates$`,this._stateUpdates$.asObservable()),m(this,`_state$`,new a.BehaviorSubject(w())),m(this,`state$`,this._state$.asObservable()),m(this,`_findString`,``),m(this,`_inputtingFindString`,``),m(this,`_replaceString`,``),m(this,`_revealed`,!1),m(this,`_replaceRevealed`,!1),m(this,`_matchesPosition`,0),m(this,`_matchesCount`,0),m(this,`_caseSensitive`,!0),m(this,`_matchesTheWholeCell`,!1),m(this,`_findDirection`,x.ROW),m(this,`_findScope`,C.SUBUNIT),m(this,`_findBy`,S.VALUE),m(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let E=class extends t.Disposable{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,m(this,`_providers`,new Set),m(this,`_state`,new T),m(this,`_model`,void 0),m(this,`_currentMatch$`,new a.BehaviorSubject(null)),m(this,`currentMatch$`,this._currentMatch$.asObservable()),m(this,`_replaceables$`,new a.BehaviorSubject([])),m(this,`replaceables$`,this._replaceables$.asObservable()),m(this,`_focusSignal$`,new a.Subject),m(this,`focusSignal$`,this._focusSignal$.asObservable())}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===S.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 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(b,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=w();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),(0,t.toDisposable)(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(u,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(i.RENDER_RAW_FORMULA_KEY,e)}};E=g([h(0,(0,t.Inject)(t.Injector)),h(1,t.IContextService)],E);let D={id:`ui.command.replace-current-match`,type:t.CommandType.COMMAND,handler:e=>e.get(v).replace()},O={id:`ui.command.replace-all-matches`,type:t.CommandType.COMMAND,handler:async e=>{let i=e.get(t.IConfirmService),a=e.get(t.LocaleService),o=e.get(r.IMessageService);if(!await i.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:a.t(`find-replace.replace.confirm.title`)},cancelText:a.t(`button.cancel`),confirmText:a.t(`button.confirm`)}))return!1;let{success:s,failure:c}=await e.get(v).replaceAll();return c>0?(s===0?o.show({type:n.MessageType.Error,content:a.t(`find-replace.replace.all-failure`)}):o.show({type:n.MessageType.Warning,content:a.t(`find-replace.replace.partial-success`,`${s}`,`${c}`)}),!1):(o.show({type:n.MessageType.Success,content:a.t(`find-replace.replace.all-success`,`${s}`)}),!0)}},k={id:`ui.operation.open-find-dialog`,type:t.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.focusFindInput():t.start(),!0}},A={id:`ui.operation.open-replace-dialog`,type:t.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},j={type:t.CommandType.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(v).moveToNextMatch(),!0)},M={type:t.CommandType.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(v).moveToPreviousMatch(),!0)},N={type:t.CommandType.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(v).focusSelection(),!0)};function P({ref:e,...t}){let{icon:n,id:r,className:i,extend:a,...s}=t,c=`univerjs-icon univerjs-icon-${r} ${i||``}`.trim(),l=(0,o.useRef)(`_${R()}`);return F(n,`${r}`,{defIds:n.defIds,idSuffix:l.current},{ref:e,className:c,...s},a)}function F(e,t,n,r,i){return(0,o.createElement)(e.tag,{key:t,...I(e,n,i),...r},(L(e,n).children||[]).map((r,a)=>F(r,`${t}-${e.tag}-${a}`,n,void 0,i)))}function I(e,t,n){let r={...e.attrs};n!=null&&n.colorChannel1&&r.fill===`colorChannel1`&&(r.fill=n.colorChannel1),e.tag===`mask`&&r.id&&(r.id+=t.idSuffix),Object.entries(r).forEach(([e,n])=>{e===`mask`&&typeof n==`string`&&(r[e]=n.replace(/url\(#(.*)\)/,`url(#$1${t.idSuffix})`))});let{defIds:i}=t;return!i||i.length===0?r:(e.tag===`use`&&r[`xlink:href`]&&(r[`xlink:href`]+=t.idSuffix),Object.entries(r).forEach(([e,n])=>{typeof n==`string`&&(r[e]=n.replace(/url\(#(.*)\)/,`url(#$1${t.idSuffix})`))}),r)}function L(e,t){var n;let{defIds:r}=t;return!r||r.length===0?e:e.tag===`defs`&&(n=e.children)!=null&&n.length?{...e,children:e.children.map(e=>typeof e.attrs.id==`string`&&r&&r.includes(e.attrs.id)?{...e,attrs:{...e.attrs,id:e.attrs.id+t.idSuffix}}:e)}:e}function R(){return Math.random().toString(36).substring(2,8)}P.displayName=`UniverIcon`;let z={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`}}]},B=(0,o.forwardRef)(function(e,t){return(0,o.createElement)(P,Object.assign({},e,{id:`search-icon`,ref:t,icon:z}))});B.displayName=`SearchIcon`;function V(e){let n=e.get(t.IContextService);return{id:k.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:r.MenuItemType.BUTTON,hidden$:(0,r.getMenuHiddenObservable)(e,t.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.combineLatest)([n.subscribeContextValue$(t.EDITOR_ACTIVATED),n.subscribeContextValue$(t.FOCUSING_SHEET)]).pipe((0,a.map)(([e,t])=>e||!t))}}let ee={[r.RibbonDataGroup.ORGANIZATION]:{[k.id]:{order:2,menuItemFactory:V}}};function H(e){let{findCompleted:t,localeService:r,matchesCount:i,matchesPosition:a,initialFindString:c,findReplaceService:l,onChange:u,...d}=e,[f,p]=(0,o.useState)(c),m=t&&i===0?r.t(`find-replace.dialog.no-result`):i===0?` `:void 0;function h(e){a===i&&e===1?l.moveToNextMatch():a===1&&e===i||e<a?l.moveToPreviousMatch():l.moveToNextMatch()}return(0,s.jsx)(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:(0,s.jsx)(n.Input,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:r.t(`find-replace.dialog.find-placeholder`),value:f,onChange:e=>{p(e),u==null||u(e)},slot:(0,s.jsx)(n.Pager,{loop:!0,text:m,value:a,total:i,onChange:h}),...d})})}function U(e,t){let n=(0,o.useCallback)(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=(0,o.useCallback)(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return(0,o.useImperativeHandle)(t,()=>({focus:n,selectHasFocus:r})),(0,o.useEffect)(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}let te=(0,o.forwardRef)(function(e,n){let i=(0,r.useDependency)(t.LocaleService),a=(0,r.useDependency)(v),c=(0,r.useDependency)(t.ICommandService),{findCompleted:l,findString:u,matchesCount:d,matchesPosition:f}=(0,r.useObservable)(a.state$,void 0,!0),p=(0,o.useCallback)(()=>{c.executeCommand(A.id)},[c]),m=(0,r.useDebounceFn)(e=>a.changeFindString(e),500);return U(a,n),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(H,{findCompleted:l,matchesCount:d,matchesPosition:f,findReplaceService:a,localeService:i,initialFindString:u,onChange:m}),(0,s.jsx)(`div`,{className:`univer-mt-4 univer-text-center`,children:(0,s.jsx)(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:p,children:i.t(`find-replace.dialog.advanced-finding`)})})]})}),ne=(0,o.forwardRef)(function(e,i){let a=(0,r.useDependency)(v),c=(0,r.useDependency)(t.LocaleService),l=(0,r.useDependency)(t.ICommandService),u=(0,r.useDependency)(r.IMessageService),d=(0,r.useObservable)(a.currentMatch$,void 0,!0),f=(0,r.useObservable)(a.replaceables$,void 0,!0),{matchesCount:p,matchesPosition:m,findString:h,inputtingFindString:g,replaceString:_,caseSensitive:y,matchesTheWholeCell:b,findDirection:x,findScope:S,findBy:C,findCompleted:w}=(0,r.useObservable)(a.state$,void 0,!0),T=g.length===0,E=p===0||!(d!=null&&d.replaceable),k=f.length===0,A=(0,o.useCallback)(e=>a.changeInputtingFindString(e),[a]),j=(0,o.useCallback)(e=>a.changeReplaceString(e),[a]),{focus:M}=U(a,i),N=(0,o.useCallback)(()=>{h===g?a.moveToNextMatch():(a.changeFindString(g),a.find())},[h,g,a]),P=(0,o.useCallback)(()=>l.executeCommand(D.id),[l]),F=(0,o.useCallback)(async()=>{await l.executeCommand(O.id),M()},[l]),I=(0,o.useCallback)(e=>{a.changeFindDirection(e)},[a]),L=(0,o.useCallback)(e=>{a.changeFindScope(e)},[a]),R=(0,o.useCallback)(e=>{a.changeFindBy(e)},[a]),z=ie(c),B=ae(c),V=oe(c);return(0,o.useEffect)(()=>{w&&p===0&&u.show({content:c.t(`find-replace.dialog.no-match`),type:n.MessageType.Warning,duration:5e3})},[w,p,u,c]),(0,s.jsxs)(`div`,{children:[(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find`),children:(0,s.jsx)(H,{findCompleted:w,className:`univer-find-input`,matchesCount:p,matchesPosition:m,findReplaceService:a,localeService:c,initialFindString:g,onChange:A})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.replace`),children:(0,s.jsx)(n.Input,{placeholder:c.t(`find-replace.dialog.replace-placeholder`),value:_,onChange:e=>j(e)})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-direction.title`),children:(0,s.jsx)(n.Select,{value:x,options:B,onChange:I})}),(0,s.jsx)(n.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-scope.title`),children:(0,s.jsx)(n.Select,{value:S,options:z,onChange:L})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-by.title`),children:(0,s.jsx)(n.Select,{value:C,options:V,onChange:R})})]})}),(0,s.jsx)(n.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.FormLayout,{children:(0,s.jsx)(n.Checkbox,{checked:y,onChange:e=>{a.changeCaseSensitive(e)},children:c.t(`find-replace.dialog.case-sensitive`)})}),(0,s.jsx)(n.FormLayout,{children:(0,s.jsx)(n.Checkbox,{checked:b,onChange:e=>{a.changeMatchesTheWholeCell(e)},children:c.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),(0,s.jsxs)(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[(0,s.jsx)(n.Button,{variant:`primary`,onClick:N,disabled:T,children:c.t(`find-replace.dialog.find`)}),(0,s.jsxs)(`span`,{className:`univer-inline-flex univer-gap-2`,children:[(0,s.jsx)(n.Button,{disabled:E,onClick:P,children:c.t(`find-replace.dialog.replace`)}),(0,s.jsx)(n.Button,{disabled:k,onClick:F,children:c.t(`find-replace.dialog.replace-all`)})]})]})]})});function re(){let e=(0,r.useDependency)(v),n=(0,r.useDependency)(r.ILayoutService),i=(0,r.useDependency)(t.IContextService),u=(0,r.useObservable)(e.state$,void 0,!0),d=(0,o.useRef)(null);(0,o.useEffect)(()=>{let e;return d.current&&(e=n.registerContainerElement(d.current)),()=>e==null?void 0:e.dispose()},[n]);let f=(0,o.useRef)(null),p=(0,o.useCallback)(e=>i.setContextValue(l,e),[i]),m=(0,o.useCallback)(e=>i.setContextValue(c,e),[i]);return(0,o.useEffect)(()=>{var e;let t=(0,a.fromEvent)(document,`focusin`).subscribe(e=>{var t;e.target&&(t=d.current)!=null&&t.contains(e.target)?p(!0):p(!1),!f.current||!f.current.selectHasFocus()?m(!1):m(!0)});return(e=f.current)==null||e.focus(),p(!0),m(!0),()=>{t.unsubscribe(),p(!1)}},[p,m]),(0,s.jsx)(`div`,{ref:d,"data-u-comp":`find-replace-dialog`,children:u.replaceRevealed?(0,s.jsx)(ne,{ref:f}):(0,s.jsx)(te,{ref:f})})}function ie(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:C.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:C.UNIT}],[e.getCurrentLocale()])}function ae(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:x.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:x.COLUMN}],[e.getCurrentLocale()])}function oe(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:S.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:S.FORMULA}],[e.getCurrentLocale()])}function W(e){return e.getContextValue(l)}function se(e){return e.getContextValue(u)}function G(e){return e.getContextValue(c)}let K=`7_find-replace-shortcuts`;function q(e){return e.getContextValue(t.FOCUSING_SHEET)}function J(e){return!e.getContextValue(t.EDITOR_ACTIVATED)}let ce={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:r.KeyCode.F|r.MetaKeys.CTRL_COMMAND,group:K,preconditions(e){return!W(e)&&q(e)&&J(e)}},le={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:r.KeyCode.F|r.MetaKeys.CTRL_COMMAND,mac:r.KeyCode.F|r.MetaKeys.MAC_CTRL,preconditions(e){return!W(e)&&q(e)&&J(e)}},ue={id:A.id,description:`find-replace.shortcut.open-replace-dialog`,binding:r.KeyCode.H|r.MetaKeys.CTRL_COMMAND,mac:r.KeyCode.H|r.MetaKeys.MAC_CTRL,group:K,preconditions(e){return q(e)&&J(e)&&(!W(e)||!se(e))}},de={id:j.id,description:`find-replace.shortcut.go-to-next-match`,binding:r.KeyCode.ENTER,group:K,priority:1e3,preconditions(e){return G(e)&&W(e)}},fe={id:M.id,description:`find-replace.shortcut.go-to-previous-match`,binding:r.KeyCode.ENTER|r.MetaKeys.SHIFT,group:K,priority:1e3,preconditions(e){return G(e)&&W(e)}},pe={id:N.id,description:`find-replace.shortcut.focus-selection`,binding:r.KeyCode.ESC,group:K,priority:1e3,preconditions(e){return W(e)}},Y=`DESKTOP_FIND_REPLACE_DIALOG`,X=class extends t.RxDisposable{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,m(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[k,A,j,M,O,D,N].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[ue,ce,le,fe,de,pe].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,re],[`SearchIcon`,B]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ee),this._findReplaceService.stateUpdates$.pipe((0,a.takeUntil)(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:Y,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:me(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=(0,t.toDisposable)(this._univerInstanceService.focused$.pipe((0,a.takeUntil)(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(Y),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};X=g([h(0,t.IUniverInstanceService),h(1,r.IMenuManagerService),h(2,r.IShortcutService),h(3,t.ICommandService),h(4,v),h(5,r.IDialogService),h(6,r.ILayoutService),h(7,(0,t.Inject)(t.LocaleService)),h(8,(0,t.Inject)(r.ComponentManager))],X);function me(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var he=`@univerjs/find-replace`,ge=`0.20.0`;let Z=`find-replace.config`;Symbol(Z);let Q={},$=class extends t.Plugin{constructor(e=Q,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},Q,this._config);this._configService.setConfig(Z,i)}onStarting(){[[X],[v,{useClass:E}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(X)}};m($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),m($,`packageName`,he),m($,`version`,ge),$=g([h(1,(0,t.Inject)(t.Injector)),h(2,t.IConfigService)],$),e.FindBy=S,e.FindDirection=x,e.FindModel=_,Object.defineProperty(e,`FindReplaceController`,{enumerable:!0,get:function(){return X}}),Object.defineProperty(e,`FindReplaceModel`,{enumerable:!0,get:function(){return b}}),e.FindReplaceState=T,e.FindScope=C,e.GoToNextMatchOperation=j,e.GoToPreviousMatchOperation=M,e.IFindReplaceService=v,e.OpenFindDialogOperation=k,e.OpenReplaceDialogOperation=A,e.ReplaceAllMatchesCommand=O,e.ReplaceCurrentMatchCommand=D,Object.defineProperty(e,`UniverFindReplacePlugin`,{enumerable:!0,get:function(){return $}}),e.createInitFindReplaceState=w});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`@univerjs/core`),require(`@univerjs/design`),require(`@univerjs/ui`),require(`@univerjs/engine-render`),require(`rxjs`),require(`react`),require(`react/jsx-runtime`)):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/design`,`@univerjs/ui`,`@univerjs/engine-render`,`rxjs`,`react`,`react/jsx-runtime`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverFindReplace={},e.UniverCore,e.UniverDesign,e.UniverUi,e.UniverEngineRender,e.rxjs,e.React,e.React))})(this,function(e,t,n,r,i,a,o,s){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let c=`FIND_REPLACE_INPUT_FOCUS`,l=`FIND_REPLACE_DIALOG_FOCUS`,u=`FIND_REPLACE_REPLACE_REVEALED`;function d(e){"@babel/helpers - typeof";return d=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},d(e)}function f(e,t){if(d(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(d(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function p(e){var t=f(e,`string`);return d(t)==`symbol`?t:t+``}function m(e,t,n){return(t=p(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function h(e,t){return function(n,r){t(n,r,e)}}function g(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}var _=class extends t.Disposable{};let v=(0,t.createIdentifier)(`find-replace.service`);function y(e){return e.findString!==void 0||e.inputtingFindString!==void 0||e.findDirection!==void 0||e.matchesTheWholeCell!==void 0||e.caseSensitive!==void 0||e.findScope!==void 0||e.findBy!==void 0}let b=class extends t.Disposable{get searched(){return this._findModels.length>0}constructor(e,t,n,r){super(),this._state=e,this._providers=t,this._univerInstanceService=n,this._commandService=r,m(this,`currentMatch$`,new a.BehaviorSubject(null)),m(this,`replaceables$`,new a.BehaviorSubject([])),m(this,`_findModels`,[]),m(this,`_matchingModel`,null),m(this,`_matches`,[]),m(this,`_currentSearchingDisposables`,null),this.disposeWithMe(this._state.stateUpdates$.pipe((0,a.throttleTime)(200,void 0,{leading:!0,trailing:!0})).subscribe(async e=>{let t=this._state.state;y(e)&&(t.findString!==``&&!t.replaceRevealed?(await this._startSearching(),this._state.changeState({findCompleted:!0})):e.replaceRevealed!==!0&&this._stopSearching())}))}dispose(){super.dispose(),this._stopSearching(),this.currentMatch$.complete(),this.replaceables$.complete(),this._state.changeState({...w(),revealed:!1})}async start(){if(!this._state.findString)return{results:[]};let e=await this._startSearching();return this._state.changeState({findCompleted:!0}),e}focusSelection(){var e;(e=this._matchingModel)==null||e.focusSelection()}async _startSearching(){if(!this._state.findString)return{results:[]};let e=Array.from(this._providers),t=this._findModels=(await Promise.all(e.map(e=>e.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(t);let n=this._matches=t.map(e=>e.getMatches()).flat();return this.replaceables$.next(n.filter(e=>e.replaceable)),n.length?(this._moveToInitialMatch(t),this._state.changeState({matchesCount:n.length}),{results:n}):(this._state.changeState({matchesCount:0,matchesPosition:0}),{results:[]})}_stopSearching(){var e;this._providers.forEach(e=>e.terminate()),this._findModels=[],this._matches=[],this._matchingModel=null,(e=this._currentSearchingDisposables)==null||e.dispose(),this._currentSearchingDisposables=null,this.currentMatch$.next(null),this.replaceables$.next([]),this._state.changeState({findCompleted:!1,matchesCount:0,matchesPosition:0})}_subscribeToModelsChanges(e){let n=this._currentSearchingDisposables=new t.DisposableCollection,r=(0,a.combineLatest)(e.map(e=>e.matchesUpdate$)).pipe((0,a.debounceTime)(220)).subscribe(([...e])=>{let t=this._matches=e.flat();t.length?(this._moveToInitialMatch(this._findModels,!0),this._state.changeState({matchesCount:t.length}),this.replaceables$.next(t.filter(e=>e.replaceable))):(this._state.changeState({matchesCount:0,matchesPosition:0}),this.replaceables$.next([]))});e.forEach(e=>n.add((0,t.toDisposable)(e.activelyChangingMatch$.subscribe(e=>{let t=this._matches.findIndex(t=>t===e);this._state.changeState({matchesPosition:t+1})})))),n.add((0,t.toDisposable)(r))}async replace(){return this._matchingModel?this._matchingModel.replace(this._state.replaceString):!1}async replaceAll(){let e=await Promise.all(this._findModels.map(e=>e.replaceAll(this._state.replaceString))).then(e=>e.reduce((e,t)=>(e.success+=t.success,e.failure+=t.failure,e),{success:0,failure:0}));return e.failure===0&&this._stopSearching(),e}getCurrentMatch(){return this._state.matchesPosition>0?this._matches[this._state.matchesPosition-1]:null}_markMatch(e){let t=this._matches.findIndex(t=>t===e);this.currentMatch$.next(e),this._state.changeState({matchesPosition:t+1})}moveToNextMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToNextMatch({loop:e});if(t)return this._markMatch(t),t;{let e=this._findModels.findIndex(e=>e===this._matchingModel);return this._moveToNextUnitMatch(e)}}_moveToNextUnitMatch(e){let t=this._findModels.length;for(let n=(e+1)%t;n!==e;){let e=this._findModels[n],r=e.moveToNextMatch({ignoreSelection:!0});if(r)return this._matchingModel=e,this._markMatch(r),r;n=(n+1)%t}if(this._matchingModel){let e=this._matchingModel.moveToNextMatch({ignoreSelection:!0});return e&&this._markMatch(e),e}}moveToPreviousMatch(){if(!this._matchingModel)return;let e=this._findModels.length===1,t=this._matchingModel.moveToPreviousMatch({loop:e});if(t){let e=this._matches.findIndex(e=>e===t);return this.currentMatch$.next(t),this._state.changeState({matchesPosition:e+1}),t}else{let e=this._findModels.length,t=this._findModels.findIndex(e=>e===this._matchingModel);for(let n=(t-1+e)%e;n!==t;){let t=this._findModels[n],r=t.moveToPreviousMatch({ignoreSelection:!0});if(r)return this._matchingModel=t,this._markMatch(r),r;n=(n-1)%e}let n=this._matchingModel.moveToPreviousMatch({ignoreSelection:!0});return n&&this._markMatch(n),n}}_moveToInitialMatch(e,t=!1){var n;let r=(n=this._univerInstanceService.getFocusedUnit())==null?void 0:n.getUnitId();if(!r)return-1;let i=e.findIndex(e=>e.unitId===r);if(i!==-1){this._matchingModel=e[i];let n=this._matchingModel.moveToNextMatch({stayIfOnMatch:!0,noFocus:t});if(n)return this._markMatch(n),i}return this._moveToNextUnitMatch(i),0}};b=g([h(2,t.IUniverInstanceService),h(3,t.ICommandService)],b);let x=function(e){return e.ROW=`row`,e.COLUMN=`column`,e}({}),S=function(e){return e.VALUE=`value`,e.FORMULA=`formula`,e}({}),C=function(e){return e.SUBUNIT=`subunit`,e.UNIT=`unit`,e}({});function w(){return{caseSensitive:!1,findBy:S.VALUE,findCompleted:!1,findDirection:x.ROW,findScope:C.SUBUNIT,findString:``,inputtingFindString:``,matchesCount:0,matchesPosition:0,matchesTheWholeCell:!1,replaceRevealed:!1,replaceString:``,revealed:!0}}var T=class{constructor(){m(this,`_stateUpdates$`,new a.Subject),m(this,`stateUpdates$`,this._stateUpdates$.asObservable()),m(this,`_state$`,new a.BehaviorSubject(w())),m(this,`state$`,this._state$.asObservable()),m(this,`_findString`,``),m(this,`_inputtingFindString`,``),m(this,`_replaceString`,``),m(this,`_revealed`,!1),m(this,`_replaceRevealed`,!1),m(this,`_matchesPosition`,0),m(this,`_matchesCount`,0),m(this,`_caseSensitive`,!0),m(this,`_matchesTheWholeCell`,!1),m(this,`_findDirection`,x.ROW),m(this,`_findScope`,C.SUBUNIT),m(this,`_findBy`,S.VALUE),m(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 t=!1,n={};e.findString!==void 0&&e.findString!==this._findString&&(this._findString=e.findString,n.findString=this._findString,t=!0),e.revealed!==void 0&&e.revealed!==this._revealed&&(this._revealed=e.revealed,n.revealed=e.revealed,t=!0),e.replaceRevealed!==void 0&&e.replaceRevealed!==this._replaceRevealed&&(this._replaceRevealed=e.replaceRevealed,n.replaceRevealed=e.replaceRevealed,t=!0),e.replaceString!==void 0&&e.replaceString!==this._replaceString&&(this._replaceString=e.replaceString,n.replaceString=e.replaceString,t=!0),e.matchesCount!==void 0&&e.matchesCount!==this._matchesCount&&(this._matchesCount=e.matchesCount,n.matchesCount=e.matchesCount,t=!0),e.matchesPosition!==void 0&&e.matchesPosition!==this._matchesPosition&&(this._matchesPosition=e.matchesPosition,n.matchesPosition=e.matchesPosition,t=!0),e.findBy!==void 0&&e.findBy!==this._findBy&&(this._findBy=e.findBy,n.findBy=e.findBy,t=!0),e.findScope!==void 0&&e.findScope!==this._findScope&&(this._findScope=e.findScope,n.findScope=e.findScope,t=!0),e.findDirection!==void 0&&e.findDirection!==this._findDirection&&(this._findDirection=e.findDirection,n.findDirection=e.findDirection,t=!0),e.caseSensitive!==void 0&&e.caseSensitive!==this._caseSensitive&&(this._caseSensitive=e.caseSensitive,n.caseSensitive=e.caseSensitive,t=!0),e.matchesTheWholeCell!==void 0&&e.matchesTheWholeCell!==this._matchesTheWholeCell&&(this._matchesTheWholeCell=e.matchesTheWholeCell,n.matchesTheWholeCell=e.matchesTheWholeCell,t=!0),e.inputtingFindString!==void 0&&e.inputtingFindString!==this._inputtingFindString&&(this._inputtingFindString=e.inputtingFindString,n.inputtingFindString=e.inputtingFindString,t=!0),e.findCompleted!==void 0&&e.findCompleted!==this._findCompleted&&(this._findCompleted=e.findCompleted,n.findCompleted=e.findCompleted,t=!0),t&&(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(n))}};let E=class extends t.Disposable{get stateUpdates$(){return this._state.stateUpdates$}get state$(){return this._state.state$}get revealed(){return this._state.revealed}get replaceRevealed(){return this._state.replaceRevealed}constructor(e,t){super(),this._injector=e,this._contextService=t,m(this,`_providers`,new Set),m(this,`_state`,new T),m(this,`_model`,void 0),m(this,`_currentMatch$`,new a.BehaviorSubject(null)),m(this,`currentMatch$`,this._currentMatch$.asObservable()),m(this,`_replaceables$`,new a.BehaviorSubject([])),m(this,`replaceables$`,this._replaceables$.asObservable()),m(this,`_focusSignal$`,new a.Subject),m(this,`focusSignal$`,this._focusSignal$.asObservable())}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===S.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 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(b,this._state,this._providers),this._model.currentMatch$.subscribe(e=>this._currentMatch$.next(e)),this._model.replaceables$.subscribe(e=>this._replaceables$.next(e));let t=w();return e&&(t.replaceRevealed=!0),this._state.changeState(t),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),(0,t.toDisposable)(()=>this._providers.delete(e))}_toggleRevealReplace(e){this._contextService.setContextValue(u,e)}_toggleDisplayRawFormula(e){this._contextService.setContextValue(i.RENDER_RAW_FORMULA_KEY,e)}};E=g([h(0,(0,t.Inject)(t.Injector)),h(1,t.IContextService)],E);let D={id:`ui.command.replace-current-match`,type:t.CommandType.COMMAND,handler:e=>e.get(v).replace()},O={id:`ui.command.replace-all-matches`,type:t.CommandType.COMMAND,handler:async e=>{let i=e.get(t.IConfirmService),a=e.get(t.LocaleService),o=e.get(r.IMessageService);if(!await i.confirm({id:`CONFIRM_REPLACE_ALL`,title:{title:a.t(`find-replace.replace.confirm.title`)},cancelText:a.t(`button.cancel`),confirmText:a.t(`button.confirm`)}))return!1;let{success:s,failure:c}=await e.get(v).replaceAll();return c>0?(s===0?o.show({type:n.MessageType.Error,content:a.t(`find-replace.replace.all-failure`)}):o.show({type:n.MessageType.Warning,content:a.t(`find-replace.replace.partial-success`,`${s}`,`${c}`)}),!1):(o.show({type:n.MessageType.Success,content:a.t(`find-replace.replace.all-success`,`${s}`)}),!0)}},k={id:`ui.operation.open-find-dialog`,type:t.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.focusFindInput():t.start(),!0}},A={id:`ui.operation.open-replace-dialog`,type:t.CommandType.OPERATION,handler:e=>{let t=e.get(v);return t.revealed?t.replaceRevealed?t.focusFindInput():t.revealReplace():t.start(!0),!0}},j={type:t.CommandType.OPERATION,id:`ui.operation.go-to-next-match`,handler:e=>(e.get(v).moveToNextMatch(),!0)},M={type:t.CommandType.OPERATION,id:`ui.operation.go-to-previous-match`,handler:e=>(e.get(v).moveToPreviousMatch(),!0)},N={type:t.CommandType.OPERATION,id:`ui.operation.focus-selection`,handler:e=>(e.get(v).focusSelection(),!0)};function P({ref:e,...t}){let{icon:n,id:r,className:i,extend:a,...s}=t,c=`univerjs-icon univerjs-icon-${r} ${i||``}`.trim(),l=(0,o.useRef)(`_${R()}`);return F(n,`${r}`,{defIds:n.defIds,idSuffix:l.current},{ref:e,className:c,...s},a)}function F(e,t,n,r,i){return(0,o.createElement)(e.tag,{key:t,...I(e,n,i),...r},(L(e,n).children||[]).map((r,a)=>F(r,`${t}-${e.tag}-${a}`,n,void 0,i)))}function I(e,t,n){let r={...e.attrs};n!=null&&n.colorChannel1&&r.fill===`colorChannel1`&&(r.fill=n.colorChannel1),e.tag===`mask`&&r.id&&(r.id+=t.idSuffix),Object.entries(r).forEach(([e,n])=>{e===`mask`&&typeof n==`string`&&(r[e]=n.replace(/url\(#(.*)\)/,`url(#$1${t.idSuffix})`))});let{defIds:i}=t;return!i||i.length===0?r:(e.tag===`use`&&r[`xlink:href`]&&(r[`xlink:href`]+=t.idSuffix),Object.entries(r).forEach(([e,n])=>{typeof n==`string`&&(r[e]=n.replace(/url\(#(.*)\)/,`url(#$1${t.idSuffix})`))}),r)}function L(e,t){var n;let{defIds:r}=t;return!r||r.length===0?e:e.tag===`defs`&&(n=e.children)!=null&&n.length?{...e,children:e.children.map(e=>typeof e.attrs.id==`string`&&r&&r.includes(e.attrs.id)?{...e,attrs:{...e.attrs,id:e.attrs.id+t.idSuffix}}:e)}:e}function R(){return Math.random().toString(36).substring(2,8)}P.displayName=`UniverIcon`;let z={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`}}]},B=(0,o.forwardRef)(function(e,t){return(0,o.createElement)(P,Object.assign({},e,{id:`search-icon`,ref:t,icon:z}))});B.displayName=`SearchIcon`;function V(e){let n=e.get(t.IContextService);return{id:k.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:r.MenuItemType.BUTTON,hidden$:(0,r.getMenuHiddenObservable)(e,t.UniverInstanceType.UNIVER_SHEET),disabled$:(0,a.combineLatest)([n.subscribeContextValue$(t.EDITOR_ACTIVATED),n.subscribeContextValue$(t.FOCUSING_SHEET)]).pipe((0,a.map)(([e,t])=>e||!t))}}let ee={[r.RibbonDataGroup.ORGANIZATION]:{[k.id]:{order:2,menuItemFactory:V}}};function H(e){let{findCompleted:t,localeService:r,matchesCount:i,matchesPosition:a,initialFindString:c,findReplaceService:l,onChange:u,...d}=e,[f,p]=(0,o.useState)(c),m=t&&i===0?r.t(`find-replace.dialog.no-result`):i===0?` `:void 0;function h(e){a===i&&e===1?l.moveToNextMatch():a===1&&e===i||e<a?l.moveToPreviousMatch():l.moveToNextMatch()}return(0,s.jsx)(`div`,{className:`univer-relative univer-flex univer-items-center univer-gap-2`,onDrag:e=>e.stopPropagation(),children:(0,s.jsx)(n.Input,{"data-u-comp":`search-input`,autoFocus:!0,placeholder:r.t(`find-replace.dialog.find-placeholder`),value:f,onChange:e=>{p(e),u==null||u(e)},slot:(0,s.jsx)(n.Pager,{loop:!0,text:m,value:a,total:i,onChange:h}),...d})})}function U(e,t){let n=(0,o.useCallback)(()=>{var e;(e=document.querySelector(`.univer-find-input input`))==null||e.focus()},[]),r=(0,o.useCallback)(()=>{let e=document.querySelectorAll(`[data-u-comp=find-replace-dialog] [data-u-comp=search-input]`);return Array.from(e).some(e=>e===document.activeElement)},[]);return(0,o.useImperativeHandle)(t,()=>({focus:n,selectHasFocus:r})),(0,o.useEffect)(()=>{let t=e.focusSignal$.subscribe(()=>n());return()=>t.unsubscribe()},[e,n]),{focus:n,selectHasFocus:r}}let te=(0,o.forwardRef)(function(e,n){let i=(0,r.useDependency)(t.LocaleService),a=(0,r.useDependency)(v),c=(0,r.useDependency)(t.ICommandService),{findCompleted:l,findString:u,matchesCount:d,matchesPosition:f}=(0,r.useObservable)(a.state$,void 0,!0),p=(0,o.useCallback)(()=>{c.executeCommand(A.id)},[c]),m=(0,r.useDebounceFn)(e=>a.changeFindString(e),500);return U(a,n),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(H,{findCompleted:l,matchesCount:d,matchesPosition:f,findReplaceService:a,localeService:i,initialFindString:u,onChange:m}),(0,s.jsx)(`div`,{className:`univer-mt-4 univer-text-center`,children:(0,s.jsx)(`a`,{className:`hover:univer-text-primary-500/80 univer-cursor-pointer univer-text-sm univer-text-primary-500 univer-transition-colors`,onClick:p,children:i.t(`find-replace.dialog.advanced-finding`)})})]})}),ne=(0,o.forwardRef)(function(e,i){let a=(0,r.useDependency)(v),c=(0,r.useDependency)(t.LocaleService),l=(0,r.useDependency)(t.ICommandService),u=(0,r.useDependency)(r.IMessageService),d=(0,r.useObservable)(a.currentMatch$,void 0,!0),f=(0,r.useObservable)(a.replaceables$,void 0,!0),{matchesCount:p,matchesPosition:m,findString:h,inputtingFindString:g,replaceString:_,caseSensitive:y,matchesTheWholeCell:b,findDirection:x,findScope:S,findBy:C,findCompleted:w}=(0,r.useObservable)(a.state$,void 0,!0),T=g.length===0,E=p===0||!(d!=null&&d.replaceable),k=f.length===0,A=(0,o.useCallback)(e=>a.changeInputtingFindString(e),[a]),j=(0,o.useCallback)(e=>a.changeReplaceString(e),[a]),{focus:M}=U(a,i),N=(0,o.useCallback)(()=>{h===g?a.moveToNextMatch():(a.changeFindString(g),a.find())},[h,g,a]),P=(0,o.useCallback)(()=>l.executeCommand(D.id),[l]),F=(0,o.useCallback)(async()=>{await l.executeCommand(O.id),M()},[l]),I=(0,o.useCallback)(e=>{a.changeFindDirection(e)},[a]),L=(0,o.useCallback)(e=>{a.changeFindScope(e)},[a]),R=(0,o.useCallback)(e=>{a.changeFindBy(e)},[a]),z=ie(c),B=ae(c),V=oe(c);return(0,o.useEffect)(()=>{w&&p===0&&u.show({content:c.t(`find-replace.dialog.no-match`),type:n.MessageType.Warning,duration:5e3})},[w,p,u,c]),(0,s.jsxs)(`div`,{children:[(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find`),children:(0,s.jsx)(H,{findCompleted:w,className:`univer-find-input`,matchesCount:p,matchesPosition:m,findReplaceService:a,localeService:c,initialFindString:g,onChange:A})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.replace`),children:(0,s.jsx)(n.Input,{placeholder:c.t(`find-replace.dialog.replace-placeholder`),value:_,onChange:e=>j(e)})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-direction.title`),children:(0,s.jsx)(n.Select,{value:x,options:B,onChange:I})}),(0,s.jsx)(n.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-scope.title`),children:(0,s.jsx)(n.Select,{value:S,options:z,onChange:L})}),(0,s.jsx)(n.FormLayout,{label:c.t(`find-replace.dialog.find-by.title`),children:(0,s.jsx)(n.Select,{value:C,options:V,onChange:R})})]})}),(0,s.jsx)(n.FormDualColumnLayout,{children:(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.FormLayout,{children:(0,s.jsx)(n.Checkbox,{checked:y,onChange:e=>{a.changeCaseSensitive(e)},children:c.t(`find-replace.dialog.case-sensitive`)})}),(0,s.jsx)(n.FormLayout,{children:(0,s.jsx)(n.Checkbox,{checked:b,onChange:e=>{a.changeMatchesTheWholeCell(e)},children:c.t(`find-replace.dialog.match-the-whole-cell`)})})]})}),(0,s.jsxs)(`div`,{className:`univer-mt-6 univer-flex univer-justify-between`,children:[(0,s.jsx)(n.Button,{variant:`primary`,onClick:N,disabled:T,children:c.t(`find-replace.dialog.find`)}),(0,s.jsxs)(`span`,{className:`univer-inline-flex univer-gap-2`,children:[(0,s.jsx)(n.Button,{disabled:E,onClick:P,children:c.t(`find-replace.dialog.replace`)}),(0,s.jsx)(n.Button,{disabled:k,onClick:F,children:c.t(`find-replace.dialog.replace-all`)})]})]})]})});function re(){let e=(0,r.useDependency)(v),n=(0,r.useDependency)(r.ILayoutService),i=(0,r.useDependency)(t.IContextService),u=(0,r.useObservable)(e.state$,void 0,!0),d=(0,o.useRef)(null);(0,o.useEffect)(()=>{let e;return d.current&&(e=n.registerContainerElement(d.current)),()=>e==null?void 0:e.dispose()},[n]);let f=(0,o.useRef)(null),p=(0,o.useCallback)(e=>i.setContextValue(l,e),[i]),m=(0,o.useCallback)(e=>i.setContextValue(c,e),[i]);return(0,o.useEffect)(()=>{var e;let t=(0,a.fromEvent)(document,`focusin`).subscribe(e=>{var t;e.target&&(t=d.current)!=null&&t.contains(e.target)?p(!0):p(!1),!f.current||!f.current.selectHasFocus()?m(!1):m(!0)});return(e=f.current)==null||e.focus(),p(!0),m(!0),()=>{t.unsubscribe(),p(!1)}},[p,m]),(0,s.jsx)(`div`,{ref:d,"data-u-comp":`find-replace-dialog`,children:u.replaceRevealed?(0,s.jsx)(ne,{ref:f}):(0,s.jsx)(te,{ref:f})})}function ie(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-scope.current-sheet`),value:C.SUBUNIT},{label:e.t(`find-replace.dialog.find-scope.workbook`),value:C.UNIT}],[e.getCurrentLocale()])}function ae(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-direction.row`),value:x.ROW},{label:e.t(`find-replace.dialog.find-direction.column`),value:x.COLUMN}],[e.getCurrentLocale()])}function oe(e){return(0,o.useMemo)(()=>[{label:e.t(`find-replace.dialog.find-by.value`),value:S.VALUE},{label:e.t(`find-replace.dialog.find-by.formula`),value:S.FORMULA}],[e.getCurrentLocale()])}function W(e){return e.getContextValue(l)}function se(e){return e.getContextValue(u)}function G(e){return e.getContextValue(c)}let K=`7_find-replace-shortcuts`;function q(e){return e.getContextValue(t.FOCUSING_SHEET)}function J(e){return!e.getContextValue(t.EDITOR_ACTIVATED)}let ce={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:r.KeyCode.F|r.MetaKeys.CTRL_COMMAND,group:K,preconditions(e){return!W(e)&&q(e)&&J(e)}},le={id:k.id,description:`find-replace.shortcut.open-find-dialog`,binding:r.KeyCode.F|r.MetaKeys.CTRL_COMMAND,mac:r.KeyCode.F|r.MetaKeys.MAC_CTRL,preconditions(e){return!W(e)&&q(e)&&J(e)}},ue={id:A.id,description:`find-replace.shortcut.open-replace-dialog`,binding:r.KeyCode.H|r.MetaKeys.CTRL_COMMAND,mac:r.KeyCode.H|r.MetaKeys.MAC_CTRL,group:K,preconditions(e){return q(e)&&J(e)&&(!W(e)||!se(e))}},de={id:j.id,description:`find-replace.shortcut.go-to-next-match`,binding:r.KeyCode.ENTER,group:K,priority:1e3,preconditions(e){return G(e)&&W(e)}},fe={id:M.id,description:`find-replace.shortcut.go-to-previous-match`,binding:r.KeyCode.ENTER|r.MetaKeys.SHIFT,group:K,priority:1e3,preconditions(e){return G(e)&&W(e)}},pe={id:N.id,description:`find-replace.shortcut.focus-selection`,binding:r.KeyCode.ESC,group:K,priority:1e3,preconditions(e){return W(e)}},Y=`DESKTOP_FIND_REPLACE_DIALOG`,X=class extends t.RxDisposable{constructor(e,t,n,r,i,a,o,s,c){super(),this._univerInstanceService=e,this._menuManagerService=t,this._shortcutService=n,this._commandService=r,this._findReplaceService=i,this._dialogService=a,this._layoutService=o,this._localeService=s,this._componentManager=c,m(this,`_closingListenerDisposable`,void 0),this._initCommands(),this._initUI(),this._initShortcuts()}dispose(){var e;super.dispose(),(e=this._closingListenerDisposable)==null||e.dispose(),this._closingListenerDisposable=null}_initCommands(){[k,A,j,M,O,D,N].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}_initShortcuts(){[ue,ce,le,fe,de,pe].forEach(e=>this.disposeWithMe(this._shortcutService.registerShortcut(e)))}_initUI(){[[`FindReplaceDialog`,re],[`SearchIcon`,B]].forEach(([e,t])=>{this.disposeWithMe(this._componentManager.register(e,t))}),this._menuManagerService.mergeMenu(ee),this._findReplaceService.stateUpdates$.pipe((0,a.takeUntil)(this.dispose$)).subscribe(e=>{e.revealed===!0&&this._openPanel()})}_openPanel(){this._dialogService.open({id:Y,draggable:!0,width:350,title:{title:this._localeService.t(`find-replace.dialog.title`)},children:{label:`FindReplaceDialog`},destroyOnClose:!0,mask:!1,maskClosable:!1,defaultPosition:me(),preservePositionOnDestroy:!0,onClose:()=>this.closePanel()}),this._closingListenerDisposable=(0,t.toDisposable)(this._univerInstanceService.focused$.pipe((0,a.takeUntil)(this.dispose$)).subscribe(e=>{(!e||!this._univerInstanceService.getUniverSheetInstance(e))&&this.closePanel()}))}closePanel(){this._closingListenerDisposable&&(this._closingListenerDisposable.dispose(),this._closingListenerDisposable=null,this._dialogService.close(Y),this._findReplaceService.terminate(),queueMicrotask(()=>this._layoutService.focus()))}};X=g([h(0,t.IUniverInstanceService),h(1,r.IMenuManagerService),h(2,r.IShortcutService),h(3,t.ICommandService),h(4,v),h(5,r.IDialogService),h(6,r.ILayoutService),h(7,(0,t.Inject)(t.LocaleService)),h(8,(0,t.Inject)(r.ComponentManager))],X);function me(){let{innerWidth:e}=window;return{x:e-350-20,y:64}}var he=`@univerjs/find-replace`,ge=`0.20.1`;let Z=`find-replace.config`;Symbol(Z);let Q={},$=class extends t.Plugin{constructor(e=Q,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},Q,this._config);this._configService.setConfig(Z,i)}onStarting(){[[X],[v,{useClass:E}]].forEach(e=>this._injector.add(e))}onRendered(){this._injector.get(X)}};m($,`pluginName`,`UNIVER_FIND_REPLACE_PLUGIN`),m($,`packageName`,he),m($,`version`,ge),$=g([h(1,(0,t.Inject)(t.Injector)),h(2,t.IConfigService)],$),e.FindBy=S,e.FindDirection=x,e.FindModel=_,Object.defineProperty(e,`FindReplaceController`,{enumerable:!0,get:function(){return X}}),Object.defineProperty(e,`FindReplaceModel`,{enumerable:!0,get:function(){return b}}),e.FindReplaceState=T,e.FindScope=C,e.GoToNextMatchOperation=j,e.GoToPreviousMatchOperation=M,e.IFindReplaceService=v,e.OpenFindDialogOperation=k,e.OpenReplaceDialogOperation=A,e.ReplaceAllMatchesCommand=O,e.ReplaceCurrentMatchCommand=D,Object.defineProperty(e,`UniverFindReplacePlugin`,{enumerable:!0,get:function(){return $}}),e.createInitFindReplaceState=w});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/find-replace",
3
- "version": "0.20.0",
3
+ "version": "0.20.1",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -55,10 +55,10 @@
55
55
  },
56
56
  "dependencies": {
57
57
  "@univerjs/icons": "^1.1.1",
58
- "@univerjs/core": "0.20.0",
59
- "@univerjs/design": "0.20.0",
60
- "@univerjs/ui": "0.20.0",
61
- "@univerjs/engine-render": "0.20.0"
58
+ "@univerjs/design": "0.20.1",
59
+ "@univerjs/engine-render": "0.20.1",
60
+ "@univerjs/core": "0.20.1",
61
+ "@univerjs/ui": "0.20.1"
62
62
  },
63
63
  "devDependencies": {
64
64
  "postcss": "^8.5.8",
@@ -67,7 +67,7 @@
67
67
  "tailwindcss": "3.4.18",
68
68
  "typescript": "^6.0.2",
69
69
  "vitest": "^4.1.1",
70
- "@univerjs-infra/shared": "0.20.0"
70
+ "@univerjs-infra/shared": "0.20.1"
71
71
  },
72
72
  "scripts": {
73
73
  "test": "vitest run",