From 6a8fe9b4a0c81f4a33b0becac5c7e67e46fdbd38 Mon Sep 17 00:00:00 2001 From: Novout Date: Sun, 7 Nov 2021 20:57:44 -0300 Subject: [PATCH] feat(editor): cursor in new delete entity --- .../editor/entity/EditorEntityShow.vue | 22 ++++++++++- src/types/context.ts | 1 + src/types/emitter.ts | 1 + src/use/pdf.ts | 5 +-- src/use/scroll.ts | 38 ++++++++++++++++++- 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/components/editor/entity/EditorEntityShow.vue b/src/components/editor/entity/EditorEntityShow.vue index 7ab23f829..408015ce4 100644 --- a/src/components/editor/entity/EditorEntityShow.vue +++ b/src/components/editor/entity/EditorEntityShow.vue @@ -79,6 +79,7 @@ const edit = ref(false) const keyboard = ref(false) const commands = ref(false) + const old_raw = ref(props.entity.raw.length) const data = ref('') const input = ref(null) @@ -246,6 +247,7 @@ keyboard: true, selectionInitial: payload?.selectionInitial, switch: payload?.switch, + cursor: payload?.cursor, }) } @@ -254,6 +256,7 @@ keyboard: true, selectionInitial: payload?.selectionInitial, switch: payload?.switch, + cursor: payload?.cursor, }) } }) @@ -385,6 +388,19 @@ edit.value = true await nextTick + + if (options?.cursor) { + // set cursor in max old content position + raw + .v2() + .caret() + .set(input.value as HTMLDivElement, old_raw.value) + + // update old raw + old_raw.value = data.value.length + + await nextTick + } } const onEnter = async () => { @@ -577,7 +593,11 @@ await nextTick - emitter.emit('entity-open', { entity: props.entity, up: true }) + emitter.emit('entity-open', { + entity: props.entity, + up: true, + cursor: true, + }) } else if (e.key === 'ArrowUp') { // to top if (start) { diff --git a/src/types/context.ts b/src/types/context.ts index bdf36b9ab..afdb391f5 100644 --- a/src/types/context.ts +++ b/src/types/context.ts @@ -53,6 +53,7 @@ export interface EntityShowEditOptions { keyboard?: boolean switch?: boolean selectionInitial?: boolean + cursor?: boolean } export interface EntityExternal { diff --git a/src/types/emitter.ts b/src/types/emitter.ts index 072ea8306..b7288a6a7 100644 --- a/src/types/emitter.ts +++ b/src/types/emitter.ts @@ -8,6 +8,7 @@ export interface VueEmitterEntityOpen { up?: boolean selectionInitial?: boolean switch?: boolean + cursor?: boolean } export type VueEmitterName = diff --git a/src/use/pdf.ts b/src/use/pdf.ts index 6764f114b..29e70adf9 100644 --- a/src/use/pdf.ts +++ b/src/use/pdf.ts @@ -529,9 +529,8 @@ export const usePDF = () => { set[s] = PDF.normalize[s] }) - if (PDF.normalize['Roboto']) set['Roboto'] = PDF.normalize['Roboto'] - - (pdfMake as any).fonts = set + if (PDF.normalize['Roboto']) + set['Roboto'] = PDF.normalize['Roboto'](pdfMake as any).fonts = set } const create = (): void => { diff --git a/src/use/scroll.ts b/src/use/scroll.ts index 7a4775e4c..f8ed7d858 100644 --- a/src/use/scroll.ts +++ b/src/use/scroll.ts @@ -1,3 +1,4 @@ +import { ID } from '../types/utils' export const useScroll = () => { const force = (tag: string) => { setTimeout(() => { @@ -24,5 +25,40 @@ export const useScroll = () => { ;(scr as HTMLElement).scrollIntoView() } - return { force, to } + const entity = (id: ID, display?: 'center' | 'top' | 'bottom') => { + const scr = document.querySelector(`#entity-${id}`) + if (display === 'center') { + ;(scr as HTMLElement).scrollIntoView({ + behavior: 'auto', + block: 'center', + inline: 'center', + }) + + return + } + + if (display === 'bottom') { + ;(scr as HTMLElement).scrollIntoView({ + behavior: 'auto', + block: 'end', + inline: 'end', + }) + + return + } + + if (display === 'top') { + ;(scr as HTMLElement).scrollIntoView({ + behavior: 'auto', + block: 'start', + inline: 'start', + }) + + return + } + + ;(scr as HTMLElement).scrollIntoView() + } + + return { force, entity, to } }