Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df323f8590 |
No files matched your search
@@ -16,7 +16,11 @@ import { useStore } from "../store";
|
|||||||
import { ToggleGroup, Toggle } from "./ui/toggle-group";
|
import { ToggleGroup, Toggle } from "./ui/toggle-group";
|
||||||
|
|
||||||
type DiffRenderMode = "stacked" | "split";
|
type DiffRenderMode = "stacked" | "split";
|
||||||
type DiffThemeType = "light" | "dark" | "system";
|
type DiffThemeType = "light" | "dark";
|
||||||
|
|
||||||
|
function resolveDiffThemeName(theme: "light" | "dark") {
|
||||||
|
return theme === "dark" ? "pierre-dark" : "pierre-light";
|
||||||
|
}
|
||||||
|
|
||||||
type RenderablePatch =
|
type RenderablePatch =
|
||||||
| {
|
| {
|
||||||
@@ -472,13 +476,15 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
|
|||||||
{renderableFiles.map((fileDiff) => {
|
{renderableFiles.map((fileDiff) => {
|
||||||
const filePath = resolveFileDiffPath(fileDiff);
|
const filePath = resolveFileDiffPath(fileDiff);
|
||||||
const fileKey = buildFileDiffRenderKey(fileDiff);
|
const fileKey = buildFileDiffRenderKey(fileDiff);
|
||||||
|
const themedFileKey = `${fileKey}:${resolvedTheme}`;
|
||||||
return (
|
return (
|
||||||
<div key={fileKey} data-diff-file-path={filePath} className="rounded-md">
|
<div key={themedFileKey} data-diff-file-path={filePath} className="rounded-md">
|
||||||
<FileDiff
|
<FileDiff
|
||||||
fileDiff={fileDiff}
|
fileDiff={fileDiff}
|
||||||
options={{
|
options={{
|
||||||
diffStyle: diffRenderMode === "split" ? "split" : "unified",
|
diffStyle: diffRenderMode === "split" ? "split" : "unified",
|
||||||
lineDiffType: "none",
|
lineDiffType: "none",
|
||||||
|
theme: resolveDiffThemeName(resolvedTheme),
|
||||||
themeType: resolvedTheme as DiffThemeType,
|
themeType: resolvedTheme as DiffThemeType,
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,8 +1,39 @@
|
|||||||
import { WorkerPoolContextProvider } from "@pierre/diffs/react";
|
import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react";
|
||||||
import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
|
import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
|
||||||
import { useMemo, type ReactNode } from "react";
|
import { useEffect, useMemo, type ReactNode } from "react";
|
||||||
|
import { useTheme } from "../hooks/useTheme";
|
||||||
|
|
||||||
|
function resolveDiffThemeName(theme: "light" | "dark") {
|
||||||
|
return theme === "dark" ? "pierre-dark" : "pierre-light";
|
||||||
|
}
|
||||||
|
|
||||||
|
function DiffWorkerThemeSync({ themeName }: { themeName: "pierre-light" | "pierre-dark" }) {
|
||||||
|
const workerPool = useWorkerPool();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!workerPool) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const current = workerPool.getDiffRenderOptions();
|
||||||
|
if (current.theme === themeName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void workerPool
|
||||||
|
.setRenderOptions({
|
||||||
|
...current,
|
||||||
|
theme: themeName,
|
||||||
|
})
|
||||||
|
.catch(() => undefined);
|
||||||
|
}, [themeName, workerPool]);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
|
export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
|
||||||
|
const { resolvedTheme } = useTheme();
|
||||||
|
const diffThemeName = resolveDiffThemeName(resolvedTheme);
|
||||||
const workerPoolSize = useMemo(() => {
|
const workerPoolSize = useMemo(() => {
|
||||||
const cores =
|
const cores =
|
||||||
typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4);
|
typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4);
|
||||||
@@ -17,9 +48,11 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
|
|||||||
totalASTLRUCacheSize: 240,
|
totalASTLRUCacheSize: 240,
|
||||||
}}
|
}}
|
||||||
highlighterOptions={{
|
highlighterOptions={{
|
||||||
|
theme: diffThemeName,
|
||||||
tokenizeMaxLineLength: 1_000,
|
tokenizeMaxLineLength: 1_000,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<DiffWorkerThemeSync themeName={diffThemeName} />
|
||||||
{children}
|
{children}
|
||||||
</WorkerPoolContextProvider>
|
</WorkerPoolContextProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
import { useCallback, useEffect, useSyncExternalStore } from "react";
|
import { useCallback, useEffect, useSyncExternalStore } from "react";
|
||||||
|
|
||||||
type Theme = "light" | "dark" | "system";
|
type Theme = "light" | "dark" | "system";
|
||||||
|
type ThemeSnapshot = {
|
||||||
|
theme: Theme;
|
||||||
|
systemDark: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
const STORAGE_KEY = "t3code:theme";
|
const STORAGE_KEY = "t3code:theme";
|
||||||
const MEDIA_QUERY = "(prefers-color-scheme: dark)";
|
const MEDIA_QUERY = "(prefers-color-scheme: dark)";
|
||||||
|
|
||||||
let listeners: Array<() => void> = [];
|
let listeners: Array<() => void> = [];
|
||||||
|
let lastSnapshot: ThemeSnapshot | null = null;
|
||||||
function emitChange() {
|
function emitChange() {
|
||||||
for (const listener of listeners) listener();
|
for (const listener of listeners) listener();
|
||||||
}
|
}
|
||||||
@@ -39,8 +44,16 @@ function applyTheme(theme: Theme, suppressTransitions = false) {
|
|||||||
// Apply immediately on module load to prevent flash
|
// Apply immediately on module load to prevent flash
|
||||||
applyTheme(getStored());
|
applyTheme(getStored());
|
||||||
|
|
||||||
function getSnapshot(): Theme {
|
function getSnapshot(): ThemeSnapshot {
|
||||||
return getStored();
|
const theme = getStored();
|
||||||
|
const systemDark = theme === "system" ? getSystemDark() : false;
|
||||||
|
|
||||||
|
if (lastSnapshot && lastSnapshot.theme === theme && lastSnapshot.systemDark === systemDark) {
|
||||||
|
return lastSnapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSnapshot = { theme, systemDark };
|
||||||
|
return lastSnapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribe(listener: () => void): () => void {
|
function subscribe(listener: () => void): () => void {
|
||||||
@@ -71,10 +84,11 @@ function subscribe(listener: () => void): () => void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useTheme() {
|
export function useTheme() {
|
||||||
const theme = useSyncExternalStore(subscribe, getSnapshot);
|
const snapshot = useSyncExternalStore(subscribe, getSnapshot);
|
||||||
|
const theme = snapshot.theme;
|
||||||
|
|
||||||
const resolvedTheme: "light" | "dark" =
|
const resolvedTheme: "light" | "dark" =
|
||||||
theme === "system" ? (getSystemDark() ? "dark" : "light") : theme;
|
theme === "system" ? (snapshot.systemDark ? "dark" : "light") : theme;
|
||||||
|
|
||||||
const setTheme = useCallback((next: Theme) => {
|
const setTheme = useCallback((next: Theme) => {
|
||||||
localStorage.setItem(STORAGE_KEY, next);
|
localStorage.setItem(STORAGE_KEY, next);
|
||||||
|
|||||||
Reference in new issue
Block a user