Skip to content

Commit fb99df9

Browse files
committed
feat(project): auto save option
1 parent a3ad0a4 commit fb99df9

File tree

9 files changed

+75
-11
lines changed

9 files changed

+75
-11
lines changed

src/components/editor/provider/project/ProviderProjectPreferences.vue

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
dark:text-gray-300
4343
justify-between
4444
items-center
45-
w-96
45+
w-full
46+
md:w-1/2
4647
px-2
4748
py-1
4849
"
@@ -51,7 +52,7 @@
5152
<InputSelect v-model="lang" :arr="['Português do Brasil', 'English']" />
5253
</div>
5354
<SwitchGroup>
54-
<div class="flex px-2 items-center w-96 justify-between">
55+
<div class="flex px-2 items-center w-full md:w-1/2 justify-between">
5556
<SwitchLabel
5657
class="
5758
mr-4
@@ -92,22 +93,42 @@
9293
</Switch>
9394
</div>
9495
</SwitchGroup>
96+
<div
97+
class="
98+
flex
99+
font-bold
100+
text-base text-black
101+
dark:text-gray-300
102+
justify-between
103+
items-center
104+
w-full
105+
md:w-1/2
106+
px-2
107+
py-1
108+
"
109+
>
110+
<p>{{ t('editor.aside.configuration.autosave') }}</p>
111+
<InputSelect v-model="auto" :arr="[1, 2, 5, 15, 30, 'never']" />
112+
</div>
95113
</div>
96114
</div>
97115
</template>
98116

99117
<script setup lang="ts">
100118
import { useEditorStore } from '@/store/editor'
101-
import { ref, watch } from 'vue'
119+
import { ref, watch, nextTick } from 'vue'
102120
import { useI18n } from 'vue-i18n'
103121
import { useAbsoluteStore } from '@/store/absolute'
122+
import { useLocalStorage } from '@/use/storage/local'
104123
105124
const ABSOLUTE = useAbsoluteStore()
106-
107125
const EDITOR = useEditorStore()
108126
127+
const local = useLocalStorage()
128+
109129
const { t, locale } = useI18n()
110130
131+
const auto = ref(EDITOR.configuration.auto)
111132
const dark = ref(EDITOR.configuration.dark)
112133
watch(dark, (_dark: boolean) => {
113134
EDITOR.configuration.dark = _dark
@@ -118,6 +139,16 @@
118139
_dark ? (localStorage.theme = 'dark') : localStorage.removeItem('theme')
119140
})
120141
142+
watch(auto, async (_auto) => {
143+
EDITOR.setAutoSave(_auto)
144+
145+
await nextTick
146+
147+
local.onSaveProject().then(() => {
148+
window.location.reload()
149+
})
150+
})
151+
121152
const convert = (iso: string) => {
122153
return (
123154
{

src/components/material/InputSelect.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
const props = defineProps({
115115
modelValue: {
116116
required: true,
117-
type: String,
118117
},
119118
arr: {
120119
required: false,

src/lang/br/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export default {
119119
dark: 'Modo Escuro',
120120
lang: 'Linguagem',
121121
draggable: 'Arrastável',
122+
autosave: 'Salvamento Automático (em minutos)',
122123
},
123124
entity: {
124125
delete: 'Deletar',

src/lang/en/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111
load: 'Load',
1212
save: 'Save',
1313
preferences: 'Preferences',
14+
autosave: 'Auto Save (in minutes)',
1415
},
1516
chapter: {
1617
new: 'New',

src/pages/Editor.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
const keyboard = useKeyboard()
2525
const env = useEnv()
2626
const { t } = useI18n()
27+
const local = useLocalStorage()
2728
29+
local.init()
2830
keyboard.init()
2931
30-
useLocalStorage().onAutoSave(60 * 10)
31-
3232
onUnmounted(() => {
3333
keyboard.destroy()
3434
})
@@ -39,7 +39,7 @@
3939
const _title = computed(() =>
4040
PROJECT.nameRaw === env.projectEmpty() || !CONTEXT.entities[0]
4141
? title.value
42-
: PROJECT.nameRaw + ' - ' + CONTEXT.entities[0]?.raw || ''
42+
: PROJECT.nameRaw + ' - ' + CONTEXT.entities[0]?.raw
4343
)
4444
4545
useHead({

src/store/editor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const useEditorStore = defineStore('editor', {
4848
configuration: {
4949
dark: true,
5050
draggable: false,
51+
auto: 5,
5152
},
5253
actives: {
5354
text: {
@@ -61,6 +62,11 @@ export const useEditorStore = defineStore('editor', {
6162
}
6263
},
6364
actions: {
65+
load(content: EditorState) {
66+
this.styles = content.styles
67+
this.configuration = content.configuration
68+
this.actives = content.actives
69+
},
6470
switchTheme(dark: boolean) {
6571
this.configuration.dark = dark
6672
},
@@ -69,5 +75,8 @@ export const useEditorStore = defineStore('editor', {
6975
this.actives.text.selection.end = payload.end
7076
this.actives.text.selection.start = payload.start
7177
},
78+
setAutoSave(auto: number | 'never') {
79+
this.configuration.auto = auto
80+
},
7281
},
7382
})

src/store/project.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Entity } from '../types/context'
77
import isElectron from 'is-electron'
88
import { useEnv } from '@/use/env'
99
import { useLoggerStore } from './logger'
10+
import { useEditorStore } from './editor'
1011

1112
export const useProjectStore = defineStore('project', {
1213
state: (): ProjectState => {
@@ -30,6 +31,11 @@ export const useProjectStore = defineStore('project', {
3031
},
3132
actions: {
3233
load(payload: ProjectState) {
34+
const logger = useLoggerStore()
35+
const editor = useEditorStore()
36+
37+
editor.$reset()
38+
logger.reset()
3339
this.$reset()
3440

3541
this.name = payload.name
@@ -47,8 +53,10 @@ export const useProjectStore = defineStore('project', {
4753
},
4854
create(payload: Record<any, any>) {
4955
const logger = useLoggerStore()
50-
logger.reset()
56+
const editor = useEditorStore()
5157

58+
editor.$reset()
59+
logger.reset()
5260
this.$reset()
5361

5462
this.name = useText().kebab(payload.name)
@@ -87,6 +95,9 @@ export const useProjectStore = defineStore('project', {
8795
},
8896
createBlank(payload: Record<any, any>) {
8997
const logger = useLoggerStore()
98+
const editor = useEditorStore()
99+
100+
editor.$reset()
90101
logger.reset()
91102

92103
this.$reset()

src/types/editor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface EditorStateConfiguration {
2222
dark: boolean
2323
lang?: boolean
2424
draggable: boolean
25+
auto: number | 'never'
2526
}
2627

2728
export interface EditorStateStyles {

src/use/storage/local.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ export const useLocalStorage = () => {
8181
toast.success(t('toast.project.save'))
8282
}
8383

84-
const onAutoSave = (time: number) => {
84+
const onAutoSave = (time: number | 'never') => {
85+
if (time === 'never' || !time) return
86+
8587
setInterval(() => {
8688
if (PROJECT.name === env.projectEmpty()) return
8789

@@ -97,7 +99,7 @@ export const useLocalStorage = () => {
9799
})
98100

99101
plugin.emit('plugin-auto-save')
100-
}, parseInt(`${time}000`))
102+
}, 1000 * 60 * time)
101103
}
102104

103105
const onLoadProject = async () => {
@@ -115,13 +117,21 @@ export const useLocalStorage = () => {
115117

116118
LOGGER.load(context.logger.content)
117119

120+
await nextTick
121+
122+
EDITOR.load(context.editor)
123+
118124
PDF.load(context.pdf)
119125

120126
ABSOLUTE.aside = true
121127

122128
toast.success(t('toast.project.load'))
123129
}
124130

131+
const init = () => {
132+
onAutoSave(EDITOR.configuration.auto)
133+
}
134+
125135
return {
126136
set,
127137
get,
@@ -130,5 +140,6 @@ export const useLocalStorage = () => {
130140
onSaveProject,
131141
onLoadProject,
132142
onAutoSave,
143+
init,
133144
}
134145
}

0 commit comments

Comments
 (0)