Skip to content

Commit 377dec7

Browse files
committed
feat: rename getProp and updateProps in lexical components
1 parent d7957df commit 377dec7

File tree

7 files changed

+67
-47
lines changed

7 files changed

+67
-47
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
### Features
1010

11+
#### Lexical components
12+
13+
- ✨ rename `getPropValue` to `getProp`
14+
- ✨ rename `setProps` to `updateProps`
15+
16+
## 1.4.2
17+
18+
2025-8-10
19+
20+
### Features
21+
1122
-`SelectNode` component now supports `onXXX` event props like `onClick`, `onMouseEnter`, etc.
1223

1324
## 1.4.1

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tiny-codes/react-easy",
3-
"version": "1.4.2",
3+
"version": "1.4.3",
44
"description": "Simplify React and AntDesign development with practical components and hooks",
55
"keywords": [
66
"react",

src/components/Lexical/helpers/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,26 @@ export function getDomAttributes(dom: HTMLElement | undefined): HtmlHTMLAttribut
278278

279279
return attributes;
280280
}
281+
282+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
283+
export function shallowEqual(obj1: any, obj2: any): boolean {
284+
if (obj1 === obj2) return true;
285+
if (!obj1 || !obj2) return false;
286+
287+
const keys1 = Object.keys(obj1);
288+
const keys2 = Object.keys(obj2);
289+
290+
if (keys1.length !== keys2.length) return false;
291+
292+
for (const key of keys1) {
293+
if (key === 'style') {
294+
// - EN: Special handling for style objects.
295+
// - CN: 特殊处理 style 对象。
296+
if (!shallowEqual(obj1[key], obj2[key])) return false;
297+
} else if (obj1[key] !== obj2[key]) {
298+
return false;
299+
}
300+
}
301+
302+
return true;
303+
}

src/components/Lexical/nodes/CloseIcon.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,6 @@ export class CloseIconNode extends BaseDecoratorNode<ReactNode, CloseIconNodePro
128128
return false;
129129
}
130130

131-
getPropValue(propName: keyof CloseIconNodeProps): CloseIconNodeProps[typeof propName] {
132-
return this.__props?.[propName];
133-
}
134-
135-
setProps(props: Partial<CloseIconNodeProps>): void {
136-
const writable = this.getWritable();
137-
writable.__props = {
138-
...writable.__props!,
139-
...props,
140-
};
141-
}
142-
143131
getUnderlyingProps(props: CloseIconNodeProps | undefined): Omit<CloseIconNodeProps, keyof BaseNodeProps> {
144132
const excludeProps = super.getUnderlyingProps(props);
145133
/* eslint-disable @typescript-eslint/no-unused-vars */

src/components/Lexical/nodes/DivNode.tsx

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
SerializedElementNode,
88
Spread,
99
} from 'lexical';
10-
import { updateDomProps } from '../helpers';
10+
import { shallowEqual, updateDomProps } from '../helpers';
1111
import { BaseElementNode, type BaseElementProps } from './base';
1212

1313
/**
@@ -52,7 +52,7 @@ export class DivNode extends BaseElementNode<DivNodeProps> {
5252
updateDOM(prevNode: DivNode, dom: HTMLElement): boolean {
5353
const prevProps = prevNode.__props;
5454
const currentProps = this.__props;
55-
const propsChanged = !this.shallowEqual(prevProps, currentProps);
55+
const propsChanged = !shallowEqual(prevProps, currentProps);
5656
if (propsChanged) {
5757
updateDomProps(dom, this.getUnderlyingProps(currentProps));
5858
}
@@ -97,10 +97,7 @@ export class DivNode extends BaseElementNode<DivNodeProps> {
9797
);
9898
}
9999

100-
getPropValue<K extends keyof DivNodeProps>(key: K): DivNodeProps[K] | undefined {
101-
return this.__props?.[key];
102-
}
103-
setProps(props: DivNodeProps): void {
100+
updateProps(props: DivNodeProps): void {
104101
const writable = this.getWritable();
105102
writable.__props = {
106103
...writable.__props,
@@ -111,29 +108,6 @@ export class DivNode extends BaseElementNode<DivNodeProps> {
111108
},
112109
};
113110
}
114-
115-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
116-
private shallowEqual(obj1: any, obj2: any): boolean {
117-
if (obj1 === obj2) return true;
118-
if (!obj1 || !obj2) return false;
119-
120-
const keys1 = Object.keys(obj1);
121-
const keys2 = Object.keys(obj2);
122-
123-
if (keys1.length !== keys2.length) return false;
124-
125-
for (const key of keys1) {
126-
if (key === 'style') {
127-
// - EN: Special handling for style objects.
128-
// - CN: 特殊处理 style 对象。
129-
if (!this.shallowEqual(obj1[key], obj2[key])) return false;
130-
} else if (obj1[key] !== obj2[key]) {
131-
return false;
132-
}
133-
}
134-
135-
return true;
136-
}
137111
}
138112
/**
139113
* - EN: Convert a DOM node to a DivNode during import.

src/components/Lexical/nodes/SelectNode.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Select } from 'antd';
44
import type { SelectProps } from 'antd';
55
import type { BaseOptionType, DefaultOptionType } from 'antd/es/select';
66
import type { LexicalEditor, LexicalNode, SerializedLexicalNode, Spread } from 'lexical';
7-
import { insertNodeAtCursor, updateDomStyle } from '../helpers';
7+
import { insertNodeAtCursor, shallowEqual, updateDomStyle } from '../helpers';
88
import type { BaseDecoratorNodeProps } from './base';
99
import { BaseDecoratorNode } from './base';
1010

@@ -81,8 +81,9 @@ export class SelectNode<
8181
return span;
8282
}
8383

84-
updateDOM(): false {
85-
return false;
84+
updateDOM(prevNode: this, dom: HTMLElement): boolean {
85+
const propsChanged = !shallowEqual(prevNode.__props, this.__props);
86+
return propsChanged;
8687
}
8788

8889
decorate(): ReactNode {
@@ -117,13 +118,13 @@ export class SelectNode<
117118
writable.__value = value;
118119
}
119120

120-
getPropValue(
121+
getProp(
121122
propName: keyof SelectNodeProps<ValueType, OptionType>
122123
): SelectNodeProps<ValueType, OptionType>[typeof propName] {
123124
return this.__props?.[propName];
124125
}
125126

126-
setProps(props: Partial<SelectNodeProps<ValueType, OptionType>>): void {
127+
updateProps(props: Partial<SelectNodeProps<ValueType, OptionType>>): void {
127128
const writable = this.getWritable();
128129
writable.__props = {
129130
...writable.__props,

src/components/Lexical/nodes/base.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ export class BaseElementNode<P extends BaseElementProps> extends ElementNode {
123123
return this.__props?.canInsertTextAfter ?? true;
124124
}
125125

126+
getProp(propName: keyof P): P[typeof propName] {
127+
return this.__props?.[propName] as P[typeof propName];
128+
}
129+
130+
updateProps(props: Partial<P>): void {
131+
const writable = this.getWritable();
132+
writable.__props = {
133+
...writable.__props!,
134+
...props,
135+
};
136+
}
137+
126138
/**
127139
* - EN: Strip element-specific flags and return DOM props.
128140
* - CN: 去除元素特有的标记并返回 DOM 属性。
@@ -163,6 +175,17 @@ export class BaseDecoratorNode<T, P extends BaseDecoratorNodeProps> extends Deco
163175
}
164176
});
165177
}
178+
getProp(propName: keyof P): P[typeof propName] {
179+
return this.__props?.[propName] as P[typeof propName];
180+
}
181+
182+
updateProps(props: Partial<P>): void {
183+
const writable = this.getWritable();
184+
writable.__props = {
185+
...writable.__props!,
186+
...props,
187+
};
188+
}
166189

167190
/**
168191
* - EN: Strip decorator-specific flags and return DOM props.

0 commit comments

Comments
 (0)