{"version":3,"file":"TableExtensions.cjs","sources":["../../../../../../../src/components/ui/editor/ihub-editor/extensions/TableExtensions.ts"],"sourcesContent":["import Table, { TableView } from \"@tiptap/extension-table\";\nimport TableRow from \"@tiptap/extension-table-row\";\nimport TableCell from \"@tiptap/extension-table-cell\";\nimport TableHeader from \"@tiptap/extension-table-header\";\nimport type { Node as PMNode } from \"@tiptap/pm/model\";\nimport { readStyleValue, safeColor, sanitizeStyle, splitDeclarations } from \"./colorUtils\";\n\n/** HTML attributes email templates rely on (role=\"presentation\", cellpadding…). */\nconst TABLE_HTML_ATTRS = [\"width\", \"cellpadding\", \"cellspacing\", \"border\", \"align\", \"role\", \"bgcolor\"] as const;\nconst CELL_HTML_ATTRS = [\"width\", \"align\", \"valign\", \"height\"] as const;\n\nconst htmlAttr = (name: string) => ({\n  default: null,\n  parseHTML: (el: HTMLElement) => el.getAttribute(name),\n  renderHTML: (attrs: Record<string, unknown>) => (attrs[name] ? { [name]: attrs[name] } : {}),\n});\n\nconst backgroundColorAttr = {\n  default: null,\n  // bgcolor first: it stays hex, while style colours come back as rgb() after serialising.\n  parseHTML: (el: HTMLElement) => el.getAttribute(\"bgcolor\") || readStyleValue(el, \"background-color\") || null,\n  renderHTML: (attrs: Record<string, unknown>) => {\n    if (!attrs.backgroundColor) return {};\n    const color = safeColor(attrs.backgroundColor, \"\");\n    return color ? { style: `background-color:${color}`, bgcolor: color.startsWith(\"#\") ? color : null } : {};\n  },\n};\n\n/** Applies preserved attributes to the live <table> the resizable view renders. */\nexport class TableViewWithAttrs extends TableView {\n  /** Style properties this view set, so they can be cleared when the style changes. */\n  private appliedProps: string[] = [];\n\n  constructor(node: PMNode, cellMinWidth: number) {\n    super(node, cellMinWidth);\n    this.applyAttrs(node);\n  }\n\n  update(node: PMNode): boolean {\n    const ok = super.update(node);\n    if (ok) this.applyAttrs(node);\n    return ok;\n  }\n\n  private applyAttrs(node: PMNode): void {\n    TABLE_HTML_ATTRS.forEach((name) => {\n      const value = node.attrs[name];\n      if (value) this.table.setAttribute(name, String(value));\n      else this.table.removeAttribute(name);\n    });\n    const firstRow = node.firstChild;\n    const hasFixedWidths = Boolean(firstRow && firstRow.childCount && firstRow.child(0).attrs.colwidth);\n    this.appliedProps.forEach((prop) => this.table.style.removeProperty(prop));\n    this.appliedProps = [];\n    const style = sanitizeStyle(node.attrs.style);\n    if (!style) return;\n    splitDeclarations(style).forEach((decl) => {\n      const [prop, ...rest] = decl.split(\":\");\n      const property = prop.trim().toLowerCase();\n      if (hasFixedWidths && (property === \"width\" || property === \"min-width\")) return;\n      const raw = rest.join(\":\").trim();\n      const important = /!important$/i.test(raw);\n      this.table.style.setProperty(property, raw.replace(/!important$/i, \"\").trim(), important ? \"important\" : \"\");\n      this.appliedProps.push(property);\n    });\n  }\n}\n\nexport const IHubTable = Table.extend({\n  addAttributes() {\n    return {\n      ...this.parent?.(),\n      ...Object.fromEntries(TABLE_HTML_ATTRS.map((name) => [name, htmlAttr(name)])),\n    };\n  },\n}).configure({ resizable: true, View: TableViewWithAttrs, HTMLAttributes: { class: \"ihub-te-table\" } });\n\nexport const IHubTableRow = TableRow.extend({\n  addAttributes() {\n    return { ...this.parent?.(), bgcolor: htmlAttr(\"bgcolor\") };\n  },\n});\n\nexport const IHubTableCell = TableCell.extend({\n  addAttributes() {\n    return {\n      ...this.parent?.(),\n      backgroundColor: backgroundColorAttr,\n      ...Object.fromEntries(CELL_HTML_ATTRS.map((name) => [name, htmlAttr(name)])),\n    };\n  },\n});\n\nexport const IHubTableHeader = TableHeader.extend({\n  addAttributes() {\n    return {\n      ...this.parent?.(),\n      backgroundColor: backgroundColorAttr,\n      ...Object.fromEntries(CELL_HTML_ATTRS.map((name) => [name, htmlAttr(name)])),\n    };\n  },\n});\n"],"names":["TABLE_HTML_ATTRS","CELL_HTML_ATTRS","htmlAttr","name","default","parseHTML","el","getAttribute","renderHTML","attrs","_defineProperty","backgroundColorAttr","readStyleValue","backgroundColor","color","safeColor","style","concat","bgcolor","startsWith","TableViewWithAttrs","_TableView","node","cellMinWidth","_this","_classCallCheck","_callSuper","callSuper","this","applyAttrs","_inherits","_createClass","key","value","ok","_superPropGet","_this2","forEach","table","setAttribute","String","removeAttribute","firstRow","firstChild","hasFixedWidths","Boolean","childCount","child","colwidth","appliedProps","prop","removeProperty","sanitizeStyle","splitDeclarations","decl","_decl$split","split","_decl$split2","_toArray","toArray","rest","_arrayLikeToArray","slice","property","trim","toLowerCase","raw","join","important","test","setProperty","replace","push","TableView","IHubTable","Table","extend","addAttributes","_this$parent","_objectSpread","objectSpread2","parent","call","Object","fromEntries","map","configure","resizable","View","HTMLAttributes","class","IHubTableRow","TableRow","_this$parent2","IHubTableCell","TableCell","_this$parent3","IHubTableHeader","TableHeader","_this$parent4"],"mappings":"84BAQMA,EAAmB,CAAC,QAAS,cAAe,cAAe,SAAU,QAAS,OAAQ,WACtFC,EAAkB,CAAC,QAAS,QAAS,SAAU,UAE/CC,EAAW,SAACC,GAAY,MAAM,CAClCC,QAAS,KACTC,UAAW,SAACC,GAAe,OAAKA,EAAGC,aAAaJ,EAAK,EACrDK,WAAY,SAACC,GAA8B,OAAMA,EAAMN,GAAKO,iBAAA,CAAA,EAAMP,EAAOM,EAAMN,IAAU,EAAE,EAC5F,EAEKQ,EAAsB,CAC1BP,QAAS,KAETC,UAAW,SAACC,GAAe,OAAKA,EAAGC,aAAa,YAAcK,EAAcA,eAACN,EAAI,qBAAuB,IAAI,EAC5GE,WAAY,SAACC,GACX,IAAKA,EAAMI,gBAAiB,MAAO,GACnC,IAAMC,EAAQC,EAASA,UAACN,EAAMI,gBAAiB,IAC/C,OAAOC,EAAQ,CAAEE,MAAKC,oBAAAA,OAAsBH,GAASI,QAASJ,EAAMK,WAAW,KAAOL,EAAQ,MAAS,EACzG,GAIWM,WAAmBC,GAI9B,SAAAD,EAAYE,EAAcC,GAAoB,IAAAC,EAEtB,OAFsBC,sBAAAL,GAC5CI,EAAAE,EAAAC,UAAAC,KAAAR,EAAME,CAAAA,E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