Skip to content

Commit fe4681a

Browse files
committed
[pr] scroll constraints
1 parent 108441c commit fe4681a

File tree

14 files changed

+1018
-67
lines changed

14 files changed

+1018
-67
lines changed

excalidraw-app/App.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,20 @@ const initializeScene = async (opts: {
214214
)
215215
> => {
216216
const searchParams = new URLSearchParams(window.location.search);
217+
const hashParams = new URLSearchParams(window.location.hash.slice(1));
217218
const id = searchParams.get("id");
218-
const jsonBackendMatch = window.location.hash.match(
219-
/^#json=([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+)$/,
220-
);
219+
const shareableLink = hashParams.get("json")?.split(",");
220+
221+
if (shareableLink) {
222+
hashParams.delete("json");
223+
const hash = `#${decodeURIComponent(hashParams.toString())}`;
224+
window.history.replaceState(
225+
{},
226+
APP_NAME,
227+
`${window.location.origin}${hash}`,
228+
);
229+
}
230+
221231
const externalUrlMatch = window.location.hash.match(/^#url=(.*)$/);
222232

223233
const localDataState = importFromLocalStorage();
@@ -227,7 +237,7 @@ const initializeScene = async (opts: {
227237
} = await loadScene(null, null, localDataState);
228238

229239
let roomLinkData = getCollaborationLinkData(window.location.href);
230-
const isExternalScene = !!(id || jsonBackendMatch || roomLinkData);
240+
const isExternalScene = !!(id || shareableLink || roomLinkData);
231241
if (isExternalScene) {
232242
if (
233243
// don't prompt if scene is empty
@@ -237,16 +247,16 @@ const initializeScene = async (opts: {
237247
// otherwise, prompt whether user wants to override current scene
238248
(await openConfirmModal(shareableLinkConfirmDialog))
239249
) {
240-
if (jsonBackendMatch) {
250+
if (shareableLink) {
241251
scene = await loadScene(
242-
jsonBackendMatch[1],
243-
jsonBackendMatch[2],
252+
shareableLink[0],
253+
shareableLink[1],
244254
localDataState,
245255
);
246256
}
247257
scene.scrollToContent = true;
248258
if (!roomLinkData) {
249-
window.history.replaceState({}, APP_NAME, window.location.origin);
259+
// window.history.replaceState({}, APP_NAME, window.location.origin);
250260
}
251261
} else {
252262
// https://github.com/excalidraw/excalidraw/issues/1919
@@ -263,7 +273,7 @@ const initializeScene = async (opts: {
263273
}
264274

265275
roomLinkData = null;
266-
window.history.replaceState({}, APP_NAME, window.location.origin);
276+
// window.history.replaceState({}, APP_NAME, window.location.origin);
267277
}
268278
} else if (externalUrlMatch) {
269279
window.history.replaceState({}, APP_NAME, window.location.origin);
@@ -324,12 +334,12 @@ const initializeScene = async (opts: {
324334
key: roomLinkData.roomKey,
325335
};
326336
} else if (scene) {
327-
return isExternalScene && jsonBackendMatch
337+
return isExternalScene && shareableLink
328338
? {
329339
scene,
330340
isExternalScene,
331-
id: jsonBackendMatch[1],
332-
key: jsonBackendMatch[2],
341+
id: shareableLink[0],
342+
key: shareableLink[1],
333343
}
334344
: { scene, isExternalScene: false };
335345
}

packages/excalidraw/actions/actionCanvas.tsx

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

4546
import { t } from "../i18n";
4647
import { getNormalizedZoom } from "../scene";
@@ -137,7 +138,7 @@ export const actionZoomIn = register({
137138
trackEvent: { category: "canvas" },
138139
perform: (_elements, appState, _, app) => {
139140
return {
140-
appState: {
141+
appState: constrainScrollState({
141142
...appState,
142143
...getStateForZoom(
143144
{
@@ -148,7 +149,7 @@ export const actionZoomIn = register({
148149
appState,
149150
),
150151
userToFollow: null,
151-
},
152+
}),
152153
captureUpdate: CaptureUpdateAction.EVENTUALLY,
153154
};
154155
},
@@ -178,7 +179,7 @@ export const actionZoomOut = register({
178179
trackEvent: { category: "canvas" },
179180
perform: (_elements, appState, _, app) => {
180181
return {
181-
appState: {
182+
appState: constrainScrollState({
182183
...appState,
183184
...getStateForZoom(
184185
{
@@ -189,7 +190,7 @@ export const actionZoomOut = register({
189190
appState,
190191
),
191192
userToFollow: null,
192-
},
193+
}),
193194
captureUpdate: CaptureUpdateAction.EVENTUALLY,
194195
};
195196
},

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,
@@ -244,6 +244,7 @@ const APP_STATE_STORAGE_CONF = (<
244244
objectsSnapModeEnabled: { browser: true, export: false, server: false },
245245
userToFollow: { browser: false, export: false, server: false },
246246
followedBy: { browser: false, export: false, server: false },
247+
scrollConstraints: { browser: false, export: false, server: false },
247248
isCropping: { browser: false, export: false, server: false },
248249
croppingElementId: { browser: false, export: false, server: false },
249250
searchMatches: { browser: false, export: false, server: false },

0 commit comments

Comments
 (0)