mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
Add chrome.json slot mode "toolbar" with grouped and per-action recovery buttons, page-specific anchor candidates and sanitized toolbar style tokens. Keep the floating capsule as the bad-anchor fallback, hide the duplicate plugin launcher, and preserve legacy capsule slots. Document the full contract, add a minimal toolbar example package, and cover the new behavior in unit and browser tests.
271 lines
19 KiB
Vue
271 lines
19 KiB
Vue
<template>
|
|
<Teleport :to="toolbarTarget ?? 'body'">
|
|
<div v-if="thirdPartyAllowed && connected && sessionToken" ref="toolbar" class="ws-plugin-toolbar" :class="{ 'ws-plugin-toolbar-fallback': !toolbarTarget }" :data-ws-float="toolbarTarget ? undefined : fallbackPosition" data-ws-part="voice.plugin-launcher" :data-presentation="presentation" :data-theme="theme" @keydown.esc="dismissDrawer">
|
|
<button v-if="!isHostPartHidden('host.plugins.launcher') && !inlineLibraryEntry" ref="launcher" class="header-action ws-plugin-toolbar-trigger" data-ws-part="host.plugins.launcher" type="button" :title="tr('title')" :aria-label="tr('title')" :aria-expanded="presentation === 'dialog' ? visible : drawerOpen" :aria-controls="drawerOpen ? drawerId : undefined" @click="toggle">
|
|
<svg viewBox="0 0 24 24" data-ws-icon-slot="host.plugins.launcher" aria-hidden="true"><path d="M4 4h6v6H4zM14 4h6v6h-6zM4 14h6v6H4zM14 14h6v6h-6z" /></svg>
|
|
<span v-if="presentation === 'drawer-side'">{{ tr('title') }}</span>
|
|
</button>
|
|
<section v-if="drawerOpen" :id="drawerId" ref="drawer" class="ws-plugin-drawer" data-ws-part="voice.plugin-drawer" :aria-label="tr('title')">
|
|
<input ref="drawerSearch" v-model="search" class="ws-plugin-drawer-search" type="search" :placeholder="ui.search" :aria-label="ui.search" />
|
|
<p v-if="error" role="alert">{{ error }}</p>
|
|
<p v-else-if="busy && !available.length" role="status">{{ tr('loading') }}</p>
|
|
<p v-else-if="!filtered.length">{{ available.length ? ui.noResults : tr('empty') }}</p>
|
|
<ul v-if="filtered.length">
|
|
<li v-for="entry in filtered" :key="entry.manifest.id">
|
|
<button type="button" class="ws-plugin-drawer-app" data-ws-part="voice.plugin-drawer.app" :disabled="busy || entry.runtime.state === 'failed'" :title="entry.runtime.error || entry.manifest.name" @click="launch(entry.manifest.id)">
|
|
<span class="ws-plugin-drawer-mark" aria-hidden="true">{{ entry.manifest.name.slice(0, 1).toUpperCase() }}</span>
|
|
<span>{{ entry.manifest.name }}</span><span v-if="entry.toolbarPinned" aria-hidden="true">★</span>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</section>
|
|
</div>
|
|
</Teleport>
|
|
<Teleport to="body">
|
|
<div v-if="thirdPartyAllowed && connected && sessionToken" class="ws-plugin-overlay" :data-plugin-theme="theme">
|
|
<section v-show="visible" ref="windowElement" class="ws-plugin-window" data-ws-part="host.plugins.overlay" :class="{ 'is-maximized': maximized, 'is-dragging': dragging }" :style="position" role="dialog" :aria-label="current && appView ? current.manifest.name : tr('title')" tabindex="-1" @keydown.esc.stop="minimize">
|
|
<header class="ws-plugin-titlebar" @pointerdown="startDrag" @lostpointercapture="endDrag" @dblclick="toggleMaximize">
|
|
<span class="ws-plugin-mark" aria-hidden="true">◈</span>
|
|
<div class="ws-plugin-window-title"><strong>{{ current && appView ? current.manifest.name : tr('title') }}</strong><small>WebSpeak</small></div>
|
|
<div class="ws-plugin-window-controls" @pointerdown.stop @dblclick.stop>
|
|
<button type="button" :title="ui.minimize" :aria-label="ui.minimize" @click="minimize"><svg viewBox="0 0 24 24"><path d="M5 17h14" /></svg></button>
|
|
<button type="button" :title="maximized ? ui.restore : ui.maximize" :aria-label="maximized ? ui.restore : ui.maximize" @click="toggleMaximize"><svg viewBox="0 0 24 24"><path :d="maximized ? 'M8 4h12v12M4 8h12v12H4z' : 'M5 5h14v14H5z'" /></svg></button>
|
|
<button class="ws-plugin-close" type="button" :title="tr('close')" :aria-label="tr('close')" @click="close"><svg viewBox="0 0 24 24"><path d="m6 6 12 12M18 6 6 18" /></svg></button>
|
|
</div>
|
|
</header>
|
|
<nav v-if="dialogLibrary" class="ws-plugin-navigation" :aria-label="tr('title')">
|
|
<button type="button" :class="{ active: !appView }" @click="appView = false">{{ ui.workspace }}</button>
|
|
<button v-if="current" type="button" :class="{ active: appView }" @click="appView = true">{{ current.manifest.name }}</button>
|
|
<span v-if="current && appView" class="ws-plugin-version">v{{ current.manifest.version }}</span>
|
|
</nav>
|
|
<p v-if="error" role="alert" class="ws-plugin-error">{{ error }}</p>
|
|
<div v-if="dialogLibrary" v-show="!appView" class="ws-plugin-library">
|
|
<div class="ws-plugin-library-heading"><div><h2>{{ tr('title') }}</h2><span>{{ available.length }} {{ ui.count }}</span></div><button type="button" :disabled="busy" @click="refresh">{{ busy ? tr('loading') : tr('refresh') }}</button></div>
|
|
<input v-model="search" class="ws-plugin-search" type="search" :placeholder="ui.search" :aria-label="ui.search" />
|
|
<p v-if="!busy && !filtered.length" class="ws-plugin-empty">{{ available.length ? ui.noResults : tr('empty') }}</p>
|
|
<div class="ws-plugin-grid">
|
|
<article v-for="entry in filtered" :key="entry.manifest.id" class="ws-plugin-entry">
|
|
<div class="ws-plugin-app-mark" aria-hidden="true">{{ entry.manifest.name.slice(0, 1).toUpperCase() }}</div>
|
|
<div class="ws-plugin-entry-copy"><h3>{{ entry.manifest.name }}</h3><small>v{{ entry.manifest.version }} · {{ entry.scope === 'server' ? tr('server') : tr('instance') }}</small><p>{{ entry.manifest.description }}</p></div>
|
|
<p v-if="entry.runtime.state === 'failed'" class="ws-plugin-entry-error">{{ entry.runtime.error }}</p>
|
|
<button type="button" :disabled="busy || entry.runtime.state === 'failed'" @click="launch(entry.manifest.id)">{{ tr('open') }} <span aria-hidden="true">↗</span></button>
|
|
</article>
|
|
</div>
|
|
</div>
|
|
<div v-show="appView && current" class="ws-plugin-browser">
|
|
<iframe v-if="current" ref="frame" :key="current.id" :title="current.manifest.name" :src="current.url" :allow="browserFeatures" referrerpolicy="no-referrer"></iframe>
|
|
</div>
|
|
<div v-if="dragging" class="ws-plugin-drag-shield" />
|
|
</section>
|
|
</div>
|
|
</Teleport>
|
|
</template>
|
|
<script setup lang="ts">
|
|
import { computed, nextTick, onBeforeUnmount, onMounted, ref, shallowRef, useId, watch } from 'vue';
|
|
import { createPluginAppClient, type AvailablePluginApp, type PluginAppClient } from '../services/plugin-app-client.js';
|
|
import { type PluginAppLaunch, pluginRecord } from '../../../src/shared/plugins.js';
|
|
import { pluginTranslations, type PluginTextKey } from '../i18n/plugins.js';
|
|
import { pluginWindowTranslations } from '../i18n/plugin-window.js';
|
|
import type { SiteLanguage } from '../../../src/site-copy.js';
|
|
import { thirdPartyAllowed } from '../services/instance-authorization.js';
|
|
import { isHostPartHidden, isHostPartVisuallyReachable, isInlineLibraryEntryVisible, pluginLibraryRequests } from '../services/host-chrome.js';
|
|
import type { SkinPluginWidgetOptions } from '../../../src/shared/skin-plugin.js';
|
|
import { bindFloatingAvoidance, type FloatingAvoidance, type FloatingPosition } from '../services/host-floating-placement.js';
|
|
const props = defineProps<{ sessionToken: string; gatewayUrl: string; connected: boolean; language: SiteLanguage; toolbarTarget: HTMLElement | null; presentation: NonNullable<SkinPluginWidgetOptions['pluginPresentation']> }>();
|
|
const tr = (key: PluginTextKey) => pluginTranslations[props.language][key];
|
|
const ui = computed(() => pluginWindowTranslations[props.language]);
|
|
const windowElement = ref<HTMLElement | null>(null), launcher = ref<HTMLButtonElement | null>(null), frame = ref<HTMLIFrameElement | null>(null);
|
|
const available = shallowRef<AvailablePluginApp[]>([]), current = shallowRef<PluginAppLaunch | null>(null);
|
|
const toolbar = ref<HTMLElement | null>(null);
|
|
// A toolbar-mode recovery slot already renders the host-owned application
|
|
// library button; do not render a second launcher on the same page.
|
|
const inlineLibraryEntry = computed(() => isInlineLibraryEntryVisible());
|
|
// The fallback toolbar floats in a viewport corner the skin knows nothing
|
|
// about; move it off any skin control it would cover (runtime only).
|
|
const fallbackPosition = ref<FloatingPosition>('bottom-right');
|
|
let fallbackAvoidance: FloatingAvoidance | undefined;
|
|
const drawerOpen = ref(false);
|
|
const drawer = ref<HTMLElement | null>(null);
|
|
const drawerSearch = ref<HTMLInputElement | null>(null);
|
|
const drawerId = useId();
|
|
const dialogLibrary = ref(false);
|
|
const busy = ref(false), error = ref(''), theme = ref<'light' | 'dark'>('light');
|
|
const visible = ref(false), maximized = ref(false), appView = ref(false), dragging = ref(false), search = ref('');
|
|
const position = ref<Record<string, string>>({});
|
|
const filtered = computed(() => available.value
|
|
.filter(entry => `${entry.manifest.name} ${entry.manifest.description}`.toLocaleLowerCase().includes(search.value.trim().toLocaleLowerCase()))
|
|
.sort((a, b) => Number(b.toolbarPinned) - Number(a.toolbarPinned)));
|
|
let epoch = 0, pending: AbortController | undefined, opening: { client: PluginAppClient; key: string } | undefined, observer: MutationObserver | undefined;
|
|
let drag: { id: number; x: number; y: number; left: number; top: number; handle: HTMLElement; moved: boolean } | undefined;
|
|
let resizeObserver: ResizeObserver | undefined;
|
|
const browserFeatures = computed(() => {
|
|
const policy = (document as Document & { featurePolicy?: { features(): string[] } }).featurePolicy;
|
|
const features = policy?.features() ?? ['fullscreen', 'autoplay', 'camera', 'microphone', 'display-capture', 'clipboard-read', 'clipboard-write', 'geolocation', 'screen-wake-lock'];
|
|
return features.filter(feature => /^[a-z0-9-]+$/.test(feature)).map(feature => feature + ' *').join('; ');
|
|
});
|
|
function detectTheme() {
|
|
const root = document.querySelector('.ws-skin-root');
|
|
const style = getComputedStyle(root ?? document.documentElement);
|
|
if (style.colorScheme === 'dark' || style.colorScheme === 'light') { theme.value = style.colorScheme; return; }
|
|
const color = root ? style.color.match(/[\d.]+/g)?.map(Number) : null;
|
|
theme.value = color && color.length >= 3 ? (color[0] * .2126 + color[1] * .7152 + color[2] * .0722 > 140 ? 'dark' : 'light') : matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
}
|
|
function client() { return createPluginAppClient(props.gatewayUrl, props.sessionToken); }
|
|
async function refresh() {
|
|
if (!thirdPartyAllowed.value) return;
|
|
const owner = ++epoch; pending?.abort(); const controller = new AbortController(); pending = controller; busy.value = true; error.value = '';
|
|
try { const entries = await client().list(controller.signal); if (owner === epoch) available.value = entries; }
|
|
catch (e) { if (owner === epoch && !controller.signal.aborted) error.value = `${tr('error')}: ${e instanceof Error ? e.message : ''}`; }
|
|
finally { if (owner === epoch) { busy.value = false; pending = undefined; } }
|
|
}
|
|
function toggle() {
|
|
if (props.presentation === 'dialog') {
|
|
if (visible.value && dialogLibrary.value) { minimize(); return; }
|
|
openLibraryDialog();
|
|
return;
|
|
}
|
|
if (drawerOpen.value) { drawerOpen.value = false; return; }
|
|
openDrawer();
|
|
}
|
|
function openDrawer() {
|
|
drawerOpen.value = true;
|
|
detectTheme();
|
|
visible.value = false;
|
|
search.value = '';
|
|
if (!busy.value) void refresh();
|
|
void nextTick(() => { fitDrawer(); drawerSearch.value?.focus({ preventScroll: true }); });
|
|
}
|
|
function openLibraryDialog() {
|
|
drawerOpen.value = false;
|
|
detectTheme();
|
|
dialogLibrary.value = true;
|
|
visible.value = true;
|
|
appView.value = false;
|
|
if (!busy.value) void refresh();
|
|
void nextTick(() => { fitWindow(); windowElement.value?.focus(); });
|
|
}
|
|
function dismissDrawer(event: KeyboardEvent) {
|
|
if (!drawerOpen.value) return;
|
|
event.stopPropagation();
|
|
drawerOpen.value = false;
|
|
launcher.value?.focus();
|
|
}
|
|
function closeDrawerOutside(event: PointerEvent) {
|
|
if (drawerOpen.value && toolbar.value && !event.composedPath().includes(toolbar.value)) drawerOpen.value = false;
|
|
}
|
|
function fitDrawer() {
|
|
const element = drawer.value;
|
|
if (!element || props.presentation !== 'drawer-below') return;
|
|
element.style.translate = '';
|
|
const box = element.getBoundingClientRect();
|
|
const delta = Math.max(8 - box.left, Math.min(0, document.documentElement.clientWidth - 8 - box.right));
|
|
if (delta) element.style.translate = `${delta}px 0`;
|
|
}
|
|
function minimize() { endDrag(); visible.value = false; launcher.value?.focus(); }
|
|
function close() { drawerOpen.value = false; stop(); error.value = ''; available.value = []; search.value = ''; minimize(); }
|
|
function stop() {
|
|
++epoch; pending?.abort(); pending = undefined; const old = opening; opening = undefined; current.value = null; appView.value = false; busy.value = false;
|
|
if (old) void old.client.cancelOpen(old.key).catch(() => undefined);
|
|
}
|
|
async function launch(id: string) {
|
|
if (!thirdPartyAllowed.value || !props.connected || !props.sessionToken || busy.value) return;
|
|
if (drawerOpen.value) dialogLibrary.value = false;
|
|
drawerOpen.value = false;
|
|
detectTheme(); visible.value = true;
|
|
if (current.value?.manifest.id === id) { appView.value = true; focusAppWindow(); return; }
|
|
stop(); const owner = ++epoch, controller = new AbortController(), service = client(), key = crypto.randomUUID(); pending = controller; opening = { client: service, key }; busy.value = true; error.value = '';
|
|
try {
|
|
const app = await service.open(id, { openId: key, theme: theme.value, language: props.language }, controller.signal);
|
|
if (owner !== epoch) return;
|
|
if (new URL(app.url).origin === location.origin) throw new Error('PLUGIN_SEPARATE_ORIGIN_REQUIRED');
|
|
current.value = app; appView.value = true;
|
|
focusAppWindow();
|
|
} catch (e) {
|
|
if (owner === epoch) { const old = opening; opening = undefined; if (old) void old.client.cancelOpen(old.key).catch(() => undefined); if (!controller.signal.aborted) error.value = `${tr('error')}: ${e instanceof Error ? e.message : ''}`; }
|
|
} finally { if (owner === epoch) { busy.value = false; pending = undefined; } }
|
|
}
|
|
function focusAppWindow() {
|
|
void nextTick(() => { fitWindow(); windowElement.value?.focus(); });
|
|
}
|
|
watch([() => props.toolbarTarget, () => props.presentation], () => { drawerOpen.value = false; });
|
|
function message(event: MessageEvent) {
|
|
if (!current.value || event.source !== frame.value?.contentWindow || event.origin !== new URL(current.value.url).origin || !pluginRecord(event.data)) return;
|
|
if (event.data.type === 'webspeak-app-closed' && event.data.id === current.value.id) { stop(); error.value = tr('context'); }
|
|
}
|
|
function appearance() { if (current.value && opening) void opening.client.appearance(current.value.id, theme.value, props.language).catch(() => undefined); }
|
|
function fitWindow() {
|
|
if (!visible.value || maximized.value || !windowElement.value) return;
|
|
const box = windowElement.value.getBoundingClientRect();
|
|
position.value = { left: `${Math.max(8, Math.min(box.left, innerWidth - box.width - 8))}px`, top: `${Math.max(8, Math.min(box.top, innerHeight - box.height - 8))}px`, transform: 'none' };
|
|
}
|
|
function toggleMaximize() { endDrag(); maximized.value = !maximized.value; if (!maximized.value) void nextTick(fitWindow); }
|
|
function startDrag(event: PointerEvent) {
|
|
if (event.button !== 0 || maximized.value || (event.target as HTMLElement).closest('button')) return;
|
|
const box = windowElement.value?.getBoundingClientRect(); if (!box) return;
|
|
event.preventDefault(); const handle = event.currentTarget as HTMLElement;
|
|
drag = { id: event.pointerId, x: event.clientX, y: event.clientY, left: box.left, top: box.top, handle, moved: false }; dragging.value = true;
|
|
handle.setPointerCapture(event.pointerId);
|
|
}
|
|
function moveDrag(event: PointerEvent) {
|
|
if (!drag || drag.id !== event.pointerId) return;
|
|
const target = windowElement.value; if (!target) return;
|
|
if (Math.hypot(event.clientX - drag.x, event.clientY - drag.y) < 5 && !drag.moved) return;
|
|
drag.moved = true; const box = target.getBoundingClientRect();
|
|
const next = { left: `${Math.max(8, Math.min(drag.left + event.clientX - drag.x, innerWidth - box.width - 8))}px`, top: `${Math.max(8, Math.min(drag.top + event.clientY - drag.y, innerHeight - box.height - 8))}px`, transform: 'none' };
|
|
position.value = next;
|
|
}
|
|
function endDrag() {
|
|
const old = drag; drag = undefined; dragging.value = false;
|
|
if (old?.handle.hasPointerCapture(old.id)) old.handle.releasePointerCapture(old.id);
|
|
}
|
|
watch(windowElement, element => { resizeObserver?.disconnect(); if (element) { resizeObserver = new ResizeObserver(fitWindow); resizeObserver.observe(element); } });
|
|
watch(() => [props.sessionToken, props.gatewayUrl, props.connected] as const, () => {
|
|
close();
|
|
if (props.connected && thirdPartyAllowed.value) void refresh();
|
|
});
|
|
watch(thirdPartyAllowed, allowed => {
|
|
if (!allowed) close();
|
|
else if (props.connected) void refresh();
|
|
}, { flush: 'sync' });
|
|
watch(() => props.language, appearance); watch(theme, appearance);
|
|
// Trusted recovery entry (host chrome / skin menu) keeps the application
|
|
// library reachable when a skin hides the toolbar launcher.
|
|
watch(pluginLibraryRequests, () => {
|
|
if (!thirdPartyAllowed.value || !props.connected || !props.sessionToken) return;
|
|
if (props.presentation !== 'dialog' && isHostPartVisuallyReachable('host.plugins.launcher')) openDrawer();
|
|
else openLibraryDialog();
|
|
});
|
|
watch([toolbar, () => props.toolbarTarget], ([element, target]) => {
|
|
fallbackAvoidance?.dispose();
|
|
fallbackAvoidance = undefined;
|
|
fallbackPosition.value = 'bottom-right';
|
|
if (!element || target) return;
|
|
fallbackAvoidance = bindFloatingAvoidance({
|
|
element,
|
|
isSelf: node => element.contains(node),
|
|
// Same numbers as .ws-plugin-toolbar-fallback in plugin-overlay.css.
|
|
margins: insets => ({ top: 16 + insets.top, right: 16, bottom: 16 + insets.bottom, left: 16 }),
|
|
defaultPosition: 'bottom-right',
|
|
apply: next => { fallbackPosition.value = next; },
|
|
// Sticky :hover after a tap on touch screens must not pause avoidance.
|
|
paused: () => drawerOpen.value
|
|
|| (matchMedia('(hover: hover)').matches && element.matches(':hover'))
|
|
|| element.contains(document.activeElement),
|
|
});
|
|
}, { flush: 'post' });
|
|
onBeforeUnmount(() => { fallbackAvoidance?.dispose(); fallbackAvoidance = undefined; });
|
|
onMounted(() => {
|
|
document.addEventListener('pointerdown', closeDrawerOutside, true);
|
|
window.addEventListener('resize', fitDrawer);
|
|
detectTheme(); window.addEventListener('message', message); window.addEventListener('resize', fitWindow); window.addEventListener('pointermove', moveDrag); window.addEventListener('pointerup', endDrag); window.addEventListener('pointercancel', endDrag); window.addEventListener('blur', endDrag);
|
|
if (props.connected && thirdPartyAllowed.value) void refresh();
|
|
observer = new MutationObserver(detectTheme); observer.observe(document.documentElement, { attributes: true, subtree: true, attributeFilter: ['data-ws-skin', 'data-ws-host-skin', 'data-theme'] });
|
|
});
|
|
onBeforeUnmount(() => {
|
|
document.removeEventListener('pointerdown', closeDrawerOutside, true);
|
|
window.removeEventListener('resize', fitDrawer);
|
|
});
|
|
onBeforeUnmount(() => { stop(); endDrag(); observer?.disconnect(); resizeObserver?.disconnect(); window.removeEventListener('message', message); window.removeEventListener('resize', fitWindow); window.removeEventListener('pointermove', moveDrag); window.removeEventListener('pointerup', endDrag); window.removeEventListener('pointercancel', endDrag); window.removeEventListener('blur', endDrag); });
|
|
</script>
|
|
<style src="../styles/plugin-overlay.css"></style>
|