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

fix(ui-markdown-editor): fixes cursor movement around headings - I260 #262

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions packages/ui-markdown-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { HorizontalRule } from './Span';
import {
PARAGRAPH, LINK, IMAGE, H1, H2, H3, H4, H5, H6, HR,
CODE_BLOCK, HTML_BLOCK, BLOCK_QUOTE, UL_LIST, OL_LIST, LIST_ITEM,
HTML_INLINE, SOFTBREAK, LINEBREAK, HEADINGS
HTML_INLINE, SOFTBREAK, HEADINGS
} from '../utilities/schema';
import {
DROPDOWN_STYLE_H1,
Expand All @@ -33,8 +33,7 @@ const Element = (props) => {
[H4]: () => (<Heading id={headingId} as="h4" style={DROPDOWN_STYLE_H4} {...attributes}>{children}</Heading>),
[H5]: () => (<Heading id={headingId} as="h5" style={DROPDOWN_STYLE_H5} {...attributes}>{children}</Heading>),
[H6]: () => (<Heading id={headingId} as="h6" style={DROPDOWN_STYLE_H6} {...attributes}>{children}</Heading>),
[SOFTBREAK]: () => (<span className={SOFTBREAK} {...attributes}> {children}</span>),
[LINEBREAK]: () => (<span {...attributes}>
[SOFTBREAK]: () => (<span {...attributes}>
<span contentEditable={false} style={{ userSelect: 'none' }}>
<br />
</span>
Expand All @@ -58,6 +57,7 @@ const Element = (props) => {
}
};
const elementRenderer = customElements
// console.log(customElements)
? { ...baseElementRenderer, ...customElements(attributes, children, element, editor) }
: baseElementRenderer;
return (elementRenderer[type] || elementRenderer.default)();
Expand Down
25 changes: 14 additions & 11 deletions packages/ui-markdown-editor/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import React, {
useCallback, useMemo, useState
} from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { CiceroMarkTransformer } from '@accordproject/markdown-cicero';
import { HtmlTransformer } from '@accordproject/markdown-html';
import { SlateTransformer } from '@accordproject/markdown-slate';
Expand All @@ -15,7 +13,7 @@ import withSchema from './utilities/schema';
import Element from './components';
import Leaf from './components/Leaf';
import { toggleMark, toggleBlock, insertThematicBreak,
insertLinebreak, insertHeadingbreak, isBlockHeading
insertSoftBreak, insertLineBreak, isBlockHeading, insertHeadingBreak
} from './utilities/toolbarHelpers';
import { withImages, insertImage } from './plugins/withImages';
import { withLinks, isSelectionLinkBody } from './plugins/withLinks';
Expand All @@ -29,6 +27,9 @@ export const markdownToSlate = (markdown) => {
};

export const MarkdownEditor = (props) => {



const {
canCopy,
canKeyDown,
Expand Down Expand Up @@ -74,10 +75,10 @@ export const MarkdownEditor = (props) => {
setShowLinkModal(true);
},
horizontal_rule: (code) => insertThematicBreak(editor, code),
linebreak: (code) => insertLinebreak(editor, code),
headingbreak: () => insertHeadingbreak(editor)
softbreak: (code) => insertSoftBreak(editor, code),
linebreak: () => insertLineBreak(editor),
headingbreak: () => insertHeadingBreak(editor)
};

const onKeyDown = useCallback((event) => {
if (!canKeyDown(editor, event)) {
event.preventDefault();
Expand All @@ -90,15 +91,17 @@ export const MarkdownEditor = (props) => {
return;
}

if (event.key === "Enter" && !isBlockHeading(editor)) {
return;
}

const hotkeys = Object.keys(HOTKEYS);
hotkeys.forEach((hotkey) => {
if (isHotkey(hotkey, event)) {
event.preventDefault();
const { code, type } = HOTKEYS[hotkey];
// if linebreak happens form a heading block then we run the insertHeadingBrake function
// other it is a linebreak every time
if(type === 'linebreak' && isBlockHeading(editor)){
hotkeyActions['headingbreak']()
return;
}
hotkeyActions[type](code);
}
});
Expand Down
10 changes: 5 additions & 5 deletions packages/ui-markdown-editor/src/utilities/hotkeys.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
LINK, IMAGE, BLOCK_QUOTE, UL_LIST, OL_LIST,
MARK_BOLD, MARK_ITALIC, MARK_CODE, HR, LINEBREAK,
HEADINGBREAK
SOFTBREAK
} from './schema';

const HOTKEYS = {
Expand Down Expand Up @@ -50,12 +50,12 @@ const HOTKEYS = {
code: HR,
},
'shift+enter': {
type: 'linebreak',
code: LINEBREAK,
type: 'softbreak',
code: SOFTBREAK,
},
'enter': {
type: 'headingbreak',
code: HEADINGBREAK
type: 'linebreak',
code: LINEBREAK
}
};

Expand Down
18 changes: 13 additions & 5 deletions packages/ui-markdown-editor/src/utilities/toolbarHelpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Node, Editor, Transforms } from 'slate';
import {
LIST_ITEM, BLOCK_QUOTE, LIST_TYPES, PARAGRAPH
} from './schema';
import { LIST_ITEM, BLOCK_QUOTE, LIST_TYPES, PARAGRAPH, LINEBREAK } from './schema';

export const isBlockActive = (editor, format) => {
const [match] = Editor.nodes(editor, {
Expand Down Expand Up @@ -66,6 +64,7 @@ export const toggleMark = (editor, format) => {
};

export const toggleHistory = (editor, format) => {
console.log(format)
if (format === 'undo') {
editor.undo();
} else { editor.redo(); }
Expand All @@ -91,17 +90,26 @@ export const insertThematicBreak = (editor, type) => {
Transforms.insertNodes(editor, tBreakNode);
};

export const insertLinebreak = (editor, type) => {
export const insertSoftBreak = (editor, type) => {
const text = { object: 'text', text: '' };
const br = { type, children: [text], data: {} };
Transforms.insertNodes(editor, br);
Transforms.move(editor, { distance: 1, unit: 'character' });
};

export const insertHeadingbreak = (editor) => {
export const insertLineBreak = (editor) => {
const text = { object: 'text', text: '' };
const lineBreakNode = { type: LINEBREAK, children: [text] };
Transforms.insertNodes(editor, lineBreakNode);
Transforms.move(editor, { distance: 1, unit: 'line' })
return;
}

export const insertHeadingBreak = (editor) => {
const text = { object: 'text', text: '' };
const n = { object: "block", type: 'paragraph', children: [text] };
Transforms.insertNodes(editor, n);
Transforms.move(editor, { distance: 1, unit: 'character' })
return;
}

Expand Down