From ea5d2bc29053fd7d73449e760de624f4d723d486 Mon Sep 17 00:00:00 2001 From: Guilherme DIniz <64857365+guilherme-diniz@users.noreply.github.com> Date: Sat, 29 Aug 2020 00:57:51 -0300 Subject: [PATCH] feat: add insert new line inside a table cell (#255) * feat: add insert new line inside a table cell --- .gitignore | 3 ++- src/index.tsx | 2 ++ src/lib/markdown/tables.ts | 18 +++++++++++++++ src/nodes/HardBreak.ts | 45 ++++++++++++++++++++++++++++++++++++++ src/nodes/Table.ts | 20 ++++++++--------- 5 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/nodes/HardBreak.ts diff --git a/.gitignore b/.gitignore index 66ad27a1e..a00be0ef3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ dist node_modules/* .log -.DS_Store \ No newline at end of file +.DS_Store +.idea diff --git a/src/index.tsx b/src/index.tsx index 6a1f26902..5a53eb7f3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -64,6 +64,7 @@ import Placeholder from "./plugins/Placeholder"; import SmartText from "./plugins/SmartText"; import TrailingNode from "./plugins/TrailingNode"; import MarkdownPaste from "./plugins/MarkdownPaste"; +import HardBreak from "./nodes/HardBreak"; export { schema, parser, serializer } from "./server"; @@ -218,6 +219,7 @@ class RichMarkdownEditor extends React.PureComponent { [ new Doc(), new Text(), + new HardBreak(), new Paragraph(), new Blockquote(), new BulletList(), diff --git a/src/lib/markdown/tables.ts b/src/lib/markdown/tables.ts index 7b70b6704..9083c5898 100644 --- a/src/lib/markdown/tables.ts +++ b/src/lib/markdown/tables.ts @@ -12,6 +12,24 @@ export default function markdownTables(md: MarkdownIt) { tokens[i].level--; } + // convert break line into br tag + if (tokens[i].type === "inline" && tokens[i].content.includes("\\n")) { + const nodes: Token[] = []; + const breakParts = tokens[i].content.split("\\n"); + breakParts.forEach((part, index) => { + const token = new Token("text", "", 1); + token.content = part.trim(); + nodes.push(token); + + if (index < breakParts.length - 1) { + const brToken = new Token("br", "br", 1); + nodes.push(brToken); + } + }); + + tokens.splice(i, 1, ...nodes); + } + // filter out incompatible tokens from markdown-it that we don't need // in prosemirror. thead/tbody do nothing. if ( diff --git a/src/nodes/HardBreak.ts b/src/nodes/HardBreak.ts new file mode 100644 index 000000000..016949d1d --- /dev/null +++ b/src/nodes/HardBreak.ts @@ -0,0 +1,45 @@ +import Node from "./Node"; +import { isInTable } from "prosemirror-tables"; + +export default class HardBreak extends Node { + get name() { + return "br"; + } + + get schema() { + return { + inline: true, + group: "inline", + selectable: false, + parseDOM: [{ tag: "br" }], + toDOM() { + return ["br"]; + }, + }; + } + + commands({ type }) { + return () => (state, dispatch) => { + dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView()); + return true; + }; + } + + keys({ type }) { + return { + "Shift-Enter": (state, dispatch) => { + if (!isInTable(state)) return false; + dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView()); + return true; + }, + }; + } + + toMarkdown(state) { + state.write(" \\n "); + } + + parseMarkdown() { + return { node: "br" }; + } +} diff --git a/src/nodes/Table.ts b/src/nodes/Table.ts index 5897bf4fc..e86728572 100644 --- a/src/nodes/Table.ts +++ b/src/nodes/Table.ts @@ -1,25 +1,25 @@ import Node from "./Node"; -import { DecorationSet, Decoration } from "prosemirror-view"; +import { Decoration, DecorationSet } from "prosemirror-view"; import { - tableEditing, - goToNextCell, - addColumnBefore, addColumnAfter, + addColumnBefore, deleteColumn, deleteRow, deleteTable, - toggleHeaderColumn, - toggleHeaderRow, - toggleHeaderCell, - setCellAttr, fixTables, + goToNextCell, isInTable, + setCellAttr, + tableEditing, + toggleHeaderCell, + toggleHeaderColumn, + toggleHeaderRow, } from "prosemirror-tables"; import { - getCellsInColumn, + addRowAt, createTable, + getCellsInColumn, moveRow, - addRowAt, } from "prosemirror-utils"; import { Plugin, TextSelection } from "prosemirror-state";