-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Editor, type BaseEditor, Transforms, Element as SlateElement } from "slate"; | ||
import type { ReactEditor } from "slate-react"; | ||
|
||
const LIST_TYPES = ["numbered-list", "bulleted-list"]; | ||
|
||
export function isMarkActive(editor: BaseEditor & ReactEditor, format: string) { | ||
const marks = Editor.marks(editor); | ||
|
||
return marks ? (marks as any)[format] === true : false; | ||
} | ||
|
||
export function toggleBlock(editor: BaseEditor & ReactEditor, format: string) { | ||
const isActive = isBlockActive(editor, format); | ||
const isList = LIST_TYPES.includes(format); | ||
|
||
Transforms.unwrapNodes(editor, { | ||
match: (n) => !Editor.isEditor(n) && SlateElement.isElement(n) && LIST_TYPES.includes(n.type), | ||
split: true, | ||
}); | ||
|
||
const newProperties: Partial<SlateElement> = { | ||
// @ts-expect-error ignore | ||
type: isActive ? "paragraph" : isList ? "list-item" : format, | ||
}; | ||
Transforms.setNodes<SlateElement>(editor, newProperties); | ||
|
||
if (!isActive && isList) { | ||
const block = { type: format, children: [] }; | ||
// @ts-expect-error ignore | ||
Transforms.wrapNodes(editor, block); | ||
} | ||
} | ||
|
||
export function toggleMark(editor: BaseEditor & ReactEditor, format: string) { | ||
const isActive = isMarkActive(editor, format); | ||
|
||
if (isActive) { | ||
Editor.removeMark(editor, format); | ||
} else { | ||
Editor.addMark(editor, format, true); | ||
} | ||
} | ||
|
||
export function isBlockActive(editor: BaseEditor & ReactEditor, format: string) { | ||
const { selection } = editor; | ||
if (!selection) return false; | ||
|
||
const [match] = Array.from( | ||
Editor.nodes(editor, { | ||
at: Editor.unhangRange(editor, selection), | ||
match: (n) => { | ||
if (Editor.isEditor(n)) return false; | ||
|
||
if ("text" in n) { | ||
return (n as any)[format]; | ||
} | ||
|
||
return n.type === format; | ||
}, | ||
}), | ||
); | ||
|
||
return !!match; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
import type { ReactEditor } from "slate-react"; | ||
import { Editor, Transforms, Range, Point, Element as SlateElement, type BaseEditor } from "slate"; | ||
|
||
const SHORTCUTS = { | ||
"*": "list-item", | ||
"-": "list-item", | ||
"+": "list-item", | ||
">": "block-quote", | ||
"#": "heading-one", | ||
"##": "heading-two", | ||
}; | ||
|
||
export const withShortcuts = (editor: BaseEditor & ReactEditor) => { | ||
const { deleteBackward, insertText } = editor; | ||
|
||
editor.insertText = (text) => { | ||
const { selection } = editor; | ||
|
||
if (text === " " && selection && Range.isCollapsed(selection)) { | ||
const { anchor } = selection; | ||
const block = Editor.above(editor, { | ||
match: (n) => Editor.isBlock(editor, n), | ||
}); | ||
const path = block ? block[1] : []; | ||
const start = Editor.start(editor, path); | ||
const range = { anchor, focus: start }; | ||
const beforeText = Editor.string(editor, range); | ||
const type = SHORTCUTS[beforeText as keyof typeof SHORTCUTS]; | ||
|
||
if (type) { | ||
Transforms.select(editor, range); | ||
Transforms.delete(editor); | ||
const newProperties = { | ||
type, | ||
}; | ||
|
||
Transforms.setNodes<SlateElement>(editor, newProperties as any, { | ||
match: (n) => Editor.isBlock(editor, n), | ||
}); | ||
|
||
if (type === "list-item") { | ||
const list = { | ||
type: "bulleted-list", | ||
children: [], | ||
}; | ||
|
||
Transforms.wrapNodes(editor, list as any, { | ||
match: (n) => | ||
!Editor.isEditor(n) && SlateElement.isElement(n) && (n as any).type === "list-item", | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
insertText(text); | ||
}; | ||
|
||
editor.deleteBackward = (...args) => { | ||
const { selection } = editor; | ||
|
||
if (selection && Range.isCollapsed(selection)) { | ||
const match = Editor.above(editor, { | ||
match: (n) => Editor.isBlock(editor, n), | ||
}); | ||
|
||
if (match) { | ||
const [block, path] = match; | ||
const start = Editor.start(editor, path); | ||
|
||
if ( | ||
!Editor.isEditor(block) && | ||
SlateElement.isElement(block) && | ||
block.type !== "paragraph" && | ||
Point.equals(selection.anchor, start) | ||
) { | ||
const newProperties: Partial<SlateElement> = { | ||
type: "paragraph", | ||
}; | ||
Transforms.setNodes(editor, newProperties); | ||
|
||
if (block.type === "list-item") { | ||
Transforms.unwrapNodes(editor, { | ||
match: (n) => | ||
!Editor.isEditor(n) && | ||
SlateElement.isElement(n) && | ||
(n as any).type === "bulleted-list", | ||
split: true, | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
} | ||
|
||
deleteBackward(...args); | ||
} | ||
}; | ||
|
||
return editor; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters