Skip to content

Commit

Permalink
moving blockly files
Browse files Browse the repository at this point in the history
  • Loading branch information
tfloxolodeiro committed Sep 4, 2023
1 parent 80f7524 commit dfad233
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useLocation } from 'react-router-dom';
import ReactGA from "react-ga4";
import { CreatorViewMode } from './components/creator/Editor/CreatorViewMode';
import { useThemeContext } from './theme/ThemeContext';
import { BlocklyPrueba } from './components/BlocklyPrueba';
import { ToolboxPreview } from './components/creator/Editor/ChallengeDetailsEdition/ToolboxPreview';

const AnalyticsComponent = () => {
const location = useLocation();
Expand Down Expand Up @@ -84,7 +84,7 @@ const router = createHashRouter([{
},
{
path: "/blocklyPrueba",
element: <BlocklyPrueba/>
element: <ToolboxPreview/>
}
]}]);

Expand Down
73 changes: 73 additions & 0 deletions src/blockly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Blockly from "blockly"
import { BlockType } from "./components/blocks"

type BlocklyBlockDefinition = {
message0: string
args0: any[]
colour: string
previousStatement?: boolean
nextStatement?: boolean
}

type Toolbox = { kind: "categoryToolbox" | "flyoutToolbox", contents: ToolboxItem[]}


type ToolboxItem = ToolboxBlock | ToolBoxCategory
type ToolboxBlock = {kind: "block", type: string}
type ToolBoxCategory = {kind: "category", name: string, contents: ToolboxItem[]}

const blockTypeToToolboxBlock = (block: BlockType): ToolboxBlock => ({kind: "block", type: block.id})

const createPrimitiveBlock = (id: string, message: string, icon?: string) => {
const colour = '#4a6cd4'

const jsonInit: BlocklyBlockDefinition = {
message0: message,
colour,
args0: [],
previousStatement: true,
nextStatement: true
}

if(icon) {
jsonInit.message0 = `%1 ${message}`
jsonInit.args0.push({
"type": "field_image",
"src": `imagenes/iconos/${icon}`,
"width": 16,
"height": 16,
"alt": "*"
})
}

Blockly.Blocks[id] = {
init: function () {
this.jsonInit(jsonInit)
}}
}


export const categoryToolboxFromBlocks = (blocks: BlockType[]): Toolbox => ({
kind: "categoryToolbox",
contents: [
{
kind: "category",
name: "Primitivas",
contents: blocks.filter(block => block.categoryId === "primitives").map(blockTypeToToolboxBlock)
}
]
})

export const uncategorizedToolboxFromBlocks = (blocks: BlockType[]): Toolbox => ({
kind: "flyoutToolbox",
contents: blocks.map(blockTypeToToolboxBlock)
})

export const setupBlocklyBlocks = (t: (key: string) => string) => {

createPrimitiveBlock("MoverACasillaArriba", t("blocks.blocks.moveUp"), "icono.arriba.png")
createPrimitiveBlock("MoverACasillaAbajo", t("blocks.blocks.moveDown"), "icono.abajo.png")
createPrimitiveBlock("MoverACasillaIzquierda", t("blocks.blocks.moveLeft"), "icono.izquierda.png")
createPrimitiveBlock("MoverACasillaDerecha", t("blocks.blocks.moveRight"), "icono.derecha.png")

}
113 changes: 0 additions & 113 deletions src/components/BlocklyPrueba.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import "./prueba.css";
import { BlocklyWorkspace } from "react-blockly";
import Blockly from "blockly";
import { useTranslation } from "react-i18next";
import { BlockType } from "../../../blocks";
import { categoryToolboxFromBlocks, setupBlocklyBlocks } from "../../../../blockly";


export const ToolboxPreview = () => {
const {t} = useTranslation()

setupBlocklyBlocks(t)

const toolboxBlocks: BlockType[] = [
{
id: 'MoverACasillaDerecha',
intlId: 'moveRight',
categoryId: 'primitives'
},
{
id: 'MoverACasillaIzquierda',
intlId: 'moveLeft',
categoryId: 'primitives'
},
{
id: 'MoverACasillaArriba',
intlId: 'moveUp',
categoryId: 'primitives'
},
{
id: 'MoverACasillaAbajo',
intlId: 'moveDown',
categoryId: 'primitives'
},
]

return (
<>
<div style={{ height: "600px", width: "800px"}}>
<BlocklyWorkspace
toolboxConfiguration={categoryToolboxFromBlocks(toolboxBlocks)}
className="fill-height"
workspaceConfiguration={{trashcan:false, scrollbars: false}}
onWorkspaceChange={() => {Blockly.getMainWorkspace().clear()}}
/>
</div>
</>
);
};
File renamed without changes.

0 comments on commit dfad233

Please sign in to comment.