-
Notifications
You must be signed in to change notification settings - Fork 20
/
gatsby-browser.js
71 lines (63 loc) · 2.05 KB
/
gatsby-browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Implement Gatsby's Browser APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/browser-apis/
*/
// You can delete this file if you're not using it
import './src/styles/global.css';
import 'prismjs/themes/prism-okaidia.css';
export const onClientEntry = () => {
window.onload = () => {
brython({ debug: 1, indexedDB: false, pythonpath: '/' });
};
};
import React from 'react';
import { Provider } from 'jotai';
import { BeaconProvider } from './src/context/beacon-context';
export const wrapRootElement = ({ element }) => {
return (
<BeaconProvider>
<Provider>{element}</Provider>
</BeaconProvider>
);
};
const addScript = url => {
const script = document.createElement('script');
script.src = url;
script.async = true;
script.type = 'text/javascript';
script.charSet = 'utf-8';
document.body.appendChild(script);
};
export const onRouteUpdate = ({ location, prevLocation }) => {
/*
1. Check if the URL contains 'module-02' or 'module-03'
2. If the URL has the keywords, check if SmartPy script is added or not.
3. If SmartPy script isn't added -> add the necessary scripts.
*/
if (
location.pathname.indexOf('module-02') != -1 ||
location.pathname.indexOf('module-03') != -1
) {
// checking is '/smartpyio.py' script is added or not.
const scripts = Array.from(document.querySelectorAll('script'));
const srcVals = scripts
.filter(sc => typeof sc.attributes.src != 'undefined')
.map(sc => sc.attributes.src.value);
const hasSmartpy = srcVals.indexOf('/smartpyio.py');
if (hasSmartpy == -1) {
// adding the necessary scripts
addScript('/execute.js');
addScript('/smartjs/smart.js');
addScript('/smartjs/smartmljs.bc.js');
const script = document.createElement('script');
script.src = '/smartpyio.py';
script.type = 'text/python';
document.body.appendChild(script);
if (typeof window.brython != 'undefined') {
// Initialize brython
brython({ debug: 1, indexedDB: false, pythonpath: '/' });
}
}
}
};