frontispiece / apps / frontispiece-editor / src / bootstrap / bootstrap.ts
bootstrap.ts
Raw
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import htm from "htm"
import { h } from "preact"
import {
	createInkHookState,
	InkHookStateControllerType,
	UseInkHookStateType,
} from "@a-morphous/frontispiece-ink-hookstate"
import { Compiler } from "inkjs"
import { CompilerOptions } from "inkjs/compiler/CompilerOptions"
import { BGColorCommand } from "../commands/bg-command"
import { ClearCommand } from "../commands/clear-command"
import { MusicCommand } from "../commands/music-command"
import { SFXCommand } from "../commands/sfx-command"
import { StopMusicCommand } from "../commands/stop-music-command"

export const html = htm.bind(h)

export let InkHookStateController: InkHookStateControllerType
export let useInkHookState: UseInkHookStateType

type AugmentedWindow = typeof window & {
	engine: InkHookStateControllerType
}

export const setupStory = async (storyURL: string) => {
	const response = await fetch(storyURL)
	const inkText = await response.text()
	const story = new Compiler(inkText, new CompilerOptions(undefined, undefined, true)).Compile()
	const ink = createInkHookState(story, {
		commandIdentifier: ">> ",
	})

	InkHookStateController = ink.InkHookStateController
	useInkHookState = ink.useInkHookState

	ink.InkHookStateController.registerCommand("bg", BGColorCommand)
	ink.InkHookStateController.registerCommand("clear", ClearCommand)

	ink.InkHookStateController.registerCommand("music", MusicCommand)
	ink.InkHookStateController.registerCommand("sfx", SFXCommand)
	ink.InkHookStateController.registerCommand("stop_music", StopMusicCommand)

	const augmentedWindow = window as AugmentedWindow
	augmentedWindow.engine = ink.InkHookStateController
}