@underverse-ui/underverse 1.0.146 → 1.0.147

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/dist/index.js CHANGED
@@ -25758,42 +25758,77 @@ function extractClipboardHtmlFragment(html) {
25758
25758
  }
25759
25759
  return html;
25760
25760
  }
25761
- var EMPTY_TABLE_CELL_HTML = "<p></p>";
25762
- var TABLE_MEDIA_SELECTOR = "img,video,audio,iframe,svg,canvas,embed,object";
25763
- var TABLE_BLOCK_SELECTOR = "p,ul,ol,blockquote,pre,h1,h2,h3,h4,h5,h6,hr,table";
25764
- function hasMeaningfulTableCellText(value) {
25765
- return Boolean(value?.replace(/\u00a0/g, " ").trim());
25761
+ function normalizeClipboardCellText(value) {
25762
+ return value.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\u00a0/g, " ").replace(/[ \t]+\n/g, "\n").replace(/\n[ \t]+/g, "\n").replace(/\n+$/g, "").replace(/^\n+/g, "").trim();
25766
25763
  }
25767
- function normalizeClipboardTableCell(cell) {
25768
- const hasText = hasMeaningfulTableCellText(cell.textContent);
25769
- const hasBlockContent = Boolean(cell.querySelector(TABLE_BLOCK_SELECTOR));
25770
- const hasMediaContent = Boolean(cell.querySelector(TABLE_MEDIA_SELECTOR));
25771
- if (!hasText && !hasBlockContent && !hasMediaContent) {
25772
- cell.innerHTML = EMPTY_TABLE_CELL_HTML;
25764
+ function getClipboardCellText(node) {
25765
+ if (node.nodeType === Node.TEXT_NODE) {
25766
+ return node.textContent ?? "";
25773
25767
  }
25774
- }
25775
- function normalizeClipboardTable(table) {
25776
- for (const cell of Array.from(table.querySelectorAll("td,th"))) {
25777
- if (!(cell instanceof HTMLTableCellElement)) continue;
25778
- normalizeClipboardTableCell(cell);
25768
+ if (!(node instanceof HTMLElement)) {
25769
+ return "";
25770
+ }
25771
+ if (node.tagName === "BR") {
25772
+ return "\n";
25779
25773
  }
25774
+ const childText = Array.from(node.childNodes).map(getClipboardCellText).join("");
25775
+ if ((node.tagName === "P" || node.tagName === "DIV" || node.tagName === "LI") && childText && !childText.endsWith("\n")) {
25776
+ return `${childText}
25777
+ `;
25778
+ }
25779
+ return childText;
25780
+ }
25781
+ function getHtmlTableRows(table) {
25782
+ const rows = Array.from(table.querySelectorAll("tr")).map(
25783
+ (row) => Array.from(row.children).filter((cell) => cell instanceof HTMLTableCellElement).map((cell) => ({
25784
+ text: normalizeClipboardCellText(getClipboardCellText(cell)),
25785
+ isHeader: cell.tagName === "TH"
25786
+ }))
25787
+ );
25788
+ return rows.filter((row) => row.length > 0);
25789
+ }
25790
+ function createParagraphContent(text) {
25791
+ return text ? {
25792
+ type: "paragraph",
25793
+ content: [{ type: "text", text }]
25794
+ } : { type: "paragraph" };
25795
+ }
25796
+ function createTableCellContent(cell) {
25797
+ const lines = cell.text.split("\n");
25798
+ const paragraphs = (lines.length > 0 ? lines : [""]).map(createParagraphContent);
25799
+ return {
25800
+ type: cell.isHeader ? "tableHeader" : "tableCell",
25801
+ content: paragraphs.length > 0 ? paragraphs : [{ type: "paragraph" }]
25802
+ };
25780
25803
  }
25781
- function getClipboardTableHtml(dataTransfer) {
25804
+ function createTableContent(rows, minColumnCount = 1) {
25805
+ const tableRows = rows.filter((row) => row.length > 0);
25806
+ if (tableRows.length === 0) return null;
25807
+ const columnCount = tableRows.reduce((max, row) => Math.max(max, row.length), 0);
25808
+ if (columnCount < minColumnCount) return null;
25809
+ return {
25810
+ type: "table",
25811
+ content: tableRows.map((row) => {
25812
+ const normalizedRow = Array.from(
25813
+ { length: columnCount },
25814
+ (_, index) => row[index] ?? { text: "", isHeader: false }
25815
+ );
25816
+ return {
25817
+ type: "tableRow",
25818
+ content: normalizedRow.map(createTableCellContent)
25819
+ };
25820
+ })
25821
+ };
25822
+ }
25823
+ function getClipboardTableContent(dataTransfer) {
25782
25824
  const html = getClipboardData(dataTransfer, "text/html");
25783
- if (!/<table(?:\s|>)/i.test(html)) return "";
25825
+ if (!/<table(?:\s|>)/i.test(html)) return null;
25826
+ if (typeof DOMParser === "undefined") return null;
25784
25827
  const fragment = extractClipboardHtmlFragment(html);
25785
- if (typeof DOMParser !== "undefined") {
25786
- const doc = new DOMParser().parseFromString(fragment, "text/html");
25787
- const table = doc.querySelector("table");
25788
- if (table instanceof HTMLTableElement) {
25789
- normalizeClipboardTable(table);
25790
- return table.outerHTML;
25791
- }
25792
- }
25793
- return fragment;
25794
- }
25795
- function escapeHtml(value) {
25796
- return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
25828
+ const doc = new DOMParser().parseFromString(fragment, "text/html");
25829
+ const table = doc.querySelector("table");
25830
+ if (!(table instanceof HTMLTableElement)) return null;
25831
+ return createTableContent(getHtmlTableRows(table));
25797
25832
  }
25798
25833
  function parseClipboardTsvRows(text) {
25799
25834
  const rows = [];
@@ -25845,22 +25880,14 @@ function parseClipboardTsvRows(text) {
25845
25880
  }
25846
25881
  return rows;
25847
25882
  }
25848
- function renderClipboardTableCellHtml(value) {
25849
- const lines = value.split("\n");
25850
- const paragraphs = lines.length > 0 ? lines : [""];
25851
- return paragraphs.map((line) => `<p>${escapeHtml(line)}</p>`).join("");
25852
- }
25853
- function getClipboardTsvTableHtml(dataTransfer) {
25883
+ function getClipboardTsvTableContent(dataTransfer) {
25854
25884
  const text = getClipboardData(dataTransfer, "text/plain").replace(/\r\n/g, "\n").replace(/\r/g, "\n");
25855
- if (!text.includes(" ")) return "";
25885
+ if (!text.includes(" ")) return null;
25856
25886
  const rows = parseClipboardTsvRows(text);
25857
- if (rows.length === 0 || rows.every((row) => row.length < 2)) return "";
25858
- const columnCount = rows.reduce((max, row) => Math.max(max, row.length), 0);
25859
- const body = rows.map((row) => {
25860
- const normalizedRow = Array.from({ length: columnCount }, (_, index) => row[index] ?? "");
25861
- return `<tr>${normalizedRow.map((cell) => `<td>${renderClipboardTableCellHtml(cell)}</td>`).join("")}</tr>`;
25862
- }).join("");
25863
- return `<table><tbody>${body}</tbody></table>`;
25887
+ return createTableContent(
25888
+ rows.map((row) => row.map((cell) => ({ text: normalizeClipboardCellText(cell), isHeader: false }))),
25889
+ 2
25890
+ );
25864
25891
  }
25865
25892
  function fileToDataUrl(file) {
25866
25893
  return new Promise((resolve, reject) => {
@@ -25916,16 +25943,16 @@ var ClipboardImages = Extension2.create({
25916
25943
  props: {
25917
25944
  handlePaste: (_view, event) => {
25918
25945
  if (!event || !event.clipboardData) return false;
25919
- const tableHtml = getClipboardTableHtml(event.clipboardData);
25920
- if (tableHtml) {
25946
+ const tableContent = getClipboardTableContent(event.clipboardData);
25947
+ if (tableContent) {
25921
25948
  event.preventDefault();
25922
- editor.chain().focus().insertContent(tableHtml).run();
25949
+ editor.chain().focus().insertContent(tableContent).run();
25923
25950
  return true;
25924
25951
  }
25925
- const tsvTableHtml = getClipboardTsvTableHtml(event.clipboardData);
25926
- if (tsvTableHtml) {
25952
+ const tsvTableContent = getClipboardTsvTableContent(event.clipboardData);
25953
+ if (tsvTableContent) {
25927
25954
  event.preventDefault();
25928
- editor.chain().focus().insertContent(tsvTableHtml).run();
25955
+ editor.chain().focus().insertContent(tsvTableContent).run();
25929
25956
  return true;
25930
25957
  }
25931
25958
  const files = getImageFiles(event.clipboardData);