Skip to content

Commit

Permalink
feat(editor): paste in show entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Nov 2, 2021
1 parent c4c158a commit 69b0ca0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/components/editor/entity/EditorEntityShow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
@keydown="onKeyboard"
@input="onChangeArea"
@click="onClick"
@paste="entity.base().onPaste(props.entity, data, $event)"
/>
</section>
</template>
Expand All @@ -151,6 +152,7 @@
import { useAbsoluteStore } from '@/store/absolute'
import usePlugin from '@/use/plugin/core'
import { useUtils } from '@/use/utils'
import { ID } from '@/types/utils'
const props = defineProps({
entity: {
Expand Down Expand Up @@ -354,6 +356,20 @@
}
})
emitter.on('entity-open-by-index', (index: ID<number>) => {
if (CONTEXT.entities[index] === props.entity) {
onEdit()
return
}
})
emitter.on('entity-scroll-by-index', async (index: ID<number>) => {
if (CONTEXT.entities[index] === props.entity) {
await nextTick
scroll.to(`#entity-${index}`, 'center')
}
})
emitter.on('entity-focus', () => {
if (document.activeElement === input.value) return
Expand Down
6 changes: 4 additions & 2 deletions src/pages/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
project.onLoadProject()
})
window.onbeforeunload = function () {
if (router.currentRoute.value.path === '/') local.onSaveProject()
if (!env.isDev()) {
window.onbeforeunload = function () {
if (router.currentRoute.value.path === '/') local.onSaveProject()
}
}
onUnmounted(() => {
Expand Down
12 changes: 12 additions & 0 deletions src/store/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useEnv } from '../use/env'
import { useFormat } from '../use/format'
import { useUtils } from '../use/utils'
import { useProjectStore } from './project'
import { Entities } from '../types/context'

export const useContextStore = defineStore('context', {
state: (): ContextState => {
Expand Down Expand Up @@ -160,5 +161,16 @@ export const useContextStore = defineStore('context', {
this.entities[index - 1].raw = target.raw + entity.raw
}
},
newInPaste(entities: Entities, initial: Entity) {
const start = this.entities.indexOf(initial)

entities.reverse().forEach((entity: Entity) => {
this.entities = useUtils()
.array()
.insert(this.entities, start + 1, entity)
})

this.removeInPage(initial)
},
},
})
2 changes: 2 additions & 0 deletions src/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ export type Entity = {
updatedAt: string
external?: EntityExternal
}

export type Entities = Array<Entity>
2 changes: 2 additions & 0 deletions src/types/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type VueEmitterName =
| 'entity-input-focus'
| 'entity-input-force-enter'
| 'entity-open'
| 'entity-open-by-index'
| 'entity-scroll-by-index'
| 'entity-close'
| 'entity-focus'
| 'entity-input-raw'
Expand Down
48 changes: 47 additions & 1 deletion src/use/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { useContextStore } from '@/store/context'
import useEmitter from './emitter'
import usePlugin from './plugin/core'
import { useI18n } from 'vue-i18n'
import { useInput } from './input'
import { useFormat } from './format'
import { useFactory } from './factory'
import { Entities } from '../types/context'
import { useStorage } from './storage/storage'

export const useEntity = () => {
const PROJECT = useProjectStore()
Expand All @@ -19,8 +24,12 @@ export const useEntity = () => {
const emitter = useEmitter()
const plugin = usePlugin()
const { t } = useI18n()
const pages = computed(() => PROJECT.pages)
const input = useInput()
const format = useFormat()
const storage = useStorage()
const factory = useFactory()

const pages = computed(() => PROJECT.pages)
const selection = computed(() => EDITOR.actives.text.selection.content)

watch(selection, (_content) => {
Expand Down Expand Up @@ -209,6 +218,42 @@ export const useEntity = () => {
}

const base = () => {
const onPaste = async (entity: Entity, value: string, event: any) => {
if (value !== '') return

event.preventDefault()
event.stopPropagation()

const data = input.pasteText(event)
const entities: Entities = []

const index = CONTEXT.entities.indexOf(entity)

data.forEach(async (raw: string) => {
const normalize = raw.replace(/\s+/g, ' ').trim()

if (normalize) {
const content = factory.entity().create(entity.type)

content.raw = normalize

entities.push(content)
}
})

storage.normalize().then(async () => {
await CONTEXT.newInPaste(entities, entity)
})

await nextTick

const position = index + entities.length - 1

emitter.emit('entity-scroll-by-index', position)

emitter.emit('entity-open-by-index', position)
}

const onUp = async (entity: Entity, index: number) => {
await nextTick

Expand Down Expand Up @@ -314,6 +359,7 @@ export const useEntity = () => {
}

return {
onPaste,
onUp,
onDown,
onUpCursor,
Expand Down

0 comments on commit 69b0ca0

Please sign in to comment.