-
-
Notifications
You must be signed in to change notification settings - Fork 3
Make sidebar resizable #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| "chroma-js": "^3.1.2", | ||
| "diff": "^8.0.2", | ||
| "luxon": "^3.7.2", | ||
| "paneforge": "^1.0.2", | ||
| "runed": "^0.36.0", | ||
| "shiki": "^3.15.0", | ||
| "svelte-toolbelt": "^0.10.6", | ||
|
|
@@ -882,6 +883,8 @@ | |
|
|
||
| "package-manager-detector": ["[email protected]", "", {}, "sha512-ZsEbbZORsyHuO00lY1kV3/t72yp6Ysay6Pd17ZAlNGuGwmWDLCJxFpRs0IzfXfj1o4icJOkUEioexFHzyPurSQ=="], | ||
|
|
||
| "paneforge": ["[email protected]", "", { "dependencies": { "runed": "^0.23.4", "svelte-toolbelt": "^0.9.2" }, "peerDependencies": { "svelte": "^5.29.0" } }, "sha512-KzmIXQH1wCfwZ4RsMohD/IUtEjVhteR+c+ulb/CHYJHX8SuDXoJmChtsc/Xs5Wl8NHS4L5Q7cxL8MG40gSU1bA=="], | ||
|
|
||
| "parent-module": ["[email protected]", "", { "dependencies": { "callsites": "^3.0.0" } }, "sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g=="], | ||
|
|
||
| "parse5": ["[email protected]", "", { "dependencies": { "entities": "^6.0.0" } }, "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw=="], | ||
|
|
@@ -1196,6 +1199,10 @@ | |
|
|
||
| "minizlib/minipass": ["[email protected]", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], | ||
|
|
||
| "paneforge/runed": ["[email protected]", "", { "dependencies": { "esm-env": "^1.0.0" }, "peerDependencies": { "svelte": "^5.7.0" } }, "sha512-9q8oUiBYeXIDLWNK5DfCWlkL0EW3oGbk845VdKlPeia28l751VpfesaB/+7pI6rnbx1I6rqoZ2fZxptOJLxILA=="], | ||
|
|
||
| "paneforge/svelte-toolbelt": ["[email protected]", "", { "dependencies": { "clsx": "^2.1.1", "runed": "^0.29.0", "style-to-object": "^1.0.8" }, "peerDependencies": { "svelte": "^5.30.2" } }, "sha512-HCSWxCtVmv+c6g1ACb8LTwHVbDqLKJvHpo6J8TaqwUme2hj9ATJCpjCPNISR1OCq2Q4U1KT41if9ON0isINQZw=="], | ||
|
|
||
| "parse5/entities": ["[email protected]", "", {}, "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g=="], | ||
|
|
||
| "svelte-check/fdir": ["[email protected]", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-hiFoqpyZcfNm1yc4u8oWCf9A2c4D3QjCrks3zmoVKVxpQRzmPNar1hUJcBG2RQHvEVGDN+Jm81ZheVLAQMK6+w=="], | ||
|
|
@@ -1224,6 +1231,8 @@ | |
|
|
||
| "csso/css-tree/mdn-data": ["[email protected]", "", {}, "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g=="], | ||
|
|
||
| "paneforge/svelte-toolbelt/runed": ["[email protected]", "", { "dependencies": { "esm-env": "^1.0.0" }, "peerDependencies": { "svelte": "^5.7.0" } }, "sha512-0cq6cA6sYGZwl/FvVqjx9YN+1xEBu9sDDyuWdDW1yWX7JF2wmvmVKfH+hVCZs+csW+P3ARH92MjI3H9QTagOQA=="], | ||
|
|
||
| "wrangler/esbuild/@esbuild/aix-ppc64": ["@esbuild/[email protected]", "", { "os": "aix", "cpu": "ppc64" }, "sha512-1VCICWypeQKhVbE9oW/sJaAmjLxhVqacdkvPLEjwlttjfwENRSClS8EjBz0KzRyFSCPDIkuXW34Je/vk7zdB7Q=="], | ||
|
|
||
| "wrangler/esbuild/@esbuild/android-arm": ["@esbuild/[email protected]", "", { "os": "android", "cpu": "arm" }, "sha512-QNdQEps7DfFwE3hXiU4BZeOV68HHzYwGd0Nthhd3uCkkEKK7/R6MTgM0P7H7FAs5pU/DIWsviMmEGxEoxIZ+ZQ=="], | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import type { Pane } from "paneforge"; | ||
| import { clearCookie, setCookie } from "./util"; | ||
| import { watch } from "runed"; | ||
|
|
||
| export const ROOT_LAYOUT_KEY = "diff-viewer-root-layout"; | ||
| export interface PersistentLayoutState { | ||
| sidebarWidth: number; | ||
| } | ||
|
|
||
| export class LayoutState { | ||
| sidebarCollapsed = $state(false); | ||
|
|
||
| windowInnerWidth: number | undefined = $state(); | ||
| sidebarPane: Pane | undefined = $state(); | ||
| lastSidebarWidth: number | undefined = $state(); | ||
|
|
||
| minSidebarWidth = $derived.by(() => { | ||
| return this.getContainerProportion(200, 0); | ||
| }); | ||
| defaultSidebarWidth = $derived.by(() => { | ||
| if (this.lastSidebarWidth !== undefined) { | ||
| return this.lastSidebarWidth; | ||
| } | ||
| return this.getContainerProportion(350, 0.25); | ||
| }); | ||
|
|
||
| defaultMainWidth = $derived.by(() => { | ||
| if (this.lastSidebarWidth !== undefined) { | ||
| return 100 - this.lastSidebarWidth; | ||
| } | ||
| return undefined; | ||
| }); | ||
|
|
||
| constructor(persistentState: PersistentLayoutState | null) { | ||
| this.loadFrom(persistentState); | ||
|
|
||
| // Maintain sidebar size when resizing window | ||
| watch.pre( | ||
| () => this.windowInnerWidth, | ||
| (newValue, oldValue) => { | ||
| if (oldValue !== undefined && newValue !== undefined && this.sidebarPane) { | ||
| const oldPx = (this.sidebarPane.getSize() / 100) * oldValue; | ||
| const newProportion = this.getProportion(oldPx, newValue); | ||
| this.sidebarPane.resize(newProportion); | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| private loadFrom(persistentState: PersistentLayoutState | null) { | ||
| if (persistentState === null) { | ||
| return; | ||
| } | ||
|
|
||
| const sidebarWidth = persistentState.sidebarWidth; | ||
| if (Number.isFinite(sidebarWidth) && sidebarWidth >= 0 && sidebarWidth <= 100) { | ||
| this.lastSidebarWidth = sidebarWidth; | ||
| } | ||
| } | ||
|
|
||
| toggleSidebar() { | ||
| this.sidebarCollapsed = !this.sidebarCollapsed; | ||
| } | ||
|
|
||
| private getContainerProportion(px: number, defaultValue: number) { | ||
| if (this.windowInnerWidth === undefined) { | ||
| return defaultValue; | ||
| } | ||
| return this.getProportion(px, this.windowInnerWidth); | ||
| } | ||
|
|
||
| private getProportion(px: number, max: number) { | ||
| return Math.max(0, Math.min(100, (px / max) * 100)); | ||
| } | ||
|
|
||
| resetLayout() { | ||
| clearCookie(ROOT_LAYOUT_KEY); | ||
| this.lastSidebarWidth = undefined; | ||
| if (this.sidebarPane) { | ||
| this.sidebarPane.resize(this.defaultSidebarWidth); | ||
| } | ||
| } | ||
|
|
||
| onSidebarResize(size: number, prevSize: number | undefined) { | ||
| if (prevSize === undefined) { | ||
| // Prevent initial resize from triggering update loop | ||
| return; | ||
| } | ||
|
|
||
| /* | ||
| TODO: | ||
| *also* persist size in px to avoid sidebar changing size when reopening with | ||
| a different sized window | ||
|
|
||
| need to keep the proportion for SSR as paneforge does not currently provide | ||
| a way to preset a size in px (it generally works in proportions only) | ||
|
|
||
| this means there may be a shift on hydration when a new window uses an old cookie | ||
|
|
||
| see GH:svecosystem/paneforge/issues/91 | ||
| */ | ||
| this.lastSidebarWidth = size; | ||
| const rootLayout: PersistentLayoutState = { | ||
| sidebarWidth: this.lastSidebarWidth, | ||
| }; | ||
| setCookie(ROOT_LAYOUT_KEY, JSON.stringify(rootLayout)); | ||
jpenilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { ROOT_LAYOUT_KEY, type PersistentLayoutState } from "$lib/layout.svelte"; | ||
| import type { PageServerLoad } from "./$types"; | ||
|
|
||
| export const load: PageServerLoad = async ({ cookies }) => { | ||
| const cookie = cookies.get(ROOT_LAYOUT_KEY); | ||
| let parsed: PersistentLayoutState | null = null; | ||
| if (cookie) { | ||
| try { | ||
| parsed = JSON.parse(cookie); | ||
| } catch { | ||
| // Ignore invalid cookie | ||
| } | ||
| } | ||
| return { | ||
| rootLayout: parsed, | ||
| }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.