Skip to content

Commit

Permalink
Merge pull request #62 from githubnext/aw/edit-and-preview
Browse files Browse the repository at this point in the history
feat: add edit-and-preview block
  • Loading branch information
Wattenberger authored Jul 18, 2022
2 parents adaab1d + 5701223 commit 1448d26
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
dist-ssr
*.local
.yalc
10 changes: 10 additions & 0 deletions blocks.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@
"matches": ["*"],
"example_path": "https://github.com/githubnext/blocks-tutorial/blob/main/processing-sketch.js"
},
{
"type": "file",
"id": "edit-and-preview-block",
"title": "Edit with Preview",
"description": "Edit code side-by-side with a preview",
"sandbox": false,
"entry": "blocks/file-blocks/edit-and-preview/index.tsx",
"matches": ["*"],
"example_path": "https://github.com/githubnext/blocks-tutorial/blob/main/README.md"
},
{
"type": "folder",
"id": "minimap-block",
Expand Down
84 changes: 84 additions & 0 deletions blocks/file-blocks/edit-and-preview/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
Block,
BlockPicker,
BlocksRepo,
FileBlockProps,
} from "@githubnext/blocks";
import { Box } from "@primer/react";
import { useEffect, useState } from "react";

export default function (props: FileBlockProps) {
const { metadata, BlockComponent, context, onRequestBlocksRepos } = props;

const [panes, setPanes] = useState<Block[]>(metadata?.panes || []);

const setDefaultBlockOptions = async () => {
const blockRepoOptions = await onRequestBlocksRepos(context.path);
const blockOptions = blockRepoOptions.reduce(
(acc: Block[], repo: BlocksRepo) => {
return [...acc, ...repo.blocks];
},
[]
);
setPanes(blockOptions.slice(0, 2));
};
useEffect(() => {
// default to first two relevant Blocks
if (panes.length) return;
setDefaultBlockOptions();
}, [context.path]);

return (
<Box
display="grid"
gridTemplateColumns={`repeat(${panes.length}, 1fr)`}
gridGap={2}
height="100%"
width="100%"
overflow="hidden"
p={2}
relative
>
{panes.map((pane, index) => (
<Box
display="flex"
flex="1"
flexDirection="column"
borderColor="border.default"
borderWidth={1}
borderStyle="solid"
overflow="hidden"
key={index}
>
<Box
flex="none"
p={1}
backgroundColor="pageHeaderBg"
borderColor="border.default"
borderBottomWidth={1}
borderBottomStyle="solid"
>
<BlockPicker
value={pane}
onChange={(pane) => {
let newPanes = [...panes];
newPanes[index] = pane;
setPanes(newPanes);
}}
onRequestBlocksRepos={onRequestBlocksRepos}
/>
</Box>
<Box flex="1" overflow="auto">
{BlockComponent ? (
<BlockComponent context={context} block={pane} />
) : (
<Box p={4} color="fg.subtle" fontStyle="italic">
No BlockComponent
</Box>
)}
</Box>
</Box>
))}
</Box>
);
}

0 comments on commit 1448d26

Please sign in to comment.