Skip to content

Commit

Permalink
feat(richtext): use own querynode
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy committed Apr 4, 2024
1 parent ab5132e commit 181d644
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import ListElement from './components/Element/ListElement';
import { markdownToSlate, slateToMarkdown } from '../serializers';
import LinkElement from './components/Element/LinkElement';
import BlockquoteElement from './components/Element/BlockquoteElement';
import createBlockquoteExtPlugin from './plugins/createBlockquoteExitBreak';
import createBlockquoteExtPlugin from './plugins/createBlockquoteExtPlugin';

function visualEditorStyles({ minimal }) {
return `
Expand All @@ -64,7 +64,6 @@ const emptyValue = [
];

export default function VisualEditor({ t, field, className, isDisabled, onChange, ...props }) {
console.log('plff', createBlockquotePlugin())
const plugins = createPlugins(
[
createParagraphPlugin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isAncestorEmpty,
unwrapNodes,
isFirstChild,
isSelectionAtBlockStart,
} from '@udecode/plate-common';
import { ELEMENT_BLOCKQUOTE } from '@udecode/plate-block-quote';

Expand All @@ -15,6 +16,14 @@ function isWithinBlockquote(editor, entry) {
return blockAbove?.[0]?.type === ELEMENT_BLOCKQUOTE;
}

function queryNode(editor, entry, { empty, first, start }) {
return (
(!empty || isAncestorEmpty(editor, entry[0])) &&
(!first || isFirstChild(entry[1])) &&
(!start || isSelectionAtBlockStart(editor))
);
}

function unwrap(editor) {
unwrapNodes(editor, { split: true, match: n => n.type === ELEMENT_BLOCKQUOTE });
return true;
Expand All @@ -27,12 +36,11 @@ function onKeyDownBlockquoteExitBreak(editor, { options: { rules } }) {
const entry = getBlockAbove(editor);
if (!entry) return;

rules.forEach(({ hotkey, isFirstParagraph }) => {
rules.forEach(({ hotkey, query }) => {
if (
isHotkey(hotkey, event) &&
isAncestorEmpty(editor, entry[0]) &&
isWithinBlockquote(editor, entry) &&
(!isFirstParagraph || isFirstChild(entry[1])) &&
queryNode(editor, entry, query) &&
unwrap(editor)
) {
event.preventDefault();
Expand All @@ -48,7 +56,10 @@ const createBlockquoteExtPlugin = createPluginFactory({
onKeyDown: onKeyDownBlockquoteExitBreak,
},
options: {
rules: [{ hotkey: 'enter' }, { hotkey: 'backspace', isFirstParagraph: true }],
rules: [
{ hotkey: 'enter', query: { empty: true } },
{ hotkey: 'backspace', query: { first: true, start: true } },
],
},
});

Expand Down

0 comments on commit 181d644

Please sign in to comment.