Skip to content

Commit

Permalink
feat(Mentions): add EditablePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Carrotzpc committed Jan 24, 2025
1 parent 61e3750 commit 0e71b47
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
34 changes: 21 additions & 13 deletions src/Mentions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import React, { useMemo } from 'react';

import { isBrowser } from '@/utils/tools';

import { OnBlurBlockPlugin } from './plugins/OnBlurBlockPlugin';
import { CustomTextNode } from './plugins/custom-text/node';
import { EditablePlugin } from './plugins/editable';
import {
MentionNode,
MentionNodePlugin,
MentionNodePluginReplacement,
} from './plugins/mention-node';
import { MentionPickerPlugin, type MentionPickerPluginProps } from './plugins/mention-picker';
import OnBlurBlock from './plugins/on-blur-or-focus-block';
import { MentionsConfigProvider } from './provider';
import { useStyles } from './style';
import type { AutoSize, MentionOption, MentionsOptionsMap } from './types';
Expand Down Expand Up @@ -67,8 +68,12 @@ export const Mentions: React.FC<MentionsProps> = ({
}) => {
const { componentDisabled } = ConfigProvider.useConfig();
const { styles, cx } = useStyles({ autoSize });
const disabled = customDisabled ?? componentDisabled;
const editable = !readOnly && !disabled;

const disabled = useMemo(
() => customDisabled ?? componentDisabled,
[componentDisabled, customDisabled]
);
const editable = useMemo(() => !readOnly && !disabled, [disabled, readOnly]);
const initialConfig = useMemo(
() => ({
namespace: 'mentions',
Expand Down Expand Up @@ -166,20 +171,23 @@ export const Mentions: React.FC<MentionsProps> = ({
</div>
}
/>
<MentionPickerPlugin
allowSpaces={allowSpaces}
onSelect={onSelect}
options={options}
overlayClassName={classNames?.menuOverlay}
preTriggerChars={preTriggerChars}
punctuation={punctuation}
triggers={triggers}
/>
{editable && (
<MentionPickerPlugin
allowSpaces={allowSpaces}
onSelect={onSelect}
options={options}
overlayClassName={classNames?.menuOverlay}
preTriggerChars={preTriggerChars}
punctuation={punctuation}
triggers={triggers}
/>
)}
<MentionNodePlugin />
<MentionNodePluginReplacement />
<HistoryPlugin />
<OnChangePlugin onChange={handleEditorChange} />
<OnBlurBlock onBlur={onBlur} onFocus={onFocus} />
<OnBlurBlockPlugin onBlur={onBlur} onFocus={onFocus} />
<EditablePlugin editable={editable} />
</div>
</MentionsConfigProvider>
</LexicalComposer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { useEffect, useRef } from 'react';

import { CLEAR_HIDE_MENU_TIMEOUT } from './mention-node';

type OnBlurBlockProps = {
type OnBlurBlockPluginProps = {
onBlur?: () => void;
onFocus?: () => void;
};
const OnBlurBlock: FC<OnBlurBlockProps> = ({ onBlur, onFocus }) => {
export const OnBlurBlockPlugin: FC<OnBlurBlockPluginProps> = ({ onBlur, onFocus }) => {
const [editor] = useLexicalComposerContext();

const ref = useRef<any>(null);
Expand Down Expand Up @@ -57,5 +57,3 @@ const OnBlurBlock: FC<OnBlurBlockProps> = ({ onBlur, onFocus }) => {

return null;
};

export default OnBlurBlock;
11 changes: 11 additions & 0 deletions src/Mentions/plugins/editable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import React, { useEffect } from 'react';

export const EditablePlugin: React.FC<{ editable: boolean }> = ({ editable }) => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
editor.setEditable(editable);
}, [editable, editor]);

return null;
};

0 comments on commit 0e71b47

Please sign in to comment.