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 >
24- + 添加
30+ + {{ t("buttons.add") }}
31+ </div >
32+ <div @click =" handleImport()" class =" add-data-opt import" >
33+ ↓ {{ t("buttons.import") }}
2534 </div >
26- <div @click =" handleImport()" class =" add-data-opt import" >↓ 导入</div >
2735 </div >
2836 </div >
29- <CodeDisplay :dataArr =" cloneArr (graphData)" />
37+ <CodeDisplay :dataArr =" toOriginalDatum (graphData)" />
3038 </div >
3139 <div id =" divider" @mousedown =" handleDrag" ></div >
3240 <div id =" graph" ref =" shellRef" >
3341 <Graph
34- :data =" cloneArr (graphData)"
42+ :data =" toOriginalDatum (graphData)"
3543 :width =" graphWidth"
3644 :height =" graphHeight"
3745 :key =" key"
4452</template >
4553
4654<script setup lang="ts">
55+ import { useI18n } from ' vue-i18n'
56+ const { t } = useI18n ()
57+
4758import Navbar from " ./components/nav.vue" ;
4859import Graph from " ./components/graph.vue" ;
4960import DataBlock from " ./components/dataBlock.vue" ;
5061import CodeDisplay from " ./components/codeDisplay.vue" ;
5162import { VueDraggable } from " vue-draggable-plus" ;
52- import type { FunctionPlotDatum , FunctionPlotOptions } from " function-plot" ;
63+ import type { FunctionPlotDatum } from " function-plot" ;
5364import { onMounted , ref } from " vue" ;
54- import { cloneDeep } from " lodash-es" ;
5565import JSON5 from " json5" ;
5666import base64 from " base-64" ;
5767import utf8 from " utf8" ;
58- import { Datum } from " ./consts" ;
68+ import { InternalDatum , toInternalDatum , toOriginalDatum } from " ./consts" ;
5969
60- const graphData = ref <Datum []>([{ fn: " x^2" , key: 1 }]);
70+ const graphData = ref <InternalDatum []>([
71+ { fnType: " linear" , graphType: " polyline" , fn: " x^2" , key: 1 },
72+ ]);
6173
6274const graphWidth = ref (0 ),
6375 graphHeight = ref (0 );
@@ -73,38 +85,6 @@ function handleResize() {
7385 }
7486}
7587
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-
10888function fullUpdate() {
10989 fullUpdateState .value = true ;
11090 key .value ++ ;
@@ -115,14 +95,13 @@ onMounted(() => {
11595 if (rawCode )
11696 try {
11797 const code = utf8 .decode (base64 .decode (decodeURIComponent (rawCode )));
118- const data = (< FunctionPlotDatum []> JSON5 . parse ( code ). data ). map (
119- importMapper
98+ const data = toInternalDatum (
99+ ( JSON5 . parse ( code ). data as FunctionPlotDatum []) ?? []
120100 );
121- graphData .value = < Datum []>data ;
101+ graphData .value = toInternalDatum (< FunctionPlotDatum []>data ) ;
122102 console .log (code );
123103 console .log (data );
124104 } catch (e ) {}
125-
126105 window .addEventListener (" resize" , handleResize );
127106 handleResize ();
128107});
@@ -143,8 +122,9 @@ function handleDrag() {
143122function handleImport() {
144123 const raw = prompt (" 源数据:" );
145124 if (! raw ) return ;
146- graphData .value =
147- (<FunctionPlotOptions >JSON5 .parse (raw )).data ?.map (importMapper ) ?? [];
125+ graphData .value = toInternalDatum (
126+ (JSON5 .parse (raw ).data as FunctionPlotDatum []) ?? []
127+ );
148128}
149129 </script >
150130
@@ -171,6 +151,7 @@ function handleImport() {
171151#navbar {
172152 height : 50px ;
173153 width : 100vw ;
154+ box-sizing : border-box ;
174155 background : var (--c-bk1 );
175156 border-bottom : var (--c-border ) 1px solid ;
176157 position : relative ;
@@ -179,15 +160,14 @@ function handleImport() {
179160#editor {
180161 border-right : var (--c-border ) 1px solid ;
181162 position : relative ;
163+ display : flex ;
164+ flex-direction : column ;
182165}
183166.editor-inner {
184167 overflow : scroll ;
185- position : absolute ;
186- top : 0 ;
187- left : 0 ;
188- right : 0 ;
189- bottom : 300px ;
168+ flex-grow : 1 ;
190169}
170+
191171#graph {
192172 flex-grow : 1 ;
193173 position : relative ;
@@ -197,7 +177,6 @@ function handleImport() {
197177 padding : 0 ;
198178 display : flex ;
199179 flex-direction : row ;
200- cursor : default ;
201180}
202181.add-data-opt {
203182 padding : 10px 30px ;
0 commit comments