Skip to content

Commit

Permalink
fix(editor): save sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Oct 30, 2021
1 parent 2e79140 commit cd73202
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 62 deletions.
2 changes: 0 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import { useDestroy } from '@/use/destroy'
import { LoggerPlugin } from './plugin/logger'
const { t } = useI18n()
const start = useStart()
const destroy = useDestroy()
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/main/EditorBaseHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
:divider="true"
:text="t('editor.bar.project.load')"
shortcut="CTRL + P"
@action="local.onLoadProject"
@action="project.onLoadProject"
/>
<EditorHeaderItem
v-if="PROJECT.name !== env.projectEmpty()"
Expand Down
3 changes: 0 additions & 3 deletions src/pages/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import { useProjectStore } from '@/store/project'
import { useEnv } from '@/use/env'
import { useKeyboard } from '@/use/keyboard'
import { useLocalStorage } from '@/use/storage/local'
import { useHead } from '@vueuse/head'
import { computed, onUnmounted } from 'vue'
import { useI18n } from 'vue-i18n'
Expand All @@ -24,9 +23,7 @@
const keyboard = useKeyboard()
const env = useEnv()
const { t } = useI18n()
const local = useLocalStorage()
local.init()
keyboard.init()
onUnmounted(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/store/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const useEditorStore = defineStore('editor', {
configuration: {
dark: true,
draggable: false,
auto: 'never',
auto: 5,
},
actives: {
text: {
Expand Down
3 changes: 3 additions & 0 deletions src/use/destroy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Callback } from '@/types/utils'
import { useLocalStorage } from './storage/local'
import { useProject } from './project'

export const useDestroy = () => {
const local = useLocalStorage()
const project = useProject()

const init: Callback<any> = () => {
project.destroy()
local.onSaveProject()
}

Expand Down
2 changes: 1 addition & 1 deletion src/use/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const useKeyboard = () => {
keyboard.bind(SHORTCUTS.localLoadProject[1], async (e: Event) => {
utils.prevent(e)

local.onLoadProject()
project.onLoadProject()
})
}

Expand Down
64 changes: 63 additions & 1 deletion src/use/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,36 @@ import i18n from '@/lang'
import { useProjectStore } from '@/store/project'
import { useContextStore } from '@/store/context'
import useEmitter from './emitter'
import { useLocalStorage } from './storage/local'
import { useEditorStore } from '@/store/editor'
import { useLoggerStore } from '@/store/logger'
import { usePDFStore } from '@/store/pdf'
import { useAbsoluteStore } from '@/store/absolute'
import { ProjectObject } from '@/types/project'

export const useProject = () => {
const PROJECT = useProjectStore()
const CONTEXT = useContextStore()
const EDITOR = useEditorStore()
const LOGGER = useLoggerStore()
const PDF = usePDFStore()
const ABSOLUTE = useAbsoluteStore()

const toast = useToast()
const emitter = useEmitter()
const local = useLocalStorage()
const { t } = i18n.global

let timer: NodeJS.Timer | null

const init = () => {
timer = local.onAutoSave(EDITOR.configuration.auto)
}

const destroy = () => {
clearInterval(timer as NodeJS.Timer)
}

const create = (project: Record<string, any>) => {
normalize().then(async () => {
project.type === 'blank'
Expand All @@ -23,10 +44,43 @@ export const useProject = () => {

CONTEXT.load(PROJECT.pages[0])

await nextTick

destroy()
init()

toast.success(t('toast.project.create'))
})
}

const onLoadProject = async (context?: ProjectObject) => {
if (!context) context = local.getProject()

PROJECT.load(context.project)

await nextTick

CONTEXT.load(PROJECT.pages[0])

await nextTick

LOGGER.load(context.logger.content)

await nextTick

EDITOR.load(context.editor)

PDF.load(context.pdf)

await nextTick

init()

ABSOLUTE.aside = true

toast.success(t('toast.project.load'))
}

const isBlankProject = () => {
return PROJECT.type === 'blank'
}
Expand All @@ -48,5 +102,13 @@ export const useProject = () => {
await nextTick
}

return { create, isBlankProject, isCreativeProject, normalize }
return {
init,
destroy,
create,
onLoadProject,
isBlankProject,
isCreativeProject,
normalize,
}
}
19 changes: 5 additions & 14 deletions src/use/storage/dropbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import useEmitter from '@/use/emitter'
import isElectron from 'is-electron'
import { useAbsoluteStore } from '@/store/absolute'
import usePlugin from '../plugin/core'
import { useProject } from '../project'
import { ProjectObject } from '../../types/project'

export const useDropbox = () => {
const CONTEXT = useContextStore()
Expand All @@ -27,24 +29,13 @@ export const useDropbox = () => {
const emitter = useEmitter()
const env = useEnv()
const plugin = usePlugin()
const project = useProject()
const { t } = i18n.global

const loadContext = async (context: any) => {
const loadContext = async (context: ProjectObject) => {
if (!context) return

PROJECT.load(context.project)

await nextTick

CONTEXT.load(PROJECT.pages[0])

await nextTick

LOGGER.load(context.logger.content)

PDF.load(context.pdf)

toast.success(t('toast.project.load'))
project.onLoadProject(context)
}

const save = async () => {
Expand Down
44 changes: 5 additions & 39 deletions src/use/storage/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ import { useProjectStore } from '@/store/project'
import { useEditorStore } from '@/store/editor'
import { useLoggerStore } from '@/store/logger'
import { usePDFStore } from '@/store/pdf'
import { useContextStore } from '@/store/context'
import useEmitter from '@/use/emitter'
import isElectron from 'is-electron'
import { useAbsoluteStore } from '@/store/absolute'
import usePlugin from '../plugin/core'

export const useLocalStorage = () => {
const CONTEXT = useContextStore()
const PROJECT = useProjectStore()
const EDITOR = useEditorStore()
const LOGGER = useLoggerStore()
const PDF = usePDFStore()
const ABSOLUTE = useAbsoluteStore()

const toast = useToast()
const env = useEnv()
Expand Down Expand Up @@ -82,8 +78,10 @@ export const useLocalStorage = () => {
}

const onAutoSave = (time: number | 'never') => {
setInterval(() => {
if (PROJECT.name === env.projectEmpty() || time === 'never') return
if (time === 'never') return null

return setInterval(() => {
if (PROJECT.name === env.projectEmpty()) return

setProject({
project: PROJECT.$state,
Expand All @@ -97,37 +95,7 @@ export const useLocalStorage = () => {
})

plugin.emit('plugin-auto-save')
}, 1000 * 60 * (time === 'never' ? Infinity : time))
}

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

if (!context) return

PROJECT.load(context.project)

await nextTick

CONTEXT.load(PROJECT.pages[0])

await nextTick

LOGGER.load(context.logger.content)

await nextTick

EDITOR.load(context.editor)

PDF.load(context.pdf)

ABSOLUTE.aside = true

toast.success(t('toast.project.load'))
}

const init = () => {
onAutoSave(EDITOR.configuration.auto)
}, 1000 * 60 * (time as number))
}

return {
Expand All @@ -136,8 +104,6 @@ export const useLocalStorage = () => {
setProject,
getProject,
onSaveProject,
onLoadProject,
onAutoSave,
init,
}
}

0 comments on commit cd73202

Please sign in to comment.