Skip to content

Commit d5d966b

Browse files
ColinEgeBOCOVO
authored andcommitted
feat(visualizer): Add scroll direction toggle to extension settings
Scroll direction is set in extension settings and added to visualizer via new provider
1 parent 0769e64 commit d5d966b

File tree

9 files changed

+102
-3
lines changed

9 files changed

+102
-3
lines changed

packages/dbml-vs-code-extension/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464
"To use dark mode theme colors",
6565
"To use light mode theme colors"
6666
]
67+
},
68+
"dbmlERDPreviewer.scrollDirection": {
69+
"type": "string",
70+
"default": "up-out",
71+
"enum": [
72+
"up-out",
73+
"up-in"
74+
],
75+
"enumDescriptions": [
76+
"To zoom out when scrolling up",
77+
"To zoom in when scrolling up"
78+
]
6779
}
6880
}
6981
}

packages/extension-shared/extension/helper/extensionConfigs.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Theme } from "json-table-schema-visualizer/src/types/theme";
2+
import { ScrollDirection } from "json-table-schema-visualizer/src/types/scrollDirection";
23
import { workspace, type WorkspaceConfiguration } from "vscode";
34

45
import { ConfigKeys } from "../types/configKeys";
@@ -29,9 +30,19 @@ export class ExtensionConfig {
2930
return Theme.dark;
3031
}
3132

33+
getScrollDirection(): ScrollDirection {
34+
const scrollDirection = this.config.get(ConfigKeys.scrollDirection);
35+
if (ScrollDirection.UpIn === scrollDirection) {
36+
return scrollDirection;
37+
}
38+
39+
return ScrollDirection.UpOut;
40+
}
41+
3242
getDefaultPageConfig(): DefaultPageConfig {
3343
const theme = this.getPreferredTheme();
44+
const scrollDirection = this.getScrollDirection();
3445

35-
return { theme };
46+
return { theme, scrollDirection };
3647
}
3748
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export enum ConfigKeys {
22
preferredTheme = "preferredTheme",
3+
scrollDirection = "scrollDirection",
34
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { type ScrollDirection } from "json-table-schema-visualizer/src/types/scrollDirection";
12
import { type Theme } from "json-table-schema-visualizer/src/types/theme";
23

34
export interface DefaultPageConfig {
45
theme: Theme;
6+
scrollDirection: ScrollDirection;
57
}

packages/extension-shared/src/App.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useCreateTheme } from "json-table-schema-visualizer/src/hooks/theme";
33
import ThemeProvider from "json-table-schema-visualizer/src/providers/ThemeProvider";
44
import NoSchemaMessage from "json-table-schema-visualizer/src/components/Messages/NoSchemaMessage";
55
import { type Theme } from "json-table-schema-visualizer/src/types/theme";
6+
import ScrollDirectionProvider from "json-table-schema-visualizer/src/providers/ScrollDirectionProvider";
67

78
import {
89
WebviewCommand,
@@ -44,7 +45,11 @@ const App = () => {
4445
setTheme={saveThemePreference}
4546
themeColors={themeColors}
4647
>
47-
<DiagramViewer key={key} {...schema} />
48+
<ScrollDirectionProvider
49+
scrollDirection={window.EXTENSION_DEFAULT_CONFIG?.scrollDirection}
50+
>
51+
<DiagramViewer key={key} {...schema} />
52+
</ScrollDirectionProvider>
4853
</ThemeProvider>
4954
);
5055
};

packages/json-table-schema-visualizer/src/components/DiagramViewer/DiagramWrapper.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { useThemeColors, useThemeContext } from "@/hooks/theme";
1313
import { Theme } from "@/types/theme";
1414
import { useStageStartingState } from "@/hooks/stage";
1515
import { stageStateStore } from "@/stores/stagesState";
16+
import { useScrollDirectionContext } from "@/hooks/scrollDirection";
17+
import { ScrollDirection } from "@/types/scrollDirection";
1618

1719
interface DiagramWrapperProps {
1820
children: ReactNode;
@@ -22,6 +24,7 @@ const DiagramWrapper = ({ children }: DiagramWrapperProps) => {
2224
const scaleBy = 1.02;
2325
const { height: windowHeight, width: windowWidth } = useWindowSize();
2426
const { theme } = useThemeContext();
27+
const { scrollDirection } = useScrollDirectionContext();
2528
const { onChange: onGrabbing, onRestore: onGrabRelease } =
2629
useCursorChanger("grabbing");
2730
const themeColors = useThemeColors();
@@ -53,7 +56,12 @@ const DiagramWrapper = ({ children }: DiagramWrapperProps) => {
5356
};
5457

5558
// how to scale? Zoom in? Or zoom out?
56-
let direction = e.evt.deltaY > 0 ? 1 : -1;
59+
let direction = 0;
60+
if (scrollDirection === ScrollDirection.UpOut) {
61+
direction = e.evt.deltaY > 0 ? 1 : -1;
62+
} else if (scrollDirection === ScrollDirection.UpIn) {
63+
direction = e.evt.deltaY > 0 ? -1 : 1;
64+
}
5765

5866
// when we zoom on trackpad, e.evt.ctrlKey is true
5967
// in that case lets revert direction
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useContext } from "react";
2+
3+
import {
4+
type ScrollDirection,
5+
type ScrollDirectionProviderValue,
6+
} from "@/types/scrollDirection";
7+
import { ScrollDirectionContext } from "@/providers/ScrollDirectionProvider";
8+
9+
export const useScrollDirectionContext = (): ScrollDirectionProviderValue => {
10+
const contextValue = useContext(ScrollDirectionContext);
11+
if (contextValue === undefined) {
12+
throw new Error(
13+
"it seem you forgot to wrap your app with ScrollDirectionProvider",
14+
);
15+
}
16+
17+
return contextValue;
18+
};
19+
20+
export const useScrollDirection = (): ScrollDirection => {
21+
const contextValue = useScrollDirectionContext();
22+
23+
return contextValue.scrollDirection;
24+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { createContext, type PropsWithChildren } from "react";
2+
3+
import {
4+
ScrollDirection,
5+
type ScrollDirectionProviderValue,
6+
} from "@/types/scrollDirection";
7+
8+
export const ScrollDirectionContext =
9+
createContext<ScrollDirectionProviderValue>({
10+
scrollDirection: ScrollDirection.UpOut,
11+
});
12+
13+
interface ScrollDirectionProviderProps extends PropsWithChildren {
14+
scrollDirection: ScrollDirection;
15+
}
16+
17+
const ScrollDirectionProvider = ({
18+
scrollDirection,
19+
children,
20+
}: ScrollDirectionProviderProps) => {
21+
return (
22+
<ScrollDirectionContext.Provider value={{ scrollDirection }}>
23+
{children}
24+
</ScrollDirectionContext.Provider>
25+
);
26+
};
27+
28+
export default ScrollDirectionProvider;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export enum ScrollDirection {
2+
UpIn = "up-in",
3+
UpOut = "up-out",
4+
}
5+
6+
export interface ScrollDirectionProviderValue {
7+
scrollDirection: ScrollDirection;
8+
}

0 commit comments

Comments
 (0)