forked from jsbin/jsbin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sneaking in the live preview - only hacked it together in a few minut…
…es, so it's not very sexy, but it kinda works the way I want to. To give it a whirl, open the console and hit: jsbin.livePreview(); (it's sticky after that)
- Loading branch information
Showing
8 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var $live = $('#live'), | ||
$bin = $('#bin'), | ||
throttledPreview = throttle(renderLivePreview, 250); | ||
|
||
// could chain - but it's more readable like this | ||
$live.bind('show', function () { | ||
$bin.addClass('live'); | ||
localStorage && localStorage.setItem('livepreview', true); | ||
|
||
|
||
// start timer | ||
$(document).bind('codeChange.live', throttledPreview); | ||
renderLivePreview(); | ||
}).bind('hide', function () { | ||
$(document).unbind('codeChange.live'); | ||
localStorage && localStorage.removeItem('livepreview'); | ||
$bin.removeClass('live'); | ||
}).bind('toggle', function () { | ||
$live.trigger($bin.is('.live') ? 'hide' : 'show'); | ||
}); | ||
|
||
function two(s) { | ||
return (s+'').length < 2 ? '0' + s : s; | ||
} | ||
|
||
function renderLivePreview() { | ||
$live.find('iframe').remove(); | ||
var frame = $live.append('<iframe class="stretch"></iframe>').find('iframe')[0], | ||
document = frame.contentDocument || frame.contentWindow.document, | ||
source = getPreparedCode(), | ||
window = document.defaultView || document.parentWindow, | ||
d = new Date(); | ||
|
||
// nullify the blocking functions | ||
window.alert = function () {}; | ||
window.prompt = function () {}; | ||
window.confirm = function () {}; | ||
|
||
if (!useCustomConsole) console.log('--- refreshing live preview @ ' + [two(d.getHours()),two(d.getMinutes()),two(d.getSeconds())].join(':') + ' ---'); | ||
document.open(); | ||
|
||
if (debug) { | ||
document.write('<pre>' + source.replace(/[<>&]/g, function (m) { | ||
if (m == '<') return '<'; | ||
if (m == '>') return '>'; | ||
if (m == '"') return '"'; | ||
}) + '</pre>'); | ||
} else { | ||
document.write(source); | ||
} | ||
document.close(); | ||
} | ||
|
||
$live.find('.close').click(function () { | ||
$live.trigger('hide'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters