Skip to content

Commit 038cac9

Browse files
update result signature & prevent editor flickering by state
1 parent 3729384 commit 038cac9

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

editor/scaffolds/code/index.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,35 @@ export function CodeSegment() {
5656
const targetted =
5757
designq.find_node_by_id_under_entry(targetId, root?.entry) ?? root?.entry;
5858

59-
const targetStateRef = useRef<ReflectSceneNode>();
60-
targetStateRef.current = targetted;
59+
const targetStateRef =
60+
useRef<{
61+
node: ReflectSceneNode;
62+
config: config.FrameworkConfig;
63+
}>();
64+
targetStateRef.current = { node: targetted, config: framework_config };
6165

6266
const on_result = (result: Result) => {
67+
if (
68+
result.framework.framework !==
69+
targetStateRef?.current?.config.framework ||
70+
result.id !== targetStateRef?.current?.node.id
71+
) {
72+
return;
73+
}
74+
6375
if (framework_config.language == "dart") {
6476
// special formatter support for dartlang
6577
result.code.raw = utils_dart.format(result.code.raw);
6678
result.scaffold.raw = utils_dart.format(result.scaffold.raw);
6779
}
6880

69-
if (result.id == targetStateRef?.current?.id) {
70-
setResult(result);
71-
}
81+
setResult(result);
7282
};
7383

7484
useEffect(() => {
7585
const __target = targetted;
76-
if (__target && framework_config) {
86+
const __framework_config = framework_config;
87+
if (__target && __framework_config) {
7788
const _input = {
7889
id: __target.id,
7990
name: __target.name,
@@ -88,7 +99,7 @@ export function CodeSegment() {
8899
// build code without assets fetch
89100
designToCode({
90101
input: _input,
91-
framework: framework_config,
102+
framework: __framework_config,
92103
asset_config: { skip_asset_replacement: true },
93104
build_config: build_config,
94105
})
@@ -99,7 +110,7 @@ export function CodeSegment() {
99110
if (!MainImageRepository.instance.empty) {
100111
designToCode({
101112
input: root,
102-
framework: framework_config,
113+
framework: __framework_config,
103114
asset_config: { asset_repository: MainImageRepository.instance },
104115
build_config: build_config,
105116
})

packages/designto-code/universal/design-to-code.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
default_tokenizer_config,
1616
TokenizerConfig,
1717
} from "@designto/token/config";
18-
import { default_build_configuration } from "@designto/config";
18+
import { default_build_configuration, FrameworkConfig } from "@designto/config";
1919
import { reusable } from "@code-features/component";
2020
import assert from "assert";
2121

@@ -28,7 +28,9 @@ interface AssetsConfig {
2828
custom_asset_replacement?: { type: "static"; resource: string };
2929
}
3030

31-
export type Result = output.ICodeOutput & { widget: Widget };
31+
export type Result = output.ICodeOutput & { widget: Widget } & {
32+
framework: FrameworkConfig;
33+
};
3234

3335
export async function designToCode({
3436
input,
@@ -101,6 +103,11 @@ export async function designToCode({
101103
reusable_widget_tree: reusable_widget_tree,
102104
};
103105

106+
const _extend_result = {
107+
..._tokenized_widget_input,
108+
framework: framework_config,
109+
};
110+
104111
switch (framework_config.framework) {
105112
case "vanilla":
106113
return {
@@ -110,7 +117,7 @@ export async function designToCode({
110117
vanilla_config: framework_config,
111118
asset_config: asset_config,
112119
})),
113-
..._tokenized_widget_input,
120+
..._extend_result,
114121
};
115122
case "react":
116123
return {
@@ -120,7 +127,7 @@ export async function designToCode({
120127
react_config: framework_config,
121128
asset_config: asset_config,
122129
})),
123-
..._tokenized_widget_input,
130+
..._extend_result,
124131
};
125132
case "react-native":
126133
return {
@@ -130,7 +137,7 @@ export async function designToCode({
130137
reactnative_config: framework_config,
131138
asset_config: asset_config,
132139
})),
133-
..._tokenized_widget_input,
140+
..._extend_result,
134141
};
135142
case "flutter":
136143
return {
@@ -140,7 +147,7 @@ export async function designToCode({
140147
flutter_config: framework_config,
141148
asset_config: asset_config,
142149
})),
143-
..._tokenized_widget_input,
150+
..._extend_result,
144151
};
145152
}
146153

0 commit comments

Comments
 (0)