2026-03-30 11:40:45 +08:00
|
|
|
import { For, createSignal, onCleanup, onMount } from 'solid-js';
|
2026-02-27 15:33:23 +08:00
|
|
|
import type { DeckStore } from '../hooks/deckStore';
|
2026-03-30 12:54:53 +08:00
|
|
|
import alignLeftIcon from './icons/align-left.png';
|
|
|
|
|
import alignCenterIcon from './icons/align-center.png';
|
|
|
|
|
import alignRightIcon from './icons/align-right.png';
|
2026-02-27 15:33:23 +08:00
|
|
|
|
|
|
|
|
export interface LayerEditorPanelProps {
|
|
|
|
|
store: DeckStore;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-27 22:56:06 +08:00
|
|
|
const ORIENTATION_OPTIONS = [
|
|
|
|
|
{ value: 'n', label: '↑ 北' },
|
|
|
|
|
{ value: 'e', label: '→ 东' },
|
|
|
|
|
{ value: 's', label: '↓ 南' },
|
|
|
|
|
{ value: 'w', label: '← 西' }
|
|
|
|
|
] as const;
|
|
|
|
|
|
2026-03-27 15:17:25 +08:00
|
|
|
const ALIGN_OPTIONS = [
|
2026-03-30 12:54:53 +08:00
|
|
|
{ value: '', label: '默认', icon: alignCenterIcon },
|
|
|
|
|
{ value: 'l', label: '左对齐', icon: alignLeftIcon },
|
|
|
|
|
{ value: 'c', label: '居中', icon: alignCenterIcon },
|
|
|
|
|
{ value: 'r', label: '右对齐', icon: alignRightIcon }
|
2026-03-27 15:17:25 +08:00
|
|
|
] as const;
|
|
|
|
|
|
2026-03-30 11:40:45 +08:00
|
|
|
const FONT_PRESETS = [3, 5, 8, 12] as const;
|
|
|
|
|
|
|
|
|
|
function OrientationIcon(value: string): string {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 'n': return '↑';
|
|
|
|
|
case 'e': return '→';
|
|
|
|
|
case 's': return '↓';
|
|
|
|
|
case 'w': return '←';
|
|
|
|
|
default: return '↑';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 12:54:53 +08:00
|
|
|
function AlignIconSrc(value: string): string {
|
2026-03-30 11:40:45 +08:00
|
|
|
switch (value) {
|
2026-03-30 12:54:53 +08:00
|
|
|
case 'l': return alignLeftIcon;
|
|
|
|
|
case 'r': return alignRightIcon;
|
|
|
|
|
default: return alignCenterIcon;
|
2026-03-30 11:40:45 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function LayerEditorPanel(props: LayerEditorPanelProps) {
|
2026-02-27 15:33:23 +08:00
|
|
|
const { store } = props;
|
2026-03-30 11:40:45 +08:00
|
|
|
const [openDropdown, setOpenDropdown] = createSignal<string | null>(null);
|
|
|
|
|
let dropdownRef: HTMLDivElement | undefined;
|
2026-02-27 15:33:23 +08:00
|
|
|
|
2026-03-13 17:26:00 +08:00
|
|
|
const currentLayerConfigs = () =>
|
|
|
|
|
store.state.activeSide === 'front'
|
|
|
|
|
? store.state.frontLayerConfigs
|
|
|
|
|
: store.state.backLayerConfigs;
|
|
|
|
|
|
2026-02-27 22:56:06 +08:00
|
|
|
const updateLayerOrientation = (layerProp: string, orientation: 'n' | 's' | 'e' | 'w') => {
|
2026-03-13 17:26:00 +08:00
|
|
|
const updateFn = store.state.activeSide === 'front'
|
|
|
|
|
? store.actions.updateFrontLayerConfig
|
|
|
|
|
: store.actions.updateBackLayerConfig;
|
|
|
|
|
updateFn(layerProp, { orientation });
|
2026-03-30 11:40:45 +08:00
|
|
|
setOpenDropdown(null);
|
2026-02-27 22:56:06 +08:00
|
|
|
};
|
|
|
|
|
|
2026-02-28 12:28:23 +08:00
|
|
|
const updateLayerFontSize = (layerProp: string, fontSize?: number) => {
|
2026-03-13 17:26:00 +08:00
|
|
|
const updateFn = store.state.activeSide === 'front'
|
|
|
|
|
? store.actions.updateFrontLayerConfig
|
|
|
|
|
: store.actions.updateBackLayerConfig;
|
|
|
|
|
updateFn(layerProp, { fontSize });
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-27 15:17:25 +08:00
|
|
|
const updateLayerAlign = (layerProp: string, align?: 'l' | 'c' | 'r') => {
|
|
|
|
|
const updateFn = store.state.activeSide === 'front'
|
|
|
|
|
? store.actions.updateFrontLayerConfig
|
|
|
|
|
: store.actions.updateBackLayerConfig;
|
|
|
|
|
updateFn(layerProp, { align });
|
2026-03-30 11:40:45 +08:00
|
|
|
setOpenDropdown(null);
|
2026-03-27 15:17:25 +08:00
|
|
|
};
|
|
|
|
|
|
2026-03-13 17:26:00 +08:00
|
|
|
const toggleLayerVisible = (layerProp: string) => {
|
|
|
|
|
const toggleFn = store.state.activeSide === 'front'
|
|
|
|
|
? store.actions.toggleFrontLayerVisible
|
|
|
|
|
: store.actions.toggleBackLayerVisible;
|
|
|
|
|
toggleFn(layerProp);
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-30 12:01:08 +08:00
|
|
|
const selectLayer = (layerProp: string) => {
|
|
|
|
|
store.actions.setSelectedLayer(
|
|
|
|
|
store.state.selectedLayer === layerProp ? null : layerProp
|
2026-03-13 17:26:00 +08:00
|
|
|
);
|
2026-02-28 12:28:23 +08:00
|
|
|
};
|
|
|
|
|
|
2026-03-30 11:40:45 +08:00
|
|
|
const handleDropdownClick = (e: MouseEvent) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClickOutside = (e: MouseEvent) => {
|
|
|
|
|
if (dropdownRef && !dropdownRef.contains(e.target as Node)) {
|
|
|
|
|
setOpenDropdown(null);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
document.addEventListener('click', handleClickOutside);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onCleanup(() => {
|
|
|
|
|
document.removeEventListener('click', handleClickOutside);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const layerCount = () => currentLayerConfigs().length;
|
|
|
|
|
|
2026-02-27 15:33:23 +08:00
|
|
|
return (
|
|
|
|
|
<div class="w-64 flex-shrink-0">
|
2026-03-13 17:26:00 +08:00
|
|
|
<h3 class="font-bold mb-2 mt-0">
|
|
|
|
|
图层 ({store.state.activeSide === 'front' ? '正面' : '背面'})
|
|
|
|
|
</h3>
|
2026-02-27 15:33:23 +08:00
|
|
|
|
2026-03-30 11:40:45 +08:00
|
|
|
<div ref={dropdownRef}>
|
2026-03-13 17:26:00 +08:00
|
|
|
<For each={currentLayerConfigs()}>
|
2026-03-30 11:40:45 +08:00
|
|
|
{(layer, index) => (
|
|
|
|
|
<div
|
|
|
|
|
class={`flex items-center gap-1 py-1.5 px-1 ${
|
|
|
|
|
index() < layerCount() - 1 ? 'border-b border-gray-200' : ''
|
2026-03-30 12:01:08 +08:00
|
|
|
} ${
|
|
|
|
|
store.state.selectedLayer === layer.prop ? 'bg-blue-50' : ''
|
2026-03-30 11:40:45 +08:00
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={layer.visible}
|
|
|
|
|
onChange={() => toggleLayerVisible(layer.prop)}
|
|
|
|
|
class="cursor-pointer"
|
|
|
|
|
/>
|
2026-03-30 12:01:08 +08:00
|
|
|
<span
|
|
|
|
|
class="text-sm flex-1 truncate cursor-pointer hover:text-blue-600"
|
|
|
|
|
onClick={() => selectLayer(layer.prop)}
|
2026-03-30 11:40:45 +08:00
|
|
|
>
|
2026-03-30 12:01:08 +08:00
|
|
|
{layer.prop}
|
|
|
|
|
</span>
|
2026-03-30 11:40:45 +08:00
|
|
|
<div class="relative">
|
|
|
|
|
<button
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setOpenDropdown(openDropdown() === `orient-${layer.prop}` ? null : `orient-${layer.prop}`);
|
|
|
|
|
}}
|
|
|
|
|
class={`w-7 h-7 text-sm rounded cursor-pointer flex items-center justify-center bg-gray-200 text-gray-700 hover:bg-gray-300 ${
|
|
|
|
|
!layer.visible ? 'invisible pointer-events-none' : ''
|
|
|
|
|
}`}
|
|
|
|
|
title="方向"
|
|
|
|
|
>
|
|
|
|
|
{OrientationIcon(layer.orientation || 'n')}
|
|
|
|
|
</button>
|
|
|
|
|
{openDropdown() === `orient-${layer.prop}` && (
|
|
|
|
|
<div
|
|
|
|
|
class="absolute top-full right-0 mt-1 bg-white border border-gray-300 rounded shadow-lg z-10"
|
|
|
|
|
onClick={handleDropdownClick}
|
|
|
|
|
>
|
|
|
|
|
<For each={ORIENTATION_OPTIONS}>
|
|
|
|
|
{(opt) => (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => updateLayerOrientation(layer.prop, opt.value)}
|
|
|
|
|
class="block w-full text-left px-3 py-1.5 text-sm hover:bg-gray-100 cursor-pointer whitespace-nowrap"
|
|
|
|
|
>
|
|
|
|
|
{opt.label}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</For>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-02-27 22:56:06 +08:00
|
|
|
</div>
|
2026-03-30 11:40:45 +08:00
|
|
|
|
|
|
|
|
<div class="relative">
|
|
|
|
|
<button
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setOpenDropdown(openDropdown() === `align-${layer.prop}` ? null : `align-${layer.prop}`);
|
|
|
|
|
}}
|
2026-03-30 12:54:53 +08:00
|
|
|
class={`w-7 h-7 rounded cursor-pointer flex items-center justify-center bg-gray-200 hover:bg-gray-300 ${
|
2026-03-30 11:40:45 +08:00
|
|
|
!layer.visible ? 'invisible pointer-events-none' : ''
|
|
|
|
|
}`}
|
|
|
|
|
title="对齐"
|
|
|
|
|
>
|
2026-03-30 12:54:53 +08:00
|
|
|
<img src={AlignIconSrc(layer.align || '')} alt="align" class="w-5 h-5 not-prose" />
|
2026-03-30 11:40:45 +08:00
|
|
|
</button>
|
|
|
|
|
{openDropdown() === `align-${layer.prop}` && (
|
|
|
|
|
<div
|
|
|
|
|
class="absolute top-full right-0 mt-1 bg-white border border-gray-300 rounded shadow-lg z-10"
|
|
|
|
|
onClick={handleDropdownClick}
|
2026-03-13 12:34:39 +08:00
|
|
|
>
|
2026-03-30 11:40:45 +08:00
|
|
|
<For each={ALIGN_OPTIONS}>
|
|
|
|
|
{(opt) => (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => updateLayerAlign(layer.prop, opt.value as 'l' | 'c' | 'r' | undefined || undefined)}
|
2026-03-30 12:54:53 +08:00
|
|
|
class="flex items-center gap-2 w-full px-3 py-1.5 text-sm hover:bg-gray-100 cursor-pointer whitespace-nowrap"
|
2026-03-30 11:40:45 +08:00
|
|
|
>
|
2026-03-30 12:54:53 +08:00
|
|
|
<img src={opt.icon} alt="" class="w-4 h-4 not-prose max-w-none" />
|
2026-03-30 11:40:45 +08:00
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</For>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="relative">
|
|
|
|
|
<button
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setOpenDropdown(openDropdown() === `font-${layer.prop}` ? null : `font-${layer.prop}`);
|
|
|
|
|
}}
|
|
|
|
|
class={`w-7 h-7 text-xs rounded cursor-pointer flex items-center justify-center bg-gray-200 text-gray-700 hover:bg-gray-300 ${
|
|
|
|
|
!layer.visible ? 'invisible pointer-events-none' : ''
|
|
|
|
|
}`}
|
|
|
|
|
title="字体大小 (mm)"
|
|
|
|
|
>
|
|
|
|
|
{layer.fontSize ?? 3}
|
|
|
|
|
</button>
|
|
|
|
|
{openDropdown() === `font-${layer.prop}` && (
|
|
|
|
|
<div
|
|
|
|
|
class="absolute top-full right-0 mt-1 bg-white border border-gray-300 rounded shadow-lg z-10 p-2"
|
|
|
|
|
onClick={handleDropdownClick}
|
|
|
|
|
>
|
|
|
|
|
<div class="flex items-center gap-1 mb-2">
|
|
|
|
|
<input
|
|
|
|
|
type="number"
|
|
|
|
|
value={layer.fontSize ?? 3}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
const value = e.target.value;
|
|
|
|
|
updateLayerFontSize(layer.prop, value ? Number(value) : undefined);
|
|
|
|
|
}}
|
|
|
|
|
class="w-14 text-xs px-1.5 py-1 rounded border border-gray-300"
|
|
|
|
|
step="0.1"
|
|
|
|
|
min="0.1"
|
|
|
|
|
/>
|
|
|
|
|
<span class="text-xs text-gray-500">mm</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="flex gap-1">
|
|
|
|
|
<For each={FONT_PRESETS}>
|
|
|
|
|
{(preset) => (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => updateLayerFontSize(layer.prop, preset)}
|
|
|
|
|
class={`px-2 py-1 text-xs rounded cursor-pointer ${
|
|
|
|
|
(layer.fontSize ?? 3) === preset
|
|
|
|
|
? 'bg-blue-500 text-white'
|
|
|
|
|
: 'bg-gray-100 hover:bg-gray-200 text-gray-700'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{preset}
|
|
|
|
|
</button>
|
2026-03-13 12:34:39 +08:00
|
|
|
)}
|
|
|
|
|
</For>
|
2026-03-30 11:40:45 +08:00
|
|
|
</div>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => updateLayerFontSize(layer.prop, undefined)}
|
|
|
|
|
class="mt-2 w-full text-xs text-gray-500 hover:text-gray-700 cursor-pointer"
|
2026-03-27 15:17:25 +08:00
|
|
|
>
|
2026-03-30 11:40:45 +08:00
|
|
|
重置
|
|
|
|
|
</button>
|
2026-03-13 12:34:39 +08:00
|
|
|
</div>
|
2026-03-30 11:40:45 +08:00
|
|
|
)}
|
|
|
|
|
</div>
|
2026-02-27 15:33:23 +08:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</For>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<hr class="my-4" />
|
|
|
|
|
|
|
|
|
|
<button
|
2026-03-13 17:26:00 +08:00
|
|
|
onClick={() => store.actions.copyCode()}
|
|
|
|
|
class="w-full bg-blue-600 hover:bg-blue-700 text-white px-3 py-2 rounded text-sm font-medium cursor-pointer flex items-center gap-2 justify-center"
|
2026-02-27 15:33:23 +08:00
|
|
|
>
|
2026-03-13 17:26:00 +08:00
|
|
|
<span>📋</span>
|
|
|
|
|
<span>复制代码</span>
|
2026-02-27 15:33:23 +08:00
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-30 11:40:45 +08:00
|
|
|
|
2026-03-30 12:54:53 +08:00
|
|
|
export { LayerEditorPanel };
|