-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcode-options-control.tsx
273 lines (253 loc) · 7.46 KB
/
code-options-control.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React, { useEffect } from "react";
import { IField, LanguageType, Option } from "@code-ui/docstring/dist/lib/type";
import { Docstring as DocstringView } from "@code-ui/docstring";
import {
FrameworkOption,
getpreset,
Language,
all_preset_options_map__prod,
lang_by_framework,
ReactOption,
VanillaOption,
FlutterOption,
react_styles,
} from "./framework-options";
import styled from "@emotion/styled";
import assert from "assert";
type DesigntoCodeUserOptions = FrameworkOption;
// FIXME: get useroption as props from parent. userprops & preset (optional) should be managed on its parent
interface CodeOptionsControlProps {
customFields?: IField[];
fallbackPreset?: string;
initialPreset?: string;
onUseroptionChange: (op: DesigntoCodeUserOptions) => void;
}
export function CodeOptionsControl(props: CodeOptionsControlProps) {
const __presetname = props.initialPreset ?? props.fallbackPreset;
const [presetname, setPresetname] = React.useState<string>(__presetname);
const [useroption, setUseroption] = React.useState<DesigntoCodeUserOptions>(
all_preset_options_map__prod[__presetname]
);
useEffect(() => {
// trigger initial value
props.onUseroptionChange(useroption);
}, []);
assert(useroption, "option must be specified");
// FIXME: this should be fixed on https://github.com/gridaco/code-like-ui (view CURSOR)
const __dirty_sort_framework = (): Option<string>[] => {
const presets = [
{
name: "React",
value: "react_default",
description: "(default)",
},
{
name: "React",
value: "react_with_styled_components",
description: "with styled-component",
},
{
name: "React",
value: "react_with_inline_css",
description: "with inline-css",
},
{
name: "React",
value: "react_with_css_module",
description: "with css-module",
},
{
name: "React Native",
value: "reactnative_default",
description: "react-native",
},
{
name: "React Native",
value: "reactnative_with_styled_components",
description: "with styled-components",
},
{
name: "React Native",
value: "reactnative_with_inline_style",
description: "with inline-style",
},
{
name: "Solid",
value: "solid_default",
description: "solid-js",
},
{
name: "Solid",
value: "solid_with_inline_css",
description: "with inline-css",
},
{
name: "Flutter",
value: "flutter_default",
description: "flutter",
},
{
name: "Preact",
value: "preact_default",
description: "preact",
},
{
name: "Vanilla",
value: "vanilla_default",
description: "vanilla Html",
},
];
/* !CURSOR! */
const sorted_plats: Option<string>[] = presets.sort((o) => {
if (o.value == presetname) {
return -1;
}
return 1;
});
return sorted_plats;
};
/**
* actually platform preset
*/
const platform_field_config: IField = {
tag: "@",
name: "platform", // actually platform preset
template: `{{ tag }}{{ name }} {{ options.name }} `,
options: __dirty_sort_framework(),
};
const getreactstyle = (frameworkPreset: string) => {
const preset = getpreset(frameworkPreset) as ReactOption;
const selected_styling = preset.styling;
const sorted_styles = [
selected_styling,
/* remove target item // - https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice */
...react_styles.splice(1, 0, selected_styling),
];
return sorted_styles;
};
const react_style_field_config: IField = {
tag: "@",
name: "style", // actually platform preset
template: `{{ tag }}{{ name }} {{ options.name }} `,
options: getreactstyle(presetname).map((l) => {
return {
name: l,
value: l,
};
}),
};
const getlang = (frameworkPreset: string) => {
const preset = getpreset(frameworkPreset);
// if user has selected preest, get framework value by preset name, otherwise get the selected framework via `useroption`
let frameowrk = preset?.framework ?? useroption.framework;
const langoptions = lang_by_framework[frameowrk];
const selected_lang = preset.language;
const sorted_langs = [
selected_lang,
/* remove target item // - https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice */
...langoptions.splice(1, 0, selected_lang),
];
return sorted_langs;
};
const lang_field_config: IField = {
tag: "@",
name: "lang",
template: `{{ tag }}{{ name }} {{ options.name }} `,
options: getlang(presetname).map((l) => {
return {
name: l,
value: l,
};
}),
};
const fields_config = {
react: [platform_field_config, lang_field_config, react_style_field_config],
preact: [
platform_field_config,
lang_field_config,
react_style_field_config,
],
"react-native": [platform_field_config, lang_field_config],
"solid-js": [platform_field_config, lang_field_config],
flutter: [platform_field_config, lang_field_config],
vanilla: [platform_field_config, lang_field_config],
};
function onChagne(field: string, value: string) {
// console.log("code-screen-control::onChagne", field, value);
// platform here stands for platform preset
if (field === "platform") {
const preset = getpreset(value);
setPresetname(value);
setUseroption({ ...preset });
props.onUseroptionChange({ ...preset });
} else if (field === "lang") {
let op: FrameworkOption;
switch (value) {
case "tsx":
case "jsx":
op = {
...useroption,
language: value, // TODO: add generic type checker
} as ReactOption;
setUseroption(op); // FIXME: state from p
props.onUseroptionChange(op);
break;
case "dart":
op = {
...useroption,
language: Language.dart,
} as FlutterOption;
setUseroption(op); // FIXME: state from p
props.onUseroptionChange(op);
break;
case "html":
op = {
...useroption,
language: Language.html,
} as VanillaOption;
setUseroption(op); // FIXME: state from p
props.onUseroptionChange(op);
break;
default:
throw `This lang (${value}) is not currently supported.`;
}
}
}
const _controls = [
...(props.customFields ?? []),
...fields_config[useroption.framework],
];
// console.log("code-screen-control::useroption", useroption);
return (
<Wrapper>
<DocstringView
key={JSON.stringify(useroption)}
lang={__lang_to_docstring_lang(useroption.language)}
theme={"monokai"}
padding={"16px"}
controls={_controls}
expandableConfig={{
lines: 2,
expandable: true,
hidable: true,
}}
onChange={onChagne}
/>
</Wrapper>
);
}
function __lang_to_docstring_lang(lang: Language): LanguageType {
switch (lang) {
case Language.dart:
return "dart";
case Language.jsx:
case Language.tsx:
return "js";
}
}
const Wrapper = styled.div`
div,
ul {
font-family: "Source Code Pro", "Courier New", "Lucida Console", Monaco;
}
`;