Skip to content

Commit 6b88fbd

Browse files
committed
fix(editor): default inserts
1 parent 0597b35 commit 6b88fbd

File tree

5 files changed

+109
-12
lines changed

5 files changed

+109
-12
lines changed

packages/better-write-app/src/components/page/editor/entity/default/blocks/EditorEntityDefaultHeading.vue

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
class="flex items-center justify-center text-center w-full cursor-text"
55
>
66
<h2
7+
ref="__INPUT__"
78
:contenteditable="true"
89
:spellcheck="true"
910
class="font-bold"
@@ -29,8 +30,9 @@
2930
<script setup lang="ts">
3031
import { useContextStore } from '@/store/context'
3132
import useEmitter from '@/use/emitter'
33+
import { useRaw } from '@/use/raw'
3234
import { Entity } from 'better-write-types'
33-
import { computed, nextTick } from 'vue'
35+
import { computed, nextTick, onMounted, ref } from 'vue'
3436
3537
const props = defineProps<{
3638
entity: Entity
@@ -39,9 +41,39 @@
3941
const CONTEXT = useContextStore()
4042
4143
const emitter = useEmitter()
44+
const raw = useRaw()
45+
46+
const __INPUT__ = ref()
4247
4348
const _index = computed(() => CONTEXT.entities.indexOf(props.entity))
4449
50+
onMounted(() => {
51+
emitter.on(
52+
'entity-text-focus',
53+
async ({ target, position, positionOffset }) => {
54+
if (CONTEXT.entities[target] === props.entity) {
55+
switch (position) {
56+
case 'start':
57+
raw.v2().caret().set(__INPUT__.value, 0)
58+
break
59+
case 'end':
60+
raw.v2().caret().setEnd(__INPUT__.value)
61+
break
62+
case 'offset':
63+
raw
64+
.v2()
65+
.caret()
66+
.set(__INPUT__.value, positionOffset as number)
67+
break
68+
case 'auto':
69+
__INPUT__.value?.focus()
70+
break
71+
}
72+
}
73+
}
74+
)
75+
})
76+
4577
const onEnter = async () => {
4678
CONTEXT.newInPagePosEdit({
4779
entity: props.entity,

packages/better-write-app/src/components/page/editor/entity/default/blocks/EditorEntityDefaultText.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
<div
77
ref="__INPUT__"
88
:class="!isAttached ? 'indent-lg' : ''"
9+
:style="{
10+
paddingBottom: last ? '5rem' : '',
11+
}"
912
class="editable whitespace-pre-line text-justify text-theme-editor-entity-text hover:text-theme-editor-entity-text-hover active:text-theme-editor-entity-text-active"
1013
:spellcheck="true"
1114
:contenteditable="true"
12-
:data-placeholder="
13-
focused
14-
? t('editor.text.placeholder.base', {
15-
prefix: EDITOR.configuration.commands.prefix,
16-
})
17-
: ''
18-
"
15+
:data-placeholder="focused ? t('editor.text.placeholder.base') : ''"
1916
@input="onInput"
2017
@keydown="onKeyboard"
2118
@keypress.enter="onEnter"
@@ -63,6 +60,7 @@
6360
const storage = useStorage()
6461
6562
const _index = computed(() => CONTEXT.entities.indexOf(props.entity))
63+
const last = computed(() => _index.value === CONTEXT.entities.length - 1)
6664
6765
const { focused } = useFocus(__INPUT__)
6866

packages/better-write-app/src/use/listener.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,103 @@
11
import { useAbsoluteStore } from '@/store/absolute'
2+
import { useContextStore } from '@/store/context'
3+
import { useEditorStore } from '@/store/editor'
24
import { useEventListener } from '@vueuse/core'
3-
import { ProjectObject } from 'better-write-types'
5+
import { EntityType, ProjectObject } from 'better-write-types'
6+
import { nextTick } from 'vue'
47
import { useI18n } from 'vue-i18n'
8+
import useEmitter from './emitter'
9+
import { useFactory } from './factory'
510
import { useProject } from './project'
11+
import { useStorage } from './storage/storage'
612
import { useUtils } from './utils'
713

814
export const useListener = () => {
915
const ABSOLUTE = useAbsoluteStore()
16+
const CONTEXT = useContextStore()
17+
const EDITOR = useEditorStore()
1018

1119
const project = useProject()
20+
const factory = useFactory()
1221

1322
const utils = useUtils()
1423
const { t } = useI18n()
24+
const storage = useStorage()
25+
const emitter = useEmitter()
1526

1627
const keyboard = () => {
1728
const start = () => {
1829
useEventListener('keydown', cb)
1930
}
2031

21-
const cb = (e: KeyboardEvent) => {
32+
const cb = async (e: KeyboardEvent) => {
33+
// finder
2234
if (e.ctrlKey && (e.key === 'f' || e.key === 'F')) {
2335
e.preventDefault()
2436
e.stopPropagation()
2537

2638
ABSOLUTE.shortcuts.finder = !ABSOLUTE.shortcuts.finder
39+
40+
return
2741
}
2842

43+
// switcher
2944
if (e.ctrlKey && (e.key === 'h' || e.key === 'H')) {
3045
e.preventDefault()
3146
e.stopPropagation()
3247

3348
ABSOLUTE.shortcuts.switcher = !ABSOLUTE.shortcuts.switcher
49+
50+
return
51+
}
52+
53+
// commands
54+
if (e.ctrlKey) {
55+
const index = EDITOR.actives.entity.index
56+
57+
// text attached
58+
if (
59+
e.key === '1' ||
60+
e.key === '2' ||
61+
e.key === '3' ||
62+
e.key === '6' ||
63+
e.key === '7'
64+
) {
65+
e.preventDefault()
66+
e.stopPropagation()
67+
68+
await storage.normalize()
69+
70+
const value = index + 1
71+
72+
let type: EntityType = 'paragraph'
73+
74+
switch (e.key) {
75+
case '1':
76+
type = 'paragraph'
77+
break
78+
case '2':
79+
type = 'heading-two'
80+
break
81+
case '3':
82+
type = 'heading-three'
83+
break
84+
case '6':
85+
type = 'checkbox'
86+
break
87+
case '7':
88+
type = 'list'
89+
break
90+
}
91+
92+
CONTEXT.insert(factory.entity().create(type), value)
93+
94+
await nextTick
95+
96+
emitter.emit('entity-text-focus', {
97+
target: value,
98+
position: 'auto',
99+
})
100+
}
34101
}
35102
}
36103

packages/better-write-localisation/src/en-US/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default {
139139
},
140140
text: {
141141
placeholder: {
142-
base: `Insert '{prefix}' to display a list of commands.`,
142+
base: `Press or Right-Click to display the options menu.`,
143143
paragraph: 'Paragraph',
144144
headingone: 'H1',
145145
headingtwo: 'H2',

packages/better-write-localisation/src/pt-BR/editor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export default {
136136
},
137137
text: {
138138
placeholder: {
139-
base: `Insira '{prefix}' para exibir a lista de comandos.`,
139+
base: `Pressione ou clique com o botão direito para exibir o menu de opções.`,
140140
paragraph: 'Parágrafo',
141141
headingone: 'H1',
142142
headingtwo: 'H2',

0 commit comments

Comments
 (0)