Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client'
import type { EditorConfig, LexicalEditor, LexicalNode } from 'lexical'

import ObjectID from 'bson-objectid'
import {
$applyNodeReplacement,
type EditorConfig,
type LexicalEditor,
type LexicalNode,
} from 'lexical'
import React, { type JSX } from 'react'

import type { BlockFieldsOptionalID, SerializedBlockNode } from '../../server/nodes/BlocksNode.js'
Expand Down Expand Up @@ -50,12 +54,14 @@ export class BlockNode extends ServerBlockNode {
}

export function $createBlockNode(fields: BlockFieldsOptionalID): BlockNode {
return new BlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
})
return $applyNodeReplacement(
new BlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
}),
)
}

export function $isBlockNode(node: BlockNode | LexicalNode | null | undefined): node is BlockNode {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client'
import type { EditorConfig, LexicalEditor, LexicalNode } from 'lexical'

import ObjectID from 'bson-objectid'
import {
$applyNodeReplacement,
type EditorConfig,
type LexicalEditor,
type LexicalNode,
} from 'lexical'
import React, { type JSX } from 'react'

import type {
Expand Down Expand Up @@ -47,12 +51,14 @@ export class InlineBlockNode extends ServerInlineBlockNode {
}

export function $createInlineBlockNode(fields: Exclude<InlineBlockFields, 'id'>): InlineBlockNode {
return new InlineBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
})
return $applyNodeReplacement(
new InlineBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
}),
)
}

export function $isInlineBlockNode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { SerializedDecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js'
import type {
DOMConversionMap,
DOMExportOutput,
EditorConfig,
ElementFormatType,
LexicalEditor,
LexicalNode,
NodeKey,
} from 'lexical'
import type { JsonObject } from 'payload'
import type { JSX } from 'react'

import { DecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js'
import ObjectID from 'bson-objectid'
import {
$applyNodeReplacement,
type DOMConversionMap,
type DOMExportOutput,
type EditorConfig,
type ElementFormatType,
type LexicalEditor,
type LexicalNode,
type NodeKey,
} from 'lexical'

import type { StronglyTypedLeafNode } from '../../../../nodeTypes.js'

Expand Down Expand Up @@ -134,12 +135,14 @@ export class ServerBlockNode extends DecoratorBlockNode {
}

export function $createServerBlockNode(fields: BlockFieldsOptionalID): ServerBlockNode {
return new ServerBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
})
return $applyNodeReplacement(
new ServerBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
}),
)
}

export function $isServerBlockNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type React from 'react'
import type { JSX } from 'react'

import ObjectID from 'bson-objectid'
import { DecoratorNode } from 'lexical'
import { $applyNodeReplacement, DecoratorNode } from 'lexical'

import type { StronglyTypedLeafNode } from '../../../../nodeTypes.js'

Expand Down Expand Up @@ -131,12 +131,14 @@ export class ServerInlineBlockNode extends DecoratorNode<null | React.ReactEleme
export function $createServerInlineBlockNode(
fields: Exclude<InlineBlockFields, 'id'>,
): ServerInlineBlockNode {
return new ServerInlineBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
})
return $applyNodeReplacement(
new ServerInlineBlockNode({
fields: {
...fields,
id: fields?.id || new ObjectID.default().toHexString(),
},
}),
)
}

export function $isServerInlineBlockNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { LinkNode } from './LinkNode.js'

export class AutoLinkNode extends LinkNode {
static override clone(node: AutoLinkNode): AutoLinkNode {
return new AutoLinkNode({ id: '', fields: node.__fields, key: node.__key })
return new this({ id: '', fields: node.__fields, key: node.__key })
}

static override getType(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class LinkNode extends ElementNode {
}

static override clone(node: LinkNode): LinkNode {
return new LinkNode({
return new this({
id: node.__id,
fields: node.__fields,
key: node.__key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EditorConfig, LexicalNode, NodeKey, SerializedLexicalNode, Spread
import type { JSX } from 'react'

import { addClassNamesToElement } from '@lexical/utils'
import { DecoratorNode } from 'lexical'
import { $applyNodeReplacement, DecoratorNode } from 'lexical'
import * as React from 'react'

export type UnknownConvertedNodeData = {
Expand Down Expand Up @@ -33,7 +33,7 @@ export class UnknownConvertedNode extends DecoratorNode<JSX.Element> {
}

static override clone(node: UnknownConvertedNode): UnknownConvertedNode {
return new UnknownConvertedNode({
return new this({
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think these changes in clone are necessary. It's already handled by $applyNodeReplacement. The Lexical codebase doesn't use this pattern.

Copy link
Member Author

Choose a reason for hiding this comment

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

I mainly did this for consistency within our codebase. We use this pattern ( ;) ) for our ServerBlockNode and BlockNode, so that we don't have to override clone in BlockNode when we extend from ServerBlockNode.

We don't use this ServerUnknownConvertedNode here, but consistency I just used this all across our codebase

data: node.__data,
key: node.__key,
})
Expand Down Expand Up @@ -90,9 +90,11 @@ export function $createUnknownConvertedNode({
}: {
data: UnknownConvertedNodeData
}): UnknownConvertedNode {
return new UnknownConvertedNode({
data,
})
return $applyNodeReplacement(
new UnknownConvertedNode({
data,
}),
)
}

export function $isUnknownConvertedNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { EditorConfig, LexicalNode, NodeKey, SerializedLexicalNode, Spread
import type { JSX } from 'react'

import { addClassNamesToElement } from '@lexical/utils'
import { DecoratorNode } from 'lexical'
import { $applyNodeReplacement, DecoratorNode } from 'lexical'
import * as React from 'react'

export type UnknownConvertedNodeData = {
Expand Down Expand Up @@ -33,7 +33,7 @@ export class UnknownConvertedNode extends DecoratorNode<JSX.Element> {
}

static override clone(node: UnknownConvertedNode): UnknownConvertedNode {
return new UnknownConvertedNode({
return new this({
data: node.__data,
key: node.__key,
})
Expand Down Expand Up @@ -90,9 +90,11 @@ export function $createUnknownConvertedNode({
}: {
data: UnknownConvertedNodeData
}): UnknownConvertedNode {
return new UnknownConvertedNode({
data,
})
return $applyNodeReplacement(
new UnknownConvertedNode({
data,
}),
)
}

export function $isUnknownConvertedNode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use client'
import type {
DOMConversionMap,
DOMConversionOutput,
EditorConfig,
LexicalEditor,
LexicalNode,
} from 'lexical'
import type { JSX } from 'react'

import {
$applyNodeReplacement,
type DOMConversionMap,
type DOMConversionOutput,
type EditorConfig,
type LexicalEditor,
type LexicalNode,
} from 'lexical'
import * as React from 'react'

import type {
Expand Down Expand Up @@ -94,9 +95,11 @@ export class RelationshipNode extends RelationshipServerNode {
}

export function $createRelationshipNode(data: RelationshipData): RelationshipNode {
return new RelationshipNode({
data,
})
return $applyNodeReplacement(
new RelationshipNode({
data,
}),
)
}

export function $isRelationshipNode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { SerializedDecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js'
import type {
DOMConversionMap,
DOMConversionOutput,
DOMExportOutput,
EditorConfig,
ElementFormatType,
LexicalEditor,
LexicalNode,
NodeKey,
} from 'lexical'
import type { CollectionSlug, DataFromCollectionSlug } from 'payload'
import type { JSX } from 'react'

import { DecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js'
import {
$applyNodeReplacement,
type DOMConversionMap,
type DOMConversionOutput,
type DOMExportOutput,
type EditorConfig,
type ElementFormatType,
type LexicalEditor,
type LexicalNode,
type NodeKey,
} from 'lexical'

import type { StronglyTypedLeafNode } from '../../../../nodeTypes.js'

Expand Down Expand Up @@ -144,9 +145,11 @@ export class RelationshipServerNode extends DecoratorBlockNode {
}

export function $createServerRelationshipNode(data: RelationshipData): RelationshipServerNode {
return new RelationshipServerNode({
data,
})
return $applyNodeReplacement(
new RelationshipServerNode({
data,
}),
)
}

export function $isServerRelationshipNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export const UploadPlugin: PluginComponent<UploadFeaturePropsClient> = ({ client

if ($isRangeSelection(selection)) {
for (const file of files) {
const pendingUploadNode = new UploadNode({
const pendingUploadNode = $createUploadNode({
data: {
pending: {
formID: file.formID,
Expand Down Expand Up @@ -363,7 +363,7 @@ export const UploadPlugin: PluginComponent<UploadFeaturePropsClient> = ({ client
$setSelection(selection)

for (const file of files) {
const pendingUploadNode = new UploadNode({
const pendingUploadNode = $createUploadNode({
data: {
pending: {
formID: file.formID,
Expand Down