Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add table rich editor extension (#31153)

Renato Atilio9f51f648035e
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
index c4dfe82819b..a7d689b891d 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
@@ -7,6 +7,7 @@ import link from "./link";
import mention from "./mention";
import quote from "./quote";
import strikethrough from "./strikethrough";
+import table from "./table";
import underline from "./underline";
/**
@@ -25,6 +26,7 @@ const defaultExtensions = [
mention,
strikethrough,
underline,
+ table,
];
defaultExtensions.forEach(registerRichEditorExtension);
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/table.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/table.js
new file mode 100644
index 00000000000..d38d12c1aa8
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/table.js
@@ -0,0 +1,233 @@
+// It makes sense to use prosemirror-tables for some of the table functionality,
+// but it has some differences from our structure (e.g. no thead/tbody).
+//
+// The main missing part of this extension for now is a UI companion
+
+// Example:
+//
+// | Left-aligned | Center-aligned | Right-aligned |
+// | :--- | :---: | ---: |
+// | git status | git status | git status |
+// | git diff | git diff | git diff |
+
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ table: {
+ content: "table_head? table_body",
+ group: "block",
+ tableRole: "table",
+ isolating: true,
+ selectable: true,
+ draggable: true,
+ parseDOM: [{ tag: "table" }],
+ toDOM() {
+ return ["table", 0];
+ },
+ },
+ table_head: {
+ content: "table_row",
+ tableRole: "head",
+ isolating: true,
+ parseDOM: [{ tag: "thead" }],
+ toDOM() {
+ return ["thead", 0];
+ },
+ },
+ table_body: {
+ content: "table_row+",
+ tableRole: "body",
+ isolating: true,
+ parseDOM: [{ tag: "tbody" }],
+ toDOM() {
+ return ["tbody", 0];
+ },
+ },
+ table_row: {
+ content: "(table_cell | table_header_cell)+",
+ tableRole: "row",
+ parseDOM: [{ tag: "tr" }],
+ toDOM() {
+ return ["tr", 0];
+ },
+ },
+ table_header_cell: {
+ content: "inline*",
+ tableRole: "header_cell",
+ attrs: { alignment: { default: null } },
+ parseDOM: [
+ {
+ tag: "th",
+ getAttrs(dom) {
+ return { alignment: dom.style.textAlign };
+ },
+ },
+ ],
+ toDOM(node) {
+ return [
+ "th",
+ {
+ style: node.attrs.alignment
+ ? `text-align: ${node.attrs.alignment}`
+ : undefined,
+ },
+ 0,
+ ];
+ },
+ },
+ table_cell: {
+ content: "inline*",
+ tableRole: "cell",
+ attrs: { alignment: { default: null } },
+ parseDOM: [
+ {
+ tag: "td",
+ getAttrs(dom) {
+ return { alignment: dom.style.textAlign };
+ },
+ },
+ ],
+ toDOM(node) {
+ return [
+ "td",
+ {
+ style: node.attrs.alignment
+ ? `text-align: ${node.attrs.alignment}`
+ : undefined,
+ },
+ 0,
+ ];
+ },
+ },
+ },
+ parse: {
+ table: { block: "table" },
+ thead: { block: "table_head" },
+ tbody: { block: "table_body" },
+ tr: { block: "table_row" },
+ th: {
+ block: "table_header_cell",
+ getAttrs(token) {
+ return {
+ alignment: token.attrGet("style")?.match(/text-align:(\w+)/)?.[1],
+ };
+ },
+ },
+ td: {
+ block: "table_cell",
+ getAttrs(token) {
+ return {
+ alignment: token.attrGet("style")?.match(/text-align:(\w+)/)?.[1],
+ };
+ },
+ },
+ },
+ serializeNode: {
+ // TODO(renato): state.renderInline should escape `|` if `state.inTable`
+ table(state, node) {
+ state.flushClose(1);
+
+ let headerBuffer = state.delim;
+ const prevInTable = state.inTable;
+ state.inTable = true;
+
+ // leading newline, it seems to have issues in a line just below a > blockquote otherwise
+ if (state.out) {
+ state.out += "\n";
+ }
+
+ // group is table_head or table_body
+ node.forEach((group, groupOffset, groupIndex) => {
+ group.forEach((row) => {
+ row.forEach((cell, cellOffset, cellIndex) => {
+ if (state.delim && state.atBlank()) {
+ state.out += state.delim;
+ }
+ state.out += cellIndex === 0 ? "| " : " | ";
+
+ state.renderInline(cell);
+
+ // if table_head
+ if (groupIndex === 0) {
+ if (cell.attrs.alignment === "center") {
+ headerBuffer += "|:---:";
+ } else if (cell.attrs.alignment === "left") {
+ headerBuffer += "|:---";
+ } else if (cell.attrs.alignment === "right") {
+ headerBuffer += "|---:";
+ } else {
+ headerBuffer += "|----";
+ }
+ }
+ });
+
+ state.out += " |\n";
+
+ if (headerBuffer) {
+ state.out += `${headerBuffer}|\n`;
+ headerBuffer = undefined;
+ }
+ });
+ });
+ state.out += "\n";
+ state.inTable = prevInTable;
+ },
+ table_head() {},
+ table_body() {},
+ table_row() {},
+ table_header_cell() {},
+ table_cell() {},
+ },
+ plugins({ pmState: { Plugin }, pmModel: { Slice } }) {
+ return new Plugin({
+ props: {
+ transformPasted(paste, view) {
+ let updatedPaste = paste;
+ paste.content.descendants((child, tableIndex) => {
+ if (child.type.name === "table") {
+ let tbody, thead;
+ child.descendants((node) => {
+ if (node.type.name === "table_body") {
+ tbody = node;
+ return false;
+ }
+
+ if (node.type.name === "table_head") {
+ thead = node;
+ return false;
+ }
+ });
+
+ if (!thead && tbody) {
+ thead = view.state.schema.nodes.table_head.create(
+ {},
+ tbody.firstChild
+ );
+
+ tbody = view.state.schema.nodes.table_body.create(
+ {},
+ tbody.content.content.slice(1)
+ );
+
+ const newTableContent = view.state.schema.nodes.table.create(
+ {},
+ [thead, tbody]
+ );
+
+ updatedPaste = new Slice(
+ paste.content.replaceChild(tableIndex, newTableContent),
+ paste.openStart,
+ paste.openEnd
+ );
+ }
+ }
+ });
+
+ return updatedPaste;
+ },
+ },
+ });
+ },
+};
+
+export default extension;
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/table-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/table-test.gjs
new file mode 100644
index 00000000000..b1dcee4d13a
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/table-test.gjs
@@ -0,0 +1,33 @@
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
+
+module(
+ "Integration | Component | prosemirror-editor - table extension",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ Object.entries({
+ "basic table": [
+ "| Header 1 | Header 2 |\n| --- | --- |\n| Cell 1 | Cell 2 |",
+ `<table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table>`,
+ `| Header 1 | Header 2 |\n|----|----|\n| Cell 1 | Cell 2 |\n\n`,
+ ],
+ "table with alignment": [
+ `| Left | Center | Right |\n| :--- | :---: | ---: |\n| A | B | C |`,
+ `<table><thead><tr><th style="text-align: left">Left</th><th style="text-align: center">Center</th><th style="text-align: right">Right</th></tr></thead><tbody><tr><td style="text-align: left">A</td><td style="text-align: center">B</td><td style="text-align: right">C</td></tr></tbody></table>`,
+ `| Left | Center | Right |\n|:---|:---:|---:|\n| A | B | C |\n\n`,
+ ],
+ "table within quotes": [
+ `> \n> | Header 1 | Header 2 |\n> | --- | --- |\n> | Cell 1 | Cell 2 |\n`,
+ `<blockquote><table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Cell 1</td><td>Cell 2</td></tr></tbody></table></blockquote>`,
+ `> \n> | Header 1 | Header 2 |\n> |----|----|\n> | Cell 1 | Cell 2 |\n\n`,
+ ],
+ }).forEach(([name, [markdown, html, expectedMarkdown]]) => {
+ test(name, async function (assert) {
+ this.siteSettings.rich_editor = true;
+ await testMarkdown(assert, markdown, html, expectedMarkdown);
+ });
+ });
+ }
+);