|
1 | 1 | import { localized, msg } from "@lit/localize";
|
2 | 2 | import { Task, TaskStatus } from "@lit/task";
|
3 | 3 | import type { SlInput, SlMenuItem } from "@shoelace-style/shoelace";
|
| 4 | +import { TwoWayMap } from "@solancer/two-way-map"; |
4 | 5 | import Fuse from "fuse.js";
|
5 | 6 | import { html, nothing } from "lit";
|
6 | 7 | import { customElement, property, query, state } from "lit/decorators.js";
|
@@ -62,8 +63,8 @@ export class CollectionsAdd extends BtrixElement {
|
62 | 63 | @query("btrix-combobox")
|
63 | 64 | private readonly combobox?: Combobox | null;
|
64 | 65 |
|
65 |
| - // Map collection names to ID for filtering fuzzy search |
66 |
| - private readonly collectionNames = new Map<string, string>(); |
| 66 | + // Map collection names to ID for managing search options |
| 67 | + private readonly nameSearchMap = new TwoWayMap<string, string>(); |
67 | 68 |
|
68 | 69 | private get collectionIds() {
|
69 | 70 | return this.collections.map(({ id }) => id);
|
@@ -137,13 +138,20 @@ export class CollectionsAdd extends BtrixElement {
|
137 | 138 | const { item } = e.detail;
|
138 | 139 |
|
139 | 140 | if (item.name) {
|
140 |
| - this.collectionNames.set(item.name, item.id); |
| 141 | + this.nameSearchMap.set(item.name, item.id); |
141 | 142 | }
|
142 | 143 | }}
|
143 | 144 | @btrix-remove=${(e: BtrixRemoveLinkedCollectionEvent) => {
|
144 | 145 | const { id } = e.detail.item;
|
145 | 146 |
|
146 | 147 | this.removeCollection(id);
|
| 148 | +
|
| 149 | + // Remove from search mapping |
| 150 | + const name = this.nameSearchMap.getByValue(id); |
| 151 | +
|
| 152 | + if (name) { |
| 153 | + this.nameSearchMap.delete(name); |
| 154 | + } |
147 | 155 | }}
|
148 | 156 | ></btrix-linked-collections>
|
149 | 157 | </div>
|
@@ -174,7 +182,7 @@ export class CollectionsAdd extends BtrixElement {
|
174 | 182 | this.collections = [...this.collections, coll];
|
175 | 183 | void this.dispatchChange();
|
176 | 184 |
|
177 |
| - this.collectionNames.set(coll.name, coll.id); |
| 185 | + this.nameSearchMap.set(coll.name, coll.id); |
178 | 186 |
|
179 | 187 | if (this.input) {
|
180 | 188 | this.input.value = "";
|
@@ -238,7 +246,7 @@ export class CollectionsAdd extends BtrixElement {
|
238 | 246 | const results = fuse
|
239 | 247 | ?.search(this.searchByValue)
|
240 | 248 | // Filter out items that have been selected
|
241 |
| - .filter(({ item }) => !this.collectionNames.get(item)) |
| 249 | + .filter(({ item }) => !this.nameSearchMap.get(item)) |
242 | 250 | // Show first few results
|
243 | 251 | .slice(0, 5);
|
244 | 252 |
|
|
0 commit comments