Skip to content
This repository was archived by the owner on Nov 11, 2025. It is now read-only.

Commit a55af5d

Browse files
committed
initial commit
0 parents  commit a55af5d

File tree

13 files changed

+2637
-0
lines changed

13 files changed

+2637
-0
lines changed

.github/workflows/release.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Create Release
2+
on:
3+
push:
4+
tags:
5+
- "*"
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout code
11+
uses: actions/checkout@v3
12+
13+
- name: Set up Node.js
14+
uses: actions/setup-node@v3
15+
with:
16+
node-version: 18.x
17+
cache: yarn
18+
19+
- name: Install dependencies
20+
run: yarn install --immutable
21+
22+
- name: Compile assets
23+
run: yarn build
24+
25+
- name: Create block tarballs
26+
run: node ./release.ts
27+
28+
- name: Release
29+
uses: softprops/action-gh-release@v1
30+
with:
31+
files: dist/*.tar.gz

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
.DS_Store
3+
dist-ssr
4+
*.local
5+
.yalc.lock
6+
.yalc
7+
dist

LICENSE.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2022 GitHub and contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# GitHub Blocks Template
2+
3+
Use this repository as a starter template for building your own Blocks.
4+
## Quickstart
5+
6+
> 🛑 Currently, you must be flagged into the [GitHub Blocks Technical Preview](https://blocks.githubnext.com) in order to develop blocks. There is no "offline" development mode at this time.
7+
8+
Fork this repo using the [`Use this template`](https://github.com/githubnext/blocks-template/generate) button above:
9+
10+
!["Use this template" button](https://user-images.githubusercontent.com/8978670/144893319-5d45ab5c-12c0-42b4-99f8-97f658deb03b.png)
11+
12+
Then, clone _your_ repo (not [this one!](https://github.com/githubnext/blocks-template)) and get ready for action:
13+
14+
```bash
15+
yarn # install dependencies
16+
yarn start # start the dev server
17+
# Or use npm, pnpm, you know the drill
18+
```
19+
20+
When you visit [localhost:4000](https://localhost:4000) in your browser, you'll be
21+
redirected to the Blocks app, but your locally-developed blocks will appear in the block picker:
22+
23+
<img alt="Block picker" src="https://user-images.githubusercontent.com/56439/181648955-101b6567-3f9b-44b3-af99-7ef3ca6161b9.png" width="418" />
24+
25+
(if you're using Safari (or another browser that doesn't permit calling `http` URLs from an `https` page), run `yarn start-https` and visit [https://localhost:4000](https://localhost:4000) instead.)
26+
27+
This template includes one example File Block and one Folder Block. The dev server supports hot reloading, so make some changes, and see what they do!
28+
29+
## Under the hood
30+
31+
Currently, Blocks are [React](https://reactjs.org/) components. They have a well-defined contract with their surroundings, and receive a [fixed set of props](https://github.com/githubnext/blocks/blob/main/docs/Developing%20blocks/4%20API%20reference%20and%20types.md) when they are instantiated. They are developed in [TypeScript](https://www.typescriptlang.org/), and bundled with [Vite](https://vitejs.dev/).
32+
33+
## More Info
34+
35+
Visit [githubnext/blocks](https://blocks.githubnext.com/githubnext/blocks) for a full tutorial, documentation, and examples.
36+
37+
You should also join us in our discord! There's a [#blocks channel](https://discord.com/channels/735557230698692749/1039950186136469535) where you can connect with us and other folks who are building Blocks:
38+
39+
> 👋 https://discord.gg/githubnext
40+
## License
41+
42+
MIT
43+
44+
✌️ ❤️
45+
_GitHub Next_

blocks.config.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"type": "file",
4+
"id": "scrim-file-block",
5+
"title": "Scrimba File Block",
6+
"description": "Record interactive code-casts inside Github",
7+
"entry": "blocks/scrimba-block.tsx",
8+
"matches": ["*"],
9+
"example_path": "https://github.com/imba/imba/README.md"
10+
},
11+
{
12+
"type": "folder",
13+
"id": "scrim-folder-block",
14+
"title": "Scrimba Folder Block",
15+
"description": "Record interactive code-casts inside Github",
16+
"entry": "blocks/scrimba-block.tsx",
17+
"matches": ["*"],
18+
"example_path": "https://github.com/imba/imba"
19+
}
20+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pre {
2+
background: papayawhip;
3+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { FileBlockProps, getLanguageFromFilename } from "@githubnext/blocks";
2+
import { Button, Box } from "@primer/react";
3+
import "./index.css";
4+
5+
export default function ExampleFileBlock(props: FileBlockProps) {
6+
const { context, content, metadata, onUpdateMetadata } = props;
7+
const language = Boolean(context.path)
8+
? getLanguageFromFilename(context.path)
9+
: "N/A";
10+
11+
return (
12+
<Box p={4}>
13+
<Box
14+
borderColor="border.default"
15+
borderWidth={1}
16+
borderStyle="solid"
17+
borderRadius={6}
18+
overflow="hidden"
19+
>
20+
<Box
21+
bg="canvas.subtle"
22+
p={3}
23+
borderBottomWidth={1}
24+
borderBottomStyle="solid"
25+
borderColor="border.default"
26+
>
27+
File: {context.path} {language}
28+
</Box>
29+
<Box p={4}>
30+
<p>Metadata example: this button has been clicked:</p>
31+
<Button
32+
onClick={() =>
33+
onUpdateMetadata({ number: (metadata.number || 0) + 1 })
34+
}
35+
>
36+
{metadata.number || 0} times
37+
</Button>
38+
<pre className="mt-3 p-3">{content}</pre>
39+
</Box>
40+
</Box>
41+
</Box>
42+
);
43+
}

blocks/scrimba-block.tsx

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { tw } from "twind";
2+
import { FolderBlockProps } from "@githubnext/blocks";
3+
import { useCallback, useEffect, useRef, useState } from "react";
4+
import "./style.css";
5+
import { Button, Text, Link } from "@primer/react";
6+
7+
const SCRIMBA_BASE_URL = import.meta.env.DEV
8+
? "https://dev.scrimba.com:3000"
9+
: "https://scrimba.com";
10+
const SCRIMBA_URL = `${SCRIMBA_BASE_URL}/new/htmlblocks`;
11+
12+
export default function (props: FolderBlockProps) {
13+
const { tree, context, onRequestGitHubData, files } = props;
14+
const [scrimMounted, setScrimMounted] = useState(false);
15+
const [scrimRecorded, setScrimRecorded] = useState("");
16+
17+
const frame = useRef(null);
18+
19+
const getFilesRecursive = useCallback(
20+
async (folderPath: string) => {
21+
const apiUrl = `/repos/${context.owner}/${context.repo}/contents/${folderPath}`;
22+
const folderContents = await onRequestGitHubData(apiUrl, {
23+
ref: context.sha,
24+
});
25+
26+
const filePromises = folderContents.map(async (item) => {
27+
if (item.type === "file") {
28+
const fileResponse = await fetch(item.download_url);
29+
return { path: item.path, content: await fileResponse.text() };
30+
} else if (item.type === "dir") {
31+
const subFolderFiles = await getFilesRecursive(item.path);
32+
return subFolderFiles;
33+
}
34+
});
35+
36+
let files = await Promise.all(filePromises)
37+
.then((files) => files.flat())
38+
.catch((error) => {
39+
console.error(
40+
`Failed to get contents of files in ${folderPath}`,
41+
error
42+
);
43+
return [];
44+
});
45+
46+
return files;
47+
},
48+
[context.repo, context.owner, context.sha]
49+
);
50+
51+
const getFiles = useCallback(
52+
async (all?: boolean) => {
53+
if (!tree && !all) {
54+
return [{ ...context, type: "blob", content: props.content }];
55+
}
56+
return getFilesRecursive(context.path);
57+
},
58+
[context.repo, context.owner, context.sha, files, tree, props.content]
59+
);
60+
61+
const sendData = useCallback(
62+
(files, other?: boolean) => {
63+
if (frame.current) {
64+
let payload = {
65+
files,
66+
title: `Walkthrough of ${context.path.split("/").slice(-1)[0]} in ${
67+
context.owner
68+
}/${context.repo}`,
69+
};
70+
if (other) payload = files;
71+
frame.current.contentWindow.postMessage(payload, "*");
72+
}
73+
},
74+
[frame, context.owner, context.repo, context.path]
75+
);
76+
77+
const listenToRecorded = useCallback(() => {
78+
window.addEventListener("message", (e) => {
79+
if (e.origin !== SCRIMBA_BASE_URL) return;
80+
if (e.data.event == "recorded") {
81+
setScrimRecorded(e.data.id);
82+
}
83+
});
84+
}, []);
85+
86+
const reload = useCallback(async () => {
87+
let files;
88+
if (!tree) {
89+
const parts = context.path.split("/");
90+
const path = parts[parts.length - 1];
91+
files = [{ path, content: props.content, type: "blob" }];
92+
} else {
93+
files = await getFiles();
94+
}
95+
sendData(files);
96+
setScrimMounted(true);
97+
listenToRecorded();
98+
}, [props.content, context.path]);
99+
100+
const init = useCallback(async () => {
101+
let files = await getFiles();
102+
window.addEventListener("message", (e) => {
103+
if (e.origin !== SCRIMBA_BASE_URL) return;
104+
105+
if (e.data.mounted) {
106+
setScrimMounted(true);
107+
if (!tree) {
108+
const parts = context.path.split("/");
109+
const path = parts[parts.length - 1];
110+
files = [{ path, content: props.content, type: "blob" }];
111+
}
112+
sendData(files);
113+
}
114+
});
115+
}, [getFiles, props.content, context.path, frame]);
116+
117+
const loadFiles = useCallback(async () => {
118+
if (!frame.current) return;
119+
const files = await getFiles(true);
120+
sendData(files);
121+
}, [getFiles, frame, context.folder]);
122+
123+
useEffect(() => {
124+
if (frame.current) {
125+
init();
126+
listenToRecorded();
127+
}
128+
}, [frame]);
129+
const publish = useCallback(() => {
130+
sendData({ action: "publish" }, true);
131+
}, [sendData]);
132+
133+
return (
134+
<div className={tw(`w-full h-full`)} id="example-block-code-block">
135+
<div className={tw(`flex w-full h-full overflow-x-hidden flex-col`)}>
136+
<div className={tw(`flex ai-center jc-between flex-row`)}>
137+
{scrimMounted ? null : (
138+
<Button onClick={reload}>Not working? Reload</Button>
139+
)}
140+
<Button onClick={loadFiles}>Load all files</Button>
141+
</div>
142+
{scrimRecorded ? (
143+
<Text>
144+
<Button onClick={publish} className={tw("inline-block")}>
145+
Publish the scrim{" "}
146+
</Button>
147+
to make it available for everyone at{" "}
148+
<Link
149+
href={`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`}
150+
>{`${SCRIMBA_BASE_URL}/scrim/${scrimRecorded}`}</Link>
151+
</Text>
152+
) : null}
153+
<iframe
154+
allow="microphone;"
155+
ref={frame}
156+
src={SCRIMBA_URL}
157+
className={tw`my-2 h-full w-full`}
158+
/>
159+
</div>
160+
</div>
161+
);
162+
}

blocks/style.css

Whitespace-only changes.

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "custom-block-template",
3+
"version": "0.0.0",
4+
"license": "MIT",
5+
"scripts": {
6+
"start": "blocks start",
7+
"start-https": "blocks start --https=true",
8+
"build": "blocks build"
9+
},
10+
"dependencies": {
11+
"@githubnext/blocks": "^2.3.5",
12+
"@primer/react": "^35.24.0",
13+
"react": "^18.1.0",
14+
"react-dom": "^18.1.0"
15+
}
16+
}

0 commit comments

Comments
 (0)