refactor(schema-diff): simplify comparison options layout

This commit is contained in:
t8y2 committed 2026-09-28 02:58:42 +08:00
1 parent 5730b6473d
commit b49e288dde
5 files changed
+189 -94

No files matched your search

@@ -1647,12 +1647,14 @@ const targetConnectionInfo = computed(() => {
<!-- Options Panel Overlay -->
<div v-if="showOptionsPanel" class="absolute inset-0 bg-background/80 backdrop-blur-sm z-50 flex items-center justify-center" @click.self="showOptionsPanel = false">
<div class="bg-card border rounded-lg shadow-lg w-[760px] max-w-[calc(100vw-2rem)] max-h-[80vh] overflow-auto p-4">
<div class="flex items-center justify-between mb-4">
<div class="flex h-[calc(100%-2rem)] max-h-[680px] w-[900px] max-w-[calc(100%-2rem)] flex-col overflow-hidden rounded-lg border bg-card shadow-lg">
<div class="flex shrink-0 items-center justify-between border-b px-4 py-3">
<h3 class="text-sm font-medium">{{ t("schemaDiff.optionsTitle") }}</h3>
<Button variant="ghost" size="sm" @click="showOptionsPanel = false" :aria-label="t('common.close')">✕</Button>
</div>
<SchemaDiffOptionsPanel :options="schemaDiffPanelOptions" :option-tree="optionTree" @update:options="handleOptionsUpdate" @close="showOptionsPanel = false" />
<div class="min-h-0 flex-1 p-4">
<SchemaDiffOptionsPanel :options="schemaDiffPanelOptions" :option-tree="optionTree" @update:options="handleOptionsUpdate" @close="showOptionsPanel = false" />
</div>
</div>
</div>
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { ref } from "vue";
import { computed, ref } from "vue";
import { ListChecks, ListFilter, Settings2, TableProperties } from "@lucide/vue";
import { useI18n } from "vue-i18n";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
@@ -22,6 +23,13 @@ const { t } = useI18n();
// The panel is a transactional editor: it is recreated each time it opens and
// only commits its local snapshot when the user clicks Done.
const localOptions = ref<SchemaDiffCompareOptions>(normalizeSchemaDiffCompareOptions(props.options));
const objectOptionIds = new Set<BooleanSchemaDiffCompareOptionKey>(["views", "functions", "indexes", "sequences", "triggers", "rules", "owners"]);
const tableDetailOptionIds = new Set<BooleanSchemaDiffCompareOptionKey>(["compareColumnOrder", "compareCharset"]);
const tableOption = computed(() => props.optionTree.find((item) => item.id === "tables"));
const objectOptions = computed(() => props.optionTree.filter((item) => objectOptionIds.has(item.id)));
const tableStructureOptions = computed(() => [...(tableOption.value?.children ?? []), ...props.optionTree.filter((item) => tableDetailOptionIds.has(item.id))]);
const behaviorOptions = computed(() => props.optionTree.filter((item) => item.id !== "tables" && !objectOptionIds.has(item.id) && !tableDetailOptionIds.has(item.id)));
function isChecked(id: BooleanSchemaDiffCompareOptionKey): boolean {
return !!localOptions.value[id];
@@ -66,7 +74,7 @@ function handleCancel() {
}
function getItemClasses(state: "checked" | "unchecked" | "indeterminate"): string {
const base = "h-4 w-4 rounded border flex items-center justify-center transition-colors cursor-pointer";
const base = "flex h-4 w-4 shrink-0 items-center justify-center rounded border transition-colors";
if (state === "checked") {
return `${base} bg-primary border-primary text-primary-foreground`;
}
@@ -75,104 +83,165 @@ function getItemClasses(state: "checked" | "unchecked" | "indeterminate"): strin
}
return `${base} bg-background border-input hover:border-muted-foreground`;
}
function getItemRowClasses(item: SchemaDiffOptionItem): string {
const state = getChildState(item);
const base = "group flex min-h-8 w-full items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring";
return state === "unchecked" ? `${base} text-foreground hover:bg-muted/60` : `${base} bg-primary/5 text-foreground hover:bg-primary/10`;
}
function getAriaChecked(item: SchemaDiffOptionItem): boolean | "mixed" {
const state = getChildState(item);
return state === "indeterminate" ? "mixed" : state === "checked";
}
</script>
<template>
<div class="flex flex-col h-full">
<div class="grid flex-1 grid-cols-[minmax(0,1fr)_minmax(300px,0.95fr)] gap-4 overflow-auto pr-1">
<div class="min-w-0 space-y-1">
<div class="px-2 pb-1 text-xs font-medium text-muted-foreground">{{ t("schemaDiff.optionsTitle") }}</div>
<div class="space-y-1">
<template v-for="item in optionTree" :key="item.id">
<div class="space-y-1">
<div class="flex items-center gap-2 py-1 px-2 rounded hover:bg-muted/50 cursor-pointer" @click="toggleItem(item)">
<div :class="getItemClasses(getChildState(item))">
<svg v-if="getChildState(item) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4">
<polyline points="20 6 9 17 4 12" />
</svg>
<svg v-else-if="getChildState(item) === 'indeterminate'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4">
<line x1="5" y1="12" x2="19" y2="12" />
</svg>
</div>
<span class="text-sm select-none">{{ t(item.labelKey) }}</span>
</div>
<div v-if="item.children" class="ml-6 space-y-1">
<div v-for="child in item.children" :key="child.id" class="flex items-center gap-2 py-1 px-2 rounded hover:bg-muted/50 cursor-pointer" @click="toggleItem(child)">
<div :class="getItemClasses(getChildState(child))">
<svg v-if="getChildState(child) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4">
<polyline points="20 6 9 17 4 12" />
</svg>
</div>
<span class="text-sm select-none">{{ t(child.labelKey) }}</span>
</div>
</div>
</div>
</template>
</div>
<div class="flex h-full min-h-0 flex-col">
<div class="grid min-h-0 flex-1 grid-cols-[minmax(0,1.15fr)_minmax(300px,0.85fr)] overflow-hidden">
<div class="min-h-0 space-y-4 overflow-y-auto pr-4">
<section data-section="object-scope">
<div class="mb-2 flex items-center gap-2 text-xs font-semibold">
<ListChecks class="h-4 w-4 shrink-0 text-muted-foreground" />
<h4>{{ t("schemaDiff.options.objectScopeSection") }}</h4>
</div>
<div class="grid grid-cols-2 gap-1">
<button v-if="tableOption" type="button" role="checkbox" :aria-checked="getAriaChecked(tableOption)" :class="getItemRowClasses(tableOption)" @click="toggleItem(tableOption)">
<span :class="getItemClasses(getChildState(tableOption))">
<svg v-if="getChildState(tableOption) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4"><polyline points="20 6 9 17 4 12" /></svg>
<svg v-else-if="getChildState(tableOption) === 'indeterminate'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4"><line x1="5" y1="12" x2="19" y2="12" /></svg>
</span>
<span class="select-none">{{ t(tableOption.labelKey) }}</span>
</button>
<button v-for="item in objectOptions" :key="item.id" type="button" role="checkbox" :aria-checked="getAriaChecked(item)" :class="getItemRowClasses(item)" @click="toggleItem(item)">
<span :class="getItemClasses(getChildState(item))">
<svg v-if="getChildState(item) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4"><polyline points="20 6 9 17 4 12" /></svg>
</span>
<span class="select-none">{{ t(item.labelKey) }}</span>
</button>
</div>
</section>
<section data-section="table-structure" class="border-t pt-4">
<div class="mb-2 flex items-center gap-2 text-xs font-semibold">
<TableProperties class="h-4 w-4 shrink-0 text-muted-foreground" />
<h4>{{ t("schemaDiff.options.tableStructureSection") }}</h4>
</div>
<div class="grid grid-cols-2 gap-1">
<button v-for="item in tableStructureOptions" :key="item.id" type="button" role="checkbox" :aria-checked="getAriaChecked(item)" :class="getItemRowClasses(item)" @click="toggleItem(item)">
<span :class="getItemClasses(getChildState(item))">
<svg v-if="getChildState(item) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4"><polyline points="20 6 9 17 4 12" /></svg>
</span>
<span class="select-none">{{ t(item.labelKey) }}</span>
</button>
</div>
</section>
<section v-if="behaviorOptions.length" data-section="compare-behavior" class="border-t pt-4">
<div class="mb-2 flex items-center gap-2 text-xs font-semibold">
<Settings2 class="h-4 w-4 shrink-0 text-muted-foreground" />
<h4>{{ t("schemaDiff.options.behaviorSection") }}</h4>
</div>
<div class="grid grid-cols-2 gap-1">
<button v-for="item in behaviorOptions" :key="item.id" type="button" role="checkbox" :aria-checked="getAriaChecked(item)" :class="getItemRowClasses(item)" @click="toggleItem(item)">
<span :class="getItemClasses(getChildState(item))">
<svg v-if="getChildState(item) === 'checked'" class="h-3 w-3" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4"><polyline points="20 6 9 17 4 12" /></svg>
</span>
<span class="select-none">{{ t(item.labelKey) }}</span>
</button>
</div>
</section>
</div>
<div class="grid min-w-0 grid-cols-[112px_minmax(0,1fr)] content-start gap-x-3 gap-y-2 border-l pl-4">
<div class="pt-1 text-xs font-medium text-muted-foreground">{{ t("schemaDiff.tableFilter") }}</div>
<div class="flex min-w-0 items-center justify-between gap-2">
<span class="truncate text-xs text-muted-foreground">{{ t("schemaDiff.tableFilterHintShort") }}</span>
<HelpTooltip :label="t('schemaDiff.tableFilterHelp')" side="left">
<div class="space-y-1">
<div>{{ t("schemaDiff.tableFilterHelpRule") }}</div>
<div>{{ t("schemaDiff.tableFilterHelpBlank") }}</div>
<div>{{ t("schemaDiff.tableFilterHelpExample") }}</div>
<div class="min-h-0 space-y-4 overflow-y-auto border-l pl-4 pr-1">
<section>
<div class="mb-3 flex items-center justify-between gap-3">
<div class="flex items-center gap-2 text-xs font-semibold">
<ListFilter class="h-4 w-4 shrink-0 text-muted-foreground" />
<h4>{{ t("schemaDiff.tableFilter") }}</h4>
</div>
</HelpTooltip>
</div>
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-table-include">{{ t("schemaDiff.tableIncludePattern") }}</label>
<input id="schema-diff-table-include" v-model="localOptions.tableIncludePattern" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.tableIncludePatternPlaceholder')" />
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-table-exclude">{{ t("schemaDiff.tableExcludePattern") }}</label>
<input id="schema-diff-table-exclude" v-model="localOptions.tableExcludePattern" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.tableExcludePatternPlaceholder')" />
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-table-priority">{{ t("schemaDiff.tableFilterPriority") }}</label>
<div>
<Select v-model="localOptions.tableFilterPriority">
<SelectTrigger id="schema-diff-table-priority" class="h-8 w-full text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="exclude">{{ t("schemaDiff.tableFilterPriorityExclude") }}</SelectItem>
<SelectItem value="include">{{ t("schemaDiff.tableFilterPriorityInclude") }}</SelectItem>
</SelectContent>
</Select>
</div>
<HelpTooltip :label="t('schemaDiff.tableFilterHelp')" side="left">
<div class="space-y-1">
<div>{{ t("schemaDiff.tableFilterHelpRule") }}</div>
<div>{{ t("schemaDiff.tableFilterHelpBlank") }}</div>
<div>{{ t("schemaDiff.tableFilterHelpExample") }}</div>
</div>
</HelpTooltip>
</div>
<div class="space-y-2.5">
<div class="space-y-1">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-table-include">{{ t("schemaDiff.tableIncludePattern") }}</label>
<input id="schema-diff-table-include" v-model="localOptions.tableIncludePattern" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none transition-shadow focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.tableIncludePatternPlaceholder')" />
</div>
<div class="space-y-1">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-table-exclude">{{ t("schemaDiff.tableExcludePattern") }}</label>
<input id="schema-diff-table-exclude" v-model="localOptions.tableExcludePattern" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none transition-shadow focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.tableExcludePatternPlaceholder')" />
</div>
<div class="space-y-1">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-table-priority">{{ t("schemaDiff.tableFilterPriority") }}</label>
<Select v-model="localOptions.tableFilterPriority">
<SelectTrigger id="schema-diff-table-priority" class="h-8 w-full text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="exclude">{{ t("schemaDiff.tableFilterPriorityExclude") }}</SelectItem>
<SelectItem value="include">{{ t("schemaDiff.tableFilterPriorityInclude") }}</SelectItem>
</SelectContent>
</Select>
</div>
</div>
</section>
<!-- Advanced options -->
<div class="col-span-2 pt-3 pb-1 text-xs font-medium text-muted-foreground border-t mt-2">{{ t("schemaDiff.options.advancedSection") }}</div>
<section class="border-t pt-4">
<div class="mb-3 flex items-center gap-2 text-xs font-semibold">
<Settings2 class="h-4 w-4 shrink-0 text-muted-foreground" />
<h4>{{ t("schemaDiff.options.advancedSection") }}</h4>
</div>
<div class="space-y-3">
<div class="space-y-1.5">
<div class="flex items-center justify-between gap-2">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-rename-threshold">{{ t("schemaDiff.options.renameThreshold") }}</label>
<span class="font-mono text-[11px]" :class="localOptions.detectRenames || localOptions.detectTableRenames ? 'text-foreground' : 'text-muted-foreground'">{{ localOptions.renameThreshold.toFixed(2) }}</span>
</div>
<input
id="schema-diff-rename-threshold"
v-model.number="localOptions.renameThreshold"
type="range"
min="0"
max="1"
step="0.05"
:disabled="!localOptions.detectRenames && !localOptions.detectTableRenames"
class="h-1.5 w-full cursor-pointer accent-primary disabled:cursor-not-allowed disabled:opacity-40"
/>
</div>
<div class="space-y-1.5">
<div class="flex items-center justify-between gap-2">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-compat-threshold">{{ t("schemaDiff.options.compatibilityThreshold") }}</label>
<span class="font-mono text-[11px] text-foreground">{{ localOptions.compatibilityThreshold.toFixed(2) }}</span>
</div>
<input id="schema-diff-compat-threshold" v-model.number="localOptions.compatibilityThreshold" type="range" min="0" max="1" step="0.05" class="h-1.5 w-full cursor-pointer accent-primary" />
</div>
<div class="grid grid-cols-2 gap-2">
<div class="min-w-0 space-y-1">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-source-dialect">{{ t("schemaDiff.options.sourceDialect") }}</label>
<input id="schema-diff-source-dialect" v-model="localOptions.sourceDialect" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none transition-shadow focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.dialectAuto')" />
</div>
<div class="min-w-0 space-y-1">
<label class="text-[11px] font-medium text-muted-foreground" for="schema-diff-target-dialect">{{ t("schemaDiff.options.targetDialect") }}</label>
<input id="schema-diff-target-dialect" v-model="localOptions.targetDialect" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none transition-shadow focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.dialectAuto')" />
</div>
</div>
</div>
</section>
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-rename-threshold">{{ t("schemaDiff.options.renameThreshold") }}</label>
<div v-if="localOptions.detectRenames || localOptions.detectTableRenames" class="flex items-center gap-2 pt-1">
<input id="schema-diff-rename-threshold" type="range" min="0" max="1" step="0.05" v-model.number="localOptions.renameThreshold" class="flex-1 h-1.5 accent-primary cursor-pointer" />
<span class="text-xs font-mono w-10 text-right">{{ localOptions.renameThreshold.toFixed(2) }}</span>
</div>
<div v-else class="pt-1 text-xs text-muted-foreground italic">
{{ t("schemaDiff.options.detectRenames") }}
</div>
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-compat-threshold">{{ t("schemaDiff.options.compatibilityThreshold") }}</label>
<div class="flex items-center gap-2 pt-1">
<input id="schema-diff-compat-threshold" type="range" min="0" max="1" step="0.05" v-model.number="localOptions.compatibilityThreshold" class="flex-1 h-1.5 accent-primary cursor-pointer" />
<span class="text-xs font-mono w-10 text-right">{{ localOptions.compatibilityThreshold.toFixed(2) }}</span>
</div>
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-source-dialect">{{ t("schemaDiff.options.sourceDialect") }}</label>
<input id="schema-diff-source-dialect" v-model="localOptions.sourceDialect" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.dialectAuto')" />
<label class="pt-2 text-xs font-medium text-muted-foreground" for="schema-diff-target-dialect">{{ t("schemaDiff.options.targetDialect") }}</label>
<input id="schema-diff-target-dialect" v-model="localOptions.targetDialect" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.dialectAuto')" />
<div class="col-span-2 pt-3 pb-1 text-xs font-medium text-muted-foreground border-t mt-2">{{ t("schemaDiff.options.batchSection") }}</div>
<div class="flex items-center gap-2 col-span-2 pt-1">
<input id="schema-diff-batch-patterns" v-model="localOptions.batchPatterns" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.batchPatterns')" />
</div>
<section class="border-t pt-4">
<h4 class="mb-2.5 text-xs font-semibold">{{ t("schemaDiff.options.batchSection") }}</h4>
<input id="schema-diff-batch-patterns" v-model="localOptions.batchPatterns" class="h-8 w-full rounded-md border border-input bg-background px-2 text-xs outline-none transition-shadow focus:ring-1 focus:ring-ring" :placeholder="t('schemaDiff.options.batchPatterns')" />
</section>
</div>
</div>
<div class="flex items-center justify-end gap-2 pt-4 border-t mt-2">
<div class="mt-3 flex shrink-0 items-center justify-end gap-2 border-t pt-3">
<Button variant="outline" size="sm" @click="handleCancel">
{{ t("common.cancel") }}
</Button>
@@ -67,6 +67,24 @@ describe("SchemaDiffOptionsPanel", () => {
expect(normalizeSchemaDiffCompareOptions({}, "mysql").compareCharset).toBe(true);
});
it("groups MySQL charset comparison with table structure options", async () => {
const host = document.createElement("div");
document.body.append(host);
const app = createApp(SchemaDiffOptionsPanel, {
options: normalizeSchemaDiffCompareOptions({}, "mysql"),
optionTree: getSchemaDiffOptionsForDbType("mysql"),
});
mountedApps.push(app);
app.mount(host);
await nextTick();
const tableStructure = host.querySelector('[data-section="table-structure"]');
const behavior = host.querySelector('[data-section="compare-behavior"]');
expect(tableStructure?.textContent).toContain("schemaDiff.options.compareCharset");
expect(behavior?.textContent).not.toContain("schemaDiff.options.compareCharset");
});
it("keeps unsaved checkbox edits when the parent refreshes equivalent options", async () => {
const options = ref<SchemaDiffCompareOptions>({ ...DEFAULT_POSTGRES_OPTIONS });
const optionTree: SchemaDiffOptionItem[] = [{ id: "views", labelKey: "views", defaultChecked: true }];
+4 -1
View File
@@ -7423,7 +7423,7 @@ export default {
selectedCount: "{selected} selected of {total}",
saveConfig: "Save config",
loadConfig: "Load config",
options: "Options",
options: "Compare options",
ddlCompare: "DDL compare",
deployScript: "Deploy script",
deployScriptAll: "All deploy scripts",
@@ -7582,6 +7582,9 @@ export default {
tableFilterPriorityExclude: "Exclude wins",
tableFilterPriorityInclude: "Include wins",
options: {
objectScopeSection: "Object scope",
tableStructureSection: "Table structure",
behaviorSection: "Comparison behavior",
tables: "Compare tables",
primaryKeys: "Compare primary keys",
foreignKeys: "Compare foreign keys",
+4 -1
View File
@@ -7323,7 +7323,7 @@ export default withEnglishFallback({
selectedCount: "已选择 {selected} 个 (共 {total} 个)",
saveConfig: "保存配置",
loadConfig: "加载配置",
options: "选项",
options: "比较选项",
ddlCompare: "DDL 比较",
deployScript: "部署脚本",
deployScriptDesc: "所选对象的 SQL 脚本",
@@ -7548,6 +7548,9 @@ export default withEnglishFallback({
tableFilterPriorityExclude: "排除优先",
tableFilterPriorityInclude: "包含优先",
options: {
objectScopeSection: "对象范围",
tableStructureSection: "表结构",
behaviorSection: "比较行为",
tables: "比较表",
primaryKeys: "比较主键",
foreignKeys: "比较外键",