mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat(web): implement open skin layouts and migrate Illusia
This commit is contained in:
1 parent
58559cf839
commit
02441410a0
60 files changed
+6511
-50
No files matched your search
@@ -0,0 +1,245 @@
|
||||
export const SKIN_EXTENSION_MANIFEST_SCHEMA_VERSION = 1 as const;
|
||||
export const SKIN_EXTENSION_API_VERSION = 1 as const;
|
||||
export const SKIN_EXTENSION_MESSAGE_CHANNEL = "webspeak.skin-extension" as const;
|
||||
|
||||
export const SKIN_EXTENSION_PERMISSION_DEFINITIONS = Object.freeze({
|
||||
"session.status.read": Object.freeze({
|
||||
policyVersion: 1,
|
||||
risk: "low",
|
||||
description: "Read a minimal snapshot of the current voice session status.",
|
||||
}),
|
||||
} as const);
|
||||
|
||||
export type SkinExtensionPermissionId = keyof typeof SKIN_EXTENSION_PERMISSION_DEFINITIONS;
|
||||
|
||||
export interface SkinExtensionManifest {
|
||||
schemaVersion: typeof SKIN_EXTENSION_MANIFEST_SCHEMA_VERSION;
|
||||
packageType: "extension";
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
apiVersion: typeof SKIN_EXTENSION_API_VERSION;
|
||||
permissions: readonly SkinExtensionPermissionId[];
|
||||
}
|
||||
|
||||
export type SkinExtensionPermissionVersions = Readonly<Partial<Record<SkinExtensionPermissionId, number>>>;
|
||||
|
||||
/** A host-created approval record. Package contents can request permissions but cannot create this record. */
|
||||
export interface SkinExtensionApproval {
|
||||
pluginId: string;
|
||||
apiVersion: number;
|
||||
permissions: SkinExtensionPermissionVersions;
|
||||
}
|
||||
|
||||
export interface SkinExtensionUpgradeConsentDelta {
|
||||
addedPermissions: SkinExtensionPermissionId[];
|
||||
changedMeaningPermissions: SkinExtensionPermissionId[];
|
||||
removedPermissions: SkinExtensionPermissionId[];
|
||||
apiVersionChanged: boolean;
|
||||
requiresConsent: boolean;
|
||||
}
|
||||
|
||||
export interface SkinExtensionSessionStatus {
|
||||
connected: boolean;
|
||||
channelName: string | null;
|
||||
memberCount: number;
|
||||
}
|
||||
|
||||
export interface SkinExtensionRequestMessage {
|
||||
channel: typeof SKIN_EXTENSION_MESSAGE_CHANNEL;
|
||||
apiVersion: typeof SKIN_EXTENSION_API_VERSION;
|
||||
nonce: string;
|
||||
id: string;
|
||||
type: "request";
|
||||
permission: SkinExtensionPermissionId;
|
||||
payload: Record<string, never>;
|
||||
}
|
||||
|
||||
export class SkinExtensionValidationError extends Error {
|
||||
constructor(readonly code: string, message: string) {
|
||||
super(message);
|
||||
this.name = "SkinExtensionValidationError";
|
||||
}
|
||||
}
|
||||
|
||||
const MANIFEST_FIELDS = new Set(["schemaVersion", "packageType", "id", "name", "version", "apiVersion", "permissions"]);
|
||||
const PLUGIN_ID_PATTERN = /^[a-z0-9]+(?:[.-][a-z0-9]+)*$/;
|
||||
const SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
|
||||
const KNOWN_PERMISSIONS = new Set<string>(Object.keys(SKIN_EXTENSION_PERMISSION_DEFINITIONS));
|
||||
|
||||
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
||||
const prototype = Object.getPrototypeOf(value);
|
||||
return prototype === Object.prototype || prototype === null;
|
||||
}
|
||||
|
||||
function boundedString(value: unknown, field: string, maximumLength: number): string {
|
||||
if (typeof value !== "string" || value.length === 0 || value.length > maximumLength || value.trim() !== value) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_MANIFEST_INVALID", `Invalid ${field}.`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function parseSkinExtensionManifest(input: unknown): SkinExtensionManifest {
|
||||
if (!isPlainRecord(input)) throw new SkinExtensionValidationError("SKIN_EXTENSION_MANIFEST_INVALID", "The extension manifest must be an object.");
|
||||
if (Object.keys(input).some((key) => !MANIFEST_FIELDS.has(key))) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_UNKNOWN_FIELD", "The extension manifest contains an unsupported field.");
|
||||
}
|
||||
if (input.schemaVersion !== SKIN_EXTENSION_MANIFEST_SCHEMA_VERSION || input.packageType !== "extension") {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_SCHEMA_UNSUPPORTED", "The extension package schema is not supported.");
|
||||
}
|
||||
|
||||
const id = boundedString(input.id, "id", 100);
|
||||
if (!PLUGIN_ID_PATTERN.test(id)) throw new SkinExtensionValidationError("SKIN_EXTENSION_MANIFEST_INVALID", "The extension id must use lowercase letters, numbers, dots, or hyphens.");
|
||||
const name = boundedString(input.name, "name", 120);
|
||||
const version = boundedString(input.version, "version", 64);
|
||||
if (!SEMVER_PATTERN.test(version)) throw new SkinExtensionValidationError("SKIN_EXTENSION_MANIFEST_INVALID", "The extension version must use semantic version syntax.");
|
||||
if (input.apiVersion !== SKIN_EXTENSION_API_VERSION) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_API_UNSUPPORTED", "The extension API version is not supported.");
|
||||
}
|
||||
if (!Array.isArray(input.permissions) || input.permissions.length > 8) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_MANIFEST_INVALID", "The extension permissions must be a small array.");
|
||||
}
|
||||
|
||||
const permissions: SkinExtensionPermissionId[] = [];
|
||||
for (const permission of input.permissions) {
|
||||
if (typeof permission !== "string" || !KNOWN_PERMISSIONS.has(permission)) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_PERMISSION_UNKNOWN", "The extension requests an unsupported permission.");
|
||||
}
|
||||
if (permissions.includes(permission as SkinExtensionPermissionId)) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_PERMISSION_DUPLICATE", "The extension requests a permission more than once.");
|
||||
}
|
||||
permissions.push(permission as SkinExtensionPermissionId);
|
||||
}
|
||||
|
||||
return Object.freeze({
|
||||
schemaVersion: SKIN_EXTENSION_MANIFEST_SCHEMA_VERSION,
|
||||
packageType: "extension",
|
||||
id,
|
||||
name,
|
||||
version,
|
||||
apiVersion: SKIN_EXTENSION_API_VERSION,
|
||||
permissions: Object.freeze(permissions),
|
||||
});
|
||||
}
|
||||
|
||||
/** Called only by a future host approval flow after the user approves the exact capabilities. */
|
||||
export function createSkinExtensionApproval(manifest: SkinExtensionManifest): SkinExtensionApproval {
|
||||
const permissions: Partial<Record<SkinExtensionPermissionId, number>> = {};
|
||||
for (const permission of manifest.permissions) {
|
||||
permissions[permission] = SKIN_EXTENSION_PERMISSION_DEFINITIONS[permission].policyVersion;
|
||||
}
|
||||
return Object.freeze({
|
||||
pluginId: manifest.id,
|
||||
apiVersion: manifest.apiVersion,
|
||||
permissions: Object.freeze(permissions),
|
||||
});
|
||||
}
|
||||
|
||||
export function parseSkinExtensionApproval(input: unknown): SkinExtensionApproval {
|
||||
if (!isPlainRecord(input) || Object.keys(input).some((key) => !["pluginId", "apiVersion", "permissions"].includes(key))) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_APPROVAL_INVALID", "The extension approval record is invalid.");
|
||||
}
|
||||
const pluginId = boundedString(input.pluginId, "approval.pluginId", 100);
|
||||
if (!PLUGIN_ID_PATTERN.test(pluginId) || input.apiVersion !== SKIN_EXTENSION_API_VERSION || !isPlainRecord(input.permissions)) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_APPROVAL_INVALID", "The extension approval record is invalid.");
|
||||
}
|
||||
const permissions: Partial<Record<SkinExtensionPermissionId, number>> = {};
|
||||
for (const [permission, policyVersion] of Object.entries(input.permissions)) {
|
||||
if (!KNOWN_PERMISSIONS.has(permission) || !Number.isSafeInteger(policyVersion) || (policyVersion as number) < 1) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_APPROVAL_INVALID", "The extension approval record contains an unsupported grant.");
|
||||
}
|
||||
permissions[permission as SkinExtensionPermissionId] = policyVersion as number;
|
||||
}
|
||||
return Object.freeze({
|
||||
pluginId,
|
||||
apiVersion: SKIN_EXTENSION_API_VERSION,
|
||||
permissions: Object.freeze(permissions),
|
||||
});
|
||||
}
|
||||
|
||||
export function getSkinExtensionUpgradeConsentDelta(
|
||||
previousApproval: SkinExtensionApproval,
|
||||
nextManifest: SkinExtensionManifest,
|
||||
): SkinExtensionUpgradeConsentDelta {
|
||||
if (previousApproval.pluginId !== nextManifest.id) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_ID_CHANGED", "An approval cannot be reused for a different extension id.");
|
||||
}
|
||||
|
||||
const addedPermissions = nextManifest.permissions.filter((permission) => previousApproval.permissions[permission] === undefined);
|
||||
const changedMeaningPermissions = nextManifest.permissions.filter((permission) => {
|
||||
const grantedPolicyVersion = previousApproval.permissions[permission];
|
||||
return grantedPolicyVersion !== undefined
|
||||
&& grantedPolicyVersion !== SKIN_EXTENSION_PERMISSION_DEFINITIONS[permission].policyVersion;
|
||||
});
|
||||
const removedPermissions = Object.keys(previousApproval.permissions)
|
||||
.filter((permission): permission is SkinExtensionPermissionId => KNOWN_PERMISSIONS.has(permission))
|
||||
.filter((permission) => !nextManifest.permissions.includes(permission));
|
||||
const apiVersionChanged = previousApproval.apiVersion !== nextManifest.apiVersion;
|
||||
|
||||
return {
|
||||
addedPermissions,
|
||||
changedMeaningPermissions,
|
||||
removedPermissions,
|
||||
apiVersionChanged,
|
||||
requiresConsent: addedPermissions.length > 0 || changedMeaningPermissions.length > 0 || apiVersionChanged,
|
||||
};
|
||||
}
|
||||
|
||||
export function isSkinExtensionPermissionApproved(
|
||||
approval: SkinExtensionApproval,
|
||||
pluginId: string,
|
||||
permission: SkinExtensionPermissionId,
|
||||
): boolean {
|
||||
return approval.pluginId === pluginId
|
||||
&& approval.permissions[permission] === SKIN_EXTENSION_PERMISSION_DEFINITIONS[permission].policyVersion;
|
||||
}
|
||||
|
||||
export function isSkinExtensionBootstrapHello(
|
||||
event: { source: unknown; origin: string; data: unknown; ports: readonly unknown[] },
|
||||
expectedSource: unknown,
|
||||
expectedNonce: string,
|
||||
): boolean {
|
||||
if (event.source !== expectedSource || event.origin !== "null" || event.ports.length !== 1 || !isPlainRecord(event.data)) return false;
|
||||
const data = event.data;
|
||||
return Object.keys(data).length === 4
|
||||
&& data.channel === SKIN_EXTENSION_MESSAGE_CHANNEL
|
||||
&& data.type === "hello"
|
||||
&& data.apiVersion === SKIN_EXTENSION_API_VERSION
|
||||
&& data.nonce === expectedNonce;
|
||||
}
|
||||
|
||||
export function parseSkinExtensionRequestMessage(value: unknown, expectedNonce: string): SkinExtensionRequestMessage | null {
|
||||
if (!isPlainRecord(value) || !isPlainRecord(value.payload)) return null;
|
||||
const expectedFields = ["channel", "apiVersion", "nonce", "id", "type", "permission", "payload"];
|
||||
if (Object.keys(value).length !== expectedFields.length || expectedFields.some((field) => !(field in value))) return null;
|
||||
if (Object.keys(value.payload).length !== 0) return null;
|
||||
if (value.channel !== SKIN_EXTENSION_MESSAGE_CHANNEL || value.apiVersion !== SKIN_EXTENSION_API_VERSION
|
||||
|| value.nonce !== expectedNonce || value.type !== "request") return null;
|
||||
if (typeof value.id !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(value.id)) return null;
|
||||
if (typeof value.permission !== "string" || !KNOWN_PERMISSIONS.has(value.permission)) return null;
|
||||
return value as unknown as SkinExtensionRequestMessage;
|
||||
}
|
||||
|
||||
/** Copies only the low-sensitivity fields registered for the prototype capability. */
|
||||
export function sanitizeSkinExtensionSessionStatus(value: unknown): SkinExtensionSessionStatus {
|
||||
if (!isPlainRecord(value) || typeof value.connected !== "boolean") {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_STATUS_INVALID", "The host session status is invalid.");
|
||||
}
|
||||
const channelName = value.channelName;
|
||||
const memberCount = value.memberCount;
|
||||
if (channelName !== null && typeof channelName !== "string") {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_STATUS_INVALID", "The host session status is invalid.");
|
||||
}
|
||||
if (!Number.isSafeInteger(memberCount) || (memberCount as number) < 0 || (memberCount as number) > 10000) {
|
||||
throw new SkinExtensionValidationError("SKIN_EXTENSION_STATUS_INVALID", "The host session status is invalid.");
|
||||
}
|
||||
const safeChannelName = typeof channelName === "string"
|
||||
? channelName.slice(0, 120).replace(/[\u0000-\u001f\u007f]/g, " ")
|
||||
: null;
|
||||
return Object.freeze({
|
||||
connected: value.connected,
|
||||
channelName: safeChannelName,
|
||||
memberCount: memberCount as number,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import test from "node:test";
|
||||
import {
|
||||
getSkinLayoutComponents,
|
||||
parseSkinLayoutJson,
|
||||
SKIN_LAYOUT_COMPONENTS,
|
||||
validateSkinLayout,
|
||||
canEditSkinLayoutComponent,
|
||||
} from "./skin-layout.js";
|
||||
|
||||
async function vueSources(directory: URL): Promise<string[]> {
|
||||
const files = (await readdir(directory, { recursive: true })).filter((file) => file.endsWith(".vue"));
|
||||
return Promise.all(files.map((file) => readFile(new URL(file.replaceAll("\\", "/"), directory), "utf8")));
|
||||
}
|
||||
|
||||
test("the stable layout registry covers public skin hooks and excludes the admin console", async () => {
|
||||
const [webClient, demo, sharedComponents, skinSwitcher, languageSwitcher, admin, adminComponents] = await Promise.all([
|
||||
readFile(new URL("../../web/src/views/WebClient.vue", import.meta.url), "utf8"),
|
||||
readFile(new URL("../../web/src/views/DemoView.vue", import.meta.url), "utf8"),
|
||||
vueSources(new URL("../../web/src/components/web-client/", import.meta.url)),
|
||||
readFile(new URL("../../web/src/components/SkinSwitcher.vue", import.meta.url), "utf8"),
|
||||
readFile(new URL("../../web/src/components/LanguageSwitcher.vue", import.meta.url), "utf8"),
|
||||
readFile(new URL("../../web/src/views/AdminView.vue", import.meta.url), "utf8"),
|
||||
vueSources(new URL("../../web/src/components/admin/", import.meta.url)),
|
||||
]);
|
||||
const publicSource = [webClient, demo, ...sharedComponents, skinSwitcher, languageSwitcher].join("\n");
|
||||
const publicParts = new Set([...publicSource.matchAll(/data-ws-part="([^"]+)"/g)].map((match) => match[1]));
|
||||
const interactiveParts = new Set<string>();
|
||||
for (const match of publicSource.matchAll(/<[A-Za-z][\w.-]*\b[^>]*>/gs)) {
|
||||
const attributes = match[0];
|
||||
const part = attributes.match(/\bdata-ws-part="([^"]+)"/)?.[1];
|
||||
if (!part) continue;
|
||||
const isNativeControl = /^<(?:button|a|input|select|textarea)\b/i.test(attributes);
|
||||
const hasButtonRole = /\brole="button"/.test(attributes);
|
||||
const keyboardAction = /@keydown\.(?:enter|space)\b/.test(attributes);
|
||||
const dragAction = /@(?:dragstart|dragover|dragleave|drop)\b|:draggable=/.test(attributes);
|
||||
if (isNativeControl || hasButtonRole || keyboardAction || dragAction) interactiveParts.add(part);
|
||||
}
|
||||
const registered = new Set(SKIN_LAYOUT_COMPONENTS.map((component) => component.selectorPart));
|
||||
for (const part of publicParts) assert.ok(registered.has(part), "Unregistered public layout hook: " + part);
|
||||
for (const part of interactiveParts) {
|
||||
const component = SKIN_LAYOUT_COMPONENTS.find((entry) => entry.id === part);
|
||||
assert.ok(component, "Interactive public hook must have its own component metadata: " + part);
|
||||
assert.ok(component.minTouchTarget >= 44, "Interactive public hook needs a 44px touch target: " + part);
|
||||
assert.ok(component.minWidth >= 44 && component.minHeight >= 44, "Interactive public hook needs bounded minimum dimensions: " + part);
|
||||
assert.ok(component.minScale >= 1, "Interactive public hook must not scale below its natural hit target: " + part);
|
||||
}
|
||||
assert.match(publicSource, /data-ws-part="demo\.voice-card"[\s\S]*?role="button"[\s\S]*?tabindex="0"[\s\S]*?@keydown\.enter[\s\S]*?@keydown\.space/);
|
||||
assert.doesNotMatch([admin, ...adminComponents].join("\n"), /ws-skin-root|data-ws-page=/);
|
||||
assert.ok(SKIN_LAYOUT_COMPONENTS.every((component) => !component.id.startsWith("admin.")));
|
||||
});
|
||||
|
||||
test("valid layouts accept only bounded positions and safe visual tokens", () => {
|
||||
const layout = validateSkinLayout({
|
||||
schemaVersion: 1,
|
||||
pages: {
|
||||
home: {
|
||||
desktop: {
|
||||
"home.header": { x: -120, y: 40, scale: 1.25, visible: true, foreground: "#aabbcc", radius: 16 },
|
||||
"control.button": { background: "#010203", border: "#ffffff" },
|
||||
},
|
||||
mobile: { "home.join-card": { x: 8, scale: 0.9 } },
|
||||
tablet: { "home.join-card": { width: 720, height: 460 } },
|
||||
},
|
||||
},
|
||||
});
|
||||
assert.equal(layout.pages.home?.desktop?.["home.header"]?.x, -120);
|
||||
assert.equal(layout.pages.home?.desktop?.["home.header"]?.foreground, "#aabbcc");
|
||||
assert.equal(layout.pages.home?.tablet?.["home.join-card"]?.width, 720);
|
||||
assert.equal(getSkinLayoutComponents("home").some((component) => component.id === "home.join-card"), true);
|
||||
assert.equal(getSkinLayoutComponents("voice").some((component) => component.id === "home.join-card"), false);
|
||||
});
|
||||
|
||||
test("layout validation fails closed for unknown pages, components, trusted notices, fields, and values", () => {
|
||||
const invalidValues: unknown[] = [
|
||||
{ schemaVersion: 2, pages: {} },
|
||||
{ schemaVersion: 1, pages: { admin: { desktop: {} } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.not-registered": { x: 0 } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.security-note": { visible: false } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { app: { visible: false } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.header": { position: "fixed" } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.header": { x: 2001 } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.header": { width: 8 } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "voice.connection-status": { visible: false } } } } },
|
||||
{ schemaVersion: 1, pages: { home: { desktop: { "home.header": { background: "url(https://example.invalid)" } } } } },
|
||||
];
|
||||
for (const invalid of invalidValues) assert.throws(() => validateSkinLayout(invalid));
|
||||
});
|
||||
|
||||
test("registered components carry accessibility, responsive, purpose, and data-scope metadata", () => {
|
||||
assert.ok(SKIN_LAYOUT_COMPONENTS.length > 100);
|
||||
for (const component of SKIN_LAYOUT_COMPONENTS) {
|
||||
assert.ok(component.name.length > 0);
|
||||
assert.notEqual(component.name, component.id, "registry names must be readable metadata rather than the ID repeated");
|
||||
assert.ok(component.purpose.length > 0);
|
||||
assert.ok(["core", "optional", "trusted-chrome"].includes(component.category));
|
||||
if (component.editable) {
|
||||
assert.deepEqual(component.allowedContainers, ["app"]);
|
||||
assert.deepEqual(component.layoutModes, ["position", "size", "visibility", "appearance"]);
|
||||
assert.ok(component.minWidth > 0 && component.maxWidth >= component.minWidth);
|
||||
assert.ok(component.minHeight > 0 && component.maxHeight >= component.minHeight);
|
||||
assert.ok(component.minScale > 0 && component.maxScale >= component.minScale);
|
||||
assert.ok(component.allowedStyleTokens.length > 0);
|
||||
} else {
|
||||
assert.deepEqual(component.allowedContainers, []);
|
||||
assert.deepEqual(component.layoutModes, []);
|
||||
assert.deepEqual(component.allowedStyleTokens, []);
|
||||
}
|
||||
assert.equal(component.keyboardBehavior, "host-native");
|
||||
assert.equal(component.focusOrder, "host-dom");
|
||||
assert.equal(component.accessibleNamePolicy, "preserve-host-name");
|
||||
assert.ok(component.narrowScreenBehavior.length > 0);
|
||||
assert.ok(component.dataSensitivity.length > 0);
|
||||
}
|
||||
assert.equal(SKIN_LAYOUT_COMPONENTS.find((component) => component.id === "home.connect")?.minTouchTarget, 44);
|
||||
assert.equal(SKIN_LAYOUT_COMPONENTS.find((component) => component.id === "home.feature")?.category, "optional");
|
||||
assert.equal(SKIN_LAYOUT_COMPONENTS.find((component) => component.id === "home.security-note")?.category, "trusted-chrome");
|
||||
assert.equal(SKIN_LAYOUT_COMPONENTS.find((component) => component.id === "home.identity-import.security")?.dataSensitivity, "identity");
|
||||
assert.equal(SKIN_LAYOUT_COMPONENTS.find((component) => component.id === "demo.voice-card")?.minTouchTarget, 44);
|
||||
});
|
||||
|
||||
test("trusted host components cannot be changed through a nested generic control", () => {
|
||||
assert.equal(canEditSkinLayoutComponent("control.button", "home", ["home.form"]), true);
|
||||
assert.equal(canEditSkinLayoutComponent("control.button", "home", ["home.identity-import.security"]), false);
|
||||
assert.equal(canEditSkinLayoutComponent("control.button", "voice", ["voice.screen-share-error"]), false);
|
||||
assert.equal(canEditSkinLayoutComponent("home.security-note", "home"), false);
|
||||
});
|
||||
|
||||
test("touch controls cannot be shrunk below their registered hit-target bounds", () => {
|
||||
assert.throws(() => validateSkinLayout({
|
||||
schemaVersion: 1,
|
||||
pages: { home: { desktop: { "home.connect": { scale: 0.75 } } } },
|
||||
}));
|
||||
assert.throws(() => validateSkinLayout({
|
||||
schemaVersion: 1,
|
||||
pages: { home: { desktop: { "control.button": { width: 20 } } } },
|
||||
}));
|
||||
assert.equal(validateSkinLayout({
|
||||
schemaVersion: 1,
|
||||
pages: { home: { desktop: { "home.feature": { width: 20, scale: 0.5 } } } },
|
||||
}).pages.home?.desktop?.["home.feature"]?.width, 20);
|
||||
});
|
||||
|
||||
test("layout JSON parsing rejects malformed and oversized documents", () => {
|
||||
assert.throws(() => parseSkinLayoutJson("{"));
|
||||
assert.throws(() => parseSkinLayoutJson(" ".repeat(128 * 1024 + 1)));
|
||||
assert.deepEqual(parseSkinLayoutJson('{"schemaVersion":1,"pages":{}}'), { schemaVersion: 1, pages: {} });
|
||||
});
|
||||
@@ -0,0 +1,559 @@
|
||||
export type SkinLayoutPage = "home" | "voice" | "demo";
|
||||
export type SkinLayoutProfile = "desktop" | "tablet" | "mobile";
|
||||
|
||||
export interface SkinLayoutPlacement {
|
||||
visible?: boolean;
|
||||
x?: number;
|
||||
y?: number;
|
||||
scale?: number;
|
||||
width?: number;
|
||||
height?: number;
|
||||
foreground?: string;
|
||||
background?: string;
|
||||
border?: string;
|
||||
radius?: number;
|
||||
}
|
||||
|
||||
export interface SkinLayoutDocument {
|
||||
schemaVersion: 1;
|
||||
pages: Partial<Record<SkinLayoutPage, Partial<Record<SkinLayoutProfile, Record<string, SkinLayoutPlacement>>>>>;
|
||||
}
|
||||
|
||||
export interface SkinLayoutComponent {
|
||||
id: string;
|
||||
page: SkinLayoutPage | "common";
|
||||
selectorPart: string;
|
||||
controlKind?: string;
|
||||
name: string;
|
||||
purpose: "host-shell" | "host-action" | "session-content" | "identity-content" | "page-content" | "decoration" | "trusted-status";
|
||||
category: "core" | "optional" | "trusted-chrome";
|
||||
allowedContainers: readonly string[];
|
||||
layoutModes: readonly ("position" | "size" | "visibility" | "appearance")[];
|
||||
dataSensitivity: "none" | "public" | "session" | "identity";
|
||||
keyboardBehavior: "host-native";
|
||||
focusOrder: "host-dom";
|
||||
accessibleNamePolicy: "preserve-host-name";
|
||||
narrowScreenBehavior: "host-responsive" | "decorative" | "trusted-fixed";
|
||||
minWidth: number;
|
||||
minHeight: number;
|
||||
maxWidth: number;
|
||||
maxHeight: number;
|
||||
minScale: number;
|
||||
maxScale: number;
|
||||
minTouchTarget: number;
|
||||
allowedStyleTokens: readonly ("foreground" | "background" | "border" | "radius")[];
|
||||
editable: boolean;
|
||||
}
|
||||
|
||||
const PUBLIC_PART_IDS = new Set<string>([
|
||||
"app",
|
||||
"app.toast",
|
||||
"control",
|
||||
"demo.action.poke",
|
||||
"demo.action.reconnect",
|
||||
"demo.action.speaking",
|
||||
"demo.actions",
|
||||
"demo.actions.heading",
|
||||
"demo.avatar",
|
||||
"demo.badge",
|
||||
"demo.brand",
|
||||
"demo.channel",
|
||||
"demo.channel.members",
|
||||
"demo.channel.select",
|
||||
"demo.channels",
|
||||
"demo.channels.heading",
|
||||
"demo.chat.composer",
|
||||
"demo.chat.empty",
|
||||
"demo.chat.heading",
|
||||
"demo.chat.input",
|
||||
"demo.chat.message",
|
||||
"demo.chat.messages",
|
||||
"demo.chat.send",
|
||||
"demo.chat.tab",
|
||||
"demo.chat.tabs",
|
||||
"demo.header",
|
||||
"demo.header-tools",
|
||||
"demo.hero",
|
||||
"demo.hero.description",
|
||||
"demo.hero.online",
|
||||
"demo.hero.title",
|
||||
"demo.home-link",
|
||||
"demo.language-switcher",
|
||||
"demo.layout",
|
||||
"demo.live",
|
||||
"demo.main",
|
||||
"demo.member-name",
|
||||
"demo.member-status",
|
||||
"demo.note",
|
||||
"demo.poke-notification",
|
||||
"demo.poke.dismiss",
|
||||
"demo.reconnect",
|
||||
"demo.reconnect.restore",
|
||||
"demo.skin-switcher",
|
||||
"demo.user",
|
||||
"demo.voice-card",
|
||||
"demo.voice-grid",
|
||||
"demo.voice-heading",
|
||||
"demo.wave",
|
||||
"home",
|
||||
"home.brand",
|
||||
"home.community-dialog",
|
||||
"home.connect",
|
||||
"home.content",
|
||||
"home.favorite-toggle",
|
||||
"home.feature",
|
||||
"home.features",
|
||||
"home.field",
|
||||
"home.field-label",
|
||||
"home.footer",
|
||||
"home.form",
|
||||
"home.gateway-status",
|
||||
"home.header",
|
||||
"home.header-tools",
|
||||
"home.hero",
|
||||
"home.hero.description",
|
||||
"home.hero.eyebrow",
|
||||
"home.hero.title",
|
||||
"home.identity",
|
||||
"home.identity-actions",
|
||||
"home.identity-export.button",
|
||||
"home.identity-import-dialog",
|
||||
"home.identity-import.cancel",
|
||||
"home.identity-import.close",
|
||||
"home.identity-import.drop-zone",
|
||||
"home.identity-import.error",
|
||||
"home.identity-import.file-button",
|
||||
"home.identity-import.footer",
|
||||
"home.identity-import.header",
|
||||
"home.identity-import.open",
|
||||
"home.identity-import.security",
|
||||
"home.identity-import.submit",
|
||||
"home.identity-import.textarea",
|
||||
"home.join-card",
|
||||
"home.join-card.sonar",
|
||||
"home.join-card.waveform",
|
||||
"home.join-description",
|
||||
"home.join-title",
|
||||
"home.notice",
|
||||
"home.relay-choice",
|
||||
"home.relay-choice.copy",
|
||||
"home.security-note",
|
||||
"home.server-history",
|
||||
"home.server-history.favorite-toggle",
|
||||
"home.server-history.group",
|
||||
"home.server-history.item",
|
||||
"home.server-history.select",
|
||||
"home.server-target",
|
||||
"home.visitors",
|
||||
"language.menu",
|
||||
"language.option",
|
||||
"language.trigger",
|
||||
"skin.menu",
|
||||
"skin.option",
|
||||
"skin.trigger",
|
||||
"voice.activity",
|
||||
"voice.activity-heading",
|
||||
"voice.activity.artwork",
|
||||
"voice.audio-dock",
|
||||
"voice.audio-dock.microphone",
|
||||
"voice.audio-dock.microphone-panel",
|
||||
"voice.audio-dock.output",
|
||||
"voice.audio-dock.output-panel",
|
||||
"voice.audio-settings",
|
||||
"voice.audio-status",
|
||||
"voice.breadcrumbs",
|
||||
"voice.channel-group",
|
||||
"voice.channel-group.heading",
|
||||
"voice.channel-group.members",
|
||||
"voice.chat",
|
||||
"voice.chat.composer",
|
||||
"voice.chat.empty",
|
||||
"voice.chat.event",
|
||||
"voice.chat.heading",
|
||||
"voice.chat.message",
|
||||
"voice.chat.message-avatar",
|
||||
"voice.chat.message-body",
|
||||
"voice.chat.message-bubble",
|
||||
"voice.chat.messages",
|
||||
"voice.chat.status",
|
||||
"voice.chat.tab",
|
||||
"voice.chat.tabs",
|
||||
"voice.connection-status",
|
||||
"voice.content",
|
||||
"voice.context-menu",
|
||||
"voice.context-menu-backdrop",
|
||||
"voice.context-menu.header",
|
||||
"voice.context-menu.move-submenu",
|
||||
"voice.favorite-servers.add",
|
||||
"voice.favorite-servers.avatar",
|
||||
"voice.favorite-servers.current-toggle",
|
||||
"voice.favorite-servers.dialog",
|
||||
"voice.favorite-servers.dialog-backdrop",
|
||||
"voice.favorite-servers.dialog.actions",
|
||||
"voice.favorite-servers.dialog.cancel",
|
||||
"voice.favorite-servers.dialog.close",
|
||||
"voice.favorite-servers.dialog.field",
|
||||
"voice.favorite-servers.dialog.form",
|
||||
"voice.favorite-servers.dialog.header",
|
||||
"voice.favorite-servers.dialog.hint",
|
||||
"voice.favorite-servers.dialog.input",
|
||||
"voice.favorite-servers.dialog.submit",
|
||||
"voice.favorite-servers.dialog.target",
|
||||
"voice.favorite-servers.favorite-toggle",
|
||||
"voice.favorite-servers.rail",
|
||||
"voice.favorite-servers.rail.item",
|
||||
"voice.favorite-servers.rail.list",
|
||||
"voice.favorite-servers.server",
|
||||
"voice.favorite-servers.strip",
|
||||
"voice.favorite-servers.strip.add",
|
||||
"voice.favorite-servers.strip.favorite-toggle",
|
||||
"voice.favorite-servers.strip.item",
|
||||
"voice.favorite-servers.strip.server",
|
||||
"voice.favorite-servers.switch-status",
|
||||
"voice.favorite-servers.tooltip",
|
||||
"voice.header",
|
||||
"voice.header-actions",
|
||||
"voice.member",
|
||||
"voice.member-panel",
|
||||
"voice.member-panel.channels",
|
||||
"voice.member-panel.heading",
|
||||
"voice.member-panel.search",
|
||||
"voice.member-row",
|
||||
"voice.member-row.avatar",
|
||||
"voice.member-row.copy",
|
||||
"voice.member-row.flags",
|
||||
"voice.member-row.volume",
|
||||
"voice.member.avatar",
|
||||
"voice.member.avatar-wrap",
|
||||
"voice.member.live-indicator",
|
||||
"voice.member.name",
|
||||
"voice.member.share-actions",
|
||||
"voice.member.status",
|
||||
"voice.member.stop-share",
|
||||
"voice.members",
|
||||
"voice.members.empty",
|
||||
"voice.mobile-more",
|
||||
"voice.mobile-nav",
|
||||
"voice.performance",
|
||||
"voice.performance.metrics",
|
||||
"voice.performance.panel",
|
||||
"voice.performance.route",
|
||||
"voice.performance.status",
|
||||
"voice.performance.webrtc-stats",
|
||||
"voice.poke",
|
||||
"voice.screen-player",
|
||||
"voice.screen-player.controls",
|
||||
"voice.screen-player.exit",
|
||||
"voice.screen-player.fullscreen",
|
||||
"voice.screen-player.live",
|
||||
"voice.screen-player.placeholder",
|
||||
"voice.screen-player.source",
|
||||
"voice.screen-player.stage",
|
||||
"voice.screen-player.video",
|
||||
"voice.screen-player.viewer-avatar",
|
||||
"voice.screen-player.viewers",
|
||||
"voice.screen-player.volume",
|
||||
"voice.screen-share-error",
|
||||
"voice.screen-share-settings",
|
||||
"voice.screen-share-settings.actions",
|
||||
"voice.screen-share-settings.fields",
|
||||
"voice.screen-share-settings.heading",
|
||||
"voice.screen-share-settings.note",
|
||||
"voice.scroll",
|
||||
"voice.shell",
|
||||
"voice.whisper-strip",
|
||||
"voice.workspace",
|
||||
]);
|
||||
|
||||
const CONTROL_KINDS = ["button", "link", "input", "checkbox", "range", "select", "textarea", "menuitem", "draggable"] as const;
|
||||
const TRUSTED_PART_IDS = new Set([
|
||||
"app",
|
||||
"app.toast",
|
||||
"home.security-note",
|
||||
"home.identity-import.security",
|
||||
"voice.connection-status",
|
||||
"voice.screen-share-error",
|
||||
]);
|
||||
const DECORATIVE_PART_IDS = new Set([
|
||||
"demo.avatar", "demo.badge", "demo.note", "demo.poke-notification", "demo.wave",
|
||||
"home.feature", "home.features", "home.footer", "home.hero.eyebrow", "home.join-card.sonar",
|
||||
"home.join-card.waveform", "home.visitors", "voice.activity.artwork", "voice.member.avatar-wrap",
|
||||
"voice.member.live-indicator", "voice.performance.metrics", "voice.performance.webrtc-stats",
|
||||
"voice.screen-player.viewer-avatar",
|
||||
]);
|
||||
const SHELL_PART_IDS = new Set([
|
||||
"app", "home", "home.content", "home.form", "home.header", "voice.shell", "voice.workspace",
|
||||
"voice.content", "demo.layout", "demo.main",
|
||||
]);
|
||||
const ACTION_PART_IDS = new Set([
|
||||
"control", "home.connect", "home.favorite-toggle", "home.identity-actions", "home.identity-export.button",
|
||||
"home.identity-import.cancel", "home.identity-import.close", "home.identity-import.file-button",
|
||||
"home.identity-import.open", "home.identity-import.submit", "home.relay-choice.copy",
|
||||
"home.server-history.favorite-toggle", "home.server-history.select", "language.trigger", "language.menu",
|
||||
"language.option", "skin.trigger", "skin.menu", "skin.option", "voice.audio-dock.microphone",
|
||||
"voice.audio-dock.output", "voice.context-menu", "voice.favorite-servers.add",
|
||||
"voice.favorite-servers.current-toggle", "voice.favorite-servers.dialog.cancel",
|
||||
"voice.favorite-servers.dialog.close", "voice.favorite-servers.dialog.submit",
|
||||
"voice.favorite-servers.favorite-toggle", "voice.favorite-servers.strip.add",
|
||||
"voice.favorite-servers.strip.favorite-toggle", "voice.member-row.copy", "voice.member-row.volume",
|
||||
"voice.member.share-actions", "voice.member.stop-share", "voice.member.stop-share",
|
||||
"voice.mobile-more", "voice.mobile-nav", "voice.poke", "voice.screen-player.controls",
|
||||
"voice.screen-player.exit", "voice.screen-player.fullscreen", "voice.screen-player.volume",
|
||||
"demo.action.poke", "demo.action.reconnect", "demo.action.speaking", "demo.channel.select",
|
||||
"demo.chat.send", "demo.chat.tab", "demo.home-link", "demo.poke.dismiss", "demo.reconnect.restore",
|
||||
"voice.channel-group.heading", "voice.chat.tab", "voice.favorite-servers.server",
|
||||
"voice.favorite-servers.strip.server", "demo.voice-card", "home.identity-import.drop-zone",
|
||||
"voice.channel-group", "voice.member-row", "demo.skin-switcher", "demo.language-switcher",
|
||||
]);
|
||||
const INTERACTIVE_PART_IDS = new Set([
|
||||
"demo.action.poke", "demo.action.reconnect", "demo.action.speaking", "demo.channel.select",
|
||||
"demo.chat.input", "demo.chat.send", "demo.chat.tab", "demo.home-link", "demo.poke.dismiss",
|
||||
"demo.reconnect.restore", "home.connect", "home.favorite-toggle", "home.identity-export.button",
|
||||
"home.identity-import.cancel", "home.identity-import.close", "home.identity-import.file-button",
|
||||
"home.identity-import.open", "home.identity-import.submit", "home.identity-import.textarea",
|
||||
"home.server-history.favorite-toggle", "home.server-history.select", "language.option",
|
||||
"language.trigger", "skin.option", "skin.trigger", "voice.channel-group.heading", "voice.chat.tab",
|
||||
"voice.favorite-servers.add", "voice.favorite-servers.current-toggle", "voice.favorite-servers.dialog.cancel",
|
||||
"voice.favorite-servers.dialog.close", "voice.favorite-servers.dialog.input", "voice.favorite-servers.dialog.submit",
|
||||
"voice.favorite-servers.favorite-toggle", "voice.favorite-servers.server", "voice.favorite-servers.strip.add",
|
||||
"voice.favorite-servers.strip.favorite-toggle", "voice.favorite-servers.strip.server", "voice.member.stop-share",
|
||||
"voice.screen-player.exit", "voice.screen-player.fullscreen", "demo.voice-card",
|
||||
"home.identity-import.drop-zone", "voice.channel-group", "voice.member-row", "demo.skin-switcher",
|
||||
"demo.language-switcher",
|
||||
]);
|
||||
const IDENTITY_PART_IDS = new Set([
|
||||
"home.identity", "home.identity-actions", "home.identity-export.button", "home.identity-import-dialog",
|
||||
"home.identity-import.cancel", "home.identity-import.close", "home.identity-import.drop-zone",
|
||||
"home.identity-import.error", "home.identity-import.file-button", "home.identity-import.footer",
|
||||
"home.identity-import.header", "home.identity-import.open", "home.identity-import.submit",
|
||||
"home.identity-import.textarea", "home.identity-import.security",
|
||||
]);
|
||||
const COMMON_PART_IDS = new Set([
|
||||
"app", "app.toast", "control", "skin.trigger", "skin.menu", "skin.option",
|
||||
"language.trigger", "language.menu", "language.option",
|
||||
]);
|
||||
|
||||
function pageOfPart(id: string): SkinLayoutPage | "common" | null {
|
||||
if (COMMON_PART_IDS.has(id)) return "common";
|
||||
if (id === "home" || id.startsWith("home.")) return "home";
|
||||
if (id.startsWith("voice.")) return "voice";
|
||||
if (id.startsWith("demo.")) return "demo";
|
||||
return null;
|
||||
}
|
||||
|
||||
const LAYOUT_MODES = ["position", "size", "visibility", "appearance"] as const;
|
||||
const STYLE_TOKENS = ["foreground", "background", "border", "radius"] as const;
|
||||
const COMPONENT_NAME_PARTS: Record<string, string> = {
|
||||
action: "Action", actions: "Actions", activity: "Activity", add: "Add", app: "Application", artwork: "Artwork",
|
||||
audio: "Audio", avatar: "Avatar", backdrop: "Backdrop", badge: "Badge", body: "Body", brand: "Brand",
|
||||
breadcrumbs: "Breadcrumbs", bubble: "Bubble", button: "Button", cancel: "Cancel", card: "Card",
|
||||
channel: "Channel", channels: "Channels", chat: "Chat", checkbox: "Checkbox", choice: "Choice", close: "Close",
|
||||
community: "Community", composer: "Composer", connect: "Connect", connection: "Connection", content: "Content",
|
||||
context: "Context", control: "Control", controls: "Controls", copy: "Copy", current: "Current", demo: "Demo",
|
||||
description: "Description", dialog: "Dialog", dismiss: "Dismiss", dock: "Dock", draggable: "Draggable",
|
||||
drop: "Drop", empty: "Empty state", error: "Error", event: "Event", exit: "Exit", export: "Export",
|
||||
eyebrow: "Eyebrow", favorite: "Favorite", feature: "Feature", features: "Features", field: "Field", fields: "Fields",
|
||||
file: "File", flags: "Flags", footer: "Footer", form: "Form", fullscreen: "Fullscreen", gateway: "Gateway",
|
||||
grid: "Grid", group: "Group", header: "Header", heading: "Heading", hero: "Hero", hint: "Hint", history: "History",
|
||||
home: "Home page", identity: "Identity", import: "Import", indicator: "Indicator", input: "Input", item: "Item",
|
||||
join: "Join", label: "Label", language: "Language", layout: "Layout", link: "Link", list: "List", live: "Live status",
|
||||
main: "Main content", member: "Member", members: "Members", menu: "Menu", menuitem: "Menu item", message: "Message",
|
||||
messages: "Messages", metrics: "Metrics", microphone: "Microphone", mobile: "Mobile", more: "More", move: "Move",
|
||||
name: "Name", nav: "Navigation", note: "Note", notice: "Notice", notification: "Notification", online: "Online status",
|
||||
open: "Open", option: "Option", output: "Output", panel: "Panel", performance: "Performance", placeholder: "Placeholder",
|
||||
player: "Player", poke: "Poke", rail: "Rail", range: "Range", reconnect: "Reconnect", relay: "Relay", restore: "Restore",
|
||||
route: "Route", row: "Row", screen: "Screen", scroll: "Scrollable area", search: "Search", security: "Security",
|
||||
select: "Select", send: "Send", server: "Server", servers: "Servers", settings: "Settings", share: "Share",
|
||||
shell: "Application shell", skin: "Skin", sonar: "Sonar effect", source: "Source", speaking: "Speaking status",
|
||||
stage: "Stage", stats: "Statistics", status: "Status", stop: "Stop", strip: "Strip", submenu: "Submenu",
|
||||
submit: "Submit", switch: "Switch", switcher: "Switcher", tab: "Tab", tabs: "Tabs", target: "Target", textarea: "Text area",
|
||||
title: "Title", toast: "Notifications", toggle: "Toggle", tools: "Tools", tooltip: "Tooltip", trigger: "Trigger",
|
||||
user: "User", video: "Video", viewer: "Viewer", viewers: "Viewers", visitors: "Visitors", voice: "Voice",
|
||||
volume: "Volume", wave: "Wave", waveform: "Waveform", webrtc: "WebRTC", whisper: "Whisper", workspace: "Workspace",
|
||||
wrap: "Wrapper", zone: "Drop zone",
|
||||
};
|
||||
|
||||
function componentName(id: string): string {
|
||||
return id.split(".").map((part) => part.split("-").map((word) => COMPONENT_NAME_PARTS[word]
|
||||
?? (word.charAt(0).toUpperCase() + word.slice(1))).join(" ")).join(" · ");
|
||||
}
|
||||
|
||||
function describeComponent(id: string, page: SkinLayoutPage | "common", controlKind?: string): SkinLayoutComponent {
|
||||
const category: SkinLayoutComponent["category"] = TRUSTED_PART_IDS.has(id) ? "trusted-chrome"
|
||||
: DECORATIVE_PART_IDS.has(id) ? "optional" : "core";
|
||||
const purpose: SkinLayoutComponent["purpose"] = TRUSTED_PART_IDS.has(id) ? "trusted-status"
|
||||
: SHELL_PART_IDS.has(id) ? "host-shell"
|
||||
: ACTION_PART_IDS.has(id) || Boolean(controlKind) ? "host-action"
|
||||
: IDENTITY_PART_IDS.has(id) ? "identity-content"
|
||||
: DECORATIVE_PART_IDS.has(id) ? "decoration"
|
||||
: page === "voice" ? "session-content" : "page-content";
|
||||
const allowedContainers = category === "trusted-chrome" ? [] : ["app"];
|
||||
const editable = category !== "trusted-chrome";
|
||||
const isControl = editable && (Boolean(controlKind) || id === "control" || purpose === "host-action" || INTERACTIVE_PART_IDS.has(id));
|
||||
return {
|
||||
id,
|
||||
page,
|
||||
selectorPart: id.startsWith("control.") ? "control" : id,
|
||||
...(controlKind ? { controlKind } : {}),
|
||||
name: componentName(id),
|
||||
purpose,
|
||||
category,
|
||||
allowedContainers,
|
||||
layoutModes: editable ? LAYOUT_MODES : [],
|
||||
dataSensitivity: IDENTITY_PART_IDS.has(id) ? "identity" : page === "voice" ? "session" : purpose === "decoration" ? "none" : "public",
|
||||
keyboardBehavior: "host-native",
|
||||
focusOrder: "host-dom",
|
||||
accessibleNamePolicy: "preserve-host-name",
|
||||
narrowScreenBehavior: category === "trusted-chrome" ? "trusted-fixed" : category === "optional" ? "decorative" : "host-responsive",
|
||||
minWidth: editable ? isControl ? 44 : 12 : 0,
|
||||
minHeight: editable ? isControl ? 44 : 12 : 0,
|
||||
maxWidth: editable ? isControl ? 1600 : 2400 : 0,
|
||||
maxHeight: editable ? isControl ? 1200 : 2400 : 0,
|
||||
minScale: editable ? isControl ? 1 : 0.5 : 1,
|
||||
maxScale: editable ? isControl ? 3 : 4 : 1,
|
||||
minTouchTarget: isControl ? 44 : 0,
|
||||
allowedStyleTokens: editable ? STYLE_TOKENS : [],
|
||||
editable,
|
||||
};
|
||||
}
|
||||
|
||||
const regularComponents: SkinLayoutComponent[] = [...PUBLIC_PART_IDS].flatMap((id) => {
|
||||
const page = pageOfPart(id);
|
||||
return page ? [describeComponent(id, page)] : [];
|
||||
});
|
||||
const virtualControls: SkinLayoutComponent[] = CONTROL_KINDS.map((kind) => ({
|
||||
...describeComponent("control." + kind, "common", kind),
|
||||
}));
|
||||
|
||||
export const SKIN_LAYOUT_COMPONENTS: readonly SkinLayoutComponent[] = [
|
||||
...regularComponents,
|
||||
...virtualControls,
|
||||
];
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
export function getSkinLayoutComponent(id: string, page?: SkinLayoutPage): SkinLayoutComponent | undefined {
|
||||
const component = componentById.get(id);
|
||||
if (!component || (page && component.page !== "common" && component.page !== page)) return undefined;
|
||||
return component;
|
||||
}
|
||||
|
||||
export function canEditSkinLayoutComponent(id: string, page: SkinLayoutPage, ancestorIds: readonly string[] = []): boolean {
|
||||
const component = getSkinLayoutComponent(id, page);
|
||||
if (!component?.editable) return false;
|
||||
return ancestorIds.every((ancestorId) => getSkinLayoutComponent(ancestorId, page)?.category !== "trusted-chrome");
|
||||
}
|
||||
|
||||
export function getSkinLayoutComponentId(part: string | undefined, controlKind?: string): string | null {
|
||||
if (!part) return null;
|
||||
if (part === "control" && controlKind) return "control." + controlKind;
|
||||
return part;
|
||||
}
|
||||
|
||||
const ALLOWED_PLACEMENT_FIELDS = new Set([
|
||||
"visible", "x", "y", "scale", "width", "height", "foreground", "background", "border", "radius",
|
||||
]);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function assertOnlyKeys(value: Record<string, unknown>, allowed: ReadonlySet<string>, context: string): void {
|
||||
for (const key of Object.keys(value)) {
|
||||
if (!allowed.has(key)) throw new Error(context + " contains an unknown field: " + key);
|
||||
}
|
||||
}
|
||||
|
||||
function readNumber(value: unknown, key: string, minimum: number, maximum: number): number | undefined {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value !== "number" || !Number.isFinite(value) || value < minimum || value > maximum) {
|
||||
throw new Error("Skin layout field " + key + " is outside its allowed range.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function readColor(value: unknown, key: string): string | undefined {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value !== "string" || !/^#[0-9a-f]{6}$/i.test(value)) throw new Error("Skin layout field " + key + " must be a six-digit hex color.");
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
export function validateSkinLayout(value: unknown): SkinLayoutDocument {
|
||||
if (!isRecord(value) || value.schemaVersion !== 1 || !isRecord(value.pages)) {
|
||||
throw new Error("Skin layout must use schemaVersion 1 and contain a pages object.");
|
||||
}
|
||||
assertOnlyKeys(value, new Set(["schemaVersion", "pages"]), "Skin layout");
|
||||
const pages: SkinLayoutDocument["pages"] = {};
|
||||
let placementCount = 0;
|
||||
|
||||
for (const [pageKey, pageValue] of Object.entries(value.pages)) {
|
||||
if (!(["home", "voice", "demo"] as string[]).includes(pageKey) || !isRecord(pageValue)) {
|
||||
throw new Error("Skin layout contains an unsupported page.");
|
||||
}
|
||||
assertOnlyKeys(pageValue, new Set(["desktop", "tablet", "mobile"]), "Skin layout page");
|
||||
const profiles: Partial<Record<SkinLayoutProfile, Record<string, SkinLayoutPlacement>>> = {};
|
||||
for (const profile of ["desktop", "tablet", "mobile"] as const) {
|
||||
const profileValue = pageValue[profile];
|
||||
if (profileValue === undefined) continue;
|
||||
if (!isRecord(profileValue)) throw new Error("Skin layout profile must be a component map.");
|
||||
const placements: Record<string, SkinLayoutPlacement> = {};
|
||||
for (const [id, rawPlacement] of Object.entries(profileValue)) {
|
||||
placementCount += 1;
|
||||
if (placementCount > 512) throw new Error("Skin layout contains too many component placements.");
|
||||
const component = getSkinLayoutComponent(id, pageKey as SkinLayoutPage);
|
||||
if (!component) throw new Error("Skin layout references an unknown component: " + id);
|
||||
if (!component.editable) throw new Error("Skin layout cannot change trusted component: " + id);
|
||||
if (!isRecord(rawPlacement)) throw new Error("Skin component placement must be an object.");
|
||||
assertOnlyKeys(rawPlacement, ALLOWED_PLACEMENT_FIELDS, "Skin component placement");
|
||||
const supportedFields = new Set<string>();
|
||||
if (component.layoutModes.includes("visibility")) supportedFields.add("visible");
|
||||
if (component.layoutModes.includes("position")) { supportedFields.add("x"); supportedFields.add("y"); }
|
||||
if (component.layoutModes.includes("size")) { supportedFields.add("scale"); supportedFields.add("width"); supportedFields.add("height"); }
|
||||
if (component.layoutModes.includes("appearance")) component.allowedStyleTokens.forEach((token) => supportedFields.add(token));
|
||||
assertOnlyKeys(rawPlacement, supportedFields, "Skin component placement");
|
||||
const placement: SkinLayoutPlacement = {};
|
||||
if (rawPlacement.visible !== undefined) {
|
||||
if (typeof rawPlacement.visible !== "boolean") throw new Error("Skin layout visible must be a boolean.");
|
||||
placement.visible = rawPlacement.visible;
|
||||
}
|
||||
const x = readNumber(rawPlacement.x, "x", -2000, 2000);
|
||||
const y = readNumber(rawPlacement.y, "y", -2000, 2000);
|
||||
const scale = readNumber(rawPlacement.scale, "scale", component.minScale, component.maxScale);
|
||||
const width = readNumber(rawPlacement.width, "width", component.minWidth, component.maxWidth);
|
||||
const height = readNumber(rawPlacement.height, "height", component.minHeight, component.maxHeight);
|
||||
const radius = readNumber(rawPlacement.radius, "radius", 0, 64);
|
||||
if (x !== undefined) placement.x = x;
|
||||
if (y !== undefined) placement.y = y;
|
||||
if (scale !== undefined) placement.scale = scale;
|
||||
if (width !== undefined) placement.width = width;
|
||||
if (height !== undefined) placement.height = height;
|
||||
if (radius !== undefined) placement.radius = radius;
|
||||
const foreground = readColor(rawPlacement.foreground, "foreground");
|
||||
const background = readColor(rawPlacement.background, "background");
|
||||
const border = readColor(rawPlacement.border, "border");
|
||||
if (foreground !== undefined) placement.foreground = foreground;
|
||||
if (background !== undefined) placement.background = background;
|
||||
if (border !== undefined) placement.border = border;
|
||||
placements[id] = placement;
|
||||
}
|
||||
profiles[profile] = placements;
|
||||
}
|
||||
pages[pageKey as SkinLayoutPage] = profiles;
|
||||
}
|
||||
|
||||
return { schemaVersion: 1, pages };
|
||||
}
|
||||
|
||||
export function parseSkinLayoutJson(source: string): SkinLayoutDocument {
|
||||
if (!source || new TextEncoder().encode(source).byteLength > 128 * 1024) {
|
||||
throw new Error("Skin layout must be smaller than 128 KiB.");
|
||||
}
|
||||
let value: unknown;
|
||||
try { value = JSON.parse(source); }
|
||||
catch { throw new Error("Skin layout is not valid JSON."); }
|
||||
return validateSkinLayout(value);
|
||||
}
|
||||
Reference in new issue
Block a user