mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 20:12:52 +08:00
feat(skin): expose custom nodes to layout editor
This commit is contained in:
1 parent
c243f272fe
commit
067f0c4403
8 files changed
+153
-15
No files matched your search
@@ -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: {} },
|
||||
|
||||
@@ -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<string, SkinLayoutComponent>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user