@mache-hangar/lunar-editor-react 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@mache-hangar/lunar-editor-core/pm"),G=require("@mache-hangar/lunar-editor-core/source/compile-transaction"),P=require("@mache-hangar/lunar-editor-core/source/source-map-plugin"),J=require("@mache-hangar/lunar-editor-core/source/source-mutation-step"),k=48;function K(s){const t=Array.from(s.rows),e=[],o=[],n=s.ownerDocument.defaultView.getComputedStyle(s).direction==="rtl",r=n?-1:1;let l=0;const h=(g,m)=>(o[g]??=[]).push(m),i=new Map;t.forEach((g,m)=>i.set(g.parentElement,m+1)),t.forEach((g,m)=>{let a=0;for(const c of Array.from(g.cells)){for(;e[m]?.[a];)a++;const p=c.colSpan,w=i.get(g.parentElement),x=c.rowSpan||w-m;for(let E=m;E<Math.min(m+x,w);E++)for(let D=a;D<a+p;D++)(e[E]??=[])[D]=!0;const v=c.getBoundingClientRect();h(a,n?v.right:v.left),h(a+p,n?v.left:v.right),a+=p,l=Math.max(l,a)}});let d=0;for(const g of Array.from(s.children).filter(m=>m.tagName==="COLGROUP")){const m=Array.from(g.children);for(const a of m.length?m:[g]){const c=a.getBoundingClientRect();c.width>0&&(h(d,n?c.right:c.left),h(d+a.span,n?c.left:c.right)),d+=a.span}}if(l=Math.max(l,d),l<2||s.getBoundingClientRect().width<=0)return;const u=s.getBoundingClientRect();o[0]??=[n?u.right:u.left],o[l]??=[n?u.left:u.right];const f=Array.from({length:l+1},(g,m)=>{const a=o[m];return a?a.reduce((c,p)=>c+p,0)/a.length:NaN});let b=0;for(;b<l;){let g=b+1;for(;g<l&&!Number.isFinite(f[g]);)g++;for(let m=b+1;m<g;m++)f[m]=f[b]+(f[g]-f[b])*(m-b)/(g-b);b=g}const A=f.slice(1).map((g,m)=>Math.abs(g-f[m]));return A.every(g=>Number.isFinite(g)&&g>0)?{widths:A,boundaries:f,direction:r}:void 0}function H(s,t,e){const o=[...s],n=Math.min(k,s[t]),r=Math.min(k,s[t+1]),l=Math.max(n-s[t],Math.min(e,s[t+1]-r));return o[t]+=l,o[t+1]-=l,o}function B(s,t,e,o=!1){const n=e.createElement("span").style;return n.cssText=s||"",n.setProperty("width",`${Math.round(t*100)/100}px`),o&&(n.setProperty("min-width","0"),n.setProperty("max-width","none"),n.setProperty("table-layout","fixed")),n.cssText}function q(s,t,e){const o=s.state.doc.nodeAt(t);if(!s.editable||o?.type.name!=="table")return;const n=s.dom.ownerDocument,r=[];let l=t+1;o.forEach((a,c)=>{a.type.name==="caption"&&(l=t+1+c+a.nodeSize),a.type.name==="colgroup"&&r.push({node:a,position:t+1+c})});const{col:h,colgroup:i}=s.state.schema.nodes,d=[];let u=0;const f=(a,c=!1)=>{const{span:p,width:w,...x}=a?.attrs.htmlAttrs||{};return c||delete x.id,h.create({sourceKey:c?a?.attrs.sourceKey:null,htmlAttrs:{...x,style:B(x.style,e[u++],n)}})};for(const{node:a}of r){const c=[];if(a.forEach(E=>{const D=Math.min(1e3,Math.max(1,parseInt(E.attrs.htmlAttrs?.span,10)||1));for(let C=0;C<D&&u<e.length;C++)c.push(f(E,C===0))}),!a.childCount){const E=Math.min(1e3,Math.max(1,parseInt(a.attrs.htmlAttrs?.span,10)||1));for(let D=0;D<E&&u<e.length;D++)c.push(f())}const{span:p,width:w,...x}=a.attrs.htmlAttrs||{},v=n.createElement("span").style;v.cssText=x.style||"",v.removeProperty("width"),v.cssText?x.style=v.cssText:delete x.style,d.push(i.create({...a.attrs,htmlAttrs:x},c))}if(u<e.length){const a=[];for(;u<e.length;)a.push(f());if(d.length){const c=d[d.length-1];d[d.length-1]=c.copy(c.content.append(y.Fragment.fromArray(a)))}else d.push(i.create(null,a))}const b=r[0]?.position??l,A=r[r.length-1],g=A?A.position+A.node.nodeSize:b,m=s.state.tr.replaceWith(b,g,y.Fragment.fromArray(d));m.setNodeMarkup(t,void 0,{...o.attrs,htmlAttrs:{...o.attrs.htmlAttrs,style:B(o.attrs.htmlAttrs?.style,e.reduce((a,c)=>a+c,0),n,!0)}}),s.dispatch(y.closeHistory(m)),s.dispatch(y.closeHistory(s.state.tr))}function U(s,t){Array.from(s.attributes).forEach(e=>{e.name in t||s.removeAttribute(e.name)}),Object.entries(t).forEach(([e,o])=>{s.setAttribute(e,o)})}class Q{constructor(t,e,o){this.view=e,this.getPos=o;const n=e.dom.ownerDocument;this.node=t,this.dom=n.createElement("div"),this.dom.className="lunar-table-scroll",this.contentDOM=n.createElement("table"),this.dom.appendChild(this.contentDOM),this.controls=n.createElement("div"),this.controls.className="lunar-table-resize-controls",this.controls.contentEditable="false",this.dom.append(this.controls),this.dom.addEventListener("pointerover",this.showControls),this.dom.addEventListener("pointerleave",this.hideControls),this.dom.addEventListener("focusin",this.showControls),this.dom.addEventListener("focusout",this.hideControls),this.dom.addEventListener("scroll",this.scheduleLayout),this.controls.addEventListener("pointerdown",this.startDrag),this.controls.addEventListener("keydown",this.onHandleKeyDown),this.update(t)}view;getPos;dom;contentDOM;controls;node;observer;frame=0;previewNodes=new WeakSet;drag;update(t){return t.type.name!=="table"?!1:(this.drag&&(t!==this.node||!this.view.editable)&&this.finishDrag(!1),this.node=t,this.drag||U(this.contentDOM,t.attrs.htmlAttrs||{}),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed"),this.controls.childElementCount&&this.scheduleLayout(),!0)}showControls=t=>{if(!this.view.editable||this.drag||this.controls.childElementCount||t.target.closest?.(".lunar-table-scroll")!==this.dom)return;this.refreshControls();const o=this.dom.ownerDocument.defaultView?.ResizeObserver;o&&!this.observer&&(this.observer=new o(this.scheduleLayout),this.observer.observe(this.contentDOM))};hideControls=t=>{if(this.drag)return;const e=t.relatedTarget;e&&this.dom.contains(e)||t.type==="pointerleave"&&this.controls.contains(this.dom.ownerDocument.activeElement)||(this.controls.replaceChildren(),this.observer?.disconnect(),this.observer=void 0)};scheduleLayout=()=>{this.frame||(this.frame=this.dom.ownerDocument.defaultView.requestAnimationFrame(()=>{this.frame=0,this.controls.childElementCount&&this.refreshControls()}))};refreshControls(){if(!this.view.editable){this.controls.replaceChildren();return}const t=this.contentDOM.getBoundingClientRect(),e=this.drag;let o;if(e){const h=[t.left+e.edgeOffset];e.widths.forEach(i=>h.push(h[h.length-1]+i*e.layout.direction)),o={widths:e.widths,boundaries:h,direction:e.layout.direction}}else o=K(this.contentDOM);if(!o){this.controls.replaceChildren();return}const n=o.widths.length-1;if(this.controls.childElementCount!==n){this.controls.replaceChildren();for(let h=0;h<n;h++){const i=this.dom.ownerDocument.createElement("div");i.className="lunar-table-column-handle",i.dataset.column=String(h),i.setAttribute("role","separator"),i.setAttribute("aria-orientation","vertical"),i.setAttribute("aria-label",`Resize column ${h+1}`),i.title="Drag to resize column · Arrow keys to adjust · Esc to cancel",i.tabIndex=0,this.controls.append(i)}}const r=this.controls.getBoundingClientRect(),l=Math.max(t.top,this.contentDOM.rows[0]?.getBoundingClientRect().top??t.top);Array.from(this.controls.children).forEach((h,i)=>{const d=h;d.style.left=`${o.boundaries[i+1]-r.left}px`,d.style.top=`${l-r.top}px`,d.style.height=`${t.bottom-l}px`,d.setAttribute("aria-valuenow",String(Math.round(o.widths[i]))),d.setAttribute("aria-valuemin",String(Math.min(k,Math.round(o.widths[i])))),d.setAttribute("aria-valuemax",String(Math.round(o.widths[i]+o.widths[i+1]-Math.min(k,o.widths[i+1]))))})}handleColumn(t){const e=t.target.closest(".lunar-table-column-handle");return e&&this.controls.contains(e)?Number(e.dataset.column):void 0}startDrag=t=>{const e=this.handleColumn(t),o=K(this.contentDOM);if(!this.view.editable||t.button!==0||e===void 0||!o||e>=o.widths.length-1||this.drag)return;t.preventDefault(),t.stopPropagation();const n=this.dom.ownerDocument,r=n.createElement("colgroup");this.previewNodes.add(r);const l=Array.from(this.contentDOM.children).filter(i=>i.tagName==="COLGROUP").map(i=>({node:i,next:i.nextSibling}));this.drag={column:e,pointerId:t.pointerId,startX:t.clientX,layout:o,edgeOffset:o.boundaries[0]-this.contentDOM.getBoundingClientRect().left,widths:o.widths,document:this.view.state.doc,preview:r,originals:l,style:this.contentDOM.getAttribute("style")};for(const{node:i}of l)this.previewNodes.add(i),i.remove();o.widths.forEach(i=>{const d=n.createElement("col");d.style.width=`${i}px`,this.previewNodes.add(d),r.append(d)});const h=this.contentDOM.caption;this.contentDOM.insertBefore(r,h?h.nextSibling:this.contentDOM.firstChild),this.contentDOM.style.cssText=B(this.drag.style||"",o.widths.reduce((i,d)=>i+d,0),n,!0),this.dom.classList.add("lunar-table-scroll--fixed","lunar-table-scroll--resizing"),this.controls.children[e]?.classList.add("lunar-table-column-handle--active"),n.addEventListener("pointermove",this.moveDrag,!0),n.addEventListener("pointerup",this.endDrag,!0),n.addEventListener("pointercancel",this.cancelDrag,!0),n.addEventListener("keydown",this.onDragKeyDown,!0),n.defaultView?.addEventListener("blur",this.cancelDrag),this.scheduleLayout()};moveDrag=t=>{const e=this.drag;if(!(!e||t.pointerId!==e.pointerId)){if(!this.view.editable||this.view.state.doc!==e.document){this.finishDrag(!1);return}t.preventDefault(),e.widths=H(e.layout.widths,e.column,(t.clientX-e.startX)*e.layout.direction);for(const o of[e.column,e.column+1])e.preview.children[o].style.width=`${e.widths[o]}px`;this.scheduleLayout()}};endDrag=t=>{t.pointerId===this.drag?.pointerId&&(this.moveDrag(t),this.finishDrag(!0))};cancelDrag=()=>this.finishDrag(!1);onDragKeyDown=t=>{t.key==="Escape"&&(t.preventDefault(),t.stopPropagation(),this.finishDrag(!1))};finishDrag(t){const e=this.drag;if(!e)return;this.drag=void 0;const o=this.dom.ownerDocument;o.removeEventListener("pointermove",this.moveDrag,!0),o.removeEventListener("pointerup",this.endDrag,!0),o.removeEventListener("pointercancel",this.cancelDrag,!0),o.removeEventListener("keydown",this.onDragKeyDown,!0),o.defaultView?.removeEventListener("blur",this.cancelDrag),e.preview.remove(),[...e.originals].reverse().forEach(({node:r,next:l})=>this.contentDOM.insertBefore(r,l)),e.style===null?this.contentDOM.removeAttribute("style"):this.contentDOM.setAttribute("style",e.style),this.dom.classList.remove("lunar-table-scroll--resizing"),this.controls.children[e.column]?.classList.remove("lunar-table-column-handle--active"),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed");const n=this.getPos();t&&this.view.editable&&this.view.state.doc===e.document&&n!==void 0&&e.widths.some((r,l)=>Math.abs(r-e.layout.widths[l])>=.5)&&q(this.view,n,e.widths),this.scheduleLayout()}onHandleKeyDown=t=>{const e=this.handleColumn(t),o=K(this.contentDOM),n=this.getPos();if(!this.view.editable||e===void 0||!o||e>=o.widths.length-1||n===void 0||!["ArrowLeft","ArrowRight"].includes(t.key))return;t.preventDefault();const r=(t.key==="ArrowRight"?10:-10)*o.direction,l=H(o.widths,e,r);l[e]!==o.widths[e]&&q(this.view,n,l),this.scheduleLayout()};stopEvent(t){return this.controls.contains(t.target)}ignoreMutation(t){return t.type==="selection"?!1:!this.contentDOM.contains(t.target)||t.type==="attributes"&&t.target===this.contentDOM||this.previewNodes.has(t.target)?!0:t.type==="childList"&&t.target===this.contentDOM&&[...Array.from(t.addedNodes),...Array.from(t.removedNodes)].every(e=>this.previewNodes.has(e))}destroy(){this.finishDrag(!1),this.observer?.disconnect(),this.dom.ownerDocument.defaultView?.cancelAnimationFrame(this.frame),this.dom.removeEventListener("pointerover",this.showControls),this.dom.removeEventListener("pointerleave",this.hideControls),this.dom.removeEventListener("focusin",this.showControls),this.dom.removeEventListener("focusout",this.hideControls),this.dom.removeEventListener("scroll",this.scheduleLayout),this.controls.removeEventListener("pointerdown",this.startDrag),this.controls.removeEventListener("keydown",this.onHandleKeyDown)}}const Z=s=>["table_cell","table_header"].includes(s.type.name),N=s=>Math.min(100,Math.max(1,Number.parseInt(String(s),10)||1));function T(s){for(let t=s.depth;t>0;t--)if(s.node(t).type.name==="table")return{node:s.node(t),pos:s.before(t)}}function V(s,t){const e=[];if(s.forEach((h,i,d)=>{h.type.name==="table_row"?e.push({node:h,section:-1,pos:t+1+i}):["table_head","table_body","table_foot"].includes(h.type.name)&&h.forEach((u,f)=>e.push({node:u,section:d,pos:t+2+i+f}))}),!e.length||e.length>200)return;const o=[],n=[];let r=0,l=!0;if(e.forEach((h,i)=>{let d=0;h.node.forEach((u,f)=>{if(!Z(u)){l=!1;return}for(;o[i]?.[d];)d++;const b=e.findIndex((c,p)=>p>i&&c.section!==h.section),A=b<0?e.length:b;if(Number(u.attrs.htmlAttrs?.colspan)>100||Number(u.attrs.htmlAttrs?.rowspan)>100){l=!1;return}const g=N(u.attrs.htmlAttrs?.colspan),m=u.attrs.htmlAttrs?.rowspan==="0"?A-i:N(u.attrs.htmlAttrs?.rowspan);if(i+m>A||d+g>100){l=!1;return}const a={node:u,row:i,col:d,rows:m,cols:g,pos:h.pos+1+f};n.push(a);for(let c=i;c<i+m;c++)for(let p=d;p<d+g;p++)o[c]??=[],o[c][p]&&(l=!1),o[c][p]=a;d+=g,r=Math.max(r,d)})}),!(!l||!r||e.some((h,i)=>Array.from({length:r},(d,u)=>!o[i]?.[u]).some(Boolean))))return{node:s,pos:t,rows:e,cells:n,matrix:o,width:r}}function _(s,t){return s.cells.find(e=>t>=e.pos&&t<e.pos+e.node.nodeSize)}function tt(s,t,e){const o={top:Math.min(t.row,e.row),bottom:Math.max(t.row+t.rows,e.row+e.rows),left:Math.min(t.col,e.col),right:Math.max(t.col+t.cols,e.col+e.cols)};let n=!0;for(;n;){n=!1;for(const r of s.cells)if($(r,o)){const l=[Math.min(o.top,r.row),Math.max(o.bottom,r.row+r.rows),Math.min(o.left,r.col),Math.max(o.right,r.col+r.cols)];l.some((h,i)=>h!==[o.top,o.bottom,o.left,o.right][i])&&([o.top,o.bottom,o.left,o.right]=l,n=!0)}}return o}function $(s,t){return s.row<t.bottom&&s.row+s.rows>t.top&&s.col<t.right&&s.col+s.cols>t.left}function z(s,t,e){const o={...s.attrs.htmlAttrs};for(const n of["id","rowspan","colspan","headers"])delete o[n];return{node:s.type.createAndFill({...s.attrs,sourceKey:null,htmlAttrs:o}),row:t,col:e,rows:1,cols:1,pos:-1}}function et(s,t,e,o){const n=t.map((l,h)=>{const i=e.filter(u=>u.row===h).sort((u,f)=>u.col-f.col).map(u=>{const f={...u.node.attrs.htmlAttrs};return u.rows>1?f.rowspan=String(u.rows):delete f.rowspan,u.cols>1?f.colspan=String(u.cols):delete f.colspan,JSON.stringify(f)===JSON.stringify(u.node.attrs.htmlAttrs||{})?u.node:u.node.type.create({...u.node.attrs,htmlAttrs:f},u.node.content)}),d=y.Fragment.fromArray(i);return l.node.content.eq(d)?l.node:l.node.copy(d)}),r=[];return s.node.forEach((l,h,i)=>{if(l.type.name==="colgroup"&&o){const d=o.filter(f=>f.group===i),u={...l.attrs.htmlAttrs};delete u.span,d.length&&r.push(l.type.create({...l.attrs,htmlAttrs:u},d.map(f=>f.node)))}else if(["table_head","table_body","table_foot"].includes(l.type.name)){const d=n.filter((u,f)=>t[f].section===i);d.length&&r.push(l.copy(y.Fragment.fromArray(d)))}else l.type.name!=="table_row"&&r.push(l)}),r.push(...n.filter((l,h)=>t[h].section===-1)),s.node.copy(y.Fragment.fromArray(r))}function W(s,t,e,o){const n=[];if(s.node.forEach((r,l,h)=>{if(r.type.name!=="colgroup")return;const i={...r.attrs.htmlAttrs};if(!r.childCount)for(let d=0;d<N(i.span);d++)n.push({node:r.type.schema.nodes.col.create({htmlAttrs:{},sourceKey:null}),group:h});r.forEach(d=>{for(let u=0;u<N(d.attrs.htmlAttrs?.span);u++){const f={...d.attrs.htmlAttrs};delete f.span,u&&delete f.id,n.push({node:d.type.create({...d.attrs,sourceKey:u?null:d.attrs.sourceKey,htmlAttrs:f}),group:h})}})}),!!n.length){for(;n.length<s.width;)n.push({node:s.node.type.schema.nodes.col.create(),group:n[n.length-1].group});return n.length=s.width,o?n.splice(t,e):n.splice(t,0,{node:s.node.type.schema.nodes.col.create(),group:n[Math.min(t,n.length-1)].group}),n}}const M=new y.PluginKey("lunar-table-selection"),O={available:!1,inTable:!1,valid:!1,inCell:!1,rows:0,columns:0,row:0,column:0,selectedCells:0,canMerge:!1,canSplit:!1,headerRow:!1,headerColumn:!1,caption:!1,textAlign:"left",verticalAlign:"top",backgroundColor:"#ffffff"};function S(s){const t=M.getState(s),e=s.selection instanceof y.NodeSelection&&s.selection.node.type.name==="table"?{node:s.selection.node,pos:s.selection.from}:void 0,o=t?T(s.doc.resolve(t.anchor)):e||T(s.selection.$anchor);if(!o||!t&&!e&&T(s.selection.$head)?.pos!==o.pos)return;const n=V(o.node,o.pos);if(!n)return{table:o};const r=_(n,t?.anchor??s.selection.anchor),l=r||n.cells[0],h=_(n,t?.head??s.selection.head)||l,i=e?{top:0,bottom:n.rows.length,left:0,right:n.width}:tt(n,l,h);return{table:o,grid:n,rect:i,inCell:!!(e||r),cells:n.cells.filter(d=>$(d,i))}}function L(s){if(!s)return O;const t=s.editable&&(!P.getLunarSourceState(s.state)||P.getLunarSourceState(s.state)?.status==="synchronized"),e=S(s.state);if(!e)return{...O,available:t};if(!e.grid)return{...O,available:t,inTable:!0};const{grid:o,rect:n,cells:r}=e,l=o.rows[n.top].section,h=s.dom.ownerDocument.createElement("span").style;return h.cssText=r[0].node.attrs.htmlAttrs?.style||"",{available:t,inTable:!0,valid:!0,inCell:e.inCell,rows:o.rows.length,columns:o.width,row:n.top+1,column:n.left+1,selectedCells:r.length,canMerge:r.length>1&&o.rows.slice(n.top,n.bottom).every(i=>i.section===l),canSplit:r.some(i=>i.rows>1||i.cols>1),headerRow:o.cells.filter(i=>i.row<n.bottom&&i.row+i.rows>n.top).every(i=>i.node.type.name==="table_header"),headerColumn:o.cells.filter(i=>i.col<n.right&&i.col+i.cols>n.left).every(i=>i.node.type.name==="table_header"),caption:Array.from({length:o.node.childCount},(i,d)=>o.node.child(d)).some(i=>i.type.name==="caption"),textAlign:h.textAlign||"left",verticalAlign:h.verticalAlign||"top",backgroundColor:h.backgroundColor||"#ffffff"}}function I(s,t){if(!t.docChanged)return!1;const e=P.getLunarSourceState(s.state),o=G.compileLunarSourceTransaction(s.state,y.closeHistory(t));return e&&!J.getSourceMutationSteps(o.steps).length?!1:(s.dispatch(o.scrollIntoView()),s.dispatch(y.closeHistory(s.state.tr)),s.focus(),!0)}function F(s,t,e){const o=s.state.doc.resolve(e),n=o.parent.canReplace(o.index(),o.index()+1)?y.Fragment.empty:y.Fragment.from(s.state.schema.nodes.paragraph.createAndFill());return I(s,s.state.tr.replaceWith(e,e+t.nodeSize,n))}function j(s,t,e){const o=e.createElement("span").style;o.cssText=s.attrs.htmlAttrs?.style||"";for(const[n,r]of Object.entries(t))o.setProperty(n,r);return s.type.create({...s.attrs,htmlAttrs:{...s.attrs.htmlAttrs,style:o.cssText}},s.content)}function ot(s,t,e,o){const n=e?t.bottom:t.top,r=s.rows[e?t.bottom-1:t.top],l={...r.node.attrs.htmlAttrs};delete l.id;const h={node:r.node.type.create({...r.node.attrs,sourceKey:null,htmlAttrs:l}),section:r.section,pos:-1};for(const d of o)d.row>=n?d.row++:d.row+d.rows>n&&d.rows++;for(let d=0;d<s.width;d++)o.some(u=>u.row<n&&u.row+u.rows>n&&d>=u.col&&d<u.col+u.cols)||o.push(z(s.matrix[e?t.bottom-1:t.top][d].node,n,d));const i=[...s.rows];return i.splice(n,0,h),{rows:i,focusRow:n}}function R(s,t,e){if(!s||!L(s).available)return!1;try{if(t.action==="insert"){const{rows:a,columns:c}=t;if(![a,c].every(D=>Number.isInteger(D)&&D>=1&&D<=50))return!1;const p=s.state.schema,w=Array.from({length:a},(D,C)=>p.nodes.table_row.create(null,Array.from({length:c},()=>(t.headerRow&&C===0?p.nodes.table_header:p.nodes.table_cell).createAndFill({htmlAttrs:t.headerRow&&C===0?{scope:"col"}:{}})))),x=p.nodes.table.create({htmlAttrs:{style:"width: 100%;"}},p.nodes.table_body.create(null,w)),v=s.state.tr.replaceSelectionWith(x);let E=-1;return v.doc.descendants((D,C)=>{E<0&&D===x&&(E=C)}),E>=0&&v.setSelection(y.TextSelection.near(v.doc.resolve(E+4))),I(s,v)}const o=S(s.state);if(!o)return!1;if(t.action==="delete_table")return F(s,o.table.node,o.table.pos);if(!o.grid||!o.rect||!o.cells||!o.inCell&&!["caption","table_style"].includes(t.action))return!1;const{grid:n,rect:r}=o;let l=n.cells.map(a=>({...a})),h=[...n.rows],i,d=r.top,u=r.left;const f=a=>$(a,r);if(t.action==="row_before"||t.action==="row_after"){if(h.length>=200)return!1;({rows:h,focusRow:d}=ot(n,r,t.action==="row_after",l))}else if(t.action==="delete_rows"){if(r.bottom-r.top===h.length)return F(s,n.node,n.pos);const a=r.bottom-r.top;h.splice(r.top,a),l=l.filter(c=>{const p=Math.max(0,Math.min(c.row+c.rows,r.bottom)-Math.max(c.row,r.top));return p===c.rows?!1:(c.row>=r.bottom?c.row-=a:c.row>=r.top&&(c.row=r.top),c.rows-=p,!0)})}else if(t.action==="column_before"||t.action==="column_after"){if(n.width>=100)return!1;const a=t.action==="column_after"?r.right:r.left;for(const c of l)c.col>=a?c.col++:c.col+c.cols>a&&c.cols++;for(let c=0;c<h.length;c++)l.some(p=>p.col<a&&p.col+p.cols>a&&c>=p.row&&c<p.row+p.rows)||l.push(z(n.matrix[c][Math.min(r.left,n.width-1)].node,c,a));i=W(n,a,1,!1),u=a}else if(t.action==="delete_columns"){if(r.right-r.left===n.width)return F(s,n.node,n.pos);const a=r.right-r.left;l=l.filter(c=>{const p=Math.max(0,Math.min(c.col+c.cols,r.right)-Math.max(c.col,r.left));return p===c.cols?!1:(c.col>=r.right?c.col-=a:c.col>=r.left&&(c.col=r.left),c.cols-=p,!0)}),i=W(n,r.left,a,!0)}else if(t.action==="merge"){if(!L(s).canMerge)return!1;const a=l.filter(f),c=a[0],p=a.reduce((w,x)=>w.append(x.node.content),y.Fragment.empty);c.node=c.node.copy(p),c.rows=r.bottom-r.top,c.cols=r.right-r.left,l=l.filter(w=>w===c||!a.includes(w))}else if(t.action==="split")for(const a of[...l].filter(f)){const{rows:c,cols:p}=a;for(let w=0;w<c;w++)for(let x=0;x<p;x++)(w||x)&&l.push(z(a.node,a.row+w,a.col+x));a.rows=1,a.cols=1}else if(t.action==="header_row"||t.action==="header_column"){const a=t.action==="header_row",c=a?!L(s).headerRow:!L(s).headerColumn;for(const p of l.filter(w=>a?w.row<r.bottom&&w.row+w.rows>r.top:w.col<r.right&&w.col+w.cols>r.left)){const w={...p.node.attrs.htmlAttrs};c?w.scope=a?"col":"row":delete w.scope;const x=s.state.schema.nodes[c?"table_header":"table_cell"];p.node=x.create({...p.node.attrs,htmlAttrs:w},p.node.content)}}else if(t.action==="clear_cells"){let a=!0;for(const c of l.filter(f))c.node=c.node.copy(y.Fragment.from(s.state.schema.nodes.paragraph.create(null,a&&e?s.state.schema.text(e):void 0))),a=!1}else if(t.action==="cell_style"){const a={};if(t.textAlign&&["left","center","right","justify"].includes(t.textAlign)&&(a["text-align"]=t.textAlign),t.verticalAlign&&["top","middle","bottom"].includes(t.verticalAlign)&&(a["vertical-align"]=t.verticalAlign),t.backgroundColor&&/^(#[\da-f]{6}|transparent)$/i.test(t.backgroundColor)&&(a["background-color"]=t.backgroundColor),!Object.keys(a).length)return!1;for(const c of l.filter(f))c.node=j(c.node,a,s.dom.ownerDocument)}let b=et(n,h,l,i);if(t.action==="caption"){const a=[];b.forEach(c=>{c.type.name!=="caption"&&a.push(c)}),L(s).caption||a.unshift(s.state.schema.nodes.caption.createAndFill(null,s.state.schema.nodes.paragraph.create(null,s.state.schema.text("Table caption")))),b=b.copy(y.Fragment.fromArray(a))}else if(t.action==="table_style"){const a={};if(t.align&&["left","center","right"].includes(t.align)&&(a["margin-left"]=t.align==="left"?"0":"auto",a["margin-right"]=t.align==="right"?"0":"auto",a.float="none"),t.width&&["25%","50%","75%","100%"].includes(t.width)&&(a.width=t.width),!Object.keys(a).length)return!1;b=j(b,a,s.dom.ownerDocument)}if(b.eq(n.node))return!1;const A=s.state.tr.replaceWith(n.pos,n.pos+n.node.nodeSize,b),g=V(b,n.pos);if(!g)return!1;const m=g?.matrix[Math.min(d,g.rows.length-1)]?.[Math.min(u,g.width-1)];if(m&&A.setSelection(y.TextSelection.near(A.doc.resolve(m.pos+1))),o.cells.length>1&&["cell_style","table_style","header_row","header_column","caption"].includes(t.action)){const a=g.matrix[r.top][r.left],c=g.matrix[r.bottom-1][r.right-1];A.setMeta(M,{anchor:a.pos+1,head:c.pos+1})}return I(s,A)}catch{return!1}}function X(s,t){const e=t?.closest?.("td, th");if(!e||!s.dom.contains(e))return;const o=s.posAtDOM(e,0),n=T(s.state.doc.resolve(o)),r=n&&V(n.node,n.pos),l=r&&_(r,o),h=e.closest("table");return r&&l&&h?{grid:r,cell:l,tableElement:h}:void 0}function st(s,t,e){const o=s.state.doc.resolve(t);if(e>=3&&o.parent.inlineContent)return y.TextSelection.create(s.state.doc,o.start(),o.end());if(e===2&&o.parent.inlineContent){const n=o.parent.textBetween(0,o.parent.content.size,"",""),r=o.parent.textBetween(0,o.parentOffset,"","").length,l=[...new Intl.Segmenter(void 0,{granularity:"word"}).segment(n)].find(h=>r>=h.index&&r<h.index+h.segment.length);if(l){let h=0,i,d;if(o.parent.descendants((u,f)=>{if(!u.isText&&!u.isLeaf)return;const b=u.isText?u.text.length:1;i===void 0&&l.index<h+b&&(i=o.start()+f+Math.max(0,l.index-h));const A=l.index+l.segment.length;d===void 0&&A<=h+b&&(d=o.start()+f+Math.max(0,A-h)),h+=b}),i!==void 0&&d!==void 0)return y.TextSelection.create(s.state.doc,i,d)}}return y.TextSelection.near(o)}class nt{drag;get active(){return!!this.drag?.view.editable}start(t,e){if(this.cancel(),!t.editable||e.button!==0||e.shiftKey||e.ctrlKey||e.metaKey||e.altKey)return!1;const o=X(t,e.target);if(!o)return!1;const n=t.posAtCoords({left:e.clientX,top:e.clientY})?.pos,r=n!==void 0&&_(o.grid,n)?.pos===o.cell.pos?n:o.cell.pos+1,l=st(t,r,e.detail);this.drag={view:t,doc:t.state.doc,...o,anchor:o.cell,head:o.cell,selecting:!1,textFrom:l.from,textTo:l.to},e.preventDefault(),t.dispatch(t.state.tr.setSelection(l).setMeta(M,{anchor:o.cell.pos+1,head:o.cell.pos+1,dragging:!0})),t.focus();const h=t.dom.ownerDocument;return h.addEventListener("mousemove",this.move,!0),h.addEventListener("mouseup",this.end,!0),h.defaultView?.addEventListener("blur",this.cancel),!0}cancelIfStale(t){this.drag&&(this.drag.doc!==t.state.doc||!t.editable)&&this.destroy()}point(t,e){const o=e.tableElement.getBoundingClientRect(),n=e.view.dom.getBoundingClientRect(),r=e.view.dom.ownerDocument.defaultView;return{left:Math.max(Math.max(o.left+1,n.left+1,1),Math.min(t.clientX,o.right-1,n.right-1,r.innerWidth-1)),top:Math.max(Math.max(o.top+1,n.top+1,1),Math.min(t.clientY,o.bottom-1,n.bottom-1,r.innerHeight-1))}}move=t=>{const e=this.drag;if(!e)return;if(!(t.buttons&1)){this.end();return}if(e.doc!==e.view.state.doc||!e.view.editable){this.cancel();return}t.preventDefault();const o=this.point(t,e),r=e.view.dom.ownerDocument.elementFromPoint?.(o.left,o.top)??t.target,l=X(e.view,r);if(!l||l.grid.pos!==e.grid.pos)return;const{view:h,anchor:i}=e;l.cell.pos!==i.pos&&(e.selecting=!0),e.head=l.cell;let d;if(e.selecting){const f=i.pos<=l.cell.pos;d=y.TextSelection.between(h.state.doc.resolve(i.pos+(f?1:i.node.nodeSize-1)),h.state.doc.resolve(l.cell.pos+(f?l.cell.node.nodeSize-1:1)))}else{const f=h.posAtCoords(o)?.pos;if(f===void 0||_(e.grid,f)?.pos!==i.pos)return;d=y.TextSelection.between(h.state.doc.resolve(f<e.textFrom?e.textTo:e.textFrom),h.state.doc.resolve(f))}const u=M.getState(h.state);d.eq(h.state.selection)&&u?.head===l.cell.pos+1||h.dispatch(h.state.tr.setSelection(d).setMeta(M,{anchor:i.pos+1,head:l.cell.pos+1,dragging:!0}))};end=()=>{const t=this.drag;this.destroy(),!(!t||t.doc!==t.view.state.doc||t.view.isDestroyed)&&t.view.dispatch(t.view.state.tr.setMeta(M,t.selecting?{anchor:t.anchor.pos+1,head:t.head.pos+1}:null))};cancel=()=>{const t=this.drag;if(this.destroy(),!t||t.doc!==t.view.state.doc||t.view.isDestroyed)return;const e=M.getState(t.view.state);e?.dragging&&t.view.dispatch(t.view.state.tr.setMeta(M,{...e,dragging:!1}))};destroy=()=>{const t=this.drag?.view.dom.ownerDocument;t?.removeEventListener("mousemove",this.move,!0),t?.removeEventListener("mouseup",this.end,!0),t?.defaultView?.removeEventListener("blur",this.cancel),this.drag=void 0}}function Y(s,t){s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.doc.resolve(t+1)))),s.focus()}function rt(s){const t=new nt;return new y.Plugin({key:M,state:{init:()=>null,apply(e,o,n){const r=e.getMeta(M);return r!==void 0?r:e.docChanged||e.selectionSet&&!t.active&&!e.selection.eq(n.selection)?null:o}},props:{attributes(e){const o=[];return(S(e)?.cells?.length||0)>1&&o.push("lunar-table-cell-selection"),M.getState(e)?.dragging&&t.active&&o.push("lunar-table-dragging"),{class:o.join(" ")}},decorations(e){const o=S(e);return!o?.cells||o.cells.length<2?null:y.DecorationSet.create(e.doc,o.cells.map(n=>y.Decoration.node(n.pos,n.pos+n.node.nodeSize,{class:"lunar-table-cell--selected"})))},handleDOMEvents:{keydown(e,o){t.cancel();const n=lt(e,o);return n&&o.preventDefault(),n},mousedown(e,o){if(t.cancel(),!e.editable||o.button!==0)return!1;const n=o.target?.closest?.("td, th");if(o.shiftKey&&n&&e.dom.contains(n)){const r=S(e.state),l=e.posAtDOM(n,0);if(r?.grid&&T(e.state.doc.resolve(l))?.pos===r.grid.pos){const h=_(r.grid,l),i=_(r.grid,M.getState(e.state)?.anchor??e.state.selection.anchor);if(i&&h)return o.preventDefault(),e.dispatch(e.state.tr.setMeta(M,{anchor:i.pos+1,head:h.pos+1})),!0}}return M.getState(e.state)&&e.dispatch(e.state.tr.setMeta(M,null)),t.start(e,o)}},handleTextInput(e,o,n,r){return(S(e.state)?.cells?.length||0)<2?!1:R(e,{action:"clear_cells"},r)}},view(e){let o="";const n=()=>{const r=L(e),l=JSON.stringify(r);o!==l&&(o=l,s.onStateChange?.(r))};return n(),{update(){t.cancelIfStale(e),n()},destroy:t.destroy}}})}function lt(s,t){if(!s.editable)return!1;const e=S(s.state);if(!e?.grid||!e.rect)return!1;if(t.key==="Escape"&&(M.getState(s.state)||e.cells.length>1))return s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.selection.$head)).setMeta(M,null)),!0;if(t.ctrlKey||t.metaKey||t.altKey)return!1;if(["Backspace","Delete"].includes(t.key)&&e.cells.length>1)return R(s,{action:"clear_cells"});if(t.key!=="Tab")return!1;const o=e.grid,n=_(o,s.state.selection.head);if(!n)return!1;const r=o.cells.indexOf(n)+(t.shiftKey?-1:1);if(r<0)return!1;if(r<o.cells.length)return Y(s,o.cells[r].pos),!0;if(!R(s,{action:"row_after"}))return!1;const l=S(s.state)?.grid,h=l?.matrix[l.rows.length-1]?.[0];return h&&Y(s,h.pos),!0}function it(s={}){return{name:"table-view",plugins:()=>[rt(s)],nodeViews:{table:(t,e,o)=>new Q(t,e,o)}}}exports.EMPTY_TABLE_STATE=O;exports.createTableExtension=it;exports.getTableState=L;exports.runTableCommand=R;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@mache-hangar/lunar-editor-core/pm"),G=require("@mache-hangar/lunar-editor-core/source/compile-transaction"),P=require("@mache-hangar/lunar-editor-core/source/source-map-plugin"),J=require("@mache-hangar/lunar-editor-core/source/source-mutation-step"),k=48;function K(s){const t=Array.from(s.rows),e=[],o=[],n=s.ownerDocument.defaultView.getComputedStyle(s).direction==="rtl",r=n?-1:1;let l=0;const h=(m,g)=>(o[m]??=[]).push(g),i=new Map;t.forEach((m,g)=>i.set(m.parentElement,g+1)),t.forEach((m,g)=>{let a=0;for(const c of Array.from(m.cells)){for(;e[g]?.[a];)a++;const p=c.colSpan,w=i.get(m.parentElement),M=c.rowSpan||w-g;for(let E=g;E<Math.min(g+M,w);E++)for(let D=a;D<a+p;D++)(e[E]??=[])[D]=!0;const v=c.getBoundingClientRect();h(a,n?v.right:v.left),h(a+p,n?v.left:v.right),a+=p,l=Math.max(l,a)}});let d=0;for(const m of Array.from(s.children).filter(g=>g.tagName==="COLGROUP")){const g=Array.from(m.children);for(const a of g.length?g:[m]){const c=a.getBoundingClientRect();c.width>0&&(h(d,n?c.right:c.left),h(d+a.span,n?c.left:c.right)),d+=a.span}}if(l=Math.max(l,d),l<2||s.getBoundingClientRect().width<=0)return;const u=s.getBoundingClientRect();o[0]??=[n?u.right:u.left],o[l]??=[n?u.left:u.right];const f=Array.from({length:l+1},(m,g)=>{const a=o[g];return a?a.reduce((c,p)=>c+p,0)/a.length:NaN});let b=0;for(;b<l;){let m=b+1;for(;m<l&&!Number.isFinite(f[m]);)m++;for(let g=b+1;g<m;g++)f[g]=f[b]+(f[m]-f[b])*(g-b)/(m-b);b=m}const x=f.slice(1).map((m,g)=>Math.abs(m-f[g]));return x.every(m=>Number.isFinite(m)&&m>0)?{widths:x,boundaries:f,direction:r}:void 0}function H(s,t,e){const o=[...s],n=Math.min(k,s[t]),r=Math.min(k,s[t+1]),l=Math.max(n-s[t],Math.min(e,s[t+1]-r));return o[t]+=l,o[t+1]-=l,o}function B(s,t,e,o=!1){const n=e.createElement("span").style;return n.cssText=s||"",n.setProperty("width",`${Math.round(t*100)/100}px`),o&&(n.setProperty("min-width","0"),n.setProperty("max-width","none"),n.setProperty("table-layout","fixed")),n.cssText}function q(s,t,e){const o=s.state.doc.nodeAt(t);if(!s.editable||o?.type.name!=="table")return;const n=s.dom.ownerDocument,r=[];let l=t+1;o.forEach((a,c)=>{a.type.name==="caption"&&(l=t+1+c+a.nodeSize),a.type.name==="colgroup"&&r.push({node:a,position:t+1+c})});const{col:h,colgroup:i}=s.state.schema.nodes,d=[];let u=0;const f=(a,c=!1)=>{const{span:p,width:w,...M}=a?.attrs.htmlAttrs||{};return c||delete M.id,h.create({sourceKey:c?a?.attrs.sourceKey:null,htmlAttrs:{...M,style:B(M.style,e[u++],n)}})};for(const{node:a}of r){const c=[];if(a.forEach(E=>{const D=Math.min(1e3,Math.max(1,parseInt(E.attrs.htmlAttrs?.span,10)||1));for(let C=0;C<D&&u<e.length;C++)c.push(f(E,C===0))}),!a.childCount){const E=Math.min(1e3,Math.max(1,parseInt(a.attrs.htmlAttrs?.span,10)||1));for(let D=0;D<E&&u<e.length;D++)c.push(f())}const{span:p,width:w,...M}=a.attrs.htmlAttrs||{},v=n.createElement("span").style;v.cssText=M.style||"",v.removeProperty("width"),v.cssText?M.style=v.cssText:delete M.style,d.push(i.create({...a.attrs,htmlAttrs:M},c))}if(u<e.length){const a=[];for(;u<e.length;)a.push(f());if(d.length){const c=d[d.length-1];d[d.length-1]=c.copy(c.content.append(y.Fragment.fromArray(a)))}else d.push(i.create(null,a))}const b=r[0]?.position??l,x=r[r.length-1],m=x?x.position+x.node.nodeSize:b,g=s.state.tr.replaceWith(b,m,y.Fragment.fromArray(d));g.setNodeMarkup(t,void 0,{...o.attrs,htmlAttrs:{...o.attrs.htmlAttrs,style:B(o.attrs.htmlAttrs?.style,e.reduce((a,c)=>a+c,0),n,!0)}}),s.dispatch(y.closeHistory(g)),s.dispatch(y.closeHistory(s.state.tr))}function U(s,t){Array.from(s.attributes).forEach(e=>{e.name in t||s.removeAttribute(e.name)}),Object.entries(t).forEach(([e,o])=>{s.setAttribute(e,o)})}class Q{constructor(t,e,o){this.view=e,this.getPos=o;const n=e.dom.ownerDocument;this.node=t,this.dom=n.createElement("div"),this.dom.className="lunar-table-scroll",this.contentDOM=n.createElement("table"),this.dom.appendChild(this.contentDOM),this.controls=n.createElement("div"),this.controls.className="lunar-table-resize-controls",this.controls.contentEditable="false",this.dom.append(this.controls),this.dom.addEventListener("pointerover",this.showControls),this.dom.addEventListener("pointerleave",this.hideControls),this.dom.addEventListener("focusin",this.showControls),this.dom.addEventListener("focusout",this.hideControls),this.dom.addEventListener("scroll",this.scheduleLayout),this.controls.addEventListener("pointerdown",this.startDrag),this.controls.addEventListener("keydown",this.onHandleKeyDown),this.update(t)}view;getPos;dom;contentDOM;controls;node;observer;frame=0;previewNodes=new WeakSet;drag;update(t){return t.type.name!=="table"?!1:(this.drag&&(t!==this.node||!this.view.editable)&&this.finishDrag(!1),this.node=t,this.drag||U(this.contentDOM,t.attrs.htmlAttrs||{}),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed"),this.controls.childElementCount&&this.scheduleLayout(),!0)}showControls=t=>{if(!this.view.editable||this.drag||this.controls.childElementCount||t.target.closest?.(".lunar-table-scroll")!==this.dom)return;this.refreshControls();const o=this.dom.ownerDocument.defaultView?.ResizeObserver;o&&!this.observer&&(this.observer=new o(this.scheduleLayout),this.observer.observe(this.contentDOM))};hideControls=t=>{if(this.drag)return;const e=t.relatedTarget;e&&this.dom.contains(e)||t.type==="pointerleave"&&this.controls.contains(this.dom.ownerDocument.activeElement)||(this.controls.replaceChildren(),this.observer?.disconnect(),this.observer=void 0)};scheduleLayout=()=>{this.frame||(this.frame=this.dom.ownerDocument.defaultView.requestAnimationFrame(()=>{this.frame=0,this.controls.childElementCount&&this.refreshControls()}))};refreshControls(){if(!this.view.editable){this.controls.replaceChildren();return}const t=this.contentDOM.getBoundingClientRect(),e=this.drag;let o;if(e){const h=[t.left+e.edgeOffset];e.widths.forEach(i=>h.push(h[h.length-1]+i*e.layout.direction)),o={widths:e.widths,boundaries:h,direction:e.layout.direction}}else o=K(this.contentDOM);if(!o){this.controls.replaceChildren();return}const n=o.widths.length-1;if(this.controls.childElementCount!==n){this.controls.replaceChildren();for(let h=0;h<n;h++){const i=this.dom.ownerDocument.createElement("div");i.className="lunar-table-column-handle",i.dataset.column=String(h),i.setAttribute("role","separator"),i.setAttribute("aria-orientation","vertical"),i.setAttribute("aria-label",`Resize column ${h+1}`),i.title="Drag to resize column · Arrow keys to adjust · Esc to cancel",i.tabIndex=0,this.controls.append(i)}}const r=this.controls.getBoundingClientRect(),l=Math.max(t.top,this.contentDOM.rows[0]?.getBoundingClientRect().top??t.top);Array.from(this.controls.children).forEach((h,i)=>{const d=h;d.style.left=`${o.boundaries[i+1]-r.left}px`,d.style.top=`${l-r.top}px`,d.style.height=`${t.bottom-l}px`,d.setAttribute("aria-valuenow",String(Math.round(o.widths[i]))),d.setAttribute("aria-valuemin",String(Math.min(k,Math.round(o.widths[i])))),d.setAttribute("aria-valuemax",String(Math.round(o.widths[i]+o.widths[i+1]-Math.min(k,o.widths[i+1]))))})}handleColumn(t){const e=t.target.closest(".lunar-table-column-handle");return e&&this.controls.contains(e)?Number(e.dataset.column):void 0}startDrag=t=>{const e=this.handleColumn(t),o=K(this.contentDOM);if(!this.view.editable||t.button!==0||e===void 0||!o||e>=o.widths.length-1||this.drag)return;t.preventDefault(),t.stopPropagation();const n=this.dom.ownerDocument,r=n.createElement("colgroup");this.previewNodes.add(r);const l=Array.from(this.contentDOM.children).filter(i=>i.tagName==="COLGROUP").map(i=>({node:i,next:i.nextSibling}));this.drag={column:e,pointerId:t.pointerId,startX:t.clientX,layout:o,edgeOffset:o.boundaries[0]-this.contentDOM.getBoundingClientRect().left,widths:o.widths,document:this.view.state.doc,preview:r,originals:l,style:this.contentDOM.getAttribute("style")};for(const{node:i}of l)this.previewNodes.add(i),i.remove();o.widths.forEach(i=>{const d=n.createElement("col");d.style.width=`${i}px`,this.previewNodes.add(d),r.append(d)});const h=this.contentDOM.caption;this.contentDOM.insertBefore(r,h?h.nextSibling:this.contentDOM.firstChild),this.contentDOM.style.cssText=B(this.drag.style||"",o.widths.reduce((i,d)=>i+d,0),n,!0),this.dom.classList.add("lunar-table-scroll--fixed","lunar-table-scroll--resizing"),this.controls.children[e]?.classList.add("lunar-table-column-handle--active"),n.addEventListener("pointermove",this.moveDrag,!0),n.addEventListener("pointerup",this.endDrag,!0),n.addEventListener("pointercancel",this.cancelDrag,!0),n.addEventListener("keydown",this.onDragKeyDown,!0),n.defaultView?.addEventListener("blur",this.cancelDrag),this.scheduleLayout()};moveDrag=t=>{const e=this.drag;if(!(!e||t.pointerId!==e.pointerId)){if(!this.view.editable||this.view.state.doc!==e.document){this.finishDrag(!1);return}t.preventDefault(),e.widths=H(e.layout.widths,e.column,(t.clientX-e.startX)*e.layout.direction);for(const o of[e.column,e.column+1])e.preview.children[o].style.width=`${e.widths[o]}px`;this.scheduleLayout()}};endDrag=t=>{t.pointerId===this.drag?.pointerId&&(this.moveDrag(t),this.finishDrag(!0))};cancelDrag=()=>this.finishDrag(!1);onDragKeyDown=t=>{t.key==="Escape"&&(t.preventDefault(),t.stopPropagation(),this.finishDrag(!1))};finishDrag(t){const e=this.drag;if(!e)return;this.drag=void 0;const o=this.dom.ownerDocument;o.removeEventListener("pointermove",this.moveDrag,!0),o.removeEventListener("pointerup",this.endDrag,!0),o.removeEventListener("pointercancel",this.cancelDrag,!0),o.removeEventListener("keydown",this.onDragKeyDown,!0),o.defaultView?.removeEventListener("blur",this.cancelDrag),e.preview.remove(),[...e.originals].reverse().forEach(({node:r,next:l})=>this.contentDOM.insertBefore(r,l)),e.style===null?this.contentDOM.removeAttribute("style"):this.contentDOM.setAttribute("style",e.style),this.dom.classList.remove("lunar-table-scroll--resizing"),this.controls.children[e.column]?.classList.remove("lunar-table-column-handle--active"),this.dom.classList.toggle("lunar-table-scroll--fixed",this.contentDOM.style.tableLayout==="fixed");const n=this.getPos();t&&this.view.editable&&this.view.state.doc===e.document&&n!==void 0&&e.widths.some((r,l)=>Math.abs(r-e.layout.widths[l])>=.5)&&q(this.view,n,e.widths),this.scheduleLayout()}onHandleKeyDown=t=>{const e=this.handleColumn(t),o=K(this.contentDOM),n=this.getPos();if(!this.view.editable||e===void 0||!o||e>=o.widths.length-1||n===void 0||!["ArrowLeft","ArrowRight"].includes(t.key))return;t.preventDefault();const r=(t.key==="ArrowRight"?10:-10)*o.direction,l=H(o.widths,e,r);l[e]!==o.widths[e]&&q(this.view,n,l),this.scheduleLayout()};stopEvent(t){return this.controls.contains(t.target)}ignoreMutation(t){return t.type==="selection"?!1:!this.contentDOM.contains(t.target)||t.type==="attributes"&&t.target===this.contentDOM||this.previewNodes.has(t.target)?!0:t.type==="childList"&&t.target===this.contentDOM&&[...Array.from(t.addedNodes),...Array.from(t.removedNodes)].every(e=>this.previewNodes.has(e))}destroy(){this.finishDrag(!1),this.observer?.disconnect(),this.dom.ownerDocument.defaultView?.cancelAnimationFrame(this.frame),this.dom.removeEventListener("pointerover",this.showControls),this.dom.removeEventListener("pointerleave",this.hideControls),this.dom.removeEventListener("focusin",this.showControls),this.dom.removeEventListener("focusout",this.hideControls),this.dom.removeEventListener("scroll",this.scheduleLayout),this.controls.removeEventListener("pointerdown",this.startDrag),this.controls.removeEventListener("keydown",this.onHandleKeyDown)}}const Z=s=>["table_cell","table_header"].includes(s.type.name),N=s=>Math.min(100,Math.max(1,Number.parseInt(String(s),10)||1));function T(s){for(let t=s.depth;t>0;t--)if(s.node(t).type.name==="table")return{node:s.node(t),pos:s.before(t)}}function V(s,t){const e=[];if(s.forEach((h,i,d)=>{h.type.name==="table_row"?e.push({node:h,section:-1,pos:t+1+i}):["table_head","table_body","table_foot"].includes(h.type.name)&&h.forEach((u,f)=>e.push({node:u,section:d,pos:t+2+i+f}))}),!e.length||e.length>200)return;const o=[],n=[];let r=0,l=!0;if(e.forEach((h,i)=>{let d=0;h.node.forEach((u,f)=>{if(!Z(u)){l=!1;return}for(;o[i]?.[d];)d++;const b=e.findIndex((c,p)=>p>i&&c.section!==h.section),x=b<0?e.length:b;if(Number(u.attrs.htmlAttrs?.colspan)>100||Number(u.attrs.htmlAttrs?.rowspan)>100){l=!1;return}const m=N(u.attrs.htmlAttrs?.colspan),g=u.attrs.htmlAttrs?.rowspan==="0"?x-i:N(u.attrs.htmlAttrs?.rowspan);if(i+g>x||d+m>100){l=!1;return}const a={node:u,row:i,col:d,rows:g,cols:m,pos:h.pos+1+f};n.push(a);for(let c=i;c<i+g;c++)for(let p=d;p<d+m;p++)o[c]??=[],o[c][p]&&(l=!1),o[c][p]=a;d+=m,r=Math.max(r,d)})}),!(!l||!r||e.some((h,i)=>Array.from({length:r},(d,u)=>!o[i]?.[u]).some(Boolean))))return{node:s,pos:t,rows:e,cells:n,matrix:o,width:r}}function _(s,t){return s.cells.find(e=>t>=e.pos&&t<e.pos+e.node.nodeSize)}function tt(s,t,e){const o={top:Math.min(t.row,e.row),bottom:Math.max(t.row+t.rows,e.row+e.rows),left:Math.min(t.col,e.col),right:Math.max(t.col+t.cols,e.col+e.cols)};let n=!0;for(;n;){n=!1;for(const r of s.cells)if($(r,o)){const l=[Math.min(o.top,r.row),Math.max(o.bottom,r.row+r.rows),Math.min(o.left,r.col),Math.max(o.right,r.col+r.cols)];l.some((h,i)=>h!==[o.top,o.bottom,o.left,o.right][i])&&([o.top,o.bottom,o.left,o.right]=l,n=!0)}}return o}function $(s,t){return s.row<t.bottom&&s.row+s.rows>t.top&&s.col<t.right&&s.col+s.cols>t.left}function z(s,t,e){const o={...s.attrs.htmlAttrs};for(const n of["id","rowspan","colspan","headers"])delete o[n];return{node:s.type.createAndFill({...s.attrs,sourceKey:null,htmlAttrs:o}),row:t,col:e,rows:1,cols:1,pos:-1}}function et(s,t,e,o){const n=t.map((l,h)=>{const i=e.filter(u=>u.row===h).sort((u,f)=>u.col-f.col).map(u=>{const f={...u.node.attrs.htmlAttrs};return u.rows>1?f.rowspan=String(u.rows):delete f.rowspan,u.cols>1?f.colspan=String(u.cols):delete f.colspan,JSON.stringify(f)===JSON.stringify(u.node.attrs.htmlAttrs||{})?u.node:u.node.type.create({...u.node.attrs,htmlAttrs:f},u.node.content)}),d=y.Fragment.fromArray(i);return l.node.content.eq(d)?l.node:l.node.copy(d)}),r=[];return s.node.forEach((l,h,i)=>{if(l.type.name==="colgroup"&&o){const d=o.filter(f=>f.group===i),u={...l.attrs.htmlAttrs};delete u.span,d.length&&r.push(l.type.create({...l.attrs,htmlAttrs:u},d.map(f=>f.node)))}else if(["table_head","table_body","table_foot"].includes(l.type.name)){const d=n.filter((u,f)=>t[f].section===i);d.length&&r.push(l.copy(y.Fragment.fromArray(d)))}else l.type.name!=="table_row"&&r.push(l)}),r.push(...n.filter((l,h)=>t[h].section===-1)),s.node.copy(y.Fragment.fromArray(r))}function W(s,t,e,o){const n=[];if(s.node.forEach((r,l,h)=>{if(r.type.name!=="colgroup")return;const i={...r.attrs.htmlAttrs};if(!r.childCount)for(let d=0;d<N(i.span);d++)n.push({node:r.type.schema.nodes.col.create({htmlAttrs:{},sourceKey:null}),group:h});r.forEach(d=>{for(let u=0;u<N(d.attrs.htmlAttrs?.span);u++){const f={...d.attrs.htmlAttrs};delete f.span,u&&delete f.id,n.push({node:d.type.create({...d.attrs,sourceKey:u?null:d.attrs.sourceKey,htmlAttrs:f}),group:h})}})}),!!n.length){for(;n.length<s.width;)n.push({node:s.node.type.schema.nodes.col.create(),group:n[n.length-1].group});return n.length=s.width,o?n.splice(t,e):n.splice(t,0,{node:s.node.type.schema.nodes.col.create(),group:n[Math.min(t,n.length-1)].group}),n}}const A=new y.PluginKey("lunar-table-selection"),O={available:!1,inTable:!1,valid:!1,inCell:!1,rows:0,columns:0,row:0,column:0,selectedCells:0,canMerge:!1,canSplit:!1,headerRow:!1,headerColumn:!1,caption:!1,textAlign:"left",verticalAlign:"top",backgroundColor:"#ffffff"};function S(s){const t=A.getState(s),e=s.selection instanceof y.NodeSelection&&s.selection.node.type.name==="table"?{node:s.selection.node,pos:s.selection.from}:void 0,o=t?T(s.doc.resolve(t.anchor)):e||T(s.selection.$anchor);if(!o||!t&&!e&&T(s.selection.$head)?.pos!==o.pos)return;const n=V(o.node,o.pos);if(!n)return{table:o};const r=_(n,t?.anchor??s.selection.anchor),l=r||n.cells[0],h=_(n,t?.head??s.selection.head)||l,i=e?{top:0,bottom:n.rows.length,left:0,right:n.width}:tt(n,l,h);return{table:o,grid:n,rect:i,inCell:!!(e||r),cells:n.cells.filter(d=>$(d,i))}}function L(s){if(!s)return O;const t=s.editable&&(!P.getLunarSourceState(s.state)||P.getLunarSourceState(s.state)?.status==="synchronized"),e=S(s.state);if(!e)return{...O,available:t};if(!e.grid)return{...O,available:t,inTable:!0};const{grid:o,rect:n,cells:r}=e,l=o.rows[n.top].section,h=s.dom.ownerDocument.createElement("span").style;return h.cssText=r[0].node.attrs.htmlAttrs?.style||"",{available:t,inTable:!0,valid:!0,inCell:e.inCell,rows:o.rows.length,columns:o.width,row:n.top+1,column:n.left+1,selectedCells:r.length,canMerge:r.length>1&&o.rows.slice(n.top,n.bottom).every(i=>i.section===l),canSplit:r.some(i=>i.rows>1||i.cols>1),headerRow:o.cells.filter(i=>i.row<n.bottom&&i.row+i.rows>n.top).every(i=>i.node.type.name==="table_header"),headerColumn:o.cells.filter(i=>i.col<n.right&&i.col+i.cols>n.left).every(i=>i.node.type.name==="table_header"),caption:Array.from({length:o.node.childCount},(i,d)=>o.node.child(d)).some(i=>i.type.name==="caption"),textAlign:h.textAlign||"left",verticalAlign:h.verticalAlign||"top",backgroundColor:h.backgroundColor||"#ffffff"}}function I(s,t){if(!t.docChanged)return!1;const e=P.getLunarSourceState(s.state),o=G.compileLunarSourceTransaction(s.state,y.closeHistory(t));return e&&!J.getSourceMutationSteps(o.steps).length?!1:(s.dispatch(o.scrollIntoView()),s.dispatch(y.closeHistory(s.state.tr)),s.focus(),!0)}function F(s,t,e){const o=s.state.doc.resolve(e),n=o.parent.canReplace(o.index(),o.index()+1)?y.Fragment.empty:y.Fragment.from(s.state.schema.nodes.paragraph.createAndFill());return I(s,s.state.tr.replaceWith(e,e+t.nodeSize,n))}function j(s,t,e){const o=e.createElement("span").style;o.cssText=s.attrs.htmlAttrs?.style||"";for(const[n,r]of Object.entries(t))o.setProperty(n,r);return s.type.create({...s.attrs,htmlAttrs:{...s.attrs.htmlAttrs,style:o.cssText}},s.content)}function ot(s,t,e,o){const n=e?t.bottom:t.top,r=s.rows[e?t.bottom-1:t.top],l={...r.node.attrs.htmlAttrs};delete l.id;const h={node:r.node.type.create({...r.node.attrs,sourceKey:null,htmlAttrs:l}),section:r.section,pos:-1};for(const d of o)d.row>=n?d.row++:d.row+d.rows>n&&d.rows++;for(let d=0;d<s.width;d++)o.some(u=>u.row<n&&u.row+u.rows>n&&d>=u.col&&d<u.col+u.cols)||o.push(z(s.matrix[e?t.bottom-1:t.top][d].node,n,d));const i=[...s.rows];return i.splice(n,0,h),{rows:i,focusRow:n}}function R(s,t,e){if(!s||!L(s).available)return!1;try{if(t.action==="insert"){const{rows:a,columns:c}=t;if(![a,c].every(D=>Number.isInteger(D)&&D>=1&&D<=50))return!1;const p=s.state.schema,w=Array.from({length:a},(D,C)=>p.nodes.table_row.create(null,Array.from({length:c},()=>(t.headerRow&&C===0?p.nodes.table_header:p.nodes.table_cell).createAndFill({htmlAttrs:t.headerRow&&C===0?{scope:"col"}:{}})))),M=p.nodes.table.create({htmlAttrs:{style:"width: 100%;"}},p.nodes.table_body.create(null,w)),v=s.state.tr.replaceSelectionWith(M);let E=-1;return v.doc.descendants((D,C)=>{E<0&&D===M&&(E=C)}),E>=0&&v.setSelection(y.TextSelection.near(v.doc.resolve(E+4))),I(s,v)}const o=S(s.state);if(!o)return!1;if(t.action==="delete_table")return F(s,o.table.node,o.table.pos);if(!o.grid||!o.rect||!o.cells||!o.inCell&&!["caption","table_style"].includes(t.action))return!1;const{grid:n,rect:r}=o;let l=n.cells.map(a=>({...a})),h=[...n.rows],i,d=r.top,u=r.left;const f=a=>$(a,r);if(t.action==="row_before"||t.action==="row_after"){if(h.length>=200)return!1;({rows:h,focusRow:d}=ot(n,r,t.action==="row_after",l))}else if(t.action==="delete_rows"){if(r.bottom-r.top===h.length)return F(s,n.node,n.pos);const a=r.bottom-r.top;h.splice(r.top,a),l=l.filter(c=>{const p=Math.max(0,Math.min(c.row+c.rows,r.bottom)-Math.max(c.row,r.top));return p===c.rows?!1:(c.row>=r.bottom?c.row-=a:c.row>=r.top&&(c.row=r.top),c.rows-=p,!0)})}else if(t.action==="column_before"||t.action==="column_after"){if(n.width>=100)return!1;const a=t.action==="column_after"?r.right:r.left;for(const c of l)c.col>=a?c.col++:c.col+c.cols>a&&c.cols++;for(let c=0;c<h.length;c++)l.some(p=>p.col<a&&p.col+p.cols>a&&c>=p.row&&c<p.row+p.rows)||l.push(z(n.matrix[c][Math.min(r.left,n.width-1)].node,c,a));i=W(n,a,1,!1),u=a}else if(t.action==="delete_columns"){if(r.right-r.left===n.width)return F(s,n.node,n.pos);const a=r.right-r.left;l=l.filter(c=>{const p=Math.max(0,Math.min(c.col+c.cols,r.right)-Math.max(c.col,r.left));return p===c.cols?!1:(c.col>=r.right?c.col-=a:c.col>=r.left&&(c.col=r.left),c.cols-=p,!0)}),i=W(n,r.left,a,!0)}else if(t.action==="merge"){if(!L(s).canMerge)return!1;const a=l.filter(f),c=a[0],p=a.reduce((w,M)=>w.append(M.node.content),y.Fragment.empty);c.node=c.node.copy(p),c.rows=r.bottom-r.top,c.cols=r.right-r.left,l=l.filter(w=>w===c||!a.includes(w))}else if(t.action==="split")for(const a of[...l].filter(f)){const{rows:c,cols:p}=a;for(let w=0;w<c;w++)for(let M=0;M<p;M++)(w||M)&&l.push(z(a.node,a.row+w,a.col+M));a.rows=1,a.cols=1}else if(t.action==="header_row"||t.action==="header_column"){const a=t.action==="header_row",c=a?!L(s).headerRow:!L(s).headerColumn;for(const p of l.filter(w=>a?w.row<r.bottom&&w.row+w.rows>r.top:w.col<r.right&&w.col+w.cols>r.left)){const w={...p.node.attrs.htmlAttrs};c?w.scope=a?"col":"row":delete w.scope;const M=s.state.schema.nodes[c?"table_header":"table_cell"];p.node=M.create({...p.node.attrs,htmlAttrs:w},p.node.content)}}else if(t.action==="clear_cells"){let a=!0;for(const c of l.filter(f))c.node=c.node.copy(y.Fragment.from(s.state.schema.nodes.paragraph.create(null,a&&e?s.state.schema.text(e):void 0))),a=!1}else if(t.action==="cell_style"){const a={};if(t.textAlign&&["left","center","right","justify"].includes(t.textAlign)&&(a["text-align"]=t.textAlign),t.verticalAlign&&["top","middle","bottom"].includes(t.verticalAlign)&&(a["vertical-align"]=t.verticalAlign),t.backgroundColor&&/^(#[\da-f]{6}|transparent)$/i.test(t.backgroundColor)&&(a["background-color"]=t.backgroundColor),!Object.keys(a).length)return!1;for(const c of l.filter(f))c.node=j(c.node,a,s.dom.ownerDocument)}let b=et(n,h,l,i);if(t.action==="caption"){const a=[];b.forEach(c=>{c.type.name!=="caption"&&a.push(c)}),L(s).caption||a.unshift(s.state.schema.nodes.caption.createAndFill(null,s.state.schema.nodes.paragraph.create(null,s.state.schema.text("Table caption")))),b=b.copy(y.Fragment.fromArray(a))}else if(t.action==="table_style"){const a={};if(t.align&&["left","center","right"].includes(t.align)&&(a["margin-left"]=t.align==="left"?"0":"auto",a["margin-right"]=t.align==="right"?"0":"auto",a.float="none"),t.width&&["25%","50%","75%","100%"].includes(t.width)&&(a.width=t.width),!Object.keys(a).length)return!1;b=j(b,a,s.dom.ownerDocument)}if(b.eq(n.node))return!1;const x=s.state.tr.replaceWith(n.pos,n.pos+n.node.nodeSize,b),m=V(b,n.pos);if(!m)return!1;const g=m?.matrix[Math.min(d,m.rows.length-1)]?.[Math.min(u,m.width-1)];if(g&&x.setSelection(y.TextSelection.near(x.doc.resolve(g.pos+1))),o.cells.length>1&&["cell_style","table_style","header_row","header_column","caption"].includes(t.action)){const a=m.matrix[r.top][r.left],c=m.matrix[r.bottom-1][r.right-1];x.setMeta(A,{anchor:a.pos+1,head:c.pos+1})}return I(s,x)}catch{return!1}}function X(s,t){const e=t?.closest?.("td, th");if(!e||!s.dom.contains(e))return;const o=s.posAtDOM(e,0),n=T(s.state.doc.resolve(o)),r=n&&V(n.node,n.pos),l=r&&_(r,o),h=e.closest("table");return r&&l&&h?{grid:r,cell:l,tableElement:h}:void 0}function st(s,t){let e;return s.node.descendants((o,n)=>{if(o.isAtom)return!1;if(!o.inlineContent)return;const r=s.pos+n+2,l=Math.max(r,Math.min(t,r+o.content.size));return(e===void 0||Math.abs(l-t)<Math.abs(e-t)||Math.abs(l-t)===Math.abs(e-t)&&l>e)&&(e=l),!1}),e}function nt(s,t,e){const o=s.state.doc.resolve(t);if(e>=3&&o.parent.inlineContent)return y.TextSelection.create(s.state.doc,o.start(),o.end());if(e===2&&o.parent.inlineContent){const n=o.parent.textBetween(0,o.parent.content.size,"",""),r=o.parent.textBetween(0,o.parentOffset,"","").length,l=[...new Intl.Segmenter(void 0,{granularity:"word"}).segment(n)].find(h=>r>=h.index&&r<h.index+h.segment.length);if(l){let h=0,i,d;if(o.parent.descendants((u,f)=>{if(!u.isText&&!u.isLeaf)return;const b=u.isText?u.text.length:1;i===void 0&&l.index<h+b&&(i=o.start()+f+Math.max(0,l.index-h));const x=l.index+l.segment.length;d===void 0&&x<=h+b&&(d=o.start()+f+Math.max(0,x-h)),h+=b}),i!==void 0&&d!==void 0)return y.TextSelection.create(s.state.doc,i,d)}}return y.TextSelection.create(s.state.doc,t)}class rt{drag;get active(){return!!this.drag?.view.editable}start(t,e){if(this.cancel(),!t.editable||e.button!==0||e.shiftKey||e.ctrlKey||e.metaKey||e.altKey)return!1;const o=X(t,e.target);if(!o)return!1;const n=t.posAtCoords({left:e.clientX,top:e.clientY})?.pos,r=st(o.cell,n!==void 0&&_(o.grid,n)?.pos===o.cell.pos?n:o.cell.pos+1);if(r===void 0)return!1;const l=nt(t,r,e.detail);this.drag={view:t,doc:t.state.doc,...o,anchor:o.cell,head:o.cell,selecting:!1,textFrom:l.from,textTo:l.to},e.preventDefault(),t.dispatch(t.state.tr.setSelection(l).setMeta(A,{anchor:o.cell.pos+1,head:o.cell.pos+1,dragging:!0})),t.focus();const h=t.dom.ownerDocument;return h.addEventListener("mousemove",this.move,!0),h.addEventListener("mouseup",this.end,!0),h.defaultView?.addEventListener("blur",this.cancel),!0}cancelIfStale(t){this.drag&&(this.drag.doc!==t.state.doc||!t.editable)&&this.destroy()}point(t,e){const o=e.tableElement.getBoundingClientRect(),n=e.view.dom.getBoundingClientRect(),r=e.view.dom.ownerDocument.defaultView;return{left:Math.max(Math.max(o.left+1,n.left+1,1),Math.min(t.clientX,o.right-1,n.right-1,r.innerWidth-1)),top:Math.max(Math.max(o.top+1,n.top+1,1),Math.min(t.clientY,o.bottom-1,n.bottom-1,r.innerHeight-1))}}move=t=>{const e=this.drag;if(!e)return;if(!(t.buttons&1)){this.end();return}if(e.doc!==e.view.state.doc||!e.view.editable){this.cancel();return}t.preventDefault();const o=this.point(t,e),r=e.view.dom.ownerDocument.elementFromPoint?.(o.left,o.top)??t.target,l=X(e.view,r);if(!l||l.grid.pos!==e.grid.pos)return;const{view:h,anchor:i}=e;l.cell.pos!==i.pos&&(e.selecting=!0),e.head=l.cell;let d;if(e.selecting){const f=i.pos<=l.cell.pos;d=y.TextSelection.between(h.state.doc.resolve(i.pos+(f?1:i.node.nodeSize-1)),h.state.doc.resolve(l.cell.pos+(f?l.cell.node.nodeSize-1:1)))}else{const f=h.posAtCoords(o)?.pos;if(f===void 0||_(e.grid,f)?.pos!==i.pos)return;d=y.TextSelection.between(h.state.doc.resolve(f<e.textFrom?e.textTo:e.textFrom),h.state.doc.resolve(f))}const u=A.getState(h.state);d.eq(h.state.selection)&&u?.head===l.cell.pos+1||h.dispatch(h.state.tr.setSelection(d).setMeta(A,{anchor:i.pos+1,head:l.cell.pos+1,dragging:!0}))};end=()=>{const t=this.drag;this.destroy(),!(!t||t.doc!==t.view.state.doc||t.view.isDestroyed)&&t.view.dispatch(t.view.state.tr.setMeta(A,t.selecting?{anchor:t.anchor.pos+1,head:t.head.pos+1}:null))};cancel=()=>{const t=this.drag;if(this.destroy(),!t||t.doc!==t.view.state.doc||t.view.isDestroyed)return;const e=A.getState(t.view.state);e?.dragging&&t.view.dispatch(t.view.state.tr.setMeta(A,{...e,dragging:!1}))};destroy=()=>{const t=this.drag?.view.dom.ownerDocument;t?.removeEventListener("mousemove",this.move,!0),t?.removeEventListener("mouseup",this.end,!0),t?.defaultView?.removeEventListener("blur",this.cancel),this.drag=void 0}}function Y(s,t){s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.doc.resolve(t+1)))),s.focus()}function lt(s){const t=new rt;return new y.Plugin({key:A,state:{init:()=>null,apply(e,o,n){const r=e.getMeta(A);return r!==void 0?r:e.docChanged||e.selectionSet&&!t.active&&!e.selection.eq(n.selection)?null:o}},props:{attributes(e){const o=[];return(S(e)?.cells?.length||0)>1&&o.push("lunar-table-cell-selection"),A.getState(e)?.dragging&&t.active&&o.push("lunar-table-dragging"),{class:o.join(" ")}},decorations(e){const o=S(e);return!o?.cells||o.cells.length<2?null:y.DecorationSet.create(e.doc,o.cells.map(n=>y.Decoration.node(n.pos,n.pos+n.node.nodeSize,{class:"lunar-table-cell--selected"})))},handleDOMEvents:{keydown(e,o){t.cancel();const n=it(e,o);return n&&o.preventDefault(),n},mousedown(e,o){if(t.cancel(),!e.editable||o.button!==0)return!1;const n=o.target?.closest?.("td, th");if(o.shiftKey&&n&&e.dom.contains(n)){const r=S(e.state),l=e.posAtDOM(n,0);if(r?.grid&&T(e.state.doc.resolve(l))?.pos===r.grid.pos){const h=_(r.grid,l),i=_(r.grid,A.getState(e.state)?.anchor??e.state.selection.anchor);if(i&&h)return o.preventDefault(),e.dispatch(e.state.tr.setMeta(A,{anchor:i.pos+1,head:h.pos+1})),!0}}return A.getState(e.state)&&e.dispatch(e.state.tr.setMeta(A,null)),t.start(e,o)}},handleTextInput(e,o,n,r){return(S(e.state)?.cells?.length||0)<2?!1:R(e,{action:"clear_cells"},r)}},view(e){let o="";const n=()=>{const r=L(e),l=JSON.stringify(r);o!==l&&(o=l,s.onStateChange?.(r))};return n(),{update(){t.cancelIfStale(e),n()},destroy:t.destroy}}})}function it(s,t){if(!s.editable)return!1;const e=S(s.state);if(!e?.grid||!e.rect)return!1;if(t.key==="Escape"&&(A.getState(s.state)||e.cells.length>1))return s.dispatch(s.state.tr.setSelection(y.TextSelection.near(s.state.selection.$head)).setMeta(A,null)),!0;if(t.ctrlKey||t.metaKey||t.altKey)return!1;if(["Backspace","Delete"].includes(t.key)&&e.cells.length>1)return R(s,{action:"clear_cells"});if(t.key!=="Tab")return!1;const o=e.grid,n=_(o,s.state.selection.head);if(!n)return!1;const r=o.cells.indexOf(n)+(t.shiftKey?-1:1);if(r<0)return!1;if(r<o.cells.length)return Y(s,o.cells[r].pos),!0;if(!R(s,{action:"row_after"}))return!1;const l=S(s.state)?.grid,h=l?.matrix[l.rows.length-1]?.[0];return h&&Y(s,h.pos),!0}function at(s={}){return{name:"table-view",plugins:()=>[lt(s)],nodeViews:{table:(t,e,o)=>new Q(t,e,o)}}}exports.EMPTY_TABLE_STATE=O;exports.createTableExtension=at;exports.getTableState=L;exports.runTableCommand=R;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|