Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#Custom block: how to get data from parent component with Constructor #96

Closed
orlenkoo opened this issue Oct 26, 2022 · 2 comments
Closed
Labels
help wanted Extra attention is needed

Comments

@orlenkoo
Copy link

orlenkoo commented Oct 26, 2022

Hello,
Hope you are doing well..
I want to get data when create new custom block in app.component.ts in example.block.ts.

Here is the code:

  • app.component.ts
import {
  Category,
  CustomBlock,
  LOGIC_CATEGORY,
  LOOP_CATEGORY,
  MATH_CATEGORY,
  NgxBlocklyConfig,
  NgxBlocklyGenerator,
  NgxBlocklyToolbox,
  Blockly
} from "ngx-blockly";

import { ExampleBlock } from "./blocks/example.block";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  title = "CodeSandbox";
  public config: NgxBlocklyConfig = {
    trashcan: true,
    generators: [
      NgxBlocklyGenerator.DART,
      NgxBlocklyGenerator.LUA,
      NgxBlocklyGenerator.JAVASCRIPT,
      NgxBlocklyGenerator.PHP,
      NgxBlocklyGenerator.PYTHON,
      NgxBlocklyGenerator.XML
    ],
    defaultBlocks: true,
    move: {
      scrollbars: true,
      wheel: true
    }
  };

  public customBlocks: CustomBlock[] = [new ExampleBlock([{key: "test"}])];


  constructor() {
    const workspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
    const toolbox: NgxBlocklyToolbox = new NgxBlocklyToolbox(workspace);
    toolbox.nodes = [
      LOGIC_CATEGORY,
      new Category("bla", "#ff0000", [
        ...this.customBlocks,
      ]),
      LOOP_CATEGORY,
      MATH_CATEGORY,
    ];
    this.config.toolbox = toolbox.toXML();
  }
}
  • example.block.ts

export class ExampleBlock extends CustomBlock {
  constructor(args: any) {
    super("example_block");
    this.class = ExampleBlock;
    // this.args = [{key:"test"}];
  }

  public defineBlock() {
    this.block.appendDummyInput().appendField(this.args[0].key);
  }

  public toPythonCode(block: Blockly.Block): string | any[] {
    return "";
  }
}

I have created new example block with args: [{key:"test"}] like:

public customBlocks: CustomBlock[] = [new ExampleBlock([{key: "test"}])];

I want to get this args in example.block.ts so that I can use for appendField like.

public defineBlock() { this.block.appendDummyInput().appendField(this.args[0].key); }

Can you help?

@orlenkoo orlenkoo changed the title Custom block: how to get data from parent component Custom block: how to get data from parent component with Constructor Oct 26, 2022
@orlenkoo orlenkoo changed the title Custom block: how to get data from parent component with Constructor #Custom block: how to get data from parent component with Constructor Oct 26, 2022
@roroettg roroettg added the help wanted Extra attention is needed label Oct 26, 2022
@roroettg
Copy link
Owner

Hi,

you could do it like this. args is already defined in a parent class.

public customBlocks: CustomBlock[] = [new ExampleBlock(null, null, [{key: "test"}])];
export class ExampleBlock extends CustomBlock {
  constructor(type: string, blockMutator?: BlockMutator, ...args: any[]) {
    super("example_block");
    this.class = ExampleBlock;
  }

  public defineBlock() {
    this.block.appendDummyInput().appendField(this.args[0].key);
  }

  public toPythonCode(block: Blockly.Block): string | any[] {
    return "";
  }
}

@orlenkoo
Copy link
Author

Yeah, it worked..
Thanks very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants