@univerjs/sheets-find-replace 0.1.16 → 0.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/index.js +1 -1
- package/lib/es/index.js +14 -14
- package/lib/umd/index.js +1 -1
- package/package.json +13 -13
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var ee=Object.defineProperty;var te=(c,e,t)=>e in c?ee(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var S=(c,e,t)=>te(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),C=require("@wendellhu/redi"),_=require("@univerjs/find-replace"),R=require("@univerjs/sheets"),y=require("@univerjs/engine-render"),k=require("@univerjs/sheets-ui"),I=require("rxjs");class ne extends y.Shape{constructor(t,n){super(t,n);S(this,"_activated",!1);S(this,"_inHiddenRange",!1);S(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;y.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const $={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),n=c.get(d.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(R.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),ie(o,r)}};function ie(c,e){let t=0,n=0;return c.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function E(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function A(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function D(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function se(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function re(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function oe(c,e){const{range:t}=c,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var ce=Object.defineProperty,he=Object.getOwnPropertyDescriptor,B=(c,e,t,n)=>{for(var i=n>1?void 0:n?he(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&ce(e,t,i),i},m=(c,e)=>(t,n)=>e(t,n,c);exports.SheetsFindReplaceController=class extends d.Disposable{constructor(t,n,i,r,s){super();S(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(P);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(I.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[$].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}};exports.SheetsFindReplaceController=B([d.OnLifecycle(d.LifecycleStages.Steady,exports.SheetsFindReplaceController),m(0,C.Inject(C.Injector)),m(1,C.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],exports.SheetsFindReplaceController);const ae="sheets-find-replace-provider",le=1e4;let F=class extends _.FindModel{constructor(e,t,n,i,r,s,o,a){super();S(this,"_matchesUpdate$",new I.Subject);S(this,"matchesUpdate$",this._matchesUpdate$.asObservable());S(this,"_activelyChangingMatch$",new I.Subject);S(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());S(this,"_matchesByWorksheet",new Map);S(this,"_matches",[]);S(this,"_matchesPosition",0);S(this,"_activeHighlightIndex",-1);S(this,"_highlightShapes",[]);S(this,"_currentHighlightShape",null);S(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(y.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:h}=a;return h.length?this._matchesByWorksheet.set(l,a.results):this._matchesByWorksheet.delete(l),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s,o])=>s.id===R.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),I.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const l=this._selectionManagerService.getSelections();return(u=l==null?void 0:l.some(f=>!oe(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const l=this.currentMatch;s=n();const h=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,h,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(I.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(I.filter(([a])=>{if(a.type===d.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===R.SetSelectionsOperation.id&&a.params.unitId===t){const l=n();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(I.skip(1))).pipe(I.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const l of a){const{row:h,col:u,colSpan:f,rowSpan:v,value:p}=l;if(r!=null&&r(h,u)||!p||e.getRowFiltered(h))continue;const{hit:b,replaceable:M,isFormula:w}=de(e,h,u,t,p);if(b){const x={provider:ae,unitId:i,replaceable:M,isFormula:w,range:{subUnitId:o,range:{startRow:h,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:h+(v!=null?v:1)-1}}};s.push(x)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===_.FindDirection.ROW?A:D,o=new Set;return{results:t.map(l=>this._findInRange(e,n,l.range,i,(h,u)=>{const f=`${h}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((l,h)=>s(l.range.range,h.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var v,p;this._disposeHighlights();const e=(v=this._sheetSkeletonManagerService.getCurrent())==null?void 0:v.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(a).toRgb(),h=this._workbook.getActiveSheet();if(!h)return;const u=h.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,M)=>{const{startColumn:w,startRow:x,endColumn:V,endRow:q}=b.range.range,Q=k.getCoordByCell(x,w,s,e),X=k.getCoordByCell(q,V,s,e),{startX:W,startY:H}=Q,{endX:Y,endY:G}=X,T=!h.getRowRawVisible(x),O=!h.getColVisible(w),K=T||O,z=O?2:Y-W,J=T?2:G-H,Z={left:W,top:H,color:l,width:z,height:J,evented:!1,inHiddenRange:K,zIndex:le};return new ne(`find-highlight-${M}`,Z)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(R.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:l}=o.range.range;return a===i&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const l=this._matches.length,h=(a+l)%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,l=this._matches.length;if(!e&&a>=l)return null;const h=a%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?A(t,a):D(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?se(t,a):re(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===_.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(R.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===_.FindBy.FORMULA,o=i?"g":"ig",a=[];return d.groupBy(this._matches.filter(h=>h.replaceable),h=>h.range.subUnitId).forEach((h,u)=>{const f=new d.ObjectMatrix,v=this._workbook.getSheetBySheetId(u);h.forEach(p=>{const{startColumn:b,startRow:M}=p.range.range,w=this._getReplacedCellData(p,v,s,n,e,o);w&&f.setValue(M,b,w)}),a.push({count:h.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand($.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var v;const o=e.range.range,{startRow:a,startColumn:l}=o,h=t.getCellRaw(a,l);if(e.isFormula)return n?{f:h.f.replace(new RegExp(N(i),s),r),v:null}:null;if(!!((v=h.p)!=null&&v.body)){const p=d.Tools.deepClone(h.p);return d.replaceInDocumentBody(p.body,i,r),{p}}return{v:h.v.toString().replace(new RegExp(N(i),s),r)}}};F=B([m(2,d.IUniverInstanceService),m(3,y.IRenderManagerService),m(4,d.ICommandService),m(5,d.IContextService),m(6,C.Inject(d.ThemeService)),m(7,C.Inject(R.SelectionManagerService))],F);function N(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let P=class extends d.Disposable{constructor(e,t,n){super();S(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(k.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(F,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};P=B([m(0,d.IUniverInstanceService),m(1,y.IRenderManagerService),m(2,C.Inject(C.Injector))],P);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function de(c,e,t,n,i){const{findBy:r}=n,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,U(i,n)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?U({v:o.f},n)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,U(i,n)?g.hit=!0:g.hit=!1,g))}function U(c,e){let t=ue(c);return t?e.matchesTheWholeCell?(t=ge(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function ue(c){var t,n,i;const e=(i=(n=(t=c==null?void 0:c.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:c==null?void 0:c.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ge(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var j=Object.defineProperty,fe=Object.getOwnPropertyDescriptor,_e=(c,e,t)=>e in c?j(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,Se=(c,e,t,n)=>{for(var i=n>1?void 0:n?fe(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&j(e,t,i),i},me=(c,e)=>(t,n)=>e(t,n,c),L=(c,e,t)=>_e(c,typeof e!="symbol"?e+"":e,t);const pe="SHEET_FIND_REPLACE_PLUGIN";exports.UniverSheetsFindReplacePlugin=class extends d.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[exports.SheetsFindReplaceController]].forEach(t=>e.add(t))}};L(exports.UniverSheetsFindReplacePlugin,"pluginName",pe);L(exports.UniverSheetsFindReplacePlugin,"type",d.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=Se([d.DependentOn(R.UniverSheetsPlugin,R.UniverSheetsPlugin,_.UniverFindReplacePlugin),me(1,C.Inject(C.Injector))],exports.UniverSheetsFindReplacePlugin);
|
|
1
|
+
"use strict";var ee=Object.defineProperty;var te=(c,e,t)=>e in c?ee(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var S=(c,e,t)=>te(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),C=require("@wendellhu/redi"),_=require("@univerjs/find-replace"),R=require("@univerjs/sheets"),y=require("@univerjs/engine-render"),k=require("@univerjs/sheets-ui"),I=require("rxjs");class ne extends y.Shape{constructor(t,n){super(t,n);S(this,"_activated",!1);S(this,"_inHiddenRange",!1);S(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;y.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const $={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),n=c.get(d.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(R.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),ie(o,r)}};function ie(c,e){let t=0,n=0;return c.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function E(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function A(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function D(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function se(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function re(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function oe(c,e){const{range:t}=c,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var ce=Object.defineProperty,he=Object.getOwnPropertyDescriptor,B=(c,e,t,n)=>{for(var i=n>1?void 0:n?he(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&ce(e,t,i),i},m=(c,e)=>(t,n)=>e(t,n,c);exports.SheetsFindReplaceController=class extends d.Disposable{constructor(t,n,i,r,s){super();S(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(P);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(I.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[$].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}};exports.SheetsFindReplaceController=B([d.OnLifecycle(d.LifecycleStages.Steady,exports.SheetsFindReplaceController),m(0,C.Inject(C.Injector)),m(1,C.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],exports.SheetsFindReplaceController);const ae="sheets-find-replace-provider",le=1e4;let F=class extends _.FindModel{constructor(e,t,n,i,r,s,o,a){super();S(this,"_matchesUpdate$",new I.Subject);S(this,"matchesUpdate$",this._matchesUpdate$.asObservable());S(this,"_activelyChangingMatch$",new I.Subject);S(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());S(this,"_matchesByWorksheet",new Map);S(this,"_matches",[]);S(this,"_matchesPosition",0);S(this,"_activeHighlightIndex",-1);S(this,"_highlightShapes",[]);S(this,"_currentHighlightShape",null);S(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(y.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:h}=a;return h.length?this._matchesByWorksheet.set(l,a.results):this._matchesByWorksheet.delete(l),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService.onCommandExecuted)).pipe(I.filter(([s,o])=>s.id===R.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(I.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),I.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const l=this._selectionManagerService.getSelections();return(u=l==null?void 0:l.some(f=>!oe(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const l=this.currentMatch;s=n();const h=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,h,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(I.merge(d.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(I.filter(([a])=>{if(a.type===d.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===R.SetSelectionsOperation.id&&a.params.unitId===t){const l=n();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(I.skip(1))).pipe(I.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const l of a){const{row:h,col:u,colSpan:f,rowSpan:v,value:p}=l;if(r!=null&&r(h,u)||!p||e.getRowFiltered(h))continue;const{hit:b,replaceable:M,isFormula:w}=de(e,h,u,t,p);if(b){const x={provider:ae,unitId:i,replaceable:M,isFormula:w,range:{subUnitId:o,range:{startRow:h,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:h+(v!=null?v:1)-1}}};s.push(x)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===_.FindDirection.ROW?A:D,o=new Set;return{results:t.map(l=>this._findInRange(e,n,l.range,i,(h,u)=>{const f=`${h}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((l,h)=>s(l.range.range,h.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var v,p;this._disposeHighlights();const e=(v=this._sheetSkeletonManagerService.getCurrent())==null?void 0:v.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(a).toRgb(),h=this._workbook.getActiveSheet();if(!h)return;const u=h.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,M)=>{const{startColumn:w,startRow:x,endColumn:V,endRow:q}=b.range.range,Q=k.getCoordByCell(x,w,s,e),X=k.getCoordByCell(q,V,s,e),{startX:W,startY:H}=Q,{endX:Y,endY:G}=X,T=!h.getRowRawVisible(x),O=!h.getColVisible(w),K=T||O,z=O?2:Y-W,J=T?2:G-H,Z={left:W,top:H,color:l,width:z,height:J,evented:!1,inHiddenRange:K,zIndex:le};return new ne(`find-highlight-${M}`,Z)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(R.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:l}=o.range.range;return a===i&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,l;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[h,u]=r;return this._matchesPosition=u+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(h.range.subUnitId).findIndex(f=>f===h):this._activeHighlightIndex=u,i||this._focusMatch(h),((l=this._workbook.getActiveSheet())==null?void 0:l.getSheetId())===h.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),h}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const l=this._matches.length,h=(a+l)%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,l=this._matches.length;if(!e&&a>=l)return null;const h=a%l;return[this._matches[h],h]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?d.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?A(t,a):D(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?se(t,a):re(t,a)))return!1;const h=E(t,a);return n?h:!h});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===_.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(R.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===_.FindBy.FORMULA,o=i?"g":"ig",a=[];return d.groupBy(this._matches.filter(h=>h.replaceable),h=>h.range.subUnitId).forEach((h,u)=>{const f=new d.ObjectMatrix,v=this._workbook.getSheetBySheetId(u);h.forEach(p=>{const{startColumn:b,startRow:M}=p.range.range,w=this._getReplacedCellData(p,v,s,n,e,o);w&&f.setValue(M,b,w)}),a.push({count:h.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand($.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var v;const o=e.range.range,{startRow:a,startColumn:l}=o,h=t.getCellRaw(a,l);if(e.isFormula)return n?{f:h.f.replace(new RegExp(N(i),s),r),v:null}:null;if(!!((v=h.p)!=null&&v.body)){const p=d.Tools.deepClone(h.p);return d.replaceInDocumentBody(p.body,i,r),{p}}return{v:h.v.toString().replace(new RegExp(N(i),s),r)}}};F=B([m(2,d.IUniverInstanceService),m(3,y.IRenderManagerService),m(4,d.ICommandService),m(5,d.IContextService),m(6,C.Inject(d.ThemeService)),m(7,C.Inject(R.SelectionManagerService))],F);function N(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let P=class extends d.Disposable{constructor(e,t,n){super();S(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(k.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(F,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};P=B([m(0,d.IUniverInstanceService),m(1,y.IRenderManagerService),m(2,C.Inject(C.Injector))],P);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function de(c,e,t,n,i){const{findBy:r}=n,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,U(i,n)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?U({v:o.f},n)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,U(i,n)?g.hit=!0:g.hit=!1,g))}function U(c,e){let t=ue(c);return t?e.matchesTheWholeCell?(t=ge(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function ue(c){var t,n,i;const e=(i=(n=(t=c==null?void 0:c.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:c==null?void 0:c.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ge(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var j=Object.defineProperty,fe=Object.getOwnPropertyDescriptor,_e=(c,e,t)=>e in c?j(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t,Se=(c,e,t,n)=>{for(var i=n>1?void 0:n?fe(e,t):e,r=c.length-1,s;r>=0;r--)(s=c[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&j(e,t,i),i},me=(c,e)=>(t,n)=>e(t,n,c),L=(c,e,t)=>_e(c,typeof e!="symbol"?e+"":e,t);const pe="SHEET_FIND_REPLACE_PLUGIN";exports.UniverSheetsFindReplacePlugin=class extends d.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[exports.SheetsFindReplaceController]].forEach(t=>e.add(t))}};L(exports.UniverSheetsFindReplacePlugin,"pluginName",pe);L(exports.UniverSheetsFindReplacePlugin,"type",d.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsFindReplacePlugin=Se([d.DependentOn(R.UniverSheetsPlugin,R.UniverSheetsPlugin,_.UniverFindReplacePlugin),me(1,C.Inject(C.Injector))],exports.UniverSheetsFindReplacePlugin);
|
package/lib/es/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
var fe = Object.defineProperty;
|
|
2
2
|
var _e = (h, e, t) => e in h ? fe(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
|
|
3
3
|
var f = (h, e, t) => _e(h, typeof e != "symbol" ? e + "" : e, 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
|
|
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 be, ObjectMatrix as we, Tools as Me, replaceInDocumentBody as ke, DependentOn as xe, Plugin as ye } from "@univerjs/core";
|
|
5
5
|
import { Inject as C, Injector as O } from "@wendellhu/redi";
|
|
6
|
-
import { FindReplaceController as Ue, IFindReplaceService as Pe, FindModel as Be, FindBy as M, FindScope as I, FindDirection as
|
|
6
|
+
import { FindReplaceController as Ue, IFindReplaceService as Pe, FindModel as Be, FindBy as M, FindScope as I, FindDirection as w, UniverFindReplacePlugin as We } from "@univerjs/find-replace";
|
|
7
7
|
import { SetRangeValuesCommand as q, SelectionManagerService as Fe, SetWorksheetActiveOperation as He, SetSelectionsOperation as Oe, SetWorksheetActivateCommand as Te, UniverSheetsPlugin as L } from "@univerjs/sheets";
|
|
8
8
|
import { Shape as Ee, Rect as Ae, IRenderManagerService as ee, RENDER_RAW_FORMULA_KEY as Ne } from "@univerjs/engine-render";
|
|
9
9
|
import { SheetSkeletonManagerService as De, getCoordByCell as j, getSheetObject as $e, ScrollToCellCommand as Le } from "@univerjs/sheets-ui";
|
|
@@ -179,7 +179,7 @@ let W = class extends Be {
|
|
|
179
179
|
return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
|
|
180
180
|
this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
|
|
181
181
|
})), this.disposeWithMe(
|
|
182
|
-
U(this._commandService.onCommandExecuted).pipe(
|
|
182
|
+
U(this._commandService.onCommandExecuted.bind(this._commandService.onCommandExecuted)).pipe(
|
|
183
183
|
k(([s, o]) => s.id === He.id && !(o != null && o.fromFindReplace))
|
|
184
184
|
).subscribe(() => {
|
|
185
185
|
const s = this._workbook.getActiveSheet();
|
|
@@ -189,7 +189,7 @@ let W = class extends Be {
|
|
|
189
189
|
this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
|
|
190
190
|
})
|
|
191
191
|
), this.disposeWithMe(
|
|
192
|
-
U(this._commandService.onCommandExecuted).pipe(
|
|
192
|
+
U(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
|
|
193
193
|
k(
|
|
194
194
|
([s]) => s.type === B.MUTATION && s.params.unitId === this._workbook.getUnitId()
|
|
195
195
|
),
|
|
@@ -230,7 +230,7 @@ let W = class extends Be {
|
|
|
230
230
|
};
|
|
231
231
|
return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
|
|
232
232
|
Ve(
|
|
233
|
-
U(this._commandService.onCommandExecuted).pipe(
|
|
233
|
+
U(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(
|
|
234
234
|
k(([a]) => {
|
|
235
235
|
if (a.type === B.MUTATION && a.params.unitId === this._workbook.getUnitId())
|
|
236
236
|
return !0;
|
|
@@ -247,14 +247,14 @@ let W = class extends Be {
|
|
|
247
247
|
), o(), n;
|
|
248
248
|
}
|
|
249
249
|
_findInRange(e, t, i, n, r) {
|
|
250
|
-
const s = [], o = e.getSheetId(), a = (t.findDirection ===
|
|
250
|
+
const s = [], o = e.getSheetId(), a = (t.findDirection === w.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
|
|
251
251
|
for (const l of a) {
|
|
252
252
|
const { row: c, col: d, colSpan: g, rowSpan: S, value: m } = l;
|
|
253
253
|
if (r != null && r(c, d) || !m || e.getRowFiltered(c))
|
|
254
254
|
continue;
|
|
255
255
|
const { hit: p, replaceable: R, isFormula: v } = it(e, c, d, t, m);
|
|
256
256
|
if (p) {
|
|
257
|
-
const
|
|
257
|
+
const b = {
|
|
258
258
|
provider: et,
|
|
259
259
|
unitId: n,
|
|
260
260
|
replaceable: R,
|
|
@@ -269,13 +269,13 @@ let W = class extends Be {
|
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
271
|
};
|
|
272
|
-
s.push(
|
|
272
|
+
s.push(b);
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
return { results: s };
|
|
276
276
|
}
|
|
277
277
|
_findInSelections(e, t, i, n) {
|
|
278
|
-
const { findDirection: r } = i, s = r ===
|
|
278
|
+
const { findDirection: r } = i, s = r === w.ROW ? X : Y, o = /* @__PURE__ */ new Set();
|
|
279
279
|
return { results: t.map((l) => this._findInRange(
|
|
280
280
|
e,
|
|
281
281
|
i,
|
|
@@ -311,7 +311,7 @@ let W = class extends Be {
|
|
|
311
311
|
if (!c)
|
|
312
312
|
return;
|
|
313
313
|
const d = c.getSheetId(), g = o.filter((p) => p.range.subUnitId === d).map((p, R) => {
|
|
314
|
-
const { startColumn: v, startRow:
|
|
314
|
+
const { startColumn: v, startRow: b, endColumn: se, endRow: re } = p.range.range, oe = j(b, v, s, e), he = j(re, se, s, e), { startX: E, startY: A } = oe, { endX: ce, endY: ae } = he, N = !c.getRowRawVisible(b), D = !c.getColVisible(v), le = N || D, de = D ? 2 : ce - E, ue = N ? 2 : ae - A, ge = {
|
|
315
315
|
left: E,
|
|
316
316
|
top: A,
|
|
317
317
|
color: l,
|
|
@@ -437,7 +437,7 @@ let W = class extends Be {
|
|
|
437
437
|
return s != null ? s : null;
|
|
438
438
|
}
|
|
439
439
|
_findNextMatchByRange(e, t, i = !1) {
|
|
440
|
-
const n = this._query.findDirection ===
|
|
440
|
+
const n = this._query.findDirection === w.ROW;
|
|
441
441
|
let r = e.findIndex((o) => {
|
|
442
442
|
const a = o.range.range;
|
|
443
443
|
if (!(n ? X(t, a) : Y(t, a)))
|
|
@@ -450,7 +450,7 @@ let W = class extends Be {
|
|
|
450
450
|
return [s, this._matches.findIndex((o) => o === s)];
|
|
451
451
|
}
|
|
452
452
|
_findPreviousMatchByRange(e, t, i = !1) {
|
|
453
|
-
const n = this._query.findDirection ===
|
|
453
|
+
const n = this._query.findDirection === w.ROW;
|
|
454
454
|
let r = this._matches.findLastIndex((o) => {
|
|
455
455
|
const a = o.range.range;
|
|
456
456
|
if (!(n ? Ke(t, a) : ze(t, a)))
|
|
@@ -487,8 +487,8 @@ let W = class extends Be {
|
|
|
487
487
|
if (this._matchesCount === 0 || !this._query)
|
|
488
488
|
return { success: 0, failure: 0 };
|
|
489
489
|
const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === M.FORMULA, o = n ? "g" : "ig", a = [];
|
|
490
|
-
return
|
|
491
|
-
const g = new
|
|
490
|
+
return be(this._matches.filter((c) => c.replaceable), (c) => c.range.subUnitId).forEach((c, d) => {
|
|
491
|
+
const g = new we(), S = this._workbook.getSheetBySheetId(d);
|
|
492
492
|
c.forEach((m) => {
|
|
493
493
|
const { startColumn: p, startRow: R } = m.range.range, v = this._getReplacedCellData(m, S, s, i, e, o);
|
|
494
494
|
v && g.setValue(R, p, v);
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(g,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/find-replace"),require("@univerjs/sheets"),require("@univerjs/engine-render"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/sheets","@univerjs/engine-render","@univerjs/sheets-ui","rxjs"],l):(g=typeof globalThis<"u"?globalThis:g||self,l(g.UniverSheetsFindReplace={},g.UniverCore,g["@wendellhu/redi"],g.UniverFindReplace,g.UniverSheets,g.UniverEngineRender,g.UniverSheetsUi,g.rxjs))})(this,function(g,l,v,S,w,k,y,R){"use strict";var ve=Object.defineProperty;var Ie=(g,l,v)=>l in g?ve(g,l,{enumerable:!0,configurable:!0,writable:!0,value:v}):g[l]=v;var m=(g,l,v)=>Ie(g,typeof l!="symbol"?l+"":l,v);class q extends k.Shape{constructor(t,n){super(t,n);m(this,"_activated",!1);m(this,"_inHiddenRange",!1);m(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;k.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const T={id:"sheet.command.replace",type:l.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(l.IUndoRedoService),n=h.get(l.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(w.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),Q(o,r)}};function Q(h,e){let t=0,n=0;return h.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function H(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function O(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function E(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function X(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function Y(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function G(h,e){const{range:t}=h,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?l.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var K=Object.defineProperty,z=Object.getOwnPropertyDescriptor,x=(h,e,t,n)=>{for(var i=n>1?void 0:n?z(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&K(e,t,i),i},I=(h,e)=>(t,n)=>e(t,n,h);g.SheetsFindReplaceController=class extends l.Disposable{constructor(t,n,i,r,s){super();m(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(B);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(l.EDITOR_ACTIVATED).pipe(R.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[T].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}},g.SheetsFindReplaceController=x([l.OnLifecycle(l.LifecycleStages.Steady,g.SheetsFindReplaceController),I(0,v.Inject(v.Injector)),I(1,v.Inject(S.FindReplaceController)),I(2,l.IContextService),I(3,S.IFindReplaceService),I(4,l.ICommandService)],g.SheetsFindReplaceController);const J="sheets-find-replace-provider",Z=1e4;let P=class extends S.FindModel{constructor(e,t,n,i,r,s,o,a){super();m(this,"_matchesUpdate$",new R.Subject);m(this,"matchesUpdate$",this._matchesUpdate$.asObservable());m(this,"_activelyChangingMatch$",new R.Subject);m(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());m(this,"_matchesByWorksheet",new Map);m(this,"_matches",[]);m(this,"_matchesPosition",0);m(this,"_activeHighlightIndex",-1);m(this,"_highlightShapes",[]);m(this,"_currentHighlightShape",null);m(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===S.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case S.FindScope.UNIT:this.findInWorkbook(e);break;case S.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(k.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),d=o.getSheetId(),{results:c}=a;return c.length?this._matchesByWorksheet.set(d,a.results):this._matchesByWorksheet.delete(d),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([s])=>s.type===l.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),R.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const d=this._selectionManagerService.getSelections();return(u=d==null?void 0:d.some(f=>!G(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const d=this.currentMatch;s=n();const c=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,c,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(d,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(R.merge(l.fromCallback(this._commandService.onCommandExecuted).pipe(R.filter(([a])=>{if(a.type===l.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===w.SetSelectionsOperation.id&&a.params.unitId===t){const d=n();return d===!1&&s===!1?!1:(s=d,!0)}return!1})),this._workbook.activeSheet$.pipe(R.skip(1))).pipe(R.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const d of a){const{row:c,col:u,colSpan:f,rowSpan:C,value:p}=d;if(r!=null&&r(c,u)||!p||e.getRowFiltered(c))continue;const{hit:b,replaceable:U,isFormula:M}=ee(e,c,u,t,p);if(b){const F={provider:J,unitId:i,replaceable:U,isFormula:M,range:{subUnitId:o,range:{startRow:c,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:c+(C!=null?C:1)-1}}};s.push(F)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===S.FindDirection.ROW?O:E,o=new Set;return{results:t.map(d=>this._findInRange(e,n,d.range,i,(c,u)=>{const f=`${c}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((d,c)=>s(d.range.range,c.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,p;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,d=new l.ColorKit(a).toRgb(),c=this._workbook.getActiveSheet();if(!c)return;const u=c.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,U)=>{const{startColumn:M,startRow:F,endColumn:ce,endRow:ae}=b.range.range,le=y.getCoordByCell(F,M,s,e),de=y.getCoordByCell(ae,ce,s,e),{startX:j,startY:$}=le,{endX:ue,endY:ge}=de,L=!c.getRowRawVisible(F),V=!c.getColVisible(M),_e=L||V,fe=V?2:ue-j,Se=L?2:ge-$,me={left:j,top:$,color:d,width:fe,height:Se,evented:!1,inHiddenRange:_e,zIndex:Z};return new q(`find-highlight-${U}`,me)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return y.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(w.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(y.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:d}=o.range.range;return a===i&&d===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const d=this._matches.length,c=(a+d)%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,d=this._matches.length;if(!e&&a>=d)return null;const c=a%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?O(t,a):E(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?X(t,a):Y(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===S.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(w.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===S.FindBy.FORMULA,o=i?"g":"ig",a=[];return l.groupBy(this._matches.filter(c=>c.replaceable),c=>c.range.subUnitId).forEach((c,u)=>{const f=new l.ObjectMatrix,C=this._workbook.getSheetBySheetId(u);c.forEach(p=>{const{startColumn:b,startRow:U}=p.range.range,M=this._getReplacedCellData(p,C,s,n,e,o);M&&f.setValue(U,b,M)}),a.push({count:c.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand(T.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var C;const o=e.range.range,{startRow:a,startColumn:d}=o,c=t.getCellRaw(a,d);if(e.isFormula)return n?{f:c.f.replace(new RegExp(A(i),s),r),v:null}:null;if(!!((C=c.p)!=null&&C.body)){const p=l.Tools.deepClone(c.p);return l.replaceInDocumentBody(p.body,i,r),{p}}return{v:c.v.toString().replace(new RegExp(A(i),s),r)}}};P=x([I(2,l.IUniverInstanceService),I(3,k.IRenderManagerService),I(4,l.ICommandService),I(5,l.IContextService),I(6,v.Inject(l.ThemeService)),I(7,v.Inject(w.SelectionManagerService))],P);function A(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let B=class extends l.Disposable{constructor(e,t,n){super();m(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(l.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(y.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(P,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};B=x([I(0,l.IUniverInstanceService),I(1,k.IRenderManagerService),I(2,v.Inject(v.Injector))],B);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ee(h,e,t,n,i){const{findBy:r}=n,s=r===S.FindBy.FORMULA,o=h.getCellRaw(e,t);return _.rawData=o,!(o!=null&&o.f)?(_.isFormula=!1,W(i,n)?o?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?W({v:o.f},n)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,W(i,n)?_.hit=!0:_.hit=!1,_))}function W(h,e){let t=te(h);return t?e.matchesTheWholeCell?(t=ne(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function te(h){var t,n,i;const e=(i=(n=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ne(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var D=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,se=(h,e,t)=>e in h?D(h,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[e]=t,re=(h,e,t,n)=>{for(var i=n>1?void 0:n?ie(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&D(e,t,i),i},oe=(h,e)=>(t,n)=>e(t,n,h),N=(h,e,t)=>se(h,typeof e!="symbol"?e+"":e,t);const he="SHEET_FIND_REPLACE_PLUGIN";g.UniverSheetsFindReplacePlugin=class extends l.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[g.SheetsFindReplaceController]].forEach(t=>e.add(t))}},N(g.UniverSheetsFindReplacePlugin,"pluginName",he),N(g.UniverSheetsFindReplacePlugin,"type",l.UniverInstanceType.UNIVER_SHEET),g.UniverSheetsFindReplacePlugin=re([l.DependentOn(w.UniverSheetsPlugin,w.UniverSheetsPlugin,S.UniverFindReplacePlugin),oe(1,v.Inject(v.Injector))],g.UniverSheetsFindReplacePlugin),Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(g,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/find-replace"),require("@univerjs/sheets"),require("@univerjs/engine-render"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/sheets","@univerjs/engine-render","@univerjs/sheets-ui","rxjs"],l):(g=typeof globalThis<"u"?globalThis:g||self,l(g.UniverSheetsFindReplace={},g.UniverCore,g["@wendellhu/redi"],g.UniverFindReplace,g.UniverSheets,g.UniverEngineRender,g.UniverSheetsUi,g.rxjs))})(this,function(g,l,v,S,w,k,y,R){"use strict";var ve=Object.defineProperty;var Ie=(g,l,v)=>l in g?ve(g,l,{enumerable:!0,configurable:!0,writable:!0,value:v}):g[l]=v;var m=(g,l,v)=>Ie(g,typeof l!="symbol"?l+"":l,v);class q extends k.Shape{constructor(t,n){super(t,n);m(this,"_activated",!1);m(this,"_inHiddenRange",!1);m(this,"_color");n&&this.setShapeProps(n)}setShapeProps(t){this._activated=!!t.activated,typeof t.inHiddenRange<"u"&&(this._inHiddenRange=t.inHiddenRange),typeof t.color<"u"&&(this._color=t.color),this.transformByState({width:t.width,height:t.height})}_draw(t){const n=this._activated,i=`rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`,r=`rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;k.Rect.drawWith(t,{width:this.width,height:this.height,fill:i,stroke:n?r:void 0,strokeWidth:n?2:0,evented:!1})}}const T={id:"sheet.command.replace",type:l.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(l.IUndoRedoService),n=h.get(l.ICommandService),{unitId:i,replacements:r}=e,s=t.__tempBatchingUndoRedo(i),o=await Promise.all(r.map(a=>n.executeCommand(w.SetRangeValuesCommand.id,{unitId:i,subUnitId:a.subUnitId,value:a.value})));return s.dispose(),Q(o,r)}};function Q(h,e){let t=0,n=0;return h.forEach((i,r)=>{const s=e[r].count;i?t+=s:n+=s}),{success:t,failure:n}}function H(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function O(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function E(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function X(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function Y(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function G(h,e){const{range:t}=h,{startRow:n,startColumn:i}=t,r=e.getMergedCell(n,i);return r?l.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var K=Object.defineProperty,z=Object.getOwnPropertyDescriptor,x=(h,e,t,n)=>{for(var i=n>1?void 0:n?z(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&K(e,t,i),i},I=(h,e)=>(t,n)=>e(t,n,h);g.SheetsFindReplaceController=class extends l.Disposable{constructor(t,n,i,r,s){super();m(this,"_provider");this._injector=t,this._findReplaceController=n,this._contextService=i,this._findReplaceService=r,this._commandService=s,this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){const t=this._injector.createInstance(B);this._provider=t,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(t)),this.disposeWithMe(this._contextService.subscribeContextValue$(l.EDITOR_ACTIVATED).pipe(R.filter(n=>!!n)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[T].forEach(t=>this.disposeWithMe(this._commandService.registerCommand(t)))}},g.SheetsFindReplaceController=x([l.OnLifecycle(l.LifecycleStages.Steady,g.SheetsFindReplaceController),I(0,v.Inject(v.Injector)),I(1,v.Inject(S.FindReplaceController)),I(2,l.IContextService),I(3,S.IFindReplaceService),I(4,l.ICommandService)],g.SheetsFindReplaceController);const J="sheets-find-replace-provider",Z=1e4;let P=class extends S.FindModel{constructor(e,t,n,i,r,s,o,a){super();m(this,"_matchesUpdate$",new R.Subject);m(this,"matchesUpdate$",this._matchesUpdate$.asObservable());m(this,"_activelyChangingMatch$",new R.Subject);m(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());m(this,"_matchesByWorksheet",new Map);m(this,"_matches",[]);m(this,"_matchesPosition",0);m(this,"_activeHighlightIndex",-1);m(this,"_highlightShapes",[]);m(this,"_currentHighlightShape",null);m(this,"_query",null);this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=i,this._commandService=r,this._contextService=s,this._themeService=o,this._selectionManagerService=a}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===S.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case S.FindScope.UNIT:this.findInWorkbook(e);break;case S.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(k.RENDER_RAW_FORMULA_KEY,e)}findInWorkbook(e){const t=this._workbook.getUnitId();let n,i=!0;const r=()=>{const s=this._workbook.getSheets().filter(o=>!o.isSheetHidden()).map(o=>{const a=this._findInWorksheet(o,e,t),d=o.getSheetId(),{results:c}=a;return c.length?this._matchesByWorksheet.set(d,a.results):this._matchesByWorksheet.delete(d),a});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),i?(n={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService.onCommandExecuted)).pipe(R.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet();if(!s)return;const o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(R.filter(([s])=>s.type===l.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),R.throttleTime(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>r())),r(),n}_findNextMatchOnActiveSheetChange(e){let t,n,i=0;const r=this._matchesByWorksheet.get(e.getSheetId()),s=this._selectionManagerService.getSelections();s!=null&&s.length?([t,i]=this._findNextMatchByRange(r,s[0].range),n=r.findIndex(o=>o===t)):(t=r[0],n=0,i=this._matches.findIndex(o=>o===t)),this._matchesPosition=i+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){const t=this._workbook.getUnitId(),n=()=>{var u;const a=this._workbook.getActiveSheet();if(!a)return!1;const d=this._selectionManagerService.getSelections();return(u=d==null?void 0:d.some(f=>!G(f,a)))!=null?u:!1};let i,r=!0,s=!1;const o=()=>{const a=this._workbook.getActiveSheet();if(!a)return{results:[]};const d=this.currentMatch;s=n();const c=this._selectionManagerService.getSelections(),u=s?this._findInSelections(a,c,e,t):this._findInWorksheet(a,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(d,this._matches),r?(i=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(R.merge(l.fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(R.filter(([a])=>{if(a.type===l.CommandType.MUTATION&&a.params.unitId===this._workbook.getUnitId())return!0;if(a.id===w.SetSelectionsOperation.id&&a.params.unitId===t){const d=n();return d===!1&&s===!1?!1:(s=d,!0)}return!1})),this._workbook.activeSheet$.pipe(R.skip(1))).pipe(R.debounceTime(200)).subscribe(()=>o())),o(),i}_findInRange(e,t,n,i,r){const s=[],o=e.getSheetId(),a=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(const d of a){const{row:c,col:u,colSpan:f,rowSpan:C,value:p}=d;if(r!=null&&r(c,u)||!p||e.getRowFiltered(c))continue;const{hit:b,replaceable:U,isFormula:M}=ee(e,c,u,t,p);if(b){const F={provider:J,unitId:i,replaceable:U,isFormula:M,range:{subUnitId:o,range:{startRow:c,startColumn:u,endColumn:u+(f!=null?f:1)-1,endRow:c+(C!=null?C:1)-1}}};s.push(F)}}return{results:s}}_findInSelections(e,t,n,i){const{findDirection:r}=n,s=r===S.FindDirection.ROW?O:E,o=new Set;return{results:t.map(d=>this._findInRange(e,n,d.range,i,(c,u)=>{const f=`${c}-${u}`;return o.has(f)?!0:(o.add(f),!1)}).results).flat().sort((d,c)=>s(d.range.range,c.range.range)?-1:1)}}_findInWorksheet(e,t,n){const i=e.getRowCount(),r=e.getColumnCount(),s={startRow:0,startColumn:0,endRow:i-1,endColumn:r-1};return this._findInRange(e,t,s,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(t=>{t.getScene().makeDirty(),t.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,p;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUnit())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const i=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(i);if(r==null)return;const{scene:s}=r,o=this._matches,a=this._themeService.getCurrentTheme().gold400,d=new l.ColorKit(a).toRgb(),c=this._workbook.getActiveSheet();if(!c)return;const u=c.getSheetId(),f=o.filter(b=>b.range.subUnitId===u).map((b,U)=>{const{startColumn:M,startRow:F,endColumn:ce,endRow:ae}=b.range.range,le=y.getCoordByCell(F,M,s,e),de=y.getCoordByCell(ae,ce,s,e),{startX:j,startY:$}=le,{endX:ue,endY:ge}=de,L=!c.getRowRawVisible(F),V=!c.getColVisible(M),_e=L||V,fe=V?2:ue-j,Se=L?2:ge-$,me={left:j,top:$,color:d,width:fe,height:Se,evented:!1,inHiddenRange:_e,zIndex:Z};return new q(`find-highlight-${U}`,me)});s.addObjects(f),this._highlightShapes=f,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const n=this._highlightShapes[e];if(!n)return;this._currentHighlightShape=n,n.setShapeProps({activated:!0})}}_getSheetObject(){return y.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var n;const t=e.range.subUnitId;t!==((n=this._workbook.getActiveSheet())==null?void 0:n.getSheetId())&&this._commandService.syncExecuteCommand(w.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(y.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:n}=e.range,{startColumn:i,startRow:r}=e.range.range,s=t.findIndex(o=>{if(n!==o.range.subUnitId)return!1;const{startColumn:a,startRow:d}=o.range.range;return a===i&&d===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findNextMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,a,d;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,n=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,i=(a=e==null?void 0:e.noFocus)!=null?a:!1,r=this._findPreviousMatch(t,n);if(r){const[c,u]=r;return this._matchesPosition=u+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(c.range.subUnitId).findIndex(f=>f===c):this._activeHighlightIndex=u,i||this._focusMatch(c),((d=this._workbook.getActiveSheet())==null?void 0:d.getSheetId())===c.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),c}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o-1;if(!e&&a<0)return null;const d=this._matches.length,c=(a+d)%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,n[0].range);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findPreviousWorksheetThatHasAMatch(i,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findNextMatch(e=!1,t=!1){var s;if(this.currentMatch){const o=this._matches.findIndex(u=>u===this.currentMatch);if(t)return[this.currentMatch,o];const a=o+1,d=this._matches.length;if(!e&&a>=d)return null;const c=a%d;return[this._matches[c],c]}const n=this._selectionManagerService.getSelections();if(!(n!=null&&n.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,n[0].range,t);const i=(s=this._workbook.getActiveSheet())==null?void 0:s.getSheetId();if(!i)return null;const r=this._findNextWorksheetThatHasAMatch(i,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),n[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i+1):n.slice(0,i+1)).findLast(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextWorksheetThatHasAMatch(e,t=!1){const n=this._workbook.getSheetOrders(),i=n.findIndex(o=>o===e),s=(t?l.rotate(n,i):n.slice(i)).find(o=>this._matchesByWorksheet.has(o));return s!=null?s:null}_findNextMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=e.findIndex(o=>{const a=o.range.range;if(!(i?O(t,a):E(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=e.length-1);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}_findPreviousMatchByRange(e,t,n=!1){const i=this._query.findDirection===S.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const a=o.range.range;if(!(i?X(t,a):Y(t,a)))return!1;const c=H(t,a);return n?c:!c});r===-1&&(r=0);const s=e[r];return[s,this._matches.findIndex(o=>o===s)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;const t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),i=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===S.FindBy.FORMULA,this._query.findString,e,this._query.caseSensitive?"g":"ig"),r={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:i}}};return this._commandService.executeCommand(w.SetRangeValuesCommand.id,r)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};const t=this._workbook.getUnitId(),{findString:n,caseSensitive:i,findBy:r}=this._query,s=r===S.FindBy.FORMULA,o=i?"g":"ig",a=[];return l.groupBy(this._matches.filter(c=>c.replaceable),c=>c.range.subUnitId).forEach((c,u)=>{const f=new l.ObjectMatrix,C=this._workbook.getSheetBySheetId(u);c.forEach(p=>{const{startColumn:b,startRow:U}=p.range.range,M=this._getReplacedCellData(p,C,s,n,e,o);M&&f.setValue(U,b,M)}),a.push({count:c.length,subUnitId:u,value:f.getMatrix()})}),a?this._commandService.executeCommand(T.id,{unitId:t,replacements:a}):{success:0,failure:0}}_getReplacedCellData(e,t,n,i,r,s){var C;const o=e.range.range,{startRow:a,startColumn:d}=o,c=t.getCellRaw(a,d);if(e.isFormula)return n?{f:c.f.replace(new RegExp(A(i),s),r),v:null}:null;if(!!((C=c.p)!=null&&C.body)){const p=l.Tools.deepClone(c.p);return l.replaceInDocumentBody(p.body,i,r),{p}}return{v:c.v.toString().replace(new RegExp(A(i),s),r)}}};P=x([I(2,l.IUniverInstanceService),I(3,k.IRenderManagerService),I(4,l.ICommandService),I(5,l.IContextService),I(6,v.Inject(l.ThemeService)),I(7,v.Inject(w.SelectionManagerService))],P);function A(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let B=class extends l.Disposable{constructor(e,t,n){super();m(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._renderManagerService=t,this._injector=n}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(l.UniverInstanceType.UNIVER_SHEET),n=this._renderManagerService.getRenderById(t.getUnitId()).with(y.SheetSkeletonManagerService);if(t){const i=this._injector.createInstance(P,t,n);this._findModelsByUnitId.set(t.getUnitId(),i);const r=this._preprocessQuery(e);return i.start(r),[i]}return[]}terminate(){this._terminate()}_terminate(){this._findModelsByUnitId.forEach(e=>e.dispose()),this._findModelsByUnitId.clear()}_preprocessQuery(e){let t=e.caseSensitive?e.findString:e.findString.toLowerCase();return t=t.trim(),{...e,findString:t}}};B=x([I(0,l.IUniverInstanceService),I(1,k.IRenderManagerService),I(2,v.Inject(v.Injector))],B);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ee(h,e,t,n,i){const{findBy:r}=n,s=r===S.FindBy.FORMULA,o=h.getCellRaw(e,t);return _.rawData=o,!(o!=null&&o.f)?(_.isFormula=!1,W(i,n)?o?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?W({v:o.f},n)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,W(i,n)?_.hit=!0:_.hit=!1,_))}function W(h,e){let t=te(h);return t?e.matchesTheWholeCell?(t=ne(t),e.caseSensitive?t===e.findString:t.toLowerCase()===e.findString):e.caseSensitive?t.indexOf(e.findString)>-1:t.toLowerCase().indexOf(e.findString)>-1:!1}function te(h){var t,n,i;const e=(i=(n=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:n.dataStream)!=null?i:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function ne(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var D=Object.defineProperty,ie=Object.getOwnPropertyDescriptor,se=(h,e,t)=>e in h?D(h,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[e]=t,re=(h,e,t,n)=>{for(var i=n>1?void 0:n?ie(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(i=(n?s(e,t,i):s(i))||i);return n&&i&&D(e,t,i),i},oe=(h,e)=>(t,n)=>e(t,n,h),N=(h,e,t)=>se(h,typeof e!="symbol"?e+"":e,t);const he="SHEET_FIND_REPLACE_PLUGIN";g.UniverSheetsFindReplacePlugin=class extends l.Plugin{constructor(e,t){super(),this._injector=t}onStarting(e){[[g.SheetsFindReplaceController]].forEach(t=>e.add(t))}},N(g.UniverSheetsFindReplacePlugin,"pluginName",he),N(g.UniverSheetsFindReplacePlugin,"type",l.UniverInstanceType.UNIVER_SHEET),g.UniverSheetsFindReplacePlugin=re([l.DependentOn(w.UniverSheetsPlugin,w.UniverSheetsPlugin,S.UniverFindReplacePlugin),oe(1,v.Inject(v.Injector))],g.UniverSheetsFindReplacePlugin),Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-find-replace",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "UniverSheet find replace plugin",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -49,11 +49,11 @@
|
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@wendellhu/redi": "0.15.4",
|
|
51
51
|
"rxjs": ">=7.0.0",
|
|
52
|
-
"@univerjs/core": "0.1.
|
|
53
|
-
"@univerjs/
|
|
54
|
-
"@univerjs/sheets": "0.1.
|
|
55
|
-
"@univerjs/
|
|
56
|
-
"@univerjs/sheets
|
|
52
|
+
"@univerjs/core": "0.1.17",
|
|
53
|
+
"@univerjs/find-replace": "0.1.17",
|
|
54
|
+
"@univerjs/sheets-ui": "0.1.17",
|
|
55
|
+
"@univerjs/engine-render": "0.1.17",
|
|
56
|
+
"@univerjs/sheets": "0.1.17"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@univerjs/protocol": "0.1.38-alpha.6"
|
|
@@ -64,13 +64,13 @@
|
|
|
64
64
|
"typescript": "^5.4.5",
|
|
65
65
|
"vite": "^5.3.1",
|
|
66
66
|
"vitest": "^1.6.0",
|
|
67
|
-
"@univerjs/core": "0.1.
|
|
68
|
-
"@univerjs/engine-formula": "0.1.
|
|
69
|
-
"@univerjs/
|
|
70
|
-
"@univerjs/
|
|
71
|
-
"@univerjs/shared": "0.1.
|
|
72
|
-
"@univerjs/sheets": "0.1.
|
|
73
|
-
"@univerjs/sheets-ui": "0.1.
|
|
67
|
+
"@univerjs/core": "0.1.17",
|
|
68
|
+
"@univerjs/engine-formula": "0.1.17",
|
|
69
|
+
"@univerjs/find-replace": "0.1.17",
|
|
70
|
+
"@univerjs/engine-render": "0.1.17",
|
|
71
|
+
"@univerjs/shared": "0.1.17",
|
|
72
|
+
"@univerjs/sheets": "0.1.17",
|
|
73
|
+
"@univerjs/sheets-ui": "0.1.17"
|
|
74
74
|
},
|
|
75
75
|
"univerSpace": {
|
|
76
76
|
".": {
|