@univerjs/sheets-find-replace 0.1.6 → 0.1.8
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 +166 -170
- package/lib/types/plugin.d.ts +5 -5
- package/lib/umd/index.js +1 -1
- package/package.json +18 -18
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var ee=Object.defineProperty;var te=(c,e,t)=>e in c?ee(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var f=(c,e,t)=>(te(c,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),C=require("@wendellhu/redi"),U=require("@univerjs/engine-render"),_=require("@univerjs/find-replace"),w=require("@univerjs/sheets"),k=require("@univerjs/sheets-ui"),v=require("rxjs"),ie={"sheet-find-replace":{replace:{"partial-failure":"Failed to replace some cells",failure:"Failed to replace all cells"}}},ne={"sheet-find-replace":{replace:{"partial-failure":"部分单元格替换失败",failure:"替换失败"}}};class se extends U.Shape{constructor(t,i){super(t,i);f(this,"_activated",!1);f(this,"_inHiddenRange",!1);f(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})`;U.Rect.drawWith(t,{width:this.width,height:this.height,fill:n,stroke:i?r:void 0,strokeWidth:i?2:0,evented:!1})}}const V={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),i=c.get(d.ICommandService),{unitId:n,replacements:r}=e,s=t.__tempBatchingUndoRedo(n),o=await Promise.all(r.map(h=>i.executeCommand(w.SetRangeValuesCommand.id,{unitId:n,subUnitId:h.subUnitId,value:h.value})));return s.dispose(),re(o,r)}};function re(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}}function N(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function j(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function $(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function oe(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function ce(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function he(c,e){const{range:t}=c,{startRow:i,startColumn:n}=t,r=e.getMergedCell(i,n);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var ae=Object.defineProperty,le=Object.getOwnPropertyDescriptor,O=(c,e,t,i)=>{for(var n=i>1?void 0:i?le(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&&ae(e,t,n),n},m=(c,e)=>(t,i)=>e(t,i,c);let x=class extends d.Disposable{constructor(e,t,i,n,r){super();f(this,"_provider");this._injector=e,this._findReplaceController=t,this._contextService=i,this._findReplaceService=n,this._commandService=r,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const e=this._injector.createInstance(H);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(v.filter(t=>!!t)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[V].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};x=O([d.OnLifecycle(d.LifecycleStages.Steady,x),m(0,C.Inject(C.Injector)),m(1,C.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],x);const de="sheets-find-replace-provider",ue=1e4;let W=class extends _.FindModel{constructor(e,t,i,n,r,s,o,h){super();f(this,"_matchesUpdate$",new v.Subject);f(this,"matchesUpdate$",this._matchesUpdate$.asObservable());f(this,"_activelyChangingMatch$",new v.Subject);f(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());f(this,"_matchesByWorksheet",new Map);f(this,"_matches",[]);f(this,"_matchesPosition",0);f(this,"_activeHighlightIndex",-1);f(this,"_highlightShapes",[]);f(this,"_currentHighlightShape",null);f(this,"_query",null);this._workbook=e,this._univerInstanceService=t,this._renderManagerService=i,this._commandService=n,this._contextService=r,this._themeService=s,this._sheetSkeletonManagerService=o,this._selectionManagerService=h}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(U.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),a=o.getSheetId(),{results:l}=h;return l.length?this._matchesByWorksheet.set(a,h.results):this._matchesByWorksheet.delete(a),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(d.fromCallback(this._commandService.onCommandExecuted).pipe(v.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(v.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),v.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._selectionManagerService.getSelections();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(),a=this._selectionManagerService.getSelections();return(u=a==null?void 0:a.some(S=>!he(S,h)))!=null?u:!1};let n,r=!0,s=!1;const o=()=>{const h=this._workbook.getActiveSheet(),a=this.currentMatch;s=i();const l=this._selectionManagerService.getSelections(),u=s?this._findInSelections(h,l,e,t):this._findInWorksheet(h,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(a,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(v.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(v.filter(([h])=>{if(h.type===d.CommandType.MUTATION&&h.params.unitId===this._workbook.getUnitId())return!0;if(h.id===w.SetSelectionsOperation.id&&h.params.unitId===t){const a=i();return a===!1&&s===!1?!1:(s=a,!0)}return!1})),this._workbook.activeSheet$.pipe(v.skip(1))).pipe(v.debounceTime(200)).subscribe(()=>o())),o(),n}_findInRange(e,t,i,n,r){const s=[],o=e.getSheetId(),h=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(;;){const{done:a,value:l}=h.next();if(a)break;const{row:u,col:S,colSpan:I,rowSpan:p,value:R}=l;if(r!=null&&r(u,S))continue;const{hit:M,replaceable:b,isFormula:y}=ge(e,u,S,t,R);if(M){const F={provider:de,unitId:n,replaceable:b,isFormula:y,range:{subUnitId:o,range:{startRow:u,startColumn:S,endColumn:S+(I!=null?I:1)-1,endRow:u+(p!=null?p:1)-1}}};s.push(F)}}return{results:s}}_findInSelections(e,t,i,n){const{findDirection:r}=i,s=r===_.FindDirection.ROW?j:$,o=new Set;return{results:t.map(a=>this._findInRange(e,i,a.range,n,(l,u)=>{const S=`${l}-${u}`;return o.has(S)?!0:(o.add(S),!1)}).results).flat().sort((a,l)=>s(a.range.range,l.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=>{t.dispose(),t.getScene().makeDirty()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var I,p;this._disposeHighlights();const e=(I=this._sheetSkeletonManagerService.getCurrent())==null?void 0:I.skeleton;if(!e||!this._getSheetObject()||((p=this._univerInstanceService.getFocusedUniverInstance())==null?void 0:p.getUnitId())!==this._workbook.getUnitId())return;const n=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(n);if(r==null)return;const{scene:s}=r,o=this._matches,h=this._themeService.getCurrentTheme().gold400,a=new d.ColorKit(h).toRgb(),l=this._workbook.getActiveSheet(),u=l.getSheetId(),S=o.filter(R=>R.range.subUnitId===u).map((R,M)=>{const{startColumn:b,startRow:y,endColumn:F,endRow:q}=R.range.range,Q=k.getCoordByCell(y,b,s,e),X=k.getCoordByCell(q,F,s,e),{startX:T,startY:A}=Q,{endX:Y,endY:z}=X,E=!l.getRowVisible(y),D=!l.getColVisible(b),G=E||D,K=D?2:Y-T,J=E?2:z-A,Z={left:T,top:A,color:a,width:K,height:J,evented:!1,inHiddenRange:G};return new se(`find-highlight-${M}`,Z)});s.addObjects(S,ue),this._highlightShapes=S,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const i=this._highlightShapes[e];if(!i)return;this._currentHighlightShape=i,i.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(w.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: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:a}=o.range.range;return h===n&&a===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findNextMatch(t,i);if(r){const[a,l]=r;return this._matchesPosition=l+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(u=>u===a):this._activeHighlightIndex=l,n||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findPreviousMatch(t,i);if(r){const[a,l]=r;return this._matchesPosition=l+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(u=>u===a):this._activeHighlightIndex=l,n||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const h=this._matches.length,a=(o+h)%h;return[this._matches[a],a]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,i[0].range);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(n,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(l=>l===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,h=this._matches.length;if(!e&&o>=h)return null;const a=o%h;return[this._matches[a],a]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,i[0].range,t);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(n,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(o=>o===e),s=(t?d.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?d.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===_.FindDirection.ROW;let r=e.findIndex(o=>{const h=o.range.range;if(!(n?j(t,h):$(t,h)))return!1;const l=N(t,h);return i?l:!l});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===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const h=o.range.range;if(!(n?oe(t,h):ce(t,h)))return!1;const l=N(t,h);return i?l:!l});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===_.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(w.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===_.FindBy.FORMULA,o=n?"g":"ig",h=[];return d.groupBy(this._matches.filter(l=>l.replaceable),l=>l.range.subUnitId).forEach((l,u)=>{const S=new d.ObjectMatrix,I=this._workbook.getSheetBySheetId(u);l.forEach(p=>{const{startColumn:R,startRow:M}=p.range.range,b=this._getReplacedCellData(p,I,s,i,e,o);b&&S.setValue(M,R,b)}),h.push({count:l.length,subUnitId:u,value:S.getMatrix()})}),h?this._commandService.executeCommand(V.id,{unitId:t,replacements:h}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,s){var I;const o=e.range.range,{startRow:h,startColumn:a}=o,l=t.getCellRaw(h,a);if(e.isFormula)return i?{f:l.f.replace(new RegExp(L(n),s),r),v:null}:null;if(!!((I=l.p)!=null&&I.body)){const p=d.Tools.deepClone(l.p);return d.replaceInDocumentBody(p.body,n,r),{p}}return{v:l.v.toString().replace(new RegExp(L(n),s),r)}}};W=O([m(1,d.IUniverInstanceService),m(2,U.IRenderManagerService),m(3,d.ICommandService),m(4,d.IContextService),m(5,C.Inject(d.ThemeService)),m(6,C.Inject(k.SheetSkeletonManagerService)),m(7,C.Inject(w.SelectionManagerService))],W);function L(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let H=class extends d.Disposable{constructor(e,t){super();f(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=t}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUniverSheetInstance();if(t){const i=this._injector.createInstance(W,t);this._findModelsByUnitId.set(t.getUnitId(),i);const n=this._preprocessQuery(e);return i.start(n),[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}}};H=O([m(0,d.IUniverInstanceService),m(1,C.Inject(C.Injector))],H);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ge(c,e,t,i,n){const{findBy:r}=i,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,P(n,i)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?P({v:o.f},i)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,P(n,i)?g.hit=!0:g.hit=!1,g))}function P(c,e){let t=fe(c);return t?e.matchesTheWholeCell?(t=_e(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 fe(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 _e(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var Se=Object.defineProperty,me=Object.getOwnPropertyDescriptor,pe=(c,e,t,i)=>{for(var n=i>1?void 0:i?me(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&&Se(e,t,n),n},Ie=(c,e)=>(t,i)=>e(t,i,c);const ve="UNIVER_SHEETS_FIND_REPLACE_PLUGIN";var B;exports.UniverSheetsFindReplacePlugin=(B=class extends d.Plugin{constructor(e,t){super(ve),this._injector=t}onStarting(e){[[x]].forEach(t=>{e.add(t)})}},f(B,"type",d.PluginType.Sheet),B);exports.UniverSheetsFindReplacePlugin=pe([Ie(1,C.Inject(C.Injector))],exports.UniverSheetsFindReplacePlugin);exports.enUS=ie;exports.zhCN=ne;
|
|
1
|
+
"use strict";var te=Object.defineProperty;var ie=(c,e,t)=>e in c?te(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var f=(c,e,t)=>(ie(c,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@univerjs/core"),I=require("@wendellhu/redi"),_=require("@univerjs/find-replace"),F=require("@univerjs/engine-render"),w=require("@univerjs/sheets"),k=require("@univerjs/sheets-ui"),C=require("rxjs"),ne={"sheet-find-replace":{replace:{"partial-failure":"Failed to replace some cells",failure:"Failed to replace all cells"}}},se={"sheet-find-replace":{replace:{"partial-failure":"部分单元格替换失败",failure:"替换失败"}}};class re extends F.Shape{constructor(t,i){super(t,i);f(this,"_activated",!1);f(this,"_inHiddenRange",!1);f(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})`;F.Rect.drawWith(t,{width:this.width,height:this.height,fill:n,stroke:i?r:void 0,strokeWidth:i?2:0,evented:!1})}}const V={id:"sheet.command.replace",type:d.CommandType.COMMAND,handler:async(c,e)=>{const t=c.get(d.IUndoRedoService),i=c.get(d.ICommandService),{unitId:n,replacements:r}=e,s=t.__tempBatchingUndoRedo(n),o=await Promise.all(r.map(h=>i.executeCommand(w.SetRangeValuesCommand.id,{unitId:n,subUnitId:h.subUnitId,value:h.value})));return s.dispose(),oe(o,r)}};function oe(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}}function D(c,e){return c.startRow===e.startRow&&c.startColumn===e.startColumn}function N(c,e){return c.startRow<e.startRow||c.startRow===e.startRow&&c.startColumn<=e.startColumn}function j(c,e){return c.startColumn<e.startColumn||c.startColumn===e.startColumn&&c.startRow<=e.startRow}function ce(c,e){return c.startRow>e.startRow||c.startRow===e.startRow&&c.startColumn>=e.startColumn}function he(c,e){return c.startColumn>e.startColumn||c.startColumn===e.startColumn&&c.startRow>=e.startRow}function ae(c,e){const{range:t}=c,{startRow:i,startColumn:n}=t,r=e.getMergedCell(i,n);return r?d.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var le=Object.defineProperty,de=Object.getOwnPropertyDescriptor,H=(c,e,t,i)=>{for(var n=i>1?void 0:i?de(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&&le(e,t,n),n},m=(c,e)=>(t,i)=>e(t,i,c);let U=class extends d.Disposable{constructor(e,t,i,n,r){super();f(this,"_provider");this._injector=e,this._findReplaceController=t,this._contextService=i,this._findReplaceService=n,this._commandService=r,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const e=this._injector.createInstance(W);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(d.EDITOR_ACTIVATED).pipe(C.filter(t=>!!t)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[V].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};U=H([d.OnLifecycle(d.LifecycleStages.Steady,U),m(0,I.Inject(I.Injector)),m(1,I.Inject(_.FindReplaceController)),m(2,d.IContextService),m(3,_.IFindReplaceService),m(4,d.ICommandService)],U);const ue="sheets-find-replace-provider",ge=1e4;let B=class extends _.FindModel{constructor(e,t,i,n,r,s,o,h){super();f(this,"_matchesUpdate$",new C.Subject);f(this,"matchesUpdate$",this._matchesUpdate$.asObservable());f(this,"_activelyChangingMatch$",new C.Subject);f(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());f(this,"_matchesByWorksheet",new Map);f(this,"_matches",[]);f(this,"_matchesPosition",0);f(this,"_activeHighlightIndex",-1);f(this,"_highlightShapes",[]);f(this,"_currentHighlightShape",null);f(this,"_query",null);this._workbook=e,this._univerInstanceService=t,this._renderManagerService=i,this._commandService=n,this._contextService=r,this._themeService=s,this._sheetSkeletonManagerService=o,this._selectionManagerService=h}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===_.FindBy.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case _.FindScope.UNIT:this.findInWorkbook(e);break;case _.FindScope.SUBUNIT:default:this.findInActiveWorksheet(e);break}}_toggleDisplayRawFormula(e){this._contextService.setContextValue(F.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),l=o.getSheetId(),{results:a}=h;return a.length?this._matchesByWorksheet.set(l,h.results):this._matchesByWorksheet.delete(l),h});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),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(d.fromCallback(this._commandService.onCommandExecuted).pipe(C.filter(([s,o])=>s.id===w.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(d.fromCallback(this._commandService.onCommandExecuted).pipe(C.filter(([s])=>s.type===d.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),C.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._selectionManagerService.getSelections();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(),l=this._selectionManagerService.getSelections();return(u=l==null?void 0:l.some(S=>!ae(S,h)))!=null?u:!1};let n,r=!0,s=!1;const o=()=>{const h=this._workbook.getActiveSheet(),l=this.currentMatch;s=i();const a=this._selectionManagerService.getSelections(),u=s?this._findInSelections(h,a,e,t):this._findInWorksheet(h,e,t);return this._matches=u.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(n=u,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),u};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(C.merge(d.fromCallback(this._commandService.onCommandExecuted).pipe(C.filter(([h])=>{if(h.type===d.CommandType.MUTATION&&h.params.unitId===this._workbook.getUnitId())return!0;if(h.id===w.SetSelectionsOperation.id&&h.params.unitId===t){const l=i();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(C.skip(1))).pipe(C.debounceTime(200)).subscribe(()=>o())),o(),n}_findInRange(e,t,i,n,r){const s=[],o=e.getSheetId(),h=(t.findDirection===_.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(const l of h){const{row:a,col:u,colSpan:S,rowSpan:v,value:p}=l;if(r!=null&&r(a,u)||!p||e.getRowFiltered(a))continue;const{hit:R,replaceable:M,isFormula:b}=fe(e,a,u,t,p);if(R){const y={provider:ue,unitId:n,replaceable:M,isFormula:b,range:{subUnitId:o,range:{startRow:a,startColumn:u,endColumn:u+(S!=null?S:1)-1,endRow:a+(v!=null?v:1)-1}}};s.push(y)}}return{results:s}}_findInSelections(e,t,i,n){const{findDirection:r}=i,s=r===_.FindDirection.ROW?N:j,o=new Set;return{results:t.map(l=>this._findInRange(e,i,l.range,n,(a,u)=>{const S=`${a}-${u}`;return o.has(S)?!0:(o.add(S),!1)}).results).flat().sort((l,a)=>s(l.range.range,a.range.range)?-1:1)}}_findInWorksheet(e,t,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=>{t.dispose(),t.getScene().makeDirty()}),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 n=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(n);if(r==null)return;const{scene:s}=r,o=this._matches,h=this._themeService.getCurrentTheme().gold400,l=new d.ColorKit(h).toRgb(),a=this._workbook.getActiveSheet(),u=a.getSheetId(),S=o.filter(R=>R.range.subUnitId===u).map((R,M)=>{const{startColumn:b,startRow:y,endColumn:q,endRow:Q}=R.range.range,X=k.getCoordByCell(y,b,s,e),Y=k.getCoordByCell(Q,q,s,e),{startX:T,startY:O}=X,{endX:z,endY:G}=Y,E=!a.getRowRawVisible(y),A=!a.getColVisible(b),K=E||A,J=A?2:z-T,Z=E?2:G-O,ee={left:T,top:O,color:l,width:J,height:Z,evented:!1,inHiddenRange:K};return new re(`find-highlight-${M}`,ee)});s.addObjects(S,ge),this._highlightShapes=S,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const i=this._highlightShapes[e];if(!i)return;this._currentHighlightShape=i,i.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(w.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: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:l}=o.range.range;return h===n&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findNextMatch(t,i);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(u=>u===l):this._activeHighlightIndex=a,n||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,h;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(h=e==null?void 0:e.noFocus)!=null?h:!1,r=this._findPreviousMatch(t,i);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===_.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(u=>u===l):this._activeHighlightIndex=a,n||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const h=this._matches.length,l=(o+h)%h;return[this._matches[l],l]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,i[0].range);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(n,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,h=this._matches.length;if(!e&&o>=h)return null;const l=o%h;return[this._matches[l],l]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==_.FindScope.UNIT)return this._findNextMatchByRange(this._matches,i[0].range,t);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(n,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(o=>o===e),s=(t?d.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?d.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===_.FindDirection.ROW;let r=e.findIndex(o=>{const h=o.range.range;if(!(n?N(t,h):j(t,h)))return!1;const a=D(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===_.FindDirection.ROW;let r=this._matches.findLastIndex(o=>{const h=o.range.range;if(!(n?ce(t,h):he(t,h)))return!1;const a=D(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===_.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(w.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===_.FindBy.FORMULA,o=n?"g":"ig",h=[];return d.groupBy(this._matches.filter(a=>a.replaceable),a=>a.range.subUnitId).forEach((a,u)=>{const S=new d.ObjectMatrix,v=this._workbook.getSheetBySheetId(u);a.forEach(p=>{const{startColumn:R,startRow:M}=p.range.range,b=this._getReplacedCellData(p,v,s,i,e,o);b&&S.setValue(M,R,b)}),h.push({count:a.length,subUnitId:u,value:S.getMatrix()})}),h?this._commandService.executeCommand(V.id,{unitId:t,replacements:h}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,s){var v;const o=e.range.range,{startRow:h,startColumn:l}=o,a=t.getCellRaw(h,l);if(e.isFormula)return i?{f:a.f.replace(new RegExp($(n),s),r),v:null}:null;if(!!((v=a.p)!=null&&v.body)){const p=d.Tools.deepClone(a.p);return d.replaceInDocumentBody(p.body,n,r),{p}}return{v:a.v.toString().replace(new RegExp($(n),s),r)}}};B=H([m(1,d.IUniverInstanceService),m(2,F.IRenderManagerService),m(3,d.ICommandService),m(4,d.IContextService),m(5,I.Inject(d.ThemeService)),m(6,I.Inject(k.SheetSkeletonManagerService)),m(7,I.Inject(w.SelectionManagerService))],B);function $(c){return c.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let W=class extends d.Disposable{constructor(e,t){super();f(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=t}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(d.UniverInstanceType.UNIVER_SHEET);if(t){const i=this._injector.createInstance(B,t);this._findModelsByUnitId.set(t.getUnitId(),i);const n=this._preprocessQuery(e);return i.start(n),[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}}};W=H([m(0,d.IUniverInstanceService),m(1,I.Inject(I.Injector))],W);const g={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function fe(c,e,t,i,n){const{findBy:r}=i,s=r===_.FindBy.FORMULA,o=c.getCellRaw(e,t);return g.rawData=o,!(o!=null&&o.f)?(g.isFormula=!1,P(n,i)?o?(g.hit=!0,g.replaceable=!0):(g.hit=!0,g.replaceable=!1):(g.hit=!1,g.replaceable=!1),g):(g.isFormula=!0,s?P({v:o.f},i)?(g.hit=!0,g.replaceable=!0,g):(g.hit=!1,g.replaceable=!1,g):(g.replaceable=!1,P(n,i)?g.hit=!0:g.hit=!1,g))}function P(c,e){let t=_e(c);return t?e.matchesTheWholeCell?(t=Se(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 _e(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 Se(c){return c.replace(/^ +/g,"").replace(/ +$/g,"")}var me=Object.defineProperty,pe=Object.getOwnPropertyDescriptor,ve=(c,e,t,i)=>{for(var n=i>1?void 0:i?pe(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&&me(e,t,n),n},L=(c,e)=>(t,i)=>e(t,i,c);const Ie="UNIVER_SHEETS_FIND_REPLACE_PLUGIN";var x;exports.UniverSheetsFindReplacePlugin=(x=class extends d.Plugin{constructor(e,t,i){super(),this._injector=t,this._pluginService=i,this._pluginService.registerPlugin(_.UniverFindReplacePlugin)}onStarting(e){[[U]].forEach(t=>e.add(t))}},f(x,"pluginName",Ie),f(x,"type",d.UniverInstanceType.UNIVER_SHEET),x);exports.UniverSheetsFindReplacePlugin=ve([L(1,I.Inject(I.Injector)),L(2,I.Inject(d.PluginService))],exports.UniverSheetsFindReplacePlugin);exports.enUS=ne;exports.zhCN=se;
|
package/lib/es/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var g = (h, e, t) => (
|
|
4
|
-
import { CommandType as
|
|
5
|
-
import { Inject as
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { SetRangeValuesCommand as
|
|
9
|
-
import { SheetSkeletonManagerService as
|
|
10
|
-
import { filter as
|
|
11
|
-
const
|
|
1
|
+
var ue = Object.defineProperty;
|
|
2
|
+
var ge = (h, e, t) => e in h ? ue(h, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : h[e] = t;
|
|
3
|
+
var g = (h, e, t) => (ge(h, typeof e != "symbol" ? e + "" : e, t), t);
|
|
4
|
+
import { CommandType as B, IUndoRedoService as fe, ICommandService as H, Rectangle as _e, OnLifecycle as me, LifecycleStages as Se, ThemeService as pe, Disposable as z, EDITOR_ACTIVATED as ve, IContextService as J, IUniverInstanceService as Z, UniverInstanceType as q, fromCallback as U, ColorKit as Ie, rotate as $, groupBy as Ce, ObjectMatrix as Re, Tools as we, replaceInDocumentBody as be, Plugin as Me, PluginService as ke } from "@univerjs/core";
|
|
5
|
+
import { Inject as p, Injector as O } from "@wendellhu/redi";
|
|
6
|
+
import { FindReplaceController as ye, IFindReplaceService as xe, FindModel as Ue, FindBy as k, FindScope as C, FindDirection as b, UniverFindReplacePlugin as Pe } from "@univerjs/find-replace";
|
|
7
|
+
import { Shape as Be, Rect as Fe, IRenderManagerService as We, RENDER_RAW_FORMULA_KEY as He } from "@univerjs/engine-render";
|
|
8
|
+
import { SetRangeValuesCommand as ee, SelectionManagerService as Oe, SetWorksheetActiveOperation as Te, SetSelectionsOperation as Ee, SetWorksheetActivateCommand as Ae } from "@univerjs/sheets";
|
|
9
|
+
import { SheetSkeletonManagerService as Ne, getCoordByCell as L, getSheetObject as De, ScrollToCellCommand as $e } from "@univerjs/sheets-ui";
|
|
10
|
+
import { filter as y, Subject as j, throttleTime as Le, merge as je, skip as Ve, debounceTime as Qe } from "rxjs";
|
|
11
|
+
const mt = {
|
|
12
12
|
"sheet-find-replace": {
|
|
13
13
|
replace: {
|
|
14
14
|
"partial-failure": "Failed to replace some cells",
|
|
15
15
|
failure: "Failed to replace all cells"
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
},
|
|
18
|
+
}, St = {
|
|
19
19
|
"sheet-find-replace": {
|
|
20
20
|
replace: {
|
|
21
21
|
"partial-failure": "部分单元格替换失败",
|
|
@@ -23,7 +23,7 @@ const ft = {
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
class
|
|
26
|
+
class Xe extends Be {
|
|
27
27
|
constructor(t, i) {
|
|
28
28
|
super(t, i);
|
|
29
29
|
g(this, "_activated", !1);
|
|
@@ -39,7 +39,7 @@ class je extends Me {
|
|
|
39
39
|
}
|
|
40
40
|
_draw(t) {
|
|
41
41
|
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})`;
|
|
42
|
-
|
|
42
|
+
Fe.drawWith(t, {
|
|
43
43
|
width: this.width,
|
|
44
44
|
height: this.height,
|
|
45
45
|
fill: n,
|
|
@@ -49,48 +49,48 @@ class je extends Me {
|
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
const
|
|
52
|
+
const te = {
|
|
53
53
|
id: "sheet.command.replace",
|
|
54
|
-
type:
|
|
54
|
+
type: B.COMMAND,
|
|
55
55
|
handler: async (h, e) => {
|
|
56
|
-
const t = h.get(
|
|
56
|
+
const t = h.get(fe), i = h.get(H), { unitId: n, replacements: r } = e, s = t.__tempBatchingUndoRedo(n), o = await Promise.all(r.map((c) => i.executeCommand(ee.id, {
|
|
57
57
|
unitId: n,
|
|
58
58
|
subUnitId: c.subUnitId,
|
|
59
59
|
value: c.value
|
|
60
60
|
})));
|
|
61
|
-
return s.dispose(),
|
|
61
|
+
return s.dispose(), Ye(o, r);
|
|
62
62
|
}
|
|
63
63
|
};
|
|
64
|
-
function
|
|
64
|
+
function Ye(h, e) {
|
|
65
65
|
let t = 0, i = 0;
|
|
66
66
|
return h.forEach((n, r) => {
|
|
67
67
|
const s = e[r].count;
|
|
68
68
|
n ? t += s : i += s;
|
|
69
69
|
}), { success: t, failure: i };
|
|
70
70
|
}
|
|
71
|
-
function
|
|
71
|
+
function V(h, e) {
|
|
72
72
|
return h.startRow === e.startRow && h.startColumn === e.startColumn;
|
|
73
73
|
}
|
|
74
|
-
function
|
|
74
|
+
function Q(h, e) {
|
|
75
75
|
return h.startRow < e.startRow || h.startRow === e.startRow && h.startColumn <= e.startColumn;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
77
|
+
function X(h, e) {
|
|
78
78
|
return h.startColumn < e.startColumn || h.startColumn === e.startColumn && h.startRow <= e.startRow;
|
|
79
79
|
}
|
|
80
|
-
function
|
|
80
|
+
function Ge(h, e) {
|
|
81
81
|
return h.startRow > e.startRow || h.startRow === e.startRow && h.startColumn >= e.startColumn;
|
|
82
82
|
}
|
|
83
|
-
function
|
|
83
|
+
function Ke(h, e) {
|
|
84
84
|
return h.startColumn > e.startColumn || h.startColumn === e.startColumn && h.startRow >= e.startRow;
|
|
85
85
|
}
|
|
86
|
-
function
|
|
86
|
+
function ze(h, e) {
|
|
87
87
|
const { range: t } = h, { startRow: i, startColumn: n } = t, r = e.getMergedCell(i, n);
|
|
88
|
-
return r ?
|
|
88
|
+
return r ? _e.equals(t, r) : t.endRow === t.startRow && t.endColumn === t.startColumn;
|
|
89
89
|
}
|
|
90
|
-
var
|
|
91
|
-
for (var n = i > 1 ? void 0 : i ?
|
|
90
|
+
var Je = Object.defineProperty, Ze = Object.getOwnPropertyDescriptor, T = (h, e, t, i) => {
|
|
91
|
+
for (var n = i > 1 ? void 0 : i ? Ze(e, t) : e, r = h.length - 1, s; r >= 0; r--)
|
|
92
92
|
(s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
|
|
93
|
-
return i && n &&
|
|
93
|
+
return i && n && Je(e, t, n), n;
|
|
94
94
|
}, _ = (h, e) => (t, i) => e(t, i, h);
|
|
95
95
|
let x = class extends z {
|
|
96
96
|
constructor(e, t, i, n, r) {
|
|
@@ -102,31 +102,31 @@ let x = class extends z {
|
|
|
102
102
|
super.dispose(), this._provider.dispose();
|
|
103
103
|
}
|
|
104
104
|
_init() {
|
|
105
|
-
const e = this._injector.createInstance(
|
|
105
|
+
const e = this._injector.createInstance(W);
|
|
106
106
|
this._provider = e, this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)), this.disposeWithMe(
|
|
107
|
-
this._contextService.subscribeContextValue$(
|
|
107
|
+
this._contextService.subscribeContextValue$(ve).pipe(y((t) => !!t)).subscribe(() => this._findReplaceController.closePanel())
|
|
108
108
|
);
|
|
109
109
|
}
|
|
110
110
|
_initCommands() {
|
|
111
|
-
[
|
|
111
|
+
[te].forEach((e) => this.disposeWithMe(this._commandService.registerCommand(e)));
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
|
-
x =
|
|
115
|
-
|
|
116
|
-
_(0,
|
|
117
|
-
_(1,
|
|
114
|
+
x = T([
|
|
115
|
+
me(Se.Steady, x),
|
|
116
|
+
_(0, p(O)),
|
|
117
|
+
_(1, p(ye)),
|
|
118
118
|
_(2, J),
|
|
119
|
-
_(3,
|
|
120
|
-
_(4,
|
|
119
|
+
_(3, xe),
|
|
120
|
+
_(4, H)
|
|
121
121
|
], x);
|
|
122
|
-
const
|
|
123
|
-
let F = class extends
|
|
122
|
+
const qe = "sheets-find-replace-provider", et = 1e4;
|
|
123
|
+
let F = class extends Ue {
|
|
124
124
|
constructor(e, t, i, n, r, s, o, c) {
|
|
125
125
|
super();
|
|
126
126
|
// We can directly inject the `FindReplaceService` here, and call its methods instead of using the observables.
|
|
127
|
-
g(this, "_matchesUpdate$", new
|
|
127
|
+
g(this, "_matchesUpdate$", new j());
|
|
128
128
|
g(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
|
|
129
|
-
g(this, "_activelyChangingMatch$", new
|
|
129
|
+
g(this, "_activelyChangingMatch$", new j());
|
|
130
130
|
g(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
|
|
131
131
|
/** Hold matches by the worksheet they are in. Make it easier to track the next (or previous) match when searching in the whole workbook. */
|
|
132
132
|
g(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
|
|
@@ -163,7 +163,7 @@ let F = class extends Be {
|
|
|
163
163
|
return this._matches;
|
|
164
164
|
}
|
|
165
165
|
start(e) {
|
|
166
|
-
switch (this._query = e, e.findBy ===
|
|
166
|
+
switch (this._query = e, e.findBy === k.FORMULA ? this._toggleDisplayRawFormula(!0) : this._toggleDisplayRawFormula(!1), e.findScope) {
|
|
167
167
|
case C.UNIT:
|
|
168
168
|
this.findInWorkbook(e);
|
|
169
169
|
break;
|
|
@@ -174,7 +174,7 @@ let F = class extends Be {
|
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
_toggleDisplayRawFormula(e) {
|
|
177
|
-
this._contextService.setContextValue(
|
|
177
|
+
this._contextService.setContextValue(He, e);
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
180
|
* Find all matches in the current workbook no matter which worksheet is activated.
|
|
@@ -186,8 +186,8 @@ let F = class extends Be {
|
|
|
186
186
|
let i, n = !0;
|
|
187
187
|
const r = () => {
|
|
188
188
|
const s = this._workbook.getSheets().filter((o) => !o.isSheetHidden()).map((o) => {
|
|
189
|
-
const c = this._findInWorksheet(o, e, t),
|
|
190
|
-
return
|
|
189
|
+
const c = this._findInWorksheet(o, e, t), l = o.getSheetId(), { results: a } = c;
|
|
190
|
+
return a.length ? this._matchesByWorksheet.set(l, c.results) : this._matchesByWorksheet.delete(l), c;
|
|
191
191
|
});
|
|
192
192
|
this._matches = s.map((o) => o.results).flat(), this._updateFindHighlight(), n ? (i = { results: this._matches }, n = !1) : this._matchesUpdate$.next(this._matches);
|
|
193
193
|
};
|
|
@@ -195,17 +195,17 @@ let F = class extends Be {
|
|
|
195
195
|
this._updateFindHighlight(), this._updateCurrentHighlightShape(this._activeHighlightIndex);
|
|
196
196
|
})), this.disposeWithMe(
|
|
197
197
|
U(this._commandService.onCommandExecuted).pipe(
|
|
198
|
-
|
|
198
|
+
y(([s, o]) => s.id === Te.id && !(o != null && o.fromFindReplace))
|
|
199
199
|
).subscribe(() => {
|
|
200
200
|
const s = this._workbook.getActiveSheet(), o = s.getSheetId();
|
|
201
201
|
this._matchesByWorksheet.has(o) && this._findNextMatchOnActiveSheetChange(s);
|
|
202
202
|
})
|
|
203
203
|
), this.disposeWithMe(
|
|
204
204
|
U(this._commandService.onCommandExecuted).pipe(
|
|
205
|
-
|
|
206
|
-
([s]) => s.type ===
|
|
205
|
+
y(
|
|
206
|
+
([s]) => s.type === B.MUTATION && s.params.unitId === this._workbook.getUnitId()
|
|
207
207
|
),
|
|
208
|
-
|
|
208
|
+
Le(600, void 0, { leading: !1, trailing: !0 })
|
|
209
209
|
).subscribe(() => r())
|
|
210
210
|
), r(), i;
|
|
211
211
|
}
|
|
@@ -226,77 +226,74 @@ let F = class extends Be {
|
|
|
226
226
|
findInActiveWorksheet(e) {
|
|
227
227
|
const t = this._workbook.getUnitId(), i = () => {
|
|
228
228
|
var d;
|
|
229
|
-
const c = this._workbook.getActiveSheet(),
|
|
230
|
-
return (d =
|
|
229
|
+
const c = this._workbook.getActiveSheet(), l = this._selectionManagerService.getSelections();
|
|
230
|
+
return (d = l == null ? void 0 : l.some((f) => !ze(f, c))) != null ? d : !1;
|
|
231
231
|
};
|
|
232
232
|
let n, r = !0, s = !1;
|
|
233
233
|
const o = () => {
|
|
234
|
-
const c = this._workbook.getActiveSheet(),
|
|
234
|
+
const c = this._workbook.getActiveSheet(), l = this.currentMatch;
|
|
235
235
|
s = i();
|
|
236
|
-
const
|
|
237
|
-
return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(
|
|
236
|
+
const a = this._selectionManagerService.getSelections(), d = s ? this._findInSelections(c, a, e, t) : this._findInWorksheet(c, e, t);
|
|
237
|
+
return this._matches = d.results, this._matchesPosition = this._tryRestoreLastMatchesPosition(l, this._matches), r ? (n = d, r = !1) : this._matchesUpdate$.next(this._matches), this._updateFindHighlight(), d;
|
|
238
238
|
};
|
|
239
239
|
return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight())), this.disposeWithMe(
|
|
240
|
-
|
|
240
|
+
je(
|
|
241
241
|
U(this._commandService.onCommandExecuted).pipe(
|
|
242
|
-
|
|
243
|
-
if (c.type ===
|
|
242
|
+
y(([c]) => {
|
|
243
|
+
if (c.type === B.MUTATION && c.params.unitId === this._workbook.getUnitId())
|
|
244
244
|
return !0;
|
|
245
|
-
if (c.id ===
|
|
246
|
-
const
|
|
247
|
-
return
|
|
245
|
+
if (c.id === Ee.id && c.params.unitId === t) {
|
|
246
|
+
const l = i();
|
|
247
|
+
return l === !1 && s === !1 ? !1 : (s = l, !0);
|
|
248
248
|
}
|
|
249
249
|
return !1;
|
|
250
250
|
})
|
|
251
251
|
),
|
|
252
252
|
// activeSheet$ is a BehaviorSubject, so we need to skip the first
|
|
253
|
-
this._workbook.activeSheet$.pipe(
|
|
254
|
-
).pipe(
|
|
253
|
+
this._workbook.activeSheet$.pipe(Ve(1))
|
|
254
|
+
).pipe(Qe(200)).subscribe(() => o())
|
|
255
255
|
), o(), n;
|
|
256
256
|
}
|
|
257
257
|
_findInRange(e, t, i, n, r) {
|
|
258
258
|
const s = [], o = e.getSheetId(), c = (t.findDirection === b.COLUMN ? e.iterateByColumn : e.iterateByRow).bind(e)(i);
|
|
259
|
-
for (
|
|
260
|
-
const {
|
|
261
|
-
if (a)
|
|
262
|
-
break;
|
|
263
|
-
const { row: d, col: f, colSpan: S, rowSpan: m, value: p } = l;
|
|
264
|
-
if (r != null && r(d, f))
|
|
259
|
+
for (const l of c) {
|
|
260
|
+
const { row: a, col: d, colSpan: f, rowSpan: S, value: m } = l;
|
|
261
|
+
if (r != null && r(a, d) || !m || e.getRowFiltered(a))
|
|
265
262
|
continue;
|
|
266
|
-
const { hit:
|
|
267
|
-
if (
|
|
268
|
-
const
|
|
269
|
-
provider:
|
|
263
|
+
const { hit: v, replaceable: R, isFormula: I } = tt(e, a, d, t, m);
|
|
264
|
+
if (v) {
|
|
265
|
+
const w = {
|
|
266
|
+
provider: qe,
|
|
270
267
|
unitId: n,
|
|
271
|
-
replaceable:
|
|
272
|
-
isFormula:
|
|
268
|
+
replaceable: R,
|
|
269
|
+
isFormula: I,
|
|
273
270
|
range: {
|
|
274
271
|
subUnitId: o,
|
|
275
272
|
range: {
|
|
276
|
-
startRow:
|
|
277
|
-
startColumn:
|
|
278
|
-
endColumn:
|
|
279
|
-
endRow:
|
|
273
|
+
startRow: a,
|
|
274
|
+
startColumn: d,
|
|
275
|
+
endColumn: d + (f != null ? f : 1) - 1,
|
|
276
|
+
endRow: a + (S != null ? S : 1) - 1
|
|
280
277
|
}
|
|
281
278
|
}
|
|
282
279
|
};
|
|
283
|
-
s.push(
|
|
280
|
+
s.push(w);
|
|
284
281
|
}
|
|
285
282
|
}
|
|
286
283
|
return { results: s };
|
|
287
284
|
}
|
|
288
285
|
_findInSelections(e, t, i, n) {
|
|
289
|
-
const { findDirection: r } = i, s = r === b.ROW ?
|
|
290
|
-
return { results: t.map((
|
|
286
|
+
const { findDirection: r } = i, s = r === b.ROW ? Q : X, o = /* @__PURE__ */ new Set();
|
|
287
|
+
return { results: t.map((l) => this._findInRange(
|
|
291
288
|
e,
|
|
292
289
|
i,
|
|
293
|
-
|
|
290
|
+
l.range,
|
|
294
291
|
n,
|
|
295
|
-
(
|
|
296
|
-
const f = `${
|
|
292
|
+
(a, d) => {
|
|
293
|
+
const f = `${a}-${d}`;
|
|
297
294
|
return o.has(f) ? !0 : (o.add(f), !1);
|
|
298
295
|
}
|
|
299
|
-
).results).flat().sort((
|
|
296
|
+
).results).flat().sort((l, a) => s(l.range.range, a.range.range) ? -1 : 1) };
|
|
300
297
|
}
|
|
301
298
|
/** Find matches in a given worksheet. */
|
|
302
299
|
_findInWorksheet(e, t, i) {
|
|
@@ -313,24 +310,24 @@ let F = class extends Be {
|
|
|
313
310
|
var S, m;
|
|
314
311
|
this._disposeHighlights();
|
|
315
312
|
const e = (S = this._sheetSkeletonManagerService.getCurrent()) == null ? void 0 : S.skeleton;
|
|
316
|
-
if (!e || !this._getSheetObject() || ((m = this._univerInstanceService.
|
|
313
|
+
if (!e || !this._getSheetObject() || ((m = this._univerInstanceService.getFocusedUnit()) == null ? void 0 : m.getUnitId()) !== this._workbook.getUnitId())
|
|
317
314
|
return;
|
|
318
315
|
const n = this._workbook.getUnitId(), r = this._renderManagerService.getRenderById(n);
|
|
319
316
|
if (r == null)
|
|
320
317
|
return;
|
|
321
|
-
const { scene: s } = r, o = this._matches, c = this._themeService.getCurrentTheme().gold400,
|
|
322
|
-
const { startColumn:
|
|
318
|
+
const { scene: s } = r, o = this._matches, c = this._themeService.getCurrentTheme().gold400, l = new Ie(c).toRgb(), a = this._workbook.getActiveSheet(), d = a.getSheetId(), f = o.filter((v) => v.range.subUnitId === d).map((v, R) => {
|
|
319
|
+
const { startColumn: I, startRow: w, endColumn: ie, endRow: ne } = v.range.range, se = L(w, I, s, e), re = L(ne, ie, s, e), { startX: E, startY: A } = se, { endX: oe, endY: he } = re, N = !a.getRowRawVisible(w), D = !a.getColVisible(I), ce = N || D, ae = D ? 2 : oe - E, le = N ? 2 : he - A, de = {
|
|
323
320
|
left: E,
|
|
324
|
-
top:
|
|
325
|
-
color:
|
|
326
|
-
width:
|
|
327
|
-
height:
|
|
321
|
+
top: A,
|
|
322
|
+
color: l,
|
|
323
|
+
width: ae,
|
|
324
|
+
height: le,
|
|
328
325
|
evented: !1,
|
|
329
|
-
inHiddenRange:
|
|
326
|
+
inHiddenRange: ce
|
|
330
327
|
};
|
|
331
|
-
return new
|
|
328
|
+
return new Xe(`find-highlight-${R}`, de);
|
|
332
329
|
});
|
|
333
|
-
s.addObjects(f,
|
|
330
|
+
s.addObjects(f, et), this._highlightShapes = f, s.makeDirty();
|
|
334
331
|
}
|
|
335
332
|
_updateCurrentHighlightShape(e) {
|
|
336
333
|
var t;
|
|
@@ -342,16 +339,16 @@ let F = class extends Be {
|
|
|
342
339
|
}
|
|
343
340
|
}
|
|
344
341
|
_getSheetObject() {
|
|
345
|
-
return
|
|
342
|
+
return De(this._univerInstanceService, this._renderManagerService);
|
|
346
343
|
}
|
|
347
344
|
_focusMatch(e) {
|
|
348
345
|
const t = e.range.subUnitId;
|
|
349
346
|
t !== this._workbook.getActiveSheet().getSheetId() && this._commandService.syncExecuteCommand(
|
|
350
|
-
|
|
347
|
+
Ae.id,
|
|
351
348
|
{ unitId: this._workbook.getUnitId(), subUnitId: t },
|
|
352
349
|
{ fromFindReplace: !0 }
|
|
353
350
|
), this._commandService.syncExecuteCommand(
|
|
354
|
-
|
|
351
|
+
$e.id,
|
|
355
352
|
{ range: e.range.range },
|
|
356
353
|
{ fromFindReplace: !0 }
|
|
357
354
|
);
|
|
@@ -362,8 +359,8 @@ let F = class extends Be {
|
|
|
362
359
|
const { subUnitId: i } = e.range, { startColumn: n, startRow: r } = e.range.range, s = t.findIndex((o) => {
|
|
363
360
|
if (i !== o.range.subUnitId)
|
|
364
361
|
return !1;
|
|
365
|
-
const { startColumn: c, startRow:
|
|
366
|
-
return c === n &&
|
|
362
|
+
const { startColumn: c, startRow: l } = o.range.range;
|
|
363
|
+
return c === n && l === r;
|
|
367
364
|
});
|
|
368
365
|
return s > -1 ? s + 1 : 0;
|
|
369
366
|
}
|
|
@@ -373,8 +370,8 @@ let F = class extends Be {
|
|
|
373
370
|
return null;
|
|
374
371
|
const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, r = this._findNextMatch(t, i);
|
|
375
372
|
if (r) {
|
|
376
|
-
const [
|
|
377
|
-
return this._matchesPosition =
|
|
373
|
+
const [l, a] = r;
|
|
374
|
+
return this._matchesPosition = a + 1, this._query.findScope === C.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(l.range.subUnitId).findIndex((d) => d === l) : this._activeHighlightIndex = a, n || this._focusMatch(l), this._workbook.getActiveSheet().getSheetId() === l.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), l;
|
|
378
375
|
}
|
|
379
376
|
return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
|
|
380
377
|
}
|
|
@@ -384,21 +381,21 @@ let F = class extends Be {
|
|
|
384
381
|
return null;
|
|
385
382
|
const t = (s = e == null ? void 0 : e.loop) != null ? s : !1, i = (o = e == null ? void 0 : e.stayIfOnMatch) != null ? o : !1, n = (c = e == null ? void 0 : e.noFocus) != null ? c : !1, r = this._findPreviousMatch(t, i);
|
|
386
383
|
if (r) {
|
|
387
|
-
const [
|
|
388
|
-
return this._matchesPosition =
|
|
384
|
+
const [l, a] = r;
|
|
385
|
+
return this._matchesPosition = a + 1, this._query.findScope === C.UNIT ? this._activeHighlightIndex = this._matchesByWorksheet.get(l.range.subUnitId).findIndex((d) => d === l) : this._activeHighlightIndex = a, n || this._focusMatch(l), this._workbook.getActiveSheet().getSheetId() === l.range.subUnitId && this._updateCurrentHighlightShape(this._activeHighlightIndex), l;
|
|
389
386
|
}
|
|
390
387
|
return this._matchesPosition = 0, this._updateCurrentHighlightShape(), null;
|
|
391
388
|
}
|
|
392
389
|
_findPreviousMatch(e = !1, t = !1) {
|
|
393
390
|
if (this.currentMatch) {
|
|
394
|
-
const s = this._matches.findIndex((
|
|
391
|
+
const s = this._matches.findIndex((a) => a === this.currentMatch);
|
|
395
392
|
if (t)
|
|
396
393
|
return [this.currentMatch, s];
|
|
397
394
|
const o = s - 1;
|
|
398
395
|
if (!e && o < 0)
|
|
399
396
|
return null;
|
|
400
|
-
const c = this._matches.length,
|
|
401
|
-
return [this._matches[
|
|
397
|
+
const c = this._matches.length, l = (o + c) % c;
|
|
398
|
+
return [this._matches[l], l];
|
|
402
399
|
}
|
|
403
400
|
const i = this._selectionManagerService.getSelections();
|
|
404
401
|
if (!(i != null && i.length))
|
|
@@ -410,14 +407,14 @@ let F = class extends Be {
|
|
|
410
407
|
}
|
|
411
408
|
_findNextMatch(e = !1, t = !1) {
|
|
412
409
|
if (this.currentMatch) {
|
|
413
|
-
const s = this._matches.findIndex((
|
|
410
|
+
const s = this._matches.findIndex((a) => a === this.currentMatch);
|
|
414
411
|
if (t)
|
|
415
412
|
return [this.currentMatch, s];
|
|
416
413
|
const o = s + 1, c = this._matches.length;
|
|
417
414
|
if (!e && o >= c)
|
|
418
415
|
return null;
|
|
419
|
-
const
|
|
420
|
-
return [this._matches[
|
|
416
|
+
const l = o % c;
|
|
417
|
+
return [this._matches[l], l];
|
|
421
418
|
}
|
|
422
419
|
const i = this._selectionManagerService.getSelections();
|
|
423
420
|
if (!(i != null && i.length))
|
|
@@ -428,21 +425,21 @@ let F = class extends Be {
|
|
|
428
425
|
return r ? this._findNextMatchByRange(this._matchesByWorksheet.get(r), i[0].range) : null;
|
|
429
426
|
}
|
|
430
427
|
_findPreviousWorksheetThatHasAMatch(e, t = !1) {
|
|
431
|
-
const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ?
|
|
428
|
+
const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n + 1) : i.slice(0, n + 1)).findLast((o) => this._matchesByWorksheet.has(o));
|
|
432
429
|
return s != null ? s : null;
|
|
433
430
|
}
|
|
434
431
|
_findNextWorksheetThatHasAMatch(e, t = !1) {
|
|
435
|
-
const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ?
|
|
432
|
+
const i = this._workbook.getSheetOrders(), n = i.findIndex((o) => o === e), s = (t ? $(i, n) : i.slice(n)).find((o) => this._matchesByWorksheet.has(o));
|
|
436
433
|
return s != null ? s : null;
|
|
437
434
|
}
|
|
438
435
|
_findNextMatchByRange(e, t, i = !1) {
|
|
439
436
|
const n = this._query.findDirection === b.ROW;
|
|
440
437
|
let r = e.findIndex((o) => {
|
|
441
438
|
const c = o.range.range;
|
|
442
|
-
if (!(n ?
|
|
439
|
+
if (!(n ? Q(t, c) : X(t, c)))
|
|
443
440
|
return !1;
|
|
444
|
-
const
|
|
445
|
-
return i ?
|
|
441
|
+
const a = V(t, c);
|
|
442
|
+
return i ? a : !a;
|
|
446
443
|
});
|
|
447
444
|
r === -1 && (r = e.length - 1);
|
|
448
445
|
const s = e[r];
|
|
@@ -452,10 +449,10 @@ let F = class extends Be {
|
|
|
452
449
|
const n = this._query.findDirection === b.ROW;
|
|
453
450
|
let r = this._matches.findLastIndex((o) => {
|
|
454
451
|
const c = o.range.range;
|
|
455
|
-
if (!(n ?
|
|
452
|
+
if (!(n ? Ge(t, c) : Ke(t, c)))
|
|
456
453
|
return !1;
|
|
457
|
-
const
|
|
458
|
-
return i ?
|
|
454
|
+
const a = V(t, c);
|
|
455
|
+
return i ? a : !a;
|
|
459
456
|
});
|
|
460
457
|
r === -1 && (r = 0);
|
|
461
458
|
const s = e[r];
|
|
@@ -467,7 +464,7 @@ let F = class extends Be {
|
|
|
467
464
|
const t = this.currentMatch.range, i = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId), n = this._getReplacedCellData(
|
|
468
465
|
this.currentMatch,
|
|
469
466
|
i,
|
|
470
|
-
this._query.findBy ===
|
|
467
|
+
this._query.findBy === k.FORMULA,
|
|
471
468
|
this._query.findString,
|
|
472
469
|
e,
|
|
473
470
|
this._query.caseSensitive ? "g" : "ig"
|
|
@@ -480,52 +477,52 @@ let F = class extends Be {
|
|
|
480
477
|
}
|
|
481
478
|
}
|
|
482
479
|
};
|
|
483
|
-
return this._commandService.executeCommand(
|
|
480
|
+
return this._commandService.executeCommand(ee.id, r);
|
|
484
481
|
}
|
|
485
482
|
async replaceAll(e) {
|
|
486
483
|
if (this._matchesCount === 0 || !this._query)
|
|
487
484
|
return { success: 0, failure: 0 };
|
|
488
|
-
const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r ===
|
|
489
|
-
return
|
|
490
|
-
const f = new
|
|
491
|
-
|
|
492
|
-
const { startColumn:
|
|
493
|
-
|
|
485
|
+
const t = this._workbook.getUnitId(), { findString: i, caseSensitive: n, findBy: r } = this._query, s = r === k.FORMULA, o = n ? "g" : "ig", c = [];
|
|
486
|
+
return Ce(this._matches.filter((a) => a.replaceable), (a) => a.range.subUnitId).forEach((a, d) => {
|
|
487
|
+
const f = new Re(), S = this._workbook.getSheetBySheetId(d);
|
|
488
|
+
a.forEach((m) => {
|
|
489
|
+
const { startColumn: v, startRow: R } = m.range.range, I = this._getReplacedCellData(m, S, s, i, e, o);
|
|
490
|
+
I && f.setValue(R, v, I);
|
|
494
491
|
}), c.push({
|
|
495
|
-
count:
|
|
492
|
+
count: a.length,
|
|
496
493
|
subUnitId: d,
|
|
497
494
|
value: f.getMatrix()
|
|
498
495
|
});
|
|
499
|
-
}), c ? this._commandService.executeCommand(
|
|
496
|
+
}), c ? this._commandService.executeCommand(te.id, {
|
|
500
497
|
unitId: t,
|
|
501
498
|
replacements: c
|
|
502
499
|
}) : { success: 0, failure: 0 };
|
|
503
500
|
}
|
|
504
501
|
_getReplacedCellData(e, t, i, n, r, s) {
|
|
505
502
|
var S;
|
|
506
|
-
const o = e.range.range, { startRow: c, startColumn:
|
|
503
|
+
const o = e.range.range, { startRow: c, startColumn: l } = o, a = t.getCellRaw(c, l);
|
|
507
504
|
if (e.isFormula)
|
|
508
|
-
return i ? { f:
|
|
509
|
-
if (!!((S =
|
|
510
|
-
const m =
|
|
511
|
-
return
|
|
505
|
+
return i ? { f: a.f.replace(new RegExp(Y(n), s), r), v: null } : null;
|
|
506
|
+
if (!!((S = a.p) != null && S.body)) {
|
|
507
|
+
const m = we.deepClone(a.p);
|
|
508
|
+
return be(m.body, n, r), { p: m };
|
|
512
509
|
}
|
|
513
|
-
return { v:
|
|
510
|
+
return { v: a.v.toString().replace(new RegExp(Y(n), s), r) };
|
|
514
511
|
}
|
|
515
512
|
};
|
|
516
|
-
F =
|
|
513
|
+
F = T([
|
|
517
514
|
_(1, Z),
|
|
518
|
-
_(2,
|
|
519
|
-
_(3,
|
|
515
|
+
_(2, We),
|
|
516
|
+
_(3, H),
|
|
520
517
|
_(4, J),
|
|
521
|
-
_(5,
|
|
522
|
-
_(6,
|
|
523
|
-
_(7,
|
|
518
|
+
_(5, p(pe)),
|
|
519
|
+
_(6, p(Ne)),
|
|
520
|
+
_(7, p(Oe))
|
|
524
521
|
], F);
|
|
525
|
-
function
|
|
522
|
+
function Y(h) {
|
|
526
523
|
return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
527
524
|
}
|
|
528
|
-
let
|
|
525
|
+
let W = class extends z {
|
|
529
526
|
constructor(e, t) {
|
|
530
527
|
super();
|
|
531
528
|
/**
|
|
@@ -536,7 +533,7 @@ let H = class extends z {
|
|
|
536
533
|
}
|
|
537
534
|
async find(e) {
|
|
538
535
|
this._terminate();
|
|
539
|
-
const t = this._univerInstanceService.
|
|
536
|
+
const t = this._univerInstanceService.getCurrentUnitForType(q.UNIVER_SHEET);
|
|
540
537
|
if (t) {
|
|
541
538
|
const i = this._injector.createInstance(F, t);
|
|
542
539
|
this._findModelsByUnitId.set(t.getUnitId(), i);
|
|
@@ -564,49 +561,48 @@ let H = class extends z {
|
|
|
564
561
|
};
|
|
565
562
|
}
|
|
566
563
|
};
|
|
567
|
-
|
|
564
|
+
W = T([
|
|
568
565
|
_(0, Z),
|
|
569
|
-
_(1,
|
|
570
|
-
],
|
|
566
|
+
_(1, p(O))
|
|
567
|
+
], W);
|
|
571
568
|
const u = { hit: !1, replaceable: !1, isFormula: !1, rawData: null };
|
|
572
|
-
function
|
|
573
|
-
const { findBy: r } = i, s = r ===
|
|
569
|
+
function tt(h, e, t, i, n) {
|
|
570
|
+
const { findBy: r } = i, s = r === k.FORMULA, o = h.getCellRaw(e, t);
|
|
574
571
|
return u.rawData = o, !(o != null && o.f) ? (u.isFormula = !1, P(n, i) ? o ? (u.hit = !0, u.replaceable = !0) : (u.hit = !0, u.replaceable = !1) : (u.hit = !1, u.replaceable = !1), u) : (u.isFormula = !0, s ? P({ v: o.f }, i) ? (u.hit = !0, u.replaceable = !0, u) : (u.hit = !1, u.replaceable = !1, u) : (u.replaceable = !1, P(n, i) ? u.hit = !0 : u.hit = !1, u));
|
|
575
572
|
}
|
|
576
573
|
function P(h, e) {
|
|
577
|
-
let t =
|
|
578
|
-
return t ? e.matchesTheWholeCell ? (t =
|
|
574
|
+
let t = it(h);
|
|
575
|
+
return t ? e.matchesTheWholeCell ? (t = nt(t), e.caseSensitive ? t === e.findString : t.toLowerCase() === e.findString) : e.caseSensitive ? t.indexOf(e.findString) > -1 : t.toLowerCase().indexOf(e.findString) > -1 : !1;
|
|
579
576
|
}
|
|
580
|
-
function
|
|
577
|
+
function it(h) {
|
|
581
578
|
var t, i, n;
|
|
582
579
|
const e = (n = (i = (t = h == null ? void 0 : h.p) == null ? void 0 : t.body) == null ? void 0 : i.dataStream) != null ? n : h == null ? void 0 : h.v;
|
|
583
580
|
return typeof e == "number" ? `${e}` : typeof e == "boolean" ? e ? "1" : "0" : e;
|
|
584
581
|
}
|
|
585
|
-
function
|
|
582
|
+
function nt(h) {
|
|
586
583
|
return h.replace(/^ +/g, "").replace(/ +$/g, "");
|
|
587
584
|
}
|
|
588
|
-
var
|
|
589
|
-
for (var n = i > 1 ? void 0 : i ?
|
|
585
|
+
var st = Object.defineProperty, rt = Object.getOwnPropertyDescriptor, ot = (h, e, t, i) => {
|
|
586
|
+
for (var n = i > 1 ? void 0 : i ? rt(e, t) : e, r = h.length - 1, s; r >= 0; r--)
|
|
590
587
|
(s = h[r]) && (n = (i ? s(e, t, n) : s(n)) || n);
|
|
591
|
-
return i && n &&
|
|
592
|
-
},
|
|
593
|
-
const
|
|
594
|
-
var
|
|
595
|
-
let K = (
|
|
596
|
-
constructor(h, e) {
|
|
597
|
-
super(
|
|
588
|
+
return i && n && st(e, t, n), n;
|
|
589
|
+
}, G = (h, e) => (t, i) => e(t, i, h);
|
|
590
|
+
const ht = "UNIVER_SHEETS_FIND_REPLACE_PLUGIN";
|
|
591
|
+
var M;
|
|
592
|
+
let K = (M = class extends Me {
|
|
593
|
+
constructor(h, e, t) {
|
|
594
|
+
super(), this._injector = e, this._pluginService = t, this._pluginService.registerPlugin(Pe);
|
|
598
595
|
}
|
|
599
596
|
onStarting(h) {
|
|
600
|
-
[[x]].forEach((e) =>
|
|
601
|
-
h.add(e);
|
|
602
|
-
});
|
|
597
|
+
[[x]].forEach((e) => h.add(e));
|
|
603
598
|
}
|
|
604
|
-
}, g(
|
|
605
|
-
K =
|
|
606
|
-
|
|
599
|
+
}, g(M, "pluginName", ht), g(M, "type", q.UNIVER_SHEET), M);
|
|
600
|
+
K = ot([
|
|
601
|
+
G(1, p(O)),
|
|
602
|
+
G(2, p(ke))
|
|
607
603
|
], K);
|
|
608
604
|
export {
|
|
609
605
|
K as UniverSheetsFindReplacePlugin,
|
|
610
|
-
|
|
611
|
-
|
|
606
|
+
mt as enUS,
|
|
607
|
+
St as zhCN
|
|
612
608
|
};
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Injector } from '@wendellhu/redi';
|
|
2
|
-
import { Plugin,
|
|
2
|
+
import { Plugin, PluginService, UniverInstanceType } from '@univerjs/core';
|
|
3
3
|
|
|
4
|
-
export interface IFindPluginConfig {
|
|
5
|
-
}
|
|
6
4
|
export declare class UniverSheetsFindReplacePlugin extends Plugin {
|
|
7
5
|
protected readonly _injector: Injector;
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
private readonly _pluginService;
|
|
7
|
+
static pluginName: string;
|
|
8
|
+
static type: UniverInstanceType;
|
|
9
|
+
constructor(_config: unknown, _injector: Injector, _pluginService: PluginService);
|
|
10
10
|
onStarting(injector: Injector): void;
|
|
11
11
|
}
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(u,l){typeof exports=="object"&&typeof module<"u"?l(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/engine-render"),require("@univerjs/find-replace"),require("@univerjs/sheets"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/engine-render","@univerjs/find-replace","@univerjs/sheets","@univerjs/sheets-ui","rxjs"],l):(u=typeof globalThis<"u"?globalThis:u||self,l(u.UniverSheetsFindReplace={},u.UniverCore,u["@wendellhu/redi"],u.UniverEngineRender,u.UniverFindReplace,u.UniverSheets,u.UniverSheetsUi,u.rxjs))})(this,function(u,l,m,x,S,R,k,w){"use strict";var Ie=Object.defineProperty;var Ce=(u,l,m)=>l in u?Ie(u,l,{enumerable:!0,configurable:!0,writable:!0,value:m}):u[l]=m;var _=(u,l,m)=>(Ce(u,typeof l!="symbol"?l+"":l,m),m);var T;const Q={"sheet-find-replace":{replace:{"partial-failure":"Failed to replace some cells",failure:"Failed to replace all cells"}}},X={"sheet-find-replace":{replace:{"partial-failure":"部分单元格替换失败",failure:"替换失败"}}};class Y extends x.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})`;x.Rect.drawWith(t,{width:this.width,height:this.height,fill:n,stroke:i?r:void 0,strokeWidth:i?2:0,evented:!1})}}const A={id:"sheet.command.replace",type:l.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(l.IUndoRedoService),i=h.get(l.ICommandService),{unitId:n,replacements:r}=e,s=t.__tempBatchingUndoRedo(n),o=await Promise.all(r.map(c=>i.executeCommand(R.SetRangeValuesCommand.id,{unitId:n,subUnitId:c.subUnitId,value:c.value})));return s.dispose(),z(o,r)}};function z(h,e){let t=0,i=0;return h.forEach((n,r)=>{const s=e[r].count;n?t+=s:i+=s}),{success:t,failure:i}}function E(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function D(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function j(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function G(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function K(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function J(h,e){const{range:t}=h,{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 Z=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,P=(h,e,t,i)=>{for(var n=i>1?void 0:i?ee(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&Z(e,t,n),n},p=(h,e)=>(t,i)=>e(t,i,h);let U=class extends l.Disposable{constructor(e,t,i,n,r){super();_(this,"_provider");this._injector=e,this._findReplaceController=t,this._contextService=i,this._findReplaceService=n,this._commandService=r,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const e=this._injector.createInstance(W);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(l.EDITOR_ACTIVATED).pipe(w.filter(t=>!!t)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[A].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};U=P([l.OnLifecycle(l.LifecycleStages.Steady,U),p(0,m.Inject(m.Injector)),p(1,m.Inject(S.FindReplaceController)),p(2,l.IContextService),p(3,S.IFindReplaceService),p(4,l.ICommandService)],U);const te="sheets-find-replace-provider",ie=1e4;let B=class extends S.FindModel{constructor(e,t,i,n,r,s,o,c){super();_(this,"_matchesUpdate$",new w.Subject);_(this,"matchesUpdate$",this._matchesUpdate$.asObservable());_(this,"_activelyChangingMatch$",new w.Subject);_(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());_(this,"_matchesByWorksheet",new Map);_(this,"_matches",[]);_(this,"_matchesPosition",0);_(this,"_activeHighlightIndex",-1);_(this,"_highlightShapes",[]);_(this,"_currentHighlightShape",null);_(this,"_query",null);this._workbook=e,this._univerInstanceService=t,this._renderManagerService=i,this._commandService=n,this._contextService=r,this._themeService=s,this._sheetSkeletonManagerService=o,this._selectionManagerService=c}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===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(x.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 c=this._findInWorksheet(o,e,t),a=o.getSheetId(),{results:d}=c;return d.length?this._matchesByWorksheet.set(a,c.results):this._matchesByWorksheet.delete(a),c});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).pipe(w.filter(([s,o])=>s.id===R.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(l.fromCallback(this._commandService.onCommandExecuted).pipe(w.filter(([s])=>s.type===l.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),w.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._selectionManagerService.getSelections();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 g;const c=this._workbook.getActiveSheet(),a=this._selectionManagerService.getSelections();return(g=a==null?void 0:a.some(v=>!J(v,c)))!=null?g:!1};let n,r=!0,s=!1;const o=()=>{const c=this._workbook.getActiveSheet(),a=this.currentMatch;s=i();const d=this._selectionManagerService.getSelections(),g=s?this._findInSelections(c,d,e,t):this._findInWorksheet(c,e,t);return this._matches=g.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(a,this._matches),r?(n=g,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),g};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(w.merge(l.fromCallback(this._commandService.onCommandExecuted).pipe(w.filter(([c])=>{if(c.type===l.CommandType.MUTATION&&c.params.unitId===this._workbook.getUnitId())return!0;if(c.id===R.SetSelectionsOperation.id&&c.params.unitId===t){const a=i();return a===!1&&s===!1?!1:(s=a,!0)}return!1})),this._workbook.activeSheet$.pipe(w.skip(1))).pipe(w.debounceTime(200)).subscribe(()=>o())),o(),n}_findInRange(e,t,i,n,r){const s=[],o=e.getSheetId(),c=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(;;){const{done:a,value:d}=c.next();if(a)break;const{row:g,col:v,colSpan:C,rowSpan:I,value:b}=d;if(r!=null&&r(g,v))continue;const{hit:y,replaceable:M,isFormula:F}=ne(e,g,v,t,b);if(y){const O={provider:te,unitId:n,replaceable:M,isFormula:F,range:{subUnitId:o,range:{startRow:g,startColumn:v,endColumn:v+(C!=null?C:1)-1,endRow:g+(I!=null?I:1)-1}}};s.push(O)}}return{results:s}}_findInSelections(e,t,i,n){const{findDirection:r}=i,s=r===S.FindDirection.ROW?D:j,o=new Set;return{results:t.map(a=>this._findInRange(e,i,a.range,n,(d,g)=>{const v=`${d}-${g}`;return o.has(v)?!0:(o.add(v),!1)}).results).flat().sort((a,d)=>s(a.range.range,d.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=>{t.dispose(),t.getScene().makeDirty()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,I;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((I=this._univerInstanceService.getFocusedUniverInstance())==null?void 0:I.getUnitId())!==this._workbook.getUnitId())return;const n=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(n);if(r==null)return;const{scene:s}=r,o=this._matches,c=this._themeService.getCurrentTheme().gold400,a=new l.ColorKit(c).toRgb(),d=this._workbook.getActiveSheet(),g=d.getSheetId(),v=o.filter(b=>b.range.subUnitId===g).map((b,y)=>{const{startColumn:M,startRow:F,endColumn:O,endRow:de}=b.range.range,ue=k.getCoordByCell(F,M,s,e),ge=k.getCoordByCell(de,O,s,e),{startX:$,startY:L}=ue,{endX:fe,endY:_e}=ge,V=!d.getRowVisible(F),q=!d.getColVisible(M),Se=V||q,me=q?2:fe-$,ve=V?2:_e-L,pe={left:$,top:L,color:a,width:me,height:ve,evented:!1,inHiddenRange:Se};return new Y(`find-highlight-${y}`,pe)});s.addObjects(v,ie),this._highlightShapes=v,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const i=this._highlightShapes[e];if(!i)return;this._currentHighlightShape=i,i.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(R.SetWorksheetActivateCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:t},{fromFindReplace:!0}),this._commandService.syncExecuteCommand(k.ScrollToCellCommand.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;const{subUnitId:i}=e.range,{startColumn:n,startRow:r}=e.range.range,s=t.findIndex(o=>{if(i!==o.range.subUnitId)return!1;const{startColumn:c,startRow:a}=o.range.range;return c===n&&a===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findNextMatch(t,i);if(r){const[a,d]=r;return this._matchesPosition=d+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(g=>g===a):this._activeHighlightIndex=d,n||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findPreviousMatch(t,i);if(r){const[a,d]=r;return this._matchesPosition=d+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(a.range.subUnitId).findIndex(g=>g===a):this._activeHighlightIndex=d,n||this._focusMatch(a),this._workbook.getActiveSheet().getSheetId()===a.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),a}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(d=>d===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const c=this._matches.length,a=(o+c)%c;return[this._matches[a],a]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,i[0].range);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(n,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(d=>d===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,c=this._matches.length;if(!e&&o>=c)return null;const a=o%c;return[this._matches[a],a]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,i[0].range,t);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(n,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),i[0].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 c=o.range.range;if(!(n?D(t,c):j(t,c)))return!1;const d=E(t,c);return i?d:!d});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 c=o.range.range;if(!(n?G(t,c):K(t,c)))return!1;const d=E(t,c);return i?d:!d});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(R.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",c=[];return l.groupBy(this._matches.filter(d=>d.replaceable),d=>d.range.subUnitId).forEach((d,g)=>{const v=new l.ObjectMatrix,C=this._workbook.getSheetBySheetId(g);d.forEach(I=>{const{startColumn:b,startRow:y}=I.range.range,M=this._getReplacedCellData(I,C,s,i,e,o);M&&v.setValue(y,b,M)}),c.push({count:d.length,subUnitId:g,value:v.getMatrix()})}),c?this._commandService.executeCommand(A.id,{unitId:t,replacements:c}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,s){var C;const o=e.range.range,{startRow:c,startColumn:a}=o,d=t.getCellRaw(c,a);if(e.isFormula)return i?{f:d.f.replace(new RegExp(N(n),s),r),v:null}:null;if(!!((C=d.p)!=null&&C.body)){const I=l.Tools.deepClone(d.p);return l.replaceInDocumentBody(I.body,n,r),{p:I}}return{v:d.v.toString().replace(new RegExp(N(n),s),r)}}};B=P([p(1,l.IUniverInstanceService),p(2,x.IRenderManagerService),p(3,l.ICommandService),p(4,l.IContextService),p(5,m.Inject(l.ThemeService)),p(6,m.Inject(k.SheetSkeletonManagerService)),p(7,m.Inject(R.SelectionManagerService))],B);function N(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let W=class extends l.Disposable{constructor(e,t){super();_(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=t}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUniverSheetInstance();if(t){const i=this._injector.createInstance(B,t);this._findModelsByUnitId.set(t.getUnitId(),i);const n=this._preprocessQuery(e);return i.start(n),[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}}};W=P([p(0,l.IUniverInstanceService),p(1,m.Inject(m.Injector))],W);const f={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ne(h,e,t,i,n){const{findBy:r}=i,s=r===S.FindBy.FORMULA,o=h.getCellRaw(e,t);return f.rawData=o,!(o!=null&&o.f)?(f.isFormula=!1,H(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?H({v:o.f},i)?(f.hit=!0,f.replaceable=!0,f):(f.hit=!1,f.replaceable=!1,f):(f.replaceable=!1,H(n,i)?f.hit=!0:f.hit=!1,f))}function H(h,e){let t=se(h);return t?e.matchesTheWholeCell?(t=re(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 se(h){var t,i,n;const e=(n=(i=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:i.dataStream)!=null?n:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function re(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var oe=Object.defineProperty,he=Object.getOwnPropertyDescriptor,ce=(h,e,t,i)=>{for(var n=i>1?void 0:i?he(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&oe(e,t,n),n},ae=(h,e)=>(t,i)=>e(t,i,h);const le="UNIVER_SHEETS_FIND_REPLACE_PLUGIN";u.UniverSheetsFindReplacePlugin=(T=class extends l.Plugin{constructor(e,t){super(le),this._injector=t}onStarting(e){[[U]].forEach(t=>{e.add(t)})}},_(T,"type",l.PluginType.Sheet),T),u.UniverSheetsFindReplacePlugin=ce([ae(1,m.Inject(m.Injector))],u.UniverSheetsFindReplacePlugin),u.enUS=Q,u.zhCN=X,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(d,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("@univerjs/core"),require("@wendellhu/redi"),require("@univerjs/find-replace"),require("@univerjs/engine-render"),require("@univerjs/sheets"),require("@univerjs/sheets-ui"),require("rxjs")):typeof define=="function"&&define.amd?define(["exports","@univerjs/core","@wendellhu/redi","@univerjs/find-replace","@univerjs/engine-render","@univerjs/sheets","@univerjs/sheets-ui","rxjs"],u):(d=typeof globalThis<"u"?globalThis:d||self,u(d.UniverSheetsFindReplace={},d.UniverCore,d["@wendellhu/redi"],d.UniverFindReplace,d.UniverEngineRender,d.UniverSheets,d.UniverSheetsUi,d.rxjs))})(this,function(d,u,m,S,U,M,k,w){"use strict";var Ie=Object.defineProperty;var Ce=(d,u,m)=>u in d?Ie(d,u,{enumerable:!0,configurable:!0,writable:!0,value:m}):d[u]=m;var f=(d,u,m)=>(Ce(d,typeof u!="symbol"?u+"":u,m),m);var F;const Q={"sheet-find-replace":{replace:{"partial-failure":"Failed to replace some cells",failure:"Failed to replace all cells"}}},X={"sheet-find-replace":{replace:{"partial-failure":"部分单元格替换失败",failure:"替换失败"}}};class Y extends U.Shape{constructor(t,i){super(t,i);f(this,"_activated",!1);f(this,"_inHiddenRange",!1);f(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})`;U.Rect.drawWith(t,{width:this.width,height:this.height,fill:n,stroke:i?r:void 0,strokeWidth:i?2:0,evented:!1})}}const O={id:"sheet.command.replace",type:u.CommandType.COMMAND,handler:async(h,e)=>{const t=h.get(u.IUndoRedoService),i=h.get(u.ICommandService),{unitId:n,replacements:r}=e,s=t.__tempBatchingUndoRedo(n),o=await Promise.all(r.map(c=>i.executeCommand(M.SetRangeValuesCommand.id,{unitId:n,subUnitId:c.subUnitId,value:c.value})));return s.dispose(),z(o,r)}};function z(h,e){let t=0,i=0;return h.forEach((n,r)=>{const s=e[r].count;n?t+=s:i+=s}),{success:t,failure:i}}function E(h,e){return h.startRow===e.startRow&&h.startColumn===e.startColumn}function A(h,e){return h.startRow<e.startRow||h.startRow===e.startRow&&h.startColumn<=e.startColumn}function D(h,e){return h.startColumn<e.startColumn||h.startColumn===e.startColumn&&h.startRow<=e.startRow}function G(h,e){return h.startRow>e.startRow||h.startRow===e.startRow&&h.startColumn>=e.startColumn}function K(h,e){return h.startColumn>e.startColumn||h.startColumn===e.startColumn&&h.startRow>=e.startRow}function J(h,e){const{range:t}=h,{startRow:i,startColumn:n}=t,r=e.getMergedCell(i,n);return r?u.Rectangle.equals(t,r):t.endRow===t.startRow&&t.endColumn===t.startColumn}var Z=Object.defineProperty,ee=Object.getOwnPropertyDescriptor,B=(h,e,t,i)=>{for(var n=i>1?void 0:i?ee(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&Z(e,t,n),n},p=(h,e)=>(t,i)=>e(t,i,h);let x=class extends u.Disposable{constructor(e,t,i,n,r){super();f(this,"_provider");this._injector=e,this._findReplaceController=t,this._contextService=i,this._findReplaceService=n,this._commandService=r,this._init(),this._initCommands()}dispose(){super.dispose(),this._provider.dispose()}_init(){const e=this._injector.createInstance(T);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(u.EDITOR_ACTIVATED).pipe(w.filter(t=>!!t)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[O].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};x=B([u.OnLifecycle(u.LifecycleStages.Steady,x),p(0,m.Inject(m.Injector)),p(1,m.Inject(S.FindReplaceController)),p(2,u.IContextService),p(3,S.IFindReplaceService),p(4,u.ICommandService)],x);const te="sheets-find-replace-provider",ie=1e4;let W=class extends S.FindModel{constructor(e,t,i,n,r,s,o,c){super();f(this,"_matchesUpdate$",new w.Subject);f(this,"matchesUpdate$",this._matchesUpdate$.asObservable());f(this,"_activelyChangingMatch$",new w.Subject);f(this,"activelyChangingMatch$",this._activelyChangingMatch$.asObservable());f(this,"_matchesByWorksheet",new Map);f(this,"_matches",[]);f(this,"_matchesPosition",0);f(this,"_activeHighlightIndex",-1);f(this,"_highlightShapes",[]);f(this,"_currentHighlightShape",null);f(this,"_query",null);this._workbook=e,this._univerInstanceService=t,this._renderManagerService=i,this._commandService=n,this._contextService=r,this._themeService=s,this._sheetSkeletonManagerService=o,this._selectionManagerService=c}get _matchesCount(){return this._matches.length}get unitId(){return this._workbook.getUnitId()}get matchesCount(){return this._matchesCount}get matchesPosition(){return this._matchesPosition}get currentMatch(){return this._matchesPosition>0?this._matches[this._matchesPosition-1]:null}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===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(U.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 c=this._findInWorksheet(o,e,t),l=o.getSheetId(),{results:a}=c;return a.length?this._matchesByWorksheet.set(l,c.results):this._matchesByWorksheet.delete(l),c});this._matches=s.map(o=>o.results).flat(),this._updateFindHighlight(),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(u.fromCallback(this._commandService.onCommandExecuted).pipe(w.filter(([s,o])=>s.id===M.SetWorksheetActiveOperation.id&&!(o!=null&&o.fromFindReplace))).subscribe(()=>{const s=this._workbook.getActiveSheet(),o=s.getSheetId();this._matchesByWorksheet.has(o)&&this._findNextMatchOnActiveSheetChange(s)})),this.disposeWithMe(u.fromCallback(this._commandService.onCommandExecuted).pipe(w.filter(([s])=>s.type===u.CommandType.MUTATION&&s.params.unitId===this._workbook.getUnitId()),w.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._selectionManagerService.getSelections();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 g;const c=this._workbook.getActiveSheet(),l=this._selectionManagerService.getSelections();return(g=l==null?void 0:l.some(v=>!J(v,c)))!=null?g:!1};let n,r=!0,s=!1;const o=()=>{const c=this._workbook.getActiveSheet(),l=this.currentMatch;s=i();const a=this._selectionManagerService.getSelections(),g=s?this._findInSelections(c,a,e,t):this._findInWorksheet(c,e,t);return this._matches=g.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(l,this._matches),r?(n=g,r=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),g};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(w.merge(u.fromCallback(this._commandService.onCommandExecuted).pipe(w.filter(([c])=>{if(c.type===u.CommandType.MUTATION&&c.params.unitId===this._workbook.getUnitId())return!0;if(c.id===M.SetSelectionsOperation.id&&c.params.unitId===t){const l=i();return l===!1&&s===!1?!1:(s=l,!0)}return!1})),this._workbook.activeSheet$.pipe(w.skip(1))).pipe(w.debounceTime(200)).subscribe(()=>o())),o(),n}_findInRange(e,t,i,n,r){const s=[],o=e.getSheetId(),c=(t.findDirection===S.FindDirection.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(i);for(const l of c){const{row:a,col:g,colSpan:v,rowSpan:C,value:I}=l;if(r!=null&&r(a,g)||!I||e.getRowFiltered(a))continue;const{hit:R,replaceable:y,isFormula:b}=ne(e,a,g,t,I);if(R){const P={provider:te,unitId:n,replaceable:y,isFormula:b,range:{subUnitId:o,range:{startRow:a,startColumn:g,endColumn:g+(v!=null?v:1)-1,endRow:a+(C!=null?C:1)-1}}};s.push(P)}}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(l=>this._findInRange(e,i,l.range,n,(a,g)=>{const v=`${a}-${g}`;return o.has(v)?!0:(o.add(v),!1)}).results).flat().sort((l,a)=>s(l.range.range,a.range.range)?-1:1)}}_findInWorksheet(e,t,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=>{t.dispose(),t.getScene().makeDirty()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){var C,I;this._disposeHighlights();const e=(C=this._sheetSkeletonManagerService.getCurrent())==null?void 0:C.skeleton;if(!e||!this._getSheetObject()||((I=this._univerInstanceService.getFocusedUnit())==null?void 0:I.getUnitId())!==this._workbook.getUnitId())return;const n=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(n);if(r==null)return;const{scene:s}=r,o=this._matches,c=this._themeService.getCurrentTheme().gold400,l=new u.ColorKit(c).toRgb(),a=this._workbook.getActiveSheet(),g=a.getSheetId(),v=o.filter(R=>R.range.subUnitId===g).map((R,y)=>{const{startColumn:b,startRow:P,endColumn:le,endRow:ue}=R.range.range,de=k.getCoordByCell(P,b,s,e),ge=k.getCoordByCell(ue,le,s,e),{startX:$,startY:L}=de,{endX:_e,endY:fe}=ge,V=!a.getRowRawVisible(P),q=!a.getColVisible(b),Se=V||q,me=q?2:_e-$,ve=V?2:fe-L,pe={left:$,top:L,color:l,width:me,height:ve,evented:!1,inHiddenRange:Se};return new Y(`find-highlight-${y}`,pe)});s.addObjects(v,ie),this._highlightShapes=v,s.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){const i=this._highlightShapes[e];if(!i)return;this._currentHighlightShape=i,i.setShapeProps({activated:!0})}}_getSheetObject(){return k.getSheetObject(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){const t=e.range.subUnitId;t!==this._workbook.getActiveSheet().getSheetId()&&this._commandService.syncExecuteCommand(M.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:i}=e.range,{startColumn:n,startRow:r}=e.range.range,s=t.findIndex(o=>{if(i!==o.range.subUnitId)return!1;const{startColumn:c,startRow:l}=o.range.range;return c===n&&l===r});return s>-1?s+1:0}moveToNextMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findNextMatch(t,i);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(g=>g===l):this._activeHighlightIndex=a,n||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var s,o,c;if(!this._matches.length)return null;const t=(s=e==null?void 0:e.loop)!=null?s:!1,i=(o=e==null?void 0:e.stayIfOnMatch)!=null?o:!1,n=(c=e==null?void 0:e.noFocus)!=null?c:!1,r=this._findPreviousMatch(t,i);if(r){const[l,a]=r;return this._matchesPosition=a+1,this._query.findScope===S.FindScope.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(l.range.subUnitId).findIndex(g=>g===l):this._activeHighlightIndex=a,n||this._focusMatch(l),this._workbook.getActiveSheet().getSheetId()===l.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),l}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s-1;if(!e&&o<0)return null;const c=this._matches.length,l=(o+c)%c;return[this._matches[l],l]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findPreviousMatchByRange(this._matches,i[0].range);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findPreviousWorksheetThatHasAMatch(n,e);return r?this._findPreviousMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findNextMatch(e=!1,t=!1){if(this.currentMatch){const s=this._matches.findIndex(a=>a===this.currentMatch);if(t)return[this.currentMatch,s];const o=s+1,c=this._matches.length;if(!e&&o>=c)return null;const l=o%c;return[this._matches[l],l]}const i=this._selectionManagerService.getSelections();if(!(i!=null&&i.length))return[this._matches[0],0];if(this._query.findScope!==S.FindScope.UNIT)return this._findNextMatchByRange(this._matches,i[0].range,t);const n=this._workbook.getActiveSheet().getSheetId(),r=this._findNextWorksheetThatHasAMatch(n,e);return r?this._findNextMatchByRange(this._matchesByWorksheet.get(r),i[0].range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){const i=this._workbook.getSheetOrders(),n=i.findIndex(o=>o===e),s=(t?u.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?u.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 c=o.range.range;if(!(n?A(t,c):D(t,c)))return!1;const a=E(t,c);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 c=o.range.range;if(!(n?G(t,c):K(t,c)))return!1;const a=E(t,c);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(M.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",c=[];return u.groupBy(this._matches.filter(a=>a.replaceable),a=>a.range.subUnitId).forEach((a,g)=>{const v=new u.ObjectMatrix,C=this._workbook.getSheetBySheetId(g);a.forEach(I=>{const{startColumn:R,startRow:y}=I.range.range,b=this._getReplacedCellData(I,C,s,i,e,o);b&&v.setValue(y,R,b)}),c.push({count:a.length,subUnitId:g,value:v.getMatrix()})}),c?this._commandService.executeCommand(O.id,{unitId:t,replacements:c}):{success:0,failure:0}}_getReplacedCellData(e,t,i,n,r,s){var C;const o=e.range.range,{startRow:c,startColumn:l}=o,a=t.getCellRaw(c,l);if(e.isFormula)return i?{f:a.f.replace(new RegExp(N(n),s),r),v:null}:null;if(!!((C=a.p)!=null&&C.body)){const I=u.Tools.deepClone(a.p);return u.replaceInDocumentBody(I.body,n,r),{p:I}}return{v:a.v.toString().replace(new RegExp(N(n),s),r)}}};W=B([p(1,u.IUniverInstanceService),p(2,U.IRenderManagerService),p(3,u.ICommandService),p(4,u.IContextService),p(5,m.Inject(u.ThemeService)),p(6,m.Inject(k.SheetSkeletonManagerService)),p(7,m.Inject(M.SelectionManagerService))],W);function N(h){return h.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&")}let T=class extends u.Disposable{constructor(e,t){super();f(this,"_findModelsByUnitId",new Map);this._univerInstanceService=e,this._injector=t}async find(e){this._terminate();const t=this._univerInstanceService.getCurrentUnitForType(u.UniverInstanceType.UNIVER_SHEET);if(t){const i=this._injector.createInstance(W,t);this._findModelsByUnitId.set(t.getUnitId(),i);const n=this._preprocessQuery(e);return i.start(n),[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}}};T=B([p(0,u.IUniverInstanceService),p(1,m.Inject(m.Injector))],T);const _={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function ne(h,e,t,i,n){const{findBy:r}=i,s=r===S.FindBy.FORMULA,o=h.getCellRaw(e,t);return _.rawData=o,!(o!=null&&o.f)?(_.isFormula=!1,H(n,i)?o?(_.hit=!0,_.replaceable=!0):(_.hit=!0,_.replaceable=!1):(_.hit=!1,_.replaceable=!1),_):(_.isFormula=!0,s?H({v:o.f},i)?(_.hit=!0,_.replaceable=!0,_):(_.hit=!1,_.replaceable=!1,_):(_.replaceable=!1,H(n,i)?_.hit=!0:_.hit=!1,_))}function H(h,e){let t=se(h);return t?e.matchesTheWholeCell?(t=re(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 se(h){var t,i,n;const e=(n=(i=(t=h==null?void 0:h.p)==null?void 0:t.body)==null?void 0:i.dataStream)!=null?n:h==null?void 0:h.v;return typeof e=="number"?`${e}`:typeof e=="boolean"?e?"1":"0":e}function re(h){return h.replace(/^ +/g,"").replace(/ +$/g,"")}var oe=Object.defineProperty,he=Object.getOwnPropertyDescriptor,ce=(h,e,t,i)=>{for(var n=i>1?void 0:i?he(e,t):e,r=h.length-1,s;r>=0;r--)(s=h[r])&&(n=(i?s(e,t,n):s(n))||n);return i&&n&&oe(e,t,n),n},j=(h,e)=>(t,i)=>e(t,i,h);const ae="UNIVER_SHEETS_FIND_REPLACE_PLUGIN";d.UniverSheetsFindReplacePlugin=(F=class extends u.Plugin{constructor(e,t,i){super(),this._injector=t,this._pluginService=i,this._pluginService.registerPlugin(S.UniverFindReplacePlugin)}onStarting(e){[[x]].forEach(t=>e.add(t))}},f(F,"pluginName",ae),f(F,"type",u.UniverInstanceType.UNIVER_SHEET),F),d.UniverSheetsFindReplacePlugin=ce([j(1,m.Inject(m.Injector)),j(2,m.Inject(u.PluginService))],d.UniverSheetsFindReplacePlugin),d.enUS=Q,d.zhCN=X,Object.defineProperty(d,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.8",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "UniverSheet find replace plugin",
|
|
6
6
|
"author": "DreamNum <developer@univer.ai>",
|
|
@@ -50,27 +50,27 @@
|
|
|
50
50
|
"npm": ">=8.0.0"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"@wendellhu/redi": "0.13.
|
|
53
|
+
"@wendellhu/redi": "^0.13.3",
|
|
54
54
|
"rxjs": ">=7.0.0",
|
|
55
|
-
"@univerjs/core": "0.1.
|
|
56
|
-
"@univerjs/
|
|
57
|
-
"@univerjs/
|
|
58
|
-
"@univerjs/sheets": "0.1.
|
|
59
|
-
"@univerjs/sheets-ui": "0.1.
|
|
55
|
+
"@univerjs/core": "0.1.8",
|
|
56
|
+
"@univerjs/find-replace": "0.1.8",
|
|
57
|
+
"@univerjs/engine-render": "0.1.8",
|
|
58
|
+
"@univerjs/sheets": "0.1.8",
|
|
59
|
+
"@univerjs/sheets-ui": "0.1.8"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@wendellhu/redi": "^0.13.
|
|
62
|
+
"@wendellhu/redi": "^0.13.3",
|
|
63
63
|
"rxjs": "^7.8.1",
|
|
64
|
-
"typescript": "^5.4.
|
|
65
|
-
"vite": "^5.2.
|
|
66
|
-
"vitest": "^1.
|
|
67
|
-
"@univerjs/
|
|
68
|
-
"@univerjs/engine-
|
|
69
|
-
"@univerjs/
|
|
70
|
-
"@univerjs/find-replace": "0.1.
|
|
71
|
-
"@univerjs/sheets": "0.1.
|
|
72
|
-
"@univerjs/
|
|
73
|
-
"@univerjs/sheets-ui": "0.1.
|
|
64
|
+
"typescript": "^5.4.5",
|
|
65
|
+
"vite": "^5.2.10",
|
|
66
|
+
"vitest": "^1.5.0",
|
|
67
|
+
"@univerjs/core": "0.1.8",
|
|
68
|
+
"@univerjs/engine-formula": "0.1.8",
|
|
69
|
+
"@univerjs/shared": "0.1.8",
|
|
70
|
+
"@univerjs/find-replace": "0.1.8",
|
|
71
|
+
"@univerjs/sheets": "0.1.8",
|
|
72
|
+
"@univerjs/engine-render": "0.1.8",
|
|
73
|
+
"@univerjs/sheets-ui": "0.1.8"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"test": "vitest run",
|