-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathSaveSession.vue
More file actions
95 lines (86 loc) · 2.3 KB
/
SaveSession.vue
File metadata and controls
95 lines (86 loc) · 2.3 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<template>
<v-card>
<v-card-title class="d-flex flex-row align-center">
Saving Session State
</v-card-title>
<v-card-text>
<v-form v-model="valid" @submit.prevent="saveSession">
<v-text-field
v-model="fileName"
hint="The filename to use for the session state file."
label="Session State Filename"
:rules="[validFileName]"
required
id="session-state-filename"
/>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer />
<v-btn
:loading="saving"
color="secondary"
@click="saveSession"
:disabled="!valid"
>
<v-icon class="mr-2">mdi-content-save-all</v-icon>
<span data-testid="save-session-confirm-button">Save</span>
</v-btn>
</v-card-actions>
</v-card>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue';
import { saveAs } from 'file-saver';
import { onKeyDown } from '@vueuse/core';
import { serialize } from '../io/state-file/serialize';
import { useMessageStore } from '../store/messages';
const DEFAULT_FILENAME = 'session.volview.zip';
export default defineComponent({
props: {
close: {
type: Function,
required: true,
},
},
setup(props) {
const fileName = ref('');
const valid = ref(true);
const saving = ref(false);
async function saveSession() {
if (fileName.value.trim().length >= 0) {
saving.value = true;
try {
const blob = await serialize();
saveAs(blob, fileName.value);
props.close();
} catch (err) {
const messageStore = useMessageStore();
messageStore.addError('Failed to save session', {
error: err instanceof Error ? err : new Error(String(err)),
});
} finally {
saving.value = false;
}
}
}
onMounted(() => {
// triggers form validation check so can immediately save with default value
fileName.value = DEFAULT_FILENAME;
});
onKeyDown('Enter', () => {
saveSession();
});
function validFileName(name: string) {
return name.trim().length > 0 || 'Required';
}
return {
saving,
saveSession,
fileName,
validFileName,
valid,
};
},
});
</script>