mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 15:33:57 +00:00
favorite editor json tab
This commit is contained in:
@@ -190,11 +190,13 @@ export function registerFileCommands({
|
|||||||
folder,
|
folder,
|
||||||
format,
|
format,
|
||||||
fileExtension,
|
fileExtension,
|
||||||
|
save = true,
|
||||||
execute = false,
|
execute = false,
|
||||||
toggleComment = false,
|
toggleComment = false,
|
||||||
findReplace = false,
|
findReplace = false,
|
||||||
undoRedo = false,
|
undoRedo = false,
|
||||||
}) {
|
}) {
|
||||||
|
if (save) {
|
||||||
registerCommand({
|
registerCommand({
|
||||||
id: idPrefix + '.save',
|
id: idPrefix + '.save',
|
||||||
group: 'save',
|
group: 'save',
|
||||||
@@ -214,6 +216,7 @@ export function registerFileCommands({
|
|||||||
testEnabled: () => getCurrentEditor() != null,
|
testEnabled: () => getCurrentEditor() != null,
|
||||||
onClick: () => saveTabFile(getCurrentEditor(), true, folder, format, fileExtension),
|
onClick: () => saveTabFile(getCurrentEditor(), true, folder, format, fileExtension),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (execute) {
|
if (execute) {
|
||||||
registerCommand({
|
registerCommand({
|
||||||
|
|||||||
125
packages/web/src/tabs/FavoriteEditorTab.svelte
Normal file
125
packages/web/src/tabs/FavoriteEditorTab.svelte
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
<script lang="ts" context="module">
|
||||||
|
let lastFocusedEditor = null;
|
||||||
|
const getCurrentEditor = () =>
|
||||||
|
lastFocusedEditor?.getTabId && lastFocusedEditor?.getTabId() == getActiveTabId() ? lastFocusedEditor : null;
|
||||||
|
|
||||||
|
registerFileCommands({
|
||||||
|
idPrefix: 'favoriteJsonEditor',
|
||||||
|
category: 'Favorite JSON editor',
|
||||||
|
getCurrentEditor,
|
||||||
|
folder: null,
|
||||||
|
format: null,
|
||||||
|
fileExtension: null,
|
||||||
|
|
||||||
|
save: false,
|
||||||
|
findReplace: true,
|
||||||
|
});
|
||||||
|
registerCommand({
|
||||||
|
id: 'favoriteJsonEditor.save',
|
||||||
|
group: 'save',
|
||||||
|
name: 'Save',
|
||||||
|
category: 'Favorite JSON editor',
|
||||||
|
testEnabled: () => getCurrentEditor() != null,
|
||||||
|
onClick: () => getCurrentEditor().save(),
|
||||||
|
});
|
||||||
|
registerCommand({
|
||||||
|
id: 'favoriteJsonEditor.preview',
|
||||||
|
name: 'Preview',
|
||||||
|
category: 'Favorite JSON editor',
|
||||||
|
keyText: 'F5 | Ctrl+Enter',
|
||||||
|
testEnabled: () => getCurrentEditor() != null,
|
||||||
|
onClick: () => getCurrentEditor().preview(),
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script lang="ts">
|
||||||
|
import { get_current_component } from 'svelte/internal';
|
||||||
|
import { getContext } from 'svelte';
|
||||||
|
import registerCommand from '../commands/registerCommand';
|
||||||
|
import { registerFileCommands } from '../commands/stdCommands';
|
||||||
|
|
||||||
|
import AceEditor from '../query/AceEditor.svelte';
|
||||||
|
import useEditorData from '../query/useEditorData';
|
||||||
|
import { getActiveTabId } from '../stores';
|
||||||
|
import invalidateCommands from '../commands/invalidateCommands';
|
||||||
|
import axiosInstance from '../utility/axiosInstance';
|
||||||
|
import { showModal } from '../modals/modalTools';
|
||||||
|
import ErrorMessageModal from '../modals/ErrorMessageModal.svelte';
|
||||||
|
import { openFavorite } from '../appobj/FavoriteFileAppObject.svelte';
|
||||||
|
|
||||||
|
export let tabid;
|
||||||
|
export let savedFile;
|
||||||
|
|
||||||
|
const tabVisible: any = getContext('tabVisible');
|
||||||
|
|
||||||
|
const instance = get_current_component();
|
||||||
|
|
||||||
|
let domEditor;
|
||||||
|
|
||||||
|
$: if ($tabVisible && domEditor) {
|
||||||
|
domEditor?.getEditor()?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getData() {
|
||||||
|
return $editorState.value || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function find() {
|
||||||
|
domEditor.getEditor().execCommand('find');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function replace() {
|
||||||
|
domEditor.getEditor().execCommand('replace');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTabId() {
|
||||||
|
return tabid;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function preview() {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(getData());
|
||||||
|
openFavorite(data);
|
||||||
|
} catch (err) {
|
||||||
|
showModal(ErrorMessageModal, { message: err.message, title: 'Error parsing JSON' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { editorState, editorValue, setEditorData } = useEditorData({ tabid });
|
||||||
|
|
||||||
|
function createMenu() {
|
||||||
|
return [
|
||||||
|
{ command: 'favoriteJsonEditor.save' },
|
||||||
|
{ command: 'favoriteJsonEditor.preview' },
|
||||||
|
{ divider: true },
|
||||||
|
{ command: 'favoriteJsonEditor.find' },
|
||||||
|
{ command: 'favoriteJsonEditor.replace' },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function save() {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(getData());
|
||||||
|
axiosInstance.post('files/save', {
|
||||||
|
file: savedFile,
|
||||||
|
folder: 'favorites',
|
||||||
|
format: 'json',
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
showModal(ErrorMessageModal, { message: err.message, title: 'Error parsing JSON' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<AceEditor
|
||||||
|
value={$editorState.value || ''}
|
||||||
|
menu={createMenu()}
|
||||||
|
on:input={e => setEditorData(e.detail)}
|
||||||
|
on:focus={() => {
|
||||||
|
lastFocusedEditor = instance;
|
||||||
|
invalidateCommands();
|
||||||
|
}}
|
||||||
|
bind:this={domEditor}
|
||||||
|
mode="json"
|
||||||
|
/>
|
||||||
@@ -10,7 +10,7 @@ import * as ChartTab from './ChartTab.svelte';
|
|||||||
import * as MarkdownEditorTab from './MarkdownEditorTab.svelte';
|
import * as MarkdownEditorTab from './MarkdownEditorTab.svelte';
|
||||||
// import MarkdownViewTab from './MarkdownViewTab';
|
// import MarkdownViewTab from './MarkdownViewTab';
|
||||||
// import MarkdownPreviewTab from './MarkdownPreviewTab';
|
// import MarkdownPreviewTab from './MarkdownPreviewTab';
|
||||||
// import FavoriteEditorTab from './FavoriteEditorTab';
|
import * as FavoriteEditorTab from './FavoriteEditorTab.svelte';
|
||||||
import * as QueryDesignTab from './QueryDesignTab.svelte';
|
import * as QueryDesignTab from './QueryDesignTab.svelte';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -26,6 +26,6 @@ export default {
|
|||||||
MarkdownEditorTab,
|
MarkdownEditorTab,
|
||||||
// MarkdownViewTab,
|
// MarkdownViewTab,
|
||||||
// MarkdownPreviewTab,
|
// MarkdownPreviewTab,
|
||||||
// FavoriteEditorTab,
|
FavoriteEditorTab,
|
||||||
QueryDesignTab,
|
QueryDesignTab,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user