-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add block editor components and context
- Loading branch information
1 parent
ee12367
commit 241effc
Showing
9 changed files
with
190 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use client"; | ||
|
||
import { useBlockEditor } from "@/contexts/BlockEditorContext"; | ||
import { FunctionEditor } from "./FunctionEditor"; | ||
import { IfEditor } from "./IfEditor"; | ||
import { WhileEditor } from "./WhileEditor"; | ||
|
||
export const BlockEditor = () => { | ||
const { currentBlock } = useBlockEditor(); | ||
|
||
if (!currentBlock) return <EmptyBlockEditor />; | ||
|
||
return ( | ||
<div className="flex flex-1 w-full pb-4"> | ||
{currentBlock.blockType === "functionCall" && ( | ||
<FunctionEditor block={currentBlock} /> | ||
)} | ||
{currentBlock.blockType === "if" && <IfEditor block={currentBlock} />} | ||
{currentBlock.blockType === "while" && ( | ||
<WhileEditor block={currentBlock} /> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
const EmptyBlockEditor = () => { | ||
return ( | ||
<div> | ||
<p className="text-center">Select a block to edit</p> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { FunctionCallBlock } from "@ozhanefe/ts-codegenerator"; | ||
|
||
export const FunctionEditor: React.FC<{ block: FunctionCallBlock }> = ({ | ||
block, | ||
}) => { | ||
const { functionInfo } = block; | ||
return ( | ||
<div className=""> | ||
Editing: <span className="font-bold">{functionInfo.name}</span> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IfBlock } from "@ozhanefe/ts-codegenerator"; | ||
|
||
export const IfEditor: React.FC<{ block: IfBlock }> = ({ block }) => { | ||
return ( | ||
<div className=""> | ||
Editing: <span className="font-bold">if({block.condition})</span> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { WhileLoopBlock } from "@ozhanefe/ts-codegenerator"; | ||
|
||
export const WhileEditor: React.FC<{ block: WhileLoopBlock }> = ({ block }) => { | ||
return ( | ||
<div className=""> | ||
Editing: <span className="font-bold">while({block.condition})</span> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./BlockEditor"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
createContext, | ||
Dispatch, | ||
PropsWithChildren, | ||
SetStateAction, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
import { CodeBlock } from "@ozhanefe/ts-codegenerator"; | ||
|
||
interface Context { | ||
currentBlock: CodeBlock | null; | ||
setCurrentBlock: Dispatch<SetStateAction<CodeBlock | null>>; | ||
} | ||
|
||
const BlockEditorContext = createContext<Context>({ | ||
currentBlock: null, | ||
setCurrentBlock: () => {}, | ||
}); | ||
|
||
export const BlockEditorProvider = ({ children }: PropsWithChildren<{}>) => { | ||
const [currentBlock, setCurrentBlock] = useState<CodeBlock | null>(null); | ||
|
||
useEffect(() => { | ||
console.log("currentBlock:", currentBlock); | ||
}, [currentBlock]); | ||
|
||
return ( | ||
<BlockEditorContext.Provider | ||
value={{ | ||
currentBlock, | ||
setCurrentBlock, | ||
}} | ||
> | ||
{children} | ||
</BlockEditorContext.Provider> | ||
); | ||
}; | ||
|
||
export const useBlockEditor = () => { | ||
return useContext(BlockEditorContext); | ||
}; |