mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
262 lines
8.0 KiB
262 lines
8.0 KiB
4 years ago
|
<template>
|
||
3 years ago
|
<v-container class="ma-0 pa-0" style="height: calc(100% - 50px)" fluid :style="cssStyle ? cssStyle : ''">
|
||
4 years ago
|
<monaco-editor
|
||
3 years ago
|
ref="editor"
|
||
|
v-model="codeLocal"
|
||
3 years ago
|
style="height: 100%"
|
||
4 years ago
|
class="editor card"
|
||
|
theme="vs-dark"
|
||
3 years ago
|
lang="sql"
|
||
4 years ago
|
:minimap="minimap"
|
||
3 years ago
|
:read-only="readOnly"
|
||
4 years ago
|
:actions="actions"
|
||
|
:tables="tables"
|
||
3 years ago
|
:column-names="columnNames"
|
||
|
:column-name-cbk="columnNameCbk"
|
||
|
@selection="selectionFn"
|
||
|
/>
|
||
4 years ago
|
</v-container>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
3 years ago
|
import sqlFormatter from 'sql-formatter';
|
||
|
import MonacoEditor from './index.js';
|
||
4 years ago
|
|
||
3 years ago
|
const monaco = require('monaco-editor');
|
||
3 years ago
|
export default {
|
||
|
ssr: false,
|
||
|
components: {
|
||
3 years ago
|
MonacoEditor,
|
||
3 years ago
|
// sqlFormatter
|
||
|
},
|
||
|
props: ['code', 'cssStyle', 'readOnly', 'heading', 'tables', 'columnNames', 'columnNameCbk'],
|
||
3 years ago
|
data() {
|
||
3 years ago
|
const vm = this;
|
||
3 years ago
|
return {
|
||
|
codeLocal: `${this.code || ''}`,
|
||
|
selection: null,
|
||
|
minimap: {
|
||
3 years ago
|
enabled: true,
|
||
3 years ago
|
},
|
||
3 years ago
|
actions: [
|
||
|
{
|
||
|
// An unique identifier of the contributed action.
|
||
|
id: 'x-sql-pretify',
|
||
|
// A label of the action that will be presented to the user.
|
||
|
label: 'Format SQL',
|
||
|
// An optional array of keybindings for the action.
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.KEY_L],
|
||
|
// A precondition for this action.
|
||
|
precondition: null,
|
||
|
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
// Method that will be executed when the action is triggered.
|
||
|
// @param editor The editor instance is passed in as a convinience
|
||
|
run(ed) {
|
||
|
vm.pretify();
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
// An unique identifier of the contributed action.
|
||
|
id: 'x-sql-execute',
|
||
|
// A label of the action that will be presented to the user.
|
||
|
label: 'Run Query',
|
||
|
// An optional array of keybindings for the action.
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
|
||
|
// A precondition for this action.
|
||
|
precondition: null,
|
||
|
// A rule to evaluate on top of the precondition in order to dispatch the keybindings.
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
// Method that will be executed when the action is triggered.
|
||
|
// @param editor The editor instance is passed in as a convinience
|
||
|
run(ed) {
|
||
|
vm.$emit('execute', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-run-all',
|
||
|
label: 'Run All',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyMod.Alt | monaco.KeyCode.Enter],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('runAll', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-bookmark',
|
||
|
label: 'Bookmark Query',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_B],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('bookmark', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-search',
|
||
|
label: 'Search Query History',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_P],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('searchHistory', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-toggle-bookmark',
|
||
|
label: 'Show/Hide Bookmarks',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_K],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('toggleBookmark', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-file-open',
|
||
|
label: 'Open File',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_O],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('openFile', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-save-file',
|
||
|
label: 'Save File',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('saveFile', ed);
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
id: 'x-sql-clear-editor',
|
||
|
label: 'Clear Editor',
|
||
|
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_N],
|
||
|
precondition: null,
|
||
|
keybindingContext: null,
|
||
|
contextMenuGroupId: 'navigation',
|
||
|
contextMenuOrder: 1.5,
|
||
|
run(ed) {
|
||
|
vm.$emit('clearEditor', ed);
|
||
|
},
|
||
|
},
|
||
|
],
|
||
|
};
|
||
3 years ago
|
},
|
||
|
computed: {},
|
||
|
watch: {
|
||
3 years ago
|
codeLocal(newValue) {
|
||
3 years ago
|
// INFO: for updating value of prop `code` in parent comp
|
||
|
// console.log("update:code Event Emitted", newValue);
|
||
3 years ago
|
this.$emit('update:code', newValue);
|
||
4 years ago
|
},
|
||
3 years ago
|
code(newValue) {
|
||
3 years ago
|
this.codeLocal = newValue;
|
||
|
},
|
||
3 years ago
|
},
|
||
|
methods: {
|
||
3 years ago
|
selectionFn() {
|
||
3 years ago
|
const editor = this.$refs.editor.getMonaco();
|
||
|
const range = editor.getSelection();
|
||
|
const selectedText = editor.getModel().getValueInRange(range);
|
||
|
this.selection = selectedText;
|
||
|
this.selectionRange = range;
|
||
3 years ago
|
},
|
||
3 years ago
|
pretify() {
|
||
3 years ago
|
const editor = this.$refs.editor.getMonaco();
|
||
4 years ago
|
|
||
3 years ago
|
if (this.selection && this.selectionRange) {
|
||
|
const op = {
|
||
|
identifier: 'prettifySelection',
|
||
|
range: this.selectionRange,
|
||
|
text: sqlFormatter.format(this.selection),
|
||
3 years ago
|
forceMoveMarkers: true,
|
||
|
};
|
||
|
editor.executeEdits('sqlFormatter', [op]);
|
||
|
this.selection = null;
|
||
|
this.selectionRange = null;
|
||
3 years ago
|
} else {
|
||
|
// console.log("selected format before:: ", this.codeLocal);
|
||
|
const op = {
|
||
|
identifier: 'prettifyDoc',
|
||
|
range: editor.getModel().getFullModelRange(),
|
||
|
text: sqlFormatter.format(this.codeLocal || ''),
|
||
3 years ago
|
forceMoveMarkers: true,
|
||
|
};
|
||
|
editor.executeEdits('sqlFormatter', [op]);
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
toggleMiniMap() {
|
||
3 years ago
|
const editor = this.$refs.editor.getMonaco();
|
||
|
this.minimap.enabled = !this.minimap.enabled;
|
||
3 years ago
|
editor.updateOptions({
|
||
|
minimap: {
|
||
3 years ago
|
enabled: this.minimap.enabled,
|
||
|
},
|
||
|
});
|
||
3 years ago
|
},
|
||
3 years ago
|
getCurrentQuery() {
|
||
3 years ago
|
return this.$refs.editor.getCurrentQuery();
|
||
4 years ago
|
},
|
||
3 years ago
|
getAllContent() {
|
||
3 years ago
|
return this.$refs.editor.getAllContent();
|
||
3 years ago
|
},
|
||
3 years ago
|
focus() {
|
||
3 years ago
|
this.$refs.editor.focus();
|
||
|
},
|
||
|
},
|
||
|
};
|
||
4 years ago
|
</script>
|
||
|
|
||
|
<style>
|
||
3 years ago
|
.editor {
|
||
|
/* width: 100%;
|
||
4 years ago
|
height: 800px; */
|
||
3 years ago
|
}
|
||
4 years ago
|
</style>
|
||
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|