Skip to content

Commit

Permalink
[lexical-website][lexical-react] Documentation Update: documentation …
Browse files Browse the repository at this point in the history
…for LexicalTreeView plugin (#6898)
  • Loading branch information
Kingscliq authored Dec 3, 2024
1 parent 71454c3 commit a9dfc93
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/lexical-react/src/LexicalTreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ import {mergeRegister} from '@lexical/utils';
import * as React from 'react';
import {useEffect, useState} from 'react';

/**
* TreeView is a React component that provides a visual representation of
* the Lexical editor's state and enables debugging features like time travel
* and custom tree node rendering.
*
* @param {Object} props - The properties passed to the TreeView component.
* @param {LexicalEditor} props.editor - The Lexical editor instance to be visualized and debugged.
* @param {string} [props.treeTypeButtonClassName] - Custom class name for the tree type toggle button.
* @param {string} [props.timeTravelButtonClassName] - Custom class name for the time travel toggle button.
* @param {string} [props.timeTravelPanelButtonClassName] - Custom class name for buttons inside the time travel panel.
* @param {string} [props.timeTravelPanelClassName] - Custom class name for the overall time travel panel container.
* @param {string} [props.timeTravelPanelSliderClassName] - Custom class name for the time travel slider in the panel.
* @param {string} [props.viewClassName] - Custom class name for the tree view container.
* @param {CustomPrintNodeFn} [props.customPrintNode] - A function for customizing the display of nodes in the tree.
*
* @returns {JSX.Element} - A React element that visualizes the editor's state and supports debugging interactions.
*/

export function TreeView({
treeTypeButtonClassName,
timeTravelButtonClassName,
timeTravelPanelSliderClassName,
timeTravelPanelButtonClassName,
viewClassName,
timeTravelPanelClassName,
viewClassName,
editor,
customPrintNode,
}: {
Expand All @@ -38,13 +56,15 @@ export function TreeView({
customPrintNode?: CustomPrintNodeFn;
}): JSX.Element {
const treeElementRef = React.createRef<HTMLPreElement>();

const [editorCurrentState, setEditorCurrentState] = useState<EditorState>(
editor.getEditorState(),
);

const commandsLog = useLexicalCommandsLog(editor);

useEffect(() => {
// Registers listeners to update the tree view when the editor state changes
return mergeRegister(
editor.registerUpdateListener(({editorState}) => {
setEditorCurrentState(editorState);
Expand All @@ -59,16 +79,23 @@ export function TreeView({
const element = treeElementRef.current;

if (element !== null) {
// @ts-ignore Internal field
// Assigns the editor instance to the tree view DOM element for internal tracking
// @ts-ignore Internal field used by Lexical
element.__lexicalEditor = editor;

return () => {
// @ts-ignore Internal field
// Cleans up the reference when the component is unmounted
// @ts-ignore Internal field used by Lexical
element.__lexicalEditor = null;
};
}
}, [editor, treeElementRef]);

/**
* Handles toggling the readonly state of the editor.
*
* @param {boolean} isReadonly - Whether the editor should be set to readonly.
*/
const handleEditorReadOnly = (isReadonly: boolean) => {
const rootElement = editor.getRootElement();
if (rootElement == null) {
Expand All @@ -90,6 +117,7 @@ export function TreeView({
editorState={editorCurrentState}
setEditorState={(state) => editor.setEditorState(state)}
generateContent={async function (exportDOM) {
// Generates the content for the tree view, allowing customization with exportDOM and customPrintNode
return generateContent(editor, commandsLog, exportDOM, customPrintNode);
}}
ref={treeElementRef}
Expand Down

0 comments on commit a9dfc93

Please sign in to comment.