2024-03-13 22:37:18 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-02-02 15:05:18 +09:00
|
|
|
import { getHighlighterCore, loadWasm } from 'shiki/core';
|
|
|
|
import darkPlus from 'shiki/themes/dark-plus.mjs';
|
2024-03-20 10:30:45 +09:00
|
|
|
import { bundledThemesInfo } from 'shiki/themes';
|
|
|
|
import { bundledLanguagesInfo } from 'shiki/langs';
|
2024-02-06 15:03:07 +09:00
|
|
|
import { unique } from './array.js';
|
|
|
|
import { deepClone } from './clone.js';
|
|
|
|
import { deepMerge } from './merge.js';
|
2024-03-20 10:30:45 +09:00
|
|
|
import type { HighlighterCore, LanguageRegistration, ThemeRegistration, ThemeRegistrationRaw } from 'shiki/core';
|
2024-02-06 15:03:07 +09:00
|
|
|
import { ColdDeviceStorage } from '@/store.js';
|
|
|
|
import lightTheme from '@/themes/_light.json5';
|
|
|
|
import darkTheme from '@/themes/_dark.json5';
|
2023-10-29 14:12:40 +09:00
|
|
|
|
2024-03-20 10:30:45 +09:00
|
|
|
let _highlighter: HighlighterCore | null = null;
|
2023-10-29 14:12:40 +09:00
|
|
|
|
2024-02-06 15:03:07 +09:00
|
|
|
export async function getTheme(mode: 'light' | 'dark', getName: true): Promise<string>;
|
|
|
|
export async function getTheme(mode: 'light' | 'dark', getName?: false): Promise<ThemeRegistration | ThemeRegistrationRaw>;
|
|
|
|
export async function getTheme(mode: 'light' | 'dark', getName = false): Promise<ThemeRegistration | ThemeRegistrationRaw | string | null> {
|
|
|
|
const theme = deepClone(ColdDeviceStorage.get(mode === 'light' ? 'lightTheme' : 'darkTheme'));
|
|
|
|
|
|
|
|
if (theme.base) {
|
|
|
|
const base = [lightTheme, darkTheme].find(x => x.id === theme.base);
|
|
|
|
if (base && base.codeHighlighter) theme.codeHighlighter = Object.assign({}, base.codeHighlighter, theme.codeHighlighter);
|
|
|
|
}
|
2024-02-09 00:08:33 +09:00
|
|
|
|
2024-02-06 15:03:07 +09:00
|
|
|
if (theme.codeHighlighter) {
|
|
|
|
let _res: ThemeRegistration = {};
|
|
|
|
if (theme.codeHighlighter.base === '_none_') {
|
|
|
|
_res = deepClone(theme.codeHighlighter.overrides);
|
|
|
|
} else {
|
|
|
|
const base = await bundledThemesInfo.find(t => t.id === theme.codeHighlighter!.base)?.import() ?? darkPlus;
|
|
|
|
_res = deepMerge(theme.codeHighlighter.overrides ?? {}, 'default' in base ? base.default : base);
|
|
|
|
}
|
|
|
|
if (_res.name == null) {
|
|
|
|
_res.name = theme.id;
|
|
|
|
}
|
|
|
|
_res.type = mode;
|
|
|
|
|
|
|
|
if (getName) {
|
|
|
|
return _res.name;
|
|
|
|
}
|
|
|
|
return _res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getName) {
|
|
|
|
return 'dark-plus';
|
|
|
|
}
|
|
|
|
return darkPlus;
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:30:45 +09:00
|
|
|
export async function getHighlighter(): Promise<HighlighterCore> {
|
2023-10-29 14:12:40 +09:00
|
|
|
if (!_highlighter) {
|
|
|
|
return await initHighlighter();
|
|
|
|
}
|
|
|
|
return _highlighter;
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:30:45 +09:00
|
|
|
async function initHighlighter() {
|
2024-02-02 15:05:18 +09:00
|
|
|
await loadWasm(import('shiki/onig.wasm?init'));
|
2023-10-29 14:12:40 +09:00
|
|
|
|
2024-02-06 15:03:07 +09:00
|
|
|
// テーマの重複を消す
|
|
|
|
const themes = unique([
|
|
|
|
darkPlus,
|
|
|
|
...(await Promise.all([getTheme('light'), getTheme('dark')])),
|
|
|
|
]);
|
|
|
|
|
2024-03-20 10:30:45 +09:00
|
|
|
const jsLangInfo = bundledLanguagesInfo.find(t => t.id === 'javascript');
|
2024-02-02 15:05:18 +09:00
|
|
|
const highlighter = await getHighlighterCore({
|
2024-02-06 15:03:07 +09:00
|
|
|
themes,
|
2024-02-02 15:05:18 +09:00
|
|
|
langs: [
|
2024-03-20 10:30:45 +09:00
|
|
|
...(jsLangInfo ? [async () => await jsLangInfo.import()] : []),
|
|
|
|
async () => (await import('aiscript-vscode/aiscript/syntaxes/aiscript.tmLanguage.json')).default as unknown as LanguageRegistration,
|
2024-02-02 15:05:18 +09:00
|
|
|
],
|
2023-10-29 14:12:40 +09:00
|
|
|
});
|
|
|
|
|
2024-02-06 15:03:07 +09:00
|
|
|
ColdDeviceStorage.watch('lightTheme', async () => {
|
|
|
|
const newTheme = await getTheme('light');
|
|
|
|
if (newTheme.name && !highlighter.getLoadedThemes().includes(newTheme.name)) {
|
|
|
|
highlighter.loadTheme(newTheme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ColdDeviceStorage.watch('darkTheme', async () => {
|
|
|
|
const newTheme = await getTheme('dark');
|
|
|
|
if (newTheme.name && !highlighter.getLoadedThemes().includes(newTheme.name)) {
|
|
|
|
highlighter.loadTheme(newTheme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-10-29 14:12:40 +09:00
|
|
|
_highlighter = highlighter;
|
|
|
|
|
|
|
|
return highlighter;
|
|
|
|
}
|