@univerjs/sheets-find-replace 0.20.1 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/facade.js +204 -1
- package/lib/cjs/index.js +936 -1
- package/lib/cjs/locale/ca-ES.js +9 -1
- package/lib/cjs/locale/en-US.js +24 -1
- package/lib/cjs/locale/es-ES.js +9 -1
- package/lib/cjs/locale/fa-IR.js +9 -1
- package/lib/cjs/locale/fr-FR.js +9 -1
- package/lib/cjs/locale/ja-JP.js +9 -1
- package/lib/cjs/locale/ko-KR.js +9 -1
- package/lib/cjs/locale/ru-RU.js +9 -1
- package/lib/cjs/locale/sk-SK.js +9 -1
- package/lib/cjs/locale/vi-VN.js +9 -1
- package/lib/cjs/locale/zh-CN.js +24 -1
- package/lib/cjs/locale/zh-TW.js +9 -1
- package/lib/es/facade.js +205 -1
- package/lib/es/index.js +923 -1
- package/lib/es/locale/ca-ES.js +8 -1
- package/lib/es/locale/en-US.js +23 -1
- package/lib/es/locale/es-ES.js +8 -1
- package/lib/es/locale/fa-IR.js +8 -1
- package/lib/es/locale/fr-FR.js +8 -1
- package/lib/es/locale/ja-JP.js +8 -1
- package/lib/es/locale/ko-KR.js +8 -1
- package/lib/es/locale/ru-RU.js +8 -1
- package/lib/es/locale/sk-SK.js +8 -1
- package/lib/es/locale/vi-VN.js +8 -1
- package/lib/es/locale/zh-CN.js +23 -1
- package/lib/es/locale/zh-TW.js +8 -1
- package/lib/facade.js +205 -1
- package/lib/index.js +923 -1
- package/lib/locale/ca-ES.js +8 -1
- package/lib/locale/en-US.js +23 -1
- package/lib/locale/es-ES.js +8 -1
- package/lib/locale/fa-IR.js +8 -1
- package/lib/locale/fr-FR.js +8 -1
- package/lib/locale/ja-JP.js +8 -1
- package/lib/locale/ko-KR.js +8 -1
- package/lib/locale/ru-RU.js +8 -1
- package/lib/locale/sk-SK.js +8 -1
- package/lib/locale/vi-VN.js +8 -1
- package/lib/locale/zh-CN.js +23 -1
- package/lib/locale/zh-TW.js +8 -1
- package/lib/umd/index.js +1 -1
- package/package.json +8 -8
package/lib/es/index.js
CHANGED
|
@@ -1 +1,923 @@
|
|
|
1
|
-
import{ColorKit as e,CommandType as t,DependentOn as n,Disposable as r,EDITOR_ACTIVATED as i,ICommandService as a,IConfigService as o,IContextService as s,IUndoRedoService as c,IUniverInstanceService as l,Inject as u,Injector as d,ObjectMatrix as f,Plugin as p,Rectangle as m,ThemeService as h,Tools as g,UniverInstanceType as _,fromCallback as v,groupBy as y,merge as b,replaceInDocumentBody as ee,rotate as x}from"@univerjs/core";import{SelectRangeCommand as S,SetRangeValuesCommand as C,SetSelectionsOperation as te,SetWorksheetActivateCommand as ne,SetWorksheetActiveOperation as re,SheetsSelectionsService as ie,UniverSheetsPlugin as w}from"@univerjs/sheets";import{IRenderManagerService as T,RENDER_RAW_FORMULA_KEY as ae,Rect as oe,Shape as se}from"@univerjs/engine-render";import{FindBy as E,FindDirection as D,FindModel as ce,FindReplaceController as le,FindScope as O,IFindReplaceService as ue,UniverFindReplacePlugin as de}from"@univerjs/find-replace";import{ScrollToCellCommand as fe,SheetSkeletonManagerService as pe,getCoordByCell as k,getSheetObject as me}from"@univerjs/sheets-ui";import{Subject as A,debounceTime as he,filter as j,merge as M,skip as N,throttleTime as P}from"rxjs";const F={id:`sheet.command.replace`,type:t.COMMAND,handler:async(e,t)=>{let n=e.get(c),r=e.get(a),{unitId:i,replacements:o}=t,s=n.__tempBatchingUndoRedo(i),l=await Promise.all(o.map(e=>r.executeCommand(C.id,{unitId:i,subUnitId:e.subUnitId,value:e.value})));return s.dispose(),I(l,o)}};function I(e,t){let n=0,r=0;return e.forEach((e,i)=>{let a=t[i].count;e?n+=a:r+=a}),{success:n,failure:r}}function L(e){"@babel/helpers - typeof";return L=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},L(e)}function ge(e,t){if(L(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(L(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function _e(e){var t=ge(e,`string`);return L(t)==`symbol`?t:t+``}function R(e,t,n){return(t=_e(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var ve=class extends se{constructor(e,t){super(e,t),R(this,`_activated`,!1),R(this,`_inHiddenRange`,!1),R(this,`_color`,void 0),t&&this.setShapeProps(t)}setShapeProps(e){this._activated=!!e.activated,e.inHiddenRange!==void 0&&(this._inHiddenRange=e.inHiddenRange),e.color!==void 0&&(this._color=e.color),this.transformByState({width:e.width,height:e.height})}_draw(e){let t=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})`;oe.drawWith(e,{width:this.width,height:this.height,fill:n,stroke:t?r:void 0,strokeWidth:t?2:0,evented:!1})}};function z(e,t){return e.startRow===t.startRow&&e.startColumn===t.startColumn}function B(e,t){return e.startRow<t.startRow||e.startRow===t.startRow&&e.startColumn<=t.startColumn}function V(e,t){return e.startColumn<t.startColumn||e.startColumn===t.startColumn&&e.startRow<=t.startRow}function ye(e,t){return e.startRow>t.startRow||e.startRow===t.startRow&&e.startColumn>=t.startColumn}function be(e,t){return e.startColumn>t.startColumn||e.startColumn===t.startColumn&&e.startRow>=t.startRow}function H(e,t){let{range:n}=e,{startRow:r,startColumn:i}=n,a=t.getMergedCell(r,i);return a?m.equals(n,a):n.endRow===n.startRow&&n.endColumn===n.startColumn}function U(e,t){return function(n,r){t(n,r,e)}}function W(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let G=class extends r{constructor(e,t,n,r,i){super(),this._injector=e,this._findReplaceController=t,this._contextService=n,this._findReplaceService=r,this._commandService=i,R(this,`_provider`,void 0),this._init(),this._initCommands()}dispose(){super.dispose(),this._findReplaceController.closePanel(),this._provider.dispose()}_init(){let e=this._injector.createInstance(J);this._provider=e,this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(e)),this.disposeWithMe(this._contextService.subscribeContextValue$(i).pipe(j(e=>!!e)).subscribe(()=>this._findReplaceController.closePanel()))}_initCommands(){[F].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};G=W([U(0,u(d)),U(1,u(le)),U(2,s),U(3,ue),U(4,a)],G);let K=class extends ce{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}constructor(e,t,n,r,i,a,o,s){super(),this._workbook=e,this._sheetSkeletonManagerService=t,this._univerInstanceService=n,this._renderManagerService=r,this._commandService=i,this._contextService=a,this._themeService=o,R(this,`_matchesUpdate$`,new A),R(this,`matchesUpdate$`,this._matchesUpdate$.asObservable()),R(this,`_activelyChangingMatch$`,new A),R(this,`activelyChangingMatch$`,this._activelyChangingMatch$.asObservable()),R(this,`_matchesByWorksheet`,new Map),R(this,`_matches`,[]),R(this,`_matchesPosition`,0),R(this,`_activeHighlightIndex`,-1),R(this,`_highlightShapes`,[]),R(this,`_currentHighlightShape`,null),R(this,`_query`,null),R(this,`_workbookSelections`,void 0),this._workbookSelections=s.getWorkbookSelections(this.unitId)}dispose(){super.dispose(),this._disposeHighlights(),this._toggleDisplayRawFormula(!1)}getMatches(){return this._matches}start(e){switch(this._query=e,e.findBy===E.FORMULA?this._toggleDisplayRawFormula(!0):this._toggleDisplayRawFormula(!1),e.findScope){case O.UNIT:this.findInWorkbook(e);break;case O.SUBUNIT:default:this.findInActiveWorksheet(e);break}}focusSelection(){let e=this.currentMatch;e&&this._commandService.executeCommand(S.id,{unitId:e.unitId,subUnit:e.range.subUnitId,range:e.range.range})}_toggleDisplayRawFormula(e){this._contextService.setContextValue(ae,e)}findInWorkbook(e){let n=this._workbook.getUnitId(),r,i=!0,a=()=>{this._matches=this._workbook.getSheets().filter(e=>!e.isSheetHidden()).map(t=>{let r=this._findInWorksheet(t,e,n),i=t.getSheetId(),{results:a}=r;return a.length?this._matchesByWorksheet.set(i,r.results):this._matchesByWorksheet.delete(i),r}).map(e=>e.results).flat(),this._updateFindHighlight(),i?(r={results:this._matches},i=!1):this._matchesUpdate$.next(this._matches)};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>{this._updateFindHighlight(),this._updateCurrentHighlightShape(this._activeHighlightIndex)})),this.disposeWithMe(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e,t])=>e.id===re.id&&!(t!=null&&t.fromFindReplace))).subscribe(()=>{let e=this._workbook.getActiveSheet();if(!e)return;let t=e.getSheetId();this._matchesByWorksheet.has(t)&&this._findNextMatchOnActiveSheetChange(e)})),this.disposeWithMe(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e])=>e.type===t.MUTATION&&e.params.unitId===this._workbook.getUnitId()),P(600,void 0,{leading:!1,trailing:!0})).subscribe(()=>a())),a(),r}_findNextMatchOnActiveSheetChange(e){let t,n,r=0,i=this._matchesByWorksheet.get(e.getSheetId()),a=this._workbookSelections.getCurrentSelections();a!=null&&a.length?([t,r]=this._findNextMatchByRange(i,a[0].range),n=i.findIndex(e=>e===t)):(t=i[0],n=0,r=this._matches.findIndex(e=>e===t)),this._matchesPosition=r+1,this._activelyChangingMatch$.next(t),this._activeHighlightIndex=n,this._updateFindHighlight(),this._updateCurrentHighlightShape(n)}findInActiveWorksheet(e){let n=this._workbook.getUnitId(),r=()=>{var e;let t=this._workbook.getActiveSheet();if(!t)return!1;let n=this._workbookSelections.getCurrentSelections();return(e=n==null?void 0:n.some(e=>!H(e,t)))==null?!1:e},i,a=!0,o=!1,s=()=>{let t=this._workbook.getActiveSheet();if(!t)return{results:[]};let s=this.currentMatch;o=r();let c=this._workbookSelections.getCurrentSelections(),l=o?this._findInSelections(t,c,e,n):this._findInWorksheet(t,e,n);return this._matches=l.results,this._matchesPosition=this._tryRestoreLastMatchesPosition(s,this._matches),a?(i=l,a=!1):this._matchesUpdate$.next(this._matches),this._updateFindHighlight(),l};return this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(()=>this._updateFindHighlight())),this.disposeWithMe(M(v(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(j(([e])=>{if(e.type===t.MUTATION&&e.params.unitId===this._workbook.getUnitId())return!0;if(e.id===te.id&&e.params.unitId===n){let e=r();return e===!1&&o===!1?!1:(o=e,!0)}return!1})),this._workbook.activeSheet$.pipe(N(1))).pipe(he(200)).subscribe(()=>s())),s(),i}_findInRange(e,t,n,r,i){let a=[],o=e.getSheetId(),s=(t.findDirection===D.COLUMN?e.iterateByColumn:e.iterateByRow).bind(e)(n);for(let n of s){let{row:s,col:c,colSpan:l,rowSpan:u,value:d}=n;if(i!=null&&i(s,c)||!d||e.getRowFiltered(s))continue;let{hit:f,replaceable:p,isFormula:m}=xe(e,s,c,t,d);if(f){let e={provider:`sheets-find-replace-provider`,unitId:r,replaceable:p,isFormula:m,range:{subUnitId:o,range:{startRow:s,startColumn:c,endColumn:c+(l==null?1:l)-1,endRow:s+(u==null?1:u)-1}}};a.push(e)}}return{results:a}}_findInSelections(e,t,n,r){let{findDirection:i}=n,a=i===D.ROW?B:V,o=new Set;return{results:t.map(t=>this._findInRange(e,n,t.range,r,(e,t)=>{let n=`${e}-${t}`;return o.has(n)?!0:(o.add(n),!1)}).results).flat().sort((e,t)=>a(e.range.range,t.range.range)?-1:1)}}_findInWorksheet(e,t,n){let r=e.getRowCount(),i=e.getColumnCount(),a={startRow:0,startColumn:0,endRow:r-1,endColumn:i-1};return this._findInRange(e,t,a,n)}_disposeHighlights(){var e;this._highlightShapes.forEach(e=>{var t;(t=e.getScene())==null||t.makeDirty(),e.dispose()}),this._highlightShapes=[],(e=this._currentHighlightShape)==null||e.dispose(),this._currentHighlightShape=null}_updateFindHighlight(){this._disposeHighlights();let t=this._sheetSkeletonManagerService.getCurrentSkeleton();if(!t)return;let n=this._workbook.getUnitId(),r=this._renderManagerService.getRenderById(n);if(r==null)return;let{scene:i}=r,a=this._matches,o=new e(this._themeService.getColorFromTheme(`yellow.400`)).toRgb(),s=this._workbook.getActiveSheet();if(!s)return;let c=s.getSheetId(),l=a.filter(e=>e.range.subUnitId===c).map((e,n)=>{let{startColumn:r,startRow:a,endColumn:c,endRow:l}=e.range.range,u=k(a,r,i,t),d=k(l,c,i,t),{startX:f,startY:p}=u,{endX:m,endY:h}=d,g=!0;for(let e=a;e<=l;e++)if(s.getRowRawVisible(e)){g=!1;break}let _=!0;for(let e=r;e<=c;e++)if(s.getColVisible(e)){_=!1;break}let v=g||_,y={left:f,top:p,color:o,width:_?2:m-f,height:g?2:h-p,evented:!1,inHiddenRange:v,zIndex:1e4};return new ve(`find-highlight-${n}`,y)});i.addObjects(l),this._highlightShapes=l,i.makeDirty()}_updateCurrentHighlightShape(e){var t;if((t=this._currentHighlightShape)==null||t.setShapeProps({activated:!1}),this._currentHighlightShape=null,e!==void 0){let t=this._highlightShapes[e];if(!t)return;this._currentHighlightShape=t,t.setShapeProps({activated:!0})}}_getSheetObject(){return me(this._univerInstanceService,this._renderManagerService)}_focusMatch(e){var t;let n=e.range.subUnitId;n!==((t=this._workbook.getActiveSheet())==null?void 0:t.getSheetId())&&this._commandService.executeCommand(ne.id,{unitId:this._workbook.getUnitId(),subUnitId:n},{fromFindReplace:!0}),this._commandService.executeCommand(fe.id,{range:e.range.range},{fromFindReplace:!0})}_tryRestoreLastMatchesPosition(e,t){if(!e)return 0;let{subUnitId:n}=e.range,{startColumn:r,startRow:i}=e.range.range,a=t.findIndex(e=>{if(n!==e.range.subUnitId)return!1;let{startColumn:t,startRow:a}=e.range.range;return t===r&&a===i});return a>-1?a+1:0}moveToNextMatch(e){var t,n,r,i;if(!this._matches.length)return null;let a=(t=e==null?void 0:e.loop)==null?!1:t,o=(n=e==null?void 0:e.stayIfOnMatch)==null?!1:n,s=(r=e==null?void 0:e.noFocus)==null?!1:r,c=(i=e==null?void 0:e.ignoreSelection)==null?!1:i,l=this._findNextMatch(a,o,c);if(l){var u;let[e,t]=l;return this._matchesPosition=t+1,this._query.findScope===O.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,s||this._focusMatch(e),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===e.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}moveToPreviousMatch(e){var t,n,r,i;if(!this._matches.length)return null;let a=(t=e==null?void 0:e.loop)==null?!1:t,o=(n=e==null?void 0:e.stayIfOnMatch)==null?!1:n,s=(r=e==null?void 0:e.noFocus)==null?!1:r,c=(i=e==null?void 0:e.ignoreSelection)==null?!1:i,l=this._findPreviousMatch(a,o,c);if(l){var u;let[e,t]=l;return this._matchesPosition=t+1,this._query.findScope===O.UNIT?this._activeHighlightIndex=this._matchesByWorksheet.get(e.range.subUnitId).findIndex(t=>t===e):this._activeHighlightIndex=t,s||this._focusMatch(e),((u=this._workbook.getActiveSheet())==null?void 0:u.getSheetId())===e.range.subUnitId&&this._updateCurrentHighlightShape(this._activeHighlightIndex),e}return this._matchesPosition=0,this._updateCurrentHighlightShape(),null}_findPreviousMatch(e=!1,t=!1,n=!1){var r;if(this.currentMatch){let n=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,n];let r=n-1;if(!e&&r<0)return null;let i=this._matches.length,a=(r+i)%i;return[this._matches[a],a]}let i=this._workbookSelections.getCurrentLastSelection();if(n||!i){let e=this._matches.length-1;return[this._matches[e],e]}if(this._query.findScope!==O.UNIT)return this._findPreviousMatchByRange(this._matches,i.range);let a=(r=this._workbook.getActiveSheet())==null?void 0:r.getSheetId();if(!a)return null;let o=this._findPreviousWorksheetThatHasAMatch(a,e);return o?this._findPreviousMatchByRange(this._matchesByWorksheet.get(o),i.range):null}_findNextMatch(e=!1,t=!1,n=!1){var r;if(this.currentMatch){let n=this._matches.findIndex(e=>e===this.currentMatch);if(t)return[this.currentMatch,n];let r=n+1,i=this._matches.length;if(!e&&r>=i)return null;let a=r%i;return[this._matches[a],a]}let i=this._workbookSelections.getCurrentLastSelection();if(n||!i)return[this._matches[0],0];if(this._query.findScope!==O.UNIT)return this._findNextMatchByRange(this._matches,i.range,t);let a=(r=this._workbook.getActiveSheet())==null?void 0:r.getSheetId();if(!a)return null;let o=this._findNextWorksheetThatHasAMatch(a,e);return o?this._findNextMatchByRange(this._matchesByWorksheet.get(o),i.range):null}_findPreviousWorksheetThatHasAMatch(e,t=!1){let n=this._workbook.getSheetOrders(),r=n.findIndex(t=>t===e),i=(t?x(n,r+1):n.slice(0,r+1)).findLast(e=>this._matchesByWorksheet.has(e));return i==null?null:i}_findNextWorksheetThatHasAMatch(e,t=!1){let n=this._workbook.getSheetOrders(),r=n.findIndex(t=>t===e),i=(t?x(n,r):n.slice(r)).find(e=>this._matchesByWorksheet.has(e));return i==null?null:i}_findNextMatchByRange(e,t,n=!1){let r=this._query.findDirection===D.ROW,i=e.findIndex(e=>{let i=e.range.range;if(!(r?B(t,i):V(t,i)))return!1;let a=z(t,i);return n?a:!a});i===-1&&(i=e.length-1);let a=e[i];return[a,this._matches.findIndex(e=>e===a)]}_findPreviousMatchByRange(e,t,n=!1){let r=this._query.findDirection===D.ROW,i=this._matches.findLastIndex(e=>{let i=e.range.range;if(!(r?ye(t,i):be(t,i)))return!1;let a=z(t,i);return n?a:!a});i===-1&&(i=0);let a=e[i];return[a,this._matches.findIndex(e=>e===a)]}async replace(e){if(this._matchesCount===0||!this.currentMatch||!this._query||!this.currentMatch.replaceable)return!1;let t=this.currentMatch.range,n=this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId),r=this._getReplacedCellData(this.currentMatch,n,this._query.findBy===E.FORMULA,this._query.findString,e,this._query.caseSensitive?`g`:`ig`),i={unitId:this.currentMatch.unitId,subUnitId:t.subUnitId,value:{[t.range.startRow]:{[t.range.startColumn]:r}}};return this._commandService.executeCommand(C.id,i)}async replaceAll(e){if(this._matchesCount===0||!this._query)return{success:0,failure:0};let t=this._workbook.getUnitId(),{findString:n,caseSensitive:r,findBy:i}=this._query,a=i===E.FORMULA,o=r?`g`:`ig`,s=[];return y(this._matches.filter(e=>e.replaceable),e=>e.range.subUnitId).forEach((t,r)=>{let i=new f,c=this._workbook.getSheetBySheetId(r);t.forEach(t=>{let{startColumn:r,startRow:s}=t.range.range,l=this._getReplacedCellData(t,c,a,n,e,o);l&&i.setValue(s,r,l)}),s.push({count:t.length,subUnitId:r,value:i.getMatrix()})}),s?this._commandService.executeCommand(F.id,{unitId:t,replacements:s}):{success:0,failure:0}}_getReplacedCellData(e,t,n,r,i,a){var o;let{startRow:s,startColumn:c}=e.range.range,l=t.getCellRaw(s,c);if(e.isFormula)return n?{f:l.f.replace(new RegExp(q(r),a),i),v:null}:null;if((o=l.p)!=null&&o.body){let e=g.deepClone(l.p);return ee(e.body,r,i,this._query.caseSensitive),{p:e}}return{v:l.v.toString().replace(new RegExp(q(r),a),i)}}};K=W([U(2,l),U(3,T),U(4,a),U(5,s),U(6,u(h)),U(7,u(ie))],K);function q(e){return e.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,`\\$&`)}let J=class extends r{constructor(e,t,n){super(),this._univerInstanceService=e,this._renderManagerService=t,this._injector=n,R(this,`_findModelsByUnitId`,new Map)}async find(e){this._terminate();let t=this._univerInstanceService.getCurrentUnitOfType(_.UNIVER_SHEET);if(!t)return[];let n=this._preprocessQuery(e),r=this._renderManagerService.getRenderById(t.getUnitId()).with(pe),i=this._injector.createInstance(K,t,r);return this._findModelsByUnitId.set(t.getUnitId(),i),i.start(n),[i]}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}}};J=W([U(0,l),U(1,T),U(2,u(d))],J);const Y={hit:!1,replaceable:!1,isFormula:!1,rawData:null};function xe(e,t,n,r,i){let{findBy:a}=r,o=a===E.FORMULA,s=e.getCellRaw(t,n);return Y.rawData=s,s!=null&&s.f?(Y.isFormula=!0,o?X({v:s.f},r)?(Y.hit=!0,Y.replaceable=!0,Y):(Y.hit=!1,Y.replaceable=!1,Y):(Y.replaceable=!1,X(i,r)?Y.hit=!0:Y.hit=!1,Y)):(Y.isFormula=!1,X(i,r)?s?(Y.hit=!0,Y.replaceable=!0):(Y.hit=!0,Y.replaceable=!1):(Y.hit=!1,Y.replaceable=!1),Y)}function X(e,t){let n=Se(e);return n?t.matchesTheWholeCell?(n=Ce(n),t.caseSensitive?n===t.findString:n.toLowerCase()===t.findString):t.caseSensitive?n.indexOf(t.findString)>-1:n.toLowerCase().indexOf(t.findString)>-1:!1}function Se(e){var t,n;let r=(t=e==null||(n=e.p)==null||(n=n.body)==null?void 0:n.dataStream)==null?e==null?void 0:e.v:t;return typeof r==`number`?`${r}`:typeof r==`boolean`?r?`1`:`0`:r}function Ce(e){return e.replace(/^ +/g,``).replace(/ +$/g,``)}var we=`@univerjs/sheets-find-replace`,Te=`0.20.1`;const Z=`sheets-find-replace.config`;Symbol(Z);const Q={};let $=class extends p{constructor(e=Q,t,n){super(),this._config=e,this._injector=t,this._configService=n;let{...r}=b({},Q,this._config);this._configService.setConfig(Z,r)}onStarting(){[[G]].forEach(e=>this._injector.add(e))}onSteady(){this._injector.get(G)}};R($,`pluginName`,`SHEET_FIND_REPLACE_PLUGIN`),R($,`packageName`,we),R($,`version`,Te),R($,`type`,_.UNIVER_SHEET),$=W([n(w,w,de),U(1,u(d)),U(2,o)],$);export{F as SheetReplaceCommand,G as SheetsFindReplaceController,$ as UniverSheetsFindReplacePlugin};
|
|
1
|
+
import { ColorKit, CommandType, DependentOn, Disposable, EDITOR_ACTIVATED, ICommandService, IConfigService, IContextService, IUndoRedoService, IUniverInstanceService, Inject, Injector, ObjectMatrix, Plugin, Rectangle, ThemeService, Tools, UniverInstanceType, fromCallback, groupBy, merge, replaceInDocumentBody, rotate } from "@univerjs/core";
|
|
2
|
+
import { SelectRangeCommand, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetActivateCommand, SetWorksheetActiveOperation, SheetsSelectionsService, UniverSheetsPlugin } from "@univerjs/sheets";
|
|
3
|
+
import { IRenderManagerService, RENDER_RAW_FORMULA_KEY, Rect, Shape } from "@univerjs/engine-render";
|
|
4
|
+
import { FindBy, FindDirection, FindModel, FindReplaceController, FindScope, IFindReplaceService, UniverFindReplacePlugin } from "@univerjs/find-replace";
|
|
5
|
+
import { ScrollToCellCommand, SheetSkeletonManagerService, getCoordByCell, getSheetObject } from "@univerjs/sheets-ui";
|
|
6
|
+
import { Subject, debounceTime, filter, merge as merge$1, skip, throttleTime } from "rxjs";
|
|
7
|
+
|
|
8
|
+
//#region src/commands/commands/sheet-replace.command.ts
|
|
9
|
+
/**
|
|
10
|
+
* This command is used for the SheetFindReplaceController to deal with replacing, including undo redo.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
const SheetReplaceCommand = {
|
|
14
|
+
id: "sheet.command.replace",
|
|
15
|
+
type: CommandType.COMMAND,
|
|
16
|
+
handler: async (accessor, params) => {
|
|
17
|
+
const undoRedoService = accessor.get(IUndoRedoService);
|
|
18
|
+
const commandService = accessor.get(ICommandService);
|
|
19
|
+
const { unitId, replacements } = params;
|
|
20
|
+
const disposeBatchingHandler = undoRedoService.__tempBatchingUndoRedo(unitId);
|
|
21
|
+
const results = await Promise.all(replacements.map((replacement) => commandService.executeCommand(SetRangeValuesCommand.id, {
|
|
22
|
+
unitId,
|
|
23
|
+
subUnitId: replacement.subUnitId,
|
|
24
|
+
value: replacement.value
|
|
25
|
+
})));
|
|
26
|
+
disposeBatchingHandler.dispose();
|
|
27
|
+
return getReplaceAllResult(results, replacements);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function getReplaceAllResult(results, replacements) {
|
|
31
|
+
let success = 0;
|
|
32
|
+
let failure = 0;
|
|
33
|
+
results.forEach((r, index) => {
|
|
34
|
+
const count = replacements[index].count;
|
|
35
|
+
if (r) success += count;
|
|
36
|
+
else failure += count;
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
success,
|
|
40
|
+
failure
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
|
|
46
|
+
function _typeof(o) {
|
|
47
|
+
"@babel/helpers - typeof";
|
|
48
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
49
|
+
return typeof o;
|
|
50
|
+
} : function(o) {
|
|
51
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
52
|
+
}, _typeof(o);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
|
|
57
|
+
function toPrimitive(t, r) {
|
|
58
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
59
|
+
var e = t[Symbol.toPrimitive];
|
|
60
|
+
if (void 0 !== e) {
|
|
61
|
+
var i = e.call(t, r || "default");
|
|
62
|
+
if ("object" != _typeof(i)) return i;
|
|
63
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
64
|
+
}
|
|
65
|
+
return ("string" === r ? String : Number)(t);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
|
|
70
|
+
function toPropertyKey(t) {
|
|
71
|
+
var i = toPrimitive(t, "string");
|
|
72
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
|
|
77
|
+
function _defineProperty(e, r, t) {
|
|
78
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
79
|
+
value: t,
|
|
80
|
+
enumerable: !0,
|
|
81
|
+
configurable: !0,
|
|
82
|
+
writable: !0
|
|
83
|
+
}) : e[r] = t, e;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/views/shapes/find-replace-highlight.shape.ts
|
|
88
|
+
var SheetFindReplaceHighlightShape = class extends Shape {
|
|
89
|
+
constructor(key, props) {
|
|
90
|
+
super(key, props);
|
|
91
|
+
_defineProperty(this, "_activated", false);
|
|
92
|
+
_defineProperty(this, "_inHiddenRange", false);
|
|
93
|
+
_defineProperty(this, "_color", void 0);
|
|
94
|
+
if (props) this.setShapeProps(props);
|
|
95
|
+
}
|
|
96
|
+
setShapeProps(props) {
|
|
97
|
+
this._activated = !!props.activated;
|
|
98
|
+
if (typeof props.inHiddenRange !== "undefined") this._inHiddenRange = props.inHiddenRange;
|
|
99
|
+
if (typeof props.color !== "undefined") this._color = props.color;
|
|
100
|
+
this.transformByState({
|
|
101
|
+
width: props.width,
|
|
102
|
+
height: props.height
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
_draw(ctx) {
|
|
106
|
+
const activated = this._activated;
|
|
107
|
+
const color = `rgba(${this._color.r}, ${this._color.g}, ${this._color.b}, 0.35)`;
|
|
108
|
+
const borderColor = `rgb(${this._color.r}, ${this._color.g}, ${this._color.b})`;
|
|
109
|
+
Rect.drawWith(ctx, {
|
|
110
|
+
width: this.width,
|
|
111
|
+
height: this.height,
|
|
112
|
+
fill: color,
|
|
113
|
+
stroke: activated ? borderColor : void 0,
|
|
114
|
+
strokeWidth: activated ? 2 : 0,
|
|
115
|
+
evented: false
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/controllers/utils.ts
|
|
122
|
+
function isSamePosition(range1, range2) {
|
|
123
|
+
return range1.startRow === range2.startRow && range1.startColumn === range2.startColumn;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Tell if `range2` is after (or the same as) `range1` with row direction is at priority.
|
|
127
|
+
* @param range1
|
|
128
|
+
* @param range2
|
|
129
|
+
* @returns
|
|
130
|
+
*/
|
|
131
|
+
function isBehindPositionWithRowPriority(range1, range2) {
|
|
132
|
+
return range1.startRow < range2.startRow || range1.startRow === range2.startRow && range1.startColumn <= range2.startColumn;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Tell if `range2` is after (or the same as) `range1` with column direction is at priority.
|
|
136
|
+
* @param range1
|
|
137
|
+
* @param range2
|
|
138
|
+
* @returns
|
|
139
|
+
*/
|
|
140
|
+
function isBehindPositionWithColumnPriority(range1, range2) {
|
|
141
|
+
return range1.startColumn < range2.startColumn || range1.startColumn === range2.startColumn && range1.startRow <= range2.startRow;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Tell if `range2` is before (or the same as) `range1` with column direction is at priority.
|
|
145
|
+
* @param range1
|
|
146
|
+
* @param range2
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
function isBeforePositionWithRowPriority(range1, range2) {
|
|
150
|
+
return range1.startRow > range2.startRow || range1.startRow === range2.startRow && range1.startColumn >= range2.startColumn;
|
|
151
|
+
}
|
|
152
|
+
function isBeforePositionWithColumnPriority(range1, range2) {
|
|
153
|
+
return range1.startColumn > range2.startColumn || range1.startColumn === range2.startColumn && range1.startRow >= range2.startRow;
|
|
154
|
+
}
|
|
155
|
+
function isSelectionSingleCell(selection, worksheet) {
|
|
156
|
+
const { range } = selection;
|
|
157
|
+
const { startRow, startColumn } = range;
|
|
158
|
+
const hasMergedCell = worksheet.getMergedCell(startRow, startColumn);
|
|
159
|
+
if (hasMergedCell) return Rectangle.equals(range, hasMergedCell);
|
|
160
|
+
else return range.endRow === range.startRow && range.endColumn === range.startColumn;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/decorateParam.js
|
|
165
|
+
function __decorateParam(paramIndex, decorator) {
|
|
166
|
+
return function(target, key) {
|
|
167
|
+
decorator(target, key, paramIndex);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
|
|
173
|
+
function __decorate(decorators, target, key, desc) {
|
|
174
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
175
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
176
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
177
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/controllers/sheet-find-replace.controller.ts
|
|
182
|
+
let SheetsFindReplaceController = class SheetsFindReplaceController extends Disposable {
|
|
183
|
+
constructor(_injector, _findReplaceController, _contextService, _findReplaceService, _commandService) {
|
|
184
|
+
super();
|
|
185
|
+
this._injector = _injector;
|
|
186
|
+
this._findReplaceController = _findReplaceController;
|
|
187
|
+
this._contextService = _contextService;
|
|
188
|
+
this._findReplaceService = _findReplaceService;
|
|
189
|
+
this._commandService = _commandService;
|
|
190
|
+
_defineProperty(this, "_provider", void 0);
|
|
191
|
+
this._init();
|
|
192
|
+
this._initCommands();
|
|
193
|
+
}
|
|
194
|
+
dispose() {
|
|
195
|
+
super.dispose();
|
|
196
|
+
this._findReplaceController.closePanel();
|
|
197
|
+
this._provider.dispose();
|
|
198
|
+
}
|
|
199
|
+
_init() {
|
|
200
|
+
const provider = this._injector.createInstance(SheetsFindReplaceProvider);
|
|
201
|
+
this._provider = provider;
|
|
202
|
+
this.disposeWithMe(this._findReplaceService.registerFindReplaceProvider(provider));
|
|
203
|
+
this.disposeWithMe(this._contextService.subscribeContextValue$(EDITOR_ACTIVATED).pipe(filter((v) => !!v)).subscribe(() => this._findReplaceController.closePanel()));
|
|
204
|
+
}
|
|
205
|
+
_initCommands() {
|
|
206
|
+
[SheetReplaceCommand].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
SheetsFindReplaceController = __decorate([
|
|
210
|
+
__decorateParam(0, Inject(Injector)),
|
|
211
|
+
__decorateParam(1, Inject(FindReplaceController)),
|
|
212
|
+
__decorateParam(2, IContextService),
|
|
213
|
+
__decorateParam(3, IFindReplaceService),
|
|
214
|
+
__decorateParam(4, ICommandService)
|
|
215
|
+
], SheetsFindReplaceController);
|
|
216
|
+
const SHEETS_FIND_REPLACE_PROVIDER_NAME = "sheets-find-replace-provider";
|
|
217
|
+
const FIND_REPLACE_Z_INDEX = 1e4;
|
|
218
|
+
let SheetFindModel = class SheetFindModel extends FindModel {
|
|
219
|
+
get _matchesCount() {
|
|
220
|
+
return this._matches.length;
|
|
221
|
+
}
|
|
222
|
+
get unitId() {
|
|
223
|
+
return this._workbook.getUnitId();
|
|
224
|
+
}
|
|
225
|
+
get matchesCount() {
|
|
226
|
+
return this._matchesCount;
|
|
227
|
+
}
|
|
228
|
+
get matchesPosition() {
|
|
229
|
+
return this._matchesPosition;
|
|
230
|
+
}
|
|
231
|
+
get currentMatch() {
|
|
232
|
+
return this._matchesPosition > 0 ? this._matches[this._matchesPosition - 1] : null;
|
|
233
|
+
}
|
|
234
|
+
constructor(_workbook, _sheetSkeletonManagerService, _univerInstanceService, _renderManagerService, _commandService, _contextService, _themeService, _selectionManagerService) {
|
|
235
|
+
super();
|
|
236
|
+
this._workbook = _workbook;
|
|
237
|
+
this._sheetSkeletonManagerService = _sheetSkeletonManagerService;
|
|
238
|
+
this._univerInstanceService = _univerInstanceService;
|
|
239
|
+
this._renderManagerService = _renderManagerService;
|
|
240
|
+
this._commandService = _commandService;
|
|
241
|
+
this._contextService = _contextService;
|
|
242
|
+
this._themeService = _themeService;
|
|
243
|
+
_defineProperty(this, "_matchesUpdate$", new Subject());
|
|
244
|
+
_defineProperty(this, "matchesUpdate$", this._matchesUpdate$.asObservable());
|
|
245
|
+
_defineProperty(this, "_activelyChangingMatch$", new Subject());
|
|
246
|
+
_defineProperty(this, "activelyChangingMatch$", this._activelyChangingMatch$.asObservable());
|
|
247
|
+
_defineProperty(this, "_matchesByWorksheet", /* @__PURE__ */ new Map());
|
|
248
|
+
_defineProperty(this, "_matches", []);
|
|
249
|
+
_defineProperty(this, "_matchesPosition", 0);
|
|
250
|
+
_defineProperty(this, "_activeHighlightIndex", -1);
|
|
251
|
+
_defineProperty(this, "_highlightShapes", []);
|
|
252
|
+
_defineProperty(this, "_currentHighlightShape", null);
|
|
253
|
+
_defineProperty(this, "_query", null);
|
|
254
|
+
_defineProperty(this, "_workbookSelections", void 0);
|
|
255
|
+
this._workbookSelections = _selectionManagerService.getWorkbookSelections(this.unitId);
|
|
256
|
+
}
|
|
257
|
+
dispose() {
|
|
258
|
+
super.dispose();
|
|
259
|
+
this._disposeHighlights();
|
|
260
|
+
this._toggleDisplayRawFormula(false);
|
|
261
|
+
}
|
|
262
|
+
getMatches() {
|
|
263
|
+
return this._matches;
|
|
264
|
+
}
|
|
265
|
+
start(query) {
|
|
266
|
+
this._query = query;
|
|
267
|
+
if (query.findBy === FindBy.FORMULA) this._toggleDisplayRawFormula(true);
|
|
268
|
+
else this._toggleDisplayRawFormula(false);
|
|
269
|
+
switch (query.findScope) {
|
|
270
|
+
case FindScope.UNIT:
|
|
271
|
+
this.findInWorkbook(query);
|
|
272
|
+
break;
|
|
273
|
+
case FindScope.SUBUNIT:
|
|
274
|
+
default:
|
|
275
|
+
this.findInActiveWorksheet(query);
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
focusSelection() {
|
|
280
|
+
const currentMatch = this.currentMatch;
|
|
281
|
+
if (!currentMatch) return;
|
|
282
|
+
this._commandService.executeCommand(SelectRangeCommand.id, {
|
|
283
|
+
unitId: currentMatch.unitId,
|
|
284
|
+
subUnit: currentMatch.range.subUnitId,
|
|
285
|
+
range: currentMatch.range.range
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
_toggleDisplayRawFormula(force) {
|
|
289
|
+
this._contextService.setContextValue(RENDER_RAW_FORMULA_KEY, force);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Find all matches in the current workbook no matter which worksheet is activated.
|
|
293
|
+
* @param query the query object
|
|
294
|
+
* @returns the query complete event
|
|
295
|
+
*/
|
|
296
|
+
findInWorkbook(query) {
|
|
297
|
+
const unitId = this._workbook.getUnitId();
|
|
298
|
+
let complete;
|
|
299
|
+
let firstSearch = true;
|
|
300
|
+
const findInWorkbook = () => {
|
|
301
|
+
this._matches = this._workbook.getSheets().filter((worksheet) => !worksheet.isSheetHidden()).map((worksheet) => {
|
|
302
|
+
const complete = this._findInWorksheet(worksheet, query, unitId);
|
|
303
|
+
const sheetId = worksheet.getSheetId();
|
|
304
|
+
const { results } = complete;
|
|
305
|
+
if (results.length) this._matchesByWorksheet.set(sheetId, complete.results);
|
|
306
|
+
else this._matchesByWorksheet.delete(sheetId);
|
|
307
|
+
return complete;
|
|
308
|
+
}).map((c) => c.results).flat();
|
|
309
|
+
this._updateFindHighlight();
|
|
310
|
+
if (firstSearch) {
|
|
311
|
+
complete = { results: this._matches };
|
|
312
|
+
firstSearch = false;
|
|
313
|
+
} else this._matchesUpdate$.next(this._matches);
|
|
314
|
+
};
|
|
315
|
+
this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => {
|
|
316
|
+
this._updateFindHighlight();
|
|
317
|
+
this._updateCurrentHighlightShape(this._activeHighlightIndex);
|
|
318
|
+
}));
|
|
319
|
+
this.disposeWithMe(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([command, options]) => command.id === SetWorksheetActiveOperation.id && !(options === null || options === void 0 ? void 0 : options.fromFindReplace))).subscribe(() => {
|
|
320
|
+
const activeSheet = this._workbook.getActiveSheet();
|
|
321
|
+
if (!activeSheet) return;
|
|
322
|
+
const activeSheetId = activeSheet.getSheetId();
|
|
323
|
+
if (!this._matchesByWorksheet.has(activeSheetId)) return;
|
|
324
|
+
this._findNextMatchOnActiveSheetChange(activeSheet);
|
|
325
|
+
}));
|
|
326
|
+
this.disposeWithMe(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([command]) => command.type === CommandType.MUTATION && command.params.unitId === this._workbook.getUnitId()), throttleTime(600, void 0, {
|
|
327
|
+
leading: false,
|
|
328
|
+
trailing: true
|
|
329
|
+
})).subscribe(() => findInWorkbook()));
|
|
330
|
+
findInWorkbook();
|
|
331
|
+
return complete;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* This method is used in `findInWorkbook`. When the active sheet changes, this method helps to find the next match
|
|
335
|
+
* in the new worksheet.
|
|
336
|
+
*/
|
|
337
|
+
_findNextMatchOnActiveSheetChange(activeSheet) {
|
|
338
|
+
let match;
|
|
339
|
+
let index;
|
|
340
|
+
let globalIndex = 0;
|
|
341
|
+
const matchesByWorksheet = this._matchesByWorksheet.get(activeSheet.getSheetId());
|
|
342
|
+
const selections = this._workbookSelections.getCurrentSelections();
|
|
343
|
+
if (!(selections === null || selections === void 0 ? void 0 : selections.length)) {
|
|
344
|
+
match = matchesByWorksheet[0];
|
|
345
|
+
index = 0;
|
|
346
|
+
globalIndex = this._matches.findIndex((m) => m === match);
|
|
347
|
+
} else {
|
|
348
|
+
[match, globalIndex] = this._findNextMatchByRange(matchesByWorksheet, selections[0].range);
|
|
349
|
+
index = matchesByWorksheet.findIndex((m) => m === match);
|
|
350
|
+
}
|
|
351
|
+
this._matchesPosition = globalIndex + 1;
|
|
352
|
+
this._activelyChangingMatch$.next(match);
|
|
353
|
+
this._activeHighlightIndex = index;
|
|
354
|
+
this._updateFindHighlight();
|
|
355
|
+
this._updateCurrentHighlightShape(index);
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Find all matches (only) in the currently activated worksheet.
|
|
359
|
+
* @param query the query object
|
|
360
|
+
* @returns the query complete event
|
|
361
|
+
*/
|
|
362
|
+
findInActiveWorksheet(query) {
|
|
363
|
+
const unitId = this._workbook.getUnitId();
|
|
364
|
+
const checkShouldFindInSelections = () => {
|
|
365
|
+
var _currentSelections$so;
|
|
366
|
+
const currentWorksheet = this._workbook.getActiveSheet();
|
|
367
|
+
if (!currentWorksheet) return false;
|
|
368
|
+
const currentSelections = this._workbookSelections.getCurrentSelections();
|
|
369
|
+
return (_currentSelections$so = currentSelections === null || currentSelections === void 0 ? void 0 : currentSelections.some((selection) => !isSelectionSingleCell(selection, currentWorksheet))) !== null && _currentSelections$so !== void 0 ? _currentSelections$so : false;
|
|
370
|
+
};
|
|
371
|
+
let complete;
|
|
372
|
+
let firstSearch = true;
|
|
373
|
+
let findBySelections = false;
|
|
374
|
+
const performFindInWorksheet = () => {
|
|
375
|
+
const currentWorksheet = this._workbook.getActiveSheet();
|
|
376
|
+
if (!currentWorksheet) return { results: [] };
|
|
377
|
+
const lastMatch = this.currentMatch;
|
|
378
|
+
findBySelections = checkShouldFindInSelections();
|
|
379
|
+
const currentSelections = this._workbookSelections.getCurrentSelections();
|
|
380
|
+
const newComplete = findBySelections ? this._findInSelections(currentWorksheet, currentSelections, query, unitId) : this._findInWorksheet(currentWorksheet, query, unitId);
|
|
381
|
+
this._matches = newComplete.results;
|
|
382
|
+
this._matchesPosition = this._tryRestoreLastMatchesPosition(lastMatch, this._matches);
|
|
383
|
+
if (firstSearch) {
|
|
384
|
+
complete = newComplete;
|
|
385
|
+
firstSearch = false;
|
|
386
|
+
} else this._matchesUpdate$.next(this._matches);
|
|
387
|
+
this._updateFindHighlight();
|
|
388
|
+
return newComplete;
|
|
389
|
+
};
|
|
390
|
+
this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe(() => this._updateFindHighlight()));
|
|
391
|
+
this.disposeWithMe(merge$1(fromCallback(this._commandService.onCommandExecuted.bind(this._commandService)).pipe(filter(([command]) => {
|
|
392
|
+
if (command.type === CommandType.MUTATION && command.params.unitId === this._workbook.getUnitId()) return true;
|
|
393
|
+
if (command.id === SetSelectionsOperation.id && command.params.unitId === unitId) {
|
|
394
|
+
const shouldFindBySelections = checkShouldFindInSelections();
|
|
395
|
+
if (shouldFindBySelections === false && findBySelections === false) return false;
|
|
396
|
+
findBySelections = shouldFindBySelections;
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
})), this._workbook.activeSheet$.pipe(skip(1))).pipe(debounceTime(200)).subscribe(() => performFindInWorksheet()));
|
|
401
|
+
performFindInWorksheet();
|
|
402
|
+
return complete;
|
|
403
|
+
}
|
|
404
|
+
_findInRange(worksheet, query, range, unitId, dedupeFn) {
|
|
405
|
+
const results = [];
|
|
406
|
+
const subUnitId = worksheet.getSheetId();
|
|
407
|
+
const iter = (query.findDirection === FindDirection.COLUMN ? worksheet.iterateByColumn : worksheet.iterateByRow).bind(worksheet)(range);
|
|
408
|
+
for (const value of iter) {
|
|
409
|
+
const { row, col, colSpan, rowSpan, value: cellData } = value;
|
|
410
|
+
if ((dedupeFn === null || dedupeFn === void 0 ? void 0 : dedupeFn(row, col)) || !cellData) continue;
|
|
411
|
+
if (worksheet.getRowFiltered(row)) continue;
|
|
412
|
+
const { hit, replaceable, isFormula } = hitCell(worksheet, row, col, query, cellData);
|
|
413
|
+
if (hit) {
|
|
414
|
+
const result = {
|
|
415
|
+
provider: SHEETS_FIND_REPLACE_PROVIDER_NAME,
|
|
416
|
+
unitId,
|
|
417
|
+
replaceable,
|
|
418
|
+
isFormula,
|
|
419
|
+
range: {
|
|
420
|
+
subUnitId,
|
|
421
|
+
range: {
|
|
422
|
+
startRow: row,
|
|
423
|
+
startColumn: col,
|
|
424
|
+
endColumn: col + (colSpan !== null && colSpan !== void 0 ? colSpan : 1) - 1,
|
|
425
|
+
endRow: row + (rowSpan !== null && rowSpan !== void 0 ? rowSpan : 1) - 1
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
results.push(result);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return { results };
|
|
433
|
+
}
|
|
434
|
+
_findInSelections(worksheet, selections, query, unitId) {
|
|
435
|
+
const { findDirection } = query;
|
|
436
|
+
const sortFn = findDirection === FindDirection.ROW ? isBehindPositionWithRowPriority : isBehindPositionWithColumnPriority;
|
|
437
|
+
const dedupeSet = /* @__PURE__ */ new Set();
|
|
438
|
+
return { results: selections.map((selection) => this._findInRange(worksheet, query, selection.range, unitId, (row, col) => {
|
|
439
|
+
const key = `${row}-${col}`;
|
|
440
|
+
if (dedupeSet.has(key)) return true;
|
|
441
|
+
dedupeSet.add(key);
|
|
442
|
+
return false;
|
|
443
|
+
}).results).flat().sort((a, b) => sortFn(a.range.range, b.range.range) ? -1 : 1) };
|
|
444
|
+
}
|
|
445
|
+
/** Find matches in a given worksheet. */
|
|
446
|
+
_findInWorksheet(worksheet, query, unitId) {
|
|
447
|
+
const rowCount = worksheet.getRowCount();
|
|
448
|
+
const colCount = worksheet.getColumnCount();
|
|
449
|
+
const range = {
|
|
450
|
+
startRow: 0,
|
|
451
|
+
startColumn: 0,
|
|
452
|
+
endRow: rowCount - 1,
|
|
453
|
+
endColumn: colCount - 1
|
|
454
|
+
};
|
|
455
|
+
return this._findInRange(worksheet, query, range, unitId);
|
|
456
|
+
}
|
|
457
|
+
_disposeHighlights() {
|
|
458
|
+
var _this$_currentHighlig;
|
|
459
|
+
this._highlightShapes.forEach((shape) => {
|
|
460
|
+
var _shape$getScene;
|
|
461
|
+
(_shape$getScene = shape.getScene()) === null || _shape$getScene === void 0 || _shape$getScene.makeDirty();
|
|
462
|
+
shape.dispose();
|
|
463
|
+
});
|
|
464
|
+
this._highlightShapes = [];
|
|
465
|
+
(_this$_currentHighlig = this._currentHighlightShape) === null || _this$_currentHighlig === void 0 || _this$_currentHighlig.dispose();
|
|
466
|
+
this._currentHighlightShape = null;
|
|
467
|
+
}
|
|
468
|
+
_updateFindHighlight() {
|
|
469
|
+
this._disposeHighlights();
|
|
470
|
+
const skeleton = this._sheetSkeletonManagerService.getCurrentSkeleton();
|
|
471
|
+
if (!skeleton) return;
|
|
472
|
+
const unitId = this._workbook.getUnitId();
|
|
473
|
+
const currentRender = this._renderManagerService.getRenderById(unitId);
|
|
474
|
+
if (currentRender == null) return;
|
|
475
|
+
const { scene } = currentRender;
|
|
476
|
+
const matches = this._matches;
|
|
477
|
+
const color = new ColorKit(this._themeService.getColorFromTheme("yellow.400")).toRgb();
|
|
478
|
+
const worksheet = this._workbook.getActiveSheet();
|
|
479
|
+
if (!worksheet) return;
|
|
480
|
+
const activeSheetId = worksheet.getSheetId();
|
|
481
|
+
const highlightShapes = matches.filter((match) => match.range.subUnitId === activeSheetId).map((find, index) => {
|
|
482
|
+
const { startColumn, startRow, endColumn, endRow } = find.range.range;
|
|
483
|
+
const startPosition = getCoordByCell(startRow, startColumn, scene, skeleton);
|
|
484
|
+
const endPosition = getCoordByCell(endRow, endColumn, scene, skeleton);
|
|
485
|
+
const { startX, startY } = startPosition;
|
|
486
|
+
const { endX, endY } = endPosition;
|
|
487
|
+
let isAllRowHidden = true;
|
|
488
|
+
for (let row = startRow; row <= endRow; row++) if (worksheet.getRowRawVisible(row)) {
|
|
489
|
+
isAllRowHidden = false;
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
let isAllColHidden = true;
|
|
493
|
+
for (let col = startColumn; col <= endColumn; col++) if (worksheet.getColVisible(col)) {
|
|
494
|
+
isAllColHidden = false;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
const inHiddenRange = isAllRowHidden || isAllColHidden;
|
|
498
|
+
const props = {
|
|
499
|
+
left: startX,
|
|
500
|
+
top: startY,
|
|
501
|
+
color,
|
|
502
|
+
width: isAllColHidden ? 2 : endX - startX,
|
|
503
|
+
height: isAllRowHidden ? 2 : endY - startY,
|
|
504
|
+
evented: false,
|
|
505
|
+
inHiddenRange,
|
|
506
|
+
zIndex: FIND_REPLACE_Z_INDEX
|
|
507
|
+
};
|
|
508
|
+
return new SheetFindReplaceHighlightShape(`find-highlight-${index}`, props);
|
|
509
|
+
});
|
|
510
|
+
scene.addObjects(highlightShapes);
|
|
511
|
+
this._highlightShapes = highlightShapes;
|
|
512
|
+
scene.makeDirty();
|
|
513
|
+
}
|
|
514
|
+
_updateCurrentHighlightShape(matchIndex) {
|
|
515
|
+
var _this$_currentHighlig2;
|
|
516
|
+
(_this$_currentHighlig2 = this._currentHighlightShape) === null || _this$_currentHighlig2 === void 0 || _this$_currentHighlig2.setShapeProps({ activated: false });
|
|
517
|
+
this._currentHighlightShape = null;
|
|
518
|
+
if (matchIndex !== void 0) {
|
|
519
|
+
const shape = this._highlightShapes[matchIndex];
|
|
520
|
+
if (!shape) return;
|
|
521
|
+
this._currentHighlightShape = shape;
|
|
522
|
+
shape.setShapeProps({ activated: true });
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
_getSheetObject() {
|
|
526
|
+
return getSheetObject(this._univerInstanceService, this._renderManagerService);
|
|
527
|
+
}
|
|
528
|
+
_focusMatch(match) {
|
|
529
|
+
var _this$_workbook$getAc;
|
|
530
|
+
const subUnitId = match.range.subUnitId;
|
|
531
|
+
if (subUnitId !== ((_this$_workbook$getAc = this._workbook.getActiveSheet()) === null || _this$_workbook$getAc === void 0 ? void 0 : _this$_workbook$getAc.getSheetId())) this._commandService.executeCommand(SetWorksheetActivateCommand.id, {
|
|
532
|
+
unitId: this._workbook.getUnitId(),
|
|
533
|
+
subUnitId
|
|
534
|
+
}, { fromFindReplace: true });
|
|
535
|
+
this._commandService.executeCommand(ScrollToCellCommand.id, { range: match.range.range }, { fromFindReplace: true });
|
|
536
|
+
}
|
|
537
|
+
_tryRestoreLastMatchesPosition(lastMatch, newMatches) {
|
|
538
|
+
if (!lastMatch) return 0;
|
|
539
|
+
const { subUnitId: lastSubUnitId } = lastMatch.range;
|
|
540
|
+
const { startColumn: lastStartColumn, startRow: lastStartRow } = lastMatch.range.range;
|
|
541
|
+
const index = newMatches.findIndex((match) => {
|
|
542
|
+
if (lastSubUnitId !== match.range.subUnitId) return false;
|
|
543
|
+
const { startColumn, startRow } = match.range.range;
|
|
544
|
+
return startColumn === lastStartColumn && startRow === lastStartRow;
|
|
545
|
+
});
|
|
546
|
+
return index > -1 ? index + 1 : 0;
|
|
547
|
+
}
|
|
548
|
+
moveToNextMatch(params) {
|
|
549
|
+
var _params$loop, _params$stayIfOnMatch, _params$noFocus, _params$ignoreSelecti;
|
|
550
|
+
if (!this._matches.length) return null;
|
|
551
|
+
const loop = (_params$loop = params === null || params === void 0 ? void 0 : params.loop) !== null && _params$loop !== void 0 ? _params$loop : false;
|
|
552
|
+
const stayIfOnMatch = (_params$stayIfOnMatch = params === null || params === void 0 ? void 0 : params.stayIfOnMatch) !== null && _params$stayIfOnMatch !== void 0 ? _params$stayIfOnMatch : false;
|
|
553
|
+
const noFocus = (_params$noFocus = params === null || params === void 0 ? void 0 : params.noFocus) !== null && _params$noFocus !== void 0 ? _params$noFocus : false;
|
|
554
|
+
const ignoreSelection = (_params$ignoreSelecti = params === null || params === void 0 ? void 0 : params.ignoreSelection) !== null && _params$ignoreSelecti !== void 0 ? _params$ignoreSelecti : false;
|
|
555
|
+
const matchToMove = this._findNextMatch(loop, stayIfOnMatch, ignoreSelection);
|
|
556
|
+
if (matchToMove) {
|
|
557
|
+
var _this$_workbook$getAc2;
|
|
558
|
+
const [match, index] = matchToMove;
|
|
559
|
+
this._matchesPosition = index + 1;
|
|
560
|
+
if (this._query.findScope === FindScope.UNIT) this._activeHighlightIndex = this._matchesByWorksheet.get(match.range.subUnitId).findIndex((m) => m === match);
|
|
561
|
+
else this._activeHighlightIndex = index;
|
|
562
|
+
if (!noFocus) this._focusMatch(match);
|
|
563
|
+
if (((_this$_workbook$getAc2 = this._workbook.getActiveSheet()) === null || _this$_workbook$getAc2 === void 0 ? void 0 : _this$_workbook$getAc2.getSheetId()) === match.range.subUnitId) this._updateCurrentHighlightShape(this._activeHighlightIndex);
|
|
564
|
+
return match;
|
|
565
|
+
}
|
|
566
|
+
this._matchesPosition = 0;
|
|
567
|
+
this._updateCurrentHighlightShape();
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
moveToPreviousMatch(params) {
|
|
571
|
+
var _params$loop2, _params$stayIfOnMatch2, _params$noFocus2, _params$ignoreSelecti2;
|
|
572
|
+
if (!this._matches.length) return null;
|
|
573
|
+
const loop = (_params$loop2 = params === null || params === void 0 ? void 0 : params.loop) !== null && _params$loop2 !== void 0 ? _params$loop2 : false;
|
|
574
|
+
const stayIfOnMatch = (_params$stayIfOnMatch2 = params === null || params === void 0 ? void 0 : params.stayIfOnMatch) !== null && _params$stayIfOnMatch2 !== void 0 ? _params$stayIfOnMatch2 : false;
|
|
575
|
+
const noFocus = (_params$noFocus2 = params === null || params === void 0 ? void 0 : params.noFocus) !== null && _params$noFocus2 !== void 0 ? _params$noFocus2 : false;
|
|
576
|
+
const ignoreSelection = (_params$ignoreSelecti2 = params === null || params === void 0 ? void 0 : params.ignoreSelection) !== null && _params$ignoreSelecti2 !== void 0 ? _params$ignoreSelecti2 : false;
|
|
577
|
+
const matchToMove = this._findPreviousMatch(loop, stayIfOnMatch, ignoreSelection);
|
|
578
|
+
if (matchToMove) {
|
|
579
|
+
var _this$_workbook$getAc3;
|
|
580
|
+
const [match, index] = matchToMove;
|
|
581
|
+
this._matchesPosition = index + 1;
|
|
582
|
+
if (this._query.findScope === FindScope.UNIT) this._activeHighlightIndex = this._matchesByWorksheet.get(match.range.subUnitId).findIndex((m) => m === match);
|
|
583
|
+
else this._activeHighlightIndex = index;
|
|
584
|
+
if (!noFocus) this._focusMatch(match);
|
|
585
|
+
if (((_this$_workbook$getAc3 = this._workbook.getActiveSheet()) === null || _this$_workbook$getAc3 === void 0 ? void 0 : _this$_workbook$getAc3.getSheetId()) === match.range.subUnitId) this._updateCurrentHighlightShape(this._activeHighlightIndex);
|
|
586
|
+
return match;
|
|
587
|
+
}
|
|
588
|
+
this._matchesPosition = 0;
|
|
589
|
+
this._updateCurrentHighlightShape();
|
|
590
|
+
return null;
|
|
591
|
+
}
|
|
592
|
+
_findPreviousMatch(loop = false, stayIfOnMatch = false, ignoreSelection = false) {
|
|
593
|
+
var _this$_workbook$getAc4;
|
|
594
|
+
if (this.currentMatch) {
|
|
595
|
+
const currentMatchIndex = this._matches.findIndex((match) => match === this.currentMatch);
|
|
596
|
+
if (stayIfOnMatch) return [this.currentMatch, currentMatchIndex];
|
|
597
|
+
const nextMatchIndex = currentMatchIndex - 1;
|
|
598
|
+
if (!loop && nextMatchIndex < 0) return null;
|
|
599
|
+
const length = this._matches.length;
|
|
600
|
+
const modded = (nextMatchIndex + length) % length;
|
|
601
|
+
return [this._matches[modded], modded];
|
|
602
|
+
}
|
|
603
|
+
const lastSelection = this._workbookSelections.getCurrentLastSelection();
|
|
604
|
+
if (ignoreSelection || !lastSelection) {
|
|
605
|
+
const lastIndex = this._matches.length - 1;
|
|
606
|
+
return [this._matches[lastIndex], lastIndex];
|
|
607
|
+
}
|
|
608
|
+
if (this._query.findScope !== FindScope.UNIT) return this._findPreviousMatchByRange(this._matches, lastSelection.range);
|
|
609
|
+
const currentSheetId = (_this$_workbook$getAc4 = this._workbook.getActiveSheet()) === null || _this$_workbook$getAc4 === void 0 ? void 0 : _this$_workbook$getAc4.getSheetId();
|
|
610
|
+
if (!currentSheetId) return null;
|
|
611
|
+
const worksheetThatHasMatch = this._findPreviousWorksheetThatHasAMatch(currentSheetId, loop);
|
|
612
|
+
if (!worksheetThatHasMatch) return null;
|
|
613
|
+
return this._findPreviousMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch), lastSelection.range);
|
|
614
|
+
}
|
|
615
|
+
_findNextMatch(loop = false, stayIfOnMatch = false, ignoreSelection = false) {
|
|
616
|
+
var _this$_workbook$getAc5;
|
|
617
|
+
if (this.currentMatch) {
|
|
618
|
+
const currentMatchIndex = this._matches.findIndex((match) => match === this.currentMatch);
|
|
619
|
+
if (stayIfOnMatch) return [this.currentMatch, currentMatchIndex];
|
|
620
|
+
const nextMatchIndex = currentMatchIndex + 1;
|
|
621
|
+
const length = this._matches.length;
|
|
622
|
+
if (!loop && nextMatchIndex >= length) return null;
|
|
623
|
+
const modded = nextMatchIndex % length;
|
|
624
|
+
return [this._matches[modded], modded];
|
|
625
|
+
}
|
|
626
|
+
const last = this._workbookSelections.getCurrentLastSelection();
|
|
627
|
+
if (ignoreSelection || !last) return [this._matches[0], 0];
|
|
628
|
+
if (this._query.findScope !== FindScope.UNIT) return this._findNextMatchByRange(this._matches, last.range, stayIfOnMatch);
|
|
629
|
+
const currentSheetId = (_this$_workbook$getAc5 = this._workbook.getActiveSheet()) === null || _this$_workbook$getAc5 === void 0 ? void 0 : _this$_workbook$getAc5.getSheetId();
|
|
630
|
+
if (!currentSheetId) return null;
|
|
631
|
+
const worksheetThatHasMatch = this._findNextWorksheetThatHasAMatch(currentSheetId, loop);
|
|
632
|
+
if (!worksheetThatHasMatch) return null;
|
|
633
|
+
return this._findNextMatchByRange(this._matchesByWorksheet.get(worksheetThatHasMatch), last.range);
|
|
634
|
+
}
|
|
635
|
+
_findPreviousWorksheetThatHasAMatch(currentWorksheet, loop = false) {
|
|
636
|
+
const rawWorksheetsInOrder = this._workbook.getSheetOrders();
|
|
637
|
+
const currentSheetIndex = rawWorksheetsInOrder.findIndex((sheet) => sheet === currentWorksheet);
|
|
638
|
+
const first = (loop ? rotate(rawWorksheetsInOrder, currentSheetIndex + 1) : rawWorksheetsInOrder.slice(0, currentSheetIndex + 1)).findLast((worksheet) => this._matchesByWorksheet.has(worksheet));
|
|
639
|
+
return first !== null && first !== void 0 ? first : null;
|
|
640
|
+
}
|
|
641
|
+
_findNextWorksheetThatHasAMatch(currentWorksheet, loop = false) {
|
|
642
|
+
const rawWorksheetsInOrder = this._workbook.getSheetOrders();
|
|
643
|
+
const currentSheetIndex = rawWorksheetsInOrder.findIndex((sheet) => sheet === currentWorksheet);
|
|
644
|
+
const first = (loop ? rotate(rawWorksheetsInOrder, currentSheetIndex) : rawWorksheetsInOrder.slice(currentSheetIndex)).find((worksheet) => this._matchesByWorksheet.has(worksheet));
|
|
645
|
+
return first !== null && first !== void 0 ? first : null;
|
|
646
|
+
}
|
|
647
|
+
_findNextMatchByRange(matches, range, stayIfOnMatch = false) {
|
|
648
|
+
const findByRow = this._query.findDirection === FindDirection.ROW;
|
|
649
|
+
let index = matches.findIndex((match) => {
|
|
650
|
+
const matchRange = match.range.range;
|
|
651
|
+
if (!(findByRow ? isBehindPositionWithRowPriority(range, matchRange) : isBehindPositionWithColumnPriority(range, matchRange))) return false;
|
|
652
|
+
const isSame = isSamePosition(range, matchRange);
|
|
653
|
+
return stayIfOnMatch ? isSame : !isSame;
|
|
654
|
+
});
|
|
655
|
+
if (index === -1) index = matches.length - 1;
|
|
656
|
+
const match = matches[index];
|
|
657
|
+
return [match, this._matches.findIndex((m) => m === match)];
|
|
658
|
+
}
|
|
659
|
+
_findPreviousMatchByRange(matches, range, stayIfOnMatch = false) {
|
|
660
|
+
const findByRow = this._query.findDirection === FindDirection.ROW;
|
|
661
|
+
let index = this._matches.findLastIndex((match) => {
|
|
662
|
+
const matchRange = match.range.range;
|
|
663
|
+
if (!(findByRow ? isBeforePositionWithRowPriority(range, matchRange) : isBeforePositionWithColumnPriority(range, matchRange))) return false;
|
|
664
|
+
const isSame = isSamePosition(range, matchRange);
|
|
665
|
+
return stayIfOnMatch ? isSame : !isSame;
|
|
666
|
+
});
|
|
667
|
+
if (index === -1) index = 0;
|
|
668
|
+
const match = matches[index];
|
|
669
|
+
return [match, this._matches.findIndex((m) => m === match)];
|
|
670
|
+
}
|
|
671
|
+
async replace(replaceString) {
|
|
672
|
+
if (this._matchesCount === 0 || !this.currentMatch || !this._query || !this.currentMatch.replaceable) return false;
|
|
673
|
+
const range = this.currentMatch.range;
|
|
674
|
+
const targetWorksheet = this._workbook.getSheetBySheetId(this.currentMatch.range.subUnitId);
|
|
675
|
+
const newContent = this._getReplacedCellData(this.currentMatch, targetWorksheet, this._query.findBy === FindBy.FORMULA, this._query.findString, replaceString, this._query.caseSensitive ? "g" : "ig");
|
|
676
|
+
const params = {
|
|
677
|
+
unitId: this.currentMatch.unitId,
|
|
678
|
+
subUnitId: range.subUnitId,
|
|
679
|
+
value: { [range.range.startRow]: { [range.range.startColumn]: newContent } }
|
|
680
|
+
};
|
|
681
|
+
return this._commandService.executeCommand(SetRangeValuesCommand.id, params);
|
|
682
|
+
}
|
|
683
|
+
async replaceAll(replaceString) {
|
|
684
|
+
if (this._matchesCount === 0 || !this._query) return {
|
|
685
|
+
success: 0,
|
|
686
|
+
failure: 0
|
|
687
|
+
};
|
|
688
|
+
const unitId = this._workbook.getUnitId();
|
|
689
|
+
const { findString, caseSensitive, findBy } = this._query;
|
|
690
|
+
const shouldReplaceFormula = findBy === FindBy.FORMULA;
|
|
691
|
+
const replaceFlag = caseSensitive ? "g" : "ig";
|
|
692
|
+
const replacements = [];
|
|
693
|
+
groupBy(this._matches.filter((m) => m.replaceable), (match) => match.range.subUnitId).forEach((matches, subUnitId) => {
|
|
694
|
+
const matrix = new ObjectMatrix();
|
|
695
|
+
const worksheet = this._workbook.getSheetBySheetId(subUnitId);
|
|
696
|
+
matches.forEach((match) => {
|
|
697
|
+
const { startColumn, startRow } = match.range.range;
|
|
698
|
+
const newCellData = this._getReplacedCellData(match, worksheet, shouldReplaceFormula, findString, replaceString, replaceFlag);
|
|
699
|
+
if (newCellData) matrix.setValue(startRow, startColumn, newCellData);
|
|
700
|
+
});
|
|
701
|
+
replacements.push({
|
|
702
|
+
count: matches.length,
|
|
703
|
+
subUnitId,
|
|
704
|
+
value: matrix.getMatrix()
|
|
705
|
+
});
|
|
706
|
+
});
|
|
707
|
+
if (!replacements) return {
|
|
708
|
+
success: 0,
|
|
709
|
+
failure: 0
|
|
710
|
+
};
|
|
711
|
+
return this._commandService.executeCommand(SheetReplaceCommand.id, {
|
|
712
|
+
unitId,
|
|
713
|
+
replacements
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
_getReplacedCellData(match, worksheet, shouldReplaceFormula, findString, replaceString, replaceFlag) {
|
|
717
|
+
var _currentContent$p;
|
|
718
|
+
const { startRow, startColumn } = match.range.range;
|
|
719
|
+
const currentContent = worksheet.getCellRaw(startRow, startColumn);
|
|
720
|
+
if (match.isFormula) {
|
|
721
|
+
if (!shouldReplaceFormula) return null;
|
|
722
|
+
return {
|
|
723
|
+
f: currentContent.f.replace(new RegExp(escapeRegExp(findString), replaceFlag), replaceString),
|
|
724
|
+
v: null
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
if (!!((_currentContent$p = currentContent.p) === null || _currentContent$p === void 0 ? void 0 : _currentContent$p.body)) {
|
|
728
|
+
const clonedRichText = Tools.deepClone(currentContent.p);
|
|
729
|
+
replaceInDocumentBody(clonedRichText.body, findString, replaceString, this._query.caseSensitive);
|
|
730
|
+
return { p: clonedRichText };
|
|
731
|
+
}
|
|
732
|
+
return { v: currentContent.v.toString().replace(new RegExp(escapeRegExp(findString), replaceFlag), replaceString) };
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
SheetFindModel = __decorate([
|
|
736
|
+
__decorateParam(2, IUniverInstanceService),
|
|
737
|
+
__decorateParam(3, IRenderManagerService),
|
|
738
|
+
__decorateParam(4, ICommandService),
|
|
739
|
+
__decorateParam(5, IContextService),
|
|
740
|
+
__decorateParam(6, Inject(ThemeService)),
|
|
741
|
+
__decorateParam(7, Inject(SheetsSelectionsService))
|
|
742
|
+
], SheetFindModel);
|
|
743
|
+
function escapeRegExp(text) {
|
|
744
|
+
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* This module is responsible for searching and replacing in the sheets.
|
|
748
|
+
* It also adds the search results to the search view by highlighting them.
|
|
749
|
+
*/
|
|
750
|
+
let SheetsFindReplaceProvider = class SheetsFindReplaceProvider extends Disposable {
|
|
751
|
+
constructor(_univerInstanceService, _renderManagerService, _injector) {
|
|
752
|
+
super();
|
|
753
|
+
this._univerInstanceService = _univerInstanceService;
|
|
754
|
+
this._renderManagerService = _renderManagerService;
|
|
755
|
+
this._injector = _injector;
|
|
756
|
+
_defineProperty(this, "_findModelsByUnitId", /* @__PURE__ */ new Map());
|
|
757
|
+
}
|
|
758
|
+
async find(query) {
|
|
759
|
+
this._terminate();
|
|
760
|
+
const workbook = this._univerInstanceService.getCurrentUnitOfType(UniverInstanceType.UNIVER_SHEET);
|
|
761
|
+
if (!workbook) return [];
|
|
762
|
+
const parsedQuery = this._preprocessQuery(query);
|
|
763
|
+
const skeletonManagerService = this._renderManagerService.getRenderById(workbook.getUnitId()).with(SheetSkeletonManagerService);
|
|
764
|
+
const sheetFind = this._injector.createInstance(SheetFindModel, workbook, skeletonManagerService);
|
|
765
|
+
this._findModelsByUnitId.set(workbook.getUnitId(), sheetFind);
|
|
766
|
+
sheetFind.start(parsedQuery);
|
|
767
|
+
return [sheetFind];
|
|
768
|
+
}
|
|
769
|
+
terminate() {
|
|
770
|
+
this._terminate();
|
|
771
|
+
}
|
|
772
|
+
_terminate() {
|
|
773
|
+
this._findModelsByUnitId.forEach((model) => model.dispose());
|
|
774
|
+
this._findModelsByUnitId.clear();
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* Parsed the query object before do actual searching in favor of performance.
|
|
778
|
+
* @param query the raw query object
|
|
779
|
+
* @returns the parsed query object
|
|
780
|
+
*/
|
|
781
|
+
_preprocessQuery(query) {
|
|
782
|
+
let findString = query.caseSensitive ? query.findString : query.findString.toLowerCase();
|
|
783
|
+
findString = findString.trim();
|
|
784
|
+
return {
|
|
785
|
+
...query,
|
|
786
|
+
findString
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
SheetsFindReplaceProvider = __decorate([
|
|
791
|
+
__decorateParam(0, IUniverInstanceService),
|
|
792
|
+
__decorateParam(1, IRenderManagerService),
|
|
793
|
+
__decorateParam(2, Inject(Injector))
|
|
794
|
+
], SheetsFindReplaceProvider);
|
|
795
|
+
const VALUE_PASSING_OBJECT = {
|
|
796
|
+
hit: false,
|
|
797
|
+
replaceable: false,
|
|
798
|
+
isFormula: false,
|
|
799
|
+
rawData: null
|
|
800
|
+
};
|
|
801
|
+
/**
|
|
802
|
+
* This function determines if a cell's content matches what is searched for.
|
|
803
|
+
* @param worksheet worksheet the Worksheet to search
|
|
804
|
+
* @param row the row index
|
|
805
|
+
* @param col the column index
|
|
806
|
+
* @param query the parsed query object
|
|
807
|
+
* @returns if the cell is hit, replaceable and is a formula
|
|
808
|
+
*/
|
|
809
|
+
function hitCell(worksheet, row, col, query, cellData) {
|
|
810
|
+
const { findBy } = query;
|
|
811
|
+
const findByFormula = findBy === FindBy.FORMULA;
|
|
812
|
+
const rawData = worksheet.getCellRaw(row, col);
|
|
813
|
+
VALUE_PASSING_OBJECT.rawData = rawData;
|
|
814
|
+
if (!!(rawData === null || rawData === void 0 ? void 0 : rawData.f)) {
|
|
815
|
+
VALUE_PASSING_OBJECT.isFormula = true;
|
|
816
|
+
if (findByFormula) {
|
|
817
|
+
if (matchCellData({ v: rawData.f }, query)) {
|
|
818
|
+
VALUE_PASSING_OBJECT.hit = true;
|
|
819
|
+
VALUE_PASSING_OBJECT.replaceable = true;
|
|
820
|
+
return VALUE_PASSING_OBJECT;
|
|
821
|
+
}
|
|
822
|
+
VALUE_PASSING_OBJECT.hit = false;
|
|
823
|
+
VALUE_PASSING_OBJECT.replaceable = false;
|
|
824
|
+
return VALUE_PASSING_OBJECT;
|
|
825
|
+
}
|
|
826
|
+
VALUE_PASSING_OBJECT.replaceable = false;
|
|
827
|
+
if (matchCellData(cellData, query)) VALUE_PASSING_OBJECT.hit = true;
|
|
828
|
+
else VALUE_PASSING_OBJECT.hit = false;
|
|
829
|
+
return VALUE_PASSING_OBJECT;
|
|
830
|
+
}
|
|
831
|
+
VALUE_PASSING_OBJECT.isFormula = false;
|
|
832
|
+
if (!matchCellData(cellData, query)) {
|
|
833
|
+
VALUE_PASSING_OBJECT.hit = false;
|
|
834
|
+
VALUE_PASSING_OBJECT.replaceable = false;
|
|
835
|
+
} else if (!rawData) {
|
|
836
|
+
VALUE_PASSING_OBJECT.hit = true;
|
|
837
|
+
VALUE_PASSING_OBJECT.replaceable = false;
|
|
838
|
+
} else {
|
|
839
|
+
VALUE_PASSING_OBJECT.hit = true;
|
|
840
|
+
VALUE_PASSING_OBJECT.replaceable = true;
|
|
841
|
+
}
|
|
842
|
+
return VALUE_PASSING_OBJECT;
|
|
843
|
+
}
|
|
844
|
+
function matchCellData(cellData, query) {
|
|
845
|
+
let value = extractPureValue(cellData);
|
|
846
|
+
if (!value) return false;
|
|
847
|
+
if (query.matchesTheWholeCell) {
|
|
848
|
+
value = trimLeadingTrailingWhitespace(value);
|
|
849
|
+
return query.caseSensitive ? value === query.findString : value.toLowerCase() === query.findString;
|
|
850
|
+
}
|
|
851
|
+
return query.caseSensitive ? value.indexOf(query.findString) > -1 : value.toLowerCase().indexOf(query.findString) > -1;
|
|
852
|
+
}
|
|
853
|
+
function extractPureValue(cell) {
|
|
854
|
+
var _cell$p$body$dataStre, _cell$p;
|
|
855
|
+
const rawValue = (_cell$p$body$dataStre = cell === null || cell === void 0 || (_cell$p = cell.p) === null || _cell$p === void 0 || (_cell$p = _cell$p.body) === null || _cell$p === void 0 ? void 0 : _cell$p.dataStream) !== null && _cell$p$body$dataStre !== void 0 ? _cell$p$body$dataStre : cell === null || cell === void 0 ? void 0 : cell.v;
|
|
856
|
+
if (typeof rawValue === "number") return `${rawValue}`;
|
|
857
|
+
if (typeof rawValue === "boolean") return rawValue ? "1" : "0";
|
|
858
|
+
return rawValue;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Trim only white spaces but not line breaks from the start and end of a string.
|
|
862
|
+
* @param value the string to be trimmed
|
|
863
|
+
*/
|
|
864
|
+
function trimLeadingTrailingWhitespace(value) {
|
|
865
|
+
return value.replace(/^ +/g, "").replace(/ +$/g, "");
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
//#endregion
|
|
869
|
+
//#region package.json
|
|
870
|
+
var name = "@univerjs/sheets-find-replace";
|
|
871
|
+
var version = "0.21.0";
|
|
872
|
+
|
|
873
|
+
//#endregion
|
|
874
|
+
//#region src/config/config.ts
|
|
875
|
+
/**
|
|
876
|
+
* Copyright 2023-present DreamNum Co., Ltd.
|
|
877
|
+
*
|
|
878
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
879
|
+
* you may not use this file except in compliance with the License.
|
|
880
|
+
* You may obtain a copy of the License at
|
|
881
|
+
*
|
|
882
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
883
|
+
*
|
|
884
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
885
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
886
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
887
|
+
* See the License for the specific language governing permissions and
|
|
888
|
+
* limitations under the License.
|
|
889
|
+
*/
|
|
890
|
+
const SHEETS_FIND_REPLACE_PLUGIN_CONFIG_KEY = "sheets-find-replace.config";
|
|
891
|
+
const configSymbol = Symbol(SHEETS_FIND_REPLACE_PLUGIN_CONFIG_KEY);
|
|
892
|
+
const defaultPluginConfig = {};
|
|
893
|
+
|
|
894
|
+
//#endregion
|
|
895
|
+
//#region src/plugin.ts
|
|
896
|
+
let UniverSheetsFindReplacePlugin = class UniverSheetsFindReplacePlugin extends Plugin {
|
|
897
|
+
constructor(_config = defaultPluginConfig, _injector, _configService) {
|
|
898
|
+
super();
|
|
899
|
+
this._config = _config;
|
|
900
|
+
this._injector = _injector;
|
|
901
|
+
this._configService = _configService;
|
|
902
|
+
const { ...rest } = merge({}, defaultPluginConfig, this._config);
|
|
903
|
+
this._configService.setConfig(SHEETS_FIND_REPLACE_PLUGIN_CONFIG_KEY, rest);
|
|
904
|
+
}
|
|
905
|
+
onStarting() {
|
|
906
|
+
[[SheetsFindReplaceController]].forEach((d) => this._injector.add(d));
|
|
907
|
+
}
|
|
908
|
+
onSteady() {
|
|
909
|
+
this._injector.get(SheetsFindReplaceController);
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
_defineProperty(UniverSheetsFindReplacePlugin, "pluginName", "SHEET_FIND_REPLACE_PLUGIN");
|
|
913
|
+
_defineProperty(UniverSheetsFindReplacePlugin, "packageName", name);
|
|
914
|
+
_defineProperty(UniverSheetsFindReplacePlugin, "version", version);
|
|
915
|
+
_defineProperty(UniverSheetsFindReplacePlugin, "type", UniverInstanceType.UNIVER_SHEET);
|
|
916
|
+
UniverSheetsFindReplacePlugin = __decorate([
|
|
917
|
+
DependentOn(UniverSheetsPlugin, UniverSheetsPlugin, UniverFindReplacePlugin),
|
|
918
|
+
__decorateParam(1, Inject(Injector)),
|
|
919
|
+
__decorateParam(2, IConfigService)
|
|
920
|
+
], UniverSheetsFindReplacePlugin);
|
|
921
|
+
|
|
922
|
+
//#endregion
|
|
923
|
+
export { SheetReplaceCommand, SheetsFindReplaceController, UniverSheetsFindReplacePlugin };
|