From 067f0c440346022280d4190bf12933330777ab62 Mon Sep 17 00:00:00 2001 From: illusia <64828020+EchoSixHIYA@users.noreply.github.com> Date: Sat, 10 Oct 2026 03:56:15 +0800 Subject: [PATCH] feat(skin): expose custom nodes to layout editor --- docs/OPEN_SKIN_SYSTEM_ROADMAP.zh-CN.md | 7 ++++ docs/OPEN_SKIN_SYSTEM_SPEC.zh-CN.md | 2 +- docs/SKIN_DEVELOPMENT.md | 2 +- src/shared/skin-layout.test.ts | 37 ++++++++++++++++++++++ src/shared/skin-layout.ts | 36 +++++++++++++++++++-- web/src/components/SkinPluginOutlet.ts | 27 +++++++++++++--- web/src/composables/useSkinLayoutEditor.ts | 23 ++++++++++---- web/src/services/skin-extension-ui.test.ts | 34 ++++++++++++++++++++ 8 files changed, 153 insertions(+), 15 deletions(-) diff --git a/docs/OPEN_SKIN_SYSTEM_ROADMAP.zh-CN.md b/docs/OPEN_SKIN_SYSTEM_ROADMAP.zh-CN.md index 5a2c17f..13fa423 100644 --- a/docs/OPEN_SKIN_SYSTEM_ROADMAP.zh-CN.md +++ b/docs/OPEN_SKIN_SYSTEM_ROADMAP.zh-CN.md @@ -55,6 +55,13 @@ schema v2 支持公开组件布局、桌面/平板/手机配置、本地覆盖 - 插件生命周期包括批准前不加载、逐插件/逐权限批准、版本变化重新确认、撤销立即销毁上下文、超时/输出限额/崩溃隔离、旧包兼容和宿主恢复 UI。完整威胁模型、资源耗尽与跨浏览器测试未完成前不启用生产运行开关。 - 复核零新增预算约束:插件及本地素材随皮肤包分发,不依赖 CDN、外部脚本托管或付费沙箱服务。 +### R3.4:声明式自定义节点布局编辑(已实施) + +- 发现本地布局编辑器只登记了 WebSpeak 静态组件;皮肤 `components.json` 中自定义元素能用 CSS 排版,但不能由用户逐项移动、改尺寸或隐藏。 +- 为每个自定义组件根和每个实际 DOM 节点生成带 page/component 的动态布局 ID,接入现有每皮肤、每设备的本地覆盖、撤销/重做、导入导出和恢复流程。 +- 显式 `part` 名称用于保持关键节点覆盖稳定;未命名节点采用组件树路径,树结构调整可能改变对应覆盖。声明式可操作节点仍按至少 44 px 触控目标进行校验。 +- 本节点扩展的是已授权渲染的声明式皮肤 UI,不执行作者脚本;v4 插件多实例布局、安装授权 UI 和沙箱运行仍属未完成工作。 + ### R4:整页皮肤界面和宿主控件组合(进行中) - components.json schema v2 引入、schema v3 继续支持 `mode: "surface"`,允许替换首页或语音工作区的完整可见页面;旧 schema v1/v2 继续兼容。 diff --git a/docs/OPEN_SKIN_SYSTEM_SPEC.zh-CN.md b/docs/OPEN_SKIN_SYSTEM_SPEC.zh-CN.md index 7348a04..211194b 100644 --- a/docs/OPEN_SKIN_SYSTEM_SPEC.zh-CN.md +++ b/docs/OPEN_SKIN_SYSTEM_SPEC.zh-CN.md @@ -14,7 +14,7 @@ 皮肤格式按版本区分:v1 保持视觉皮肤兼容;v2 提供有界布局 JSON;v3 使用 open-skin 清单、受控 CSS 和声明式 components.json;v4 在 open-skin 包中增加必需的 plugins.json 和插件专属 JS/CSS/素材路径。客户端与服务端共用 v4 插件描述校验器,并检查引用文件、UTF-8 和大小;当前不执行或加载这些代码。v1/v2 的 CSS 继续拒绝布局类声明,旧包不会因升级而获得新权限;v3/v4 才允许 CSS 控制公开页面中的布局、位置、尺寸、顺序、文字排版和显隐。 -v3 自定义组件不是脚本插件。components.json schema v1 是既有格式;schema v2 增加 `mode: "surface"` 与宿主控件节点;schema v3 扩大自定义节点、嵌套、重复列表和声明式交互的边界,并增加映射到现有语音功能的动作。宿主仍解析固定元素/属性白名单、固定数据集合、简单绑定与有限动作,再由 Vue 创建节点。作者不必使用固定宿主控件目录,但 v3 仍不足以表达任意自定义 UI 逻辑和完整插件生命周期。管理员面板和演示页不能被 surface 替换。 +v3 自定义组件不是脚本插件。components.json schema v1 是既有格式;schema v2 增加 `mode: "surface"` 与宿主控件节点;schema v3 扩大自定义节点、嵌套、重复列表和声明式交互的边界,并增加映射到现有语音功能的动作。宿主仍解析固定元素/属性白名单、固定数据集合、简单绑定与有限动作,再由 Vue 创建节点。每个自定义组件根和每个有 DOM 的节点都会作为独立本地布局项,可移动、调整尺寸、改变外观或隐藏。节点指定 `part` 后使用该标识;未指定时使用树路径,树结构调整后旧的本地覆盖可能要重新设置。可操作节点保留至少 44 px 的触控目标。作者不必使用固定宿主控件目录,但 v3 仍不足以表达任意自定义 UI 逻辑和完整插件生命周期。管理员面板和演示页不能被 surface 替换。 宿主控件通过固定 ID 嵌入,始终由 WebSpeak 代码运行。当前可选注册表包括 `home.connection-form`、`app.skin-switcher`、`app.language-switcher`、`voice.channel-panel`、`voice.member-cards`、`voice.chat-panel`、`voice.audio-controls`、`voice.screen-share-player`、`voice.whisper-controls`、`voice.performance-panel`、`voice.connection-controls` 和 `voice.disconnect-control`。每个控件在同一页面最多出现一次;未知 ID 或重复的有状态控件会拒绝导入。作者可以不用这些控件,自行绘制界面并将点击、双击等事件绑定到公开数据与受权限保护的动作。 diff --git a/docs/SKIN_DEVELOPMENT.md b/docs/SKIN_DEVELOPMENT.md index 61ebda7..8fc61b5 100644 --- a/docs/SKIN_DEVELOPMENT.md +++ b/docs/SKIN_DEVELOPMENT.md @@ -114,7 +114,7 @@ v1 和 v2 都只支持公开页面。`/admin/**` 不加载布局、用户覆盖 v3 清单的 packageType 固定为 open-skin,entry 指向 CSS,components 指向 components.json;不含 v2 的 layout 或 permissions 字段。组件文件可以省略。仅 v3 开放公开页面的 CSS 排版能力,v1/v2 仍按旧 CSS 规则校验。客户端导入和服务端登记使用同一共享组件 schema 与权限白名单。 -components.json 顶层接受 schemaVersion 1、2 或 3 和 components 数组。schema v1 维持旧兼容;schema v2 引入 `mode: "widget"`(默认)或 `mode: "surface"`,并提供可选宿主控件节点;schema v3 保持这些能力,扩大声明式 UI 的节点/状态/动作边界,并增加用户操作事件和语音功能动作。v3 仍使用固定元素/属性、公开数据集合和宿主动作白名单,不是任意 HTML 或任意脚本。每个组件需声明 id、name、page、accessibleName、permissions、actions 和 root,可选 state。page 接受 home、voice、demo;surface 仅可用于 home 和 voice,并且必须请求 `ui.surface.replace`。用户批准前不会显示 surface,撤销后宿主立即恢复普通页面。v4 完整插件协议见[规格](./OPEN_SKIN_SYSTEM_SPEC.zh-CN.md#21-完整插件目标协议v4尚未实现),当前不可作为已支持包格式使用。 +components.json 顶层接受 schemaVersion 1、2 或 3 和 components 数组。schema v1 维持旧兼容;schema v2 引入 `mode: "widget"`(默认)或 `mode: "surface"`,并提供可选宿主控件节点;schema v3 保持这些能力,扩大声明式 UI 的节点/状态/动作边界,并增加用户操作事件和语音功能动作。v3 仍使用固定元素/属性、公开数据集合和宿主动作白名单,不是任意 HTML 或任意脚本。每个组件需声明 id、name、page、accessibleName、permissions、actions 和 root,可选 state。page 接受 home、voice、demo;surface 仅可用于 home 和 voice,并且必须请求 `ui.surface.replace`。用户批准前不会显示 surface,撤销后宿主立即恢复普通页面。所有皮肤组件根节点及其中的每个可见元素都以动态布局项加入本地布局编辑器,可独立移动、缩放、改尺寸、改外观或隐藏。按钮、输入框及显式用户操作节点保留 44 px 最小触控区域。为让用户覆盖在树结构调整后仍能指向同一元素,请给重要节点设置稳定的 `part` 名称;未设置时按组件树位置生成标识。v4 完整插件协议见[规格](./OPEN_SKIN_SYSTEM_SPEC.zh-CN.md#21-完整插件目标协议v4尚未实现),当前不可作为已支持包格式使用。 普通节点使用白名单 HTML/SVG 元素。节点可包含 text、part、className、受限 attributes、包内图片 asset、bindValue、本地 repeat/when、events 和 children。文本与属性支持简单的双大括号数据路径;不支持表达式、HTML 字符串或脚本。schema v2 另允许固定宿主控件节点,例如: diff --git a/src/shared/skin-layout.test.ts b/src/shared/skin-layout.test.ts index 7b6d9a4..3fab1ac 100644 --- a/src/shared/skin-layout.test.ts +++ b/src/shared/skin-layout.test.ts @@ -3,6 +3,7 @@ import { readFile, readdir } from "node:fs/promises"; import test from "node:test"; import { getSkinLayoutComponents, + getSkinLayoutComponent, parseSkinLayoutJson, SKIN_LAYOUT_COMPONENTS, validateSkinLayout, @@ -72,6 +73,42 @@ test("valid layouts accept only bounded positions and safe visual tokens", () => assert.equal(getSkinLayoutComponents("voice").some((component) => component.id === "home.join-card"), false); }); +test("skin-authored component roots and nodes become independently editable layout parts", () => { + const dynamicIds = [ + "skin.voice.toolbar", + "skin.voice.toolbar.node-root", + "skin.voice.toolbar.part-chat-list", + "skin.voice.toolbar.control-node-root-0", + ]; + const components = getSkinLayoutComponents("voice", dynamicIds); + assert.deepEqual(components.filter((component) => dynamicIds.includes(component.id)).map((component) => component.id), dynamicIds); + assert.ok(dynamicIds.every((id) => getSkinLayoutComponent(id, "voice")?.editable)); + assert.equal(getSkinLayoutComponent("skin.home.toolbar", "voice"), undefined); + assert.equal(getSkinLayoutComponent("skin.voice.toolbar.node-root-onclick", "voice"), undefined); + + const layout = validateSkinLayout({ + schemaVersion: 1, + pages: { voice: { desktop: { + "skin.voice.toolbar": { x: 18, y: 12, visible: true }, + "skin.voice.toolbar.node-root": { width: 720 }, + "skin.voice.toolbar.part-chat-list": { visible: false }, + "skin.voice.toolbar.control-node-root-0": { scale: 1, width: 60, height: 48 }, + } } }, + }); + assert.equal(layout.pages.voice?.desktop?.["skin.voice.toolbar.part-chat-list"]?.visible, false); + assert.equal(getSkinLayoutComponent("skin.voice.toolbar.control-node-root-0")?.minTouchTarget, 44); + assert.ok(getSkinLayoutComponent("skin.voice.toolbar.control-node-root-0")!.minScale >= 1); + + assert.throws(() => validateSkinLayout({ + schemaVersion: 1, + pages: { voice: { desktop: { "skin.home.toolbar": { x: 0 } } } }, + })); + assert.throws(() => validateSkinLayout({ + schemaVersion: 1, + pages: { voice: { desktop: { "skin.voice.toolbar.control-node-root-0": { scale: 0.8 } } } }, + })); +}); + test("layout validation fails closed for unknown pages, components, trusted notices, fields, and values", () => { const invalidValues: unknown[] = [ { schemaVersion: 2, pages: {} }, diff --git a/src/shared/skin-layout.ts b/src/shared/skin-layout.ts index bd81ddb..646e785 100644 --- a/src/shared/skin-layout.ts +++ b/src/shared/skin-layout.ts @@ -432,12 +432,42 @@ export const SKIN_LAYOUT_COMPONENTS: readonly SkinLayoutComponent[] = [ const componentById = new Map(SKIN_LAYOUT_COMPONENTS.map((component) => [component.id, component])); -export function getSkinLayoutComponents(page: SkinLayoutPage): SkinLayoutComponent[] { - return SKIN_LAYOUT_COMPONENTS.filter((component) => component.page === "common" || component.page === page); +function dynamicSkinPartPage(id: string): SkinLayoutPage | undefined { + const match = /^skin\.(home|voice|demo)\.([a-z][a-z0-9-]{0,63})(?:\.(control-)?(node-root(?:-\d+){0,32}|part-[a-z][a-z0-9-]{0,63}))?$/.exec(id); + return match?.[1] as SkinLayoutPage | undefined; +} + +function isDynamicSkinControl(id: string): boolean { + return /^skin\.(?:home|voice|demo)\.[a-z][a-z0-9-]{0,63}\.control-(?:node-root(?:-\d+){0,32}|part-[a-z][a-z0-9-]{0,63})$/.test(id); +} + +const dynamicSkinPartCache = new Map(); + +function dynamicSkinLayoutComponent(id: string, page: SkinLayoutPage): SkinLayoutComponent { + let component = dynamicSkinPartCache.get(id); + if (!component) { + component = describeComponent(id, page, isDynamicSkinControl(id) ? "button" : undefined); + if (dynamicSkinPartCache.size >= 4096) dynamicSkinPartCache.clear(); + dynamicSkinPartCache.set(id, component); + } + return component; +} + +/** Dynamic IDs are generated only for declared skin component roots and nodes. */ +export function getSkinLayoutComponents(page: SkinLayoutPage, dynamicParts: readonly string[] = []): SkinLayoutComponent[] { + const components = SKIN_LAYOUT_COMPONENTS.filter((component) => component.page === "common" || component.page === page); + const seen = new Set(components.map((component) => component.id)); + for (const id of dynamicParts) { + if (seen.has(id) || dynamicSkinPartPage(id) !== page) continue; + seen.add(id); + components.push(dynamicSkinLayoutComponent(id, page)); + } + return components; } export function getSkinLayoutComponent(id: string, page?: SkinLayoutPage): SkinLayoutComponent | undefined { - const component = componentById.get(id); + const component = componentById.get(id) + ?? (dynamicSkinPartPage(id) ? dynamicSkinLayoutComponent(id, dynamicSkinPartPage(id)!) : undefined); if (!component || (page && component.page !== "common" && component.page !== page)) return undefined; return component; } diff --git a/web/src/components/SkinPluginOutlet.ts b/web/src/components/SkinPluginOutlet.ts index 377f976..5dd20d8 100644 --- a/web/src/components/SkinPluginOutlet.ts +++ b/web/src/components/SkinPluginOutlet.ts @@ -153,7 +153,16 @@ export default defineComponent({ return url; } - function renderNode(component: SkinPluginComponent, node: SkinPluginNode, context: SafeContext, state: Record, skipRepeat = false): VNodeChild { + function layoutPart(component: SkinPluginComponent, node: SkinPluginNode, nodePath: string): string { + const interactive = ["a", "button", "input", "select", "summary", "textarea"].includes(node.tag ?? "") + || node.attributes?.role === "button" + || ["click", "dblclick", "contextmenu", "keydown", "keyup", "pointerdown", "pointerup", "drop"] + .some((eventName) => Boolean(node.events?.[eventName as keyof typeof node.events])); + const part = node.part ? `part-${node.part}` : nodePath; + return `skin.${component.page}.${component.id}.${interactive ? "control-" : ""}${part}`; + } + + function renderNode(component: SkinPluginComponent, node: SkinPluginNode, context: SafeContext, state: Record, skipRepeat = false, nodePath = "node-root"): VNodeChild { if (node.widget) { if (node.when) { const value = resolvePath(node.when.path, context, state); @@ -165,15 +174,23 @@ export default defineComponent({ class: ["ws-plugin-host-widget", node.className], "data-ws-plugin-widget": node.widget, "data-ws-plugin-part": node.part ? `${component.id}.${node.part}` : component.id, + "data-ws-part": layoutPart(component, node, nodePath), "aria-label": component.accessibleName, }, [widget()]); } - if (!node.tag) return interpolate(node.text ?? "", context, state); + if (!node.tag) { + const text = interpolate(node.text ?? "", context, state); + return node.part ? h("span", { + class: "ws-plugin-node", + "data-ws-plugin-part": `${component.id}.${node.part}`, + "data-ws-part": layoutPart(component, node, nodePath), + }, text) : text; + } if (!skipRepeat && node.repeat) { const values = resolvePath(node.repeat.path, context, state); if (!Array.isArray(values)) return null; const repeatLimit = props.document.schemaVersion >= 3 ? SKIN_PLUGIN_REPEAT_LIMIT : 100; - return h(Fragment, null, values.slice(0, repeatLimit).map((value) => renderNode(component, node, { ...context, [node.repeat!.as]: value }, state, true))); + return h(Fragment, null, values.slice(0, repeatLimit).map((value) => renderNode(component, node, { ...context, [node.repeat!.as]: value }, state, true, nodePath))); } if (node.when) { const value = resolvePath(node.when.path, context, state); @@ -191,6 +208,7 @@ export default defineComponent({ attrs.class = ["ws-plugin-node", node.className, attrs.class].filter(Boolean).join(" "); } else attrs.class = "ws-plugin-node"; attrs["data-ws-plugin-part"] = node.part ? `${component.id}.${node.part}` : component.id; + attrs["data-ws-part"] = layoutPart(component, node, nodePath); if (node.bindValue) attrs.value = state[node.bindValue]; for (const [eventName, actionId] of Object.entries(node.events ?? {})) { @@ -220,7 +238,7 @@ export default defineComponent({ }; } if (node.tag === "form" && !node.events?.submit) attrs.onSubmit = (event: Event) => event.preventDefault(); - const children = (node.children ?? []).map((child) => renderNode(component, child, context, state)); + const children = (node.children ?? []).map((child, index) => renderNode(component, child, context, state, false, `${nodePath}-${index}`)); return h(node.tag, attrs, children); } @@ -257,6 +275,7 @@ export default defineComponent({ "data-ws-plugin-component": component.id, "data-ws-plugin-name": component.name, "data-ws-plugin-part": component.id, + "data-ws-part": `skin.${component.page}.${component.id}`, ...(component.mode === "surface" ? { "data-ws-plugin-surface": component.page } : {}), "aria-label": component.accessibleName, }, [renderNode(component, component.root, props.data, localState(component))])); diff --git a/web/src/composables/useSkinLayoutEditor.ts b/web/src/composables/useSkinLayoutEditor.ts index b81d9d6..ca35c72 100644 --- a/web/src/composables/useSkinLayoutEditor.ts +++ b/web/src/composables/useSkinLayoutEditor.ts @@ -60,6 +60,11 @@ function componentIdForElement(element: HTMLElement): string | null { return getSkinLayoutComponentId(element.dataset.wsPart, element.dataset.wsControlKind); } +function dynamicPartsInRoot(root: HTMLElement): string[] { + return [...root.querySelectorAll("[data-ws-part^='skin.']")] + .map((element) => element.dataset.wsPart ?? ""); +} + export function useSkinLayoutEditor() { const isOpen = ref(false); const isEditing = ref(false); @@ -86,8 +91,8 @@ export function useSkinLayoutEditor() { const available = computed(() => Boolean(context.value)); const components = computed(() => { revision.value; - const page = context.value?.page; - return page ? getSkinLayoutComponents(page).filter((component) => component.editable) : []; + const active = context.value; + return active ? getSkinLayoutComponents(active.page, dynamicPartsInRoot(active.root)).filter((component) => component.editable) : []; }); const filteredComponents = computed(() => { const search = filter.value.trim().toLowerCase(); @@ -281,7 +286,7 @@ export function useSkinLayoutEditor() { historyIndex = history.length ? 0 : -1; lastHistoryChangeAt = 0; updateHistoryControls(); - const first = next ? getSkinLayoutComponents(next.page).find((component) => component.editable)?.id ?? "" : ""; + const first = next ? getSkinLayoutComponents(next.page, dynamicPartsInRoot(next.root)).find((component) => component.editable)?.id ?? "" : ""; if (!next || !getSkinLayoutComponent(selectedComponentId.value, next.page)?.editable) selectedComponentId.value = first; reviseLayout(); setSkinLayoutEditorState(isEditing.value, selectedComponentId.value || null); @@ -555,16 +560,22 @@ export function useSkinLayoutEditor() { onMounted(() => { initializeSkinLayoutRuntime(); syncContext(); - observer = new MutationObserver(() => { + observer = new MutationObserver((records) => { syncContext(); - if (focusPreviewEnabled.value) revision.value += 1; + const includesDynamicPart = (node: Node) => node instanceof Element + && (node.matches("[data-ws-part^='skin.']") || Boolean(node.querySelector("[data-ws-part^='skin.']"))); + const dynamicPartsChanged = records.some((record) => + (record.type === "attributes" && record.attributeName === "data-ws-part" + && record.target instanceof Element && record.target.matches("[data-ws-part^='skin.']")) + || (record.type === "childList" && [...record.addedNodes, ...record.removedNodes].some(includesDynamicPart))); + if (dynamicPartsChanged || focusPreviewEnabled.value) revision.value += 1; }); observer.observe(document.body, { childList: true, subtree: true, characterData: true, attributes: true, - attributeFilter: ["data-ws-page", "data-ws-skin", "lang", "tabindex", "disabled", "href", "aria-label", "aria-labelledby", "title", "placeholder", "hidden", "inert", "aria-hidden", "class", "style"], + attributeFilter: ["data-ws-page", "data-ws-skin", "data-ws-part", "lang", "tabindex", "disabled", "href", "aria-label", "aria-labelledby", "title", "placeholder", "hidden", "inert", "aria-hidden", "class", "style"], }); document.addEventListener("pointerdown", startDrag, true); document.addEventListener("pointermove", moveDrag, true); diff --git a/web/src/services/skin-extension-ui.test.ts b/web/src/services/skin-extension-ui.test.ts index 6e9f46a..e8e9aaa 100644 --- a/web/src/services/skin-extension-ui.test.ts +++ b/web/src/services/skin-extension-ui.test.ts @@ -95,3 +95,37 @@ test("SkinPluginOutlet renders validated dynamic output and preserves package UI assert.doesNotMatch(collisionHtml, /Generated by Wasm/); assert.match(collisionHtml, /Package UI/); }); + +test("skin-authored DOM nodes expose independent layout parts and retain touch bounds for controls", async () => { + const document: SkinPluginDocument = { + schemaVersion: 3, + components: [{ + id: "voice-toolbar", + name: "Voice toolbar", + page: "voice", + accessibleName: "Voice toolbar", + permissions: [], + actions: {}, + root: { + tag: "section", + children: [ + { tag: "button", part: "disconnect", attributes: { type: "button" }, children: [{ text: "Disconnect" }] }, + { text: "Voice tools", part: "caption" }, + ], + }, + }], + }; + const html = await renderToString(createSSRApp(SkinPluginOutlet, { + skinId: "community.test", + skinVersion: "1.0.0", + document, + page: "voice", + data: {}, + assets: {}, + actions: {}, + })); + assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar"/); + assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.node-root"/); + assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.control-part-disconnect"/); + assert.match(html, /data-ws-part="skin\.voice\.voice-toolbar\.part-caption"/); +});