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

Add LexicalNodeEventPlugin to @lexical/react #3392

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.name_mapper='^@lexical/react/LexicalTablePlugin' -> '<PROJECT_ROOT>/packa
module.name_mapper='^@lexical/react/LexicalLinkPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalLinkPlugin.js.flow'
module.name_mapper='^@lexical/react/LexicalAutoLinkPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalAutoLinkPlugin.js.flow'
module.name_mapper='^@lexical/react/LexicalAutoEmbedPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalAutoEmbedPlugin.js.flow'
module.name_mapper='^@lexical/react/LexicalNodeEventPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalNodeEventPlugin.js.flow'

module.name_mapper='^@lexical/react/LexicalListPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalListPlugin.js.flow'
module.name_mapper='^@lexical/react/LexicalCheckListPlugin' -> '<PROJECT_ROOT>/packages/lexical-react/flow/LexicalCheckListPlugin.js.flow'
Expand Down
7 changes: 4 additions & 3 deletions packages/lexical-playground/src/ui/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ function PortalImpl({
onClose();
}
};
if (modalRef.current !== null) {
modalOverlayElement = modalRef.current?.parentElement;
const modelElement = modalRef.current;
if (modelElement !== null) {
modalOverlayElement = modelElement.parentElement;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just removed experimental syntax as part of this PR because it annoyed me :P

if (modalOverlayElement !== null) {
modalOverlayElement?.addEventListener('click', clickOutsideHandler);
modalOverlayElement.addEventListener('click', clickOutsideHandler);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const moduleResolution = [
'LexicalAutoLinkPlugin',
'LexicalAutoEmbedPlugin',
'LexicalOnChangePlugin',
'LexicalNodeEventPlugin',
].forEach((module) => {
let resolvedPath = path.resolve(`../lexical-react/src/${module}.ts`);

Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/vite.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const moduleResolution = [
'LexicalAutoLinkPlugin',
'LexicalAutoEmbedPlugin',
'LexicalOnChangePlugin',
'LexicalNodeEventPlugin',
].forEach((module) => {
let resolvedPath = path.resolve(`../lexical-react/dist/${module}.js`);
moduleResolution.push({
Expand Down
47 changes: 47 additions & 0 deletions packages/lexical-react/src/LexicalNodeEventPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {type Klass, type LexicalNode, type NodeKey} from 'lexical';
import {useRef} from 'react';
import useLayoutEffect from 'shared/useLayoutEffect';

export function NodeEventPlugin({
nodeType,
eventType,
eventListener,
}: {
nodeType: Klass<LexicalNode>;
eventType: string;
eventListener: (event: Event, nodeKey: NodeKey) => void;
}): null {
const [editor] = useLexicalComposerContext();
const listenerRef = useRef(eventListener);

listenerRef.current = eventListener;

useLayoutEffect(() => {
return editor.registerMutationListener(nodeType, (mutations) => {
editor.getEditorState().read(() => {
for (const [key, mutation] of mutations) {
const element: null | HTMLElement = editor.getElementByKey(key);

if (mutation === 'created' && element !== null) {
element.addEventListener(eventType, (event: Event) => {
listenerRef.current(event, key);
});
}
}
});
});
// wW intentionally don't respect changes to eventType.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [editor, nodeType]);

return null;
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
"@lexical/react/LexicalOnChangePlugin": [
"./packages/lexical-react/src/LexicalOnChangePlugin.ts"
],
"@lexical/react/LexicalNodeEventPlugin": [
"./packages/lexical-react/src/LexicalNodeEventPlugin.ts"
],
"@lexical/react/LexicalHashtagPlugin": [
"./packages/lexical-react/src/LexicalHashtagPlugin.ts"
],
Expand Down