@@ -49,9 +49,11 @@ import type { FunctionPlotDatum, FunctionPlotOptions } from "function-plot";
4949import { onMounted , ref , watch } from " vue" ;
5050import { cloneDeep } from " lodash-es" ;
5151import JSON5 from " json5" ;
52- const graphData = ref <(FunctionPlotDatum & { key: number })[]>([
53- { fn: " x^2" , key: 1 },
54- ]);
52+ import base64 from " base-64" ;
53+ import utf8 from " utf8" ;
54+ import { Datum } from " ./consts" ;
55+
56+ const graphData = ref <Datum []>([{ fn: " x^2" , key: 1 }]);
5557const graphWidth = ref (0 ),
5658 graphHeight = ref (0 );
5759const key = ref (0 );
@@ -64,7 +66,34 @@ function handleResize() {
6466 graphHeight .value = shellRef .value .clientHeight ;
6567 }
6668}
69+
70+ function importMapper(item : FunctionPlotDatum ): Datum {
71+ if (item .graphType === " text" )
72+ return {
73+ ... item ,
74+ fnType: " text" ,
75+ key: Math .random (),
76+ };
77+ else
78+ return {
79+ ... item ,
80+ key: Math .random (),
81+ };
82+ }
83+
6784onMounted (() => {
85+ const rawCode = window .location .search .match (/ \? code=(. + )$ / )?.[1 ];
86+ if (rawCode )
87+ try {
88+ const code = utf8 .decode (base64 .decode (rawCode ));
89+ const data = (<FunctionPlotDatum []>JSON5 .parse (code ).data ).map (
90+ importMapper
91+ );
92+ graphData .value = <Datum []>data ;
93+ console .log (code );
94+ console .log (data );
95+ } catch (e ) {}
96+
6897 window .addEventListener (" resize" , handleResize );
6998 handleResize ();
7099 watch (graphData , () => key .value ++ , { deep: true });
@@ -85,10 +114,7 @@ function handleImport() {
85114 const raw = prompt (" 源数据:" );
86115 if (! raw ) return ;
87116 graphData .value =
88- (<FunctionPlotOptions >JSON5 .parse (raw )).data ?.map ((item ) => ({
89- key: Math .random (),
90- ... item ,
91- })) ?? [];
117+ (<FunctionPlotOptions >JSON5 .parse (raw )).data ?.map (importMapper ) ?? [];
92118}
93119 </script >
94120
0 commit comments