From 83ac03eb007aca611d66b58fe047865d1d267853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 22 May 2023 18:15:43 +0200 Subject: [PATCH] Lexical: Conditionally include nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- app/soapbox/features/compose/editor/index.tsx | 3 +- app/soapbox/features/compose/editor/nodes.ts | 37 ------------- .../features/compose/editor/nodes/index.ts | 52 +++++++++++++++++++ 3 files changed, 54 insertions(+), 38 deletions(-) delete mode 100644 app/soapbox/features/compose/editor/nodes.ts create mode 100644 app/soapbox/features/compose/editor/nodes/index.ts diff --git a/app/soapbox/features/compose/editor/index.tsx b/app/soapbox/features/compose/editor/index.tsx index fb6b5d63a0..4dd273881f 100644 --- a/app/soapbox/features/compose/editor/index.tsx +++ b/app/soapbox/features/compose/editor/index.tsx @@ -32,7 +32,7 @@ const LINK_MATCHERS = [ ), ]; -import nodes from './nodes'; +import { useNodes } from './nodes'; import AutosuggestPlugin from './plugins/autosuggest-plugin'; import FloatingLinkEditorPlugin from './plugins/floating-link-editor-plugin'; import FloatingTextFormatToolbarPlugin from './plugins/floating-text-format-toolbar-plugin'; @@ -69,6 +69,7 @@ const ComposeEditor = React.forwardRef(({ }, editorStateRef) => { const dispatch = useAppDispatch(); const features = useFeatures(); + const nodes = useNodes(); const [suggestionsHidden, setSuggestionsHidden] = useState(true); diff --git a/app/soapbox/features/compose/editor/nodes.ts b/app/soapbox/features/compose/editor/nodes.ts deleted file mode 100644 index 57b31103c8..0000000000 --- a/app/soapbox/features/compose/editor/nodes.ts +++ /dev/null @@ -1,37 +0,0 @@ -/* -MIT License - -Copyright (c) Meta Platforms, Inc. and affiliates. - -This source code is licensed under the MIT license found in the -LICENSE file in the /app/soapbox/features/compose/editor directory. -*/ - -import { CodeHighlightNode, CodeNode } from '@lexical/code'; -import { HashtagNode } from '@lexical/hashtag'; -import { AutoLinkNode, LinkNode } from '@lexical/link'; -import { ListItemNode, ListNode } from '@lexical/list'; -import { HorizontalRuleNode } from '@lexical/react/LexicalHorizontalRuleNode'; -import { HeadingNode, QuoteNode } from '@lexical/rich-text'; - -import { EmojiNode } from './nodes/emoji-node'; -import { MentionNode } from './nodes/mention-node'; - -import type { Klass, LexicalNode } from 'lexical'; - -const ComposeNodes: Array> = [ - HeadingNode, - QuoteNode, - CodeNode, - CodeHighlightNode, - AutoLinkNode, - LinkNode, - ListItemNode, - ListNode, - HorizontalRuleNode, - HashtagNode, - EmojiNode, - MentionNode, -]; - -export default ComposeNodes; diff --git a/app/soapbox/features/compose/editor/nodes/index.ts b/app/soapbox/features/compose/editor/nodes/index.ts new file mode 100644 index 0000000000..04c206eb77 --- /dev/null +++ b/app/soapbox/features/compose/editor/nodes/index.ts @@ -0,0 +1,52 @@ +/* +MIT License + +Copyright (c) Meta Platforms, Inc. and affiliates. + +This source code is licensed under the MIT license found in the +LICENSE file in the /app/soapbox/features/compose/editor directory. +*/ + +import { CodeHighlightNode, CodeNode } from '@lexical/code'; +import { HashtagNode } from '@lexical/hashtag'; +import { AutoLinkNode, LinkNode } from '@lexical/link'; +import { ListItemNode, ListNode } from '@lexical/list'; +import { HorizontalRuleNode } from '@lexical/react/LexicalHorizontalRuleNode'; +import { HeadingNode, QuoteNode } from '@lexical/rich-text'; + +import { useFeatures, useInstance } from 'soapbox/hooks'; + +import { EmojiNode } from './emoji-node'; +import { MentionNode } from './mention-node'; + +import type { Klass, LexicalNode } from 'lexical'; + +const useNodes = () => { + const features = useFeatures(); + const instance = useInstance(); + + const nodes: Array> = [ + AutoLinkNode, + HashtagNode, + EmojiNode, + MentionNode, + ]; + + if (features.richText) { + nodes.push( + QuoteNode, + CodeNode, + CodeHighlightNode, + LinkNode, + ListItemNode, + ListNode, + HorizontalRuleNode, + ); + } + + if (instance.pleroma.getIn(['metadata', 'markup', 'allow_headings'])) nodes.push(HeadingNode); + + return nodes; +}; + +export { useNodes };