Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add open with processing to examples #2

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/components/CopyButton.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.root {
position: absolute;
right: var(--gutter);
height: 32px;
padding: 6px 12px;
font-size: var(--text-small);
Expand Down
43 changes: 43 additions & 0 deletions src/components/OpenWithButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState } from 'react';
import classnames from 'classnames';

import ProcessingIcon from '../images/logo-processing.svg';

import * as css from './OpenWithButton.module.css';

const OpenWithButton = ({ pdes }) => {
const main = pdes[0]
let sketchURL = `pde://sketch/base64/${stringToBase64(main.code)}?pde=`
for (let pde of pdes) {
if (pde === main) continue
sketchURL += `${pde.name}:${stringToBase64(pde.code)},`
}
const [showInstructions, setShowInstructions] = useState(false)

return (
<a
href={sketchURL}
type="button"
className={classnames(css.root)}
onClick={() => setShowInstructions(true)}
>
<ProcessingIcon /> {'Open With Processing'}
{showInstructions && (
<div className={classnames(css.instructions)}>
<p>If this did not open Processing, make sure you have it installed and opened at least once.</p>
<p>Currently only versions of Processing later than 4.4 are supported</p>
<a href="https://www.processing.org/download/" target="_black">Download Processing</a>
</div>
)}
</a>
)
}

export default OpenWithButton;

function stringToBase64(str) {
if (typeof Buffer !== 'undefined') {
return Buffer.from(str).toString('base64');
}
return btoa(str)
}
53 changes: 53 additions & 0 deletions src/components/OpenWithButton.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.root {
height: 32px;
padding: 6px 12px;
font-size: var(--text-small);
font-weight: bold;
color: var(--darkgray);
cursor: pointer;
fill: var(--darkgray);
position: relative;

& svg {
height: 12px;
width: auto;
}
}

.root:hover {
color: var(--darkergray);
fill: var(--darkergray);
}

.root:active {
color: var(--processing-blue-mid);
fill: var(--processing-blue-mid);
}

.instructions {
bottom: 100%;
margin-bottom: 10px;
left: 50%;
position: absolute;
transform: translateX(-50%);
min-width: 16em;
background-color: #555;
color: #fff;
text-align: left;
border-radius: 6px;
padding: 8px 10px 10px 25px;
transition: opacity 0.3s;
a {
color: var(--processing-blue-mid);
}
&::before {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -10px;
border-width: 10px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
}
6 changes: 5 additions & 1 deletion src/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classnames from 'classnames';

import Button from './Button';
import CopyButton from './CopyButton';
import OpenWithButton from './OpenWithButton';

import { useHighlight } from '../hooks';
import { escapeHtml } from '../utils';
Expand Down Expand Up @@ -40,7 +41,10 @@ const Tabs = ({ pdes, className }) => {
[css.activeCode]: pde.name === active
})}
key={key}>
<CopyButton text={pde.code} />
<div className={css.actions}>
<CopyButton text={pde.code} />
<OpenWithButton pdes={pdes} />
</div>
<pre className={css.codeBlock}>
<code
dangerouslySetInnerHTML={{
Expand Down
5 changes: 5 additions & 0 deletions src/components/Tabs.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@
padding-bottom: var(--gutter-double);
white-space: break-spaces;
}

.actions{
position: absolute;
right: var(--gutter);
}