feat: add persistent word-wrap setting for chat code blocks and tables (#3480)
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
f5f98cf0ae
commit
fb1034546f
8 files changed
+84
-31
No files matched your search
@@ -18,7 +18,6 @@ const clientSettings: ClientSettings = {
|
||||
confirmThreadDelete: false,
|
||||
dismissedProviderUpdateNotificationKeys: [],
|
||||
diffIgnoreWhitespace: true,
|
||||
diffWordWrap: true,
|
||||
favorites: [],
|
||||
providerModelPreferences: {},
|
||||
sidebarProjectGroupingMode: "repository_path",
|
||||
@@ -29,6 +28,7 @@ const clientSettings: ClientSettings = {
|
||||
sidebarThreadSortOrder: "created_at",
|
||||
sidebarThreadPreviewCount: 6,
|
||||
timestampFormat: "24-hour",
|
||||
wordWrap: true,
|
||||
};
|
||||
|
||||
const decodeClientSettingsJson = Schema.decodeEffect(Schema.fromJsonString(ClientSettingsSchema));
|
||||
|
||||
@@ -69,4 +69,25 @@ describe("clientPersistenceStorage", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults word wrap on and discards obsolete wrapping preferences", async () => {
|
||||
const testWindow = getTestWindow();
|
||||
testWindow.localStorage.setItem(
|
||||
"t3code:client-settings:v1",
|
||||
JSON.stringify({
|
||||
chatWordWrap: false,
|
||||
diffWordWrap: false,
|
||||
}),
|
||||
);
|
||||
const { readBrowserClientSettings } = await import("./clientPersistenceStorage");
|
||||
const settings = readBrowserClientSettings();
|
||||
|
||||
expect(settings).toEqual(
|
||||
expect.objectContaining({
|
||||
wordWrap: true,
|
||||
}),
|
||||
);
|
||||
expect(settings).not.toHaveProperty("chatWordWrap");
|
||||
expect(settings).not.toHaveProperty("diffWordWrap");
|
||||
});
|
||||
});
|
||||
@@ -54,6 +54,7 @@ import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering";
|
||||
import { fnv1a32 } from "../lib/diffRendering";
|
||||
import { LRUCache } from "../lib/lruCache";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import { useClientSettings } from "../hooks/useSettings";
|
||||
import {
|
||||
chatMarkdownClipboardPayload,
|
||||
serializeTableElementToCsv,
|
||||
@@ -295,7 +296,7 @@ function getHighlighterPromise(language: string): Promise<DiffsHighlighter> {
|
||||
function MarkdownTable({ children, ...props }: React.ComponentProps<"table">) {
|
||||
const containerRef = useRef<HTMLDivElement | null>(null);
|
||||
const tableRef = useRef<HTMLTableElement | null>(null);
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const [expanded, setExpanded] = useState(useClientSettings((settings) => settings.wordWrap));
|
||||
const [copied, setCopied] = useState(false);
|
||||
const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const expandLabel = expanded ? "Collapse table cells" : "Expand table cells";
|
||||
@@ -525,10 +526,11 @@ function MarkdownCodeBlock({
|
||||
children: ReactNode;
|
||||
}) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [wrapped, setWrapped] = useState(false);
|
||||
const [wrapped, setWrapped] = useState(useClientSettings((settings) => settings.wordWrap));
|
||||
const copiedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const wrapLabel = wrapped ? "Disable line wrap" : "Wrap lines";
|
||||
const copyLabel = copied ? "Copied" : "Copy code";
|
||||
|
||||
const handleCopy = useCallback(() => {
|
||||
if (typeof navigator === "undefined" || navigator.clipboard == null) {
|
||||
return;
|
||||
|
||||
@@ -186,7 +186,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
const { resolvedTheme } = useTheme();
|
||||
const settings = useClientSettings();
|
||||
const [diffRenderMode, setDiffRenderMode] = useState<DiffRenderMode>("stacked");
|
||||
const [diffWordWrap, setDiffWordWrap] = useState(settings.diffWordWrap);
|
||||
const [wordWrap, setWordWrap] = useState(settings.wordWrap);
|
||||
const [diffIgnoreWhitespace, setDiffIgnoreWhitespace] = useState(settings.diffIgnoreWhitespace);
|
||||
const [baseRefQuery, setBaseRefQuery] = useState("");
|
||||
const [collapsedDiffFiles, setCollapsedDiffFiles] = useState<CollapsedDiffFilesState>(() => ({
|
||||
@@ -194,6 +194,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
fileKeys: EMPTY_COLLAPSED_DIFF_FILE_KEYS,
|
||||
}));
|
||||
const codeViewRef = useRef<AnnotatableCodeViewHandle>(null);
|
||||
|
||||
const routeThreadRef = useParams({
|
||||
strict: false,
|
||||
select: (params) => resolveThreadRouteRef(params),
|
||||
@@ -695,14 +696,12 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
<TooltipTrigger
|
||||
render={
|
||||
<Toggle
|
||||
aria-label={
|
||||
diffWordWrap ? "Disable diff line wrapping" : "Enable diff line wrapping"
|
||||
}
|
||||
aria-label={wordWrap ? "Disable diff line wrapping" : "Enable diff line wrapping"}
|
||||
variant="outline"
|
||||
size="xs"
|
||||
pressed={diffWordWrap}
|
||||
pressed={wordWrap}
|
||||
onPressedChange={(pressed) => {
|
||||
setDiffWordWrap(Boolean(pressed));
|
||||
setWordWrap(Boolean(pressed));
|
||||
}}
|
||||
/>
|
||||
}
|
||||
@@ -710,7 +709,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
<TextWrapIcon className="size-3" />
|
||||
</TooltipTrigger>
|
||||
<TooltipPopup side="top">
|
||||
{diffWordWrap ? "Disable line wrapping" : "Enable line wrapping"}
|
||||
{wordWrap ? "Disable line wrapping" : "Enable line wrapping"}
|
||||
</TooltipPopup>
|
||||
</Tooltip>
|
||||
<Tooltip>
|
||||
@@ -844,7 +843,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
options={{
|
||||
diffStyle: diffRenderMode === "split" ? "split" : "unified",
|
||||
lineDiffType: "none",
|
||||
overflow: diffWordWrap ? "wrap" : "scroll",
|
||||
overflow: wordWrap ? "wrap" : "scroll",
|
||||
theme: resolveDiffThemeName(resolvedTheme),
|
||||
themeType: resolvedTheme as DiffThemeType,
|
||||
unsafeCSS: DIFF_PANEL_UNSAFE_CSS,
|
||||
@@ -860,7 +859,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
<pre
|
||||
className={cn(
|
||||
"max-h-[72vh] rounded-md border border-border/70 bg-background/70 p-3 font-mono text-[11px] leading-relaxed text-muted-foreground/90",
|
||||
diffWordWrap
|
||||
wordWrap
|
||||
? "overflow-auto whitespace-pre-wrap wrap-break-word"
|
||||
: "overflow-auto",
|
||||
)}
|
||||
|
||||
@@ -18,6 +18,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { isBrowserPreviewFile, openFileInPreview } from "~/browser/openFileInPreview";
|
||||
import ChatMarkdown from "~/components/ChatMarkdown";
|
||||
import { OpenInPicker } from "~/components/chat/OpenInPicker";
|
||||
import { useClientSettings } from "~/hooks/useSettings";
|
||||
import { useTheme } from "~/hooks/useTheme";
|
||||
import { getLocalStorageItem, setLocalStorageItem } from "~/hooks/useLocalStorage";
|
||||
import { resolveDiffThemeName } from "~/lib/diffRendering";
|
||||
@@ -248,6 +249,7 @@ interface EditableFileSurfaceProps {
|
||||
contents: string;
|
||||
resolvedTheme: "light" | "dark";
|
||||
revealRequestId: number;
|
||||
wordWrap: boolean;
|
||||
onPostRender: FilePostRender;
|
||||
onPendingChange: (relativePath: string, pending: boolean) => void;
|
||||
}
|
||||
@@ -296,6 +298,7 @@ function EditableFileSurface({
|
||||
contents,
|
||||
resolvedTheme,
|
||||
revealRequestId,
|
||||
wordWrap,
|
||||
onPostRender,
|
||||
onPendingChange,
|
||||
}: EditableFileSurfaceProps) {
|
||||
@@ -516,7 +519,7 @@ function EditableFileSurface({
|
||||
onGutterUtilityClick: setSelectedRange,
|
||||
onLineSelectionChange: setSelectedRange,
|
||||
onLineSelectionEnd: handleLineSelectionEnd,
|
||||
overflow: "scroll",
|
||||
overflow: wordWrap ? "wrap" : "scroll",
|
||||
theme: resolveDiffThemeName(resolvedTheme),
|
||||
themeType: resolvedTheme,
|
||||
unsafeCSS: FILE_LINK_REVEAL_UNSAFE_CSS,
|
||||
@@ -557,7 +560,12 @@ function RenderedMarkdownSurface({
|
||||
onPendingChange,
|
||||
}: Omit<
|
||||
EditableFileSurfaceProps,
|
||||
"resolvedTheme" | "composerDraftTarget" | "revealLine" | "revealRequestId" | "onPostRender"
|
||||
| "resolvedTheme"
|
||||
| "composerDraftTarget"
|
||||
| "revealLine"
|
||||
| "revealRequestId"
|
||||
| "wordWrap"
|
||||
| "onPostRender"
|
||||
> & {
|
||||
threadRef: ScopedThreadRef;
|
||||
}) {
|
||||
@@ -613,6 +621,7 @@ export default function FilePreviewPanel({
|
||||
onPendingChange,
|
||||
}: FilePreviewPanelProps) {
|
||||
const { resolvedTheme } = useTheme();
|
||||
const wordWrap = useClientSettings((settings) => settings.wordWrap);
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(environmentId);
|
||||
const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
|
||||
@@ -844,7 +853,7 @@ export default function FilePreviewPanel({
|
||||
}}
|
||||
options={{
|
||||
disableFileHeader: true,
|
||||
overflow: "scroll",
|
||||
overflow: wordWrap ? "wrap" : "scroll",
|
||||
theme: resolveDiffThemeName(resolvedTheme),
|
||||
themeType: resolvedTheme,
|
||||
unsafeCSS: FILE_LINK_REVEAL_UNSAFE_CSS,
|
||||
@@ -863,6 +872,7 @@ export default function FilePreviewPanel({
|
||||
contents={file.data.contents}
|
||||
resolvedTheme={resolvedTheme}
|
||||
revealRequestId={revealRequestId}
|
||||
wordWrap={wordWrap}
|
||||
onPostRender={onFilePostRender}
|
||||
onPendingChange={onPendingChange}
|
||||
/>
|
||||
|
||||
@@ -391,9 +391,7 @@ export function useSettingsRestore(onRestored?: () => void) {
|
||||
...(settings.sidebarThreadPreviewCount !== DEFAULT_UNIFIED_SETTINGS.sidebarThreadPreviewCount
|
||||
? ["Visible threads"]
|
||||
: []),
|
||||
...(settings.diffWordWrap !== DEFAULT_UNIFIED_SETTINGS.diffWordWrap
|
||||
? ["Diff line wrapping"]
|
||||
: []),
|
||||
...(settings.wordWrap !== DEFAULT_UNIFIED_SETTINGS.wordWrap ? ["Word wrap"] : []),
|
||||
...(settings.diffIgnoreWhitespace !== DEFAULT_UNIFIED_SETTINGS.diffIgnoreWhitespace
|
||||
? ["Diff whitespace changes"]
|
||||
: []),
|
||||
@@ -434,11 +432,11 @@ export function useSettingsRestore(onRestored?: () => void) {
|
||||
settings.defaultThreadEnvMode,
|
||||
settings.newWorktreesStartFromOrigin,
|
||||
settings.diffIgnoreWhitespace,
|
||||
settings.diffWordWrap,
|
||||
settings.automaticGitFetchInterval,
|
||||
settings.enableAssistantStreaming,
|
||||
settings.sidebarThreadPreviewCount,
|
||||
settings.timestampFormat,
|
||||
settings.wordWrap,
|
||||
theme,
|
||||
],
|
||||
);
|
||||
@@ -456,7 +454,7 @@ export function useSettingsRestore(onRestored?: () => void) {
|
||||
setTheme("system");
|
||||
updateSettings({
|
||||
timestampFormat: DEFAULT_UNIFIED_SETTINGS.timestampFormat,
|
||||
diffWordWrap: DEFAULT_UNIFIED_SETTINGS.diffWordWrap,
|
||||
wordWrap: DEFAULT_UNIFIED_SETTINGS.wordWrap,
|
||||
diffIgnoreWhitespace: DEFAULT_UNIFIED_SETTINGS.diffIgnoreWhitespace,
|
||||
sidebarThreadPreviewCount: DEFAULT_UNIFIED_SETTINGS.sidebarThreadPreviewCount,
|
||||
autoOpenPlanSidebar: DEFAULT_UNIFIED_SETTINGS.autoOpenPlanSidebar,
|
||||
@@ -594,15 +592,15 @@ export function GeneralSettingsPanel() {
|
||||
/>
|
||||
|
||||
<SettingsRow
|
||||
title="Diff line wrapping"
|
||||
description="Set the default wrap state when the diff panel opens."
|
||||
title="Word wrap"
|
||||
description="Wrap long lines in code blocks, tables, diffs, and file previews by default."
|
||||
resetAction={
|
||||
settings.diffWordWrap !== DEFAULT_UNIFIED_SETTINGS.diffWordWrap ? (
|
||||
settings.wordWrap !== DEFAULT_UNIFIED_SETTINGS.wordWrap ? (
|
||||
<SettingResetButton
|
||||
label="diff line wrapping"
|
||||
label="word wrapping"
|
||||
onClick={() =>
|
||||
updateSettings({
|
||||
diffWordWrap: DEFAULT_UNIFIED_SETTINGS.diffWordWrap,
|
||||
wordWrap: DEFAULT_UNIFIED_SETTINGS.wordWrap,
|
||||
})
|
||||
}
|
||||
/>
|
||||
@@ -610,9 +608,9 @@ export function GeneralSettingsPanel() {
|
||||
}
|
||||
control={
|
||||
<Switch
|
||||
checked={settings.diffWordWrap}
|
||||
onCheckedChange={(checked) => updateSettings({ diffWordWrap: Boolean(checked) })}
|
||||
aria-label="Wrap diff lines by default"
|
||||
checked={settings.wordWrap}
|
||||
onCheckedChange={(checked) => updateSettings({ wordWrap: Boolean(checked) })}
|
||||
aria-label="Wrap code, tables, diffs, and file previews by default"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
|
||||
@@ -2,12 +2,35 @@ import { describe, expect, it } from "vite-plus/test";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { ProviderInstanceId } from "./providerInstance.ts";
|
||||
import { DEFAULT_SERVER_SETTINGS, ServerSettings, ServerSettingsPatch } from "./settings.ts";
|
||||
import {
|
||||
ClientSettingsSchema,
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
ServerSettings,
|
||||
ServerSettingsPatch,
|
||||
} from "./settings.ts";
|
||||
|
||||
const decodeClientSettings = Schema.decodeUnknownSync(ClientSettingsSchema);
|
||||
const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
|
||||
const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
|
||||
const encodeServerSettings = Schema.encodeSync(ServerSettings);
|
||||
|
||||
describe("ClientSettings word wrap", () => {
|
||||
it("defaults word wrap on", () => {
|
||||
expect(decodeClientSettings({}).wordWrap).toBe(true);
|
||||
});
|
||||
|
||||
it("ignores obsolete wrapping preferences", () => {
|
||||
const decoded = decodeClientSettings({
|
||||
chatWordWrap: false,
|
||||
diffWordWrap: false,
|
||||
});
|
||||
|
||||
expect(decoded.wordWrap).toBe(true);
|
||||
expect(decoded).not.toHaveProperty("chatWordWrap");
|
||||
expect(decoded).not.toHaveProperty("diffWordWrap");
|
||||
});
|
||||
});
|
||||
|
||||
describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
|
||||
it("defaults to an empty record so legacy configs without the key still decode", () => {
|
||||
expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
|
||||
|
||||
@@ -47,7 +47,6 @@ export const ClientSettingsSchema = Schema.Struct({
|
||||
Schema.withDecodingDefault(Effect.succeed([])),
|
||||
),
|
||||
diffIgnoreWhitespace: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
|
||||
diffWordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
|
||||
// Model favorites. Historically keyed by provider kind, now
|
||||
// widened to `ProviderInstanceId` so users can favorite a specific model
|
||||
// on a custom provider instance (e.g. "Codex Personal · gpt-5") without
|
||||
@@ -92,6 +91,7 @@ export const ClientSettingsSchema = Schema.Struct({
|
||||
timestampFormat: TimestampFormat.pipe(
|
||||
Schema.withDecodingDefault(Effect.succeed(DEFAULT_TIMESTAMP_FORMAT)),
|
||||
),
|
||||
wordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
|
||||
});
|
||||
export type ClientSettings = typeof ClientSettingsSchema.Type;
|
||||
|
||||
@@ -538,7 +538,6 @@ export const ClientSettingsPatch = Schema.Struct({
|
||||
confirmThreadArchive: Schema.optionalKey(Schema.Boolean),
|
||||
confirmThreadDelete: Schema.optionalKey(Schema.Boolean),
|
||||
diffIgnoreWhitespace: Schema.optionalKey(Schema.Boolean),
|
||||
diffWordWrap: Schema.optionalKey(Schema.Boolean),
|
||||
favorites: Schema.optionalKey(
|
||||
Schema.Array(
|
||||
Schema.Struct({
|
||||
@@ -568,5 +567,6 @@ export const ClientSettingsPatch = Schema.Struct({
|
||||
sidebarThreadSortOrder: Schema.optionalKey(SidebarThreadSortOrder),
|
||||
sidebarThreadPreviewCount: Schema.optionalKey(SidebarThreadPreviewCount),
|
||||
timestampFormat: Schema.optionalKey(TimestampFormat),
|
||||
wordWrap: Schema.optionalKey(Schema.Boolean),
|
||||
});
|
||||
export type ClientSettingsPatch = typeof ClientSettingsPatch.Type;
|
||||
Reference in new issue
Block a user