@univerjs/sheets-find-replace 0.1.15 → 0.1.16

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
- "use strict";var ee=Object.defineProperty;var te=(c,e,t)=>e in c?ee(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var f=(c,e,t)=>(te(c,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),C=require("@wendellhu/redi"),_=require("@univerjs/find-replace"),R=require("@univerjs/sheets"),y=require("@univerjs/engine-render"),k=require("@univerjs/sheets-ui"),I=require("rxjs");class ne extends y.Shape{constructor(t,n){super(t,n);f(this,"_activated",!1);f(this,"_inHiddenRange",!1);f(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;y.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const $={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),n=c.get(d.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(h=>n.executeCommand(R.SetRangeValuesCommand.id,{unitId:i,subUnitId:h.subUnitId,value:h.value})));return s.dispose(),ie(o,r)}};function ie(c,e){let t=0,n=0;return c.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function E(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function A(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function D(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function se(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function re(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function oe(c,e){const{range:t}=c,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var ce=Object.defineProperty,he=Object.getOwnPropertyDescriptor,B=(c,e,t,n)=>{for(var i=n>1?void 0:n?he(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&ce(e,t,i),i},m=(c,e)=>(t,n)=>e(t,n,c);exports.SheetsFindReplaceController=class extends d.Disposable{constructor(t,n,i,r,s){super();f(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(P);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(I.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[$].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}};exports.SheetsFindReplaceController=B([d.OnLifecycle(d.LifecycleStages.Steady,exports.SheetsFindReplaceController),m(0,C.Inject(C.Injector)),m(1,C.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],exports.SheetsFindReplaceController);const ae="sheets-find-replace-provider",le=1e4;let F=class extends _.FindModel{constructor(e,t,n,i,r,s,o,h){super();f(this,"_matchesUpdate$",new I.Subject);f(this,"matchesUpdate$",this._matchesUpdate$.asObservable());f(this,"_activelyChangingMatch$",new I.Subject);f(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());f(this,"_matchesByWorksheet",new Map);f(this,"_matches",[]);f(this,"_matchesPosition",0);f(this,"_activeHighlightIndex",-1);f(this,"_highlightShapes",[]);f(this,"_currentHighlightShape",null);f(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=h}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(y.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const h=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:a}=h;return a.length?this._matchesByWorksheet.set(l,h.results):this._matchesByWorksheet.delete(l),h});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s,o])=>s.id===R.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),I.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const h=this._workbook.getActiveSheet(),l=this._selectionManagerService.getSelections();return(u=l==null?void 0:l.some(S=>!oe(S,h)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const h=this._workbook.getActiveSheet(),l=this.currentMatch;s=n();const a=this._selectionManagerService.getSelections(),u=s?this._findInSelections(h,a,e,t):this._findInWorksheet(h,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(I.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([h])=>{if(h.type===d.CommandType.MUTATION&&h.params.unitId===this._workbook.getUnitId())return!0;if(h.id===R.SetSelectionsOperation.id&&h.params.unitId===t){const l=n();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(I.skip(1))).pipe(I.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),h=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const l of h){const{row:a,col:u,colSpan:S,rowSpan:v,value:p}=l;if(r!=null&&r(a,u)||!p||e.getRowFiltered(a))continue;const{hit:b,replaceable:M,isFormula:w}=de(e,a,u,t,p);if(b){const x={provider:ae,unitId:i,replaceable:M,isFormula:w,range:{subUnitId:o,range:{startRow:a,startColumn:u,endColumn:u+(S!=null?S:1)-1,endRow:a+(v!=null?v:1)-1}}};s.push(x)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===_.FindDirection.ROW?A:D,o=new Set;return{results:t.map(l=>this._findInRange(e,n,l.range,i,(a,u)=>{const S=`${a}-${u}`;return o.has(S)?!0:(o.add(S),!1)}).results).flat().sort((l,a)=>s(l.range.range,a.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var v,p;this._disposeHighlights();const e=(v=this._sheetSkeletonManagerService.getCurrent())==null?void 0:v.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,h=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(h).toRgb(),a=this._workbook.getActiveSheet(),u=a.getSheetId(),S=o.filter(b=>b.range.subUnitId===u).map((b,M)=>{const{startColumn:w,startRow:x,endColumn:V,endRow:q}=b.range.range,Q=k.getCoordByCell(x,w,s,e),X=k.getCoordByCell(q,V,s,e),{startX:W,startY:H}=Q,{endX:Y,endY:G}=X,T=!a.getRowRawVisible(x),O=!a.getColVisible(w),K=T||O,z=O?2:Y-W,J=T?2:G-H,Z={left:W,top:H,color:l,width:z,height:J,evented:!1,inHiddenRange:K,zIndex:le};return new ne(`find-highlight-${M}`,Z)});s.addObjects(S),this._highlightShapes=S,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(R.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:h,startRow:l}=o.range.range;return h===i&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findNextMatch(t,n);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(u=>u===l):this._activeHighlightIndex=a,i||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findPreviousMatch(t,n);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(u=>u===l):this._activeHighlightIndex=a,i||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const h=this._matches.length,l=(o+h)%h;return[this._matches[l],l]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,h=this._matches.length;if(!e&&o>=h)return null;const l=o%h;return[this._matches[l],l]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=e.findIndex(o=>{const h=o.range.range;if(!(i?A(t,h):D(t,h)))return!1;const a=E(t,h);return n?a:!a});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const h=o.range.range;if(!(i?se(t,h):re(t,h)))return!1;const a=E(t,h);return n?a:!a});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===_.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(R.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===_.FindBy.FORMULA,o=i?"g":"ig",h=[];return d.groupBy(this._matches.filter(a=>a.replaceable),a=>a.range.subUnitId).forEach((a,u)=>{const S=new d.ObjectMatrix,v=this._workbook.getSheetBySheetId(u);a.forEach(p=>{const{startColumn:b,startRow:M}=p.range.range,w=this._getReplacedCellData(p,v,s,n,e,o);w&&S.setValue(M,b,w)}),h.push({count:a.length,subUnitId:u,value:S.getMatrix()})}),h?this._commandService.executeCommand($.id,{unitId:t,replacements:h}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var v;const o=e.range.range,{startRow:h,startColumn:l}=o,a=t.getCellRaw(h,l);if(e.isFormula)return n?{f:a.f.replace(new RegExp(N(i),s),r),v:null}:null;if(!!((v=a.p)!=null&&v.body)){const p=d.Tools.deepClone(a.p);return d.replaceInDocumentBody(p.body,i,r),{p}}return{v:a.v.toString().replace(new RegExp(N(i),s),r)}}};F=B([m(2,d.IUniverInstanceService),m(3,y.IRenderManagerService),m(4,d.ICommandService),m(5,d.IContextService),m(6,C.Inject(d.ThemeService)),m(7,C.Inject(R.SelectionManagerService))],F);function N(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let P=class extends d.Disposable{constructor(e,t,n){super();f(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(k.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(F,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};P=B([m(0,d.IUniverInstanceService),m(1,y.IRenderManagerService),m(2,C.Inject(C.Injector))],P);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function de(c,e,t,n,i){const{findBy:r}=n,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,U(i,n)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?U({v:o.f},n)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,U(i,n)?g.hit=!0:g.hit=!1,g))}function U(c,e){let t=ue(c);return t?e.matchesTheWholeCell?(t=ge(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function ue(c){var t,n,i;const e=(i=(n=(t=c==null?void 0:c.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:c==null?void 0:c.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ge(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var j=Object.defineProperty,_e=Object.getOwnPropertyDescriptor,fe=(c,e,t)=>e in c?j(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,Se=(c,e,t,n)=>{for(var i=n>1?void 0:n?_e(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&j(e,t,i),i},me=(c,e)=>(t,n)=>e(t,n,c),L=(c,e,t)=>(fe(c,typeof e!="symbol"?e+"":e,t),t);const pe="SHEET_FIND_REPLACE_PLUGIN";exports.UniverSheetsFindReplacePlugin=class extends d.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[exports.SheetsFindReplaceController]].forEach(t=>e.add(t))}};L(exports.UniverSheetsFindReplacePlugin,"pluginName",pe);L(exports.UniverSheetsFindReplacePlugin,"type",d.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=Se([d.DependentOn(R.UniverSheetsPlugin,R.UniverSheetsPlugin,_.UniverFindReplacePlugin),me(1,C.Inject(C.Injector))],exports.UniverSheetsFindReplacePlugin);
1
+ "use strict";var ee=Object.defineProperty;var te=(c,e,t)=>e in c?ee(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var S=(c,e,t)=>te(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),C=require("@wendellhu/redi"),_=require("@univerjs/find-replace"),R=require("@univerjs/sheets"),y=require("@univerjs/engine-render"),k=require("@univerjs/sheets-ui"),I=require("rxjs");class ne extends y.Shape{constructor(t,n){super(t,n);S(this,"_activated",!1);S(this,"_inHiddenRange",!1);S(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;y.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const $={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),n=c.get(d.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(R.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),ie(o,r)}};function ie(c,e){let t=0,n=0;return c.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function E(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function A(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function D(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function se(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function re(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function oe(c,e){const{range:t}=c,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var ce=Object.defineProperty,he=Object.getOwnPropertyDescriptor,B=(c,e,t,n)=>{for(var i=n>1?void 0:n?he(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&ce(e,t,i),i},m=(c,e)=>(t,n)=>e(t,n,c);exports.SheetsFindReplaceController=class extends d.Disposable{constructor(t,n,i,r,s){super();S(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(P);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(I.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[$].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}};exports.SheetsFindReplaceController=B([d.OnLifecycle(d.LifecycleStages.Steady,exports.SheetsFindReplaceController),m(0,C.Inject(C.Injector)),m(1,C.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],exports.SheetsFindReplaceController);const ae="sheets-find-replace-provider",le=1e4;let F=class extends _.FindModel{constructor(e,t,n,i,r,s,o,a){super();S(this,"_matchesUpdate$",new I.Subject);S(this,"matchesUpdate$",this._matchesUpdate$.asObservable());S(this,"_activelyChangingMatch$",new I.Subject);S(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());S(this,"_matchesByWorksheet",new Map);S(this,"_matches",[]);S(this,"_matchesPosition",0);S(this,"_activeHighlightIndex",-1);S(this,"_highlightShapes",[]);S(this,"_currentHighlightShape",null);S(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(y.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:h}=a;return h.length?this._matchesByWorksheet.set(l,a.results):this._matchesByWorksheet.delete(l),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s,o])=>s.id===R.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),I.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const l=this._selectionManagerService.getSelections();return(u=l==null?void 0:l.some(f=>!oe(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const l=this.currentMatch;s=n();const h=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,h,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(I.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([a])=>{if(a.type===d.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===R.SetSelectionsOperation.id&&a.params.unitId===t){const l=n();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(I.skip(1))).pipe(I.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const l of a){const{row:h,col:u,colSpan:f,rowSpan:v,value:p}=l;if(r!=null&&r(h,u)||!p||e.getRowFiltered(h))continue;const{hit:b,replaceable:M,isFormula:w}=de(e,h,u,t,p);if(b){const x={provider:ae,unitId:i,replaceable:M,isFormula:w,range:{subUnitId:o,range:{startRow:h,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:h+(v!=null?v:1)-1}}};s.push(x)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===_.FindDirection.ROW?A:D,o=new Set;return{results:t.map(l=>this._findInRange(e,n,l.range,i,(h,u)=>{const f=`${h}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((l,h)=>s(l.range.range,h.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var v,p;this._disposeHighlights();const e=(v=this._sheetSkeletonManagerService.getCurrent())==null?void 0:v.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(a).toRgb(),h=this._workbook.getActiveSheet();if(!h)return;const u=h.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,M)=>{const{startColumn:w,startRow:x,endColumn:V,endRow:q}=b.range.range,Q=k.getCoordByCell(x,w,s,e),X=k.getCoordByCell(q,V,s,e),{startX:W,startY:H}=Q,{endX:Y,endY:G}=X,T=!h.getRowRawVisible(x),O=!h.getColVisible(w),K=T||O,z=O?2:Y-W,J=T?2:G-H,Z={left:W,top:H,color:l,width:z,height:J,evented:!1,inHiddenRange:K,zIndex:le};return new ne(`find-highlight-${M}`,Z)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(R.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:l}=o.range.range;return a===i&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const l=this._matches.length,h=(a+l)%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,l=this._matches.length;if(!e&&a>=l)return null;const h=a%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?A(t,a):D(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?se(t,a):re(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===_.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(R.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===_.FindBy.FORMULA,o=i?"g":"ig",a=[];return d.groupBy(this._matches.filter(h=>h.replaceable),h=>h.range.subUnitId).forEach((h,u)=>{const f=new d.ObjectMatrix,v=this._workbook.getSheetBySheetId(u);h.forEach(p=>{const{startColumn:b,startRow:M}=p.range.range,w=this._getReplacedCellData(p,v,s,n,e,o);w&&f.setValue(M,b,w)}),a.push({count:h.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand($.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var v;const o=e.range.range,{startRow:a,startColumn:l}=o,h=t.getCellRaw(a,l);if(e.isFormula)return n?{f:h.f.replace(new RegExp(N(i),s),r),v:null}:null;if(!!((v=h.p)!=null&&v.body)){const p=d.Tools.deepClone(h.p);return d.replaceInDocumentBody(p.body,i,r),{p}}return{v:h.v.toString().replace(new RegExp(N(i),s),r)}}};F=B([m(2,d.IUniverInstanceService),m(3,y.IRenderManagerService),m(4,d.ICommandService),m(5,d.IContextService),m(6,C.Inject(d.ThemeService)),m(7,C.Inject(R.SelectionManagerService))],F);function N(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let P=class extends d.Disposable{constructor(e,t,n){super();S(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(k.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(F,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};P=B([m(0,d.IUniverInstanceService),m(1,y.IRenderManagerService),m(2,C.Inject(C.Injector))],P);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function de(c,e,t,n,i){const{findBy:r}=n,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,U(i,n)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?U({v:o.f},n)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,U(i,n)?g.hit=!0:g.hit=!1,g))}function U(c,e){let t=ue(c);return t?e.matchesTheWholeCell?(t=ge(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function ue(c){var t,n,i;const e=(i=(n=(t=c==null?void 0:c.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:c==null?void 0:c.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ge(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var j=Object.defineProperty,fe=Object.getOwnPropertyDescriptor,_e=(c,e,t)=>e in c?j(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,Se=(c,e,t,n)=>{for(var i=n>1?void 0:n?fe(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&j(e,t,i),i},me=(c,e)=>(t,n)=>e(t,n,c),L=(c,e,t)=>_e(c,typeof e!="symbol"?e+"":e,t);const pe="SHEET_FIND_REPLACE_PLUGIN";exports.UniverSheetsFindReplacePlugin=class extends d.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[exports.SheetsFindReplaceController]].forEach(t=>e.add(t))}};L(exports.UniverSheetsFindReplacePlugin,"pluginName",pe);L(exports.UniverSheetsFindReplacePlugin,"type",d.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=Se([d.DependentOn(R.UniverSheetsPlugin,R.UniverSheetsPlugin,_.UniverFindReplacePlugin),me(1,C.Inject(C.Injector))],exports.UniverSheetsFindReplacePlugin);
package/lib/es/index.js CHANGED
@@ -1,6 +1,6 @@
1
- var _e = Object.defineProperty;
2
- var fe = (h, e, t) => e in h ? _e(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
3
- var g = (h, e, t) => (fe(h, typeof e != "symbol" ? e + "" : e, t), t);
1
+ var fe = Object.defineProperty;
2
+ var _e = (h, e, t) => e in h ? fe(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
3
+ var f = (h, e, t) => _e(h, typeof e != "symbol" ? e + "" : e, t);
4
4
  import { CommandType as B, IUndoRedoService as me, ICommandService as H, Rectangle as Se, OnLifecycle as pe, LifecycleStages as ve, ThemeService as Ie, Disposable as K, EDITOR_ACTIVATED as Ce, IContextService as z, IUniverInstanceService as J, UniverInstanceType as Z, fromCallback as U, ColorKit as Re, rotate as $, groupBy as we, ObjectMatrix as be, Tools as Me, replaceInDocumentBody as ke, DependentOn as xe, Plugin as ye } from "@univerjs/core";
5
5
  import { Inject as C, Injector as O } from "@wendellhu/redi";
6
6
  import { FindReplaceController as Ue, IFindReplaceService as Pe, FindModel as Be, FindBy as M, FindScope as I, FindDirection as b, UniverFindReplacePlugin as We } from "@univerjs/find-replace";
@@ -11,9 +11,9 @@ import { filter as k, Subject as V, throttleTime as je, merge as Ve, skip as Qe,
11
11
  class Ye extends Ee {
12
12
  constructor(t, i) {
13
13
  super(t, i);
14
- g(this, "_activated", !1);
15
- g(this, "_inHiddenRange", !1);
16
- g(this, "_color");
14
+ f(this, "_activated", !1);
15
+ f(this, "_inHiddenRange", !1);
16
+ f(this, "_color");
17
17
  i && this.setShapeProps(i);
18
18
  }
19
19
  setShapeProps(t) {
@@ -38,10 +38,10 @@ const te = {
38
38
  id: "sheet.command.replace",
39
39
  type: B.COMMAND,
40
40
  handler: async (h, e) => {
41
- const t = h.get(me), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(q.id, {
41
+ const t = h.get(me), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((a) => i.executeCommand(q.id, {
42
42
  unitId: n,
43
- subUnitId: c.subUnitId,
44
- value: c.value
43
+ subUnitId: a.subUnitId,
44
+ value: a.value
45
45
  })));
46
46
  return s.dispose(), Ge(o, r);
47
47
  }
@@ -76,11 +76,11 @@ var Ze = Object.defineProperty, qe = Object.getOwnPropertyDescriptor, T = (h, e,
76
76
  for (var n = i > 1 ? void 0 : i ? qe(e, t) : e, r = h.length - 1, s; r >= 0; r--)
77
77
  (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
78
78
  return i && n && Ze(e, t, n), n;
79
- }, f = (h, e) => (t, i) => e(t, i, h);
79
+ }, _ = (h, e) => (t, i) => e(t, i, h);
80
80
  let x = class extends K {
81
81
  constructor(e, t, i, n, r) {
82
82
  super();
83
- g(this, "_provider");
83
+ f(this, "_provider");
84
84
  this._injector = e, this._findReplaceController = t, this._contextService = i, this._findReplaceService = n, this._commandService = r, this._init(), this._initCommands();
85
85
  }
86
86
  dispose() {
@@ -98,33 +98,33 @@ let x = class extends K {
98
98
  };
99
99
  x = T([
100
100
  pe(ve.Steady, x),
101
- f(0, C(O)),
102
- f(1, C(Ue)),
103
- f(2, z),
104
- f(3, Pe),
105
- f(4, H)
101
+ _(0, C(O)),
102
+ _(1, C(Ue)),
103
+ _(2, z),
104
+ _(3, Pe),
105
+ _(4, H)
106
106
  ], x);
107
107
  const et = "sheets-find-replace-provider", tt = 1e4;
108
108
  let W = class extends Be {
109
- constructor(e, t, i, n, r, s, o, c) {
109
+ constructor(e, t, i, n, r, s, o, a) {
110
110
  super();
111
111
  // We can directly inject the `FindReplaceService` here, and call its methods instead of using the observables.
112
- g(this, "_matchesUpdate$", new V());
113
- g(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
114
- g(this, "_activelyChangingMatch$", new V());
115
- g(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
112
+ f(this, "_matchesUpdate$", new V());
113
+ f(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
114
+ f(this, "_activelyChangingMatch$", new V());
115
+ f(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
116
116
  /** Hold matches by the worksheet they are in. Make it easier to track the next (or previous) match when searching in the whole workbook. */
117
- g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
117
+ f(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
118
118
  /** Hold all matches in the currently searching scope. */
119
- g(this, "_matches", []);
119
+ f(this, "_matches", []);
120
120
  /** Position of the current focused ISheetCellMatch, starting from 1. */
121
- g(this, "_matchesPosition", 0);
122
- g(this, "_activeHighlightIndex", -1);
123
- g(this, "_highlightShapes", []);
124
- g(this, "_currentHighlightShape", null);
121
+ f(this, "_matchesPosition", 0);
122
+ f(this, "_activeHighlightIndex", -1);
123
+ f(this, "_highlightShapes", []);
124
+ f(this, "_currentHighlightShape", null);
125
125
  /** This properties holds the query params during this searching session. */
126
- g(this, "_query", null);
127
- this._workbook = e, this._sheetSkeletonManagerService = t, this._univerInstanceService = i, this._renderManagerService = n, this._commandService = r, this._contextService = s, this._themeService = o, this._selectionManagerService = c;
126
+ f(this, "_query", null);
127
+ this._workbook = e, this._sheetSkeletonManagerService = t, this._univerInstanceService = i, this._renderManagerService = n, this._commandService = r, this._contextService = s, this._themeService = o, this._selectionManagerService = a;
128
128
  }
129
129
  get _matchesCount() {
130
130
  return this._matches.length;
@@ -171,8 +171,8 @@ let W = class extends Be {
171
171
  let i, n = !0;
172
172
  const r = () => {
173
173
  const s = this._workbook.getSheets().filter((o) => !o.isSheetHidden()).map((o) => {
174
- const c = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: a } = c;
175
- return a.length ? this._matchesByWorksheet.set(l, c.results) : this._matchesByWorksheet.delete(l), c;
174
+ const a = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: c } = a;
175
+ return c.length ? this._matchesByWorksheet.set(l, a.results) : this._matchesByWorksheet.delete(l), a;
176
176
  });
177
177
  this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
178
178
  };
@@ -182,7 +182,10 @@ let W = class extends Be {
182
182
  U(this._commandService.onCommandExecuted).pipe(
183
183
  k(([s, o]) => s.id === He.id && !(o != null && o.fromFindReplace))
184
184
  ).subscribe(() => {
185
- const s = this._workbook.getActiveSheet(), o = s.getSheetId();
185
+ const s = this._workbook.getActiveSheet();
186
+ if (!s)
187
+ return;
188
+ const o = s.getSheetId();
186
189
  this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
187
190
  })
188
191
  ), this.disposeWithMe(
@@ -211,23 +214,27 @@ let W = class extends Be {
211
214
  findInActiveWorksheet(e) {
212
215
  const t = this._workbook.getUnitId(), i = () => {
213
216
  var d;
214
- const c = this._workbook.getActiveSheet(), l = this._selectionManagerService.getSelections();
215
- return (d = l == null ? void 0 : l.some((_) => !Je(_, c))) != null ? d : !1;
217
+ const a = this._workbook.getActiveSheet();
218
+ if (!a) return !1;
219
+ const l = this._selectionManagerService.getSelections();
220
+ return (d = l == null ? void 0 : l.some((g) => !Je(g, a))) != null ? d : !1;
216
221
  };
217
222
  let n, r = !0, s = !1;
218
223
  const o = () => {
219
- const c = this._workbook.getActiveSheet(), l = this.currentMatch;
224
+ const a = this._workbook.getActiveSheet();
225
+ if (!a) return { results: [] };
226
+ const l = this.currentMatch;
220
227
  s = i();
221
- const a = this._selectionManagerService.getSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
228
+ const c = this._selectionManagerService.getSelections(), d = s ? this._findInSelections(a, c, e, t) : this._findInWorksheet(a, e, t);
222
229
  return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(l, this._matches), r ? (n = d, r = !1) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), d;
223
230
  };
224
231
  return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
225
232
  Ve(
226
233
  U(this._commandService.onCommandExecuted).pipe(
227
- k(([c]) => {
228
- if (c.type === B.MUTATION && c.params.unitId === this._workbook.getUnitId())
234
+ k(([a]) => {
235
+ if (a.type === B.MUTATION && a.params.unitId === this._workbook.getUnitId())
229
236
  return !0;
230
- if (c.id === Oe.id && c.params.unitId === t) {
237
+ if (a.id === Oe.id && a.params.unitId === t) {
231
238
  const l = i();
232
239
  return l === !1 && s === !1 ? !1 : (s = l, !0);
233
240
  }
@@ -240,12 +247,12 @@ let W = class extends Be {
240
247
  ), o(), n;
241
248
  }
242
249
  _findInRange(e, t, i, n, r) {
243
- const s = [], o = e.getSheetId(), c = (t.findDirection === b.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
244
- for (const l of c) {
245
- const { row: a, col: d, colSpan: _, rowSpan: S, value: m } = l;
246
- if (r != null && r(a, d) || !m || e.getRowFiltered(a))
250
+ const s = [], o = e.getSheetId(), a = (t.findDirection === b.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
251
+ for (const l of a) {
252
+ const { row: c, col: d, colSpan: g, rowSpan: S, value: m } = l;
253
+ if (r != null && r(c, d) || !m || e.getRowFiltered(c))
247
254
  continue;
248
- const { hit: p, replaceable: R, isFormula: v } = it(e, a, d, t, m);
255
+ const { hit: p, replaceable: R, isFormula: v } = it(e, c, d, t, m);
249
256
  if (p) {
250
257
  const w = {
251
258
  provider: et,
@@ -255,10 +262,10 @@ let W = class extends Be {
255
262
  range: {
256
263
  subUnitId: o,
257
264
  range: {
258
- startRow: a,
265
+ startRow: c,
259
266
  startColumn: d,
260
- endColumn: d + (_ != null ? _ : 1) - 1,
261
- endRow: a + (S != null ? S : 1) - 1
267
+ endColumn: d + (g != null ? g : 1) - 1,
268
+ endRow: c + (S != null ? S : 1) - 1
262
269
  }
263
270
  }
264
271
  };
@@ -274,11 +281,11 @@ let W = class extends Be {
274
281
  i,
275
282
  l.range,
276
283
  n,
277
- (a, d) => {
278
- const _ = `${a}-${d}`;
279
- return o.has(_) ? !0 : (o.add(_), !1);
284
+ (c, d) => {
285
+ const g = `${c}-${d}`;
286
+ return o.has(g) ? !0 : (o.add(g), !1);
280
287
  }
281
- ).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
288
+ ).results).flat().sort((l, c) => s(l.range.range, c.range.range) ? -1 : 1) };
282
289
  }
283
290
  /** Find matches in a given worksheet. */
284
291
  _findInWorksheet(e, t, i) {
@@ -300,8 +307,11 @@ let W = class extends Be {
300
307
  const n = this._workbook.getUnitId(), r = this._renderManagerService.getRenderById(n);
301
308
  if (r == null)
302
309
  return;
303
- const { scene: s } = r, o = this._matches, c = this._themeService.getCurrentTheme().gold400, l = new Re(c).toRgb(), a = this._workbook.getActiveSheet(), d = a.getSheetId(), _ = o.filter((p) => p.range.subUnitId === d).map((p, R) => {
304
- const { startColumn: v, startRow: w, endColumn: se, endRow: re } = p.range.range, oe = j(w, v, s, e), he = j(re, se, s, e), { startX: E, startY: A } = oe, { endX: ce, endY: ae } = he, N = !a.getRowRawVisible(w), D = !a.getColVisible(v), le = N || D, de = D ? 2 : ce - E, ue = N ? 2 : ae - A, ge = {
310
+ const { scene: s } = r, o = this._matches, a = this._themeService.getCurrentTheme().gold400, l = new Re(a).toRgb(), c = this._workbook.getActiveSheet();
311
+ if (!c)
312
+ return;
313
+ const d = c.getSheetId(), g = o.filter((p) => p.range.subUnitId === d).map((p, R) => {
314
+ const { startColumn: v, startRow: w, endColumn: se, endRow: re } = p.range.range, oe = j(w, v, s, e), he = j(re, se, s, e), { startX: E, startY: A } = oe, { endX: ce, endY: ae } = he, N = !c.getRowRawVisible(w), D = !c.getColVisible(v), le = N || D, de = D ? 2 : ce - E, ue = N ? 2 : ae - A, ge = {
305
315
  left: E,
306
316
  top: A,
307
317
  color: l,
@@ -313,7 +323,7 @@ let W = class extends Be {
313
323
  };
314
324
  return new Ye(`find-highlight-${R}`, ge);
315
325
  });
316
- s.addObjects(_), this._highlightShapes = _, s.makeDirty();
326
+ s.addObjects(g), this._highlightShapes = g, s.makeDirty();
317
327
  }
318
328
  _updateCurrentHighlightShape(e) {
319
329
  var t;
@@ -328,8 +338,9 @@ let W = class extends Be {
328
338
  return $e(this._univerInstanceService, this._renderManagerService);
329
339
  }
330
340
  _focusMatch(e) {
341
+ var i;
331
342
  const t = e.range.subUnitId;
332
- t !== this._workbook.getActiveSheet().getSheetId() && this._commandService.syncExecuteCommand(
343
+ t !== ((i = this._workbook.getActiveSheet()) == null ? void 0 : i.getSheetId()) && this._commandService.syncExecuteCommand(
333
344
  Te.id,
334
345
  { unitId: this._workbook.getUnitId(), subUnitId: t },
335
346
  { fromFindReplace: !0 }
@@ -340,74 +351,81 @@ let W = class extends Be {
340
351
  );
341
352
  }
342
353
  _tryRestoreLastMatchesPosition(e, t) {
343
- if (!e)
344
- return 0;
354
+ if (!e) return 0;
345
355
  const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
346
356
  if (i !== o.range.subUnitId)
347
357
  return !1;
348
- const { startColumn: c, startRow: l } = o.range.range;
349
- return c === n && l === r;
358
+ const { startColumn: a, startRow: l } = o.range.range;
359
+ return a === n && l === r;
350
360
  });
351
361
  return s > -1 ? s + 1 : 0;
352
362
  }
353
363
  moveToNextMatch(e) {
354
- var s, o, c;
364
+ var s, o, a, l;
355
365
  if (!this._matches.length)
356
366
  return null;
357
- const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, r = this._findNextMatch(t, i);
367
+ const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (a = e == null ? void 0 : e.noFocus) != null ? a : !1, r = this._findNextMatch(t, i);
358
368
  if (r) {
359
- const [l, a] = r;
360
- return this._matchesPosition = a + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(l.range.subUnitId).findIndex((d) => d === l) : this._activeHighlightIndex = a, n || this._focusMatch(l), this._workbook.getActiveSheet().getSheetId() === l.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), l;
369
+ const [c, d] = r;
370
+ return this._matchesPosition = d + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(c.range.subUnitId).findIndex((g) => g === c) : this._activeHighlightIndex = d, n || this._focusMatch(c), ((l = this._workbook.getActiveSheet()) == null ? void 0 : l.getSheetId()) === c.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), c;
361
371
  }
362
372
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
363
373
  }
364
374
  moveToPreviousMatch(e) {
365
- var s, o, c;
375
+ var s, o, a, l;
366
376
  if (!this._matches.length)
367
377
  return null;
368
- const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, r = this._findPreviousMatch(t, i);
378
+ const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (a = e == null ? void 0 : e.noFocus) != null ? a : !1, r = this._findPreviousMatch(t, i);
369
379
  if (r) {
370
- const [l, a] = r;
371
- return this._matchesPosition = a + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(l.range.subUnitId).findIndex((d) => d === l) : this._activeHighlightIndex = a, n || this._focusMatch(l), this._workbook.getActiveSheet().getSheetId() === l.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), l;
380
+ const [c, d] = r;
381
+ return this._matchesPosition = d + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(c.range.subUnitId).findIndex((g) => g === c) : this._activeHighlightIndex = d, n || this._focusMatch(c), ((l = this._workbook.getActiveSheet()) == null ? void 0 : l.getSheetId()) === c.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), c;
372
382
  }
373
383
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
374
384
  }
375
385
  _findPreviousMatch(e = !1, t = !1) {
386
+ var s;
376
387
  if (this.currentMatch) {
377
- const s = this._matches.findIndex((a) => a === this.currentMatch);
388
+ const o = this._matches.findIndex((d) => d === this.currentMatch);
378
389
  if (t)
379
- return [this.currentMatch, s];
380
- const o = s - 1;
381
- if (!e && o < 0)
390
+ return [this.currentMatch, o];
391
+ const a = o - 1;
392
+ if (!e && a < 0)
382
393
  return null;
383
- const c = this._matches.length, l = (o + c) % c;
384
- return [this._matches[l], l];
394
+ const l = this._matches.length, c = (a + l) % l;
395
+ return [this._matches[c], c];
385
396
  }
386
397
  const i = this._selectionManagerService.getSelections();
387
398
  if (!(i != null && i.length))
388
399
  return [this._matches[0], 0];
389
400
  if (this._query.findScope !== I.UNIT)
390
401
  return this._findPreviousMatchByRange(this._matches, i[0].range);
391
- const n = this._workbook.getActiveSheet().getSheetId(), r = this._findPreviousWorksheetThatHasAMatch(n, e);
402
+ const n = (s = this._workbook.getActiveSheet()) == null ? void 0 : s.getSheetId();
403
+ if (!n)
404
+ return null;
405
+ const r = this._findPreviousWorksheetThatHasAMatch(n, e);
392
406
  return r ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(r), i[0].range) : null;
393
407
  }
394
408
  _findNextMatch(e = !1, t = !1) {
409
+ var s;
395
410
  if (this.currentMatch) {
396
- const s = this._matches.findIndex((a) => a === this.currentMatch);
411
+ const o = this._matches.findIndex((d) => d === this.currentMatch);
397
412
  if (t)
398
- return [this.currentMatch, s];
399
- const o = s + 1, c = this._matches.length;
400
- if (!e && o >= c)
413
+ return [this.currentMatch, o];
414
+ const a = o + 1, l = this._matches.length;
415
+ if (!e && a >= l)
401
416
  return null;
402
- const l = o % c;
403
- return [this._matches[l], l];
417
+ const c = a % l;
418
+ return [this._matches[c], c];
404
419
  }
405
420
  const i = this._selectionManagerService.getSelections();
406
421
  if (!(i != null && i.length))
407
422
  return [this._matches[0], 0];
408
423
  if (this._query.findScope !== I.UNIT)
409
424
  return this._findNextMatchByRange(this._matches, i[0].range, t);
410
- const n = this._workbook.getActiveSheet().getSheetId(), r = this._findNextWorksheetThatHasAMatch(n, e);
425
+ const n = (s = this._workbook.getActiveSheet()) == null ? void 0 : s.getSheetId();
426
+ if (!n)
427
+ return null;
428
+ const r = this._findNextWorksheetThatHasAMatch(n, e);
411
429
  return r ? this._findNextMatchByRange(this._matchesByWorksheet.get(r), i[0].range) : null;
412
430
  }
413
431
  _findPreviousWorksheetThatHasAMatch(e, t = !1) {
@@ -421,11 +439,11 @@ let W = class extends Be {
421
439
  _findNextMatchByRange(e, t, i = !1) {
422
440
  const n = this._query.findDirection === b.ROW;
423
441
  let r = e.findIndex((o) => {
424
- const c = o.range.range;
425
- if (!(n ? X(t, c) : Y(t, c)))
442
+ const a = o.range.range;
443
+ if (!(n ? X(t, a) : Y(t, a)))
426
444
  return !1;
427
- const a = Q(t, c);
428
- return i ? a : !a;
445
+ const c = Q(t, a);
446
+ return i ? c : !c;
429
447
  });
430
448
  r === -1 && (r = e.length - 1);
431
449
  const s = e[r];
@@ -434,11 +452,11 @@ let W = class extends Be {
434
452
  _findPreviousMatchByRange(e, t, i = !1) {
435
453
  const n = this._query.findDirection === b.ROW;
436
454
  let r = this._matches.findLastIndex((o) => {
437
- const c = o.range.range;
438
- if (!(n ? Ke(t, c) : ze(t, c)))
455
+ const a = o.range.range;
456
+ if (!(n ? Ke(t, a) : ze(t, a)))
439
457
  return !1;
440
- const a = Q(t, c);
441
- return i ? a : !a;
458
+ const c = Q(t, a);
459
+ return i ? c : !c;
442
460
  });
443
461
  r === -1 && (r = 0);
444
462
  const s = e[r];
@@ -468,41 +486,41 @@ let W = class extends Be {
468
486
  async replaceAll(e) {
469
487
  if (this._matchesCount === 0 || !this._query)
470
488
  return { success: 0, failure: 0 };
471
- const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === M.FORMULA, o = n ? "g" : "ig", c = [];
472
- return we(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
473
- const _ = new be(), S = this._workbook.getSheetBySheetId(d);
474
- a.forEach((m) => {
489
+ const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === M.FORMULA, o = n ? "g" : "ig", a = [];
490
+ return we(this._matches.filter((c) => c.replaceable), (c) => c.range.subUnitId).forEach((c, d) => {
491
+ const g = new be(), S = this._workbook.getSheetBySheetId(d);
492
+ c.forEach((m) => {
475
493
  const { startColumn: p, startRow: R } = m.range.range, v = this._getReplacedCellData(m, S, s, i, e, o);
476
- v && _.setValue(R, p, v);
477
- }), c.push({
478
- count: a.length,
494
+ v && g.setValue(R, p, v);
495
+ }), a.push({
496
+ count: c.length,
479
497
  subUnitId: d,
480
- value: _.getMatrix()
498
+ value: g.getMatrix()
481
499
  });
482
- }), c ? this._commandService.executeCommand(te.id, {
500
+ }), a ? this._commandService.executeCommand(te.id, {
483
501
  unitId: t,
484
- replacements: c
502
+ replacements: a
485
503
  }) : { success: 0, failure: 0 };
486
504
  }
487
505
  _getReplacedCellData(e, t, i, n, r, s) {
488
506
  var S;
489
- const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
507
+ const o = e.range.range, { startRow: a, startColumn: l } = o, c = t.getCellRaw(a, l);
490
508
  if (e.isFormula)
491
- return i ? { f: a.f.replace(new RegExp(G(n), s), r), v: null } : null;
492
- if (!!((S = a.p) != null && S.body)) {
493
- const m = Me.deepClone(a.p);
509
+ return i ? { f: c.f.replace(new RegExp(G(n), s), r), v: null } : null;
510
+ if (!!((S = c.p) != null && S.body)) {
511
+ const m = Me.deepClone(c.p);
494
512
  return ke(m.body, n, r), { p: m };
495
513
  }
496
- return { v: a.v.toString().replace(new RegExp(G(n), s), r) };
514
+ return { v: c.v.toString().replace(new RegExp(G(n), s), r) };
497
515
  }
498
516
  };
499
517
  W = T([
500
- f(2, J),
501
- f(3, ee),
502
- f(4, H),
503
- f(5, z),
504
- f(6, C(Ie)),
505
- f(7, C(Fe))
518
+ _(2, J),
519
+ _(3, ee),
520
+ _(4, H),
521
+ _(5, z),
522
+ _(6, C(Ie)),
523
+ _(7, C(Fe))
506
524
  ], W);
507
525
  function G(h) {
508
526
  return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
@@ -513,7 +531,7 @@ let F = class extends K {
513
531
  /**
514
532
  * Hold all find results in this kind of univer business instances (Workbooks).
515
533
  */
516
- g(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
534
+ f(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
517
535
  this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
518
536
  }
519
537
  async find(e) {
@@ -547,9 +565,9 @@ let F = class extends K {
547
565
  }
548
566
  };
549
567
  F = T([
550
- f(0, J),
551
- f(1, ee),
552
- f(2, C(O))
568
+ _(0, J),
569
+ _(1, ee),
570
+ _(2, C(O))
553
571
  ], F);
554
572
  const u = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
555
573
  function it(h, e, t, i, n) {
@@ -572,7 +590,7 @@ var ie = Object.defineProperty, rt = Object.getOwnPropertyDescriptor, ot = (h, e
572
590
  for (var n = i > 1 ? void 0 : i ? rt(e, t) : e, r = h.length - 1, s; r >= 0; r--)
573
591
  (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
574
592
  return i && n && ie(e, t, n), n;
575
- }, ct = (h, e) => (t, i) => e(t, i, h), ne = (h, e, t) => (ot(h, typeof e != "symbol" ? e + "" : e, t), t);
593
+ }, ct = (h, e) => (t, i) => e(t, i, h), ne = (h, e, t) => ot(h, typeof e != "symbol" ? e + "" : e, t);
576
594
  const at = "SHEET_FIND_REPLACE_PLUGIN";
577
595
  let y = class extends ye {
578
596
  constructor(h, e) {
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(u,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/find-replace"),require("@univerjs/sheets"),require("@univerjs/engine-render"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/sheets","@univerjs/engine-render","@univerjs/sheets-ui","rxjs"],d):(u=typeof globalThis<"u"?globalThis:u||self,d(u.UniverSheetsFindReplace={},u.UniverCore,u["@wendellhu/redi"],u.UniverFindReplace,u.UniverSheets,u.UniverEngineRender,u.UniverSheetsUi,u.rxjs))})(this,function(u,d,m,f,w,k,y,R){"use strict";var ve=Object.defineProperty;var Ie=(u,d,m)=>d in u?ve(u,d,{enumerable:!0,configurable:!0,writable:!0,value:m}):u[d]=m;var S=(u,d,m)=>(Ie(u,typeof d!="symbol"?d+"":d,m),m);class q extends k.Shape{constructor(t,n){super(t,n);S(this,"_activated",!1);S(this,"_inHiddenRange",!1);S(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;k.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const T={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(d.IUndoRedoService),n=h.get(d.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(c=>n.executeCommand(w.SetRangeValuesCommand.id,{unitId:i,subUnitId:c.subUnitId,value:c.value})));return s.dispose(),Q(o,r)}};function Q(h,e){let t=0,n=0;return h.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function H(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function O(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function E(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function X(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function Y(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function G(h,e){const{range:t}=h,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var K=Object.defineProperty,z=Object.getOwnPropertyDescriptor,x=(h,e,t,n)=>{for(var i=n>1?void 0:n?z(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&K(e,t,i),i},I=(h,e)=>(t,n)=>e(t,n,h);u.SheetsFindReplaceController=class extends d.Disposable{constructor(t,n,i,r,s){super();S(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(B);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(R.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[T].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}},u.SheetsFindReplaceController=x([d.OnLifecycle(d.LifecycleStages.Steady,u.SheetsFindReplaceController),I(0,m.Inject(m.Injector)),I(1,m.Inject(f.FindReplaceController)),I(2,d.IContextService),I(3,f.IFindReplaceService),I(4,d.ICommandService)],u.SheetsFindReplaceController);const J="sheets-find-replace-provider",Z=1e4;let P=class extends f.FindModel{constructor(e,t,n,i,r,s,o,c){super();S(this,"_matchesUpdate$",new R.Subject);S(this,"matchesUpdate$",this._matchesUpdate$.asObservable());S(this,"_activelyChangingMatch$",new R.Subject);S(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());S(this,"_matchesByWorksheet",new Map);S(this,"_matches",[]);S(this,"_matchesPosition",0);S(this,"_activeHighlightIndex",-1);S(this,"_highlightShapes",[]);S(this,"_currentHighlightShape",null);S(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=c}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===f.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case f.FindScope.UNIT:this.findInWorkbook(e);break;case f.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(k.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const c=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:a}=c;return a.length?this._matchesByWorksheet.set(l,c.results):this._matchesByWorksheet.delete(l),c});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),R.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var g;const c=this._workbook.getActiveSheet(),l=this._selectionManagerService.getSelections();return(g=l==null?void 0:l.some(v=>!G(v,c)))!=null?g:!1};let i,r=!0,s=!1;const o=()=>{const c=this._workbook.getActiveSheet(),l=this.currentMatch;s=n();const a=this._selectionManagerService.getSelections(),g=s?this._findInSelections(c,a,e,t):this._findInWorksheet(c,e,t);return this._matches=g.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(i=g,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),g};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(R.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([c])=>{if(c.type===d.CommandType.MUTATION&&c.params.unitId===this._workbook.getUnitId())return!0;if(c.id===w.SetSelectionsOperation.id&&c.params.unitId===t){const l=n();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(R.skip(1))).pipe(R.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),c=(t.findDirection===f.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const l of c){const{row:a,col:g,colSpan:v,rowSpan:C,value:p}=l;if(r!=null&&r(a,g)||!p||e.getRowFiltered(a))continue;const{hit:b,replaceable:U,isFormula:M}=ee(e,a,g,t,p);if(b){const F={provider:J,unitId:i,replaceable:U,isFormula:M,range:{subUnitId:o,range:{startRow:a,startColumn:g,endColumn:g+(v!=null?v:1)-1,endRow:a+(C!=null?C:1)-1}}};s.push(F)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===f.FindDirection.ROW?O:E,o=new Set;return{results:t.map(l=>this._findInRange(e,n,l.range,i,(a,g)=>{const v=`${a}-${g}`;return o.has(v)?!0:(o.add(v),!1)}).results).flat().sort((l,a)=>s(l.range.range,a.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,p;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,c=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(c).toRgb(),a=this._workbook.getActiveSheet(),g=a.getSheetId(),v=o.filter(b=>b.range.subUnitId===g).map((b,U)=>{const{startColumn:M,startRow:F,endColumn:ce,endRow:ae}=b.range.range,le=y.getCoordByCell(F,M,s,e),de=y.getCoordByCell(ae,ce,s,e),{startX:j,startY:$}=le,{endX:ue,endY:ge}=de,L=!a.getRowRawVisible(F),V=!a.getColVisible(M),_e=L||V,fe=V?2:ue-j,Se=L?2:ge-$,me={left:j,top:$,color:l,width:fe,height:Se,evented:!1,inHiddenRange:_e,zIndex:Z};return new q(`find-highlight-${U}`,me)});s.addObjects(v),this._highlightShapes=v,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return y.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(w.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(y.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:c,startRow:l}=o.range.range;return c===i&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findNextMatch(t,n);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===f.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(g=>g===l):this._activeHighlightIndex=a,i||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findPreviousMatch(t,n);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===f.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(g=>g===l):this._activeHighlightIndex=a,i||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const c=this._matches.length,l=(o+c)%c;return[this._matches[l],l]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==f.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,c=this._matches.length;if(!e&&o>=c)return null;const l=o%c;return[this._matches[l],l]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==f.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===f.FindDirection.ROW;let r=e.findIndex(o=>{const c=o.range.range;if(!(i?O(t,c):E(t,c)))return!1;const a=H(t,c);return n?a:!a});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===f.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const c=o.range.range;if(!(i?X(t,c):Y(t,c)))return!1;const a=H(t,c);return n?a:!a});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===f.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(w.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===f.FindBy.FORMULA,o=i?"g":"ig",c=[];return d.groupBy(this._matches.filter(a=>a.replaceable),a=>a.range.subUnitId).forEach((a,g)=>{const v=new d.ObjectMatrix,C=this._workbook.getSheetBySheetId(g);a.forEach(p=>{const{startColumn:b,startRow:U}=p.range.range,M=this._getReplacedCellData(p,C,s,n,e,o);M&&v.setValue(U,b,M)}),c.push({count:a.length,subUnitId:g,value:v.getMatrix()})}),c?this._commandService.executeCommand(T.id,{unitId:t,replacements:c}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var C;const o=e.range.range,{startRow:c,startColumn:l}=o,a=t.getCellRaw(c,l);if(e.isFormula)return n?{f:a.f.replace(new RegExp(A(i),s),r),v:null}:null;if(!!((C=a.p)!=null&&C.body)){const p=d.Tools.deepClone(a.p);return d.replaceInDocumentBody(p.body,i,r),{p}}return{v:a.v.toString().replace(new RegExp(A(i),s),r)}}};P=x([I(2,d.IUniverInstanceService),I(3,k.IRenderManagerService),I(4,d.ICommandService),I(5,d.IContextService),I(6,m.Inject(d.ThemeService)),I(7,m.Inject(w.SelectionManagerService))],P);function A(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let B=class extends d.Disposable{constructor(e,t,n){super();S(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(y.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(P,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};B=x([I(0,d.IUniverInstanceService),I(1,k.IRenderManagerService),I(2,m.Inject(m.Injector))],B);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ee(h,e,t,n,i){const{findBy:r}=n,s=r===f.FindBy.FORMULA,o=h.getCellRaw(e,t);return _.rawData=o,!(o!=null&&o.f)?(_.isFormula=!1,W(i,n)?o?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?W({v:o.f},n)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,W(i,n)?_.hit=!0:_.hit=!1,_))}function W(h,e){let t=te(h);return t?e.matchesTheWholeCell?(t=ne(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function te(h){var t,n,i;const e=(i=(n=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ne(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var D=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,se=(h,e,t)=>e in h?D(h,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[e]=t,re=(h,e,t,n)=>{for(var i=n>1?void 0:n?ie(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&D(e,t,i),i},oe=(h,e)=>(t,n)=>e(t,n,h),N=(h,e,t)=>(se(h,typeof e!="symbol"?e+"":e,t),t);const he="SHEET_FIND_REPLACE_PLUGIN";u.UniverSheetsFindReplacePlugin=class extends d.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[u.SheetsFindReplaceController]].forEach(t=>e.add(t))}},N(u.UniverSheetsFindReplacePlugin,"pluginName",he),N(u.UniverSheetsFindReplacePlugin,"type",d.UniverInstanceType.UNIVER_SHEET),u.UniverSheetsFindReplacePlugin=re([d.DependentOn(w.UniverSheetsPlugin,w.UniverSheetsPlugin,f.UniverFindReplacePlugin),oe(1,m.Inject(m.Injector))],u.UniverSheetsFindReplacePlugin),Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
1
+ (function(g,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/find-replace"),require("@univerjs/sheets"),require("@univerjs/engine-render"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/sheets","@univerjs/engine-render","@univerjs/sheets-ui","rxjs"],l):(g=typeof globalThis<"u"?globalThis:g||self,l(g.UniverSheetsFindReplace={},g.UniverCore,g["@wendellhu/redi"],g.UniverFindReplace,g.UniverSheets,g.UniverEngineRender,g.UniverSheetsUi,g.rxjs))})(this,function(g,l,v,S,w,k,y,R){"use strict";var ve=Object.defineProperty;var Ie=(g,l,v)=>l in g?ve(g,l,{enumerable:!0,configurable:!0,writable:!0,value:v}):g[l]=v;var m=(g,l,v)=>Ie(g,typeof l!="symbol"?l+"":l,v);class q extends k.Shape{constructor(t,n){super(t,n);m(this,"_activated",!1);m(this,"_inHiddenRange",!1);m(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;k.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const T={id:"sheet.command.replace",type:l.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(l.IUndoRedoService),n=h.get(l.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(w.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),Q(o,r)}};function Q(h,e){let t=0,n=0;return h.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function H(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function O(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function E(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function X(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function Y(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function G(h,e){const{range:t}=h,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?l.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var K=Object.defineProperty,z=Object.getOwnPropertyDescriptor,x=(h,e,t,n)=>{for(var i=n>1?void 0:n?z(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&K(e,t,i),i},I=(h,e)=>(t,n)=>e(t,n,h);g.SheetsFindReplaceController=class extends l.Disposable{constructor(t,n,i,r,s){super();m(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(B);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(l.EDITOR_ACTIVATED).pipe(R.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[T].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}},g.SheetsFindReplaceController=x([l.OnLifecycle(l.LifecycleStages.Steady,g.SheetsFindReplaceController),I(0,v.Inject(v.Injector)),I(1,v.Inject(S.FindReplaceController)),I(2,l.IContextService),I(3,S.IFindReplaceService),I(4,l.ICommandService)],g.SheetsFindReplaceController);const J="sheets-find-replace-provider",Z=1e4;let P=class extends S.FindModel{constructor(e,t,n,i,r,s,o,a){super();m(this,"_matchesUpdate$",new R.Subject);m(this,"matchesUpdate$",this._matchesUpdate$.asObservable());m(this,"_activelyChangingMatch$",new R.Subject);m(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());m(this,"_matchesByWorksheet",new Map);m(this,"_matches",[]);m(this,"_matchesPosition",0);m(this,"_activeHighlightIndex",-1);m(this,"_highlightShapes",[]);m(this,"_currentHighlightShape",null);m(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===S.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case S.FindScope.UNIT:this.findInWorkbook(e);break;case S.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(k.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),d=o.getSheetId(),{results:c}=a;return c.length?this._matchesByWorksheet.set(d,a.results):this._matchesByWorksheet.delete(d),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s])=>s.type===l.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),R.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const d=this._selectionManagerService.getSelections();return(u=d==null?void 0:d.some(f=>!G(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const d=this.currentMatch;s=n();const c=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,c,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(d,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(R.merge(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([a])=>{if(a.type===l.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===w.SetSelectionsOperation.id&&a.params.unitId===t){const d=n();return d===!1&&s===!1?!1:(s=d,!0)}return!1})),this._workbook.activeSheet$.pipe(R.skip(1))).pipe(R.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const d of a){const{row:c,col:u,colSpan:f,rowSpan:C,value:p}=d;if(r!=null&&r(c,u)||!p||e.getRowFiltered(c))continue;const{hit:b,replaceable:U,isFormula:M}=ee(e,c,u,t,p);if(b){const F={provider:J,unitId:i,replaceable:U,isFormula:M,range:{subUnitId:o,range:{startRow:c,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:c+(C!=null?C:1)-1}}};s.push(F)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===S.FindDirection.ROW?O:E,o=new Set;return{results:t.map(d=>this._findInRange(e,n,d.range,i,(c,u)=>{const f=`${c}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((d,c)=>s(d.range.range,c.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,p;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,d=new l.ColorKit(a).toRgb(),c=this._workbook.getActiveSheet();if(!c)return;const u=c.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,U)=>{const{startColumn:M,startRow:F,endColumn:ce,endRow:ae}=b.range.range,le=y.getCoordByCell(F,M,s,e),de=y.getCoordByCell(ae,ce,s,e),{startX:j,startY:$}=le,{endX:ue,endY:ge}=de,L=!c.getRowRawVisible(F),V=!c.getColVisible(M),_e=L||V,fe=V?2:ue-j,Se=L?2:ge-$,me={left:j,top:$,color:d,width:fe,height:Se,evented:!1,inHiddenRange:_e,zIndex:Z};return new q(`find-highlight-${U}`,me)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return y.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(w.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(y.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:d}=o.range.range;return a===i&&d===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const d=this._matches.length,c=(a+d)%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,d=this._matches.length;if(!e&&a>=d)return null;const c=a%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?O(t,a):E(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?X(t,a):Y(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===S.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(w.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===S.FindBy.FORMULA,o=i?"g":"ig",a=[];return l.groupBy(this._matches.filter(c=>c.replaceable),c=>c.range.subUnitId).forEach((c,u)=>{const f=new l.ObjectMatrix,C=this._workbook.getSheetBySheetId(u);c.forEach(p=>{const{startColumn:b,startRow:U}=p.range.range,M=this._getReplacedCellData(p,C,s,n,e,o);M&&f.setValue(U,b,M)}),a.push({count:c.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand(T.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var C;const o=e.range.range,{startRow:a,startColumn:d}=o,c=t.getCellRaw(a,d);if(e.isFormula)return n?{f:c.f.replace(new RegExp(A(i),s),r),v:null}:null;if(!!((C=c.p)!=null&&C.body)){const p=l.Tools.deepClone(c.p);return l.replaceInDocumentBody(p.body,i,r),{p}}return{v:c.v.toString().replace(new RegExp(A(i),s),r)}}};P=x([I(2,l.IUniverInstanceService),I(3,k.IRenderManagerService),I(4,l.ICommandService),I(5,l.IContextService),I(6,v.Inject(l.ThemeService)),I(7,v.Inject(w.SelectionManagerService))],P);function A(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let B=class extends l.Disposable{constructor(e,t,n){super();m(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(l.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(y.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(P,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};B=x([I(0,l.IUniverInstanceService),I(1,k.IRenderManagerService),I(2,v.Inject(v.Injector))],B);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ee(h,e,t,n,i){const{findBy:r}=n,s=r===S.FindBy.FORMULA,o=h.getCellRaw(e,t);return _.rawData=o,!(o!=null&&o.f)?(_.isFormula=!1,W(i,n)?o?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?W({v:o.f},n)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,W(i,n)?_.hit=!0:_.hit=!1,_))}function W(h,e){let t=te(h);return t?e.matchesTheWholeCell?(t=ne(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function te(h){var t,n,i;const e=(i=(n=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ne(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var D=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,se=(h,e,t)=>e in h?D(h,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[e]=t,re=(h,e,t,n)=>{for(var i=n>1?void 0:n?ie(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&D(e,t,i),i},oe=(h,e)=>(t,n)=>e(t,n,h),N=(h,e,t)=>se(h,typeof e!="symbol"?e+"":e,t);const he="SHEET_FIND_REPLACE_PLUGIN";g.UniverSheetsFindReplacePlugin=class extends l.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[g.SheetsFindReplaceController]].forEach(t=>e.add(t))}},N(g.UniverSheetsFindReplacePlugin,"pluginName",he),N(g.UniverSheetsFindReplacePlugin,"type",l.UniverInstanceType.UNIVER_SHEET),g.UniverSheetsFindReplacePlugin=re([l.DependentOn(w.UniverSheetsPlugin,w.UniverSheetsPlugin,S.UniverFindReplacePlugin),oe(1,v.Inject(v.Injector))],g.UniverSheetsFindReplacePlugin),Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-find-replace",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "private": false,
5
5
  "description": "UniverSheet find replace plugin",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -49,28 +49,28 @@
49
49
  "peerDependencies": {
50
50
  "@wendellhu/redi": "0.15.4",
51
51
  "rxjs": ">=7.0.0",
52
- "@univerjs/core": "0.1.15",
53
- "@univerjs/engine-render": "0.1.15",
54
- "@univerjs/find-replace": "0.1.15",
55
- "@univerjs/sheets": "0.1.15",
56
- "@univerjs/sheets-ui": "0.1.15"
52
+ "@univerjs/core": "0.1.16",
53
+ "@univerjs/engine-render": "0.1.16",
54
+ "@univerjs/sheets": "0.1.16",
55
+ "@univerjs/find-replace": "0.1.16",
56
+ "@univerjs/sheets-ui": "0.1.16"
57
57
  },
58
58
  "dependencies": {
59
- "@univerjs/protocol": "0.1.38-alpha.3"
59
+ "@univerjs/protocol": "0.1.38-alpha.6"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@wendellhu/redi": "0.15.4",
63
63
  "rxjs": "^7.8.1",
64
64
  "typescript": "^5.4.5",
65
- "vite": "^5.2.13",
65
+ "vite": "^5.3.1",
66
66
  "vitest": "^1.6.0",
67
- "@univerjs/core": "0.1.15",
68
- "@univerjs/engine-formula": "0.1.15",
69
- "@univerjs/find-replace": "0.1.15",
70
- "@univerjs/engine-render": "0.1.15",
71
- "@univerjs/shared": "0.1.15",
72
- "@univerjs/sheets": "0.1.15",
73
- "@univerjs/sheets-ui": "0.1.15"
67
+ "@univerjs/core": "0.1.16",
68
+ "@univerjs/engine-formula": "0.1.16",
69
+ "@univerjs/engine-render": "0.1.16",
70
+ "@univerjs/find-replace": "0.1.16",
71
+ "@univerjs/shared": "0.1.16",
72
+ "@univerjs/sheets": "0.1.16",
73
+ "@univerjs/sheets-ui": "0.1.16"
74
74
  },
75
75
  "univerSpace": {
76
76
  ".": {