@eox/itemfilter 1.10.2 → 1.10.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eox/itemfilter",
3
- "version": "1.10.2",
3
+ "version": "1.10.4",
4
4
  "type": "module",
5
5
  "devDependencies": {
6
6
  "@types/geojson": "^7946.0.14",
@@ -35,7 +35,7 @@
35
35
  "watch": "vite build --watch"
36
36
  },
37
37
  "dependencies": {
38
- "@eox/elements-utils": "^1.0.0",
38
+ "@eox/elements-utils": "^1.1.0",
39
39
  "@eox/ui": "^0.3.6",
40
40
  "@floating-ui/dom": "^1.6.8",
41
41
  "@turf/boolean-intersects": "^7.0.0",
@@ -17,6 +17,7 @@ export class EOxItemFilterText extends LitElement {
17
17
  static get properties() {
18
18
  return {
19
19
  filterObject: { attribute: false, type: Object },
20
+ results: { state: true, type: Array },
20
21
  tabIndex: { attribute: false, type: Number },
21
22
  unstyled: { type: Boolean },
22
23
  isValid: { state: true, type: Boolean },
@@ -31,6 +32,11 @@ export class EOxItemFilterText extends LitElement {
31
32
  */
32
33
  this.filterObject = {};
33
34
 
35
+ /**
36
+ * @type Array
37
+ */
38
+ this.results = null;
39
+
34
40
  /**
35
41
  * @type Boolean
36
42
  */
@@ -54,6 +60,28 @@ export class EOxItemFilterText extends LitElement {
54
60
  textInputHandlerMethod(this);
55
61
  };
56
62
 
63
+ /**
64
+ * Handles the keydown event when enter is pressed and result is just one item
65
+ * then add the item to selected result by triggering the result event
66
+ */
67
+ #keydownHandler = (e) => {
68
+ if (
69
+ e.key === "Enter" &&
70
+ !!e.target.value &&
71
+ this.results &&
72
+ this.results.length === 1
73
+ ) {
74
+ this.dispatchEvent(
75
+ new CustomEvent("result", {
76
+ detail: this.results[0],
77
+ }),
78
+ );
79
+
80
+ e.target.value = "";
81
+ this.#inputHandler();
82
+ }
83
+ };
84
+
57
85
  /**
58
86
  * Resets the text input by calling the resetTextMethod with the current context.
59
87
  */
@@ -99,6 +127,7 @@ export class EOxItemFilterText extends LitElement {
99
127
  pattern="${this.filterObject.validation?.pattern || ".*"}"
100
128
  @input="${this.debouncedInputHandler}"
101
129
  @click=${(evt) => evt.stopPropagation()}
130
+ @keydown=${this.#keydownHandler}
102
131
  />
103
132
  </div>
104
133
  </div>
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Capitalize the first letter of each word in a string.
3
+ *
4
+ * @param {string} str - The string to capitalize.
5
+ * @returns {string} The capitalized string.
6
+ */
7
+ function capitalize(str) {
8
+ return str
9
+ .split(" ")
10
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
11
+ .join(" ");
12
+ }
13
+
14
+ export default capitalize;
@@ -14,6 +14,7 @@ export const indexItems = (items, fuseConfig) => {
14
14
  _fuse = new Fuse(items, {
15
15
  threshold: 0.4,
16
16
  distance: 50,
17
+ ignoreLocation: true,
17
18
  includeMatches: true,
18
19
  useExtendedSearch: true,
19
20
  ...fuseConfig,
@@ -1,3 +1,5 @@
1
+ import { capitalize, mergeHighlightIndices } from "./";
2
+
1
3
  /**
2
4
  * Highlights matched text in the Fuse.js search results.
3
5
  *
@@ -11,24 +13,6 @@ function highlight(
11
13
  highlightClassName = "highlight",
12
14
  matchKey = "title",
13
15
  ) {
14
- /**
15
- * Sets a value at a specified path within an object.
16
- *
17
- * @param {Object} obj - The object to modify.
18
- * @param {string} path - The dot-separated path specifying the location within the object.
19
- * @param {*} value - The value to set at the specified path.
20
- */
21
- const set = (obj, path, value) => {
22
- const pathValue = path.split(".");
23
- let i;
24
-
25
- for (i = 0; i < pathValue.length - 1; i++) {
26
- obj = obj[pathValue[i]];
27
- }
28
-
29
- obj[pathValue[i]] = value;
30
- };
31
-
32
16
  /**
33
17
  * Generates HTML string with highlighted regions.
34
18
  *
@@ -39,9 +23,12 @@ function highlight(
39
23
  const generateHighlightedText = (inputText, regions = []) => {
40
24
  let content = "";
41
25
  let nextUnhighlightedRegionStartingIndex = 0;
26
+ let longestIndexValue = 0;
42
27
 
43
- regions.forEach((region) => {
28
+ regions.forEach((region, index) => {
44
29
  const lastRegionNextIndex = region[1] + 1;
30
+ if (index && longestIndexValue > region[0]) return;
31
+ longestIndexValue = region[1];
45
32
 
46
33
  content += [
47
34
  inputText.substring(nextUnhighlightedRegionStartingIndex, region[0]),
@@ -65,11 +52,11 @@ function highlight(
65
52
 
66
53
  matches.forEach((match) => {
67
54
  if (match.key !== matchKey) return; // Skip if the match key does not match
68
- set(
69
- highlightedItem,
70
- match.key,
71
- generateHighlightedText(match.value, match.indices), // Highlight the matched text
72
- );
55
+ const value = generateHighlightedText(
56
+ capitalize(match.value),
57
+ mergeHighlightIndices(match.indices),
58
+ ); // Highlight the matched text
59
+ highlightedItem["highlightedText"] = value;
73
60
  });
74
61
 
75
62
  return highlightedItem;
@@ -9,3 +9,5 @@ export { default as isFiltersDirty } from "./is-filters-dirty";
9
9
  export { default as filterExternal } from "./filter-external";
10
10
  export { default as toggleAccordion } from "./toggle-accordion";
11
11
  export { default as getValue } from "./get-value";
12
+ export { default as capitalize } from "./capitalize";
13
+ export { default as mergeHighlightIndices } from "./merge-highlight-indices";
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Merge highlight indices which are overlapping with existing indices and sort the range in ascending order
3
+ *
4
+ * @param {number[][]} indices
5
+ * @param {Object} [opts]
6
+ * @param {boolean} [opts.mergeAdjacent=false]
7
+ * @returns {number[][]}
8
+ */
9
+ function mergeHighlightIndices(indices, { mergeAdjacent = false } = {}) {
10
+ if (!Array.isArray(indices)) return [];
11
+
12
+ // Normalize and sort
13
+ const sortedIndices = indices
14
+ .map(([startRange, endRange]) => {
15
+ if (startRange <= endRange) return [startRange, endRange];
16
+ else return [endRange, startRange];
17
+ })
18
+ .sort((range1, range2) => {
19
+ return range1[0] - range2[0] || range1[1] - range2[1];
20
+ });
21
+
22
+ // Sweep to merge & drop contained
23
+ const finalIndices = [];
24
+ for (const [startRange, endRange] of sortedIndices) {
25
+ if (finalIndices.length === 0) {
26
+ finalIndices.push([startRange, endRange]);
27
+ continue;
28
+ }
29
+ const lastRange = finalIndices[finalIndices.length - 1];
30
+ const canTwoRangeMerge = mergeAdjacent
31
+ ? startRange <= lastRange[1] + 1 // overlap or touching
32
+ : startRange <= lastRange[1]; // overlap only
33
+
34
+ if (canTwoRangeMerge) {
35
+ if (endRange > lastRange[1]) lastRange[1] = endRange;
36
+ } else {
37
+ finalIndices.push([startRange, endRange]);
38
+ }
39
+ }
40
+ return finalIndices;
41
+ }
42
+
43
+ export default mergeHighlightIndices;
@@ -25,7 +25,9 @@ function createFilterMethod(filterObject, tabIndex, EOxItemFilter) {
25
25
  id="${filterId}"
26
26
  .tabIndex=${tabIndex}
27
27
  .filterObject=${filterObject}
28
+ .results=${EOxItemFilter.results}
28
29
  .unstyled=${EOxItemFilter.unstyled}
30
+ @result=${EOxItemFilter.updateResult}
29
31
  @filter=${() => EOxItemFilter.search()}
30
32
  ></eox-itemfilter-text>`;
31
33
 
@@ -151,8 +151,13 @@ export function createItemListMethod(
151
151
  `,
152
152
  )}
153
153
  <div class="title-container small-line max truncate">
154
- <span class="title truncate"
155
- >${unsafeHTML(item[config.titleProperty])}</span
154
+ <span
155
+ class="title truncate ${item.highlightedText
156
+ ? "highlight-enabled"
157
+ : ""}"
158
+ >${unsafeHTML(
159
+ item.highlightedText || item[config.titleProperty],
160
+ )}</span
156
161
  >
157
162
  ${when(
158
163
  !!item[config.subTitleProperty],
@@ -168,8 +173,13 @@ export function createItemListMethod(
168
173
  `,
169
174
  () => html`
170
175
  <div class="small-line max truncate">
171
- <span class="title truncate"
172
- >${unsafeHTML(item[config.titleProperty])}</span
176
+ <span
177
+ class="title truncate ${item.highlightedText
178
+ ? "highlight-enabled"
179
+ : ""}"
180
+ >${unsafeHTML(
181
+ item.highlightedText || item[config.titleProperty],
182
+ )}</span
173
183
  >
174
184
  </div>
175
185
  `,
package/src/style.eox.js CHANGED
@@ -80,6 +80,9 @@ li label {
80
80
  .title {
81
81
  text-transform: var(--text-transform);
82
82
  }
83
+ .title.highlight-enabled {
84
+ text-transform: inherit;
85
+ }
83
86
  .subtitle {
84
87
  opacity: .7;
85
88
  }
@@ -13,6 +13,10 @@ export class EOxItemFilterText extends LitElement {
13
13
  attribute: boolean;
14
14
  type: ObjectConstructor;
15
15
  };
16
+ results: {
17
+ state: boolean;
18
+ type: ArrayConstructor;
19
+ };
16
20
  tabIndex: {
17
21
  attribute: boolean;
18
22
  type: NumberConstructor;
@@ -29,6 +33,10 @@ export class EOxItemFilterText extends LitElement {
29
33
  * @type Object
30
34
  */
31
35
  filterObject: any;
36
+ /**
37
+ * @type Array
38
+ */
39
+ results: any[];
32
40
  /**
33
41
  * @type Boolean
34
42
  */
@@ -1 +1 @@
1
- {"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../src/components/filters/text.js"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH;IAEE;;;;;;;;;;;;;;;;MAOC;IAKC;;OAEG;IACH,kBAAsB;IAEtB;;OAEG;IACH,kBAAqB;IAOrB;;OAEG;IACH,iBAAoB;IAUtB;;OAEG;IACH,cAEC;IAED;;OAEG;IACH,yEAEG;IAEH;;;;OAIG;IACH,oBAFa,IAAI,CAIhB;IAGD,oBA+BC;;CACF;2BAhHgC,KAAK"}
1
+ {"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../../../src/components/filters/text.js"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH;IAEE;;;;;;;;;;;;;;;;;;;;MAQC;IAKC;;OAEG;IACH,kBAAsB;IAEtB;;OAEG;IACH,eAAmB;IAEnB;;OAEG;IACH,kBAAqB;IAOrB;;OAEG;IACH,iBAAoB;IAgCtB;;OAEG;IACH,cAEC;IAED;;OAEG;IACH,yEAEG;IAEH;;;;OAIG;IACH,oBAFa,IAAI,CAIhB;IAGD,oBAgCC;;CACF;2BA7IgC,KAAK"}
@@ -0,0 +1,9 @@
1
+ export default capitalize;
2
+ /**
3
+ * Capitalize the first letter of each word in a string.
4
+ *
5
+ * @param {string} str - The string to capitalize.
6
+ * @returns {string} The capitalized string.
7
+ */
8
+ declare function capitalize(str: string): string;
9
+ //# sourceMappingURL=capitalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capitalize.d.ts","sourceRoot":"","sources":["../../src/helpers/capitalize.js"],"names":[],"mappings":";AAAA;;;;;GAKG;AACH,iCAHW,MAAM,GACJ,MAAM,CAOlB"}
@@ -1 +1 @@
1
- {"version":3,"file":"filter-client.d.ts","sourceRoot":"","sources":["../../src/helpers/filter-client.js"],"names":[],"mappings":"AAYO,kCAHI,KAAK,KAAQ,yBAWvB;AAUM,8BALI,KAAK,KAAQ,8BAGX,OAAO,CAAC,KAAK,KAAQ,CAAC,CAsJlC"}
1
+ {"version":3,"file":"filter-client.d.ts","sourceRoot":"","sources":["../../src/helpers/filter-client.js"],"names":[],"mappings":"AAYO,kCAHI,KAAK,KAAQ,yBAYvB;AAUM,8BALI,KAAK,KAAQ,8BAGX,OAAO,CAAC,KAAK,KAAQ,CAAC,CAsJlC"}
@@ -1 +1 @@
1
- {"version":3,"file":"highlighting.d.ts","sourceRoot":"","sources":["../../src/helpers/highlighting.js"],"names":[],"mappings":";AAAA;;;;;;;GAOG;AACH,6CALW,KAAK,KAAQ,uBACb,MAAM,aACN,MAAM,GACJ,KAAK,KAAQ,CAsEzB"}
1
+ {"version":3,"file":"highlighting.d.ts","sourceRoot":"","sources":["../../src/helpers/highlighting.js"],"names":[],"mappings":";AAEA;;;;;;;GAOG;AACH,6CALW,KAAK,KAAQ,uBACb,MAAM,aACN,MAAM,GACJ,KAAK,KAAQ,CAuDzB"}
@@ -7,6 +7,8 @@ export { default as isFiltersDirty } from "./is-filters-dirty";
7
7
  export { default as filterExternal } from "./filter-external";
8
8
  export { default as toggleAccordion } from "./toggle-accordion";
9
9
  export { default as getValue } from "./get-value";
10
+ export { default as capitalize } from "./capitalize";
11
+ export { default as mergeHighlightIndices } from "./merge-highlight-indices";
10
12
  export { intersects, within } from "./spatial";
11
13
  export { indexItems, filter } from "./filter-client";
12
14
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,13 @@
1
+ export default mergeHighlightIndices;
2
+ /**
3
+ * Merge highlight indices which are overlapping with existing indices and sort the range in ascending order
4
+ *
5
+ * @param {number[][]} indices
6
+ * @param {Object} [opts]
7
+ * @param {boolean} [opts.mergeAdjacent=false]
8
+ * @returns {number[][]}
9
+ */
10
+ declare function mergeHighlightIndices(indices: number[][], { mergeAdjacent }?: {
11
+ mergeAdjacent?: boolean;
12
+ }): number[][];
13
+ //# sourceMappingURL=merge-highlight-indices.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merge-highlight-indices.d.ts","sourceRoot":"","sources":["../../src/helpers/merge-highlight-indices.js"],"names":[],"mappings":";AAAA;;;;;;;GAOG;AACH,gDALW,MAAM,EAAE,EAAE,sBAElB;IAAuB,aAAa,GAA5B,OAAO;CACf,GAAU,MAAM,EAAE,EAAE,CAkCtB"}
@@ -1 +1 @@
1
- {"version":3,"file":"create-filter.d.ts","sourceRoot":"","sources":["../../../src/methods/itemfilter/create-filter.js"],"names":[],"mappings":";AAIA;;;;;;;GAOG;AACH,iEAJW,MAAM,uBAEJ,OAAO,KAAK,EAAE,kBAAkB,CAsE5C"}
1
+ {"version":3,"file":"create-filter.d.ts","sourceRoot":"","sources":["../../../src/methods/itemfilter/create-filter.js"],"names":[],"mappings":";AAIA;;;;;;;GAOG;AACH,iEAJW,MAAM,uBAEJ,OAAO,KAAK,EAAE,kBAAkB,CAwE5C"}
@@ -1 +1 @@
1
- {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../../src/methods/results/create.js"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,6DAJW,MAAM,8BAEJ,OAAO,KAAK,EAAE,kBAAkB,CAuC5C;AAED;;;;;;GAMG;AACH,sFAHW,MAAM,GACJ,OAAO,KAAK,EAAE,cAAc,CAkJxC"}
1
+ {"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../../src/methods/results/create.js"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,6DAJW,MAAM,8BAEJ,OAAO,KAAK,EAAE,kBAAkB,CAuC5C;AAED;;;;;;GAMG;AACH,sFAHW,MAAM,GACJ,OAAO,KAAK,EAAE,cAAc,CA4JxC"}
@@ -1 +1 @@
1
- {"version":3,"file":"style.eox.d.ts","sourceRoot":"","sources":["../src/style.eox.js"],"names":[],"mappings":"AAKA,8BA0cE"}
1
+ {"version":3,"file":"style.eox.d.ts","sourceRoot":"","sources":["../src/style.eox.js"],"names":[],"mappings":"AAKA,8BA6cE"}