Skip to content

Commit

Permalink
Add function customGetAdjacentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
aquarius-wing committed May 13, 2024
1 parent 93cf85e commit 624476f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/lexical-react/src/LexicalComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Klass,
LexicalEditor,
LexicalNode,
LexicalNodeReplacement,
LexicalNodeReplacement, type PointType,
} from 'lexical';
import {useMemo} from 'react';
import * as React from 'react';
Expand All @@ -47,6 +47,10 @@ export type InitialConfigType = Readonly<{
theme?: EditorThemeClasses;
editorState?: InitialEditorStateType;
html?: HTMLConfig;
customGetAdjacentNode?: (
focus: PointType,
isBackward: boolean,
) => null | LexicalNode;
}>;

type Props = React.PropsWithChildren<{
Expand Down Expand Up @@ -75,6 +79,7 @@ export function LexicalComposer({initialConfig, children}: Props): JSX.Element {

if (editor === null) {
const newEditor = createEditor({
customGetAdjacentNode: initialConfig.customGetAdjacentNode,
editable: initialConfig.editable,
html,
namespace,
Expand Down
11 changes: 10 additions & 1 deletion packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {

import invariant from 'shared/invariant';

import {$getRoot, $getSelection, TextNode} from '.';
import {type PointType, $getRoot, $getSelection, TextNode} from '.';
import {FULL_RECONCILE, NO_DIRTY_NODES} from './LexicalConstants';
import {createEmptyEditorState} from './LexicalEditorState';
import {addRootElementEvents, removeRootElementEvents} from './LexicalEvents';
Expand Down Expand Up @@ -155,6 +155,10 @@ export type EditorConfig = {
disableEvents?: boolean;
namespace: string;
theme: EditorThemeClasses;
customGetAdjacentNode?: (
focus: PointType,
isBackward: boolean,
) => null | LexicalNode;
};

export type LexicalNodeReplacement = {
Expand Down Expand Up @@ -184,6 +188,10 @@ export type CreateEditorArgs = {
editable?: boolean;
theme?: EditorThemeClasses;
html?: HTMLConfig;
customGetAdjacentNode?: (
focus: PointType,
isBackward: boolean,
) => null | LexicalNode
};

export type RegisteredNodes = Map<string, RegisteredNode>;
Expand Down Expand Up @@ -509,6 +517,7 @@ export function createEditor(editorConfig?: CreateEditorArgs): LexicalEditor {
parentEditor,
registeredNodes,
{
customGetAdjacentNode: config.customGetAdjacentNode,
disableEvents,
namespace,
theme,
Expand Down
8 changes: 8 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,14 @@ export function $getAdjacentNode(
focus: PointType,
isBackward: boolean,
): null | LexicalNode {
const customGetAdjacentNode = getActiveEditor()._config.customGetAdjacentNode
if (customGetAdjacentNode) {
const result = customGetAdjacentNode(focus, isBackward)
if (result) {
return result
}
}

const focusOffset = focus.offset;
if (focus.type === 'element') {
const block = focus.getNode();
Expand Down

0 comments on commit 624476f

Please sign in to comment.