From 688c4d064a8ccafe2130e14b7cc865030aad15a3 Mon Sep 17 00:00:00 2001 From: Liyuan Li Date: Thu, 28 May 2020 13:57:34 +0800 Subject: [PATCH] :bug: fix #456 --- CHANGELOG.md | 2 ++ src/ts/wysiwyg/toolbarEvent.ts | 43 ++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 146c1c4d7..915a23131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,8 @@ ### v3.2.11 / 2020-05-xx +* [456](https://github.com/Vanessa219/vditor/pull/456) 根据逗号/制表符生成表格 `引入特性` + ### v3.2.10 / 2020-05-27 * [453](https://github.com/Vanessa219/vditor/issues/453) bold 位于子菜单中无作用 `修复缺陷` diff --git a/src/ts/wysiwyg/toolbarEvent.ts b/src/ts/wysiwyg/toolbarEvent.ts index 33590b7a8..a6933fde2 100644 --- a/src/ts/wysiwyg/toolbarEvent.ts +++ b/src/ts/wysiwyg/toolbarEvent.ts @@ -228,44 +228,47 @@ export const toolbarEvent = (vditor: IVditor, actionBtn: Element) => { setSelectionFocus(range); } } else if (commandName === "table") { - if (blockElement && blockElement.innerHTML.trim().replace(Constants.ZWSP, "") === "") { - const tableHTML = `
col1col2col3
`; - console.log("outerHTML"); - blockElement.outerHTML = tableHTML; + let tableHTML = `
col1col2col3
` + if (range.toString().trim() === "") { + if (blockElement && blockElement.innerHTML.trim().replace(Constants.ZWSP, "") === "") { + blockElement.outerHTML = tableHTML; + } else { + document.execCommand("insertHTML", false, tableHTML); + } + range.selectNode(vditor.wysiwyg.element.querySelector("wbr").previousSibling); + vditor.wysiwyg.element.querySelector("wbr").remove(); + setSelectionFocus(range); } else { - let tableHTML = ``; - - const tableText = range.toString().split("\n") + tableHTML = `
`; + const tableText = range.toString().split("\n"); + const delimiter = tableText[0].split(",").length > tableText[0].split("\t").length ? "," : "\t"; tableText.forEach((rows, index) => { if (index === 0) { - rows.split(",").forEach((header, index) => { - if(index === 0) { + rows.split(delimiter).forEach((header, subIndex) => { + if (subIndex === 0) { tableHTML += ``; } else { tableHTML += ``; } - }) - tableHTML += ``; + }); + tableHTML += ""; } else { if (index === 1) { - tableHTML += ``; + tableHTML += ""; } else { - tableHTML += ``; + tableHTML += ""; } - rows.split(",").forEach(cell => { + rows.split(delimiter).forEach((cell) => { tableHTML += ``; - }) + }); tableHTML += ``; - console.log(index, tableHTML); } }); - tableHTML += `
${header}${header}
${cell}
`; + tableHTML += ""; document.execCommand("insertHTML", false, tableHTML); + setRangeByWbr(vditor.wysiwyg.element, range); } - range.selectNode(vditor.wysiwyg.element.querySelector("wbr").previousSibling); - vditor.wysiwyg.element.querySelector("wbr").remove(); - setSelectionFocus(range); } else if (commandName === "line") { if (blockElement) { const hrHTML = '

\n

';