@cas-smartdesign/token-selector 0.17.2 → 0.18.1

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.
@@ -82,7 +82,17 @@ export default class TokenSelector extends LitElement {
82
82
  protected firstUpdated(_changedProperties: Map<string | number | symbol, unknown>): void;
83
83
  private handleInputKeyDown;
84
84
  private handleKeyDown;
85
- private addMatchingItems;
85
+ /**
86
+ *
87
+ * Called when items were given by paste or drag events to token-selector.
88
+ * The purpose of this function is to add or create items accordingly.
89
+ *
90
+ * @param {unknown[]} items the pasted or dropped items
91
+ * @return {number[]} the indexes that were added successfully
92
+ */
93
+ selectOrAddItems: (items: unknown & {
94
+ caption?: string;
95
+ }[]) => number[];
86
96
  removeSelectionOrActiveToken(): void;
87
97
  private updateActiveToken;
88
98
  private commitTokenValue;
@@ -94,7 +104,7 @@ export default class TokenSelector extends LitElement {
94
104
  private isTokenNotSelected;
95
105
  private get tokenSuggestPopover();
96
106
  private updateItems;
97
- removeTokens(tokenIndexes: number[]): void;
107
+ removeTokens(indexes: number[]): void;
98
108
  private onTokenClick;
99
109
  private disableIfNeeded;
100
110
  private handleTokenSelection;
@@ -102,7 +112,7 @@ export default class TokenSelector extends LitElement {
102
112
  private get inputElement();
103
113
  get activeTokenElement(): Token | null;
104
114
  private getTokenElement;
105
- private findIndex;
115
+ findIndex(token: TokenData): number;
106
116
  get activeTokenIndex(): number;
107
117
  set activeTokenIndex(value: number);
108
118
  private get hasInputValue();
@@ -1,40 +1,46 @@
1
- import { LitElement as x, unsafeCSS as T, html as m, nothing as I } from "lit";
1
+ import { LitElement as I, unsafeCSS as T, html as m, nothing as x } from "lit";
2
2
  import { property as a } from "lit/decorators/property.js";
3
3
  import "@cas-smartdesign/lit-input";
4
4
  import S from "@cas-smartdesign/field-validation-message";
5
5
  import { generator as _ } from "@cas-smartdesign/list-item";
6
- import C from "@cas-smartdesign/popover";
6
+ import L from "@cas-smartdesign/popover";
7
7
  import y from "@cas-smartdesign/virtual-list";
8
- import { KeyDownDelegator as L } from "@cas-smartdesign/element-utils";
9
- import { ifDefined as A } from "lit/directives/if-defined.js";
8
+ import { KeyDownDelegator as A } from "@cas-smartdesign/element-utils";
9
+ import { ifDefined as C } from "lit/directives/if-defined.js";
10
10
  import { unsafeSVG as P } from "lit/directives/unsafe-svg.js";
11
11
  import D from "@cas-smartdesign/image-tools";
12
12
  class O {
13
13
  constructor(e, t, i, n) {
14
- this.inputElement = e, this.notSelectedTokensProvider = t, this.initializeCallback = n, this.filter = (s, o) => s ? o.filter((d) => d.disabled || d.deactivated ? !1 : d.caption && d.caption.toLowerCase().includes(s)) : o, y.ensureDefined(), this._tokenList = new y(), this._tokenList.style.minWidth = "250px", this._tokenList.style.maxHeight = "49vh", this._tokenList.itemHeight = 50, this._tokenList.addEventListener("selection", (s) => {
14
+ this.inputElement = e, this.notSelectedTokensProvider = t, this.initializeCallback = n, this.filter = (s, o) => s ? o.filter((d) => d.disabled || d.deactivated ? !1 : d.caption && d.caption.toLowerCase().includes(s)) : o, y.ensureDefined(), this._tokenList = new y(), this._tokenList.style.minWidth = "250px", this._tokenList.style.maxHeight = "49vh", this._tokenList.itemHeight = 50, this._tokenList.addEventListener("pointerdown", () => {
15
+ this.pointerDown = !0, window.addEventListener(
16
+ "pointerup",
17
+ () => {
18
+ this.pointerDown = !1, this._tokenList.increaseWidthOnNextRenderIfNeeded(), this._tokenList.isUpdatePending || this._tokenList.requestUpdate();
19
+ },
20
+ { once: !0, capture: !0 }
21
+ );
22
+ }), this._tokenList.addEventListener("selection", (s) => {
15
23
  const o = s.detail.index, d = this._suggestItems[o];
16
24
  i(d), this.hide();
17
25
  }), this._tokenList.addEventListener("data-request", (s) => {
18
- this.lastRequestedStartIndex = s.detail.startIndex, this.lastRequestedStopIndex = s.detail.stopIndex, this._tokenList.items = this._suggestItems.slice(
26
+ this.lastRequestedStartIndex = s.detail.startIndex, this.lastRequestedStopIndex = s.detail.stopIndex, this.pointerDown || this._tokenList.increaseWidthOnNextRenderIfNeeded(), this._tokenList.items = this._suggestItems.slice(
19
27
  this.lastRequestedStartIndex,
20
28
  this.lastRequestedStopIndex + 1
21
29
  );
22
- }), new L(this._tokenList, (s, o, d) => {
30
+ }), new A(this._tokenList, (s, o, d) => {
23
31
  this._tokenList.dispatchEvent(new KeyboardEvent(s.type, s)), !d && !this.isOpened && this.show();
24
32
  }).connect(e);
25
33
  }
26
34
  show() {
27
- this.inputElement.effectiveDisabled || (this.popover, this._suggestItems = this.filterItems((this.inputElement.value || "").toLowerCase()), this._suggestItems.length == 0 ? this.hide() : (this._tokenList.itemCount = this._suggestItems.length, this._tokenList.focusIndex = -1, requestAnimationFrame(() => {
28
- this.lastRequestedStartIndex != null && (this._tokenList.items = this._suggestItems.slice(
35
+ this.inputElement.effectiveDisabled || (this.popover, this._suggestItems = this.filterItems((this.inputElement.value || "").toLowerCase()), this._suggestItems.length == 0 ? this.hide() : (this._tokenList.itemCount = this._suggestItems.length, this._tokenList.focusIndex = -1, this._tokenList.style.width = null, requestAnimationFrame(() => {
36
+ this.lastRequestedStartIndex != null && (this._tokenList.increaseWidthOnNextRenderIfNeeded(), this._tokenList.items = this._suggestItems.slice(
29
37
  this.lastRequestedStartIndex,
30
38
  this.lastRequestedStopIndex + 1
31
39
  ));
32
- }), Object.assign(this._tokenList.style, {
33
- minWidth: `${Math.max(this.popover.targetElement.offsetWidth, 250)}px`
34
40
  }), this.popover.show()));
35
41
  }
36
42
  refreshItems() {
37
- this.isOpened && (this._suggestItems = this.filterItems((this.inputElement.value || "").toLowerCase()), this._suggestItems.length == 0 ? this.hide() : (this._tokenList.itemCount = this._suggestItems.length, this._tokenList.items = []));
43
+ this.isOpened && (this._suggestItems = this.filterItems((this.inputElement.value || "").toLowerCase()), this._suggestItems.length == 0 ? this.hide() : (this._tokenList.itemCount = this._suggestItems.length, this._tokenList.items = [], this._tokenList.style.width = null));
38
44
  }
39
45
  filterItems(e) {
40
46
  const t = this.notSelectedTokensProvider();
@@ -51,7 +57,7 @@ class O {
51
57
  return this._popover || (this._popover = this.createPopover(), this.initializeCallback(this)), this._popover;
52
58
  }
53
59
  createPopover() {
54
- const e = new C();
60
+ const e = new L();
55
61
  return e.setAttribute("trigger-type", "manual"), e.setAttribute("placement", "bottom-start"), e.setAttribute("modal", ""), e.setAttribute("popover-for", "token-autosuggest-popover"), e.setAttribute("offset", "-2"), e.targetElement = this.inputElement, e.appendChild(this._tokenList), e;
56
62
  }
57
63
  get isOpened() {
@@ -64,17 +70,17 @@ class O {
64
70
  const q = ":host{flex-shrink:0;max-width:100%}.container{display:flex;height:28px;background-color:var(--token-background-color, #f3f3f3);border:var(--token-border);box-sizing:border-box}.container .icon-wrapper{display:flex;flex-shrink:0;justify-content:center;align-items:center;width:28px;height:100%;overflow:hidden;background-color:var(--token-icon-background-color, transparent)}.container .icon-wrapper .icon{height:100%;width:100%;object-fit:contain;background-size:cover;background-repeat:no-repeat;background-position:center}.container .value{display:inline-block;align-self:center;padding:0 8px;font-family:Segoe UI,Lucida Sans,Arial,sans-serif;font-size:16px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.container .delete-button-wrapper{display:flex;align-self:center;height:16px;width:16px;padding-right:8px;opacity:.5}.container .delete-button-wrapper:hover{cursor:pointer;filter:brightness(10%);opacity:1}", M = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16">\r
65
71
  <path d="m3.5 12.5 9-9m-9 0 9 9" style="fill:none;stroke:#333;stroke-linecap:square;stroke-width:1.1px"/>\r
66
72
  </svg>`;
67
- var $ = Object.defineProperty, R = Object.getOwnPropertyDescriptor, g = (r, e, t, i) => {
68
- for (var n = i > 1 ? void 0 : i ? R(e, t) : e, s = r.length - 1, o; s >= 0; s--)
73
+ var R = Object.defineProperty, $ = Object.getOwnPropertyDescriptor, g = (r, e, t, i) => {
74
+ for (var n = i > 1 ? void 0 : i ? $(e, t) : e, s = r.length - 1, o; s >= 0; s--)
69
75
  (o = r[s]) && (n = (i ? o(e, t, n) : o(n)) || n);
70
- return i && n && $(e, t, n), n;
76
+ return i && n && R(e, t, n), n;
71
77
  };
72
78
  const w = {
73
79
  fromAttribute: (r) => r == "true",
74
80
  toAttribute: (r) => r
75
81
  };
76
82
  var u;
77
- const p = (u = class extends x {
83
+ const p = (u = class extends I {
78
84
  constructor() {
79
85
  super(...arguments), this.value = "", this.type = "", this.icon = "", this.iconPlaceholder = "", this.iconBackgroundColor = "", this._checked = !1;
80
86
  }
@@ -104,7 +110,7 @@ const p = (u = class extends x {
104
110
  ${this.renderIcon()}
105
111
  <slot name="after-icon"></slot>
106
112
  <div class="value">${this.value}</div>
107
- ${this.disabled ? I : m` <div class="delete-button-wrapper">${P(M)}</div> `}
113
+ ${this.disabled ? x : m` <div class="delete-button-wrapper">${P(M)}</div> `}
108
114
  </div>
109
115
  `;
110
116
  }
@@ -112,12 +118,12 @@ const p = (u = class extends x {
112
118
  if (this.icon || this.iconPlaceholder) {
113
119
  const e = this.iconBackgroundColor != null ? `--token-icon-background-color: ${this.iconBackgroundColor}` : void 0;
114
120
  return m`
115
- <div class="icon-wrapper" style="${A(e)}" role="img">
121
+ <div class="icon-wrapper" style="${C(e)}" role="img">
116
122
  <div part="icon" class="icon"></div>
117
123
  </div>
118
124
  `;
119
125
  }
120
- return I;
126
+ return x;
121
127
  }
122
128
  updated(e) {
123
129
  super.updated(e), (e.has("icon") || e.has("iconPlaceholder")) && (this.icon || this.iconPlaceholder) && D.showImage(this.shadowRoot.querySelector(".icon"), this.icon, this.iconPlaceholder), e.has("disabled") && !this.disabled && this.checked && (this.checked = !1);
@@ -156,13 +162,13 @@ g([
156
162
  a({ converter: w, reflect: !0, attribute: "aria-checked" })
157
163
  ], p.prototype, "checked", 1);
158
164
  let b = p;
159
- const G = (r, e) => {
165
+ const N = (r, e) => {
160
166
  const t = document.createElement(b.ID);
161
167
  return r && (t.value = r.caption, t.type = r.type, t.icon = r.icon, t.iconPlaceholder = r.iconPlaceholder, t.iconBackgroundColor = r.iconBackgroundColor, t.disabled = r.disabled, t.index = e), t;
162
168
  };
163
169
  b.ensureDefined();
164
170
  const k = "web application/json";
165
- class B {
171
+ class G {
166
172
  constructor(e, t, i) {
167
173
  navigator.clipboard && (e.addEventListener("copy", () => {
168
174
  t() || this.writeToClipboard(e);
@@ -198,13 +204,13 @@ class B {
198
204
  }
199
205
  }
200
206
  }
201
- const F = ":host{display:flex;contain:layout style}:host .additonal-content::slotted(*){align-self:end;flex-shrink:0}:host([drop]) ::slotted(*){pointer-events:none}:host(:not([readonly])) ::slotted(sd-token:last-of-type){margin-right:24px;margin-bottom:4px}.container{display:flex;flex-wrap:wrap;gap:8px;flex-grow:1;min-width:0;margin-top:auto}.container .placeholder{color:#767676;line-height:24px;align-self:flex-end}.input{flex-grow:1;min-width:0;width:100%;margin-top:auto}.input .token-wrapper{display:flex;flex-wrap:wrap;flex:1 0 auto;width:100%;gap:8px}";
202
- class H {
207
+ const B = ":host{display:flex;contain:layout style}:host .additonal-content::slotted(*){align-self:end;flex-shrink:0}:host([drop]) ::slotted(*){pointer-events:none}:host(:not([readonly])) ::slotted(sd-token:last-of-type){max-width:calc(100% - 24px);margin-right:24px;margin-bottom:4px}.container{display:flex;flex-wrap:wrap;gap:8px;flex-grow:1;min-width:0;margin-top:auto}.container .placeholder{color:#767676;line-height:24px;align-self:flex-end}.input{flex-grow:1;min-width:0;width:100%;margin-top:auto}.input .token-wrapper{display:flex;flex-wrap:wrap;flex:1 0 auto;width:100%;gap:8px}";
208
+ class F {
203
209
  constructor(e, t) {
204
210
  this.tokenSelector = e;
205
211
  const i = /* @__PURE__ */ new Map(), n = (s) => {
206
212
  const o = s.target;
207
- if (!e.contains(o)) {
213
+ if (e.removeAttribute("drag-source"), !e.contains(o)) {
208
214
  const d = s.addedIndexes;
209
215
  d && e.removeTokens(d.map((v) => i.get(v)));
210
216
  }
@@ -217,7 +223,7 @@ class H {
217
223
  s.preventDefault();
218
224
  return;
219
225
  }
220
- o.setAttribute("aria-checked", "true");
226
+ e.setAttribute("drag-source", ""), o.setAttribute("aria-checked", "true");
221
227
  const d = e.querySelectorAll("[slot='items'][aria-checked='true']");
222
228
  if (d.length > 0) {
223
229
  const v = Array.from(d).filter((f) => !f.disabled).map((f, E) => (i.set(E, f.index), e.items[f.index]));
@@ -242,7 +248,7 @@ class H {
242
248
  } catch {
243
249
  }
244
250
  }), e.addEventListener("dragend", () => {
245
- window.removeEventListener("drop", n), i.clear();
251
+ window.removeEventListener("drop", n), e.removeAttribute("drag-source"), i.clear();
246
252
  });
247
253
  }
248
254
  createCustomDragImage(e, t) {
@@ -271,21 +277,21 @@ class H {
271
277
  }), i.slot = "items", this.tokenSelector.appendChild(i), requestAnimationFrame(() => i.remove()), i;
272
278
  }
273
279
  }
274
- var N = Object.defineProperty, j = Object.getOwnPropertyDescriptor, h = (r, e, t, i) => {
275
- for (var n = i > 1 ? void 0 : i ? j(e, t) : e, s = r.length - 1, o; s >= 0; s--)
280
+ var H = Object.defineProperty, K = Object.getOwnPropertyDescriptor, h = (r, e, t, i) => {
281
+ for (var n = i > 1 ? void 0 : i ? K(e, t) : e, s = r.length - 1, o; s >= 0; s--)
276
282
  (o = r[s]) && (n = (i ? o(e, t, n) : o(n)) || n);
277
- return i && n && N(e, t, n), n;
283
+ return i && n && H(e, t, n), n;
278
284
  };
279
- function K(r, e) {
285
+ function j(r, e) {
280
286
  let t;
281
287
  return function(...i) {
282
288
  t != null && clearTimeout(t), t = window.setTimeout(() => r(...i), e);
283
289
  };
284
290
  }
285
- var V = /* @__PURE__ */ ((r) => (r.RemoveOnly = "remove-only", r.Multi = "multi", r))(V || {}), c;
286
- const l = (c = class extends x {
291
+ var U = /* @__PURE__ */ ((r) => (r.RemoveOnly = "remove-only", r.Multi = "multi", r))(U || {}), c;
292
+ const l = (c = class extends I {
287
293
  constructor() {
288
- super(...arguments), this.selectionMode = "multi", this.items = [], this.selectedIndexes = [], this._tokenGenerator = G, this._autoSuggestItemGenerator = _, this.additionalTokenCommittingKeys = [], this._activeTokenIndex = -1, this.handleInputKeyDown = (e, t) => {
294
+ super(...arguments), this.selectionMode = "multi", this.items = [], this.selectedIndexes = [], this._tokenGenerator = N, this._autoSuggestItemGenerator = _, this.additionalTokenCommittingKeys = [], this._activeTokenIndex = -1, this.handleInputKeyDown = (e, t) => {
289
295
  if ((e.key === "Enter" || this.additionalTokenCommittingKeys.includes(e.key)) && t.value) {
290
296
  e.preventDefault(), e.stopPropagation(), this.commitTokenValue(t.value);
291
297
  return;
@@ -333,6 +339,12 @@ const l = (c = class extends x {
333
339
  break;
334
340
  }
335
341
  }
342
+ }, this.selectOrAddItems = (e) => {
343
+ const t = [], i = e.map((n, s) => {
344
+ const o = n.caption == null ? -1 : this.findIndex(n);
345
+ return o != -1 && !this.items[o].disabled && !this.selectedIndexes.includes(o) ? (t.push(s), o) : -1;
346
+ }).filter((n) => n != -1);
347
+ return i.length > 0 && this.handleTokenSelection(i), t;
336
348
  }, this.handleWindowPointerDown = (e) => {
337
349
  const t = this.inputElement;
338
350
  if (!t || !t.value)
@@ -341,10 +353,10 @@ const l = (c = class extends x {
341
353
  if (n instanceof HTMLElement && (e.composedPath().indexOf(t) > -1 || n.getAttribute("popover-for") === "token-autosuggest-popover"))
342
354
  return !0;
343
355
  }) || this.commitTokenValue(t.value);
344
- }, this.debouncedShowTokenSuggestPopover = K(this.showFilteredTokenSuggestions.bind(this), 200);
356
+ }, this.debouncedShowTokenSuggestPopover = j(this.showFilteredTokenSuggestions.bind(this), 200);
345
357
  }
346
358
  static get styles() {
347
- return T(F);
359
+ return T(B);
348
360
  }
349
361
  get tokenGenerator() {
350
362
  return this._tokenGenerator;
@@ -372,7 +384,7 @@ const l = (c = class extends x {
372
384
  <slot name="items"
373
385
  >${this.placeholder ? m`<span part="remove-only-placeholder" class="placeholder"
374
386
  >${this.placeholder}</span
375
- >` : I}</slot
387
+ >` : x}</slot
376
388
  >
377
389
  </div>
378
390
  <slot class="additonal-content" name="additional-content"></slot>` : m`
@@ -398,21 +410,14 @@ const l = (c = class extends x {
398
410
  !this.contains(i) && !this.shadowRoot.contains(i) && this.querySelectorAll("[slot='items'][aria-checked='true']").forEach(
399
411
  (n) => n.checked = !1
400
412
  );
401
- }), new B(
413
+ }), new G(
402
414
  this,
403
415
  () => {
404
416
  var t;
405
417
  return (t = this.inputElement) == null ? void 0 : t.value;
406
418
  },
407
- (t) => this.addMatchingItems(t)
408
- ), new H(this, (t) => this.addMatchingItems(t));
409
- }
410
- addMatchingItems(e) {
411
- const t = [], i = e.map((n, s) => {
412
- const o = n.caption == null ? -1 : this.findIndex(n);
413
- return o != -1 && !this.items[o].disabled && !this.selectedIndexes.includes(o) ? (t.push(s), o) : -1;
414
- }).filter((n) => n != -1);
415
- return i.length > 0 && this.handleTokenSelection(i), t;
419
+ (t) => this.selectOrAddItems(t)
420
+ ), new F(this, (t) => this.selectOrAddItems(t));
416
421
  }
417
422
  removeSelectionOrActiveToken() {
418
423
  if (!this.disabled) {
@@ -503,20 +508,26 @@ const l = (c = class extends x {
503
508
  }), this.appendChild(t), this._tokenSuggestPopover && this._tokenSuggestPopover.refreshItems();
504
509
  }
505
510
  removeTokens(e) {
506
- this.disabled || (this.activeTokenIndex != -1 && e.includes(this.activeTokenElement.index) && (this.activeTokenIndex = -1), this.selectionMode == "remove-only" ? this.dispatchEvent(
507
- new CustomEvent("tokens-removed", {
508
- detail: {
509
- removedIndices: e
510
- }
511
- })
512
- ) : (this.selectedIndexes = this.selectedIndexes.filter((t) => !e.includes(t)), this.dispatchEvent(
513
- new CustomEvent("tokens-removed", {
514
- detail: {
515
- removedIndices: e,
516
- selectedIndices: [...this.selectedIndexes]
517
- }
518
- })
519
- )));
511
+ if (!(this.disabled || e == null || e.length == 0))
512
+ if (this.activeTokenIndex != -1 && e.includes(this.activeTokenElement.index) && (this.activeTokenIndex = -1), this.selectionMode == "remove-only")
513
+ this.dispatchEvent(
514
+ new CustomEvent("tokens-removed", {
515
+ detail: {
516
+ removedIndices: e
517
+ }
518
+ })
519
+ );
520
+ else {
521
+ const t = e.filter((i) => this.selectedIndexes.includes(i));
522
+ t.length > 0 && (this.selectedIndexes = this.selectedIndexes.filter((i) => !t.includes(i)), this.dispatchEvent(
523
+ new CustomEvent("tokens-removed", {
524
+ detail: {
525
+ removedIndices: t,
526
+ selectedIndices: [...this.selectedIndexes]
527
+ }
528
+ })
529
+ ));
530
+ }
520
531
  }
521
532
  onTokenClick(e, t) {
522
533
  let i;
@@ -588,7 +599,7 @@ const l = (c = class extends x {
588
599
  }, c.ID = "sd-token-selector", c.ensureDefined = () => {
589
600
  b.ensureDefined(), customElements.get(c.ID) || customElements.define(c.ID, c);
590
601
  }, c.shadowRootOptions = {
591
- ...x.shadowRootOptions,
602
+ ...I.shadowRootOptions,
592
603
  delegatesFocus: !0
593
604
  }, c);
594
605
  h([
@@ -636,8 +647,8 @@ h([
636
647
  ], l.prototype, "caseSensitive", 2);
637
648
  let se = l;
638
649
  export {
639
- V as SelectionMode,
650
+ U as SelectionMode,
640
651
  se as default,
641
- G as generator
652
+ N as generator
642
653
  };
643
654
  //# sourceMappingURL=token-selector.mjs.map