feat: enhance plugin functionality

This commit is contained in:
时雨
2026-01-06 16:54:49 +08:00
committed by GitHub
parent 31d97b2968
commit 24255744df
48 changed files with 3089 additions and 3811 deletions

View File

@@ -212,6 +212,7 @@ export const AppWindowsLayer: React.FC<AppWindowsLayerProps> = ({ windows, onClo
<div
key={w.id}
ref={el => { windowEls.current[w.id] = el; }}
onMouseDown={() => onBringToFront(w.id)}
style={{
position: 'fixed',
top: w.maximized ? 0 : w.y,

View File

@@ -1,654 +0,0 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
FileOutlined,
DatabaseOutlined,
ExpandOutlined,
BgColorsOutlined,
ClockCircleOutlined,
FolderOutlined,
AimOutlined,
BulbOutlined,
ThunderboltOutlined,
AlertOutlined,
CameraOutlined,
ApiOutlined,
FieldTimeOutlined,
} from '@ant-design/icons';
import { API_BASE_URL, vfsApi, type VfsEntry } from '../../api/client';
import type { AppComponentProps } from '../types';
import { ImageCanvas } from './components/ImageCanvas';
import { ViewerControls } from './components/ViewerControls';
import { Filmstrip } from './components/Filmstrip';
import { InfoPanel } from './components/InfoPanel';
import type { HistogramData, RgbColor, InfoItem } from './components/types';
import { viewerStyles } from './styles';
interface ExplorerSnapshot {
path: string;
entries: VfsEntry[];
pagination?: { page: number; page_size: number; total: number };
sortBy?: string;
sortOrder?: string;
timestamp: number;
}
interface FileStat {
name?: string;
is_dir?: boolean;
size?: number;
mtime?: number;
mode?: number;
path?: string;
type?: string;
exif?: Record<string, unknown>;
}
declare global {
interface WindowEventMap {
'foxel:file-explorer-page': CustomEvent<ExplorerSnapshot>;
}
}
type ExplorerAwareWindow = Window & { __FOXEL_LAST_EXPLORER_PAGE__?: ExplorerSnapshot };
const DEFAULT_TONE: RgbColor = { r: 28, g: 32, b: 46 };
const isImageEntry = (ent: VfsEntry) => {
if (ent.is_dir) return false;
const maybe = ent as VfsEntry & { has_thumbnail?: boolean };
if (typeof maybe.has_thumbnail === 'boolean' && maybe.has_thumbnail) return true;
const ext = ent.name.split('.').pop()?.toLowerCase();
if (!ext) return false;
return ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'ico', 'tif', 'tiff', 'svg', 'heic', 'heif', 'arw', 'cr2', 'cr3', 'nef', 'rw2', 'orf', 'pef', 'dng'].includes(ext);
};
const buildThumbUrl = (fullPath: string, w = 180, h = 120) => {
const base = API_BASE_URL.replace(/\/+$/, '');
const clean = fullPath.replace(/^\/+/, '');
return `${base}/fs/thumb/${encodeURI(clean)}?w=${w}&h=${h}&fit=cover`;
};
const getDirectory = (fullPath: string) => {
const path = fullPath.startsWith('/') ? fullPath : `/${fullPath}`;
const idx = path.lastIndexOf('/');
if (idx <= 0) return '/';
return path.slice(0, idx) || '/';
};
const joinPath = (dir: string, name: string) => {
if (dir === '/' || dir === '') return `/${name}`;
return `${dir.replace(/\/$/, '')}/${name}`;
};
const clamp = (value: number, min: number, max: number) => Math.max(min, Math.min(max, value));
const parseNumberish = (raw: unknown): number | null => {
if (typeof raw === 'number') return raw;
if (typeof raw !== 'string') return null;
if (raw.includes('/')) {
const [a, b] = raw.split('/').map(v => Number(v));
if (!Number.isNaN(a) && !Number.isNaN(b) && b !== 0) return a / b;
}
const val = Number(raw);
return Number.isNaN(val) ? null : val;
};
const humanFileSize = (size: number | undefined) => {
if (typeof size !== 'number') return '-';
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let value = size;
let index = 0;
while (value >= 1024 && index < units.length - 1) {
value /= 1024;
index += 1;
}
return `${value.toFixed(index === 0 ? 0 : 1)} ${units[index]}`;
};
const readExplorerSnapshot = (dir: string): ExplorerSnapshot | null => {
if (typeof window === 'undefined') return null;
const snap = (window as ExplorerAwareWindow).__FOXEL_LAST_EXPLORER_PAGE__;
if (!snap) return null;
const snapshotPath = snap.path === '' ? '/' : snap.path;
const normalizedSnap = snapshotPath.endsWith('/') && snapshotPath !== '/' ? snapshotPath.slice(0, -1) : snapshotPath;
const normalizedTarget = dir.endsWith('/') && dir !== '/' ? dir.slice(0, -1) : dir;
if (normalizedSnap !== normalizedTarget) return null;
return snap;
};
const formatDateTime = (ts?: number) => {
if (!ts) return '-';
try {
return new Date(ts * 1000).toLocaleString();
} catch {
return '-';
}
};
const clampChannel = (value: number) => Math.max(0, Math.min(255, value));
const mixColor = (base: RgbColor, target: RgbColor, ratio: number): RgbColor => ({
r: clampChannel(base.r * (1 - ratio) + target.r * ratio),
g: clampChannel(base.g * (1 - ratio) + target.g * ratio),
b: clampChannel(base.b * (1 - ratio) + target.b * ratio),
});
const rgbToRgba = (color: RgbColor, alpha: number) => `rgba(${Math.round(color.r)}, ${Math.round(color.g)}, ${Math.round(color.b)}, ${alpha})`;
const computeImageStats = (img: HTMLImageElement): { histogram: HistogramData | null; dominantColor: RgbColor | null } => {
try {
const maxSide = 720;
const naturalWidth = img.naturalWidth || 1;
const naturalHeight = img.naturalHeight || 1;
const ratio = Math.min(1, maxSide / Math.max(naturalWidth, naturalHeight));
const width = Math.max(1, Math.floor(naturalWidth * ratio));
const height = Math.max(1, Math.floor(naturalHeight * ratio));
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d', { willReadFrequently: true });
if (!ctx) return { histogram: null, dominantColor: null };
ctx.drawImage(img, 0, 0, width, height);
const { data } = ctx.getImageData(0, 0, width, height);
const r = new Array(256).fill(0);
const g = new Array(256).fill(0);
const b = new Array(256).fill(0);
let rTotal = 0;
let gTotal = 0;
let bTotal = 0;
let count = 0;
for (let i = 0; i < data.length; i += 4) {
r[data[i]] += 1;
g[data[i + 1]] += 1;
b[data[i + 2]] += 1;
rTotal += data[i];
gTotal += data[i + 1];
bTotal += data[i + 2];
count += 1;
}
const histogram: HistogramData = { r, g, b };
if (count === 0) return { histogram, dominantColor: null };
const dominantColor: RgbColor = {
r: rTotal / count,
g: gTotal / count,
b: bTotal / count,
};
return { histogram, dominantColor };
} catch {
return { histogram: null, dominantColor: null };
}
};
export const ImageViewerApp: React.FC<AppComponentProps> = ({ filePath, entry, onRequestClose }) => {
const normalizedInitialPath = filePath.startsWith('/') ? filePath : `/${filePath}`;
const [activeEntry, setActiveEntry] = useState<VfsEntry>(entry);
const [activePath, setActivePath] = useState<string>(normalizedInitialPath);
const [imageUrl, setImageUrl] = useState<string>();
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string>();
const [stat, setStat] = useState<FileStat | null>(null);
const [histogram, setHistogram] = useState<HistogramData | null>(null);
const [dominantColor, setDominantColor] = useState<RgbColor | null>(null);
const [scale, setScale] = useState(1);
const [offset, setOffset] = useState({ x: 0, y: 0 });
const [rotate, setRotate] = useState(0);
const [isDragging, setIsDragging] = useState(false);
const [filmstrip, setFilmstrip] = useState<VfsEntry[]>([]);
const [pageInfo, setPageInfo] = useState<{ page: number; total: number; pageSize: number } | null>(null);
const containerRef = useRef<HTMLDivElement | null>(null);
const imageRef = useRef<HTMLImageElement | null>(null);
const dragPointRef = useRef<{ x: number; y: number } | null>(null);
const pinchDistanceRef = useRef<number | null>(null);
const transitionRef = useRef(false);
const filmstripRefs = useRef<Record<string, HTMLDivElement | null>>({});
const directory = useMemo(() => getDirectory(activePath), [activePath]);
const baseTone = useMemo<RgbColor>(() => dominantColor ?? DEFAULT_TONE, [dominantColor]);
const containerStyle = useMemo(() => {
const light = mixColor(baseTone, { r: 255, g: 255, b: 255 }, 0.18);
const shadow = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.62);
return {
...viewerStyles.container,
background: `linear-gradient(135deg, ${rgbToRgba(light, 0.78)} 0%, ${rgbToRgba(baseTone, 0.86)} 48%, ${rgbToRgba(shadow, 0.96)} 100%)`,
};
}, [baseTone]);
const mainBackdropStyle = useMemo(() => {
const glow = mixColor(baseTone, { r: 255, g: 255, b: 255 }, 0.32);
const shade = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.7);
return {
...viewerStyles.mainBackdrop,
background: `radial-gradient(circle at 18% 22%, ${rgbToRgba(glow, 0.38)}, ${rgbToRgba(shade, 0.94)} 68%)`,
};
}, [baseTone]);
const viewerStyle = useMemo(() => {
const surface = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.45);
const edge = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.65);
return {
...viewerStyles.viewer,
background: `linear-gradient(145deg, ${rgbToRgba(surface, 0.7)} 0%, ${rgbToRgba(edge, 0.92)} 100%)`,
backdropFilter: 'blur(28px)',
};
}, [baseTone]);
const controlsStyle = useMemo(() => {
const tone = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.52);
return {
...viewerStyles.controls,
background: rgbToRgba(tone, 0.74),
backdropFilter: 'blur(18px)',
};
}, [baseTone]);
const filmstripShellStyle = useMemo(() => {
const tone = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.56);
return {
...viewerStyles.filmstripShell,
background: rgbToRgba(tone, 0.7),
backdropFilter: 'blur(22px)',
};
}, [baseTone]);
const getThumbUrl = useCallback((item: VfsEntry) => {
const full = joinPath(directory, item.name);
return buildThumbUrl(full, 160, 120);
}, [directory]);
const sidePanelStyle = useMemo(() => {
const panel = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.6);
const border = rgbToRgba(mixColor(baseTone, { r: 255, g: 255, b: 255 }, 0.1), 0.28);
return {
...viewerStyles.sidePanel,
background: rgbToRgba(panel, 0.8),
backdropFilter: 'blur(28px)',
borderLeft: `1px solid ${border}`,
};
}, [baseTone]);
const histogramCardStyle = useMemo(() => {
const tone = mixColor(baseTone, { r: 0, g: 0, b: 0 }, 0.55);
const stroke = rgbToRgba(mixColor(baseTone, { r: 255, g: 255, b: 255 }, 0.12), 0.2);
return {
...viewerStyles.histogramCard,
background: rgbToRgba(tone, 0.58),
border: `1px solid ${stroke}`,
};
}, [baseTone]);
useEffect(() => {
const normalized = filePath.startsWith('/') ? filePath : `/${filePath}`;
setActiveEntry(entry);
setActivePath(normalized);
}, [entry, filePath]);
useEffect(() => {
let cancelled = false;
setLoading(true);
setError(undefined);
setHistogram(null);
setDominantColor(null);
const cleaned = activePath.replace(/^\/+/, '');
Promise.all([
vfsApi.getTempLinkToken(cleaned),
vfsApi.stat(activePath) as Promise<FileStat>,
])
.then(([token, metadata]) => {
if (cancelled) return;
setImageUrl(vfsApi.getTempPublicUrl(token.token));
setStat(metadata);
setScale(1);
setRotate(0);
setOffset({ x: 0, y: 0 });
})
.catch((err: unknown) => {
if (!cancelled) {
setError(err instanceof Error ? err.message : '加载失败');
}
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [activePath]);
const refreshFilmstrip = useCallback((dir: string) => {
const snap = readExplorerSnapshot(dir);
if (snap) {
const images = snap.entries.filter(isImageEntry);
const ensured = images.some(item => item.name === activeEntry.name) ? images : [...images, activeEntry];
setFilmstrip(ensured);
if (snap.pagination) {
setPageInfo({
page: snap.pagination.page,
pageSize: snap.pagination.page_size,
total: snap.pagination.total,
});
} else {
setPageInfo(null);
}
return;
}
setFilmstrip([activeEntry]);
setPageInfo(null);
}, [activeEntry]);
useEffect(() => {
refreshFilmstrip(directory);
}, [directory, refreshFilmstrip]);
useEffect(() => {
const handler = () => refreshFilmstrip(directory);
window.addEventListener('foxel:file-explorer-page', handler);
return () => window.removeEventListener('foxel:file-explorer-page', handler);
}, [directory, refreshFilmstrip]);
useEffect(() => {
const el = filmstripRefs.current[activeEntry.name];
if (el) {
el.scrollIntoView({ behavior: 'smooth', inline: 'center', block: 'nearest' });
}
}, [activeEntry, filmstrip]);
useEffect(() => {
const keyHandler = (e: KeyboardEvent) => {
if (e.key === 'ArrowRight') {
e.preventDefault();
switchRelative(1);
} else if (e.key === 'ArrowLeft') {
e.preventDefault();
switchRelative(-1);
} else if ((e.key === '+' || e.key === '=') && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
zoom(1.15);
} else if ((e.key === '-' || e.key === '_') && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
zoom(0.85);
}
};
window.addEventListener('keydown', keyHandler);
return () => window.removeEventListener('keydown', keyHandler);
});
const zoom = useCallback((factor: number) => {
setScale(prev => {
const next = clamp(prev * factor, 0.08, 10);
transitionRef.current = true;
window.setTimeout(() => { transitionRef.current = false; }, 120);
return next;
});
}, []);
const rotateImage = () => {
setRotate(prev => {
transitionRef.current = true;
window.setTimeout(() => { transitionRef.current = false; }, 180);
return (prev + 90) % 360;
});
};
const resetView = () => {
transitionRef.current = true;
window.setTimeout(() => { transitionRef.current = false; }, 160);
setScale(1);
setOffset({ x: 0, y: 0 });
setRotate(0);
};
const fitToScreen = () => {
resetView();
};
const onWheel = (e: React.WheelEvent) => {
e.preventDefault();
const container = containerRef.current;
if (!container) return;
const rect = container.getBoundingClientRect();
const cx = e.clientX - rect.left - rect.width / 2;
const cy = e.clientY - rect.top - rect.height / 2;
setScale(prev => {
const factor = e.deltaY < 0 ? 1.12 : 0.88;
const next = clamp(prev * factor, 0.08, 10);
const ratio = next / prev;
setOffset(off => ({ x: off.x - cx * (ratio - 1), y: off.y - cy * (ratio - 1) }));
transitionRef.current = true;
window.setTimeout(() => { transitionRef.current = false; }, 120);
return next;
});
};
const onMouseDown = (e: React.MouseEvent) => {
if (e.button !== 0) return;
e.preventDefault();
setIsDragging(true);
dragPointRef.current = { x: e.clientX, y: e.clientY };
};
const onMouseMove = (e: React.MouseEvent) => {
if (!isDragging || !dragPointRef.current) return;
e.preventDefault();
const dx = e.clientX - dragPointRef.current.x;
const dy = e.clientY - dragPointRef.current.y;
dragPointRef.current = { x: e.clientX, y: e.clientY };
setOffset(off => ({ x: off.x + dx, y: off.y + dy }));
};
const stopDragging = () => {
setIsDragging(false);
dragPointRef.current = null;
};
const dist = (t1: React.Touch, t2: React.Touch) => Math.hypot(t1.clientX - t2.clientX, t1.clientY - t2.clientY);
const onTouchStart = (e: React.TouchEvent) => {
if (e.touches.length === 1) {
const t = e.touches[0];
dragPointRef.current = { x: t.clientX, y: t.clientY };
} else if (e.touches.length === 2) {
pinchDistanceRef.current = dist(e.touches[0], e.touches[1]);
}
};
const onTouchMove = (e: React.TouchEvent) => {
if (e.touches.length === 1 && dragPointRef.current) {
const t = e.touches[0];
const dx = t.clientX - dragPointRef.current.x;
const dy = t.clientY - dragPointRef.current.y;
dragPointRef.current = { x: t.clientX, y: t.clientY };
setOffset(off => ({ x: off.x + dx, y: off.y + dy }));
} else if (e.touches.length === 2 && pinchDistanceRef.current) {
const dNow = dist(e.touches[0], e.touches[1]);
const ratio = dNow / pinchDistanceRef.current;
pinchDistanceRef.current = dNow;
setScale(prev => clamp(prev * ratio, 0.08, 10));
}
};
const onTouchEnd = () => {
pinchDistanceRef.current = null;
dragPointRef.current = null;
};
const onDoubleClick = (e: React.MouseEvent) => {
e.preventDefault();
const next = scale > 1.4 ? 1 : 2.2;
const container = containerRef.current;
if (!container) {
setScale(next);
return;
}
const rect = container.getBoundingClientRect();
const cx = e.clientX - rect.left - rect.width / 2;
const cy = e.clientY - rect.top - rect.height / 2;
const ratio = next / scale;
setScale(next);
setOffset(off => ({ x: off.x - cx * (ratio - 1), y: off.y - cy * (ratio - 1) }));
};
const handleImageLoaded = () => {
const img = imageRef.current;
if (!img) return;
const stats = computeImageStats(img);
setHistogram(stats.histogram);
setDominantColor(stats.dominantColor);
};
const switchEntry = (target: VfsEntry) => {
const nextPath = joinPath(directory, target.name);
setActiveEntry(target);
setActivePath(nextPath);
};
const switchRelative = (step: number) => {
if (filmstrip.length <= 1) return;
const currentIndex = filmstrip.findIndex(item => item.name === activeEntry.name);
if (currentIndex === -1) return;
const target = filmstrip[(currentIndex + step + filmstrip.length) % filmstrip.length];
if (target) switchEntry(target);
};
const scaleLabel = `${(scale * 100).toFixed(scale >= 1 ? 0 : 1)}%`;
const imageStyle: React.CSSProperties = {
maxWidth: '100%',
maxHeight: '100%',
transform: `translate(${offset.x}px, ${offset.y}px) scale(${scale}) rotate(${rotate}deg)`,
transition: transitionRef.current ? 'transform 0.18s cubic-bezier(.4,.8,.4,1)' : undefined,
cursor: isDragging ? 'grabbing' : scale > 1 ? 'grab' : 'zoom-in',
willChange: 'transform',
};
const controlsNode = (
<ViewerControls
style={controlsStyle}
onPrev={() => switchRelative(-1)}
onNext={() => switchRelative(1)}
onZoomIn={() => zoom(1.18)}
onZoomOut={() => zoom(0.82)}
onRotate={rotateImage}
onReset={resetView}
onFit={fitToScreen}
disableSwitch={filmstrip.length <= 1}
/>
);
const exif = (stat?.exif ?? {}) as Record<string, unknown>;
const infoIconStyle: React.CSSProperties = { fontSize: 15, color: 'rgba(255,255,255,0.62)' };
const exifValue = (key: string): string | number | null => {
const value = exif[key];
if (typeof value === 'string' || typeof value === 'number') return value;
return null;
};
const focalLength = (() => {
const v = parseNumberish(exifValue('37386') ?? exifValue('37377'));
return v ? `${v.toFixed(1)} mm` : null;
})();
const aperture = (() => {
const v = parseNumberish(exifValue('33437') ?? exifValue('37378'));
return v ? `f/${v.toFixed(1)}` : null;
})();
const exposure = (() => {
const v = parseNumberish(exifValue('33434'));
if (!v) return null;
if (v >= 1) return `${v.toFixed(1)} s`;
const denom = Math.max(1, Math.round(1 / v));
return `1/${denom}`;
})();
const isoValue = exifValue('34855') ?? exifValue('34864');
const width = parseNumberish(exifValue('40962'));
const height = parseNumberish(exifValue('40963'));
const colorSpace = exifValue('40961');
const cameraMake = exifValue('271');
const cameraModel = exifValue('272');
const lensModel = exifValue('42036');
const captureTime = exifValue('36867') ?? exifValue('36868') ?? exifValue('306');
const basicList: InfoItem[] = [
{ label: '文件名', value: activeEntry.name, icon: <FileOutlined style={infoIconStyle} /> },
{ label: '文件大小', value: humanFileSize(stat?.size), icon: <DatabaseOutlined style={infoIconStyle} /> },
{ label: '分辨率', value: width && height ? `${width} × ${height}` : null, icon: <ExpandOutlined style={infoIconStyle} /> },
{ label: '颜色空间', value: colorSpace ?? null, icon: <BgColorsOutlined style={infoIconStyle} /> },
{ label: '修改时间', value: stat?.mtime ? formatDateTime(stat.mtime) : null, icon: <ClockCircleOutlined style={infoIconStyle} /> },
{ label: '路径', value: typeof stat?.path === 'string' ? stat.path : activePath, icon: <FolderOutlined style={infoIconStyle} /> },
];
const shootingList: InfoItem[] = [
{ label: '焦距', value: focalLength, icon: <AimOutlined style={infoIconStyle} /> },
{ label: '光圈', value: aperture, icon: <BulbOutlined style={infoIconStyle} /> },
{ label: '快门', value: exposure, icon: <ThunderboltOutlined style={infoIconStyle} /> },
{ label: 'ISO', value: isoValue != null ? isoValue.toString() : null, icon: <AlertOutlined style={infoIconStyle} /> },
];
const deviceList: InfoItem[] = [
{
label: '相机',
value: cameraModel ? `${cameraMake ? `${cameraMake} ` : ''}${cameraModel}` : (cameraMake ?? null),
icon: <CameraOutlined style={infoIconStyle} />,
},
{ label: '镜头', value: lensModel ?? null, icon: <ApiOutlined style={infoIconStyle} /> },
];
const miscList: InfoItem[] = [
{ label: '拍摄时间', value: captureTime, icon: <FieldTimeOutlined style={infoIconStyle} /> },
];
return (
<div style={containerStyle}>
<section style={viewerStyles.main}>
<div style={mainBackdropStyle} />
<div style={viewerStyles.mainContent}>
<ImageCanvas
containerRef={containerRef}
imageRef={imageRef}
viewerStyle={viewerStyle}
controls={controlsNode}
scaleLabel={scaleLabel}
imageStyle={imageStyle}
loading={loading}
error={error}
imageUrl={imageUrl}
activeEntry={activeEntry}
onRequestClose={onRequestClose}
onImageLoad={handleImageLoaded}
onWheel={onWheel}
onMouseDown={onMouseDown}
onMouseMove={onMouseMove}
onMouseLeave={stopDragging}
onMouseUp={stopDragging}
onDoubleClick={onDoubleClick}
onTouchStart={onTouchStart}
onTouchMove={onTouchMove}
onTouchEnd={onTouchEnd}
/>
<Filmstrip
shellStyle={filmstripShellStyle}
listStyle={viewerStyles.filmstrip}
entries={filmstrip}
activeEntry={activeEntry}
onSelect={switchEntry}
filmstripRefs={filmstripRefs}
pageInfo={pageInfo}
getThumbUrl={getThumbUrl}
/>
</div>
</section>
<InfoPanel
style={sidePanelStyle}
histogramCardStyle={histogramCardStyle}
title={activeEntry.name}
captureTime={captureTime ?? null}
basicList={basicList}
shootingList={shootingList}
deviceList={deviceList}
miscList={miscList}
histogram={histogram}
/>
</div>
);
};

View File

@@ -1,94 +0,0 @@
import React from 'react';
import { Typography } from 'antd';
import type { VfsEntry } from '../../../api/client';
interface PageInfo {
page: number;
total: number;
pageSize: number;
}
interface FilmstripProps {
shellStyle: React.CSSProperties;
listStyle: React.CSSProperties;
entries: VfsEntry[];
activeEntry: VfsEntry;
onSelect: (entry: VfsEntry) => void;
filmstripRefs: React.MutableRefObject<Record<string, HTMLDivElement | null>>;
pageInfo: PageInfo | null;
getThumbUrl: (entry: VfsEntry) => string;
}
export const Filmstrip: React.FC<FilmstripProps> = ({
shellStyle,
listStyle,
entries,
activeEntry,
onSelect,
filmstripRefs,
pageInfo,
getThumbUrl,
}) => (
<div style={shellStyle}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 10 }}>
<Typography.Text style={{ color: 'rgba(255,255,255,0.72)', fontWeight: 500 }}>
· {entries.length}
</Typography.Text>
{pageInfo && (
<Typography.Text style={{ color: 'rgba(255,255,255,0.45)', fontSize: 12 }}>
{pageInfo.page} / {Math.max(1, Math.ceil(pageInfo.total / pageInfo.pageSize))}
</Typography.Text>
)}
</div>
<div style={listStyle}>
{entries.map(item => {
const active = item.name === activeEntry.name;
return (
<div
key={`${item.name}-${item.mtime ?? ''}`}
ref={el => { filmstripRefs.current[item.name] = el; }}
onClick={() => onSelect(item)}
style={{
width: 84,
height: 64,
overflow: 'hidden',
border: active ? '2px solid #4e9bff' : '2px solid transparent',
boxShadow: active ? '0 0 0 4px rgba(78,155,255,0.28)' : '0 10px 28px rgba(0,0,0,0.45)',
cursor: 'pointer',
position: 'relative',
flex: '0 0 auto',
}}
>
<img
src={getThumbUrl(item)}
alt={item.name}
style={{ width: '100%', height: '100%', objectFit: 'cover', filter: active ? 'saturate(1)' : 'saturate(0.65)' }}
/>
{active && (
<div
style={{
position: 'absolute',
bottom: 4,
left: 6,
right: 6,
padding: '2px 4px',
background: 'rgba(0,0,0,0.55)',
color: '#fff',
fontSize: 10,
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
}}
>
{item.name}
</div>
)}
</div>
);
})}
{entries.length === 0 && (
<div style={{ color: 'rgba(255,255,255,0.45)' }}></div>
)}
</div>
</div>
);

View File

@@ -1,99 +0,0 @@
import React from 'react';
import { Spin, Typography, Tooltip, Button } from 'antd';
import { CloseOutlined } from '@ant-design/icons';
import type { VfsEntry } from '../../../api/client';
import { viewerStyles } from '../styles';
interface ImageCanvasProps {
containerRef: React.RefObject<HTMLDivElement | null>;
imageRef: React.RefObject<HTMLImageElement | null>;
viewerStyle: React.CSSProperties;
controls: React.ReactNode;
scaleLabel: string;
imageStyle: React.CSSProperties;
loading: boolean;
error?: string;
imageUrl?: string;
activeEntry: VfsEntry;
onRequestClose: () => void;
onImageLoad: () => void;
onWheel: React.WheelEventHandler<HTMLDivElement>;
onMouseDown: React.MouseEventHandler<HTMLDivElement>;
onMouseMove: React.MouseEventHandler<HTMLDivElement>;
onMouseLeave: React.MouseEventHandler<HTMLDivElement>;
onMouseUp: React.MouseEventHandler<HTMLDivElement>;
onDoubleClick: React.MouseEventHandler<HTMLDivElement>;
onTouchStart: React.TouchEventHandler<HTMLDivElement>;
onTouchMove: React.TouchEventHandler<HTMLDivElement>;
onTouchEnd: React.TouchEventHandler<HTMLDivElement>;
}
export const ImageCanvas: React.FC<ImageCanvasProps> = ({
containerRef,
imageRef,
viewerStyle,
controls,
scaleLabel,
imageStyle,
loading,
error,
imageUrl,
activeEntry,
onRequestClose,
onImageLoad,
onWheel,
onMouseDown,
onMouseMove,
onMouseLeave,
onMouseUp,
onDoubleClick,
onTouchStart,
onTouchMove,
onTouchEnd,
}) => (
<div
ref={containerRef}
style={viewerStyle}
onWheel={onWheel}
onMouseDown={onMouseDown}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
onMouseUp={onMouseUp}
onDoubleClick={onDoubleClick}
onTouchStart={onTouchStart}
onTouchMove={onTouchMove}
onTouchEnd={onTouchEnd}
>
<div style={viewerStyles.viewerCloseWrap}>
<Tooltip title="关闭">
<Button
type="text"
icon={<CloseOutlined />}
onClick={onRequestClose}
style={viewerStyles.viewerClose}
/>
</Tooltip>
</div>
{loading ? (
<Spin tip="加载中" />
) : error ? (
<Typography.Text type="danger">{error}</Typography.Text>
) : imageUrl ? (
<img
ref={imageRef}
src={imageUrl}
alt={activeEntry.name}
onLoad={onImageLoad}
draggable={false}
crossOrigin="anonymous"
style={imageStyle}
/>
) : (
<Typography.Text></Typography.Text>
)}
<div style={viewerStyles.scaleBadge}>{scaleLabel}</div>
{controls}
</div>
);

View File

@@ -1,116 +0,0 @@
import React from 'react';
import { Typography, Empty } from 'antd';
import type { HistogramData, InfoItem } from './types';
interface InfoPanelProps {
style: React.CSSProperties;
histogramCardStyle: React.CSSProperties;
title: string;
captureTime: string | number | null;
basicList: InfoItem[];
shootingList: InfoItem[];
deviceList: InfoItem[];
miscList: InfoItem[];
histogram: HistogramData | null;
}
const SectionTitle: React.FC<{ children: React.ReactNode }> = ({ children }) => (
<Typography.Title level={5} style={{ color: '#fff', fontSize: 15, marginTop: 24, marginBottom: 12 }}>
{children}
</Typography.Title>
);
const HistogramPlot: React.FC<{ data: HistogramData | null }> = ({ data }) => {
if (!data) {
return <Empty description="无法解析直方图" image={Empty.PRESENTED_IMAGE_SIMPLE} />;
}
const width = 260;
const height = 140;
const max = Math.max(...data.r, ...data.g, ...data.b, 1);
const toPath = (arr: number[]) => arr
.map((value, index) => {
const x = (index / 255) * width;
const y = height - (value / max) * height;
return `${index === 0 ? 'M' : 'L'}${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(' ');
return (
<svg width={width} height={height} viewBox={`0 0 ${width} ${height}`} style={{ width: '100%' }}>
<rect x={0} y={0} width={width} height={height} fill="rgba(255,255,255,0.04)" />
<path d={toPath(data.r)} stroke="rgba(255,99,132,0.88)" fill="none" strokeWidth={1.3} />
<path d={toPath(data.g)} stroke="rgba(75,192,192,0.88)" fill="none" strokeWidth={1.3} />
<path d={toPath(data.b)} stroke="rgba(54,162,235,0.88)" fill="none" strokeWidth={1.3} />
</svg>
);
};
const InfoRows: React.FC<{ items: InfoItem[] }> = ({ items }) => (
<div style={{ display: 'grid', gridTemplateColumns: '100px 1fr', rowGap: 10, columnGap: 12 }}>
{items
.filter(item => item.value !== null && item.value !== undefined && item.value !== '')
.map(item => (
<React.Fragment key={item.label}>
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, color: 'rgba(255,255,255,0.55)' }}>
{item.icon && <span style={{ display: 'inline-flex', alignItems: 'center' }}>{item.icon}</span>}
<span>{item.label}</span>
</span>
<span style={{ color: '#fff', wordBreak: 'break-all' }}>{item.value}</span>
</React.Fragment>
))}
</div>
);
export const InfoPanel: React.FC<InfoPanelProps> = ({
style,
histogramCardStyle,
title,
captureTime,
basicList,
shootingList,
deviceList,
miscList,
histogram,
}) => (
<aside style={style}>
<Typography.Title level={3} style={{ color: '#fff', marginTop: 6, wordBreak: 'break-all' }}>
{title}
</Typography.Title>
{captureTime && (
<Typography.Text style={{ color: 'rgba(255,255,255,0.6)' }}> {captureTime}</Typography.Text>
)}
<SectionTitle></SectionTitle>
<InfoRows items={basicList} />
{shootingList.some(i => i.value) && (
<>
<SectionTitle></SectionTitle>
<InfoRows items={shootingList} />
</>
)}
{deviceList.some(i => i.value) && (
<>
<SectionTitle></SectionTitle>
<InfoRows items={deviceList} />
</>
)}
{miscList.some(i => i.value) && (
<>
<SectionTitle></SectionTitle>
<InfoRows items={miscList} />
</>
)}
<SectionTitle></SectionTitle>
<div style={histogramCardStyle}>
<HistogramPlot data={histogram} />
<div style={{ marginTop: 12, display: 'flex', gap: 12, fontSize: 12 }}>
<span style={{ color: 'rgba(255,99,132,0.88)' }}>R</span>
<span style={{ color: 'rgba(75,192,192,0.88)' }}>G</span>
<span style={{ color: 'rgba(54,162,235,0.88)' }}>B</span>
</div>
</div>
</aside>
);

View File

@@ -1,73 +0,0 @@
import React from 'react';
import { Button, Tooltip } from 'antd';
import {
LeftOutlined,
RightOutlined,
ZoomInOutlined,
ZoomOutOutlined,
RotateRightOutlined,
ReloadOutlined,
CompressOutlined,
} from '@ant-design/icons';
interface ViewerControlsProps {
style: React.CSSProperties;
onPrev: () => void;
onNext: () => void;
onZoomIn: () => void;
onZoomOut: () => void;
onRotate: () => void;
onReset: () => void;
onFit: () => void;
disableSwitch: boolean;
}
export const ViewerControls: React.FC<ViewerControlsProps> = ({
style,
onPrev,
onNext,
onZoomIn,
onZoomOut,
onRotate,
onReset,
onFit,
disableSwitch,
}) => (
<div style={style}>
<Tooltip title="上一张">
<Button
shape="circle"
type="text"
icon={<LeftOutlined />}
onClick={onPrev}
disabled={disableSwitch}
style={{ color: '#fff' }}
/>
</Tooltip>
<Tooltip title="缩小">
<Button shape="circle" type="text" icon={<ZoomOutOutlined />} onClick={onZoomOut} style={{ color: '#fff' }} />
</Tooltip>
<Tooltip title="放大">
<Button shape="circle" type="text" icon={<ZoomInOutlined />} onClick={onZoomIn} style={{ color: '#fff' }} />
</Tooltip>
<Tooltip title="旋转 90°">
<Button shape="circle" type="text" icon={<RotateRightOutlined />} onClick={onRotate} style={{ color: '#fff' }} />
</Tooltip>
<Tooltip title="重置">
<Button shape="circle" type="text" icon={<ReloadOutlined />} onClick={onReset} style={{ color: '#fff' }} />
</Tooltip>
<Tooltip title="适应窗口">
<Button shape="circle" type="text" icon={<CompressOutlined />} onClick={onFit} style={{ color: '#fff' }} />
</Tooltip>
<Tooltip title="下一张">
<Button
shape="circle"
type="text"
icon={<RightOutlined />}
onClick={onNext}
disabled={disableSwitch}
style={{ color: '#fff' }}
/>
</Tooltip>
</div>
);

View File

@@ -1,19 +0,0 @@
import type { ReactNode } from 'react';
export interface HistogramData {
r: number[];
g: number[];
b: number[];
}
export interface RgbColor {
r: number;
g: number;
b: number;
}
export interface InfoItem {
label: string;
value: string | number | null;
icon?: ReactNode;
}

View File

@@ -1,23 +0,0 @@
import type { AppDescriptor } from '../types';
import { ImageViewerApp } from './ImageViewer.tsx';
const supportedExts = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico', 'avif', 'arw', 'cr2', 'cr3', 'nef', 'rw2', 'orf', 'pef', 'dng'];
export const descriptor: AppDescriptor = {
key: 'image-viewer',
name: '图片查看器',
iconUrl: 'https://api.iconify.design/mdi:image.svg',
description: '内置图片查看器,支持常见图片与部分 RAW 格式预览。',
author: 'Foxel',
supportedExts,
supported: (entry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
return supportedExts.includes(ext);
},
component: ImageViewerApp,
default: true,
defaultMaximized:true,
useSystemWindow:false,
defaultBounds: { width: 820, height: 620, x: 140, y: 96 }
};

View File

@@ -1,106 +0,0 @@
export const viewerStyles = {
container: {
width: '100%',
height: '100%',
boxSizing: 'border-box' as const,
display: 'grid',
gridTemplateColumns: 'minmax(0, 1fr) 320px',
columnGap: 0,
color: '#fff',
overflow: 'hidden',
},
main: {
position: 'relative' as const,
overflow: 'hidden',
display: 'flex',
flexDirection: 'column' as const,
boxShadow: '0 28px 80px rgba(0,0,0,0.55)',
minHeight: 0,
},
mainBackdrop: {
position: 'absolute' as const,
inset: 0,
},
mainContent: {
position: 'relative' as const,
zIndex: 1,
display: 'flex',
flexDirection: 'column' as const,
flex: 1,
padding: 0,
minHeight: 0,
minWidth: 0,
},
viewer: {
flex: 1,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
position: 'relative' as const,
overflow: 'hidden',
boxShadow: '0 24px 60px rgba(0,0,0,0.5)',
touchAction: 'none' as const,
minHeight: 0,
},
controls: {
position: 'absolute' as const,
bottom: 16,
left: '50%',
transform: 'translateX(-50%)',
display: 'flex',
gap: 16,
padding: '8px 18px',
borderRadius: 24,
alignItems: 'center',
},
scaleBadge: {
position: 'absolute' as const,
bottom: 64,
left: 16,
color: 'rgba(255,255,255,0.7)',
fontSize: 12,
letterSpacing: 0.2,
},
filmstripShell: {
marginTop: 0,
padding: '3px 12px',
boxShadow: '0 16px 42px rgba(0,0,0,0.52)',
},
filmstrip: {
display: 'flex',
overflowX: 'auto' as const,
gap: 12,
paddingBottom: 4,
},
sidePanel: {
boxShadow: '0 28px 80px rgba(0,0,0,0.55)',
padding: '20px 24px',
display: 'flex',
flexDirection: 'column' as const,
overflowY: 'auto' as const,
minHeight: 0,
},
histogramCard: {
padding: '12px 12px 18px',
background: 'rgba(0,0,0,0.34)',
borderRadius: 0,
},
viewerCloseWrap: {
position: 'absolute' as const,
top: 16,
right: 16,
zIndex: 2,
},
viewerClose: {
color: '#fff',
background: 'rgba(0,0,0,0.4)',
border: '1px solid rgba(255,255,255,0.25)',
boxShadow: '0 8px 18px rgba(0,0,0,0.45)',
borderRadius: '100%',
width: 32,
height: 32,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
};

View File

@@ -1,83 +0,0 @@
import React, { useEffect, useState } from 'react';
import { vfsApi } from '../../api/client';
import type { AppComponentProps } from '../types';
import { Spin, Result, Button } from 'antd';
import { useSystemStatus } from '../../contexts/SystemContext';
export const OfficeViewerApp: React.FC<AppComponentProps> = ({ filePath, onRequestClose }) => {
const systemStatus = useSystemStatus();
const fileDomain = systemStatus?.file_domain;
const [url, setUrl] = useState<string>();
const [loading, setLoading] = useState(true);
const [err, setErr] = useState<string>();
useEffect(() => {
let cancelled = false;
setLoading(true);
setErr(undefined);
setUrl(undefined);
vfsApi.getTempLinkToken(filePath.replace(/^\/+/, ''))
.then(res => {
if (cancelled) return;
const baseUrl = fileDomain || window.location.origin;
const fullUrl = new URL(res.url, baseUrl).href;
const officeUrl = `https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(fullUrl)}`;
setUrl(officeUrl);
})
.catch(e => {
if (!cancelled) {
setErr(e.message || '加载文档链接失败');
}
})
.finally(() => {
if (!cancelled) {
setLoading(false);
}
});
return () => {
cancelled = true;
};
}, [filePath, fileDomain]);
if (loading) {
return (
<div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<Spin tip="正在准备文档..." />
</div>
);
}
if (err) {
return (
<Result
status="error"
title="无法加载文档"
subTitle={err}
extra={<Button type="primary" onClick={onRequestClose}></Button>}
/>
);
}
return (
<div style={{ width: '100%', height: '100%', background: 'var(--ant-color-bg-container, #fff)' }}>
{url ? (
<iframe
src={url}
width="100%"
height="100%"
frameBorder="0"
title="Office Document Viewer"
/>
) : (
<Result
status="warning"
title="文档链接无效"
subTitle="未能成功生成文档的在线查看链接。"
extra={<Button type="primary" onClick={onRequestClose}></Button>}
/>
)}
</div>
);
};

View File

@@ -1,21 +0,0 @@
import type { AppDescriptor } from '../types';
import { OfficeViewerApp } from './OfficeViewer.tsx';
const supportedExts = ['docx', 'xlsx', 'pptx', 'doc', 'xls', 'ppt'];
export const descriptor: AppDescriptor = {
key: 'office-viewer',
name: 'Office 文档查看器',
iconUrl: 'https://api.iconify.design/mdi:file-word-box.svg',
description: '内置 Office 文档查看器,支持 Word/Excel/PowerPoint 文件预览。',
author: 'Foxel',
supportedExts,
supported: (entry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
return supportedExts.includes(ext);
},
component: OfficeViewerApp,
default: true,
defaultBounds: { width: 1024, height: 768, x: 150, y: 100 }
};

View File

@@ -1,74 +0,0 @@
import React, { useEffect, useState } from 'react';
import { Spin, Result, Button } from 'antd';
import type { AppComponentProps } from '../types';
import { vfsApi } from '../../api/client';
export const PdfViewerApp: React.FC<AppComponentProps> = ({ filePath, onRequestClose }) => {
const [url, setUrl] = useState<string>();
const [loading, setLoading] = useState(true);
const [err, setErr] = useState<string>();
useEffect(() => {
let cancelled = false;
setLoading(true);
setErr(undefined);
setUrl(undefined);
vfsApi.getTempLinkToken(filePath.replace(/^\/+/, ''))
.then(res => {
if (cancelled) return;
const publicUrl = vfsApi.getTempPublicUrl(res.token);
setUrl(publicUrl + '#toolbar=1&navpanes=1');
})
.catch(e => {
if (!cancelled) setErr(e.message || '获取临时链接失败');
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => { cancelled = true; };
}, [filePath]);
if (loading) {
return (
<div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<Spin tip="正在加载 PDF..." />
</div>
);
}
if (err) {
return (
<Result
status="error"
title="无法加载 PDF"
subTitle={err}
extra={<Button type="primary" onClick={onRequestClose}></Button>}
/>
);
}
if (!url) {
return (
<Result
status="warning"
title="无可用链接"
subTitle="未能生成 PDF 的临时访问链接"
extra={<Button type="primary" onClick={onRequestClose}></Button>}
/>
);
}
return (
<div style={{ width: '100%', height: '100%', background: 'var(--ant-color-bg-container, #fff)' }}>
<iframe
src={url}
width="100%"
height="100%"
title="PDF Viewer"
style={{ border: 'none' }}
/>
</div>
);
};

View File

@@ -1,21 +0,0 @@
import type { AppDescriptor } from '../types';
import { PdfViewerApp } from './PdfViewer';
const supportedExts = ['pdf'];
export const descriptor: AppDescriptor = {
key: 'pdf-viewer',
name: 'PDF 查看器',
iconUrl: 'https://api.iconify.design/mdi:file-pdf-box.svg',
description: '内置 PDF 查看器。',
author: 'Foxel',
supportedExts,
supported: (entry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
return supportedExts.includes(ext);
},
component: PdfViewerApp,
default: true,
defaultBounds: { width: 1024, height: 768, x: 160, y: 100 },
};

View File

@@ -1,108 +1,109 @@
import React, { useRef, useState } from 'react';
import React, { useEffect, useMemo, useRef } from 'react';
import type { AppComponentProps, AppOpenComponentProps } from '../types';
import { vfsApi } from '../../api/vfs';
import { loadPlugin, ensureManifest, type RegisteredPlugin } from '../../plugins/runtime';
import type { PluginItem } from '../../api/plugins';
import { useAsyncSafeEffect } from '../../hooks/useAsyncSafeEffect';
import { useI18n } from '../../i18n';
export interface PluginAppHostProps extends AppComponentProps {
plugin: PluginItem;
}
export const PluginAppHost: React.FC<PluginAppHostProps> = ({ plugin, filePath, entry, onRequestClose }) => {
const containerRef = useRef<HTMLDivElement>(null);
const [error, setError] = useState<string | null>(null);
function buildPluginFrameUrl(params: Record<string, string>): string {
const qs = new URLSearchParams(params);
return `/plugin-frame.html?${qs.toString()}`;
}
/**
* 插件宿主组件 - 文件打开模式
* 使用 iframe 隔离渲染与样式,避免插件污染宿主 DOM/CSS。
* 注意:同源且不加 sandbox 时,不是安全沙箱(插件仍可通过 window.parent 访问宿主)。
*/
export const PluginAppHost: React.FC<PluginAppHostProps> = ({
plugin,
filePath,
onRequestClose,
}) => {
const iframeRef = useRef<HTMLIFrameElement>(null);
const onCloseRef = useRef(onRequestClose);
onCloseRef.current = onRequestClose;
const { t } = useI18n();
const pluginRef = useRef<RegisteredPlugin | null>(null);
useAsyncSafeEffect(
async ({ isDisposed }) => {
try {
const p = await loadPlugin(plugin);
if (isDisposed()) return;
pluginRef.current = p;
await ensureManifest(plugin.id, p);
if (isDisposed()) return;
const token = await vfsApi.getTempLinkToken(filePath);
if (isDisposed()) return;
const downloadUrl = vfsApi.getTempPublicUrl(token.token);
if (isDisposed() || !containerRef.current) return;
await p.mount(containerRef.current, {
filePath,
entry,
urls: { downloadUrl },
host: { close: () => onCloseRef.current() },
});
} catch (e: any) {
if (!isDisposed()) setError(e?.message || t('Plugin run failed'));
}
},
[plugin.id, plugin.url, filePath],
() => {
try {
if (pluginRef.current?.unmount && containerRef.current) {
pluginRef.current.unmount(containerRef.current);
}
} catch { void 0; }
},
const src = useMemo(
() =>
buildPluginFrameUrl({
pluginKey: plugin.key,
mode: 'file',
filePath,
}),
[plugin.key, filePath]
);
if (error) {
return <div style={{ padding: 12, color: 'red' }}>{t('Plugin Error')}: {error}</div>;
}
useEffect(() => {
const onMessage = (ev: MessageEvent) => {
if (ev.origin !== window.location.origin) return;
if (ev.source !== iframeRef.current?.contentWindow) return;
const data = ev.data as any;
if (!data || typeof data !== 'object') return;
if (data.type === 'foxel-plugin:close' && data.pluginKey === plugin.key) {
onCloseRef.current();
}
};
return <div ref={containerRef} style={{ width: '100%', height: '100%', overflow: 'auto' }} />;
window.addEventListener('message', onMessage);
return () => window.removeEventListener('message', onMessage);
}, [plugin.key]);
return (
<iframe
ref={iframeRef}
src={src}
title={`plugin:${plugin.key}`}
style={{ width: '100%', height: '100%', border: 0, display: 'block' }}
/>
);
};
export interface PluginAppOpenHostProps extends AppOpenComponentProps {
plugin: PluginItem;
}
/**
* 插件宿主组件 - 独立应用模式
* 使用 iframe 隔离渲染与样式,避免插件污染宿主 DOM/CSS。
* 注意:同源且不加 sandbox 时,不是安全沙箱(插件仍可通过 window.parent 访问宿主)。
*/
export const PluginAppOpenHost: React.FC<PluginAppOpenHostProps> = ({ plugin, onRequestClose }) => {
const containerRef = useRef<HTMLDivElement>(null);
const [error, setError] = useState<string | null>(null);
const iframeRef = useRef<HTMLIFrameElement>(null);
const onCloseRef = useRef(onRequestClose);
onCloseRef.current = onRequestClose;
const { t } = useI18n();
const pluginRef = useRef<RegisteredPlugin | null>(null);
useAsyncSafeEffect(
async ({ isDisposed }) => {
try {
const p = await loadPlugin(plugin);
if (isDisposed()) return;
pluginRef.current = p;
await ensureManifest(plugin.id, p);
if (isDisposed() || !containerRef.current) return;
if (typeof p.mountApp !== 'function') {
throw new Error('该插件不支持独立打开');
}
await p.mountApp(containerRef.current, {
host: { close: () => onCloseRef.current() },
});
} catch (e: any) {
if (!isDisposed()) setError(e?.message || t('Plugin run failed'));
}
},
[plugin.id, plugin.url],
() => {
try {
if (!containerRef.current) return;
const p = pluginRef.current;
if (p?.unmountApp) return p.unmountApp(containerRef.current);
if (p?.unmount) return p.unmount(containerRef.current);
} catch { void 0; }
},
const src = useMemo(
() =>
buildPluginFrameUrl({
pluginKey: plugin.key,
mode: 'app',
}),
[plugin.key]
);
if (error) {
return <div style={{ padding: 12, color: 'red' }}>{t('Plugin Error')}: {error}</div>;
}
useEffect(() => {
const onMessage = (ev: MessageEvent) => {
if (ev.origin !== window.location.origin) return;
if (ev.source !== iframeRef.current?.contentWindow) return;
const data = ev.data as any;
if (!data || typeof data !== 'object') return;
if (data.type === 'foxel-plugin:close' && data.pluginKey === plugin.key) {
onCloseRef.current();
}
};
return <div ref={containerRef} style={{ width: '100%', height: '100%', overflow: 'auto' }} />;
window.addEventListener('message', onMessage);
return () => window.removeEventListener('message', onMessage);
}, [plugin.key]);
return (
<iframe
ref={iframeRef}
src={src}
title={`plugin:${plugin.key}:app`}
style={{ width: '100%', height: '100%', border: 0, display: 'block' }}
/>
);
};

View File

@@ -1,274 +0,0 @@
import React, { useState, useEffect, useCallback, useRef, useMemo, Suspense } from 'react';
import { Layout, Spin, Button, Space, message } from 'antd';
import type { AppComponentProps } from '../types';
import { vfsApi } from '../../api/vfs';
import request from '../../api/client';
const MonacoEditor = React.lazy(() => import('@monaco-editor/react'));
const MarkdownEditor = React.lazy(() => import('@uiw/react-md-editor'));
const { Header, Content } = Layout;
const MAX_PREVIEW_BYTES = 1024 * 1024; // 1MB
export const TextEditorApp: React.FC<AppComponentProps> = ({ filePath, entry, onRequestClose }) => {
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [content, setContent] = useState('');
const [initialContent, setInitialContent] = useState('');
const [truncated, setTruncated] = useState(false);
const isDirty = content !== initialContent;
const onRequestCloseRef = useRef(onRequestClose);
onRequestCloseRef.current = onRequestClose;
const ext = useMemo(() => entry.name.split('.').pop()?.toLowerCase() || '', [entry.name]);
const isMarkdown = ext === 'md' || ext === 'markdown';
const monacoLanguage = useMemo(() => {
switch (ext) {
// Web technologies
case 'js':
case 'jsx':
return 'javascript';
case 'ts':
case 'tsx':
return 'typescript';
case 'html':
case 'htm':
return 'html';
case 'css':
return 'css';
case 'scss':
case 'sass':
return 'scss';
case 'less':
return 'less';
case 'vue':
return 'html'; // Vue files are primarily HTML with some JS/TS
// Data formats
case 'json':
return 'json';
case 'yaml':
case 'yml':
return 'yaml';
case 'xml':
return 'xml';
case 'toml':
return 'ini'; // TOML is similar to INI
case 'ini':
case 'cfg':
case 'conf':
return 'ini';
// Programming languages
case 'py':
return 'python';
case 'java':
return 'java';
case 'c':
return 'c';
case 'cpp':
case 'cc':
case 'cxx':
return 'cpp';
case 'h':
case 'hpp':
case 'hxx':
return 'cpp'; // Header files use C++ highlighting
case 'php':
return 'php';
case 'rb':
return 'ruby';
case 'go':
return 'go';
case 'rs':
return 'rust';
case 'swift':
return 'swift';
case 'kt':
return 'kotlin';
case 'scala':
return 'scala';
case 'cs':
return 'csharp';
case 'fs':
return 'fsharp';
case 'vb':
return 'vb';
case 'pl':
case 'pm':
return 'perl';
case 'r':
return 'r';
case 'lua':
return 'lua';
case 'dart':
return 'dart';
// Database
case 'sql':
return 'sql';
// Shell and scripts
case 'sh':
case 'bash':
case 'zsh':
case 'fish':
return 'shell';
case 'ps1':
return 'powershell';
case 'bat':
case 'cmd':
return 'bat';
// Build and config files
case 'dockerfile':
return 'dockerfile';
case 'makefile':
return 'makefile';
case 'gradle':
return 'groovy';
case 'cmake':
return 'cmake';
// Markdown
case 'md':
case 'markdown':
return 'markdown';
// Plain text and logs
case 'txt':
case 'log':
case 'gitignore':
case 'gitattributes':
case 'editorconfig':
case 'prettierrc':
default:
return 'plaintext';
}
}, [ext]);
useEffect(() => {
const loadFile = async () => {
try {
setLoading(true);
setTruncated(false);
const shouldTruncate = (entry.size ?? 0) > MAX_PREVIEW_BYTES;
if (shouldTruncate) {
const enc = encodeURI(filePath.replace(/^\/+/, ''));
const resp = await request(`/fs/file/${enc}`, {
method: 'GET',
headers: { Range: `bytes=0-${MAX_PREVIEW_BYTES - 1}` },
rawResponse: true,
});
const buf = await (resp as Response).arrayBuffer();
const text = new TextDecoder().decode(buf);
setContent(text);
setInitialContent(text);
setTruncated(true);
} else {
const data = await vfsApi.readFile(filePath);
const text = typeof data === 'string' ? data : new TextDecoder().decode(data);
setContent(text);
setInitialContent(text);
}
} catch (error) {
message.error(`加载文件失败: ${error instanceof Error ? error.message : '未知错误'}`);
onRequestCloseRef.current();
} finally {
setLoading(false);
}
};
loadFile();
}, [filePath, entry.size]);
const handleSave = useCallback(async () => {
if (truncated) {
message.warning('大文件仅预览前 1MB已禁用保存');
return;
}
if (!isDirty) return;
try {
setSaving(true);
const blob = new Blob([content], { type: 'text/plain' });
await vfsApi.uploadFile(filePath, blob);
setInitialContent(content);
message.success('保存成功');
} catch (error) {
message.error(`保存文件失败: ${error instanceof Error ? error.message : '未知错误'}`);
} finally {
setSaving(false);
}
}, [content, filePath, isDirty, truncated]);
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if ((event.ctrlKey || event.metaKey) && event.key === 's') {
event.preventDefault();
handleSave();
}
};
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [handleSave]);
return (
<Layout style={{ height: '100%', background: 'var(--ant-color-bg-container, #ffffff)' }}>
<Header
style={{
background: 'var(--ant-color-bg-layout, #f0f2f5)',
padding: '0 16px',
height: 40,
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
borderBottom: '1px solid var(--ant-color-border-secondary, #d9d9d9)'
}}
>
<span style={{ color: 'var(--ant-color-text, rgba(0,0,0,0.88))' }}>
{entry.name} {isDirty && '*'} {truncated && '(大文件仅预览前 1MB编辑与保存已禁用'}
</span>
<Space>
<Button type="primary" size="small" onClick={handleSave} loading={saving} disabled={!isDirty || truncated}>
</Button>
</Space>
</Header>
<Content style={{ position: 'relative', overflow: 'auto', height: 'calc(100% - 40px)' }}>
{loading ? (
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100%' }}>
<Spin />
</div>
) : (
isMarkdown ? (
<Suspense fallback={<Spin style={{ marginTop: 24 }} />}>
<MarkdownEditor
value={content}
onChange={(val) => setContent(val || '')}
height="100%"
preview={truncated ? 'preview' : 'live'}
/>
</Suspense>
) : (
<Suspense fallback={<Spin style={{ marginTop: 24 }} />}>
<MonacoEditor
value={content}
onChange={(val) => setContent(val || '')}
height="100%"
language={monacoLanguage}
options={{
readOnly: truncated,
minimap: { enabled: false },
scrollBeyondLastLine: false,
wordWrap: 'on',
fontSize: 13,
}}
/>
</Suspense>
)
)}
</Content>
</Layout>
);
};

View File

@@ -1,41 +0,0 @@
import type { AppDescriptor } from '../types';
import { TextEditorApp } from './TextEditor.tsx';
const supportedExts = [
// Text formats
'txt', 'md', 'markdown', 'log',
// Data formats
'json', 'yaml', 'yml', 'xml', 'toml', 'ini', 'cfg', 'conf',
// Web technologies
'html', 'htm', 'css', 'scss', 'sass', 'less', 'js', 'jsx', 'ts', 'tsx', 'vue',
// Programming languages
'py', 'java', 'c', 'cpp', 'cc', 'cxx', 'h', 'hpp', 'hxx',
'php', 'rb', 'go', 'rs', 'swift', 'kt', 'scala', 'clj', 'cljs',
'cs', 'vb', 'fs', 'pl', 'pm', 'r', 'lua', 'dart', 'elm',
// Database
'sql',
// Shell and scripts
'sh', 'bash', 'zsh', 'fish', 'ps1', 'bat', 'cmd',
// Build and config files
'dockerfile', 'makefile', 'gradle', 'cmake',
// Other common text files
'gitignore', 'gitattributes', 'editorconfig', 'prettierrc'
];
export const descriptor: AppDescriptor = {
key: 'text-editor',
name: '文本编辑器',
iconUrl: 'https://api.iconify.design/mdi:file-document-outline.svg',
description: '内置文本/代码编辑器,支持常见文本与代码格式。',
author: 'Foxel',
supportedExts,
supported: (entry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
// Supports common text and code formats
return supportedExts.includes(ext);
},
component: TextEditorApp,
default: true,
defaultBounds: { width: 1024, height: 768, x: 120, y: 80 }
};

View File

@@ -1,708 +0,0 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import {
Avatar,
Button,
Card,
Collapse,
Descriptions,
Divider,
Drawer,
Empty,
Flex,
Image,
Input,
List,
Segmented,
Skeleton,
Space,
Tabs,
Tag,
Typography,
message,
theme,
} from 'antd';
import { PlayCircleOutlined, ReloadOutlined, SearchOutlined, VideoCameraOutlined } from '@ant-design/icons';
import type { AppOpenComponentProps } from '../types';
import { videoLibraryApi, type VideoLibraryItem } from '../../api/videoLibrary';
import { useI18n } from '../../i18n';
import { ensureAppsLoaded, getAppByKey } from '../registry';
import { useAppWindows } from '../../contexts/AppWindowsContext';
import type { VfsEntry } from '../../api/client';
type LibraryFilter = 'all' | 'tv' | 'movie';
const TMDB_IMAGE_BASE = 'https://image.tmdb.org/t/p/';
function tmdbImage(path: string | null | undefined, size: string) {
if (!path) return undefined;
return `${TMDB_IMAGE_BASE}${size}${path}`;
}
function splitAbsolutePath(fullPath: string): { dir: string; name: string } | null {
const normalized = fullPath.replace(/\/+$/, '');
const idx = normalized.lastIndexOf('/');
if (idx < 0) return null;
const dir = idx === 0 ? '/' : normalized.slice(0, idx);
const name = normalized.slice(idx + 1);
if (!name) return null;
return { dir, name };
}
export const VideoLibraryApp: React.FC<AppOpenComponentProps> = () => {
const { token } = theme.useToken();
const { t } = useI18n();
const { openWithApp } = useAppWindows();
const [q, setQ] = useState('');
const [filter, setFilter] = useState<LibraryFilter>('all');
const [items, setItems] = useState<VideoLibraryItem[]>([]);
const [loading, setLoading] = useState(false);
const [detailOpen, setDetailOpen] = useState(false);
const [selected, setSelected] = useState<VideoLibraryItem | null>(null);
const [detailLoading, setDetailLoading] = useState(false);
const [detail, setDetail] = useState<any | null>(null);
const loadLibrary = useCallback(async () => {
setLoading(true);
try {
const list = await videoLibraryApi.list();
setItems(list);
} catch (err: any) {
setItems([]);
const msg = err instanceof Error ? err.message : t('Load failed');
message.error(msg);
} finally {
setLoading(false);
}
}, [t]);
useEffect(() => {
loadLibrary();
}, [loadLibrary]);
const stats = useMemo(() => {
let tv = 0;
let movie = 0;
items.forEach((it) => {
if (it.type === 'tv') tv += 1;
if (it.type === 'movie') movie += 1;
});
return { total: items.length, tv, movie };
}, [items]);
const filtered = useMemo(() => {
const s = q.trim().toLowerCase();
return items.filter((v) => {
if (filter !== 'all' && v.type !== filter) return false;
if (!s) return true;
const haystack = `${v.title || ''} ${v.overview || ''} ${(v.genres || []).join(' ')}`.toLowerCase();
return haystack.includes(s);
});
}, [filter, items, q]);
const playByPath = useCallback(async (fullPath: string) => {
const splitted = splitAbsolutePath(fullPath);
if (!splitted) return;
await ensureAppsLoaded();
const app = getAppByKey('video-player');
if (!app) {
message.error(t('App "{key}" not found.', { key: 'video-player' }));
return;
}
const entry: VfsEntry = { name: splitted.name, is_dir: false, size: 0, mtime: 0 };
openWithApp(entry, app, splitted.dir);
}, [openWithApp, t]);
const fetchDetail = useCallback(async (item: VideoLibraryItem) => {
setDetailLoading(true);
setDetail(null);
try {
const payload = await videoLibraryApi.get(item.id);
setDetail(payload);
} catch (err: any) {
setDetail(null);
const msg = err instanceof Error ? err.message : t('Load failed');
message.error(msg);
} finally {
setDetailLoading(false);
}
}, [t]);
const openDetail = (item: VideoLibraryItem) => {
setSelected(item);
setDetailOpen(true);
fetchDetail(item);
};
const closeDetail = () => {
setDetailOpen(false);
setSelected(null);
setDetail(null);
};
const renderCover = (item: VideoLibraryItem) => {
const label = item.type === 'tv' ? 'TV' : 'Movie';
const coverUrl = tmdbImage(item.poster_path, 'w342') || tmdbImage(item.backdrop_path, 'w780');
return (
<div
style={{
position: 'relative',
width: '100%',
aspectRatio: '2 / 3',
background: 'linear-gradient(135deg, #0b1020 0%, #22314a 55%, #2b3b5c 100%)',
overflow: 'hidden',
}}
>
{coverUrl ? (
<Image
src={coverUrl}
alt={item.title || label}
preview={false}
wrapperStyle={{ width: '100%', height: '100%', display: 'block' }}
style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }}
fallback="/logo.svg"
/>
) : (
<div
style={{
position: 'absolute',
inset: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'rgba(255,255,255,0.7)',
}}
>
<VideoCameraOutlined style={{ fontSize: 28 }} />
</div>
)}
<div
style={{
position: 'absolute',
inset: 0,
background: 'linear-gradient(180deg, rgba(0,0,0,0.48) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0.45) 100%)',
pointerEvents: 'none',
}}
/>
<div
style={{
position: 'absolute',
top: 10,
left: 10,
right: 10,
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
gap: 8,
pointerEvents: 'none',
}}
>
<Tag color={item.type === 'tv' ? 'geekblue' : 'gold'} style={{ marginInlineEnd: 0 }}>
{label}
</Tag>
<VideoCameraOutlined style={{ fontSize: 18, opacity: 0.9, color: 'rgba(255,255,255,0.9)' }} />
</div>
<div
style={{
position: 'absolute',
left: 10,
bottom: 10,
width: 36,
height: 36,
borderRadius: 999,
background: 'rgba(2,6,23,0.55)',
border: '1px solid rgba(255,255,255,0.22)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'rgba(255,255,255,0.92)',
boxShadow: token.boxShadowTertiary,
pointerEvents: 'none',
}}
>
<PlayCircleOutlined style={{ fontSize: 20 }} />
</div>
</div>
);
};
const detailTitle = useMemo(() => {
const d = detail?.tmdb?.detail;
if (!d) return selected?.title || '';
if (detail?.type === 'tv') return d.name || d.original_name || selected?.title || '';
return d.title || d.original_title || selected?.title || '';
}, [detail, selected?.title]);
const detailPosterUrl = useMemo(() => tmdbImage(detail?.tmdb?.detail?.poster_path, 'w342'), [detail]);
const detailBackdropUrl = useMemo(() => tmdbImage(detail?.tmdb?.detail?.backdrop_path, 'w1280'), [detail]);
const detailGenres = useMemo(() => {
const genres = detail?.tmdb?.detail?.genres;
if (!Array.isArray(genres)) return [];
return genres.map((g: any) => g?.name).filter(Boolean);
}, [detail]);
const detailOverview = useMemo(() => {
const overview = detail?.tmdb?.detail?.overview;
if (!overview) return '';
return String(overview);
}, [detail]);
const castTop = useMemo(() => {
const cast = detail?.tmdb?.detail?.credits?.cast;
if (!Array.isArray(cast)) return [];
return cast.slice(0, 12);
}, [detail]);
const episodesBySeason = useMemo(() => {
if (detail?.type !== 'tv') return [];
const episodes = Array.isArray(detail?.episodes) ? detail.episodes : [];
const map = new Map<number, any[]>();
episodes.forEach((ep: any) => {
const season = typeof ep?.season === 'number' ? ep.season : 1;
if (!map.has(season)) map.set(season, []);
map.get(season)!.push(ep);
});
const seasons = Array.from(map.keys()).sort((a, b) => a - b);
return seasons.map((season) => {
const list = (map.get(season) || []).slice().sort((a, b) => {
const ae = typeof a?.episode === 'number' ? a.episode : 10_000;
const be = typeof b?.episode === 'number' ? b.episode : 10_000;
return ae - be;
});
return { season, episodes: list };
});
}, [detail]);
const renderHero = () => {
const year = detail?.type === 'tv'
? (detail?.tmdb?.detail?.first_air_date || '').slice(0, 4)
: (detail?.tmdb?.detail?.release_date || '').slice(0, 4);
const vote = detail?.tmdb?.detail?.vote_average;
const voteText = typeof vote === 'number' ? vote.toFixed(1) : '--';
return (
<div
style={{
position: 'relative',
padding: 16,
minHeight: 220,
background: detailBackdropUrl
? `url(${detailBackdropUrl}) center / cover no-repeat`
: 'linear-gradient(135deg, #0b1020 0%, #22314a 55%, #2b3b5c 100%)',
overflow: 'hidden',
}}
>
<div
style={{
position: 'absolute',
inset: 0,
background: 'linear-gradient(90deg, rgba(2,6,23,0.92) 0%, rgba(2,6,23,0.55) 55%, rgba(2,6,23,0.15) 100%)',
}}
/>
<div style={{ position: 'relative', display: 'flex', gap: 16, alignItems: 'flex-end' }}>
<div style={{ width: 132, flex: 'none' }}>
<div style={{ borderRadius: 12, overflow: 'hidden', boxShadow: token.boxShadowTertiary, border: '1px solid rgba(255,255,255,0.18)' }}>
{detailPosterUrl ? (
<Image
src={detailPosterUrl}
alt={detailTitle}
preview={false}
width={132}
height={198}
style={{ objectFit: 'cover', display: 'block' }}
fallback="/logo.svg"
/>
) : (
<div
style={{
width: 132,
height: 198,
background: 'rgba(255,255,255,0.08)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'rgba(255,255,255,0.75)',
}}
>
<VideoCameraOutlined style={{ fontSize: 28 }} />
</div>
)}
</div>
</div>
<div style={{ minWidth: 0, flex: 1, color: 'rgba(255,255,255,0.92)' }}>
<Typography.Title level={3} style={{ margin: 0, color: 'rgba(255,255,255,0.92)' }} ellipsis>
{detailTitle}
</Typography.Title>
<div style={{ marginTop: 6, display: 'flex', flexWrap: 'wrap', gap: 10, alignItems: 'center', color: 'rgba(255,255,255,0.72)' }}>
{year && <span>{year}</span>}
<span>·</span>
<span>TMDB {voteText}</span>
{detail?.type === 'tv' && (
<>
<span>·</span>
<span>{episodesBySeason.reduce((acc, s) => acc + s.episodes.length, 0)} {t('Episodes')}</span>
</>
)}
</div>
<div style={{ marginTop: 10, display: 'flex', flexWrap: 'wrap', gap: 6 }}>
{(detailGenres || []).slice(0, 6).map((g: string) => (
<Tag key={g} color="geekblue" style={{ marginInlineEnd: 0 }}>
{g}
</Tag>
))}
</div>
{detail?.type === 'movie' && (
<div style={{ marginTop: 14 }}>
<Button
type="primary"
icon={<PlayCircleOutlined />}
onClick={() => playByPath(String(detail?.source_path || ''))}
disabled={!detail?.source_path}
>
{t('Play')}
</Button>
</div>
)}
</div>
</div>
</div>
);
};
const renderMeta = () => {
if (!detail?.tmdb?.detail) return null;
const d = detail.tmdb.detail;
const isTv = detail?.type === 'tv';
const release = isTv ? d.first_air_date : d.release_date;
const runtime = isTv ? (Array.isArray(d.episode_run_time) ? d.episode_run_time[0] : undefined) : d.runtime;
const status = d.status;
const language = d.original_language;
const origin = isTv ? d.original_name : d.original_title;
const seasons = isTv ? d.number_of_seasons : undefined;
const eps = isTv ? d.number_of_episodes : undefined;
return (
<Descriptions
size="small"
column={2}
styles={{ label: { width: 110, color: token.colorTextSecondary } }}
style={{ marginTop: 10 }}
>
<Descriptions.Item label={t('Type')}>{isTv ? t('TV') : t('Movies')}</Descriptions.Item>
<Descriptions.Item label="TMDB ID">{String(detail?.tmdb?.id || '')}</Descriptions.Item>
{origin && <Descriptions.Item label={t('Original Title')}>{String(origin)}</Descriptions.Item>}
{release && <Descriptions.Item label={t('Release Date')}>{String(release)}</Descriptions.Item>}
{status && <Descriptions.Item label={t('Status')}>{String(status)}</Descriptions.Item>}
{runtime && <Descriptions.Item label={t('Runtime')}>{String(runtime)} min</Descriptions.Item>}
{language && <Descriptions.Item label={t('Language')}>{String(language).toUpperCase()}</Descriptions.Item>}
{isTv && seasons !== undefined && <Descriptions.Item label={t('Seasons')}>{String(seasons)}</Descriptions.Item>}
{isTv && eps !== undefined && <Descriptions.Item label={t('Episodes')}>{String(eps)}</Descriptions.Item>}
{detail?.source_path && <Descriptions.Item label={t('Source Path')} span={2}>{String(detail.source_path)}</Descriptions.Item>}
</Descriptions>
);
};
const renderCast = () => {
if (!castTop.length) return null;
return (
<div style={{ marginTop: 14 }}>
<Typography.Title level={5} style={{ margin: 0 }}>
{t('Cast')}
</Typography.Title>
<div style={{ marginTop: 10, display: 'flex', flexWrap: 'wrap', gap: 10 }}>
{castTop.map((c: any) => {
const avatar = tmdbImage(c?.profile_path, 'w185');
return (
<div
key={String(c?.id || `${c?.name}-${c?.character}`)}
style={{
display: 'flex',
alignItems: 'center',
gap: 10,
padding: '8px 10px',
borderRadius: 12,
background: token.colorFillQuaternary,
border: `1px solid ${token.colorBorderSecondary}`,
minWidth: 200,
}}
>
<Avatar size={36} src={avatar} style={{ flex: 'none' }}>
{(c?.name || '?').slice(0, 1)}
</Avatar>
<div style={{ minWidth: 0 }}>
<Typography.Text strong ellipsis style={{ display: 'block', maxWidth: 140 }}>
{c?.name || '--'}
</Typography.Text>
<Typography.Text type="secondary" style={{ fontSize: 12 }} ellipsis>
{c?.character || ''}
</Typography.Text>
</div>
</div>
);
})}
</div>
</div>
);
};
const renderEpisodes = () => {
if (detail?.type !== 'tv') return null;
if (!episodesBySeason.length) {
return (
<Empty description={t('No data')} style={{ marginTop: 24 }} />
);
}
return (
<Collapse
accordion={false}
items={episodesBySeason.map(({ season, episodes }) => ({
key: String(season),
label: `${t('Season')} ${season} · ${episodes.length} ${t('Episodes')}`,
children: (
<List
itemLayout="horizontal"
dataSource={episodes}
renderItem={(ep: any) => {
const seasonNo = typeof ep?.season === 'number' ? ep.season : season;
const epNo = typeof ep?.episode === 'number' ? ep.episode : undefined;
const tmdbEp = ep?.tmdb_episode || {};
const still = tmdbImage(tmdbEp?.still_path, 'w300');
const title = tmdbEp?.name || ep?.name || ep?.rel || '--';
const air = tmdbEp?.air_date ? String(tmdbEp.air_date) : '';
const runtime = tmdbEp?.runtime ? `${tmdbEp.runtime} min` : '';
const sub = [air, runtime].filter(Boolean).join(' · ');
const prefix = epNo !== undefined ? `S${String(seasonNo).padStart(2, '0')}E${String(epNo).padStart(2, '0')}` : `S${String(seasonNo).padStart(2, '0')}`;
return (
<List.Item
style={{ paddingInline: 0 }}
actions={[
<Button
key="play"
type="primary"
icon={<PlayCircleOutlined />}
onClick={() => playByPath(String(ep?.path || ''))}
disabled={!ep?.path}
>
{t('Play')}
</Button>,
]}
>
<List.Item.Meta
avatar={still ? (
<Image
src={still}
preview={false}
width={120}
height={68}
style={{ objectFit: 'cover', borderRadius: 10, overflow: 'hidden' }}
fallback="/logo.svg"
/>
) : (
<div
style={{
width: 120,
height: 68,
borderRadius: 10,
background: token.colorFillQuaternary,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: token.colorTextTertiary,
}}
>
<VideoCameraOutlined />
</div>
)}
title={(
<div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
<Tag style={{ marginInlineEnd: 0 }}>{prefix}</Tag>
<Typography.Text strong ellipsis style={{ display: 'block', maxWidth: 360 }}>
{title}
</Typography.Text>
</div>
)}
description={sub ? <Typography.Text type="secondary">{sub}</Typography.Text> : null}
/>
</List.Item>
);
}}
/>
),
}))}
/>
);
};
return (
<div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: token.colorBgLayout }}>
<div
style={{
padding: 14,
borderRadius: 12,
background: token.colorBgContainer,
border: `1px solid ${token.colorBorderSecondary}`,
}}
>
<Flex align="center" justify="space-between" gap={12} wrap>
<div style={{ minWidth: 240 }}>
<Typography.Title level={4} style={{ margin: 0 }}>
{t('Video Library')}
</Typography.Title>
<Typography.Text type="secondary">
{t('Total')}: {stats.total} · {t('Movies')}: {stats.movie} · {t('TV')}: {stats.tv}
</Typography.Text>
</div>
<Space size={10} wrap>
<Segmented
value={filter}
onChange={(v) => setFilter(v as LibraryFilter)}
options={[
{ value: 'all', label: t('All') },
{ value: 'movie', label: t('Movies') },
{ value: 'tv', label: t('TV') },
]}
/>
<Input
allowClear
value={q}
onChange={(e) => setQ(e.target.value)}
prefix={<SearchOutlined />}
placeholder={t('Search')}
style={{ width: 260 }}
/>
<Button icon={<ReloadOutlined />} onClick={loadLibrary} loading={loading}>
{t('Refresh')}
</Button>
</Space>
</Flex>
</div>
<div style={{ flex: 1, minHeight: 0, overflow: 'auto', padding: '12px 2px' }}>
{loading ? (
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(210px, 1fr))', gap: 12 }}>
{Array.from({ length: 10 }).map((_, idx) => (
<Card
key={idx}
size="small"
style={{ borderRadius: 12, overflow: 'hidden' }}
cover={(
<div style={{ width: '100%', aspectRatio: '2 / 3' }}>
<Skeleton.Image active style={{ width: '100%', height: '100%' }} />
</div>
)}
>
<Skeleton active paragraph={{ rows: 2 }} />
</Card>
))}
</div>
) : filtered.length === 0 ? (
<Empty description={t('No data')} style={{ marginTop: 48 }} />
) : (
<div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(210px, 1fr))', gap: 12 }}>
{filtered.map((v) => (
<Card
key={v.id}
hoverable
size="small"
styles={{ body: { padding: 10 } } as any}
style={{
borderRadius: 12,
overflow: 'hidden',
boxShadow: token.boxShadowTertiary,
border: `1px solid ${token.colorBorderSecondary}`,
}}
cover={renderCover(v)}
onClick={() => openDetail(v)}
>
<Typography.Text strong ellipsis style={{ display: 'block' }}>
{v.title || '--'}
</Typography.Text>
<div style={{ marginTop: 6, display: 'flex', gap: 8, color: token.colorTextTertiary, fontSize: 12 }}>
<span>{v.year || '--'}</span>
{v.type === 'tv' ? (
<>
<span>·</span>
<span>{v.episodes_count || 0} {t('Episodes')}</span>
</>
) : null}
</div>
<div style={{ marginTop: 8, display: 'flex', flexWrap: 'wrap', gap: 6 }}>
{(v.genres || []).slice(0, 3).map((tag) => (
<Tag key={tag} style={{ marginInlineEnd: 0 }}>
{tag}
</Tag>
))}
{(v.genres || []).length > 3 && (
<Tag style={{ marginInlineEnd: 0 }}>+{(v.genres || []).length - 3}</Tag>
)}
</div>
</Card>
))}
</div>
)}
</div>
<Drawer
title={detailTitle || selected?.title || t('Details')}
open={detailOpen}
onClose={closeDetail}
width="100%"
destroyOnHidden
getContainer={false}
styles={{ body: { padding: 0 } }}
>
{detailLoading ? (
<div style={{ padding: 16 }}>
<Skeleton active paragraph={{ rows: 10 }} />
</div>
) : !detail ? (
<Empty description={t('No data')} style={{ marginTop: 48 }} />
) : (
<div style={{ display: 'flex', flexDirection: 'column' }}>
{renderHero()}
<div style={{ padding: 16 }}>
<Tabs
items={[
...(detail?.type === 'tv'
? [{
key: 'episodes',
label: t('Episodes'),
children: renderEpisodes(),
}]
: []),
{
key: 'detail',
label: t('Details'),
children: (
<>
{renderMeta()}
{detailOverview && (
<>
<Divider style={{ margin: '14px 0' }} />
<Typography.Title level={5} style={{ margin: 0 }}>
{t('Overview')}
</Typography.Title>
<Typography.Paragraph style={{ marginTop: 8, whiteSpace: 'pre-wrap' }}>
{detailOverview}
</Typography.Paragraph>
</>
)}
{renderCast()}
</>
),
},
]}
/>
</div>
</div>
)}
</Drawer>
</div>
);
};

View File

@@ -1,46 +0,0 @@
import React, { useEffect, useRef } from 'react';
import Artplayer from 'artplayer';
import { vfsApi } from '../../api/client';
import type { AppComponentProps } from '../types';
export const VideoPlayerApp: React.FC<AppComponentProps> = ({ filePath }) => {
const artRef = useRef<HTMLDivElement | null>(null);
const artInstance = useRef<Artplayer | null>(null);
useEffect(() => {
//
const safePath = filePath.replace(/^\/+/, '').split('#').map((seg, idx) => idx === 0 ? seg : encodeURIComponent('#') + seg).join('');
const videoUrl = vfsApi.streamUrl(safePath);
if (artRef.current) {
artInstance.current = new Artplayer({
container: artRef.current,
url: videoUrl,
autoplay: true,
fullscreen: true,
fullscreenWeb: true,
pip: true,
setting: true,
playbackRate: true,
});
}
return () => {
if (artInstance.current) {
artInstance.current.destroy();
}
};
}, [filePath]);
return (
<div
ref={artRef}
style={{
width: '100%',
height: '100%',
backgroundColor: '#000'
}}
/>
);
};

View File

@@ -1,23 +0,0 @@
import type { AppDescriptor } from '../types';
import { VideoPlayerApp } from './VideoPlayer.tsx';
import { VideoLibraryApp } from './VideoLibrary.tsx';
const supportedExts = ['mp4','webm','ogg','m4v','mov','mkv','avi','wmv','flv','3gp'];
export const descriptor: AppDescriptor = {
key: 'video-player',
name: '视频播放器',
iconUrl: 'https://api.iconify.design/mdi:video.svg',
description: '内置视频播放器,支持常见视频格式播放。',
author: 'Foxel',
openAppComponent: VideoLibraryApp,
supportedExts,
supported: (entry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
return supportedExts.includes(ext);
},
component: VideoPlayerApp,
default: true,
defaultBounds: { width: 960, height: 600, x: 180, y: 120 }
};

View File

@@ -3,76 +3,112 @@ import type { AppDescriptor } from './types';
import React from 'react';
import { pluginsApi, type PluginItem } from '../api/plugins';
import { PluginAppHost, PluginAppOpenHost } from './PluginHost';
import { getPluginAssetUrl } from '../plugins/runtime';
const apps: AppDescriptor[] = [];
// 使用 import.meta.glob 动态导入所有应用
const appModules = import.meta.glob('./*/index.ts');
/**
* 获取插件的唯一 key
*/
function getPluginAppKey(p: PluginItem): string {
return `plugin:${p.key}`;
}
async function loadApps() {
for (const path in appModules) {
const module = await appModules[path]();
if (module && typeof module === 'object' && 'descriptor' in module) {
const descriptor = (module as { descriptor: AppDescriptor }).descriptor;
if (!apps.find(a => a.key === descriptor.key)) {
apps.push(descriptor);
}
}
/**
* 解析插件图标 URL
* 支持绝对路径、相对路径(插件资源)、外部 URL
*/
function resolvePluginIcon(p: PluginItem): string | undefined {
if (!p.icon) return undefined;
// 外部 URL
if (p.icon.startsWith('http://') || p.icon.startsWith('https://')) {
return p.icon;
}
try {
const items = await pluginsApi.list();
items.filter(p => p.enabled !== false).forEach((p) => registerPluginAsApp(p));
} catch { void 0; }
// 绝对路径
if (p.icon.startsWith('/')) {
return p.icon;
}
// 插件资源路径
return getPluginAssetUrl(p.key, p.icon);
}
function resolvePluginUseSystemWindow(p: PluginItem): boolean | undefined {
const frontend = (p.manifest as any)?.frontend as any;
const value = frontend?.use_system_window ?? frontend?.useSystemWindow;
return typeof value === 'boolean' ? value : undefined;
}
function registerPluginAsApp(p: PluginItem) {
const key = 'plugin:' + p.id;
if (apps.find(a => a.key === key)) return;
const key = getPluginAppKey(p);
if (apps.find((a) => a.key === key)) return;
const supported = (entry: VfsEntry) => {
if (entry.is_dir) return false;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
if (!p.supported_exts || p.supported_exts.length === 0) return true;
return p.supported_exts.includes(ext);
};
apps.push({
key,
name: p.name || `插件 ${p.id}`,
name: p.name || `插件 ${p.key}`,
supported,
component: (props: any) => React.createElement(PluginAppHost, { plugin: p, ...props }),
openAppComponent: p.open_app ? ((props: any) => React.createElement(PluginAppOpenHost, { plugin: p, ...props })) : undefined,
iconUrl: p.icon || undefined,
openAppComponent: p.open_app
? (props: any) => React.createElement(PluginAppOpenHost, { plugin: p, ...props })
: undefined,
iconUrl: resolvePluginIcon(p),
default: false,
defaultBounds: p.default_bounds || undefined,
defaultMaximized: p.default_maximized || undefined,
useSystemWindow: resolvePluginUseSystemWindow(p),
description: p.description || undefined,
author: p.author || undefined,
supportedExts: p.supported_exts || undefined,
website: p.website || undefined,
github: p.github || undefined,
});
}
async function loadApps() {
try {
const items = await pluginsApi.list();
items.forEach((p) => registerPluginAsApp(p));
} catch {
void 0;
}
}
const appsLoadedPromise = loadApps();
export async function ensureAppsLoaded() {
await appsLoadedPromise;
}
export function listSystemApps(): AppDescriptor[] {
return apps.filter(a => !a.key.startsWith('plugin:'));
export function listPluginApps(): AppDescriptor[] {
return apps;
}
export function getAppsForEntry(entry: VfsEntry): AppDescriptor[] {
return apps.filter(a => a.supported(entry));
return apps.filter((a) => a.supported(entry));
}
export function getAppByKey(key: string): AppDescriptor | undefined {
return apps.find(a => a.key === key);
return apps.find((a) => a.key === key);
}
export function getDefaultAppForEntry(entry: VfsEntry): AppDescriptor | undefined {
if (entry.is_dir) return;
const ext = entry.name.split('.').pop()?.toLowerCase() || '';
if (!ext) return apps.find(a => a.supported(entry) && a.default);
if (!ext) return apps.find((a) => a.supported(entry) && a.default);
const saved = localStorage.getItem(`app.default.${ext}`);
if (saved) {
return apps.find(a => a.key === saved && a.supported(entry)) || undefined;
return apps.find((a) => a.key === saved && a.supported(entry)) || undefined;
}
return apps.find(a => a.supported(entry) && a.default);
return apps.find((a) => a.supported(entry) && a.default);
}
export type { AppDescriptor };
@@ -81,27 +117,40 @@ export type { AppComponentProps } from './types';
export async function reloadPluginApps() {
try {
const items = await pluginsApi.list();
const keepKeys = new Set(items.filter(p => p.enabled !== false).map(p => 'plugin:' + p.id));
// 生成要保留的 key 集合
const keepKeys = new Set(items.map((p) => getPluginAppKey(p)));
// 移除已卸载的插件应用
for (let i = apps.length - 1; i >= 0; i--) {
const a = apps[i];
if (a.key.startsWith('plugin:') && !keepKeys.has(a.key)) {
if (!keepKeys.has(a.key)) {
apps.splice(i, 1);
}
}
items.filter(p => p.enabled !== false).forEach(p => {
const key = 'plugin:' + p.id;
const existing = apps.find(a => a.key === key);
// 更新或添加插件应用
items.forEach((p) => {
const key = getPluginAppKey(p);
const existing = apps.find((a) => a.key === key);
if (!existing) {
registerPluginAsApp(p);
} else {
existing.name = p.name || `插件 ${p.id}`;
// 更新现有应用信息
existing.name = p.name || `插件 ${p.key}`;
existing.defaultBounds = p.default_bounds || undefined;
existing.defaultMaximized = p.default_maximized || undefined;
existing.iconUrl = p.icon || existing.iconUrl;
existing.useSystemWindow = resolvePluginUseSystemWindow(p);
existing.iconUrl = resolvePluginIcon(p);
existing.description = p.description || undefined;
existing.author = p.author || undefined;
existing.supportedExts = p.supported_exts || undefined;
existing.openAppComponent = p.open_app
? ((props: any) => React.createElement(PluginAppOpenHost, { plugin: p, ...props }))
? (props: any) => React.createElement(PluginAppOpenHost, { plugin: p, ...props })
: undefined;
}
});
} catch { void 0; }
} catch {
void 0;
}
}