Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
72a67c6
rebase from origin main
lateefazeez Jul 13, 2022
b75c863
set threshold to 10000
Jun 17, 2022
0f11d0e
rebase from origin main
lateefazeez Jul 13, 2022
a0eba5f
update highlight threshold
lateefazeez Jun 28, 2022
7e8b6e0
rebase from origin main
lateefazeez Jul 13, 2022
8abe6ef
revert codeLineNode
lateefazeez Jul 13, 2022
b013fff
add comment with cmd+enter
lateefazeez Jul 13, 2022
a366f20
open comment box when a comment is added
lateefazeez Jul 13, 2022
3ffa5e5
implement comment and clipboard tasks
lateefazeez Jul 18, 2022
ec25055
Add TableOfContentsPlugin (#2634)
karam-qaoud Jul 18, 2022
841ad32
fix(lexical): calculate range selection formatting (#2643)
LuciNyan Jul 18, 2022
ac3fc12
fix(doc): RichTextPlugin placeholder (#2655)
unvalley Jul 19, 2022
e63b456
Autolink default protocol (#2654)
zurfyx Jul 19, 2022
19d3813
Specify the return type of getNearestNodeOfType. (#2651)
y-hiraoka Jul 19, 2022
84d5433
Selection.extract fix (#2620)
acywatson Jul 19, 2022
b9958ea
chore(lexical-playground): remove files that should not be submitted …
LuciNyan Jul 20, 2022
14df568
updated branch
lateefazeez Jul 20, 2022
feb517a
Remove default styling imports on HTML paste (#2663)
acywatson Jul 20, 2022
c42f222
updated branch
lateefazeez Jul 20, 2022
82f65ed
updated branch
lateefazeez Jul 20, 2022
74964d5
rebase from main
lateefazeez Jul 20, 2022
e473080
updated branch
lateefazeez Jul 20, 2022
53ed760
update to highlighter code split
lateefazeez Jun 10, 2022
de7af79
rebase from main
lateefazeez Jul 20, 2022
79f614d
rebase from main
lateefazeez Jul 20, 2022
7cb79cb
rebase from main
lateefazeez Jul 20, 2022
928b79b
rebase from main
lateefazeez Jul 20, 2022
e5cdb85
rebase from main
lateefazeez Jul 20, 2022
52d2cc1
rebase from main
lateefazeez Jul 20, 2022
667f29e
rebase from main
lateefazeez Jul 20, 2022
c298e09
rebase from main
lateefazeez Jul 20, 2022
4bbb752
rebase from main
lateefazeez Jul 20, 2022
1e55839
rebase from main
lateefazeez Jul 20, 2022
ac8be0d
implement comment and clipboard tasks
lateefazeez Jul 18, 2022
3374cbb
updated branch
lateefazeez Jul 20, 2022
e3477a2
Merge branch 'main' into playgroundCommentSubmit
lateefazeez Jul 20, 2022
baedf27
resolve merge conflicts
lateefazeez Jul 21, 2022
1adc79d
fix import conflicts
lateefazeez Jul 21, 2022
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.

114 changes: 114 additions & 0 deletions packages/lexical-code/LexicalCode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* 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.
*
*/

import type {
EditorConfig,
LexicalNode,
NodeKey,
ParagraphNode,
RangeSelection,
EditorThemeClasses,
LexicalEditor,
SerializedElementNode,
SerializedTextNode,
} from 'lexical';

import {ElementNode, TextNode} from 'lexical';
import {Spread} from 'libdefs/globals';

declare class CodeNode extends ElementNode {
static getType(): string;
static clone(node: CodeNode): CodeNode;
constructor(key?: NodeKey);
createDOM(config: EditorConfig): HTMLElement;
updateDOM(prevNode: CodeNode, dom: HTMLElement): boolean;
insertNewAfter(
selection: RangeSelection,
): null | ParagraphNode | CodeHighlightNode;
canInsertTab(): boolean;
collapseAtStart(): true;
setLanguage(language: string): void;
getLanguage(): string | void;
importJSON(serializedNode: SerializedCodeNode): CodeNode;
exportJSON(): SerializedElementNode;
}
declare function $createCodeNode(language?: string): CodeNode;
declare function $isCodeNode(
node: null | undefined | LexicalNode,
): node is CodeNode;

declare function getFirstCodeHighlightNodeOfLine(
anchor: LexicalNode,
): null | undefined | CodeHighlightNode;

declare function getLastCodeHighlightNodeOfLine(
anchor: LexicalNode,
): null | undefined | CodeHighlightNode;

declare function getDefaultCodeLanguage(): string;
declare function getCodeLanguages(): Array<string>;

declare class CodeHighlightNode extends TextNode {
__highlightType: null | undefined | string;
constructor(text: string, highlightType?: string, key?: NodeKey);
static getType(): string;
static clone(node: CodeHighlightNode): CodeHighlightNode;
createDOM(config: EditorConfig): HTMLElement;
updateDOM(
prevNode: CodeHighlightNode,
dom: HTMLElement,
config: EditorConfig,
): boolean;
setFormat(format: number): this;
}
declare function getHighlightThemeClass(
theme: EditorThemeClasses,
highlightType: null | undefined | string,
): null | undefined | string;
declare function $createCodeHighlightNode(
text: string,
highlightType?: string,
): CodeHighlightNode;
declare function $isCodeHighlightNode(
node: LexicalNode | null | undefined,
): node is CodeHighlightNode;

declare function registerCodeHighlighting(
editor: LexicalEditor,
threshold: number,
): () => void;
declare function registerCodeIndent(editor: LexicalEditor): () => void;

declare function getStartOfCodeInLine(
editor: LexicalEditor,
node: TextNode | null,
offset: number,
): () => void;
declare function getEndOfCodeInLine(
editor: LexicalEditor,
node: TextNode | null,
offset: number,
): () => void;

type SerializedCodeNode = Spread<
{
language: string | null | undefined;
type: 'code';
version: 1;
},
SerializedElementNode
>;

type SerializedCodeHighlightNode = Spread<
{
highlightType: string | null | undefined;
type: 'code-highlight';
version: 1;
},
SerializedTextNode
>;
149 changes: 149 additions & 0 deletions packages/lexical-code/src/CodeHighlightNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* 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 type {
EditorConfig,
EditorThemeClasses,
LexicalNode,
NodeKey,
SerializedTextNode,
Spread,
} from 'lexical';

import {
addClassNamesToElement,
removeClassNamesFromElement,
} from '@lexical/utils';
import {TextNode} from 'lexical';

type SerializedCodeHighlightNode = Spread<
{
highlightType: string | null | undefined;
type: 'code-highlight';
version: 1;
},
SerializedTextNode
>;
export class CodeHighlightNode extends TextNode {
__highlightType: string | null | undefined;

constructor(
text: string,
highlightType?: string | null | undefined,
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.text,
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',
version: 1,
};
}

// Prevent formatting (bold, underline, etc)
setFormat(format: number): this {
return this;
}
}

function getHighlightThemeClass(
theme: EditorThemeClasses,
highlightType: string | null | undefined,
): string | null | undefined {
return (
highlightType &&
theme &&
theme.codeHighlight &&
theme.codeHighlight[highlightType]
);
}

export function $createCodeHighlightNode(
text: string,
highlightType?: string | null | undefined,
): CodeHighlightNode {
return new CodeHighlightNode(text, highlightType);
}

export function $isCodeHighlightNode(
node: LexicalNode | CodeHighlightNode | null | undefined,
): node is CodeHighlightNode {
return node instanceof CodeHighlightNode;
}
Loading