@agile-team/mach-table 0.38.0 → 0.39.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.
- package/README.md +2 -2
- package/dist/adapter.cjs +1 -1
- package/dist/adapter.d.cts +6 -2
- package/dist/adapter.d.ts +6 -2
- package/dist/adapter.js +1 -1
- package/dist/analysis.cjs +1 -1
- package/dist/analysis.d.cts +2 -2
- package/dist/analysis.d.ts +2 -2
- package/dist/analysis.js +2 -2
- package/dist/{api-Bgcy7aRd.d.cts → api-ChEKWrvH.d.cts} +6 -1
- package/dist/{api-Bgcy7aRd.d.ts → api-ChEKWrvH.d.ts} +6 -1
- package/dist/{configuration-B5dSDN9d.d.ts → configuration-BPj5MIUw.d.ts} +1 -1
- package/dist/{configuration-BVWcsdGm.d.cts → configuration-CZm-aiTf.d.cts} +1 -1
- package/dist/import.d.cts +1 -1
- package/dist/import.d.ts +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +6 -6
- package/dist/multi-range.cjs +3 -3
- package/dist/multi-range.d.cts +1 -1
- package/dist/multi-range.d.ts +1 -1
- package/dist/multi-range.js +2 -2
- package/dist/ordering.cjs +1 -1
- package/dist/ordering.d.cts +1 -1
- package/dist/ordering.d.ts +1 -1
- package/dist/ordering.js +1 -1
- package/dist/preferences.d.cts +1 -1
- package/dist/preferences.d.ts +1 -1
- package/dist/server-side.d.cts +1 -1
- package/dist/server-side.d.ts +1 -1
- package/dist/{shared-DfLrRGYY.d.cts → shared-BIBSfPcs.d.cts} +1 -1
- package/dist/{shared-CLEc9O2p.d.ts → shared-Qnznvth3.d.ts} +1 -1
- package/dist/workbench.cjs +1 -1
- package/dist/workbench.d.cts +3 -3
- package/dist/workbench.d.ts +3 -3
- package/dist/workbench.js +1 -1
- package/dist/worker.cjs +1 -1
- package/dist/worker.d.cts +2 -2
- package/dist/worker.d.ts +2 -2
- package/dist/worker.js +1 -1
- package/package.json +2 -2
package/dist/multi-range.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)}var g=32,R=1e5;function
|
|
1
|
+
"use strict";var d=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var G=(n,t)=>{for(var r in t)d(n,r,{get:t[r],enumerable:!0})},C=(n,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of A(t))!E.call(n,i)&&i!==r&&d(n,i,{get:()=>t[i],enumerable:!(e=D(t,i))||e.enumerable});return n};var I=n=>C(d({},"__esModule",{value:!0}),n);var L={};G(L,{createMultiRangeSelection:()=>T});module.exports=I(L);var m=Symbol.for("mach-table.adapter.lifecycle.v1");function y(n,t,r){return n.isDestroyed()?"":n[m]?.readCell(t,r)??""}function w(n,t){n.isDestroyed()||n[m]?.writeInput(t)}var v=/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?$/;function x(n){return typeof n!="string"||n===""||v.test(n)?n:/^[\s\u0000-\u001f]*[=+\-@]/.test(n)?"'"+n:n}function M(n){return n==null?"":String(n)}function S(n,t){let r=t?x(n):n,e=M(r);return/["\t\n\r]/.test(e)?'"'+e.replace(/"/g,'""')+'"':e}function b(n,t=!0){return n.map(r=>r.map(e=>S(e,t)).join(" ")).join(`
|
|
2
|
+
`)}var g=32,R=1e5;function u(n){let t=[...new Map(n.map(r=>[JSON.stringify(r.range),r])).values()];if(t.length>g)throw new RangeError("At most 32 ranges are supported.");if(t.reduce((r,e)=>r+e.rows.length*e.columns.length,0)>R)throw new RangeError("A multi-range operation may affect at most 100000 cells.");return t}function F(n){let t=n.columns.getWorkbenchItems().filter(r=>r.visible);return["left",null,"right"].flatMap(r=>t.filter(e=>e.pinned===r).map(e=>e.colId))}function T(){return new f}var f=class{constructor(){this.key="multi-range";this.context=null;this.saved=[];this.e=0;this.r=null}setup(t){if(this.context)throw new Error("Create a separate multi-range feature for each grid.");if(!t.api.getOption("enableRangeSelection"))throw new Error("Multi-range selection requires enableRangeSelection: true.");this.context=t;let r=null;t.onCleanup(()=>{cancelAnimationFrame(this.e),this.e=0;try{r?.disconnect()}finally{this.o(),this.saved=[],this.context===t&&(this.context=null)}});let{root:e}=t,i=(o,s)=>{t.addManagedDomListener(e,o,a=>{try{s(a)}catch(c){a.preventDefault(),a.stopImmediatePropagation(),t.reportError(c,"multi-range")}},!0)};i("mousedown",o=>this.s(o)),i("keydown",o=>this.a(o)),i("copy",o=>this.i(o)),i("cut",o=>this.i(o,!0)),t.addEventListener("rangeSelectionChanged",()=>this.t()),r=new MutationObserver(()=>this.t()),r.observe(e,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["data-id","data-col-id"]}),t.addEventListener("modelUpdated",()=>this.clear())}snapshot(t){let r=this.context?.api;if(!r)return null;let e=F(r),i=e.indexOf(t.colId1),o=e.indexOf(t.colId2),s=Math.max(0,Math.min(t.row1,t.row2)),a=Math.min(r.rows.getCount()-1,Math.max(t.row1,t.row2));if(i<0||o<0||!Number.isInteger(s)||!Number.isInteger(a)||a<s)return null;let c=e.slice(Math.min(i,o),Math.max(i,o)+1);if((a-s+1)*c.length>R)throw new RangeError("A range may contain at most 100000 cells.");let h=[];for(let p=s;p<=a;p++){let l=r.rows.getAt(p);l?.data&&!l.isGroup&&!l.isDetail&&!l.loading&&h.push(l.id)}return{range:{row1:s,row2:a,colId1:e[Math.min(i,o)],colId2:e[Math.max(i,o)]},rows:h,columns:c}}n(){let t=this.context?.api.selection.getRange(),r=t?this.snapshot(t):null,e=r?[...this.saved,r]:[...this.saved];return u(e)}getRanges(){return this.n().map(t=>({...t.range}))}setRanges(t){if(!this.context)throw new Error("Install the multi-range feature before setting ranges.");if(t.length>g)throw new RangeError("At most 32 ranges are supported.");let r=u(t.map(e=>this.snapshot(e)).filter(e=>e!==null));this.context.api.selection.clearRange(),this.saved=r,this.t()}clear(){this.saved=[],this.context?.api.selection.clearRange(),this.o(),this.t()}getText(){let t=this.context?.api;return t?this.n().map(r=>b(r.rows.map(e=>r.columns.map(i=>y(t,e,i))))).join(`
|
|
3
3
|
|
|
4
|
-
`):""}clearValues(){let t=this.context?.api;if(!t)return;let r=new Map;for(let e of this.n())for(let i of e.rows)for(let o of e.columns)r.set(JSON.stringify([i,o]),{rowId:i,colId:o,value:null});w(t,[...r.values()])}s(t){if(!(!t.target.closest(".mach-cell")||t.button!==0)){if(t.ctrlKey||t.metaKey){let e=this.context?.api.selection.getRange(),i=e?this.snapshot(e):null,o=
|
|
4
|
+
`):""}clearValues(){let t=this.context?.api;if(!t)return;let r=new Map;for(let e of this.n())for(let i of e.rows)for(let o of e.columns)r.set(JSON.stringify([i,o]),{rowId:i,colId:o,value:null});w(t,[...r.values()])}s(t){if(!(!t.target.closest(".mach-cell")||t.button!==0)){if(t.ctrlKey||t.metaKey){let e=this.context?.api.selection.getRange(),i=e?this.snapshot(e):null,o=u(i?[...this.saved,i]:this.saved);if(o.length>=g)throw new RangeError("At most 32 ranges are supported.");this.saved=o}else t.shiftKey||(this.saved=[]);this.t()}}a(t){if(!t.target.closest("input,textarea,select,[contenteditable=true]")){if(t.key==="Escape"){this.clear();return}t.key!=="Delete"&&t.key!=="Backspace"||this.saved.length===0||this.n().length<2||(t.preventDefault(),t.stopImmediatePropagation(),this.clearValues())}}i(t,r=!1){t.target.closest("input,textarea,select,[contenteditable=true]")||this.saved.length===0||this.n().length<2||this.context?.api.getOption("suppressClipboard")||t.clipboardData&&(t.clipboardData.setData("text/plain",this.getText()),t.preventDefault(),t.stopImmediatePropagation(),r&&this.clearValues())}t(){!this.context||this.e||(this.e=requestAnimationFrame(()=>{this.e=0,this.l()}))}o(){this.r?.(),this.r=null}l(){if(this.o(),!this.context)return;let t=this.saved.map(e=>({rows:new Set(e.rows),columns:new Set(e.columns)})),r=[];for(let e of this.context.root.querySelectorAll(".mach-cell")){let i=e.closest(".mach-row");t.some(o=>o.rows.has(i?.dataset.id??"")&&o.columns.has(e.dataset.colId??""))&&(e.classList.add("mach-cell--multi-range"),r.push(e))}this.r=()=>r.forEach(e=>e.classList.remove("mach-cell--multi-range"))}};0&&(module.exports={createMultiRangeSelection});
|
package/dist/multi-range.d.cts
CHANGED
package/dist/multi-range.d.ts
CHANGED
package/dist/multi-range.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
var h=Symbol.for("mach-table.adapter.lifecycle.v1");function m(n,t,r){return n.isDestroyed()?"":n[h]?.readCell(t,r)??""}function y(n,t){n.isDestroyed()||n[h]?.writeInput(t)}var w=/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:[eE][+-]?\d+)?$/;function v(n){return typeof n!="string"||n===""||w.test(n)?n:/^[\s\u0000-\u001f]*[=+\-@]/.test(n)?"'"+n:n}function R(n){return n==null?"":String(n)}function D(n,t){let r=t?v(n):n,e=R(r);return/["\t\n\r]/.test(e)?'"'+e.replace(/"/g,'""')+'"':e}function x(n,t=!0){return n.map(r=>r.map(e=>D(e,t)).join(" ")).join(`
|
|
2
|
-
`)}var
|
|
2
|
+
`)}var u=32,b=1e5;function d(n){let t=[...new Map(n.map(r=>[JSON.stringify(r.range),r])).values()];if(t.length>u)throw new RangeError("At most 32 ranges are supported.");if(t.reduce((r,e)=>r+e.rows.length*e.columns.length,0)>b)throw new RangeError("A multi-range operation may affect at most 100000 cells.");return t}function A(n){let t=n.columns.getWorkbenchItems().filter(r=>r.visible);return["left",null,"right"].flatMap(r=>t.filter(e=>e.pinned===r).map(e=>e.colId))}function L(){return new g}var g=class{constructor(){this.key="multi-range";this.context=null;this.saved=[];this.e=0;this.r=null}setup(t){if(this.context)throw new Error("Create a separate multi-range feature for each grid.");if(!t.api.getOption("enableRangeSelection"))throw new Error("Multi-range selection requires enableRangeSelection: true.");this.context=t;let r=null;t.onCleanup(()=>{cancelAnimationFrame(this.e),this.e=0;try{r?.disconnect()}finally{this.o(),this.saved=[],this.context===t&&(this.context=null)}});let{root:e}=t,o=(i,s)=>{t.addManagedDomListener(e,i,a=>{try{s(a)}catch(c){a.preventDefault(),a.stopImmediatePropagation(),t.reportError(c,"multi-range")}},!0)};o("mousedown",i=>this.s(i)),o("keydown",i=>this.a(i)),o("copy",i=>this.i(i)),o("cut",i=>this.i(i,!0)),t.addEventListener("rangeSelectionChanged",()=>this.t()),r=new MutationObserver(()=>this.t()),r.observe(e,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["data-id","data-col-id"]}),t.addEventListener("modelUpdated",()=>this.clear())}snapshot(t){let r=this.context?.api;if(!r)return null;let e=A(r),o=e.indexOf(t.colId1),i=e.indexOf(t.colId2),s=Math.max(0,Math.min(t.row1,t.row2)),a=Math.min(r.rows.getCount()-1,Math.max(t.row1,t.row2));if(o<0||i<0||!Number.isInteger(s)||!Number.isInteger(a)||a<s)return null;let c=e.slice(Math.min(o,i),Math.max(o,i)+1);if((a-s+1)*c.length>b)throw new RangeError("A range may contain at most 100000 cells.");let f=[];for(let p=s;p<=a;p++){let l=r.rows.getAt(p);l?.data&&!l.isGroup&&!l.isDetail&&!l.loading&&f.push(l.id)}return{range:{row1:s,row2:a,colId1:e[Math.min(o,i)],colId2:e[Math.max(o,i)]},rows:f,columns:c}}n(){let t=this.context?.api.selection.getRange(),r=t?this.snapshot(t):null,e=r?[...this.saved,r]:[...this.saved];return d(e)}getRanges(){return this.n().map(t=>({...t.range}))}setRanges(t){if(!this.context)throw new Error("Install the multi-range feature before setting ranges.");if(t.length>u)throw new RangeError("At most 32 ranges are supported.");let r=d(t.map(e=>this.snapshot(e)).filter(e=>e!==null));this.context.api.selection.clearRange(),this.saved=r,this.t()}clear(){this.saved=[],this.context?.api.selection.clearRange(),this.o(),this.t()}getText(){let t=this.context?.api;return t?this.n().map(r=>x(r.rows.map(e=>r.columns.map(o=>m(t,e,o))))).join(`
|
|
3
3
|
|
|
4
|
-
`):""}clearValues(){let t=this.context?.api;if(!t)return;let r=new Map;for(let e of this.n())for(let o of e.rows)for(let i of e.columns)r.set(JSON.stringify([o,i]),{rowId:o,colId:i,value:null});y(t,[...r.values()])}s(t){if(!(!t.target.closest(".mach-cell")||t.button!==0)){if(t.ctrlKey||t.metaKey){let e=this.context?.api.selection.getRange(),o=e?this.snapshot(e):null,i=
|
|
4
|
+
`):""}clearValues(){let t=this.context?.api;if(!t)return;let r=new Map;for(let e of this.n())for(let o of e.rows)for(let i of e.columns)r.set(JSON.stringify([o,i]),{rowId:o,colId:i,value:null});y(t,[...r.values()])}s(t){if(!(!t.target.closest(".mach-cell")||t.button!==0)){if(t.ctrlKey||t.metaKey){let e=this.context?.api.selection.getRange(),o=e?this.snapshot(e):null,i=d(o?[...this.saved,o]:this.saved);if(i.length>=u)throw new RangeError("At most 32 ranges are supported.");this.saved=i}else t.shiftKey||(this.saved=[]);this.t()}}a(t){if(!t.target.closest("input,textarea,select,[contenteditable=true]")){if(t.key==="Escape"){this.clear();return}t.key!=="Delete"&&t.key!=="Backspace"||this.saved.length===0||this.n().length<2||(t.preventDefault(),t.stopImmediatePropagation(),this.clearValues())}}i(t,r=!1){t.target.closest("input,textarea,select,[contenteditable=true]")||this.saved.length===0||this.n().length<2||this.context?.api.getOption("suppressClipboard")||t.clipboardData&&(t.clipboardData.setData("text/plain",this.getText()),t.preventDefault(),t.stopImmediatePropagation(),r&&this.clearValues())}t(){!this.context||this.e||(this.e=requestAnimationFrame(()=>{this.e=0,this.l()}))}o(){this.r?.(),this.r=null}l(){if(this.o(),!this.context)return;let t=this.saved.map(e=>({rows:new Set(e.rows),columns:new Set(e.columns)})),r=[];for(let e of this.context.root.querySelectorAll(".mach-cell")){let o=e.closest(".mach-row");t.some(i=>i.rows.has(o?.dataset.id??"")&&i.columns.has(e.dataset.colId??""))&&(e.classList.add("mach-cell--multi-range"),r.push(e))}this.r=()=>r.forEach(e=>e.classList.remove("mach-cell--multi-range"))}};export{L as createMultiRangeSelection};
|
package/dist/ordering.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var ae=Object.defineProperty;var Fe=Object.getOwnPropertyDescriptor;var He=Object.getOwnPropertyNames;var Be=Object.prototype.hasOwnProperty;var qe=(r,e)=>{for(var t in e)ae(r,t,{get:e[t],enumerable:!0})},Ke=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of He(e))!Be.call(r,o)&&o!==t&&ae(r,o,{get:()=>e[o],enumerable:!(n=Fe(e,o))||n.enumerable});return r};var Ue=r=>Ke(ae({},"__esModule",{value:!0}),r);var nt={};qe(nt,{RowOrderConflictError:()=>L,createRowOrderController:()=>Me,moveTreeNodes:()=>Q,reorderRows:()=>J});module.exports=Ue(nt);var Ye=new Set(["__proto__","prototype","constructor"]),Y=new Map;function _e(r){if(typeof r!="string"||r.length===0)return null;let e=Y.get(r);if(e!==void 0)return e;let t=r.split("."),n=t.every(o=>o.length>0&&!Ye.has(o))?t:null;return r.length<=1024&&(Y.size>=256&&Y.delete(Y.keys().next().value),Y.set(r,n)),n}function V(r,e){if(r==null)return;let t=_e(e);if(!t)return;let n=r;for(let o of t){if(n==null)return;n=n[o]}return n}function $e(r,e){let t=typeof e.rowKey=="function"?e.rowKey(r):V(r,e.rowKey);if(t==null||String(t).length===0)throw new Error("[MachTable] Row ordering requires a non-empty stable row key.");return String(t)}function We(r,e){let t=r[e];if(t==null)return[];if(!Array.isArray(t))throw new Error(`[MachTable] Tree ordering expects an array at "${e}".`);return t}function _(r,e,t,n){let o={entries:[],byId:new Map,byRow:new Map,children:new Map([[null,r]])},i=[...r].reverse().map(a=>({row:a,parentId:null,selectedAncestor:!1})),s=t.childrenKey??"children";for(;i.length;){let a=i.pop(),c=$e(a.row,t);if(o.byId.has(c))throw new Error(`[MachTable] Row ordering found duplicate or cyclic row key "${c}".`);let d=n?We(a.row,s):[],w={...a,id:c,children:d};o.entries.push(w),o.byId.set(c,w),o.byRow.set(w.row,w),o.children.set(c,d);for(let f=d.length-1;f>=0;f--)i.push({row:d[f],parentId:c,selectedAncestor:a.selectedAncestor||e.has(c)})}return o}function ze(r){if(!["before","after","inside"].includes(r.position))throw new Error("[MachTable] Unknown row ordering position.");let e=[...new Set(r.rowIds.map(String))];if(!e.length)throw new Error("[MachTable] Row ordering requires at least one source row.");return{...r,rowIds:e,targetId:String(r.targetId)}}function le(r,e){for(let n of e)if(!r.byId.has(n))throw new Error(`[MachTable] Ordering source "${n}" is unavailable.`);let t=new Set(e);return{selected:t,moved:r.entries.filter(n=>t.has(n.id)&&!n.selectedAncestor)}}function Re(r,e,t,n){let o=r.byId.get(t.targetId);if(!o)throw new Error(`[MachTable] Ordering target "${t.targetId}" is unavailable.`);if(e.selected.has(o.id)||o.selectedAncestor)throw new Error("[MachTable] A row cannot be moved into or relative to its own subtree.");let i=t.position==="inside"?o.id:o.parentId,s=i===null?null:r.byId.get(i).row;if(n.canMove)for(let a of e.moved){let c=a.parentId===null?null:r.byId.get(a.parentId).row,d=n.canMove({row:a.row,previousParent:c,parent:s,target:o.row,intent:t});if(d===!1||typeof d=="string")throw new Error(typeof d=="string"?d:"[MachTable] This row cannot be moved to the selected parent.")}return i}function Te(r,e,t,n){let o=[...new Set(e.map(String))];if(!o.length)throw new Error("[MachTable] Row ordering requires at least one source row.");let i=_(r,new Set(o),t,n),s=le(i,o);return a=>{if(!n&&a.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");Re(i,s,a,t)}}function je(r,e,t,n){let o=new Set(e.map(d=>d.id)),i=new Map;for(let d of e)i.has(d.parentId)||i.set(d.parentId,r.children.get(d.parentId).filter(w=>!o.has(r.byRow.get(w).id)));let s=i.get(t)??r.children.get(t),a=s.findIndex(d=>d===r.byId.get(n.targetId).row),c=n.position==="inside"?s.length:a+ +(n.position==="after");return i.set(t,[...s.slice(0,c),...e.map(d=>d.row),...s.slice(c)]),i}function be(r,e){return r.length===e.length&&r.every((t,n)=>t===e[n])}function Xe(r,e,t,n){let o=new Map,i=n.childrenKey??"children";for(let a=e.entries.length-1;a>=0;a--){let c=e.entries[a],w=(t.get(c.id)??c.children).map(f=>o.get(f)??f);be(c.children,w)||o.set(c.row,{...c.row,[i]:w})}let s=(t.get(null)??r).map(a=>o.get(a)??a);return be(r,s)?r:s}function Ve(r,e,t,n){let o=t.indexOf(e[0].row),i=o+e.length;return{...r,rowIds:e.map(s=>s.id),beforeId:o>0?n.byRow.get(t[o-1]).id:null,afterId:i<t.length?n.byRow.get(t[i]).id:null}}function Ie(r,e,t){let n=_(r.before,new Set(r.rowIds),e,t),o=new Map;for(let i of n.children.values())i.forEach((s,a)=>o.set(n.byRow.get(s).id,a));return le(n,r.rowIds).moved.reverse().map(i=>{let s=n.children.get(i.parentId),a=o.get(i.id),c=s[a+1];if(c)return{rowIds:[i.id],targetId:n.byRow.get(c).id,position:"before"};if(i.parentId!==null)return{rowIds:[i.id],targetId:i.parentId,position:"inside"};let d=s[a-1];if(!d)throw new Error("[MachTable] Row ordering cannot restore an isolated root.");return{rowIds:[i.id],targetId:n.byRow.get(d).id,position:"after"}})}function De(r,e,t,n,o){if(!n.canMove)return;let i=_(r,new Set,n,o),s=_(e,new Set,n,o);for(let a of t)for(let c of a.rowIds){let d=i.byId.get(c),w=s.byId.get(c),f=w.parentId===null?null:i.byId.get(w.parentId).row,O=d.parentId===null?null:i.byId.get(d.parentId).row,S=n.canMove({row:d.row,previousParent:O,parent:f,target:i.byId.get(a.targetId).row,intent:a});if(S===!1||typeof S=="string")throw new Error(typeof S=="string"?S:"[MachTable] This row cannot be restored to its previous parent.")}}function Oe(r,e,t,n){let o=ze(e);if(!n&&o.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");let i=_(r,new Set(o.rowIds),t,n),{moved:s,selected:a}=le(i,o.rowIds),c=Re(i,{moved:s,selected:a},o,t),d=je(i,s,c,o),w=Xe(r,i,d,t),f=Ve(o,s,d.get(c),i);return{before:r,after:w,intent:f,rowIds:f.rowIds,changed:r!==w}}function J(r,e,t){return Oe(r,e,t,!1)}function Q(r,e,t){return Oe(r,e,t,!0)}function de(r,e,t){let n=document.createElement(r);if(e&&(n.className=e),t)for(let o of Object.keys(t))n.setAttribute(o,t[o]);return n}function C(r,e,t){return r<e?e:r>t?t:r}var ce=Symbol.for("mach-table.adapter.lifecycle.v1");function Z(r){return!r.isDestroyed()&&(r[ce]?.canReplaceData()??!1)}function ue(r,e){let t=r[ce]?.rowDrag;return t?.original===e&&t.policy===r.getOption("rowDragPolicy")}function pe(r,e){let t=r[ce];if(!t)return()=>{};t.rowDrag?.activeInteraction?.destroy();let n={original:e,policy:r.getOption("rowDragPolicy"),factory:o=>(n.activeInteraction=e(o),n.activeInteraction)};return t.rowDrag=n,()=>{n.activeInteraction?.destroy(),t.rowDrag===n&&(t.rowDrag=void 0)}}function Je(r,e){if(r instanceof Error)return r;let t=new Error(typeof r=="string"?r:e);return t.name="AbortError",t}var Qe=()=>Je(void 0,"[MachTable] Operation cancelled.");function xe(r,e,t=Qe){return e?new Promise((n,o)=>{let i=()=>{e.removeEventListener("abort",i),o(t())};if(r.then(n,o).finally(()=>e.removeEventListener("abort",i)),e.aborted){i();return}e.addEventListener("abort",i,{once:!0})}):r}function we(r,e,t){return r.replace("{a}",String(e)).replace("{b}",String(t))}function Ze(r,e,t){return r&&e>.25&&e<.75?"inside":t}function Ee(r,e){let t=0,n=r.length;for(;t<n;){let o=t+n>>>1;r[o]<e?t=o+1:n=o}return t}function et(r,e,t,n,o,i){let s=r.getOption("locale")??{},a=e>1?we(s.rowDragSelection??"\u5DF2\u9009 {a} \u884C \u2192 \u7B2C {b} \u884C",e,n+1):we(s.rowDragPosition??"\u7B2C {a} \u884C \u2192 \u7B2C {b} \u884C",t+1,n+1),c=o==="inside"?s.rowDragInside??"\u79FB\u5165":o==="before"?s.rowDragBefore??"\u4E4B\u524D":s.rowDragAfter??"\u4E4B\u540E";return{text:`${a} \xB7 ${c}`,indicatorStyle:o==="inside"?`height:${i}px;background:color-mix(in srgb,var(--mach-primary) 12%,transparent);box-shadow:inset 0 0 0 1px var(--mach-primary)`:void 0}}function tt(r,e,t){let n={parents:new Map,siblings:new Map([[null,[]]]),depths:new Map},o=[...r].reverse().map(s=>({row:s,parent:null,depth:0})),i=e.childrenKey??"children";for(;o.length;){let{row:s,parent:a,depth:c}=o.pop(),d=String(typeof e.rowKey=="function"?e.rowKey(s):V(s,e.rowKey));if(n.depths.has(d))throw new Error("[MachTable] Row ordering requires unique, acyclic row keys.");n.depths.set(d,c),n.siblings.get(a).push(d),n.siblings.set(d,[]),a!==null&&n.parents.set(d,a);let w=t?s[i]:void 0;if(w!=null&&!Array.isArray(w))throw new Error("[MachTable] Row ordering children must be an array.");for(let f=(w?.length??0)-1;f>=0;f--)o.push({row:w[f],parent:d,depth:c+1})}return n}function Se(r,e,t,n,o,i){let s=new Map;r.rows.forEach(u=>{!u.loading&&!u.isDetail&&!u.isGroup&&u.data!=null&&s.set(u.id,u)});let a=tt(t,n,o),c=new Set(e.selected?[...a.depths.keys()].filter(u=>s.get(u)?.selected):[e.id]),d=[...c],w=u=>{let g=u;for(;g!==void 0;){if(c.has(g))return!0;g=a.parents.get(g)}return!1},f=new Map,O=new Map;for(let[u,g]of a.siblings){let y=g.filter(m=>!c.has(m));f.set(u,y),y.forEach((m,T)=>O.set(m,T))}let S=new Map,A=new Set,x=[],E=[],B=0;r.rows.forEachDisplayed(u=>{A.add(u.id),w(u.id)&&x.push(u.rowIndex),B=u.rowIndex+1;let g=a.depths.get(u.id)??0;for(;E.length&&(a.depths.get(E[E.length-1].id)??0)>=g;)S.set(E.pop().id,u.rowIndex);a.siblings.get(u.id)?.length&&E.push(u)});for(let u of E)S.set(u.id,B);return{rowIds:d,isSource:w,validate:()=>{for(let u of d){let g=s.get(u);if(g.treeLoading)return!1;let y=r.getOption("rowDragPolicy")?.canDrag?.({node:g,api:r});if(y===!1||typeof y=="string")return y}return!0},resolve:({targetNode:u,position:g,fraction:y,height:m})=>{let T=Ze(o,y,g);if(w(u.id))return"Cannot target own subtree.";if(!o&&T==="inside")return!1;let k=T==="inside"?u.id:a.parents.get(u.id)??null,M=f.get(k)??[],q=T==="inside"?M.length:(O.get(u.id)??0)+ +(T==="after"),N=T==="before"?u.rowIndex:S.get(u.id)??u.rowIndex+1,z=N-Ee(x,N)+Ee(x,e.rowIndex);return{intent:{rowIds:d,targetId:u.id,position:T,beforeId:M[q-1]??null,afterId:M[q]??null,expectedRevision:i},indicatorIndex:T==="inside"?u.rowIndex:T==="after"?S.get(u.id):void 0,toIndex:N,targetIndex:z,...et(r,d.length,e.rowIndex,u.rowIndex,T,m)}},keyboardTarget:u=>{let g=e.id;for(;c.has(a.parents.get(g)??"");)g=a.parents.get(g);let y=a.siblings.get(a.parents.get(g)??null)??[],m=y.indexOf(g)+u;for(;m>=0&&m<y.length;){let T=y[m];if(!w(T)&&A.has(T)&&s.has(T))return s.get(T);m+=u}}}}var ee=class{constructor(e){this.host=e;this.e=null;this.n=0;this.o=0;this.l=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){if(t.clientX=e.clientX,t.clientY=e.clientY,!t.active){if(Math.abs(e.clientY-t.startY)<5)return;t.active=!0,this.host.root.classList.add("mach-root--row-dragging"),this.host.root.querySelectorAll(".mach-row").forEach(n=>n.classList.toggle("mach-row--drag-source",this.sourceContains(n.dataset.id??"")))}this.u()}};this.y=()=>{if(this.n=0,!!this.e?.active){if(!this.current()){this.destroy();return}this.p(),this.m()&&this.u()}};this.d=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){t.clientX=e.clientX,t.clientY=e.clientY,t.active&&this.current()&&this.p(),this.h();try{t.active&&t.drop?.valid&&this.s(e.clientX,e.clientY)&&this.current()&&this.commitDrop(t.drop)}finally{this.release()}}};this.c=e=>{e.key==="Escape"&&(e.preventDefault(),this.destroy())};this.i=e=>{e.type==="pointercancel"&&e.pointerId!==this.e?.pointerId||this.destroy()}}begin(e){let t=this.host.containers.map(o=>{let i=de("div","mach-row-drop-indicator");return i.hidden=!0,o.append(i),i}),n=de("div","mach-row-drag-preview");n.setAttribute("role","status"),n.hidden=!0,this.host.root.append(n),this.e={startY:e.clientY,clientX:e.clientX,clientY:e.clientY,pointerId:e.pointerId,indicators:t,preview:n,drop:null,active:!1},window.addEventListener("pointermove",this.l),window.addEventListener("pointerup",this.d),window.addEventListener("pointercancel",this.i),window.addEventListener("keydown",this.c),window.addEventListener("blur",this.i)}g(e){return this.e?.active===!0&&this.sourceContains(e)}f(e,t=()=>!0){this.o=requestAnimationFrame(()=>{this.o=0,t()&&[...this.host.root.querySelectorAll(".mach-row-drag-handle")].find(n=>n.closest(".mach-row")?.dataset.id===e)?.focus()})}u(){this.n||(this.n=requestAnimationFrame(this.y))}s(e,t,n=0){let o=this.host.body.getBoundingClientRect();return e>=o.left&&e<=o.right&&t>=o.top-n&&t<=o.bottom+n}m(){let e=this.e;if(!e||!this.s(e.clientX,e.clientY,28))return!1;let t=this.host.viewport,n=t.getBoundingClientRect(),o=Math.min(36,n.height/3);if(o<=0)return!1;let i=e.clientY<n.top+o?-Math.ceil(14*C((n.top+o-e.clientY)/o,0,1)):Math.ceil(14*C((e.clientY-n.bottom+o)/o,0,1));if(this.host.scrollBy)return this.host.scrollBy(i);let s=t.scrollTop;return t.scrollTop=C(s+i,0,Math.max(0,t.scrollHeight-t.clientHeight)),t.scrollTop!==s}p(){let e=this.e;if(!e?.active)return;let t=e.clientY-this.host.viewport.getBoundingClientRect().top+this.host.viewport.scrollTop,n=this.describe(t);e.drop=n;let o=this.s(e.clientX,e.clientY);for(let s of e.indicators){if(s.hidden=!o||!n?.valid,!n)continue;s.dataset.dropPosition=n.position??"",s.style.cssText=n.style??"";let a=parseFloat(s.parentElement.style.height),c=Math.min(this.host.getRowTop(n.boundary),Number.isFinite(a)?Math.max(0,a-2):1/0);s.style.transform=`translate3d(0, ${c}px, 0)`}let i=this.host.root.getBoundingClientRect();e.preview.hidden=!1,e.preview.textContent=o?n?.text??this.outsideText():this.outsideText(),e.preview.style.transform=`translate3d(${C(e.clientX-i.left+16,8,Math.max(8,i.width-224))}px, ${C(e.clientY-i.top+16,8,Math.max(8,i.height-48))}px, 0)`}h(){let e=this.e;this.e=null,window.removeEventListener("pointermove",this.l),window.removeEventListener("pointerup",this.d),window.removeEventListener("pointercancel",this.i),window.removeEventListener("keydown",this.c),window.removeEventListener("blur",this.i),cancelAnimationFrame(this.n),this.n=0,this.host.root?.classList.remove("mach-root--row-dragging"),this.host.root?.querySelectorAll(".mach-row--drag-source").forEach(t=>t.classList.remove("mach-row--drag-source")),e?.indicators.forEach(t=>t.remove()),e?.preview.remove()}destroy(){this.h(),this.release(),cancelAnimationFrame(this.o),this.o=0}};var te=class extends ee{constructor(t,n,o){super(t);this.t=t;this.prepare=n;this.v=o;this.scope=null;this.a=!1}r(t,n,o){try{return n()}catch(i){return this.scope&&(this.scope.failed=!0),this.t.report(i,t),o}}w(t){return this.r("prepare",()=>{let n=this.t.capture(t),o=this.prepare(t);return this.scope={node:t,from:t.rowIndex,guard:n,session:o,sourceDecision:!0,failed:!1},this.scope.sourceDecision=o.validate(),this.scope},null)}start(t,n){this.destroy(),this.w(n)&&this.current()&&this.begin(t)}moveWithKeyboard(t,n){this.destroy();let o=this.w(n);if(!(!o||!this.current())){this.a=!0;try{let i=t.key==="ArrowUp"?-1:1,s=this.r("keyboardTarget",()=>o.session.keyboardTarget(i),void 0);s&&this.commitDrop(this.destination(s,i<0?0:1))}finally{this.a=!1,this.release()}}}isSource(t){return this.g(t.id)}current(){return!!(this.scope&&!this.scope.failed&&this.scope.guard.current()&&this.t.api.rows.getById(this.scope.node.id)===this.scope.node)}sourceContains(t){return this.r("isSource",()=>this.scope?.session.isSource(t)??!1,!1)}release(){this.scope=null}outsideText(){return this.t.api.getOption("locale")?.rowDragOutside??"\u79FB\u56DE\u8868\u683C\u5185\u653E\u7F6E \xB7 Esc \u53D6\u6D88"}describe(t){let n=this.t.api,o=C(this.host.findRowAt(t),0,n.rows.getCount()-1),i=n.rows.getAt(o);if(i?.isDetail&&(i=n.rows.getById(i.masterId)),!i)return null;let s=this.host.getRowTop(i.rowIndex),a=this.host.getRowTop(i.rowIndex+1)-s;return this.destination(i,(t-s)/Math.max(1,a))}destination(t,n){let o=this.scope,i=n<.5?"before":"after",s=this.host.getRowTop(t.rowIndex+1)-this.host.getRowTop(t.rowIndex),a=this.r("resolve",()=>o.session.resolve({targetNode:t,position:i,fraction:n,height:s}),!1),c=typeof o.sourceDecision=="string"?o.sourceDecision:typeof a=="string"?a:"\u6B64\u4F4D\u7F6E\u4E0D\u53EF\u653E\u7F6E",d={node:t,fraction:n,boundary:t.rowIndex,valid:!1,text:c};if(!a||typeof a=="string"||o.sourceDecision!==!0||t.loading||t.treeLoading||t.isGroup)return d;d.intent=a.intent,d.position=a.intent.position,d.toIndex=a.toIndex,d.targetIndex=a.targetIndex,d.boundary=a.indicatorIndex??t.rowIndex+ +(a.intent.position==="after"),d.style=a.indicatorStyle,d.text=a.text??c;let w=this.r("canDrop",()=>this.t.api.getOption("rowDragPolicy")?.canDrop?.({node:o.node,api:this.t.api,targetNode:t,intent:a.intent})??!0,!1);return d.valid=w===!0,typeof w=="string"&&(d.text=w),d}commitDrop(t){let n=this.scope;if(!n||!this.current()||this.r("validate",()=>n.session.validate(),!1)!==!0||!this.current())return!1;let o=this.destination(t.node,t.fraction);return!o.valid||!o.intent||!this.current()?!1:(this.a&&this.v(o.intent,i=>this.f(n.node.id,i)),this.t.emit(n.node,n.from,o.toIndex,o.targetIndex,o.intent),!0)}};function $(r,e,t){let n=[],o=[...r].reverse().map(s=>({row:s,parent:null})),i=new Set;for(;o.length;){let s=o.pop();if(i.has(s.row))throw new Error("[MachTable] Installed order contains duplicate or cyclic rows.");i.add(s.row),n.push(s);let a=t?s.row[e]??[]:[];for(let c=a.length-1;c>=0;c--)o.push({row:a[c],parent:s.row})}return n}function H(r,e,t,n){try{let o=$(r,t,n);return o.length===e.length&&o.every((i,s)=>i.row===e[s].row&&i.parent===e[s].parent)}catch{return!1}}function he(r){let e=[];return r?.rows.forEach(t=>e.push({node:t,data:t.data})),e}function W(r,e){if(!r)return!0;if(r.isDestroyed())return!1;let t=0,n=!0;return r.rows.forEach(o=>{let i=e[t++];(i?.node!==o||i.data!==o.data)&&(n=!1)}),n&&t===e.length}var re=class{constructor(){this.entries=[];this.cursor=0}get canUndo(){return this.cursor>0}get canRedo(){return this.cursor<this.entries.length}peek(e){return this.entries[e==="undo"?this.cursor-1:this.cursor]}commit(e,t){e==="undo"?this.cursor--:e==="redo"?this.cursor++:(this.entries.splice(this.cursor),this.entries.push(t),this.entries.length>50&&this.entries.shift(),this.cursor=this.entries.length)}clear(){this.entries=[],this.cursor=0}};var L=class extends Error{constructor(t="The row order changed on the server. Reload before trying again.",n){super(t);this.serverRevision=n;this.name="RowOrderConflictError"}};function rt(r){if(r?.status==="conflict")throw new L(r.message,r.revision);return r?.revision}function Ae(r){if(r.targetId===null)throw new Error("[MachTable] Row ordering requires an existing destination.");return{...r,targetId:r.targetId}}function Me(r){let e={status:"idle",error:null,revision:r.getRevision?.(),plan:null,canUndo:!1,canRedo:!1},t=0,n=!1,o=null,i=null,s=null,a=()=>{},c=()=>{},d=0,w=null,f=new re,O=null,S=new Set,A=r.childrenKey??"children",x=null,E=()=>{f.clear(),O=null},B=()=>{if(!o)return!1;let l=o;return t++,o=null,i=null,l.abort(),!0},u=(l=e.status)=>({canUndo:!n&&l!=="saving"&&f.canUndo,canRedo:!n&&l!=="saving"&&f.canRedo}),g=l=>{e=Object.freeze({...l,...u(l.status)}),s&&!s.isDestroyed()&&s.view.refreshCells();let p=[h=>r.onStateChange?.(h),...S];for(let h of p)try{h?.(e)}catch(v){console.error("[MachTable] Row order state observer failed",v)}},y=()=>r.getRevision?.()??e.revision,m=(l=w)=>{l?.release(),w===l&&(w=null)},T=()=>{!i||i()||(B(),E(),m(),g({...e,status:"idle",plan:null,error:null}))},k=l=>new te(l,p=>Se(l.api,p,r.getRows(),r,r.treeData===!0,y()),(p,h)=>{m();let v=document.activeElement,R=G=>{G.target!==v&&G.target!==document.body&&m(b)},b={api:l.api,intent:p,restore:h,generation:t+1,release:()=>document.removeEventListener("focusin",R,!0)};w=b,document.addEventListener("focusin",R,!0)}),M=()=>{T(),!o&&O&&(O.api!==s||O.revision!==y()||!W(s,O.nodes)||!H(r.getRows(),O.rows,A,r.treeData===!0))&&E();let l=u();(e.canUndo!==l.canUndo||e.canRedo!==l.canRedo)&&(e=Object.freeze({...e,...l}))},q=l=>{if(l===s){T(),ue(l,k)||(c(),c=pe(l,k));return}let p=B();a(),c(),m(),E(),s=l,c=pe(l,k),a=l.on("cellValueChanged",()=>{d++;let h=f.canUndo||f.canRedo;E(),h&&g({...e,status:o?"saving":"idle",plan:o?e.plan:null,error:null})}),p&&g({...e,status:"idle",plan:null,error:null})},N=l=>{if(M(),n)throw new Error("[MachTable] The row order controller is disposed.");if(o)throw new Error("[MachTable] A row order save is already in progress.");let p=y();if(p!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==p)throw new L;let h={...l,expectedRevision:l.expectedRevision??p};return(r.treeData?Q:J)(r.getRows(),h,r)},z=async(l,p,h,v,R)=>{if(!l.changed)return l;let b=s;if(b&&!Z(b))throw new Error("[MachTable] Save or discard edited rows before changing their order.");let G=++t;x=null;let j=new AbortController;o=j,i=null;let X=y(),ke=d,F=null,K=[],me=!1,ne=!1,oe=X,ve=I=>{if(!R)return;if(!I||R!==w||R.api!==s){m(R);return}let D=O;R.restore(()=>{let se=D!==null&&D===O&&D.revision===y()&&W(R.api,D.nodes)&&H(r.getRows(),D.rows,A,r.treeData===!0)&&w===R&&!n&&t===R.generation&&s===R.api&&ue(R.api,k);return m(R),se})},P=()=>!n&&t===G,ie=()=>P()&&(!b||s===b&&!b.isDestroyed())&&F!==null&&y()===X&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0),Ne=I=>{if(!ie()){P()&&!F&&g({...e,status:"error",error:I,plan:l});return}let D=!b||Z(b),U=D?I:Object.assign(new Error("[MachTable] Row order could not be saved. Automatic rollback was skipped to preserve new edits. Save or discard those edits, then reload the row order."),{cause:I});if(D){i=null;try{r.applyRows(l.before),ne=!0}catch(se){U=Object.assign(new Error("[MachTable] Row order could not be saved or restored. Reload the row order before trying again."),{cause:I,rollbackError:se})}}P()&&(ne||E(),g({...e,status:I instanceof L?"conflict":"error",error:U}))},Ge=()=>{if(!P()){ve(!1);return}let I=y(),D=!1;me&&P()&&(I===X||I===oe)&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0)?(O={rows:F,nodes:K,api:b,revision:I},D=!0):ne&&P()?(O={rows:$(r.getRows(),A,r.treeData===!0),nodes:he(b),api:b,revision:I},D=H(r.getRows(),$(l.before,A,r.treeData===!0),A,r.treeData===!0)):E(),o===j&&(o=null,i=null,s&&!s.isDestroyed()&&s.view.refreshCells()),P()&&e.status==="saving"&&g({...e,status:"idle",plan:null}),M(),ve(D)};try{r.applyRows(l.after),F=$(r.getRows(),A,r.treeData===!0),K=he(b);let I=r.getRows(),D=K[0]?.node;if(i=b&&D?()=>!b.isDestroyed()&&b.rows.getById(D.id)===D:()=>r.getRows()===I,g({...e,status:"saving",error:null,plan:l}),!ie())return l;let U=rt(await xe(Promise.resolve(r.save?.(l.intent,{signal:j.signal,plan:l,operation:p,operations:h})),j.signal));return ie()&&(me=!0,oe=U??X,d===ke?f.commit(p,v):E(),g({...e,status:"saved",revision:oe})),l}catch(I){throw Ne(I),I}finally{Ge()}},ge=async(l,p)=>{p!==w&&(p=void 0),w!==p&&m();let h=N(l);h.changed||m(p);let v=h.changed?Ie(h,r,r.treeData===!0):[];return z(h,"move",[h.intent],{plan:h,inverse:v},p)},Ce=l=>ge(l),fe=async l=>{if(m(),M(),n||o)throw new Error("[MachTable] Row ordering is unavailable while disposed or saving.");let p=f.peek(l);if(!p)throw new Error(`[MachTable] Nothing to ${l}.`);let h=(l==="undo"?p.inverse:[p.plan.intent]).map(G=>({...G,expectedRevision:y()})),v=r.getRows(),R=l==="undo"?p.plan.before:p.plan.after;De(v,R,h,r,r.treeData===!0);let b={before:v,after:R,intent:h[0],rowIds:p.plan.rowIds,changed:!0};return z(b,l,h,p)},Le=l=>{try{if(n||o)return!1;let p=r.getRows(),h=y();if(h!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==h)return!1;let v=x?.rowIds===l.rowIds?x.sources:JSON.stringify(l.rowIds.map(String));return(!x||x.rows!==p||x.revision!==h||x.sources!==v)&&(x={rows:p,revision:h,sources:v,rowIds:l.rowIds,validate:Te(p,l.rowIds,r,r.treeData===!0)}),x.rowIds=l.rowIds,x.validate(l),!0}catch(p){return p instanceof Error?p.message:!1}},Pe=l=>{let p=l.intent;if(l.applied||!p)return;q(l.api);let h=t,v=w?.intent===p?w:void 0;Promise.resolve().then(()=>ge(Ae(p),v)).catch(R=>{v&&v.generation!==t&&m(v),!n&&t===h&&e.status!=="saving"&&g({...e,status:R instanceof L?"conflict":"error",error:R})})},ye=l=>n?!1:(q(l),o?"Wait for the current row order to finish saving.":Z(l)||"Save or discard edited rows before changing their order.");return{get state(){return M(),e},bindings:{applyRowDrag:!1,rowDragPolicy:{canDrag:({api:l})=>ye(l),canDrop:({intent:l,api:p})=>{let h=ye(p);if(h!==!0)return h;try{return Le(Ae(l))}catch(v){return v instanceof Error?v.message:!1}},getRevision:y},onRowDragEnd:Pe},preview:N,move:Ce,undo:()=>fe("undo"),redo:()=>fe("redo"),subscribe(l){return n?()=>{}:(S.add(l),()=>{S.delete(l)})},dispose(){n=!0,t++,o?.abort(),o=null,i=null,a(),c(),m(),E(),s&&!s.isDestroyed()&&s.view.refreshCells(),s=null,x=null,S.clear(),e=Object.freeze({...e,status:"idle",error:null,plan:null,canUndo:!1,canRedo:!1})}}}0&&(module.exports={RowOrderConflictError,createRowOrderController,moveTreeNodes,reorderRows});
|
|
1
|
+
"use strict";var ae=Object.defineProperty;var He=Object.getOwnPropertyDescriptor;var Be=Object.getOwnPropertyNames;var qe=Object.prototype.hasOwnProperty;var Ke=(r,e)=>{for(var t in e)ae(r,t,{get:e[t],enumerable:!0})},Ue=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Be(e))!qe.call(r,o)&&o!==t&&ae(r,o,{get:()=>e[o],enumerable:!(n=He(e,o))||n.enumerable});return r};var Ye=r=>Ue(ae({},"__esModule",{value:!0}),r);var ot={};Ke(ot,{RowOrderConflictError:()=>P,createRowOrderController:()=>Ce,moveTreeNodes:()=>Q,reorderRows:()=>J});module.exports=Ye(ot);var _e=new Set(["__proto__","prototype","constructor"]),Y=new Map;function $e(r){if(typeof r!="string"||r.length===0)return null;let e=Y.get(r);if(e!==void 0)return e;let t=r.split("."),n=t.every(o=>o.length>0&&!_e.has(o))?t:null;return r.length<=1024&&(Y.size>=256&&Y.delete(Y.keys().next().value),Y.set(r,n)),n}function V(r,e){if(r==null)return;let t=$e(e);if(!t)return;let n=r;for(let o of t){if(n==null)return;n=n[o]}return n}function We(r,e){let t=typeof e.rowKey=="function"?e.rowKey(r):V(r,e.rowKey);if(t==null||String(t).length===0)throw new Error("[MachTable] Row ordering requires a non-empty stable row key.");return String(t)}function ze(r,e){let t=r[e];if(t==null)return[];if(!Array.isArray(t))throw new Error(`[MachTable] Tree ordering expects an array at "${e}".`);return t}function _(r,e,t,n){let o={entries:[],byId:new Map,byRow:new Map,children:new Map([[null,r]])},i=[...r].reverse().map(a=>({row:a,parentId:null,selectedAncestor:!1})),s=t.childrenKey??"children";for(;i.length;){let a=i.pop(),c=We(a.row,t);if(o.byId.has(c))throw new Error(`[MachTable] Row ordering found duplicate or cyclic row key "${c}".`);let d=n?ze(a.row,s):[],h={...a,id:c,children:d};o.entries.push(h),o.byId.set(c,h),o.byRow.set(h.row,h),o.children.set(c,d);for(let f=d.length-1;f>=0;f--)i.push({row:d[f],parentId:c,selectedAncestor:a.selectedAncestor||e.has(c)})}return o}function je(r){if(!["before","after","inside"].includes(r.position))throw new Error("[MachTable] Unknown row ordering position.");let e=[...new Set(r.rowIds.map(String))];if(!e.length)throw new Error("[MachTable] Row ordering requires at least one source row.");return{...r,rowIds:e,targetId:String(r.targetId)}}function le(r,e){for(let n of e)if(!r.byId.has(n))throw new Error(`[MachTable] Ordering source "${n}" is unavailable.`);let t=new Set(e);return{selected:t,moved:r.entries.filter(n=>t.has(n.id)&&!n.selectedAncestor)}}function Re(r,e,t,n){let o=r.byId.get(t.targetId);if(!o)throw new Error(`[MachTable] Ordering target "${t.targetId}" is unavailable.`);if(e.selected.has(o.id)||o.selectedAncestor)throw new Error("[MachTable] A row cannot be moved into or relative to its own subtree.");let i=t.position==="inside"?o.id:o.parentId,s=i===null?null:r.byId.get(i).row;if(n.canMove)for(let a of e.moved){let c=a.parentId===null?null:r.byId.get(a.parentId).row,d=n.canMove({row:a.row,previousParent:c,parent:s,target:o.row,intent:t});if(d===!1||typeof d=="string")throw new Error(typeof d=="string"?d:"[MachTable] This row cannot be moved to the selected parent.")}return i}function Te(r,e,t,n){let o=[...new Set(e.map(String))];if(!o.length)throw new Error("[MachTable] Row ordering requires at least one source row.");let i=_(r,new Set(o),t,n),s=le(i,o);return a=>{if(!n&&a.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");Re(i,s,a,t)}}function Xe(r,e,t,n){let o=new Set(e.map(d=>d.id)),i=new Map;for(let d of e)i.has(d.parentId)||i.set(d.parentId,r.children.get(d.parentId).filter(h=>!o.has(r.byRow.get(h).id)));let s=i.get(t)??r.children.get(t),a=s.findIndex(d=>d===r.byId.get(n.targetId).row),c=n.position==="inside"?s.length:a+ +(n.position==="after");return i.set(t,[...s.slice(0,c),...e.map(d=>d.row),...s.slice(c)]),i}function be(r,e){return r.length===e.length&&r.every((t,n)=>t===e[n])}function Ve(r,e,t,n){let o=new Map,i=n.childrenKey??"children";for(let a=e.entries.length-1;a>=0;a--){let c=e.entries[a],h=(t.get(c.id)??c.children).map(f=>o.get(f)??f);be(c.children,h)||o.set(c.row,{...c.row,[i]:h})}let s=(t.get(null)??r).map(a=>o.get(a)??a);return be(r,s)?r:s}function Je(r,e,t,n){let o=t.indexOf(e[0].row),i=o+e.length;return{...r,rowIds:e.map(s=>s.id),beforeId:o>0?n.byRow.get(t[o-1]).id:null,afterId:i<t.length?n.byRow.get(t[i]).id:null}}function Ie(r,e,t){let n=_(r.before,new Set(r.rowIds),e,t),o=new Map;for(let i of n.children.values())i.forEach((s,a)=>o.set(n.byRow.get(s).id,a));return le(n,r.rowIds).moved.reverse().map(i=>{let s=n.children.get(i.parentId),a=o.get(i.id),c=s[a+1];if(c)return{rowIds:[i.id],targetId:n.byRow.get(c).id,position:"before"};if(i.parentId!==null)return{rowIds:[i.id],targetId:i.parentId,position:"inside"};let d=s[a-1];if(!d)throw new Error("[MachTable] Row ordering cannot restore an isolated root.");return{rowIds:[i.id],targetId:n.byRow.get(d).id,position:"after"}})}function De(r,e,t,n,o){if(!n.canMove)return;let i=_(r,new Set,n,o),s=_(e,new Set,n,o);for(let a of t)for(let c of a.rowIds){let d=i.byId.get(c),h=s.byId.get(c),f=h.parentId===null?null:i.byId.get(h.parentId).row,O=d.parentId===null?null:i.byId.get(d.parentId).row,S=n.canMove({row:d.row,previousParent:O,parent:f,target:i.byId.get(a.targetId).row,intent:a});if(S===!1||typeof S=="string")throw new Error(typeof S=="string"?S:"[MachTable] This row cannot be restored to its previous parent.")}}function Oe(r,e,t,n){let o=je(e);if(!n&&o.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");let i=_(r,new Set(o.rowIds),t,n),{moved:s,selected:a}=le(i,o.rowIds),c=Re(i,{moved:s,selected:a},o,t),d=Xe(i,s,c,o),h=Ve(r,i,d,t),f=Je(o,s,d.get(c),i);return{before:r,after:h,intent:f,rowIds:f.rowIds,changed:r!==h}}function J(r,e,t){return Oe(r,e,t,!1)}function Q(r,e,t){return Oe(r,e,t,!0)}function de(r,e,t){let n=document.createElement(r);if(e&&(n.className=e),t)for(let o of Object.keys(t))n.setAttribute(o,t[o]);return n}function xe(r,e,t,n=!1){let o=e?"addEventListener":"removeEventListener";for(let[i,s]of Object.entries(t))r[o](i,s,n)}function C(r,e,t){return r<e?e:r>t?t:r}var ce=Symbol.for("mach-table.adapter.lifecycle.v1");function Z(r){return!r.isDestroyed()&&(r[ce]?.canReplaceData()??!1)}function ue(r,e){let t=r[ce]?.rowDrag;return t?.original===e&&t.policy===r.getOption("rowDragPolicy")}function pe(r,e){let t=r[ce];if(!t)return()=>{};t.rowDrag?.activeInteraction?.destroy();let n={original:e,policy:r.getOption("rowDragPolicy"),factory:o=>(n.activeInteraction=e(o),n.activeInteraction)};return t.rowDrag=n,()=>{n.activeInteraction?.destroy(),t.rowDrag===n&&(t.rowDrag=void 0)}}function Qe(r,e){if(r instanceof Error)return r;let t=new Error(typeof r=="string"?r:e);return t.name="AbortError",t}var Ze=()=>Qe(void 0,"[MachTable] Operation cancelled.");function Ee(r,e,t=Ze){return e?new Promise((n,o)=>{let i=()=>{e.removeEventListener("abort",i),o(t())};if(r.then(n,o).finally(()=>e.removeEventListener("abort",i)),e.aborted){i();return}e.addEventListener("abort",i,{once:!0})}):r}function he(r,e,t){return r.replace("{a}",String(e)).replace("{b}",String(t))}function et(r,e,t){return r&&e>.25&&e<.75?"inside":t}function Se(r,e){let t=0,n=r.length;for(;t<n;){let o=t+n>>>1;r[o]<e?t=o+1:n=o}return t}function tt(r,e,t,n,o,i){let s=r.getOption("locale")??{},a=e>1?he(s.rowDragSelection??"\u5DF2\u9009 {a} \u884C \u2192 \u7B2C {b} \u884C",e,n+1):he(s.rowDragPosition??"\u7B2C {a} \u884C \u2192 \u7B2C {b} \u884C",t+1,n+1),c=o==="inside"?s.rowDragInside??"\u79FB\u5165":o==="before"?s.rowDragBefore??"\u4E4B\u524D":s.rowDragAfter??"\u4E4B\u540E";return{text:`${a} \xB7 ${c}`,indicatorStyle:o==="inside"?`height:${i}px;background:color-mix(in srgb,var(--mach-primary) 12%,transparent);box-shadow:inset 0 0 0 1px var(--mach-primary)`:void 0}}function rt(r,e,t){let n={parents:new Map,siblings:new Map([[null,[]]]),depths:new Map},o=[...r].reverse().map(s=>({row:s,parent:null,depth:0})),i=e.childrenKey??"children";for(;o.length;){let{row:s,parent:a,depth:c}=o.pop(),d=String(typeof e.rowKey=="function"?e.rowKey(s):V(s,e.rowKey));if(n.depths.has(d))throw new Error("[MachTable] Row ordering requires unique, acyclic row keys.");n.depths.set(d,c),n.siblings.get(a).push(d),n.siblings.set(d,[]),a!==null&&n.parents.set(d,a);let h=t?s[i]:void 0;if(h!=null&&!Array.isArray(h))throw new Error("[MachTable] Row ordering children must be an array.");for(let f=(h?.length??0)-1;f>=0;f--)o.push({row:h[f],parent:d,depth:c+1})}return n}function Ae(r,e,t,n,o,i){let s=new Map;r.rows.forEach(u=>{!u.loading&&!u.isDetail&&!u.isGroup&&u.data!=null&&s.set(u.id,u)});let a=rt(t,n,o),c=new Set(e.selected?[...a.depths.keys()].filter(u=>s.get(u)?.selected):[e.id]),d=[...c],h=u=>{let g=u;for(;g!==void 0;){if(c.has(g))return!0;g=a.parents.get(g)}return!1},f=new Map,O=new Map;for(let[u,g]of a.siblings){let y=g.filter(m=>!c.has(m));f.set(u,y),y.forEach((m,T)=>O.set(m,T))}let S=new Map,A=new Set,x=[],E=[],B=0;r.rows.forEachDisplayed(u=>{A.add(u.id),h(u.id)&&x.push(u.rowIndex),B=u.rowIndex+1;let g=a.depths.get(u.id)??0;for(;E.length&&(a.depths.get(E[E.length-1].id)??0)>=g;)S.set(E.pop().id,u.rowIndex);a.siblings.get(u.id)?.length&&E.push(u)});for(let u of E)S.set(u.id,B);return{rowIds:d,isSource:h,validate:()=>{for(let u of d){let g=s.get(u);if(g.treeLoading)return!1;let y=r.getOption("rowDragPolicy")?.canDrag?.({node:g,api:r});if(y===!1||typeof y=="string")return y}return!0},resolve:({targetNode:u,position:g,fraction:y,height:m})=>{let T=et(o,y,g);if(h(u.id))return"Cannot target own subtree.";if(!o&&T==="inside")return!1;let k=T==="inside"?u.id:a.parents.get(u.id)??null,M=f.get(k)??[],q=T==="inside"?M.length:(O.get(u.id)??0)+ +(T==="after"),N=T==="before"?u.rowIndex:S.get(u.id)??u.rowIndex+1,z=N-Se(x,N)+Se(x,e.rowIndex);return{intent:{rowIds:d,targetId:u.id,position:T,beforeId:M[q-1]??null,afterId:M[q]??null,expectedRevision:i},indicatorIndex:T==="inside"?u.rowIndex:T==="after"?S.get(u.id):void 0,toIndex:N,targetIndex:z,...tt(r,d.length,e.rowIndex,u.rowIndex,T,m)}},keyboardTarget:u=>{let g=e.id;for(;c.has(a.parents.get(g)??"");)g=a.parents.get(g);let y=a.siblings.get(a.parents.get(g)??null)??[],m=y.indexOf(g)+u;for(;m>=0&&m<y.length;){let T=y[m];if(!h(T)&&A.has(T)&&s.has(T))return s.get(T);m+=u}}}}var ee=class{constructor(e){this.host=e;this.e=null;this.n=0;this.o=0;this.h=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){if(t.clientX=e.clientX,t.clientY=e.clientY,!t.active){if(Math.abs(e.clientY-t.startY)<5)return;t.active=!0,this.host.root.classList.add("mach-root--row-dragging"),this.host.root.querySelectorAll(".mach-row").forEach(n=>n.classList.toggle("mach-row--drag-source",this.sourceContains(n.dataset.id??"")))}this.d()}};this.m=()=>{if(this.n=0,!!this.e?.active){if(!this.current()){this.destroy();return}this.c(),this.v()&&this.d()}};this.g=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){t.clientX=e.clientX,t.clientY=e.clientY,t.active&&this.current()&&this.c(),this.u();try{t.active&&t.drop?.valid&&this.i(e.clientX,e.clientY)&&this.current()&&this.commitDrop(t.drop)}finally{this.release()}}};this.w=e=>{e.key==="Escape"&&(e.preventDefault(),this.destroy())};this.l=e=>{e.type==="pointercancel"&&e.pointerId!==this.e?.pointerId||this.destroy()}}begin(e){let t=this.host.containers.map(o=>{let i=de("div","mach-row-drop-indicator");return i.hidden=!0,o.append(i),i}),n=de("div","mach-row-drag-preview");n.setAttribute("role","status"),n.hidden=!0,this.host.root.append(n),this.e={startY:e.clientY,clientX:e.clientX,clientY:e.clientY,pointerId:e.pointerId,indicators:t,preview:n,drop:null,active:!1},this.a(!0)}a(e){xe(window,e,{pointermove:this.h,pointerup:this.g,pointercancel:this.l,keydown:this.w,blur:this.l})}f(e){return this.e?.active===!0&&this.sourceContains(e)}y(e,t=()=>!0){this.o=requestAnimationFrame(()=>{this.o=0,t()&&[...this.host.root.querySelectorAll(".mach-row-drag-handle")].find(n=>n.closest(".mach-row")?.dataset.id===e)?.focus()})}d(){this.n||(this.n=requestAnimationFrame(this.m))}i(e,t,n=0){let o=this.host.body.getBoundingClientRect();return e>=o.left&&e<=o.right&&t>=o.top-n&&t<=o.bottom+n}v(){let e=this.e;if(!e||!this.i(e.clientX,e.clientY,28))return!1;let t=this.host.viewport,n=t.getBoundingClientRect(),o=Math.min(36,n.height/3);if(o<=0)return!1;let i=e.clientY<n.top+o?-Math.ceil(14*C((n.top+o-e.clientY)/o,0,1)):Math.ceil(14*C((e.clientY-n.bottom+o)/o,0,1));if(this.host.scrollBy)return this.host.scrollBy(i);let s=t.scrollTop;return t.scrollTop=C(s+i,0,Math.max(0,t.scrollHeight-t.clientHeight)),t.scrollTop!==s}c(){let e=this.e;if(!e?.active)return;let t=e.clientY-this.host.viewport.getBoundingClientRect().top+this.host.viewport.scrollTop,n=this.describe(t);e.drop=n;let o=this.i(e.clientX,e.clientY);for(let s of e.indicators){if(s.hidden=!o||!n?.valid,!n)continue;s.dataset.dropPosition=n.position??"",s.style.cssText=n.style??"";let a=parseFloat(s.parentElement.style.height),c=Math.min(this.host.getRowTop(n.boundary),Number.isFinite(a)?Math.max(0,a-2):1/0);s.style.transform=`translate3d(0, ${c}px, 0)`}let i=this.host.root.getBoundingClientRect();e.preview.hidden=!1,e.preview.textContent=o?n?.text??this.outsideText():this.outsideText(),e.preview.style.transform=`translate3d(${C(e.clientX-i.left+16,8,Math.max(8,i.width-224))}px, ${C(e.clientY-i.top+16,8,Math.max(8,i.height-48))}px, 0)`}u(){let e=this.e;this.e=null,this.a(!1),cancelAnimationFrame(this.n),this.n=0,this.host.root?.classList.remove("mach-root--row-dragging"),this.host.root?.querySelectorAll(".mach-row--drag-source").forEach(t=>t.classList.remove("mach-row--drag-source")),e?.indicators.forEach(t=>t.remove()),e?.preview.remove()}destroy(){this.u(),this.release(),cancelAnimationFrame(this.o),this.o=0}};var te=class extends ee{constructor(t,n,o){super(t);this.t=t;this.prepare=n;this.b=o;this.scope=null;this.s=!1}r(t,n,o){try{return n()}catch(i){return this.scope&&(this.scope.failed=!0),this.t.report(i,t),o}}p(t){return this.r("prepare",()=>{let n=this.t.capture(t),o=this.prepare(t);return this.scope={node:t,from:t.rowIndex,guard:n,session:o,sourceDecision:!0,failed:!1},this.scope.sourceDecision=o.validate(),this.scope},null)}start(t,n){this.destroy(),this.p(n)&&this.current()&&this.begin(t)}moveWithKeyboard(t,n){this.destroy();let o=this.p(n);if(!(!o||!this.current())){this.s=!0;try{let i=t.key==="ArrowUp"?-1:1,s=this.r("keyboardTarget",()=>o.session.keyboardTarget(i),void 0);s&&this.commitDrop(this.destination(s,i<0?0:1))}finally{this.s=!1,this.release()}}}isSource(t){return this.f(t.id)}current(){return!!(this.scope&&!this.scope.failed&&this.scope.guard.current()&&this.t.api.rows.getById(this.scope.node.id)===this.scope.node)}sourceContains(t){return this.r("isSource",()=>this.scope?.session.isSource(t)??!1,!1)}release(){this.scope=null}outsideText(){return this.t.api.getOption("locale")?.rowDragOutside??"\u79FB\u56DE\u8868\u683C\u5185\u653E\u7F6E \xB7 Esc \u53D6\u6D88"}describe(t){let n=this.t.api,o=C(this.host.findRowAt(t),0,n.rows.getCount()-1),i=n.rows.getAt(o);if(i?.isDetail&&(i=n.rows.getById(i.masterId)),!i)return null;let s=this.host.getRowTop(i.rowIndex),a=this.host.getRowTop(i.rowIndex+1)-s;return this.destination(i,(t-s)/Math.max(1,a))}destination(t,n){let o=this.scope,i=n<.5?"before":"after",s=this.host.getRowTop(t.rowIndex+1)-this.host.getRowTop(t.rowIndex),a=this.r("resolve",()=>o.session.resolve({targetNode:t,position:i,fraction:n,height:s}),!1),c=typeof o.sourceDecision=="string"?o.sourceDecision:typeof a=="string"?a:"\u6B64\u4F4D\u7F6E\u4E0D\u53EF\u653E\u7F6E",d={node:t,fraction:n,boundary:t.rowIndex,valid:!1,text:c};if(!a||typeof a=="string"||o.sourceDecision!==!0||t.loading||t.treeLoading||t.isGroup)return d;d.intent=a.intent,d.position=a.intent.position,d.toIndex=a.toIndex,d.targetIndex=a.targetIndex,d.boundary=a.indicatorIndex??t.rowIndex+ +(a.intent.position==="after"),d.style=a.indicatorStyle,d.text=a.text??c;let h=this.r("canDrop",()=>this.t.api.getOption("rowDragPolicy")?.canDrop?.({node:o.node,api:this.t.api,targetNode:t,intent:a.intent})??!0,!1);return d.valid=h===!0,typeof h=="string"&&(d.text=h),d}commitDrop(t){let n=this.scope;if(!n||!this.current()||this.r("validate",()=>n.session.validate(),!1)!==!0||!this.current())return!1;let o=this.destination(t.node,t.fraction);return!o.valid||!o.intent||!this.current()?!1:(this.s&&this.b(o.intent,i=>this.y(n.node.id,i)),this.t.emit(n.node,n.from,o.toIndex,o.targetIndex,o.intent),!0)}};function $(r,e,t){let n=[],o=[...r].reverse().map(s=>({row:s,parent:null})),i=new Set;for(;o.length;){let s=o.pop();if(i.has(s.row))throw new Error("[MachTable] Installed order contains duplicate or cyclic rows.");i.add(s.row),n.push(s);let a=t?s.row[e]??[]:[];for(let c=a.length-1;c>=0;c--)o.push({row:a[c],parent:s.row})}return n}function H(r,e,t,n){try{let o=$(r,t,n);return o.length===e.length&&o.every((i,s)=>i.row===e[s].row&&i.parent===e[s].parent)}catch{return!1}}function we(r){let e=[];return r?.rows.forEach(t=>e.push({node:t,data:t.data})),e}function W(r,e){if(!r)return!0;if(r.isDestroyed())return!1;let t=0,n=!0;return r.rows.forEach(o=>{let i=e[t++];(i?.node!==o||i.data!==o.data)&&(n=!1)}),n&&t===e.length}var re=class{constructor(){this.entries=[];this.cursor=0}get canUndo(){return this.cursor>0}get canRedo(){return this.cursor<this.entries.length}peek(e){return this.entries[e==="undo"?this.cursor-1:this.cursor]}commit(e,t){e==="undo"?this.cursor--:e==="redo"?this.cursor++:(this.entries.splice(this.cursor),this.entries.push(t),this.entries.length>50&&this.entries.shift(),this.cursor=this.entries.length)}clear(){this.entries=[],this.cursor=0}};var P=class extends Error{constructor(t="The row order changed on the server. Reload before trying again.",n){super(t);this.serverRevision=n;this.name="RowOrderConflictError"}};function nt(r){if(r?.status==="conflict")throw new P(r.message,r.revision);return r?.revision}function Me(r){if(r.targetId===null)throw new Error("[MachTable] Row ordering requires an existing destination.");return{...r,targetId:r.targetId}}function Ce(r){let e={status:"idle",error:null,revision:r.getRevision?.(),plan:null,canUndo:!1,canRedo:!1},t=0,n=!1,o=null,i=null,s=null,a=()=>{},c=()=>{},d=0,h=null,f=new re,O=null,S=new Set,A=r.childrenKey??"children",x=null,E=()=>{f.clear(),O=null},B=()=>{if(!o)return!1;let l=o;return t++,o=null,i=null,l.abort(),!0},u=(l=e.status)=>({canUndo:!n&&l!=="saving"&&f.canUndo,canRedo:!n&&l!=="saving"&&f.canRedo}),g=l=>{e=Object.freeze({...l,...u(l.status)}),s&&!s.isDestroyed()&&s.view.refreshCells();let p=[w=>r.onStateChange?.(w),...S];for(let w of p)try{w?.(e)}catch(v){console.error("[MachTable] Row order state observer failed",v)}},y=()=>r.getRevision?.()??e.revision,m=(l=h)=>{l?.release(),h===l&&(h=null)},T=()=>{!i||i()||(B(),E(),m(),g({...e,status:"idle",plan:null,error:null}))},k=l=>new te(l,p=>Ae(l.api,p,r.getRows(),r,r.treeData===!0,y()),(p,w)=>{m();let v=document.activeElement,R=G=>{G.target!==v&&G.target!==document.body&&m(b)},b={api:l.api,intent:p,restore:w,generation:t+1,release:()=>document.removeEventListener("focusin",R,!0)};h=b,document.addEventListener("focusin",R,!0)}),M=()=>{T(),!o&&O&&(O.api!==s||O.revision!==y()||!W(s,O.nodes)||!H(r.getRows(),O.rows,A,r.treeData===!0))&&E();let l=u();(e.canUndo!==l.canUndo||e.canRedo!==l.canRedo)&&(e=Object.freeze({...e,...l}))},q=l=>{if(l===s){T(),ue(l,k)||(c(),c=pe(l,k));return}let p=B();a(),c(),m(),E(),s=l,c=pe(l,k),a=l.on("cellValueChanged",()=>{d++;let w=f.canUndo||f.canRedo;E(),w&&g({...e,status:o?"saving":"idle",plan:o?e.plan:null,error:null})}),p&&g({...e,status:"idle",plan:null,error:null})},N=l=>{if(M(),n)throw new Error("[MachTable] The row order controller is disposed.");if(o)throw new Error("[MachTable] A row order save is already in progress.");let p=y();if(p!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==p)throw new P;let w={...l,expectedRevision:l.expectedRevision??p};return(r.treeData?Q:J)(r.getRows(),w,r)},z=async(l,p,w,v,R)=>{if(!l.changed)return l;let b=s;if(b&&!Z(b))throw new Error("[MachTable] Save or discard edited rows before changing their order.");let G=++t;x=null;let j=new AbortController;o=j,i=null;let X=y(),Ne=d,F=null,K=[],me=!1,ne=!1,oe=X,ve=I=>{if(!R)return;if(!I||R!==h||R.api!==s){m(R);return}let D=O;R.restore(()=>{let se=D!==null&&D===O&&D.revision===y()&&W(R.api,D.nodes)&&H(r.getRows(),D.rows,A,r.treeData===!0)&&h===R&&!n&&t===R.generation&&s===R.api&&ue(R.api,k);return m(R),se})},L=()=>!n&&t===G,ie=()=>L()&&(!b||s===b&&!b.isDestroyed())&&F!==null&&y()===X&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0),Ge=I=>{if(!ie()){L()&&!F&&g({...e,status:"error",error:I,plan:l});return}let D=!b||Z(b),U=D?I:Object.assign(new Error("[MachTable] Row order could not be saved. Automatic rollback was skipped to preserve new edits. Save or discard those edits, then reload the row order."),{cause:I});if(D){i=null;try{r.applyRows(l.before),ne=!0}catch(se){U=Object.assign(new Error("[MachTable] Row order could not be saved or restored. Reload the row order before trying again."),{cause:I,rollbackError:se})}}L()&&(ne||E(),g({...e,status:I instanceof P?"conflict":"error",error:U}))},Fe=()=>{if(!L()){ve(!1);return}let I=y(),D=!1;me&&L()&&(I===X||I===oe)&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0)?(O={rows:F,nodes:K,api:b,revision:I},D=!0):ne&&L()?(O={rows:$(r.getRows(),A,r.treeData===!0),nodes:we(b),api:b,revision:I},D=H(r.getRows(),$(l.before,A,r.treeData===!0),A,r.treeData===!0)):E(),o===j&&(o=null,i=null,s&&!s.isDestroyed()&&s.view.refreshCells()),L()&&e.status==="saving"&&g({...e,status:"idle",plan:null}),M(),ve(D)};try{r.applyRows(l.after),F=$(r.getRows(),A,r.treeData===!0),K=we(b);let I=r.getRows(),D=K[0]?.node;if(i=b&&D?()=>!b.isDestroyed()&&b.rows.getById(D.id)===D:()=>r.getRows()===I,g({...e,status:"saving",error:null,plan:l}),!ie())return l;let U=nt(await Ee(Promise.resolve(r.save?.(l.intent,{signal:j.signal,plan:l,operation:p,operations:w})),j.signal));return ie()&&(me=!0,oe=U??X,d===Ne?f.commit(p,v):E(),g({...e,status:"saved",revision:oe})),l}catch(I){throw Ge(I),I}finally{Fe()}},ge=async(l,p)=>{p!==h&&(p=void 0),h!==p&&m();let w=N(l);w.changed||m(p);let v=w.changed?Ie(w,r,r.treeData===!0):[];return z(w,"move",[w.intent],{plan:w,inverse:v},p)},Pe=l=>ge(l),fe=async l=>{if(m(),M(),n||o)throw new Error("[MachTable] Row ordering is unavailable while disposed or saving.");let p=f.peek(l);if(!p)throw new Error(`[MachTable] Nothing to ${l}.`);let w=(l==="undo"?p.inverse:[p.plan.intent]).map(G=>({...G,expectedRevision:y()})),v=r.getRows(),R=l==="undo"?p.plan.before:p.plan.after;De(v,R,w,r,r.treeData===!0);let b={before:v,after:R,intent:w[0],rowIds:p.plan.rowIds,changed:!0};return z(b,l,w,p)},Le=l=>{try{if(n||o)return!1;let p=r.getRows(),w=y();if(w!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==w)return!1;let v=x?.rowIds===l.rowIds?x.sources:JSON.stringify(l.rowIds.map(String));return(!x||x.rows!==p||x.revision!==w||x.sources!==v)&&(x={rows:p,revision:w,sources:v,rowIds:l.rowIds,validate:Te(p,l.rowIds,r,r.treeData===!0)}),x.rowIds=l.rowIds,x.validate(l),!0}catch(p){return p instanceof Error?p.message:!1}},ke=l=>{let p=l.intent;if(l.applied||!p)return;q(l.api);let w=t,v=h?.intent===p?h:void 0;Promise.resolve().then(()=>ge(Me(p),v)).catch(R=>{v&&v.generation!==t&&m(v),!n&&t===w&&e.status!=="saving"&&g({...e,status:R instanceof P?"conflict":"error",error:R})})},ye=l=>n?!1:(q(l),o?"Wait for the current row order to finish saving.":Z(l)||"Save or discard edited rows before changing their order.");return{get state(){return M(),e},bindings:{applyRowDrag:!1,rowDragPolicy:{canDrag:({api:l})=>ye(l),canDrop:({intent:l,api:p})=>{let w=ye(p);if(w!==!0)return w;try{return Le(Me(l))}catch(v){return v instanceof Error?v.message:!1}},getRevision:y},onRowDragEnd:ke},preview:N,move:Pe,undo:()=>fe("undo"),redo:()=>fe("redo"),subscribe(l){return n?()=>{}:(S.add(l),()=>{S.delete(l)})},dispose(){n=!0,t++,o?.abort(),o=null,i=null,a(),c(),m(),E(),s&&!s.isDestroyed()&&s.view.refreshCells(),s=null,x=null,S.clear(),e=Object.freeze({...e,status:"idle",error:null,plan:null,canUndo:!1,canRedo:!1})}}}0&&(module.exports={RowOrderConflictError,createRowOrderController,moveTreeNodes,reorderRows});
|
package/dist/ordering.d.cts
CHANGED
package/dist/ordering.d.ts
CHANGED
package/dist/ordering.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var Ne=new Set(["__proto__","prototype","constructor"]),Y=new Map;function Ge(r){if(typeof r!="string"||r.length===0)return null;let t=Y.get(r);if(t!==void 0)return t;let e=r.split("."),n=e.every(o=>o.length>0&&!Ne.has(o))?e:null;return r.length<=1024&&(Y.size>=256&&Y.delete(Y.keys().next().value),Y.set(r,n)),n}function V(r,t){if(r==null)return;let e=Ge(t);if(!e)return;let n=r;for(let o of e){if(n==null)return;n=n[o]}return n}function Fe(r,t){let e=typeof t.rowKey=="function"?t.rowKey(r):V(r,t.rowKey);if(e==null||String(e).length===0)throw new Error("[MachTable] Row ordering requires a non-empty stable row key.");return String(e)}function He(r,t){let e=r[t];if(e==null)return[];if(!Array.isArray(e))throw new Error(`[MachTable] Tree ordering expects an array at "${t}".`);return e}function _(r,t,e,n){let o={entries:[],byId:new Map,byRow:new Map,children:new Map([[null,r]])},i=[...r].reverse().map(a=>({row:a,parentId:null,selectedAncestor:!1})),s=e.childrenKey??"children";for(;i.length;){let a=i.pop(),c=Fe(a.row,e);if(o.byId.has(c))throw new Error(`[MachTable] Row ordering found duplicate or cyclic row key "${c}".`);let d=n?He(a.row,s):[],w={...a,id:c,children:d};o.entries.push(w),o.byId.set(c,w),o.byRow.set(w.row,w),o.children.set(c,d);for(let f=d.length-1;f>=0;f--)i.push({row:d[f],parentId:c,selectedAncestor:a.selectedAncestor||t.has(c)})}return o}function Be(r){if(!["before","after","inside"].includes(r.position))throw new Error("[MachTable] Unknown row ordering position.");let t=[...new Set(r.rowIds.map(String))];if(!t.length)throw new Error("[MachTable] Row ordering requires at least one source row.");return{...r,rowIds:t,targetId:String(r.targetId)}}function ie(r,t){for(let n of t)if(!r.byId.has(n))throw new Error(`[MachTable] Ordering source "${n}" is unavailable.`);let e=new Set(t);return{selected:e,moved:r.entries.filter(n=>e.has(n.id)&&!n.selectedAncestor)}}function be(r,t,e,n){let o=r.byId.get(e.targetId);if(!o)throw new Error(`[MachTable] Ordering target "${e.targetId}" is unavailable.`);if(t.selected.has(o.id)||o.selectedAncestor)throw new Error("[MachTable] A row cannot be moved into or relative to its own subtree.");let i=e.position==="inside"?o.id:o.parentId,s=i===null?null:r.byId.get(i).row;if(n.canMove)for(let a of t.moved){let c=a.parentId===null?null:r.byId.get(a.parentId).row,d=n.canMove({row:a.row,previousParent:c,parent:s,target:o.row,intent:e});if(d===!1||typeof d=="string")throw new Error(typeof d=="string"?d:"[MachTable] This row cannot be moved to the selected parent.")}return i}function Re(r,t,e,n){let o=[...new Set(t.map(String))];if(!o.length)throw new Error("[MachTable] Row ordering requires at least one source row.");let i=_(r,new Set(o),e,n),s=ie(i,o);return a=>{if(!n&&a.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");be(i,s,a,e)}}function qe(r,t,e,n){let o=new Set(t.map(d=>d.id)),i=new Map;for(let d of t)i.has(d.parentId)||i.set(d.parentId,r.children.get(d.parentId).filter(w=>!o.has(r.byRow.get(w).id)));let s=i.get(e)??r.children.get(e),a=s.findIndex(d=>d===r.byId.get(n.targetId).row),c=n.position==="inside"?s.length:a+ +(n.position==="after");return i.set(e,[...s.slice(0,c),...t.map(d=>d.row),...s.slice(c)]),i}function ve(r,t){return r.length===t.length&&r.every((e,n)=>e===t[n])}function Ke(r,t,e,n){let o=new Map,i=n.childrenKey??"children";for(let a=t.entries.length-1;a>=0;a--){let c=t.entries[a],w=(e.get(c.id)??c.children).map(f=>o.get(f)??f);ve(c.children,w)||o.set(c.row,{...c.row,[i]:w})}let s=(e.get(null)??r).map(a=>o.get(a)??a);return ve(r,s)?r:s}function Ue(r,t,e,n){let o=e.indexOf(t[0].row),i=o+t.length;return{...r,rowIds:t.map(s=>s.id),beforeId:o>0?n.byRow.get(e[o-1]).id:null,afterId:i<e.length?n.byRow.get(e[i]).id:null}}function Te(r,t,e){let n=_(r.before,new Set(r.rowIds),t,e),o=new Map;for(let i of n.children.values())i.forEach((s,a)=>o.set(n.byRow.get(s).id,a));return ie(n,r.rowIds).moved.reverse().map(i=>{let s=n.children.get(i.parentId),a=o.get(i.id),c=s[a+1];if(c)return{rowIds:[i.id],targetId:n.byRow.get(c).id,position:"before"};if(i.parentId!==null)return{rowIds:[i.id],targetId:i.parentId,position:"inside"};let d=s[a-1];if(!d)throw new Error("[MachTable] Row ordering cannot restore an isolated root.");return{rowIds:[i.id],targetId:n.byRow.get(d).id,position:"after"}})}function Ie(r,t,e,n,o){if(!n.canMove)return;let i=_(r,new Set,n,o),s=_(t,new Set,n,o);for(let a of e)for(let c of a.rowIds){let d=i.byId.get(c),w=s.byId.get(c),f=w.parentId===null?null:i.byId.get(w.parentId).row,O=d.parentId===null?null:i.byId.get(d.parentId).row,S=n.canMove({row:d.row,previousParent:O,parent:f,target:i.byId.get(a.targetId).row,intent:a});if(S===!1||typeof S=="string")throw new Error(typeof S=="string"?S:"[MachTable] This row cannot be restored to its previous parent.")}}function De(r,t,e,n){let o=Be(t);if(!n&&o.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");let i=_(r,new Set(o.rowIds),e,n),{moved:s,selected:a}=ie(i,o.rowIds),c=be(i,{moved:s,selected:a},o,e),d=qe(i,s,c,o),w=Ke(r,i,d,e),f=Ue(o,s,d.get(c),i);return{before:r,after:w,intent:f,rowIds:f.rowIds,changed:r!==w}}function se(r,t,e){return De(r,t,e,!1)}function ae(r,t,e){return De(r,t,e,!0)}function le(r,t,e){let n=document.createElement(r);if(t&&(n.className=t),e)for(let o of Object.keys(e))n.setAttribute(o,e[o]);return n}function C(r,t,e){return r<t?t:r>e?e:r}var de=Symbol.for("mach-table.adapter.lifecycle.v1");function J(r){return!r.isDestroyed()&&(r[de]?.canReplaceData()??!1)}function ce(r,t){let e=r[de]?.rowDrag;return e?.original===t&&e.policy===r.getOption("rowDragPolicy")}function ue(r,t){let e=r[de];if(!e)return()=>{};e.rowDrag?.activeInteraction?.destroy();let n={original:t,policy:r.getOption("rowDragPolicy"),factory:o=>(n.activeInteraction=t(o),n.activeInteraction)};return e.rowDrag=n,()=>{n.activeInteraction?.destroy(),e.rowDrag===n&&(e.rowDrag=void 0)}}function Ye(r,t){if(r instanceof Error)return r;let e=new Error(typeof r=="string"?r:t);return e.name="AbortError",e}var _e=()=>Ye(void 0,"[MachTable] Operation cancelled.");function Oe(r,t,e=_e){return t?new Promise((n,o)=>{let i=()=>{t.removeEventListener("abort",i),o(e())};if(r.then(n,o).finally(()=>t.removeEventListener("abort",i)),t.aborted){i();return}t.addEventListener("abort",i,{once:!0})}):r}function pe(r,t,e){return r.replace("{a}",String(t)).replace("{b}",String(e))}function $e(r,t,e){return r&&t>.25&&t<.75?"inside":e}function xe(r,t){let e=0,n=r.length;for(;e<n;){let o=e+n>>>1;r[o]<t?e=o+1:n=o}return e}function We(r,t,e,n,o,i){let s=r.getOption("locale")??{},a=t>1?pe(s.rowDragSelection??"\u5DF2\u9009 {a} \u884C \u2192 \u7B2C {b} \u884C",t,n+1):pe(s.rowDragPosition??"\u7B2C {a} \u884C \u2192 \u7B2C {b} \u884C",e+1,n+1),c=o==="inside"?s.rowDragInside??"\u79FB\u5165":o==="before"?s.rowDragBefore??"\u4E4B\u524D":s.rowDragAfter??"\u4E4B\u540E";return{text:`${a} \xB7 ${c}`,indicatorStyle:o==="inside"?`height:${i}px;background:color-mix(in srgb,var(--mach-primary) 12%,transparent);box-shadow:inset 0 0 0 1px var(--mach-primary)`:void 0}}function ze(r,t,e){let n={parents:new Map,siblings:new Map([[null,[]]]),depths:new Map},o=[...r].reverse().map(s=>({row:s,parent:null,depth:0})),i=t.childrenKey??"children";for(;o.length;){let{row:s,parent:a,depth:c}=o.pop(),d=String(typeof t.rowKey=="function"?t.rowKey(s):V(s,t.rowKey));if(n.depths.has(d))throw new Error("[MachTable] Row ordering requires unique, acyclic row keys.");n.depths.set(d,c),n.siblings.get(a).push(d),n.siblings.set(d,[]),a!==null&&n.parents.set(d,a);let w=e?s[i]:void 0;if(w!=null&&!Array.isArray(w))throw new Error("[MachTable] Row ordering children must be an array.");for(let f=(w?.length??0)-1;f>=0;f--)o.push({row:w[f],parent:d,depth:c+1})}return n}function Ee(r,t,e,n,o,i){let s=new Map;r.rows.forEach(u=>{!u.loading&&!u.isDetail&&!u.isGroup&&u.data!=null&&s.set(u.id,u)});let a=ze(e,n,o),c=new Set(t.selected?[...a.depths.keys()].filter(u=>s.get(u)?.selected):[t.id]),d=[...c],w=u=>{let g=u;for(;g!==void 0;){if(c.has(g))return!0;g=a.parents.get(g)}return!1},f=new Map,O=new Map;for(let[u,g]of a.siblings){let y=g.filter(m=>!c.has(m));f.set(u,y),y.forEach((m,T)=>O.set(m,T))}let S=new Map,A=new Set,x=[],E=[],B=0;r.rows.forEachDisplayed(u=>{A.add(u.id),w(u.id)&&x.push(u.rowIndex),B=u.rowIndex+1;let g=a.depths.get(u.id)??0;for(;E.length&&(a.depths.get(E[E.length-1].id)??0)>=g;)S.set(E.pop().id,u.rowIndex);a.siblings.get(u.id)?.length&&E.push(u)});for(let u of E)S.set(u.id,B);return{rowIds:d,isSource:w,validate:()=>{for(let u of d){let g=s.get(u);if(g.treeLoading)return!1;let y=r.getOption("rowDragPolicy")?.canDrag?.({node:g,api:r});if(y===!1||typeof y=="string")return y}return!0},resolve:({targetNode:u,position:g,fraction:y,height:m})=>{let T=$e(o,y,g);if(w(u.id))return"Cannot target own subtree.";if(!o&&T==="inside")return!1;let k=T==="inside"?u.id:a.parents.get(u.id)??null,M=f.get(k)??[],q=T==="inside"?M.length:(O.get(u.id)??0)+ +(T==="after"),N=T==="before"?u.rowIndex:S.get(u.id)??u.rowIndex+1,z=N-xe(x,N)+xe(x,t.rowIndex);return{intent:{rowIds:d,targetId:u.id,position:T,beforeId:M[q-1]??null,afterId:M[q]??null,expectedRevision:i},indicatorIndex:T==="inside"?u.rowIndex:T==="after"?S.get(u.id):void 0,toIndex:N,targetIndex:z,...We(r,d.length,t.rowIndex,u.rowIndex,T,m)}},keyboardTarget:u=>{let g=t.id;for(;c.has(a.parents.get(g)??"");)g=a.parents.get(g);let y=a.siblings.get(a.parents.get(g)??null)??[],m=y.indexOf(g)+u;for(;m>=0&&m<y.length;){let T=y[m];if(!w(T)&&A.has(T)&&s.has(T))return s.get(T);m+=u}}}}var Q=class{constructor(t){this.host=t;this.e=null;this.n=0;this.o=0;this.l=t=>{let e=this.e;if(!(!e||t.pointerId!==e.pointerId)){if(e.clientX=t.clientX,e.clientY=t.clientY,!e.active){if(Math.abs(t.clientY-e.startY)<5)return;e.active=!0,this.host.root.classList.add("mach-root--row-dragging"),this.host.root.querySelectorAll(".mach-row").forEach(n=>n.classList.toggle("mach-row--drag-source",this.sourceContains(n.dataset.id??"")))}this.u()}};this.y=()=>{if(this.n=0,!!this.e?.active){if(!this.current()){this.destroy();return}this.p(),this.m()&&this.u()}};this.d=t=>{let e=this.e;if(!(!e||t.pointerId!==e.pointerId)){e.clientX=t.clientX,e.clientY=t.clientY,e.active&&this.current()&&this.p(),this.h();try{e.active&&e.drop?.valid&&this.s(t.clientX,t.clientY)&&this.current()&&this.commitDrop(e.drop)}finally{this.release()}}};this.c=t=>{t.key==="Escape"&&(t.preventDefault(),this.destroy())};this.i=t=>{t.type==="pointercancel"&&t.pointerId!==this.e?.pointerId||this.destroy()}}begin(t){let e=this.host.containers.map(o=>{let i=le("div","mach-row-drop-indicator");return i.hidden=!0,o.append(i),i}),n=le("div","mach-row-drag-preview");n.setAttribute("role","status"),n.hidden=!0,this.host.root.append(n),this.e={startY:t.clientY,clientX:t.clientX,clientY:t.clientY,pointerId:t.pointerId,indicators:e,preview:n,drop:null,active:!1},window.addEventListener("pointermove",this.l),window.addEventListener("pointerup",this.d),window.addEventListener("pointercancel",this.i),window.addEventListener("keydown",this.c),window.addEventListener("blur",this.i)}g(t){return this.e?.active===!0&&this.sourceContains(t)}f(t,e=()=>!0){this.o=requestAnimationFrame(()=>{this.o=0,e()&&[...this.host.root.querySelectorAll(".mach-row-drag-handle")].find(n=>n.closest(".mach-row")?.dataset.id===t)?.focus()})}u(){this.n||(this.n=requestAnimationFrame(this.y))}s(t,e,n=0){let o=this.host.body.getBoundingClientRect();return t>=o.left&&t<=o.right&&e>=o.top-n&&e<=o.bottom+n}m(){let t=this.e;if(!t||!this.s(t.clientX,t.clientY,28))return!1;let e=this.host.viewport,n=e.getBoundingClientRect(),o=Math.min(36,n.height/3);if(o<=0)return!1;let i=t.clientY<n.top+o?-Math.ceil(14*C((n.top+o-t.clientY)/o,0,1)):Math.ceil(14*C((t.clientY-n.bottom+o)/o,0,1));if(this.host.scrollBy)return this.host.scrollBy(i);let s=e.scrollTop;return e.scrollTop=C(s+i,0,Math.max(0,e.scrollHeight-e.clientHeight)),e.scrollTop!==s}p(){let t=this.e;if(!t?.active)return;let e=t.clientY-this.host.viewport.getBoundingClientRect().top+this.host.viewport.scrollTop,n=this.describe(e);t.drop=n;let o=this.s(t.clientX,t.clientY);for(let s of t.indicators){if(s.hidden=!o||!n?.valid,!n)continue;s.dataset.dropPosition=n.position??"",s.style.cssText=n.style??"";let a=parseFloat(s.parentElement.style.height),c=Math.min(this.host.getRowTop(n.boundary),Number.isFinite(a)?Math.max(0,a-2):1/0);s.style.transform=`translate3d(0, ${c}px, 0)`}let i=this.host.root.getBoundingClientRect();t.preview.hidden=!1,t.preview.textContent=o?n?.text??this.outsideText():this.outsideText(),t.preview.style.transform=`translate3d(${C(t.clientX-i.left+16,8,Math.max(8,i.width-224))}px, ${C(t.clientY-i.top+16,8,Math.max(8,i.height-48))}px, 0)`}h(){let t=this.e;this.e=null,window.removeEventListener("pointermove",this.l),window.removeEventListener("pointerup",this.d),window.removeEventListener("pointercancel",this.i),window.removeEventListener("keydown",this.c),window.removeEventListener("blur",this.i),cancelAnimationFrame(this.n),this.n=0,this.host.root?.classList.remove("mach-root--row-dragging"),this.host.root?.querySelectorAll(".mach-row--drag-source").forEach(e=>e.classList.remove("mach-row--drag-source")),t?.indicators.forEach(e=>e.remove()),t?.preview.remove()}destroy(){this.h(),this.release(),cancelAnimationFrame(this.o),this.o=0}};var Z=class extends Q{constructor(e,n,o){super(e);this.t=e;this.prepare=n;this.v=o;this.scope=null;this.a=!1}r(e,n,o){try{return n()}catch(i){return this.scope&&(this.scope.failed=!0),this.t.report(i,e),o}}w(e){return this.r("prepare",()=>{let n=this.t.capture(e),o=this.prepare(e);return this.scope={node:e,from:e.rowIndex,guard:n,session:o,sourceDecision:!0,failed:!1},this.scope.sourceDecision=o.validate(),this.scope},null)}start(e,n){this.destroy(),this.w(n)&&this.current()&&this.begin(e)}moveWithKeyboard(e,n){this.destroy();let o=this.w(n);if(!(!o||!this.current())){this.a=!0;try{let i=e.key==="ArrowUp"?-1:1,s=this.r("keyboardTarget",()=>o.session.keyboardTarget(i),void 0);s&&this.commitDrop(this.destination(s,i<0?0:1))}finally{this.a=!1,this.release()}}}isSource(e){return this.g(e.id)}current(){return!!(this.scope&&!this.scope.failed&&this.scope.guard.current()&&this.t.api.rows.getById(this.scope.node.id)===this.scope.node)}sourceContains(e){return this.r("isSource",()=>this.scope?.session.isSource(e)??!1,!1)}release(){this.scope=null}outsideText(){return this.t.api.getOption("locale")?.rowDragOutside??"\u79FB\u56DE\u8868\u683C\u5185\u653E\u7F6E \xB7 Esc \u53D6\u6D88"}describe(e){let n=this.t.api,o=C(this.host.findRowAt(e),0,n.rows.getCount()-1),i=n.rows.getAt(o);if(i?.isDetail&&(i=n.rows.getById(i.masterId)),!i)return null;let s=this.host.getRowTop(i.rowIndex),a=this.host.getRowTop(i.rowIndex+1)-s;return this.destination(i,(e-s)/Math.max(1,a))}destination(e,n){let o=this.scope,i=n<.5?"before":"after",s=this.host.getRowTop(e.rowIndex+1)-this.host.getRowTop(e.rowIndex),a=this.r("resolve",()=>o.session.resolve({targetNode:e,position:i,fraction:n,height:s}),!1),c=typeof o.sourceDecision=="string"?o.sourceDecision:typeof a=="string"?a:"\u6B64\u4F4D\u7F6E\u4E0D\u53EF\u653E\u7F6E",d={node:e,fraction:n,boundary:e.rowIndex,valid:!1,text:c};if(!a||typeof a=="string"||o.sourceDecision!==!0||e.loading||e.treeLoading||e.isGroup)return d;d.intent=a.intent,d.position=a.intent.position,d.toIndex=a.toIndex,d.targetIndex=a.targetIndex,d.boundary=a.indicatorIndex??e.rowIndex+ +(a.intent.position==="after"),d.style=a.indicatorStyle,d.text=a.text??c;let w=this.r("canDrop",()=>this.t.api.getOption("rowDragPolicy")?.canDrop?.({node:o.node,api:this.t.api,targetNode:e,intent:a.intent})??!0,!1);return d.valid=w===!0,typeof w=="string"&&(d.text=w),d}commitDrop(e){let n=this.scope;if(!n||!this.current()||this.r("validate",()=>n.session.validate(),!1)!==!0||!this.current())return!1;let o=this.destination(e.node,e.fraction);return!o.valid||!o.intent||!this.current()?!1:(this.a&&this.v(o.intent,i=>this.f(n.node.id,i)),this.t.emit(n.node,n.from,o.toIndex,o.targetIndex,o.intent),!0)}};function $(r,t,e){let n=[],o=[...r].reverse().map(s=>({row:s,parent:null})),i=new Set;for(;o.length;){let s=o.pop();if(i.has(s.row))throw new Error("[MachTable] Installed order contains duplicate or cyclic rows.");i.add(s.row),n.push(s);let a=e?s.row[t]??[]:[];for(let c=a.length-1;c>=0;c--)o.push({row:a[c],parent:s.row})}return n}function H(r,t,e,n){try{let o=$(r,e,n);return o.length===t.length&&o.every((i,s)=>i.row===t[s].row&&i.parent===t[s].parent)}catch{return!1}}function we(r){let t=[];return r?.rows.forEach(e=>t.push({node:e,data:e.data})),t}function W(r,t){if(!r)return!0;if(r.isDestroyed())return!1;let e=0,n=!0;return r.rows.forEach(o=>{let i=t[e++];(i?.node!==o||i.data!==o.data)&&(n=!1)}),n&&e===t.length}var ee=class{constructor(){this.entries=[];this.cursor=0}get canUndo(){return this.cursor>0}get canRedo(){return this.cursor<this.entries.length}peek(t){return this.entries[t==="undo"?this.cursor-1:this.cursor]}commit(t,e){t==="undo"?this.cursor--:t==="redo"?this.cursor++:(this.entries.splice(this.cursor),this.entries.push(e),this.entries.length>50&&this.entries.shift(),this.cursor=this.entries.length)}clear(){this.entries=[],this.cursor=0}};var P=class extends Error{constructor(e="The row order changed on the server. Reload before trying again.",n){super(e);this.serverRevision=n;this.name="RowOrderConflictError"}};function je(r){if(r?.status==="conflict")throw new P(r.message,r.revision);return r?.revision}function Se(r){if(r.targetId===null)throw new Error("[MachTable] Row ordering requires an existing destination.");return{...r,targetId:r.targetId}}function Xe(r){let t={status:"idle",error:null,revision:r.getRevision?.(),plan:null,canUndo:!1,canRedo:!1},e=0,n=!1,o=null,i=null,s=null,a=()=>{},c=()=>{},d=0,w=null,f=new ee,O=null,S=new Set,A=r.childrenKey??"children",x=null,E=()=>{f.clear(),O=null},B=()=>{if(!o)return!1;let l=o;return e++,o=null,i=null,l.abort(),!0},u=(l=t.status)=>({canUndo:!n&&l!=="saving"&&f.canUndo,canRedo:!n&&l!=="saving"&&f.canRedo}),g=l=>{t=Object.freeze({...l,...u(l.status)}),s&&!s.isDestroyed()&&s.view.refreshCells();let p=[h=>r.onStateChange?.(h),...S];for(let h of p)try{h?.(t)}catch(v){console.error("[MachTable] Row order state observer failed",v)}},y=()=>r.getRevision?.()??t.revision,m=(l=w)=>{l?.release(),w===l&&(w=null)},T=()=>{!i||i()||(B(),E(),m(),g({...t,status:"idle",plan:null,error:null}))},k=l=>new Z(l,p=>Ee(l.api,p,r.getRows(),r,r.treeData===!0,y()),(p,h)=>{m();let v=document.activeElement,R=G=>{G.target!==v&&G.target!==document.body&&m(b)},b={api:l.api,intent:p,restore:h,generation:e+1,release:()=>document.removeEventListener("focusin",R,!0)};w=b,document.addEventListener("focusin",R,!0)}),M=()=>{T(),!o&&O&&(O.api!==s||O.revision!==y()||!W(s,O.nodes)||!H(r.getRows(),O.rows,A,r.treeData===!0))&&E();let l=u();(t.canUndo!==l.canUndo||t.canRedo!==l.canRedo)&&(t=Object.freeze({...t,...l}))},q=l=>{if(l===s){T(),ce(l,k)||(c(),c=ue(l,k));return}let p=B();a(),c(),m(),E(),s=l,c=ue(l,k),a=l.on("cellValueChanged",()=>{d++;let h=f.canUndo||f.canRedo;E(),h&&g({...t,status:o?"saving":"idle",plan:o?t.plan:null,error:null})}),p&&g({...t,status:"idle",plan:null,error:null})},N=l=>{if(M(),n)throw new Error("[MachTable] The row order controller is disposed.");if(o)throw new Error("[MachTable] A row order save is already in progress.");let p=y();if(p!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==p)throw new P;let h={...l,expectedRevision:l.expectedRevision??p};return(r.treeData?ae:se)(r.getRows(),h,r)},z=async(l,p,h,v,R)=>{if(!l.changed)return l;let b=s;if(b&&!J(b))throw new Error("[MachTable] Save or discard edited rows before changing their order.");let G=++e;x=null;let j=new AbortController;o=j,i=null;let X=y(),Le=d,F=null,K=[],ye=!1,te=!1,re=X,me=I=>{if(!R)return;if(!I||R!==w||R.api!==s){m(R);return}let D=O;R.restore(()=>{let oe=D!==null&&D===O&&D.revision===y()&&W(R.api,D.nodes)&&H(r.getRows(),D.rows,A,r.treeData===!0)&&w===R&&!n&&e===R.generation&&s===R.api&&ce(R.api,k);return m(R),oe})},L=()=>!n&&e===G,ne=()=>L()&&(!b||s===b&&!b.isDestroyed())&&F!==null&&y()===X&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0),Pe=I=>{if(!ne()){L()&&!F&&g({...t,status:"error",error:I,plan:l});return}let D=!b||J(b),U=D?I:Object.assign(new Error("[MachTable] Row order could not be saved. Automatic rollback was skipped to preserve new edits. Save or discard those edits, then reload the row order."),{cause:I});if(D){i=null;try{r.applyRows(l.before),te=!0}catch(oe){U=Object.assign(new Error("[MachTable] Row order could not be saved or restored. Reload the row order before trying again."),{cause:I,rollbackError:oe})}}L()&&(te||E(),g({...t,status:I instanceof P?"conflict":"error",error:U}))},ke=()=>{if(!L()){me(!1);return}let I=y(),D=!1;ye&&L()&&(I===X||I===re)&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0)?(O={rows:F,nodes:K,api:b,revision:I},D=!0):te&&L()?(O={rows:$(r.getRows(),A,r.treeData===!0),nodes:we(b),api:b,revision:I},D=H(r.getRows(),$(l.before,A,r.treeData===!0),A,r.treeData===!0)):E(),o===j&&(o=null,i=null,s&&!s.isDestroyed()&&s.view.refreshCells()),L()&&t.status==="saving"&&g({...t,status:"idle",plan:null}),M(),me(D)};try{r.applyRows(l.after),F=$(r.getRows(),A,r.treeData===!0),K=we(b);let I=r.getRows(),D=K[0]?.node;if(i=b&&D?()=>!b.isDestroyed()&&b.rows.getById(D.id)===D:()=>r.getRows()===I,g({...t,status:"saving",error:null,plan:l}),!ne())return l;let U=je(await Oe(Promise.resolve(r.save?.(l.intent,{signal:j.signal,plan:l,operation:p,operations:h})),j.signal));return ne()&&(ye=!0,re=U??X,d===Le?f.commit(p,v):E(),g({...t,status:"saved",revision:re})),l}catch(I){throw Pe(I),I}finally{ke()}},he=async(l,p)=>{p!==w&&(p=void 0),w!==p&&m();let h=N(l);h.changed||m(p);let v=h.changed?Te(h,r,r.treeData===!0):[];return z(h,"move",[h.intent],{plan:h,inverse:v},p)},Ae=l=>he(l),ge=async l=>{if(m(),M(),n||o)throw new Error("[MachTable] Row ordering is unavailable while disposed or saving.");let p=f.peek(l);if(!p)throw new Error(`[MachTable] Nothing to ${l}.`);let h=(l==="undo"?p.inverse:[p.plan.intent]).map(G=>({...G,expectedRevision:y()})),v=r.getRows(),R=l==="undo"?p.plan.before:p.plan.after;Ie(v,R,h,r,r.treeData===!0);let b={before:v,after:R,intent:h[0],rowIds:p.plan.rowIds,changed:!0};return z(b,l,h,p)},Me=l=>{try{if(n||o)return!1;let p=r.getRows(),h=y();if(h!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==h)return!1;let v=x?.rowIds===l.rowIds?x.sources:JSON.stringify(l.rowIds.map(String));return(!x||x.rows!==p||x.revision!==h||x.sources!==v)&&(x={rows:p,revision:h,sources:v,rowIds:l.rowIds,validate:Re(p,l.rowIds,r,r.treeData===!0)}),x.rowIds=l.rowIds,x.validate(l),!0}catch(p){return p instanceof Error?p.message:!1}},Ce=l=>{let p=l.intent;if(l.applied||!p)return;q(l.api);let h=e,v=w?.intent===p?w:void 0;Promise.resolve().then(()=>he(Se(p),v)).catch(R=>{v&&v.generation!==e&&m(v),!n&&e===h&&t.status!=="saving"&&g({...t,status:R instanceof P?"conflict":"error",error:R})})},fe=l=>n?!1:(q(l),o?"Wait for the current row order to finish saving.":J(l)||"Save or discard edited rows before changing their order.");return{get state(){return M(),t},bindings:{applyRowDrag:!1,rowDragPolicy:{canDrag:({api:l})=>fe(l),canDrop:({intent:l,api:p})=>{let h=fe(p);if(h!==!0)return h;try{return Me(Se(l))}catch(v){return v instanceof Error?v.message:!1}},getRevision:y},onRowDragEnd:Ce},preview:N,move:Ae,undo:()=>ge("undo"),redo:()=>ge("redo"),subscribe(l){return n?()=>{}:(S.add(l),()=>{S.delete(l)})},dispose(){n=!0,e++,o?.abort(),o=null,i=null,a(),c(),m(),E(),s&&!s.isDestroyed()&&s.view.refreshCells(),s=null,x=null,S.clear(),t=Object.freeze({...t,status:"idle",error:null,plan:null,canUndo:!1,canRedo:!1})}}}export{P as RowOrderConflictError,Xe as createRowOrderController,ae as moveTreeNodes,se as reorderRows};
|
|
1
|
+
var Ge=new Set(["__proto__","prototype","constructor"]),Y=new Map;function Fe(r){if(typeof r!="string"||r.length===0)return null;let e=Y.get(r);if(e!==void 0)return e;let t=r.split("."),n=t.every(o=>o.length>0&&!Ge.has(o))?t:null;return r.length<=1024&&(Y.size>=256&&Y.delete(Y.keys().next().value),Y.set(r,n)),n}function V(r,e){if(r==null)return;let t=Fe(e);if(!t)return;let n=r;for(let o of t){if(n==null)return;n=n[o]}return n}function He(r,e){let t=typeof e.rowKey=="function"?e.rowKey(r):V(r,e.rowKey);if(t==null||String(t).length===0)throw new Error("[MachTable] Row ordering requires a non-empty stable row key.");return String(t)}function Be(r,e){let t=r[e];if(t==null)return[];if(!Array.isArray(t))throw new Error(`[MachTable] Tree ordering expects an array at "${e}".`);return t}function _(r,e,t,n){let o={entries:[],byId:new Map,byRow:new Map,children:new Map([[null,r]])},i=[...r].reverse().map(a=>({row:a,parentId:null,selectedAncestor:!1})),s=t.childrenKey??"children";for(;i.length;){let a=i.pop(),c=He(a.row,t);if(o.byId.has(c))throw new Error(`[MachTable] Row ordering found duplicate or cyclic row key "${c}".`);let d=n?Be(a.row,s):[],h={...a,id:c,children:d};o.entries.push(h),o.byId.set(c,h),o.byRow.set(h.row,h),o.children.set(c,d);for(let f=d.length-1;f>=0;f--)i.push({row:d[f],parentId:c,selectedAncestor:a.selectedAncestor||e.has(c)})}return o}function qe(r){if(!["before","after","inside"].includes(r.position))throw new Error("[MachTable] Unknown row ordering position.");let e=[...new Set(r.rowIds.map(String))];if(!e.length)throw new Error("[MachTable] Row ordering requires at least one source row.");return{...r,rowIds:e,targetId:String(r.targetId)}}function ie(r,e){for(let n of e)if(!r.byId.has(n))throw new Error(`[MachTable] Ordering source "${n}" is unavailable.`);let t=new Set(e);return{selected:t,moved:r.entries.filter(n=>t.has(n.id)&&!n.selectedAncestor)}}function be(r,e,t,n){let o=r.byId.get(t.targetId);if(!o)throw new Error(`[MachTable] Ordering target "${t.targetId}" is unavailable.`);if(e.selected.has(o.id)||o.selectedAncestor)throw new Error("[MachTable] A row cannot be moved into or relative to its own subtree.");let i=t.position==="inside"?o.id:o.parentId,s=i===null?null:r.byId.get(i).row;if(n.canMove)for(let a of e.moved){let c=a.parentId===null?null:r.byId.get(a.parentId).row,d=n.canMove({row:a.row,previousParent:c,parent:s,target:o.row,intent:t});if(d===!1||typeof d=="string")throw new Error(typeof d=="string"?d:"[MachTable] This row cannot be moved to the selected parent.")}return i}function Re(r,e,t,n){let o=[...new Set(e.map(String))];if(!o.length)throw new Error("[MachTable] Row ordering requires at least one source row.");let i=_(r,new Set(o),t,n),s=ie(i,o);return a=>{if(!n&&a.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");be(i,s,a,t)}}function Ke(r,e,t,n){let o=new Set(e.map(d=>d.id)),i=new Map;for(let d of e)i.has(d.parentId)||i.set(d.parentId,r.children.get(d.parentId).filter(h=>!o.has(r.byRow.get(h).id)));let s=i.get(t)??r.children.get(t),a=s.findIndex(d=>d===r.byId.get(n.targetId).row),c=n.position==="inside"?s.length:a+ +(n.position==="after");return i.set(t,[...s.slice(0,c),...e.map(d=>d.row),...s.slice(c)]),i}function ve(r,e){return r.length===e.length&&r.every((t,n)=>t===e[n])}function Ue(r,e,t,n){let o=new Map,i=n.childrenKey??"children";for(let a=e.entries.length-1;a>=0;a--){let c=e.entries[a],h=(t.get(c.id)??c.children).map(f=>o.get(f)??f);ve(c.children,h)||o.set(c.row,{...c.row,[i]:h})}let s=(t.get(null)??r).map(a=>o.get(a)??a);return ve(r,s)?r:s}function Ye(r,e,t,n){let o=t.indexOf(e[0].row),i=o+e.length;return{...r,rowIds:e.map(s=>s.id),beforeId:o>0?n.byRow.get(t[o-1]).id:null,afterId:i<t.length?n.byRow.get(t[i]).id:null}}function Te(r,e,t){let n=_(r.before,new Set(r.rowIds),e,t),o=new Map;for(let i of n.children.values())i.forEach((s,a)=>o.set(n.byRow.get(s).id,a));return ie(n,r.rowIds).moved.reverse().map(i=>{let s=n.children.get(i.parentId),a=o.get(i.id),c=s[a+1];if(c)return{rowIds:[i.id],targetId:n.byRow.get(c).id,position:"before"};if(i.parentId!==null)return{rowIds:[i.id],targetId:i.parentId,position:"inside"};let d=s[a-1];if(!d)throw new Error("[MachTable] Row ordering cannot restore an isolated root.");return{rowIds:[i.id],targetId:n.byRow.get(d).id,position:"after"}})}function Ie(r,e,t,n,o){if(!n.canMove)return;let i=_(r,new Set,n,o),s=_(e,new Set,n,o);for(let a of t)for(let c of a.rowIds){let d=i.byId.get(c),h=s.byId.get(c),f=h.parentId===null?null:i.byId.get(h.parentId).row,O=d.parentId===null?null:i.byId.get(d.parentId).row,S=n.canMove({row:d.row,previousParent:O,parent:f,target:i.byId.get(a.targetId).row,intent:a});if(S===!1||typeof S=="string")throw new Error(typeof S=="string"?S:"[MachTable] This row cannot be restored to its previous parent.")}}function De(r,e,t,n){let o=qe(e);if(!n&&o.position==="inside")throw new Error("[MachTable] Flat rows cannot have child destinations.");let i=_(r,new Set(o.rowIds),t,n),{moved:s,selected:a}=ie(i,o.rowIds),c=be(i,{moved:s,selected:a},o,t),d=Ke(i,s,c,o),h=Ue(r,i,d,t),f=Ye(o,s,d.get(c),i);return{before:r,after:h,intent:f,rowIds:f.rowIds,changed:r!==h}}function se(r,e,t){return De(r,e,t,!1)}function ae(r,e,t){return De(r,e,t,!0)}function le(r,e,t){let n=document.createElement(r);if(e&&(n.className=e),t)for(let o of Object.keys(t))n.setAttribute(o,t[o]);return n}function Oe(r,e,t,n=!1){let o=e?"addEventListener":"removeEventListener";for(let[i,s]of Object.entries(t))r[o](i,s,n)}function C(r,e,t){return r<e?e:r>t?t:r}var de=Symbol.for("mach-table.adapter.lifecycle.v1");function J(r){return!r.isDestroyed()&&(r[de]?.canReplaceData()??!1)}function ce(r,e){let t=r[de]?.rowDrag;return t?.original===e&&t.policy===r.getOption("rowDragPolicy")}function ue(r,e){let t=r[de];if(!t)return()=>{};t.rowDrag?.activeInteraction?.destroy();let n={original:e,policy:r.getOption("rowDragPolicy"),factory:o=>(n.activeInteraction=e(o),n.activeInteraction)};return t.rowDrag=n,()=>{n.activeInteraction?.destroy(),t.rowDrag===n&&(t.rowDrag=void 0)}}function _e(r,e){if(r instanceof Error)return r;let t=new Error(typeof r=="string"?r:e);return t.name="AbortError",t}var $e=()=>_e(void 0,"[MachTable] Operation cancelled.");function xe(r,e,t=$e){return e?new Promise((n,o)=>{let i=()=>{e.removeEventListener("abort",i),o(t())};if(r.then(n,o).finally(()=>e.removeEventListener("abort",i)),e.aborted){i();return}e.addEventListener("abort",i,{once:!0})}):r}function pe(r,e,t){return r.replace("{a}",String(e)).replace("{b}",String(t))}function We(r,e,t){return r&&e>.25&&e<.75?"inside":t}function Ee(r,e){let t=0,n=r.length;for(;t<n;){let o=t+n>>>1;r[o]<e?t=o+1:n=o}return t}function ze(r,e,t,n,o,i){let s=r.getOption("locale")??{},a=e>1?pe(s.rowDragSelection??"\u5DF2\u9009 {a} \u884C \u2192 \u7B2C {b} \u884C",e,n+1):pe(s.rowDragPosition??"\u7B2C {a} \u884C \u2192 \u7B2C {b} \u884C",t+1,n+1),c=o==="inside"?s.rowDragInside??"\u79FB\u5165":o==="before"?s.rowDragBefore??"\u4E4B\u524D":s.rowDragAfter??"\u4E4B\u540E";return{text:`${a} \xB7 ${c}`,indicatorStyle:o==="inside"?`height:${i}px;background:color-mix(in srgb,var(--mach-primary) 12%,transparent);box-shadow:inset 0 0 0 1px var(--mach-primary)`:void 0}}function je(r,e,t){let n={parents:new Map,siblings:new Map([[null,[]]]),depths:new Map},o=[...r].reverse().map(s=>({row:s,parent:null,depth:0})),i=e.childrenKey??"children";for(;o.length;){let{row:s,parent:a,depth:c}=o.pop(),d=String(typeof e.rowKey=="function"?e.rowKey(s):V(s,e.rowKey));if(n.depths.has(d))throw new Error("[MachTable] Row ordering requires unique, acyclic row keys.");n.depths.set(d,c),n.siblings.get(a).push(d),n.siblings.set(d,[]),a!==null&&n.parents.set(d,a);let h=t?s[i]:void 0;if(h!=null&&!Array.isArray(h))throw new Error("[MachTable] Row ordering children must be an array.");for(let f=(h?.length??0)-1;f>=0;f--)o.push({row:h[f],parent:d,depth:c+1})}return n}function Se(r,e,t,n,o,i){let s=new Map;r.rows.forEach(u=>{!u.loading&&!u.isDetail&&!u.isGroup&&u.data!=null&&s.set(u.id,u)});let a=je(t,n,o),c=new Set(e.selected?[...a.depths.keys()].filter(u=>s.get(u)?.selected):[e.id]),d=[...c],h=u=>{let g=u;for(;g!==void 0;){if(c.has(g))return!0;g=a.parents.get(g)}return!1},f=new Map,O=new Map;for(let[u,g]of a.siblings){let y=g.filter(m=>!c.has(m));f.set(u,y),y.forEach((m,T)=>O.set(m,T))}let S=new Map,A=new Set,x=[],E=[],B=0;r.rows.forEachDisplayed(u=>{A.add(u.id),h(u.id)&&x.push(u.rowIndex),B=u.rowIndex+1;let g=a.depths.get(u.id)??0;for(;E.length&&(a.depths.get(E[E.length-1].id)??0)>=g;)S.set(E.pop().id,u.rowIndex);a.siblings.get(u.id)?.length&&E.push(u)});for(let u of E)S.set(u.id,B);return{rowIds:d,isSource:h,validate:()=>{for(let u of d){let g=s.get(u);if(g.treeLoading)return!1;let y=r.getOption("rowDragPolicy")?.canDrag?.({node:g,api:r});if(y===!1||typeof y=="string")return y}return!0},resolve:({targetNode:u,position:g,fraction:y,height:m})=>{let T=We(o,y,g);if(h(u.id))return"Cannot target own subtree.";if(!o&&T==="inside")return!1;let k=T==="inside"?u.id:a.parents.get(u.id)??null,M=f.get(k)??[],q=T==="inside"?M.length:(O.get(u.id)??0)+ +(T==="after"),N=T==="before"?u.rowIndex:S.get(u.id)??u.rowIndex+1,z=N-Ee(x,N)+Ee(x,e.rowIndex);return{intent:{rowIds:d,targetId:u.id,position:T,beforeId:M[q-1]??null,afterId:M[q]??null,expectedRevision:i},indicatorIndex:T==="inside"?u.rowIndex:T==="after"?S.get(u.id):void 0,toIndex:N,targetIndex:z,...ze(r,d.length,e.rowIndex,u.rowIndex,T,m)}},keyboardTarget:u=>{let g=e.id;for(;c.has(a.parents.get(g)??"");)g=a.parents.get(g);let y=a.siblings.get(a.parents.get(g)??null)??[],m=y.indexOf(g)+u;for(;m>=0&&m<y.length;){let T=y[m];if(!h(T)&&A.has(T)&&s.has(T))return s.get(T);m+=u}}}}var Q=class{constructor(e){this.host=e;this.e=null;this.n=0;this.o=0;this.h=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){if(t.clientX=e.clientX,t.clientY=e.clientY,!t.active){if(Math.abs(e.clientY-t.startY)<5)return;t.active=!0,this.host.root.classList.add("mach-root--row-dragging"),this.host.root.querySelectorAll(".mach-row").forEach(n=>n.classList.toggle("mach-row--drag-source",this.sourceContains(n.dataset.id??"")))}this.d()}};this.m=()=>{if(this.n=0,!!this.e?.active){if(!this.current()){this.destroy();return}this.c(),this.v()&&this.d()}};this.g=e=>{let t=this.e;if(!(!t||e.pointerId!==t.pointerId)){t.clientX=e.clientX,t.clientY=e.clientY,t.active&&this.current()&&this.c(),this.u();try{t.active&&t.drop?.valid&&this.i(e.clientX,e.clientY)&&this.current()&&this.commitDrop(t.drop)}finally{this.release()}}};this.w=e=>{e.key==="Escape"&&(e.preventDefault(),this.destroy())};this.l=e=>{e.type==="pointercancel"&&e.pointerId!==this.e?.pointerId||this.destroy()}}begin(e){let t=this.host.containers.map(o=>{let i=le("div","mach-row-drop-indicator");return i.hidden=!0,o.append(i),i}),n=le("div","mach-row-drag-preview");n.setAttribute("role","status"),n.hidden=!0,this.host.root.append(n),this.e={startY:e.clientY,clientX:e.clientX,clientY:e.clientY,pointerId:e.pointerId,indicators:t,preview:n,drop:null,active:!1},this.a(!0)}a(e){Oe(window,e,{pointermove:this.h,pointerup:this.g,pointercancel:this.l,keydown:this.w,blur:this.l})}f(e){return this.e?.active===!0&&this.sourceContains(e)}y(e,t=()=>!0){this.o=requestAnimationFrame(()=>{this.o=0,t()&&[...this.host.root.querySelectorAll(".mach-row-drag-handle")].find(n=>n.closest(".mach-row")?.dataset.id===e)?.focus()})}d(){this.n||(this.n=requestAnimationFrame(this.m))}i(e,t,n=0){let o=this.host.body.getBoundingClientRect();return e>=o.left&&e<=o.right&&t>=o.top-n&&t<=o.bottom+n}v(){let e=this.e;if(!e||!this.i(e.clientX,e.clientY,28))return!1;let t=this.host.viewport,n=t.getBoundingClientRect(),o=Math.min(36,n.height/3);if(o<=0)return!1;let i=e.clientY<n.top+o?-Math.ceil(14*C((n.top+o-e.clientY)/o,0,1)):Math.ceil(14*C((e.clientY-n.bottom+o)/o,0,1));if(this.host.scrollBy)return this.host.scrollBy(i);let s=t.scrollTop;return t.scrollTop=C(s+i,0,Math.max(0,t.scrollHeight-t.clientHeight)),t.scrollTop!==s}c(){let e=this.e;if(!e?.active)return;let t=e.clientY-this.host.viewport.getBoundingClientRect().top+this.host.viewport.scrollTop,n=this.describe(t);e.drop=n;let o=this.i(e.clientX,e.clientY);for(let s of e.indicators){if(s.hidden=!o||!n?.valid,!n)continue;s.dataset.dropPosition=n.position??"",s.style.cssText=n.style??"";let a=parseFloat(s.parentElement.style.height),c=Math.min(this.host.getRowTop(n.boundary),Number.isFinite(a)?Math.max(0,a-2):1/0);s.style.transform=`translate3d(0, ${c}px, 0)`}let i=this.host.root.getBoundingClientRect();e.preview.hidden=!1,e.preview.textContent=o?n?.text??this.outsideText():this.outsideText(),e.preview.style.transform=`translate3d(${C(e.clientX-i.left+16,8,Math.max(8,i.width-224))}px, ${C(e.clientY-i.top+16,8,Math.max(8,i.height-48))}px, 0)`}u(){let e=this.e;this.e=null,this.a(!1),cancelAnimationFrame(this.n),this.n=0,this.host.root?.classList.remove("mach-root--row-dragging"),this.host.root?.querySelectorAll(".mach-row--drag-source").forEach(t=>t.classList.remove("mach-row--drag-source")),e?.indicators.forEach(t=>t.remove()),e?.preview.remove()}destroy(){this.u(),this.release(),cancelAnimationFrame(this.o),this.o=0}};var Z=class extends Q{constructor(t,n,o){super(t);this.t=t;this.prepare=n;this.b=o;this.scope=null;this.s=!1}r(t,n,o){try{return n()}catch(i){return this.scope&&(this.scope.failed=!0),this.t.report(i,t),o}}p(t){return this.r("prepare",()=>{let n=this.t.capture(t),o=this.prepare(t);return this.scope={node:t,from:t.rowIndex,guard:n,session:o,sourceDecision:!0,failed:!1},this.scope.sourceDecision=o.validate(),this.scope},null)}start(t,n){this.destroy(),this.p(n)&&this.current()&&this.begin(t)}moveWithKeyboard(t,n){this.destroy();let o=this.p(n);if(!(!o||!this.current())){this.s=!0;try{let i=t.key==="ArrowUp"?-1:1,s=this.r("keyboardTarget",()=>o.session.keyboardTarget(i),void 0);s&&this.commitDrop(this.destination(s,i<0?0:1))}finally{this.s=!1,this.release()}}}isSource(t){return this.f(t.id)}current(){return!!(this.scope&&!this.scope.failed&&this.scope.guard.current()&&this.t.api.rows.getById(this.scope.node.id)===this.scope.node)}sourceContains(t){return this.r("isSource",()=>this.scope?.session.isSource(t)??!1,!1)}release(){this.scope=null}outsideText(){return this.t.api.getOption("locale")?.rowDragOutside??"\u79FB\u56DE\u8868\u683C\u5185\u653E\u7F6E \xB7 Esc \u53D6\u6D88"}describe(t){let n=this.t.api,o=C(this.host.findRowAt(t),0,n.rows.getCount()-1),i=n.rows.getAt(o);if(i?.isDetail&&(i=n.rows.getById(i.masterId)),!i)return null;let s=this.host.getRowTop(i.rowIndex),a=this.host.getRowTop(i.rowIndex+1)-s;return this.destination(i,(t-s)/Math.max(1,a))}destination(t,n){let o=this.scope,i=n<.5?"before":"after",s=this.host.getRowTop(t.rowIndex+1)-this.host.getRowTop(t.rowIndex),a=this.r("resolve",()=>o.session.resolve({targetNode:t,position:i,fraction:n,height:s}),!1),c=typeof o.sourceDecision=="string"?o.sourceDecision:typeof a=="string"?a:"\u6B64\u4F4D\u7F6E\u4E0D\u53EF\u653E\u7F6E",d={node:t,fraction:n,boundary:t.rowIndex,valid:!1,text:c};if(!a||typeof a=="string"||o.sourceDecision!==!0||t.loading||t.treeLoading||t.isGroup)return d;d.intent=a.intent,d.position=a.intent.position,d.toIndex=a.toIndex,d.targetIndex=a.targetIndex,d.boundary=a.indicatorIndex??t.rowIndex+ +(a.intent.position==="after"),d.style=a.indicatorStyle,d.text=a.text??c;let h=this.r("canDrop",()=>this.t.api.getOption("rowDragPolicy")?.canDrop?.({node:o.node,api:this.t.api,targetNode:t,intent:a.intent})??!0,!1);return d.valid=h===!0,typeof h=="string"&&(d.text=h),d}commitDrop(t){let n=this.scope;if(!n||!this.current()||this.r("validate",()=>n.session.validate(),!1)!==!0||!this.current())return!1;let o=this.destination(t.node,t.fraction);return!o.valid||!o.intent||!this.current()?!1:(this.s&&this.b(o.intent,i=>this.y(n.node.id,i)),this.t.emit(n.node,n.from,o.toIndex,o.targetIndex,o.intent),!0)}};function $(r,e,t){let n=[],o=[...r].reverse().map(s=>({row:s,parent:null})),i=new Set;for(;o.length;){let s=o.pop();if(i.has(s.row))throw new Error("[MachTable] Installed order contains duplicate or cyclic rows.");i.add(s.row),n.push(s);let a=t?s.row[e]??[]:[];for(let c=a.length-1;c>=0;c--)o.push({row:a[c],parent:s.row})}return n}function H(r,e,t,n){try{let o=$(r,t,n);return o.length===e.length&&o.every((i,s)=>i.row===e[s].row&&i.parent===e[s].parent)}catch{return!1}}function he(r){let e=[];return r?.rows.forEach(t=>e.push({node:t,data:t.data})),e}function W(r,e){if(!r)return!0;if(r.isDestroyed())return!1;let t=0,n=!0;return r.rows.forEach(o=>{let i=e[t++];(i?.node!==o||i.data!==o.data)&&(n=!1)}),n&&t===e.length}var ee=class{constructor(){this.entries=[];this.cursor=0}get canUndo(){return this.cursor>0}get canRedo(){return this.cursor<this.entries.length}peek(e){return this.entries[e==="undo"?this.cursor-1:this.cursor]}commit(e,t){e==="undo"?this.cursor--:e==="redo"?this.cursor++:(this.entries.splice(this.cursor),this.entries.push(t),this.entries.length>50&&this.entries.shift(),this.cursor=this.entries.length)}clear(){this.entries=[],this.cursor=0}};var L=class extends Error{constructor(t="The row order changed on the server. Reload before trying again.",n){super(t);this.serverRevision=n;this.name="RowOrderConflictError"}};function Xe(r){if(r?.status==="conflict")throw new L(r.message,r.revision);return r?.revision}function Ae(r){if(r.targetId===null)throw new Error("[MachTable] Row ordering requires an existing destination.");return{...r,targetId:r.targetId}}function Ve(r){let e={status:"idle",error:null,revision:r.getRevision?.(),plan:null,canUndo:!1,canRedo:!1},t=0,n=!1,o=null,i=null,s=null,a=()=>{},c=()=>{},d=0,h=null,f=new ee,O=null,S=new Set,A=r.childrenKey??"children",x=null,E=()=>{f.clear(),O=null},B=()=>{if(!o)return!1;let l=o;return t++,o=null,i=null,l.abort(),!0},u=(l=e.status)=>({canUndo:!n&&l!=="saving"&&f.canUndo,canRedo:!n&&l!=="saving"&&f.canRedo}),g=l=>{e=Object.freeze({...l,...u(l.status)}),s&&!s.isDestroyed()&&s.view.refreshCells();let p=[w=>r.onStateChange?.(w),...S];for(let w of p)try{w?.(e)}catch(v){console.error("[MachTable] Row order state observer failed",v)}},y=()=>r.getRevision?.()??e.revision,m=(l=h)=>{l?.release(),h===l&&(h=null)},T=()=>{!i||i()||(B(),E(),m(),g({...e,status:"idle",plan:null,error:null}))},k=l=>new Z(l,p=>Se(l.api,p,r.getRows(),r,r.treeData===!0,y()),(p,w)=>{m();let v=document.activeElement,R=G=>{G.target!==v&&G.target!==document.body&&m(b)},b={api:l.api,intent:p,restore:w,generation:t+1,release:()=>document.removeEventListener("focusin",R,!0)};h=b,document.addEventListener("focusin",R,!0)}),M=()=>{T(),!o&&O&&(O.api!==s||O.revision!==y()||!W(s,O.nodes)||!H(r.getRows(),O.rows,A,r.treeData===!0))&&E();let l=u();(e.canUndo!==l.canUndo||e.canRedo!==l.canRedo)&&(e=Object.freeze({...e,...l}))},q=l=>{if(l===s){T(),ce(l,k)||(c(),c=ue(l,k));return}let p=B();a(),c(),m(),E(),s=l,c=ue(l,k),a=l.on("cellValueChanged",()=>{d++;let w=f.canUndo||f.canRedo;E(),w&&g({...e,status:o?"saving":"idle",plan:o?e.plan:null,error:null})}),p&&g({...e,status:"idle",plan:null,error:null})},N=l=>{if(M(),n)throw new Error("[MachTable] The row order controller is disposed.");if(o)throw new Error("[MachTable] A row order save is already in progress.");let p=y();if(p!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==p)throw new L;let w={...l,expectedRevision:l.expectedRevision??p};return(r.treeData?ae:se)(r.getRows(),w,r)},z=async(l,p,w,v,R)=>{if(!l.changed)return l;let b=s;if(b&&!J(b))throw new Error("[MachTable] Save or discard edited rows before changing their order.");let G=++t;x=null;let j=new AbortController;o=j,i=null;let X=y(),Le=d,F=null,K=[],ye=!1,te=!1,re=X,me=I=>{if(!R)return;if(!I||R!==h||R.api!==s){m(R);return}let D=O;R.restore(()=>{let oe=D!==null&&D===O&&D.revision===y()&&W(R.api,D.nodes)&&H(r.getRows(),D.rows,A,r.treeData===!0)&&h===R&&!n&&t===R.generation&&s===R.api&&ce(R.api,k);return m(R),oe})},P=()=>!n&&t===G,ne=()=>P()&&(!b||s===b&&!b.isDestroyed())&&F!==null&&y()===X&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0),ke=I=>{if(!ne()){P()&&!F&&g({...e,status:"error",error:I,plan:l});return}let D=!b||J(b),U=D?I:Object.assign(new Error("[MachTable] Row order could not be saved. Automatic rollback was skipped to preserve new edits. Save or discard those edits, then reload the row order."),{cause:I});if(D){i=null;try{r.applyRows(l.before),te=!0}catch(oe){U=Object.assign(new Error("[MachTable] Row order could not be saved or restored. Reload the row order before trying again."),{cause:I,rollbackError:oe})}}P()&&(te||E(),g({...e,status:I instanceof L?"conflict":"error",error:U}))},Ne=()=>{if(!P()){me(!1);return}let I=y(),D=!1;ye&&P()&&(I===X||I===re)&&W(b,K)&&H(r.getRows(),F,A,r.treeData===!0)?(O={rows:F,nodes:K,api:b,revision:I},D=!0):te&&P()?(O={rows:$(r.getRows(),A,r.treeData===!0),nodes:he(b),api:b,revision:I},D=H(r.getRows(),$(l.before,A,r.treeData===!0),A,r.treeData===!0)):E(),o===j&&(o=null,i=null,s&&!s.isDestroyed()&&s.view.refreshCells()),P()&&e.status==="saving"&&g({...e,status:"idle",plan:null}),M(),me(D)};try{r.applyRows(l.after),F=$(r.getRows(),A,r.treeData===!0),K=he(b);let I=r.getRows(),D=K[0]?.node;if(i=b&&D?()=>!b.isDestroyed()&&b.rows.getById(D.id)===D:()=>r.getRows()===I,g({...e,status:"saving",error:null,plan:l}),!ne())return l;let U=Xe(await xe(Promise.resolve(r.save?.(l.intent,{signal:j.signal,plan:l,operation:p,operations:w})),j.signal));return ne()&&(ye=!0,re=U??X,d===Le?f.commit(p,v):E(),g({...e,status:"saved",revision:re})),l}catch(I){throw ke(I),I}finally{Ne()}},we=async(l,p)=>{p!==h&&(p=void 0),h!==p&&m();let w=N(l);w.changed||m(p);let v=w.changed?Te(w,r,r.treeData===!0):[];return z(w,"move",[w.intent],{plan:w,inverse:v},p)},Me=l=>we(l),ge=async l=>{if(m(),M(),n||o)throw new Error("[MachTable] Row ordering is unavailable while disposed or saving.");let p=f.peek(l);if(!p)throw new Error(`[MachTable] Nothing to ${l}.`);let w=(l==="undo"?p.inverse:[p.plan.intent]).map(G=>({...G,expectedRevision:y()})),v=r.getRows(),R=l==="undo"?p.plan.before:p.plan.after;Ie(v,R,w,r,r.treeData===!0);let b={before:v,after:R,intent:w[0],rowIds:p.plan.rowIds,changed:!0};return z(b,l,w,p)},Ce=l=>{try{if(n||o)return!1;let p=r.getRows(),w=y();if(w!==void 0&&l.expectedRevision!==void 0&&l.expectedRevision!==w)return!1;let v=x?.rowIds===l.rowIds?x.sources:JSON.stringify(l.rowIds.map(String));return(!x||x.rows!==p||x.revision!==w||x.sources!==v)&&(x={rows:p,revision:w,sources:v,rowIds:l.rowIds,validate:Re(p,l.rowIds,r,r.treeData===!0)}),x.rowIds=l.rowIds,x.validate(l),!0}catch(p){return p instanceof Error?p.message:!1}},Pe=l=>{let p=l.intent;if(l.applied||!p)return;q(l.api);let w=t,v=h?.intent===p?h:void 0;Promise.resolve().then(()=>we(Ae(p),v)).catch(R=>{v&&v.generation!==t&&m(v),!n&&t===w&&e.status!=="saving"&&g({...e,status:R instanceof L?"conflict":"error",error:R})})},fe=l=>n?!1:(q(l),o?"Wait for the current row order to finish saving.":J(l)||"Save or discard edited rows before changing their order.");return{get state(){return M(),e},bindings:{applyRowDrag:!1,rowDragPolicy:{canDrag:({api:l})=>fe(l),canDrop:({intent:l,api:p})=>{let w=fe(p);if(w!==!0)return w;try{return Ce(Ae(l))}catch(v){return v instanceof Error?v.message:!1}},getRevision:y},onRowDragEnd:Pe},preview:N,move:Me,undo:()=>ge("undo"),redo:()=>ge("redo"),subscribe(l){return n?()=>{}:(S.add(l),()=>{S.delete(l)})},dispose(){n=!0,t++,o?.abort(),o=null,i=null,a(),c(),m(),E(),s&&!s.isDestroyed()&&s.view.refreshCells(),s=null,x=null,S.clear(),e=Object.freeze({...e,status:"idle",error:null,plan:null,canUndo:!1,canRedo:!1})}}}export{L as RowOrderConflictError,Ve as createRowOrderController,ae as moveTreeNodes,se as reorderRows};
|
package/dist/preferences.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as GridState, g as GridStateStore, F as FilterType } from './api-
|
|
1
|
+
import { f as GridState, g as GridStateStore, F as FilterType } from './api-ChEKWrvH.cjs';
|
|
2
2
|
|
|
3
3
|
interface GridStateMigrationOptions {
|
|
4
4
|
/** Current stable column IDs; removed IDs are not restored. */
|
package/dist/preferences.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as GridState, g as GridStateStore, F as FilterType } from './api-
|
|
1
|
+
import { f as GridState, g as GridStateStore, F as FilterType } from './api-ChEKWrvH.js';
|
|
2
2
|
|
|
3
3
|
interface GridStateMigrationOptions {
|
|
4
4
|
/** Current stable column IDs; removed IDs are not restored. */
|
package/dist/server-side.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SortModel, h as FilterModel, A as AdvancedFilterModel, b as GridApi, C as ColDef, i as FieldPath } from './api-
|
|
1
|
+
import { S as SortModel, h as FilterModel, A as AdvancedFilterModel, b as GridApi, C as ColDef, i as FieldPath } from './api-ChEKWrvH.cjs';
|
|
2
2
|
|
|
3
3
|
interface ServerSideGroupColumn {
|
|
4
4
|
colId: string;
|
package/dist/server-side.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SortModel, h as FilterModel, A as AdvancedFilterModel, b as GridApi, C as ColDef, i as FieldPath } from './api-
|
|
1
|
+
import { S as SortModel, h as FilterModel, A as AdvancedFilterModel, b as GridApi, C as ColDef, i as FieldPath } from './api-ChEKWrvH.js';
|
|
2
2
|
|
|
3
3
|
interface ServerSideGroupColumn {
|
|
4
4
|
colId: string;
|