Skip to content
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

[lexical]: Breaking: add ability to use type discrimination for lexical node types #6543

Closed
wants to merge 3 commits into from
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
1 change: 1 addition & 0 deletions examples/vanilla-js-plugin/src/emoji-plugin/EmojiNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {TextNode} from 'lexical';
export type SerializedEmojiNode = Spread<
{
unifiedID: string;
type: 'emoji';
},
SerializedTextNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-code/flow/LexicalCode.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ declare export function normalizeCodeLang(lang: string): string;
export type SerializedCodeNode = {
...SerializedElementNode,
language: string | null | void,
type: 'code',
...
};

Expand Down
1 change: 1 addition & 0 deletions packages/lexical-code/src/CodeHighlightNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const DEFAULT_CODE_LANGUAGE = 'javascript';
type SerializedCodeHighlightNode = Spread<
{
highlightType: string | null | undefined;
type: 'code-highlight';
},
SerializedTextNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-code/src/CodeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
export type SerializedCodeNode = Spread<
{
language: string | null | undefined;
type: 'code';
},
SerializedElementNode
>;
Expand Down
3 changes: 2 additions & 1 deletion packages/lexical-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type SerializedLinkNode = Spread<
{
url: string;
},
Spread<LinkAttributes, SerializedElementNode>
Spread<LinkAttributes & {type: 'link'}, SerializedElementNode>
>;

type LinkHTMLElementType = HTMLAnchorElement | HTMLSpanElement;
Expand Down Expand Up @@ -330,6 +330,7 @@ export function $isLinkNode(
export type SerializedAutoLinkNode = Spread<
{
isUnlinked: boolean;
type: 'autolink';
},
SerializedLinkNode
>;
Expand Down
6 changes: 3 additions & 3 deletions packages/lexical-list/flow/LexicalList.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ParagraphNode,
RangeSelection,
LexicalCommand,
SerializedElementNode,
SerializedElementNode, Spread,
} from 'lexical';
import {ElementNode} from 'lexical';

Expand Down Expand Up @@ -79,12 +79,12 @@ declare export var INSERT_ORDERED_LIST_COMMAND: LexicalCommand<void>;
declare export var INSERT_CHECK_LIST_COMMAND: LexicalCommand<void>;
declare export var REMOVE_LIST_COMMAND: LexicalCommand<void>;

export type SerializedListItemNode = SerializedElementNode & {
export type SerializedListItemNode = Spread<{type: 'listitem'}, SerializedElementNode> & {
checked: boolean | void,
value: number,
};

export type SerializedListNode = SerializedElementNode & {
export type SerializedListNode = Spread<{type: 'list'}, SerializedElementNode> & {
listType: ListType,
start: number,
tag: ListNodeTagType,
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-list/src/LexicalListItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type SerializedListItemNode = Spread<
{
checked: boolean | undefined;
value: number;
type: 'listitem';
},
SerializedElementNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-list/src/LexicalListNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type SerializedListNode = Spread<
listType: ListType;
start: number;
tag: ListNodeTagType;
type: 'list';
},
SerializedElementNode
>;
Expand Down
12 changes: 8 additions & 4 deletions packages/lexical-mark/flow/LexicalMark.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import type {
NodeKey,
RangeSelection,
TextNode,
SerializedElementNode,
SerializedElementNode, Spread,
} from 'lexical';
import {ElementNode, LexicalNode} from 'lexical';

export type SerializedMarkNode = SerializedElementNode & {
ids: Array<string>,
};
export type SerializedMarkNode = Spread<
{
ids: Array<string>;
type: 'mark';
},
SerializedElementNode
>;

declare export class MarkNode extends ElementNode {
__ids: Array<string>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-mark/src/MarkNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {$applyNodeReplacement, $isRangeSelection, ElementNode} from 'lexical';
export type SerializedMarkNode = Spread<
{
ids: Array<string>;
type: 'mark';
},
SerializedElementNode
>;
Expand Down
5 changes: 3 additions & 2 deletions packages/lexical-overflow/flow/LexicalOverflow.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
LexicalNode,
NodeKey,
RangeSelection,
SerializedElementNode,
SerializedElementNode, Spread,
} from 'lexical';
import {ElementNode} from 'lexical';
declare export class OverflowNode extends ElementNode {
Expand All @@ -29,4 +29,5 @@ declare export function $isOverflowNode(
node: ?LexicalNode,
): node is OverflowNode;

export type SerializedOverflowNode = SerializedElementNode;
export type SerializedOverflowNode = Spread<{type: 'overflow'}, SerializedElementNode>;

8 changes: 7 additions & 1 deletion packages/lexical-overflow/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import type {
NodeKey,
RangeSelection,
SerializedElementNode,
Spread,
} from 'lexical';

import {$applyNodeReplacement, ElementNode} from 'lexical';

export type SerializedOverflowNode = SerializedElementNode;
export type SerializedOverflowNode = Spread<
{
type: 'overflow';
},
SerializedElementNode
>;

/** @noInheritDoc */
export class OverflowNode extends ElementNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare global {
export type SerializedAutocompleteNode = Spread<
{
uuid: string;
type: 'autocomplete';
},
SerializedLexicalNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/EmojiNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {$applyNodeReplacement, TextNode} from 'lexical';
export type SerializedEmojiNode = Spread<
{
className: string;
type: 'emoji';
},
SerializedTextNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/EquationNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type SerializedEquationNode = Spread<
{
equation: string;
inline: boolean;
type: 'equation';
},
SerializedLexicalNode
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type SerializedExcalidrawNode = Spread<
data: string;
width: Dimension;
height: Dimension;
type: 'excalidraw';
},
SerializedLexicalNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/FigmaNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function FigmaComponent({
export type SerializedFigmaNode = Spread<
{
documentID: string;
type: 'figma';
},
SerializedDecoratorBlockNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/ImageNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type SerializedImageNode = Spread<
showCaption: boolean;
src: string;
width?: number;
type: 'image';
},
SerializedLexicalNode
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type SerializedInlineImageNode = Spread<
src: string;
width?: number;
position?: Position;
type: 'inline-image';
},
SerializedLexicalNode
>;
Expand Down
14 changes: 12 additions & 2 deletions packages/lexical-playground/src/nodes/KeywordNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
*
*/

import type {EditorConfig, LexicalNode, SerializedTextNode} from 'lexical';
import type {
EditorConfig,
LexicalNode,
SerializedTextNode,
Spread,
} from 'lexical';

import {TextNode} from 'lexical';

export type SerializedKeywordNode = SerializedTextNode;
export type SerializedKeywordNode = Spread<
{
type: 'keyword';
},
SerializedTextNode
>;

export class KeywordNode extends TextNode {
static getType(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {ElementNode} from 'lexical';
export type SerializedLayoutContainerNode = Spread<
{
templateColumns: string;
type: 'layout-container';
},
SerializedElementNode
>;
Expand Down
8 changes: 7 additions & 1 deletion packages/lexical-playground/src/nodes/LayoutItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import type {
EditorConfig,
LexicalNode,
SerializedElementNode,
Spread,
} from 'lexical';

import {addClassNamesToElement} from '@lexical/utils';
import {ElementNode} from 'lexical';

export type SerializedLayoutItemNode = SerializedElementNode;
export type SerializedLayoutItemNode = Spread<
{
type: 'layout-item';
},
SerializedElementNode
>;

export class LayoutItemNode extends ElementNode {
static getType(): string {
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/MentionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
export type SerializedMentionNode = Spread<
{
mentionName: string;
type: 'mention';
},
SerializedTextNode
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ import {
LexicalNode,
NodeKey,
SerializedLexicalNode,
Spread,
} from 'lexical';
import * as React from 'react';
import {useCallback, useEffect} from 'react';

export type SerializedPageBreakNode = SerializedLexicalNode;
export type SerializedPageBreakNode = Spread<
{
type: 'page-break';
},
SerializedLexicalNode
>;

function PageBreakComponent({nodeKey}: {nodeKey: NodeKey}) {
const [editor] = useLexicalComposerContext();
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/PollNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type SerializedPollNode = Spread<
{
question: string;
options: Options;
type: 'poll';
},
SerializedLexicalNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/StickyNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type SerializedStickyNode = Spread<
yOffset: number;
color: StickyNoteColor;
caption: SerializedEditor;
type: 'sticky';
},
SerializedLexicalNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/TweetNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function TweetComponent({
export type SerializedTweetNode = Spread<
{
id: string;
type: 'tweet';
},
SerializedDecoratorBlockNode
>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-playground/src/nodes/YouTubeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function YouTubeComponent({
export type SerializedYouTubeNode = Spread<
{
videoID: string;
type: 'youtube';
},
SerializedDecoratorBlockNode
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {setDomHiddenUntilFound} from './CollapsibleUtils';
type SerializedCollapsibleContainerNode = Spread<
{
open: boolean;
type: 'collapsible-container';
},
SerializedElementNode
>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ import {
LexicalEditor,
LexicalNode,
SerializedElementNode,
Spread,
} from 'lexical';
import {IS_CHROME} from 'shared/environment';
import invariant from 'shared/invariant';

import {$isCollapsibleContainerNode} from './CollapsibleContainerNode';
import {domOnBeforeMatch, setDomHiddenUntilFound} from './CollapsibleUtils';

type SerializedCollapsibleContentNode = SerializedElementNode;
type SerializedCollapsibleContentNode = Spread<
{
type: 'collapsible-content';
},
SerializedElementNode
>;

export function $convertCollapsibleContentElement(
domNode: HTMLElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ import {
LexicalNode,
RangeSelection,
SerializedElementNode,
Spread,
} from 'lexical';
import {IS_CHROME} from 'shared/environment';
import invariant from 'shared/invariant';

import {$isCollapsibleContainerNode} from './CollapsibleContainerNode';
import {$isCollapsibleContentNode} from './CollapsibleContentNode';

type SerializedCollapsibleTitleNode = SerializedElementNode;
type SerializedCollapsibleTitleNode = Spread<
{
type: 'collapsible-title';
},
SerializedElementNode
>;

export function $convertSummaryElement(
domNode: HTMLElement,
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-react/src/LexicalDecoratorBlockNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {DecoratorNode} from 'lexical';
export type SerializedDecoratorBlockNode = Spread<
{
format: ElementFormatType;
type: 'decorator-block';
},
SerializedLexicalNode
>;
Expand Down
Loading
Loading