Skip to content

How to get sorted nodes in order of their appearance? #6263

Answered by 2wheeh
reactuz asked this question in Q&A
Discussion options

You must be logged in to vote

you can do dfs to get what you want.
there is $dfs exported from @lexical/utils.

import {$dfs} from '@lexical/utils';

const $getOrderedNodeKeys = () => $dfs().map(({node}) => node.getKey());

you should call $ functions inside of update, read or command listener. for example:

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {$dfs} from '@lexical/utils';
import {COMMAND_PRIORITY_EDITOR, createCommand} from 'lexical';
import {useEffect} from 'react';

const $getOrderedNodeKeys = () => $dfs().map(({node}) => node.getKey());

const PRINT_COMMAND = createCommand('PRINT_COMMAND');

export function MyPlugin() {
  const [editor] = useLexicalComposerContext();

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@reactuz
Comment options

Answer selected by reactuz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants