@visactor/vtable-search 1.1.3-alpha.2 → 1.2.0-alpha.3

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.
@@ -50,12 +50,16 @@ class SearchComponent {
50
50
  };
51
51
  }
52
52
  updateCellStyle(highlight = !0) {
53
- if (this.queryResult) for (let i = 0; i < this.queryResult.length; i++) {
54
- const {col: col, row: row} = this.queryResult[i];
55
- this.table.arrangeCustomCellStyle({
56
- col: col,
57
- row: row
58
- }, highlight ? "__search_component_highlight" : null);
53
+ if (this.queryResult) {
54
+ this.table.hasCustomCellStyle("__search_component_highlight") || this.table.registerCustomCellStyle("__search_component_highlight", this.highlightCellStyle),
55
+ this.table.hasCustomCellStyle("__search_component_focuse") || this.table.registerCustomCellStyle("__search_component_focuse", this.focuseHighlightCellStyle);
56
+ for (let i = 0; i < this.queryResult.length; i++) {
57
+ const {col: col, row: row} = this.queryResult[i];
58
+ this.table.arrangeCustomCellStyle({
59
+ col: col,
60
+ row: row
61
+ }, highlight ? "__search_component_highlight" : null);
62
+ }
59
63
  }
60
64
  }
61
65
  next() {
@@ -1 +1 @@
1
- {"version":3,"sources":["search-component/search-component.ts"],"names":[],"mappings":";;;AACA,6CAA2C;AAuB3C,MAAM,0BAA0B,GAAoC;IAClE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,MAAM,+BAA+B,GAAoC;IACvE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,SAAS,mBAAmB,CAAC,QAAgB,EAAE,KAAa;IAC1D,OAAO,IAAA,gBAAO,EAAC,QAAQ,CAAC,IAAI,IAAA,gBAAO,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACpF,CAAC;AAED,MAAa,eAAe;IAiB1B,YAAY,MAA6B;QACvC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,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I,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;IACzB,CAAC;CACF;AA9KD,0CA8KC","file":"search-component.js","sourcesContent":["import type * as VTable from '@visactor/vtable';\nimport { isValid } from '@visactor/vutils';\n\ntype IVTable = VTable.ListTable | VTable.PivotTable | VTable.PivotChart;\n\nexport type QueryResult = {\n queryStr: string;\n results: {\n col: number;\n row: number;\n value: string;\n }[];\n};\n\nexport type SearchComponentOption = {\n table: IVTable;\n autoJump?: boolean;\n skipHeader?: boolean;\n highlightCellStyle?: VTable.TYPES.CellStyle;\n focuseHighlightCellStyle?: VTable.TYPES.CellStyle;\n queryMethod?: (queryStr: string, value: string, option?: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n};\n\nconst defalutHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 255, 0, 0.2)'\n};\n\nconst defalutFocusHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 155, 0, 0.2)'\n};\n\nfunction defalultQueryMethod(queryStr: string, value: string) {\n return isValid(queryStr) && isValid(value) && value.toString().includes(queryStr);\n}\n\nexport class SearchComponent {\n table: IVTable;\n skipHeader: boolean;\n autoJump: boolean;\n highlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n focuseHighlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n queryMethod: (queryStr: string, value: string, option: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n\n queryStr: string;\n queryResult: {\n col: number;\n row: number;\n value: string;\n }[];\n currentIndex: number;\n\n constructor(option: SearchComponentOption) {\n this.table = option.table;\n this.autoJump = option.autoJump || false;\n this.skipHeader = option.skipHeader || false;\n this.highlightCellStyle = option.highlightCellStyle || defalutHightlightCellStyle;\n this.focuseHighlightCellStyle = option.focuseHighlightCellStyle || defalutFocusHightlightCellStyle;\n this.queryMethod = option.queryMethod || defalultQueryMethod;\n this.callback = option.callback;\n\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n\n search(str: string) {\n this.clear();\n this.queryStr = str;\n\n if (!str) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n for (let row = 0; row < this.table.rowCount; row++) {\n for (let col = 0; col < this.table.colCount; col++) {\n if (this.skipHeader && this.table.isHeader(col, row)) {\n continue;\n }\n const value = this.table.getCellValue(col, row);\n if (this.queryMethod(this.queryStr, value, { col, row, table: this.table })) {\n this.queryResult.push({\n col,\n row,\n value\n });\n }\n }\n }\n\n this.updateCellStyle();\n\n if (this.callback) {\n this.callback(\n {\n queryStr: this.queryStr,\n results: this.queryResult\n },\n this.table\n );\n }\n\n if (this.autoJump) {\n return this.next();\n }\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n updateCellStyle(highlight: boolean = true) {\n if (!this.queryResult) {\n return;\n }\n for (let i = 0; i < this.queryResult.length; i++) {\n const { col, row } = this.queryResult[i];\n this.table.arrangeCustomCellStyle(\n {\n col,\n row\n },\n highlight ? '__search_component_highlight' : null\n );\n }\n }\n\n next() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex++;\n if (this.currentIndex >= this.queryResult.length) {\n this.currentIndex = 0;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n prev() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex--;\n if (this.currentIndex < 0) {\n this.currentIndex = this.queryResult.length - 1;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n jumpToCell(col: number, row: number) {\n // if focus cell out of screen, jump to cell\n const { rowStart, rowEnd } = this.table.getBodyVisibleRowRange();\n const { colStart, colEnd } = this.table.getBodyVisibleColRange();\n if (row <= rowStart || row >= rowEnd || col <= colStart || col >= colEnd) {\n this.table.scrollToCell({ col, row });\n }\n }\n\n clear() {\n // reset highlight cell style\n this.updateCellStyle(null);\n this.queryStr = '';\n this.queryResult = [];\n this.currentIndex = -1;\n }\n}\n"]}
1
+ {"version":3,"sources":["search-component/search-component.ts"],"names":[],"mappings":";;;AACA,6CAA2C;AAuB3C,MAAM,0BAA0B,GAAoC;IAClE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,MAAM,+BAA+B,GAAoC;IACvE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,SAAS,mBAAmB,CAAC,QAAgB,EAAE,KAAa;IAC1D,OAAO,IAAA,gBAAO,EAAC,QAAQ,CAAC,IAAI,IAAA,gBAAO,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACpF,CAAC;AAED,MAAa,eAAe;IAiB1B,YAAY,MAA6B;QACvC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,KAAK,CAAC;QAC7C,IAAI,CAAC,kBAAkB,GAAG,MAAM,CA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AAE,CAAC,CAAC;SACvC;IACH,CAAC;IAED,KAAK;QAEH,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;IACzB,CAAC;CACF;AApLD,0CAoLC","file":"search-component.js","sourcesContent":["import type * as VTable from '@visactor/vtable';\nimport { isValid } from '@visactor/vutils';\n\ntype IVTable = VTable.ListTable | VTable.PivotTable | VTable.PivotChart;\n\nexport type QueryResult = {\n queryStr: string;\n results: {\n col: number;\n row: number;\n value: string;\n }[];\n};\n\nexport type SearchComponentOption = {\n table: IVTable;\n autoJump?: boolean;\n skipHeader?: boolean;\n highlightCellStyle?: VTable.TYPES.CellStyle;\n focuseHighlightCellStyle?: VTable.TYPES.CellStyle;\n queryMethod?: (queryStr: string, value: string, option?: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n};\n\nconst defalutHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 255, 0, 0.2)'\n};\n\nconst defalutFocusHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 155, 0, 0.2)'\n};\n\nfunction defalultQueryMethod(queryStr: string, value: string) {\n return isValid(queryStr) && isValid(value) && value.toString().includes(queryStr);\n}\n\nexport class SearchComponent {\n table: IVTable;\n skipHeader: boolean;\n autoJump: boolean;\n highlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n focuseHighlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n queryMethod: (queryStr: string, value: string, option: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n\n queryStr: string;\n queryResult: {\n col: number;\n row: number;\n value: string;\n }[];\n currentIndex: number;\n\n constructor(option: SearchComponentOption) {\n this.table = option.table;\n this.autoJump = option.autoJump || false;\n this.skipHeader = option.skipHeader || false;\n this.highlightCellStyle = option.highlightCellStyle || defalutHightlightCellStyle;\n this.focuseHighlightCellStyle = option.focuseHighlightCellStyle || defalutFocusHightlightCellStyle;\n this.queryMethod = option.queryMethod || defalultQueryMethod;\n this.callback = option.callback;\n\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n\n search(str: string) {\n this.clear();\n this.queryStr = str;\n\n if (!str) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n for (let row = 0; row < this.table.rowCount; row++) {\n for (let col = 0; col < this.table.colCount; col++) {\n if (this.skipHeader && this.table.isHeader(col, row)) {\n continue;\n }\n const value = this.table.getCellValue(col, row);\n if (this.queryMethod(this.queryStr, value, { col, row, table: this.table })) {\n this.queryResult.push({\n col,\n row,\n value\n });\n }\n }\n }\n\n this.updateCellStyle();\n\n if (this.callback) {\n this.callback(\n {\n queryStr: this.queryStr,\n results: this.queryResult\n },\n this.table\n );\n }\n\n if (this.autoJump) {\n return this.next();\n }\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n updateCellStyle(highlight: boolean = true) {\n if (!this.queryResult) {\n return;\n }\n if (!this.table.hasCustomCellStyle('__search_component_highlight')) {\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n }\n if (!this.table.hasCustomCellStyle('__search_component_focuse')) {\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n for (let i = 0; i < this.queryResult.length; i++) {\n const { col, row } = this.queryResult[i];\n this.table.arrangeCustomCellStyle(\n {\n col,\n row\n },\n highlight ? '__search_component_highlight' : null\n );\n }\n }\n\n next() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex++;\n if (this.currentIndex >= this.queryResult.length) {\n this.currentIndex = 0;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n prev() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex--;\n if (this.currentIndex < 0) {\n this.currentIndex = this.queryResult.length - 1;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n jumpToCell(col: number, row: number) {\n // if focus cell out of screen, jump to cell\n const { rowStart, rowEnd } = this.table.getBodyVisibleRowRange();\n const { colStart, colEnd } = this.table.getBodyVisibleColRange();\n if (row <= rowStart || row >= rowEnd || col <= colStart || col >= colEnd) {\n this.table.scrollToCell({ col, row });\n }\n }\n\n clear() {\n // reset highlight cell style\n this.updateCellStyle(null);\n this.queryStr = '';\n this.queryResult = [];\n this.currentIndex = -1;\n }\n}\n"]}
@@ -81,6 +81,12 @@
81
81
  if (!this.queryResult) {
82
82
  return;
83
83
  }
84
+ if (!this.table.hasCustomCellStyle('__search_component_highlight')) {
85
+ this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle);
86
+ }
87
+ if (!this.table.hasCustomCellStyle('__search_component_focuse')) {
88
+ this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle);
89
+ }
84
90
  for (let i = 0; i < this.queryResult.length; i++) {
85
91
  const { col, row } = this.queryResult[i];
86
92
  this.table.arrangeCustomCellStyle({
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).VTable=e.VTable||{},e.VTable.export={}))}(this,(function(e){"use strict";var t=e=>null!=e;const l={bgColor:"rgba(255, 255, 0, 0.2)"},s={bgColor:"rgba(255, 155, 0, 0.2)"};function r(e,l){return t(e)&&t(l)&&l.toString().includes(e)}e.SearchComponent=class{table;skipHeader;autoJump;highlightCellStyle;focuseHighlightCellStyle;queryMethod;callback;queryStr;queryResult;currentIndex;constructor(e){this.table=e.table,this.autoJump=e.autoJump||!1,this.skipHeader=e.skipHeader||!1,this.highlightCellStyle=e.highlightCellStyle||l,this.focuseHighlightCellStyle=e.focuseHighlightCellStyle||s,this.queryMethod=e.queryMethod||r,this.callback=e.callback,this.table.registerCustomCellStyle("__search_component_highlight",this.highlightCellStyle),this.table.registerCustomCellStyle("__search_component_focuse",this.focuseHighlightCellStyle)}search(e){if(this.clear(),this.queryStr=e,!e)return{index:0,results:this.queryResult};for(let e=0;e<this.table.rowCount;e++)for(let t=0;t<this.table.colCount;t++){if(this.skipHeader&&this.table.isHeader(t,e))continue;const l=this.table.getCellValue(t,e);this.queryMethod(this.queryStr,l,{col:t,row:e,table:this.table})&&this.queryResult.push({col:t,row:e,value:l})}return this.updateCellStyle(),this.callback&&this.callback({queryStr:this.queryStr,results:this.queryResult},this.table),this.autoJump?this.next():{index:0,results:this.queryResult}}updateCellStyle(e=!0){if(this.queryResult)for(let t=0;t<this.queryResult.length;t++){const{col:l,row:s}=this.queryResult[t];this.table.arrangeCustomCellStyle({col:l,row:s},e?"__search_component_highlight":null)}}next(){if(!this.queryResult.length)return{index:0,results:this.queryResult};-1!==this.currentIndex&&this.table.arrangeCustomCellStyle({col:this.queryResult[this.currentIndex].col,row:this.queryResult[this.currentIndex].row},"__search_component_highlight"),this.currentIndex++,this.currentIndex>=this.queryResult.length&&(this.currentIndex=0);const{col:e,row:t}=this.queryResult[this.currentIndex];return this.table.arrangeCustomCellStyle({col:e,row:t},"__search_component_focuse"),this.jumpToCell(e,t),{index:this.currentIndex,results:this.queryResult}}prev(){if(!this.queryResult.length)return{index:0,results:this.queryResult};-1!==this.currentIndex&&this.table.arrangeCustomCellStyle({col:this.queryResult[this.currentIndex].col,row:this.queryResult[this.currentIndex].row},"__search_component_highlight"),this.currentIndex--,this.currentIndex<0&&(this.currentIndex=this.queryResult.length-1);const{col:e,row:t}=this.queryResult[this.currentIndex];return this.table.arrangeCustomCellStyle({col:e,row:t},"__search_component_focuse"),this.jumpToCell(e,t),{index:this.currentIndex,results:this.queryResult}}jumpToCell(e,t){const{rowStart:l,rowEnd:s}=this.table.getBodyVisibleRowRange(),{colStart:r,colEnd:i}=this.table.getBodyVisibleColRange();(t<=l||t>=s||e<=r||e>=i)&&this.table.scrollToCell({col:e,row:t})}clear(){this.updateCellStyle(null),this.queryStr="",this.queryResult=[],this.currentIndex=-1}}}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).VTable=e.VTable||{},e.VTable.export={}))}(this,(function(e){"use strict";var t=e=>null!=e;const l={bgColor:"rgba(255, 255, 0, 0.2)"},s={bgColor:"rgba(255, 155, 0, 0.2)"};function r(e,l){return t(e)&&t(l)&&l.toString().includes(e)}e.SearchComponent=class{table;skipHeader;autoJump;highlightCellStyle;focuseHighlightCellStyle;queryMethod;callback;queryStr;queryResult;currentIndex;constructor(e){this.table=e.table,this.autoJump=e.autoJump||!1,this.skipHeader=e.skipHeader||!1,this.highlightCellStyle=e.highlightCellStyle||l,this.focuseHighlightCellStyle=e.focuseHighlightCellStyle||s,this.queryMethod=e.queryMethod||r,this.callback=e.callback,this.table.registerCustomCellStyle("__search_component_highlight",this.highlightCellStyle),this.table.registerCustomCellStyle("__search_component_focuse",this.focuseHighlightCellStyle)}search(e){if(this.clear(),this.queryStr=e,!e)return{index:0,results:this.queryResult};for(let e=0;e<this.table.rowCount;e++)for(let t=0;t<this.table.colCount;t++){if(this.skipHeader&&this.table.isHeader(t,e))continue;const l=this.table.getCellValue(t,e);this.queryMethod(this.queryStr,l,{col:t,row:e,table:this.table})&&this.queryResult.push({col:t,row:e,value:l})}return this.updateCellStyle(),this.callback&&this.callback({queryStr:this.queryStr,results:this.queryResult},this.table),this.autoJump?this.next():{index:0,results:this.queryResult}}updateCellStyle(e=!0){if(this.queryResult){this.table.hasCustomCellStyle("__search_component_highlight")||this.table.registerCustomCellStyle("__search_component_highlight",this.highlightCellStyle),this.table.hasCustomCellStyle("__search_component_focuse")||this.table.registerCustomCellStyle("__search_component_focuse",this.focuseHighlightCellStyle);for(let t=0;t<this.queryResult.length;t++){const{col:l,row:s}=this.queryResult[t];this.table.arrangeCustomCellStyle({col:l,row:s},e?"__search_component_highlight":null)}}}next(){if(!this.queryResult.length)return{index:0,results:this.queryResult};-1!==this.currentIndex&&this.table.arrangeCustomCellStyle({col:this.queryResult[this.currentIndex].col,row:this.queryResult[this.currentIndex].row},"__search_component_highlight"),this.currentIndex++,this.currentIndex>=this.queryResult.length&&(this.currentIndex=0);const{col:e,row:t}=this.queryResult[this.currentIndex];return this.table.arrangeCustomCellStyle({col:e,row:t},"__search_component_focuse"),this.jumpToCell(e,t),{index:this.currentIndex,results:this.queryResult}}prev(){if(!this.queryResult.length)return{index:0,results:this.queryResult};-1!==this.currentIndex&&this.table.arrangeCustomCellStyle({col:this.queryResult[this.currentIndex].col,row:this.queryResult[this.currentIndex].row},"__search_component_highlight"),this.currentIndex--,this.currentIndex<0&&(this.currentIndex=this.queryResult.length-1);const{col:e,row:t}=this.queryResult[this.currentIndex];return this.table.arrangeCustomCellStyle({col:e,row:t},"__search_component_focuse"),this.jumpToCell(e,t),{index:this.currentIndex,results:this.queryResult}}jumpToCell(e,t){const{rowStart:l,rowEnd:s}=this.table.getBodyVisibleRowRange(),{colStart:r,colEnd:i}=this.table.getBodyVisibleColRange();(t<=l||t>=s||e<=r||e>=i)&&this.table.scrollToCell({col:e,row:t})}clear(){this.updateCellStyle(null),this.queryStr="",this.queryResult=[],this.currentIndex=-1}}}));
@@ -46,12 +46,16 @@ export class SearchComponent {
46
46
  };
47
47
  }
48
48
  updateCellStyle(highlight = !0) {
49
- if (this.queryResult) for (let i = 0; i < this.queryResult.length; i++) {
50
- const {col: col, row: row} = this.queryResult[i];
51
- this.table.arrangeCustomCellStyle({
52
- col: col,
53
- row: row
54
- }, highlight ? "__search_component_highlight" : null);
49
+ if (this.queryResult) {
50
+ this.table.hasCustomCellStyle("__search_component_highlight") || this.table.registerCustomCellStyle("__search_component_highlight", this.highlightCellStyle),
51
+ this.table.hasCustomCellStyle("__search_component_focuse") || this.table.registerCustomCellStyle("__search_component_focuse", this.focuseHighlightCellStyle);
52
+ for (let i = 0; i < this.queryResult.length; i++) {
53
+ const {col: col, row: row} = this.queryResult[i];
54
+ this.table.arrangeCustomCellStyle({
55
+ col: col,
56
+ row: row
57
+ }, highlight ? "__search_component_highlight" : null);
58
+ }
55
59
  }
56
60
  }
57
61
  next() {
@@ -1 +1 @@
1
- {"version":3,"sources":["search-component/search-component.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAuB3C,MAAM,0BAA0B,GAAoC;IAClE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,MAAM,+BAA+B,GAAoC;IACvE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,SAAS,mBAAmB,CAAC,QAAgB,EAAE,KAAa;IAC1D,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACpF,CAAC;AAED,MAAM,OAAO,eAAe;IAiB1B,YAAY,MAA6B;QACvC,I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G,EAAE,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;IACzB,CAAC;CACF","file":"search-component.js","sourcesContent":["import type * as VTable from '@visactor/vtable';\nimport { isValid } from '@visactor/vutils';\n\ntype IVTable = VTable.ListTable | VTable.PivotTable | VTable.PivotChart;\n\nexport type QueryResult = {\n queryStr: string;\n results: {\n col: number;\n row: number;\n value: string;\n }[];\n};\n\nexport type SearchComponentOption = {\n table: IVTable;\n autoJump?: boolean;\n skipHeader?: boolean;\n highlightCellStyle?: VTable.TYPES.CellStyle;\n focuseHighlightCellStyle?: VTable.TYPES.CellStyle;\n queryMethod?: (queryStr: string, value: string, option?: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n};\n\nconst defalutHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 255, 0, 0.2)'\n};\n\nconst defalutFocusHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 155, 0, 0.2)'\n};\n\nfunction defalultQueryMethod(queryStr: string, value: string) {\n return isValid(queryStr) && isValid(value) && value.toString().includes(queryStr);\n}\n\nexport class SearchComponent {\n table: IVTable;\n skipHeader: boolean;\n autoJump: boolean;\n highlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n focuseHighlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n queryMethod: (queryStr: string, value: string, option: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n\n queryStr: string;\n queryResult: {\n col: number;\n row: number;\n value: string;\n }[];\n currentIndex: number;\n\n constructor(option: SearchComponentOption) {\n this.table = option.table;\n this.autoJump = option.autoJump || false;\n this.skipHeader = option.skipHeader || false;\n this.highlightCellStyle = option.highlightCellStyle || defalutHightlightCellStyle;\n this.focuseHighlightCellStyle = option.focuseHighlightCellStyle || defalutFocusHightlightCellStyle;\n this.queryMethod = option.queryMethod || defalultQueryMethod;\n this.callback = option.callback;\n\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n\n search(str: string) {\n this.clear();\n this.queryStr = str;\n\n if (!str) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n for (let row = 0; row < this.table.rowCount; row++) {\n for (let col = 0; col < this.table.colCount; col++) {\n if (this.skipHeader && this.table.isHeader(col, row)) {\n continue;\n }\n const value = this.table.getCellValue(col, row);\n if (this.queryMethod(this.queryStr, value, { col, row, table: this.table })) {\n this.queryResult.push({\n col,\n row,\n value\n });\n }\n }\n }\n\n this.updateCellStyle();\n\n if (this.callback) {\n this.callback(\n {\n queryStr: this.queryStr,\n results: this.queryResult\n },\n this.table\n );\n }\n\n if (this.autoJump) {\n return this.next();\n }\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n updateCellStyle(highlight: boolean = true) {\n if (!this.queryResult) {\n return;\n }\n for (let i = 0; i < this.queryResult.length; i++) {\n const { col, row } = this.queryResult[i];\n this.table.arrangeCustomCellStyle(\n {\n col,\n row\n },\n highlight ? '__search_component_highlight' : null\n );\n }\n }\n\n next() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex++;\n if (this.currentIndex >= this.queryResult.length) {\n this.currentIndex = 0;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n prev() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex--;\n if (this.currentIndex < 0) {\n this.currentIndex = this.queryResult.length - 1;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n jumpToCell(col: number, row: number) {\n // if focus cell out of screen, jump to cell\n const { rowStart, rowEnd } = this.table.getBodyVisibleRowRange();\n const { colStart, colEnd } = this.table.getBodyVisibleColRange();\n if (row <= rowStart || row >= rowEnd || col <= colStart || col >= colEnd) {\n this.table.scrollToCell({ col, row });\n }\n }\n\n clear() {\n // reset highlight cell style\n this.updateCellStyle(null);\n this.queryStr = '';\n this.queryResult = [];\n this.currentIndex = -1;\n }\n}\n"]}
1
+ {"version":3,"sources":["search-component/search-component.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAuB3C,MAAM,0BAA0B,GAAoC;IAClE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,MAAM,+BAA+B,GAAoC;IACvE,OAAO,EAAE,wBAAwB;CAClC,CAAC;AAEF,SAAS,mBAAmB,CAAC,QAAgB,EAAE,KAAa;IAC1D,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACpF,CAAC;AAED,MAAM,OAAO,eAAe;IAiB1B,YAAY,MAA6B;QACvC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,KAAK,CAAC;QACzC,IAAI,CAAC,UAAU,GAAG,MA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M,EAAE;YACxE,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;SACvC;IACH,CAAC;IAED,KAAK;QAEH,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;IACzB,CAAC;CACF","file":"search-component.js","sourcesContent":["import type * as VTable from '@visactor/vtable';\nimport { isValid } from '@visactor/vutils';\n\ntype IVTable = VTable.ListTable | VTable.PivotTable | VTable.PivotChart;\n\nexport type QueryResult = {\n queryStr: string;\n results: {\n col: number;\n row: number;\n value: string;\n }[];\n};\n\nexport type SearchComponentOption = {\n table: IVTable;\n autoJump?: boolean;\n skipHeader?: boolean;\n highlightCellStyle?: VTable.TYPES.CellStyle;\n focuseHighlightCellStyle?: VTable.TYPES.CellStyle;\n queryMethod?: (queryStr: string, value: string, option?: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n};\n\nconst defalutHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 255, 0, 0.2)'\n};\n\nconst defalutFocusHightlightCellStyle: Partial<VTable.TYPES.CellStyle> = {\n bgColor: 'rgba(255, 155, 0, 0.2)'\n};\n\nfunction defalultQueryMethod(queryStr: string, value: string) {\n return isValid(queryStr) && isValid(value) && value.toString().includes(queryStr);\n}\n\nexport class SearchComponent {\n table: IVTable;\n skipHeader: boolean;\n autoJump: boolean;\n highlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n focuseHighlightCellStyle: Partial<VTable.TYPES.CellStyle>;\n queryMethod: (queryStr: string, value: string, option: { col: number; row: number; table: IVTable }) => boolean;\n callback?: (queryResult: QueryResult, table: IVTable) => void;\n\n queryStr: string;\n queryResult: {\n col: number;\n row: number;\n value: string;\n }[];\n currentIndex: number;\n\n constructor(option: SearchComponentOption) {\n this.table = option.table;\n this.autoJump = option.autoJump || false;\n this.skipHeader = option.skipHeader || false;\n this.highlightCellStyle = option.highlightCellStyle || defalutHightlightCellStyle;\n this.focuseHighlightCellStyle = option.focuseHighlightCellStyle || defalutFocusHightlightCellStyle;\n this.queryMethod = option.queryMethod || defalultQueryMethod;\n this.callback = option.callback;\n\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n\n search(str: string) {\n this.clear();\n this.queryStr = str;\n\n if (!str) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n for (let row = 0; row < this.table.rowCount; row++) {\n for (let col = 0; col < this.table.colCount; col++) {\n if (this.skipHeader && this.table.isHeader(col, row)) {\n continue;\n }\n const value = this.table.getCellValue(col, row);\n if (this.queryMethod(this.queryStr, value, { col, row, table: this.table })) {\n this.queryResult.push({\n col,\n row,\n value\n });\n }\n }\n }\n\n this.updateCellStyle();\n\n if (this.callback) {\n this.callback(\n {\n queryStr: this.queryStr,\n results: this.queryResult\n },\n this.table\n );\n }\n\n if (this.autoJump) {\n return this.next();\n }\n return {\n index: 0,\n results: this.queryResult\n };\n }\n\n updateCellStyle(highlight: boolean = true) {\n if (!this.queryResult) {\n return;\n }\n if (!this.table.hasCustomCellStyle('__search_component_highlight')) {\n this.table.registerCustomCellStyle('__search_component_highlight', this.highlightCellStyle as any);\n }\n if (!this.table.hasCustomCellStyle('__search_component_focuse')) {\n this.table.registerCustomCellStyle('__search_component_focuse', this.focuseHighlightCellStyle as any);\n }\n for (let i = 0; i < this.queryResult.length; i++) {\n const { col, row } = this.queryResult[i];\n this.table.arrangeCustomCellStyle(\n {\n col,\n row\n },\n highlight ? '__search_component_highlight' : null\n );\n }\n }\n\n next() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex++;\n if (this.currentIndex >= this.queryResult.length) {\n this.currentIndex = 0;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n prev() {\n if (!this.queryResult.length) {\n return {\n index: 0,\n results: this.queryResult\n };\n }\n if (this.currentIndex !== -1) {\n // reset last focus\n this.table.arrangeCustomCellStyle(\n {\n col: this.queryResult[this.currentIndex].col,\n row: this.queryResult[this.currentIndex].row\n },\n '__search_component_highlight'\n );\n }\n this.currentIndex--;\n if (this.currentIndex < 0) {\n this.currentIndex = this.queryResult.length - 1;\n }\n const { col, row } = this.queryResult[this.currentIndex];\n this.table.arrangeCustomCellStyle({ col, row }, '__search_component_focuse');\n\n this.jumpToCell(col, row);\n\n return {\n index: this.currentIndex,\n results: this.queryResult\n };\n }\n\n jumpToCell(col: number, row: number) {\n // if focus cell out of screen, jump to cell\n const { rowStart, rowEnd } = this.table.getBodyVisibleRowRange();\n const { colStart, colEnd } = this.table.getBodyVisibleColRange();\n if (row <= rowStart || row >= rowEnd || col <= colStart || col >= colEnd) {\n this.table.scrollToCell({ col, row });\n }\n }\n\n clear() {\n // reset highlight cell style\n this.updateCellStyle(null);\n this.queryStr = '';\n this.queryResult = [];\n this.currentIndex = -1;\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@visactor/vtable-search",
3
- "version": "1.1.3-alpha.2",
3
+ "version": "1.2.0-alpha.3",
4
4
  "description": "The search util of VTable",
5
5
  "author": {
6
6
  "name": "VisActor",
@@ -30,7 +30,7 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@visactor/vtable": "1.1.3-alpha.2",
33
+ "@visactor/vtable": "1.2.0-alpha.3",
34
34
  "@visactor/vutils": "~0.18.9"
35
35
  },
36
36
  "devDependencies": {