Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui-markdown-editor): direct page break - #276 #288

Merged
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -22,6 +22,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;
}