-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport.vue
More file actions
68 lines (63 loc) · 1.78 KB
/
import.vue
File metadata and controls
68 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<template>
<s-dialog>
<s-tooltip class="data-import" slot="trigger">
<s-fab slot="trigger">
<SIconImport />
</s-fab>
{{ t("buttons.import") }}
</s-tooltip>
<div slot="headline">{{ t("title.source") }}</div>
<s-text-field
slot="text"
label="JSON5 / JSON"
multiLine
style="min-height: 180px; min-width: 40vw"
v-model.lazy="importStr"
class="monospace"
></s-text-field>
<s-button slot="action" type="text">
{{ t("buttons.cancel") }}
</s-button>
<s-button slot="action" type="text" @click="handleImport">
{{ t("buttons.confirm") }}
</s-button>
</s-dialog>
</template>
<script setup lang="ts">
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import SIconImport from "@/ui/icons/import.vue";
import { useProfile } from "@/states";
const profile = useProfile();
import { ref } from "vue";
import JSON5 from "json5";
import type { FunctionPlotDatum } from "function-plot";
import { toInternalDatum } from "@/consts";
const importStr = ref("");
import { Snackbar } from "sober";
function handleImport() {
if (importStr.value !== "") {
try {
const parsed = JSON5.parse(importStr.value);
if (typeof parsed !== "object" || parsed === null) throw null;
if (typeof parsed.data !== "object" || !Array.isArray(parsed.data))
throw null;
const newData = toInternalDatum(
(JSON5.parse(importStr.value).data as FunctionPlotDatum[]) ?? []
);
profile.data = [];
profile.data = newData;
Snackbar.builder({
text: t("title.importSuccess"),
type: "success",
});
} catch (e) {
Snackbar.builder({
text: t("title.importFail"),
type: "error",
});
}
importStr.value = "";
}
}
</script>