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][lexical-list] Bug Fix: Style lost on list insertion from empty paragraph #6925

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions packages/lexical-list/src/formatList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,14 @@ function $createListOrMerge(node: ElementNode, listType: ListType): ListNode {
return node;
}

const selection = $getSelection();
const selectionStyle = $isRangeSelection(selection) ? selection.style : '';
const selectionFormat = $isRangeSelection(selection) ? selection.format : 0;
const previousSibling = node.getPreviousSibling();
const nextSibling = node.getNextSibling();
const listItem = $createListItemNode();
listItem.setTextStyle(selectionStyle);
listItem.setTextFormat(selectionFormat);
append(listItem, node.getChildren());

let targetList;
Expand All @@ -183,6 +188,7 @@ function $createListOrMerge(node: ElementNode, listType: ListType): ListNode {
node.replace(list);
targetList = list;
}

// listItem needs to be attached to root prior to setting indent
listItem.setFormat(node.getFormatType());
listItem.setIndent(node.getIndent());
Expand Down
5 changes: 2 additions & 3 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import type {LexicalEditor} from './LexicalEditor';
import type {NodeKey} from './LexicalNode';
import type {ElementNode} from './nodes/LexicalElementNode';
import type {TextNode} from './nodes/LexicalTextNode';

import {
Expand Down Expand Up @@ -59,7 +58,6 @@ import {
KEY_TAB_COMMAND,
MOVE_TO_END,
MOVE_TO_START,
ParagraphNode,
PASTE_COMMAND,
REDO_COMMAND,
REMOVE_TEXT_COMMAND,
Expand Down Expand Up @@ -130,6 +128,7 @@ import {
isUnderline,
isUndo,
} from './LexicalUtils';
import {ElementNode} from './nodes/LexicalElementNode';

type RootElementRemoveHandles = Array<() => void>;
type RootElementEvents = Array<
Expand Down Expand Up @@ -351,7 +350,7 @@ function onSelectionChange(
const lastNode = anchor.getNode();
selection.style = '';
if (
lastNode instanceof ParagraphNode &&
lastNode instanceof ElementNode &&
lastNode.getChildrenSize() === 0
) {
selection.format = lastNode.getTextFormat();
Expand Down
43 changes: 42 additions & 1 deletion packages/lexical/src/nodes/LexicalElementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import type {
PointType,
RangeSelection,
} from '../LexicalSelection';
import type {KlassConstructor, LexicalEditor, Spread} from 'lexical';
import type {
KlassConstructor,
LexicalEditor,
Spread,
TextFormatType,
} from 'lexical';

import {IS_IOS, IS_SAFARI} from 'shared/environment';
import invariant from 'shared/invariant';
Expand All @@ -27,6 +32,7 @@ import {
DOUBLE_LINE_BREAK,
ELEMENT_FORMAT_TO_TYPE,
ELEMENT_TYPE_TO_FORMAT,
TEXT_TYPE_TO_FORMAT,
} from '../LexicalConstants';
import {LexicalNode} from '../LexicalNode';
import {
Expand All @@ -51,6 +57,8 @@ export type SerializedElementNode<
direction: 'ltr' | 'rtl' | null;
format: ElementFormatType;
indent: number;
textStyle: string;
textFormat: number;
},
SerializedLexicalNode
>;
Expand Down Expand Up @@ -307,6 +315,10 @@ export class ElementNode extends LexicalNode {
__indent: number;
/** @internal */
__dir: 'ltr' | 'rtl' | null;
/** @internal */
__textStyle: string;
/** @internal */
__textFormat: number;

constructor(key?: NodeKey) {
super(key);
Expand All @@ -317,6 +329,8 @@ export class ElementNode extends LexicalNode {
this.__style = '';
this.__indent = 0;
this.__dir = null;
this.__textStyle = '';
this.__textFormat = 0;
}

afterCloneFrom(prevNode: this) {
Expand All @@ -328,6 +342,8 @@ export class ElementNode extends LexicalNode {
this.__format = prevNode.__format;
this.__style = prevNode.__style;
this.__dir = prevNode.__dir;
this.__textStyle = prevNode.__textStyle;
this.__textFormat = prevNode.__textFormat;
}

getFormat(): number {
Expand All @@ -342,6 +358,14 @@ export class ElementNode extends LexicalNode {
const self = this.getLatest();
return self.__style;
}
getTextStyle(): string {
const self = this.getLatest();
return self.__textStyle;
}
getTextFormat(): number {
const self = this.getLatest();
return self.__textFormat;
}
getIndent(): number {
const self = this.getLatest();
return self.__indent;
Expand Down Expand Up @@ -614,6 +638,21 @@ export class ElementNode extends LexicalNode {
self.__style = style || '';
return this;
}
setTextStyle(style: string): this {
const self = this.getWritable();
self.__textStyle = style || '';
return this;
}
setTextFormat(format: number): this {
const self = this.getWritable();
self.__textFormat = format;
return this;
}

hasTextFormat(type: TextFormatType): boolean {
const formatFlag = TEXT_TYPE_TO_FORMAT[type];
return (this.getTextFormat() & formatFlag) !== 0;
}
setIndent(indentLevel: number): this {
const self = this.getWritable();
self.__indent = indentLevel;
Expand Down Expand Up @@ -792,6 +831,8 @@ export class ElementNode extends LexicalNode {
direction: this.getDirection(),
format: this.getFormatType(),
indent: this.getIndent(),
textFormat: this.getTextFormat(),
textStyle: this.getTextStyle(),
type: 'element',
version: 1,
};
Expand Down
46 changes: 1 addition & 45 deletions packages/lexical/src/nodes/LexicalParagraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
EditorConfig,
KlassConstructor,
LexicalEditor,
Spread,
} from '../LexicalEditor';
import type {
DOMConversionMap,
Expand All @@ -25,7 +24,6 @@ import type {
} from './LexicalElementNode';
import type {RangeSelection} from 'lexical';

import {TEXT_TYPE_TO_FORMAT} from '../LexicalConstants';
import {
$applyNodeReplacement,
getCachedClassNameArray,
Expand All @@ -36,47 +34,20 @@ import {
import {ElementNode} from './LexicalElementNode';
import {$isTextNode, TextFormatType} from './LexicalTextNode';

export type SerializedParagraphNode = Spread<
{
textFormat: number;
textStyle: string;
},
SerializedElementNode
>;
export type SerializedParagraphNode = SerializedElementNode;

/** @noInheritDoc */
export class ParagraphNode extends ElementNode {
['constructor']!: KlassConstructor<typeof ParagraphNode>;
/** @internal */
__textFormat: number;
__textStyle: string;

constructor(key?: NodeKey) {
super(key);
this.__textFormat = 0;
this.__textStyle = '';
}

static getType(): string {
return 'paragraph';
}

getTextFormat(): number {
const self = this.getLatest();
return self.__textFormat;
}

setTextFormat(type: number): this {
const self = this.getWritable();
self.__textFormat = type;
return self;
}

hasTextFormat(type: TextFormatType): boolean {
const formatFlag = TEXT_TYPE_TO_FORMAT[type];
return (this.getTextFormat() & formatFlag) !== 0;
}

/**
* Returns the format flags applied to the node as a 32-bit integer.
*
Expand All @@ -88,25 +59,12 @@ export class ParagraphNode extends ElementNode {
return toggleTextFormatType(format, type, alignWithFormat);
}

getTextStyle(): string {
const self = this.getLatest();
return self.__textStyle;
}

setTextStyle(style: string): this {
const self = this.getWritable();
self.__textStyle = style;
return self;
}

static clone(node: ParagraphNode): ParagraphNode {
return new ParagraphNode(node.__key);
}

afterCloneFrom(prevNode: this) {
super.afterCloneFrom(prevNode);
this.__textFormat = prevNode.__textFormat;
this.__textStyle = prevNode.__textStyle;
}

// View
Expand Down Expand Up @@ -171,8 +129,6 @@ export class ParagraphNode extends ElementNode {
exportJSON(): SerializedParagraphNode {
return {
...super.exportJSON(),
textFormat: this.getTextFormat(),
textStyle: this.getTextStyle(),
type: 'paragraph',
version: 1,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/src/nodes/LexicalRootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class RootNode extends ElementNode {
direction: this.getDirection(),
format: this.getFormatType(),
indent: this.getIndent(),
textFormat: 0,
textStyle: '',
type: 'root',
version: 1,
};
Expand Down
Loading