-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (112 loc) · 3.8 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { setProperties, getNodesByType, modifyNodesByName } from 'idyll-ast';
import { getVars } from 'idyll-document/dist/cjs/utils/index'
import fs from 'fs';
const hashCode = (s) => {
return "" + Math.abs(s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a }, 0));
}
const buildComponent = (contents, id, vars) => {
const jsURL = 'https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js';
return `
const React = require('react');
const select = require('d3-selection').select;
class p5js${id} extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
const jsId = 'idyll-p5js-minified';
if (
document &&
!document.getElementById(jsId) &&
!select('script[src = "${jsURL}"]').size()
) {
const heads = document.getElementsByTagName('head');
if (heads.length) {
const head = heads[0];
const link = document.createElement('script');
link.src = '${jsURL}';
head.appendChild(link);
}
}
const heads = document.getElementsByTagName('head');
if (heads.length) {
const head = heads[0];
const script = document.createElement('script');
script.id = 'p5js${id}';
const { hasError, updateProps, idyll, children, ...props } = this.props;
const stringifiedProps = JSON.stringify(props);
const inlineScript = \`
const idyllProps = \${stringifiedProps};
function defer(method) {
if (window.p5) {
method();
} else {
setTimeout(()=> { defer(method) }, 50);
}
}
const createSketch = () => {
const sketch${id} = (p) => {
for(const [key,value] of Object.entries(idyllProps)){
eval(\\\`
window.\\\${key} = \\\${value};
\\\`);
}
(function(){
eval(\\\`
${contents}
p.setup = setup;
p.draw = draw;
\\\`)
}).call(p);
}
let myp5${id} = new p5(sketch${id}, 'idyll-container-p5-${id}');
};
defer(createSketch);
\`;
script.appendChild(document.createTextNode(inlineScript));
head.appendChild(script);
}
}
shouldComponentUpdate(nextProps) {
const { hasError, updateProps, idyll, children, ...props } = nextProps;
for(const [key,value] of Object.entries(props)) {
eval(\`
window.\${key} = \${value};
\`);
}
return false;
}
render() {
return (
<span id='idyll-container-p5-${id}'>
</span>
)
}
}
module.exports = p5js${id};
`
};
module.exports = ast => {
const vars = getNodesByType(ast, 'var');
const idyllVars = vars.reduce((acc, node) => {
const { properties: { name: { value } } } = node;
acc[`_${value}_`] = { type: "variable", value: value };
return acc;
}, {});
return modifyNodesByName(ast, 'codehighlight', (node) => {
if (node.properties.language && node.properties.language.value.startsWith('exec:')) {
const language = node.properties.language.value.replace('exec:', '');
if (['p5', 'p5js'].indexOf(language.toLowerCase()) > -1) {
const contents = node.children[0].value;
contents.split('\n')
const id = hashCode(contents);
const fileName = `components/p5js${id}.js`;
fs.writeFileSync(fileName, buildComponent(contents, id, vars));
node.name = `p5js${id}`;
node.properties = { ...idyllVars };
node.children = [];
}
}
return node;
})
};