Skip to content

Getting previous node type #3502

Answered by zbeyens
ajmnz asked this question in Q&A
Discussion options

You must be logged in to vote

By Cursor:

import { findNodePath } from '@udecode/plate-common/react';

const Heading = withRef<typeof HeadingElementVariants>(
  ({ children, isFirstBlock: _, variant = "h1", ...props }, ref) => {
    const { element } = props;

    const isFirstBlock = useEditorSelector(
      (editor) => {
        const path = findNodePath(editor, element);
        
        if (!path) return false;

        if (path.length === 1 && path[0] === 0) {
          return true; // It's the first block
        }

        const previousNode = getPreviousNode(editor, { at: path });
        return previousNode?.[0].type === HorizontalRulePlugin.key;
      },
      [element]
    );

    return (
      // ...
    );

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ajmnz
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants