LexicalNode,
NodeKey,
} from '../LexicalNode';
-import type {RangeSelection} from 'lexical';
+import {RangeSelection, TEXT_TYPE_TO_FORMAT, TextFormatType} from 'lexical';
import {
$applyNodeReplacement,
export type SerializedParagraphNode = Spread<
{
+ textFormat: number;
textStyle: string;
},
SerializedCommonBlockNode
export class ParagraphNode extends CommonBlockNode {
['constructor']!: KlassConstructor<typeof ParagraphNode>;
/** @internal */
+ __textFormat: number;
__textStyle: string;
constructor(key?: NodeKey) {
super(key);
+ this.__textFormat = 0;
this.__textStyle = '';
}
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;
+ }
+
getTextStyle(): string {
const self = this.getLatest();
return self.__textStyle;
afterCloneFrom(prevNode: this) {
super.afterCloneFrom(prevNode);
+ this.__textFormat = prevNode.__textFormat;
this.__textStyle = prevNode.__textStyle;
copyCommonBlockProperties(prevNode, this);
}
static importJSON(serializedNode: SerializedParagraphNode): ParagraphNode {
const node = $createParagraphNode();
deserializeCommonBlockNode(serializedNode, node);
+ node.setTextFormat(serializedNode.textFormat);
return node;
}
exportJSON(): SerializedParagraphNode {
return {
...super.exportJSON(),
+ textFormat: this.getTextFormat(),
textStyle: this.getTextStyle(),
type: 'paragraph',
version: 1,
restoreSelection: boolean,
): ParagraphNode {
const newElement = $createParagraphNode();
+ newElement.setTextFormat(rangeSelection.format);
newElement.setTextStyle(rangeSelection.style);
const direction = this.getDirection();
newElement.setDirection(direction);
$createParagraphNode, $createRangeSelection,
$getRoot,
$getSelection, $isBlockElementNode, $isDecoratorNode,
- $isElementNode,
+ $isElementNode, $isParagraphNode,
$isTextNode,
$setSelection,
BaseSelection, DecoratorNode,
return false;
}
- for (const node of selection.getNodes()) {
+ // Check text nodes
+ const nodes = selection.getNodes();
+ for (const node of nodes) {
if ($isTextNode(node) && node.hasFormat(format)) {
return true;
}
}
+ // If we're in an empty paragraph, check the paragraph format
+ if (nodes.length === 1 && $isParagraphNode(nodes[0]) && nodes[0].hasTextFormat(format)) {
+ return true;
+ }
+
return false;
}