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";
4955import DataBlock from " ./components/dataBlock.vue" ;
5056import CodeDisplay from " ./components/codeDisplay.vue" ;
5157import { VueDraggable } from " vue-draggable-plus" ;
52- import type { FunctionPlotDatum , FunctionPlotOptions } from " function-plot" ;
58+ import type { FunctionPlotDatum } from " function-plot" ;
5359import { onMounted , ref } from " vue" ;
54- import { cloneDeep } from " lodash-es" ;
5560import JSON5 from " json5" ;
5661import base64 from " base-64" ;
5762import 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
6269const 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-
10883function 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() {
143117function 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 ;
0 commit comments