feat(connection): add custom color picker for connection editing (#913)
- Extend hexToRgba() to handle rgba/rgb input passthrough - Add pipette button with native color picker + text input in popover - Custom color and preset colors are mutually exclusive
This commit is contained in:
1 parent
403681e1ed
commit
b2b1a7365b
8 files changed
+82
-2
No files matched your search
@@ -9,6 +9,7 @@ import { Label } from "@/components/ui/label";
|
|||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
||||||
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
||||||
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
||||||
import type {
|
import type {
|
||||||
ConnectionConfig,
|
ConnectionConfig,
|
||||||
DatabaseType,
|
DatabaseType,
|
||||||
@@ -49,6 +50,7 @@ import {
|
|||||||
KeyRound,
|
KeyRound,
|
||||||
Link2,
|
Link2,
|
||||||
List,
|
List,
|
||||||
|
Pipette,
|
||||||
Plus,
|
Plus,
|
||||||
Search,
|
Search,
|
||||||
ShieldCheck,
|
ShieldCheck,
|
||||||
@@ -287,6 +289,28 @@ const colorOptions = [
|
|||||||
{ value: "#a855f7", class: "bg-purple-500", labelKey: "connection.colorPurple" },
|
{ value: "#a855f7", class: "bg-purple-500", labelKey: "connection.colorPurple" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const isPresetColor = (color: string | undefined) => colorOptions.some((c) => c.value === (color || ""));
|
||||||
|
const customColorInput = ref("");
|
||||||
|
const customColorOpen = ref(false);
|
||||||
|
|
||||||
|
function applyCustomColor(value: string) {
|
||||||
|
form.value.color = value;
|
||||||
|
customColorInput.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handlePresetClick(color: string) {
|
||||||
|
form.value.color = color;
|
||||||
|
customColorInput.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCustomColorPicked(value: string) {
|
||||||
|
applyCustomColor(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCustomColorInput(value: string) {
|
||||||
|
applyCustomColor(value);
|
||||||
|
}
|
||||||
|
|
||||||
const driverProfiles: Record<
|
const driverProfiles: Record<
|
||||||
string,
|
string,
|
||||||
{
|
{
|
||||||
@@ -551,6 +575,7 @@ watch(
|
|||||||
etcd_endpoints: config.etcd_endpoints || "",
|
etcd_endpoints: config.etcd_endpoints || "",
|
||||||
};
|
};
|
||||||
h2ConnectionMode.value = h2ConnectionModeForConfig(config);
|
h2ConnectionMode.value = h2ConnectionModeForConfig(config);
|
||||||
|
customColorInput.value = config.color || "";
|
||||||
selectedTransportLayerId.value = form.value.transport_layers?.[0]?.id || null;
|
selectedTransportLayerId.value = form.value.transport_layers?.[0]?.id || null;
|
||||||
selectedType.value = profile;
|
selectedType.value = profile;
|
||||||
if (profile === "oceanbase") {
|
if (profile === "oceanbase") {
|
||||||
@@ -2151,8 +2176,45 @@ function openExternalUrl(url: string) {
|
|||||||
form.color === color.value ? 'ring-2 ring-ring ring-offset-2' : 'border-border',
|
form.color === color.value ? 'ring-2 ring-ring ring-offset-2' : 'border-border',
|
||||||
]"
|
]"
|
||||||
:title="t(color.labelKey)"
|
:title="t(color.labelKey)"
|
||||||
@click="form.color = color.value"
|
@click="handlePresetClick(color.value)"
|
||||||
/>
|
/>
|
||||||
|
<Popover v-model:open="customColorOpen">
|
||||||
|
<PopoverTrigger as-child>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="h-6 w-6 rounded-full border flex items-center justify-center hover:scale-105 transition"
|
||||||
|
:class="[
|
||||||
|
!isPresetColor(form.color) && form.color
|
||||||
|
? 'border-border ring-2 ring-ring ring-offset-2'
|
||||||
|
: 'border-dashed border-border',
|
||||||
|
]"
|
||||||
|
:style="!isPresetColor(form.color) && form.color ? { backgroundColor: form.color } : {}"
|
||||||
|
:title="t('connection.colorCustom')"
|
||||||
|
>
|
||||||
|
<Pipette
|
||||||
|
class="h-3.5 w-3.5"
|
||||||
|
:class="!isPresetColor(form.color) && form.color ? 'text-white' : 'text-muted-foreground'"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</PopoverTrigger>
|
||||||
|
<PopoverContent class="w-auto p-2">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
type="color"
|
||||||
|
:value="form.color"
|
||||||
|
@input="handleCustomColorPicked(($event.target as HTMLInputElement).value)"
|
||||||
|
class="h-6 w-6 cursor-pointer rounded border-0 p-0"
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
:value="customColorInput || form.color"
|
||||||
|
@input="handleCustomColorInput(($event.target as HTMLInputElement).value)"
|
||||||
|
class="w-28 h-7 text-xs font-mono"
|
||||||
|
:placeholder="'#ff0000 或 rgba(…)'"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</PopoverContent>
|
||||||
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -306,6 +306,7 @@ export default {
|
|||||||
colorRed: "Red",
|
colorRed: "Red",
|
||||||
colorBlue: "Blue",
|
colorBlue: "Blue",
|
||||||
colorPurple: "Purple",
|
colorPurple: "Purple",
|
||||||
|
colorCustom: "Custom color",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "Press {mod}+Enter to execute",
|
pressToExecute: "Press {mod}+Enter to execute",
|
||||||
|
|||||||
@@ -288,6 +288,7 @@ export default {
|
|||||||
colorRed: "Rojo",
|
colorRed: "Rojo",
|
||||||
colorBlue: "Azul",
|
colorBlue: "Azul",
|
||||||
colorPurple: "Morado",
|
colorPurple: "Morado",
|
||||||
|
colorCustom: "Color personalizado",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "Presiona {mod}+Enter para ejecutar",
|
pressToExecute: "Presiona {mod}+Enter para ejecutar",
|
||||||
|
|||||||
@@ -300,6 +300,7 @@ export default {
|
|||||||
colorRed: "Rosso",
|
colorRed: "Rosso",
|
||||||
colorBlue: "Blu",
|
colorBlue: "Blu",
|
||||||
colorPurple: "Viola",
|
colorPurple: "Viola",
|
||||||
|
colorCustom: "Colore personalizzato",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "Premi {mod}+Enter per eseguire",
|
pressToExecute: "Premi {mod}+Enter per eseguire",
|
||||||
|
|||||||
@@ -299,6 +299,7 @@ export default {
|
|||||||
colorRed: "Vermelho",
|
colorRed: "Vermelho",
|
||||||
colorBlue: "Azul",
|
colorBlue: "Azul",
|
||||||
colorPurple: "Roxo",
|
colorPurple: "Roxo",
|
||||||
|
colorCustom: "Cor personalizada",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "Pressione {mod}+Enter para executar",
|
pressToExecute: "Pressione {mod}+Enter para executar",
|
||||||
|
|||||||
@@ -302,6 +302,7 @@ export default {
|
|||||||
colorRed: "红色",
|
colorRed: "红色",
|
||||||
colorBlue: "蓝色",
|
colorBlue: "蓝色",
|
||||||
colorPurple: "紫色",
|
colorPurple: "紫色",
|
||||||
|
colorCustom: "自定义颜色",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "按 {mod}+Enter 执行查询",
|
pressToExecute: "按 {mod}+Enter 执行查询",
|
||||||
|
|||||||
@@ -291,6 +291,7 @@ export default {
|
|||||||
colorRed: "紅色",
|
colorRed: "紅色",
|
||||||
colorBlue: "藍色",
|
colorBlue: "藍色",
|
||||||
colorPurple: "紫色",
|
colorPurple: "紫色",
|
||||||
|
colorCustom: "自訂顏色",
|
||||||
},
|
},
|
||||||
editor: {
|
editor: {
|
||||||
pressToExecute: "按 {mod}+Enter 執行查詢",
|
pressToExecute: "按 {mod}+Enter 執行查詢",
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
export function hexToRgba(color: string | undefined, alpha: number): string | undefined {
|
export function hexToRgba(color: string | undefined, alpha: number): string | undefined {
|
||||||
if (!color) return undefined;
|
if (!color) return undefined;
|
||||||
const hex = color.trim().replace(/^#/, "");
|
const trimmed = color.trim();
|
||||||
|
|
||||||
|
// Already rgba/rgb: replace or add alpha, then return
|
||||||
|
const rgbaMatch = trimmed.match(/^rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)$/i);
|
||||||
|
if (rgbaMatch) {
|
||||||
|
return `rgba(${rgbaMatch[1]}, ${rgbaMatch[2]}, ${rgbaMatch[3]}, ${alpha})`;
|
||||||
|
}
|
||||||
|
const rgbMatch = trimmed.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
|
||||||
|
if (rgbMatch) {
|
||||||
|
return `rgba(${rgbMatch[1]}, ${rgbMatch[2]}, ${rgbMatch[3]}, ${alpha})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hex = trimmed.replace(/^#/, "");
|
||||||
const normalized =
|
const normalized =
|
||||||
hex.length === 3
|
hex.length === 3
|
||||||
? hex
|
? hex
|
||||||
|
|||||||
Reference in new issue
Block a user