@@ -3,27 +3,61 @@ import base64 from "base-64";
33import utf8 from "utf8" ;
44import { defineStore } from "pinia" ;
55import { computed , ref } from "vue" ;
6- import { InternalDatum , toInternalDatum , toOriginalDatum } from "./consts" ;
7- import { FunctionPlotDatum } from "function-plot" ;
6+ import {
7+ getNewDatum ,
8+ InternalAnnotation ,
9+ InternalDatum ,
10+ toInternalAnnotation ,
11+ toInternalDatum ,
12+ toOriginalAnnotation ,
13+ toOriginalDatum ,
14+ } from "./consts" ;
15+ import { FunctionPlotOptions } from "function-plot" ;
816
917// Datum define
1018export const useProfile = defineStore ( "profile" , ( ) => {
19+ const importedProfile = ( ( ) => {
20+ const rawCode = window ?. location . search . match ( / \? c o d e = ( .+ ) $ / ) ?. [ 1 ] ;
21+ if ( ! rawCode ) return null ;
22+ try {
23+ const obj = JSON5 . parse (
24+ utf8 . decode ( base64 . decode ( decodeURIComponent ( rawCode ) ) )
25+ ) ;
26+ if ( typeof obj === "object" && obj !== null )
27+ return obj as FunctionPlotOptions ;
28+ else return null ;
29+ } catch ( e ) {
30+ return null ;
31+ }
32+ } ) ( ) ;
1133 const data = ref < InternalDatum [ ] > (
12- ( ( ) => {
13- const rawCode = window ?. location . search . match ( / \? c o d e = ( .+ ) $ / ) ?. [ 1 ] ;
14- if ( rawCode )
15- try {
16- const code = utf8 . decode ( base64 . decode ( decodeURIComponent ( rawCode ) ) ) ;
17- const data = toInternalDatum (
18- ( JSON5 . parse ( code ) . data as FunctionPlotDatum [ ] ) ?? [ ]
19- ) ;
20- return toInternalDatum ( < FunctionPlotDatum [ ] > data ) ;
21- } catch ( e ) { }
22- } ) ( ) ?? [ { fnType : "linear" , graphType : "polyline" , fn : "x^2" , key : 1 } ]
34+ toInternalDatum (
35+ importedProfile ?. data ?? [ { graphType : "polyline" , fn : "x^2" } ]
36+ )
2337 ) ;
24- const getOriginalCopy = ( forExport ?: boolean ) =>
38+ const getOriginalData = ( forExport ?: boolean ) =>
2539 toOriginalDatum ( data . value , forExport ) ;
26- return { data, getOriginalCopy } ;
40+ const addData = ( ) => data . value . push ( getNewDatum ( ) ) ;
41+
42+ const annotations = ref < InternalAnnotation [ ] > (
43+ toInternalAnnotation ( importedProfile ?. annotations ?? [ ] )
44+ ) ;
45+ const getOriginalAnnotaion = ( ) => toOriginalAnnotation ( annotations . value ) ;
46+ const addAnnotation = ( ) =>
47+ annotations . value . push ( {
48+ axis : "y" ,
49+ value : "0" ,
50+ text : "" ,
51+ } ) ;
52+
53+ return {
54+ data,
55+ getOriginalData,
56+ addData,
57+ annotations,
58+ getOriginalAnnotaion,
59+ addAnnotation,
60+ } ;
2761} ) ;
2862
2963// Theme define
0 commit comments