Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: format issue statement on save (#1241)

Liu Jia08678782110
diff --git a/frontend/package.json b/frontend/package.json
index 13a1d05900..5f077ba1df 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -18,7 +18,7 @@
"axios": "^0.26.0",
"dayjs": "^1.10.7",
"escape-string-regexp": "^5.0.0",
- "highlight.js": "^10.6.0",
+ "highlight.js": "^11.0.1",
"inter-ui": "^3.15.0",
"lodash-es": "^4.17.21",
"monaco-editor": "^0.32.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 4fa42ba6ea..c52cc10f62 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -1,4 +1,4 @@
-lockfileVersion: 5.3
+lockfileVersion: 5.4
specifiers:
'@bytebase/vue-kbar': ^0.1.6
@@ -30,7 +30,7 @@ specifiers:
eslint-config-prettier: ^8.3.0
eslint-plugin-prettier: ^4.0.0
eslint-plugin-vue: ^8.1.1
- highlight.js: ^10.6.0
+ highlight.js: ^11.0.1
inter-ui: ^3.15.0
lodash-es: ^4.17.21
monaco-editor: ^0.32.1
@@ -65,14 +65,14 @@ dependencies:
axios: 0.26.1
dayjs: 1.10.8
escape-string-regexp: 5.0.0
- highlight.js: 10.7.3
+ highlight.js: 11.5.0
inter-ui: 3.19.3
lodash-es: 4.17.21
monaco-editor: 0.32.1
node-sql-parser: 4.1.1
papaparse: 5.3.1
+ pinia: 2.0.12_5bgd5bbqiynntouprdo6x2bjeu
semver: 7.3.7
slug: 4.1.0
splitpanes: 3.1.1
@@ -86,7 +86,7 @@ dependencies:
devDependencies:
'@iconify/json': 1.1.461
- '@intlify/vite-plugin-vue-i18n': 3.3.1_ee51299bfed31ad0f4705239faf2563e
+ '@intlify/vite-plugin-vue-i18n': 3.3.1_5zistg762mnnb5dqki47v4swhy
'@tailwindcss/forms': [email protected]
'@tailwindcss/line-clamp': [email protected]
'@tailwindcss/typography': [email protected]
@@ -98,21 +98,21 @@ devDependencies:
'@types/slug': 0.9.1
'@types/splitpanes': 2.2.1
'@types/uuid': 8.3.4
- '@typescript-eslint/eslint-plugin': 5.14.0_ace6f9e7722b813e21eaf2dc993bc413
- '@typescript-eslint/parser': [email protected][email protected]
+ '@typescript-eslint/eslint-plugin': 5.14.0_vttptz3sfoat4ipk6lojso6ecm
+ '@typescript-eslint/parser': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
'@vitejs/plugin-vue': [email protected]
'@vue/compiler-sfc': 3.2.31
- '@vue/eslint-config-typescript': 9.1.0_292274e871e0ed2df848858321533513
+ '@vue/eslint-config-typescript': 9.1.0_ferhj2dr4dws36ciqwbscuzvcm
autoprefixer: [email protected]
eslint: 8.11.0
eslint-config-prettier: [email protected]
- eslint-plugin-prettier: 4.0.0_c9d5adccfd1d43a8805a302169f6a967
+ eslint-plugin-prettier: 4.0.0_zhk23th5dvb2rac2gaqwt5vjm4
eslint-plugin-vue: [email protected]
postcss: 8.4.12
prettier: 2.5.1
tailwindcss: [email protected]
typescript: 4.6.2
- unplugin-icons: 0.13.4_5dbb0cc878f3ab9caa0bd7b169eded43
+ unplugin-icons: 0.13.4_lw5qzsdy6ovzzkql26ywt3pnim
unplugin-vue-components: [email protected][email protected]
vite: 2.8.6
vitest: 0.0.139
@@ -286,8 +286,8 @@ packages:
- supports-color
dev: true
- /@intlify/bundle-utils/[email protected]:
- resolution: {integrity: sha512-y43Z5Q3ZJvxqtD8xUH6U3yrlZeay7ZTqkzv1GQ4b0mGQtk5uptOT9Ra4qvGuUv8QyPQsortrA/OHWUD5ax5ZNQ==}
+ /@intlify/bundle-utils/[email protected]:
+ resolution: {integrity: sha512-ghlJ0kR2cCQ8D+poKknC0Xx0ncOt3J3os7CcIAqqIWVF7k6AtGoCDnIru+YzlZcvFRNmP9wEZ7jKliojCdAWNg==}
engines: {node: '>= 12'}
peerDependencies:
petite-vue-i18n: '*'
@@ -349,7 +349,7 @@ packages:
engines: {node: '>= 12'}
dev: true
- /@intlify/vite-plugin-vue-i18n/3.3.1_ee51299bfed31ad0f4705239faf2563e:
+ /@intlify/vite-plugin-vue-i18n/3.3.1_5zistg762mnnb5dqki47v4swhy:
resolution: {integrity: sha512-b9HIUANzL4/LmrA5RqeiPabZl2/RiWNAJ90TVZ8kSi3APRZ4BbmwUAclKe8ip+1jCDhqaGO/qfAI7SbQ0Y76Uw==}
engines: {node: '>= 12'}
peerDependencies:
@@ -362,7 +362,7 @@ packages:
vue-i18n:
optional: true
dependencies:
- '@intlify/bundle-utils': [email protected]
+ '@intlify/bundle-utils': [email protected]
'@intlify/shared': 9.2.0-beta.35
'@rollup/pluginutils': 4.2.0
debug: 4.3.3
@@ -518,7 +518,7 @@ packages:
resolution: {integrity: sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==}
dev: true
- /@typescript-eslint/eslint-plugin/5.14.0_ace6f9e7722b813e21eaf2dc993bc413:
+ /@typescript-eslint/eslint-plugin/5.14.0_vttptz3sfoat4ipk6lojso6ecm:
resolution: {integrity: sha512-ir0wYI4FfFUDfLcuwKzIH7sMVA+db7WYen47iRSaCGl+HMAZI9fpBwfDo45ZALD3A45ZGyHWDNLhbg8tZrMX4w==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
peerDependencies:
@@ -529,10 +529,10 @@ packages:
typescript:
optional: true
dependencies:
- '@typescript-eslint/parser': [email protected][email protected]
+ '@typescript-eslint/parser': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
'@typescript-eslint/scope-manager': 5.14.0
- '@typescript-eslint/type-utils': [email protected][email protected]
- '@typescript-eslint/utils': [email protected][email protected]
+ '@typescript-eslint/type-utils': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
+ '@typescript-eslint/utils': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
debug: 4.3.3
eslint: 8.11.0
functional-red-black-tree: 1.0.1
@@ -545,7 +545,7 @@ packages:
- supports-color
dev: true
- /@typescript-eslint/parser/[email protected][email protected]:
+ /@typescript-eslint/parser/5.14.0_ynv3edxl3ah44xwgrna2g2yine:
resolution: {integrity: sha512-aHJN8/FuIy1Zvqk4U/gcO/fxeMKyoSv/rS46UXMXOJKVsLQ+iYPuXNbpbH7cBLcpSbmyyFbwrniLx5+kutu1pw==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
peerDependencies:
@@ -573,7 +573,7 @@ packages:
'@typescript-eslint/visitor-keys': 5.14.0
dev: true
- /@typescript-eslint/type-utils/[email protected][email protected]:
+ /@typescript-eslint/type-utils/5.14.0_ynv3edxl3ah44xwgrna2g2yine:
resolution: {integrity: sha512-d4PTJxsqaUpv8iERTDSQBKUCV7Q5yyXjqXUl3XF7Sd9ogNLuKLkxz82qxokqQ4jXdTPZudWpmNtr/JjbbvUixw==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
peerDependencies:
@@ -583,7 +583,7 @@ packages:
typescript:
optional: true
dependencies:
- '@typescript-eslint/utils': [email protected][email protected]
+ '@typescript-eslint/utils': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
debug: 4.3.3
eslint: 8.11.0
@@ -618,7 +618,7 @@ packages:
- supports-color
dev: true
- /@typescript-eslint/utils/[email protected][email protected]:
+ /@typescript-eslint/utils/5.14.0_ynv3edxl3ah44xwgrna2g2yine:
resolution: {integrity: sha512-EHwlII5mvUA0UsKYnVzySb/5EE/t03duUTweVy8Zqt3UQXBrpEVY144OTceFKaOe4xQXZJrkptCf7PjEBeGK4w==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
peerDependencies:
@@ -751,7 +751,7 @@ packages:
resolution: {integrity: sha512-1FtbxEEHN70WGJl1b/h8nLmyN+tOHONNsNLvgVEXF/L/vBrRqQZ0kF+dev1YAz3OtxsQ1sV/vPLKwRlq1axrgg==}
dev: false
- /@vue/eslint-config-typescript/9.1.0_292274e871e0ed2df848858321533513:
+ /@vue/eslint-config-typescript/9.1.0_ferhj2dr4dws36ciqwbscuzvcm:
resolution: {integrity: sha512-j/852/ZYQ5wDvCD3HE2q4uqJwJAceer2FwoEch1nFo+zTOsPrbzbE3cuWIs3kvu5hdFsGTMYwRwjI6fqZKDMxQ==}
engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}
peerDependencies:
@@ -760,8 +760,8 @@ packages:
eslint: ^6.2.0 || ^7.0.0 || ^8.0.0
eslint-plugin-vue: ^8.0.1
dependencies:
- '@typescript-eslint/eslint-plugin': 5.14.0_ace6f9e7722b813e21eaf2dc993bc413
- '@typescript-eslint/parser': [email protected][email protected]
+ '@typescript-eslint/eslint-plugin': 5.14.0_vttptz3sfoat4ipk6lojso6ecm
+ '@typescript-eslint/parser': 5.14.0_ynv3edxl3ah44xwgrna2g2yine
eslint: 8.11.0
eslint-plugin-vue: [email protected]
vue-eslint-parser: [email protected]
@@ -1574,7 +1574,7 @@ packages:
eslint: 8.11.0
dev: true
- /eslint-plugin-prettier/4.0.0_c9d5adccfd1d43a8805a302169f6a967:
+ /eslint-plugin-prettier/4.0.0_zhk23th5dvb2rac2gaqwt5vjm4:
resolution: {integrity: sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==}
engines: {node: '>=6.0.0'}
peerDependencies:
@@ -2476,7 +2476,7 @@ packages:
engines: {node: '>=8.6'}
dev: true
+ /pinia/2.0.12_5bgd5bbqiynntouprdo6x2bjeu:
resolution: {integrity: sha512-tUeuYGFrLU5irmGyRAIxp35q1OTcZ8sKpGT4XkPeVcG35W4R6cfXDbCGexzmVqH5lTQJJTXXbNGutIu9yS5yew==}
peerDependencies:
'@vue/composition-api': ^1.4.0
@@ -2973,7 +2973,7 @@ packages:
hasBin: true
dev: true
- /unplugin-icons/0.13.4_5dbb0cc878f3ab9caa0bd7b169eded43:
+ /unplugin-icons/0.13.4_lw5qzsdy6ovzzkql26ywt3pnim:
resolution: {integrity: sha512-gyp5H4WADnXEE1uk8+NW6gnnALOlSpU8M5GwzNCYbUgjM4QudjcFbacHKuuqETk4VeSJyzM9Z2ufbuZFMuxvuQ==}
peerDependencies:
'@svgr/core': '>=5.5.0'
diff --git a/frontend/src/components/HighlightCodeBlock.ts b/frontend/src/components/HighlightCodeBlock.ts
new file mode 100644
index 0000000000..78ae15932c
--- /dev/null
+++ b/frontend/src/components/HighlightCodeBlock.ts
@@ -0,0 +1,33 @@
+import { h, defineComponent } from "vue";
+import hljs from "highlight.js/lib/core";
+
+export default defineComponent({
+ name: "HighlightCodeBlock",
+ props: {
+ code: {
+ type: String,
+ required: true,
+ },
+ language: {
+ type: String,
+ default: "sql",
+ },
+ tag: {
+ type: String,
+ default: "div",
+ },
+ },
+ render() {
+ const { code, language, tag } = this.$props;
+
+ const result = hljs.highlight(code, {
+ language: language,
+ });
+ result.language;
+
+ return h(tag, {
+ class: language,
+ innerHTML: result.value,
+ });
+ },
+});
diff --git a/frontend/src/components/Issue/IssueDetailLayout.vue b/frontend/src/components/Issue/IssueDetailLayout.vue
index e8aa255d85..571d7beb3d 100644
--- a/frontend/src/components/Issue/IssueDetailLayout.vue
+++ b/frontend/src/components/Issue/IssueDetailLayout.vue
@@ -179,40 +179,25 @@
</template>
</template>
<template v-else>
- <!-- The way this is written is awkward and is to workaround an issue in IssueTaskStatementPanel.
- The statement panel is in non-edit mode when not creating the issue, and we use v-highlight
- to apply syntax highlighting when the panel is in non-edit mode. However, the v-highlight
- doesn't seem to work well with the reactivity. So for non-edit mode when !props.create, we
- list every IssueTaskStatementPanel for each stage and use v-if to show the active one.-->
- <template v-if="create">
- <IssueTaskStatementPanel
- :sql-hint="sqlHint()"
- :statement="selectedStatement"
- :create="create"
- :allow-edit="true"
- :show-apply-statement="showIssueTaskStatementApply"
- @update-statement="updateStatement"
- @apply-statement-to-other-stages="
- applyStatementToOtherStages
- "
- />
- </template>
- <template
- v-for="(stage, index) in (issue as Issue).pipeline.stageList"
+ <IssueTaskStatementPanel
+ v-if="create"
+ :sql-hint="sqlHint()"
+ :statement="selectedStatement"
+ :create="create"
+ :allow-edit="true"
+ :show-apply-statement="showIssueTaskStatementApply"
+ @update-statement="updateStatement"
+ @apply-statement-to-other-stages="applyStatementToOtherStages"
+ />
+ <IssueTaskStatementPanel
v-else
- :key="index"
- >
- <template v-if="(selectedStage as Stage).id == stage.id">
- <IssueTaskStatementPanel
- :sql-hint="sqlHint()"
- :statement="statement(stage)"
- :create="create"
- :allow-edit="allowEditStatement"
- :show-apply-statement="showIssueTaskStatementApply"
- @update-statement="updateStatement"
- />
- </template>
- </template>
+ :sql-hint="sqlHint()"
+ :statement="statement(selectedStage as Stage)"
+ :create="create"
+ :allow-edit="allowEditStatement"
+ :show-apply-statement="showIssueTaskStatementApply"
+ @update-statement="updateStatement"
+ />
</template>
</section>
@@ -298,6 +283,7 @@ import type {
UpdateSchemaDetail,
TaskDatabaseSchemaUpdateGhostSyncPayload,
UpdateSchemaGhostContext,
+ SQLDialect,
} from "@/types";
import {
defaultTemplate,
@@ -314,7 +300,9 @@ import {
useIssueSubscriberStore,
useProjectStore,
useTaskStore,
+ useUIStateStore,
} from "@/store";
+import formatSQL from "../MonacoEditor/sqlFormatter";
const props = defineProps({
create: {
@@ -339,6 +327,7 @@ const issueStore = useIssueStore();
const issueSubscriberStore = useIssueSubscriberStore();
const taskStore = useTaskStore();
const projectStore = useProjectStore();
+const databaseStore = useDatabaseStore();
watchEffect(function prepare() {
if (props.create) {
@@ -357,6 +346,31 @@ const project = computed((): Project => {
return (props.issue as Issue).project;
});
+const formatStatementIfNeeded = (
+ statement: string,
+ database?: Database
+): string => {
+ const uiStateStore = useUIStateStore();
+ if (!uiStateStore.issueFormatStatementOnSave) {
+ // Don't format if user closed this feature
+ return statement;
+ }
+
+ // Default to use mysql dialect but use postgresql dialect if needed
+ let dialect: SQLDialect = "mysql";
+ if (database && database.instance.engine === "POSTGRES") {
+ dialect = "postgresql";
+ }
+
+ const result = formatSQL(statement, dialect);
+ if (!result.error) {
+ return result.data;
+ }
+
+ // Fallback to the input statement if error occurs while formatting
+ return statement;
+};
+
const updateName = (
newName: string,
postUpdated: (updatedIssue: Issue) => void
@@ -403,10 +417,11 @@ const updateStatement = (
// nope, we are not allowed to update statement in tenant deploy mode anyway
} else {
// otherwise, patch the task
+ const task = selectedTask.value as Task;
patchTask(
- (selectedTask.value as Task).id,
+ task.id,
{
- statement: newStatement,
+ statement: formatStatementIfNeeded(newStatement, task.database),
},
postUpdated
);
@@ -515,10 +530,11 @@ const doCreate = () => {
(stage) => stage.taskList[0]
);
const detailList: UpdateSchemaDetail[] = taskList.map((task) => {
+ const db = databaseStore.getDatabaseById(task.databaseId!);
return {
databaseId: task.databaseId!,
databaseName: task.databaseName!,
- statement: task.statement,
+ statement: formatStatementIfNeeded(task.statement, db),
earliestAllowedTs: task.earliestAllowedTs,
};
});
@@ -535,16 +551,22 @@ const doCreate = () => {
stageList.forEach((stage, i) => {
const detail = detailList[i];
const syncTask = stage.taskList[0];
+ const db = databaseStore.getDatabaseById(syncTask.databaseId!);
detail.databaseId = syncTask.databaseId!;
detail.databaseName = syncTask.databaseName!;
- detail.statement = syncTask.statement;
+ detail.statement = formatStatementIfNeeded(syncTask.statement, db);
detail.earliestAllowedTs = syncTask.earliestAllowedTs;
});
} else {
// for multi-tenancy issue pipeline (M * N)
// createContext is up-to-date already
- // so nothing to do
+ // so we just format the statement if needed
+ const context = issue.createContext as UpdateSchemaContext;
+ context.updateSchemaDetailList.forEach((detail) => {
+ const db = databaseStore.getDatabaseById(detail.databaseId!);
+ detail.statement = formatStatementIfNeeded(detail.statement, db);
+ });
}
// then empty issue.pipeline and issue.payload
@@ -963,7 +985,7 @@ const database = computed((): Database | undefined => {
if (props.create) {
const databaseId = (selectedTask.value as TaskCreate).databaseId;
if (databaseId) {
- return useDatabaseStore().getDatabaseById(databaseId);
+ return databaseStore.getDatabaseById(databaseId);
}
return undefined;
}
diff --git a/frontend/src/components/Issue/IssueTaskStatementPanel.vue b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
index a3eb2f87d0..a8bc7b14b2 100644
--- a/frontend/src/components/Issue/IssueTaskStatementPanel.vue
+++ b/frontend/src/components/Issue/IssueTaskStatementPanel.vue
@@ -21,35 +21,58 @@
{{ $t("issue.apply-to-other-stages") }}
</button>
</div>
- <div v-if="!create" class="space-x-2">
- <button
- v-if="allowEdit && !state.editing"
- type="button"
- class="btn-icon"
- @click.prevent="beginEdit"
- >
- <!-- Heroicon name: solid/pencil -->
- <!-- Use h-5 to avoid flickering when show/hide icon -->
- <heroicons-solid:pencil class="h-5 w-5" />
- </button>
- <!-- mt-0.5 is to prevent jiggling between switching edit/none-edit -->
- <button
- v-if="state.editing"
- type="button"
- class="mt-0.5 px-3 rounded-sm text-control hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
- @click.prevent="cancelEdit"
- >
- {{ $t("common.cancel") }}
- </button>
- <button
- v-if="state.editing"
- type="button"
- class="mt-0.5 px-3 border border-control-border rounded-sm text-control bg-control-bg hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
- :disabled="state.editStatement == statement"
- @click.prevent="saveEdit"
- >
- {{ $t("common.save") }}
- </button>
+
+ <div class="space-x-2 flex items-center">
+ <template v-if="create">
+ <label class="mt-0.5 inline-flex items-center gap-1">
+ <input
+ v-model="formatOnSave"
+ type="checkbox"
+ class="h-4 w-4 text-accent rounded disabled:cursor-not-allowed border-control-border focus:ring-accent"
+ />
+ <span class="textlabel">Format on save</span>
+ </label>
+ </template>
+ <template v-else>
+ <button
+ v-if="allowEdit && !state.editing"
+ type="button"
+ class="btn-icon"
+ @click.prevent="beginEdit"
+ >
+ <!-- Heroicon name: solid/pencil -->
+ <!-- Use h-5 to avoid flickering when show/hide icon -->
+ <heroicons-solid:pencil class="h-5 w-5" />
+ </button>
+ <template v-if="state.editing">
+ <!-- mt-0.5 is to prevent jiggling between switching edit/none-edit -->
+ <label class="mt-0.5 inline-flex items-center gap-1">
+ <input
+ v-model="formatOnSave"
+ type="checkbox"
+ class="h-4 w-4 text-accent rounded disabled:cursor-not-allowed border-control-border focus:ring-accent"
+ />
+ <span class="textlabel">Format on save</span>
+ </label>
+ <button
+ v-if="state.editing"
+ type="button"
+ class="mt-0.5 px-3 rounded-sm text-control hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
+ @click.prevent="cancelEdit"
+ >
+ {{ $t("common.cancel") }}
+ </button>
+ <button
+ v-if="state.editing"
+ type="button"
+ class="mt-0.5 px-3 border border-control-border rounded-sm text-control bg-control-bg hover:bg-control-bg-hover disabled:bg-control-bg disabled:opacity-50 disabled:cursor-not-allowed text-sm leading-5 font-normal focus:ring-control focus:outline-none focus-visible:ring-2 focus:ring-offset-2"
+ :disabled="state.editStatement == statement"
+ @click.prevent="saveEdit"
+ >
+ {{ $t("common.save") }}
+ </button>
+ </template>
+ </template>
</div>
</div>
<label class="sr-only">{{ $t("common.sql-statement") }}</label>
@@ -78,9 +101,11 @@
</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">
- <div v-if="state.editStatement" v-highlight class="whitespace-pre-wrap">
- {{ state.editStatement }}
- </div>
+ <highlight-code-block
+ v-if="statement"
+ :code="statement"
+ class="whitespace-pre-wrap"
+ />
<div v-else-if="create" class="ml-2 text-control-light">
{{ $t("issue.add-sql-statement") }}
</div>
@@ -97,8 +122,10 @@ import {
reactive,
watch,
defineComponent,
+ computed,
} from "vue";
import { sizeToFit } from "../../utils";
+import { useUIStateStore } from "@/store";
interface LocalState {
editing: boolean;
@@ -107,7 +134,6 @@ interface LocalState {
export default defineComponent({
name: "IssueTaskStatementPanel",
- components: {},
props: {
statement: {
required: true,
@@ -135,11 +161,19 @@ export default defineComponent({
setup(props, { emit }) {
const editStatementTextArea = ref();
+ const uiStateStore = useUIStateStore();
+
const state = reactive<LocalState>({
editing: false,
editStatement: props.statement,
});
+ const formatOnSave = computed({
+ get: () => uiStateStore.issueFormatStatementOnSave,
+ set: (value: boolean) =>
+ uiStateStore.setIssueFormatStatementOnSave(value),
+ });
+
const resizeTextAreaHandler = () => {
if (state.editing) {
sizeToFit(editStatementTextArea.value);
@@ -174,6 +208,7 @@ export default defineComponent({
() => props.statement,
(cur) => {
state.editStatement = cur;
+ nextTick(() => sizeToFit(editStatementTextArea.value));
}
);
@@ -181,6 +216,7 @@ export default defineComponent({
state.editStatement = props.statement;
state.editing = true;
nextTick(() => {
+ sizeToFit(editStatementTextArea.value);
editStatementTextArea.value.focus();
});
};
@@ -198,6 +234,7 @@ export default defineComponent({
return {
editStatementTextArea,
+ formatOnSave,
state,
beginEdit,
saveEdit,
diff --git a/frontend/src/directives/highlight.js b/frontend/src/directives/highlight.js
deleted file mode 100644
index 0543098fdc..0000000000
--- a/frontend/src/directives/highlight.js
+++ /dev/null
@@ -1,14 +0,0 @@
-import hljs from "highlight.js";
-import "highlight.js/styles/github-gist.css";
-
-hljs.configure({
- languages: ["sql"],
-});
-
-const directive = {
- beforeMount(el) {
- hljs.highlightElement(el);
- },
-};
-
-export default directive;
diff --git a/frontend/src/main.ts b/frontend/src/main.ts
index c5c26e4258..32a2616c49 100644
--- a/frontend/src/main.ts
+++ b/frontend/src/main.ts
@@ -6,13 +6,13 @@ import App from "./App.vue";
import i18n from "./plugins/i18n";
import NaiveUI from "./plugins/naive-ui";
import dayjs from "./plugins/dayjs";
+import highlight from "./plugins/highlight";
import "./assets/css/inter.css";
import "./assets/css/tailwind.css";
import dataSourceType from "./directives/data-source-type";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
-import highlight from "./directives/highlight";
import { router } from "./router";
import {
pinia,
@@ -129,8 +129,8 @@ app
// Need to use a directive on the element.
// The normal hljs.initHighlightingOnLoad() won't work because router change would cause vue
// to re-render the page and remove the event listener required for
- .directive("highlight", highlight)
.directive("data-source-type", dataSourceType)
+ .use(highlight)
.use(pinia)
.use(router)
.use(i18n)
diff --git a/frontend/src/plugins/highlight.ts b/frontend/src/plugins/highlight.ts
new file mode 100644
index 0000000000..64d7512144
--- /dev/null
+++ b/frontend/src/plugins/highlight.ts
@@ -0,0 +1,16 @@
+import type { App } from "vue";
+import hljs from "highlight.js/lib/core";
+import sql from "highlight.js/lib/languages/sql";
+import "highlight.js/lib/common";
+import "highlight.js/styles/github.css";
+import HighlightCodeBlock from "@/components/HighlightCodeBlock";
+
+export default {
+ install(app: App) {
+ hljs.registerLanguage("sql", sql);
+ hljs.configure({
+ languages: ["sql"],
+ });
+ app.component("HighlightCodeBlock", HighlightCodeBlock);
+ },
+};
diff --git a/frontend/src/store/modules/uistate.ts b/frontend/src/store/modules/uistate.ts
index 7cc2fb5848..834c2e5514 100644
--- a/frontend/src/store/modules/uistate.ts
+++ b/frontend/src/store/modules/uistate.ts
@@ -1,9 +1,16 @@
import { defineStore } from "pinia";
import { isUndefined } from "lodash-es";
+import { useLocalStorage } from "@vueuse/core";
const COLLAPSE_MODULE = "ui.list.collapse";
const INTRO_MODULE = "ui.intro";
+// default to true
+const issueFormatStatementOnSave = useLocalStorage(
+ "ui.state.issue.format-statement-on-save",
+ true
+);
+
export interface UIState {
collapseStateByKey: Map<string, boolean>;
introStateByKey: Map<string, boolean>;
@@ -14,6 +21,11 @@ export const useUIStateStore = defineStore("uistate", {
collapseStateByKey: new Map(),
introStateByKey: new Map(),
}),
+ getters: {
+ issueFormatStatementOnSave: () => {
+ return issueFormatStatementOnSave.value;
+ },
+ },
actions: {
getCollapseStateByKey(key: string): boolean {
return stateByKey(this.collapseStateByKey, COLLAPSE_MODULE, key);
@@ -47,6 +59,9 @@ export const useUIStateStore = defineStore("uistate", {
setIntroStateByKey({ key, newState }: { key: string; newState: boolean }) {
this.introStateByKey.set(key, newState);
},
+ setIssueFormatStatementOnSave(value: boolean) {
+ issueFormatStatementOnSave.value = value;
+ },
async restoreState() {
const storedCollapseState = localStorage.getItem(COLLAPSE_MODULE);
const collapseState = storedCollapseState
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 38b0f14856..2bd923eb73 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -99,7 +99,9 @@ export function array_swap(arr: any[], old_index: number, new_index: number) {
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
}
-export function sizeToFit(el: HTMLTextAreaElement) {
+export function sizeToFit(el: HTMLTextAreaElement | undefined) {
+ if (!el) return;
+
el.style.height = "auto";
// Extra 2px is to prevent jiggling upon entering the text
el.style.height = `${el.scrollHeight + 2}px`;
diff --git a/frontend/src/views/MigrationHistoryDetail.vue b/frontend/src/views/MigrationHistoryDetail.vue
index d1bd5c0639..bfd1325215 100644
--- a/frontend/src/views/MigrationHistoryDetail.vue
+++ b/frontend/src/views/MigrationHistoryDetail.vue
@@ -105,9 +105,10 @@
<heroicons-outline:clipboard class="w-6 h-6" />
</button>
</a>
- <div v-highlight class="border px-2 whitespace-pre-wrap w-full">
- {{ migrationHistory.statement }}
- </div>
+ <highlight-code-block
+ class="border px-2 whitespace-pre-wrap w-full"
+ :code="migrationHistory.statement"
+ />
<a
id="schema"
href="#schema"
@@ -195,13 +196,11 @@
:new-string="state.rightSchema"
output-format="side-by-side"
/>
- <div
+ <highlight-code-block
v-else
- v-highlight
class="border mt-2 px-2 whitespace-pre-wrap w-full"
- >
- {{ migrationHistory.schema }}
- </div>
+ :code="migrationHistory.schema"
+ />
</div>
</main>
</div>