react-native-blockly 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +91 -0
- package/lib/module/classes/blockly.js +24 -0
- package/lib/module/classes/blockly.js.map +1 -0
- package/lib/module/classes/blocklyViewConfig.js +39 -0
- package/lib/module/classes/blocklyViewConfig.js.map +1 -0
- package/lib/module/index.js +60 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/parsers/parseToString.js +86 -0
- package/lib/module/parsers/parseToString.js.map +1 -0
- package/lib/module/parsers/stringToBlockly.js +87 -0
- package/lib/module/parsers/stringToBlockly.js.map +1 -0
- package/lib/module/types/blockly-types.d.js +4 -0
- package/lib/module/types/blockly-types.d.js.map +1 -0
- package/lib/module/types/blockly-view-types.d.js +4 -0
- package/lib/module/types/blockly-view-types.d.js.map +1 -0
- package/lib/module/utils/blocklyConstants.js +36 -0
- package/lib/module/utils/blocklyConstants.js.map +1 -0
- package/lib/module/utils/defaults.js +20 -0
- package/lib/module/utils/defaults.js.map +1 -0
- package/lib/module/utils/showWarnings.js +75 -0
- package/lib/module/utils/showWarnings.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/classes/blockly.d.ts +16 -0
- package/lib/typescript/src/classes/blockly.d.ts.map +1 -0
- package/lib/typescript/src/classes/blocklyViewConfig.d.ts +14 -0
- package/lib/typescript/src/classes/blocklyViewConfig.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +16 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/parsers/parseToString.d.ts +47 -0
- package/lib/typescript/src/parsers/parseToString.d.ts.map +1 -0
- package/lib/typescript/src/parsers/stringToBlockly.d.ts +14 -0
- package/lib/typescript/src/parsers/stringToBlockly.d.ts.map +1 -0
- package/lib/typescript/src/utils/blocklyConstants.d.ts +17 -0
- package/lib/typescript/src/utils/blocklyConstants.d.ts.map +1 -0
- package/lib/typescript/src/utils/defaults.d.ts +10 -0
- package/lib/typescript/src/utils/defaults.d.ts.map +1 -0
- package/lib/typescript/src/utils/showWarnings.d.ts +15 -0
- package/lib/typescript/src/utils/showWarnings.d.ts.map +1 -0
- package/package.json +155 -0
- package/src/classes/blockly.ts +25 -0
- package/src/classes/blocklyViewConfig.ts +36 -0
- package/src/index.tsx +65 -0
- package/src/parsers/parseToString.ts +92 -0
- package/src/parsers/stringToBlockly.ts +89 -0
- package/src/types/blockly-types.d.ts +28 -0
- package/src/types/blockly-view-types.d.ts +89 -0
- package/src/utils/blocklyConstants.ts +92 -0
- package/src/utils/defaults.ts +19 -0
- package/src/utils/showWarnings.ts +93 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Blockly } from '../classes/blockly';
|
|
2
|
+
import type { BlocklyViewConfig } from '../classes/blocklyViewConfig';
|
|
3
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
4
|
+
import type { CSSProperties } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for the run code button
|
|
8
|
+
*/
|
|
9
|
+
export interface ButtonConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Text to display on the button
|
|
12
|
+
* @default 'Run Code'
|
|
13
|
+
*/
|
|
14
|
+
text?: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* CSS styles for the button as an object
|
|
18
|
+
* @example { backgroundColor: 'red', color: 'white', padding: '10px' }
|
|
19
|
+
*/
|
|
20
|
+
style?: CSSProperties;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Props for the BlocklyView component
|
|
25
|
+
*/
|
|
26
|
+
export type BlocklyViewProps = {
|
|
27
|
+
/**
|
|
28
|
+
* Instance of the Blockly class that manages block definitions and logic.
|
|
29
|
+
* Contains all the custom blocks that will be available in the workspace.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const blockly = new Blockly('MyBlockly');
|
|
34
|
+
* blockly.createBlock({
|
|
35
|
+
* type: 'custom_block',
|
|
36
|
+
* message0: 'Custom Block',
|
|
37
|
+
* colour: 230
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
Blockly: Blockly;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Instance of the BlocklyViewConfig class that manages UI configuration.
|
|
45
|
+
* Handles toolbox layout, workspace settings, and visual appearance.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const config = new BlocklyViewConfig('MyConfig');
|
|
50
|
+
* config.setToolbox({
|
|
51
|
+
* kind: 'flyoutToolbox',
|
|
52
|
+
* contents: [{ kind: 'block', type: 'custom_block' }]
|
|
53
|
+
* });
|
|
54
|
+
* config.setWorkspace({ scrollbars: true, trashcan: true });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
Config: BlocklyViewConfig;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Optional callback function that receives messages from the Blockly WebView.
|
|
61
|
+
* Called whenever the WebView sends data using `window.ReactNativeWebView.postMessage()`.
|
|
62
|
+
*
|
|
63
|
+
* @param data - The message data sent from the WebView as a string
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const handleMessage = (data: string) => {
|
|
68
|
+
* console.log('Message from Blockly:', data);
|
|
69
|
+
* };
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
onMessage?: (data: string) => void;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Optional style prop for the WebView container.
|
|
76
|
+
* Allows customization of the BlocklyView's dimensions and layout.
|
|
77
|
+
* Defaults to `{ flex: 1 }` if not provided.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* <BlocklyView
|
|
82
|
+
* Blockly={blockly}
|
|
83
|
+
* Config={config}
|
|
84
|
+
* style={{ flex: 1, backgroundColor: '#f0f0f0' }}
|
|
85
|
+
* />
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
style?: StyleProp<ViewStyle>;
|
|
89
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Array of standard Blockly block types that are available by default.
|
|
3
|
+
* These blocks are built into the Blockly library and don't need to be
|
|
4
|
+
* defined in the custom blocks array.
|
|
5
|
+
*/
|
|
6
|
+
export const STANDARD_BLOCKLY_BLOCK_TYPES = [
|
|
7
|
+
// Logic blocks
|
|
8
|
+
'logic_compare',
|
|
9
|
+
'logic_operation',
|
|
10
|
+
'logic_negate',
|
|
11
|
+
'logic_boolean',
|
|
12
|
+
'logic_null',
|
|
13
|
+
'logic_ternary',
|
|
14
|
+
// Control flow blocks
|
|
15
|
+
'controls_if',
|
|
16
|
+
'controls_ifelse',
|
|
17
|
+
'controls_repeat_ext',
|
|
18
|
+
'controls_whileUntil',
|
|
19
|
+
'controls_for',
|
|
20
|
+
'controls_forEach',
|
|
21
|
+
'controls_flow_statements',
|
|
22
|
+
// Math blocks
|
|
23
|
+
'math_number',
|
|
24
|
+
'math_arithmetic',
|
|
25
|
+
'math_single',
|
|
26
|
+
'math_trig',
|
|
27
|
+
'math_constant',
|
|
28
|
+
'math_number_property',
|
|
29
|
+
'math_round',
|
|
30
|
+
'math_on_list',
|
|
31
|
+
'math_modulo',
|
|
32
|
+
'math_constrain',
|
|
33
|
+
'math_random_int',
|
|
34
|
+
'math_random_float',
|
|
35
|
+
'math_atan2',
|
|
36
|
+
// Text blocks
|
|
37
|
+
'text',
|
|
38
|
+
'text_multiline',
|
|
39
|
+
'text_join',
|
|
40
|
+
'text_append',
|
|
41
|
+
'text_length',
|
|
42
|
+
'text_isEmpty',
|
|
43
|
+
'text_indexOf',
|
|
44
|
+
'text_charAt',
|
|
45
|
+
'text_getSubstring',
|
|
46
|
+
'text_changeCase',
|
|
47
|
+
'text_trim',
|
|
48
|
+
'text_count',
|
|
49
|
+
'text_replace',
|
|
50
|
+
'text_reverse',
|
|
51
|
+
'text_print',
|
|
52
|
+
'text_prompt_ext',
|
|
53
|
+
// List blocks
|
|
54
|
+
'lists_create_with',
|
|
55
|
+
'lists_create_empty',
|
|
56
|
+
'lists_repeat',
|
|
57
|
+
'lists_length',
|
|
58
|
+
'lists_isEmpty',
|
|
59
|
+
'lists_indexOf',
|
|
60
|
+
'lists_getIndex',
|
|
61
|
+
'lists_setIndex',
|
|
62
|
+
'lists_getSublist',
|
|
63
|
+
'lists_split',
|
|
64
|
+
'lists_sort',
|
|
65
|
+
'lists_reverse',
|
|
66
|
+
// Color blocks
|
|
67
|
+
'colour_picker',
|
|
68
|
+
'colour_random',
|
|
69
|
+
'colour_rgb',
|
|
70
|
+
'colour_blend',
|
|
71
|
+
// Variable blocks
|
|
72
|
+
'variables_get',
|
|
73
|
+
'variables_set',
|
|
74
|
+
// Function/Procedure blocks
|
|
75
|
+
'procedures_defnoreturn',
|
|
76
|
+
'procedures_defreturn',
|
|
77
|
+
'procedures_callnoreturn',
|
|
78
|
+
'procedures_callreturn',
|
|
79
|
+
'procedures_ifreturn',
|
|
80
|
+
] as const;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Set of standard Blockly block types for efficient lookup.
|
|
84
|
+
* Used for runtime validation.
|
|
85
|
+
*/
|
|
86
|
+
export const STANDARD_BLOCKLY_BLOCKS = new Set(STANDARD_BLOCKLY_BLOCK_TYPES);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Type representing all standard Blockly block types.
|
|
90
|
+
* Derived from the constant array to ensure type and runtime values stay in sync.
|
|
91
|
+
*/
|
|
92
|
+
export type StandardBlockType = (typeof STANDARD_BLOCKLY_BLOCK_TYPES)[number];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default text for the run code button
|
|
5
|
+
*/
|
|
6
|
+
export const DEFAULT_BUTTON_TEXT = 'Run Code';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Default CSS styles for the run code button
|
|
10
|
+
*/
|
|
11
|
+
export const DEFAULT_BUTTON_STYLE: CSSProperties = {
|
|
12
|
+
marginTop: '15px',
|
|
13
|
+
padding: '10px 20px',
|
|
14
|
+
backgroundColor: '#007bff',
|
|
15
|
+
color: 'white',
|
|
16
|
+
border: 'none',
|
|
17
|
+
borderRadius: '5px',
|
|
18
|
+
cursor: 'pointer',
|
|
19
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Block, Toolbox } from '../types/blockly-types';
|
|
2
|
+
import {
|
|
3
|
+
STANDARD_BLOCKLY_BLOCKS,
|
|
4
|
+
type StandardBlockType,
|
|
5
|
+
} from './blocklyConstants';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validates if blocks and toolbox have matching pairs and shows warnings for mismatches
|
|
9
|
+
*
|
|
10
|
+
* @param blocks - Array of block definitions
|
|
11
|
+
* @param toolbox - Toolbox configuration
|
|
12
|
+
*/
|
|
13
|
+
export function blockInToolbox(blocks: Block[], toolbox: Toolbox | null): void {
|
|
14
|
+
const warnings: string[] = [];
|
|
15
|
+
const missingInToolbox: string[] = [];
|
|
16
|
+
const missingInBlocks: string[] = [];
|
|
17
|
+
|
|
18
|
+
// If no toolbox is provided, show warning and return
|
|
19
|
+
if (!toolbox) {
|
|
20
|
+
console.warn(
|
|
21
|
+
'No toolbox provided. Blocks will not be accessible in the workspace.'
|
|
22
|
+
);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Extract block types from blocks array
|
|
27
|
+
const blockTypes = new Set(blocks.map((block) => block.type));
|
|
28
|
+
|
|
29
|
+
// Extract block types from toolbox contents
|
|
30
|
+
const toolboxTypes = new Set(
|
|
31
|
+
toolbox.contents
|
|
32
|
+
.filter((item) => item.kind === 'block')
|
|
33
|
+
.map((item) => item.type)
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// Check for blocks that are defined but not in toolbox
|
|
37
|
+
blockTypes.forEach((blockType) => {
|
|
38
|
+
if (!toolboxTypes.has(blockType)) {
|
|
39
|
+
missingInToolbox.push(blockType);
|
|
40
|
+
warnings.push(
|
|
41
|
+
`Block '${blockType}' is defined but not available in the toolbox. Users won't be able to access this block.`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Check for toolbox items that reference undefined blocks
|
|
47
|
+
// Exclude standard Blockly blocks from this check
|
|
48
|
+
toolboxTypes.forEach((toolboxType) => {
|
|
49
|
+
if (
|
|
50
|
+
!blockTypes.has(toolboxType) &&
|
|
51
|
+
!STANDARD_BLOCKLY_BLOCKS.has(toolboxType as StandardBlockType)
|
|
52
|
+
) {
|
|
53
|
+
missingInBlocks.push(toolboxType);
|
|
54
|
+
warnings.push(
|
|
55
|
+
`Toolbox references block '${toolboxType}' but this block is not defined. This will cause runtime errors.`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Show warnings if any issues found
|
|
61
|
+
if (warnings.length > 0) {
|
|
62
|
+
console.warn('Block/Toolbox validation issues found:');
|
|
63
|
+
warnings.forEach((warning) => console.warn(warning));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Validates if blocks with custom code have a return statement and shows warnings for missing returns
|
|
69
|
+
*
|
|
70
|
+
* @param blocks - Array of block definitions
|
|
71
|
+
*/
|
|
72
|
+
export function noReturnStatement(blocks: Block[]): void {
|
|
73
|
+
const warnings: string[] = [];
|
|
74
|
+
|
|
75
|
+
// Check each block that has custom code
|
|
76
|
+
blocks.forEach((block) => {
|
|
77
|
+
if (block.code && typeof block.code === 'string') {
|
|
78
|
+
// Check if the code contains a return statement
|
|
79
|
+
const hasReturnStatement = /\breturn\b/.test(block.code);
|
|
80
|
+
|
|
81
|
+
if (!hasReturnStatement) {
|
|
82
|
+
warnings.push(
|
|
83
|
+
`Block '${block.type}' has custom code but no return statement. This may cause unexpected behavior in code generation.`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (warnings.length > 0) {
|
|
90
|
+
console.warn('Block code validation issues found:');
|
|
91
|
+
warnings.forEach((warning) => console.warn(warning));
|
|
92
|
+
}
|
|
93
|
+
}
|