2016-06-27 21:47:48 +02:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-06-24 00:29:55 +02:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-11-16 19:12:36 +01:00
|
|
|
// import escapeStringRegexp from 'escape-string-regexp';
|
2016-07-11 23:21:20 +02:00
|
|
|
import srcDoc from 'srcdoc-polyfill';
|
2016-06-24 00:29:55 +02:00
|
|
|
|
2016-10-05 19:58:45 +02:00
|
|
|
import loopProtect from 'loop-protect';
|
2016-10-22 22:42:43 +02:00
|
|
|
import { getBlobUrl } from '../actions/files';
|
2016-11-16 19:12:36 +01:00
|
|
|
import { resolvePathToFile } from '../../../../server/utils/filePath';
|
2016-08-28 15:52:57 +02:00
|
|
|
|
2017-06-06 04:46:19 +02:00
|
|
|
const decomment = require('decomment');
|
|
|
|
|
2016-08-31 00:06:55 +02:00
|
|
|
const startTag = '@fs-';
|
2017-06-06 04:33:32 +02:00
|
|
|
// eslint-disable-next-line max-len
|
2017-02-14 21:54:29 +01:00
|
|
|
const MEDIA_FILE_REGEX = /^('|")(?!(http:\/\/|https:\/\/)).*\.(png|jpg|jpeg|gif|bmp|mp3|wav|aiff|ogg|json|txt|csv|svg|obj|mp4|ogg|webm|mov|otf|ttf|m4a)('|")$/i;
|
2017-06-06 04:33:32 +02:00
|
|
|
// eslint-disable-next-line max-len
|
2017-02-14 21:54:29 +01:00
|
|
|
const MEDIA_FILE_REGEX_NO_QUOTES = /^(?!(http:\/\/|https:\/\/)).*\.(png|jpg|jpeg|gif|bmp|mp3|wav|aiff|ogg|json|txt|csv|svg|obj|mp4|ogg|webm|mov|otf|ttf|m4a)$/i;
|
2016-10-26 00:38:20 +02:00
|
|
|
const STRING_REGEX = /(['"])((\\\1|.)*?)\1/gm;
|
2016-11-16 19:12:36 +01:00
|
|
|
const TEXT_FILE_REGEX = /(.+\.json$|.+\.txt$|.+\.csv$)/i;
|
|
|
|
const NOT_EXTERNAL_LINK_REGEX = /^(?!(http:\/\/|https:\/\/))/;
|
|
|
|
const EXTERNAL_LINK_REGEX = /^(http:\/\/|https:\/\/)/;
|
2016-08-28 15:52:57 +02:00
|
|
|
|
2016-08-27 19:54:20 +02:00
|
|
|
function getAllScriptOffsets(htmlFile) {
|
2016-08-28 15:52:57 +02:00
|
|
|
const offs = [];
|
|
|
|
let found = true;
|
|
|
|
let lastInd = 0;
|
|
|
|
let ind = 0;
|
|
|
|
let endFilenameInd = 0;
|
|
|
|
let filename = '';
|
|
|
|
let lineOffset = 0;
|
2016-08-27 19:54:20 +02:00
|
|
|
while (found) {
|
2016-08-28 15:52:57 +02:00
|
|
|
ind = htmlFile.indexOf(startTag, lastInd);
|
|
|
|
if (ind === -1) {
|
2016-08-27 19:54:20 +02:00
|
|
|
found = false;
|
2016-08-26 20:22:58 +02:00
|
|
|
} else {
|
2016-08-28 15:52:57 +02:00
|
|
|
endFilenameInd = htmlFile.indexOf('.js', ind + startTag.length + 3);
|
|
|
|
filename = htmlFile.substring(ind + startTag.length, endFilenameInd);
|
2017-08-01 21:03:48 +02:00
|
|
|
// the length of hijackConsoleErrorsScript is 31 lines
|
|
|
|
lineOffset = htmlFile.substring(0, ind).split('\n').length + 31;
|
2016-08-28 15:52:57 +02:00
|
|
|
offs.push([lineOffset, filename]);
|
2016-08-27 19:54:20 +02:00
|
|
|
lastInd = ind + 1;
|
2016-08-26 20:22:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-27 19:54:20 +02:00
|
|
|
return offs;
|
|
|
|
}
|
2016-08-26 20:22:58 +02:00
|
|
|
|
2016-08-31 00:06:55 +02:00
|
|
|
function hijackConsoleErrorsScript(offs) {
|
2016-11-16 19:12:36 +01:00
|
|
|
const s = `
|
2016-08-27 19:54:20 +02:00
|
|
|
function getScriptOff(line) {
|
2016-08-28 15:52:57 +02:00
|
|
|
var offs = ${offs};
|
2016-08-27 19:54:20 +02:00
|
|
|
var l = 0;
|
|
|
|
var file = '';
|
|
|
|
for (var i=0; i<offs.length; i++) {
|
|
|
|
var n = offs[i][0];
|
|
|
|
if (n < line && n > l) {
|
|
|
|
l = n;
|
|
|
|
file = offs[i][1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [line - l, file];
|
|
|
|
}
|
2016-08-31 00:06:55 +02:00
|
|
|
// catch reference errors, via http://stackoverflow.com/a/12747364/2994108
|
|
|
|
window.onerror = function (msg, url, lineNumber, columnNo, error) {
|
|
|
|
var string = msg.toLowerCase();
|
|
|
|
var substring = "script error";
|
|
|
|
var data = {};
|
|
|
|
if (string.indexOf(substring) !== -1){
|
|
|
|
data = 'Script Error: See Browser Console for Detail';
|
|
|
|
} else {
|
|
|
|
var fileInfo = getScriptOff(lineNumber);
|
|
|
|
data = msg + ' (' + fileInfo[1] + ': line ' + fileInfo[0] + ')';
|
|
|
|
}
|
2016-10-09 01:18:38 +02:00
|
|
|
window.parent.postMessage([{
|
2016-08-31 00:06:55 +02:00
|
|
|
method: 'error',
|
|
|
|
arguments: data,
|
2016-12-13 21:37:11 +01:00
|
|
|
source: fileInfo[1]
|
2016-10-09 01:18:38 +02:00
|
|
|
}], '*');
|
2016-08-31 00:06:55 +02:00
|
|
|
return false;
|
|
|
|
};
|
2016-11-16 19:12:36 +01:00
|
|
|
`;
|
2016-08-28 15:52:57 +02:00
|
|
|
return s;
|
2016-08-26 20:22:58 +02:00
|
|
|
}
|
2016-07-18 01:06:43 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
class PreviewFrame extends React.Component {
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.props.isPlaying) {
|
|
|
|
this.renderFrameContents();
|
|
|
|
}
|
2016-07-18 01:06:43 +02:00
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
window.addEventListener('message', (messageEvent) => {
|
2017-10-11 18:56:44 +02:00
|
|
|
console.log(messageEvent);
|
2017-02-22 20:29:35 +01:00
|
|
|
messageEvent.data.forEach((message) => {
|
|
|
|
const args = message.arguments;
|
|
|
|
Object.keys(args).forEach((key) => {
|
|
|
|
if (args[key].includes('Exiting potential infinite loop')) {
|
|
|
|
this.props.stopSketch();
|
|
|
|
this.props.expandConsole();
|
|
|
|
}
|
2017-01-11 20:13:49 +01:00
|
|
|
});
|
2016-08-18 00:13:17 +02:00
|
|
|
});
|
2017-02-22 20:29:35 +01:00
|
|
|
this.props.dispatchConsoleEvent(messageEvent.data);
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:47:48 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
2016-09-29 06:54:35 +02:00
|
|
|
// if sketch starts or stops playing, want to rerender
|
2016-06-27 21:47:48 +02:00
|
|
|
if (this.props.isPlaying !== prevProps.isPlaying) {
|
2016-07-11 21:22:29 +02:00
|
|
|
this.renderSketch();
|
2016-09-28 21:20:54 +02:00
|
|
|
return;
|
2016-06-27 21:47:48 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 00:05:14 +02:00
|
|
|
// if the user explicitly clicks on the play button
|
|
|
|
if (this.props.isPlaying && this.props.previewIsRefreshing) {
|
2016-06-27 21:47:48 +02:00
|
|
|
this.renderSketch();
|
2016-09-29 00:05:14 +02:00
|
|
|
return;
|
2016-06-27 21:47:48 +02:00
|
|
|
}
|
2016-08-18 00:13:17 +02:00
|
|
|
|
2016-11-09 19:16:14 +01:00
|
|
|
// if user switches textoutput preferences
|
2017-05-31 21:23:30 +02:00
|
|
|
if (this.props.isAccessibleOutputPlaying !== prevProps.isAccessibleOutputPlaying) {
|
2016-11-09 19:16:14 +01:00
|
|
|
this.renderSketch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.textOutput !== prevProps.textOutput) {
|
|
|
|
this.renderSketch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-31 21:23:30 +02:00
|
|
|
if (this.props.gridOutput !== prevProps.gridOutput) {
|
|
|
|
this.renderSketch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.soundOutput !== prevProps.soundOutput) {
|
|
|
|
this.renderSketch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-09 00:52:32 +02:00
|
|
|
if (this.props.fullView && this.props.files[0].id !== prevProps.files[0].id) {
|
|
|
|
this.renderSketch();
|
|
|
|
}
|
|
|
|
|
2016-09-29 06:54:35 +02:00
|
|
|
// small bug - if autorefresh is on, and the usr changes files
|
|
|
|
// in the sketch, preview will reload
|
2016-06-27 21:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-02-22 20:29:35 +01:00
|
|
|
ReactDOM.unmountComponentAtNode(this.iframeElement.contentDocument.body);
|
2016-06-27 21:47:48 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
clearPreview() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const doc = this.iframeElement;
|
2016-07-11 21:22:29 +02:00
|
|
|
doc.srcDoc = '';
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:54:39 +02:00
|
|
|
addLoopProtect(sketchDoc) {
|
|
|
|
const scriptsInHTML = sketchDoc.getElementsByTagName('script');
|
|
|
|
const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML);
|
|
|
|
scriptsInHTMLArray.forEach((script) => {
|
|
|
|
script.innerHTML = loopProtect(script.innerHTML); // eslint-disable-line
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:22:29 +02:00
|
|
|
injectLocalFiles() {
|
2016-11-16 19:12:36 +01:00
|
|
|
const htmlFile = this.props.htmlFile.content;
|
2016-08-28 15:52:57 +02:00
|
|
|
let scriptOffs = [];
|
2016-07-20 03:36:21 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
const resolvedFiles = this.resolveJSAndCSSLinks(this.props.files);
|
|
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const sketchDoc = parser.parseFromString(htmlFile, 'text/html');
|
|
|
|
|
2016-11-30 18:38:53 +01:00
|
|
|
const base = sketchDoc.createElement('base');
|
|
|
|
base.href = `${window.location.href}/`;
|
|
|
|
sketchDoc.head.appendChild(base);
|
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
this.resolvePathsForElementsWithAttribute('src', sketchDoc, resolvedFiles);
|
|
|
|
this.resolvePathsForElementsWithAttribute('href', sketchDoc, resolvedFiles);
|
|
|
|
// should also include background, data, poster, but these are used way less often
|
|
|
|
|
|
|
|
this.resolveScripts(sketchDoc, resolvedFiles);
|
|
|
|
this.resolveStyles(sketchDoc, resolvedFiles);
|
|
|
|
|
|
|
|
let scriptsToInject = [
|
|
|
|
'/loop-protect.min.js',
|
|
|
|
'/hijackConsole.js'
|
|
|
|
];
|
2017-06-06 04:33:32 +02:00
|
|
|
if (
|
|
|
|
this.props.isAccessibleOutputPlaying ||
|
|
|
|
((this.props.textOutput || this.props.gridOutput || this.props.soundOutput) && this.props.isPlaying)) {
|
2016-11-16 19:12:36 +01:00
|
|
|
let interceptorScripts = [];
|
2017-05-31 21:23:30 +02:00
|
|
|
interceptorScripts = [
|
|
|
|
'/p5-interceptor/registry.js',
|
|
|
|
'/p5-interceptor/loadData.js',
|
|
|
|
'/p5-interceptor/interceptorHelperFunctions.js',
|
|
|
|
'/p5-interceptor/baseInterceptor.js',
|
|
|
|
'/p5-interceptor/entities/entity.min.js',
|
|
|
|
'/p5-interceptor/ntc.min.js'
|
|
|
|
];
|
|
|
|
if (!this.props.textOutput && !this.props.gridOutput && !this.props.soundOutput) {
|
|
|
|
this.props.setTextOutput(true);
|
2016-11-23 19:35:12 +01:00
|
|
|
}
|
2017-05-31 21:23:30 +02:00
|
|
|
if (this.props.textOutput) {
|
|
|
|
let textInterceptorScripts = [];
|
|
|
|
textInterceptorScripts = [
|
2017-02-15 22:17:57 +01:00
|
|
|
'/p5-interceptor/textInterceptor/interceptorFunctions.js',
|
2017-05-31 21:23:30 +02:00
|
|
|
'/p5-interceptor/textInterceptor/interceptorP5.js'
|
2016-11-16 19:12:36 +01:00
|
|
|
];
|
2017-05-31 21:23:30 +02:00
|
|
|
interceptorScripts = interceptorScripts.concat(textInterceptorScripts);
|
|
|
|
}
|
|
|
|
if (this.props.gridOutput) {
|
|
|
|
let gridInterceptorScripts = [];
|
|
|
|
gridInterceptorScripts = [
|
2017-02-15 22:17:57 +01:00
|
|
|
'/p5-interceptor/gridInterceptor/interceptorFunctions.js',
|
2017-05-31 21:23:30 +02:00
|
|
|
'/p5-interceptor/gridInterceptor/interceptorP5.js'
|
2016-11-16 19:12:36 +01:00
|
|
|
];
|
2017-05-31 21:23:30 +02:00
|
|
|
interceptorScripts = interceptorScripts.concat(gridInterceptorScripts);
|
|
|
|
}
|
|
|
|
if (this.props.soundOutput) {
|
|
|
|
let soundInterceptorScripts = [];
|
|
|
|
soundInterceptorScripts = [
|
2017-02-15 22:17:57 +01:00
|
|
|
'/p5-interceptor/soundInterceptor/interceptorP5.js'
|
2016-11-16 19:12:36 +01:00
|
|
|
];
|
2017-05-31 21:23:30 +02:00
|
|
|
interceptorScripts = interceptorScripts.concat(soundInterceptorScripts);
|
2016-11-16 19:12:36 +01:00
|
|
|
}
|
|
|
|
scriptsToInject = scriptsToInject.concat(interceptorScripts);
|
|
|
|
}
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
scriptsToInject.forEach((scriptToInject) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
const script = sketchDoc.createElement('script');
|
|
|
|
script.src = scriptToInject;
|
|
|
|
sketchDoc.head.appendChild(script);
|
2016-07-20 01:36:50 +02:00
|
|
|
});
|
2016-07-11 21:22:29 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
const sketchDocString = `<!DOCTYPE HTML>\n${sketchDoc.documentElement.outerHTML}`;
|
|
|
|
scriptOffs = getAllScriptOffsets(sketchDocString);
|
|
|
|
const consoleErrorsScript = sketchDoc.createElement('script');
|
|
|
|
consoleErrorsScript.innerHTML = hijackConsoleErrorsScript(JSON.stringify(scriptOffs));
|
2017-08-28 23:54:39 +02:00
|
|
|
this.addLoopProtect(sketchDoc);
|
2016-12-13 21:37:11 +01:00
|
|
|
sketchDoc.head.insertBefore(consoleErrorsScript, sketchDoc.head.firstElement);
|
2016-11-16 19:12:36 +01:00
|
|
|
|
|
|
|
return `<!DOCTYPE HTML>\n${sketchDoc.documentElement.outerHTML}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvePathsForElementsWithAttribute(attr, sketchDoc, files) {
|
|
|
|
const elements = sketchDoc.querySelectorAll(`[${attr}]`);
|
2016-12-07 22:12:06 +01:00
|
|
|
const elementsArray = Array.prototype.slice.call(elements);
|
2017-02-22 20:29:35 +01:00
|
|
|
elementsArray.forEach((element) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (element.getAttribute(attr).match(MEDIA_FILE_REGEX_NO_QUOTES)) {
|
|
|
|
const resolvedFile = resolvePathToFile(element.getAttribute(attr), files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
element.setAttribute(attr, resolvedFile.url);
|
2016-10-26 00:38:20 +02:00
|
|
|
}
|
2016-11-16 19:12:36 +01:00
|
|
|
}
|
2016-10-26 00:38:20 +02:00
|
|
|
});
|
2016-11-16 19:12:36 +01:00
|
|
|
}
|
2016-10-26 00:38:20 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
resolveJSAndCSSLinks(files) {
|
|
|
|
const newFiles = [];
|
2017-02-22 20:29:35 +01:00
|
|
|
files.forEach((file) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
const newFile = { ...file };
|
|
|
|
if (file.name.match(/.*\.js$/i)) {
|
|
|
|
newFile.content = this.resolveJSLinksInString(newFile.content, files);
|
|
|
|
} else if (file.name.match(/.*\.css$/i)) {
|
|
|
|
newFile.content = this.resolveCSSLinksInString(newFile.content, files);
|
2016-11-11 23:36:19 +01:00
|
|
|
}
|
2016-11-16 19:12:36 +01:00
|
|
|
newFiles.push(newFile);
|
2016-07-11 21:22:29 +02:00
|
|
|
});
|
2016-11-16 19:12:36 +01:00
|
|
|
return newFiles;
|
|
|
|
}
|
2016-07-11 21:22:29 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
resolveJSLinksInString(content, files) {
|
|
|
|
let newContent = content;
|
|
|
|
let jsFileStrings = content.match(STRING_REGEX);
|
|
|
|
jsFileStrings = jsFileStrings || [];
|
2017-02-22 20:29:35 +01:00
|
|
|
jsFileStrings.forEach((jsFileString) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (jsFileString.match(MEDIA_FILE_REGEX)) {
|
|
|
|
const filePath = jsFileString.substr(1, jsFileString.length - 2);
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
newContent = newContent.replace(filePath, resolvedFile.url);
|
|
|
|
} else if (resolvedFile.name.match(TEXT_FILE_REGEX)) {
|
|
|
|
// could also pull file from API instead of using bloburl
|
|
|
|
const blobURL = getBlobUrl(resolvedFile);
|
|
|
|
this.props.setBlobUrl(resolvedFile, blobURL);
|
|
|
|
newContent = newContent.replace(filePath, blobURL);
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 23:36:19 +01:00
|
|
|
}
|
2016-07-12 03:54:08 +02:00
|
|
|
});
|
2017-08-03 23:27:01 +02:00
|
|
|
newContent = decomment(newContent, {
|
|
|
|
ignore: /noprotect/g,
|
|
|
|
space: true
|
|
|
|
});
|
2016-11-16 19:12:36 +01:00
|
|
|
newContent = loopProtect(newContent);
|
|
|
|
return newContent;
|
|
|
|
}
|
2016-07-12 03:54:08 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
resolveCSSLinksInString(content, files) {
|
|
|
|
let newContent = content;
|
|
|
|
let cssFileStrings = content.match(STRING_REGEX);
|
|
|
|
cssFileStrings = cssFileStrings || [];
|
2017-02-22 20:29:35 +01:00
|
|
|
cssFileStrings.forEach((cssFileString) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (cssFileString.match(MEDIA_FILE_REGEX)) {
|
|
|
|
const filePath = cssFileString.substr(1, cssFileString.length - 2);
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
newContent = newContent.replace(filePath, resolvedFile.url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newContent;
|
|
|
|
}
|
2016-08-12 21:50:33 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
resolveScripts(sketchDoc, files) {
|
|
|
|
const scriptsInHTML = sketchDoc.getElementsByTagName('script');
|
|
|
|
const scriptsInHTMLArray = Array.prototype.slice.call(scriptsInHTML);
|
2017-02-22 20:29:35 +01:00
|
|
|
scriptsInHTMLArray.forEach((script) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (script.getAttribute('src') && script.getAttribute('src').match(NOT_EXTERNAL_LINK_REGEX) !== null) {
|
|
|
|
const resolvedFile = resolvePathToFile(script.getAttribute('src'), files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
script.setAttribute('src', resolvedFile.url);
|
|
|
|
} else {
|
2016-12-09 22:21:43 +01:00
|
|
|
script.setAttribute('data-tag', `${startTag}${resolvedFile.name}`);
|
2016-11-16 19:12:36 +01:00
|
|
|
script.removeAttribute('src');
|
|
|
|
script.innerHTML = resolvedFile.content; // eslint-disable-line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!(script.getAttribute('src') && script.getAttribute('src').match(EXTERNAL_LINK_REGEX)) !== null) {
|
|
|
|
script.innerHTML = this.resolveJSLinksInString(script.innerHTML, files); // eslint-disable-line
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-10-06 21:45:26 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
resolveStyles(sketchDoc, files) {
|
|
|
|
const inlineCSSInHTML = sketchDoc.getElementsByTagName('style');
|
|
|
|
const inlineCSSInHTMLArray = Array.prototype.slice.call(inlineCSSInHTML);
|
2017-02-22 20:29:35 +01:00
|
|
|
inlineCSSInHTMLArray.forEach((style) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
style.innerHTML = this.resolveCSSLinksInString(style.innerHTML, files); // eslint-disable-line
|
|
|
|
});
|
2016-08-31 00:06:55 +02:00
|
|
|
|
2016-11-16 19:12:36 +01:00
|
|
|
const cssLinksInHTML = sketchDoc.querySelectorAll('link[rel="stylesheet"]');
|
2016-12-07 22:12:06 +01:00
|
|
|
const cssLinksInHTMLArray = Array.prototype.slice.call(cssLinksInHTML);
|
2017-02-22 20:29:35 +01:00
|
|
|
cssLinksInHTMLArray.forEach((css) => {
|
2016-11-16 19:12:36 +01:00
|
|
|
if (css.getAttribute('href') && css.getAttribute('href').match(NOT_EXTERNAL_LINK_REGEX) !== null) {
|
|
|
|
const resolvedFile = resolvePathToFile(css.getAttribute('href'), files);
|
|
|
|
if (resolvedFile) {
|
|
|
|
if (resolvedFile.url) {
|
|
|
|
css.href = resolvedFile.url; // eslint-disable-line
|
|
|
|
} else {
|
|
|
|
const style = sketchDoc.createElement('style');
|
|
|
|
style.innerHTML = `\n${resolvedFile.content}`;
|
2016-12-13 21:37:11 +01:00
|
|
|
sketchDoc.head.appendChild(style);
|
2016-11-16 19:12:36 +01:00
|
|
|
css.parentElement.removeChild(css);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSketch() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const doc = this.iframeElement;
|
2016-10-06 21:45:26 +02:00
|
|
|
if (this.props.isPlaying) {
|
2016-07-11 23:21:20 +02:00
|
|
|
srcDoc.set(doc, this.injectLocalFiles());
|
2016-10-09 00:52:32 +02:00
|
|
|
if (this.props.endSketchRefresh) {
|
|
|
|
this.props.endSketchRefresh();
|
|
|
|
}
|
2016-07-11 21:22:29 +02:00
|
|
|
} else {
|
2016-07-18 02:49:10 +02:00
|
|
|
doc.srcdoc = '';
|
|
|
|
srcDoc.set(doc, ' ');
|
2016-07-11 21:22:29 +02:00
|
|
|
}
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:47:48 +02:00
|
|
|
renderFrameContents() {
|
2017-02-22 20:29:35 +01:00
|
|
|
const doc = this.iframeElement.contentDocument;
|
2016-06-27 21:47:48 +02:00
|
|
|
if (doc.readyState === 'complete') {
|
2016-06-27 23:22:54 +02:00
|
|
|
this.renderSketch();
|
2016-06-27 21:47:48 +02:00
|
|
|
} else {
|
|
|
|
setTimeout(this.renderFrameContents, 0);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-06-27 21:47:48 +02:00
|
|
|
return (
|
|
|
|
<iframe
|
|
|
|
className="preview-frame"
|
2016-08-10 23:24:52 +02:00
|
|
|
aria-label="sketch output"
|
|
|
|
role="main"
|
2016-07-13 21:23:48 +02:00
|
|
|
tabIndex="0"
|
2016-06-27 21:47:48 +02:00
|
|
|
frameBorder="0"
|
|
|
|
title="sketch output"
|
2017-02-22 20:29:35 +01:00
|
|
|
ref={(element) => { this.iframeElement = element; }}
|
2016-10-19 19:03:19 +02:00
|
|
|
sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-forms"
|
2016-07-18 01:06:43 +02:00
|
|
|
/>
|
2016-06-27 21:47:48 +02:00
|
|
|
);
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 21:47:48 +02:00
|
|
|
PreviewFrame.propTypes = {
|
|
|
|
isPlaying: PropTypes.bool.isRequired,
|
2017-05-31 21:23:30 +02:00
|
|
|
isAccessibleOutputPlaying: PropTypes.bool.isRequired,
|
|
|
|
textOutput: PropTypes.bool.isRequired,
|
|
|
|
gridOutput: PropTypes.bool.isRequired,
|
|
|
|
soundOutput: PropTypes.bool.isRequired,
|
2016-11-23 19:35:12 +01:00
|
|
|
setTextOutput: PropTypes.func.isRequired,
|
2016-07-11 21:22:29 +02:00
|
|
|
htmlFile: PropTypes.shape({
|
|
|
|
content: PropTypes.string.isRequired
|
2017-02-22 20:29:35 +01:00
|
|
|
}).isRequired,
|
|
|
|
files: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
content: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
url: PropTypes.string,
|
|
|
|
id: PropTypes.string.isRequired
|
|
|
|
})).isRequired,
|
|
|
|
dispatchConsoleEvent: PropTypes.func.isRequired,
|
2016-09-29 00:05:14 +02:00
|
|
|
endSketchRefresh: PropTypes.func.isRequired,
|
|
|
|
previewIsRefreshing: PropTypes.bool.isRequired,
|
2016-10-09 00:52:32 +02:00
|
|
|
fullView: PropTypes.bool,
|
2017-01-11 20:13:49 +01:00
|
|
|
setBlobUrl: PropTypes.func.isRequired,
|
|
|
|
stopSketch: PropTypes.func.isRequired,
|
|
|
|
expandConsole: PropTypes.func.isRequired
|
2016-06-27 21:47:48 +02:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
PreviewFrame.defaultProps = {
|
|
|
|
fullView: false
|
|
|
|
};
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export default PreviewFrame;
|