Skip to content

Commit

Permalink
chore: replace isPlugin property with subType property
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson committed Dec 20, 2024
1 parent 59e458a commit c2df968
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
8 changes: 4 additions & 4 deletions v3/src/components/web-view/component-handler-web-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ describe("DataInteractive ComponentHandler WebView and Game", () => {
expect(tileLayout?.width).toBe(newValue)

// Create game with url
const multidataUrl = "https://codap.concord.org/multidata-plugin/"
const result3 = handler.create!({}, { type: "game", URL: multidataUrl })
const multiDataUrl = "https://codap.concord.org/multidata-plugin/"
const result3 = handler.create!({}, { type: "game", URL: multiDataUrl })
expect(result3.success).toBe(true)
expect(documentContent.tileMap.size).toBe(2)
const result3Values = result3.values as DIComponentInfo
const tile3 = documentContent.tileMap.get(toV3Id(kWebViewIdPrefix, result3Values.id!))!
expect(tile3).toBeDefined()
expect(isWebViewModel(tile3.content)).toBe(true)
const gameModel = tile3.content as IWebViewModel
expect(gameModel.url).toBe(multidataUrl)
expect(gameModel.url).toBe(multiDataUrl)

// Get game
gameModel.setIsPlugin(true) // This would normally be set automatically when the plugin connects to codap
gameModel.setSubType("plugin") // This would normally be set automatically when the plugin connects to codap
testGetComponent(tile3, handler, (gameTile, values) => {
const { URL } = values as V2Game
expect(URL).toBe((gameTile.content as IWebViewModel).url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function useDataInteractiveController(iframeRef: React.RefObject<HTMLIFra
const originUrl = extractOrigin(url) ?? ""
const phone = new iframePhone.ParentEndpoint(iframeRef.current, originUrl,
() => {
webViewModel?.setIsPlugin(true)
webViewModel?.setSubType("plugin")
debugLog(DEBUG_PLUGINS, "connection with iframe established")
})
const handler: iframePhone.IframePhoneRpcEndpointHandlerFn =
Expand Down
3 changes: 3 additions & 0 deletions v3/src/components/web-view/web-view-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const kV2GuideViewType = "guideView"
export const kV2WebViewType = "webView"
export const kWebViewTileClass = "codap-web-view"
export const kWebViewBodyClass = "codap-web-view-body"

export const webViewSubTypes = ["guide", "plugin"] as const
export type WebViewSubType = typeof webViewSubTypes[number]
16 changes: 11 additions & 5 deletions v3/src/components/web-view/web-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Instance, SnapshotIn, types } from "mobx-state-tree"
import { DIMessage } from "../../data-interactive/iframe-phone-types"
import { ITileContentModel, TileContentModel } from "../../models/tiles/tile-content"
import { withoutUndo } from "../../models/history/without-undo"
import { kWebViewTileType } from "./web-view-defs"
import { kWebViewTileType, WebViewSubType, webViewSubTypes } from "./web-view-defs"

export const kDefaultAllowEmptyAttributeDeletion = true
export const kDefaultBlockAPIRequestsWhileEditing = false
Expand All @@ -18,6 +18,7 @@ export const WebViewModel = TileContentModel
.named("WebViewModel")
.props({
type: types.optional(types.literal(kWebViewTileType), kWebViewTileType),
subType: types.maybe(types.enumeration(webViewSubTypes)),
url: "",
state: types.frozen<unknown>(),
// fields controlled by plugins (like Collaborative) via interactiveFrame requests
Expand All @@ -27,8 +28,7 @@ export const WebViewModel = TileContentModel
preventBringToFront: kDefaultPreventBringToFront,
preventDataContextReorg: kDefaultPreventDataContextReorg,
preventTopLevelReorg: kDefaultPreventTopLevelReorg,
respectEditableItemAttribute: kDefaultRespectEditableItemAttribute,
isPlugin: false
respectEditableItemAttribute: kDefaultRespectEditableItemAttribute
})
.volatile(self => ({
dataInteractiveController: undefined as iframePhone.IframePhoneRpcEndpoint | undefined,
Expand All @@ -37,15 +37,21 @@ export const WebViewModel = TileContentModel
.views(self => ({
get allowBringToFront() {
return !self.preventBringToFront
},
get isGuide() {
return self.subType === "guide"

Check warning on line 42 in v3/src/components/web-view/web-view-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/web-view/web-view-model.ts#L41-L42

Added lines #L41 - L42 were not covered by tests
},
get isPlugin() {
return self.subType === "plugin"
}
}))
.actions(self => ({
setDataInteractiveController(controller?: iframePhone.IframePhoneRpcEndpoint) {
self.dataInteractiveController = controller
},
setIsPlugin(isPlugin: boolean) {
setSubType(subType: WebViewSubType) {
withoutUndo()
self.isPlugin = isPlugin
self.subType = subType
},
setSavedState(state: unknown) {
self.state = state
Expand Down
12 changes: 9 additions & 3 deletions v3/src/components/web-view/web-view-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { registerV2TileExporter, V2ExportedComponent, V2TileExportFn } from "../
import {
isV2WebViewComponent, isV2GameViewComponent, ICodapV2WebViewComponent, ICodapV2GameViewComponent
} from "../../v2/codap-v2-types"
import { kV2GameType, kV2WebViewType, kWebViewTileType } from "./web-view-defs"
import { kV2GameType, kV2WebViewType, kWebViewTileType, WebViewSubType } from "./web-view-defs"
import { isWebViewModel, IWebViewSnapshot, WebViewModel } from "./web-view-model"
import { WebViewComponent } from "./web-view"
import { WebViewInspector } from "./web-view-inspector"
Expand Down Expand Up @@ -80,12 +80,16 @@ function addWebViewSnapshot(args: V2TileImportArgs, name?: string, url?: string,
const { v2Component, insertTile } = args
const { guid } = v2Component
const { title, userSetTitle } = v2Component.componentStorage || {}
const subTypeMap: Record<string, WebViewSubType> = {
"DG.GameView": "plugin",
"DG.GuideView": "guide"
}

const content: IWebViewSnapshot = {
type: kWebViewTileType,
subType: subTypeMap[v2Component.type],
state,
url,
isPlugin: isV2GameViewComponent(v2Component)
url
}
const webViewTileSnap: ITileModelSnapshotIn = {
id: toV3Id(kWebViewIdPrefix, guid),
Expand Down Expand Up @@ -133,6 +137,8 @@ function importGameView(args: V2TileImportArgs) {
}
registerV2TileImporter("DG.GameView", importGameView)

// TODO add importer for DG.GuideView

const webViewComponentHandler: DIComponentHandler = {
create({ values }) {
const { URL } = values as V2WebView
Expand Down

0 comments on commit c2df968

Please sign in to comment.