Skip to content

Commit d79cb88

Browse files
authored
fix: add custom blocks back to sample-apps (#2374)
1 parent d387b9e commit d79cb88

7 files changed

Lines changed: 39 additions & 25 deletions

File tree

examples/sample-app-ts/src/blocks/text.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ import * as Blockly from 'blockly/core';
1212
// own custom blocks.
1313
const addText = {
1414
type: 'add_text',
15-
message0: 'Add text %1 with color %2',
15+
message0: 'Add text %1',
1616
args0: [
1717
{
1818
type: 'input_value',
1919
name: 'TEXT',
2020
check: 'String',
2121
},
22-
{
23-
type: 'input_value',
24-
name: 'COLOR',
25-
check: 'Colour',
26-
},
2722
],
2823
previousStatement: null,
2924
nextStatement: null,

examples/sample-app-ts/src/generators/javascript.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,18 @@ forBlock['add_text'] = function (
1717
generator: Blockly.CodeGenerator,
1818
) {
1919
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
20-
const color =
21-
generator.valueToCode(block, 'COLOR', Order.ATOMIC) || "'#ffffff'";
22-
2320
const addText = generator.provideFunction_(
2421
'addText',
25-
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text, color) {
22+
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text) {
2623
2724
// Add text to the output area.
2825
const outputDiv = document.getElementById('output');
2926
const textEl = document.createElement('p');
3027
textEl.innerText = text;
31-
textEl.style.color = color;
3228
outputDiv.appendChild(textEl);
3329
}`,
3430
);
3531
// Generate the function call for this block.
36-
const code = `${addText}(${text}, ${color});\n`;
32+
const code = `${addText}(${text});\n`;
3733
return code;
3834
};

examples/sample-app-ts/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ Object.assign(javascriptGenerator.forBlock, forBlock);
2020
const codeDiv = document.getElementById('generatedCode')?.firstChild;
2121
const outputDiv = document.getElementById('output');
2222
const blocklyDiv = document.getElementById('blocklyDiv');
23-
const ws = blocklyDiv && Blockly.inject(blocklyDiv, {toolbox});
23+
24+
if (!blocklyDiv) {
25+
throw new Error(`div with id 'blocklyDiv' not found`);
26+
}
27+
const ws = Blockly.inject(blocklyDiv, {toolbox});
2428

2529
// This function resets the code and output divs, shows the
2630
// generated code from the workspace, and evals the code.

examples/sample-app-ts/src/toolbox.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ export const toolbox = {
491491
},
492492
},
493493
},
494+
{
495+
kind: 'block',
496+
type: 'add_text',
497+
inputs: {
498+
TEXT: {
499+
shadow: {
500+
type: 'text',
501+
fields: {
502+
TEXT: 'abc',
503+
},
504+
},
505+
},
506+
},
507+
},
494508
],
495509
},
496510
{

examples/sample-app/src/blocks/text.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ import * as Blockly from 'blockly/core';
1212
// own custom blocks.
1313
const addText = {
1414
type: 'add_text',
15-
message0: 'Add text %1 with color %2',
15+
message0: 'Add text %1',
1616
args0: [
1717
{
1818
type: 'input_value',
1919
name: 'TEXT',
2020
check: 'String',
2121
},
22-
{
23-
type: 'input_value',
24-
name: 'COLOR',
25-
check: 'Colour',
26-
},
2722
],
2823
previousStatement: null,
2924
nextStatement: null,

examples/sample-app/src/generators/javascript.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@ export const forBlock = Object.create(null);
1313

1414
forBlock['add_text'] = function (block, generator) {
1515
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
16-
const color =
17-
generator.valueToCode(block, 'COLOR', Order.ATOMIC) || "'#ffffff'";
18-
1916
const addText = generator.provideFunction_(
2017
'addText',
21-
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text, color) {
18+
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text) {
2219
2320
// Add text to the output area.
2421
const outputDiv = document.getElementById('output');
2522
const textEl = document.createElement('p');
2623
textEl.innerText = text;
27-
textEl.style.color = color;
2824
outputDiv.appendChild(textEl);
2925
}`,
3026
);
3127
// Generate the function call for this block.
32-
const code = `${addText}(${text}, ${color});\n`;
28+
const code = `${addText}(${text});\n`;
3329
return code;
3430
};

examples/sample-app/src/toolbox.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ export const toolbox = {
491491
},
492492
},
493493
},
494+
{
495+
kind: 'block',
496+
type: 'add_text',
497+
inputs: {
498+
TEXT: {
499+
shadow: {
500+
type: 'text',
501+
fields: {
502+
TEXT: 'abc',
503+
},
504+
},
505+
},
506+
},
507+
},
494508
],
495509
},
496510
{

0 commit comments

Comments
 (0)