-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor iframe communication and use MessageChannel (#9945)
* Refactor iframe communication and use MessageChannel * remove unused typing * Update pxtservices/iframeEmbeddedClient.ts Co-authored-by: Eric Anderson <[email protected]> * Update pxtservices/iframeEmbeddedClient.ts Co-authored-by: Eric Anderson <[email protected]> * pr feedback * lint --------- Co-authored-by: Eric Anderson <[email protected]>
- Loading branch information
1 parent
a344259
commit f4e9aad
Showing
10 changed files
with
865 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
"parserOptions": { | ||
"project": "pxtservices/tsconfig.json", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { IframeDriver } from "./iframeDriver"; | ||
|
||
export class AssetEditorDriver extends IframeDriver { | ||
constructor(frame: HTMLIFrameElement) { | ||
super(frame); | ||
} | ||
|
||
async openAsset(assetId: string, assetType: pxt.AssetType, files: pxt.Map<string>, palette?: string[]) { | ||
await this.sendRequest( | ||
{ | ||
type: "open", | ||
assetId, | ||
assetType, | ||
files, | ||
palette | ||
} as pxt.editor.OpenAssetEditorRequest | ||
); | ||
} | ||
|
||
async createAsset(assetType: pxt.AssetType, files: pxt.Map<string>, displayName?: string, palette?: string[]) { | ||
await this.sendRequest({ | ||
type: "create", | ||
assetType, | ||
files, | ||
displayName, | ||
palette | ||
} as pxt.editor.CreateAssetEditorRequest); | ||
} | ||
|
||
async saveAsset() { | ||
const resp = await this.sendRequest({ | ||
type: "save" | ||
} as pxt.editor.SaveAssetEditorRequest); | ||
|
||
return (resp as pxt.editor.SaveAssetEditorResponse).files; | ||
} | ||
|
||
async duplicateAsset(assetId: string, assetType: pxt.AssetType, files: pxt.Map<string>, palette?: string[]) { | ||
await this.sendRequest({ | ||
type: "duplicate", | ||
assetId, | ||
assetType, | ||
files, | ||
palette | ||
} as pxt.editor.DuplicateAssetEditorRequest); | ||
} | ||
|
||
addEventListener(event: "ready", handler: (ev: pxt.editor.AssetEditorReadyEvent) => void): void; | ||
addEventListener(event: "done-clicked", handler: (ev: pxt.editor.AssetEditorRequestSaveEvent) => void): void; | ||
addEventListener(event: string, handler: (ev: any) => void): void { | ||
super.addEventListener(event, handler); | ||
} | ||
|
||
protected handleMessage(event: MessageEvent<any>): void { | ||
const data = event.data; | ||
if (!data) return; | ||
|
||
if (data.type === "event") { | ||
this.fireEvent((data as pxt.editor.AssetEditorEvent).kind, data); | ||
} | ||
else { | ||
this.resolvePendingMessage(event); | ||
} | ||
} | ||
} |
Oops, something went wrong.