Skip to content

Commit

Permalink
feat(context): auto save
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Sep 27, 2021
1 parent d76bc4c commit a16abaf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/pages/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

<script setup lang="ts">
import { useKeyboard } from '@/use/keyboard'
import { useLocalStorage } from '@/use/storage/local'
import { onUnmounted } from 'vue'
import { useStore } from 'vuex'
const store = useStore()
useKeyboard().init()
useLocalStorage().onAutoSave(60, store)
onUnmounted(() => {
useKeyboard().destroy()
Expand Down
21 changes: 20 additions & 1 deletion src/use/storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export const useLocalStorage: Callback<any> = (store: Store<any>) => {
})
}

const onAutoSave = (time: number, _: Store<any>) => {
setInterval(() => {
if (_.state.project.name === useEnv().projectEmpty()) return

useLocalStorage().setProject({
project: _.state.project,
editor: _.state.editor,
})
}, parseInt(`${time}000`))
}

const onLoadProject = async () => {
const context = useLocalStorage().getProject()

Expand All @@ -41,5 +52,13 @@ export const useLocalStorage: Callback<any> = (store: Store<any>) => {
store.commit('context/load', store.state.project.pages[0])
}

return { set, get, setProject, getProject, onSaveProject, onLoadProject }
return {
set,
get,
setProject,
getProject,
onSaveProject,
onLoadProject,
onAutoSave,
}
}

0 comments on commit a16abaf

Please sign in to comment.