Skip to content

Commit 9c67895

Browse files
committed
重构数据类型
1 parent 6c93856 commit 9c67895

6 files changed

Lines changed: 121 additions & 137 deletions

File tree

src/App.vue

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,26 @@
1818
</VueDraggable>
1919
<div class="plot-data add-data">
2020
<div
21-
@click="graphData.push({ key: Math.random() })"
21+
@click="
22+
graphData.push({
23+
key: Math.random(),
24+
fnType: 'linear',
25+
graphType: 'polyline',
26+
})
27+
"
2228
class="add-data-opt add"
2329
>
2430
+ 添加
2531
</div>
2632
<div @click="handleImport()" class="add-data-opt import">↓ 导入</div>
2733
</div>
2834
</div>
29-
<CodeDisplay :dataArr="cloneArr(graphData)" />
35+
<CodeDisplay :dataArr="toOriginalDatum(graphData)" />
3036
</div>
3137
<div id="divider" @mousedown="handleDrag"></div>
3238
<div id="graph" ref="shellRef">
3339
<Graph
34-
:data="cloneArr(graphData)"
40+
:data="toOriginalDatum(graphData)"
3541
:width="graphWidth"
3642
:height="graphHeight"
3743
:key="key"
@@ -49,15 +55,16 @@ import Graph from "./components/graph.vue";
4955
import DataBlock from "./components/dataBlock.vue";
5056
import CodeDisplay from "./components/codeDisplay.vue";
5157
import { VueDraggable } from "vue-draggable-plus";
52-
import type { FunctionPlotDatum, FunctionPlotOptions } from "function-plot";
58+
import type { FunctionPlotDatum } from "function-plot";
5359
import { onMounted, ref } from "vue";
54-
import { cloneDeep } from "lodash-es";
5560
import JSON5 from "json5";
5661
import base64 from "base-64";
5762
import utf8 from "utf8";
58-
import { Datum } from "./consts";
63+
import { InternalDatum, toInternalDatum, toOriginalDatum } from "./consts";
5964
60-
const graphData = ref<Datum[]>([{ fn: "x^2", key: 1 }]);
65+
const graphData = ref<InternalDatum[]>([
66+
{ fnType: "linear", graphType: "polyline", fn: "x^2", key: 1 },
67+
]);
6168
6269
const graphWidth = ref(0),
6370
graphHeight = ref(0);
@@ -73,38 +80,6 @@ function handleResize() {
7380
}
7481
}
7582
76-
function cloneArr<T extends Object[]>(obj: T) {
77-
const cloned = cloneDeep(obj);
78-
function removeUndefined(obj: Record<string, any>) {
79-
for (const key in obj) {
80-
console.log(1);
81-
if (obj[key] === undefined) delete obj[key];
82-
if (
83-
typeof obj[key] === "object" &&
84-
obj[key] !== null &&
85-
!Array.isArray(obj[key])
86-
)
87-
removeUndefined(obj[key]);
88-
}
89-
}
90-
cloned.forEach((item) => removeUndefined(item));
91-
return cloned as T;
92-
}
93-
94-
function importMapper(item: FunctionPlotDatum): Datum {
95-
if (item.graphType === "text")
96-
return {
97-
...item,
98-
fnType: "text",
99-
key: Math.random(),
100-
};
101-
else
102-
return {
103-
...item,
104-
key: Math.random(),
105-
};
106-
}
107-
10883
function fullUpdate() {
10984
fullUpdateState.value = true;
11085
key.value++;
@@ -115,14 +90,13 @@ onMounted(() => {
11590
if (rawCode)
11691
try {
11792
const code = utf8.decode(base64.decode(decodeURIComponent(rawCode)));
118-
const data = (<FunctionPlotDatum[]>JSON5.parse(code).data).map(
119-
importMapper
93+
const data = toInternalDatum(
94+
(JSON5.parse(code).data as FunctionPlotDatum[]) ?? []
12095
);
121-
graphData.value = <Datum[]>data;
96+
graphData.value = toInternalDatum(<FunctionPlotDatum[]>data);
12297
console.log(code);
12398
console.log(data);
12499
} catch (e) {}
125-
126100
window.addEventListener("resize", handleResize);
127101
handleResize();
128102
});
@@ -143,8 +117,9 @@ function handleDrag() {
143117
function handleImport() {
144118
const raw = prompt("源数据:");
145119
if (!raw) return;
146-
graphData.value =
147-
(<FunctionPlotOptions>JSON5.parse(raw)).data?.map(importMapper) ?? [];
120+
graphData.value = toInternalDatum(
121+
(JSON5.parse(raw).data as FunctionPlotDatum[]) ?? []
122+
);
148123
}
149124
</script>
150125

@@ -197,7 +172,6 @@ function handleImport() {
197172
padding: 0;
198173
display: flex;
199174
flex-direction: row;
200-
cursor: default;
201175
}
202176
.add-data-opt {
203177
padding: 10px 30px;

src/components/codeDisplay.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ import prettier from "prettier/standalone";
1111
import prettierPluginBabel from "prettier/plugins/babel";
1212
import prettierPluginEstree from "prettier/plugins/estree";
1313
import { ref, watch } from "vue";
14-
import { Datum } from "../consts";
14+
import { FunctionPlotDatum } from "function-plot";
1515
const { dataArr } = defineProps<{
16-
dataArr: (Omit<Datum, "key"> & { key?: number })[];
16+
dataArr: FunctionPlotDatum[];
1717
}>();
1818
const formatted = ref("");
1919
watch(
2020
() => dataArr,
2121
() => {
22-
dataArr.forEach((item) => {
23-
if (item.graphType === "text") delete item.fnType;
24-
delete item.key;
25-
});
2622
prettier
2723
.format(JSON5.stringify({ data: dataArr }), {
2824
parser: "json5",

src/components/dataBlock.vue

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
<div class="datablock-drag">☰</div>
44
<div class="selectors">
55
<select v-model="dataItem.fnType" @change="fnTypeChange(dataItem)">
6-
<option :value="undefined">{{ fnTypeArr[0].label }}</option>
7-
<option v-for="type in fnTypeArr.slice(1)" :value="type.value">
6+
<option v-for="type in fnTypeArr" :value="type.value">
87
{{ type.label }}
98
</option>
109
</select>
@@ -13,14 +12,7 @@
1312
v-if="dataItem.graphType !== 'text'"
1413
@change="graphTypeChange(dataItem)"
1514
>
16-
<option v-if="!fnType.notAllowedInInterval" :value="undefined">
17-
{{ graphTypeArr[0].label }}
18-
</option>
19-
<option
20-
v-if="dataItem.fnType !== 'implicit'"
21-
v-for="type in graphTypeArr.slice(1)"
22-
:value="type.value"
23-
>
15+
<option v-for="type in allowedGraphType" :value="type.value">
2416
{{ type.label }}
2517
</option>
2618
</select>
@@ -71,10 +63,10 @@
7163
<script setup lang="ts">
7264
import {
7365
fnTypeArr,
74-
graphTypeArr,
66+
getAllowedGraphType,
7567
inputTypeArr,
7668
getFnType,
77-
Datum,
69+
InternalDatum,
7870
} from "../consts";
7971
import { ref, computed } from "vue";
8072
import StrInputs from "./dataBlockInner/strInputs.vue";
@@ -84,26 +76,25 @@ import CoordArrInputs from "./dataBlockInner/coordArrInputs.vue";
8476
import OptInputs from "./dataBlockInner/optInputs.vue";
8577
8678
const emit = defineEmits(["delete", "requireFullUpdate"]);
87-
const dataItem = defineModel<Datum>();
79+
const dataItem = defineModel<InternalDatum>();
8880
const block = ref<HTMLDivElement>();
8981
const blockFolded = ref(true);
90-
function fnTypeChange(dataItem: Datum) {
82+
function fnTypeChange(dataItem: InternalDatum) {
9183
inputTypeArr.forEach((key) => delete dataItem[key]);
9284
if (dataItem.fnType === "text") {
9385
dataItem.graphType = "text";
9486
} else {
9587
if (dataItem.graphType === "text" || dataItem.fnType === "implicit")
9688
delete dataItem.graphType;
97-
if (fnType.value.notAllowedInInterval && !dataItem.graphType)
98-
dataItem.graphType = "polyline";
89+
dataItem.graphType = getAllowedGraphType(dataItem.fnType)[0].value;
9990
if (dataItem.fnType === "vector") dataItem.vector = [0, 0];
10091
if (dataItem.fnType === "points") dataItem.points = [];
10192
if (block.value)
10293
block.value.querySelectorAll("input").forEach((ele) => (ele.value = ""));
10394
}
10495
}
105-
const scatteredSet = new WeakSet<Datum>();
106-
function graphTypeChange(dataItem: Datum) {
96+
const scatteredSet = new WeakSet<InternalDatum>();
97+
function graphTypeChange(dataItem: InternalDatum) {
10798
if (dataItem.graphType === "scatter") {
10899
if (!scatteredSet.has(dataItem)) {
109100
scatteredSet.add(dataItem);
@@ -117,6 +108,9 @@ function graphTypeChange(dataItem: Datum) {
117108
}
118109
}
119110
const fnType = computed(() => getFnType(dataItem.value?.fnType));
111+
const allowedGraphType = computed(() =>
112+
getAllowedGraphType(dataItem.value?.fnType)
113+
);
120114
</script>
121115

122116
<style>

src/components/dataBlockInner/coordInputs.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
</template>
2626

2727
<script setup lang="ts">
28-
import { CoordType, Datum, InputProps } from "../../consts";
28+
import { CoordType, InternalDatum, InputProps } from "../../consts";
2929
3030
const { dataItem, fnType } = defineProps<InputProps>();
3131
3232
function handleCoordInput(
33-
dataItem: Datum,
33+
dataItem: InternalDatum,
3434
input: CoordType,
3535
index: 0 | 1,
3636
event: Event

src/components/dataBlockInner/optInputs.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
</template>
1616

1717
<script setup lang="ts">
18-
import { Datum, InputProps, OptInput } from "../../consts";
18+
import { InternalDatum, InputProps, OptInput } from "../../consts";
1919
2020
const { dataItem, fnType } = defineProps<InputProps>();
2121
22-
function handleCoordInput(dataItem: Datum, input: OptInput, event: Event) {
22+
function handleCoordInput(dataItem: InternalDatum, input: OptInput, event: Event) {
2323
const raw = (<HTMLInputElement>event.target).value;
2424
if (raw === "") {
2525
delete dataItem[input.value];

0 commit comments

Comments
 (0)