Skip to content

Commit

Permalink
feat(logger): general contents
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Oct 28, 2021
1 parent 9131907 commit 5c810d5
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/components/editor/entity/EditorEntityShow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@
edit.value = false
await nextTick
onUpdateContent()
if (data.value !== props.entity.raw && data.value) {
plugin.emit('plugin-entity-create', {
data: data.value,
index: _index.value,
})
}
}
})
})
Expand Down
11 changes: 10 additions & 1 deletion src/components/editor/main/EditorBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
import { useContextStore } from '@/store/context'
import { useProjectStore } from '@/store/project'
import { useEditorStore } from '@/store/editor'
import usePlugin from '@/use/plugin/core'
const CONTEXT = useContextStore()
const PROJECT = useProjectStore()
const EDITOR = useEditorStore()
const emitter = useEmitter()
const project = useProject()
const plugin = usePlugin()
const env = useEnv()
const main = ref<HTMLElement | null>(null)
Expand All @@ -69,11 +71,18 @@
const enterListener = async (content: Entity) => {
CONTEXT.addInPage(content)
await nextTick()
await nextTick
useScroll().force('#edit')
resetListener()
await nextTick
plugin.emit('plugin-entity-create', {
data: content.raw,
index: CONTEXT.entities.indexOf(content),
})
}
const resetListener = () => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/editor/main/EditorBaseHeader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@
@action="page.onDeletePage"
/>
<EditorHeaderItem
:text="t('editor.bar.chapter.down')"
:text="t('editor.bar.chapter.up')"
shortcut=""
@action="page.onDownPage"
@action="page.onUpPage"
/>
<EditorHeaderItem
:text="t('editor.bar.chapter.up')"
:text="t('editor.bar.chapter.down')"
shortcut=""
@action="page.onUpPage"
@action="page.onDownPage"
/>
</template>
</EditorHeaderButton>
Expand Down
10 changes: 9 additions & 1 deletion src/lang/br/plugin/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export default {
on: {
dropbox: {
save: {
success: 'SALVO NO DROPBOX COM SUCESSO!',
error: 'NÃO FOI POSSÍVEL SALVAR NO DROPBOX!',
},
},
entity: {
inputFirst: '<{arguments}> INSERIDO NA <{index}> POSIÇÃO.',
inputFirst: '<{arguments}> INSERIDO NA <{index}> POSIÇÃO.',
create: '<{data}> FOI INSERIDO NA <{index}> POSIÇÃO.',
edit: 'POSIÇÃO <{index}> ATUALIZADO PARA <{arguments}>.',
delete: 'A POSIÇÃO <{index}> FOI DELETADA!',
swap: 'POSIÇÃO <{index}> TROCADA COM A POSIÇÃO {target}.',
Expand All @@ -10,6 +17,7 @@ export default {
new: 'PÁGINA DE NÚMERO <{index}> CRIADA!',
delete: 'PÁGINA <{index}> FOI DELETADA',
swap: 'PÁGINA <{index}> FOI TROCADA COM A DE POSIÇÃO {target}.',
autosave: 'PROJETO SALVO AUTOMATICAMENTE COM SUCESSO!',
},
},
}
8 changes: 8 additions & 0 deletions src/lang/en/plugin/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export default {
on: {
dropbox: {
save: {
success: 'SUCCESSFULLY SAVED IN DROPBOX!',
error: 'UNABLE TO SAVE TO DROPBOX!',
},
},
entity: {
inputFirst: '<{arguments}> INSERTED IN <{index}> POSITION.',
create: '<{data}> HAS BEEN INSERTED IN THE <{index}> POSITION.',
edit: 'POSITION <{index}> UPDATED TO <{arguments}>.',
delete: 'POSITION <{index}> HAS BEEN DELETED!',
swap: 'POSITION <{index}> EXCHANGED WITH POSITION {destination}.',
Expand All @@ -10,6 +17,7 @@ export default {
new: 'PAGE NUMBER <{index}> CREATED!',
delete: 'PAGE <{index}> HAS BEEN DELETED',
swap: 'PAGE <{index}> HAS BEEN CHANGED WITH POSITION {target}.',
autosave: 'PROJECT AUTOMATICALLY SAVED SUCCESSFUL!',
},
},
}
38 changes: 38 additions & 0 deletions src/plugin/core/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PluginLoggerDefault,
PluginLoggerEntitySwapper,
PluginContentOn,
PluginCode,
} from '@/types/plugin/on'

export const PluginEntityInputInitial = (
Expand Down Expand Up @@ -57,6 +58,19 @@ export const PluginEntitySwapper = (
})
}

export const PluginEntityCreate = (
emitter: PluginEmitter,
content: PluginContentOn
) => {
emitter.on('plugin-entity-create', (obj: PluginLoggerDefault) => {
if (!obj) return

const created = content[0]

created && created(obj)
})
}

export const PluginProjectPageNew = (
emitter: PluginEmitter,
content: PluginContentOn
Expand Down Expand Up @@ -95,3 +109,27 @@ export const PluginProjectPageSwap = (
created && created(item)
})
}

export const PluginAutoSave = (
emitter: PluginEmitter,
content: PluginContentOn
) => {
emitter.on('plugin-auto-save', () => {
const created = content[0]

created && created()
})
}

export const PluginDropboxSave = (
emitter: PluginEmitter,
content: PluginContentOn
) => {
emitter.on('plugin-dropbox-save', (type: PluginCode) => {
const created = content[0]
const err = content[1]

if (type === 'success') created && created()
if (type === 'error') err && err()
})
}
23 changes: 22 additions & 1 deletion src/plugin/logger/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PluginEmitter, PluginStores } from '@/types/plugin/core'
import { PluginEntityDelete, PluginEntitySwapper } from '../core/on'
import {
PluginEntityDelete,
PluginEntitySwapper,
PluginEntityCreate,
} from '../core/on'
import { LoggerContent } from '@/types/logger'
import { useFormat } from '@/use/format'
import { useI18n } from 'vue-i18n'
Expand All @@ -15,6 +19,21 @@ export const PluginLoggerActions = (
const format = useFormat()
const { t } = useI18n()

PluginEntityCreate(emitter, [
(obj: PluginLoggerDefault) => {
stores.LOGGER.add({
type: 'editor',
method: 'info',
arguments: t('plugin.logger.on.entity.create', {
data: obj.data,
index: obj.index,
}),
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginEntityDelete(emitter, [
(index: number) => {
stores.LOGGER.add({
Expand All @@ -26,6 +45,7 @@ export const PluginLoggerActions = (
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginEntitySwapper(emitter, [
Expand All @@ -40,5 +60,6 @@ export const PluginLoggerActions = (
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])
}
13 changes: 11 additions & 2 deletions src/plugin/logger/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Plugin, PluginEmitter, PluginStores } from '@/types/plugin/core'
import {
Plugin,
PluginDefines,
PluginEmitter,
PluginStores,
} from '@/types/plugin/core'
import { PluginLoggerActions } from './actions'
import { PluginLoggerProject } from './project'

export const LoggerPlugin = (): Plugin => {
const defines = {
name: 'logger',
} as PluginDefines

const init = (emitter: PluginEmitter, stores: PluginStores) => {
PluginLoggerActions(emitter, stores)
PluginLoggerProject(emitter, stores)
}

return { init }
return { init, defines }
}
36 changes: 36 additions & 0 deletions src/plugin/logger/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
PluginProjectPageNew,
PluginProjectPageDelete,
PluginProjectPageSwap,
PluginAutoSave,
PluginDropboxSave,
} from '../core/on'
import { LoggerContent } from '@/types/logger'
import { useFormat } from '@/use/format'
Expand All @@ -29,6 +31,7 @@ export const PluginLoggerProject = (
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginProjectPageDelete(emitter, [
Expand All @@ -42,6 +45,7 @@ export const PluginLoggerProject = (
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginProjectPageSwap(emitter, [
Expand All @@ -62,5 +66,37 @@ export const PluginLoggerProject = (
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginAutoSave(emitter, [
() => {
stores.LOGGER.add({
type: 'project',
method: 'log',
arguments: t('plugin.logger.on.project.autosave'),
createdAt: format.actually(),
} as LoggerContent)
},
() => {},
])

PluginDropboxSave(emitter, [
() => {
stores.LOGGER.add({
type: 'project',
method: 'log',
arguments: t('plugin.logger.on.dropbox.save.success'),
createdAt: format.actually(),
} as LoggerContent)
},
() => {
stores.LOGGER.add({
type: 'project',
method: 'error',
arguments: t('plugin.logger.on.dropbox.save.error'),
createdAt: format.actually(),
} as LoggerContent)
},
])
}
8 changes: 8 additions & 0 deletions src/types/plugin/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import { ShortcutsState } from '../shortcuts'
export type PluginEmitterName =
| 'plugin-input-watch-initial'
| 'plugin-input-watch-last'
| 'plugin-entity-create'
| 'plugin-entity-delete'
| 'plugin-entity-swap'
| 'plugin-project-page-new'
| 'plugin-project-page-delete'
| 'plugin-project-page-swap'
| 'plugin-auto-save'
| 'plugin-dropbox-save'
export interface PluginEmitter {
on: (name: PluginEmitterName, callback: (...c: any) => any) => void
emit: (name: PluginEmitterName, ...c: any) => void
Expand Down Expand Up @@ -49,7 +52,12 @@ export interface PluginStores {
SHORTCUTS: PluginStore<'shortcuts', ShortcutsState>
}

export interface PluginDefines {
name: string
}

export interface Plugin {
defines: PluginDefines
init: (emitter: PluginEmitter, stores: PluginStores) => void
}
export type Plugins = Array<Plugin>
Expand Down
3 changes: 3 additions & 0 deletions src/types/plugin/on.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type PluginCreated<T = void> = (...s: Array<any>) => T
export type PluginError<T = void> = (...s: Array<any>) => T
export type PluginCode = 'success' | 'warning' | 'error'
export interface PluginContentOn extends Array<PluginCreated> {
0: PluginCreated
1: PluginError
}

export interface PluginLoggerDefault {
Expand Down
6 changes: 6 additions & 0 deletions src/use/storage/dropbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useAuthStore } from '@/store/auth'
import useEmitter from '@/use/emitter'
import isElectron from 'is-electron'
import { useAbsoluteStore } from '@/store/absolute'
import usePlugin from '../plugin/core'

export const useDropbox = () => {
const CONTEXT = useContextStore()
Expand All @@ -25,6 +26,7 @@ export const useDropbox = () => {
const toast = useToast()
const emitter = useEmitter()
const env = useEnv()
const plugin = usePlugin()
const { t } = i18n.global

const loadContext = async (context: any) => {
Expand Down Expand Up @@ -79,6 +81,7 @@ export const useDropbox = () => {
})
.then(() => {
toast.success(t('toast.project.save'))
plugin.emit('plugin-dropbox-save', 'success')
})
.catch(() => {
dbx
Expand All @@ -93,13 +96,16 @@ export const useDropbox = () => {
})
.then(() => {
toast.success(t('toast.project.save'))
plugin.emit('plugin-dropbox-save', 'success')
})
.catch(() => {
toast.error(t('toast.project.error'))
plugin.emit('plugin-dropbox-save', 'error')
})
})
.catch(() => {
toast.error(t('toast.project.error'))
plugin.emit('plugin-dropbox-save', 'error')
})
})
}
Expand Down
Loading

0 comments on commit 5c810d5

Please sign in to comment.