mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 17:36:01 +00:00
editor context menu, focus fix
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
<script lang="ts">
|
<script lang="ts" context="module">
|
||||||
import { commands } from '../stores';
|
import { commands } from '../stores';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
|
|
||||||
function handleKeyDown(e) {
|
export function handleCommandKeyDown(e) {
|
||||||
let keyText = '';
|
let keyText = '';
|
||||||
if (e.ctrlKey) keyText += 'Ctrl+';
|
if (e.ctrlKey) keyText += 'Ctrl+';
|
||||||
if (e.shiftKey) keyText += 'Shift+';
|
if (e.shiftKey) keyText += 'Shift+';
|
||||||
@@ -20,7 +20,13 @@
|
|||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.split('|')
|
.split('|')
|
||||||
.map(x => x.trim())
|
.map(x => x.trim())
|
||||||
.includes(keyText.toLowerCase())
|
.includes(keyText.toLowerCase()) &&
|
||||||
|
(x.disableHandleKeyText == null ||
|
||||||
|
!x.disableHandleKeyText
|
||||||
|
.toLowerCase()
|
||||||
|
.split('|')
|
||||||
|
.map(x => x.trim())
|
||||||
|
.includes(keyText.toLowerCase()))
|
||||||
);
|
);
|
||||||
|
|
||||||
if (command) {
|
if (command) {
|
||||||
@@ -30,4 +36,4 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:window on:keydown={handleKeyDown} />
|
<svelte:window on:keydown={handleCommandKeyDown} />
|
||||||
|
|||||||
@@ -31,7 +31,13 @@
|
|||||||
|
|
||||||
$: selectedIndex = true ? 0 : filter;
|
$: selectedIndex = true ? 0 : filter;
|
||||||
|
|
||||||
onMount(() => domInput.focus());
|
onMount(() => {
|
||||||
|
const oldFocus = document.activeElement;
|
||||||
|
domInput.focus();
|
||||||
|
return () => {
|
||||||
|
if (oldFocus) oldFocus.focus();
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
$: sortedComands = _.sortBy(
|
$: sortedComands = _.sortBy(
|
||||||
Object.values($commands).filter(x => x.enabled),
|
Object.values($commands).filter(x => x.enabled),
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export interface GlobalCommand {
|
|||||||
showDisabled?: boolean;
|
showDisabled?: boolean;
|
||||||
toolbarName?: string;
|
toolbarName?: string;
|
||||||
toolbarOrder?: number;
|
toolbarOrder?: number;
|
||||||
|
disableHandleKeyText?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function registerCommand(command: GlobalCommand) {
|
export default function registerCommand(command: GlobalCommand) {
|
||||||
|
|||||||
@@ -15,6 +15,9 @@
|
|||||||
import 'ace-builds/src-noconflict/theme-twilight';
|
import 'ace-builds/src-noconflict/theme-twilight';
|
||||||
import 'ace-builds/src-noconflict/ext-searchbox';
|
import 'ace-builds/src-noconflict/ext-searchbox';
|
||||||
import 'ace-builds/src-noconflict/ext-language_tools';
|
import 'ace-builds/src-noconflict/ext-language_tools';
|
||||||
|
import { currentDropDownMenu } from '../stores';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import { handleCommandKeyDown } from '../commands/CommandListener.svelte';
|
||||||
|
|
||||||
const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`;
|
const EDITOR_ID = `svelte-ace-editor-div:${Math.floor(Math.random() * 10000000000)}`;
|
||||||
const dispatch = createEventDispatcher<{
|
const dispatch = createEventDispatcher<{
|
||||||
@@ -40,6 +43,7 @@
|
|||||||
export let mode: string = 'text'; // String
|
export let mode: string = 'text'; // String
|
||||||
export let theme: string = 'github'; // String
|
export let theme: string = 'github'; // String
|
||||||
export let options: any = {}; // Object
|
export let options: any = {}; // Object
|
||||||
|
export let menu;
|
||||||
|
|
||||||
let editor: ace.Editor;
|
let editor: ace.Editor;
|
||||||
let contentBackup: string = '';
|
let contentBackup: string = '';
|
||||||
@@ -54,13 +58,6 @@
|
|||||||
const requireEditorPlugins = () => {};
|
const requireEditorPlugins = () => {};
|
||||||
requireEditorPlugins();
|
requireEditorPlugins();
|
||||||
|
|
||||||
onDestroy(() => {
|
|
||||||
if (editor) {
|
|
||||||
editor.destroy();
|
|
||||||
editor.container.remove();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
$: watchValue(value);
|
$: watchValue(value);
|
||||||
function watchValue(val: string) {
|
function watchValue(val: string) {
|
||||||
if (contentBackup !== val && editor && typeof val === 'string') {
|
if (contentBackup !== val && editor && typeof val === 'string') {
|
||||||
@@ -101,6 +98,17 @@
|
|||||||
resizeOnNextTick();
|
resizeOnNextTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleContextMenu = e => {
|
||||||
|
e.preventDefault();
|
||||||
|
const left = e.pageX;
|
||||||
|
const top = e.pageY;
|
||||||
|
currentDropDownMenu.set({ left, top, items: _.isFunction(menu) ? menu() : menu });
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleKeyDown = (data, hash, keyString, keyCode, event) => {
|
||||||
|
handleCommandKeyDown(event);
|
||||||
|
};
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
editor = ace.edit(EDITOR_ID);
|
editor = ace.edit(EDITOR_ID);
|
||||||
|
|
||||||
@@ -115,6 +123,18 @@
|
|||||||
if (options) {
|
if (options) {
|
||||||
editor.setOptions(options);
|
editor.setOptions(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
editor.container.addEventListener('contextmenu', handleContextMenu);
|
||||||
|
editor.keyBinding.addKeyboardHandler(handleKeyDown);
|
||||||
|
});
|
||||||
|
|
||||||
|
onDestroy(() => {
|
||||||
|
if (editor) {
|
||||||
|
editor.container.removeEventListener('contextmenu', handleContextMenu);
|
||||||
|
editor.keyBinding.removeKeyboardHandler(handleKeyDown);
|
||||||
|
editor.destroy();
|
||||||
|
editor.container.remove();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function setEventCallBacks() {
|
function setEventCallBacks() {
|
||||||
|
|||||||
@@ -30,11 +30,28 @@
|
|||||||
),
|
),
|
||||||
onClick: () => get(currentQuery).kill(),
|
onClick: () => get(currentQuery).kill(),
|
||||||
});
|
});
|
||||||
|
registerCommand({
|
||||||
|
id: 'query.toggleComment',
|
||||||
|
category: 'Query',
|
||||||
|
name: 'Toggle comment',
|
||||||
|
keyText: 'Ctrl+/',
|
||||||
|
disableHandleKeyText: 'Ctrl+/',
|
||||||
|
enabledStore: derived(currentQuery, query => query != null),
|
||||||
|
onClick: () => get(currentQuery).toggleComment(),
|
||||||
|
});
|
||||||
|
registerCommand({
|
||||||
|
id: 'query.formatCode',
|
||||||
|
category: 'Query',
|
||||||
|
name: 'Format code',
|
||||||
|
enabledStore: derived(currentQuery, query => query != null),
|
||||||
|
onClick: () => get(currentQuery).formatCode(),
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { get_current_component } from 'svelte/internal';
|
import { get_current_component } from 'svelte/internal';
|
||||||
import { getContext } from 'svelte';
|
import { getContext } from 'svelte';
|
||||||
|
import sqlFormatter from 'sql-formatter';
|
||||||
|
|
||||||
import { writable, derived, get } from 'svelte/store';
|
import { writable, derived, get } from 'svelte/store';
|
||||||
import registerCommand from '../commands/registerCommand';
|
import registerCommand from '../commands/registerCommand';
|
||||||
@@ -144,6 +161,16 @@
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toggleComment() {
|
||||||
|
domEditor.getEditor().execCommand('togglecomment');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatCode() {
|
||||||
|
const editor = domEditor.getEditor();
|
||||||
|
editor.setValue(sqlFormatter.format(editor.getValue()));
|
||||||
|
editor.clearSelection();
|
||||||
|
}
|
||||||
|
|
||||||
const handleMesageClick = message => {
|
const handleMesageClick = message => {
|
||||||
// console.log('EDITOR', editorRef.current.editor);
|
// console.log('EDITOR', editorRef.current.editor);
|
||||||
if (domEditor.getEditor()) {
|
if (domEditor.getEditor()) {
|
||||||
@@ -163,6 +190,10 @@
|
|||||||
? () => applySqlTemplate(initialArgs.sqlTemplate, $extensions, $$props)
|
? () => applySqlTemplate(initialArgs.sqlTemplate, $extensions, $$props)
|
||||||
: null,
|
: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function createMenu() {
|
||||||
|
return [{ command: 'query.execute' }, { command: 'query.toggleComment' }, { command: 'query.formatCode' }];
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<VerticalSplitter isSplitter={visibleResultTabs}>
|
<VerticalSplitter isSplitter={visibleResultTabs}>
|
||||||
@@ -170,6 +201,7 @@
|
|||||||
<SqlEditor
|
<SqlEditor
|
||||||
engine={$connection && $connection.engine}
|
engine={$connection && $connection.engine}
|
||||||
value={$editorState.value || ''}
|
value={$editorState.value || ''}
|
||||||
|
menu={createMenu()}
|
||||||
on:input={e => setEditorData(e.detail)}
|
on:input={e => setEditorData(e.detail)}
|
||||||
on:focus={() => lastFocusedQuery.set(instance)}
|
on:focus={() => lastFocusedQuery.set(instance)}
|
||||||
bind:this={domEditor}
|
bind:this={domEditor}
|
||||||
|
|||||||
@@ -17,10 +17,24 @@
|
|||||||
if (key.startsWith('archive://')) return 'icon archive';
|
if (key.startsWith('archive://')) return 'icon archive';
|
||||||
return 'icon file';
|
return 'icon file';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registerCommand({
|
||||||
|
id: 'tabs.nextTab',
|
||||||
|
category: 'Tabs',
|
||||||
|
name: 'Next tab',
|
||||||
|
keyText: 'Ctrl+Tab',
|
||||||
|
enabledStore: derived(openedTabs, tabs => tabs.filter(x => !x.closedTime).length >= 2),
|
||||||
|
onClick: () => {
|
||||||
|
const tabs = get(openedTabs).filter(x => x.closedTime == null);
|
||||||
|
if (tabs.length >= 2) setSelectedTab(tabs[tabs.length - 2].tabid);
|
||||||
|
},
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { derived, get } from 'svelte/store';
|
||||||
|
import registerCommand from '../commands/registerCommand';
|
||||||
import FontIcon from '../icons/FontIcon.svelte';
|
import FontIcon from '../icons/FontIcon.svelte';
|
||||||
|
|
||||||
import { currentDatabase, openedTabs } from '../stores';
|
import { currentDatabase, openedTabs } from '../stores';
|
||||||
|
|||||||
Reference in New Issue
Block a user