forked from processing/processing-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenWithButton.js
63 lines (54 loc) · 2.32 KB
/
OpenWithButton.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
import React, { useState, useEffect } from 'react';
import classnames from 'classnames';
import ProcessingIcon from '../images/logo-processing.svg';
import * as css from './OpenWithButton.module.css';
const OpenWithButton = ({ pdes, dataFiles }) => {
const main = pdes[0]
const [sketchURL, setSketchURL] = useState(`processing://open?sketch=${stringToBase64(main.code)}`)
useEffect(() => {
let sketchURL = `pde://sketch/base64/${stringToBase64(main.code)}?pde=`
for (let pde of pdes) {
if (pde === main) continue
sketchURL += `${pde.name}:${stringToBase64(pde.code)},`
}
if (dataFiles.length > 0) {
let data = '&data=' + dataFiles.map(file => file.relativePath.split('/').pop() + ":" + window.location.protocol + "//" + window.location.host + file.publicURL).join(',')
sketchURL += data
}
setSketchURL(sketchURL)
}, [])
const [showInstructions, setShowInstructions] = useState(false)
useEffect(() => {
const handleClickOutside = (event) => {
if (showInstructions && !event.target.closest(`.${css.root}`)) {
setShowInstructions(false);
}
};
document.addEventListener('click', handleClickOutside);
return () => document.removeEventListener('click', handleClickOutside);
}, [showInstructions]);
return (
<a
href={sketchURL}
type="button"
className={classnames(css.root)}
onClick={() => setShowInstructions(true)}
>
<ProcessingIcon /> {'Open In Processing'}
{showInstructions && (
<div className={classnames(css.instructions)}>
<h1>Opening Processing<span className={css.ellipsis}></span></h1>
<p>If nothing happens, <a href="https://www.processing.org/download/" target="_black">Download Processing</a> version 4.4.1 or later and try again.</p>
<p className={classnames(css.tooltipFootnote)}>Make sure Processing is installed and was opened at least once.</p>
</div>
)}
</a>
)
}
export default OpenWithButton;
function stringToBase64(str) {
if (typeof Buffer !== 'undefined') {
return Buffer.from(str).toString('base64');
}
return btoa(str)
}