Skip to content

Commit 086c91b

Browse files
committed
feat: improve editor components with common extensions and submit handling
1 parent 5573947 commit 086c91b

8 files changed

Lines changed: 111 additions & 36 deletions

File tree

packages/frontend/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ const openManageProjectUsersDialog = () => {
391391
@click="selectProject(project.id)"
392392
@contextmenu.prevent="showProjectMenu($event, project)"
393393
>
394-
<span>{{ project.name.slice(0, 2).toUpperCase() }}</span>
394+
<span class="select-none">{{ project.name.slice(0, 2).toUpperCase() }}</span>
395395
</button>
396396

397397
<Button

packages/frontend/src/components/SourcesSidebar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const updateDataSource = async (
9292
<div class="border-r app-border px-0.5 relative h-full min-h-0 flex flex-col sidenav-elements">
9393
<div class="flex w-full justify-between items-center gap-2 p-2 pb-1 pt-0.5 pr-0">
9494
<div class="flex flex-col min-w-0 flex-1 region-drag">
95-
<span class="text-sm opacity-45 truncate max-w-[14rem]">
95+
<span class="text-sm opacity-45 truncate max-w-[14rem] select-none">
9696
{{ workspaceStore.currentProject?.name || 'No project selected' }}
9797
</span>
9898
</div>

packages/frontend/src/components/editors/JsonEditor.vue

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { autocompletion } from '@codemirror/autocomplete'
55
import { json } from '@codemirror/lang-json'
66
import { EditorView } from '@codemirror/view'
77
import { Codemirror } from 'vue-codemirror'
8+
import { commonEditorExtensions, createEditorSubmitExtension } from './editor-extensions.ts'
89
import { starQueryTheme } from './theme-starquery.ts'
910
1011
const code = defineModel<string>({
@@ -22,25 +23,14 @@ const emit = defineEmits<{
2223
submit: []
2324
}>()
2425
25-
const submitShortcut = EditorView.domEventHandlers({
26-
keydown(event) {
27-
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
28-
event.preventDefault()
29-
emit('submit')
30-
return true
31-
}
32-
33-
return false
34-
},
35-
})
36-
3726
const extensions = computed(() => [
3827
json(),
28+
...commonEditorExtensions,
3929
autocompletion(),
4030
EditorState.tabSize.of(2),
4131
EditorView.lineWrapping,
4232
starQueryTheme,
43-
submitShortcut,
33+
createEditorSubmitExtension(() => emit('submit')),
4434
])
4535
4636
const view = shallowRef<EditorView>()

packages/frontend/src/components/editors/SQLEditor.vue

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ import { EditorState } from '@codemirror/state'
55
import { autocompletion } from '@codemirror/autocomplete'
66
import type { SQLNamespace } from '@codemirror/lang-sql'
77
import { sql } from '@codemirror/lang-sql'
8+
import { EditorView } from '@codemirror/view'
9+
import {
10+
commonEditorExtensions,
11+
createEditorSubmitExtension,
12+
createSingleLineEnterExtension,
13+
} from './editor-extensions.ts'
814
import { starQueryTheme } from './theme-starquery.ts'
9-
import { EditorView } from 'codemirror'
1015
import type { DataSourceType } from '@/types/sql'
1116
import { getSqlEditorDialect } from '@/datasources/shared-sql/dialect'
1217
@@ -52,18 +57,6 @@ const dialect = computed(() => {
5257
return getSqlEditorDialect(props.sourceType)
5358
})
5459
55-
const extensions = computed(() => [
56-
sql({
57-
dialect: dialect.value,
58-
schema: props.schema,
59-
defaultTable: props.defaultTable,
60-
defaultSchema: props.defaultSchema,
61-
}),
62-
autocompletion(),
63-
...(props.multiline ? [] : [singleLineExtension, preventNewline]),
64-
starQueryTheme,
65-
])
66-
6760
const view = shallowRef<EditorView>()
6861
const handleReady = (payload: {
6962
view: EditorView
@@ -73,13 +66,30 @@ const handleReady = (payload: {
7366
view.value = payload.view
7467
}
7568
76-
const emit = defineEmits(['enter'])
69+
const emit = defineEmits<{
70+
enter: []
71+
submit: []
72+
}>()
7773
78-
const keydownEvent = (e: KeyboardEvent) => {
79-
if (e.code === 'Enter') {
80-
emit('enter')
81-
}
82-
}
74+
const singleLineExtensions = computed(() => [
75+
singleLineExtension,
76+
preventNewline,
77+
createSingleLineEnterExtension(() => emit('enter')),
78+
])
79+
80+
const extensions = computed(() => [
81+
sql({
82+
dialect: dialect.value,
83+
schema: props.schema,
84+
defaultTable: props.defaultTable,
85+
defaultSchema: props.defaultSchema,
86+
}),
87+
...commonEditorExtensions,
88+
autocompletion(),
89+
createEditorSubmitExtension(() => emit('submit')),
90+
...(props.multiline ? [] : singleLineExtensions.value),
91+
starQueryTheme,
92+
])
8393
8494
const focusEditor = () => {
8595
view.value?.focus()
@@ -99,6 +109,5 @@ defineExpose({
99109
:tab-size="2"
100110
:extensions="extensions"
101111
@ready="handleReady"
102-
@keydown="keydownEvent"
103112
/>
104113
</template>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands'
2+
import { EditorView, keymap } from '@codemirror/view'
3+
4+
export const commonEditorExtensions = [
5+
history(),
6+
keymap.of([indentWithTab, ...defaultKeymap, ...historyKeymap]),
7+
]
8+
9+
function isMacPlatform() {
10+
if (typeof navigator === 'undefined') {
11+
return false
12+
}
13+
14+
const browserNavigator = navigator as Navigator & {
15+
userAgentData?: {
16+
platform?: string
17+
}
18+
}
19+
20+
const platform =
21+
browserNavigator.userAgentData?.platform ||
22+
navigator.platform ||
23+
navigator.userAgent
24+
25+
return /mac/i.test(platform)
26+
}
27+
28+
export function createEditorSubmitExtension(onSubmit: () => void) {
29+
const runSubmit = () => {
30+
onSubmit()
31+
return true
32+
}
33+
34+
return [
35+
keymap.of([
36+
{
37+
key: 'Mod-Enter',
38+
run: runSubmit,
39+
},
40+
{
41+
key: 'Cmd-Enter',
42+
run: runSubmit,
43+
},
44+
]),
45+
EditorView.domEventHandlers({
46+
keydown(event) {
47+
const isSubmitShortcut = isMacPlatform()
48+
? event.metaKey && !event.ctrlKey && (event.key === 'Enter' || event.code === 'NumpadEnter')
49+
: event.ctrlKey && !event.metaKey && (event.key === 'Enter' || event.code === 'NumpadEnter')
50+
51+
if (!isSubmitShortcut) {
52+
return false
53+
}
54+
55+
event.preventDefault()
56+
onSubmit()
57+
return true
58+
},
59+
}),
60+
]
61+
}
62+
63+
export function createSingleLineEnterExtension(onEnter: () => void) {
64+
return keymap.of([
65+
{
66+
key: 'Enter',
67+
run: () => {
68+
onEnter()
69+
return true
70+
},
71+
},
72+
])
73+
}

packages/frontend/src/components/workspace/WorkspaceTabsBar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ const logout = async () => {
177177
v-for="(tab, index) of props.tabs"
178178
:key="tab.id ?? `${tab.type}:${index}`"
179179
:data-tab-index="index"
180-
class="group flex flex-none min-w-[11rem] max-w-[18rem] items-center gap-1 border-r border-neutral-200 px-2 py-1.5 transition-colors dark:border-neutral-800 region-no-drag"
180+
class="group flex flex-none min-w-[11rem] max-w-[18rem] items-center gap-1 border-r border-neutral-200 px-2 py-1.5 transition-colors dark:border-neutral-800 region-no-drag select-none"
181181
:class="
182182
currentTab === index
183183
? 'bg-primary-500/10 text-primary-700 dark:text-primary-300'

packages/frontend/src/datasources/shared-sql/views/SQLQueryView.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ watch(sourceRecord, async (nextSource) => {
229229
:schema="completionSchema"
230230
:default-schema="completionDefaultSchema"
231231
class="w-full"
232+
@submit="runQuery"
232233
/>
233234
</template>
234235
</CollapsiblePanel>

packages/frontend/src/datasources/shared-sql/views/SQLTableView.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ onMounted(async () => {
727727
:default-schema="completionDefaultSchema"
728728
:default-table="props.data.tableName"
729729
@enter="applyWhereClause"
730+
@submit="applyWhereClause"
730731
/>
731732
</div>
732733
<Button
@@ -753,6 +754,7 @@ onMounted(async () => {
753754
:default-schema="completionDefaultSchema"
754755
:default-table="props.data.tableName"
755756
@enter="applySortClause"
757+
@submit="applySortClause"
756758
/>
757759
</div>
758760
<Button

0 commit comments

Comments
 (0)