diff --git a/.gitignore b/.gitignore index 763301f..7a6df1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ dist/ -node_modules/ \ No newline at end of file +node_modules/ + +# munger.py typescript file output +demo/typescript-embeddable.py \ No newline at end of file diff --git a/munger.py b/munger.py index 48e5f8c..185ba50 100644 --- a/munger.py +++ b/munger.py @@ -10,8 +10,13 @@ dict.append(f"\"{row['id']}\":\"{text}\"") output = ",".join(dict) -opener = "const stringTable: { [key: string]: string } = {"; +opener = "export const stringTable: { [key: string]: string } = {"; ender = "};" + +# create a TS file that exports expected values +tsCompiledOutput = open('typescript-embeddable.ts', 'w+') +tsCompiledOutput.write(f'{opener}{output}{ender}') +tsCompiledOutput.write('\n\n') print(f'{opener}{output}{ender}') hex = [] @@ -20,6 +25,10 @@ hex.append("0x" + byte.hex()) output = ",".join(hex) -opener = "const data = Uint8Array.from(["; +opener = " export const data = Uint8Array.from(["; ender = "]);"; +tsCompiledOutput.write(f'{opener}{output}{ender}') print(f'{opener}{output}{ender}') + +# close written typescript file +tsCompiledOutput.close() diff --git a/readme.md b/readme.md index 047f9f3..d1ce08a 100644 --- a/readme.md +++ b/readme.md @@ -57,7 +57,7 @@ If you wanted to replace the story you would need to do the following: 1. Using ysc compile your yarn into a yarnc and csv file: `ysc compile your_yarn.yarn` 2. Using `munger.py` create a TypeScript text embeddable version of your compiled story: `python munger.py` -3. Replace the `stringTable` and `data` variables inside `index.ts` with those created by `munger.py` +3. Replace the `stringTable` and `data` variables inside `index.ts` with those created by `munger.py`. This step can alternatively be skipped as the `munger.py` output is generated in a file `demo/typescript-embeddable.ts` which is imported into `src/index.ts` by default. 5. Compile the TypeScript and run webpack to munge everything up into a single js file we can use: `npm run build` 6. Open `index.html` in your browser and "enjoy" diff --git a/src/index.ts b/src/index.ts index 1baa9fd..41fed1f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,13 @@ import { YarnVM, OptionItem } from "./yarnvm"; import { Program } from "./yarn_spinner"; +import { stringTable as mungedStringTable, data as mungedProgramData } from "../demo/typescript-embeddable"; + import "./yarnspinner.scss"; import 'bootstrap'; import { Settings, LineDeliveryMode } from "./settings"; +import { stringTable } from "./data"; let currentSettings: Settings = { lineDelivery: LineDeliveryMode.OneAtATime, @@ -211,9 +214,23 @@ declare global { startDialogue: () => void; setup: () => void; addButton: (text: string, classes: string[], handler: () => void) => void; + yarnData: {programData: Uint8Array, stringTable: StringTable}; } } +/** + * These values may either be replaced with the output value from munger.py + * or they will simply be imported from the `demo/typescript-embeddable.ts` + * file that munger.py generates. + * + * if replacing: + * `mungedProgramData` is replaced with "data" value from munger.py output + * `mungedStringTable` is replaced with "stringTable" value from munger.py + */ +window.yarnData = { + programData: mungedProgramData, + stringTable: mungedStringTable +} window.loadProgram = loadProgram; window.startDialogue = startDialogue; @@ -312,7 +329,6 @@ function loadProgram(programData: Uint8Array, stringTable: StringTable): void { } } - function updateSettings(newSettings: Settings) { currentSettings = { ...currentSettings, ...newSettings }; updateLineDeliveryUI();