Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/lexical-code/LexicalCode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Copy Markdown
Collaborator

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 Infinity as default value

Copy link
Copy Markdown
Author

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

): () => void;
declare function registerCodeIndent(editor: LexicalEditor): () => void;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's remove registerCodeIndent from d.ts file for now, it is part of registerCodeHighlighting for now. I think we should eventually split them and call separately, but it'll be a breaking change so it's not urgent to do now


type SerializedCodeNode = Spread<
{
Expand Down
141 changes: 141 additions & 0 deletions packages/lexical-code/src/CodeHighlightNode.ts
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;
}
Loading