-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix/code highlighting split #2382
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
Changes from all commits
3b55097
a1e9616
422b604
bc1163d
d4749b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,11 @@ declare function $isCodeHighlightNode( | |
| node: LexicalNode | null | undefined, | ||
| ): node is CodeHighlightNode; | ||
|
|
||
| declare function registerCodeHighlighting(editor: LexicalEditor): () => void; | ||
| declare function registerCodeHighlighting( | ||
| editor: LexicalEditor, | ||
| threshold: number, | ||
| ): () => void; | ||
| declare function registerCodeIndent(editor: LexicalEditor): () => void; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove |
||
|
|
||
| type SerializedCodeNode = Spread< | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * 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. | ||
| * | ||
| */ | ||
|
|
||
| // eslint-disable-next-line simple-import-sort/imports | ||
|
|
||
| import { | ||
| addClassNamesToElement, | ||
| removeClassNamesFromElement, | ||
| } from '@lexical/utils'; | ||
| import { | ||
| EditorConfig, | ||
| EditorThemeClasses, | ||
| LexicalNode, | ||
| NodeKey, | ||
| SerializedTextNode, | ||
| TextNode, | ||
| } from 'lexical'; | ||
| import {Spread} from 'libdefs/globals'; | ||
|
|
||
| type SerializedCodeHighlightNode = Spread< | ||
| { | ||
| highlightType: string | null | undefined; | ||
| type: 'code-highlight'; | ||
| version: 1; | ||
| }, | ||
| SerializedTextNode | ||
| >; | ||
|
|
||
| function getHighlightThemeClass( | ||
| theme: EditorThemeClasses, | ||
| highlightType: string | undefined, | ||
| ): string | undefined { | ||
| return ( | ||
| highlightType && | ||
| theme && | ||
| theme.codeHighlight && | ||
| theme.codeHighlight[highlightType] | ||
| ); | ||
| } | ||
| export class CodeHighlightNode extends TextNode { | ||
| __highlightType: string | null | undefined; | ||
|
|
||
| constructor(text: string, highlightType?: string, key?: NodeKey) { | ||
| super(text, key); | ||
| this.__highlightType = highlightType; | ||
| } | ||
|
|
||
| static getType(): string { | ||
| return 'code-highlight'; | ||
| } | ||
|
|
||
| static clone(node: CodeHighlightNode): CodeHighlightNode { | ||
| return new CodeHighlightNode( | ||
| node.__text, | ||
| node.__highlightType || undefined, | ||
| node.__key, | ||
| ); | ||
| } | ||
|
|
||
| getHighlightType(): string | null | undefined { | ||
| const self = this.getLatest(); | ||
| return self.__highlightType; | ||
| } | ||
|
|
||
| createDOM(config: EditorConfig): HTMLElement { | ||
| const element = super.createDOM(config); | ||
| const className = getHighlightThemeClass( | ||
| config.theme, | ||
| this.__highlightType, | ||
| ); | ||
| addClassNamesToElement(element, className); | ||
| return element; | ||
| } | ||
|
|
||
| updateDOM( | ||
| prevNode: CodeHighlightNode, | ||
| dom: HTMLElement, | ||
| config: EditorConfig, | ||
| ): boolean { | ||
| const update = super.updateDOM(prevNode, dom, config); | ||
| const prevClassName = getHighlightThemeClass( | ||
| config.theme, | ||
| prevNode.__highlightType, | ||
| ); | ||
| const nextClassName = getHighlightThemeClass( | ||
| config.theme, | ||
| this.__highlightType, | ||
| ); | ||
| if (prevClassName !== nextClassName) { | ||
| if (prevClassName) { | ||
| removeClassNamesFromElement(dom, prevClassName); | ||
| } | ||
| if (nextClassName) { | ||
| addClassNamesToElement(dom, nextClassName); | ||
| } | ||
| } | ||
| return update; | ||
| } | ||
|
|
||
| static importJSON( | ||
| serializedNode: SerializedCodeHighlightNode, | ||
| ): CodeHighlightNode { | ||
| const node = $createCodeHighlightNode(serializedNode.highlightType); | ||
| node.setFormat(serializedNode.format); | ||
| node.setDetail(serializedNode.detail); | ||
| node.setMode(serializedNode.mode); | ||
| node.setStyle(serializedNode.style); | ||
| return node; | ||
| } | ||
|
|
||
| exportJSON(): SerializedCodeHighlightNode { | ||
| return { | ||
| ...super.exportJSON(), | ||
| highlightType: this.getHighlightType(), | ||
| type: 'code-highlight', | ||
| }; | ||
| } | ||
|
|
||
| // Prevent formatting (bold, underline, etc) | ||
| setFormat(format: number): this { | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| export function $createCodeHighlightNode( | ||
| text: string, | ||
| highlightType?: string, | ||
| ): CodeHighlightNode { | ||
| return new CodeHighlightNode(text, highlightType); | ||
| } | ||
|
|
||
| export function $isCodeHighlightNode( | ||
| node: LexicalNode | CodeHighlightNode | null | undefined, | ||
| ): node is CodeHighlightNode { | ||
| return node instanceof CodeHighlightNode; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be optional argument, with
Infinityas default valueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that will be completed as part of the codehighlighter threshold branch