Skip to content

Commit

Permalink
Markdown stability patch (#7238)
Browse files Browse the repository at this point in the history
* fix: enforce empty paragraph when converting from markdown to slate

* fix: handle unwrapping inline nodes in lists

* style: lint code

* fix: lost focus after backspace after break
  • Loading branch information
demshy authored Aug 7, 2024
1 parent 86c93a4 commit 943ba89
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import isHotkey from 'is-hotkey';
import { Editor, Transforms } from 'slate';

import keyDownEnter from './keyDownEnter';
import keyDownBackspace from './keyDownBackspace';
import isCursorInNonDefaultBlock from '../locations/isCursorInNonDefaultBlock';
import toggleBlock from './toggleBlock';
import isCursorCollapsedAfterSoftBreak from '../locations/isCursorCollapsedAfterSoftBreak';

const HEADING_HOTKEYS = {
'mod+1': 'heading-one',
Expand All @@ -25,6 +27,13 @@ function keyDown(event, editor) {
}
}

if (isHotkey('backspace', event) && isCursorCollapsedAfterSoftBreak(editor)) {
const [, path] = Editor.previous(editor);
Transforms.removeNodes(editor, { at: path });
event.preventDefault();
return false;
}

if (!isCursorInNonDefaultBlock(editor)) return;

if (isHotkey('enter', event)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Editor, Range } from 'slate';

function isCursorCollapsedAfterSoftBreak(editor) {
const { selection } = editor;
if (!selection) return false;
if (Range.isExpanded(selection)) return false;

const previous = Editor.previous(editor);

return previous && previous[0].type == 'break';
}

export default isCursorCollapsedAfterSoftBreak;
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ import lowestMatchedAncestor from '../../matchers/lowestMatchedAncestor';
function unwrapIfCursorAtStart(editor, mergeWithPrevious = false) {
if (editor.selection.anchor.offset !== 0) return false;

const node = Editor.above(editor, lowestMatchedAncestor(editor, 'non-default'));
let [node, path] = Editor.above(editor, lowestMatchedAncestor(editor, 'non-default'));

if (node[1].length == 0) return false;
if (path.length == 0) return false;

const isHeading = `${node[0].type}`.startsWith('heading-');
const isHeading = `${node.type}`.startsWith('heading-');
if (isHeading) {
Transforms.setNodes(editor, { type: 'paragraph' });
return false;
}

const isBlock = Editor.isBlock(editor, node);
const [parentBlock, parentBlockPath] = Editor.above(
editor,
lowestMatchedAncestor(editor, 'block'),
);
if (!isBlock) {
if (!Editor.isStart(editor, path, parentBlockPath)) {
return false;
}

[node, path] = [parentBlock, parentBlockPath];
}

Editor.withoutNormalizing(editor, () => {
Transforms.unwrapNodes(editor, { match: n => n.type === node[0].type, split: true });
Transforms.unwrapNodes(editor, { match: n => n.type === node.type, split: true });

if (mergeWithPrevious) {
Transforms.mergeNodes(editor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,22 @@ export default function remarkToSlate({ voidCodeBlock } = {}) {
* Convert simple cases that only require a type and children, with no
* additional properties.
*/
case 'root':
case 'paragraph':
case 'blockquote':
case 'tableRow':
case 'tableCell': {
return createBlock(typeMap[node.type], nodes);
}

/**
* Root element
* If the root node is empty, we need to add a paragraph node to it.
*/
case 'root': {
const children = isEmpty(nodes) ? [createBlock('paragraph')] : nodes;
return createBlock(typeMap[node.type], children);
}

/**
* List Items
*
Expand Down

0 comments on commit 943ba89

Please sign in to comment.