From c5574e60f11386b8e4bb908d499f167e4cdcd3b6 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Tue, 21 Feb 2023 14:47:48 -0800 Subject: [PATCH] add support for "specs" message --- jacdac-ts | 2 +- pxt-jacdac | 2 +- src/components/ServiceManagerContext.tsx | 22 ++++++++++++++----- .../tools/ServiceSpecificationEditor.tsx | 17 ++++++++++++-- src/pages/tools/model-uploader.tsx | 6 ++--- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/jacdac-ts b/jacdac-ts index 33fbaed040ab..40c5e978cd60 160000 --- a/jacdac-ts +++ b/jacdac-ts @@ -1 +1 @@ -Subproject commit 33fbaed040abb5210817ab45ad5a0438cf493fb4 +Subproject commit 40c5e978cd60cf4053f8af0ef389a7fd3efba4d5 diff --git a/pxt-jacdac b/pxt-jacdac index f9371ba9a4b3..a30a0016b781 160000 --- a/pxt-jacdac +++ b/pxt-jacdac @@ -1 +1 @@ -Subproject commit f9371ba9a4b3d81367a5a22b104b8f13f20482ea +Subproject commit a30a0016b78134a137d259e385ccccc77e849ead diff --git a/src/components/ServiceManagerContext.tsx b/src/components/ServiceManagerContext.tsx index 193dbcf9f213..5ecee3f44538 100644 --- a/src/components/ServiceManagerContext.tsx +++ b/src/components/ServiceManagerContext.tsx @@ -3,9 +3,13 @@ import { JSONTryParse } from "../../jacdac-ts/src/jdom/utils" import { BrowserFileStorage, HostedFileStorage, - IFileStorage, + FileStorage, } from "../../jacdac-ts/src/embed/filestorage" -import { IThemeMessage } from "../../jacdac-ts/src/embed/protocol" +import { + EmbedMessage, + EmbedSpecsMessage, + EmbedThemeMessage, +} from "../../jacdac-ts/src/embed/protocol" import { ModelStore, HostedModelStore, @@ -50,7 +54,7 @@ export class LocalStorageSettings implements ISettings { export interface ServiceManagerContextProps { isHosted: boolean - fileStorage: IFileStorage + fileStorage: FileStorage modelStore: ModelStore } @@ -71,15 +75,21 @@ export const ServiceManagerProvider = ({ children }) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const handleMessage = useCallback( - (ev: MessageEvent) => { + (ev: MessageEvent) => { const msg = ev.data if (msg?.source !== "jacdac") return switch (msg.type) { case "theme": { - const themeMsg = msg as IThemeMessage + const themeMsg = msg as EmbedThemeMessage toggleDarkMode(themeMsg.data.type) break } + case "specs": { + const specMsg = msg as EmbedSpecsMessage + const { services } = specMsg.data + bus.setCustomServiceSpecifications(services) + break + } } }, [toggleDarkMode] @@ -94,7 +104,7 @@ export const ServiceManagerProvider = ({ children }) => { function createProps(): ServiceManagerContextProps { const isHosted = UIFlags.hosted - let fileStorage: IFileStorage = new BrowserFileStorage() + let fileStorage: FileStorage = new BrowserFileStorage() let modelStore: ModelStore = undefined if (isHosted) { console.log(`starting hosted services`) diff --git a/src/components/tools/ServiceSpecificationEditor.tsx b/src/components/tools/ServiceSpecificationEditor.tsx index 60faf00b0d4b..6583b2e5b9d7 100644 --- a/src/components/tools/ServiceSpecificationEditor.tsx +++ b/src/components/tools/ServiceSpecificationEditor.tsx @@ -1,12 +1,12 @@ -import React, { useMemo } from "react" +import React, { useEffect, useMemo } from "react" import { Grid } from "@mui/material" import { parseServiceSpecificationMarkdownToJSON } from "../../../jacdac-ts/jacdac-spec/spectool/jdspec" import { serviceMap } from "../../../jacdac-ts/src/jdom/spec" import RandomGenerator from "../RandomGenerator" import useLocalStorage from "../hooks/useLocalStorage" import HighlightTextField from "../ui/HighlightTextField" -import ServiceSpecification from "../specification/ServiceSpecification" import ServiceSpecificationSource from "../specification/ServiceSpecificationSource" +import { EmbedSpecsMessage } from "../../../jacdac-ts/src/embed/protocol" const SERVICE_SPECIFICATION_STORAGE_KEY = "jacdac:servicespecificationeditorsource" @@ -27,6 +27,19 @@ export default function ServiceSpecificationEditor() { json.shortId || `0x${json.classIdentifier.toString(16)}` ).toLowerCase()}` + + useEffect(() => { + const services: jdspec.ServiceSpec[] = + json && !json.errors?.length ? [json] : [] + window.postMessage({ + source: "jacdac", + type: "specs", + data: { + services, + }, + } as EmbedSpecsMessage) + }, [json]) + return ( diff --git a/src/pages/tools/model-uploader.tsx b/src/pages/tools/model-uploader.tsx index f69b6e496f35..2ccc4904b8ab 100644 --- a/src/pages/tools/model-uploader.tsx +++ b/src/pages/tools/model-uploader.tsx @@ -21,7 +21,7 @@ import { import SensorAggregatorConfigView from "../../components/SensorAggregatorConfigView" import ServiceManagerContext from "../../components/ServiceManagerContext" import useChange from "../../jacdac/useChange" -import { IFile } from "../../../jacdac-ts/src/embed/protocol" +import { EmbedFile } from "../../../jacdac-ts/src/embed/protocol" import { prettySize } from "../../../jacdac-ts/src/jdom/pretty" import RegisterTrend from "../../components/RegisterTrend" import { useRegisterUnpackedValue } from "../../jacdac/useRegisterValue" @@ -149,7 +149,7 @@ export default function ModelUploader() { setImporting(false) } } - const handleLoadModel = (model: IFile) => async () => { + const handleLoadModel = (model: EmbedFile) => async () => { try { setImporting(true) console.log(`loading model`, model) @@ -162,7 +162,7 @@ export default function ModelUploader() { setImporting(false) } } - const handleLoadInputConfiguration = (model: IFile) => async () => { + const handleLoadInputConfiguration = (model: EmbedFile) => async () => { try { setImporting(true) console.log(`loading model`, model)