Skip to content

Commit

Permalink
feat(ui-markdown-editor): direct page break - #276
Browse files Browse the repository at this point in the history
Signed-off-by: d-e-v-esh <[email protected]>
  • Loading branch information
d-e-v-esh authored and jolanglinais committed Apr 2, 2021
1 parent 20ba3fc commit 0667d02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/ui-markdown-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { withLinks, isSelectionLinkBody } from './plugins/withLinks';
import { withHtml } from './plugins/withHtml';
import { withLists } from './plugins/withLists';
import FormatBar from './FormattingToolbar';
import { withText } from './plugins/withText';

export const markdownToSlate = (markdown) => {
const slateTransformer = new SlateTransformer();
Expand All @@ -40,14 +41,14 @@ export const MarkdownEditor = (props) => {
const editor = useMemo(() => {
if (augmentEditor) {
return augmentEditor(
withLists(withLinks(withHtml(withImages(
withLists(withLinks(withHtml(withImages(withText(
withSchema(withHistory(withReact(createEditor())))
))))
)))))
);
}
return withLists(withLinks(withHtml(withImages(
return withLists(withLinks(withHtml(withImages(withText(
withSchema(withHistory(withReact(createEditor())))
))));
)))));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
22 changes: 22 additions & 0 deletions packages/ui-markdown-editor/src/plugins/withText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Editor, Node } from 'slate';
import { insertThematicBreak, isBlockHeading } from '../utilities/toolbarHelpers';
import { HR } from '../utilities/schema'

export const withText = (editor) => {
// Inserts page break with dash
const { insertText } = editor;
editor.insertText = (text) => {
insertText(text);
const currentNode = Node.get(editor, editor.selection.focus.path);
if(isBlockHeading(editor)){
return;
}
const firstWord = currentNode.text;
if(firstWord !== '---'){
return;
}
Editor.deleteBackward(editor, { unit: 'word' });
insertThematicBreak(editor, HR);
}
return editor;
}

0 comments on commit 0667d02

Please sign in to comment.