Skip to content

Commit

Permalink
Strictify toScratchblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
adroitwhiz committed Feb 28, 2023
1 parent 1d4017b commit fd36f26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Sprite, Stage } from "./Target";
import fromSb3, { fromSb3JSON } from "./io/sb3/fromSb3";
import toSb3, { ToSb3Options } from "./io/sb3/toSb3";
import toLeopard from "./io/leopard/toLeopard";
import toScratchblocks from "./io/scratchblocks/toScratchblocks";
import toScratchblocks, { ToScratchblocksOptions } from "./io/scratchblocks/toScratchblocks";

export type TextToSpeechLanguage =
| "ar"
Expand Down Expand Up @@ -36,7 +36,8 @@ export default class Project {

public toSb3: (options?: Partial<ToSb3Options>) => ReturnType<typeof toSb3> = toSb3.bind(null, this);
public toLeopard: typeof toLeopard = toLeopard.bind(this);
public toScratchblocks: typeof toScratchblocks = toScratchblocks.bind(this);
public toScratchblocks: (options?: Partial<ToScratchblocksOptions>) => ReturnType<typeof toScratchblocks> =
toScratchblocks.bind(null, this);

public stage: Stage = new Stage();
public sprites: Sprite[] = [];
Expand Down
29 changes: 17 additions & 12 deletions src/io/scratchblocks/toScratchblocks.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import Project from "../../Project";
import Script from "../../Script";
import Target from "../../Target";
import Block from "../../Block";
import Block, { KnownBlock } from "../../Block";
import * as BlockInput from "../../BlockInput";
import { OpCode } from "../../OpCode";

interface ToScratchblocksOptions {
export interface ToScratchblocksOptions {
indent: string;
}

export default function toScratchblocks(options: Partial<ToScratchblocksOptions> = {}): {
export default function toScratchblocks(
project: Project,
options: Partial<ToScratchblocksOptions> = {}
): {
[targetName: string]: string;
} {
const project: Project = this;

const defaultOptions: ToScratchblocksOptions = {
indent: "\t"
};
options = { ...defaultOptions, ...options };
const fullOptions = { ...defaultOptions, ...options };

function indent(str: string): string {
return str
.split("\n")
.map((l) => options.indent + l)
.map((l) => fullOptions.indent + l)
.join("\n");
}

Expand Down Expand Up @@ -51,11 +52,11 @@ export default function toScratchblocks(options: Partial<ToScratchblocksOptions>
case "soundEffect":
case "currentMenu":
case "greaterThanMenu": {
const value =
const value = inp.value === "PAN" || inp.value === "DAYOFWEEK" ?
{
PAN: "pan left/right",
DAYOFWEEK: "day of week"
}[inp.value] || (inp.value || "").toLowerCase();
}[inp.value] : (inp.value || "").toLowerCase();
return `[${escape(value)} v]`;
}

Expand Down Expand Up @@ -110,7 +111,8 @@ export default function toScratchblocks(options: Partial<ToScratchblocksOptions>
}

case "color": {
const hex = (k: string): string => (inp.value || { r: 0, g: 0, b: 0 })[k].toString(16).padStart(2, "0");
const hex = (k: "r" | "g" | "b"): string =>
(inp.value || { r: 0, g: 0, b: 0 })[k].toString(16).padStart(2, "0");
return `[#${hex("r") + hex("g") + hex("b")}]`;
}

Expand Down Expand Up @@ -151,7 +153,10 @@ export default function toScratchblocks(options: Partial<ToScratchblocksOptions>
throw new Error("expected target");
}

const i = (key: string, ...args): string => input(block.inputs[key], target, ...args);
type BlockInputKeys<T> = T extends T ? keyof T : never;
const i = (key: BlockInputKeys<KnownBlock["inputs"]>, flag?: boolean): string =>
// TODO: type this function properly and remove this type assertion
input((block.inputs as never)[key], target, flag);
const operator = (op: string): string => `(${i("NUM1")} ${op} ${i("NUM2")})`;
const boolop = (op: string, flag = false): string => {
if (flag) {
Expand Down Expand Up @@ -517,7 +522,7 @@ export default function toScratchblocks(options: Partial<ToScratchblocksOptions>
case OpCode.music_setInstrument:
return `set instrument to ${i("INSTRUMENT")}`;
case OpCode.music_midiSetInstrument:
return `set (old midi) instrument to ${i("INSTRUENT")} :: music`;
return `set (old midi) instrument to ${i("INSTRUMENT")} :: music`;
case OpCode.music_setTempo:
return `set tempo to ${i("TEMPO")}`;
case OpCode.music_changeTempo:
Expand Down

0 comments on commit fd36f26

Please sign in to comment.