@progress/kendo-react-editor
Version:
React Editor enables users to create rich text content through a WYSIWYG interface. KendoReact Editor package
9 lines (8 loc) • 9.42 kB
JavaScript
/**
* @license
*-------------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the package root for more information
*-------------------------------------------------------------------------------------------
*/
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("@progress/kendo-editor-common"),k=require("../tableEdit.js"),d=require("./utils.js"),N=require("../utils.js"),E={rows:0,columns:0,width:null,widthUnit:"",height:null,heightUnit:"",position:null,textAlign:"",cellPadding:null,cellSpacing:null,backgroundColor:void 0,borderWidth:null,borderColor:void 0,borderStyle:null,collapseBorders:!1,id:"",className:"",caption:"",captionAlignment:null,captionPosition:null,headerRows:0,headerColumns:0,associateHeaders:"none"},A={left:{"margin-left":"","margin-right":"auto"},center:{"margin-left":"auto","margin-right":"auto"},right:{"margin-left":"auto","margin-right":""},"":{}},M=o=>{const t=[];for(let s=0;s<o.height;s++){const l=s*o.width,e=l+o.width;t.push(o.map.slice(l,e))}return t},$=(o,t)=>{let s=0,l=0;const e=M(t);e.forEach(n=>{let i=!0;n.forEach(r=>{const c=o.nodeAt(r);c&&c.type.name!=="table_header"&&(i=!1)}),i&&s++});for(let n=0;n<t.width;n++){let i=!0;for(let r=0;r<t.height;r++){const c=e[r]&&e[r][n],u=typeof c=="number"&&o.nodeAt(c);u&&u.type.name!=="table_header"&&(i=!1)}i&&l++}return{rows:s,columns:l}},v=o=>{const t=new Set;return o.content.forEach(s=>{s.content.forEach(l=>{t.add(d.parseStyle(l.attrs.style).padding||"")})}),t.size===1?Array.from(t)[0]:""},j=o=>{let t=!0,s=!0;return o.content.forEach(l=>{l.content.forEach(e=>{const n=e.type.name==="table_header",i=e.type.name==="table_cell";n&&!e.attrs.scope&&(t=!1),(n&&!e.attrs.id||i&&!e.attrs.headers)&&(s=!1)})}),t?"scope":s?"id":"none"},F=o=>{var a,h,m;const t=d.selectedCells(o),s=o.selection.$from,l=t.length?N.parentNode(s,f=>f.type.spec.tableRole==="table"):null;if(!l)return E;const e=N.parentNode(s,f=>f.type.name==="table_wrapper");let n="",i={},r={};e&&(r=d.parseStyle(e.node.attrs.style),e.node.content.forEach(f=>{if(f.type.name==="table_caption_external"){const S=w.pmDocToFragment(f);n=w.fragmentToHtml(S),n=n==="<img>"?"":n,i=d.parseStyle(f.attrs.style)}}));const c=d.parseStyle(((a=l.node.attrs)==null?void 0:a.style)||""),u=w.TableMap.get(l.node),p=r["margin-left"]==="auto"&&r["margin-right"]==="auto"?"center":r["margin-right"]==="auto"?"left":r["margin-left"]==="auto"?"right":null,y=$(l.node,u),g=v(l.node);return{...E,columns:u.width,rows:u.height,...d.nodeSize(l.node,"width"),...e&&/%/.test(r.width||"")?d.nodeSize(e.node,"width"):{},...d.nodeSize(l.node,"height"),textAlign:c["text-align"]||"",position:p||null,cellPadding:g?parseFloat(g):null,cellSpacing:c["border-spacing"]?parseFloat(c["border-spacing"]):null,backgroundColor:c["background-color"],borderWidth:c["border-width"]?parseFloat(c["border-width"]):null,borderColor:c["border-color"],borderStyle:c["border-style"]||null,collapseBorders:c["border-collapse"]==="collapse",caption:n||void 0,captionAlignment:i["text-align"]||"",captionPosition:i["caption-side"]||null,headerRows:y.rows,headerColumns:y.columns,associateHeaders:y.rows||y.columns?j(l.node):"none",id:((h=l.node.attrs)==null?void 0:h.id)||"",className:((m=l.node.attrs)==null?void 0:m.class)||""}},x=o=>{const s=o.lastChild.lastChild;return{node:s,pos:o.content.size-s.nodeSize}},_=(o,t,s,l,e)=>{const n=x(s),i=t+n.pos;if(!o.doc.nodeAt(i))return o;const c=o.doc.resolve(i),u=new w.CellSelection(c,c),p=o.tr.setSelection(u);let y=o.apply(p);for(let g=0;g<l;g++)e(y,a=>{y=y.apply(a)});return y},R=(o,t,s,l)=>{let e=o;for(let n=0;n<s;n++){const i=e.doc.nodeAt(t);if(i){const r=x(i),c=t+r.pos,u=e.doc.resolve(c),p=new w.CellSelection(u,u),y=e.tr.setSelection(p);e=e.apply(y),l(e,g=>{e=e.apply(g)})}}return e},O=(o,t,s,l)=>{let e=o,n=e.doc.nodeAt(t);if(!n)return e;let i=w.TableMap.get(n);return i.width===l&&i.height===s||(l>i.width?e=_(e,t,n,l-i.width,w.addColumnAfter):l<i.width&&(e=R(e,t,i.width-l,k.deleteColumnCmd)),n=e.doc.nodeAt(t),!n)||(i=w.TableMap.get(n),s>i.height?e=_(e,t,n,s-i.height,w.addRowAfter):s<i.height&&(e=R(e,t,i.height-s,k.deleteRowCmd))),e},q=(o,t,s)=>{const l=o.doc.nodeAt(s);if(!l)return o;let e={...l.attrs};const n=t.width!==null&&(t.widthUnit!=="%"||!t.caption&&!t.position);e=d.setNodeStyle(e,"width",n?t.width+t.widthUnit:""),e=d.setNodeStyle(e,"height",t.height!==null?t.height+t.heightUnit:""),e=d.setNodeStyle(e,"text-align",t.textAlign||""),e=d.setNodeStyle(e,"background-color",t.backgroundColor||""),e=d.setNodeStyle(e,"border-width",t.borderWidth?t.borderWidth+"px":""),e=d.setNodeStyle(e,"border-style",t.borderStyle||""),e=d.setNodeStyle(e,"border-color",t.borderColor||""),e=d.setNodeStyle(e,"border-spacing",t.cellSpacing!==null?t.cellSpacing+"px":""),t.cellSpacing!==null?e=d.setNodeStyle(e,"border-collapse","unset"):e=d.setNodeStyle(e,"border-collapse",t.collapseBorders?"collapse":"");const i=A[t.position||""]||{};Object.keys(i).forEach(c=>{e=d.setNodeStyle(e,c,i[c])}),e.id=t.id||null,e.class=t.className||null;const r=o.tr.setNodeMarkup(s,null,e);return o=o.apply(r),o},z=(o,t,s,l,e)=>{if(t.type.name!==l.name||Object.keys(e).length>0){const n=o.tr.setNodeMarkup(s,l,{...t.attrs,...e});return o.apply(n)}return o},B=(o,t)=>{/^cell-[0-9]+-[0-9]+$/.test(o.attrs.id||"")&&(t.id=null)},P=(o,t,s)=>{const l=o.doc.nodeAt(s),e=l&&w.TableMap.get(l);if(!l||!e)return o;const n=M(e);let i=o;const{table_header:r,table_cell:c}=o.schema.nodes,u=String(new Date().getTime()),p=[],y=(a,h)=>{const m=`cell-${a}${h}-${u}`;return p[a]||p.push([]),p[a][h]||p[a].push([]),p[a][h]=m,m},g=(a,h,m,f)=>{const S=[];if(f)for(let b=0;b<a;b++)p[b]&&p[b][h]&&S.push(p[b][h]);if(m)for(let b=0;b<h;b++)p[a]&&p[a][b]&&S.push(p[a][b]);return S.join(" ")};return n.forEach((a,h)=>{a.forEach((m,f)=>{const S=l.nodeAt(m);if(S){const b=f<t.headerColumns,T=h<t.headerRows,C={...S.attrs};B(S,C),C.headers=null,C.scope=null,T||b?(t.associateHeaders==="scope"&&(C.scope=T?"col":"row"),t.associateHeaders==="id"&&(C.id=y(h,f),(f>0||h>0)&&(C.headers=g(h,f,b,T)||null)),i=z(i,S,s+m+1,r,C)):(t.associateHeaders==="id"&&(C.headers=g(h,f,t.headerColumns>0,t.headerRows>0)||null),i=z(i,S,s+m+1,c,C))}})}),i},G=(o,t,s,l)=>{let e={...t.attrs};return e=d.setNodeStyle(e,"margin-left",""),e=d.setNodeStyle(e,"margin-right",""),Object.keys(l||{}).forEach(n=>{e=d.setNodeStyle(e,n,(l||{})[n]||"")}),o.apply(o.tr.setNodeMarkup(s,null,e))},D=(o,t)=>w.parseContent("<p>"+o+"</p>",t).content.firstChild.content,H=(o,t)=>{if(!o.caption)return;let s={caption:"",style:"display: table-caption"};o.captionAlignment&&(s=d.setNodeStyle(s,"text-align",o.captionAlignment)),o.captionPosition&&(s=d.setNodeStyle(s,"caption-side",o.captionPosition));const l=D(o.caption,t);return l&&t.nodes.table_caption_external.createAndFill(s,l)||void 0},U=o=>o.firstChild&&o.firstChild.type.name==="table_caption_external",J=(o,t,s,l)=>{let e=o,n;const i=s+2,r=s+1,c=e.schema;if(U(t))if(n=t.firstChild,l.caption){const u=D(l.caption,c);if(e=e.apply(e.tr.replaceWith(i,i+n.content.size,u)),n=e.doc.nodeAt(r),n){let p={...n.attrs};p=d.setNodeStyle(p,"text-align",l.captionAlignment||""),p=d.setNodeStyle(p,"caption-side",l.captionPosition||""),e=e.apply(e.tr.setNodeAttribute(r,"style",p.style))}}else e=e.apply(e.tr.deleteRange(i,i+n.content.size));else n=H(l,c),n&&(e=e.apply(e.tr.replaceWith(r,r,n)));return e},K=(o,t,s,l,e)=>{let n={state:o,pos:s};if(!l&&(t.position||t.caption)){let r={table:"",style:"display: table"};const c=[],u=t.position,p=u&&A[u];p&&Object.keys(p).forEach(h=>{r=d.setNodeStyle(r,h,p[h]||"")}),typeof t.width=="number"&&t.widthUnit==="%"&&(r=d.setNodeStyle(r,"width",t.width+t.widthUnit));const y=H(t,o.schema);y&&c.push(y);const g=o.doc.nodeAt(s);c.push(g);const a=o.schema.nodes.table_wrapper.createAndFill(r,c);if(a){const h=o.tr.replaceWith(s,s+g.content.size,a);o=o.apply(h),n={state:o,pos:s}}}else l&&(t.position||t.caption||!t.caption&&U(l.node))?(o=G(o,l.node,e,t.position&&A[t.position]),o=J(o,l.node,e,t),n={state:o,pos:e}):l&&!(t.position||t.caption)?n={state:o,node:l.node,pos:o.selection.$from.start(l.depth)-1}:n={state:o,pos:s};const i=l&&n.state.doc.nodeAt(e);if(i){let r;typeof t.width=="number"&&t.widthUnit==="%"?r=d.setNodeStyle(i.attrs,"width",t.width+t.widthUnit):t.widthUnit!=="%"&&/%/.test(d.parseStyle(i.attrs.style||"").width||"")&&(r=d.setNodeStyle(i.attrs,"width","")),r&&(n.state=n.state.apply(n.state.tr.setNodeMarkup(e,null,r)))}return n},L=(o,t)=>{const s=o.selection.$from,l=N.parentNode(s,f=>f.type.spec.tableRole==="table");if(!l)return;const e=s.start(l.depth)-1;let n=O(o,e,t.rows,t.columns);if(!n)return;const i=n.tr;d.tableCells(n).forEach(f=>{let S={...f.node.attrs};S=d.setNodeStyle(S,"padding",typeof t.cellPadding=="number"?t.cellPadding+"px":""),i.setNodeMarkup(f.pos,null,S)}),n=n.apply(i),n=q(n,t,e),n=P(n,t,e);const r=N.parentNode(s,f=>f.type.name==="table_wrapper"),c=s.start((r==null?void 0:r.depth)||0)-1,u=K(n,t,e,r,c);n=u.state;const p=u.pos,y=n.doc.nodeAt(u.pos);if(!y)return;const g=p+(r||l).node.content.size,a=o.tr.replaceWith(p,g,y),h=w.Selection.atEnd(y),m=w.TextSelection.create(a.doc,p+h.to+1);return a.setSelection(m),a};exports.applyTableData=L;exports.initialTableData=E;exports.tableDefaultData=F;exports.tablePositionStyles=A;