@univerjs/sheets-find-replace 0.4.2 → 0.5.0-alpha.0

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 __defProp=Object.defineProperty;var __defNormalProp=(obj,key,value)=>key in obj?__defProp(obj,key,{enumerable:!0,configurable:!0,writable:!0,value}):obj[key]=value;var __name=(target,value)=>__defProp(target,"name",{value,configurable:!0});var __publicField=(obj,key,value)=>__defNormalProp(obj,typeof key!="symbol"?key+"":key,value);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const core=require("@univerjs/core"),findReplace=require("@univerjs/find-replace"),sheets=require("@univerjs/sheets"),engineRender=require("@univerjs/engine-render"),sheetsUi=require("@univerjs/sheets-ui"),rxjs=require("rxjs"),PLUGIN_CONFIG_KEY="sheets-find-replace.config",defaultPluginConfig={},SheetReplaceCommand={id:"sheet.command.replace",type:core.CommandType.COMMAND,handler:__name(async(accessor,params)=>{const undoRedoService=accessor.get(core.IUndoRedoService),commandService=accessor.get(core.ICommandService),{unitId,replacements}=params,disposeBatchingHandler=undoRedoService.__tempBatchingUndoRedo(unitId),results=await Promise.all(replacements.map(replacement=>commandService.executeCommand(sheets.SetRangeValuesCommand.id,{unitId,subUnitId:replacement.subUnitId,value:replacement.value})));return disposeBatchingHandler.dispose(),getReplaceAllResult(results,replacements)},"handler")};function getReplaceAllResult(results,replacements){let success=0,failure=0;return results.forEach((r,index)=>{const count=replacements[index].count;r?success+=count:failure+=count}),{success,failure}}__name(getReplaceAllResult,"getReplaceAllResult");const _SheetFindReplaceHighlightShape=class _SheetFindReplaceHighlightShape extends engineRender.Shape{constructor(key,props){super(key,props);__publicField(this,"_activated",!1);__publicField(this,"_inHiddenRange",!1);__publicField(this,"_color");props&&this.setShapeProps(props)}setShapeProps(props){this._activated=!!props.activated,typeof props.inHiddenRange<"u"&&(this._inHiddenRange=props.inHiddenRange),typeof props.color<"u"&&(this._color=props.color),this.transformByState({width:props.width,height:props.height})}_draw(ctx){const activated=this._activated,color=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,borderColor=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;engineRender.Rect.drawWith(ctx,{width:this.width,height:this.height,fill:color,stroke:activated?borderColor:void 0,strokeWidth:activated?2:0,evented:!1})}};__name(_SheetFindReplaceHighlightShape,"SheetFindReplaceHighlightShape");let SheetFindReplaceHighlightShape=_SheetFindReplaceHighlightShape;function isSamePosition(range1,range2){return range1.startRow===range2.startRow&&range1.startColumn===range2.startColumn}__name(isSamePosition,"isSamePosition");function isBehindPositionWithRowPriority(range1,range2){return range1.startRow<range2.startRow||range1.startRow===range2.startRow&&range1.startColumn<=range2.startColumn}__name(isBehindPositionWithRowPriority,"isBehindPositionWithRowPriority");function isBehindPositionWithColumnPriority(range1,range2){return range1.startColumn<range2.startColumn||range1.startColumn===range2.startColumn&&range1.startRow<=range2.startRow}__name(isBehindPositionWithColumnPriority,"isBehindPositionWithColumnPriority");function isBeforePositionWithRowPriority(range1,range2){return range1.startRow>range2.startRow||range1.startRow===range2.startRow&&range1.startColumn>=range2.startColumn}__name(isBeforePositionWithRowPriority,"isBeforePositionWithRowPriority");function isBeforePositionWithColumnPriority(range1,range2){return range1.startColumn>range2.startColumn||range1.startColumn===range2.startColumn&&range1.startRow>=range2.startRow}__name(isBeforePositionWithColumnPriority,"isBeforePositionWithColumnPriority");function isSelectionSingleCell(selection,worksheet){const{range}=selection,{startRow,startColumn}=range,hasMergedCell=worksheet.getMergedCell(startRow,startColumn);return hasMergedCell?core.Rectangle.equals(range,hasMergedCell):range.endRow===range.startRow&&range.endColumn===range.startColumn}__name(isSelectionSingleCell,"isSelectionSingleCell");var __defProp$1=Object.defineProperty,__getOwnPropDesc$1=Object.getOwnPropertyDescriptor,__decorateClass$1=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc$1(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp$1(target,key,result),result},"__decorateClass$1"),__decorateParam$1=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam$1"),_a;exports.SheetsFindReplaceController=(_a=class extends core.Disposable{constructor(_injector,_findReplaceController,_contextService,_findReplaceService,_commandService){super();__publicField(this,"_provider");this._injector=_injector,this._findReplaceController=_findReplaceController,this._contextService=_contextService,this._findReplaceService=_findReplaceService,this._commandService=_commandService,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const provider=this._injector.createInstance(SheetsFindReplaceProvider);this._provider=provider,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(provider)),this.disposeWithMe(this._contextService.subscribeContextValue$(core.EDITOR_ACTIVATED).pipe(rxjs.filter(v=>!!v)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[SheetReplaceCommand].forEach(command=>this.disposeWithMe(this._commandService.registerCommand(command)))}},__name(_a,"SheetsFindReplaceController"),_a);exports.SheetsFindReplaceController=__decorateClass$1([__decorateParam$1(0,core.Inject(core.Injector)),__decorateParam$1(1,core.Inject(findReplace.FindReplaceController)),__decorateParam$1(2,core.IContextService),__decorateParam$1(3,findReplace.IFindReplaceService),__decorateParam$1(4,core.ICommandService)],exports.SheetsFindReplaceController);const SHEETS_FIND_REPLACE_PROVIDER_NAME="sheets-find-replace-provider",FIND_REPLACE_Z_INDEX=1e4;var _a2;let SheetFindModel=(_a2=class extends findReplace.FindModel{constructor(_workbook,_sheetSkeletonManagerService,_univerInstanceService,_renderManagerService,_commandService,_contextService,_themeService,_selectionManagerService){super();__publicField(this,"_matchesUpdate$",new rxjs.Subject);__publicField(this,"matchesUpdate$",this._matchesUpdate$.asObservable());__publicField(this,"_activelyChangingMatch$",new rxjs.Subject);__publicField(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());__publicField(this,"_matchesByWorksheet",new Map);__publicField(this,"_matches",[]);__publicField(this,"_matchesPosition",0);__publicField(this,"_activeHighlightIndex",-1);__publicField(this,"_highlightShapes",[]);__publicField(this,"_currentHighlightShape",null);__publicField(this,"_query",null);__publicField(this,"_workbookSelections");this._workbook=_workbook,this._sheetSkeletonManagerService=_sheetSkeletonManagerService,this._univerInstanceService=_univerInstanceService,this._renderManagerService=_renderManagerService,this._commandService=_commandService,this._contextService=_contextService,this._themeService=_themeService,this._workbookSelections=_selectionManagerService.getWorkbookSelections(this.unitId)}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(query){switch(this._query=query,query.findBy===findReplace.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),query.findScope){case findReplace.FindScope.UNIT:this.findInWorkbook(query);break;case findReplace.FindScope.SUBUNIT:default:this.findInActiveWorksheet(query);break}}_toggleDisplayRawFormula(force){this._contextService.setContextValue(engineRender.RENDER_RAW_FORMULA_KEY,force)}findInWorkbook(query){const unitId=this._workbook.getUnitId();let complete,firstSearch=!0;const findInWorkbook=__name(()=>{const allCompletes=this._workbook.getSheets().filter(worksheet=>!worksheet.isSheetHidden()).map(worksheet=>{const complete2=this._findInWorksheet(worksheet,query,unitId),sheetId=worksheet.getSheetId(),{results}=complete2;return results.length?this._matchesByWorksheet.set(sheetId,complete2.results):this._matchesByWorksheet.delete(sheetId),complete2});this._matches=allCompletes.map(c=>c.results).flat(),this._updateFindHighlight(),firstSearch?(complete={results:this._matches},firstSearch=!1):this._matchesUpdate$.next(this._matches)},"findInWorkbook");return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(core.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(rxjs.filter(([command,options])=>command.id===sheets.SetWorksheetActiveOperation.id&&!(options!=null&&options.fromFindReplace))).subscribe(()=>{const activeSheet=this._workbook.getActiveSheet();if(!activeSheet)return;const activeSheetId=activeSheet.getSheetId();this._matchesByWorksheet.has(activeSheetId)&&this._findNextMatchOnActiveSheetChange(activeSheet)})),this.disposeWithMe(core.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(rxjs.filter(([command])=>command.type===core.CommandType.MUTATION&&command.params.unitId===this._workbook.getUnitId()),rxjs.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>findInWorkbook())),findInWorkbook(),complete}_findNextMatchOnActiveSheetChange(activeSheet){let match,index,globalIndex=0;const matchesByWorksheet=this._matchesByWorksheet.get(activeSheet.getSheetId()),selections=this._workbookSelections.getCurrentSelections();selections!=null&&selections.length?([match,globalIndex]=this._findNextMatchByRange(matchesByWorksheet,selections[0].range),index=matchesByWorksheet.findIndex(m=>m===match)):(match=matchesByWorksheet[0],index=0,globalIndex=this._matches.findIndex(m=>m===match)),this._matchesPosition=globalIndex+1,this._activelyChangingMatch$.next(match),this._activeHighlightIndex=index,this._updateFindHighlight(),this._updateCurrentHighlightShape(index)}findInActiveWorksheet(query){const unitId=this._workbook.getUnitId(),checkShouldFindInSelections=__name(()=>{var _a5;const currentWorksheet=this._workbook.getActiveSheet();if(!currentWorksheet)return!1;const currentSelections=this._workbookSelections.getCurrentSelections();return(_a5=currentSelections==null?void 0:currentSelections.some(selection=>!isSelectionSingleCell(selection,currentWorksheet)))!=null?_a5:!1},"checkShouldFindInSelections");let complete,firstSearch=!0,findBySelections=!1;const performFindInWorksheet=__name(()=>{const currentWorksheet=this._workbook.getActiveSheet();if(!currentWorksheet)return{results:[]};const lastMatch=this.currentMatch;findBySelections=checkShouldFindInSelections();const currentSelections=this._workbookSelections.getCurrentSelections(),newComplete=findBySelections?this._findInSelections(currentWorksheet,currentSelections,query,unitId):this._findInWorksheet(currentWorksheet,query,unitId);return this._matches=newComplete.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(lastMatch,this._matches),firstSearch?(complete=newComplete,firstSearch=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),newComplete},"performFindInWorksheet");return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(rxjs.merge(core.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(rxjs.filter(([command])=>{if(command.type===core.CommandType.MUTATION&&command.params.unitId===this._workbook.getUnitId())return!0;if(command.id===sheets.SetSelectionsOperation.id&&command.params.unitId===unitId){const shouldFindBySelections=checkShouldFindInSelections();return shouldFindBySelections===!1&&findBySelections===!1?!1:(findBySelections=shouldFindBySelections,!0)}return!1})),this._workbook.activeSheet$.pipe(rxjs.skip(1))).pipe(rxjs.debounceTime(200)).subscribe(()=>performFindInWorksheet())),performFindInWorksheet(),complete}_findInRange(worksheet,query,range,unitId,dedupeFn){const results=[],subUnitId=worksheet.getSheetId(),iter=(query.findDirection===findReplace.FindDirection.COLUMN?worksheet.iterateByColumn:worksheet.iterateByRow).bind(worksheet)(range);for(const value of iter){const{row,col,colSpan,rowSpan,value:cellData}=value;if(dedupeFn!=null&&dedupeFn(row,col)||!cellData||worksheet.getRowFiltered(row))continue;const{hit,replaceable,isFormula}=hitCell(worksheet,row,col,query,cellData);if(hit){const result={provider:SHEETS_FIND_REPLACE_PROVIDER_NAME,unitId,replaceable,isFormula,range:{subUnitId,range:{startRow:row,startColumn:col,endColumn:col+(colSpan!=null?colSpan:1)-1,endRow:row+(rowSpan!=null?rowSpan:1)-1}}};results.push(result)}}return{results}}_findInSelections(worksheet,selections,query,unitId){const{findDirection}=query,sortFn=findDirection===findReplace.FindDirection.ROW?isBehindPositionWithRowPriority:isBehindPositionWithColumnPriority,dedupeSet=new Set;return{results:selections.map(selection=>this._findInRange(worksheet,query,selection.range,unitId,(row,col)=>{const key=`${row}-${col}`;return dedupeSet.has(key)?!0:(dedupeSet.add(key),!1)}).results).flat().sort((a,b)=>sortFn(a.range.range,b.range.range)?-1:1)}}_findInWorksheet(worksheet,query,unitId){const rowCount=worksheet.getRowCount(),colCount=worksheet.getColumnCount(),range={startRow:0,startColumn:0,endRow:rowCount-1,endColumn:colCount-1};return this._findInRange(worksheet,query,range,unitId)}_disposeHighlights(){var _a5;this._highlightShapes.forEach(shape=>{var _a6;(_a6=shape.getScene())==null||_a6.makeDirty(),shape.dispose()}),this._highlightShapes=[],(_a5=this._currentHighlightShape)==null||_a5.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var _a5;this._disposeHighlights();const skeleton=(_a5=this._sheetSkeletonManagerService.getCurrent())==null?void 0:_a5.skeleton;if(!skeleton)return;const unitId=this._workbook.getUnitId(),currentRender=this._renderManagerService.getRenderById(unitId);if(currentRender==null)return;const{scene}=currentRender,matches=this._matches,searchBackgroundColor=this._themeService.getCurrentTheme().gold400,color=new core.ColorKit(searchBackgroundColor).toRgb(),worksheet=this._workbook.getActiveSheet();if(!worksheet)return;const activeSheetId=worksheet.getSheetId(),highlightShapes=matches.filter(match=>match.range.subUnitId===activeSheetId).map((find,index)=>{const{startColumn,startRow,endColumn,endRow}=find.range.range,startPosition=sheetsUi.getCoordByCell(startRow,startColumn,scene,skeleton),endPosition=sheetsUi.getCoordByCell(endRow,endColumn,scene,skeleton),{startX,startY}=startPosition,{endX,endY}=endPosition,rowHidden=!worksheet.getRowRawVisible(startRow),columnHidden=!worksheet.getColVisible(startColumn),inHiddenRange=rowHidden||columnHidden,width=columnHidden?2:endX-startX,height=rowHidden?2:endY-startY,props={left:startX,top:startY,color,width,height,evented:!1,inHiddenRange,zIndex:FIND_REPLACE_Z_INDEX};return new SheetFindReplaceHighlightShape(`find-highlight-${index}`,props)});scene.addObjects(highlightShapes),this._highlightShapes=highlightShapes,scene.makeDirty()}_updateCurrentHighlightShape(matchIndex){var _a5;if((_a5=this._currentHighlightShape)==null||_a5.setShapeProps({activated:!1}),this._currentHighlightShape=null,matchIndex!==void 0){const shape=this._highlightShapes[matchIndex];if(!shape)return;this._currentHighlightShape=shape,shape.setShapeProps({activated:!0})}}_getSheetObject(){return sheetsUi.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(match){var _a5;const subUnitId=match.range.subUnitId;subUnitId!==((_a5=this._workbook.getActiveSheet())==null?void 0:_a5.getSheetId())&&this._commandService.executeCommand(sheets.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId},{fromFindReplace:!0}),this._commandService.executeCommand(sheetsUi.ScrollToCellCommand.id,{range:match.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(lastMatch,newMatches){if(!lastMatch)return 0;const{subUnitId:lastSubUnitId}=lastMatch.range,{startColumn:lastStartColumn,startRow:lastStartRow}=lastMatch.range.range,index=newMatches.findIndex(match=>{if(lastSubUnitId!==match.range.subUnitId)return!1;const{startColumn,startRow}=match.range.range;return startColumn===lastStartColumn&&startRow===lastStartRow});return index>-1?index+1:0}moveToNextMatch(params){var _a5,_b,_c,_d,_e;if(!this._matches.length)return null;const loop=(_a5=params==null?void 0:params.loop)!=null?_a5:!1,stayIfOnMatch=(_b=params==null?void 0:params.stayIfOnMatch)!=null?_b:!1,noFocus=(_c=params==null?void 0:params.noFocus)!=null?_c:!1,ignoreSelection=(_d=params==null?void 0:params.ignoreSelection)!=null?_d:!1,matchToMove=this._findNextMatch(loop,stayIfOnMatch,ignoreSelection);if(matchToMove){const[match,index]=matchToMove;return this._matchesPosition=index+1,this._query.findScope===findReplace.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(match.range.subUnitId).findIndex(m=>m===match):this._activeHighlightIndex=index,noFocus||this._focusMatch(match),((_e=this._workbook.getActiveSheet())==null?void 0:_e.getSheetId())===match.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),match}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(params){var _a5,_b,_c,_d,_e;if(!this._matches.length)return null;const loop=(_a5=params==null?void 0:params.loop)!=null?_a5:!1,stayIfOnMatch=(_b=params==null?void 0:params.stayIfOnMatch)!=null?_b:!1,noFocus=(_c=params==null?void 0:params.noFocus)!=null?_c:!1,ignoreSelection=(_d=params==null?void 0:params.ignoreSelection)!=null?_d:!1,matchToMove=this._findPreviousMatch(loop,stayIfOnMatch,ignoreSelection);if(matchToMove){const[match,index]=matchToMove;return this._matchesPosition=index+1,this._query.findScope===findReplace.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(match.range.subUnitId).findIndex(m=>m===match):this._activeHighlightIndex=index,noFocus||this._focusMatch(match),((_e=this._workbook.getActiveSheet())==null?void 0:_e.getSheetId())===match.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),match}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(loop=!1,stayIfOnMatch=!1,ignoreSelection=!1){var _a5;if(this.currentMatch){const currentMatchIndex=this._matches.findIndex(match=>match===this.currentMatch);if(stayIfOnMatch)return[this.currentMatch,currentMatchIndex];const nextMatchIndex=currentMatchIndex-1;if(!loop&&nextMatchIndex<0)return null;const length=this._matches.length,modded=(nextMatchIndex+length)%length;return[this._matches[modded],modded]}const lastSelection=this._workbookSelections.getCurrentLastSelection();if(ignoreSelection||!lastSelection){const lastIndex=this._matches.length-1;return[this._matches[lastIndex],lastIndex]}if(this._query.findScope!==findReplace.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,lastSelection.range);const currentSheetId=(_a5=this._workbook.getActiveSheet())==null?void 0:_a5.getSheetId();if(!currentSheetId)return null;const worksheetThatHasMatch=this._findPreviousWorksheetThatHasAMatch(currentSheetId,loop);return worksheetThatHasMatch?this._findPreviousMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch),lastSelection.range):null}_findNextMatch(loop=!1,stayIfOnMatch=!1,ignoreSelection=!1){var _a5;if(this.currentMatch){const currentMatchIndex=this._matches.findIndex(match=>match===this.currentMatch);if(stayIfOnMatch)return[this.currentMatch,currentMatchIndex];const nextMatchIndex=currentMatchIndex+1,length=this._matches.length;if(!loop&&nextMatchIndex>=length)return null;const modded=nextMatchIndex%length;return[this._matches[modded],modded]}const last=this._workbookSelections.getCurrentLastSelection();if(ignoreSelection||!last)return[this._matches[0],0];if(this._query.findScope!==findReplace.FindScope.UNIT)return this._findNextMatchByRange(this._matches,last.range,stayIfOnMatch);const currentSheetId=(_a5=this._workbook.getActiveSheet())==null?void 0:_a5.getSheetId();if(!currentSheetId)return null;const worksheetThatHasMatch=this._findNextWorksheetThatHasAMatch(currentSheetId,loop);return worksheetThatHasMatch?this._findNextMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch),last.range):null}_findPreviousWorksheetThatHasAMatch(currentWorksheet,loop=!1){const rawWorksheetsInOrder=this._workbook.getSheetOrders(),currentSheetIndex=rawWorksheetsInOrder.findIndex(sheet=>sheet===currentWorksheet),first=(loop?core.rotate(rawWorksheetsInOrder,currentSheetIndex+1):rawWorksheetsInOrder.slice(0,currentSheetIndex+1)).findLast(worksheet=>this._matchesByWorksheet.has(worksheet));return first!=null?first:null}_findNextWorksheetThatHasAMatch(currentWorksheet,loop=!1){const rawWorksheetsInOrder=this._workbook.getSheetOrders(),currentSheetIndex=rawWorksheetsInOrder.findIndex(sheet=>sheet===currentWorksheet),first=(loop?core.rotate(rawWorksheetsInOrder,currentSheetIndex):rawWorksheetsInOrder.slice(currentSheetIndex)).find(worksheet=>this._matchesByWorksheet.has(worksheet));return first!=null?first:null}_findNextMatchByRange(matches,range,stayIfOnMatch=!1){const findByRow=this._query.findDirection===findReplace.FindDirection.ROW;let index=matches.findIndex(match2=>{const matchRange=match2.range.range;if(!(findByRow?isBehindPositionWithRowPriority(range,matchRange):isBehindPositionWithColumnPriority(range,matchRange)))return!1;const isSame=isSamePosition(range,matchRange);return stayIfOnMatch?isSame:!isSame});index===-1&&(index=matches.length-1);const match=matches[index];return[match,this._matches.findIndex(m=>m===match)]}_findPreviousMatchByRange(matches,range,stayIfOnMatch=!1){const findByRow=this._query.findDirection===findReplace.FindDirection.ROW;let index=this._matches.findLastIndex(match2=>{const matchRange=match2.range.range;if(!(findByRow?isBeforePositionWithRowPriority(range,matchRange):isBeforePositionWithColumnPriority(range,matchRange)))return!1;const isSame=isSamePosition(range,matchRange);return stayIfOnMatch?isSame:!isSame});index===-1&&(index=0);const match=matches[index];return[match,this._matches.findIndex(m=>m===match)]}async replace(replaceString){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const range=this.currentMatch.range,targetWorksheet=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),newContent=this._getReplacedCellData(this.currentMatch,targetWorksheet,this._query.findBy===findReplace.FindBy.FORMULA,this._query.findString,replaceString,this._query.caseSensitive?"g":"ig"),params={unitId:this.currentMatch.unitId,subUnitId:range.subUnitId,value:{[range.range.startRow]:{[range.range.startColumn]:newContent}}};return this._commandService.executeCommand(sheets.SetRangeValuesCommand.id,params)}async replaceAll(replaceString){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const unitId=this._workbook.getUnitId(),{findString,caseSensitive,findBy}=this._query,shouldReplaceFormula=findBy===findReplace.FindBy.FORMULA,replaceFlag=caseSensitive?"g":"ig",replacements=[];return core.groupBy(this._matches.filter(m=>m.replaceable),match=>match.range.subUnitId).forEach((matches,subUnitId)=>{const matrix=new core.ObjectMatrix,worksheet=this._workbook.getSheetBySheetId(subUnitId);matches.forEach(match=>{const{startColumn,startRow}=match.range.range,newCellData=this._getReplacedCellData(match,worksheet,shouldReplaceFormula,findString,replaceString,replaceFlag);newCellData&&matrix.setValue(startRow,startColumn,newCellData)}),replacements.push({count:matches.length,subUnitId,value:matrix.getMatrix()})}),replacements?this._commandService.executeCommand(SheetReplaceCommand.id,{unitId,replacements}):{success:0,failure:0}}_getReplacedCellData(match,worksheet,shouldReplaceFormula,findString,replaceString,replaceFlag){var _a5;const range=match.range.range,{startRow,startColumn}=range,currentContent=worksheet.getCellRaw(startRow,startColumn);if(match.isFormula)return shouldReplaceFormula?{f:currentContent.f.replace(new RegExp(escapeRegExp(findString),replaceFlag),replaceString),v:null}:null;if(!!((_a5=currentContent.p)!=null&&_a5.body)){const clonedRichText=core.Tools.deepClone(currentContent.p);return core.replaceInDocumentBody(clonedRichText.body,findString,replaceString),{p:clonedRichText}}return{v:currentContent.v.toString().replace(new RegExp(escapeRegExp(findString),replaceFlag),replaceString)}}},__name(_a2,"SheetFindModel"),_a2);SheetFindModel=__decorateClass$1([__decorateParam$1(2,core.IUniverInstanceService),__decorateParam$1(3,engineRender.IRenderManagerService),__decorateParam$1(4,core.ICommandService),__decorateParam$1(5,core.IContextService),__decorateParam$1(6,core.Inject(core.ThemeService)),__decorateParam$1(7,core.Inject(sheets.SheetsSelectionsService))],SheetFindModel);function escapeRegExp(text){return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}__name(escapeRegExp,"escapeRegExp");var _a3;let SheetsFindReplaceProvider=(_a3=class extends core.Disposable{constructor(_univerInstanceService,_renderManagerService,_injector){super();__publicField(this,"_findModelsByUnitId",new Map);this._univerInstanceService=_univerInstanceService,this._renderManagerService=_renderManagerService,this._injector=_injector}async find(query){this._terminate();const allWorkbooks=this._univerInstanceService.getAllUnitsForType(core.UniverInstanceType.UNIVER_SHEET),parsedQuery=this._preprocessQuery(query);return allWorkbooks.map(workbook=>{const skeletonManagerService=this._renderManagerService.getRenderById(workbook.getUnitId()).with(sheetsUi.SheetSkeletonManagerService),sheetFind=this._injector.createInstance(SheetFindModel,workbook,skeletonManagerService);return this._findModelsByUnitId.set(workbook.getUnitId(),sheetFind),sheetFind.start(parsedQuery),sheetFind})}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(model=>model.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(query){let findString=query.caseSensitive?query.findString:query.findString.toLowerCase();return findString=findString.trim(),{...query,findString}}},__name(_a3,"SheetsFindReplaceProvider"),_a3);SheetsFindReplaceProvider=__decorateClass$1([__decorateParam$1(0,core.IUniverInstanceService),__decorateParam$1(1,engineRender.IRenderManagerService),__decorateParam$1(2,core.Inject(core.Injector))],SheetsFindReplaceProvider);const VALUE_PASSING_OBJECT={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function hitCell(worksheet,row,col,query,cellData){const{findBy}=query,findByFormula=findBy===findReplace.FindBy.FORMULA,rawData=worksheet.getCellRaw(row,col);return VALUE_PASSING_OBJECT.rawData=rawData,!(rawData!=null&&rawData.f)?(VALUE_PASSING_OBJECT.isFormula=!1,matchCellData(cellData,query)?rawData?(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!0):(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!1):(VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT.replaceable=!1),VALUE_PASSING_OBJECT):(VALUE_PASSING_OBJECT.isFormula=!0,findByFormula?matchCellData({v:rawData.f},query)?(VALUE_PASSING_OBJECT.hit=!0,VALUE_PASSING_OBJECT.replaceable=!0,VALUE_PASSING_OBJECT):(VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT.replaceable=!1,VALUE_PASSING_OBJECT):(VALUE_PASSING_OBJECT.replaceable=!1,matchCellData(cellData,query)?VALUE_PASSING_OBJECT.hit=!0:VALUE_PASSING_OBJECT.hit=!1,VALUE_PASSING_OBJECT))}__name(hitCell,"hitCell");function matchCellData(cellData,query){let value=extractPureValue(cellData);return value?query.matchesTheWholeCell?(value=trimLeadingTrailingWhitespace(value),query.caseSensitive?value===query.findString:value.toLowerCase()===query.findString):query.caseSensitive?value.indexOf(query.findString)>-1:value.toLowerCase().indexOf(query.findString)>-1:!1}__name(matchCellData,"matchCellData");function extractPureValue(cell){var _a5,_b,_c;const rawValue=(_c=(_b=(_a5=cell==null?void 0:cell.p)==null?void 0:_a5.body)==null?void 0:_b.dataStream)!=null?_c:cell==null?void 0:cell.v;return typeof rawValue=="number"?`${rawValue}`:typeof rawValue=="boolean"?rawValue?"1":"0":rawValue}__name(extractPureValue,"extractPureValue");function trimLeadingTrailingWhitespace(value){return value.replace(/^ +/g,"").replace(/ +$/g,"")}__name(trimLeadingTrailingWhitespace,"trimLeadingTrailingWhitespace");var __defProp2=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__defNormalProp2=__name((obj,key,value)=>key in obj?__defProp2(obj,key,{enumerable:!0,configurable:!0,writable:!0,value}):obj[key]=value,"__defNormalProp"),__decorateClass=__name((decorators,target,key,kind)=>{for(var result=kind>1?void 0:kind?__getOwnPropDesc(target,key):target,i=decorators.length-1,decorator;i>=0;i--)(decorator=decorators[i])&&(result=(kind?decorator(target,key,result):decorator(result))||result);return kind&&result&&__defProp2(target,key,result),result},"__decorateClass"),__decorateParam=__name((index,decorator)=>(target,key)=>decorator(target,key,index),"__decorateParam"),__publicField2=__name((obj,key,value)=>__defNormalProp2(obj,typeof key!="symbol"?key+"":key,value),"__publicField");const NAME="SHEET_FIND_REPLACE_PLUGIN";var _a4;exports.UniverSheetsFindReplacePlugin=(_a4=class extends core.Plugin{constructor(_config=defaultPluginConfig,_injector,_configService){super(),this._config=_config,this._injector=_injector,this._configService=_configService;const{...rest}=this._config;this._configService.setConfig(PLUGIN_CONFIG_KEY,rest)}onStarting(){[[exports.SheetsFindReplaceController]].forEach(d=>this._injector.add(d))}onSteady(){this._injector.get(exports.SheetsFindReplaceController)}},__name(_a4,"UniverSheetsFindReplacePlugin"),_a4);__publicField2(exports.UniverSheetsFindReplacePlugin,"pluginName",NAME);__publicField2(exports.UniverSheetsFindReplacePlugin,"type",core.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=__decorateClass([core.DependentOn(sheets.UniverSheetsPlugin,sheets.UniverSheetsPlugin,findReplace.UniverFindReplacePlugin),__decorateParam(1,core.Inject(core.Injector)),__decorateParam(2,core.IConfigService)],exports.UniverSheetsFindReplacePlugin);exports.SheetReplaceCommand=SheetReplaceCommand;
1
+ "use strict";var z=Object.defineProperty;var J=(c,e,t)=>e in c?z(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var _=(c,e,t)=>J(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@univerjs/core"),S=require("@univerjs/find-replace"),C=require("@univerjs/sheets"),M=require("@univerjs/engine-render"),k=require("@univerjs/sheets-ui"),I=require("rxjs"),Z="sheets-find-replace.config",ee={},U={id:"sheet.command.replace",type:l.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(l.IUndoRedoService),i=c.get(l.ICommandService),{unitId:n,replacements:r}=e,s=t.__tempBatchingUndoRedo(n),o=await Promise.all(r.map(h=>i.executeCommand(C.SetRangeValuesCommand.id,{unitId:n,subUnitId:h.subUnitId,value:h.value})));return s.dispose(),te(o,r)}};function te(c,e){let t=0,i=0;return c.forEach((n,r)=>{const s=e[r].count;n?t+=s:i+=s}),{success:t,failure:i}}class ie extends M.Shape{constructor(t,i){super(t,i);_(this,"_activated",!1);_(this,"_inHiddenRange",!1);_(this,"_color");i&&this.setShapeProps(i)}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 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})`;M.Rect.drawWith(t,{width:this.width,height:this.height,fill:n,stroke:i?r:void 0,strokeWidth:i?2:0,evented:!1})}}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 ne(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function se(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function re(c,e){const{range:t}=c,{startRow:i,startColumn:n}=t,r=e.getMergedCell(i,n);return r?l.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var oe=Object.defineProperty,ce=Object.getOwnPropertyDescriptor,B=(c,e,t,i)=>{for(var n=i>1?void 0:i?ce(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&oe(e,t,n),n},v=(c,e)=>(t,i)=>e(t,i,c);exports.SheetsFindReplaceController=class extends l.Disposable{constructor(t,i,n,r,s){super();_(this,"_provider");this._injector=t,this._findReplaceController=i,this._contextService=n,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(P);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(l.EDITOR_ACTIVATED).pipe(I.filter(i=>!!i)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[U].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}};exports.SheetsFindReplaceController=B([v(0,l.Inject(l.Injector)),v(1,l.Inject(S.FindReplaceController)),v(2,l.IContextService),v(3,S.IFindReplaceService),v(4,l.ICommandService)],exports.SheetsFindReplaceController);const he="sheets-find-replace-provider",ae=1e4;let F=class extends S.FindModel{constructor(e,t,i,n,r,s,o,h){super();_(this,"_matchesUpdate$",new I.Subject);_(this,"matchesUpdate$",this._matchesUpdate$.asObservable());_(this,"_activelyChangingMatch$",new I.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,"_workbookSelections");this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=i,this._renderManagerService=n,this._commandService=r,this._contextService=s,this._themeService=o,this._workbookSelections=h.getWorkbookSelections(this.unitId)}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===S.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case S.FindScope.UNIT:this.findInWorkbook(e);break;case S.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(M.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let i,n=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const h=this._findInWorksheet(o,e,t),d=o.getSheetId(),{results:a}=h;return a.length?this._matchesByWorksheet.set(d,h.results):this._matchesByWorksheet.delete(d),h});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),n?(i={results:this._matches},n=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(I.filter(([s,o])=>s.id===C.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(I.filter(([s])=>s.type===l.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),I.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),i}_findNextMatchOnActiveSheetChange(e){let t,i,n=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._workbookSelections.getCurrentSelections();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)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),i=()=>{var u;const h=this._workbook.getActiveSheet();if(!h)return!1;const d=this._workbookSelections.getCurrentSelections();return(u=d==null?void 0:d.some(g=>!re(g,h)))!=null?u:!1};let n,r=!0,s=!1;const o=()=>{const h=this._workbook.getActiveSheet();if(!h)return{results:[]};const d=this.currentMatch;s=i();const a=this._workbookSelections.getCurrentSelections(),u=s?this._findInSelections(h,a,e,t):this._findInWorksheet(h,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(d,this._matches),r?(n=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(I.merge(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(I.filter(([h])=>{if(h.type===l.CommandType.MUTATION&&h.params.unitId===this._workbook.getUnitId())return!0;if(h.id===C.SetSelectionsOperation.id&&h.params.unitId===t){const d=i();return d===!1&&s===!1?!1:(s=d,!0)}return!1})),this._workbook.activeSheet$.pipe(I.skip(1))).pipe(I.debounceTime(200)).subscribe(()=>o())),o(),n}_findInRange(e,t,i,n,r){const s=[],o=e.getSheetId(),h=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(const d of h){const{row:a,col:u,colSpan:g,rowSpan:m,value:p}=d;if(r!=null&&r(a,u)||!p||e.getRowFiltered(a))continue;const{hit:R,replaceable:w,isFormula:b}=le(e,a,u,t,p);if(R){const y={provider:he,unitId:n,replaceable:w,isFormula:b,range:{subUnitId:o,range:{startRow:a,startColumn:u,endColumn:u+(g!=null?g:1)-1,endRow:a+(m!=null?m:1)-1}}};s.push(y)}}return{results:s}}_findInSelections(e,t,i,n){const{findDirection:r}=i,s=r===S.FindDirection.ROW?A:D,o=new Set;return{results:t.map(d=>this._findInRange(e,i,d.range,n,(a,u)=>{const g=`${a}-${u}`;return o.has(g)?!0:(o.add(g),!1)}).results).flat().sort((d,a)=>s(d.range.range,a.range.range)?-1:1)}}_findInWorksheet(e,t,i){const n=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:n-1,endColumn:r-1};return this._findInRange(e,t,s,i)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{var i;(i=t.getScene())==null||i.makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var u;this._disposeHighlights();const e=(u=this._sheetSkeletonManagerService.getCurrent())==null?void 0:u.skeleton;if(!e)return;const t=this._workbook.getUnitId(),i=this._renderManagerService.getRenderById(t);if(i==null)return;const{scene:n}=i,r=this._matches,s=this._themeService.getCurrentTheme().gold400,o=new l.ColorKit(s).toRgb(),h=this._workbook.getActiveSheet();if(!h)return;const d=h.getSheetId(),a=r.filter(g=>g.range.subUnitId===d).map((g,m)=>{const{startColumn:p,startRow:R,endColumn:w,endRow:b}=g.range.range,y=k.getCoordByCell(R,p,n,e),V=k.getCoordByCell(b,w,n,e),{startX:W,startY:H}=y,{endX:G,endY:Y}=V,T=!h.getRowRawVisible(R),O=!h.getColVisible(p),q=T||O,K=O?2:G-W,Q=T?2:Y-H,X={left:W,top:H,color:o,width:K,height:Q,evented:!1,inHiddenRange:q,zIndex:ae};return new ie(`find-highlight-${m}`,X)});n.addObjects(a),this._highlightShapes=a,n.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const i=this._highlightShapes[e];if(!i)return;this._currentHighlightShape=i,i.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var i;const t=e.range.subUnitId;t!==((i=this._workbook.getActiveSheet())==null?void 0:i.getSheetId())&&this._commandService.executeCommand(C.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.executeCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:i}=e.range,{startColumn:n,startRow:r}=e.range.range,s=t.findIndex(o=>{if(i!==o.range.subUnitId)return!1;const{startColumn:h,startRow:d}=o.range.range;return h===n&&d===r});return s>-1?s+1:0}moveToNextMatch(e){var o,h,d,a,u;if(!this._matches.length)return null;const t=(o=e==null?void 0:e.loop)!=null?o:!1,i=(h=e==null?void 0:e.stayIfOnMatch)!=null?h:!1,n=(d=e==null?void 0:e.noFocus)!=null?d:!1,r=(a=e==null?void 0:e.ignoreSelection)!=null?a:!1,s=this._findNextMatch(t,i,r);if(s){const[g,m]=s;return this._matchesPosition=m+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(g.range.subUnitId).findIndex(p=>p===g):this._activeHighlightIndex=m,n||this._focusMatch(g),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===g.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),g}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var o,h,d,a,u;if(!this._matches.length)return null;const t=(o=e==null?void 0:e.loop)!=null?o:!1,i=(h=e==null?void 0:e.stayIfOnMatch)!=null?h:!1,n=(d=e==null?void 0:e.noFocus)!=null?d:!1,r=(a=e==null?void 0:e.ignoreSelection)!=null?a:!1,s=this._findPreviousMatch(t,i,r);if(s){const[g,m]=s;return this._matchesPosition=m+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(g.range.subUnitId).findIndex(p=>p===g):this._activeHighlightIndex=m,n||this._focusMatch(g),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===g.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),g}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1,i=!1){var o;if(this.currentMatch){const h=this._matches.findIndex(g=>g===this.currentMatch);if(t)return[this.currentMatch,h];const d=h-1;if(!e&&d<0)return null;const a=this._matches.length,u=(d+a)%a;return[this._matches[u],u]}const n=this._workbookSelections.getCurrentLastSelection();if(i||!n){const h=this._matches.length-1;return[this._matches[h],h]}if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n.range);const r=(o=this._workbook.getActiveSheet())==null?void 0:o.getSheetId();if(!r)return null;const s=this._findPreviousWorksheetThatHasAMatch(r,e);return s?this._findPreviousMatchByRange(this._matchesByWorksheet.get(s),n.range):null}_findNextMatch(e=!1,t=!1,i=!1){var o;if(this.currentMatch){const h=this._matches.findIndex(g=>g===this.currentMatch);if(t)return[this.currentMatch,h];const d=h+1,a=this._matches.length;if(!e&&d>=a)return null;const u=d%a;return[this._matches[u],u]}const n=this._workbookSelections.getCurrentLastSelection();if(i||!n)return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n.range,t);const r=(o=this._workbook.getActiveSheet())==null?void 0:o.getSheetId();if(!r)return null;const s=this._findNextWorksheetThatHasAMatch(r,e);return s?this._findNextMatchByRange(this._matchesByWorksheet.get(s),n.range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(o=>o===e),s=(t?l.rotate(i,n+1):i.slice(0,n+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(o=>o===e),s=(t?l.rotate(i,n):i.slice(n)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,i=!1){const n=this._query.findDirection===S.FindDirection.ROW;let r=e.findIndex(o=>{const h=o.range.range;if(!(n?A(t,h):D(t,h)))return!1;const a=E(t,h);return i?a:!a});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,i=!1){const n=this._query.findDirection===S.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const h=o.range.range;if(!(n?ne(t,h):se(t,h)))return!1;const a=E(t,h);return i?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,i=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),n=this._getReplacedCellData(this.currentMatch,i,this._query.findBy===S.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:n}}};return this._commandService.executeCommand(C.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:i,caseSensitive:n,findBy:r}=this._query,s=r===S.FindBy.FORMULA,o=n?"g":"ig",h=[];return l.groupBy(this._matches.filter(a=>a.replaceable),a=>a.range.subUnitId).forEach((a,u)=>{const g=new l.ObjectMatrix,m=this._workbook.getSheetBySheetId(u);a.forEach(p=>{const{startColumn:R,startRow:w}=p.range.range,b=this._getReplacedCellData(p,m,s,i,e,o);b&&g.setValue(w,R,b)}),h.push({count:a.length,subUnitId:u,value:g.getMatrix()})}),h?this._commandService.executeCommand(U.id,{unitId:t,replacements:h}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,s){var m;const o=e.range.range,{startRow:h,startColumn:d}=o,a=t.getCellRaw(h,d);if(e.isFormula)return i?{f:a.f.replace(new RegExp(N(n),s),r),v:null}:null;if(!!((m=a.p)!=null&&m.body)){const p=l.Tools.deepClone(a.p);return l.replaceInDocumentBody(p.body,n,r,this._query.caseSensitive),{p}}return{v:a.v.toString().replace(new RegExp(N(n),s),r)}}};F=B([v(2,l.IUniverInstanceService),v(3,M.IRenderManagerService),v(4,l.ICommandService),v(5,l.IContextService),v(6,l.Inject(l.ThemeService)),v(7,l.Inject(C.SheetsSelectionsService))],F);function N(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let P=class extends l.Disposable{constructor(e,t,i){super();_(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=i}async find(e){this._terminate();const t=this._univerInstanceService.getAllUnitsForType(l.UniverInstanceType.UNIVER_SHEET),i=this._preprocessQuery(e);return t.map(r=>{const s=this._renderManagerService.getRenderById(r.getUnitId()).with(k.SheetSkeletonManagerService),o=this._injector.createInstance(F,r,s);return this._findModelsByUnitId.set(r.getUnitId(),o),o.start(i),o})}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([v(0,l.IUniverInstanceService),v(1,M.IRenderManagerService),v(2,l.Inject(l.Injector))],P);const f={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function le(c,e,t,i,n){const{findBy:r}=i,s=r===S.FindBy.FORMULA,o=c.getCellRaw(e,t);return f.rawData=o,!(o!=null&&o.f)?(f.isFormula=!1,x(n,i)?o?(f.hit=!0,f.replaceable=!0):(f.hit=!0,f.replaceable=!1):(f.hit=!1,f.replaceable=!1),f):(f.isFormula=!0,s?x({v:o.f},i)?(f.hit=!0,f.replaceable=!0,f):(f.hit=!1,f.replaceable=!1,f):(f.replaceable=!1,x(n,i)?f.hit=!0:f.hit=!1,f))}function x(c,e){let t=de(c);return t?e.matchesTheWholeCell?(t=ue(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 de(c){var t,i,n;const e=(n=(i=(t=c==null?void 0:c.p)==null?void 0:t.body)==null?void 0:i.dataStream)!=null?n:c==null?void 0:c.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ue(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var j=Object.defineProperty,ge=Object.getOwnPropertyDescriptor,fe=(c,e,t)=>e in c?j(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,_e=(c,e,t,i)=>{for(var n=i>1?void 0:i?ge(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&j(e,t,n),n},$=(c,e)=>(t,i)=>e(t,i,c),L=(c,e,t)=>fe(c,typeof e!="symbol"?e+"":e,t);const Se="SHEET_FIND_REPLACE_PLUGIN";exports.UniverSheetsFindReplacePlugin=class extends l.Plugin{constructor(e=ee,t,i){super(),this._config=e,this._injector=t,this._configService=i;const{...n}=this._config;this._configService.setConfig(Z,n)}onStarting(){[[exports.SheetsFindReplaceController]].forEach(e=>this._injector.add(e))}onSteady(){this._injector.get(exports.SheetsFindReplaceController)}};L(exports.UniverSheetsFindReplacePlugin,"pluginName",Se);L(exports.UniverSheetsFindReplacePlugin,"type",l.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=_e([l.DependentOn(C.UniverSheetsPlugin,C.UniverSheetsPlugin,S.UniverFindReplacePlugin),$(1,l.Inject(l.Injector)),$(2,l.IConfigService)],exports.UniverSheetsFindReplacePlugin);exports.SheetReplaceCommand=U;
@@ -0,0 +1 @@
1
+ "use strict";const e={"sheet-find-replace":{replace:{"partial-failure":"Failed to replace some cells",failure:"Failed to replace all cells"}}};module.exports=e;
@@ -0,0 +1 @@
1
+ "use strict";const e={"sheet-find-replace":{replace:{"partial-failure":"جایگزینی برخی سلول‌ها ناموفق بود",failure:"جایگزینی همه سلول‌ها ناموفق بود"}}};module.exports=e;
@@ -0,0 +1 @@
1
+ "use strict";const e={"sheet-find-replace":{replace:{"partial-failure":"Не удалось заменить некоторые ячейки",failure:"Не удалось заменить все ячейки"}}};module.exports=e;
@@ -0,0 +1 @@
1
+ "use strict";const t={"sheet-find-replace":{replace:{"partial-failure":"Thay thế một phần ô thất bại",failure:"Thay thế thất bại"}}};module.exports=t;
@@ -0,0 +1 @@
1
+ "use strict";const e={"sheet-find-replace":{replace:{"partial-failure":"部分单元格替换失败",failure:"替换失败"}}};module.exports=e;
@@ -0,0 +1 @@
1
+ "use strict";const e={"sheet-find-replace":{replace:{"partial-failure":"部分單元格替換失敗",failure:"替換失敗"}}};module.exports=e;