This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
live-preview.js
59 lines (51 loc) · 2.03 KB
/
live-preview.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
// Displays the HTML source of a CodeMirror editor as a rendered preview
// in an iframe.
define(["jquery", "backbone-events"], function($, BackboneEvents) {
"use strict";
function LivePreview(options) {
var self = {codeMirror: options.codeMirror, title: ""},
codeMirror = options.codeMirror,
iframe = document.createElement("iframe"),
previewLoader = options.previewLoader || "/templates/previewloader.html",
previewArea = options.previewArea,
telegraph;
// set up the loader script for the preview iframe
iframe.src = previewLoader;
// set up the code-change handling.
codeMirror.on("reparse", function(event) {
if (!event.error || options.ignoreErrors) {
// add the preview iframe to the editor on the first
// attempt to parse the Code Mirror text
if(!iframe.contentWindow) {
previewArea.append(iframe);
telegraph = iframe.contentWindow;
}
// Communicate content changes. For the moment,
// we treat all changes as a full refresh.
var message = JSON.stringify({
type: "overwrite",
sourceCode: event.sourceCode
});
try {
// targetOrigin is current a blanket allow, we'll want to
// narrow it down once scripts in Thimble are operational.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=891521
telegraph.postMessage(message, "*");
} catch (e) {
console.log("An error occurred while postMessaging data to the preview pane");
throw e;
}
// check if we need to update the title for the live preview pane
var titleMatch = event.sourceCode.match(/<[tT][iI][tT][lL][eE][^>]*>([^<]*)<\/[tT][iI][tT][lL][eE]>/),
title = (titleMatch ? titleMatch[1] : false);
if (title != self.title) {
self.title = title;
self.trigger("change:title", self.title);
}
}
});
BackboneEvents.mixin(self);
return self;
};
return LivePreview;
});