Skip to content

Commit

Permalink
add iframe message for caching tutorial code
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Oct 17, 2024
1 parent d1b3a4d commit a05f3df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 7 additions & 0 deletions localtypings/pxteditor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ declare namespace pxt.editor {
| "editorcontentloaded"
| "serviceworkerregistered"
| "runeval"
| "precachetutorial"

// package extension messasges
| ExtInitializeType
Expand Down Expand Up @@ -408,6 +409,12 @@ declare namespace pxt.editor {
carryoverPreviousCode?: boolean;
}

export interface PrecacheTutorialRequest extends EditorMessageRequest {
action: "precachetutorial";
data: pxt.github.GHTutorialResponse;
lang?: string;
}

export interface InfoMessage {
versions: pxt.TargetVersions;
locale: string;
Expand Down
16 changes: 15 additions & 1 deletion pxteditor/editorcontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export function bindEditorMessages(getEditorAsync: () => Promise<IProjectView>)
})
});
}
case "renderxml": {
case "renderxml": {
const rendermsg = data as pxt.editor.EditorMessageRenderXmlRequest;
return Promise.resolve()
.then(() => {
Expand Down Expand Up @@ -303,6 +303,20 @@ case "renderxml": {
}
return projectView.setLanguageRestrictionAsync(msg.restriction);
}
case "precachetutorial": {
const msg = data as pxt.editor.PrecacheTutorialRequest;
const tutorialData = msg.data;
const lang = msg.lang || pxt.Util.userLanguage();

return pxt.github.db.cacheReposAsync(tutorialData)
.then(async () => {
if (typeof tutorialData.markdown === "string") {
// the markdown needs to be cached in the translation db
const db = await pxt.BrowserUtils.translationDbAsync();
await db.setAsync(lang, tutorialData.path, undefined, undefined, tutorialData.markdown);
}
});
}
}
return Promise.resolve();
});
Expand Down
10 changes: 10 additions & 0 deletions pxtservices/editorDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,16 @@ export class EditorDriver extends IframeDriver {
);
}

async precacheTutorial(data: pxt.github.GHTutorialResponse) {
await this.sendRequest(
{
type: "pxteditor",
action: "precachetutorial",
data
} as pxt.editor.PrecacheTutorialRequest
);
}

addEventListener(event: typeof MessageSentEvent, handler: (ev: pxt.editor.EditorMessage) => void): void;
addEventListener(event: typeof MessageReceivedEvent, handler: (ev: pxt.editor.EditorMessage) => void): void;
addEventListener(event: "event", handler: (ev: pxt.editor.EditorMessageEventRequest) => void): void;
Expand Down

0 comments on commit a05f3df

Please sign in to comment.