feat: pass scrollConstraints via props

This commit is contained in:
Arnošt Pleskot 2023-07-07 15:35:10 +02:00
parent 381ef93956
commit 19ba107041
No known key found for this signature in database
8 changed files with 21 additions and 6 deletions

View file

@ -16,7 +16,7 @@ export type ActionResult =
elements?: readonly ExcalidrawElement[] | null; elements?: readonly ExcalidrawElement[] | null;
appState?: MarkOptional< appState?: MarkOptional<
AppState, AppState,
"offsetTop" | "offsetLeft" | "width" | "height" "offsetTop" | "offsetLeft" | "width" | "height" | "scrollConstraints"
> | null; > | null;
files?: BinaryFiles | null; files?: BinaryFiles | null;
commitToHistory: boolean; commitToHistory: boolean;

View file

@ -17,7 +17,7 @@ const defaultExportScale = EXPORT_SCALES.includes(devicePixelRatio)
export const getDefaultAppState = (): Omit< export const getDefaultAppState = (): Omit<
AppState, AppState,
"offsetTop" | "offsetLeft" | "width" | "height" "offsetTop" | "offsetLeft" | "width" | "height" | "scrollConstraints"
> => { > => {
return { return {
showWelcomeScreen: false, showWelcomeScreen: false,
@ -98,7 +98,6 @@ export const getDefaultAppState = (): Omit<
pendingImageElementId: null, pendingImageElementId: null,
showHyperlinkPopup: false, showHyperlinkPopup: false,
selectedLinearElement: null, selectedLinearElement: null,
scrollConstraints: null,
}; };
}; };
@ -205,7 +204,7 @@ const APP_STATE_STORAGE_CONF = (<
pendingImageElementId: { browser: false, export: false, server: false }, pendingImageElementId: { browser: false, export: false, server: false },
showHyperlinkPopup: { browser: false, export: false, server: false }, showHyperlinkPopup: { browser: false, export: false, server: false },
selectedLinearElement: { browser: true, export: false, server: false }, selectedLinearElement: { browser: true, export: false, server: false },
scrollConstraints: { browser: true, export: false, server: false }, scrollConstraints: { browser: false, export: false, server: false },
}); });
const _clearAppStateForStorage = < const _clearAppStateForStorage = <

View file

@ -366,6 +366,7 @@ const ExcalidrawAppStateContext = React.createContext<AppState>({
height: 0, height: 0,
offsetLeft: 0, offsetLeft: 0,
offsetTop: 0, offsetTop: 0,
scrollConstraints: null,
}); });
ExcalidrawAppStateContext.displayName = "ExcalidrawAppStateContext"; ExcalidrawAppStateContext.displayName = "ExcalidrawAppStateContext";
@ -466,7 +467,9 @@ class App extends React.Component<AppProps, AppState> {
gridModeEnabled = false, gridModeEnabled = false,
theme = defaultAppState.theme, theme = defaultAppState.theme,
name = defaultAppState.name, name = defaultAppState.name,
scrollConstraints,
} = props; } = props;
this.state = { this.state = {
...defaultAppState, ...defaultAppState,
theme, theme,
@ -478,6 +481,7 @@ class App extends React.Component<AppProps, AppState> {
name, name,
width: window.innerWidth, width: window.innerWidth,
height: window.innerHeight, height: window.innerHeight,
scrollConstraints: scrollConstraints ?? null,
}; };
this.id = nanoid(); this.id = nanoid();
@ -1209,6 +1213,7 @@ class App extends React.Component<AppProps, AppState> {
height: this.state.height, height: this.state.height,
offsetTop: this.state.offsetTop, offsetTop: this.state.offsetTop,
offsetLeft: this.state.offsetLeft, offsetLeft: this.state.offsetLeft,
scrollConstraints: this.state.scrollConstraints,
}, },
null, null,
), ),

View file

@ -45,7 +45,7 @@ import { normalizeLink } from "./url";
type RestoredAppState = Omit< type RestoredAppState = Omit<
AppState, AppState,
"offsetTop" | "offsetLeft" | "width" | "height" "offsetTop" | "offsetLeft" | "width" | "height" | "scrollConstraints"
>; >;
export const AllowedExcalidrawActiveTools: Record< export const AllowedExcalidrawActiveTools: Record<

View file

@ -65,6 +65,7 @@ const canvas = exportToCanvas(
offsetLeft: 0, offsetLeft: 0,
width: 0, width: 0,
height: 0, height: 0,
scrollConstraints: null,
}, },
{}, // files {}, // files
{ {

View file

@ -42,6 +42,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onPointerDown, onPointerDown,
onScrollChange, onScrollChange,
children, children,
scrollConstraints,
} = props; } = props;
const canvasActions = props.UIOptions?.canvasActions; const canvasActions = props.UIOptions?.canvasActions;
@ -115,6 +116,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onLinkOpen={onLinkOpen} onLinkOpen={onLinkOpen}
onPointerDown={onPointerDown} onPointerDown={onPointerDown}
onScrollChange={onScrollChange} onScrollChange={onScrollChange}
scrollConstraints={scrollConstraints}
> >
{children} {children}
</App> </App>

View file

@ -64,7 +64,14 @@ export const exportToCanvas = ({
const { exportBackground, viewBackgroundColor } = restoredAppState; const { exportBackground, viewBackgroundColor } = restoredAppState;
return _exportToCanvas( return _exportToCanvas(
passElementsSafely(restoredElements), passElementsSafely(restoredElements),
{ ...restoredAppState, offsetTop: 0, offsetLeft: 0, width: 0, height: 0 }, {
...restoredAppState,
offsetTop: 0,
offsetLeft: 0,
width: 0,
height: 0,
scrollConstraints: null,
},
files || {}, files || {},
{ exportBackground, exportPadding, viewBackgroundColor }, { exportBackground, exportPadding, viewBackgroundColor },
(width: number, height: number) => { (width: number, height: number) => {

View file

@ -367,6 +367,7 @@ export interface ExcalidrawProps {
) => void; ) => void;
onScrollChange?: (scrollX: number, scrollY: number) => void; onScrollChange?: (scrollX: number, scrollY: number) => void;
children?: React.ReactNode; children?: React.ReactNode;
scrollConstraints?: AppState["scrollConstraints"];
} }
export type SceneData = { export type SceneData = {