Skip to content

Commit

Permalink
fix(editor): working markdown shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 committed Oct 21, 2023
1 parent 222a7bf commit 87f51ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 9 additions & 5 deletions apps/client/src/components/editor/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import {
slateDataToString,
} from "@snailycad/utils/editor";
import * as React from "react";
import { Editor as _Editor, Node as SlateNode, type Descendant, createEditor } from "slate";
import {
Editor as _Editor,
Node as SlateNode,
type Descendant,
createEditor,
Element as SlateElement,
} from "slate";
import {
Editable,
ReactEditor,
Expand Down Expand Up @@ -88,12 +94,12 @@ export function Editor(props: EditorProps) {
const { text } = SlateNode.leaf(editor, path);
const beforeText = text.slice(0, diff.start) + diff.text.slice(0, -1);
if (!(beforeText in SHORTCUTS)) {
return false;
return;
}

const blockEntry = _Editor.above(editor, {
at: path,
match: (n) => _Editor.isBlock(editor, n as any),
match: (n) => SlateElement.isElement(n) && _Editor.isBlock(editor, n),
});
if (!blockEntry) {
return false;
Expand All @@ -115,8 +121,6 @@ export function Editor(props: EditorProps) {
editor.onChange();
}, [props.value]); // eslint-disable-line react-hooks/exhaustive-deps

console.log({ props });

return (
<div
className={classNames(
Expand Down
6 changes: 3 additions & 3 deletions apps/client/src/lib/editor/withShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function withShortcuts(editor: SlateEditor) {
if (text.endsWith(" ") && selection && Range.isCollapsed(selection)) {
const { anchor } = selection;
const block = Editor.above(editor, {
match: (n) => Editor.isBlock(editor, n as any),
match: (n) => SlateElement.isElement(n) && Editor.isBlock(editor, n),
});
const path = block ? block[1] : [];
const start = Editor.start(editor, path);
Expand All @@ -41,7 +41,7 @@ export function withShortcuts(editor: SlateEditor) {
type,
};
Transforms.setNodes<SlateElement>(editor, newProperties, {
match: (n) => Editor.isBlock(editor, n as any),
match: (n) => SlateElement.isElement(n) && Editor.isBlock(editor, n),
});

if (type === "list-item") {
Expand All @@ -65,7 +65,7 @@ export function withShortcuts(editor: SlateEditor) {

if (selection && Range.isCollapsed(selection)) {
const match = Editor.above(editor, {
match: (n) => Editor.isBlock(editor, n as any),
match: (n) => SlateElement.isElement(n) && Editor.isBlock(editor, n),
});

if (match) {
Expand Down

0 comments on commit 87f51ec

Please sign in to comment.