From fc524715a12ed49cdd3582f34fe8b52becc915a0 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Tue, 26 Sep 2023 15:51:15 -0700 Subject: [PATCH] Fixing a few time machine bugs (#9697) * don't create an extra diff when updating history * ensure shares show up in history immediately * prevent text bleeding on small screens --- pxteditor/history.ts | 23 +++++++++++++++--- theme/timeMachine.less | 1 + webapp/src/dialogs.tsx | 2 +- webapp/src/workspace.ts | 53 ++++++++++++++++++++++++----------------- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/pxteditor/history.ts b/pxteditor/history.ts index 20777da95ad6..7a0da9d74d55 100644 --- a/pxteditor/history.ts +++ b/pxteditor/history.ts @@ -311,8 +311,7 @@ namespace pxt.workspace { } // If no source changed, we can bail at this point - const diffed = diffScriptText(previousText, toWrite, currentTime, diff); - if (!diffed) { + if (scriptEquals(previousText, toWrite)) { toWrite[pxt.HISTORY_FILE] = JSON.stringify(history); return; } @@ -340,7 +339,11 @@ namespace pxt.workspace { } } else { - history.entries.push(diffed); + const diffed = diffScriptText(previousText, toWrite, currentTime, diff); + + if (diffed) { + history.entries.push(diffed); + } } // Finally, update the snapshots. These are failsafes in case something @@ -379,4 +382,18 @@ namespace pxt.workspace { text: pxt.workspace.createSnapshot(text) }; } + + function scriptEquals(a: ScriptText, b: ScriptText) { + const aKeys = Object.keys(a); + const bKeys = Object.keys(b); + + if (aKeys.length !== bKeys.length) return false; + + for (const key of aKeys) { + if (bKeys.indexOf(key) === -1) return false; + if (a[key] !== b[key]) return false; + } + + return true; + } } \ No newline at end of file diff --git a/theme/timeMachine.less b/theme/timeMachine.less index e16bf8f164e9..bb636d40037e 100644 --- a/theme/timeMachine.less +++ b/theme/timeMachine.less @@ -138,6 +138,7 @@ .time-machine-back-button { flex-grow: 1; + flex-shrink: 0; } } diff --git a/webapp/src/dialogs.tsx b/webapp/src/dialogs.tsx index 3d4cd96cf34a..23c8b1d092d7 100644 --- a/webapp/src/dialogs.tsx +++ b/webapp/src/dialogs.tsx @@ -860,7 +860,7 @@ export function isDontShowDownloadDialogFlagSet() { } export async function showTurnBackTimeDialogAsync(header: pxt.workspace.Header, reloadHeader: () => void) { - const text = await workspace.getTextAsync(header.id); + const text = await workspace.getTextAsync(header.id, true); let history: pxt.workspace.HistoryFile; if (text?.[pxt.HISTORY_FILE]) { diff --git a/webapp/src/workspace.ts b/webapp/src/workspace.ts index 5976c4857d6a..57ee453c84d7 100644 --- a/webapp/src/workspace.ts +++ b/webapp/src/workspace.ts @@ -281,24 +281,29 @@ export function initAsync() { }) } -export function getTextAsync(id: string): Promise { - return maybeSyncHeadersAsync() - .then(() => { - let e = lookup(id) - if (!e) - return Promise.resolve(null as ScriptText) - if (e.text) - return Promise.resolve(e.text) - return headerQ.enqueue(id, () => impl.getAsync(e.header) - .then(resp => { - if (!e.text) { - // otherwise we were beaten to it - e.text = fixupFileNames(resp.text); - } - e.version = resp.version; - return e.text - })) - }) +export async function getTextAsync(id: string, getSavedText = false): Promise { + await maybeSyncHeadersAsync(); + + const e = lookup(id); + if (!e) return null; + + if (e.text && !getSavedText) { + return e.text; + } + + return headerQ.enqueue(id, async () => { + const resp = await impl.getAsync(e.header); + const fixedText = fixupFileNames(resp.text); + + if (getSavedText) { + return fixedText; + } + else if (!e.text) { + e.text = fixedText; + } + e.version = resp.version; + return e.text + }); } export interface ScriptMeta { @@ -578,14 +583,18 @@ export async function saveAsync(h: Header, text?: ScriptText, fromCloudSync?: bo await fixupVersionAsync(e); let ver: any; - const toWrite = text ? e.text : null; + let toWrite = text ? e.text : null; if (pxt.appTarget.appTheme.timeMachine) { try { - if (toWrite) { - const previous = await impl.getAsync(h); + const previous = await impl.getAsync(h); + + if (previous) { + if (!toWrite && previous.header.pubVersions?.length !== h.pubVersions?.length) { + toWrite = { ...previous.text }; + } - if (previous) { + if (toWrite) { pxt.workspace.updateHistory(previous.text, toWrite, Date.now(), h.pubVersions || [], diffText, patchText); } }