Skip to content

Commit

Permalink
Merge pull request #456 from jakekwak/pullrequest
Browse files Browse the repository at this point in the history
根据逗号/制表符生成表格
  • Loading branch information
Vanessa219 authored May 28, 2020
2 parents 869700c + 2e2a48a commit e975d1a
Show file tree
Hide file tree
Showing 2 changed files with 9,185 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/ts/wysiwyg/toolbarEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,39 @@ export const toolbarEvent = (vditor: IVditor, actionBtn: Element) => {
setSelectionFocus(range);
}
} else if (commandName === "table") {
const tableHTML = `<table data-block="0"><thead><tr><th>col1<wbr></th><th>col2</th><th>col3</th></tr></thead><tbody><tr><td> </td><td> </td><td> </td></tr><tr><td> </td><td> </td><td> </td></tr></tbody></table>`;
if (blockElement && blockElement.innerHTML.trim().replace(Constants.ZWSP, "") === "") {
const tableHTML = `<table data-block="0"><thead><tr><th>col1<wbr></th><th>col2</th><th>col3</th></tr></thead><tbody><tr><td> </td><td> </td><td> </td></tr><tr><td> </td><td> </td><td> </td></tr></tbody></table>`;
console.log("outerHTML");
blockElement.outerHTML = tableHTML;
} else {
let tableHTML = `<table data-block="0"><thead><tr>`;

const tableText = range.toString().split("\n")

tableText.forEach((rows, index) => {
if (index === 0) {
rows.split(",").forEach((header, index) => {
if(index === 0) {
tableHTML += `<th>${header}<wbr></th>`;
} else {
tableHTML += `<th>${header}</th>`;
}
})
tableHTML += `</tr></thead>`;
} else {
if (index === 1) {
tableHTML += `<tbody><tr>`;
} else {
tableHTML += `<tr>`;
}
rows.split(",").forEach(cell => {
tableHTML += `<td>${cell}</td>`;
})
tableHTML += `</tr>`;
console.log(index, tableHTML);
}
});
tableHTML += `</tbody></table>`;
document.execCommand("insertHTML", false, tableHTML);
}
range.selectNode(vditor.wysiwyg.element.querySelector("wbr").previousSibling);
Expand Down
Loading

0 comments on commit e975d1a

Please sign in to comment.