@@ -19,7 +19,7 @@ import type {
|
19 | 19 | LexicalNode,
|
20 | 20 | NodeKey,
|
21 | 21 | } from '../LexicalNode';
|
22 |
| -import type {RangeSelection} from 'lexical'; |
| 22 | +import {RangeSelection, TEXT_TYPE_TO_FORMAT, TextFormatType} from 'lexical'; |
23 | 23 |
|
24 | 24 | import {
|
25 | 25 | $applyNodeReplacement,
|
@@ -36,6 +36,7 @@ import {CommonBlockNode, copyCommonBlockProperties, SerializedCommonBlockNode} f
|
36 | 36 |
|
37 | 37 | export type SerializedParagraphNode = Spread<
|
38 | 38 | {
|
| 39 | +textFormat: number; |
39 | 40 | textStyle: string;
|
40 | 41 | },
|
41 | 42 | SerializedCommonBlockNode
|
@@ -45,17 +46,35 @@ export type SerializedParagraphNode = Spread<
|
45 | 46 | export class ParagraphNode extends CommonBlockNode {
|
46 | 47 | ['constructor']!: KlassConstructor<typeof ParagraphNode>;
|
47 | 48 | /** @internal */
|
| 49 | +__textFormat: number; |
48 | 50 | __textStyle: string;
|
49 | 51 |
|
50 | 52 | constructor(key?: NodeKey) {
|
51 | 53 | super(key);
|
| 54 | +this.__textFormat = 0; |
52 | 55 | this.__textStyle = '';
|
53 | 56 | }
|
54 | 57 |
|
55 | 58 | static getType(): string {
|
56 | 59 | return 'paragraph';
|
57 | 60 | }
|
58 | 61 |
|
| 62 | +getTextFormat(): number { |
| 63 | +const self = this.getLatest(); |
| 64 | +return self.__textFormat; |
| 65 | +} |
| 66 | + |
| 67 | +setTextFormat(type: number): this { |
| 68 | +const self = this.getWritable(); |
| 69 | +self.__textFormat = type; |
| 70 | +return self; |
| 71 | +} |
| 72 | + |
| 73 | +hasTextFormat(type: TextFormatType): boolean { |
| 74 | +const formatFlag = TEXT_TYPE_TO_FORMAT[type]; |
| 75 | +return (this.getTextFormat() & formatFlag) !== 0; |
| 76 | +} |
| 77 | + |
59 | 78 | getTextStyle(): string {
|
60 | 79 | const self = this.getLatest();
|
61 | 80 | return self.__textStyle;
|
@@ -73,6 +92,7 @@ export class ParagraphNode extends CommonBlockNode {
|
73 | 92 |
|
74 | 93 | afterCloneFrom(prevNode: this) {
|
75 | 94 | super.afterCloneFrom(prevNode);
|
| 95 | +this.__textFormat = prevNode.__textFormat; |
76 | 96 | this.__textStyle = prevNode.__textStyle;
|
77 | 97 | copyCommonBlockProperties(prevNode, this);
|
78 | 98 | }
|
@@ -125,12 +145,14 @@ export class ParagraphNode extends CommonBlockNode {
|
125 | 145 | static importJSON(serializedNode: SerializedParagraphNode): ParagraphNode {
|
126 | 146 | const node = $createParagraphNode();
|
127 | 147 | deserializeCommonBlockNode(serializedNode, node);
|
| 148 | +node.setTextFormat(serializedNode.textFormat); |
128 | 149 | return node;
|
129 | 150 | }
|
130 | 151 |
|
131 | 152 | exportJSON(): SerializedParagraphNode {
|
132 | 153 | return {
|
133 | 154 | ...super.exportJSON(),
|
| 155 | +textFormat: this.getTextFormat(), |
134 | 156 | textStyle: this.getTextStyle(),
|
135 | 157 | type: 'paragraph',
|
136 | 158 | version: 1,
|
@@ -144,6 +166,7 @@ export class ParagraphNode extends CommonBlockNode {
|
144 | 166 | restoreSelection: boolean,
|
145 | 167 | ): ParagraphNode {
|
146 | 168 | const newElement = $createParagraphNode();
|
| 169 | +newElement.setTextFormat(rangeSelection.format); |
147 | 170 | newElement.setTextStyle(rangeSelection.style);
|
148 | 171 | const direction = this.getDirection();
|
149 | 172 | newElement.setDirection(direction);
|
|
0 commit comments