Skip to content

Commit afdf467

Browse files
committed
Merge pull request #47 from toolness/base-sketch-url
Add data-base-url option.
2 parents 43abeeb + 35f9abf commit afdf467

File tree

7 files changed

+54
-4
lines changed

7 files changed

+54
-4
lines changed

index.html

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h2>Table of Contents</h2>
2424
<li><a href="#error-handling">Error Handling</a>
2525
<li><a href="#resizing">Resizing</a>
2626
<li><a href="#specifying-p5-version">Specifying The p5 Version</a>
27+
<li><a href="#base-url">Specifying The Base URL</a>
2728
</ol>
2829

2930
<h2 id="quick-start">Quick Start</h2>
@@ -176,6 +177,31 @@ <h2 id="specifying-p5-version">Specifying The p5 Version</h2>
176177
<a href="https://cdnjs.com/libraries/p5.js">cdnjs.com/libraries/p5.js</a>.
177178
</p>
178179

180+
<h2 id="base-url">Specifying The Base URL</h2>
181+
182+
<p>
183+
By default, the widget will set the base URL of the sketch to
184+
the URL of the page embedding the widget. This means that if
185+
you're embedding this widget at
186+
<code>http://example.org/index.html</code> and have the following
187+
code in your sketch:
188+
</p>
189+
190+
<pre><code class="language-javascript">loadJSON('foo.json');</code></pre>
191+
192+
<p>
193+
The actual URL retrieved will be
194+
<code>http://example.org/foo.json</code>.
195+
</p>
196+
197+
<p>
198+
If you'd like to override this default, you can specify a
199+
custom base URL with the <code>data-base-url</code>
200+
attribute like so:
201+
</p>
202+
203+
<pre><code class="language-markup">&lt;script type="text/p5" data-base-url="http://some-other-website.com/"&gt;</code></pre>
204+
179205
<script src="static/vendor/jquery.js"></script>
180206
<script src="p5-widget.js" data-manual></script>
181207
<script src="static/vendor/prism.js" data-manual></script>

lib/app.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ interface AppProps {
1515
initialContent: string,
1616
previewWidth: number,
1717
p5version: string,
18+
baseSketchURL: string,
1819
autosaver?: Autosaver,
1920
autoplay?: boolean
2021
}
@@ -139,6 +140,7 @@ export default class App extends PureComponent<AppProps, AppState> {
139140
<div className="preview-holder-wrapper">
140141
{this.state.isPlaying
141142
? <Preview content={this.state.previewContent}
143+
baseSketchURL={this.props.baseSketchURL}
142144
p5version={this.props.p5version}
143145
width={this.props.previewWidth}
144146
timestamp={this.state.startPlayTimestamp}

lib/main.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ let defaultSketchJS = require("raw!./default-sketch.js") as string;
1212
require("../css/style.css");
1313

1414
function start() {
15+
let embeddingPageURL = document.referrer;
1516
let qs = url.parse(window.location.search, true).query;
16-
let id = document.referrer + '_' + qs['id'];
17+
let id = embeddingPageURL + '_' + qs['id'];
18+
let baseSketchURL = qs['baseSketchURL'] || embeddingPageURL;
1719
let autoplay = (qs['autoplay'] === 'on');
1820
let initialContent = qs['sketch'] || defaultSketchJS;
1921
let p5version = qs['p5version'] || defaults.P5_VERSION;
@@ -28,6 +30,7 @@ function start() {
2830
ReactDOM.render(
2931
<App initialContent={initialContent}
3032
autosaver={new SessionStorageAutosaver(id)}
33+
baseSketchURL={baseSketchURL}
3134
p5version={p5version}
3235
previewWidth={previewWidth}
3336
autoplay={autoplay} />,

lib/p5-widget.ts

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function replaceScriptWithWidget(el: HTMLScriptElement) {
5050
let iframe = document.createElement('iframe');
5151
let height = getDataHeight(el);
5252
let previewWidth = parseInt(el.getAttribute('data-preview-width'));
53+
let baseSketchURL = el.getAttribute('data-base-url');
5354
let p5version = el.getAttribute('data-p5-version');
5455
let autoplay = el.hasAttribute('data-autoplay');
5556
let url;
@@ -63,6 +64,10 @@ function replaceScriptWithWidget(el: HTMLScriptElement) {
6364
qsArgs.push('previewWidth=' + previewWidth);
6465
}
6566

67+
if (baseSketchURL) {
68+
qsArgs.push('baseSketchURL=' + encodeURIComponent(baseSketchURL));
69+
}
70+
6671
if (p5version) {
6772
qsArgs.push('p5version=' + encodeURIComponent(p5version));
6873
}

lib/preview-frame.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ function LoopChecker(sketch: string, funcName: string, maxRunTime: number) {
5252
return self;
5353
}
5454

55+
function setBaseURL(url: string) {
56+
var base = document.createElement('base');
57+
base.setAttribute('href', url);
58+
59+
document.head.appendChild(base);
60+
}
61+
5562
function startSketch(sketch: string, p5version: string, maxRunTime: number,
56-
loopCheckFuncName: string,
63+
loopCheckFuncName: string, baseURL: string,
5764
errorCb: PreviewFrameErrorReporter) {
5865
let sketchScript = document.createElement('script');
5966
let loopChecker = LoopChecker(sketch, loopCheckFuncName, maxRunTime);
6067

68+
if (baseURL) {
69+
setBaseURL(baseURL);
70+
}
71+
6172
sketchScript.textContent = sketch;
6273

6374
global.addEventListener('error', (e: ErrorEvent) => {

lib/preview.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface Props {
1111
p5version: string,
1212
width: number,
1313
content: string,
14+
baseSketchURL: string,
1415
timestamp: number,
1516
onError: PreviewFrameErrorReporter
1617
}
@@ -52,7 +53,9 @@ export default class Preview extends PureComponent<Props, State> {
5253
// in which case it shouldn't be emitting events anymore.
5354
let frame = iframe.contentWindow as PreviewFrame;
5455
frame.startSketch(content, this.props.p5version, 1000,
55-
LOOP_CHECK_FUNC_NAME, this.props.onError);
56+
LOOP_CHECK_FUNC_NAME,
57+
this.props.baseSketchURL,
58+
this.props.onError);
5659
});
5760
this.refs.container.appendChild(iframe);
5861
this._iframe = iframe;

typings/custom.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ declare interface PreviewFrameErrorReporter {
2121
// to communicate with it. Thus this interface needs to be asynchronous.
2222
declare interface PreviewFrame extends Window {
2323
startSketch: (sketch: string, p5version: string, maxRunTime: number,
24-
loopCheckFuncName: string,
24+
loopCheckFuncName: string, baseURL: string,
2525
errorCb: PreviewFrameErrorReporter) => void
2626
}

0 commit comments

Comments
 (0)