@univerjs/sheets-find-replace 0.1.13 → 0.1.14

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 te=Object.defineProperty;var ie=(t,e,i)=>e in t?te(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var _=(t,e,i)=>(ie(t,typeof e!="symbol"?e+"":e,i),i);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),I=require("@wendellhu/redi"),f=require("@univerjs/find-replace"),F=require("@univerjs/engine-render"),k=require("@univerjs/sheets"),y=require("@univerjs/sheets-ui"),v=require("rxjs");var G=(t=>(t[t.View=0]="View",t[t.Edit=1]="Edit",t[t.ManageCollaborator=2]="ManageCollaborator",t[t.Print=3]="Print",t[t.Duplicate=4]="Duplicate",t[t.Comment=5]="Comment",t[t.Copy=6]="Copy",t[t.Share=7]="Share",t[t.Export=8]="Export",t[t.MoveWorksheet=9]="MoveWorksheet",t[t.DeleteWorksheet=10]="DeleteWorksheet",t[t.HideWorksheet=11]="HideWorksheet",t[t.RenameWorksheet=12]="RenameWorksheet",t[t.CreateWorksheet=13]="CreateWorksheet",t[t.SetWorksheetStyle=14]="SetWorksheetStyle",t[t.EditWorksheetCell=15]="EditWorksheetCell",t[t.InsertHyperlink=16]="InsertHyperlink",t[t.Sort=17]="Sort",t[t.Filter=18]="Filter",t[t.PivotTable=19]="PivotTable",t[t.FloatImg=20]="FloatImg",t[t.History=21]="History",t[t.RwHgtClWdt=22]="RwHgtClWdt",t[t.ViemRwHgtClWdt=23]="ViemRwHgtClWdt",t[t.ViewFilter=24]="ViewFilter",t[t.MoveSheet=25]="MoveSheet",t[t.DeleteSheet=26]="DeleteSheet",t[t.HideSheet=27]="HideSheet",t[t.CopySheet=28]="CopySheet",t[t.RenameSheet=29]="RenameSheet",t[t.CreateSheet=30]="CreateSheet",t[t.SelectProtectedCells=31]="SelectProtectedCells",t[t.SelectUnProtectedCells=32]="SelectUnProtectedCells",t[t.SetCellStyle=33]="SetCellStyle",t[t.SetCellValue=34]="SetCellValue",t[t.SetRowStyle=35]="SetRowStyle",t[t.SetColumnStyle=36]="SetColumnStyle",t[t.InsertRow=37]="InsertRow",t[t.InsertColumn=38]="InsertColumn",t[t.DeleteRow=39]="DeleteRow",t[t.DeleteColumn=40]="DeleteColumn",t[t.EditExtraObject=41]="EditExtraObject",t[t.UNRECOGNIZED=-1]="UNRECOGNIZED",t))(G||{});class ne extends F.Shape{constructor(i,n){super(i,n);_(this,"_activated",!1);_(this,"_inHiddenRange",!1);_(this,"_color");n&&this.setShapeProps(n)}setShapeProps(i){this._activated=!!i.activated,typeof i.inHiddenRange<"u"&&(this._inHiddenRange=i.inHiddenRange),typeof i.color<"u"&&(this._color=i.color),this.transformByState({width:i.width,height:i.height})}_draw(i){const n=this._activated,r=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,o=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;F.Rect.drawWith(i,{width:this.width,height:this.height,fill:r,stroke:n?o:void 0,strokeWidth:n?2:0,evented:!1})}}const q={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(t,e)=>{const i=t.get(d.IUndoRedoService),n=t.get(d.ICommandService),{unitId:r,replacements:o}=e,s=i.__tempBatchingUndoRedo(r),c=await Promise.all(o.map(h=>n.executeCommand(k.SetRangeValuesCommand.id,{unitId:r,subUnitId:h.subUnitId,value:h.value})));return s.dispose(),se(c,o)}};function se(t,e){let i=0,n=0;return t.forEach((r,o)=>{const s=e[o].count;r?i+=s:n+=s}),{success:i,failure:n}}function N(t,e){return t.startRow===e.startRow&&t.startColumn===e.startColumn}function j(t,e){return t.startRow<e.startRow||t.startRow===e.startRow&&t.startColumn<=e.startColumn}function $(t,e){return t.startColumn<e.startColumn||t.startColumn===e.startColumn&&t.startRow<=e.startRow}function re(t,e){return t.startRow>e.startRow||t.startRow===e.startRow&&t.startColumn>=e.startColumn}function oe(t,e){return t.startColumn>e.startColumn||t.startColumn===e.startColumn&&t.startRow>=e.startRow}function ce(t,e){const{range:i}=t,{startRow:n,startColumn:r}=i,o=e.getMergedCell(n,r);return o?d.Rectangle.equals(i,o):i.endRow===i.startRow&&i.endColumn===i.startColumn}var he=Object.defineProperty,ae=Object.getOwnPropertyDescriptor,B=(t,e,i,n)=>{for(var r=n>1?void 0:n?ae(e,i):e,o=t.length-1,s;o>=0;o--)(s=t[o])&&(r=(n?s(e,i,r):s(r))||r);return n&&r&&he(e,i,r),r},m=(t,e)=>(i,n)=>e(i,n,t);exports.SheetsFindReplaceController=class extends d.Disposable{constructor(i,n,r,o,s){super();_(this,"_provider");this._injector=i,this._findReplaceController=n,this._contextService=r,this._findReplaceService=o,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const i=this._injector.createInstance(H);this._provider=i,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(i)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(v.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[q].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}};exports.SheetsFindReplaceController=B([d.OnLifecycle(d.LifecycleStages.Steady,exports.SheetsFindReplaceController),m(0,I.Inject(I.Injector)),m(1,I.Inject(f.FindReplaceController)),m(2,d.IContextService),m(3,f.IFindReplaceService),m(4,d.ICommandService)],exports.SheetsFindReplaceController);const le="sheets-find-replace-provider",de=1e4;let W=class extends f.FindModel{constructor(e,i,n,r,o,s,c,h,a){super();_(this,"_matchesUpdate$",new v.Subject);_(this,"matchesUpdate$",this._matchesUpdate$.asObservable());_(this,"_activelyChangingMatch$",new v.Subject);_(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());_(this,"_matchesByWorksheet",new Map);_(this,"_matches",[]);_(this,"_matchesPosition",0);_(this,"_activeHighlightIndex",-1);_(this,"_highlightShapes",[]);_(this,"_currentHighlightShape",null);_(this,"_query",null);this._workbook=e,this._univerInstanceService=i,this._renderManagerService=n,this._commandService=r,this._contextService=o,this._themeService=s,this._sheetSkeletonManagerService=c,this._selectionManagerService=h,this._findReplaceController=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===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(F.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const i=this._workbook.getUnitId();let n,r=!0;const o=()=>{const s=this._workbook.getSheets().filter(c=>!c.isSheetHidden()).map(c=>{const h=this._findInWorksheet(c,e,i),a=c.getSheetId(),{results:l}=h;return l.length?this._matchesByWorksheet.set(a,h.results):this._matchesByWorksheet.delete(a),h});this._matches=s.map(c=>c.results).flat(),this._updateFindHighlight(),r?(n={results:this._matches},r=!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(v.filter(([s,c])=>s.id===k.SetWorksheetActiveOperation.id&&!(c!=null&&c.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),c=s.getSheetId();this._matchesByWorksheet.has(c)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(v.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),v.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>o())),o(),n}_findNextMatchOnActiveSheetChange(e){let i,n,r=0;const o=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([i,r]=this._findNextMatchByRange(o,s[0].range),n=o.findIndex(c=>c===i)):(i=o[0],n=0,r=this._matches.findIndex(c=>c===i)),this._matchesPosition=r+1,this._activelyChangingMatch$.next(i),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const i=this._workbook.getUnitId(),n=()=>{var u;const h=this._workbook.getActiveSheet(),a=this._selectionManagerService.getSelections();return(u=a==null?void 0:a.some(S=>!ce(S,h)))!=null?u:!1};let r,o=!0,s=!1;const c=()=>{const h=this._workbook.getActiveSheet(),a=this.currentMatch;s=n();const l=this._selectionManagerService.getSelections(),u=s?this._findInSelections(h,l,e,i):this._findInWorksheet(h,e,i);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(a,this._matches),o?(r=u,o=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(v.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(v.filter(([h])=>{if(h.type===d.CommandType.MUTATION&&h.params.unitId===this._workbook.getUnitId())return!0;if(h.id===k.SetSelectionsOperation.id&&h.params.unitId===i){const a=n();return a===!1&&s===!1?!1:(s=a,!0)}return!1})),this._workbook.activeSheet$.pipe(v.skip(1))).pipe(v.debounceTime(200)).subscribe(()=>c())),c(),r}_findInRange(e,i,n,r,o){const s=[],c=e.getSheetId(),h=(i.findDirection===f.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const a of h){const{row:l,col:u,colSpan:S,rowSpan:C,value:p}=a;if(!this._getFindReplacePermissionCheck({row:l,col:u,unitId:r,subUnitId:c})||o!=null&&o(l,u)||!p||e.getRowFiltered(l))continue;const{hit:b,replaceable:R,isFormula:M}=ue(e,l,u,i,p);if(b){const P={provider:le,unitId:r,replaceable:R,isFormula:M,range:{subUnitId:c,range:{startRow:l,startColumn:u,endColumn:u+(S!=null?S:1)-1,endRow:l+(C!=null?C:1)-1}}};s.push(P)}}return{results:s}}_findInSelections(e,i,n,r){const{findDirection:o}=n,s=o===f.FindDirection.ROW?j:$,c=new Set;return{results:i.map(a=>this._findInRange(e,n,a.range,r,(l,u)=>{const S=`${l}-${u}`;return c.has(S)?!0:(c.add(S),!1)}).results).flat().sort((a,l)=>s(a.range.range,l.range.range)?-1:1)}}_findInWorksheet(e,i,n){const r=e.getRowCount(),o=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:r-1,endColumn:o-1};return this._findInRange(e,i,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(i=>{i.getScene().makeDirty(),i.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 r=this._workbook.getUnitId(),o=this._renderManagerService.getRenderById(r);if(o==null)return;const{scene:s}=o,c=this._matches,h=this._themeService.getCurrentTheme().gold400,a=new d.ColorKit(h).toRgb(),l=this._workbook.getActiveSheet(),u=l.getSheetId(),S=c.filter(w=>w.range.subUnitId===u).map((w,b)=>{const{startColumn:R,startRow:M,endColumn:P,endRow:Q}=w.range.range,X=y.getCoordByCell(M,R,s,e),Y=y.getCoordByCell(Q,P,s,e),{startX:T,startY:O}=X,{endX:Z,endY:K}=Y,D=!l.getRowRawVisible(M),A=!l.getColVisible(R),z=D||A,J=A?2:Z-T,E=D?2:K-O,ee={left:T,top:O,color:a,width:J,height:E,evented:!1,inHiddenRange:z,zIndex:de};return new ne(`find-highlight-${b}`,ee)});s.addObjects(S),this._highlightShapes=S,s.makeDirty()}_updateCurrentHighlightShape(e){var i;if((i=this._currentHighlightShape)==null||i.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 i=e.range.subUnitId;i!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(k.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:i},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(y.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,i){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:r,startRow:o}=e.range.range,s=i.findIndex(c=>{if(n!==c.range.subUnitId)return!1;const{startColumn:h,startRow:a}=c.range.range;return h===r&&a===o});return s>-1?s+1:0}moveToNextMatch(e){var s,c,h;if(!this._matches.length)return null;const i=(s=e==null?void 0:e.loop)!=null?s:!1,n=(c=e==null?void 0:e.stayIfOnMatch)!=null?c:!1,r=(h=e==null?void 0:e.noFocus)!=null?h:!1,o=this._findNextMatch(i,n);if(o){const[a,l]=o;return this._matchesPosition=l+1,this._query.findScope===f.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(u=>u===a):this._activeHighlightIndex=l,r||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,c,h;if(!this._matches.length)return null;const i=(s=e==null?void 0:e.loop)!=null?s:!1,n=(c=e==null?void 0:e.stayIfOnMatch)!=null?c:!1,r=(h=e==null?void 0:e.noFocus)!=null?h:!1,o=this._findPreviousMatch(i,n);if(o){const[a,l]=o;return this._matchesPosition=l+1,this._query.findScope===f.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(u=>u===a):this._activeHighlightIndex=l,r||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,i=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(i)return[this.currentMatch,s];const c=s-1;if(!e&&c<0)return null;const h=this._matches.length,a=(c+h)%h;return[this._matches[a],a]}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 r=this._workbook.getActiveSheet().getSheetId(),o=this._findPreviousWorksheetThatHasAMatch(r,e);return o?this._findPreviousMatchByRange(this._matchesByWorksheet.get(o),n[0].range):null}_findNextMatch(e=!1,i=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(i)return[this.currentMatch,s];const c=s+1,h=this._matches.length;if(!e&&c>=h)return null;const a=c%h;return[this._matches[a],a]}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,i);const r=this._workbook.getActiveSheet().getSheetId(),o=this._findNextWorksheetThatHasAMatch(r,e);return o?this._findNextMatchByRange(this._matchesByWorksheet.get(o),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,i=!1){const n=this._workbook.getSheetOrders(),r=n.findIndex(c=>c===e),s=(i?d.rotate(n,r+1):n.slice(0,r+1)).findLast(c=>this._matchesByWorksheet.has(c));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,i=!1){const n=this._workbook.getSheetOrders(),r=n.findIndex(c=>c===e),s=(i?d.rotate(n,r):n.slice(r)).find(c=>this._matchesByWorksheet.has(c));return s!=null?s:null}_findNextMatchByRange(e,i,n=!1){const r=this._query.findDirection===f.FindDirection.ROW;let o=e.findIndex(c=>{const h=c.range.range;if(!(r?j(i,h):$(i,h)))return!1;const l=N(i,h);return n?l:!l});o===-1&&(o=e.length-1);const s=e[o];return[s,this._matches.findIndex(c=>c===s)]}_findPreviousMatchByRange(e,i,n=!1){const r=this._query.findDirection===f.FindDirection.ROW;let o=this._matches.findLastIndex(c=>{const h=c.range.range;if(!(r?re(i,h):oe(i,h)))return!1;const l=N(i,h);return n?l:!l});o===-1&&(o=0);const s=e[o];return[s,this._matches.findIndex(c=>c===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const i=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),r=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===f.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),o={unitId:this.currentMatch.unitId,subUnitId:i.subUnitId,value:{[i.range.startRow]:{[i.range.startColumn]:r}}};return this._commandService.executeCommand(k.SetRangeValuesCommand.id,o)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const i=this._workbook.getUnitId(),{findString:n,caseSensitive:r,findBy:o}=this._query,s=o===f.FindBy.FORMULA,c=r?"g":"ig",h=[];return d.groupBy(this._matches.filter(l=>l.replaceable),l=>l.range.subUnitId).forEach((l,u)=>{const S=new d.ObjectMatrix,C=this._workbook.getSheetBySheetId(u);l.forEach(p=>{const{startColumn:w,startRow:b}=p.range.range,R=this._getReplacedCellData(p,C,s,n,e,c);R&&S.setValue(b,w,R)}),h.push({count:l.length,subUnitId:u,value:S.getMatrix()})}),h?this._commandService.executeCommand(q.id,{unitId:i,replacements:h}):{success:0,failure:0}}_getReplacedCellData(e,i,n,r,o,s){var C;const c=e.range.range,{startRow:h,startColumn:a}=c,l=i.getCellRaw(h,a);if(e.isFormula)return n?{f:l.f.replace(new RegExp(V(r),s),o),v:null}:null;if(!!((C=l.p)!=null&&C.body)){const p=d.Tools.deepClone(l.p);return d.replaceInDocumentBody(p.body,r,o),{p}}return{v:l.v.toString().replace(new RegExp(V(r),s),o)}}_getFindReplacePermissionCheck(e){var a,l;const{row:i,col:n,subUnitId:r}=e,s=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET).getSheetBySheetId(r);if(!s)return!1;const c=(l=(a=s.getCell(i,n))==null?void 0:a.selectionProtection)==null?void 0:l[0];return(c==null?void 0:c[G.View])!==!1}};W=B([m(1,d.IUniverInstanceService),m(2,F.IRenderManagerService),m(3,d.ICommandService),m(4,d.IContextService),m(5,I.Inject(d.ThemeService)),m(6,I.Inject(y.SheetSkeletonManagerService)),m(7,I.Inject(k.SelectionManagerService)),m(8,I.Inject(exports.SheetsFindReplaceController))],W);function V(t){return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let H=class extends d.Disposable{constructor(e,i){super();_(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=i}async find(e){this._terminate();const i=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET);if(i){const n=this._injector.createInstance(W,i);this._findModelsByUnitId.set(i.getUnitId(),n);const r=this._preprocessQuery(e);return n.start(r),[n]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let i=e.caseSensitive?e.findString:e.findString.toLowerCase();return i=i.trim(),{...e,findString:i}}};H=B([m(0,d.IUniverInstanceService),m(1,I.Inject(I.Injector))],H);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ue(t,e,i,n,r){const{findBy:o}=n,s=o===f.FindBy.FORMULA,c=t.getCellRaw(e,i);return g.rawData=c,!(c!=null&&c.f)?(g.isFormula=!1,U(r,n)?c?(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:c.f},n)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,U(r,n)?g.hit=!0:g.hit=!1,g))}function U(t,e){let i=ge(t);return i?e.matchesTheWholeCell?(i=_e(i),e.caseSensitive?i===e.findString:i.toLowerCase()===e.findString):e.caseSensitive?i.indexOf(e.findString)>-1:i.toLowerCase().indexOf(e.findString)>-1:!1}function ge(t){var i,n,r;const e=(r=(n=(i=t==null?void 0:t.p)==null?void 0:i.body)==null?void 0:n.dataStream)!=null?r:t==null?void 0:t.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function _e(t){return t.replace(/^ +/g,"").replace(/ +$/g,"")}var fe=Object.defineProperty,Se=Object.getOwnPropertyDescriptor,me=(t,e,i,n)=>{for(var r=n>1?void 0:n?Se(e,i):e,o=t.length-1,s;o>=0;o--)(s=t[o])&&(r=(n?s(e,i,r):s(r))||r);return n&&r&&fe(e,i,r),r},L=(t,e)=>(i,n)=>e(i,n,t);const pe="SHEET_FIND_REPLACE_PLUGIN";var x;exports.UniverSheetsFindReplacePlugin=(x=class extends d.Plugin{constructor(e,i,n){super(),this._injector=i,this._pluginService=n,this._pluginService.registerPlugin(f.UniverFindReplacePlugin)}onStarting(e){[[exports.SheetsFindReplaceController]].forEach(i=>e.add(i))}},_(x,"pluginName",pe),_(x,"type",d.UniverInstanceType.UNIVER_SHEET),x);exports.UniverSheetsFindReplacePlugin=me([L(1,I.Inject(I.Injector)),L(2,I.Inject(d.PluginService))],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 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._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);
package/lib/es/index.js CHANGED
@@ -1,118 +1,117 @@
1
- var ge = Object.defineProperty;
2
- var _e = (t, e, i) => e in t ? ge(t, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : t[e] = i;
3
- var g = (t, e, i) => (_e(t, typeof e != "symbol" ? e + "" : e, i), i);
4
- import { CommandType as H, IUndoRedoService as fe, ICommandService as O, Rectangle as Se, OnLifecycle as me, LifecycleStages as pe, ThemeService as ve, Disposable as q, EDITOR_ACTIVATED as Ce, IContextService as E, IUniverInstanceService as ee, UniverInstanceType as T, fromCallback as W, ColorKit as Ie, rotate as L, groupBy as Re, ObjectMatrix as we, Tools as ke, replaceInDocumentBody as be, Plugin as Me, PluginService as ye } from "@univerjs/core";
5
- import { Inject as p, Injector as D } from "@wendellhu/redi";
6
- import { FindReplaceController as xe, IFindReplaceService as Pe, FindModel as We, FindBy as y, FindScope as I, FindDirection as b, UniverFindReplacePlugin as Ue } from "@univerjs/find-replace";
7
- import { Shape as He, Rect as Fe, IRenderManagerService as Be, RENDER_RAW_FORMULA_KEY as Oe } from "@univerjs/engine-render";
8
- import { SetRangeValuesCommand as te, SelectionManagerService as Te, SetWorksheetActiveOperation as De, SetSelectionsOperation as Ae, SetWorksheetActivateCommand as Ne } from "@univerjs/sheets";
9
- import { SheetSkeletonManagerService as $e, getCoordByCell as G, getSheetObject as Ve, ScrollToCellCommand as je } from "@univerjs/sheets-ui";
10
- import { filter as x, Subject as Q, throttleTime as Le, merge as Ge, skip as Qe, debounceTime as Xe } from "rxjs";
11
- var ie = /* @__PURE__ */ ((t) => (t[t.View = 0] = "View", t[t.Edit = 1] = "Edit", t[t.ManageCollaborator = 2] = "ManageCollaborator", t[t.Print = 3] = "Print", t[t.Duplicate = 4] = "Duplicate", t[t.Comment = 5] = "Comment", t[t.Copy = 6] = "Copy", t[t.Share = 7] = "Share", t[t.Export = 8] = "Export", t[t.MoveWorksheet = 9] = "MoveWorksheet", t[t.DeleteWorksheet = 10] = "DeleteWorksheet", t[t.HideWorksheet = 11] = "HideWorksheet", t[t.RenameWorksheet = 12] = "RenameWorksheet", t[t.CreateWorksheet = 13] = "CreateWorksheet", t[t.SetWorksheetStyle = 14] = "SetWorksheetStyle", t[t.EditWorksheetCell = 15] = "EditWorksheetCell", t[t.InsertHyperlink = 16] = "InsertHyperlink", t[t.Sort = 17] = "Sort", t[t.Filter = 18] = "Filter", t[t.PivotTable = 19] = "PivotTable", t[t.FloatImg = 20] = "FloatImg", t[t.History = 21] = "History", t[t.RwHgtClWdt = 22] = "RwHgtClWdt", t[t.ViemRwHgtClWdt = 23] = "ViemRwHgtClWdt", t[t.ViewFilter = 24] = "ViewFilter", t[t.MoveSheet = 25] = "MoveSheet", t[t.DeleteSheet = 26] = "DeleteSheet", t[t.HideSheet = 27] = "HideSheet", t[t.CopySheet = 28] = "CopySheet", t[t.RenameSheet = 29] = "RenameSheet", t[t.CreateSheet = 30] = "CreateSheet", t[t.SelectProtectedCells = 31] = "SelectProtectedCells", t[t.SelectUnProtectedCells = 32] = "SelectUnProtectedCells", t[t.SetCellStyle = 33] = "SetCellStyle", t[t.SetCellValue = 34] = "SetCellValue", t[t.SetRowStyle = 35] = "SetRowStyle", t[t.SetColumnStyle = 36] = "SetColumnStyle", t[t.InsertRow = 37] = "InsertRow", t[t.InsertColumn = 38] = "InsertColumn", t[t.DeleteRow = 39] = "DeleteRow", t[t.DeleteColumn = 40] = "DeleteColumn", t[t.EditExtraObject = 41] = "EditExtraObject", t[t.UNRECOGNIZED = -1] = "UNRECOGNIZED", t))(ie || {});
12
- class Ye extends He {
13
- constructor(i, n) {
14
- super(i, n);
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);
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
+ import { Inject as C, Injector as O } from "@wendellhu/redi";
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";
7
+ import { SetRangeValuesCommand as q, SelectionManagerService as Fe, SetWorksheetActiveOperation as He, SetSelectionsOperation as Oe, SetWorksheetActivateCommand as Te, UniverSheetsPlugin as L } from "@univerjs/sheets";
8
+ import { Shape as Ee, Rect as Ae, IRenderManagerService as ee, RENDER_RAW_FORMULA_KEY as Ne } from "@univerjs/engine-render";
9
+ import { SheetSkeletonManagerService as De, getCoordByCell as j, getSheetObject as $e, ScrollToCellCommand as Le } from "@univerjs/sheets-ui";
10
+ import { filter as k, Subject as V, throttleTime as je, merge as Ve, skip as Qe, debounceTime as Xe } from "rxjs";
11
+ class Ye extends Ee {
12
+ constructor(t, i) {
13
+ super(t, i);
15
14
  g(this, "_activated", !1);
16
15
  g(this, "_inHiddenRange", !1);
17
16
  g(this, "_color");
18
- n && this.setShapeProps(n);
17
+ i && this.setShapeProps(i);
19
18
  }
20
- setShapeProps(i) {
21
- this._activated = !!i.activated, typeof i.inHiddenRange < "u" && (this._inHiddenRange = i.inHiddenRange), typeof i.color < "u" && (this._color = i.color), this.transformByState({
22
- width: i.width,
23
- height: i.height
19
+ setShapeProps(t) {
20
+ this._activated = !!t.activated, typeof t.inHiddenRange < "u" && (this._inHiddenRange = t.inHiddenRange), typeof t.color < "u" && (this._color = t.color), this.transformByState({
21
+ width: t.width,
22
+ height: t.height
24
23
  });
25
24
  }
26
- _draw(i) {
27
- const n = this._activated, s = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`, o = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
28
- Fe.drawWith(i, {
25
+ _draw(t) {
26
+ const i = this._activated, n = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`, r = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
27
+ Ae.drawWith(t, {
29
28
  width: this.width,
30
29
  height: this.height,
31
- fill: s,
32
- stroke: n ? o : void 0,
33
- strokeWidth: n ? 2 : 0,
30
+ fill: n,
31
+ stroke: i ? r : void 0,
32
+ strokeWidth: i ? 2 : 0,
34
33
  evented: !1
35
34
  });
36
35
  }
37
36
  }
38
- const ne = {
37
+ const te = {
39
38
  id: "sheet.command.replace",
40
- type: H.COMMAND,
41
- handler: async (t, e) => {
42
- const i = t.get(fe), n = t.get(O), { unitId: s, replacements: o } = e, r = i.__tempBatchingUndoRedo(s), h = await Promise.all(o.map((c) => n.executeCommand(te.id, {
43
- unitId: s,
39
+ type: B.COMMAND,
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, {
42
+ unitId: n,
44
43
  subUnitId: c.subUnitId,
45
44
  value: c.value
46
45
  })));
47
- return r.dispose(), Ze(h, o);
46
+ return s.dispose(), Ge(o, r);
48
47
  }
49
48
  };
50
- function Ze(t, e) {
51
- let i = 0, n = 0;
52
- return t.forEach((s, o) => {
53
- const r = e[o].count;
54
- s ? i += r : n += r;
55
- }), { success: i, failure: n };
49
+ function Ge(h, e) {
50
+ let t = 0, i = 0;
51
+ return h.forEach((n, r) => {
52
+ const s = e[r].count;
53
+ n ? t += s : i += s;
54
+ }), { success: t, failure: i };
56
55
  }
57
- function X(t, e) {
58
- return t.startRow === e.startRow && t.startColumn === e.startColumn;
56
+ function Q(h, e) {
57
+ return h.startRow === e.startRow && h.startColumn === e.startColumn;
59
58
  }
60
- function Y(t, e) {
61
- return t.startRow < e.startRow || t.startRow === e.startRow && t.startColumn <= e.startColumn;
59
+ function X(h, e) {
60
+ return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
62
61
  }
63
- function Z(t, e) {
64
- return t.startColumn < e.startColumn || t.startColumn === e.startColumn && t.startRow <= e.startRow;
62
+ function Y(h, e) {
63
+ return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
65
64
  }
66
- function Ke(t, e) {
67
- return t.startRow > e.startRow || t.startRow === e.startRow && t.startColumn >= e.startColumn;
65
+ function Ke(h, e) {
66
+ return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
68
67
  }
69
- function ze(t, e) {
70
- return t.startColumn > e.startColumn || t.startColumn === e.startColumn && t.startRow >= e.startRow;
68
+ function ze(h, e) {
69
+ return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
71
70
  }
72
- function Je(t, e) {
73
- const { range: i } = t, { startRow: n, startColumn: s } = i, o = e.getMergedCell(n, s);
74
- return o ? Se.equals(i, o) : i.endRow === i.startRow && i.endColumn === i.startColumn;
71
+ function Je(h, e) {
72
+ const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
73
+ return r ? Se.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
75
74
  }
76
- var qe = Object.defineProperty, Ee = Object.getOwnPropertyDescriptor, A = (t, e, i, n) => {
77
- for (var s = n > 1 ? void 0 : n ? Ee(e, i) : e, o = t.length - 1, r; o >= 0; o--)
78
- (r = t[o]) && (s = (n ? r(e, i, s) : r(s)) || s);
79
- return n && s && qe(e, i, s), s;
80
- }, f = (t, e) => (i, n) => e(i, n, t);
81
- let w = class extends q {
82
- constructor(e, i, n, s, o) {
75
+ var Ze = Object.defineProperty, qe = Object.getOwnPropertyDescriptor, T = (h, e, t, i) => {
76
+ for (var n = i > 1 ? void 0 : i ? qe(e, t) : e, r = h.length - 1, s; r >= 0; r--)
77
+ (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
78
+ return i && n && Ze(e, t, n), n;
79
+ }, f = (h, e) => (t, i) => e(t, i, h);
80
+ let x = class extends K {
81
+ constructor(e, t, i, n, r) {
83
82
  super();
84
83
  g(this, "_provider");
85
- this._injector = e, this._findReplaceController = i, this._contextService = n, this._findReplaceService = s, this._commandService = o, this._init(), this._initCommands();
84
+ this._injector = e, this._findReplaceController = t, this._contextService = i, this._findReplaceService = n, this._commandService = r, this._init(), this._initCommands();
86
85
  }
87
86
  dispose() {
88
87
  super.dispose(), this._provider.dispose();
89
88
  }
90
89
  _init() {
91
- const e = this._injector.createInstance(B);
90
+ const e = this._injector.createInstance(F);
92
91
  this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(
93
- this._contextService.subscribeContextValue$(Ce).pipe(x((i) => !!i)).subscribe(() => this._findReplaceController.closePanel())
92
+ this._contextService.subscribeContextValue$(Ce).pipe(k((t) => !!t)).subscribe(() => this._findReplaceController.closePanel())
94
93
  );
95
94
  }
96
95
  _initCommands() {
97
- [ne].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
96
+ [te].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
98
97
  }
99
98
  };
100
- w = A([
101
- me(pe.Steady, w),
102
- f(0, p(D)),
103
- f(1, p(xe)),
104
- f(2, E),
99
+ x = T([
100
+ pe(ve.Steady, x),
101
+ f(0, C(O)),
102
+ f(1, C(Ue)),
103
+ f(2, z),
105
104
  f(3, Pe),
106
- f(4, O)
107
- ], w);
105
+ f(4, H)
106
+ ], x);
108
107
  const et = "sheets-find-replace-provider", tt = 1e4;
109
- let F = class extends We {
110
- constructor(e, i, n, s, o, r, h, c, a) {
108
+ let W = class extends Be {
109
+ constructor(e, t, i, n, r, s, o, c) {
111
110
  super();
112
111
  // We can directly inject the `FindReplaceService` here, and call its methods instead of using the observables.
113
- g(this, "_matchesUpdate$", new Q());
112
+ g(this, "_matchesUpdate$", new V());
114
113
  g(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
115
- g(this, "_activelyChangingMatch$", new Q());
114
+ g(this, "_activelyChangingMatch$", new V());
116
115
  g(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
117
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. */
118
117
  g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
@@ -125,7 +124,7 @@ let F = class extends We {
125
124
  g(this, "_currentHighlightShape", null);
126
125
  /** This properties holds the query params during this searching session. */
127
126
  g(this, "_query", null);
128
- this._workbook = e, this._univerInstanceService = i, this._renderManagerService = n, this._commandService = s, this._contextService = o, this._themeService = r, this._sheetSkeletonManagerService = h, this._selectionManagerService = c, this._findReplaceController = a;
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;
129
128
  }
130
129
  get _matchesCount() {
131
130
  return this._matches.length;
@@ -149,7 +148,7 @@ let F = class extends We {
149
148
  return this._matches;
150
149
  }
151
150
  start(e) {
152
- switch (this._query = e, e.findBy === y.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
151
+ switch (this._query = e, e.findBy === M.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
153
152
  case I.UNIT:
154
153
  this.findInWorkbook(e);
155
154
  break;
@@ -160,7 +159,7 @@ let F = class extends We {
160
159
  }
161
160
  }
162
161
  _toggleDisplayRawFormula(e) {
163
- this._contextService.setContextValue(Oe, e);
162
+ this._contextService.setContextValue(Ne, e);
164
163
  }
165
164
  /**
166
165
  * Find all matches in the current workbook no matter which worksheet is activated.
@@ -168,41 +167,41 @@ let F = class extends We {
168
167
  * @returns the query complete event
169
168
  */
170
169
  findInWorkbook(e) {
171
- const i = this._workbook.getUnitId();
172
- let n, s = !0;
173
- const o = () => {
174
- const r = this._workbook.getSheets().filter((h) => !h.isSheetHidden()).map((h) => {
175
- const c = this._findInWorksheet(h, e, i), a = h.getSheetId(), { results: l } = c;
176
- return l.length ? this._matchesByWorksheet.set(a, c.results) : this._matchesByWorksheet.delete(a), c;
170
+ const t = this._workbook.getUnitId();
171
+ let i, n = !0;
172
+ const r = () => {
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;
177
176
  });
178
- this._matches = r.map((h) => h.results).flat(), this._updateFindHighlight(), s ? (n = { results: this._matches }, s = !1) : this._matchesUpdate$.next(this._matches);
177
+ this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
179
178
  };
180
179
  return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
181
180
  this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
182
181
  })), this.disposeWithMe(
183
- W(this._commandService.onCommandExecuted).pipe(
184
- x(([r, h]) => r.id === De.id && !(h != null && h.fromFindReplace))
182
+ U(this._commandService.onCommandExecuted).pipe(
183
+ k(([s, o]) => s.id === He.id && !(o != null && o.fromFindReplace))
185
184
  ).subscribe(() => {
186
- const r = this._workbook.getActiveSheet(), h = r.getSheetId();
187
- this._matchesByWorksheet.has(h) && this._findNextMatchOnActiveSheetChange(r);
185
+ const s = this._workbook.getActiveSheet(), o = s.getSheetId();
186
+ this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
188
187
  })
189
188
  ), this.disposeWithMe(
190
- W(this._commandService.onCommandExecuted).pipe(
191
- x(
192
- ([r]) => r.type === H.MUTATION && r.params.unitId === this._workbook.getUnitId()
189
+ U(this._commandService.onCommandExecuted).pipe(
190
+ k(
191
+ ([s]) => s.type === B.MUTATION && s.params.unitId === this._workbook.getUnitId()
193
192
  ),
194
- Le(600, void 0, { leading: !1, trailing: !0 })
195
- ).subscribe(() => o())
196
- ), o(), n;
193
+ je(600, void 0, { leading: !1, trailing: !0 })
194
+ ).subscribe(() => r())
195
+ ), r(), i;
197
196
  }
198
197
  /**
199
198
  * This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
200
199
  * in the new worksheet.
201
200
  */
202
201
  _findNextMatchOnActiveSheetChange(e) {
203
- let i, n, s = 0;
204
- const o = this._matchesByWorksheet.get(e.getSheetId()), r = this._selectionManagerService.getSelections();
205
- r != null && r.length ? ([i, s] = this._findNextMatchByRange(o, r[0].range), n = o.findIndex((h) => h === i)) : (i = o[0], n = 0, s = this._matches.findIndex((h) => h === i)), this._matchesPosition = s + 1, this._activelyChangingMatch$.next(i), this._activeHighlightIndex = n, this._updateFindHighlight(), this._updateCurrentHighlightShape(n);
202
+ let t, i, n = 0;
203
+ const r = this._matchesByWorksheet.get(e.getSheetId()), s = this._selectionManagerService.getSelections();
204
+ s != null && s.length ? ([t, n] = this._findNextMatchByRange(r, s[0].range), i = r.findIndex((o) => o === t)) : (t = r[0], i = 0, n = this._matches.findIndex((o) => o === t)), this._matchesPosition = n + 1, this._activelyChangingMatch$.next(t), this._activeHighlightIndex = i, this._updateFindHighlight(), this._updateCurrentHighlightShape(i);
206
205
  }
207
206
  /**
208
207
  * Find all matches (only) in the currently activated worksheet.
@@ -210,331 +209,321 @@ let F = class extends We {
210
209
  * @returns the query complete event
211
210
  */
212
211
  findInActiveWorksheet(e) {
213
- const i = this._workbook.getUnitId(), n = () => {
212
+ const t = this._workbook.getUnitId(), i = () => {
214
213
  var d;
215
- const c = this._workbook.getActiveSheet(), a = this._selectionManagerService.getSelections();
216
- return (d = a == null ? void 0 : a.some((_) => !Je(_, c))) != null ? d : !1;
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
216
  };
218
- let s, o = !0, r = !1;
219
- const h = () => {
220
- const c = this._workbook.getActiveSheet(), a = this.currentMatch;
221
- r = n();
222
- const l = this._selectionManagerService.getSelections(), d = r ? this._findInSelections(c, l, e, i) : this._findInWorksheet(c, e, i);
223
- return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(a, this._matches), o ? (s = d, o = !1) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), d;
217
+ let n, r = !0, s = !1;
218
+ const o = () => {
219
+ const c = this._workbook.getActiveSheet(), l = this.currentMatch;
220
+ s = i();
221
+ const a = this._selectionManagerService.getSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
222
+ 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;
224
223
  };
225
224
  return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
226
- Ge(
227
- W(this._commandService.onCommandExecuted).pipe(
228
- x(([c]) => {
229
- if (c.type === H.MUTATION && c.params.unitId === this._workbook.getUnitId())
225
+ Ve(
226
+ U(this._commandService.onCommandExecuted).pipe(
227
+ k(([c]) => {
228
+ if (c.type === B.MUTATION && c.params.unitId === this._workbook.getUnitId())
230
229
  return !0;
231
- if (c.id === Ae.id && c.params.unitId === i) {
232
- const a = n();
233
- return a === !1 && r === !1 ? !1 : (r = a, !0);
230
+ if (c.id === Oe.id && c.params.unitId === t) {
231
+ const l = i();
232
+ return l === !1 && s === !1 ? !1 : (s = l, !0);
234
233
  }
235
234
  return !1;
236
235
  })
237
236
  ),
238
237
  // activeSheet$ is a BehaviorSubject, so we need to skip the first
239
238
  this._workbook.activeSheet$.pipe(Qe(1))
240
- ).pipe(Xe(200)).subscribe(() => h())
241
- ), h(), s;
242
- }
243
- _findInRange(e, i, n, s, o) {
244
- const r = [], h = e.getSheetId(), c = (i.findDirection === b.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(n);
245
- for (const a of c) {
246
- const { row: l, col: d, colSpan: _, rowSpan: m, value: S } = a;
247
- if (!this._getFindReplacePermissionCheck({ row: l, col: d, unitId: s, subUnitId: h }) || o != null && o(l, d) || !S || e.getRowFiltered(l))
239
+ ).pipe(Xe(200)).subscribe(() => o())
240
+ ), o(), n;
241
+ }
242
+ _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))
248
247
  continue;
249
- const { hit: R, replaceable: v, isFormula: k } = it(e, l, d, i, S);
250
- if (R) {
251
- const P = {
248
+ const { hit: p, replaceable: R, isFormula: v } = it(e, a, d, t, m);
249
+ if (p) {
250
+ const w = {
252
251
  provider: et,
253
- unitId: s,
254
- replaceable: v,
255
- isFormula: k,
252
+ unitId: n,
253
+ replaceable: R,
254
+ isFormula: v,
256
255
  range: {
257
- subUnitId: h,
256
+ subUnitId: o,
258
257
  range: {
259
- startRow: l,
258
+ startRow: a,
260
259
  startColumn: d,
261
260
  endColumn: d + (_ != null ? _ : 1) - 1,
262
- endRow: l + (m != null ? m : 1) - 1
261
+ endRow: a + (S != null ? S : 1) - 1
263
262
  }
264
263
  }
265
264
  };
266
- r.push(P);
265
+ s.push(w);
267
266
  }
268
267
  }
269
- return { results: r };
268
+ return { results: s };
270
269
  }
271
- _findInSelections(e, i, n, s) {
272
- const { findDirection: o } = n, r = o === b.ROW ? Y : Z, h = /* @__PURE__ */ new Set();
273
- return { results: i.map((a) => this._findInRange(
270
+ _findInSelections(e, t, i, n) {
271
+ const { findDirection: r } = i, s = r === b.ROW ? X : Y, o = /* @__PURE__ */ new Set();
272
+ return { results: t.map((l) => this._findInRange(
274
273
  e,
274
+ i,
275
+ l.range,
275
276
  n,
276
- a.range,
277
- s,
278
- (l, d) => {
279
- const _ = `${l}-${d}`;
280
- return h.has(_) ? !0 : (h.add(_), !1);
277
+ (a, d) => {
278
+ const _ = `${a}-${d}`;
279
+ return o.has(_) ? !0 : (o.add(_), !1);
281
280
  }
282
- ).results).flat().sort((a, l) => r(a.range.range, l.range.range) ? -1 : 1) };
281
+ ).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
283
282
  }
284
283
  /** Find matches in a given worksheet. */
285
- _findInWorksheet(e, i, n) {
286
- const s = e.getRowCount(), o = e.getColumnCount(), r = { startRow: 0, startColumn: 0, endRow: s - 1, endColumn: o - 1 };
287
- return this._findInRange(e, i, r, n);
284
+ _findInWorksheet(e, t, i) {
285
+ const n = e.getRowCount(), r = e.getColumnCount(), s = { startRow: 0, startColumn: 0, endRow: n - 1, endColumn: r - 1 };
286
+ return this._findInRange(e, t, s, i);
288
287
  }
289
288
  _disposeHighlights() {
290
289
  var e;
291
- this._highlightShapes.forEach((i) => {
292
- i.getScene().makeDirty(), i.dispose();
290
+ this._highlightShapes.forEach((t) => {
291
+ t.getScene().makeDirty(), t.dispose();
293
292
  }), this._highlightShapes = [], (e = this._currentHighlightShape) == null || e.dispose(), this._currentHighlightShape = null;
294
293
  }
295
294
  _updateFindHighlight() {
296
- var m, S;
295
+ var S, m;
297
296
  this._disposeHighlights();
298
- const e = (m = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : m.skeleton;
299
- if (!e || !this._getSheetObject() || ((S = this._univerInstanceService.getFocusedUnit()) == null ? void 0 : S.getUnitId()) !== this._workbook.getUnitId())
297
+ const e = (S = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : S.skeleton;
298
+ if (!e || !this._getSheetObject() || ((m = this._univerInstanceService.getFocusedUnit()) == null ? void 0 : m.getUnitId()) !== this._workbook.getUnitId())
300
299
  return;
301
- const s = this._workbook.getUnitId(), o = this._renderManagerService.getRenderById(s);
302
- if (o == null)
300
+ const n = this._workbook.getUnitId(), r = this._renderManagerService.getRenderById(n);
301
+ if (r == null)
303
302
  return;
304
- const { scene: r } = o, h = this._matches, c = this._themeService.getCurrentTheme().gold400, a = new Ie(c).toRgb(), l = this._workbook.getActiveSheet(), d = l.getSheetId(), _ = h.filter((C) => C.range.subUnitId === d).map((C, R) => {
305
- const { startColumn: v, startRow: k, endColumn: P, endRow: se } = C.range.range, re = G(k, v, r, e), oe = G(se, P, r, e), { startX: N, startY: $ } = re, { endX: he, endY: ce } = oe, V = !l.getRowRawVisible(k), j = !l.getColVisible(v), ae = V || j, le = j ? 2 : he - N, de = V ? 2 : ce - $, ue = {
306
- left: N,
307
- top: $,
308
- color: a,
309
- width: le,
310
- height: de,
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 = {
305
+ left: E,
306
+ top: A,
307
+ color: l,
308
+ width: de,
309
+ height: ue,
311
310
  evented: !1,
312
- inHiddenRange: ae,
311
+ inHiddenRange: le,
313
312
  zIndex: tt
314
313
  };
315
- return new Ye(`find-highlight-${R}`, ue);
314
+ return new Ye(`find-highlight-${R}`, ge);
316
315
  });
317
- r.addObjects(_), this._highlightShapes = _, r.makeDirty();
316
+ s.addObjects(_), this._highlightShapes = _, s.makeDirty();
318
317
  }
319
318
  _updateCurrentHighlightShape(e) {
320
- var i;
321
- if ((i = this._currentHighlightShape) == null || i.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, e !== void 0) {
322
- const n = this._highlightShapes[e];
323
- if (!n)
319
+ var t;
320
+ if ((t = this._currentHighlightShape) == null || t.setShapeProps({ activated: !1 }), this._currentHighlightShape = null, e !== void 0) {
321
+ const i = this._highlightShapes[e];
322
+ if (!i)
324
323
  return;
325
- this._currentHighlightShape = n, n.setShapeProps({ activated: !0 });
324
+ this._currentHighlightShape = i, i.setShapeProps({ activated: !0 });
326
325
  }
327
326
  }
328
327
  _getSheetObject() {
329
- return Ve(this._univerInstanceService, this._renderManagerService);
328
+ return $e(this._univerInstanceService, this._renderManagerService);
330
329
  }
331
330
  _focusMatch(e) {
332
- const i = e.range.subUnitId;
333
- i !== this._workbook.getActiveSheet().getSheetId() && this._commandService.syncExecuteCommand(
334
- Ne.id,
335
- { unitId: this._workbook.getUnitId(), subUnitId: i },
331
+ const t = e.range.subUnitId;
332
+ t !== this._workbook.getActiveSheet().getSheetId() && this._commandService.syncExecuteCommand(
333
+ Te.id,
334
+ { unitId: this._workbook.getUnitId(), subUnitId: t },
336
335
  { fromFindReplace: !0 }
337
336
  ), this._commandService.syncExecuteCommand(
338
- je.id,
337
+ Le.id,
339
338
  { range: e.range.range },
340
339
  { fromFindReplace: !0 }
341
340
  );
342
341
  }
343
- _tryRestoreLastMatchesPosition(e, i) {
342
+ _tryRestoreLastMatchesPosition(e, t) {
344
343
  if (!e)
345
344
  return 0;
346
- const { subUnitId: n } = e.range, { startColumn: s, startRow: o } = e.range.range, r = i.findIndex((h) => {
347
- if (n !== h.range.subUnitId)
345
+ const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
346
+ if (i !== o.range.subUnitId)
348
347
  return !1;
349
- const { startColumn: c, startRow: a } = h.range.range;
350
- return c === s && a === o;
348
+ const { startColumn: c, startRow: l } = o.range.range;
349
+ return c === n && l === r;
351
350
  });
352
- return r > -1 ? r + 1 : 0;
351
+ return s > -1 ? s + 1 : 0;
353
352
  }
354
353
  moveToNextMatch(e) {
355
- var r, h, c;
354
+ var s, o, c;
356
355
  if (!this._matches.length)
357
356
  return null;
358
- const i = (r = e == null ? void 0 : e.loop) != null ? r : !1, n = (h = e == null ? void 0 : e.stayIfOnMatch) != null ? h : !1, s = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, o = this._findNextMatch(i, n);
359
- if (o) {
360
- const [a, l] = o;
361
- return this._matchesPosition = l + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(a.range.subUnitId).findIndex((d) => d === a) : this._activeHighlightIndex = l, s || this._focusMatch(a), this._workbook.getActiveSheet().getSheetId() === a.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), a;
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);
358
+ 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;
362
361
  }
363
362
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
364
363
  }
365
364
  moveToPreviousMatch(e) {
366
- var r, h, c;
365
+ var s, o, c;
367
366
  if (!this._matches.length)
368
367
  return null;
369
- const i = (r = e == null ? void 0 : e.loop) != null ? r : !1, n = (h = e == null ? void 0 : e.stayIfOnMatch) != null ? h : !1, s = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, o = this._findPreviousMatch(i, n);
370
- if (o) {
371
- const [a, l] = o;
372
- return this._matchesPosition = l + 1, this._query.findScope === I.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(a.range.subUnitId).findIndex((d) => d === a) : this._activeHighlightIndex = l, s || this._focusMatch(a), this._workbook.getActiveSheet().getSheetId() === a.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), a;
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);
369
+ 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;
373
372
  }
374
373
  return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
375
374
  }
376
- _findPreviousMatch(e = !1, i = !1) {
375
+ _findPreviousMatch(e = !1, t = !1) {
377
376
  if (this.currentMatch) {
378
- const r = this._matches.findIndex((l) => l === this.currentMatch);
379
- if (i)
380
- return [this.currentMatch, r];
381
- const h = r - 1;
382
- if (!e && h < 0)
377
+ const s = this._matches.findIndex((a) => a === this.currentMatch);
378
+ if (t)
379
+ return [this.currentMatch, s];
380
+ const o = s - 1;
381
+ if (!e && o < 0)
383
382
  return null;
384
- const c = this._matches.length, a = (h + c) % c;
385
- return [this._matches[a], a];
383
+ const c = this._matches.length, l = (o + c) % c;
384
+ return [this._matches[l], l];
386
385
  }
387
- const n = this._selectionManagerService.getSelections();
388
- if (!(n != null && n.length))
386
+ const i = this._selectionManagerService.getSelections();
387
+ if (!(i != null && i.length))
389
388
  return [this._matches[0], 0];
390
389
  if (this._query.findScope !== I.UNIT)
391
- return this._findPreviousMatchByRange(this._matches, n[0].range);
392
- const s = this._workbook.getActiveSheet().getSheetId(), o = this._findPreviousWorksheetThatHasAMatch(s, e);
393
- return o ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(o), n[0].range) : null;
390
+ return this._findPreviousMatchByRange(this._matches, i[0].range);
391
+ const n = this._workbook.getActiveSheet().getSheetId(), r = this._findPreviousWorksheetThatHasAMatch(n, e);
392
+ return r ? this._findPreviousMatchByRange(this._matchesByWorksheet.get(r), i[0].range) : null;
394
393
  }
395
- _findNextMatch(e = !1, i = !1) {
394
+ _findNextMatch(e = !1, t = !1) {
396
395
  if (this.currentMatch) {
397
- const r = this._matches.findIndex((l) => l === this.currentMatch);
398
- if (i)
399
- return [this.currentMatch, r];
400
- const h = r + 1, c = this._matches.length;
401
- if (!e && h >= c)
396
+ const s = this._matches.findIndex((a) => a === this.currentMatch);
397
+ if (t)
398
+ return [this.currentMatch, s];
399
+ const o = s + 1, c = this._matches.length;
400
+ if (!e && o >= c)
402
401
  return null;
403
- const a = h % c;
404
- return [this._matches[a], a];
402
+ const l = o % c;
403
+ return [this._matches[l], l];
405
404
  }
406
- const n = this._selectionManagerService.getSelections();
407
- if (!(n != null && n.length))
405
+ const i = this._selectionManagerService.getSelections();
406
+ if (!(i != null && i.length))
408
407
  return [this._matches[0], 0];
409
408
  if (this._query.findScope !== I.UNIT)
410
- return this._findNextMatchByRange(this._matches, n[0].range, i);
411
- const s = this._workbook.getActiveSheet().getSheetId(), o = this._findNextWorksheetThatHasAMatch(s, e);
412
- return o ? this._findNextMatchByRange(this._matchesByWorksheet.get(o), n[0].range) : null;
413
- }
414
- _findPreviousWorksheetThatHasAMatch(e, i = !1) {
415
- const n = this._workbook.getSheetOrders(), s = n.findIndex((h) => h === e), r = (i ? L(n, s + 1) : n.slice(0, s + 1)).findLast((h) => this._matchesByWorksheet.has(h));
416
- return r != null ? r : null;
417
- }
418
- _findNextWorksheetThatHasAMatch(e, i = !1) {
419
- const n = this._workbook.getSheetOrders(), s = n.findIndex((h) => h === e), r = (i ? L(n, s) : n.slice(s)).find((h) => this._matchesByWorksheet.has(h));
420
- return r != null ? r : null;
421
- }
422
- _findNextMatchByRange(e, i, n = !1) {
423
- const s = this._query.findDirection === b.ROW;
424
- let o = e.findIndex((h) => {
425
- const c = h.range.range;
426
- if (!(s ? Y(i, c) : Z(i, c)))
409
+ return this._findNextMatchByRange(this._matches, i[0].range, t);
410
+ const n = this._workbook.getActiveSheet().getSheetId(), r = this._findNextWorksheetThatHasAMatch(n, e);
411
+ return r ? this._findNextMatchByRange(this._matchesByWorksheet.get(r), i[0].range) : null;
412
+ }
413
+ _findPreviousWorksheetThatHasAMatch(e, t = !1) {
414
+ const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n + 1) : i.slice(0, n + 1)).findLast((o) => this._matchesByWorksheet.has(o));
415
+ return s != null ? s : null;
416
+ }
417
+ _findNextWorksheetThatHasAMatch(e, t = !1) {
418
+ const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
419
+ return s != null ? s : null;
420
+ }
421
+ _findNextMatchByRange(e, t, i = !1) {
422
+ const n = this._query.findDirection === b.ROW;
423
+ let r = e.findIndex((o) => {
424
+ const c = o.range.range;
425
+ if (!(n ? X(t, c) : Y(t, c)))
427
426
  return !1;
428
- const l = X(i, c);
429
- return n ? l : !l;
427
+ const a = Q(t, c);
428
+ return i ? a : !a;
430
429
  });
431
- o === -1 && (o = e.length - 1);
432
- const r = e[o];
433
- return [r, this._matches.findIndex((h) => h === r)];
434
- }
435
- _findPreviousMatchByRange(e, i, n = !1) {
436
- const s = this._query.findDirection === b.ROW;
437
- let o = this._matches.findLastIndex((h) => {
438
- const c = h.range.range;
439
- if (!(s ? Ke(i, c) : ze(i, c)))
430
+ r === -1 && (r = e.length - 1);
431
+ const s = e[r];
432
+ return [s, this._matches.findIndex((o) => o === s)];
433
+ }
434
+ _findPreviousMatchByRange(e, t, i = !1) {
435
+ const n = this._query.findDirection === b.ROW;
436
+ let r = this._matches.findLastIndex((o) => {
437
+ const c = o.range.range;
438
+ if (!(n ? Ke(t, c) : ze(t, c)))
440
439
  return !1;
441
- const l = X(i, c);
442
- return n ? l : !l;
440
+ const a = Q(t, c);
441
+ return i ? a : !a;
443
442
  });
444
- o === -1 && (o = 0);
445
- const r = e[o];
446
- return [r, this._matches.findIndex((h) => h === r)];
443
+ r === -1 && (r = 0);
444
+ const s = e[r];
445
+ return [s, this._matches.findIndex((o) => o === s)];
447
446
  }
448
447
  async replace(e) {
449
448
  if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable)
450
449
  return !1;
451
- const i = this.currentMatch.range, n = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), s = this._getReplacedCellData(
450
+ const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
452
451
  this.currentMatch,
453
- n,
454
- this._query.findBy === y.FORMULA,
452
+ i,
453
+ this._query.findBy === M.FORMULA,
455
454
  this._query.findString,
456
455
  e,
457
456
  this._query.caseSensitive ? "g" : "ig"
458
- ), o = {
457
+ ), r = {
459
458
  unitId: this.currentMatch.unitId,
460
- subUnitId: i.subUnitId,
459
+ subUnitId: t.subUnitId,
461
460
  value: {
462
- [i.range.startRow]: {
463
- [i.range.startColumn]: s
461
+ [t.range.startRow]: {
462
+ [t.range.startColumn]: n
464
463
  }
465
464
  }
466
465
  };
467
- return this._commandService.executeCommand(te.id, o);
466
+ return this._commandService.executeCommand(q.id, r);
468
467
  }
469
468
  async replaceAll(e) {
470
469
  if (this._matchesCount === 0 || !this._query)
471
470
  return { success: 0, failure: 0 };
472
- const i = this._workbook.getUnitId(), { findString: n, caseSensitive: s, findBy: o } = this._query, r = o === y.FORMULA, h = s ? "g" : "ig", c = [];
473
- return Re(this._matches.filter((l) => l.replaceable), (l) => l.range.subUnitId).forEach((l, d) => {
474
- const _ = new we(), m = this._workbook.getSheetBySheetId(d);
475
- l.forEach((S) => {
476
- const { startColumn: C, startRow: R } = S.range.range, v = this._getReplacedCellData(S, m, r, n, e, h);
477
- v && _.setValue(R, C, v);
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) => {
475
+ const { startColumn: p, startRow: R } = m.range.range, v = this._getReplacedCellData(m, S, s, i, e, o);
476
+ v && _.setValue(R, p, v);
478
477
  }), c.push({
479
- count: l.length,
478
+ count: a.length,
480
479
  subUnitId: d,
481
480
  value: _.getMatrix()
482
481
  });
483
- }), c ? this._commandService.executeCommand(ne.id, {
484
- unitId: i,
482
+ }), c ? this._commandService.executeCommand(te.id, {
483
+ unitId: t,
485
484
  replacements: c
486
485
  }) : { success: 0, failure: 0 };
487
486
  }
488
- _getReplacedCellData(e, i, n, s, o, r) {
489
- var m;
490
- const h = e.range.range, { startRow: c, startColumn: a } = h, l = i.getCellRaw(c, a);
487
+ _getReplacedCellData(e, t, i, n, r, s) {
488
+ var S;
489
+ const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
491
490
  if (e.isFormula)
492
- return n ? { f: l.f.replace(new RegExp(K(s), r), o), v: null } : null;
493
- if (!!((m = l.p) != null && m.body)) {
494
- const S = ke.deepClone(l.p);
495
- return be(S.body, s, o), { p: S };
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);
494
+ return ke(m.body, n, r), { p: m };
496
495
  }
497
- return { v: l.v.toString().replace(new RegExp(K(s), r), o) };
498
- }
499
- _getFindReplacePermissionCheck(e) {
500
- var a, l;
501
- const { row: i, col: n, subUnitId: s } = e, r = this._univerInstanceService.getCurrentUnitForType(T.UNIVER_SHEET).getSheetBySheetId(s);
502
- if (!r)
503
- return !1;
504
- const h = (l = (a = r.getCell(i, n)) == null ? void 0 : a.selectionProtection) == null ? void 0 : l[0];
505
- return (h == null ? void 0 : h[ie.View]) !== !1;
496
+ return { v: a.v.toString().replace(new RegExp(G(n), s), r) };
506
497
  }
507
498
  };
508
- F = A([
509
- f(1, ee),
510
- f(2, Be),
511
- f(3, O),
512
- f(4, E),
513
- f(5, p(ve)),
514
- f(6, p($e)),
515
- f(7, p(Te)),
516
- f(8, p(w))
517
- ], F);
518
- function K(t) {
519
- return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
499
+ 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))
506
+ ], W);
507
+ function G(h) {
508
+ return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
520
509
  }
521
- let B = class extends q {
522
- constructor(e, i) {
510
+ let F = class extends K {
511
+ constructor(e, t, i) {
523
512
  super();
524
513
  /**
525
514
  * Hold all find results in this kind of univer business instances (Workbooks).
526
515
  */
527
516
  g(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
528
- this._univerInstanceService = e, this._injector = i;
517
+ this._univerInstanceService = e, this._renderManagerService = t, this._injector = i;
529
518
  }
530
519
  async find(e) {
531
520
  this._terminate();
532
- const i = this._univerInstanceService.getCurrentUnitForType(T.UNIVER_SHEET);
533
- if (i) {
534
- const n = this._injector.createInstance(F, i);
535
- this._findModelsByUnitId.set(i.getUnitId(), n);
536
- const s = this._preprocessQuery(e);
537
- return n.start(s), [n];
521
+ const t = this._univerInstanceService.getCurrentUnitForType(Z.UNIVER_SHEET), i = this._renderManagerService.getRenderById(t.getUnitId()).with(De);
522
+ if (t) {
523
+ const n = this._injector.createInstance(W, t, i);
524
+ this._findModelsByUnitId.set(t.getUnitId(), n);
525
+ const r = this._preprocessQuery(e);
526
+ return n.start(r), [n];
538
527
  }
539
528
  return [];
540
529
  }
@@ -550,54 +539,56 @@ let B = class extends q {
550
539
  * @returns the parsed query object
551
540
  */
552
541
  _preprocessQuery(e) {
553
- let i = e.caseSensitive ? e.findString : e.findString.toLowerCase();
554
- return i = i.trim(), {
542
+ let t = e.caseSensitive ? e.findString : e.findString.toLowerCase();
543
+ return t = t.trim(), {
555
544
  ...e,
556
- findString: i
545
+ findString: t
557
546
  };
558
547
  }
559
548
  };
560
- B = A([
561
- f(0, ee),
562
- f(1, p(D))
563
- ], B);
549
+ F = T([
550
+ f(0, J),
551
+ f(1, ee),
552
+ f(2, C(O))
553
+ ], F);
564
554
  const u = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
565
- function it(t, e, i, n, s) {
566
- const { findBy: o } = n, r = o === y.FORMULA, h = t.getCellRaw(e, i);
567
- return u.rawData = h, !(h != null && h.f) ? (u.isFormula = !1, U(s, n) ? h ? (u.hit = !0, u.replaceable = !0) : (u.hit = !0, u.replaceable = !1) : (u.hit = !1, u.replaceable = !1), u) : (u.isFormula = !0, r ? U({ v: h.f }, n) ? (u.hit = !0, u.replaceable = !0, u) : (u.hit = !1, u.replaceable = !1, u) : (u.replaceable = !1, U(s, n) ? u.hit = !0 : u.hit = !1, u));
555
+ function it(h, e, t, i, n) {
556
+ const { findBy: r } = i, s = r === M.FORMULA, o = h.getCellRaw(e, t);
557
+ return u.rawData = o, !(o != null && o.f) ? (u.isFormula = !1, P(n, i) ? o ? (u.hit = !0, u.replaceable = !0) : (u.hit = !0, u.replaceable = !1) : (u.hit = !1, u.replaceable = !1), u) : (u.isFormula = !0, s ? P({ v: o.f }, i) ? (u.hit = !0, u.replaceable = !0, u) : (u.hit = !1, u.replaceable = !1, u) : (u.replaceable = !1, P(n, i) ? u.hit = !0 : u.hit = !1, u));
568
558
  }
569
- function U(t, e) {
570
- let i = nt(t);
571
- return i ? e.matchesTheWholeCell ? (i = st(i), e.caseSensitive ? i === e.findString : i.toLowerCase() === e.findString) : e.caseSensitive ? i.indexOf(e.findString) > -1 : i.toLowerCase().indexOf(e.findString) > -1 : !1;
559
+ function P(h, e) {
560
+ let t = nt(h);
561
+ return t ? e.matchesTheWholeCell ? (t = st(t), e.caseSensitive ? t === e.findString : t.toLowerCase() === e.findString) : e.caseSensitive ? t.indexOf(e.findString) > -1 : t.toLowerCase().indexOf(e.findString) > -1 : !1;
572
562
  }
573
- function nt(t) {
574
- var i, n, s;
575
- const e = (s = (n = (i = t == null ? void 0 : t.p) == null ? void 0 : i.body) == null ? void 0 : n.dataStream) != null ? s : t == null ? void 0 : t.v;
563
+ function nt(h) {
564
+ var t, i, n;
565
+ const e = (n = (i = (t = h == null ? void 0 : h.p) == null ? void 0 : t.body) == null ? void 0 : i.dataStream) != null ? n : h == null ? void 0 : h.v;
576
566
  return typeof e == "number" ? `${e}` : typeof e == "boolean" ? e ? "1" : "0" : e;
577
567
  }
578
- function st(t) {
579
- return t.replace(/^ +/g, "").replace(/ +$/g, "");
568
+ function st(h) {
569
+ return h.replace(/^ +/g, "").replace(/ +$/g, "");
580
570
  }
581
- var rt = Object.defineProperty, ot = Object.getOwnPropertyDescriptor, ht = (t, e, i, n) => {
582
- for (var s = n > 1 ? void 0 : n ? ot(e, i) : e, o = t.length - 1, r; o >= 0; o--)
583
- (r = t[o]) && (s = (n ? r(e, i, s) : r(s)) || s);
584
- return n && s && rt(e, i, s), s;
585
- }, z = (t, e) => (i, n) => e(i, n, t);
586
- const ct = "SHEET_FIND_REPLACE_PLUGIN";
587
- var M;
588
- let J = (M = class extends Me {
589
- constructor(t, e, i) {
590
- super(), this._injector = e, this._pluginService = i, this._pluginService.registerPlugin(Ue);
591
- }
592
- onStarting(t) {
593
- [[w]].forEach((e) => t.add(e));
594
- }
595
- }, g(M, "pluginName", ct), g(M, "type", T.UNIVER_SHEET), M);
596
- J = ht([
597
- z(1, p(D)),
598
- z(2, p(ye))
599
- ], J);
571
+ var ie = Object.defineProperty, rt = Object.getOwnPropertyDescriptor, ot = (h, e, t) => e in h ? ie(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t, ht = (h, e, t, i) => {
572
+ for (var n = i > 1 ? void 0 : i ? rt(e, t) : e, r = h.length - 1, s; r >= 0; r--)
573
+ (s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
574
+ 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);
576
+ const at = "SHEET_FIND_REPLACE_PLUGIN";
577
+ let y = class extends ye {
578
+ constructor(h, e) {
579
+ super(), this._injector = e;
580
+ }
581
+ onStarting(h) {
582
+ [[x]].forEach((e) => h.add(e));
583
+ }
584
+ };
585
+ ne(y, "pluginName", at);
586
+ ne(y, "type", Z.UNIVER_SHEET);
587
+ y = ht([
588
+ xe(L, L, We),
589
+ ct(1, C(O))
590
+ ], y);
600
591
  export {
601
- w as SheetsFindReplaceController,
602
- J as UniverSheetsFindReplacePlugin
592
+ x as SheetsFindReplaceController,
593
+ y as UniverSheetsFindReplacePlugin
603
594
  };
@@ -33,14 +33,13 @@ export interface ISheetCellMatch extends IFindMatch {
33
33
  */
34
34
  export declare class SheetFindModel extends FindModel {
35
35
  private readonly _workbook;
36
+ private readonly _sheetSkeletonManagerService;
36
37
  private readonly _univerInstanceService;
37
38
  private readonly _renderManagerService;
38
39
  private readonly _commandService;
39
40
  private readonly _contextService;
40
41
  private readonly _themeService;
41
- private readonly _sheetSkeletonManagerService;
42
42
  private readonly _selectionManagerService;
43
- private readonly _findReplaceController;
44
43
  private readonly _matchesUpdate$;
45
44
  readonly matchesUpdate$: import('rxjs').Observable<ISheetCellMatch[]>;
46
45
  private readonly _activelyChangingMatch$;
@@ -61,7 +60,7 @@ export declare class SheetFindModel extends FindModel {
61
60
  get matchesCount(): number;
62
61
  get matchesPosition(): number;
63
62
  get currentMatch(): Nullable<ISheetCellMatch>;
64
- constructor(_workbook: Workbook, _univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _commandService: ICommandService, _contextService: IContextService, _themeService: ThemeService, _sheetSkeletonManagerService: SheetSkeletonManagerService, _selectionManagerService: SelectionManagerService, _findReplaceController: SheetsFindReplaceController);
63
+ constructor(_workbook: Workbook, _sheetSkeletonManagerService: SheetSkeletonManagerService, _univerInstanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _commandService: ICommandService, _contextService: IContextService, _themeService: ThemeService, _selectionManagerService: SelectionManagerService);
65
64
  dispose(): void;
66
65
  getMatches(): IFindMatch[];
67
66
  start(query: IFindQuery): void;
@@ -104,7 +103,6 @@ export declare class SheetFindModel extends FindModel {
104
103
  replace(replaceString: string): Promise<boolean>;
105
104
  replaceAll(replaceString: string): Promise<IReplaceAllResult>;
106
105
  private _getReplacedCellData;
107
- private _getFindReplacePermissionCheck;
108
106
  }
109
107
  interface IValuePassingObject {
110
108
  hit: boolean;
@@ -1,11 +1,10 @@
1
- import { Plugin, PluginService, UniverInstanceType } from '@univerjs/core';
1
+ import { Plugin, UniverInstanceType } from '@univerjs/core';
2
2
  import { Injector } from '@wendellhu/redi';
3
3
 
4
4
  export declare class UniverSheetsFindReplacePlugin extends Plugin {
5
5
  protected readonly _injector: Injector;
6
- private readonly _pluginService;
7
6
  static pluginName: string;
8
7
  static type: UniverInstanceType;
9
- constructor(_config: unknown, _injector: Injector, _pluginService: PluginService);
8
+ constructor(_config: unknown, _injector: Injector);
10
9
  onStarting(injector: Injector): void;
11
10
  }
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/engine-render"),require("@univerjs/sheets"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/engine-render","@univerjs/sheets","@univerjs/sheets-ui","rxjs"],d):(u=typeof globalThis<"u"?globalThis:u||self,d(u.UniverSheetsFindReplace={},u.UniverCore,u["@wendellhu/redi"],u.UniverFindReplace,u.UniverEngineRender,u.UniverSheets,u.UniverSheetsUi,u.rxjs))})(this,function(u,d,S,m,F,k,b,R){"use strict";var ve=Object.defineProperty;var Ce=(u,d,S)=>d in u?ve(u,d,{enumerable:!0,configurable:!0,writable:!0,value:S}):u[d]=S;var f=(u,d,S)=>(Ce(u,typeof d!="symbol"?d+"":d,S),S);var U;var O=(t=>(t[t.View=0]="View",t[t.Edit=1]="Edit",t[t.ManageCollaborator=2]="ManageCollaborator",t[t.Print=3]="Print",t[t.Duplicate=4]="Duplicate",t[t.Comment=5]="Comment",t[t.Copy=6]="Copy",t[t.Share=7]="Share",t[t.Export=8]="Export",t[t.MoveWorksheet=9]="MoveWorksheet",t[t.DeleteWorksheet=10]="DeleteWorksheet",t[t.HideWorksheet=11]="HideWorksheet",t[t.RenameWorksheet=12]="RenameWorksheet",t[t.CreateWorksheet=13]="CreateWorksheet",t[t.SetWorksheetStyle=14]="SetWorksheetStyle",t[t.EditWorksheetCell=15]="EditWorksheetCell",t[t.InsertHyperlink=16]="InsertHyperlink",t[t.Sort=17]="Sort",t[t.Filter=18]="Filter",t[t.PivotTable=19]="PivotTable",t[t.FloatImg=20]="FloatImg",t[t.History=21]="History",t[t.RwHgtClWdt=22]="RwHgtClWdt",t[t.ViemRwHgtClWdt=23]="ViemRwHgtClWdt",t[t.ViewFilter=24]="ViewFilter",t[t.MoveSheet=25]="MoveSheet",t[t.DeleteSheet=26]="DeleteSheet",t[t.HideSheet=27]="HideSheet",t[t.CopySheet=28]="CopySheet",t[t.RenameSheet=29]="RenameSheet",t[t.CreateSheet=30]="CreateSheet",t[t.SelectProtectedCells=31]="SelectProtectedCells",t[t.SelectUnProtectedCells=32]="SelectUnProtectedCells",t[t.SetCellStyle=33]="SetCellStyle",t[t.SetCellValue=34]="SetCellValue",t[t.SetRowStyle=35]="SetRowStyle",t[t.SetColumnStyle=36]="SetColumnStyle",t[t.InsertRow=37]="InsertRow",t[t.InsertColumn=38]="InsertColumn",t[t.DeleteRow=39]="DeleteRow",t[t.DeleteColumn=40]="DeleteColumn",t[t.EditExtraObject=41]="EditExtraObject",t[t.UNRECOGNIZED=-1]="UNRECOGNIZED",t))(O||{});class X extends F.Shape{constructor(i,n){super(i,n);f(this,"_activated",!1);f(this,"_inHiddenRange",!1);f(this,"_color");n&&this.setShapeProps(n)}setShapeProps(i){this._activated=!!i.activated,typeof i.inHiddenRange<"u"&&(this._inHiddenRange=i.inHiddenRange),typeof i.color<"u"&&(this._color=i.color),this.transformByState({width:i.width,height:i.height})}_draw(i){const n=this._activated,r=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,o=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;F.Rect.drawWith(i,{width:this.width,height:this.height,fill:r,stroke:n?o:void 0,strokeWidth:n?2:0,evented:!1})}}const D={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(t,e)=>{const i=t.get(d.IUndoRedoService),n=t.get(d.ICommandService),{unitId:r,replacements:o}=e,s=i.__tempBatchingUndoRedo(r),h=await Promise.all(o.map(c=>n.executeCommand(k.SetRangeValuesCommand.id,{unitId:r,subUnitId:c.subUnitId,value:c.value})));return s.dispose(),Y(h,o)}};function Y(t,e){let i=0,n=0;return t.forEach((r,o)=>{const s=e[o].count;r?i+=s:n+=s}),{success:i,failure:n}}function A(t,e){return t.startRow===e.startRow&&t.startColumn===e.startColumn}function j(t,e){return t.startRow<e.startRow||t.startRow===e.startRow&&t.startColumn<=e.startColumn}function N(t,e){return t.startColumn<e.startColumn||t.startColumn===e.startColumn&&t.startRow<=e.startRow}function Z(t,e){return t.startRow>e.startRow||t.startRow===e.startRow&&t.startColumn>=e.startColumn}function K(t,e){return t.startColumn>e.startColumn||t.startColumn===e.startColumn&&t.startRow>=e.startRow}function z(t,e){const{range:i}=t,{startRow:n,startColumn:r}=i,o=e.getMergedCell(n,r);return o?d.Rectangle.equals(i,o):i.endRow===i.startRow&&i.endColumn===i.startColumn}var J=Object.defineProperty,E=Object.getOwnPropertyDescriptor,x=(t,e,i,n)=>{for(var r=n>1?void 0:n?E(e,i):e,o=t.length-1,s;o>=0;o--)(s=t[o])&&(r=(n?s(e,i,r):s(r))||r);return n&&r&&J(e,i,r),r},v=(t,e)=>(i,n)=>e(i,n,t);u.SheetsFindReplaceController=class extends d.Disposable{constructor(i,n,r,o,s){super();f(this,"_provider");this._injector=i,this._findReplaceController=n,this._contextService=r,this._findReplaceService=o,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const i=this._injector.createInstance(H);this._provider=i,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(i)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(R.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[D].forEach(i=>this.disposeWithMe(this._commandService.registerCommand(i)))}},u.SheetsFindReplaceController=x([d.OnLifecycle(d.LifecycleStages.Steady,u.SheetsFindReplaceController),v(0,S.Inject(S.Injector)),v(1,S.Inject(m.FindReplaceController)),v(2,d.IContextService),v(3,m.IFindReplaceService),v(4,d.ICommandService)],u.SheetsFindReplaceController);const ee="sheets-find-replace-provider",te=1e4;let W=class extends m.FindModel{constructor(e,i,n,r,o,s,h,c,a){super();f(this,"_matchesUpdate$",new R.Subject);f(this,"matchesUpdate$",this._matchesUpdate$.asObservable());f(this,"_activelyChangingMatch$",new R.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._univerInstanceService=i,this._renderManagerService=n,this._commandService=r,this._contextService=o,this._themeService=s,this._sheetSkeletonManagerService=h,this._selectionManagerService=c,this._findReplaceController=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===m.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case m.FindScope.UNIT:this.findInWorkbook(e);break;case m.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(F.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const i=this._workbook.getUnitId();let n,r=!0;const o=()=>{const s=this._workbook.getSheets().filter(h=>!h.isSheetHidden()).map(h=>{const c=this._findInWorksheet(h,e,i),a=h.getSheetId(),{results:l}=c;return l.length?this._matchesByWorksheet.set(a,c.results):this._matchesByWorksheet.delete(a),c});this._matches=s.map(h=>h.results).flat(),this._updateFindHighlight(),r?(n={results:this._matches},r=!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,h])=>s.id===k.SetWorksheetActiveOperation.id&&!(h!=null&&h.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),h=s.getSheetId();this._matchesByWorksheet.has(h)&&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(()=>o())),o(),n}_findNextMatchOnActiveSheetChange(e){let i,n,r=0;const o=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([i,r]=this._findNextMatchByRange(o,s[0].range),n=o.findIndex(h=>h===i)):(i=o[0],n=0,r=this._matches.findIndex(h=>h===i)),this._matchesPosition=r+1,this._activelyChangingMatch$.next(i),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const i=this._workbook.getUnitId(),n=()=>{var g;const c=this._workbook.getActiveSheet(),a=this._selectionManagerService.getSelections();return(g=a==null?void 0:a.some(C=>!z(C,c)))!=null?g:!1};let r,o=!0,s=!1;const h=()=>{const c=this._workbook.getActiveSheet(),a=this.currentMatch;s=n();const l=this._selectionManagerService.getSelections(),g=s?this._findInSelections(c,l,e,i):this._findInWorksheet(c,e,i);return this._matches=g.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(a,this._matches),o?(r=g,o=!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===k.SetSelectionsOperation.id&&c.params.unitId===i){const a=n();return a===!1&&s===!1?!1:(s=a,!0)}return!1})),this._workbook.activeSheet$.pipe(R.skip(1))).pipe(R.debounceTime(200)).subscribe(()=>h())),h(),r}_findInRange(e,i,n,r,o){const s=[],h=e.getSheetId(),c=(i.findDirection===m.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const a of c){const{row:l,col:g,colSpan:C,rowSpan:p,value:I}=a;if(!this._getFindReplacePermissionCheck({row:l,col:g,unitId:r,subUnitId:h})||o!=null&&o(l,g)||!I||e.getRowFiltered(l))continue;const{hit:M,replaceable:w,isFormula:P}=ie(e,l,g,i,I);if(M){const T={provider:ee,unitId:r,replaceable:w,isFormula:P,range:{subUnitId:h,range:{startRow:l,startColumn:g,endColumn:g+(C!=null?C:1)-1,endRow:l+(p!=null?p:1)-1}}};s.push(T)}}return{results:s}}_findInSelections(e,i,n,r){const{findDirection:o}=n,s=o===m.FindDirection.ROW?j:N,h=new Set;return{results:i.map(a=>this._findInRange(e,n,a.range,r,(l,g)=>{const C=`${l}-${g}`;return h.has(C)?!0:(h.add(C),!1)}).results).flat().sort((a,l)=>s(a.range.range,l.range.range)?-1:1)}}_findInWorksheet(e,i,n){const r=e.getRowCount(),o=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:r-1,endColumn:o-1};return this._findInRange(e,i,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(i=>{i.getScene().makeDirty(),i.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var p,I;this._disposeHighlights();const e=(p=this._sheetSkeletonManagerService.getCurrent())==null?void 0:p.skeleton;if(!e||!this._getSheetObject()||((I=this._univerInstanceService.getFocusedUnit())==null?void 0:I.getUnitId())!==this._workbook.getUnitId())return;const r=this._workbook.getUnitId(),o=this._renderManagerService.getRenderById(r);if(o==null)return;const{scene:s}=o,h=this._matches,c=this._themeService.getCurrentTheme().gold400,a=new d.ColorKit(c).toRgb(),l=this._workbook.getActiveSheet(),g=l.getSheetId(),C=h.filter(y=>y.range.subUnitId===g).map((y,M)=>{const{startColumn:w,startRow:P,endColumn:T,endRow:ae}=y.range.range,le=b.getCoordByCell(P,w,s,e),de=b.getCoordByCell(ae,T,s,e),{startX:L,startY:G}=le,{endX:ue,endY:ge}=de,q=!l.getRowRawVisible(P),Q=!l.getColVisible(w),_e=q||Q,fe=Q?2:ue-L,Se=q?2:ge-G,me={left:L,top:G,color:a,width:fe,height:Se,evented:!1,inHiddenRange:_e,zIndex:te};return new X(`find-highlight-${M}`,me)});s.addObjects(C),this._highlightShapes=C,s.makeDirty()}_updateCurrentHighlightShape(e){var i;if((i=this._currentHighlightShape)==null||i.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 b.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const i=e.range.subUnitId;i!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(k.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:i},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(b.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,i){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:r,startRow:o}=e.range.range,s=i.findIndex(h=>{if(n!==h.range.subUnitId)return!1;const{startColumn:c,startRow:a}=h.range.range;return c===r&&a===o});return s>-1?s+1:0}moveToNextMatch(e){var s,h,c;if(!this._matches.length)return null;const i=(s=e==null?void 0:e.loop)!=null?s:!1,n=(h=e==null?void 0:e.stayIfOnMatch)!=null?h:!1,r=(c=e==null?void 0:e.noFocus)!=null?c:!1,o=this._findNextMatch(i,n);if(o){const[a,l]=o;return this._matchesPosition=l+1,this._query.findScope===m.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(g=>g===a):this._activeHighlightIndex=l,r||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,h,c;if(!this._matches.length)return null;const i=(s=e==null?void 0:e.loop)!=null?s:!1,n=(h=e==null?void 0:e.stayIfOnMatch)!=null?h:!1,r=(c=e==null?void 0:e.noFocus)!=null?c:!1,o=this._findPreviousMatch(i,n);if(o){const[a,l]=o;return this._matchesPosition=l+1,this._query.findScope===m.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(g=>g===a):this._activeHighlightIndex=l,r||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,i=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(i)return[this.currentMatch,s];const h=s-1;if(!e&&h<0)return null;const c=this._matches.length,a=(h+c)%c;return[this._matches[a],a]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==m.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const r=this._workbook.getActiveSheet().getSheetId(),o=this._findPreviousWorksheetThatHasAMatch(r,e);return o?this._findPreviousMatchByRange(this._matchesByWorksheet.get(o),n[0].range):null}_findNextMatch(e=!1,i=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(i)return[this.currentMatch,s];const h=s+1,c=this._matches.length;if(!e&&h>=c)return null;const a=h%c;return[this._matches[a],a]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==m.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,i);const r=this._workbook.getActiveSheet().getSheetId(),o=this._findNextWorksheetThatHasAMatch(r,e);return o?this._findNextMatchByRange(this._matchesByWorksheet.get(o),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,i=!1){const n=this._workbook.getSheetOrders(),r=n.findIndex(h=>h===e),s=(i?d.rotate(n,r+1):n.slice(0,r+1)).findLast(h=>this._matchesByWorksheet.has(h));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,i=!1){const n=this._workbook.getSheetOrders(),r=n.findIndex(h=>h===e),s=(i?d.rotate(n,r):n.slice(r)).find(h=>this._matchesByWorksheet.has(h));return s!=null?s:null}_findNextMatchByRange(e,i,n=!1){const r=this._query.findDirection===m.FindDirection.ROW;let o=e.findIndex(h=>{const c=h.range.range;if(!(r?j(i,c):N(i,c)))return!1;const l=A(i,c);return n?l:!l});o===-1&&(o=e.length-1);const s=e[o];return[s,this._matches.findIndex(h=>h===s)]}_findPreviousMatchByRange(e,i,n=!1){const r=this._query.findDirection===m.FindDirection.ROW;let o=this._matches.findLastIndex(h=>{const c=h.range.range;if(!(r?Z(i,c):K(i,c)))return!1;const l=A(i,c);return n?l:!l});o===-1&&(o=0);const s=e[o];return[s,this._matches.findIndex(h=>h===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const i=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),r=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===m.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),o={unitId:this.currentMatch.unitId,subUnitId:i.subUnitId,value:{[i.range.startRow]:{[i.range.startColumn]:r}}};return this._commandService.executeCommand(k.SetRangeValuesCommand.id,o)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const i=this._workbook.getUnitId(),{findString:n,caseSensitive:r,findBy:o}=this._query,s=o===m.FindBy.FORMULA,h=r?"g":"ig",c=[];return d.groupBy(this._matches.filter(l=>l.replaceable),l=>l.range.subUnitId).forEach((l,g)=>{const C=new d.ObjectMatrix,p=this._workbook.getSheetBySheetId(g);l.forEach(I=>{const{startColumn:y,startRow:M}=I.range.range,w=this._getReplacedCellData(I,p,s,n,e,h);w&&C.setValue(M,y,w)}),c.push({count:l.length,subUnitId:g,value:C.getMatrix()})}),c?this._commandService.executeCommand(D.id,{unitId:i,replacements:c}):{success:0,failure:0}}_getReplacedCellData(e,i,n,r,o,s){var p;const h=e.range.range,{startRow:c,startColumn:a}=h,l=i.getCellRaw(c,a);if(e.isFormula)return n?{f:l.f.replace(new RegExp($(r),s),o),v:null}:null;if(!!((p=l.p)!=null&&p.body)){const I=d.Tools.deepClone(l.p);return d.replaceInDocumentBody(I.body,r,o),{p:I}}return{v:l.v.toString().replace(new RegExp($(r),s),o)}}_getFindReplacePermissionCheck(e){var a,l;const{row:i,col:n,subUnitId:r}=e,s=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET).getSheetBySheetId(r);if(!s)return!1;const h=(l=(a=s.getCell(i,n))==null?void 0:a.selectionProtection)==null?void 0:l[0];return(h==null?void 0:h[O.View])!==!1}};W=x([v(1,d.IUniverInstanceService),v(2,F.IRenderManagerService),v(3,d.ICommandService),v(4,d.IContextService),v(5,S.Inject(d.ThemeService)),v(6,S.Inject(b.SheetSkeletonManagerService)),v(7,S.Inject(k.SelectionManagerService)),v(8,S.Inject(u.SheetsFindReplaceController))],W);function $(t){return t.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let H=class extends d.Disposable{constructor(e,i){super();f(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=i}async find(e){this._terminate();const i=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET);if(i){const n=this._injector.createInstance(W,i);this._findModelsByUnitId.set(i.getUnitId(),n);const r=this._preprocessQuery(e);return n.start(r),[n]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let i=e.caseSensitive?e.findString:e.findString.toLowerCase();return i=i.trim(),{...e,findString:i}}};H=x([v(0,d.IUniverInstanceService),v(1,S.Inject(S.Injector))],H);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ie(t,e,i,n,r){const{findBy:o}=n,s=o===m.FindBy.FORMULA,h=t.getCellRaw(e,i);return _.rawData=h,!(h!=null&&h.f)?(_.isFormula=!1,B(r,n)?h?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?B({v:h.f},n)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,B(r,n)?_.hit=!0:_.hit=!1,_))}function B(t,e){let i=ne(t);return i?e.matchesTheWholeCell?(i=se(i),e.caseSensitive?i===e.findString:i.toLowerCase()===e.findString):e.caseSensitive?i.indexOf(e.findString)>-1:i.toLowerCase().indexOf(e.findString)>-1:!1}function ne(t){var i,n,r;const e=(r=(n=(i=t==null?void 0:t.p)==null?void 0:i.body)==null?void 0:n.dataStream)!=null?r:t==null?void 0:t.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function se(t){return t.replace(/^ +/g,"").replace(/ +$/g,"")}var re=Object.defineProperty,oe=Object.getOwnPropertyDescriptor,he=(t,e,i,n)=>{for(var r=n>1?void 0:n?oe(e,i):e,o=t.length-1,s;o>=0;o--)(s=t[o])&&(r=(n?s(e,i,r):s(r))||r);return n&&r&&re(e,i,r),r},V=(t,e)=>(i,n)=>e(i,n,t);const ce="SHEET_FIND_REPLACE_PLUGIN";u.UniverSheetsFindReplacePlugin=(U=class extends d.Plugin{constructor(e,i,n){super(),this._injector=i,this._pluginService=n,this._pluginService.registerPlugin(m.UniverFindReplacePlugin)}onStarting(e){[[u.SheetsFindReplaceController]].forEach(i=>e.add(i))}},f(U,"pluginName",ce),f(U,"type",d.UniverInstanceType.UNIVER_SHEET),U),u.UniverSheetsFindReplacePlugin=he([V(1,S.Inject(S.Injector)),V(2,S.Inject(d.PluginService))],u.UniverSheetsFindReplacePlugin),Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
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._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"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/sheets-find-replace",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "private": false,
5
5
  "description": "UniverSheet find replace plugin",
6
6
  "author": "DreamNum <developer@univer.ai>",
@@ -31,8 +31,8 @@
31
31
  "require": "./lib/cjs/*",
32
32
  "types": "./lib/types/index.d.ts"
33
33
  },
34
- "./locale/*": "./lib/locale/*.json",
35
- "./lib/*": "./lib/*"
34
+ "./lib/*": "./lib/*",
35
+ "./locale/*": "./lib/locale/*.json"
36
36
  },
37
37
  "main": "./lib/cjs/index.js",
38
38
  "module": "./lib/es/index.js",
@@ -46,21 +46,17 @@
46
46
  "files": [
47
47
  "lib"
48
48
  ],
49
- "engines": {
50
- "node": ">=16.0.0",
51
- "npm": ">=8.0.0"
52
- },
53
49
  "peerDependencies": {
54
50
  "@wendellhu/redi": "0.15.2",
55
51
  "rxjs": ">=7.0.0",
56
- "@univerjs/core": "0.1.13",
57
- "@univerjs/find-replace": "0.1.13",
58
- "@univerjs/engine-render": "0.1.13",
59
- "@univerjs/sheets-ui": "0.1.13",
60
- "@univerjs/sheets": "0.1.13"
52
+ "@univerjs/core": "0.1.14",
53
+ "@univerjs/engine-render": "0.1.14",
54
+ "@univerjs/find-replace": "0.1.14",
55
+ "@univerjs/sheets": "0.1.14",
56
+ "@univerjs/sheets-ui": "0.1.14"
61
57
  },
62
58
  "dependencies": {
63
- "@univerjs/protocol": "0.1.34"
59
+ "@univerjs/protocol": "0.1.36"
64
60
  },
65
61
  "devDependencies": {
66
62
  "@wendellhu/redi": "0.15.2",
@@ -68,13 +64,27 @@
68
64
  "typescript": "^5.4.5",
69
65
  "vite": "^5.2.12",
70
66
  "vitest": "^1.6.0",
71
- "@univerjs/core": "0.1.13",
72
- "@univerjs/engine-render": "0.1.13",
73
- "@univerjs/shared": "0.1.13",
74
- "@univerjs/engine-formula": "0.1.13",
75
- "@univerjs/sheets": "0.1.13",
76
- "@univerjs/find-replace": "0.1.13",
77
- "@univerjs/sheets-ui": "0.1.13"
67
+ "@univerjs/core": "0.1.14",
68
+ "@univerjs/engine-formula": "0.1.14",
69
+ "@univerjs/engine-render": "0.1.14",
70
+ "@univerjs/shared": "0.1.14",
71
+ "@univerjs/sheets": "0.1.14",
72
+ "@univerjs/find-replace": "0.1.14",
73
+ "@univerjs/sheets-ui": "0.1.14"
74
+ },
75
+ "univerSpace": {
76
+ ".": {
77
+ "import": "./lib/es/index.js",
78
+ "require": "./lib/cjs/index.js",
79
+ "types": "./lib/types/index.d.ts"
80
+ },
81
+ "./*": {
82
+ "import": "./lib/es/*",
83
+ "require": "./lib/cjs/*",
84
+ "types": "./lib/types/index.d.ts"
85
+ },
86
+ "./lib/*": "./lib/*",
87
+ "./locale/*": "./lib/locale/*.json"
78
88
  },
79
89
  "scripts": {
80
90
  "test": "vitest run",