Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Documentation][lexical-website]: Documentation for useLexical node selection hook #6976

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions packages/lexical-react/src/useLexicalNodeSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,48 @@ import {
} from 'lexical';
import {useCallback, useEffect, useState} from 'react';

/**
* A helper function to determine if a specific node is selected in a Lexical editor.
*
* @param {LexicalEditor} editor - The LexicalEditor instance.
* @param {NodeKey} key - The key of the node to check.
* @returns {boolean} Whether the node is selected.
Comment on lines +24 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it's redundant to repeat the types in the comments, because typedoc will fill that in using the actual types. This function also isn't exported so there's not as much reason to spend the time on it, the purpose is fairly clear from the name and types alone.

*/

function isNodeSelected(editor: LexicalEditor, key: NodeKey): boolean {
return editor.getEditorState().read(() => {
const node = $getNodeByKey(key);

if (node === null) {
return false;
return false; // Node doesn't exist, so it's not selected.
}

return node.isSelected();
return node.isSelected(); // Check if the node is selected.
});
}

/**
* A custom hook to manage the selection state of a specific node in a Lexical editor.
*
* This hook provides utilities to:
* - Check if a node is selected.
* - Update its selection state.
* - Clear the selection.
*
* @param {NodeKey} key - The key of the node to track selection for.
* @returns {[boolean, (selected: boolean) => void, () => void]} A tuple containing:
* - `isSelected` (boolean): Whether the node is currently selected.
* - `setSelected` (function): A function to set the selection state of the node.
* - `clearSelected` (function): A function to clear the selection of the node.
*
*/

export function useLexicalNodeSelection(
key: NodeKey,
): [boolean, (arg0: boolean) => void, () => void] {
): [boolean, (selected: boolean) => void, () => void] {
const [editor] = useLexicalComposerContext();

// State to track whether the node is currently selected.
const [isSelected, setIsSelected] = useState(() =>
isNodeSelected(editor, key),
);
Expand All @@ -48,7 +73,7 @@ export function useLexicalNodeSelection(
});

return () => {
isMounted = false;
isMounted = false; // Prevent updates after component unmount.
unregister();
};
}, [editor, key]);
Expand All @@ -62,6 +87,7 @@ export function useLexicalNodeSelection(
selection = $createNodeSelection();
$setSelection(selection);
}

if ($isNodeSelection(selection)) {
if (selected) {
selection.add(key);
Expand Down
Loading