@taprootio/espalier 6.10.0 → 6.12.0

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.
@@ -1,4 +1,4 @@
1
- var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnPropertyDescriptor(e,t):i,r;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")o=Reflect.decorate(c,e,t,i);else for(var a=c.length-1;a>=0;a--)(r=c[a])&&(o=(s<3?r(o):s>3?r(e,t,o):r(e,t))||o);return s>3&&o&&Object.defineProperty(e,t,o),o};import{css as D,html as p,nothing as u}from"lit";import{customElement as L,property as h,state as x}from"lit/decorators.js";import{classMap as m}from"lit/directives/class-map.js";import{createRef as $,ref as S}from"lit/directives/ref.js";import{unsafeHTML as C}from"lit/directives/unsafe-html.js";import{EspalierElementBase as z}from"../shared/esp-element-base.js";import{ESP_EVENTS as _}from"../shared/events.js";import{FormFieldController as F}from"../shared/form-field-controller.js";import{getIconHrefForHost as M}from"../shared/intent-values.js";import{caretRight as U}from"../shared/svgs/caret-right.js";import{renderSpriteIcon as G}from"../shared/svgs/render-sprite-icon.js";import"../box/esp-box.js";import"../action-menu/esp-action-menu.js";import"../button/esp-button.js";import"../dialog/esp-dialog.js";import"../form-item/esp-form-item.js";import"../info/esp-info.js";import"../input/esp-input.js";import"./esp-tree-item.js";import{EspalierTreeItem as P}from"./esp-tree-item.js";const B="A branch with that name already exists here.",q="Enter a name for the new branch.",V=500,T={edit:_.TREE_EDIT,delete:_.TREE_DELETE,"move-up":_.TREE_MOVE_UP,"move-down":_.TREE_MOVE_DOWN,cut:_.TREE_CUT,"paste-child":_.TREE_PASTE_CHILD},N={edit:`<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">
1
+ var d=function(c,e,t,i){var s=arguments.length,a=s<3?e:i===null?i=Object.getOwnPropertyDescriptor(e,t):i,r;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")a=Reflect.decorate(c,e,t,i);else for(var o=c.length-1;o>=0;o--)(r=c[o])&&(a=(s<3?r(a):s>3?r(e,t,a):r(e,t))||a);return s>3&&a&&Object.defineProperty(e,t,a),a};import{css as D,html as p,nothing as u}from"lit";import{customElement as L,property as h,state as x}from"lit/decorators.js";import{classMap as m}from"lit/directives/class-map.js";import{createRef as $,ref as S}from"lit/directives/ref.js";import{unsafeHTML as C}from"lit/directives/unsafe-html.js";import{EspalierElementBase as z}from"../shared/esp-element-base.js";import{ESP_EVENTS as _}from"../shared/events.js";import{FormFieldController as F}from"../shared/form-field-controller.js";import{getIconHrefForHost as M}from"../shared/intent-values.js";import{caretRight as U}from"../shared/svgs/caret-right.js";import{renderSpriteIcon as G}from"../shared/svgs/render-sprite-icon.js";import"../box/esp-box.js";import"../action-menu/esp-action-menu.js";import"../button/esp-button.js";import"../dialog/esp-dialog.js";import"../form-item/esp-form-item.js";import"../info/esp-info.js";import"../input/esp-input.js";import"./esp-tree-item.js";import{EspalierTreeItem as T}from"./esp-tree-item.js";const B="A branch with that name already exists here.",q="Enter a name for the new branch.",V=500,P={edit:_.TREE_EDIT,delete:_.TREE_DELETE,"move-up":_.TREE_MOVE_UP,"move-down":_.TREE_MOVE_DOWN,cut:_.TREE_CUT,"paste-child":_.TREE_PASTE_CHILD},N={edit:`<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
2
  <path stroke="none" d="M0 0h24v24H0z" fill="none" />
3
3
  <path d="M7 7h-1a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-1" />
4
4
  <path d="M20.385 6.585a2.1 2.1 0 0 0 -2.97 -2.97l-8.415 8.385v3h3l8.385 -8.415z" />
@@ -45,7 +45,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
45
45
  focusable="false"
46
46
  >
47
47
  ${C(N[c])}
48
- </span>`}function H(c){return c.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")}function j({parentPath:c,segment:e,rootPath:t}){return e?!c||c===t?t.endsWith("/")?`${t}${e}`:`${t}/${e}`:c.endsWith("/")?`${c}${e}`:`${c}/${e}`:c||t}let n=class extends z{constructor(){super(...arguments),this.internals=this.attachInternals(),this.formCtrl=new F({host:this,internals:this.internals,getFormValue:()=>this.value?this.value:null,getValidity:()=>this.required&&!this.value?{flags:{valueMissing:!0},message:this.requiredMessage||"Please select an option."}:null,onReset:()=>{this.value=this._defaultValue},onRestore:e=>{this.value=e},onDisabled:e=>{this.disabled=e}}),this.nodes=[],this.value="",this.name="",this.required=!1,this.requiredMessage="",this.disabled=!1,this.allowGraft=!1,this.rootLabel="/",this.rootPath="/",this.rootSelectable=!0,this.label="",this.graftError=null,this.buildPath=j,this.normalizeGraftSegment=({raw:e})=>H(e),this.keepExpanded=!1,this.expandIcon="",this.collapseIcon="",this._itemsSlot=$(),this._graftInputRef=$(),this._graftDialogRef=$(),this._slottedIds=new WeakMap,this._nextSlottedId=0,this._model=[],this._rowIndex=new Map,this._pathIndex=new Map,this._slottedNodes=[],this._lazyLoaded=new Map,this._grafted=new Map,this._expanded=new Set,this._loading=new Set,this._generation=0,this._defaultValue="",this._pendingFocus=!1,this._pendingScrollId=null,this._pendingGraftFocus=!1,this._nextGraftId=0,this._typeBuffer="",this._typeTimer=null,this._activeId=null,this._selectedId=null,this._graftParentId=null,this._graftValue="",this._graftErrorInternal=null}willUpdate(e){super.willUpdate(e);const t=!this.hasUpdated||e.has("nodes")||e.has("rootLabel")||e.has("rootPath")||e.has("rootSelectable")||e.has("buildPath");t&&(this.hasUpdated&&this._generation++,this._rebuildModel(),this._reconcileState()),e.has("value")?this._reconcileSelectionFromValue():t&&this._reconcileSelectionFromModel(),e.has("value")&&this.hasUpdated&&this.formCtrl.syncValue(),(t||e.has("value"))&&this._revealSelectedValue()}firstUpdated(e){super.firstUpdated(e),this._defaultValue=this.value,this.childElementCount>0&&this._onSlotChange(),this.formCtrl.syncValueSilently()}updated(e){if(super.updated(e),this._pendingFocus&&this._activeId&&(this._pendingFocus=!1,this._rowEl(this._activeId)?.focus({preventScroll:!0})),this._pendingGraftFocus&&(this._pendingGraftFocus=!1,this._graftInputRef.value?.focus()),this._syncGuidePaths(),this._pendingScrollId){const t=this._pendingScrollId;this._pendingScrollId=null,this._scrollRowIntoView(t)}}formResetCallback(){this.formCtrl.handleFormReset()}formStateRestoreCallback(e){this.formCtrl.handleFormStateRestore(e)}formDisabledCallback(e){this.formCtrl.handleFormDisabled(e)}validate(){this.formCtrl.validate()}checkValidity(){return this.formCtrl.checkValidity()}focus(){if(this.disabled)return;const e=this._visibleRows(),t=this._currentActive(e);t&&this._rowEl(t)?.focus()}setGraftError(e){this.graftError=e&&e.length>0?e:null}_rebuildModel(){const e=this.nodes.length?this.nodes:this._slottedNodes,t=this.rootPath||"/",i={id:"__root",segment:"",label:this.rootLabel||t,selectable:this.rootSelectable,hasChildren:!0},s=new Set(e.map(l=>l.segment.toLowerCase())),o=(this._grafted.get(i.id)??[]).filter(l=>!s.has(l.segment.toLowerCase())),r=e===this._slottedNodes,a=[...this._buildChildren(e,t,2,!1,i,r),...this._buildChildren(o,t,2,!0,i,!0)];this._model=[{id:i.id,segment:i.segment,label:i.label??t,icon:"",selectable:this.rootSelectable,editable:!1,deletable:!1,movableUp:!1,movableDown:!1,cuttable:!1,pasteChildTarget:!1,path:t,parentPath:"",parentId:"",level:1,hasChildren:a.length>0,loaded:!0,ephemeral:!1,root:!0,actions:[],children:a}],this._expanded.add(i.id)}_build(e,t,i,s,o,r=!1){if(!this._isUsableNodeId(e.id,r))return null;const a=this._buildPath(t,e.segment,e,o),l=e.children??[],g=e.hasChildren??!1,v=this._lazyLoaded.has(e.id),b=v?this._lazyLoaded.get(e.id)??[]:[],w=new Set([...l,...b].map(f=>f.segment.toLowerCase())),y=(this._grafted.get(e.id)??[]).filter(f=>!w.has(f.segment.toLowerCase())),I=[...this._buildChildren(l,a,i+1,s,e,r),...this._buildChildren(b,a,i+1,s,e,!1),...this._buildChildren(y,a,i+1,!0,e,!0)],k=e.label||e.segment;return{id:e.id,segment:e.segment,data:e.data,label:k,icon:e.icon??"",selectable:e.selectable!==!1,editable:e.editable===!0,deletable:e.deletable===!0,movableUp:e.movableUp===!0,movableDown:e.movableDown===!0,cuttable:e.cuttable===!0,pasteChildTarget:e.pasteChildTarget===!0,path:a,parentPath:t,parentId:o?.id??"",level:i,hasChildren:g||I.length>0,loaded:!g||v,ephemeral:s,root:!1,actions:this._actionsFor(e,k),children:I}}_actionsFor(e,t){const i=[],s=(o,r,a)=>{o&&i.push({kind:r,label:a,accessibleLabel:`${a} ${t}`})};return s(e.editable===!0,"edit","Edit"),s(e.deletable===!0,"delete","Delete"),s(e.movableUp===!0,"move-up","Move up"),s(e.movableDown===!0,"move-down","Move down"),s(e.cuttable===!0,"cut","Cut"),e.pasteChildTarget===!0&&i.push({kind:"paste-child",label:"Paste as child",accessibleLabel:`Paste as child of ${t}`}),i}_buildChildren(e,t,i,s,o,r=!1){return e.flatMap(a=>{const l=this._build(a,t,i,s,o,r);return l?[l]:[]})}_isUsableNodeId(e,t){return e?!t&&this._isGeneratedId(e)?(console.warn(`esp-tree: reserved node id "${e}" ignored.`),!1):!0:(console.warn("esp-tree: node without a stable id ignored."),!1)}_isGeneratedId(e){return e==="__root"||e.startsWith("__slotted:")||e.startsWith("__graft:")}_reconcileState(){this._rowIndex.clear(),this._pathIndex.clear();const e=t=>{for(const i of t){if(this._rowIndex.has(i.id)){console.warn(`esp-tree: duplicate node id "${i.id}" ignored.`);continue}this._pathIndex.has(i.path)?console.warn(`esp-tree: duplicate path "${i.path}" for ids "${this._pathIndex.get(i.path).id}" and "${i.id}".`):this._pathIndex.set(i.path,i),this._rowIndex.set(i.id,i),e(i.children)}};e(this._model);for(const t of[...this._expanded])this._rowIndex.has(t)||this._expanded.delete(t);for(const t of[...this._lazyLoaded.keys()])this._rowIndex.has(t)||this._lazyLoaded.delete(t);for(const t of[...this._grafted.keys()])this._rowIndex.has(t)||this._grafted.delete(t);this._activeId&&!this._rowIndex.has(this._activeId)&&(this._activeId=null)}_revealSelectedValue(){if(!this.value&&!this._selectedId)return;let e=this._selectedId?this._rowIndex.get(this._selectedId):void 0;for(e??=this.value?this._pathIndex.get(this.value):void 0;e&&e.parentId;)this._expanded.add(e.parentId),e=this._rowIndex.get(e.parentId)}_reconcileSelectionFromValue(){if(this._selectedId){const e=this._findRowById(this._selectedId);if(e&&e.path===this.value)return}this._selectedId=this.value?this._findRow(this.value)?.id??null:null}_reconcileSelectionFromModel(){if(this._selectedId){const e=this._findRowById(this._selectedId);if(e){this.value!==e.path&&(this.value=e.path,this.hasUpdated&&this.formCtrl.syncValue(),this._emit(_.TREE_SELECT,{path:e.path,node:this._toPublic(e)}));return}this._selectedId=null}this.value&&(this._selectedId=this._findRow(this.value)?.id??null)}_buildPath(e,t,i,s){return this.buildPath({parentPath:e,segment:t,node:i,parentNode:s,rootPath:this.rootPath||"/"})}_joinPath(e,t){const i=e?this._findRow(e):null;return this._buildPath(e,t,{id:t,segment:t,label:t},i?this._toPublic(i):null)}_findRow(e){return this._pathIndex.get(e)??null}_findRowById(e){return this._rowIndex.get(e)??null}_isIndexedRow(e){return this._rowIndex.get(e.id)===e}_visibleRows(){const e=[],t=i=>{for(const s of i)this._isIndexedRow(s)&&(e.push(s),s.children.length&&this._expanded.has(s.id)&&t(s.children))};return t(this._model),e}_currentActive(e){return this._activeId&&e.some(t=>t.id===this._activeId)?this._activeId:e[0]?.id??null}_rowEl(e){const t=globalThis.CSS?.escape?.(e)??e.replace(/\\/g,"\\\\").replace(/"/g,'\\"');return this.shadowRoot?.querySelector(`[data-id="${t}"]`)??null}_scrollRowIntoView(e){this._rowEl(e)?.scrollIntoView?.({block:"nearest",inline:"nearest"})}_toPublic(e){return{id:e.id,segment:e.segment,data:e.data,label:e.label,icon:e.icon||void 0,selectable:e.selectable,editable:e.editable,deletable:e.deletable,movableUp:e.movableUp,movableDown:e.movableDown,cuttable:e.cuttable,pasteChildTarget:e.pasteChildTarget,hasChildren:e.hasChildren}}_emit(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t,bubbles:!0,composed:!0}))}_setExpanded(e,t){t!==this._expanded.has(e.id)&&(t?(this._expanded.add(e.id),e.hasChildren&&!e.loaded&&this._ensureLoaded(e)):this._expanded.delete(e.id),this._emit(_.TREE_TOGGLE,{path:e.path,node:this._toPublic(e),open:t}),this.requestUpdate())}_toggle(e){e.hasChildren&&this._setExpanded(e,!this._expanded.has(e.id))}async _ensureLoaded(e){if(e.loaded||!e.hasChildren||this._loading.has(e.id)||!this.loadChildren)return;const t=this._generation;this._loading.add(e.id),this.requestUpdate();try{const i=await this.loadChildren({id:e.id,segment:e.segment,data:e.data,label:e.label,path:e.path,level:e.level});if(t!==this._generation)return;this._lazyLoaded.set(e.id,i??[]),this._rebuildModel(),this._reconcileState(),this._reconcileSelectionFromModel(),this._pendingScrollId=e.id}catch{}finally{this._loading.delete(e.id),this.requestUpdate()}}_select(e){if(!e.selectable)return;e.hasChildren&&!this._expanded.has(e.id)&&this._setExpanded(e,!0);const t=this._isSelected(e)&&this.value===e.path;this._selectedId=e.id,!t&&(this.value=e.path,this._emit(_.TREE_SELECT,{path:e.path,node:this._toPublic(e)}))}_containsRow(e,t){if(e.id===t.id)return!0;for(;t.parentId;){if(t.parentId===e.id)return!0;const i=this._findRowById(t.parentId);if(!i)break;t=i}return!1}_collapseSelectedBranchOutside(e){if(this.keepExpanded||!this.value&&!this._selectedId||this._isSelected(e))return;const t=this._selectedId?this._findRowById(this._selectedId):this.value?this._findRow(this.value):null;!t?.hasChildren||!this._expanded.has(t.id)||this._containsRow(t,e)||this._setExpanded(t,!1)}_isSelected(e){return this._selectedId===e.id}_setActiveId(e){this._activeId=e,this._pendingFocus=!0,this._pendingScrollId=e}_setActiveRow(e){this._setActiveId(e.id)}_activateAndSelect(e){this._setActiveRow(e),this._collapseSelectedBranchOutside(e),this._select(e),this.requestUpdate()}_onRowClick(e){if(this.disabled)return;if(this._isSelected(e)&&e.selectable&&e.hasChildren){this._setActiveRow(e),this._toggle(e),this.requestUpdate();return}this._activateAndSelect(e)}_onToggleClick(e,t){e.stopPropagation(),!(this.disabled||!t.hasChildren)&&(this._setActiveRow(t),this._toggle(t),this.requestUpdate())}_edit(e){e.editable&&this._emit(T.edit,{path:e.path,node:this._toPublic(e)})}_onActionEvent(e,t,i){e.stopPropagation(),!this.disabled&&(this._activeId=t.id,this._emit(T[i],{path:t.path,node:this._toPublic(t)}),this.requestUpdate())}_onActionMenuSelect(e,t){e.stopPropagation();const i=e.detail.value;t.actions.some(s=>s.kind===i)&&this._onActionEvent(e,t,i)}_stopRowActionEvent(e){e.stopPropagation()}_onRowActionsKeydown(e,t){e.stopPropagation(),!e.defaultPrevented&&(e.key!=="ArrowLeft"&&e.key!=="Escape"||(e.preventDefault(),this._closeRowActionMenus(),this._focusRow(t)))}_onKeydown(e){if(this.disabled)return;const t=e.target;if(t instanceof HTMLElement&&t.closest("button, input, textarea, select, a[href], esp-button, esp-action-menu, esp-action-menu-item, .node-actions"))return;const i=this._visibleRows();if(!i.length)return;const s=this._currentActive(i);let o=i.findIndex(a=>a.id===s);o<0&&(o=0);const r=i[o];if(r){if(e.key==="F10"&&e.shiftKey||e.key==="ContextMenu"){if(r.actions.length===0)return;e.preventDefault(),this._focusRowActions(r);return}switch(e.key){case"ArrowDown":e.preventDefault(),this._moveActive(i[Math.min(o+1,i.length-1)]);break;case"ArrowUp":e.preventDefault(),this._moveActive(i[Math.max(o-1,0)]);break;case"Home":e.preventDefault(),this._moveActive(i[0]);break;case"End":e.preventDefault(),this._moveActive(i[i.length-1]);break;case"ArrowRight":if(e.preventDefault(),r.hasChildren&&!this._expanded.has(r.id)){this._setActiveRow(r),this._setExpanded(r,!0);break}if(r.actions.length>0&&this._focusRowActionTrigger(r))break;if(r.hasChildren&&this._expanded.has(r.id)){const a=i[o+1];if(a&&a.parentId===r.id){this._moveActive(a);break}}break;case"ArrowLeft":if(e.preventDefault(),r.hasChildren&&this._expanded.has(r.id))this._setActiveRow(r),this._setExpanded(r,!1);else if(r.parentId){const a=i.find(l=>l.id===r.parentId);a&&this._moveActive(a)}break;case"Enter":case" ":e.preventDefault(),this._activateAndSelect(r);break;case"F2":if(!r.editable)break;e.preventDefault(),this._setActiveRow(r),this._edit(r),this.requestUpdate();break;case"Insert":if(!this.allowGraft)break;e.preventDefault(),this._openGraftForRow(r);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),this._typeahead(e.key,i,o));break}}}_moveActive(e){e&&(this._closeRowActionMenus(),this._setActiveRow(e),this.requestUpdate())}_focusRow(e){this._closeRowActionMenus(),this._activeId=e.id,this._rowEl(e.id)?.focus({preventScroll:!0}),this.requestUpdate()}_closeRowActionMenus(){this.shadowRoot?.querySelectorAll("esp-action-menu.node-action-menu").forEach(e=>e.closeMenu?.({restoreFocus:!1}))}async _focusRowActions(e){const i=this._rowEl(e.id)?.querySelector(".node-actions");if(!i)return;if(e.actions.length===1){i.querySelector("esp-button")?.focus({preventScroll:!0});return}const s=i.querySelector("esp-action-menu");if(!s)return;s.openMenu(),await s.updateComplete;const o=s;if(typeof o.focusFirstItem=="function"){o.focusFirstItem();return}s.querySelector("esp-action-menu-item:not([disabled])")?.focus()}_focusRowActionTrigger(e){const i=this._rowEl(e.id)?.querySelector(".node-actions esp-button");return i?(i.focus({preventScroll:!0}),!0):!1}_typeahead(e,t,i){this._typeTimer&&clearTimeout(this._typeTimer),this._typeBuffer+=e.toLowerCase(),this._typeTimer=setTimeout(()=>{this._typeBuffer="",this._typeTimer=null},V);const s=t.length;for(let o=1;o<=s;o++){const r=t[(i+o)%s];if(r&&r.label.toLowerCase().startsWith(this._typeBuffer)){this._moveActive(r);return}}}_onSlotChange(){const e=Array.from(this.children).filter(t=>t instanceof P);this._slottedNodes=e.map(t=>this._parseItem(t)),this._generation++,this._rebuildModel(),this._reconcileState(),this.requestUpdate()}_parseItem(e){const t=this._idForSlottedItem(e),i=e.getAttribute("segment")??e.segment??"",s=e.getAttribute("label")??e.label,o=e.getAttribute("icon")??e.icon,r=e.getAttribute("selectable"),a=r===null?e.selectable:r!=="false",l=e.hasAttribute("editable")||e.editable,g=e.hasAttribute("deletable")||e.deletable,v=e.hasAttribute("movable-up")||e.movableUp,b=e.hasAttribute("movable-down")||e.movableDown,w=e.hasAttribute("cuttable")||e.cuttable,y=e.hasAttribute("paste-child-target")||e.pasteChildTarget,I=e.hasAttribute("has-children")||e.hasChildren,k=Array.from(e.children).filter(E=>E instanceof P),f={id:t,segment:i,data:e.data,selectable:a,hasChildren:I};s&&(f.label=s),o&&(f.icon=o),l&&(f.editable=!0),g&&(f.deletable=!0),v&&(f.movableUp=!0),b&&(f.movableDown=!0),w&&(f.cuttable=!0),y&&(f.pasteChildTarget=!0);const A=k.map(E=>this._parseItem(E));return A.length&&(f.children=A),f}_idForSlottedItem(e){const t=this._slottedIds.get(e);if(t)return t;if(e.id&&!this._isGeneratedId(e.id))return this._slottedIds.set(e,e.id),e.id;e.id&&console.warn(`esp-tree: reserved slotted item id "${e.id}" ignored.`);const i=`__slotted:${++this._nextSlottedId}`;return this._slottedIds.set(e,i),i}_onGraftAddClick(e,t){e.stopPropagation(),!this.disabled&&this._openGraftForRow(t)}_openGraftForRow(e){if(this.disabled)return;const t=e??this._model[0]??null,i=this._rowPath(t);this._requestGraft(t,i)&&(t&&(this._setActiveRow(t),this._expanded.add(t.id)),this._openGraft(t?.id??"__root"))}_requestGraft(e,t){return this.dispatchEvent(new CustomEvent(_.TREE_GRAFT_REQUEST,{detail:{parentPath:t,parentNode:e?this._toPublic(e):null},bubbles:!0,composed:!0,cancelable:!0}))}_openGraft(e){this._graftParentId=e,this._graftValue="",this._graftErrorInternal=null,this.graftError=null,this._pendingGraftFocus=!0,this.requestUpdate(),this.updateComplete.then(()=>{if(this._graftParentId!==e)return;const t=this._graftDialogRef.value;t&&!t.hasAttribute("is-open")&&t.toggleOpen()})}_cancelGraft(){this._closeGraftDialog(),this._resetGraftState()}_closeGraftDialog(){const e=this._graftDialogRef.value;e?.hasAttribute("is-open")&&e.toggleOpen()}_resetGraftState(){this._graftParentId=null,this._graftValue="",this._graftErrorInternal=null,this.graftError=null}_onGraftDialogClosed(){this._resetGraftState()}_onGraftInput(e){const t=e.detail??e.target.value;this._graftValue=t;const i=this._graftParentPath(),s=this._normalizeGraft(t,i),{valid:o,reason:r}=this._validateSegment(i,s);this._graftErrorInternal=null,this._emit(_.TREE_GRAFT_INPUT,{parentPath:i,raw:t,segment:s,path:this._joinPath(i,s),valid:o,reason:r})}_onGraftKeydown(e){if(e.stopPropagation(),e.key==="Enter")e.preventDefault(),this._commitGraft();else if(e.key==="Escape"){e.preventDefault();const t=this._graftParentId;this._cancelGraft(),t&&this._moveActive(this._findRowById(t)??void 0)}}_commitGraft(){if(this.disabled)return;const e=this._graftParentRow(),t=e?.id??"__root",i=this._rowPath(e),s=this._normalizeGraft(this._graftValue,i),{valid:o,reason:r}=this._validateSegment(i,s);if(!o){this._graftErrorInternal=r==="duplicate"?B:q;return}if(this.graftError)return;const a=this._joinPath(i,s),l={id:`__graft:${++this._nextGraftId}`,segment:s,label:s,selectable:!0},g=this._grafted.get(t)??[];g.push(l),this._grafted.set(t,g),this._rebuildModel(),this._reconcileState(),e&&this._expanded.add(e.id),this._closeGraftDialog(),this._resetGraftState(),this.value=a,this._selectedId=l.id,this._setActiveId(l.id);const v=this._findRowById(l.id),b=v?this._toPublic(v):l;this._emit(_.TREE_GRAFT,{parentPath:i,segment:s,path:a,node:b}),this._emit(_.TREE_SELECT,{path:a,node:b}),this.requestUpdate()}_validateSegment(e,t){return t?this._siblingSegments(e).has(t.toLowerCase())?{valid:!1,reason:"duplicate"}:{valid:!0,reason:""}:{valid:!1,reason:"empty"}}_siblingSegments(e){const t=e?this._findRow(e)?.children??[]:this._model;return new Set(t.map(i=>i.segment.toLowerCase()))}_normalizeGraft(e,t){return this.normalizeGraftSegment({raw:e,parentPath:t})}_graftParentPath(){return this._rowPath(this._graftParentRow())}_graftParentRow(){return this._graftParentId?this._findRowById(this._graftParentId)??this._model[0]??null:this._model[0]??null}_rowPath(e){return e?.path??(this.rootPath||"/")}render(){const e=this._visibleRows(),t=this._currentActive(e);return p`
48
+ </span>`}function H(c){return c.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")}function j({parentPath:c,segment:e,rootPath:t}){return e?!c||c===t?t.endsWith("/")?`${t}${e}`:`${t}/${e}`:c.endsWith("/")?`${c}${e}`:`${c}/${e}`:c||t}let n=class extends z{constructor(){super(...arguments),this.internals=this.attachInternals(),this.formCtrl=new F({host:this,internals:this.internals,getFormValue:()=>this.value?this.value:null,getValidity:()=>this.required&&!this.value?{flags:{valueMissing:!0},message:this.requiredMessage||"Please select an option."}:null,onReset:()=>{this.value=this._defaultValue},onRestore:e=>{this.value=e},onDisabled:e=>{this.disabled=e}}),this.nodes=[],this.value="",this.name="",this.required=!1,this.requiredMessage="",this.disabled=!1,this.allowGraft=!1,this.rootLabel="/",this.rootPath="/",this.rootSelectable=!0,this.label="",this.graftError=null,this.buildPath=j,this.normalizeGraftSegment=({raw:e})=>H(e),this.keepExpanded=!1,this.expandIcon="",this.collapseIcon="",this._itemsSlot=$(),this._graftInputRef=$(),this._graftDialogRef=$(),this._slottedIds=new WeakMap,this._nextSlottedId=0,this._model=[],this._rowIndex=new Map,this._pathIndex=new Map,this._slottedNodes=[],this._lazyLoaded=new Map,this._grafted=new Map,this._expanded=new Set,this._loading=new Set,this._generation=0,this._defaultValue="",this._pendingFocus=!1,this._pendingScrollId=null,this._pendingGraftFocus=!1,this._nextGraftId=0,this._typeBuffer="",this._typeTimer=null,this._activeId=null,this._selectedId=null,this._graftParentId=null,this._graftValue="",this._graftErrorInternal=null}willUpdate(e){super.willUpdate(e);const t=!this.hasUpdated||e.has("nodes")||e.has("rootLabel")||e.has("rootPath")||e.has("rootSelectable")||e.has("buildPath");t&&(this.hasUpdated&&this._generation++,this._rebuildModel(),this._reconcileState()),this._graftParentId!==null&&(this.disabled||!this._canGraft(this._graftParentRow()))&&this._cancelGraft(),e.has("value")?this._reconcileSelectionFromValue():t&&this._reconcileSelectionFromModel(),e.has("value")&&this.hasUpdated&&this.formCtrl.syncValue(),(t||e.has("value"))&&this._revealSelectedValue()}firstUpdated(e){super.firstUpdated(e),this._defaultValue=this.value,this.childElementCount>0&&this._onSlotChange(),this.formCtrl.syncValueSilently()}updated(e){if(super.updated(e),this._pendingFocus&&this._activeId&&(this._pendingFocus=!1,this._rowEl(this._activeId)?.focus({preventScroll:!0})),this._pendingGraftFocus&&(this._pendingGraftFocus=!1,this._graftInputRef.value?.focus()),this._syncGuidePaths(),this._pendingScrollId){const t=this._pendingScrollId;this._pendingScrollId=null,this._scrollRowIntoView(t)}}formResetCallback(){this.formCtrl.handleFormReset()}formStateRestoreCallback(e){this.formCtrl.handleFormStateRestore(e)}formDisabledCallback(e){this.formCtrl.handleFormDisabled(e)}validate(){this.formCtrl.validate()}checkValidity(){return this.formCtrl.checkValidity()}focus(){if(this.disabled)return;const e=this._visibleRows(),t=this._currentActive(e);t&&this._rowEl(t)?.focus()}setGraftError(e){this.graftError=e&&e.length>0?e:null}_rebuildModel(){const e=this.nodes.length?this.nodes:this._slottedNodes,t=this.rootPath||"/",i={id:"__root",segment:"",label:this.rootLabel||t,selectable:this.rootSelectable,hasChildren:!0},s=new Set(e.map(l=>l.segment.toLowerCase())),a=(this._grafted.get(i.id)??[]).filter(l=>!s.has(l.segment.toLowerCase())),r=e===this._slottedNodes,o=[...this._buildChildren(e,t,2,!1,i,r),...this._buildChildren(a,t,2,!0,i,!0)];this._model=[{id:i.id,segment:i.segment,label:i.label??t,icon:"",selectable:this.rootSelectable,editable:!1,deletable:!1,movableUp:!1,movableDown:!1,cuttable:!1,pasteChildTarget:!1,graftTarget:!0,path:t,parentPath:"",parentId:"",level:1,hasChildren:o.length>0,loaded:!0,ephemeral:!1,root:!0,actions:[],children:o}],this._expanded.add(i.id)}_build(e,t,i,s,a,r=!1){if(!this._isUsableNodeId(e.id,r))return null;const o=this._buildPath(t,e.segment,e,a),l=e.children??[],g=e.hasChildren??!1,v=this._lazyLoaded.has(e.id),b=v?this._lazyLoaded.get(e.id)??[]:[],w=new Set([...l,...b].map(f=>f.segment.toLowerCase())),y=(this._grafted.get(e.id)??[]).filter(f=>!w.has(f.segment.toLowerCase())),I=[...this._buildChildren(l,o,i+1,s,e,r),...this._buildChildren(b,o,i+1,s,e,!1),...this._buildChildren(y,o,i+1,!0,e,!0)],k=e.label||e.segment;return{id:e.id,segment:e.segment,data:e.data,label:k,icon:e.icon??"",selectable:e.selectable!==!1,editable:e.editable===!0,deletable:e.deletable===!0,movableUp:e.movableUp===!0,movableDown:e.movableDown===!0,cuttable:e.cuttable===!0,pasteChildTarget:e.pasteChildTarget===!0,graftTarget:e.graftTarget!==!1,path:o,parentPath:t,parentId:a?.id??"",level:i,hasChildren:g||I.length>0,loaded:!g||v,ephemeral:s,root:!1,actions:this._actionsFor(e,k),children:I}}_actionsFor(e,t){const i=[],s=(a,r,o)=>{a&&i.push({kind:r,label:o,accessibleLabel:`${o} ${t}`})};return s(e.editable===!0,"edit","Edit"),s(e.deletable===!0,"delete","Delete"),s(e.movableUp===!0,"move-up","Move up"),s(e.movableDown===!0,"move-down","Move down"),s(e.cuttable===!0,"cut","Cut"),e.pasteChildTarget===!0&&i.push({kind:"paste-child",label:"Paste as child",accessibleLabel:`Paste as child of ${t}`}),i}_buildChildren(e,t,i,s,a,r=!1){return e.flatMap(o=>{const l=this._build(o,t,i,s,a,r);return l?[l]:[]})}_isUsableNodeId(e,t){return e?!t&&this._isGeneratedId(e)?(console.warn(`esp-tree: reserved node id "${e}" ignored.`),!1):!0:(console.warn("esp-tree: node without a stable id ignored."),!1)}_isGeneratedId(e){return e==="__root"||e.startsWith("__slotted:")||e.startsWith("__graft:")}_reconcileState(){this._rowIndex.clear(),this._pathIndex.clear();const e=t=>{for(const i of t){if(this._rowIndex.has(i.id)){console.warn(`esp-tree: duplicate node id "${i.id}" ignored.`);continue}this._pathIndex.has(i.path)?console.warn(`esp-tree: duplicate path "${i.path}" for ids "${this._pathIndex.get(i.path).id}" and "${i.id}".`):this._pathIndex.set(i.path,i),this._rowIndex.set(i.id,i),e(i.children)}};e(this._model);for(const t of[...this._expanded])this._rowIndex.has(t)||this._expanded.delete(t);for(const t of[...this._lazyLoaded.keys()])this._rowIndex.has(t)||this._lazyLoaded.delete(t);for(const t of[...this._grafted.keys()])this._rowIndex.has(t)||this._grafted.delete(t);this._activeId&&!this._rowIndex.has(this._activeId)&&(this._activeId=null)}_revealSelectedValue(){if(!this.value&&!this._selectedId)return;let e=this._selectedId?this._rowIndex.get(this._selectedId):void 0;for(e??=this.value?this._pathIndex.get(this.value):void 0;e&&e.parentId;)this._expanded.add(e.parentId),e=this._rowIndex.get(e.parentId)}_reconcileSelectionFromValue(){if(this._selectedId){const e=this._findRowById(this._selectedId);if(e&&e.path===this.value)return}this._selectedId=this.value?this._findRow(this.value)?.id??null:null}_reconcileSelectionFromModel(){if(this._selectedId){const e=this._findRowById(this._selectedId);if(e){this.value!==e.path&&(this.value=e.path,this.hasUpdated&&this.formCtrl.syncValue(),this._emit(_.TREE_SELECT,{path:e.path,node:this._toPublic(e)}));return}this._selectedId=null}this.value&&(this._selectedId=this._findRow(this.value)?.id??null)}_buildPath(e,t,i,s){return this.buildPath({parentPath:e,segment:t,node:i,parentNode:s,rootPath:this.rootPath||"/"})}_joinPath(e,t){const i=e?this._findRow(e):null;return this._buildPath(e,t,{id:t,segment:t,label:t},i?this._toPublic(i):null)}_findRow(e){return this._pathIndex.get(e)??null}_findRowById(e){return this._rowIndex.get(e)??null}_isIndexedRow(e){return this._rowIndex.get(e.id)===e}_visibleRows(){const e=[],t=i=>{for(const s of i)this._isIndexedRow(s)&&(e.push(s),s.children.length&&this._expanded.has(s.id)&&t(s.children))};return t(this._model),e}_currentActive(e){return this._activeId&&e.some(t=>t.id===this._activeId)?this._activeId:e[0]?.id??null}_rowEl(e){const t=globalThis.CSS?.escape?.(e)??e.replace(/\\/g,"\\\\").replace(/"/g,'\\"');return this.shadowRoot?.querySelector(`[data-id="${t}"]`)??null}_scrollRowIntoView(e){this._rowEl(e)?.scrollIntoView?.({block:"nearest",inline:"nearest"})}_toPublic(e){return{id:e.id,segment:e.segment,data:e.data,label:e.label,icon:e.icon||void 0,selectable:e.selectable,editable:e.editable,deletable:e.deletable,movableUp:e.movableUp,movableDown:e.movableDown,cuttable:e.cuttable,pasteChildTarget:e.pasteChildTarget,graftTarget:e.graftTarget,hasChildren:e.hasChildren}}_emit(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t,bubbles:!0,composed:!0}))}_setExpanded(e,t){t!==this._expanded.has(e.id)&&(t?(this._expanded.add(e.id),e.hasChildren&&!e.loaded&&this._ensureLoaded(e)):this._expanded.delete(e.id),this._emit(_.TREE_TOGGLE,{path:e.path,node:this._toPublic(e),open:t}),this.requestUpdate())}_toggle(e){e.hasChildren&&this._setExpanded(e,!this._expanded.has(e.id))}async _ensureLoaded(e){if(e.loaded||!e.hasChildren||this._loading.has(e.id)||!this.loadChildren)return;const t=this._generation;this._loading.add(e.id),this.requestUpdate();try{const i=await this.loadChildren({id:e.id,segment:e.segment,data:e.data,label:e.label,path:e.path,level:e.level});if(t!==this._generation)return;this._lazyLoaded.set(e.id,i??[]),this._rebuildModel(),this._reconcileState(),this._reconcileSelectionFromModel(),this._pendingScrollId=e.id}catch{}finally{this._loading.delete(e.id),this.requestUpdate()}}_select(e){if(!e.selectable)return;e.hasChildren&&!this._expanded.has(e.id)&&this._setExpanded(e,!0);const t=this._isSelected(e)&&this.value===e.path;this._selectedId=e.id,!t&&(this.value=e.path,this._emit(_.TREE_SELECT,{path:e.path,node:this._toPublic(e)}))}_containsRow(e,t){if(e.id===t.id)return!0;for(;t.parentId;){if(t.parentId===e.id)return!0;const i=this._findRowById(t.parentId);if(!i)break;t=i}return!1}_collapseSelectedBranchOutside(e){if(this.keepExpanded||!this.value&&!this._selectedId||this._isSelected(e))return;const t=this._selectedId?this._findRowById(this._selectedId):this.value?this._findRow(this.value):null;!t?.hasChildren||!this._expanded.has(t.id)||this._containsRow(t,e)||this._setExpanded(t,!1)}_isSelected(e){return this._selectedId===e.id}_setActiveId(e){this._activeId=e,this._pendingFocus=!0,this._pendingScrollId=e}_setActiveRow(e){this._setActiveId(e.id)}_activateAndSelect(e){this._setActiveRow(e),this._collapseSelectedBranchOutside(e),this._select(e),this.requestUpdate()}_onRowClick(e){if(this.disabled)return;if(this._isSelected(e)&&e.selectable&&e.hasChildren){this._setActiveRow(e),this._toggle(e),this.requestUpdate();return}this._activateAndSelect(e)}_onToggleClick(e,t){e.stopPropagation(),!(this.disabled||!t.hasChildren)&&(this._setActiveRow(t),this._toggle(t),this.requestUpdate())}_edit(e){e.editable&&this._emit(P.edit,{path:e.path,node:this._toPublic(e)})}_onActionEvent(e,t,i){e.stopPropagation(),!this.disabled&&(this._activeId=t.id,this._emit(P[i],{path:t.path,node:this._toPublic(t)}),this.requestUpdate())}_onActionMenuSelect(e,t){e.stopPropagation();const i=e.detail.value;t.actions.some(s=>s.kind===i)&&this._onActionEvent(e,t,i)}_stopRowActionEvent(e){e.stopPropagation()}_onRowActionsKeydown(e,t){e.stopPropagation(),!e.defaultPrevented&&(e.key!=="ArrowLeft"&&e.key!=="Escape"||(e.preventDefault(),this._closeRowActionMenus(),this._focusRow(t)))}_onKeydown(e){if(this.disabled)return;const t=e.target;if(t instanceof HTMLElement&&t.closest("button, input, textarea, select, a[href], esp-button, esp-action-menu, esp-action-menu-item, .node-actions"))return;const i=this._visibleRows();if(!i.length)return;const s=this._currentActive(i);let a=i.findIndex(o=>o.id===s);a<0&&(a=0);const r=i[a];if(r){if(e.key==="F10"&&e.shiftKey||e.key==="ContextMenu"){if(r.actions.length===0)return;e.preventDefault(),this._focusRowActions(r);return}switch(e.key){case"ArrowDown":e.preventDefault(),this._moveActive(i[Math.min(a+1,i.length-1)]);break;case"ArrowUp":e.preventDefault(),this._moveActive(i[Math.max(a-1,0)]);break;case"Home":e.preventDefault(),this._moveActive(i[0]);break;case"End":e.preventDefault(),this._moveActive(i[i.length-1]);break;case"ArrowRight":if(e.preventDefault(),r.hasChildren&&!this._expanded.has(r.id)){this._setActiveRow(r),this._setExpanded(r,!0);break}if(r.actions.length>0&&this._focusRowActionTrigger(r))break;if(r.hasChildren&&this._expanded.has(r.id)){const o=i[a+1];if(o&&o.parentId===r.id){this._moveActive(o);break}}break;case"ArrowLeft":if(e.preventDefault(),r.hasChildren&&this._expanded.has(r.id))this._setActiveRow(r),this._setExpanded(r,!1);else if(r.parentId){const o=i.find(l=>l.id===r.parentId);o&&this._moveActive(o)}break;case"Enter":case" ":e.preventDefault(),this._activateAndSelect(r);break;case"F2":if(!r.editable)break;e.preventDefault(),this._setActiveRow(r),this._edit(r),this.requestUpdate();break;case"Insert":if(!this._canGraft(r))break;e.preventDefault(),this._openGraftForRow(r);break;default:e.key.length===1&&!e.ctrlKey&&!e.metaKey&&!e.altKey&&(e.preventDefault(),this._typeahead(e.key,i,a));break}}}_moveActive(e){e&&(this._closeRowActionMenus(),this._setActiveRow(e),this.requestUpdate())}_focusRow(e){this._closeRowActionMenus(),this._activeId=e.id,this._rowEl(e.id)?.focus({preventScroll:!0}),this.requestUpdate()}_closeRowActionMenus(){this.shadowRoot?.querySelectorAll("esp-action-menu.node-action-menu").forEach(e=>e.closeMenu?.({restoreFocus:!1}))}async _focusRowActions(e){const i=this._rowEl(e.id)?.querySelector(".node-actions");if(!i)return;if(e.actions.length===1){i.querySelector("esp-button")?.focus({preventScroll:!0});return}const s=i.querySelector("esp-action-menu");if(!s)return;s.openMenu(),await s.updateComplete;const a=s;if(typeof a.focusFirstItem=="function"){a.focusFirstItem();return}s.querySelector("esp-action-menu-item:not([disabled])")?.focus()}_focusRowActionTrigger(e){const i=this._rowEl(e.id)?.querySelector(".node-actions esp-button");return i?(i.focus({preventScroll:!0}),!0):!1}_typeahead(e,t,i){this._typeTimer&&clearTimeout(this._typeTimer),this._typeBuffer+=e.toLowerCase(),this._typeTimer=setTimeout(()=>{this._typeBuffer="",this._typeTimer=null},V);const s=t.length;for(let a=1;a<=s;a++){const r=t[(i+a)%s];if(r&&r.label.toLowerCase().startsWith(this._typeBuffer)){this._moveActive(r);return}}}_onSlotChange(){const e=Array.from(this.children).filter(t=>t instanceof T);this._slottedNodes=e.map(t=>this._parseItem(t)),this._generation++,this._rebuildModel(),this._reconcileState(),this.requestUpdate()}_parseItem(e){const t=this._idForSlottedItem(e),i=e.getAttribute("segment")??e.segment??"",s=e.getAttribute("label")??e.label,a=e.getAttribute("icon")??e.icon,r=e.getAttribute("selectable"),o=r===null?e.selectable:r!=="false",l=e.hasAttribute("editable")||e.editable,g=e.hasAttribute("deletable")||e.deletable,v=e.hasAttribute("movable-up")||e.movableUp,b=e.hasAttribute("movable-down")||e.movableDown,w=e.hasAttribute("cuttable")||e.cuttable,y=e.hasAttribute("paste-child-target")||e.pasteChildTarget,I=e.hasAttribute("has-children")||e.hasChildren,k=Array.from(e.children).filter(E=>E instanceof T),f={id:t,segment:i,data:e.data,selectable:o,hasChildren:I};s&&(f.label=s),a&&(f.icon=a),l&&(f.editable=!0),g&&(f.deletable=!0),v&&(f.movableUp=!0),b&&(f.movableDown=!0),w&&(f.cuttable=!0),y&&(f.pasteChildTarget=!0);const A=k.map(E=>this._parseItem(E));return A.length&&(f.children=A),f}_idForSlottedItem(e){const t=this._slottedIds.get(e);if(t)return t;if(e.id&&!this._isGeneratedId(e.id))return this._slottedIds.set(e,e.id),e.id;e.id&&console.warn(`esp-tree: reserved slotted item id "${e.id}" ignored.`);const i=`__slotted:${++this._nextSlottedId}`;return this._slottedIds.set(e,i),i}_canGraft(e){return this.allowGraft&&e!==null&&e.graftTarget}_onGraftAddClick(e,t){e.stopPropagation(),!this.disabled&&this._openGraftForRow(this._findRowById(t))}_openGraftForRow(e){if(this.disabled)return;const t=e?this._findRowById(e.id):null;if(!this._canGraft(t))return;const i=this._rowPath(t);this._requestGraft(t,i)&&(this._setActiveRow(t),this._expanded.add(t.id),this._openGraft(t.id))}_requestGraft(e,t){return this.dispatchEvent(new CustomEvent(_.TREE_GRAFT_REQUEST,{detail:{parentPath:t,parentNode:e?this._toPublic(e):null},bubbles:!0,composed:!0,cancelable:!0}))}_openGraft(e){this._graftParentId=e,this._graftValue="",this._graftErrorInternal=null,this.graftError=null,this._pendingGraftFocus=!0,this.requestUpdate(),this.updateComplete.then(()=>{if(this._graftParentId!==e)return;const t=this._graftDialogRef.value;t&&!t.hasAttribute("is-open")&&t.toggleOpen()})}_cancelGraft(){this._closeGraftDialog(),this._resetGraftState()}_closeGraftDialog(){const e=this._graftDialogRef.value;e?.hasAttribute("is-open")&&e.toggleOpen()}_resetGraftState(){this._graftParentId=null,this._graftValue="",this._graftErrorInternal=null,this.graftError=null}_onGraftDialogClosed(){this._resetGraftState()}_onGraftInput(e){const t=e.detail??e.target.value;this._graftValue=t;const i=this._graftParentPath(),s=this._normalizeGraft(t,i),{valid:a,reason:r}=this._validateSegment(i,s);this._graftErrorInternal=null,this._emit(_.TREE_GRAFT_INPUT,{parentPath:i,raw:t,segment:s,path:this._joinPath(i,s),valid:a,reason:r})}_onGraftKeydown(e){if(e.stopPropagation(),e.key==="Enter")e.preventDefault(),this._commitGraft();else if(e.key==="Escape"){e.preventDefault();const t=this._graftParentId;this._cancelGraft(),t&&this._moveActive(this._findRowById(t)??void 0)}}_commitGraft(){if(this.disabled)return;const e=this._graftParentRow();if(!this._canGraft(e))return;const t=e.id,i=this._rowPath(e),s=this._normalizeGraft(this._graftValue,i),{valid:a,reason:r}=this._validateSegment(i,s);if(!a){this._graftErrorInternal=r==="duplicate"?B:q;return}if(this.graftError)return;const o=this._joinPath(i,s),l={id:`__graft:${++this._nextGraftId}`,segment:s,label:s,selectable:!0},g=this._grafted.get(t)??[];g.push(l),this._grafted.set(t,g),this._rebuildModel(),this._reconcileState(),e&&this._expanded.add(e.id),this._closeGraftDialog(),this._resetGraftState(),this.value=o,this._selectedId=l.id,this._setActiveId(l.id);const v=this._findRowById(l.id),b=v?this._toPublic(v):l;this._emit(_.TREE_GRAFT,{parentPath:i,segment:s,path:o,node:b}),this._emit(_.TREE_SELECT,{path:o,node:b}),this.requestUpdate()}_validateSegment(e,t){return t?this._siblingSegments(e).has(t.toLowerCase())?{valid:!1,reason:"duplicate"}:{valid:!0,reason:""}:{valid:!1,reason:"empty"}}_siblingSegments(e){const t=e?this._findRow(e)?.children??[]:this._model;return new Set(t.map(i=>i.segment.toLowerCase()))}_normalizeGraft(e,t){return this.normalizeGraftSegment({raw:e,parentPath:t})}_graftParentPath(){return this._rowPath(this._graftParentRow())}_graftParentRow(){return this._graftParentId?this._findRowById(this._graftParentId):null}_rowPath(e){return e?.path??(this.rootPath||"/")}render(){const e=this._visibleRows(),t=this._currentActive(e);return p`
49
49
  <div
50
50
  class="tree"
51
51
  role="tree"
@@ -67,7 +67,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
67
67
  >
68
68
  <path></path>
69
69
  </svg>
70
- `}_syncGuidePaths(){const e=this.shadowRoot;if(e)for(const t of e.querySelectorAll(".children"))this._syncGuidePath(t)}_syncGuidePath(e){const t=Array.from(e.children).find(g=>g.classList.contains("branch-guide")),i=t?.querySelector("path");if(!t||!i)return;const s=e.getBoundingClientRect(),o=Number.parseFloat(getComputedStyle(e).paddingInlineStart)||24,r=[];let a=o;for(const g of Array.from(e.children)){if(g.classList.contains("branch-guide"))continue;const v=this._guideTargetFor(g);if(!v)continue;const b=v.getBoundingClientRect(),w=b.left-s.left;Number.isFinite(w)&&w>0&&(a=w);const y=b.top-s.top+b.height/2;Number.isFinite(y)&&r.push(y)}const l=Math.max(s.height,r[r.length-1]??0,1);if(t.setAttribute("viewBox",`0 0 ${this._round(a)} ${this._round(l)}`),!r.length){i.removeAttribute("d");return}i.setAttribute("d",this._guidePath(r,a))}_guideTargetFor(e){return e.classList.contains("treeitem")?Array.from(e.children).find(i=>i.classList.contains("row-shell"))?.querySelector(".row")??null:e.classList.contains("graft-insert")?e.querySelector(".graft-add"):null}_guidePath(e,t){const i=this._round(Math.max(t,1)),s=this._round(e[e.length-1]??0),o=this._round(Math.max(0,Math.min(6,i/2,s))),r=["M 0 0"];for(const a of e.slice(0,-1)){const l=this._round(a);r.push(`V ${l}`,`H ${i}`,"H 0")}return o>0&&s>o?r.push(`V ${this._round(s-o)}`,`Q 0 ${s} ${o} ${s}`,`H ${i}`):r.push(`V ${s}`,`H ${i}`),r.join(" ")}_round(e){return Math.round(e*100)/100}_renderToggleAction(e,t){if(!e.hasChildren)return p`<span
70
+ `}_syncGuidePaths(){const e=this.shadowRoot;if(e)for(const t of e.querySelectorAll(".children"))this._syncGuidePath(t)}_syncGuidePath(e){const t=Array.from(e.children).find(g=>g.classList.contains("branch-guide")),i=t?.querySelector("path");if(!t||!i)return;const s=e.getBoundingClientRect(),a=Number.parseFloat(getComputedStyle(e).paddingInlineStart)||24,r=[];let o=a;for(const g of Array.from(e.children)){if(g.classList.contains("branch-guide"))continue;const v=this._guideTargetFor(g);if(!v)continue;const b=v.getBoundingClientRect(),w=b.left-s.left;Number.isFinite(w)&&w>0&&(o=w);const y=b.top-s.top+b.height/2;Number.isFinite(y)&&r.push(y)}const l=Math.max(s.height,r[r.length-1]??0,1);if(t.setAttribute("viewBox",`0 0 ${this._round(o)} ${this._round(l)}`),!r.length){i.removeAttribute("d");return}i.setAttribute("d",this._guidePath(r,o))}_guideTargetFor(e){return e.classList.contains("treeitem")?Array.from(e.children).find(i=>i.classList.contains("row-shell"))?.querySelector(".row")??null:e.classList.contains("graft-insert")?e.querySelector(".graft-add"):null}_guidePath(e,t){const i=this._round(Math.max(t,1)),s=this._round(e[e.length-1]??0),a=this._round(Math.max(0,Math.min(6,i/2,s))),r=["M 0 0"];for(const o of e.slice(0,-1)){const l=this._round(o);r.push(`V ${l}`,`H ${i}`,"H 0")}return a>0&&s>a?r.push(`V ${this._round(s-a)}`,`Q 0 ${s} ${a} ${s}`,`H ${i}`):r.push(`V ${s}`,`H ${i}`),r.join(" ")}_round(e){return Math.round(e*100)/100}_renderToggleAction(e,t){if(!e.hasChildren)return p`<span
71
71
  class=${m({"icon-action":!0,"toggle-action":!0,placeholder:!0})}
72
72
  aria-hidden="true"
73
73
  ></span>`;const i=t?`Collapse ${e.label}`:`Expand ${e.label}`,s=t&&this.collapseIcon?this.collapseIcon:this.expandIcon;return p`
@@ -79,7 +79,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
79
79
  aria-expanded=${String(t)}
80
80
  ?disabled=${this.disabled}
81
81
  tabindex="-1"
82
- @click=${o=>this._onToggleClick(o,e)}
82
+ @click=${a=>this._onToggleClick(a,e)}
83
83
  >
84
84
  <span class="toggle-icon" aria-hidden="true">${this._renderToggleIcon(s)}</span>
85
85
  </button>
@@ -143,7 +143,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
143
143
  </esp-action-menu-item>
144
144
  `)}
145
145
  </esp-action-menu>
146
- `}_renderRow(e,t){const i=this._expanded.has(e.id),s=this._isSelected(e),o=t===e.id,r=this._loading.has(e.id),a={row:!0,selected:s,branch:e.hasChildren,unselectable:!e.selectable,ephemeral:e.ephemeral,root:e.root};return p`
146
+ `}_renderRow(e,t){const i=this._expanded.has(e.id),s=this._isSelected(e),a=t===e.id,r=this._loading.has(e.id),o={row:!0,selected:s,branch:e.hasChildren,unselectable:!e.selectable,ephemeral:e.ephemeral,root:e.root};return p`
147
147
  <div
148
148
  role="treeitem"
149
149
  class="treeitem"
@@ -154,10 +154,10 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
154
154
  aria-selected=${e.selectable?String(s):u}
155
155
  aria-expanded=${e.hasChildren?String(i):u}
156
156
  aria-busy=${r?"true":u}
157
- tabindex=${this.disabled?-1:o?0:-1}
157
+ tabindex=${this.disabled?-1:a?0:-1}
158
158
  >
159
159
  <div class="row-shell">
160
- <div class=${m(a)} @click=${()=>this._onRowClick(e)}>
160
+ <div class=${m(o)} @click=${()=>this._onRowClick(e)}>
161
161
  ${this._renderToggleAction(e,i)} ${this._renderNodeTools(e)}
162
162
  <span class="node-label">${e.label}</span>
163
163
  ${r?p`<span class="loading" aria-hidden="true"></span>`:u}
@@ -166,14 +166,14 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
166
166
  </div>
167
167
  ${this._renderChildGroup(e,t)}
168
168
  </div>
169
- `}_renderChildGroup(e,t){const i=this._expanded.has(e.id),s=i&&e.children.length>0,o=this.allowGraft&&(i||!e.hasChildren);return!s&&!o?u:p`
169
+ `}_renderChildGroup(e,t){const i=this._expanded.has(e.id),s=i&&e.children.length>0,a=this._canGraft(e)&&(i||!e.hasChildren);return!s&&!a?u:p`
170
170
  <div
171
171
  class=${m({children:!0,"add-only":!s})}
172
172
  role=${s?"group":"presentation"}
173
173
  >
174
174
  ${this._renderBranchGuide()}
175
175
  ${s?e.children.filter(r=>this._isIndexedRow(r)).map(r=>this._renderRow(r,t)):u}
176
- ${o?this._renderGraftAdd(e.id,`Add a branch under ${e.label}`,e.root):u}
176
+ ${a?this._renderGraftAdd(e.id,`Add a branch under ${e.label}`,e.root):u}
177
177
  </div>
178
178
  `}_renderGraftAdd(e,t,i){return p`
179
179
  <div
@@ -186,12 +186,12 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
186
186
  aria-label=${t}
187
187
  title=${t}
188
188
  ?disabled=${this.disabled}
189
- @click=${s=>this._onGraftAddClick(s,this._findRowById(e))}
189
+ @click=${s=>this._onGraftAddClick(s,e)}
190
190
  >
191
191
  ${this._renderGeneratedIcon("plus",{"generated-icon":!0,"add-icon":!0})}
192
192
  </button>
193
193
  </div>
194
- `}_renderGraftDialog(){const e=this._graftParentId!==null,t=this._graftParentRow(),i=this._rowPath(t),s=t?.label??(this.rootLabel||"Root"),o=this._graftValue,r=this._normalizeGraft(o,i),a=r?this._joinPath(i,r):this._joinPath(i,"..."),l=this.graftError??this._graftErrorInternal;return p`
194
+ `}_renderGraftDialog(){const e=this._graftParentId!==null,t=this._graftParentRow(),i=this._rowPath(t),s=t?.label??(this.rootLabel||"Root"),a=this._graftValue,r=this._normalizeGraft(a,i),o=r?this._joinPath(i,r):this._joinPath(i,"..."),l=this.graftError??this._graftErrorInternal;return p`
195
195
  <esp-dialog
196
196
  class="graft-dialog"
197
197
  ${S(this._graftDialogRef)}
@@ -209,7 +209,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
209
209
  </div>
210
210
  <div>
211
211
  <dt>New Path</dt>
212
- <dd>${a}</dd>
212
+ <dd>${o}</dd>
213
213
  </div>
214
214
  </dl>
215
215
  <esp-form-item class="graft-form-item" label="Branch name" .error=${l??""}>
@@ -222,7 +222,7 @@ var d=function(c,e,t,i){var s=arguments.length,o=s<3?e:i===null?i=Object.getOwnP
222
222
  @keydown=${this._onGraftKeydown}
223
223
  ></esp-input>
224
224
  </esp-form-item>
225
- ${r&&o!==r?p`<esp-info class="graft-hint" icon="info">
225
+ ${r&&a!==r?p`<esp-info class="graft-hint" icon="info">
226
226
  Normalized to <code>${r}</code>.
227
227
  </esp-info>`:u}
228
228
  <div class="graft-actions">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taprootio/espalier",
3
- "version": "6.10.0",
3
+ "version": "6.12.0",
4
4
  "packageManager": "bun@1.3.12",
5
5
  "description": "Espalier — a themeable, accessible, framework-agnostic, enterprise-grade design system built on web standards and love.",
6
6
  "customElements": "custom-elements.json",