文件
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

63 行
1.6 KiB
TypeScript

import { useEffect, useState } from 'react';
import { themeStyle } from '@joplin/lib/theme';
import shim from '@joplin/lib/shim';
import Setting from '@joplin/lib/models/Setting';
import { camelCaseToDash, formatCssSize } from '@joplin/lib/string-utils';
interface HookDependencies {
pluginId: string;
themeId: number;
}
function themeToCssVariables(theme: Record<string, unknown>) {
const lines = [];
lines.push(':root {');
for (const name in theme) {
const value = theme[name];
if (typeof value === 'object') continue;
if (['appearance', 'codeThemeCss', 'codeMirrorTheme'].indexOf(name) >= 0) continue;
const newName = `--joplin-${camelCaseToDash(name)}`;
const formattedValue = typeof value === 'number' && newName.indexOf('opacity') < 0 ? formatCssSize(value) : value;
lines.push(`\t${newName}: ${formattedValue};`);
}
lines.push('}');
return lines.join('\n');
}
export default function useThemeCss(dep: HookDependencies) {
const { pluginId, themeId } = dep;
const [cssFilePath, setCssFilePath] = useState('');
useEffect(() => {
let cancelled = false;
async function createThemeStyleSheet() {
const theme = themeStyle(themeId);
const css = themeToCssVariables(theme);
const filePath = `${Setting.value('tempDir')}/plugin_${pluginId}_theme_${themeId}.css`;
if (!(await shim.fsDriver().exists(filePath))) {
await shim.fsDriver().writeFile(filePath, css, 'utf8');
if (cancelled) return;
}
setCssFilePath(filePath);
}
void createThemeStyleSheet();
return () => {
cancelled = true;
};
}, [pluginId, themeId]);
return cssFilePath;
}