Skip to content

Commit a56cca2

Browse files
committed
标线导入导出
1 parent 61be6c1 commit a56cca2

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

src/consts.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { FunctionPlotDatum } from "function-plot";
1+
import type {
2+
FunctionPlotAnnotation,
3+
FunctionPlotDatum,
4+
} from "function-plot";
25
import { cloneDeep } from "lodash-es";
36

47
export type ValueLabel = { value: string; label: string; default?: boolean };
@@ -392,3 +395,31 @@ export const fnTypeArr = [
392395
},
393396
] as const satisfies FnType[];
394397

398+
export type InternalAnnotation = {
399+
axis: "x" | "y";
400+
value: string;
401+
text: string;
402+
};
403+
404+
export function toOriginalAnnotation(items: InternalAnnotation[]) {
405+
return items.map(({ axis, value, text }) => ({
406+
[axis]: Number(value),
407+
...(text ? { text } : {}),
408+
})) as FunctionPlotAnnotation[];
409+
}
410+
411+
export function toInternalAnnotation(items: FunctionPlotAnnotation[]) {
412+
const cloned: InternalAnnotation[] = [];
413+
items.forEach((item) => {
414+
let axis: "x" | "y";
415+
if ("x" in item && typeof item.x === "number") axis = "x";
416+
else axis = "y";
417+
const value = String(item[axis]);
418+
cloned.push({
419+
axis,
420+
value,
421+
text: item.text ?? "",
422+
});
423+
});
424+
return cloned;
425+
}

src/editor/output.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ const formatted = ref("");
4646
watch(
4747
profile,
4848
() => {
49-
const code = JSON5.stringify({ data: profile.getOriginalCopy(true) });
49+
const code = JSON5.stringify({
50+
data: profile.getOriginalData(true),
51+
...(profile.annotations.length
52+
? { annotations: profile.getOriginalAnnotaion() }
53+
: {}),
54+
});
5055
const url =
5156
window.location.href.match(/https?:\/\/[^/]+\//) +
5257
"?code=" +

src/states.ts

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,61 @@ import base64 from "base-64";
33
import utf8 from "utf8";
44
import { defineStore } from "pinia";
55
import { 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
1018
export const useProfile = defineStore("profile", () => {
19+
const importedProfile = (() => {
20+
const rawCode = window?.location.search.match(/\?code=(.+)$/)?.[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(/\?code=(.+)$/)?.[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

Comments
 (0)