Skip to content

Commit 6777fac

Browse files
committed
feat: simple save and load project
1 parent 539b365 commit 6777fac

File tree

12 files changed

+181
-3
lines changed

12 files changed

+181
-3
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
VITE_PROJECT_EMPTY=__NOT_CREATED__
1+
VITE_PROJECT_EMPTY=__NOT_CREATED__
2+
VITE_LOCAL_STORAGE=__BETTER_WRITE__

src/App.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
</template>
55

66
<script setup lang="ts">
7+
import { onMounted } from 'vue'
78
import { useI18n } from 'vue-i18n'
89
import { useStart } from '@/use/start'
10+
import { useIndexedDB } from '@/use/storage/indexed'
911
1012
const { t } = useI18n()
1113
1214
useStart().init()
15+
16+
onMounted(() => {})
1317
</script>

src/components/editor/aside/project/AsideBarProject.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
<AsideProjectNew />
1919
<AsidePageNew v-if="store.state.project.name !== useEnv().projectEmpty()" />
2020
<AsideLine v-if="store.state.project.name !== useEnv().projectEmpty()" />
21+
<AsideLoadProject />
22+
<AsideSaveProject
23+
v-if="store.state.project.name !== useEnv().projectEmpty()"
24+
/>
25+
<AsideLine v-if="store.state.project.name !== useEnv().projectEmpty()" />
2126
<AsideGeneratePDF
2227
v-if="store.state.project.name !== useEnv().projectEmpty()"
2328
/>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<InjectButtonInstance class="wb-aside-button" @click.prevent="onSaveProject">
3+
{{ t('editor.aside.project.load.title') }}
4+
</InjectButtonInstance>
5+
</template>
6+
7+
<script setup lang="ts">
8+
import { useI18n } from 'vue-i18n'
9+
import { useLocalStorage } from '@/use/storage/local'
10+
import { useStore } from 'vuex'
11+
import { nextTick } from 'vue'
12+
13+
const store = useStore()
14+
15+
const onSaveProject = async () => {
16+
const context = useLocalStorage().getProject()
17+
18+
store.commit('project/load', context.project)
19+
await nextTick
20+
21+
store.commit('context/load', store.state.project.pages[0])
22+
}
23+
24+
const { t } = useI18n()
25+
</script>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<InjectButtonInstance class="wb-aside-button" @click.prevent="onSaveProject">
3+
{{ t('editor.aside.project.save.title') }}
4+
</InjectButtonInstance>
5+
</template>
6+
7+
<script setup lang="ts">
8+
import { useI18n } from 'vue-i18n'
9+
import { useLocalStorage } from '@/use/storage/local'
10+
import { useStore } from 'vuex'
11+
12+
const store = useStore()
13+
14+
const onSaveProject = () => {
15+
useLocalStorage().setProject({
16+
project: store.state.project,
17+
editor: store.state.editor,
18+
})
19+
}
20+
21+
const { t } = useI18n()
22+
</script>

src/lang/br.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export default {
2222
},
2323
project: {
2424
title: 'Projeto',
25+
save: {
26+
title: 'Salvar Projeto',
27+
},
28+
load: {
29+
title: 'Carregar Projeto',
30+
},
2531
blocked: 'Crie ou carregue um projeto!!!',
2632
page: {
2733
new: {

src/lang/en.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ export default {
2222
},
2323
project: {
2424
title: 'Project',
25+
save: {
26+
title: 'Save Project',
27+
},
28+
load: {
29+
title: 'Load Project',
30+
},
2531
blocked: 'Create or load a project!!!',
2632
page: {
2733
new: {

src/store/module/project.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ProjectState } from '@/types/project'
22
import { useText } from '@/use/text'
33
import { useFormat } from '@/use/format'
44
import { ContextState, ContextStatePageContent } from '@/types/context'
5-
import { useI18n } from 'vue-i18n'
65

76
export default {
87
namespaced: true,
@@ -18,6 +17,16 @@ export default {
1817
pageLoaded: 0,
1918
} as ProjectState),
2019
mutations: {
20+
load(state: any, payload: ProjectState) {
21+
state.name = payload.name
22+
state.nameRaw = payload.nameRaw
23+
state.version = payload.version
24+
state.totalPagesCreated = payload.totalPagesCreated
25+
state.main = payload.main
26+
state.summary = payload.summary
27+
state.pages = payload.pages
28+
state.pageLoaded = payload.pageLoaded
29+
},
2130
create(state: any, payload: Record<any, any>) {
2231
state.name = useText().kebab(payload.name)
2332
state.nameRaw = payload.name

src/types/project.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ContextState } from '@/types/context'
2+
import { EditorState } from './editor'
23

34
export interface ProjectState {
45
name: string
@@ -10,3 +11,8 @@ export interface ProjectState {
1011
pages: Array<ContextState>
1112
pageLoaded: number
1213
}
14+
15+
export interface ProjectObject {
16+
project: ProjectState
17+
editor: EditorState
18+
}

src/use/env.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ export const useEnv: Callback<any> = () => {
55
return import.meta.env.VITE_PROJECT_EMPTY as string
66
}
77

8-
return { projectEmpty }
8+
const projectLocalStorage = () => {
9+
return import.meta.env.VITE_LOCAL_STORAGE as string
10+
}
11+
12+
return { projectEmpty, projectLocalStorage }
913
}

src/use/storage/indexed.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Callback } from '@/types/utils'
2+
3+
export const useIndexedDB: Callback<any> = () => {
4+
const supports = (): boolean => {
5+
return window.indexedDB ? true : false
6+
}
7+
8+
const open = (
9+
obj: Record<string, any>,
10+
cb?: Callback<any>
11+
): IDBOpenDBRequest => {
12+
if (!supports()) {
13+
alert('kekw')
14+
}
15+
16+
const db = window.indexedDB.open(obj.name, obj.version)
17+
18+
db.onerror = (event: any) => {
19+
console.error(`Database error: ${event.target.errorCode}`)
20+
}
21+
22+
db.onsuccess = (event: any) => {}
23+
24+
;(db as any).onupgradeneeded((event: any) => {
25+
const database = event.target.result
26+
27+
const _store = database.createObjectStore('Contacts', {
28+
autoIncrement: true,
29+
})
30+
31+
let index = _store.createIndex('email', 'email', {
32+
unique: true,
33+
})
34+
35+
const txn = database.transaction('Contacts', 'readwrite')
36+
37+
// get the Contacts object store
38+
const store = txn.objectStore('Contacts')
39+
//
40+
let query = store.put({ email: 'email', test: 'test' })
41+
42+
// handle success case
43+
query.onsuccess = function (event: any) {
44+
console.log(event)
45+
}
46+
47+
// handle the error case
48+
query.onerror = function (event: any) {
49+
console.log(event.target.errorCode)
50+
}
51+
52+
// close the database once the
53+
// transaction completes
54+
txn.oncomplete = function () {
55+
database.close()
56+
}
57+
})
58+
59+
return db
60+
}
61+
62+
const createTables = (obj: Record<any, any>) => {
63+
open(obj).onupgradeneeded
64+
}
65+
66+
return { open }
67+
}

src/use/storage/local.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ProjectObject } from '@/types/project'
2+
import { Callback } from '@/types/utils'
3+
import { useEnv } from '../env'
4+
5+
export const useLocalStorage: Callback<any> = () => {
6+
const set = (obj: ProjectObject, name: string) => {
7+
localStorage.setItem(useEnv().projectLocalStorage(), JSON.stringify(obj))
8+
}
9+
10+
const get = (name: string) => {
11+
return JSON.parse((localStorage as any).getItem(name))
12+
}
13+
14+
const setProject = (obj: ProjectObject) => {
15+
set(obj, useEnv().projectLocalStorage())
16+
}
17+
18+
const getProject = (): ProjectObject => {
19+
return get(useEnv().projectLocalStorage())
20+
}
21+
22+
return { set, get, setProject, getProject }
23+
}

0 commit comments

Comments
 (0)