Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrredo committed Aug 25, 2024
1 parent 0bdbacb commit 2e8cb91
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 676 deletions.
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 5 additions & 12 deletions src/lib/blocks/Javascript/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const blocks: BlockDefinition[] = [
helpUrl:
"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT",
code: (args) => {
console.log(args)
let code = `if(${args.if_input === "" ? "false" : args.if_input}) {\n${args.if}\n}`;
const ifInputs = args.if_input_list as string[];
const ifStatementInputs = args.if_statement_list as string[];
Expand Down Expand Up @@ -96,22 +95,16 @@ const blocks: BlockDefinition[] = [
// return `${args.A} ${args.CONDITION} ${args.B}`;

code: (args, block) => {
block.colour = rgbToHex(255, 128, 165);
block.outputType = BlockType.Any;
block.addInput(new Dropdown("bob", DropdownType.Auto, { bob: "hello", alex: "nikola" }));
block.addInput(new ValueInput("chicken", BlockType.Any));
block.addInput(new NumberInput("numberrr", 50, { max: 100, min: 50, precision: 10 }));
block.addInput(new TextInput("textttt", "I am a text input!"));
console.log("Args (code prop parameter): ", args);
return `${args.textttt}`;

return (args.A == '' || args.B == '')? `false ${args.CONDITION} false` : `${args.A} ${args.CONDITION} ${args.B}`;
}
},
{
id: "and_or",
text: "{A} {CONDITION} {B}",
args: [
new ValueInput("A", BlockType.Boolean),
new DropdownInput("CONDITION", DropdownType.Auto, { and: "&&", or: "||" }),
new Dropdown("CONDITION", DropdownType.Auto, { and: "&&", or: "||" }),
new ValueInput("B", BlockType.Boolean)
],
warnings: [
Expand Down Expand Up @@ -152,7 +145,7 @@ const blocks: BlockDefinition[] = [
id: "booleans",
text: "{INPUT}",
args: [
new DropdownInput("INPUT", DropdownType.Auto, {
new Dropdown("INPUT", DropdownType.Auto, {
true: "true",
false: "false",
//undefined: "undefined"
Expand Down Expand Up @@ -226,7 +219,7 @@ const blocks: BlockDefinition[] = [
text: "type {TYPE}",
args: [
// new ValueInput("OPERAND", BlockType.Any),
new DropdownInput("TYPE", DropdownType.Auto, {
new Dropdown("TYPE", DropdownType.Auto, {
string: "string",
number: "number",
boolean: "boolean",
Expand Down
61 changes: 32 additions & 29 deletions src/lib/types/BlockDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,39 @@ import {FlyoutButton} from "blockly";
export type Argument = BaseInput<any>;

export type BlockDefinition =
| {
id: string; // This is the "type" of the block
label?: false; // To see if the definition is a label or not
text: string; // This is "message0"
output?: BlockType;
shape: BlockShape; // The block shape
args?: Argument[]; // This is "args0"
warnings?: Warning[];
placeholders?: Placeholder<unknown>[];
inline: boolean; // This is "inputsInline"
colour: string;
tooltip: string;
helpUrl: string;
code: (args: Record<string, string | string[]>, block: Block) => string;

mutator?: Mutator;
hidden?: boolean;
imports?: `${string}@${string}`[];
}
| {
label: true;
text: string;
} |
{
kind: "button";
text: string;
callbackKey: string;
callback: (p1: FlyoutButton) => void;
};
| BlockBlockDefinition
| LabelBlockDefinition
| ButtonBlockDefinition;
export interface BlockBlockDefinition {
id: string; // This is the "type" of the block
kind?: null;
label?: false; // To see if the definition is a label or not
text: string; // This is "message0"
output?: BlockType;
shape: BlockShape; // The block shape
args?: Argument[]; // This is "args0"
warnings?: Warning[];
placeholders?: Placeholder<unknown>[];
inline: boolean; // This is "inputsInline"
colour: string;
tooltip: string;
helpUrl: string;
code: (args: Record<string, string | string[]>, block: Block) => string;

mutator?: Mutator;
hidden?: boolean;
imports?: `${string}@${string}`[];
}
export interface LabelBlockDefinition {
label: true;
text: string;
}
export interface ButtonBlockDefinition {
kind: "button";
text: string;
callbackKey: string;
callback: (p1: FlyoutButton) => void;
}
export interface MutatorBlock {
// What inputs it adds to the block
adds: Argument[];
Expand Down
71 changes: 37 additions & 34 deletions src/lib/utils/BlockGen/Blocks/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import pkg from "blockly/javascript";
import type {
Argument,
AssemblerMutator,
BlockBlockDefinition,
BlockDefinition,
CheckBoxMutatorBlock,
MutatorBlock
Expand Down Expand Up @@ -47,7 +48,6 @@ interface BlocklyBlockDefinition {

const { javascriptGenerator, Order } = pkg;

I'm making a concise list of rules that each block must follow.
export default class Block {
private _blockDefinition: BlockDefinition;
private _block!: Blockly.Block;
Expand All @@ -66,23 +66,26 @@ export default class Block {
}

public addWarning(warning: Warning): void {
if (this._blockDefinition.label) throw new Error("Cannot add a warning to a label");
const blockDefinition = this._blockDefinition as BlockBlockDefinition;
if (blockDefinition.label) throw new Error("Cannot add a warning to a label");
if (warningsObj[this._block.id] && warningsObj[this._block.id][warning.data.fieldName]) return;
this._blockDefinition.warnings = this._blockDefinition.warnings
? [...this._blockDefinition.warnings, warning]
blockDefinition.warnings = blockDefinition.warnings
? [...blockDefinition.warnings, warning]
: [warning];
}

public removeWarning(fieldName: string): void {
if (this._blockDefinition.kind) throw new Error("Cannot remove a warning from a input/button");
if (this._blockDefinition.label) throw new Error("Cannot remove a warning from a label");
const blockDefinition = this._blockDefinition as BlockBlockDefinition;

if (blockDefinition.kind) throw new Error("Cannot remove a warning from a input/button");
if (blockDefinition.label) throw new Error("Cannot remove a warning from a label");
if (
(!warningsObj[this._block.id] || !warningsObj[this._block.id][fieldName]) &&
this._blockDefinition.warnings !== undefined
blockDefinition.warnings !== undefined
) {
return;
}
this._blockDefinition.warnings = this._blockDefinition.warnings?.filter(
blockDefinition.warnings = blockDefinition.warnings?.filter(
(warning) => warning.data.fieldName !== fieldName
);
}
Expand Down Expand Up @@ -210,50 +213,52 @@ export default class Block {
}

generate(): void {
if (this._blockDefinition.label || this._blockDefinition.kind) return;
const blockDefinition = this._blockDefinition as BlockBlockDefinition;

if (blockDefinition.label || blockDefinition.kind) return;

// eslint-disable-next-line @typescript-eslint/no-this-alias
const blockClass = this; // Used because `this` is overwritten in the blockly functions.



const code = this._blockDefinition.code;
const shape = this._blockDefinition.shape;
const output = this._blockDefinition.output;
const importName = this._blockDefinition.imports;
const mutatorName: string = this._blockDefinition.mutator
? this._blockDefinition.id + salt(5)
const code = blockDefinition.code;
const shape = blockDefinition.shape;
const output = blockDefinition.output;
const importName = blockDefinition.imports;
const mutatorName: string = blockDefinition.mutator
? blockDefinition.id + salt(5)
: "";

const blockDef: BlocklyBlockDefinition = {
type: this._blockDefinition.id,
colour: this._blockDefinition.colour,
tooltip: this._blockDefinition.tooltip,
helpUrl: this._blockDefinition.helpUrl,
inputsInline: this._blockDefinition.inline,
type: blockDefinition.id,
colour: blockDefinition.colour,
tooltip: blockDefinition.tooltip,
helpUrl: blockDefinition.helpUrl,
inputsInline: blockDefinition.inline,
args0: [],
message0: "",
mutator: mutatorName == "" ? undefined : mutatorName
};

blockClass._blocklyDefinition = blockDef;

if (this._blockDefinition.mutator) {
this._blockDefinition.mutator.registerMutator(mutatorName);
if (blockDefinition.mutator) {
blockDefinition.mutator.registerMutator(mutatorName);
}

// Converts the classes into usable objects for the block definition
this._blockDefinition.args?.forEach((arg: Argument) => {
blockDefinition.args?.forEach((arg: Argument) => {
blockDef.args0.push(arg.generate() as Record<string, string>);
});

// Converts the raw text into a blockly valid "message0" with this format: "text %1 other text %2"
let counter: number = 1;
blockDef.message0 = this._blockDefinition.text.replace(/\{.*?\}/g, () => `%${counter++}`);
blockDef.message0 = blockDefinition.text.replace(/\{.*?\}/g, () => `%${counter++}`);

if (Blockly.Blocks[blockDef.type] !== undefined && !dev) {
throw Error(`Block "${blockDef.type}" is defined twice.`);
}
//const blockDefinition = this._blockDefinition;
//const blockDefinition = blockDefinition;
const BlockClass = this;
// Add The block to the blocks list
Blockly.Blocks[blockDef.type] = {
Expand Down Expand Up @@ -310,9 +315,9 @@ export default class Block {
!this.isInFlyout &&
block.id == this.id
) {
const warnings = blockClass._blockDefinition.label
const warnings = blockDefinition.label
? undefined
: blockClass._blockDefinition.warnings;
: blockDefinition.warnings;
if (!warnings) return;

const topParent = this.getRootBlock();
Expand All @@ -327,8 +332,7 @@ export default class Block {
const { warningType, message, fieldName } = warning.data;
if(this.getInput(fieldName)) {
console.log(BlockClass.handleWarning(warning.data, resultMessage, topParent))
resultMessage I'm making a concise list of rules that each block must follow.
= BlockClass.handleWarning(warning.data, resultMessage, topParent)
resultMessage = BlockClass.handleWarning(warning.data, resultMessage, topParent)
}
let input = this.getInput(fieldName + "1")
//handles mutator input warnings
Expand All @@ -353,19 +357,18 @@ export default class Block {
if (warningsObj[this.id] && Object.keys(warningsObj[this.id]).length === 0) {
delete warningsObj[this.id];
}
console.log(resultMessage)
this.setWarningText(resultMessage);
}
});
}
};
const properties = this._blockDefinition.mutator?.properties;
const properties = blockDefinition.mutator?.properties;
const propertyMap: Record<string, MutatorBlock> = {};
if (properties) {
for (const property of properties) {
if (this._blockDefinition.mutator?.type === MutatorType.Assembler) {
if (blockDefinition.mutator?.type === MutatorType.Assembler) {
propertyMap[(property as AssemblerMutator).block] = property;
} else if (this._blockDefinition.mutator?.type === MutatorType.Checkbox) {
} else if (blockDefinition.mutator?.type === MutatorType.Checkbox) {
propertyMap[(property as CheckBoxMutatorBlock).inputName] = property;
}
}
Expand Down
Loading

0 comments on commit 2e8cb91

Please sign in to comment.