Skip to content

Commit

Permalink
Force version history to update when restoring a version or sharing (#…
Browse files Browse the repository at this point in the history
…9718)

* hide history from file explorer and gitjson

* explicitly push snapshot when restoring version or sharing
  • Loading branch information
riknoll authored Oct 17, 2023
1 parent 0a4f209 commit ace3889
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 27 deletions.
45 changes: 45 additions & 0 deletions pxteditor/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,51 @@ namespace pxt.workspace {
toWrite[pxt.HISTORY_FILE] = JSON.stringify(history);
}

export function pushSnapshotOnHistory(text: ScriptText, currentTime: number) {
let history: pxt.workspace.HistoryFile;

if (text[pxt.HISTORY_FILE]) {
history = pxt.workspace.parseHistoryFile(text[pxt.HISTORY_FILE]);
}
else {
history = {
entries: [],
snapshots: [],
shares: []
};
}

history.snapshots.push(takeSnapshot(text, currentTime));

text[pxt.HISTORY_FILE] = JSON.stringify(history);
}

export function updateShareHistory(text: ScriptText, currentTime: number, shares: PublishVersion[]) {
let history: pxt.workspace.HistoryFile;

if (text[pxt.HISTORY_FILE]) {
history = pxt.workspace.parseHistoryFile(text[pxt.HISTORY_FILE]);
}
else {
history = {
entries: [],
snapshots: [],
shares: []
};
}

for (const share of shares) {
if (!history.shares.some(s => s.id === share.id)) {
history.shares.push({
id: share.id,
timestamp: currentTime,
});
}
}

text[pxt.HISTORY_FILE] = JSON.stringify(history);
}

function takeSnapshot(text: ScriptText, time: number) {
return {
timestamp: time,
Expand Down
1 change: 1 addition & 0 deletions webapp/src/dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ export async function showTurnBackTimeDialogAsync(header: pxt.workspace.Header,

header.targetVersion = editorVersion;

await workspace.saveSnapshotAsync(header.id);
await workspace.saveAsync(header, text);
reloadHeader();
}
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export class EditorPackage {
sortedFiles(): File[] {
let lst = Util.values(this.files)
if (!pxt.options.debug)
lst = lst.filter(f => f.name != pxt.github.GIT_JSON && f.name != pxt.SIMSTATE_JSON && f.name != pxt.SERIAL_EDITOR_FILE && f.name != pxt.PALETTES_FILE)
lst = lst.filter(f => f.name != pxt.github.GIT_JSON && f.name != pxt.SIMSTATE_JSON && f.name != pxt.SERIAL_EDITOR_FILE && f.name != pxt.PALETTES_FILE && f.name !== pxt.HISTORY_FILE)
lst.sort((a, b) => a.weight() - b.weight() || Util.strcmp(a.name, b.name))
return lst
}
Expand Down
97 changes: 71 additions & 26 deletions webapp/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,49 @@ export async function getTextAsync(id: string, getSavedText = false): Promise<Sc
});
}

export async function saveSnapshotAsync(id: string): Promise<void> {
await enqueueHistoryOperationAsync(
id,
text => {
pxt.workspace.pushSnapshotOnHistory(text, Date.now())
}
);
}

export async function updateShareHistoryAsync(id: string): Promise<void> {
await enqueueHistoryOperationAsync(
id,
(text, header) => {
pxt.workspace.updateShareHistory(text, Date.now(), header.pubVersions || [])
}
);
}

async function enqueueHistoryOperationAsync(id: string, op: (text: ScriptText, header: Header) => void) {
await maybeSyncHeadersAsync();

const e = lookup(id);

if (!e) return;

await headerQ.enqueue(id, async () => {
const saved = await impl.getAsync(e.header);

const h = saved.header;

op(saved.text, h);

const ver = await impl.setAsync(h, saved.version, saved.text);
e.version = ver;

data.invalidate("text:" + h.id);
data.invalidate("pkg-git-status:" + h.id);
data.invalidateHeader("header", h);

refreshHeadersSession();
});
}

export interface ScriptMeta {
description: string;
blocksWidth?: number;
Expand Down Expand Up @@ -343,46 +386,48 @@ function getScriptRequest(h: Header, text: ScriptText, meta: ScriptMeta, screens
}

// https://github.com/Microsoft/pxt-backend/blob/master/docs/sharing.md#anonymous-publishing
export function anonymousPublishAsync(h: Header, text: ScriptText, meta: ScriptMeta, screenshotUri?: string) {
export async function anonymousPublishAsync(h: Header, text: ScriptText, meta: ScriptMeta, screenshotUri?: string) {
checkHeaderSession(h);

const saveId = {}
h.saveId = saveId
const scrReq = getScriptRequest(h, text, meta, screenshotUri)

pxt.debug(`publishing script; ${scrReq.text.length} bytes`)
return Cloud.privatePostAsync("scripts", scrReq, /* forceLiveEndpoint */ true)
.then((inf: Cloud.JsonScript) => {
if (!h.pubVersions) h.pubVersions = [];
h.pubVersions.push({ id: inf.id, type: "snapshot" });
if (inf.shortid) inf.id = inf.shortid;
h.pubId = inf.shortid
h.pubCurrent = h.saveId === saveId
h.meta = inf.meta;
pxt.debug(`published; id /${h.pubId}`)
return saveAsync(h)
.then(() => inf)
})
const inf = await Cloud.privatePostAsync("scripts", scrReq, /* forceLiveEndpoint */ true) as Cloud.JsonScript;
if (!h.pubVersions) h.pubVersions = [];
h.pubVersions.push({ id: inf.id, type: "snapshot" });
if (inf.shortid) inf.id = inf.shortid;
h.pubId = inf.shortid
h.pubCurrent = h.saveId === saveId
h.meta = inf.meta;
pxt.debug(`published; id /${h.pubId}`)

await saveAsync(h);
await updateShareHistoryAsync(h.id);
return inf;
}

export function persistentPublishAsync(h: Header, text: ScriptText, meta: ScriptMeta, screenshotUri?: string) {
export async function persistentPublishAsync(h: Header, text: ScriptText, meta: ScriptMeta, screenshotUri?: string) {
checkHeaderSession(h);

const saveId = {}
h.saveId = saveId
const scrReq = getScriptRequest(h, text, meta, screenshotUri)
pxt.debug(`publishing script; ${scrReq.text.length} bytes`)
return cloud.shareAsync(h.id, scrReq)
.then((resp: { shareID: string, scr: cloud.SharedCloudProject }) => {
const { shareID, scr: script } = resp;
if (!h.pubVersions) h.pubVersions = [];
h.pubVersions.push({ id: script.id, type: "permalink" });
h.pubId = shareID
h.pubCurrent = h.saveId === saveId
h.pubPermalink = shareID;
h.meta = script.meta;
pxt.debug(`published; id /${h.pubId}`)
return saveAsync(h).then(() => h)
})

const { shareID, scr: script } = await cloud.shareAsync(h.id, scrReq);
if (!h.pubVersions) h.pubVersions = [];
h.pubVersions.push({ id: script.id, type: "permalink" });
h.pubId = shareID
h.pubCurrent = h.saveId === saveId
h.pubPermalink = shareID;
h.meta = script.meta;
pxt.debug(`published; id /${h.pubId}`)
await saveAsync(h);
await updateShareHistoryAsync(h.id);

return h;
}

function fixupVersionAsync(e: File) {
Expand Down

0 comments on commit ace3889

Please sign in to comment.