Skip to content

Commit

Permalink
fix(play): fix cursor jump on first edit (#9356)
Browse files Browse the repository at this point in the history
fix(play): fix cursor jump on fist edit

When coming to playground in a fresh session the fist time
the code is run the cursor jumped back to the start.

This was due to a state mismatch.
  • Loading branch information
fiji-flo authored Jul 20, 2023
1 parent dab1fd9 commit 216400e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions client/src/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const JS_DEFAULT = "";

enum State {
initial,
ready,
remote,
modified,
}

enum DialogState {
Expand Down Expand Up @@ -85,10 +85,16 @@ export default function Playground() {
}
gleanClick(`${PLAYGROUND}: load-shared`);

return (await response.json()) || null;
const code = await response.json();
if (code) {
setState(State.remote);
return code;
}
return null;
},
{
fallbackData: (!gistId && load(SESSION_KEY)) || undefined,
fallbackData:
(!gistId && state === State.initial && load(SESSION_KEY)) || undefined,
}
);
const htmlRef = useRef<EditorHandle | null>(null);
Expand Down Expand Up @@ -127,12 +133,11 @@ export default function Playground() {
[getEditorContent]
);
useEffect(() => {
if (state === State.initial) {
if (state === State.initial || state === State.remote) {
if (initialCode && Object.values(initialCode).some(Boolean)) {
htmlRef.current?.setContent(initialCode?.html);
cssRef.current?.setContent(initialCode?.css);
jsRef.current?.setContent(initialCode?.js);
setState(State.remote);
if (initialCode.src) {
setCodeSrc(
initialCode?.src &&
Expand All @@ -144,6 +149,7 @@ export default function Playground() {
cssRef.current?.setContent(CSS_DEFAULT);
jsRef.current?.setContent(JS_DEFAULT);
}
setState(State.ready);
}
}, [initialCode, state]);
useEffect(() => {
Expand Down

0 comments on commit 216400e

Please sign in to comment.