feat(skins): complete visual component CRUD authoring

This commit is contained in:
illusia committed 2026-10-10 15:53:51 +08:00
1 parent 3fd8e76a98
commit f2e5695bb6
30 files changed
+1858 -511

No files matched your search

+35 -12
View File
@@ -1,7 +1,8 @@
<template>
<Teleport to="body">
<div v-if="editor.available.value" class="skin-layout-editor" data-ws-layout-editor>
<div v-if="editor.available.value" ref="editorRoot" class="skin-layout-editor" data-ws-layout-editor @keydown.esc.stop.prevent="editor.setOpen(false)">
<button
ref="triggerButton"
class="skin-layout-trigger"
type="button"
:aria-expanded="editor.isOpen.value"
@@ -25,7 +26,7 @@
<h2>{{ editor.labels.value.title }}</h2>
<p>{{ editor.labels.value.hint }}</p>
</div>
<button type="button" class="skin-layout-close" :aria-label="editor.labels.value.close" @click="editor.setOpen(false)">×</button>
<button ref="closeButton" type="button" class="skin-layout-close" :aria-label="editor.labels.value.close" @click="editor.setOpen(false)">×</button>
</header>
<label class="skin-layout-toggle">
@@ -135,11 +136,21 @@
</template>
<script setup lang="ts">
import { ref } from "vue";
import { nextTick, ref, watch } from "vue";
import { useSkinLayoutEditor } from "../composables/useSkinLayoutEditor.js";
import { promoteHostEditorToTopLayer } from "../services/host-editor-top-layer.js";
const editor = useSkinLayoutEditor();
const editorRoot = ref<HTMLElement | null>(null);
const importInput = ref<HTMLInputElement | null>(null);
const triggerButton = ref<HTMLButtonElement | null>(null);
const closeButton = ref<HTMLButtonElement | null>(null);
watch(editorRoot, promoteHostEditorToTopLayer, { flush: "post" });
watch(editor.isOpen, async (isOpen) => {
await nextTick();
(isOpen ? closeButton.value : triggerButton.value)?.focus();
});
async function onImport(event: Event): Promise<void> {
const input = event.target as HTMLInputElement;
@@ -151,10 +162,22 @@ async function onImport(event: Event): Promise<void> {
<style scoped>
.skin-layout-editor {
position: fixed;
z-index: 2147483000;
inset: auto;
z-index: 2147483647;
top: auto;
left: auto;
right: 14px;
bottom: 14px;
width: auto;
height: auto;
max-width: none;
max-height: none;
overflow: visible;
margin: 0;
padding: 0;
border: 0;
color: #172033;
background: transparent;
font: 13px/1.45 Inter, ui-sans-serif, system-ui, sans-serif;
}
@@ -199,28 +222,28 @@ async function onImport(event: Event): Promise<void> {
.skin-layout-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.skin-layout-heading h2 { margin: 0; font-size: 16px; }
.skin-layout-heading p { margin: 5px 0 0; color: #475569; font-size: 12px; }
.skin-layout-close { width: 32px; height: 32px; border-radius: 8px; color: #334155; background: #f1f5f9; font-size: 22px !important; cursor: pointer; }
.skin-layout-toggle { display: flex; align-items: center; gap: 8px; min-height: 38px; }
.skin-layout-close { width: 44px; height: 44px; border-radius: 8px; color: #334155; background: #f1f5f9; font-size: 22px !important; cursor: pointer; }
.skin-layout-toggle { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.skin-layout-field { display: grid; gap: 6px; }
.skin-layout-field > span,
.skin-layout-range label,
.skin-layout-colors span { color: #475569; font-size: 12px; }
.skin-layout-field input[type="search"],
.skin-layout-field select { width: 100%; min-height: 36px; padding: 6px 9px; border: 1px solid #cbd5e1; border-radius: 8px; color: #172033; background: #fff; }
.skin-layout-field select { width: 100%; min-height: 44px; padding: 6px 9px; border: 1px solid #cbd5e1; border-radius: 8px; color: #172033; background: #fff; }
.skin-layout-range { display: grid; gap: 3px; }
.skin-layout-range label { display: flex; justify-content: space-between; gap: 8px; }
.skin-layout-range output { color: #172033; font-variant-numeric: tabular-nums; }
.skin-layout-range input { width: 100%; accent-color: #4f46e5; }
.skin-layout-range input { width: 100%; min-height: 44px; accent-color: #4f46e5; }
.skin-layout-colors { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.skin-layout-colors label { display: grid; gap: 4px; }
.skin-layout-colors input { width: 100%; height: 34px; padding: 2px; border: 1px solid #cbd5e1; border-radius: 7px; background: #fff; }
.skin-layout-colors input { width: 100%; height: 44px; padding: 2px; border: 1px solid #cbd5e1; border-radius: 7px; background: #fff; }
.skin-layout-actions { display: flex; flex-wrap: wrap; gap: 7px; }
.skin-layout-actions button { min-height: 36px; padding: 0 10px; border: 1px solid #cbd5e1; border-radius: 8px; color: #172033; background: #f8fafc; cursor: pointer; }
.skin-layout-actions button { min-height: 44px; padding: 0 10px; border: 1px solid #cbd5e1; border-radius: 8px; color: #172033; background: #f8fafc; cursor: pointer; }
.skin-layout-actions button:disabled { opacity: .5; cursor: not-allowed; }
.skin-layout-file { display: none; }
.skin-layout-status { margin: 0; color: #4338ca; }
.skin-layout-warning { margin: 0; padding: 8px 10px; border: 1px solid #fcd34d; border-radius: 8px; color: #854d0e; background: #fffbeb; }
.skin-layout-warning button { min-height: 32px; margin-top: 6px; padding: 0 9px; border: 1px solid #d97706; border-radius: 7px; color: #78350f; background: #fff; cursor: pointer; }
.skin-layout-warning button { min-height: 44px; margin-top: 6px; padding: 0 9px; border: 1px solid #d97706; border-radius: 7px; color: #78350f; background: #fff; cursor: pointer; }
.skin-layout-context { margin: 0; color: #64748b; font-size: 11px; overflow-wrap: anywhere; }
.skin-layout-editor :focus-visible { outline: 2px solid #4f46e5; outline-offset: 2px; }
.skin-layout-focus-list { display: grid; gap: 6px; max-height: 220px; overflow: auto; margin: 0; padding: 0; list-style: none; }
@@ -233,7 +256,7 @@ async function onImport(event: Event): Promise<void> {
.skin-layout-focus-name { flex: 1; }
.skin-layout-focus-name small, .skin-layout-focus-size small { color: #475569; font-size: 10px; }
.skin-layout-focus-size { flex: 0 0 auto; text-align: right; }
.skin-layout-focus-overlay { position: fixed; z-index: 2147482999; inset: 0; pointer-events: none; }
.skin-layout-focus-overlay { position: fixed; z-index: 2147483647; inset: 0; pointer-events: none; }
.skin-layout-focus-marker { position: fixed; box-sizing: border-box; display: grid; place-items: start; padding: 0 2px; border: 2px solid #15803d; border-radius: 3px; color: #fff; background: #15803d40; font: 700 11px/16px ui-sans-serif, system-ui, sans-serif; }
.skin-layout-focus-marker.is-small { border-color: #b91c1c; background: #b91c1c40; }
+124 -78
View File
@@ -1,83 +1,94 @@
<template>
<div v-if="skinId" class="skin-plugin-editor" data-ws-plugin-editor>
<button ref="triggerButton" class="skin-plugin-trigger" type="button" aria-controls="skin-plugin-panel" :aria-expanded="open" @click="open = !open" @keydown.esc.stop.prevent="open = false">
{{ english ? "Components" : "组件结构" }}
</button>
<section v-if="open" id="skin-plugin-panel" class="skin-plugin-panel" role="dialog" aria-modal="false" tabindex="-1" :aria-label="english ? 'Skin component editor' : '皮肤组件编辑器'" @keydown.esc.stop.prevent="open = false">
<header class="skin-plugin-heading">
<div>
<h2>{{ english ? "Skin components" : "皮肤组件" }}</h2>
<p>{{ english ? "Edits are local to this skin version and use the same permission and markup checks as packages." : "修改保存在本机当前皮肤版本中,并继续使用皮肤包相同的权限和标记校验。" }}</p>
</div>
<button ref="closeButton" type="button" class="skin-plugin-close" :aria-label="english ? 'Close' : '关闭'" @click="open = false">×</button>
</header>
<Teleport to="body">
<div v-if="skinId" ref="editorRoot" class="skin-plugin-editor" data-ws-plugin-editor>
<button ref="triggerButton" class="skin-plugin-trigger" type="button" aria-controls="skin-plugin-panel" :aria-expanded="open" @click="open = !open" @keydown.esc.stop.prevent="open = false">
{{ english ? "Components" : "组件结构" }}
</button>
<section v-if="open" id="skin-plugin-panel" class="skin-plugin-panel" role="dialog" aria-modal="false" tabindex="-1" :aria-label="english ? 'Skin component editor' : '皮肤组件编辑器'" @keydown.esc.stop.prevent="open = false">
<header class="skin-plugin-heading">
<div>
<h2>{{ english ? "Skin components" : "皮肤组件" }}</h2>
<p>{{ english ? "Edits are local to this skin version and use the same permission and markup checks as packages." : "修改保存在本机当前皮肤版本中,并继续使用皮肤包相同的权限和标记校验。" }}</p>
</div>
<button ref="closeButton" type="button" class="skin-plugin-close" :aria-label="english ? 'Close' : '关闭'" @click="open = false">×</button>
</header>
<div class="skin-plugin-actions">
<button type="button" :disabled="draftDirty || storageInvalid" @click="createComponent">{{ english ? "Add component" : "新增组件" }}</button>
<button type="button" :disabled="!canUndo || draftDirty || storageInvalid" @click="undo">{{ english ? "Undo" : "撤销" }}</button>
<button type="button" :disabled="!canRedo || draftDirty || storageInvalid" @click="redo">{{ english ? "Redo" : "重做" }}</button>
</div>
<label class="skin-plugin-label">
<span>{{ english ? "Components" : "组件列表" }}</span>
<select :value="selectedComponentId" :disabled="draftDirty || storageInvalid" @change="selectComponent(($event.target as HTMLSelectElement).value)">
<option value="">{{ english ? "Select a component" : "选择组件" }}</option>
<option v-for="component in document.components" :key="component.id" :value="component.id">{{ component.name }} · {{ component.page }} · {{ component.id }}</option>
</select>
</label>
<p v-if="!selectedComponent" class="skin-plugin-empty">{{ english ? "No skin components yet. Add one to begin." : "当前皮肤还没有自定义组件,可以从新增组件开始。" }}</p>
<template v-if="selectedComponent">
<div class="skin-plugin-actions">
<button type="button" :disabled="draftDirty || storageInvalid || !canMoveComponent(-1)" :aria-label="english ? 'Move component up' : '组件上移'" @click="moveComponent(-1)">↑</button>
<button type="button" :disabled="draftDirty || storageInvalid || !canMoveComponent(1)" :aria-label="english ? 'Move component down' : '组件下移'" @click="moveComponent(1)">↓</button>
<button type="button" :disabled="draftDirty || storageInvalid" @click="duplicateComponent">{{ english ? "Duplicate component" : "复制组件" }}</button>
<button type="button" class="is-danger" :disabled="draftDirty || storageInvalid" @click="deleteComponent">{{ english ? "Delete component" : "删除组件" }}</button>
<button type="button" :disabled="draftDirty || storageInvalid" @click="createComponent">{{ english ? "Add component" : "新增组件" }}</button>
<button type="button" :disabled="!canUndo || draftDirty || storageInvalid" @click="undo">{{ english ? "Undo" : "撤销" }}</button>
<button type="button" :disabled="!canRedo || draftDirty || storageInvalid" @click="redo">{{ english ? "Redo" : "重做" }}</button>
</div>
<label class="skin-plugin-label">
<span>{{ english ? "Component tree" : "组件树" }}</span>
<select :value="selectedPathKey" :disabled="draftDirty || storageInvalid" @change="selectPath(($event.target as HTMLSelectElement).value)">
<option v-for="row in nodeRows" :key="row.key" :value="row.key" :style="{ paddingInlineStart: (row.depth * 12) + 'px' }">{{ row.label }}</option>
<span>{{ english ? "Components" : "组件列表" }}</span>
<select :value="selectedComponentId" :disabled="draftDirty || storageInvalid" @change="selectComponent(($event.target as HTMLSelectElement).value)">
<option value="">{{ english ? "Select a component" : "选择组件" }}</option>
<option v-for="component in document.components" :key="component.id" :value="component.id">{{ component.name }} · {{ component.page }} · {{ component.id }}</option>
</select>
</label>
<div class="skin-plugin-actions">
<button type="button" :disabled="!canAddChild || draftDirty || storageInvalid" @click="addChild">{{ english ? "Add child" : "添加子节点" }}</button>
<button type="button" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="addSibling">{{ english ? "Add sibling" : "添加同级节点" }}</button>
<button type="button" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="duplicateNode">{{ english ? "Duplicate node" : "复制节点" }}</button>
<button type="button" :disabled="!selectedPath.length || !canMove(-1) || draftDirty || storageInvalid" :aria-label="english ? 'Move node up' : '节点上移'" @click="moveNode(-1)">↑</button>
<button type="button" :disabled="!selectedPath.length || !canMove(1) || draftDirty || storageInvalid" :aria-label="english ? 'Move node down' : '节点下移'" @click="moveNode(1)">↓</button>
<button type="button" class="is-danger" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="deleteNode">{{ english ? "Delete node" : "删除节点" }}</button>
</div>
<p v-if="!selectedComponent" class="skin-plugin-empty">{{ english ? "No skin components yet. Add one to begin." : "当前皮肤还没有自定义组件,可以从新增组件开始。" }}</p>
<template v-if="selectedComponent">
<div class="skin-plugin-actions">
<button type="button" :disabled="draftDirty || storageInvalid || !canMoveComponent(-1)" :aria-label="english ? 'Move component up' : '组件上移'" @click="moveComponent(-1)">↑</button>
<button type="button" :disabled="draftDirty || storageInvalid || !canMoveComponent(1)" :aria-label="english ? 'Move component down' : '组件下移'" @click="moveComponent(1)">↓</button>
<button type="button" :disabled="draftDirty || storageInvalid" @click="duplicateComponent">{{ english ? "Duplicate component" : "复制组件" }}</button>
<button type="button" class="is-danger" :disabled="draftDirty || storageInvalid" @click="deleteComponent">{{ english ? "Delete component" : "删除组件" }}</button>
</div>
<label class="skin-plugin-label skin-plugin-json-label">
<span>{{ editorTarget === "component" ? (english ? "Component definition" : "组件定义") : (english ? "Node definition" : "节点定义") }}</span>
<small>{{ english ? "JSON edits are validated before they are saved or rendered." : "JSON 修改会先经过 schema、权限和安全校验,再保存和渲染。" }}</small>
<textarea v-model="draftJson" :disabled="storageInvalid" spellcheck="false" :aria-label="english ? 'Component or node JSON' : '组件或节点 JSON'"></textarea>
</label>
<div class="skin-plugin-actions">
<button type="button" class="is-primary" :disabled="storageInvalid" @click="applyDraft">{{ english ? "Apply definition" : "应用定义" }}</button>
<button type="button" @click="resetDraft">{{ english ? "Discard draft" : "放弃草稿" }}</button>
</div>
</template>
<label class="skin-plugin-label">
<span>{{ english ? "Component tree" : "组件树" }}</span>
<select :value="selectedPathKey" :disabled="draftDirty || storageInvalid" @change="selectPath(($event.target as HTMLSelectElement).value)">
<option value="">{{ english ? "Select a node to edit" : "选择节点进行编辑" }}</option>
<option v-for="row in nodeRows" :key="row.key" :value="row.key" :style="{ paddingInlineStart: (row.depth * 12) + 'px' }">{{ row.label }}</option>
</select>
</label>
<div class="skin-plugin-actions skin-plugin-file-actions">
<button type="button" @click="exportDocument">{{ english ? "Export components.json" : "导出 components.json" }}</button>
<button type="button" :disabled="draftDirty || storageInvalid" @click="importInput?.click()">{{ english ? "Import components.json" : "导入 components.json" }}</button>
<button type="button" :disabled="!hasLocalEdits || draftDirty" @click="resetAll">{{ english ? "Reset local edits" : "清除本地修改" }}</button>
<input ref="importInput" class="skin-plugin-file" type="file" accept="application/json,.json" @change="importDocument" />
</div>
<p v-if="errorMessage" class="skin-plugin-error" role="alert">{{ errorMessage }}</p>
<p v-else-if="draftDirty" class="skin-plugin-status" role="status">{{ english ? "Unapplied draft. Apply it or discard it before other edits." : "有尚未应用的草稿;请先应用或放弃,再进行其他修改。" }}</p>
<p v-else-if="statusMessage" class="skin-plugin-status" role="status">{{ statusMessage }}</p>
<p class="skin-plugin-context">{{ skinId }}@{{ skinVersion }}</p>
</section>
</div>
<label class="skin-plugin-label">
<span>{{ english ? "New node type" : "新增节点类型" }}</span>
<select :value="newNodeType" :disabled="draftDirty || storageInvalid" :aria-label="english ? 'New node type' : '新增节点类型'" @change="newNodeType = ($event.target as HTMLSelectElement).value">
<option value="text">{{ english ? "Text node" : "文本节点" }}</option>
<option v-for="tag in allowedElementTags" :key="tag" :value="tag">{{ "<" + tag + ">" }}</option>
</select>
</label>
<div class="skin-plugin-actions">
<button type="button" :disabled="!canAddChild || draftDirty || storageInvalid" @click="addChild">{{ english ? "Add child" : "添加子节点" }}</button>
<button type="button" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="addSibling">{{ english ? "Add sibling" : "添加同级节点" }}</button>
<button type="button" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="duplicateNode">{{ english ? "Duplicate node" : "复制节点" }}</button>
<button type="button" :disabled="!selectedPath.length || !canMove(-1) || draftDirty || storageInvalid" :aria-label="english ? 'Move node up' : '节点上移'" @click="moveNode(-1)">↑</button>
<button type="button" :disabled="!selectedPath.length || !canMove(1) || draftDirty || storageInvalid" :aria-label="english ? 'Move node down' : '节点下移'" @click="moveNode(1)">↓</button>
<button type="button" class="is-danger" :disabled="!selectedPath.length || draftDirty || storageInvalid" @click="deleteNode">{{ english ? "Delete node" : "删除节点" }}</button>
</div>
<label class="skin-plugin-label skin-plugin-json-label">
<span>{{ editorTarget === "component" ? (english ? "Component definition" : "组件定义") : (english ? "Node definition" : "节点定义") }}</span>
<small>{{ english ? "JSON edits are validated before they are saved or rendered." : "JSON 修改会先经过 schema、权限和安全校验,再保存和渲染。" }}</small>
<textarea v-model="draftJson" :disabled="storageInvalid" spellcheck="false" :aria-label="english ? 'Component or node JSON' : '组件或节点 JSON'"></textarea>
</label>
<div class="skin-plugin-actions">
<button type="button" class="is-primary" :disabled="storageInvalid" @click="applyDraft">{{ english ? "Apply definition" : "应用定义" }}</button>
<button type="button" @click="resetDraft">{{ english ? "Discard draft" : "放弃草稿" }}</button>
</div>
</template>
<div class="skin-plugin-actions skin-plugin-file-actions">
<button type="button" @click="exportDocument">{{ english ? "Export components.json" : "导出 components.json" }}</button>
<button type="button" :disabled="draftDirty || storageInvalid" @click="importInput?.click()">{{ english ? "Import components.json" : "导入 components.json" }}</button>
<button type="button" :disabled="!hasLocalEdits || draftDirty" @click="resetAll">{{ english ? "Reset local edits" : "清除本地修改" }}</button>
<input ref="importInput" class="skin-plugin-file" type="file" accept="application/json,.json" @change="importDocument" />
</div>
<p v-if="errorMessage" class="skin-plugin-error" role="alert">{{ errorMessage }}</p>
<p v-else-if="draftDirty" class="skin-plugin-status" role="status">{{ english ? "Unapplied draft. Apply it or discard it before other edits." : "有尚未应用的草稿;请先应用或放弃,再进行其他修改。" }}</p>
<p v-else-if="statusMessage" class="skin-plugin-status" role="status">{{ statusMessage }}</p>
<p class="skin-plugin-context">{{ skinId }}@{{ skinVersion }}</p>
</section>
</div>
</Teleport>
</template>
<script setup lang="ts">
import { computed, nextTick, ref, watch } from "vue";
import { computed, nextTick, onMounted, ref, watch } from "vue";
import {
addSkinPluginComponent,
addSkinPluginNode,
@@ -92,7 +103,7 @@ import {
replaceSkinPluginComponent,
replaceSkinPluginNode,
} from "../../../src/shared/skin-plugin-editor.js";
import type { SkinPluginComponent, SkinPluginDocument, SkinPluginNode } from "../../../src/shared/skin-plugin.js";
import { SKIN_PLUGIN_ALLOWED_ELEMENTS, type SkinPluginComponent, type SkinPluginDocument, type SkinPluginNode } from "../../../src/shared/skin-plugin.js";
import {
clearSkinPluginAuthoringDocument,
exportSkinPluginAuthoringDocument,
@@ -101,6 +112,7 @@ import {
loadSkinPluginAuthoringDocument,
saveSkinPluginAuthoringDocument,
} from "../services/skin-plugin-authoring.js";
import { promoteHostEditorToTopLayer } from "../services/host-editor-top-layer.js";
const props = defineProps<{
skinId: string;
@@ -113,17 +125,19 @@ const emit = defineEmits<{ updated: [] }>();
type NodeRow = { key: string; label: string; depth: number };
const emptyDocument: SkinPluginDocument = { schemaVersion: 3, components: [] };
const allowedElementTags = SKIN_PLUGIN_ALLOWED_ELEMENTS;
const voidNodeTags = new Set(["br", "circle", "hr", "img", "input", "line", "path", "polyline", "rect", "wbr"]);
const svgContainerTags = new Set(["svg", "g"]);
const open = ref(false);
const document = ref<SkinPluginDocument>(emptyDocument);
const selectedComponentId = ref("");
const selectedPath = ref<number[]>([]);
const editorTarget = ref<"component" | "node">("component");
const newNodeType = ref("div");
const draftJson = ref("");
const errorMessage = ref("");
const statusMessage = ref("");
const importInput = ref<HTMLInputElement | null>(null);
const editorRoot = ref<HTMLElement | null>(null);
const triggerButton = ref<HTMLButtonElement | null>(null);
const closeButton = ref<HTMLButtonElement | null>(null);
const history = ref<SkinPluginDocument[]>([]);
@@ -134,7 +148,9 @@ const english = computed(() => (props.lang ?? "").toLowerCase().startsWith("en")
const selectedComponent = computed(() => document.value.components.find((component) => component.id === selectedComponentId.value) ?? null);
const selectedNode = computed(() => selectedComponent.value ? getSkinPluginNode(document.value, selectedComponentId.value, selectedPath.value) : null);
const canAddChild = computed(() => Boolean(selectedNode.value?.tag && !voidNodeTags.has(selectedNode.value.tag)));
const selectedPathKey = computed(() => selectedPath.value.join("."));
const selectedPathKey = computed(() => editorTarget.value === "node"
? (selectedPath.value.length ? selectedPath.value.join(".") : "root")
: "");
const canUndo = computed(() => historyIndex.value > 0);
const canRedo = computed(() => historyIndex.value >= 0 && historyIndex.value < history.value.length - 1);
const canMoveComponent = (direction: -1 | 1) => {
@@ -153,7 +169,7 @@ const nodeRows = computed<NodeRow[]>(() => {
const visit = (node: SkinPluginNode, path: number[], depth: number) => {
const name = node.widget ? "widget: " + node.widget : node.tag ? "<" + node.tag + ">" : "text: " + (node.text ?? "").slice(0, 42);
const label = (path.length ? path.map((index) => index + 1).join(".") : "root") + " · " + name;
rows.push({ key: path.join("."), label, depth });
rows.push({ key: path.length ? path.join(".") : "root", label, depth });
node.children?.forEach((child, index) => visit(child, [...path, index], depth + 1));
};
visit(component.root, [], 0);
@@ -191,6 +207,17 @@ function message(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
function createNodeDefinition(): SkinPluginNode {
const type = newNodeType.value;
if (type === "text") return { text: english.value ? "New content" : "新建内容" };
if (!SKIN_PLUGIN_ALLOWED_ELEMENTS.some((tag) => tag === type)) throw new Error("Choose a supported safe node type.");
const node: SkinPluginNode = { tag: type };
if (type === "img") node.attributes = { alt: english.value ? "Skin image" : "皮肤图片" };
if (type === "svg") node.children = [{ tag: "g" }];
else if (type !== "g" && !voidNodeTags.has(type)) node.children = [{ text: english.value ? "New content" : "新建内容" }];
return node;
}
function snapshot<T>(value: T): T {
return JSON.parse(JSON.stringify(value)) as T;
}
@@ -234,8 +261,13 @@ function selectComponent(id: string): void {
}
function selectPath(key: string): void {
selectedPath.value = key ? key.split(".").map(Number) : [];
editorTarget.value = "node";
if (!key) {
selectedPath.value = [];
editorTarget.value = "component";
} else {
selectedPath.value = key === "root" ? [] : key.split(".").map(Number);
editorTarget.value = "node";
}
resetDraft();
}
@@ -327,9 +359,8 @@ function addChild(): void {
if (!selectedNode.value?.tag || voidNodeTags.has(selectedNode.value.tag)) return;
const parent = selectedPath.value;
const childIndex = selectedNode.value.children?.length ?? 0;
const tag = svgContainerTags.has(selectedNode.value.tag) ? "g" : "div";
try {
const next = addSkinPluginNode(document.value, selectedComponentId.value, parent, { tag, children: [{ text: english.value ? "New content" : "新建内容" }] });
const next = addSkinPluginNode(document.value, selectedComponentId.value, parent, createNodeDefinition());
if (!save(next)) return;
selectedPath.value = [...parent, childIndex];
editorTarget.value = "node";
@@ -341,9 +372,9 @@ function addSibling(): void {
if (!selectedPath.value.length) return;
const path = [...selectedPath.value];
const parent = getSkinPluginNode(document.value, selectedComponentId.value, path.slice(0, -1));
const tag = parent.tag && svgContainerTags.has(parent.tag) ? "g" : "span";
try {
const next = addSkinPluginNodeSibling(document.value, selectedComponentId.value, path, { tag, children: [{ text: english.value ? "New content" : "新建内容" }] });
if (!parent.tag) throw new Error("Only element nodes can contain siblings.");
const next = addSkinPluginNodeSibling(document.value, selectedComponentId.value, path, createNodeDefinition());
if (!save(next)) return;
path[path.length - 1] += 1;
selectedPath.value = path;
@@ -477,6 +508,9 @@ async function importDocument(event: Event): Promise<void> {
watch(() => [props.skinId, props.skinVersion, props.baseDocument] as const, loadDocument, { immediate: true });
watch(() => [selectedComponentId.value, selectedPathKey.value, editorTarget.value] as const, resetDraft);
onMounted(() => {
promoteHostEditorToTopLayer(editorRoot.value);
});
watch(open, async (isOpen) => {
await nextTick();
const target = isOpen ? closeButton.value : triggerButton.value;
@@ -485,7 +519,7 @@ watch(open, async (isOpen) => {
</script>
<style scoped>
.skin-plugin-editor { position: fixed; z-index: 2147482998; right: 14px; bottom: 72px; color: #172033; font: 13px/1.45 Inter, ui-sans-serif, system-ui, sans-serif; }
.skin-plugin-editor { position: fixed; inset: auto; z-index: 2147483647; top: auto; left: auto; right: 14px; bottom: 72px; width: auto; height: auto; max-width: none; max-height: none; overflow: visible; margin: 0; padding: 0; border: 0; color: #172033; background: transparent; font: 13px/1.45 Inter, ui-sans-serif, system-ui, sans-serif; }
.skin-plugin-editor button, .skin-plugin-editor select, .skin-plugin-editor textarea { font: inherit; }
.skin-plugin-trigger { min-height: 44px; padding: 0 14px; border: 1px solid #cbd5e1; border-radius: 999px; color: #fff; background: #4f46e5; box-shadow: 0 5px 22px #0f172a55; cursor: pointer; }
.skin-plugin-panel { position: absolute; right: 0; bottom: 50px; display: grid; gap: 10px; width: min(460px, calc(100vw - 24px)); max-height: min(82vh, 820px); overflow: auto; padding: 14px; border: 1px solid #cbd5e1; border-radius: 15px; background: #fff; box-shadow: 0 18px 60px #0f172a44; }
@@ -508,5 +542,17 @@ watch(open, async (isOpen) => {
.skin-plugin-status { color: #4338ca; }
.skin-plugin-context { color: #64748b; font-size: 10px; }
.skin-plugin-editor :focus-visible { outline: 2px solid #4f46e5; outline-offset: 2px; }
@media (max-width: 520px) { .skin-plugin-editor { right: 10px; bottom: 62px; } .skin-plugin-panel { max-height: 76vh; } }
@media (max-width: 520px) {
.skin-plugin-editor { right: max(10px, env(safe-area-inset-right)); bottom: calc(10px + env(safe-area-inset-bottom)); }
.skin-plugin-panel {
box-sizing: border-box;
width: min(460px, calc(100vw - 20px - env(safe-area-inset-left) - env(safe-area-inset-right)));
max-height: min(76vh, 620px);
}
}
@supports (height: 100dvh) {
@media (max-width: 520px) {
.skin-plugin-panel { max-height: min(620px, calc(100dvh - 76px - env(safe-area-inset-top) - env(safe-area-inset-bottom))); }
}
}
</style>
+27 -3
View File
@@ -4,6 +4,7 @@ import {
SKIN_PLUGIN_PERMISSIONS,
SKIN_PLUGIN_RENDER_NODE_LIMIT,
SKIN_PLUGIN_REPEAT_LIMIT,
listSkinPluginAssetPaths,
parseSkinPluginDocument,
type SkinPluginActionDefinition,
type SkinPluginComponent,
@@ -120,12 +121,35 @@ export default defineComponent({
refresh.value += 1;
}, { immediate: true });
watch(() => [props.skinId, props.skinVersion, props.assets] as const, () => assetUrls.clear());
watch(renderedComponents, (components) => {
assetUrls.retain(listSkinPluginAssetPaths({ schemaVersion: 3, components }));
const activeStateKeys = new Set(components.map(componentStateKey));
for (const key of Object.keys(stateByComponent)) {
if (!activeStateKeys.has(key)) delete stateByComponent[key];
}
}, { immediate: true });
watch(activeSurface, (surface) => emit("surface-change", Boolean(surface)), { immediate: true, flush: "sync" });
function componentStateKey(component: SkinPluginComponent): string {
return JSON.stringify([props.skinId, props.skinVersion, props.componentNamespace, component.id]);
}
function localState(component: SkinPluginComponent): Record<string, Scalar> {
const key = `${props.skinId}/${component.id}`;
if (!stateByComponent[key]) stateByComponent[key] = { ...(component.state ?? {}) };
return stateByComponent[key];
const key = componentStateKey(component);
const defaults = component.state ?? {};
let state = stateByComponent[key];
if (!state) {
state = { ...defaults };
stateByComponent[key] = state;
return state;
}
for (const stateKey of Object.keys(state)) {
if (!Object.hasOwn(defaults, stateKey)) delete state[stateKey];
}
for (const [stateKey, value] of Object.entries(defaults)) {
if (!Object.hasOwn(state, stateKey) || typeof state[stateKey] !== typeof value) state[stateKey] = value;
}
return state;
}
function permissionLabel(permission: string): string {