fix(ShikiCodeblocks): spoilers (#298)

* fix(ShikiCodeblocks): spoilers

* fix a settings bug i thikn
This commit is contained in:
Justice Almanzar 2022-12-08 09:54:19 -05:00 committed by GitHub
parent 2d08dd8a9c
commit 2de461985d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View file

@ -46,9 +46,11 @@ export interface HighlighterProps {
} }
export const createHighlighter = (props: HighlighterProps) => ( export const createHighlighter = (props: HighlighterProps) => (
<ErrorBoundary> <pre className={cl("container")}>
<Highlighter {...props} /> <ErrorBoundary>
</ErrorBoundary> <Highlighter {...props} />
</ErrorBoundary>
</pre>
); );
export const Highlighter = ({ export const Highlighter = ({
lang, lang,
@ -66,7 +68,7 @@ export const Highlighter = ({
const shikiLang = lang ? resolveLang(lang) : null; const shikiLang = lang ? resolveLang(lang) : null;
const useHljs = shouldUseHljs({ lang, tryHljs }); const useHljs = shouldUseHljs({ lang, tryHljs });
const [preRef, isIntersecting] = useIntersection(true); const [rootRef, isIntersecting] = useIntersection(true);
const [tokens] = useAwaiter(async () => { const [tokens] = useAwaiter(async () => {
if (!shikiLang || useHljs || !isIntersecting) return null; if (!shikiLang || useHljs || !isIntersecting) return null;
@ -93,8 +95,8 @@ export const Highlighter = ({
if (isPreview) preClasses.push(cl("preview")); if (isPreview) preClasses.push(cl("preview"));
return ( return (
<pre <div
ref={preRef} ref={rootRef}
className={preClasses.join(" ")} className={preClasses.join(" ")}
style={{ style={{
backgroundColor: useHljs backgroundColor: useHljs
@ -123,7 +125,7 @@ export const Highlighter = ({
theme={themeBase} theme={themeBase}
/>} />}
</code> </code>
</pre> </div>
); );
}; };

View file

@ -17,17 +17,28 @@
*/ */
import { useSettings } from "@api/settings"; import { useSettings } from "@api/settings";
import { React } from "@webpack/common";
import { shiki } from "../api/shiki"; import { shiki } from "../api/shiki";
import { ShikiSettings } from "../types"; import { ShikiSettings } from "../types";
export function useShikiSettings(settingKeys: (keyof ShikiSettings)[], overrides?: Record<string, any>) { export function useShikiSettings(settingKeys: (keyof ShikiSettings)[], overrides?: Record<string, any>) {
const settings = useSettings(settingKeys.map(key => `plugins.ShikiCodeblocks.${key}`)).plugins.ShikiCodeblocks as ShikiSettings; const settings = useSettings(settingKeys.map(key => `plugins.ShikiCodeblocks.${key}`)).plugins.ShikiCodeblocks as ShikiSettings;
const [isLoading, setLoading] = React.useState(false);
const withOverrides = { ...settings, ...overrides }; const withOverrides = { ...settings, ...overrides };
const themeUrl = withOverrides.customTheme || withOverrides.theme; const themeUrl = withOverrides.customTheme || withOverrides.theme;
if (themeUrl !== shiki.currentThemeUrl) shiki.setTheme(themeUrl);
if (overrides) {
const willChangeTheme = shiki.currentThemeUrl && themeUrl !== shiki.currentThemeUrl;
const noOverrides = Object.keys(overrides).length === 0;
if (isLoading && (!willChangeTheme || noOverrides)) setLoading(false);
if ((!isLoading && willChangeTheme)) {
setLoading(true);
shiki.setTheme(themeUrl);
}
}
return { return {
...withOverrides, ...withOverrides,

View file

@ -1,5 +1,8 @@
.shiki-root { .shiki-root {
border-radius: 4px; border-radius: 4px;
/* fallback background */
background-color: var(--background-secondary);
} }
.shiki-root code { .shiki-root code {