Skip to content

Commit

Permalink
useMemo
Browse files Browse the repository at this point in the history
  • Loading branch information
danielferro69 committed Jun 18, 2024
1 parent 8199f90 commit 72afa03
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/components/blockly/PBBlocklyWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BlockType, getBlockFromId } from "./blocks";
import { Toolbox, categorizedToolbox, setupBlockly, setupBlocklyBlocks, setXml, uncategorizedToolbox } from "./blockly";
import { PBCard } from "../PBCard";
import { Box, PaperProps, Typography } from "@mui/material";
import { useState } from "react";
import { useMemo, useState } from "react";
import Blockly from "blockly/core"
import { useThemeContext } from "../../theme/ThemeContext";

Expand All @@ -21,7 +21,7 @@ export type PBBlocklyWorkspaceProps = {
export const PBBlocklyWorkspace = ({ blockIds, categorized, sx, title, ...props }: PBBlocklyWorkspaceProps) => {
const { t } = useTranslation("blocks")

const { blocklyTheme, isSmallScreen } = useThemeContext()
const { blocklyTheme } = useThemeContext()

const [blocklyContainer, setBlocklyContainer] = useState<Element>()

Expand All @@ -31,9 +31,9 @@ export const PBBlocklyWorkspace = ({ blockIds, categorized, sx, title, ...props

setupBlocklyBlocks(t)

if (blocklyContainer) setupBlockly(blocklyContainer, { theme: blocklyTheme, toolbox, ...props.workspaceConfiguration })
if (blocklyContainer) setupBlockly(blocklyContainer, { theme: blocklyTheme, toolbox, ...props.workspaceConfiguration } )

if (blocklyContainer && props.initialXml) setXml(props.initialXml)
if (blocklyContainer && props.initialXml) setXml(props.initialXml )

return (
<PBCard sx={{ ...sx }}>
Expand Down
3 changes: 1 addition & 2 deletions src/components/blockly/blockly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ export const setupBlocklyBlocks = (t: (key: string) => string) => {
createCommonCode()
}

export const setXml = (xml: string) => {
export const setXml = (xml: string ) => {
Blockly.Xml.domToWorkspace(
Blockly.utils.xml.textToDom(xml),
Blockly.getMainWorkspace()
Expand All @@ -1483,6 +1483,5 @@ export const setXml = (xml: string) => {

export const setupBlockly = (container: Element, workspaceConfiguration: Blockly.BlocklyOptions) => {
container.replaceChildren() //Removes previous injection, otherwise it might keep inserting below the current workspace

Blockly.inject(container, workspaceConfiguration)
}
23 changes: 18 additions & 5 deletions src/components/challengeView/ChallengeView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useParams } from "react-router-dom"
import { Challenge, PathToChallenge, currentIdFor, getPathToChallenge } from "../../staticData/challenges";
import { useMediaQuery, PaperProps, Stack } from "@mui/material";
import { PaperProps, Stack } from "@mui/material";
import { StatementDescription } from "./StatementDescription";
import { EditableBlocklyWorkspace } from "./EditableBlocklyWorkspace";
import { InfoButton, SceneButtons, SceneButtonsVertical } from "./SceneButtons/SceneButtons";
Expand All @@ -11,7 +11,7 @@ import { Header } from "../header/Header"
import { Scene, SceneMap, SerializedChallenge } from "../serializedChallenge";
import { useTranslation } from "react-i18next";
import { useThemeContext } from "../../theme/ThemeContext";
import { useState } from "react";
import { useMemo, useState } from "react";
import { StatementTextToShow } from "../creator/Editor/MarkDownEdition/MarkdownEditor";
import { ChallengeBreadcrumb } from "./ChallengeBreadcrumb";

Expand Down Expand Up @@ -79,14 +79,20 @@ const ChallengeWorkspace = ({ statement, challenge, clue }: ChallengeWorkspacePr
categorized: challenge.toolboxStyle !== 'noCategories'
}

const vBlocklyArea = useMemo<JSX.Element>( () => {
return <VerticalChallengeWorkspace blocklyWorkspaceProps={blocklyWorkspaceProps} challenge={challenge} />}, [] );

const hBlocklyArea = useMemo<JSX.Element>( () => {
return <HorizontalChallengeWorkspace blocklyWorkspaceProps={blocklyWorkspaceProps} challenge={challenge} />}, [] );

return <>
<Stack flexGrow={1} direction='column' height='100%'>
<StatementDescription
text={descriptionOrClue}
setShowStatement={setToShow}
clueIsEnabled={clue !== ''}
urlImage={challenge.imageURL()} />
{isSmallScreen ? <VerticalChallengeWorkspace blocklyWorkspaceProps={blocklyWorkspaceProps} challenge={challenge} /> : <HorizontalChallengeWorkspace blocklyWorkspaceProps={blocklyWorkspaceProps} challenge={challenge} />}
{isSmallScreen ? vBlocklyArea : hBlocklyArea }
</Stack>
{!isSmallScreen ? <ChallengeFooter /> : <></>}
</>
Expand All @@ -103,8 +109,12 @@ type EditableBlocklyWorkspaceProps = {
}

const HorizontalChallengeWorkspace = ({ challenge, blocklyWorkspaceProps }: ChallengeWorkspaceDistributionProps) => {
const blocklyWorkspace = useMemo<JSX.Element>( () => {
return <EditableBlocklyWorkspace blockIds={blocklyWorkspaceProps.blockIds} categorized={blocklyWorkspaceProps.categorized} isVertical={false} />
},[])

return <Stack direction="row" flexWrap={"wrap"} flexGrow={1}>
<EditableBlocklyWorkspace blockIds={blocklyWorkspaceProps.blockIds} categorized={blocklyWorkspaceProps.categorized} isVertical={false} />
{blocklyWorkspace}
<Stack>
<SceneButtons />
<SceneView descriptor={challenge.sceneDescriptor} />
Expand All @@ -115,9 +125,12 @@ const HorizontalChallengeWorkspace = ({ challenge, blocklyWorkspaceProps }: Chal
const VerticalChallengeWorkspace = ({ challenge, blocklyWorkspaceProps }: ChallengeWorkspaceDistributionProps) => {

const [openDrawer, setOpenDrawer] = useState<boolean>(false)

const blocklyWorkspace = useMemo<JSX.Element>( () => {
return <EditableBlocklyWorkspace blockIds={blocklyWorkspaceProps.blockIds} categorized={blocklyWorkspaceProps.categorized} isVertical={true} /> }, [])

return <Stack flexWrap={"wrap"} flexGrow={1} >
<EditableBlocklyWorkspace blockIds={blocklyWorkspaceProps.blockIds} categorized={blocklyWorkspaceProps.categorized} isVertical={true} />
{blocklyWorkspace}
<Stack direction='row' marginBottom='5px' justifyContent='space-evenly'>
<SceneView descriptor={challenge.sceneDescriptor} />
<Stack margin='10px' justifyContent='space-between'>
Expand Down

0 comments on commit 72afa03

Please sign in to comment.