monobill-mintui 0.6.640 → 0.6.641

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.
Files changed (116) hide show
  1. package/dist/components/alert/browser.js +3 -3
  2. package/dist/components/alert/index.cjs +1 -1
  3. package/dist/components/alert/index.js +3 -3
  4. package/dist/components/back-button/browser.js +2 -2
  5. package/dist/components/back-button/index.cjs +1 -1
  6. package/dist/components/back-button/index.js +2 -2
  7. package/dist/components/bottom-navigation/browser.js +2 -2
  8. package/dist/components/bottom-navigation/index.cjs +1 -1
  9. package/dist/components/bottom-navigation/index.js +2 -2
  10. package/dist/components/button/browser.js +1 -1
  11. package/dist/components/button/index.cjs +1 -1
  12. package/dist/components/button/index.js +1 -1
  13. package/dist/components/checkbox/browser.js +2 -2
  14. package/dist/components/checkbox/index.cjs +1 -1
  15. package/dist/components/checkbox/index.js +2 -2
  16. package/dist/components/chip/browser.js +1 -1
  17. package/dist/components/chip/index.cjs +1 -1
  18. package/dist/components/chip/index.js +1 -1
  19. package/dist/components/chunks/{button-DGuqIeFS.js → button-BNLP47mK.js} +1 -1
  20. package/dist/components/chunks/{button-M7gi8RMZ.js → button-DPSkBQsf.js} +2 -2
  21. package/dist/components/chunks/caret-down-BAb6wdmc.js +1 -0
  22. package/dist/components/chunks/{caret-down-BYsBQfd3.js → caret-down-BEZ03ALe.js} +1 -1
  23. package/dist/components/chunks/{caret-right-FEHdZI-G.js → caret-right-B2bXPFkY.js} +1 -1
  24. package/dist/components/chunks/caret-right-DJGdf2wM.js +1 -0
  25. package/dist/components/chunks/caret-up-CjzR9-3k.js +1 -0
  26. package/dist/components/chunks/{caret-up-Gd2tjBHR.js → caret-up-DPGZf8ya.js} +1 -1
  27. package/dist/components/chunks/{chip-DcqvmQKS.js → chip-Dl4i0Kiy.js} +3 -3
  28. package/dist/components/chunks/{chip-CK6f9Fj9.js → chip-QIGZRmc3.js} +1 -1
  29. package/dist/components/chunks/close-BzkX2kT6.js +1 -0
  30. package/dist/components/chunks/{close-BDBHd3xW.js → close-yUq-JzG7.js} +1 -1
  31. package/dist/components/chunks/{confirm-action-DxvkO8OY.js → confirm-action-BQVO6aFn.js} +1 -1
  32. package/dist/components/chunks/{confirm-action-D4U-R6zW.js → confirm-action-THfH8TEH.js} +2 -2
  33. package/dist/components/chunks/icon--Phz6jFp.js +1 -0
  34. package/dist/components/chunks/icon-Cvg6P_4Z.js +1 -0
  35. package/dist/components/chunks/{icon-lfkgHbsk.js → icon-JJfYHBiG.js} +3 -3
  36. package/dist/components/chunks/{icon-DZP-hLHj.js → icon-NUvffC5m.js} +124 -102
  37. package/dist/components/chunks/{input-_Wu3YvDC.js → input-BYgCqs0P.js} +1 -1
  38. package/dist/components/chunks/{input-Dvzzg6z7.js → input-D_Lw8-Tf.js} +4 -4
  39. package/dist/components/chunks/{modal-DaAORJq2.js → modal-B8dq9uff.js} +1 -1
  40. package/dist/components/chunks/{modal-DVbvAPLU.js → modal-DVc8n49b.js} +2 -2
  41. package/dist/components/chunks/{search-BvkhiIaJ.js → search-B2lYd40B.js} +1 -1
  42. package/dist/components/chunks/search-BrcqVhiu.js +1 -0
  43. package/dist/components/confirm-action/browser.js +1 -1
  44. package/dist/components/confirm-action/index.cjs +1 -1
  45. package/dist/components/confirm-action/index.js +1 -1
  46. package/dist/components/date-picker/browser.js +4 -4
  47. package/dist/components/date-picker/index.cjs +1 -1
  48. package/dist/components/date-picker/index.js +4 -4
  49. package/dist/components/dropzone/browser.js +3 -3
  50. package/dist/components/dropzone/index.cjs +1 -1
  51. package/dist/components/dropzone/index.js +3 -3
  52. package/dist/components/icon/browser.js +1 -1
  53. package/dist/components/icon/icons/index.d.ts +3 -0
  54. package/dist/components/icon/icons/index.d.ts.map +1 -1
  55. package/dist/components/icon/icons/redo.d.ts +3 -0
  56. package/dist/components/icon/icons/redo.d.ts.map +1 -0
  57. package/dist/components/icon/icons/save-alt.d.ts +3 -0
  58. package/dist/components/icon/icons/save-alt.d.ts.map +1 -0
  59. package/dist/components/icon/icons/undo.d.ts +3 -0
  60. package/dist/components/icon/icons/undo.d.ts.map +1 -0
  61. package/dist/components/icon/index.cjs +1 -1
  62. package/dist/components/icon/index.js +1 -1
  63. package/dist/components/input/browser.js +1 -1
  64. package/dist/components/input/index.cjs +1 -1
  65. package/dist/components/input/index.js +1 -1
  66. package/dist/components/modal/browser.js +1 -1
  67. package/dist/components/modal/index.cjs +1 -1
  68. package/dist/components/modal/index.js +1 -1
  69. package/dist/components/navigation/browser.js +2 -2
  70. package/dist/components/navigation/index.cjs +1 -1
  71. package/dist/components/navigation/index.js +2 -2
  72. package/dist/components/off-canvas/browser.js +2 -2
  73. package/dist/components/off-canvas/index.cjs +1 -1
  74. package/dist/components/off-canvas/index.js +2 -2
  75. package/dist/components/resource-table/browser.js +7 -7
  76. package/dist/components/resource-table/index.cjs +1 -1
  77. package/dist/components/resource-table/index.js +7 -7
  78. package/dist/components/search/browser.js +7 -7
  79. package/dist/components/search/index.cjs +1 -1
  80. package/dist/components/search/index.js +7 -7
  81. package/dist/components/select/browser.js +4 -4
  82. package/dist/components/select/index.cjs +1 -1
  83. package/dist/components/select/index.js +4 -4
  84. package/dist/components/side-navigation/browser.js +4 -4
  85. package/dist/components/side-navigation/index.cjs +1 -1
  86. package/dist/components/side-navigation/index.js +4 -4
  87. package/dist/components/switch/browser.js +2 -2
  88. package/dist/components/switch/index.cjs +1 -1
  89. package/dist/components/switch/index.js +2 -2
  90. package/dist/components/tags/browser.js +3 -3
  91. package/dist/components/tags/index.cjs +1 -1
  92. package/dist/components/tags/index.js +3 -3
  93. package/dist/components/time-picker/browser.js +2 -2
  94. package/dist/components/time-picker/index.cjs +1 -1
  95. package/dist/components/time-picker/index.js +2 -2
  96. package/dist/components/tree/Tree.d.ts +7 -0
  97. package/dist/components/tree/Tree.d.ts.map +1 -1
  98. package/dist/components/tree/browser.js +43 -20
  99. package/dist/components/tree/index.cjs +1 -1
  100. package/dist/components/tree/index.js +43 -20
  101. package/dist/icons/redo.svg +3 -0
  102. package/dist/icons/save-alt.svg +3 -0
  103. package/dist/icons/undo.svg +3 -0
  104. package/dist/index.cjs +8 -8
  105. package/dist/index.css +1 -1
  106. package/dist/index.js +303 -258
  107. package/dist/navigation-search.json +21 -0
  108. package/dist/tree.css +1 -1
  109. package/package.json +1 -1
  110. package/dist/components/chunks/caret-down-DNQIsbnn.js +0 -1
  111. package/dist/components/chunks/caret-right-yjd5D9Z4.js +0 -1
  112. package/dist/components/chunks/caret-up-nVG8u8I-.js +0 -1
  113. package/dist/components/chunks/close-K4F7NTeK.js +0 -1
  114. package/dist/components/chunks/icon-B8DI92ZN.js +0 -1
  115. package/dist/components/chunks/icon-D_hrvdzy.js +0 -1
  116. package/dist/components/chunks/search-CWG3sU5p.js +0 -1
@@ -21,11 +21,11 @@ __mintLoadStyle('confirm-action', '../../confirm-action.css')
21
21
  __mintLoadStyle('modal', '../../modal.css')
22
22
  __mintLoadStyle('text', '../../text.css')
23
23
  __mintLoadStyle('tree', '../../tree.css')
24
- import "../chunks/icon-DZP-hLHj.js";
24
+ import "../chunks/icon-NUvffC5m.js";
25
25
  import { t as e } from "../chunks/register-element-BFYmIynH.js";
26
26
  import "../chunks/spinner-BsaFHuS6.js";
27
- import "../chunks/button-M7gi8RMZ.js";
28
- import "../chunks/confirm-action-D4U-R6zW.js";
27
+ import "../chunks/button-DPSkBQsf.js";
28
+ import "../chunks/confirm-action-THfH8TEH.js";
29
29
  import "../chunks/popover-CV-QCPjW.js";
30
30
  import "../chunks/sortable-CparUKN_.js";
31
31
  //#region src/components/tree/tree-utils.js
@@ -129,7 +129,7 @@ function f(e) {
129
129
  }
130
130
  var p = class extends HTMLElement {
131
131
  constructor(...e) {
132
- super(...e), this._items = [], this._selectedId = null, this._expandedIds = /* @__PURE__ */ new Set(), this._root = null, this._confirm = null, this._treeId = `mint-tree-${++l}`, this._liveRegion = null, this._dragExpansion = null, this._autoExpandedIds = /* @__PURE__ */ new Set(), this._dragHoverExpandedId = null, this._keyboardFocusRestore = null, this._onClick = (e) => {
132
+ super(...e), this._items = [], this._undoStack = [], this._redoStack = [], this._selectedId = null, this._expandedIds = /* @__PURE__ */ new Set(), this._root = null, this._confirm = null, this._treeId = `mint-tree-${++l}`, this._liveRegion = null, this._dragExpansion = null, this._autoExpandedIds = /* @__PURE__ */ new Set(), this._dragHoverExpandedId = null, this._keyboardFocusRestore = null, this._onClick = (e) => {
133
133
  let t = e.target instanceof Element ? e.target.closest("[data-tree-command]") : null;
134
134
  if (!t || !this.contains(t)) return;
135
135
  let r = t.getAttribute("data-tree-command"), i = t.getAttribute("data-tree-id");
@@ -145,6 +145,8 @@ var p = class extends HTMLElement {
145
145
  all: !0,
146
146
  expandedIds: []
147
147
  });
148
+ else if (r === "undo") this._undo();
149
+ else if (r === "redo") this._redo();
148
150
  else if (r === "toggle" && i) {
149
151
  let e = !this._expandedIds.has(i);
150
152
  this._setExpanded(i, e, !0);
@@ -282,7 +284,7 @@ var p = class extends HTMLElement {
282
284
  let r = t.itemId || "", i = f(t.oldParentId), a = f(t.newParentId), o = this._root?.querySelector(":scope > mint-sortable");
283
285
  if (!o) return;
284
286
  let s = n(this._items, r);
285
- this._items = this._readItemsFromDOM(o), this._collapseEmptyExpandedNodes();
287
+ this._recordHistory(), this._items = this._readItemsFromDOM(o), this._collapseEmptyExpandedNodes();
286
288
  let c = n(this._items, r);
287
289
  if (!c || !s) return;
288
290
  let l = {
@@ -378,7 +380,7 @@ var p = class extends HTMLElement {
378
380
  return t(this._items);
379
381
  }
380
382
  set items(e) {
381
- this._items = t(Array.isArray(e) ? e : []), this.hasAttribute("expanded-ids") || (this._expandedIds = this._defaultExpandedIds(this._items)), this.isConnected && this._render();
383
+ this._items = t(Array.isArray(e) ? e : []), this._undoStack = [], this._redoStack = [], this.hasAttribute("expanded-ids") || (this._expandedIds = this._defaultExpandedIds(this._items)), this.isConnected && this._render();
382
384
  }
383
385
  get selectedId() {
384
386
  return this._selectedId;
@@ -443,8 +445,12 @@ var p = class extends HTMLElement {
443
445
  i.className = "mint-tree-count", i.textContent = `${this._countItems()} ${this._countItems() === 1 ? "item" : "items"}`, r.appendChild(i);
444
446
  let a = document.createElement("div");
445
447
  a.className = "mint-tree-toolbar-actions";
446
- let o = this._createButton("Expand all", "ghost", "expand-all"), s = this._createButton("Collapse all", "ghost", "collapse-all");
447
- if (a.append(o, s), r.appendChild(a), n.appendChild(r), this._isLoading()) {
448
+ let o = this._createButton("", "ghost", "undo");
449
+ o.setAttribute("icon", "undo"), o.setAttribute("aria-label", "Undo last change"), o.setAttribute("title", "Undo"), (!this._undoStack.length || this._disabled()) && o.setAttribute("disabled", "true");
450
+ let s = this._createButton("", "ghost", "redo");
451
+ s.setAttribute("icon", "redo"), s.setAttribute("aria-label", "Redo last undone change"), s.setAttribute("title", "Redo"), (!this._redoStack.length || this._disabled()) && s.setAttribute("disabled", "true");
452
+ let c = this._createButton("Expand all", "ghost", "expand-all"), l = this._createButton("Collapse all", "ghost", "collapse-all");
453
+ if (a.append(o, s, c, l), r.appendChild(a), n.appendChild(r), this._isLoading()) {
448
454
  let e = document.createElement("div");
449
455
  e.className = "mint-tree-loading", e.setAttribute("aria-label", "Loading items");
450
456
  let t = document.createElement("mint-spinner");
@@ -455,17 +461,17 @@ var p = class extends HTMLElement {
455
461
  e.className = "mint-tree-empty", e.textContent = this.getAttribute("empty-label") || "No items yet.", n.appendChild(e);
456
462
  }
457
463
  this._liveRegion = document.createElement("div"), this._liveRegion.className = "mint-tree-live-region", this._liveRegion.setAttribute("role", "status"), this._liveRegion.setAttribute("aria-live", "polite"), this._liveRegion.setAttribute("aria-atomic", "true"), n.appendChild(this._liveRegion);
458
- let c = document.createElement("mint-confirm-action");
459
- c.className = "mint-tree-confirm", this._confirm = c, n.appendChild(c), this.replaceChildren(n), this._root = n, this.classList.add("mint-tree"), this._disabled() ? this.setAttribute("aria-disabled", "true") : this.removeAttribute("aria-disabled");
460
- let l = this._keyboardFocusRestore;
461
- l && (this._focusRow(l.id), window.scrollTo({
462
- left: l.x,
463
- top: l.y,
464
+ let u = document.createElement("mint-confirm-action");
465
+ u.className = "mint-tree-confirm", this._confirm = u, n.appendChild(u), this.replaceChildren(n), this._root = n, this.classList.add("mint-tree"), this._disabled() ? this.setAttribute("aria-disabled", "true") : this.removeAttribute("aria-disabled");
466
+ let d = this._keyboardFocusRestore;
467
+ d && (this._focusRow(d.id), window.scrollTo({
468
+ left: d.x,
469
+ top: d.y,
464
470
  behavior: "instant"
465
471
  }), requestAnimationFrame(() => {
466
- this._keyboardFocusRestore === l && (this._focusRow(l.id), window.scrollTo({
467
- left: l.x,
468
- top: l.y,
472
+ this._keyboardFocusRestore === d && (this._focusRow(d.id), window.scrollTo({
473
+ left: d.x,
474
+ top: d.y,
469
475
  behavior: "instant"
470
476
  }));
471
477
  }));
@@ -583,7 +589,7 @@ var p = class extends HTMLElement {
583
589
  } catch {}
584
590
  }
585
591
  _applyMove(e) {
586
- this._items = e.items, this._collapseEmptyExpandedNodes(), e.newParentId && this._expandedIds.add(e.newParentId), this._render(), this._focusRow(String(e.item.id));
592
+ this._recordHistory(), this._items = e.items, this._collapseEmptyExpandedNodes(), e.newParentId && this._expandedIds.add(e.newParentId), this._render(), this._focusRow(String(e.item.id));
587
593
  let t = {
588
594
  itemId: String(e.item.id),
589
595
  item: { ...e.item },
@@ -687,6 +693,23 @@ var p = class extends HTMLElement {
687
693
  _announce(e) {
688
694
  this._liveRegion && (this._liveRegion.textContent = e);
689
695
  }
696
+ _recordHistory() {
697
+ this._undoStack.push(t(this._items)), this._undoStack.length > 50 && this._undoStack.shift(), this._redoStack = [];
698
+ }
699
+ _undo() {
700
+ let e = this._undoStack.pop();
701
+ e && (this._redoStack.push(t(this._items)), this._items = e, this._render(), u(this, "mint-tree-change", {
702
+ items: this.items,
703
+ itemId: this._selectedId
704
+ }), u(this, "mint-tree-undo", { items: this.items }));
705
+ }
706
+ _redo() {
707
+ let e = this._redoStack.pop();
708
+ e && (this._undoStack.push(t(this._items)), this._items = e, this._render(), u(this, "mint-tree-change", {
709
+ items: this.items,
710
+ itemId: this._selectedId
711
+ }), u(this, "mint-tree-redo", { items: this.items }));
712
+ }
690
713
  _keyboardIndent(e) {
691
714
  let t = r(this._items, e);
692
715
  if (!t || t.siblings.indexOf(t.item) <= 0) return;
@@ -696,7 +719,7 @@ var p = class extends HTMLElement {
696
719
  maxDepth: this._maxDepth(),
697
720
  allowNesting: this._allowNesting()
698
721
  });
699
- this._items = t.items, this._expandedIds.add(String(n.id)), this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
722
+ this._recordHistory(), this._items = t.items, this._expandedIds.add(String(n.id)), this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
700
723
  itemId: e,
701
724
  item: { ...t.item },
702
725
  items: this.items,
@@ -718,7 +741,7 @@ var p = class extends HTMLElement {
718
741
  maxDepth: this._maxDepth(),
719
742
  allowNesting: this._allowNesting()
720
743
  });
721
- this._items = t.items, this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
744
+ this._recordHistory(), this._items = t.items, this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
722
745
  itemId: e,
723
746
  item: { ...t.item },
724
747
  items: this.items,
@@ -1 +1 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}}),require(`../chunks/icon-lfkgHbsk.js`);const e=require(`../chunks/register-element-tCL_F1YI.js`);require(`../chunks/spinner-DbTohC6i.js`),require(`../chunks/button-DGuqIeFS.js`),require(`../chunks/confirm-action-DxvkO8OY.js`),require(`../chunks/popover-Db9ZGHd9.js`),require(`../chunks/sortable-DX2PwFLN.js`);function t(e=[]){s(e);let t=e=>e.map(e=>({...e,children:Array.isArray(e.children)?t(e.children):[]}));return t(e)}function n(e,t){let r=String(t);for(let t of e){if(String(t.id)===r)return t;let e=n(t.children||[],r);if(e)return e}return null}function r(e,t,n=null){let i=String(t);for(let t of e){if(String(t.id)===i)return{parentId:n,siblings:e,item:t};let a=r(t.children||[],i,String(t.id));if(a)return a}return null}function i(e,t,n=0){let r=String(t);for(let t of e){if(String(t.id)===r)return n;let e=i(t.children||[],r,n+1);if(e>=0)return e}return-1}function a(e){let t=Array.isArray(e?.children)?e.children:[];return t.length?1+Math.max(...t.map(a)):1}function o(e,t){return String(e.id)===String(t)?!0:(e.children||[]).some(e=>o(e,t))}function s(e){let t=new Set,n=e=>{for(let r of e){if(r.id===null||r.id===void 0||String(r.id)===``)throw Error(`Every tree item must have a non-empty identifier.`);let e=String(r.id);if(t.has(e))throw Error(`Tree item identifiers must be unique. Duplicate identifier: ${e}.`);t.add(e),n(r.children||[])}};n(e)}function c(e,s,c,l,u={}){let d=t(e),f=r(d,s);if(!f)throw Error(`Tree item ${s} was not found.`);if(f.item.disabled||f.item.draggable===!1)throw Error(`Disabled tree items cannot be moved.`);let p=c==null||c===``?null:n(d,c);if(c!=null&&c!==``&&!p)throw Error(`Target parent ${c} was not found.`);if(p&&o(f.item,p.id))throw Error(`A tree item cannot be moved inside itself or one of its descendants.`);if(p?.disabled)throw Error(`Disabled tree items cannot contain children.`);if(u.allowNesting===!1&&p)throw Error(`Nesting is disabled for this tree.`);if(p?.canHaveChildren===!1)throw Error(`The target item cannot contain children.`);if(p?.acceptsTypes?.length&&!p.acceptsTypes.includes(f.item.type))throw Error(`The target item does not accept this item type.`);let m=p?p.children:d,h=f.siblings.findIndex(e=>String(e.id)===String(s)),[g]=f.siblings.splice(h,1),_=Math.min(Math.max(0,Number.isFinite(l)?Math.trunc(l):m.length),m.length);m.splice(_,0,g);let v=Number.isFinite(u.maxDepth)?Math.max(1,Math.trunc(u.maxDepth)):1/0;if((p?i(d,p.id)+1:0)+a(g)>v)throw Error(`Moving this branch would exceed the maximum tree depth of ${v}.`);return{items:d,item:g,oldParentId:f.parentId,newParentId:p?String(p.id):null,oldIndex:h,newIndex:_}}var l=0;function u(e,t,n){e.dispatchEvent(new CustomEvent(t,{bubbles:!0,composed:!0,detail:n}))}function d(e,t){let n=e.getAttribute(t);return n===``||n===`true`}function f(e){return e==null||e===``?null:String(e)}var p=class extends HTMLElement{constructor(...e){super(...e),this._items=[],this._selectedId=null,this._expandedIds=new Set,this._root=null,this._confirm=null,this._treeId=`mint-tree-${++l}`,this._liveRegion=null,this._dragExpansion=null,this._autoExpandedIds=new Set,this._dragHoverExpandedId=null,this._keyboardFocusRestore=null,this._onClick=e=>{let t=e.target instanceof Element?e.target.closest(`[data-tree-command]`):null;if(!t||!this.contains(t))return;let r=t.getAttribute(`data-tree-command`),i=t.getAttribute(`data-tree-id`);if(r===`create-root`)u(this,`mint-tree-create`,{parentId:null,items:this.items});else if(r===`expand-all`)this._expandedIds=this._defaultExpandedIds(this._items),this._syncExpandedDOM(),u(this,`mint-tree-expand`,{all:!0,expandedIds:this.expandedIds});else if(r===`collapse-all`)this._expandedIds.clear(),this._syncExpandedDOM(),u(this,`mint-tree-collapse`,{all:!0,expandedIds:[]});else if(r===`toggle`&&i){let e=!this._expandedIds.has(i);this._setExpanded(i,e,!0)}else if(r===`select`&&i)this._select(i);else if(r===`create-child`&&i)this._closeItemPopover(t),u(this,`mint-tree-create`,{parentId:i,items:this.items});else if(r===`edit`&&i){this._closeItemPopover(t);let e=n(this._items,i);e&&u(this,`mint-tree-edit`,{item:{...e},itemId:i,items:this.items})}else if(r===`delete`&&i)this._closeItemPopover(t),this._confirmDelete(i);else if([`move-up`,`move-down`,`nest-previous`,`outdent`].includes(r||``)&&i)this._closeItemPopover(t),this._moveByCommand(i,r||``);else if(r===`custom`&&i){this._closeItemPopover(t);let e=n(this._items,i),r=t.getAttribute(`data-tree-action`)||``,a=e?.actions?.find(e=>e.id===r);if(e&&a){let t={item:{...e},itemId:i,action:{...a,onSelect:void 0},items:this.items};u(this,`mint-tree-action`,t),a.onSelect?.({item:{...e},itemId:i,action:a,items:this.items})}}},this._onSortableDrag=e=>{let t=e.detail;if(!t||!Number.isFinite(t.clientX)||!Number.isFinite(t.clientY))return;let r=document.elementFromPoint(t.clientX,t.clientY),i=r?.closest(`[data-mint-tree-row]`),a=t.item?.getAttribute(`data-tree-id`)||``,o=null;if(i&&i.getAttribute(`data-tree-id`)!==a){let e=i.querySelector(`:scope > .mint-tree-row`)?.getBoundingClientRect()||i.getBoundingClientRect(),r=e.height?(t.clientY-e.top)/e.height:0;if(r>.22&&r<.78){let e=i.getAttribute(`data-tree-id`)||``;n(this._items,e)&&this._isValidDrop(a,e)&&(o=e)}}if(!o&&this._dragHoverExpandedId){let e=[...this._root?.querySelectorAll(`[data-tree-id]`)??[]].find(e=>e.getAttribute(`data-tree-id`)===this._dragHoverExpandedId)?.querySelector(`:scope > mint-sortable.mint-tree-children`);r&&e?.contains(r)&&(o=this._dragHoverExpandedId)}if(this._dragHoverExpandedId&&this._dragHoverExpandedId!==o){let e=this._dragHoverExpandedId;this._dragHoverExpandedId=null,this._autoExpandedIds.delete(e),this._expandedIds.delete(e),this._syncNodeExpanded(e);let t=n(this._items,e);t&&u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0})}if(o){let e=n(this._items,o);e&&!this._expandedIds.has(o)&&(this._expandedIds.add(o),this._autoExpandedIds.add(o),this._dragHoverExpandedId=o,this._syncNodeExpanded(o),u(this,`mint-tree-expand`,{itemId:o,item:{...e},expandedIds:this.expandedIds,automatic:!0}))}},this._onSortableStart=()=>{this.classList.add(`mint-tree-drag-active`),this._dragHoverExpandedId=null,this._dragExpansion?.timer!==null&&this._dragExpansion?.timer!==void 0&&window.clearTimeout(this._dragExpansion.timer),this._dragExpansion=null;let e=this._root?.querySelector(`.mint-sortable-dragging[data-tree-id]`),t=e?.getAttribute(`data-tree-id`)||``,n=e?.querySelector(`:scope > mint-sortable.mint-tree-children`);if(!e||!t||!n||n.hidden||!this._expandedIds.has(t))return;let r=e.querySelector(`:scope > .mint-tree-row`),i=this._root?.querySelector(`.mint-sortable-placeholder`),a=n.getBoundingClientRect().height;n.style.height=`${a}px`,n.style.overflow=`hidden`,e.style.height=`auto`,e.style.overflow=`hidden`,this._dragExpansion={itemId:t,item:e,childList:n,timer:null},this._setTransientExpanded(t,!1),n.offsetHeight,requestAnimationFrame(()=>{this._dragExpansion?.childList===n&&(n.classList.add(`mint-tree-drag-collapse`),n.style.height=`0px`,i&&r&&(i.style.minHeight=`${r.getBoundingClientRect().height}px`))})},this._onSortableEnd=e=>{this.classList.remove(`mint-tree-drag-active`);let t=e.detail,r=t&&!t.cancelled?f(t.newParentId):null;for(let e of this._autoExpandedIds){if(e===r)continue;this._expandedIds.delete(e),this._syncNodeExpanded(e);let t=n(this._items,e);t&&u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0})}this._autoExpandedIds.clear(),this._dragHoverExpandedId=null;let i=this._dragExpansion;if(!i)return;i.item=[...this._root?.querySelectorAll(`[data-tree-id]`)??[]].find(e=>e.getAttribute(`data-tree-id`)===i.itemId)||i.item,i.childList=i.item.querySelector(`:scope > mint-sortable.mint-tree-children`)||i.childList;let{item:a,childList:o,itemId:s}=i;o.style.height=`0px`,o.style.overflow=`hidden`,o.classList.remove(`mint-tree-drag-collapse`),this._setTransientExpanded(s,!0),o.offsetHeight;let c=o.scrollHeight;requestAnimationFrame(()=>{this._dragExpansion===i&&(o.style.height=`${c}px`)}),i.timer=window.setTimeout(()=>{o.style.height=``,o.style.overflow=``,o.style.opacity=``,this._dragExpansion=null},155)},this._onSortableDragOver=e=>{let t=e,{item:r,target:i}=t.detail||{};if(!r||!i)return;this._root?.querySelectorAll(`.mint-tree-drop-invalid`).forEach(e=>e.classList.remove(`mint-tree-drop-invalid`));let a=this._root?.querySelector(`.mint-sortable-placeholder`),o=r.getAttribute(`data-tree-id`)||``,s=f(i.getAttribute(`data-tree-parent-id`)),c=i===t.detail.source||this._isValidDrop(o,s),l=s?n(this._items,s):null;c&&i!==t.detail.source&&l?a?.setAttribute(`data-drop-label`,`Drop here to nest under ${l.label}`):c&&i===this._root?.querySelector(`:scope > mint-sortable`)?a?.setAttribute(`data-drop-label`,`Drop here to move to the top level`):a?.removeAttribute(`data-drop-label`);let u=(Number.isFinite(t.detail.clientX)&&Number.isFinite(t.detail.clientY)?document.elementFromPoint(t.detail.clientX,t.detail.clientY):null)?.closest(`[data-mint-tree-row]`);u&&u.getAttribute(`data-tree-id`)!==o&&!c&&u.classList.add(`mint-tree-drop-invalid`),c||t.preventDefault()},this._onSortableBeforeDrop=e=>{let t=e,n=t.detail;if(!n?.item||!n.target)return;let r=n.item.getAttribute(`data-tree-id`)||``,i=f(n.target.getAttribute(`data-tree-parent-id`));n.target!==n.source&&!this._isValidDrop(r,i)&&t.preventDefault()},this._onSortableChange=e=>{let t=e.detail;if(!t||t.cancelled)return;let r=t.itemId||``,i=f(t.oldParentId),a=f(t.newParentId),o=this._root?.querySelector(`:scope > mint-sortable`);if(!o)return;let s=n(this._items,r);this._items=this._readItemsFromDOM(o),this._collapseEmptyExpandedNodes();let c=n(this._items,r);if(!c||!s)return;let l={itemId:r,item:{...c},items:this.items,oldParentId:i,newParentId:a,oldIndex:Number(t.oldIndex??-1),newIndex:Number(t.newIndex??-1)};this._clearDropState(),u(this,`mint-tree-change`,{items:l.items,itemId:r}),i===a?u(this,`mint-tree-reorder`,l):u(this,`mint-tree-reparent`,l);let d=`${c.label} moved${a?` into ${n(this._items,a)?.label||`a group`}`:` to the top level`}.`;this._dragExpansion?.itemId===r?window.setTimeout(()=>{this.isConnected&&(this._render(),this._focusRow(r),this._announce(d))},155):(this._render(),this._focusRow(r),this._announce(d))},this._onKeydown=e=>{let t=e.target instanceof Element?e.target.closest(`[data-mint-tree-row]`):null;if(!t)return;if(e.altKey&&[`ArrowUp`,`ArrowDown`].includes(e.key)){e.preventDefault();let n=t.getAttribute(`data-tree-id`)||``;if(n){this._keyboardFocusRestore&&window.clearTimeout(this._keyboardFocusRestore.timer);let e={id:n,x:window.scrollX,y:window.scrollY,timer:0};e.timer=window.setTimeout(()=>{this._keyboardFocusRestore===e&&(this._keyboardFocusRestore=null)},2e3),this._keyboardFocusRestore=e}return}let i=t.getAttribute(`data-tree-id`)||``;if(e.altKey&&e.key===`ArrowRight`){e.preventDefault(),this._keyboardIndent(i);return}if(e.altKey&&e.key===`ArrowLeft`){e.preventDefault(),this._keyboardOutdent(i);return}let a=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].filter(e=>!e.closest(`mint-sortable[hidden]`)),o=a.indexOf(t);if(e.key===`ArrowDown`)e.preventDefault(),a[o+1]?.focus({preventScroll:!0});else if(e.key===`ArrowUp`)e.preventDefault(),a[o-1]?.focus({preventScroll:!0});else if(e.key===`ArrowRight`)e.preventDefault(),this._expandedIds.has(i)?a[o+1]?.focus({preventScroll:!0}):this._setExpanded(i,!0,!0);else if(e.key===`ArrowLeft`)if(e.preventDefault(),this._expandedIds.has(i))this._setExpanded(i,!1,!0);else{let e=r(this._items,i)?.parentId;e&&[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e)?.focus({preventScroll:!0})}else if(e.key===`Enter`||e.key===` `||e.key===`Spacebar`){let r=e.target instanceof Element?e.target.closest(`[data-tree-command="select"]`):null;(t===e.target||r)&&(e.preventDefault(),n(this._items,i)?.children?.length?this._setExpanded(i,!this._expandedIds.has(i),!0):this._select(i))}}}static get observedAttributes(){return[`selected-id`,`max-depth`,`allow-nesting`,`disabled`,`loading`,`create-label`,`empty-label`,`delete-confirmation`,`expanded-ids`]}connectedCallback(){this._root||(this._selectedId=f(this.getAttribute(`selected-id`)),this._expandedIds=this.hasAttribute(`expanded-ids`)?this._readExpandedIds(this.getAttribute(`expanded-ids`)):this._defaultExpandedIds(this._items),this.addEventListener(`click`,this._onClick),this.addEventListener(`keydown`,this._onKeydown,!0),this.addEventListener(`mint-sortable-start`,this._onSortableStart),this.addEventListener(`mint-sortable-end`,this._onSortableEnd),this.addEventListener(`mint-sortable-drag`,this._onSortableDrag),this.addEventListener(`mint-sortable-drag-over`,this._onSortableDragOver),this.addEventListener(`mint-sortable-before-drop`,this._onSortableBeforeDrop),this.addEventListener(`mint-sortable-change`,this._onSortableChange),this._render())}disconnectedCallback(){this._dragExpansion?.timer!==null&&this._dragExpansion?.timer!==void 0&&window.clearTimeout(this._dragExpansion.timer),this._keyboardFocusRestore&&window.clearTimeout(this._keyboardFocusRestore.timer),this._keyboardFocusRestore=null,this._dragExpansion=null,this._autoExpandedIds.clear(),this._dragHoverExpandedId=null,this.classList.remove(`mint-tree-drag-active`),this.removeEventListener(`click`,this._onClick),this.removeEventListener(`keydown`,this._onKeydown,!0),this.removeEventListener(`mint-sortable-start`,this._onSortableStart),this.removeEventListener(`mint-sortable-end`,this._onSortableEnd),this.removeEventListener(`mint-sortable-drag`,this._onSortableDrag),this.removeEventListener(`mint-sortable-drag-over`,this._onSortableDragOver),this.removeEventListener(`mint-sortable-before-drop`,this._onSortableBeforeDrop),this.removeEventListener(`mint-sortable-change`,this._onSortableChange),this._root=null}attributeChangedCallback(e,t,n){if(!(t===n||!this.isConnected)){if(e===`selected-id`){this._selectedId=f(n),this._syncSelectionDOM();return}e===`expanded-ids`&&(this._expandedIds=this._readExpandedIds(n)),this._render()}}get items(){return t(this._items)}set items(e){this._items=t(Array.isArray(e)?e:[]),this.hasAttribute(`expanded-ids`)||(this._expandedIds=this._defaultExpandedIds(this._items)),this.isConnected&&this._render()}get selectedId(){return this._selectedId}set selectedId(e){this._selectedId=f(e),this._selectedId===null?this.removeAttribute(`selected-id`):this.setAttribute(`selected-id`,this._selectedId)}get expandedIds(){return[...this._expandedIds]}set expandedIds(e){this._expandedIds=new Set((Array.isArray(e)?e:[]).map(String)),this.isConnected&&this._render()}save(){u(this,`mint-tree-save`,{items:this.items})}select(e){this._select(String(e))}expand(e){this._setExpanded(String(e),!0,!0)}collapse(e){this._setExpanded(String(e),!1,!0)}_readExpandedIds(e){if(!e)return this._defaultExpandedIds(this._items);try{let t=JSON.parse(e);return new Set(Array.isArray(t)?t.map(String):[])}catch{return new Set(e.split(`,`).map(e=>e.trim()).filter(Boolean))}}_defaultExpandedIds(e){let t=new Set,n=e=>e.forEach(e=>{e.children?.length&&(t.add(String(e.id)),n(e.children))});return n(e),t}_maxDepth(){let e=Number.parseInt(this.getAttribute(`max-depth`)||``,10);return Number.isFinite(e)&&e>0?e:1/0}_allowNesting(){return!this.hasAttribute(`allow-nesting`)||d(this,`allow-nesting`)}_disabled(){return d(this,`disabled`)}_isLoading(){return d(this,`loading`)}_render(){let e=document.activeElement instanceof HTMLElement?document.activeElement.closest(`[data-mint-tree-row]`):null,t=e&&this.contains(e)?e.getAttribute(`data-tree-id`):null;t&&this._keyboardFocusRestore&&(this._keyboardFocusRestore.id=t);let n=document.createElement(`div`);n.className=`mint-tree-shell`,n.setAttribute(`role`,`tree`),n.setAttribute(`aria-label`,this.getAttribute(`aria-label`)||`Hierarchical items`),n.setAttribute(`aria-busy`,String(this._isLoading()));let r=document.createElement(`div`);r.className=`mint-tree-toolbar`;let i=document.createElement(`span`);i.className=`mint-tree-count`,i.textContent=`${this._countItems()} ${this._countItems()===1?`item`:`items`}`,r.appendChild(i);let a=document.createElement(`div`);a.className=`mint-tree-toolbar-actions`;let o=this._createButton(`Expand all`,`ghost`,`expand-all`),s=this._createButton(`Collapse all`,`ghost`,`collapse-all`);if(a.append(o,s),r.appendChild(a),n.appendChild(r),this._isLoading()){let e=document.createElement(`div`);e.className=`mint-tree-loading`,e.setAttribute(`aria-label`,`Loading items`);let t=document.createElement(`mint-spinner`);t.setAttribute(`size`,`small`),e.appendChild(t),n.appendChild(e)}else if(this._items.length)n.appendChild(this._createList(this._items,null,0));else{let e=document.createElement(`div`);e.className=`mint-tree-empty`,e.textContent=this.getAttribute(`empty-label`)||`No items yet.`,n.appendChild(e)}this._liveRegion=document.createElement(`div`),this._liveRegion.className=`mint-tree-live-region`,this._liveRegion.setAttribute(`role`,`status`),this._liveRegion.setAttribute(`aria-live`,`polite`),this._liveRegion.setAttribute(`aria-atomic`,`true`),n.appendChild(this._liveRegion);let c=document.createElement(`mint-confirm-action`);c.className=`mint-tree-confirm`,this._confirm=c,n.appendChild(c),this.replaceChildren(n),this._root=n,this.classList.add(`mint-tree`),this._disabled()?this.setAttribute(`aria-disabled`,`true`):this.removeAttribute(`aria-disabled`);let l=this._keyboardFocusRestore;l&&(this._focusRow(l.id),window.scrollTo({left:l.x,top:l.y,behavior:`instant`}),requestAnimationFrame(()=>{this._keyboardFocusRestore===l&&(this._focusRow(l.id),window.scrollTo({left:l.x,top:l.y,behavior:`instant`}))}))}_countItems(e=this._items){return e.reduce((e,t)=>e+1+this._countItems(t.children||[]),0)}_createButton(e,t,n){let r=document.createElement(`mint-button`);return r.textContent=e,r.setAttribute(`variant`,t),r.setAttribute(`data-tree-command`,n),r}_createList(e,t,n){let r=document.createElement(`mint-sortable`);r.id=`${this._treeId}-list-${t??`root`}`,r.setAttribute(`group`,this._treeId),r.setAttribute(`data-tree-parent-id`,t??``),r.setAttribute(`role`,`group`),r.setAttribute(`data-tree-depth`,String(n)),r.setAttribute(`direction`,`vertical`),r.setAttribute(`gap`,`1`),r.setAttribute(`handle-selector`,`[data-mint-sortable-handle]`),r.setAttribute(`animation-duration`,`320`),r.setAttribute(`item-key-attribute`,`data-tree-id`);let i=document.createElement(`mint-stack`);i.setAttribute(`direction`,`vertical`),i.setAttribute(`gap`,`1`);for(let t of e)i.appendChild(this._createNode(t,n));return r.appendChild(i),r}_createNode(e,t){let n=String(e.id),r=Array.isArray(e.children)?e.children:[],i=e.canHaveChildren!==!1&&this._allowNesting()&&t+a(e)<this._maxDepth(),o=document.createElement(`div`);o.className=`mint-tree-node`,o.setAttribute(`data-tree-id`,n),o.setAttribute(`role`,`treeitem`),o.setAttribute(`data-mint-tree-row`,``),o.setAttribute(`aria-level`,String(t+1)),o.setAttribute(`aria-selected`,String(this._selectedId===n)),o.setAttribute(`tabindex`,this._selectedId===n||this._selectedId===null&&t===0&&this._items[0]?.id===e.id?`0`:`-1`),o.setAttribute(`aria-label`,e.label),r.length&&o.setAttribute(`aria-expanded`,String(this._expandedIds.has(n))),(e.disabled||this._disabled())&&o.setAttribute(`aria-disabled`,`true`),(e.draggable===!1||this._disabled())&&o.setAttribute(`data-sortable-disabled`,``);let s=document.createElement(`div`);s.className=`mint-tree-row`,s.setAttribute(`data-tree-id`,n);let c=document.createElement(`span`);c.className=`mint-tree-drag-handle`,c.setAttribute(`data-mint-sortable-handle`,``),c.setAttribute(`role`,`button`),c.setAttribute(`tabindex`,`-1`),c.setAttribute(`aria-label`,`Drag ${e.label} to move`);let l=document.createElement(`mint-icon`);if(l.setAttribute(`name`,`grip-dots`),l.setAttribute(`aria-hidden`,`true`),c.appendChild(l),s.appendChild(c),r.length){let t=document.createElement(`mint-button`);t.className=`mint-tree-expand`,t.setAttribute(`variant`,`ghost`),t.setAttribute(`icon`,this._expandedIds.has(n)?`caret-up`:`caret-down`),t.setAttribute(`data-tree-command`,`toggle`),t.setAttribute(`data-tree-id`,n),t.setAttribute(`aria-label`,`${this._expandedIds.has(n)?`Collapse`:`Expand`} ${e.label}`),t.setAttribute(`aria-expanded`,String(this._expandedIds.has(n))),(this._disabled()||e.disabled===!0)&&t.setAttribute(`disabled`,`true`),s.appendChild(t)}else{let e=document.createElement(`span`);e.className=`mint-tree-expand-spacer`,e.setAttribute(`aria-hidden`,`true`),s.appendChild(e)}let u=document.createElement(`span`);if(u.className=`mint-tree-label`,u.setAttribute(`data-tree-command`,`select`),u.setAttribute(`data-tree-id`,n),i&&(u.setAttribute(`data-mint-sortable-drop-container`,`${this._treeId}-list-${n}`),u.setAttribute(`data-mint-sortable-drop-end`,``)),e.icon){let t=document.createElement(`mint-icon`);t.setAttribute(`name`,String(e.icon)),t.setAttribute(`aria-hidden`,`true`),u.appendChild(t)}let d=document.createElement(`span`);if(d.className=`mint-tree-label-text`,d.textContent=e.label,u.appendChild(d),s.appendChild(u),e.metadata){let t=document.createElement(`span`);t.className=`mint-tree-metadata`,t.textContent=String(e.metadata),s.appendChild(t)}let f=document.createElement(`span`);f.className=`mint-tree-nest-target`,f.setAttribute(`data-mint-sortable-drop-container`,`${this._treeId}-list-${n}`),f.setAttribute(`data-mint-sortable-drop-end`,``),f.setAttribute(`aria-hidden`,`true`);let p=document.createElement(`mint-icon`);p.setAttribute(`name`,`arrow-right`),p.setAttribute(`aria-hidden`,`true`),f.appendChild(p),i&&s.appendChild(f);let m=this._createActions(e,i);s.appendChild(m),o.appendChild(s);let h=this._createList(r,n,t+1);return h.classList.add(`mint-tree-children`),h.hidden=!this._expandedIds.has(n),o.appendChild(h),o}_createActions(e,t){let i=document.createElement(`div`);i.className=`mint-tree-actions`;let a=`${this._treeId}-actions-trigger-${encodeURIComponent(String(e.id))}`,o=`${a}-popover`,s=document.createElement(`mint-button`);s.id=a,s.setAttribute(`variant`,`ghost`),s.setAttribute(`icon`,`ellipsis`),s.setAttribute(`aria-label`,`Actions for ${e.label}`),s.setAttribute(`aria-haspopup`,`menu`),i.appendChild(s);let c=document.createElement(`mint-popover`);c.id=o,c.setAttribute(`trigger-id`,a),c.setAttribute(`direction`,`down`),c.setAttribute(`trigger-align`,`end`),c.setAttribute(`padding`,`0.3rem`);let l=document.createElement(`div`);l.className=`mint-tree-menu`,l.setAttribute(`role`,`menu`);let u=(t,n,r=!1)=>{let i=document.createElement(`mint-button`);i.textContent=n,i.setAttribute(`variant`,`ghost`),i.setAttribute(`full-width`,`true`),i.setAttribute(`align`,`left`),i.setAttribute(`role`,`menuitem`),i.setAttribute(`data-tree-command`,t),i.setAttribute(`data-tree-id`,String(e.id)),(r||e.disabled===!0||this._disabled())&&i.setAttribute(`disabled`,`true`),l.appendChild(i)},d=Array.isArray(e.actions)?e.actions:null;if(d)for(let t of d){let n=document.createElement(`mint-button`);n.textContent=t.label,n.setAttribute(`variant`,`ghost`),n.setAttribute(`full-width`,`true`),n.setAttribute(`align`,`left`),n.setAttribute(`role`,`menuitem`),n.setAttribute(`data-tree-command`,`custom`),n.setAttribute(`data-tree-id`,String(e.id)),n.setAttribute(`data-tree-action`,t.id),(t.disabled===!0||e.disabled===!0||this._disabled())&&n.setAttribute(`disabled`,`true`),t.icon&&n.setAttribute(`icon`,t.icon),t.tone&&n.setAttribute(`tone`,t.tone),l.appendChild(n)}else{let i=r(this._items,e.id),a=i?.siblings||[],o=a.findIndex(t=>String(t.id)===String(e.id)),s=o>0?a[o-1]:null,c=i?.parentId?n(this._items,i.parentId):null;u(`move-up`,`Move up`,o<=0),u(`move-down`,`Move down`,o<0||o>=a.length-1),u(`nest-previous`,`Nest under previous item`,!s||!this._isValidDrop(String(e.id),String(s.id))),u(`outdent`,`Move to parent level`,!c),u(`create-child`,`Add child`,!t),u(`edit`,`Edit`),u(`delete`,`Delete`)}return c.appendChild(l),i.appendChild(c),i}_closeItemPopover(e){(e.closest(`.mint-tree-menu`)?.closest(`mint-popover`))?.close?.()}async _confirmDelete(e){let t=n(this._items,e);!t||!this._confirm?.showConfirm||await this._confirm.showConfirm({question:this.getAttribute(`delete-confirmation`)||`Delete “${t.label}”?`,tone:`danger`,confirmLabel:`Delete`,cancelLabel:`Cancel`})&&u(this,`mint-tree-delete`,{item:{...t},itemId:e,items:this.items})}_moveByCommand(e,t){let n=r(this._items,e);if(!n)return;let i=n.siblings.findIndex(t=>String(t.id)===e),a=n.parentId,o=i;if(t===`move-up`)o=i-1;else if(t===`move-down`)o=i+1;else if(t===`nest-previous`){let e=n.siblings[i-1];if(!e)return;a=String(e.id),o=(e.children||[]).length}else if(t===`outdent`){if(n.parentId===null)return;let e=r(this._items,n.parentId);if(!e)return;a=e.parentId,o=e.siblings.findIndex(e=>String(e.id)===n.parentId)+1}else return;try{let t=c(this._items,e,a,o,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._applyMove(t)}catch{}}_applyMove(e){this._items=e.items,this._collapseEmptyExpandedNodes(),e.newParentId&&this._expandedIds.add(e.newParentId),this._render(),this._focusRow(String(e.item.id));let t={itemId:String(e.item.id),item:{...e.item},items:this.items,oldParentId:e.oldParentId,newParentId:e.newParentId,oldIndex:e.oldIndex,newIndex:e.newIndex};u(this,`mint-tree-change`,{items:t.items,itemId:t.itemId}),u(this,t.oldParentId===t.newParentId?`mint-tree-reorder`:`mint-tree-reparent`,t),this._announce(`${t.item.label} moved${t.newParentId?` into ${n(this._items,t.newParentId)?.label||`a group`}`:` to the top level`}.`)}_select(e){let t=n(this._items,e);!t||t.disabled||this._disabled()||(this._selectedId=e,this.setAttribute(`selected-id`,e),this._syncSelectionDOM(),u(this,`mint-tree-select`,{item:{...t},itemId:e,items:this.items}))}_syncSelectionDOM(){(this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]).forEach(e=>{let t=e.getAttribute(`data-tree-id`)===this._selectedId;e.setAttribute(`aria-selected`,String(t)),e.tabIndex=t?0:-1})}_setExpanded(e,t,r){let i=n(this._items,e);!i||i.disabled||this._disabled()||(t?this._expandedIds.add(e):this._expandedIds.delete(e),this._syncNodeExpanded(e),r&&u(this,t?`mint-tree-expand`:`mint-tree-collapse`,{itemId:e,item:{...i},expandedIds:this.expandedIds}))}_syncExpandedDOM(){this._root?.querySelectorAll(`[data-mint-tree-row]`).forEach(e=>this._syncNodeExpanded(e.getAttribute(`data-tree-id`)||``))}_syncNodeExpanded(e){let t=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e),r=t?.querySelector(`:scope > mint-sortable.mint-tree-children`);if(!t||!r)return;let i=this._expandedIds.has(e);t.setAttribute(`aria-expanded`,String(i));let a=t.querySelector(`[data-tree-command="toggle"]`);a?.setAttribute(`aria-expanded`,String(i)),a?.setAttribute(`aria-label`,`${i?`Collapse`:`Expand`} ${n(this._items,e)?.label||`item`}`),a?.setAttribute(`icon`,i?`caret-up`:`caret-down`),r.hidden=!i}_setTransientExpanded(e,t){let r=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e);if(!r)return;r.setAttribute(`aria-expanded`,String(t));let i=r.querySelector(`[data-tree-command="toggle"]`),a=n(this._items,e)?.label||`item`;i?.setAttribute(`aria-expanded`,String(t)),i?.setAttribute(`aria-label`,`${t?`Collapse`:`Expand`} ${a}`),i?.setAttribute(`icon`,t?`caret-up`:`caret-down`)}_isValidDrop(e,t){let r=n(this._items,e);if(!r)return!1;let i=t===null?null:n(this._items,t);return!(t!==null&&!i||i?.disabled||i&&(String(i.id)===e||this._isDescendant(r,i.id))||!this._allowNesting()&&i||i?.canHaveChildren===!1||i?.acceptsTypes?.length&&!i.acceptsTypes.includes(r.type||``)||(i?this._depthOf(String(i.id)):-1)+1+a(r)>this._maxDepth())}_isDescendant(e,t){return(e.children||[]).some(e=>String(e.id)===String(t)||this._isDescendant(e,t))}_depthOf(e){let t=r(this._items,e);return t?this._depthOfParent(t.parentId):-1}_depthOfParent(e){if(e===null)return 0;let t=r(this._items,e);return t?this._depthOfParent(t.parentId)+1:0}_readItemsFromDOM(e){let t=e.querySelector(`:scope > mint-stack`);return t?[...t.children].filter(e=>e instanceof HTMLElement&&e.classList.contains(`mint-tree-node`)).map(e=>{let t=e.getAttribute(`data-tree-id`)||``,r=n(this._items,t),i=e.querySelector(`:scope > mint-sortable.mint-tree-children`);return{...r||{id:t,label:t},children:i?this._readItemsFromDOM(i):[]}}):[]}_clearDropState(){this._root?.querySelectorAll(`.mint-tree-drop-invalid`).forEach(e=>e.classList.remove(`mint-tree-drop-invalid`))}_collapseEmptyExpandedNodes(){for(let e of[...this._expandedIds]){let t=n(this._items,e);!t||t.children?.length||(this._expandedIds.delete(e),this._syncNodeExpanded(e),u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0,empty:!0}))}}_announce(e){this._liveRegion&&(this._liveRegion.textContent=e)}_keyboardIndent(e){let t=r(this._items,e);if(!t||t.siblings.indexOf(t.item)<=0)return;let n=t.siblings[t.siblings.indexOf(t.item)-1];try{let t=c(this._items,e,n.id,(n.children||[]).length,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._items=t.items,this._expandedIds.add(String(n.id)),this._render(),this._focusRow(e),u(this,`mint-tree-reparent`,{itemId:e,item:{...t.item},items:this.items,oldParentId:t.oldParentId,newParentId:t.newParentId,oldIndex:t.oldIndex,newIndex:t.newIndex})}catch{}}_keyboardOutdent(e){let t=r(this._items,e);if(!t||t.parentId===null)return;let n=r(this._items,t.parentId);if(!n)return;let i=n.siblings.findIndex(e=>String(e.id)===t.parentId)+1;try{let t=c(this._items,e,n.parentId,i,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._items=t.items,this._render(),this._focusRow(e),u(this,`mint-tree-reparent`,{itemId:e,item:{...t.item},items:this.items,oldParentId:t.oldParentId,newParentId:t.newParentId,oldIndex:t.oldIndex,newIndex:t.newIndex})}catch{}}_focusRow(e){[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e)?.focus({preventScroll:!0})}};customElements.get(`mint-tree`)||e.t(`mint-tree`,p),exports.MintTree=p,exports.default=p;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}}),require(`../chunks/icon-JJfYHBiG.js`);const e=require(`../chunks/register-element-tCL_F1YI.js`);require(`../chunks/spinner-DbTohC6i.js`),require(`../chunks/button-BNLP47mK.js`),require(`../chunks/confirm-action-BQVO6aFn.js`),require(`../chunks/popover-Db9ZGHd9.js`),require(`../chunks/sortable-DX2PwFLN.js`);function t(e=[]){s(e);let t=e=>e.map(e=>({...e,children:Array.isArray(e.children)?t(e.children):[]}));return t(e)}function n(e,t){let r=String(t);for(let t of e){if(String(t.id)===r)return t;let e=n(t.children||[],r);if(e)return e}return null}function r(e,t,n=null){let i=String(t);for(let t of e){if(String(t.id)===i)return{parentId:n,siblings:e,item:t};let a=r(t.children||[],i,String(t.id));if(a)return a}return null}function i(e,t,n=0){let r=String(t);for(let t of e){if(String(t.id)===r)return n;let e=i(t.children||[],r,n+1);if(e>=0)return e}return-1}function a(e){let t=Array.isArray(e?.children)?e.children:[];return t.length?1+Math.max(...t.map(a)):1}function o(e,t){return String(e.id)===String(t)?!0:(e.children||[]).some(e=>o(e,t))}function s(e){let t=new Set,n=e=>{for(let r of e){if(r.id===null||r.id===void 0||String(r.id)===``)throw Error(`Every tree item must have a non-empty identifier.`);let e=String(r.id);if(t.has(e))throw Error(`Tree item identifiers must be unique. Duplicate identifier: ${e}.`);t.add(e),n(r.children||[])}};n(e)}function c(e,s,c,l,u={}){let d=t(e),f=r(d,s);if(!f)throw Error(`Tree item ${s} was not found.`);if(f.item.disabled||f.item.draggable===!1)throw Error(`Disabled tree items cannot be moved.`);let p=c==null||c===``?null:n(d,c);if(c!=null&&c!==``&&!p)throw Error(`Target parent ${c} was not found.`);if(p&&o(f.item,p.id))throw Error(`A tree item cannot be moved inside itself or one of its descendants.`);if(p?.disabled)throw Error(`Disabled tree items cannot contain children.`);if(u.allowNesting===!1&&p)throw Error(`Nesting is disabled for this tree.`);if(p?.canHaveChildren===!1)throw Error(`The target item cannot contain children.`);if(p?.acceptsTypes?.length&&!p.acceptsTypes.includes(f.item.type))throw Error(`The target item does not accept this item type.`);let m=p?p.children:d,h=f.siblings.findIndex(e=>String(e.id)===String(s)),[g]=f.siblings.splice(h,1),_=Math.min(Math.max(0,Number.isFinite(l)?Math.trunc(l):m.length),m.length);m.splice(_,0,g);let v=Number.isFinite(u.maxDepth)?Math.max(1,Math.trunc(u.maxDepth)):1/0;if((p?i(d,p.id)+1:0)+a(g)>v)throw Error(`Moving this branch would exceed the maximum tree depth of ${v}.`);return{items:d,item:g,oldParentId:f.parentId,newParentId:p?String(p.id):null,oldIndex:h,newIndex:_}}var l=0;function u(e,t,n){e.dispatchEvent(new CustomEvent(t,{bubbles:!0,composed:!0,detail:n}))}function d(e,t){let n=e.getAttribute(t);return n===``||n===`true`}function f(e){return e==null||e===``?null:String(e)}var p=class extends HTMLElement{constructor(...e){super(...e),this._items=[],this._undoStack=[],this._redoStack=[],this._selectedId=null,this._expandedIds=new Set,this._root=null,this._confirm=null,this._treeId=`mint-tree-${++l}`,this._liveRegion=null,this._dragExpansion=null,this._autoExpandedIds=new Set,this._dragHoverExpandedId=null,this._keyboardFocusRestore=null,this._onClick=e=>{let t=e.target instanceof Element?e.target.closest(`[data-tree-command]`):null;if(!t||!this.contains(t))return;let r=t.getAttribute(`data-tree-command`),i=t.getAttribute(`data-tree-id`);if(r===`create-root`)u(this,`mint-tree-create`,{parentId:null,items:this.items});else if(r===`expand-all`)this._expandedIds=this._defaultExpandedIds(this._items),this._syncExpandedDOM(),u(this,`mint-tree-expand`,{all:!0,expandedIds:this.expandedIds});else if(r===`collapse-all`)this._expandedIds.clear(),this._syncExpandedDOM(),u(this,`mint-tree-collapse`,{all:!0,expandedIds:[]});else if(r===`undo`)this._undo();else if(r===`redo`)this._redo();else if(r===`toggle`&&i){let e=!this._expandedIds.has(i);this._setExpanded(i,e,!0)}else if(r===`select`&&i)this._select(i);else if(r===`create-child`&&i)this._closeItemPopover(t),u(this,`mint-tree-create`,{parentId:i,items:this.items});else if(r===`edit`&&i){this._closeItemPopover(t);let e=n(this._items,i);e&&u(this,`mint-tree-edit`,{item:{...e},itemId:i,items:this.items})}else if(r===`delete`&&i)this._closeItemPopover(t),this._confirmDelete(i);else if([`move-up`,`move-down`,`nest-previous`,`outdent`].includes(r||``)&&i)this._closeItemPopover(t),this._moveByCommand(i,r||``);else if(r===`custom`&&i){this._closeItemPopover(t);let e=n(this._items,i),r=t.getAttribute(`data-tree-action`)||``,a=e?.actions?.find(e=>e.id===r);if(e&&a){let t={item:{...e},itemId:i,action:{...a,onSelect:void 0},items:this.items};u(this,`mint-tree-action`,t),a.onSelect?.({item:{...e},itemId:i,action:a,items:this.items})}}},this._onSortableDrag=e=>{let t=e.detail;if(!t||!Number.isFinite(t.clientX)||!Number.isFinite(t.clientY))return;let r=document.elementFromPoint(t.clientX,t.clientY),i=r?.closest(`[data-mint-tree-row]`),a=t.item?.getAttribute(`data-tree-id`)||``,o=null;if(i&&i.getAttribute(`data-tree-id`)!==a){let e=i.querySelector(`:scope > .mint-tree-row`)?.getBoundingClientRect()||i.getBoundingClientRect(),r=e.height?(t.clientY-e.top)/e.height:0;if(r>.22&&r<.78){let e=i.getAttribute(`data-tree-id`)||``;n(this._items,e)&&this._isValidDrop(a,e)&&(o=e)}}if(!o&&this._dragHoverExpandedId){let e=[...this._root?.querySelectorAll(`[data-tree-id]`)??[]].find(e=>e.getAttribute(`data-tree-id`)===this._dragHoverExpandedId)?.querySelector(`:scope > mint-sortable.mint-tree-children`);r&&e?.contains(r)&&(o=this._dragHoverExpandedId)}if(this._dragHoverExpandedId&&this._dragHoverExpandedId!==o){let e=this._dragHoverExpandedId;this._dragHoverExpandedId=null,this._autoExpandedIds.delete(e),this._expandedIds.delete(e),this._syncNodeExpanded(e);let t=n(this._items,e);t&&u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0})}if(o){let e=n(this._items,o);e&&!this._expandedIds.has(o)&&(this._expandedIds.add(o),this._autoExpandedIds.add(o),this._dragHoverExpandedId=o,this._syncNodeExpanded(o),u(this,`mint-tree-expand`,{itemId:o,item:{...e},expandedIds:this.expandedIds,automatic:!0}))}},this._onSortableStart=()=>{this.classList.add(`mint-tree-drag-active`),this._dragHoverExpandedId=null,this._dragExpansion?.timer!==null&&this._dragExpansion?.timer!==void 0&&window.clearTimeout(this._dragExpansion.timer),this._dragExpansion=null;let e=this._root?.querySelector(`.mint-sortable-dragging[data-tree-id]`),t=e?.getAttribute(`data-tree-id`)||``,n=e?.querySelector(`:scope > mint-sortable.mint-tree-children`);if(!e||!t||!n||n.hidden||!this._expandedIds.has(t))return;let r=e.querySelector(`:scope > .mint-tree-row`),i=this._root?.querySelector(`.mint-sortable-placeholder`),a=n.getBoundingClientRect().height;n.style.height=`${a}px`,n.style.overflow=`hidden`,e.style.height=`auto`,e.style.overflow=`hidden`,this._dragExpansion={itemId:t,item:e,childList:n,timer:null},this._setTransientExpanded(t,!1),n.offsetHeight,requestAnimationFrame(()=>{this._dragExpansion?.childList===n&&(n.classList.add(`mint-tree-drag-collapse`),n.style.height=`0px`,i&&r&&(i.style.minHeight=`${r.getBoundingClientRect().height}px`))})},this._onSortableEnd=e=>{this.classList.remove(`mint-tree-drag-active`);let t=e.detail,r=t&&!t.cancelled?f(t.newParentId):null;for(let e of this._autoExpandedIds){if(e===r)continue;this._expandedIds.delete(e),this._syncNodeExpanded(e);let t=n(this._items,e);t&&u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0})}this._autoExpandedIds.clear(),this._dragHoverExpandedId=null;let i=this._dragExpansion;if(!i)return;i.item=[...this._root?.querySelectorAll(`[data-tree-id]`)??[]].find(e=>e.getAttribute(`data-tree-id`)===i.itemId)||i.item,i.childList=i.item.querySelector(`:scope > mint-sortable.mint-tree-children`)||i.childList;let{item:a,childList:o,itemId:s}=i;o.style.height=`0px`,o.style.overflow=`hidden`,o.classList.remove(`mint-tree-drag-collapse`),this._setTransientExpanded(s,!0),o.offsetHeight;let c=o.scrollHeight;requestAnimationFrame(()=>{this._dragExpansion===i&&(o.style.height=`${c}px`)}),i.timer=window.setTimeout(()=>{o.style.height=``,o.style.overflow=``,o.style.opacity=``,this._dragExpansion=null},155)},this._onSortableDragOver=e=>{let t=e,{item:r,target:i}=t.detail||{};if(!r||!i)return;this._root?.querySelectorAll(`.mint-tree-drop-invalid`).forEach(e=>e.classList.remove(`mint-tree-drop-invalid`));let a=this._root?.querySelector(`.mint-sortable-placeholder`),o=r.getAttribute(`data-tree-id`)||``,s=f(i.getAttribute(`data-tree-parent-id`)),c=i===t.detail.source||this._isValidDrop(o,s),l=s?n(this._items,s):null;c&&i!==t.detail.source&&l?a?.setAttribute(`data-drop-label`,`Drop here to nest under ${l.label}`):c&&i===this._root?.querySelector(`:scope > mint-sortable`)?a?.setAttribute(`data-drop-label`,`Drop here to move to the top level`):a?.removeAttribute(`data-drop-label`);let u=(Number.isFinite(t.detail.clientX)&&Number.isFinite(t.detail.clientY)?document.elementFromPoint(t.detail.clientX,t.detail.clientY):null)?.closest(`[data-mint-tree-row]`);u&&u.getAttribute(`data-tree-id`)!==o&&!c&&u.classList.add(`mint-tree-drop-invalid`),c||t.preventDefault()},this._onSortableBeforeDrop=e=>{let t=e,n=t.detail;if(!n?.item||!n.target)return;let r=n.item.getAttribute(`data-tree-id`)||``,i=f(n.target.getAttribute(`data-tree-parent-id`));n.target!==n.source&&!this._isValidDrop(r,i)&&t.preventDefault()},this._onSortableChange=e=>{let t=e.detail;if(!t||t.cancelled)return;let r=t.itemId||``,i=f(t.oldParentId),a=f(t.newParentId),o=this._root?.querySelector(`:scope > mint-sortable`);if(!o)return;let s=n(this._items,r);this._recordHistory(),this._items=this._readItemsFromDOM(o),this._collapseEmptyExpandedNodes();let c=n(this._items,r);if(!c||!s)return;let l={itemId:r,item:{...c},items:this.items,oldParentId:i,newParentId:a,oldIndex:Number(t.oldIndex??-1),newIndex:Number(t.newIndex??-1)};this._clearDropState(),u(this,`mint-tree-change`,{items:l.items,itemId:r}),i===a?u(this,`mint-tree-reorder`,l):u(this,`mint-tree-reparent`,l);let d=`${c.label} moved${a?` into ${n(this._items,a)?.label||`a group`}`:` to the top level`}.`;this._dragExpansion?.itemId===r?window.setTimeout(()=>{this.isConnected&&(this._render(),this._focusRow(r),this._announce(d))},155):(this._render(),this._focusRow(r),this._announce(d))},this._onKeydown=e=>{let t=e.target instanceof Element?e.target.closest(`[data-mint-tree-row]`):null;if(!t)return;if(e.altKey&&[`ArrowUp`,`ArrowDown`].includes(e.key)){e.preventDefault();let n=t.getAttribute(`data-tree-id`)||``;if(n){this._keyboardFocusRestore&&window.clearTimeout(this._keyboardFocusRestore.timer);let e={id:n,x:window.scrollX,y:window.scrollY,timer:0};e.timer=window.setTimeout(()=>{this._keyboardFocusRestore===e&&(this._keyboardFocusRestore=null)},2e3),this._keyboardFocusRestore=e}return}let i=t.getAttribute(`data-tree-id`)||``;if(e.altKey&&e.key===`ArrowRight`){e.preventDefault(),this._keyboardIndent(i);return}if(e.altKey&&e.key===`ArrowLeft`){e.preventDefault(),this._keyboardOutdent(i);return}let a=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].filter(e=>!e.closest(`mint-sortable[hidden]`)),o=a.indexOf(t);if(e.key===`ArrowDown`)e.preventDefault(),a[o+1]?.focus({preventScroll:!0});else if(e.key===`ArrowUp`)e.preventDefault(),a[o-1]?.focus({preventScroll:!0});else if(e.key===`ArrowRight`)e.preventDefault(),this._expandedIds.has(i)?a[o+1]?.focus({preventScroll:!0}):this._setExpanded(i,!0,!0);else if(e.key===`ArrowLeft`)if(e.preventDefault(),this._expandedIds.has(i))this._setExpanded(i,!1,!0);else{let e=r(this._items,i)?.parentId;e&&[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e)?.focus({preventScroll:!0})}else if(e.key===`Enter`||e.key===` `||e.key===`Spacebar`){let r=e.target instanceof Element?e.target.closest(`[data-tree-command="select"]`):null;(t===e.target||r)&&(e.preventDefault(),n(this._items,i)?.children?.length?this._setExpanded(i,!this._expandedIds.has(i),!0):this._select(i))}}}static get observedAttributes(){return[`selected-id`,`max-depth`,`allow-nesting`,`disabled`,`loading`,`create-label`,`empty-label`,`delete-confirmation`,`expanded-ids`]}connectedCallback(){this._root||(this._selectedId=f(this.getAttribute(`selected-id`)),this._expandedIds=this.hasAttribute(`expanded-ids`)?this._readExpandedIds(this.getAttribute(`expanded-ids`)):this._defaultExpandedIds(this._items),this.addEventListener(`click`,this._onClick),this.addEventListener(`keydown`,this._onKeydown,!0),this.addEventListener(`mint-sortable-start`,this._onSortableStart),this.addEventListener(`mint-sortable-end`,this._onSortableEnd),this.addEventListener(`mint-sortable-drag`,this._onSortableDrag),this.addEventListener(`mint-sortable-drag-over`,this._onSortableDragOver),this.addEventListener(`mint-sortable-before-drop`,this._onSortableBeforeDrop),this.addEventListener(`mint-sortable-change`,this._onSortableChange),this._render())}disconnectedCallback(){this._dragExpansion?.timer!==null&&this._dragExpansion?.timer!==void 0&&window.clearTimeout(this._dragExpansion.timer),this._keyboardFocusRestore&&window.clearTimeout(this._keyboardFocusRestore.timer),this._keyboardFocusRestore=null,this._dragExpansion=null,this._autoExpandedIds.clear(),this._dragHoverExpandedId=null,this.classList.remove(`mint-tree-drag-active`),this.removeEventListener(`click`,this._onClick),this.removeEventListener(`keydown`,this._onKeydown,!0),this.removeEventListener(`mint-sortable-start`,this._onSortableStart),this.removeEventListener(`mint-sortable-end`,this._onSortableEnd),this.removeEventListener(`mint-sortable-drag`,this._onSortableDrag),this.removeEventListener(`mint-sortable-drag-over`,this._onSortableDragOver),this.removeEventListener(`mint-sortable-before-drop`,this._onSortableBeforeDrop),this.removeEventListener(`mint-sortable-change`,this._onSortableChange),this._root=null}attributeChangedCallback(e,t,n){if(!(t===n||!this.isConnected)){if(e===`selected-id`){this._selectedId=f(n),this._syncSelectionDOM();return}e===`expanded-ids`&&(this._expandedIds=this._readExpandedIds(n)),this._render()}}get items(){return t(this._items)}set items(e){this._items=t(Array.isArray(e)?e:[]),this._undoStack=[],this._redoStack=[],this.hasAttribute(`expanded-ids`)||(this._expandedIds=this._defaultExpandedIds(this._items)),this.isConnected&&this._render()}get selectedId(){return this._selectedId}set selectedId(e){this._selectedId=f(e),this._selectedId===null?this.removeAttribute(`selected-id`):this.setAttribute(`selected-id`,this._selectedId)}get expandedIds(){return[...this._expandedIds]}set expandedIds(e){this._expandedIds=new Set((Array.isArray(e)?e:[]).map(String)),this.isConnected&&this._render()}save(){u(this,`mint-tree-save`,{items:this.items})}select(e){this._select(String(e))}expand(e){this._setExpanded(String(e),!0,!0)}collapse(e){this._setExpanded(String(e),!1,!0)}_readExpandedIds(e){if(!e)return this._defaultExpandedIds(this._items);try{let t=JSON.parse(e);return new Set(Array.isArray(t)?t.map(String):[])}catch{return new Set(e.split(`,`).map(e=>e.trim()).filter(Boolean))}}_defaultExpandedIds(e){let t=new Set,n=e=>e.forEach(e=>{e.children?.length&&(t.add(String(e.id)),n(e.children))});return n(e),t}_maxDepth(){let e=Number.parseInt(this.getAttribute(`max-depth`)||``,10);return Number.isFinite(e)&&e>0?e:1/0}_allowNesting(){return!this.hasAttribute(`allow-nesting`)||d(this,`allow-nesting`)}_disabled(){return d(this,`disabled`)}_isLoading(){return d(this,`loading`)}_render(){let e=document.activeElement instanceof HTMLElement?document.activeElement.closest(`[data-mint-tree-row]`):null,t=e&&this.contains(e)?e.getAttribute(`data-tree-id`):null;t&&this._keyboardFocusRestore&&(this._keyboardFocusRestore.id=t);let n=document.createElement(`div`);n.className=`mint-tree-shell`,n.setAttribute(`role`,`tree`),n.setAttribute(`aria-label`,this.getAttribute(`aria-label`)||`Hierarchical items`),n.setAttribute(`aria-busy`,String(this._isLoading()));let r=document.createElement(`div`);r.className=`mint-tree-toolbar`;let i=document.createElement(`span`);i.className=`mint-tree-count`,i.textContent=`${this._countItems()} ${this._countItems()===1?`item`:`items`}`,r.appendChild(i);let a=document.createElement(`div`);a.className=`mint-tree-toolbar-actions`;let o=this._createButton(``,`ghost`,`undo`);o.setAttribute(`icon`,`undo`),o.setAttribute(`aria-label`,`Undo last change`),o.setAttribute(`title`,`Undo`),(!this._undoStack.length||this._disabled())&&o.setAttribute(`disabled`,`true`);let s=this._createButton(``,`ghost`,`redo`);s.setAttribute(`icon`,`redo`),s.setAttribute(`aria-label`,`Redo last undone change`),s.setAttribute(`title`,`Redo`),(!this._redoStack.length||this._disabled())&&s.setAttribute(`disabled`,`true`);let c=this._createButton(`Expand all`,`ghost`,`expand-all`),l=this._createButton(`Collapse all`,`ghost`,`collapse-all`);if(a.append(o,s,c,l),r.appendChild(a),n.appendChild(r),this._isLoading()){let e=document.createElement(`div`);e.className=`mint-tree-loading`,e.setAttribute(`aria-label`,`Loading items`);let t=document.createElement(`mint-spinner`);t.setAttribute(`size`,`small`),e.appendChild(t),n.appendChild(e)}else if(this._items.length)n.appendChild(this._createList(this._items,null,0));else{let e=document.createElement(`div`);e.className=`mint-tree-empty`,e.textContent=this.getAttribute(`empty-label`)||`No items yet.`,n.appendChild(e)}this._liveRegion=document.createElement(`div`),this._liveRegion.className=`mint-tree-live-region`,this._liveRegion.setAttribute(`role`,`status`),this._liveRegion.setAttribute(`aria-live`,`polite`),this._liveRegion.setAttribute(`aria-atomic`,`true`),n.appendChild(this._liveRegion);let u=document.createElement(`mint-confirm-action`);u.className=`mint-tree-confirm`,this._confirm=u,n.appendChild(u),this.replaceChildren(n),this._root=n,this.classList.add(`mint-tree`),this._disabled()?this.setAttribute(`aria-disabled`,`true`):this.removeAttribute(`aria-disabled`);let d=this._keyboardFocusRestore;d&&(this._focusRow(d.id),window.scrollTo({left:d.x,top:d.y,behavior:`instant`}),requestAnimationFrame(()=>{this._keyboardFocusRestore===d&&(this._focusRow(d.id),window.scrollTo({left:d.x,top:d.y,behavior:`instant`}))}))}_countItems(e=this._items){return e.reduce((e,t)=>e+1+this._countItems(t.children||[]),0)}_createButton(e,t,n){let r=document.createElement(`mint-button`);return r.textContent=e,r.setAttribute(`variant`,t),r.setAttribute(`data-tree-command`,n),r}_createList(e,t,n){let r=document.createElement(`mint-sortable`);r.id=`${this._treeId}-list-${t??`root`}`,r.setAttribute(`group`,this._treeId),r.setAttribute(`data-tree-parent-id`,t??``),r.setAttribute(`role`,`group`),r.setAttribute(`data-tree-depth`,String(n)),r.setAttribute(`direction`,`vertical`),r.setAttribute(`gap`,`1`),r.setAttribute(`handle-selector`,`[data-mint-sortable-handle]`),r.setAttribute(`animation-duration`,`320`),r.setAttribute(`item-key-attribute`,`data-tree-id`);let i=document.createElement(`mint-stack`);i.setAttribute(`direction`,`vertical`),i.setAttribute(`gap`,`1`);for(let t of e)i.appendChild(this._createNode(t,n));return r.appendChild(i),r}_createNode(e,t){let n=String(e.id),r=Array.isArray(e.children)?e.children:[],i=e.canHaveChildren!==!1&&this._allowNesting()&&t+a(e)<this._maxDepth(),o=document.createElement(`div`);o.className=`mint-tree-node`,o.setAttribute(`data-tree-id`,n),o.setAttribute(`role`,`treeitem`),o.setAttribute(`data-mint-tree-row`,``),o.setAttribute(`aria-level`,String(t+1)),o.setAttribute(`aria-selected`,String(this._selectedId===n)),o.setAttribute(`tabindex`,this._selectedId===n||this._selectedId===null&&t===0&&this._items[0]?.id===e.id?`0`:`-1`),o.setAttribute(`aria-label`,e.label),r.length&&o.setAttribute(`aria-expanded`,String(this._expandedIds.has(n))),(e.disabled||this._disabled())&&o.setAttribute(`aria-disabled`,`true`),(e.draggable===!1||this._disabled())&&o.setAttribute(`data-sortable-disabled`,``);let s=document.createElement(`div`);s.className=`mint-tree-row`,s.setAttribute(`data-tree-id`,n);let c=document.createElement(`span`);c.className=`mint-tree-drag-handle`,c.setAttribute(`data-mint-sortable-handle`,``),c.setAttribute(`role`,`button`),c.setAttribute(`tabindex`,`-1`),c.setAttribute(`aria-label`,`Drag ${e.label} to move`);let l=document.createElement(`mint-icon`);if(l.setAttribute(`name`,`grip-dots`),l.setAttribute(`aria-hidden`,`true`),c.appendChild(l),s.appendChild(c),r.length){let t=document.createElement(`mint-button`);t.className=`mint-tree-expand`,t.setAttribute(`variant`,`ghost`),t.setAttribute(`icon`,this._expandedIds.has(n)?`caret-up`:`caret-down`),t.setAttribute(`data-tree-command`,`toggle`),t.setAttribute(`data-tree-id`,n),t.setAttribute(`aria-label`,`${this._expandedIds.has(n)?`Collapse`:`Expand`} ${e.label}`),t.setAttribute(`aria-expanded`,String(this._expandedIds.has(n))),(this._disabled()||e.disabled===!0)&&t.setAttribute(`disabled`,`true`),s.appendChild(t)}else{let e=document.createElement(`span`);e.className=`mint-tree-expand-spacer`,e.setAttribute(`aria-hidden`,`true`),s.appendChild(e)}let u=document.createElement(`span`);if(u.className=`mint-tree-label`,u.setAttribute(`data-tree-command`,`select`),u.setAttribute(`data-tree-id`,n),i&&(u.setAttribute(`data-mint-sortable-drop-container`,`${this._treeId}-list-${n}`),u.setAttribute(`data-mint-sortable-drop-end`,``)),e.icon){let t=document.createElement(`mint-icon`);t.setAttribute(`name`,String(e.icon)),t.setAttribute(`aria-hidden`,`true`),u.appendChild(t)}let d=document.createElement(`span`);if(d.className=`mint-tree-label-text`,d.textContent=e.label,u.appendChild(d),s.appendChild(u),e.metadata){let t=document.createElement(`span`);t.className=`mint-tree-metadata`,t.textContent=String(e.metadata),s.appendChild(t)}let f=document.createElement(`span`);f.className=`mint-tree-nest-target`,f.setAttribute(`data-mint-sortable-drop-container`,`${this._treeId}-list-${n}`),f.setAttribute(`data-mint-sortable-drop-end`,``),f.setAttribute(`aria-hidden`,`true`);let p=document.createElement(`mint-icon`);p.setAttribute(`name`,`arrow-right`),p.setAttribute(`aria-hidden`,`true`),f.appendChild(p),i&&s.appendChild(f);let m=this._createActions(e,i);s.appendChild(m),o.appendChild(s);let h=this._createList(r,n,t+1);return h.classList.add(`mint-tree-children`),h.hidden=!this._expandedIds.has(n),o.appendChild(h),o}_createActions(e,t){let i=document.createElement(`div`);i.className=`mint-tree-actions`;let a=`${this._treeId}-actions-trigger-${encodeURIComponent(String(e.id))}`,o=`${a}-popover`,s=document.createElement(`mint-button`);s.id=a,s.setAttribute(`variant`,`ghost`),s.setAttribute(`icon`,`ellipsis`),s.setAttribute(`aria-label`,`Actions for ${e.label}`),s.setAttribute(`aria-haspopup`,`menu`),i.appendChild(s);let c=document.createElement(`mint-popover`);c.id=o,c.setAttribute(`trigger-id`,a),c.setAttribute(`direction`,`down`),c.setAttribute(`trigger-align`,`end`),c.setAttribute(`padding`,`0.3rem`);let l=document.createElement(`div`);l.className=`mint-tree-menu`,l.setAttribute(`role`,`menu`);let u=(t,n,r=!1)=>{let i=document.createElement(`mint-button`);i.textContent=n,i.setAttribute(`variant`,`ghost`),i.setAttribute(`full-width`,`true`),i.setAttribute(`align`,`left`),i.setAttribute(`role`,`menuitem`),i.setAttribute(`data-tree-command`,t),i.setAttribute(`data-tree-id`,String(e.id)),(r||e.disabled===!0||this._disabled())&&i.setAttribute(`disabled`,`true`),l.appendChild(i)},d=Array.isArray(e.actions)?e.actions:null;if(d)for(let t of d){let n=document.createElement(`mint-button`);n.textContent=t.label,n.setAttribute(`variant`,`ghost`),n.setAttribute(`full-width`,`true`),n.setAttribute(`align`,`left`),n.setAttribute(`role`,`menuitem`),n.setAttribute(`data-tree-command`,`custom`),n.setAttribute(`data-tree-id`,String(e.id)),n.setAttribute(`data-tree-action`,t.id),(t.disabled===!0||e.disabled===!0||this._disabled())&&n.setAttribute(`disabled`,`true`),t.icon&&n.setAttribute(`icon`,t.icon),t.tone&&n.setAttribute(`tone`,t.tone),l.appendChild(n)}else{let i=r(this._items,e.id),a=i?.siblings||[],o=a.findIndex(t=>String(t.id)===String(e.id)),s=o>0?a[o-1]:null,c=i?.parentId?n(this._items,i.parentId):null;u(`move-up`,`Move up`,o<=0),u(`move-down`,`Move down`,o<0||o>=a.length-1),u(`nest-previous`,`Nest under previous item`,!s||!this._isValidDrop(String(e.id),String(s.id))),u(`outdent`,`Move to parent level`,!c),u(`create-child`,`Add child`,!t),u(`edit`,`Edit`),u(`delete`,`Delete`)}return c.appendChild(l),i.appendChild(c),i}_closeItemPopover(e){(e.closest(`.mint-tree-menu`)?.closest(`mint-popover`))?.close?.()}async _confirmDelete(e){let t=n(this._items,e);!t||!this._confirm?.showConfirm||await this._confirm.showConfirm({question:this.getAttribute(`delete-confirmation`)||`Delete “${t.label}”?`,tone:`danger`,confirmLabel:`Delete`,cancelLabel:`Cancel`})&&u(this,`mint-tree-delete`,{item:{...t},itemId:e,items:this.items})}_moveByCommand(e,t){let n=r(this._items,e);if(!n)return;let i=n.siblings.findIndex(t=>String(t.id)===e),a=n.parentId,o=i;if(t===`move-up`)o=i-1;else if(t===`move-down`)o=i+1;else if(t===`nest-previous`){let e=n.siblings[i-1];if(!e)return;a=String(e.id),o=(e.children||[]).length}else if(t===`outdent`){if(n.parentId===null)return;let e=r(this._items,n.parentId);if(!e)return;a=e.parentId,o=e.siblings.findIndex(e=>String(e.id)===n.parentId)+1}else return;try{let t=c(this._items,e,a,o,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._applyMove(t)}catch{}}_applyMove(e){this._recordHistory(),this._items=e.items,this._collapseEmptyExpandedNodes(),e.newParentId&&this._expandedIds.add(e.newParentId),this._render(),this._focusRow(String(e.item.id));let t={itemId:String(e.item.id),item:{...e.item},items:this.items,oldParentId:e.oldParentId,newParentId:e.newParentId,oldIndex:e.oldIndex,newIndex:e.newIndex};u(this,`mint-tree-change`,{items:t.items,itemId:t.itemId}),u(this,t.oldParentId===t.newParentId?`mint-tree-reorder`:`mint-tree-reparent`,t),this._announce(`${t.item.label} moved${t.newParentId?` into ${n(this._items,t.newParentId)?.label||`a group`}`:` to the top level`}.`)}_select(e){let t=n(this._items,e);!t||t.disabled||this._disabled()||(this._selectedId=e,this.setAttribute(`selected-id`,e),this._syncSelectionDOM(),u(this,`mint-tree-select`,{item:{...t},itemId:e,items:this.items}))}_syncSelectionDOM(){(this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]).forEach(e=>{let t=e.getAttribute(`data-tree-id`)===this._selectedId;e.setAttribute(`aria-selected`,String(t)),e.tabIndex=t?0:-1})}_setExpanded(e,t,r){let i=n(this._items,e);!i||i.disabled||this._disabled()||(t?this._expandedIds.add(e):this._expandedIds.delete(e),this._syncNodeExpanded(e),r&&u(this,t?`mint-tree-expand`:`mint-tree-collapse`,{itemId:e,item:{...i},expandedIds:this.expandedIds}))}_syncExpandedDOM(){this._root?.querySelectorAll(`[data-mint-tree-row]`).forEach(e=>this._syncNodeExpanded(e.getAttribute(`data-tree-id`)||``))}_syncNodeExpanded(e){let t=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e),r=t?.querySelector(`:scope > mint-sortable.mint-tree-children`);if(!t||!r)return;let i=this._expandedIds.has(e);t.setAttribute(`aria-expanded`,String(i));let a=t.querySelector(`[data-tree-command="toggle"]`);a?.setAttribute(`aria-expanded`,String(i)),a?.setAttribute(`aria-label`,`${i?`Collapse`:`Expand`} ${n(this._items,e)?.label||`item`}`),a?.setAttribute(`icon`,i?`caret-up`:`caret-down`),r.hidden=!i}_setTransientExpanded(e,t){let r=[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e);if(!r)return;r.setAttribute(`aria-expanded`,String(t));let i=r.querySelector(`[data-tree-command="toggle"]`),a=n(this._items,e)?.label||`item`;i?.setAttribute(`aria-expanded`,String(t)),i?.setAttribute(`aria-label`,`${t?`Collapse`:`Expand`} ${a}`),i?.setAttribute(`icon`,t?`caret-up`:`caret-down`)}_isValidDrop(e,t){let r=n(this._items,e);if(!r)return!1;let i=t===null?null:n(this._items,t);return!(t!==null&&!i||i?.disabled||i&&(String(i.id)===e||this._isDescendant(r,i.id))||!this._allowNesting()&&i||i?.canHaveChildren===!1||i?.acceptsTypes?.length&&!i.acceptsTypes.includes(r.type||``)||(i?this._depthOf(String(i.id)):-1)+1+a(r)>this._maxDepth())}_isDescendant(e,t){return(e.children||[]).some(e=>String(e.id)===String(t)||this._isDescendant(e,t))}_depthOf(e){let t=r(this._items,e);return t?this._depthOfParent(t.parentId):-1}_depthOfParent(e){if(e===null)return 0;let t=r(this._items,e);return t?this._depthOfParent(t.parentId)+1:0}_readItemsFromDOM(e){let t=e.querySelector(`:scope > mint-stack`);return t?[...t.children].filter(e=>e instanceof HTMLElement&&e.classList.contains(`mint-tree-node`)).map(e=>{let t=e.getAttribute(`data-tree-id`)||``,r=n(this._items,t),i=e.querySelector(`:scope > mint-sortable.mint-tree-children`);return{...r||{id:t,label:t},children:i?this._readItemsFromDOM(i):[]}}):[]}_clearDropState(){this._root?.querySelectorAll(`.mint-tree-drop-invalid`).forEach(e=>e.classList.remove(`mint-tree-drop-invalid`))}_collapseEmptyExpandedNodes(){for(let e of[...this._expandedIds]){let t=n(this._items,e);!t||t.children?.length||(this._expandedIds.delete(e),this._syncNodeExpanded(e),u(this,`mint-tree-collapse`,{itemId:e,item:{...t},expandedIds:this.expandedIds,automatic:!0,empty:!0}))}}_announce(e){this._liveRegion&&(this._liveRegion.textContent=e)}_recordHistory(){this._undoStack.push(t(this._items)),this._undoStack.length>50&&this._undoStack.shift(),this._redoStack=[]}_undo(){let e=this._undoStack.pop();e&&(this._redoStack.push(t(this._items)),this._items=e,this._render(),u(this,`mint-tree-change`,{items:this.items,itemId:this._selectedId}),u(this,`mint-tree-undo`,{items:this.items}))}_redo(){let e=this._redoStack.pop();e&&(this._undoStack.push(t(this._items)),this._items=e,this._render(),u(this,`mint-tree-change`,{items:this.items,itemId:this._selectedId}),u(this,`mint-tree-redo`,{items:this.items}))}_keyboardIndent(e){let t=r(this._items,e);if(!t||t.siblings.indexOf(t.item)<=0)return;let n=t.siblings[t.siblings.indexOf(t.item)-1];try{let t=c(this._items,e,n.id,(n.children||[]).length,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._recordHistory(),this._items=t.items,this._expandedIds.add(String(n.id)),this._render(),this._focusRow(e),u(this,`mint-tree-reparent`,{itemId:e,item:{...t.item},items:this.items,oldParentId:t.oldParentId,newParentId:t.newParentId,oldIndex:t.oldIndex,newIndex:t.newIndex})}catch{}}_keyboardOutdent(e){let t=r(this._items,e);if(!t||t.parentId===null)return;let n=r(this._items,t.parentId);if(!n)return;let i=n.siblings.findIndex(e=>String(e.id)===t.parentId)+1;try{let t=c(this._items,e,n.parentId,i,{maxDepth:this._maxDepth(),allowNesting:this._allowNesting()});this._recordHistory(),this._items=t.items,this._render(),this._focusRow(e),u(this,`mint-tree-reparent`,{itemId:e,item:{...t.item},items:this.items,oldParentId:t.oldParentId,newParentId:t.newParentId,oldIndex:t.oldIndex,newIndex:t.newIndex})}catch{}}_focusRow(e){[...this._root?.querySelectorAll(`[data-mint-tree-row]`)??[]].find(t=>t.getAttribute(`data-tree-id`)===e)?.focus({preventScroll:!0})}};customElements.get(`mint-tree`)||e.t(`mint-tree`,p),exports.MintTree=p,exports.default=p;
@@ -9,11 +9,11 @@ import '../../confirm-action.css'
9
9
  import '../../modal.css'
10
10
  import '../../text.css'
11
11
  import '../../tree.css'
12
- import "../chunks/icon-DZP-hLHj.js";
12
+ import "../chunks/icon-NUvffC5m.js";
13
13
  import { t as e } from "../chunks/register-element-BFYmIynH.js";
14
14
  import "../chunks/spinner-BsaFHuS6.js";
15
- import "../chunks/button-M7gi8RMZ.js";
16
- import "../chunks/confirm-action-D4U-R6zW.js";
15
+ import "../chunks/button-DPSkBQsf.js";
16
+ import "../chunks/confirm-action-THfH8TEH.js";
17
17
  import "../chunks/popover-CV-QCPjW.js";
18
18
  import "../chunks/sortable-CparUKN_.js";
19
19
  //#region src/components/tree/tree-utils.js
@@ -117,7 +117,7 @@ function f(e) {
117
117
  }
118
118
  var p = class extends HTMLElement {
119
119
  constructor(...e) {
120
- super(...e), this._items = [], this._selectedId = null, this._expandedIds = /* @__PURE__ */ new Set(), this._root = null, this._confirm = null, this._treeId = `mint-tree-${++l}`, this._liveRegion = null, this._dragExpansion = null, this._autoExpandedIds = /* @__PURE__ */ new Set(), this._dragHoverExpandedId = null, this._keyboardFocusRestore = null, this._onClick = (e) => {
120
+ super(...e), this._items = [], this._undoStack = [], this._redoStack = [], this._selectedId = null, this._expandedIds = /* @__PURE__ */ new Set(), this._root = null, this._confirm = null, this._treeId = `mint-tree-${++l}`, this._liveRegion = null, this._dragExpansion = null, this._autoExpandedIds = /* @__PURE__ */ new Set(), this._dragHoverExpandedId = null, this._keyboardFocusRestore = null, this._onClick = (e) => {
121
121
  let t = e.target instanceof Element ? e.target.closest("[data-tree-command]") : null;
122
122
  if (!t || !this.contains(t)) return;
123
123
  let r = t.getAttribute("data-tree-command"), i = t.getAttribute("data-tree-id");
@@ -133,6 +133,8 @@ var p = class extends HTMLElement {
133
133
  all: !0,
134
134
  expandedIds: []
135
135
  });
136
+ else if (r === "undo") this._undo();
137
+ else if (r === "redo") this._redo();
136
138
  else if (r === "toggle" && i) {
137
139
  let e = !this._expandedIds.has(i);
138
140
  this._setExpanded(i, e, !0);
@@ -270,7 +272,7 @@ var p = class extends HTMLElement {
270
272
  let r = t.itemId || "", i = f(t.oldParentId), a = f(t.newParentId), o = this._root?.querySelector(":scope > mint-sortable");
271
273
  if (!o) return;
272
274
  let s = n(this._items, r);
273
- this._items = this._readItemsFromDOM(o), this._collapseEmptyExpandedNodes();
275
+ this._recordHistory(), this._items = this._readItemsFromDOM(o), this._collapseEmptyExpandedNodes();
274
276
  let c = n(this._items, r);
275
277
  if (!c || !s) return;
276
278
  let l = {
@@ -366,7 +368,7 @@ var p = class extends HTMLElement {
366
368
  return t(this._items);
367
369
  }
368
370
  set items(e) {
369
- this._items = t(Array.isArray(e) ? e : []), this.hasAttribute("expanded-ids") || (this._expandedIds = this._defaultExpandedIds(this._items)), this.isConnected && this._render();
371
+ this._items = t(Array.isArray(e) ? e : []), this._undoStack = [], this._redoStack = [], this.hasAttribute("expanded-ids") || (this._expandedIds = this._defaultExpandedIds(this._items)), this.isConnected && this._render();
370
372
  }
371
373
  get selectedId() {
372
374
  return this._selectedId;
@@ -431,8 +433,12 @@ var p = class extends HTMLElement {
431
433
  i.className = "mint-tree-count", i.textContent = `${this._countItems()} ${this._countItems() === 1 ? "item" : "items"}`, r.appendChild(i);
432
434
  let a = document.createElement("div");
433
435
  a.className = "mint-tree-toolbar-actions";
434
- let o = this._createButton("Expand all", "ghost", "expand-all"), s = this._createButton("Collapse all", "ghost", "collapse-all");
435
- if (a.append(o, s), r.appendChild(a), n.appendChild(r), this._isLoading()) {
436
+ let o = this._createButton("", "ghost", "undo");
437
+ o.setAttribute("icon", "undo"), o.setAttribute("aria-label", "Undo last change"), o.setAttribute("title", "Undo"), (!this._undoStack.length || this._disabled()) && o.setAttribute("disabled", "true");
438
+ let s = this._createButton("", "ghost", "redo");
439
+ s.setAttribute("icon", "redo"), s.setAttribute("aria-label", "Redo last undone change"), s.setAttribute("title", "Redo"), (!this._redoStack.length || this._disabled()) && s.setAttribute("disabled", "true");
440
+ let c = this._createButton("Expand all", "ghost", "expand-all"), l = this._createButton("Collapse all", "ghost", "collapse-all");
441
+ if (a.append(o, s, c, l), r.appendChild(a), n.appendChild(r), this._isLoading()) {
436
442
  let e = document.createElement("div");
437
443
  e.className = "mint-tree-loading", e.setAttribute("aria-label", "Loading items");
438
444
  let t = document.createElement("mint-spinner");
@@ -443,17 +449,17 @@ var p = class extends HTMLElement {
443
449
  e.className = "mint-tree-empty", e.textContent = this.getAttribute("empty-label") || "No items yet.", n.appendChild(e);
444
450
  }
445
451
  this._liveRegion = document.createElement("div"), this._liveRegion.className = "mint-tree-live-region", this._liveRegion.setAttribute("role", "status"), this._liveRegion.setAttribute("aria-live", "polite"), this._liveRegion.setAttribute("aria-atomic", "true"), n.appendChild(this._liveRegion);
446
- let c = document.createElement("mint-confirm-action");
447
- c.className = "mint-tree-confirm", this._confirm = c, n.appendChild(c), this.replaceChildren(n), this._root = n, this.classList.add("mint-tree"), this._disabled() ? this.setAttribute("aria-disabled", "true") : this.removeAttribute("aria-disabled");
448
- let l = this._keyboardFocusRestore;
449
- l && (this._focusRow(l.id), window.scrollTo({
450
- left: l.x,
451
- top: l.y,
452
+ let u = document.createElement("mint-confirm-action");
453
+ u.className = "mint-tree-confirm", this._confirm = u, n.appendChild(u), this.replaceChildren(n), this._root = n, this.classList.add("mint-tree"), this._disabled() ? this.setAttribute("aria-disabled", "true") : this.removeAttribute("aria-disabled");
454
+ let d = this._keyboardFocusRestore;
455
+ d && (this._focusRow(d.id), window.scrollTo({
456
+ left: d.x,
457
+ top: d.y,
452
458
  behavior: "instant"
453
459
  }), requestAnimationFrame(() => {
454
- this._keyboardFocusRestore === l && (this._focusRow(l.id), window.scrollTo({
455
- left: l.x,
456
- top: l.y,
460
+ this._keyboardFocusRestore === d && (this._focusRow(d.id), window.scrollTo({
461
+ left: d.x,
462
+ top: d.y,
457
463
  behavior: "instant"
458
464
  }));
459
465
  }));
@@ -571,7 +577,7 @@ var p = class extends HTMLElement {
571
577
  } catch {}
572
578
  }
573
579
  _applyMove(e) {
574
- this._items = e.items, this._collapseEmptyExpandedNodes(), e.newParentId && this._expandedIds.add(e.newParentId), this._render(), this._focusRow(String(e.item.id));
580
+ this._recordHistory(), this._items = e.items, this._collapseEmptyExpandedNodes(), e.newParentId && this._expandedIds.add(e.newParentId), this._render(), this._focusRow(String(e.item.id));
575
581
  let t = {
576
582
  itemId: String(e.item.id),
577
583
  item: { ...e.item },
@@ -675,6 +681,23 @@ var p = class extends HTMLElement {
675
681
  _announce(e) {
676
682
  this._liveRegion && (this._liveRegion.textContent = e);
677
683
  }
684
+ _recordHistory() {
685
+ this._undoStack.push(t(this._items)), this._undoStack.length > 50 && this._undoStack.shift(), this._redoStack = [];
686
+ }
687
+ _undo() {
688
+ let e = this._undoStack.pop();
689
+ e && (this._redoStack.push(t(this._items)), this._items = e, this._render(), u(this, "mint-tree-change", {
690
+ items: this.items,
691
+ itemId: this._selectedId
692
+ }), u(this, "mint-tree-undo", { items: this.items }));
693
+ }
694
+ _redo() {
695
+ let e = this._redoStack.pop();
696
+ e && (this._undoStack.push(t(this._items)), this._items = e, this._render(), u(this, "mint-tree-change", {
697
+ items: this.items,
698
+ itemId: this._selectedId
699
+ }), u(this, "mint-tree-redo", { items: this.items }));
700
+ }
678
701
  _keyboardIndent(e) {
679
702
  let t = r(this._items, e);
680
703
  if (!t || t.siblings.indexOf(t.item) <= 0) return;
@@ -684,7 +707,7 @@ var p = class extends HTMLElement {
684
707
  maxDepth: this._maxDepth(),
685
708
  allowNesting: this._allowNesting()
686
709
  });
687
- this._items = t.items, this._expandedIds.add(String(n.id)), this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
710
+ this._recordHistory(), this._items = t.items, this._expandedIds.add(String(n.id)), this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
688
711
  itemId: e,
689
712
  item: { ...t.item },
690
713
  items: this.items,
@@ -706,7 +729,7 @@ var p = class extends HTMLElement {
706
729
  maxDepth: this._maxDepth(),
707
730
  allowNesting: this._allowNesting()
708
731
  });
709
- this._items = t.items, this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
732
+ this._recordHistory(), this._items = t.items, this._render(), this._focusRow(e), u(this, "mint-tree-reparent", {
710
733
  itemId: e,
711
734
  item: { ...t.item },
712
735
  items: this.items,
@@ -0,0 +1,3 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="m15 14 5-5-5-5m5 5H10a6 6 0 0 0 0 12h2" />
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M5 3h12l4 4v14H3V3h2zm2 0v6h10V3M7 21v-8h10v8" />
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
2
+ <path d="M9 14 4 9l5-5M4 9h10a6 6 0 0 1 0 12h-2" />
3
+ </svg>