-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(richtext-lexical): prevent use of text formats whose features wer…
…e not enabled (#9507) Fixes #8811 Before this PR, even if you did not include text formatting features (such as BoldFeature, ItalicFeature, etc), it was possible to apply that formatting by (a) pasting content from the clipboard and (b) using keyboard shortcuts. This PR fixes that by requiring the formatting features to be registered so that they can be inserted in the editor.
- Loading branch information
1 parent
5d18a52
commit 90f893a
Showing
11 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/richtext-lexical/src/lexical/plugins/TextPlugin/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use client' | ||
import type { TextFormatType } from 'lexical' | ||
|
||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | ||
import { TEXT_TYPE_TO_FORMAT, TextNode } from 'lexical' | ||
import { type SanitizedClientFeatures } from 'packages/richtext-lexical/src/features/typesClient.js' | ||
import { useEffect } from 'react' | ||
|
||
export function TextPlugin({ features }: { features: SanitizedClientFeatures }) { | ||
const [editor] = useLexicalComposerContext() | ||
|
||
useEffect(() => { | ||
const disabledFormats = getDisabledFormats(features.enabledFormats as TextFormatType[]) | ||
if (disabledFormats.length === 0) { | ||
return | ||
} | ||
// Ideally override the TextNode with our own TextNode (changing its setFormat or toggleFormat methods), | ||
// would be more performant. If we find a noticeable perf regression we can switch to that option. | ||
// Overriding the FORMAT_TEXT_COMMAND and PASTE_COMMAND commands is not an option I considered because | ||
// there might be other forms of mutation that we might not be considering. For example: | ||
// browser extensions or Payload/Lexical plugins that have their own commands. | ||
return editor.registerNodeTransform(TextNode, (textNode) => { | ||
disabledFormats.forEach((disabledFormat) => { | ||
if (textNode.hasFormat(disabledFormat)) { | ||
textNode.toggleFormat(disabledFormat) | ||
} | ||
}) | ||
}) | ||
}, [editor, features]) | ||
|
||
return null | ||
} | ||
|
||
function getDisabledFormats(enabledFormats: TextFormatType[]): TextFormatType[] { | ||
// not sure why Lexical added highlight as TextNode format. | ||
// see https://github.com/facebook/lexical/pull/3583 | ||
// We are going to implement it in other way to support multiple colors | ||
delete TEXT_TYPE_TO_FORMAT.highlight | ||
const allFormats = Object.keys(TEXT_TYPE_TO_FORMAT) as TextFormatType[] | ||
const enabledSet = new Set(enabledFormats) | ||
|
||
return allFormats.filter((format) => !enabledSet.has(format)) | ||
} |