Skip to content

Commit dff2946

Browse files
authored
refactor: migrate legacy rich text copmonents after editing (#5275)
https://github.com/user-attachments/assets/1e6f936e-d60e-44cd-9a04-24201e7449e9
1 parent a9d31e7 commit dff2946

File tree

2 files changed

+33
-110
lines changed

2 files changed

+33
-110
lines changed

apps/builder/app/canvas/features/text-editor/interop.test.tsx

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -136,42 +136,6 @@ test("convert element instances to lexical", async () => {
136136
);
137137
});
138138

139-
test("convert lexical to legacy instances updates", async () => {
140-
const refs: Refs = new Map();
141-
const editor = createHeadlessEditor({
142-
nodes: [LinkNode],
143-
});
144-
await new Promise<void>((resolve) => {
145-
editor.update(
146-
() => {
147-
$convertToLexical(instances, "textBoxId", refs);
148-
},
149-
{ onUpdate: resolve }
150-
);
151-
});
152-
const treeRootInstance = instances.get("textBoxId");
153-
if (treeRootInstance === undefined) {
154-
throw Error("Tree root instance should be in test data");
155-
}
156-
const updates = editor.getEditorState().read(() => {
157-
return $convertToUpdates(treeRootInstance, refs, new Map());
158-
});
159-
expect(updates).toEqual(
160-
renderTemplate(
161-
<$.Box ws:id="textBoxId">
162-
Hello{"\n"}
163-
<$.Bold ws:id="boldId">
164-
<$.Italic ws:id="italicId">world</$.Italic>
165-
</$.Bold>
166-
{"\n"}
167-
<$.Span ws:id="spanId">and</$.Span>
168-
{"\n"}
169-
<$.RichTextLink ws:id="linkId">other realms</$.RichTextLink>
170-
</$.Box>
171-
).instances
172-
);
173-
});
174-
175139
test("convert lexical to element instances updates", async () => {
176140
const refs: Refs = new Map();
177141
const editor = createHeadlessEditor({

apps/builder/app/canvas/features/text-editor/interop.ts

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ const $writeUpdates = (
4141
instanceChildren: Instance["children"],
4242
instancesList: Instance[],
4343
refs: Refs,
44-
newLinkKeyToInstanceId: Refs,
45-
isElement: boolean
44+
newLinkKeyToInstanceId: Refs
4645
) => {
4746
const children = node.getChildren();
4847
for (const child of children) {
@@ -52,8 +51,7 @@ const $writeUpdates = (
5251
instanceChildren,
5352
instancesList,
5453
refs,
55-
newLinkKeyToInstanceId,
56-
isElement
54+
newLinkKeyToInstanceId
5755
);
5856
}
5957
if ($isLineBreakNode(child)) {
@@ -73,25 +71,15 @@ const $writeUpdates = (
7371
childChildren,
7472
instancesList,
7573
refs,
76-
newLinkKeyToInstanceId,
77-
isElement
74+
newLinkKeyToInstanceId
7875
);
79-
if (isElement) {
80-
instancesList.push({
81-
type: "instance",
82-
id,
83-
component: elementComponent,
84-
tag: "a",
85-
children: childChildren,
86-
});
87-
} else {
88-
instancesList.push({
89-
type: "instance",
90-
id,
91-
component: "RichTextLink",
92-
children: childChildren,
93-
});
94-
}
76+
instancesList.push({
77+
type: "instance",
78+
id,
79+
component: elementComponent,
80+
tag: "a",
81+
children: childChildren,
82+
});
9583
}
9684
if ($isTextNode(child)) {
9785
// support nesting bold into italic and vice versa
@@ -105,60 +93,32 @@ const $writeUpdates = (
10593
const id = refs.get(key) ?? nanoid();
10694
refs.set(key, id);
10795
const childChildren: Instance["children"] = [];
108-
if (isElement) {
109-
instancesList.push({
110-
type: "instance",
111-
id,
112-
component: elementComponent,
113-
tag: "span",
114-
children: childChildren,
115-
});
116-
} else {
117-
instancesList.push({
118-
type: "instance",
119-
id,
120-
component: "Span",
121-
children: childChildren,
122-
});
123-
}
96+
instancesList.push({
97+
type: "instance",
98+
id,
99+
component: elementComponent,
100+
tag: "span",
101+
children: childChildren,
102+
});
124103
parentUpdates.push({ type: "id", value: id });
125104
parentUpdates = childChildren;
126105
}
127106
// convert all lexical formats
128-
if (isElement) {
129-
for (const [format, tag] of elementLexicalFormats) {
130-
if (child.hasFormat(format)) {
131-
const key = `${child.getKey()}:${format}`;
132-
const id = refs.get(key) ?? nanoid();
133-
refs.set(key, id);
134-
const childInstance: Instance = {
135-
type: "instance",
136-
id,
137-
component: elementComponent,
138-
tag,
139-
children: [],
140-
};
141-
instancesList.push(childInstance);
142-
parentUpdates.push({ type: "id", value: id });
143-
parentUpdates = childInstance.children;
144-
}
145-
}
146-
} else {
147-
for (const [format, component] of legacyLexicalFormats) {
148-
if (child.hasFormat(format)) {
149-
const key = `${child.getKey()}:${format}`;
150-
const id = refs.get(key) ?? nanoid();
151-
refs.set(key, id);
152-
const childInstance: Instance = {
153-
type: "instance",
154-
id,
155-
component,
156-
children: [],
157-
};
158-
instancesList.push(childInstance);
159-
parentUpdates.push({ type: "id", value: id });
160-
parentUpdates = childInstance.children;
161-
}
107+
for (const [format, tag] of elementLexicalFormats) {
108+
if (child.hasFormat(format)) {
109+
const key = `${child.getKey()}:${format}`;
110+
const id = refs.get(key) ?? nanoid();
111+
refs.set(key, id);
112+
const childInstance: Instance = {
113+
type: "instance",
114+
id,
115+
component: elementComponent,
116+
tag,
117+
children: [],
118+
};
119+
instancesList.push(childInstance);
120+
parentUpdates.push({ type: "id", value: id });
121+
parentUpdates = childInstance.children;
162122
}
163123
}
164124
parentUpdates.push({ type: "text", value: text });
@@ -184,8 +144,7 @@ export const $convertToUpdates = (
184144
treeRootInstanceChildren,
185145
instancesList,
186146
refs,
187-
newLinkKeyToInstanceId,
188-
treeRootInstance.component === elementComponent
147+
newLinkKeyToInstanceId
189148
);
190149
return instancesList;
191150
};

0 commit comments

Comments
 (0)