Skip to content

Commit 729f25e

Browse files
feat(editor): add beacon and taquito types and autocompletion
1 parent 923de00 commit 729f25e

File tree

7 files changed

+100
-39
lines changed

7 files changed

+100
-39
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ yarn-error.log*
2323
build-examples
2424
src/examples/*.js
2525

26-
/docs/
26+
/docs/
27+
/src/components/monaco-types.ts

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
"write-heading-ids": "docusaurus write-heading-ids",
1616
"pretty-source": "npx prettier --ignore-unknown --write 'src/**/*'",
1717
"pretty-docs": "npx prettier --ignore-unknown --write 'docs/**/*'",
18+
"generate-monaco-types": "node scripts/generate-monaco-types.js",
1819
"remove-folders": "rm -rf build-docs && rm -rf docs",
1920
"clear-folders": "npm run remove-folders && mkdir build-docs && mkdir docs",
20-
"embed-code": "npm run pretty-source && tsc src/examples/*.ts && node scripts/copy-examples.js && npm run clear-folders && cp -r src_docs/* build-docs/ && embedme 'build-docs/**/*.mdx' --source-root ./build-examples && rm -r docs && mv build-docs docs && npm run pretty-docs"
21+
"embed-code": "npm run generate-monaco-types && npm run pretty-source && tsc src/examples/*.ts && node scripts/copy-examples.js && npm run clear-folders && cp -r src_docs/* build-docs/ && embedme 'build-docs/**/*.mdx' --source-root ./build-examples && rm -r docs && mv build-docs docs && npm run pretty-docs"
2122
},
2223
"dependencies": {
2324
"@airgap/beacon-sdk": "^2.2.3",

scripts/generate-monaco-types.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require("fs");
2+
const getFilesRecursively = require("./get-files-in-folder");
3+
4+
const files = getFilesRecursively(
5+
"./node_modules/@airgap/beacon-sdk/dist/cjs/"
6+
).filter((file) => file.endsWith(".d.ts"));
7+
files.push(
8+
...getFilesRecursively("./node_modules/@taquito/").filter((file) =>
9+
file.endsWith(".d.ts")
10+
)
11+
);
12+
13+
const getFile = (filename) => {
14+
const text = fs.readFileSync(filename, { encoding: "utf8" });
15+
16+
return text;
17+
};
18+
19+
const outArray = files.map((dir) => ({
20+
name: dir
21+
.substring(13)
22+
.split("/dist/cjs/")
23+
.join("/")
24+
.split("/taquito/dist/types/")
25+
.join("/"),
26+
dts: getFile(dir),
27+
}));
28+
29+
fs.writeFileSync(
30+
"./src/components/monaco-types.ts",
31+
`export const libs = ${JSON.stringify(outArray)}`
32+
);

src/components/Monaco.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React, { Suspense, lazy } from "react";
22
import useThemeContext from "@theme/hooks/useThemeContext";
3+
import { libs } from "./monaco-types";
34

45
const MonacoEditor = lazy(() => import("react-monaco-editor"));
56

67
function Monaco(props) {
8+
let monacoRef;
79
const { isDarkTheme } = useThemeContext();
810

911
function onEditorWillMount(monaco) {
12+
monacoRef = monaco;
1013
const vsDarkTheme = {
1114
base: "vs-dark",
1215
inherit: true,
@@ -18,16 +21,41 @@ function Monaco(props) {
1821

1922
monaco.editor.defineTheme("vs-dark", vsDarkTheme);
2023

24+
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
25+
target: monaco.languages.typescript.ScriptTarget.ES2017,
26+
allowNonTsExtensions: true,
27+
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
28+
module: monaco.languages.typescript.ModuleKind.ESNext,
29+
typeRoots: ["node_modules/@types"],
30+
});
31+
32+
libs.forEach((lib) => {
33+
const MONACO_LIB_PREFIX = "file:///node_modules/";
34+
const path = `${MONACO_LIB_PREFIX}${lib.name}`;
35+
monaco.languages.typescript.typescriptDefaults.addExtraLib(lib.dts, path);
36+
});
37+
2138
if (props.editorWillMount) {
2239
props.editorWillMount(monaco);
2340
}
2441
}
2542

43+
function onEditorDidMount(editor) {
44+
editor.setModel(
45+
monacoRef.editor.createModel(
46+
props.value,
47+
"typescript",
48+
monacoRef.Uri.parse(`file:///main-${Math.random()}.ts`)
49+
)
50+
);
51+
}
52+
2653
return (
2754
<Suspense fallback={<div>Loading</div>}>
2855
<MonacoEditor
2956
{...props}
3057
editorWillMount={onEditorWillMount}
58+
editorDidMount={onEditorDidMount}
3159
theme={isDarkTheme ? "vs-dark" : "vs-light"}
3260
/>
3361
</Suspense>

src/examples/subscribe-to-event.beacon.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ async () => {
77
const dAppClient = new DAppClient({
88
name: "Beacon Docs",
99
});
10-
await dAppClient.setActiveAccount(undefined);
1110

12-
setTimeout(async () => {
13-
console.log(await dAppClient.getActiveAccount());
11+
// TODO: Remove temporary workaround in sandbox
12+
await new Promise((resolve) => setTimeout(resolve, 1000));
1413

15-
dAppClient.subscribeToEvent(BeaconEvent.PAIR_SUCCESS, (data) => {
16-
console.log(`${BeaconEvent.PAIR_SUCCESS} triggered: `, data);
17-
});
14+
await dAppClient.clearActiveAccount();
1815

19-
await dAppClient.requestPermissions();
20-
}, 1000);
16+
console.log(await dAppClient.getActiveAccount());
17+
18+
dAppClient.subscribeToEvent(BeaconEvent.PAIR_SUCCESS, (data) => {
19+
console.log(`${BeaconEvent.PAIR_SUCCESS} triggered: `, data);
20+
});
21+
22+
await dAppClient.requestPermissions();
2123
/// END
2224
};

src/pages/playground.tsx

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,35 @@ function Playground() {
9191
setExecutionState(ExecutionState.INIT);
9292
};
9393

94-
function handleSubmit(e) {
95-
e.preventDefault();
96-
execute();
97-
}
98-
9994
function handleClickShare() {
10095
copyShareUrl(input);
10196
}
10297

10398
return (
10499
<Layout title="Beacon" description="Beacon Playground" noFooter={true}>
105-
<div className={classnames(styles.headerContainer)}>
106-
<form
107-
onSubmit={handleSubmit}
108-
className={classnames(styles.argsInputContainer)}
100+
<div className={classnames(styles.runbox)}>
101+
<button
102+
onClick={execute}
103+
className="button button--primary margin-bottom--lg margin-right--xs"
104+
>
105+
Run Code
106+
</button>
107+
<button
108+
onClick={reset}
109+
className="button button--secondary margin-bottom--lg margin-right--xs"
110+
>
111+
Reset
112+
</button>
113+
<button
114+
onClick={clear}
115+
className="button button--secondary margin-bottom--lg margin-right--xs"
116+
>
117+
Clear Output
118+
</button>
119+
<button
120+
onClick={handleClickShare}
121+
className="button button--secondary margin-bottom--lg margin-right--xs"
109122
>
110-
<button
111-
onClick={execute}
112-
className={classnames(styles.argsIconContainer)}
113-
>
114-
Run Code
115-
</button>
116-
<button
117-
onClick={reset}
118-
className={classnames(styles.argsIconContainer)}
119-
>
120-
Reset
121-
</button>
122-
<button
123-
onClick={clear}
124-
className={classnames(styles.argsIconContainer)}
125-
>
126-
Clear Output
127-
</button>
128-
</form>
129-
<button onClick={handleClickShare}>
130123
Share Code (Copy to Clipboard)
131124
</button>
132125
</div>

src/pages/styles.module.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ body > div {
77
overflow: hidden;
88
}
99

10+
.runbox {
11+
margin: 10px;
12+
}
13+
1014
.headerContainer {
1115
display: flex;
1216
flex-wrap: wrap;

0 commit comments

Comments
 (0)