Skip to content

Commit a2e45df

Browse files
committed
[pr] scroll constraints
1 parent 7d13600 commit a2e45df

File tree

16 files changed

+1055
-71
lines changed

16 files changed

+1055
-71
lines changed

excalidraw-app/App.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,20 @@ const initializeScene = async (opts: {
219219
)
220220
> => {
221221
const searchParams = new URLSearchParams(window.location.search);
222+
const hashParams = new URLSearchParams(window.location.hash.slice(1));
222223
const id = searchParams.get("id");
223-
const jsonBackendMatch = window.location.hash.match(
224-
/^#json=([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+)$/,
225-
);
224+
const shareableLink = hashParams.get("json")?.split(",");
225+
226+
if (shareableLink) {
227+
hashParams.delete("json");
228+
const hash = `#${decodeURIComponent(hashParams.toString())}`;
229+
window.history.replaceState(
230+
{},
231+
APP_NAME,
232+
`${window.location.origin}${hash}`,
233+
);
234+
}
235+
226236
const externalUrlMatch = window.location.hash.match(/^#url=(.*)$/);
227237

228238
const localDataState = importFromLocalStorage();
@@ -232,7 +242,7 @@ const initializeScene = async (opts: {
232242
} = await loadScene(null, null, localDataState);
233243

234244
let roomLinkData = getCollaborationLinkData(window.location.href);
235-
const isExternalScene = !!(id || jsonBackendMatch || roomLinkData);
245+
const isExternalScene = !!(id || shareableLink || roomLinkData);
236246
if (isExternalScene) {
237247
if (
238248
// don't prompt if scene is empty
@@ -242,16 +252,16 @@ const initializeScene = async (opts: {
242252
// otherwise, prompt whether user wants to override current scene
243253
(await openConfirmModal(shareableLinkConfirmDialog))
244254
) {
245-
if (jsonBackendMatch) {
255+
if (shareableLink) {
246256
scene = await loadScene(
247-
jsonBackendMatch[1],
248-
jsonBackendMatch[2],
257+
shareableLink[0],
258+
shareableLink[1],
249259
localDataState,
250260
);
251261
}
252262
scene.scrollToContent = true;
253263
if (!roomLinkData) {
254-
window.history.replaceState({}, APP_NAME, window.location.origin);
264+
// window.history.replaceState({}, APP_NAME, window.location.origin);
255265
}
256266
} else {
257267
// https://github.com/excalidraw/excalidraw/issues/1919
@@ -268,7 +278,7 @@ const initializeScene = async (opts: {
268278
}
269279

270280
roomLinkData = null;
271-
window.history.replaceState({}, APP_NAME, window.location.origin);
281+
// window.history.replaceState({}, APP_NAME, window.location.origin);
272282
}
273283
} else if (externalUrlMatch) {
274284
window.history.replaceState({}, APP_NAME, window.location.origin);
@@ -329,12 +339,12 @@ const initializeScene = async (opts: {
329339
key: roomLinkData.roomKey,
330340
};
331341
} else if (scene) {
332-
return isExternalScene && jsonBackendMatch
342+
return isExternalScene && shareableLink
333343
? {
334344
scene,
335345
isExternalScene,
336-
id: jsonBackendMatch[1],
337-
key: jsonBackendMatch[2],
346+
id: shareableLink[0],
347+
key: shareableLink[1],
338348
}
339349
: { scene, isExternalScene: false };
340350
}
@@ -946,13 +956,20 @@ const ExcalidrawWrapper = () => {
946956
</div>
947957
);
948958
}}
959+
// scrollConstraints={constraints.enabled ? constraints : undefined}
949960
onLinkOpen={(element, event) => {
950961
if (element.link && isElementLink(element.link)) {
951962
event.preventDefault();
952963
excalidrawAPI?.scrollToContent(element.link, { animate: true });
953964
}
954965
}}
955966
>
967+
{/* {excalidrawAPI && !isTestEnv() && (
968+
<ConstraintsSettings
969+
excalidrawAPI={excalidrawAPI}
970+
initialConstraints={constraints}
971+
/>
972+
)} */}
956973
<AppMainMenu
957974
onCollabDialogOpen={onCollabDialogOpen}
958975
isCollaborating={isCollaborating}

packages/excalidraw/actions/actionCanvas.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
ZoomResetIcon,
4141
} from "../components/icons";
4242
import { setCursor } from "../cursor";
43+
import { constrainScrollState } from "../scene/scrollConstraints";
4344

4445
import { t } from "../i18n";
4546
import { getNormalizedZoom } from "../scene";
@@ -140,7 +141,7 @@ export const actionZoomIn = register({
140141
trackEvent: { category: "canvas" },
141142
perform: (_elements, appState, _, app) => {
142143
return {
143-
appState: {
144+
appState: constrainScrollState({
144145
...appState,
145146
...getStateForZoom(
146147
{
@@ -151,7 +152,7 @@ export const actionZoomIn = register({
151152
appState,
152153
),
153154
userToFollow: null,
154-
},
155+
}),
155156
captureUpdate: CaptureUpdateAction.EVENTUALLY,
156157
};
157158
},
@@ -181,7 +182,7 @@ export const actionZoomOut = register({
181182
trackEvent: { category: "canvas" },
182183
perform: (_elements, appState, _, app) => {
183184
return {
184-
appState: {
185+
appState: constrainScrollState({
185186
...appState,
186187
...getStateForZoom(
187188
{
@@ -192,7 +193,7 @@ export const actionZoomOut = register({
192193
appState,
193194
),
194195
userToFollow: null,
195-
},
196+
}),
196197
captureUpdate: CaptureUpdateAction.EVENTUALLY,
197198
};
198199
},

packages/excalidraw/appState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const defaultExportScale = EXPORT_SCALES.includes(devicePixelRatio)
2323

2424
export const getDefaultAppState = (): Omit<
2525
AppState,
26-
"offsetTop" | "offsetLeft" | "width" | "height"
26+
"offsetTop" | "offsetLeft" | "width" | "height" | "scrollConstraints"
2727
> => {
2828
return {
2929
showWelcomeScreen: false,
@@ -249,6 +249,7 @@ const APP_STATE_STORAGE_CONF = (<
249249
objectsSnapModeEnabled: { browser: true, export: false, server: false },
250250
userToFollow: { browser: false, export: false, server: false },
251251
followedBy: { browser: false, export: false, server: false },
252+
scrollConstraints: { browser: false, export: false, server: false },
252253
isCropping: { browser: false, export: false, server: false },
253254
croppingElementId: { browser: false, export: false, server: false },
254255
searchMatches: { browser: false, export: false, server: false },

0 commit comments

Comments
 (0)