Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(frontend): embedded sql editor in issue detail page (#1507)
Liu Jib8194d344de3
diff --git a/frontend/src/components/Issue/IssueDescriptionPanel.vue b/frontend/src/components/Issue/IssueDescriptionPanel.vueindex 24396a5e7a..1dc6e90340 100644--- a/frontend/src/components/Issue/IssueDescriptionPanel.vue+++ b/frontend/src/components/Issue/IssueDescriptionPanel.vue@@ -43,7 +43,7 @@ref="editDescriptionTextArea"v-model="state.editDescription":rows="create ? 10 : 5"- class="mt-2 w-full resize-none whitespace-pre-wrap border-white focus:border-white outline-none"+ class="mt-2 mx-[2px] w-full resize-none whitespace-pre-wrap border-white focus:border-white outline-none":class="state.editing ? 'focus:ring-control focus-visible:ring-2' : ''":style="state.editingdiff --git a/frontend/src/components/Issue/IssueDetailLayout.vue b/frontend/src/components/Issue/IssueDetailLayout.vueindex 1278250dbf..8652087876 100644--- a/frontend/src/components/Issue/IssueDetailLayout.vue+++ b/frontend/src/components/Issue/IssueDetailLayout.vue@@ -65,14 +65,16 @@tabindex="-1"><div class="flex max-w-3xl mx-auto px-6 lg:max-w-full">- <div class="flex flex-col flex-1 lg:flex-row-reverse lg:col-span-2">+ <div+ class="flex flex-col flex-1 lg:flex-row-reverse lg:col-span-2 overflow-x-hidden"+ ><div- class="py-6 lg:pl-4 lg:w-96 xl:w-112 lg:border-l lg:border-block-border overflow-hidden"+ class="py-6 lg:pl-4 lg:w-72 xl:w-96 lg:border-l lg:border-block-border overflow-hidden">- <IssueSidebar :database="database" :instance="instance" />+ <IssueSidebar :database="selectedDatabase" :instance="instance" /></div><div class="lg:hidden border-t border-block-border" />- <div class="w-full py-4 pr-4">+ <div class="w-full lg:w-auto lg:flex-1 py-4 pr-4 overflow-x-hidden"><section v-if="showIssueTaskStatementPanel" class="border-b mb-4"><div v-if="!create" class="mb-4"><TaskCheckBar@@ -127,7 +129,6 @@ import TaskCheckBar from "./TaskCheckBar.vue";import type {Issue,IssueCreate,- Database,Instance,Task,TaskDatabaseSchemaUpdatePayload,@@ -137,7 +138,6 @@ import type {import { defaultTemplate, templateForType } from "@/plugins";import {featureToRef,- useDatabaseStore,useInstanceStore,useProjectStore,useTaskStore,@@ -171,7 +171,6 @@ const route = useRoute();const taskStore = useTaskStore();const projectStore = useProjectStore();-const databaseStore = useDatabaseStore();const create = computed(() => props.create);const issue = computed(() => props.issue);@@ -184,6 +183,7 @@ const {createIssue,selectedStage,selectedTask,+ selectedDatabase,selectStageOrTask,selectTask,taskStatusOfStage,@@ -251,22 +251,11 @@ const showIssueTaskStatementPanel = computed(() => {return TaskTypeWithStatement.includes(task.type);});-const database = computed((): Database | undefined => {- if (props.create) {- const databaseId = (selectedTask.value as TaskCreate).databaseId;- if (databaseId) {- return databaseStore.getDatabaseById(databaseId);- }- return undefined;- }- return (selectedTask.value as Task).database;-});-const instance = computed((): Instance => {if (props.create) {// If database is available, then we derive the instance from database because we always fetch database's instance.- if (database.value) {- return database.value.instance;+ if (selectedDatabase.value) {+ return selectedDatabase.value.instance;}return useInstanceStore().getInstanceById((selectedTask.value as TaskCreate).instanceId@@ -346,6 +335,7 @@ provideIssueLogic(template: issueTemplate,selectedStage,selectedTask,+ selectedDatabase,isTenantMode,isGhostMode,isPITRMode,diff --git a/frontend/src/components/Issue/IssueTaskStatementPanel.vue b/frontend/src/components/Issue/IssueTaskStatementPanel.vueindex e9d765b6fd..d5e3f6e268 100644--- a/frontend/src/components/Issue/IssueTaskStatementPanel.vue+++ b/frontend/src/components/Issue/IssueTaskStatementPanel.vue@@ -76,65 +76,46 @@</div></div><label class="sr-only">{{ $t("common.sql-statement") }}</label>- <template v-if="state.editing">- <textarea- ref="editStatementTextArea"- v-model="state.editStatement"- class="whitespace-pre-wrap mt-2 w-full resize-none border-white focus:border-white outline-none"- :class="state.editing ? 'focus:ring-control focus-visible:ring-2' : ''"- :placeholder="$t('issue.add-sql-statement')"- @input="- (e) => {- sizeToFit(e.target as HTMLTextAreaElement);- // When creating the issue, we update the in-memory state.- if (create) {- updateStatement( state.editStatement);- }- }- "- @focus="- (e) => {- sizeToFit(e.target as HTMLTextAreaElement);- }+ <div+ class="whitespace-pre-wrap mt-2 w-full overflow-hidden"+ :class="state.editing ? 'border-t border-x' : 'border-t border-x'"+ >+ <EmbeddedMonacoEditor+ :value="state.editing ? state.editStatement : statement"+ :database-list="databaseList"+ :table-list="tableList"+ :readonly="!state.editing"+ :min-height="+ state.editing ? EDITOR_MIN_HEIGHT.EDITABLE : EDITOR_MIN_HEIGHT.READONLY"- ></textarea>- </template>- <!-- Margin value is to prevent flickering when switching between edit/non-edit mode -->- <div v-else style="margin-left: 5px; margin-top: 8.5px; margin-bottom: 31px">- <highlight-code-block- v-if="statement"- :code="statement"- class="whitespace-pre-wrap"+ :max-height="EDITOR_MAX_HEIGHT"+ @change="onStatementChange"/>- <div v-else-if="create" class="ml-2 text-control-light">- {{ $t("issue.add-sql-statement") }}- </div>- <div v-else class="ml-2 text-control-light">None</div></div></template><script lang="ts">-import {- nextTick,- onMounted,- onUnmounted,- ref,- reactive,- watch,- defineComponent,- computed,-} from "vue";-import { sizeToFit } from "@/utils";-import { useUIStateStore } from "@/store";+import { onMounted, reactive, watch, defineComponent, computed } from "vue";+import { useTableStore, useUIStateStore } from "@/store";import { useIssueLogic } from "./logic";+import EmbeddedMonacoEditor from "../MonacoEditor/EmbeddedMonacoEditor.vue";interface LocalState {editing: boolean;editStatement: string;}+const EDITOR_MIN_HEIGHT = {+ READONLY: 0, // not limited to keep the UI compact+ EDITABLE: 120, // ~= 6 lines, a reasonable size to start writing SQL+};+const EDITOR_MAX_HEIGHT = 360; // ~= 20 lines, not too high for most screen size+export default defineComponent({name: "IssueTaskStatementPanel",+ components: {+ EmbeddedMonacoEditor,+ },props: {sqlHint: {required: false,@@ -152,8 +133,6 @@ export default defineComponent({applyStatementToOtherStages,} = useIssueLogic();- const editStatementTextArea = ref();-const uiStateStore = useUIStateStore();const state = reactive<LocalState>({@@ -167,26 +146,14 @@ export default defineComponent({uiStateStore.setIssueFormatStatementOnSave(value),});- const resizeTextAreaHandler = () => {- if (state.editing) {- sizeToFit(editStatementTextArea.value);- }- };+ const { databaseList, tableList } = useDatabaseAndTableList();onMounted(() => {- window.addEventListener("resize", resizeTextAreaHandler);if (create.value) {state.editing = true;- nextTick(() => {- sizeToFit(editStatementTextArea.value);- });}});- onUnmounted(() => {- window.removeEventListener("resize", resizeTextAreaHandler);- });-// Reset the edit state after creating the issue.watch(create, (curNew, prevNew) => {if (!curNew && prevNew) {@@ -196,16 +163,11 @@ export default defineComponent({watch(statement, (cur) => {state.editStatement = cur;- nextTick(() => sizeToFit(editStatementTextArea.value));});const beginEdit = () => {state.editStatement = statement.value;state.editing = true;- nextTick(() => {- sizeToFit(editStatementTextArea.value);- editStatementTextArea.value.focus();- });};const saveEdit = () => {@@ -219,20 +181,59 @@ export default defineComponent({state.editing = false;};+ const onStatementChange = (value: string) => {+ state.editStatement = value;+ if (create.value) {+ // If we are creating an issue, emit the event immediately when every+ // time the user types.+ updateStatement(state.editStatement);+ }+ };+return {create,allowEditStatement,statement,+ databaseList,+ tableList,updateStatement,allowApplyStatementToOtherStages,applyStatementToOtherStages,- editStatementTextArea,formatOnSave,state,beginEdit,saveEdit,cancelEdit,+ onStatementChange,+ EDITOR_MIN_HEIGHT,+ EDITOR_MAX_HEIGHT,};},});++const useDatabaseAndTableList = () => {+ const { selectedDatabase } = useIssueLogic();+ const tableStore = useTableStore();++ const databaseList = computed(() => {+ if (selectedDatabase.value) return [selectedDatabase.value];+ return [];+ });++ watch(+ databaseList,+ (list) => {+ list.forEach((db) => tableStore.fetchTableListByDatabaseId(db.id));+ },+ { immediate: true }+ );++ const tableList = computed(() => {+ return databaseList.value+ .map((item) => tableStore.getTableListByDatabaseId(item.id))+ .flat();+ });++ return { databaseList, tableList };+};</script>diff --git a/frontend/src/components/Issue/logic/IssueLogic.ts b/frontend/src/components/Issue/logic/IssueLogic.tsindex 6334257da1..e9151d3a79 100644--- a/frontend/src/components/Issue/logic/IssueLogic.ts+++ b/frontend/src/components/Issue/logic/IssueLogic.ts@@ -1,5 +1,6 @@import { IssueTemplate } from "@/plugins";import {+ Database,Issue,IssueCreate,IssuePatch,@@ -25,6 +26,7 @@ type IssueLogic = {// statesselectedStage: Ref<Stage | StageCreate>;selectedTask: Ref<Task | TaskCreate>;+ selectedDatabase: Ref<Database | undefined>;// ui state logicisTenantMode: Ref<boolean>;diff --git a/frontend/src/components/Issue/logic/base.ts b/frontend/src/components/Issue/logic/base.tsindex 7fd87dd9a0..f6041e2d5d 100644--- a/frontend/src/components/Issue/logic/base.ts+++ b/frontend/src/components/Issue/logic/base.ts@@ -21,7 +21,7 @@ import {stageSlug,taskSlug,} from "@/utils";-import { useIssueStore, useProjectStore } from "@/store";+import { useDatabaseStore, useIssueStore, useProjectStore } from "@/store";import { flattenTaskList, TaskTypeWithStatement } from "./common";export const useBaseIssueLogic = (params: {@@ -33,6 +33,7 @@ export const useBaseIssueLogic = (params: {const router = useRouter();const issueStore = useIssueStore();const projectStore = useProjectStore();+ const databaseStore = useDatabaseStore();const project = computed((): Project => {if (create.value) {@@ -145,6 +146,17 @@ export const useBaseIssueLogic = (params: {return taskList[0];});+ const selectedDatabase = computed(() => {+ if (create.value) {+ const databaseId = (selectedTask.value as TaskCreate).databaseId;+ if (databaseId) {+ return databaseStore.getDatabaseById(databaseId);+ }+ return undefined;+ }+ return (selectedTask.value as Task).database;+ });+const isTenantMode = computed((): boolean => {if (project.value.tenantMode !== "TENANT") return false;const { type } = issue.value;@@ -203,6 +215,7 @@ export const useBaseIssueLogic = (params: {createIssue,selectedStage,selectedTask,+ selectedDatabase,selectStageOrTask,selectTask,taskStatusOfStage,diff --git a/frontend/src/components/MonacoEditor/EmbeddedMonacoEditor.vue b/frontend/src/components/MonacoEditor/EmbeddedMonacoEditor.vuenew file mode 100644index 0000000000..f27187d270--- /dev/null+++ b/frontend/src/components/MonacoEditor/EmbeddedMonacoEditor.vue@@ -0,0 +1,206 @@+<template>+ <div ref="editorRef" class="w-full overflow-hidden"></div>+</template>++<script lang="ts" setup>+import {+ onMounted,+ ref,+ toRef,+ toRaw,+ nextTick,+ onUnmounted,+ watch,+ PropType,+} from "vue";+import { editor as Editor } from "monaco-editor";+import { Database, SQLDialect, Table } from "@/types";+import { useMonaco } from "./useMonaco";+import { useLineDecorations } from "./lineDecorations";++const props = defineProps({+ value: {+ type: String,+ required: true,+ },+ language: {+ type: String,+ default: "mysql",+ },+ readonly: {+ type: Boolean,+ default: false,+ },+ databaseList: {+ type: Array as PropType<Database[]>,+ default: () => [],+ },+ tableList: {+ type: Array as PropType<Table[]>,+ default: () => [],+ },+ minHeight: {+ type: Number,+ default: 0,+ },+ maxHeight: {+ type: Number,+ default: 0,+ },+});++const emit = defineEmits<{+ (e: "change", content: string): void;+}>();++const editorRef = ref();+const statement = toRef(props, "value");+const language = toRef(props, "language");+const readonly = toRef(props, "readonly");++let editorInstance: Editor.IStandaloneCodeEditor;++const {+ monaco,+ formatContent,+ setAutoCompletionContext,+ setPositionAtEndOfLine,+} = await useMonaco(language.value);++const init = async () => {+ const model = monaco.editor.createModel(+ statement.value,+ toRaw(language.value)+ );++ editorInstance = monaco.editor.create(editorRef.value, {+ model,+ tabSize: 2,+ insertSpaces: true,+ autoClosingQuotes: "always",+ detectIndentation: false,+ folding: false,+ automaticLayout: true,+ theme: "vs-light",+ minimap: {+ enabled: false,+ },+ wordWrap: "on",+ fixedOverflowWidgets: true,+ scrollBeyondLastLine: false,+ });++ // add format sql action in context menu+ editorInstance.addAction({+ id: "FormatSQL",+ label: "Format SQL",+ keybindings: [+ monaco.KeyMod.Alt | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,+ ],+ contextMenuGroupId: "operation",+ contextMenuOrder: 1,+ run: () => {+ if (readonly.value) {+ return;+ }+ formatContent(editorInstance, language.value as SQLDialect);+ nextTick(() => setPositionAtEndOfLine(editorInstance));+ },+ });++ // typed something, change the text+ editorInstance.onDidChangeModelContent(() => {+ const value = editorInstance.getValue();+ emit("change", value);+ });++ editorInstance.onDidChangeCursorPosition((e) => {+ const { defineLineDecorations, disposeLineDecorations } =+ useLineDecorations(editorInstance, e.position);+ // clear the old decorations+ disposeLineDecorations();++ // define the new decorations+ nextTick(() => {+ defineLineDecorations();+ });+ });++ // set the editor's autoComplete context+ watch(+ [() => props.databaseList, () => props.tableList],+ ([databaseList, tableList]) => {+ setAutoCompletionContext(databaseList, tableList);+ },+ {+ immediate: true,+ }+ );++ // set the editor focus when the tab is selected+ if (!readonly.value) {+ editorInstance.focus();++ nextTick(() => setPositionAtEndOfLine(editorInstance));+ }++ watch(+ () => readonly.value,+ (readOnly) => {+ if (editorInstance) {+ editorInstance.updateOptions({+ readOnly,+ renderLineHighlight: readOnly ? "none" : "line",+ });+ if (!readOnly) {+ editorInstance.focus();+ nextTick(() => setPositionAtEndOfLine(editorInstance));+ }+ }+ },+ {+ immediate: true,+ }+ );++ const fitSize = () => {+ const container = editorRef.value;+ if (!container) return;++ const contentHeight = editorInstance.getContentHeight();+ let actualHeight = contentHeight;+ if (props.minHeight > 0 && actualHeight < props.minHeight) {+ actualHeight = props.minHeight;+ }+ if (props.maxHeight > 0 && actualHeight > props.maxHeight) {+ actualHeight = props.maxHeight;+ }+ container.style.height = `${actualHeight}px`;+ };++ watch(statement, (statement) => {+ if (statement !== model.getValue()) {+ model.setValue(statement);+ }+ requestAnimationFrame(fitSize);+ });++ fitSize();+};++onMounted(() => {+ init();+});++onUnmounted(() => {+ editorInstance.dispose();+});+</script>++<style>+.monaco-editor .cldr.sql-fragment {+ @apply bg-indigo-600;+ width: 3px !important;+ margin-left: 2px;+}+</style>