Skip to content

Commit 6ed3119

Browse files
committed
加入从参数导入数据
1 parent 06d3690 commit 6ed3119

File tree

12 files changed

+110
-66
lines changed

12 files changed

+110
-66
lines changed

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
},
1212
"dependencies": {
1313
"@prettier/sync": "^0.5.2",
14+
"base-64": "^1.0.0",
1415
"function-plot": "^2.0.0-0",
1516
"json5": "^2.2.3",
1617
"lodash-es": "^4.17.21",
1718
"prettier": "^3.4.2",
19+
"utf8": "^3.0.0",
1820
"vue": "^3.5.13",
1921
"vue-draggable-plus": "^0.6.0"
2022
},
2123
"devDependencies": {
24+
"@types/base-64": "^1.0.2",
2225
"@types/lodash-es": "^4.17.12",
26+
"@types/utf8": "^3.0.3",
2327
"@vitejs/plugin-vue": "^5.2.1",
2428
"patch-package": "^8.0.0",
2529
"typescript": "~5.6.2",

src/App.vue

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ import type { FunctionPlotDatum, FunctionPlotOptions } from "function-plot";
4949
import { onMounted, ref, watch } from "vue";
5050
import { cloneDeep } from "lodash-es";
5151
import 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 }]);
5557
const graphWidth = ref(0),
5658
graphHeight = ref(0);
5759
const 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+
6784
onMounted(() => {
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

src/components/codeDisplay.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import JSON5 from "json5";
1010
import prettier from "prettier/standalone";
1111
import prettierPluginBabel from "prettier/plugins/babel";
1212
import prettierPluginEstree from "prettier/plugins/estree";
13-
import { FunctionPlotDatum } from "function-plot";
1413
import { ref, watch } from "vue";
14+
import { Datum } from "../consts";
1515
const { dataArr } = defineProps<{
16-
dataArr: (FunctionPlotDatum & { key?: number })[];
16+
dataArr: (Omit<Datum, "key"> & { key?: number })[];
1717
}>();
1818
const formatted = ref("");
1919
watch(

src/components/dataBlock.vue

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
</div>
6666
</template>
6767
<script setup lang="ts">
68-
import type { FunctionPlotDatum } from "function-plot";
69-
import { fnTypeArr, graphTypeArr, inputTypeArr, getFnType } from "../consts";
68+
import { fnTypeArr, graphTypeArr, inputTypeArr, getFnType, Datum } from "../consts";
7069
import { ref, computed } from "vue";
7170
import StrInputs from "./dataBlockInner/strInputs.vue";
7271
import CoordInputs from "./dataBlockInner/coordInputs.vue";
@@ -75,16 +74,10 @@ import CoordArrInputs from "./dataBlockInner/coordArrInputs.vue";
7574
import OptInputs from "./dataBlockInner/optInputs.vue";
7675
7776
const emit = defineEmits(["delete"]);
78-
const dataItem = defineModel<FunctionPlotDatum>();
77+
const dataItem = defineModel<Datum>();
7978
const block = ref<HTMLDivElement>();
8079
const blockFolded = ref(true);
81-
function fnTypeChange(
82-
dataItem:
83-
| (Omit<FunctionPlotDatum, "fnType"> & {
84-
fnType: "text" | FunctionPlotDatum["fnType"];
85-
})
86-
| FunctionPlotDatum
87-
) {
80+
function fnTypeChange(dataItem: Datum) {
8881
inputTypeArr.forEach((key) => delete dataItem[key]);
8982
if (dataItem.fnType === "text") {
9083
dataItem.graphType = "text";

src/components/dataBlockInner/coordArrInputs.vue

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,11 @@
4545
</template>
4646

4747
<script setup lang="ts">
48-
import { FnType } from "../../consts";
49-
import { FunctionPlotDatum } from "function-plot";
48+
import { InputProps } from "../../consts";
5049
import { onMounted, ref, watch } from "vue";
5150
import { VueDraggable } from "vue-draggable-plus";
5251
53-
const { dataItem, fnType } = defineProps<{
54-
dataItem: FunctionPlotDatum;
55-
fnType: FnType;
56-
blockFolded: boolean;
57-
}>();
52+
const { dataItem, fnType } = defineProps<InputProps>();
5853
5954
type PrivateData = {
6055
id: number;

src/components/dataBlockInner/coordInputs.vue

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,12 @@
2525
</template>
2626

2727
<script setup lang="ts">
28-
import { FnType, CoordType } from "../../consts";
29-
import { FunctionPlotDatum } from "function-plot";
28+
import { CoordType, Datum, InputProps } from "../../consts";
3029
31-
const { dataItem, fnType } = defineProps<{
32-
dataItem: FunctionPlotDatum;
33-
fnType: FnType;
34-
blockFolded: boolean;
35-
}>();
30+
const { dataItem, fnType } = defineProps<InputProps>();
3631
3732
function handleCoordInput(
38-
dataItem: FunctionPlotDatum,
33+
dataItem: Datum,
3934
input: CoordType,
4035
index: 0 | 1,
4136
event: Event

src/components/dataBlockInner/optInputs.vue

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,11 @@
1515
</template>
1616

1717
<script setup lang="ts">
18-
import { FnType, OptInput } from "../../consts";
19-
import { FunctionPlotDatum } from "function-plot";
18+
import { Datum, InputProps, OptInput } from "../../consts";
2019
21-
const { dataItem, fnType } = defineProps<{
22-
dataItem: FunctionPlotDatum;
23-
fnType: FnType;
24-
blockFolded: boolean;
25-
}>();
20+
const { dataItem, fnType } = defineProps<InputProps>();
2621
27-
function handleCoordInput(
28-
dataItem: FunctionPlotDatum,
29-
input: OptInput,
30-
event: Event
31-
) {
22+
function handleCoordInput(dataItem: Datum, input: OptInput, event: Event) {
3223
const raw = (<HTMLInputElement>event.target).value;
3324
if (raw === "") {
3425
delete dataItem[input.value];

src/components/dataBlockInner/strInputs.vue

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@
1010
</template>
1111

1212
<script setup lang="ts">
13-
import { FnType } from "../../consts";
14-
import { FunctionPlotDatum } from "function-plot";
15-
const { dataItem, fnType } = defineProps<{
16-
dataItem: FunctionPlotDatum;
17-
fnType: FnType;
18-
blockFolded: boolean;
19-
}>();
13+
import { InputProps } from "../../consts";
14+
const { dataItem, fnType } = defineProps<InputProps>();
2015
</script>
2116

2217
<style>

src/components/dataBlockInner/switchInputs.vue

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@
1010
: (dataItem[input.value] = true)
1111
"
1212
>
13-
<button class="switch" :class="dataItem[input.value] ? 'on' : 'off'"></button>
13+
<button
14+
class="switch"
15+
:class="dataItem[input.value] ? 'on' : 'off'"
16+
></button>
1417
{{ input.label }}
1518
</div>
1619
</template>
1720

1821
<script setup lang="ts">
19-
import { FnType } from "../../consts";
20-
import { FunctionPlotDatum } from "function-plot";
22+
import { InputProps } from "../../consts";
2123
22-
const { dataItem, fnType } = defineProps<{
23-
dataItem: FunctionPlotDatum;
24-
fnType: FnType;
25-
blockFolded: boolean;
26-
}>();
24+
const { dataItem, fnType } = defineProps<InputProps>();
2725
</script>
2826

2927
<style>
@@ -44,7 +42,7 @@ const { dataItem, fnType } = defineProps<{
4442
border-radius: 5px;
4543
position: relative;
4644
border-radius: 10px;
47-
border:none
45+
border: none;
4846
}
4947
.switch.on {
5048
background: var(--c-accent);

0 commit comments

Comments
 (0)