-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
225 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const kCalculatorTileType = "Calculator" | ||
export const kV2CalculatorType = "calculator" | ||
export const kV2CalculatorDGType = "DG.Calculator" // component type in documents | ||
export const kV2CalculatorDIType = "calculator" // component type in plugin API | ||
export const kCalculatorTileClass = "calculator" |
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 @@ | ||
import { DEBUG_SAVE_AS_V2 } from "./debug" | ||
|
||
// For now, this is determined by DEBUG flag, but it may be configured by url parameter | ||
// or some other means eventually. | ||
export const CONFIG_SAVE_AS_V2 = DEBUG_SAVE_AS_V2 |
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
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,65 @@ | ||
import { SetOptional } from "type-fest" | ||
import { kDefaultTileHeight, kDefaultTileWidth, kTitleBarHeight } from "../components/constants" | ||
import { IFreeTileRow, isFreeTileLayout } from "../models/document/free-tile-row" | ||
import { ISharedModelManager } from "../models/shared/shared-model-manager" | ||
import { ITileModel } from "../models/tiles/tile-model" | ||
import { CodapV2Component, CodapV2ComponentStorage } from "./codap-v2-types" | ||
import { toV2Id } from "../utilities/codap-utils" | ||
|
||
export interface V2ExporterOutput { | ||
type: CodapV2Component["type"] | ||
storage?: SetOptional<CodapV2ComponentStorage, "cannotClose" | "userSetTitle"> | ||
} | ||
|
||
export interface V2TileExportArgs { | ||
tile: ITileModel | ||
row?: IFreeTileRow | ||
sharedModelManager?: ISharedModelManager | ||
} | ||
export type V2TileExportFn = (args: V2TileExportArgs) => Maybe<V2ExporterOutput> | ||
|
||
// map from v2 component type to import function | ||
const gV2TileExporters = new Map<string, V2TileExportFn>() | ||
|
||
// register a v2 exporter for the specified tile type | ||
export function registerV2TileExporter(tileType: string, exportFn: V2TileExportFn) { | ||
gV2TileExporters.set(tileType, exportFn) | ||
} | ||
|
||
// export the specified v2 component using the appropriate registered exporter | ||
export function exportV2Component(args: V2TileExportArgs): Maybe<CodapV2Component> { | ||
const output = gV2TileExporters.get(args.tile.content.type)?.(args) | ||
if (!output) return | ||
|
||
const layout = args.row?.getTileLayout(args.tile.id) | ||
if (!isFreeTileLayout(layout)) return | ||
|
||
const id = toV2Id(args.tile.id) | ||
|
||
const tileWidth = layout.width ?? kDefaultTileWidth | ||
const tileHeight = layout.height ?? kDefaultTileHeight | ||
|
||
return { | ||
type: output.type, | ||
guid: id, | ||
id, | ||
componentStorage: { | ||
name: args.tile.name, | ||
title: args.tile._title, | ||
cannotClose: args.tile.cannotClose, | ||
// TODO_V2_EXPORT check this logic | ||
userSetTitle: !!args.tile._title && args.tile._title !== args.tile.name, | ||
// include the component-specific storage | ||
...output.storage | ||
}, | ||
layout: { | ||
width: tileWidth, | ||
height: layout.isMinimized ? kTitleBarHeight : tileHeight, | ||
left: layout.position.x, | ||
top: layout.position.y, | ||
isVisible: !layout.isHidden, | ||
zIndex: layout.zIndex | ||
}, | ||
savedHeight: layout.isMinimized ? tileHeight : null | ||
} as Maybe<CodapV2Component> | ||
} |
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,52 @@ | ||
import { kCalculatorTileType, kV2CalculatorDGType } from "../components/calculator/calculator-defs" | ||
import { createCodapDocument } from "../models/codap/create-codap-document" | ||
import { exportV2Document } from "./export-v2-document" | ||
import "../components/calculator/calculator-registration" | ||
|
||
jest.mock("../../build_number.json", () => ({ | ||
buildNumber: 1234 | ||
})) | ||
|
||
jest.mock("../../package.json", () => ({ | ||
version: "3.0.0-test" | ||
})) | ||
|
||
describe("exportV2Document", () => { | ||
it("exports an empty document", () => { | ||
const doc = createCodapDocument() | ||
const output = exportV2Document(doc) | ||
expect(output).toEqual({ | ||
type: "DG.Document", | ||
id: 1, | ||
guid: 1, | ||
name: doc.title, | ||
appName: "DG", | ||
appVersion: "3.0.0-test", | ||
appBuildNum: `${1234}`, | ||
metadata: {}, | ||
components: [], | ||
contexts: [], | ||
globalValues: [] | ||
}) | ||
}) | ||
|
||
it("exports a document with a single component", () => { | ||
const doc = createCodapDocument() | ||
doc.content?.insertTileSnapshotInDefaultRow({ content: { type: kCalculatorTileType }}) | ||
const { components, ...others } = exportV2Document(doc) | ||
expect(others).toEqual({ | ||
type: "DG.Document", | ||
id: 1, | ||
guid: 1, | ||
name: doc.title, | ||
appName: "DG", | ||
appVersion: "3.0.0-test", | ||
appBuildNum: `${1234}`, | ||
metadata: {}, | ||
contexts: [], | ||
globalValues: [] | ||
}) | ||
expect(components.length).toBe(1) | ||
expect(components[0].type).toBe(kV2CalculatorDGType) | ||
}) | ||
}) |
Oops, something went wrong.