2014-09-02 22:07:14 +03:00
|
|
|
/*
|
|
|
|
Source is modified version of http://dillinger.io/
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
$(function () {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-08-20 18:28:25 +03:00
|
|
|
var url_prefix = "";
|
2013-12-04 00:28:16 +02:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var $theme = $('#theme-list')
|
|
|
|
, $preview = $('#preview')
|
|
|
|
, $autosave = $('#autosave')
|
|
|
|
, $wordcount = $('#wordcount')
|
|
|
|
, $wordcounter = $('#wordcounter')
|
|
|
|
, $pagename = $("#page-name");
|
|
|
|
|
|
|
|
var editor
|
|
|
|
, autoInterval
|
|
|
|
, profile =
|
2013-10-01 07:10:10 +03:00
|
|
|
{
|
2014-02-07 05:40:58 +02:00
|
|
|
theme: 'ace/theme/idle_fingers', currentMd: '', autosave: {
|
|
|
|
enabled: true, interval: 3000 // might be too aggressive; don't want to block UI for large saves.
|
|
|
|
}, current_filename: $pagename.val()
|
2013-10-10 00:35:06 +03:00
|
|
|
};
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// Feature detect ish
|
|
|
|
var dillinger = 'dillinger'
|
|
|
|
, dillingerElem = document.createElement(dillinger)
|
|
|
|
, dillingerStyle = dillingerElem.style
|
|
|
|
, domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/// UTILS =================
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Utility method to async load a JavaScript file.
|
|
|
|
*
|
|
|
|
* @param {String} The name of the file to load
|
|
|
|
* @param {Function} Optional callback to be executed after the script loads.
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function asyncLoad(filename, cb) {
|
|
|
|
(function (d, t) {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var leScript = d.createElement(t)
|
|
|
|
, scripts = d.getElementsByTagName(t)[0];
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
leScript.async = 1;
|
|
|
|
leScript.src = filename;
|
2014-02-07 05:40:58 +02:00
|
|
|
scripts.parentNode.insertBefore(leScript, scripts);
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
leScript.onload = function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
cb && cb();
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
}(document, 'script'));
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to determin if localStorage is supported or not.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function hasLocalStorage() {
|
2013-10-10 00:35:06 +03:00
|
|
|
// http://mathiasbynens.be/notes/localstorage-pattern
|
|
|
|
var storage;
|
2014-02-07 05:40:58 +02:00
|
|
|
try {
|
|
|
|
if (localStorage.getItem) {
|
|
|
|
storage = localStorage
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2013-10-10 00:35:06 +03:00
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Grab the user's profile from localStorage and stash in "profile" variable.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function getUserProfile() {
|
2013-10-10 00:35:06 +03:00
|
|
|
|
|
|
|
var p;
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
try {
|
|
|
|
p = JSON.parse(localStorage.profile);
|
2013-10-10 00:35:06 +03:00
|
|
|
// Need to merge in any undefined/new properties from last release
|
|
|
|
// Meaning, if we add new features they may not have them in profile
|
|
|
|
p = $.extend(true, profile, p);
|
2014-02-07 05:40:58 +02:00
|
|
|
} catch (e) {
|
2013-10-10 00:35:06 +03:00
|
|
|
p = profile
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
if (p.filename != $pagename.val()) {
|
|
|
|
updateUserProfile({ filename: $pagename.val(), currentMd: "" });
|
|
|
|
}
|
|
|
|
profile = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update user's profile in localStorage by merging in current profile with passed in param.
|
|
|
|
*
|
|
|
|
* @param {Object} An object containg proper keys and values to be JSON.stringify'd
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function updateUserProfile(obj) {
|
2013-10-10 00:35:06 +03:00
|
|
|
localStorage.clear();
|
2014-02-07 05:40:58 +02:00
|
|
|
localStorage.profile = JSON.stringify($.extend(true, profile, obj));
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility method to test if particular property is supported by the browser or not.
|
|
|
|
* Completely ripped from Modernizr with some mods.
|
|
|
|
* Thx, Modernizr team!
|
|
|
|
*
|
|
|
|
* @param {String} The property to test
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function prefixed(prop) {
|
|
|
|
return testPropsAll(prop, 'pfx')
|
|
|
|
}
|
2013-10-10 00:35:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A generic CSS / DOM property test; if a browser supports
|
|
|
|
* a certain property, it won't return undefined for it.
|
|
|
|
* A supported CSS property returns empty string when its not yet set.
|
|
|
|
*
|
|
|
|
* @param {Object} A hash of properties to test
|
|
|
|
* @param {String} A prefix
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function testProps(props, prefixed) {
|
2013-10-10 00:35:06 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
for (var i in props) {
|
2013-10-10 00:35:06 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
if (dillingerStyle[ props[i] ] !== undefined) {
|
2013-10-10 00:35:06 +03:00
|
|
|
return prefixed === 'pfx' ? props[i] : true;
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
|
|
|
return false
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Tests a list of DOM properties we want to check against.
|
|
|
|
* We specify literally ALL possible (known and/or likely) properties on
|
|
|
|
* the element including the non-vendor prefixed one, for forward-
|
|
|
|
* compatibility.
|
|
|
|
*
|
|
|
|
* @param {String} The name of the property
|
|
|
|
* @param {String} The prefix string
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function testPropsAll(prop, prefixed) {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
var ucProp = prop.charAt(0).toUpperCase() + prop.substr(1)
|
|
|
|
, props = (prop + ' ' + domPrefixes.join(ucProp + ' ') + ucProp).split(' ');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
return testProps(props, prefixed);
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Normalize the transitionEnd event across browsers.
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function normalizeTransitionEnd() {
|
2013-10-03 21:13:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var transEndEventNames =
|
|
|
|
{
|
2014-02-07 05:40:58 +02:00
|
|
|
'WebkitTransition': 'webkitTransitionEnd', 'MozTransition': 'transitionend', 'OTransition': 'oTransitionEnd', 'msTransition': 'msTransitionEnd' // maybe?
|
|
|
|
, 'transition': 'transitionend'
|
2013-10-01 07:10:10 +03:00
|
|
|
};
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
return transEndEventNames[ prefixed('transition') ];
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Get current filename from contenteditable field.
|
|
|
|
*
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function getCurrentFilenameFromField() {
|
2013-10-10 00:35:06 +03:00
|
|
|
return $('#filename > span[contenteditable="true"]').text()
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Set current filename from profile.
|
|
|
|
*
|
|
|
|
* @param {String} Optional string to force set the value.
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function setCurrentFilenameField(str) {
|
|
|
|
$('#filename > span[contenteditable="true"]').text(str || profile.current_filename || "Untitled Document")
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Returns the full text of an element and all its children.
|
|
|
|
* The script recursively traverses all text nodes, and returns a
|
|
|
|
* concatenated string of all texts.
|
|
|
|
*
|
|
|
|
* Taken from
|
|
|
|
* http://stackoverflow.com/questions/2653670/innertext-textcontent-vs-retrieving-each-text-node
|
|
|
|
*
|
|
|
|
* @param node
|
|
|
|
* @return {int}
|
|
|
|
*/
|
|
|
|
function getTextInElement(node) {
|
|
|
|
if (node.nodeType === 3) {
|
|
|
|
return node.data;
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var txt = '';
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
if (node = node.firstChild) do {
|
|
|
|
txt += getTextInElement(node);
|
|
|
|
} while (node = node.nextSibling);
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
return txt;
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Counts the words in a string
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function countWords(string) {
|
|
|
|
var words = string.replace(/W+/g, ' ').match(/\S+/g);
|
|
|
|
return words && words.length || 0;
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/**
|
|
|
|
* Initialize application.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2014-02-07 05:40:58 +02:00
|
|
|
function init() {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
if (!hasLocalStorage()) {
|
|
|
|
sadPanda()
|
|
|
|
}
|
|
|
|
else {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// Attach to jQuery support object for later use.
|
|
|
|
$.support.transitionEnd = normalizeTransitionEnd();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
getUserProfile();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
initAce();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
initUi();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
bindPreview();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
bindNav();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
bindKeyboard();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
autoSave();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function initAce() {
|
2013-10-10 00:35:06 +03:00
|
|
|
editor = ace.edit("editor");
|
2013-12-04 00:28:16 +02:00
|
|
|
editor.focus();
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function initUi() {
|
2013-10-10 00:35:06 +03:00
|
|
|
// Set proper theme value in theme dropdown
|
2014-02-07 05:40:58 +02:00
|
|
|
fetchTheme(profile.theme, function () {
|
|
|
|
$theme.find('li > a[data-value="' + profile.theme + '"]').addClass('selected');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
editor.getSession().setUseWrapMode(true);
|
|
|
|
editor.setShowPrintMargin(false);
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
editor.getSession().setMode('ace/mode/markdown');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
editor.getSession().setValue(profile.currentMd || editor.getSession().getValue());
|
2013-10-10 00:35:06 +03:00
|
|
|
previewMd();
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// Set text for dis/enable autosave / word counter
|
2014-02-07 05:40:58 +02:00
|
|
|
$autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i> Disable Autosave' : '<i class="icon-ok"></i> Enable Autosave');
|
|
|
|
$wordcount.html(!profile.wordcount ? '<i class="icon-remove"></i> Disabled Word Count' : '<i class="icon-ok"></i> Enabled Word Count');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
setCurrentFilenameField();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/* BEGIN RE-ARCH STUFF */
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$('.dropdown-toggle').dropdown();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
/* END RE-ARCH STUFF */
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function clearSelection() {
|
2013-10-10 00:35:06 +03:00
|
|
|
editor.getSession().setValue("");
|
|
|
|
previewMd();
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function saveFile(isManual) {
|
2013-10-10 18:38:30 +03:00
|
|
|
updateUserProfile({currentMd: editor.getSession().getValue()});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
if (isManual) {
|
|
|
|
updateUserProfile({ currentMd: "" });
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var data = {
|
|
|
|
name: $pagename.val(),
|
|
|
|
message: $("#page-message").val(),
|
|
|
|
content: editor.getSession().getValue()
|
|
|
|
};
|
2014-02-07 05:40:58 +02:00
|
|
|
$.post(window.location, data, function () {
|
2013-12-04 00:28:16 +02:00
|
|
|
location.href = url_prefix + '/' + data['name'];
|
2013-10-10 00:35:06 +03:00
|
|
|
});
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function autoSave() {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
if (profile.autosave.enabled) {
|
|
|
|
autoInterval = setInterval(function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
// firefox barfs if I don't pass in anon func to setTimeout.
|
|
|
|
saveFile();
|
|
|
|
}, profile.autosave.interval);
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 18:38:30 +03:00
|
|
|
} else {
|
2014-02-07 05:40:58 +02:00
|
|
|
clearInterval(autoInterval)
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
$("#save-native").on('click', function () {
|
2013-10-01 07:10:10 +03:00
|
|
|
saveFile(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function resetProfile() {
|
2013-10-10 00:35:06 +03:00
|
|
|
// For some reason, clear() is not working in Chrome.
|
|
|
|
localStorage.clear();
|
|
|
|
// Let's turn off autosave
|
|
|
|
profile.autosave.enabled = false
|
|
|
|
// Delete the property altogether --> need ; for JSHint bug.
|
2014-02-07 05:40:58 +02:00
|
|
|
;
|
|
|
|
delete localStorage.profile;
|
2013-10-10 00:35:06 +03:00
|
|
|
// Now reload the page to start fresh
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function changeTheme(e) {
|
2013-10-10 00:35:06 +03:00
|
|
|
// check for same theme
|
|
|
|
var $target = $(e.target);
|
2014-02-07 05:40:58 +02:00
|
|
|
if ($target.attr('data-value') === profile.theme) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-10 18:38:30 +03:00
|
|
|
else {
|
2013-10-10 00:35:06 +03:00
|
|
|
// add/remove class
|
|
|
|
$theme.find('li > a.selected').removeClass('selected');
|
|
|
|
$target.addClass('selected');
|
|
|
|
// grabnew theme
|
|
|
|
var newTheme = $target.attr('data-value');
|
|
|
|
$(e.target).blur();
|
2014-02-07 05:40:58 +02:00
|
|
|
fetchTheme(newTheme, function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function fetchTheme(th, cb) {
|
2013-10-10 00:35:06 +03:00
|
|
|
var name = th.split('/').pop();
|
2014-02-07 05:40:58 +02:00
|
|
|
asyncLoad("/static/js/ace/theme-" + name + ".js", function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
editor.setTheme(th);
|
|
|
|
cb && cb();
|
|
|
|
updateBg(name);
|
|
|
|
updateUserProfile({theme: th});
|
2013-10-10 18:38:30 +03:00
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 18:38:30 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function updateBg(name) {
|
2013-10-10 00:35:06 +03:00
|
|
|
// document.body.style.backgroundColor = bgColors[name]
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function previewMd() {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var unmd = editor.getSession().getValue()
|
|
|
|
, md = MDR.convert(unmd, true);
|
|
|
|
$preview
|
|
|
|
.html('') // unnecessary?
|
|
|
|
.html(md);
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 20:26:42 +03:00
|
|
|
//refreshWordCount();
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function updateFilename(str) {
|
2013-10-10 00:35:06 +03:00
|
|
|
// Check for string because it may be keyup event object
|
2013-10-10 18:38:30 +03:00
|
|
|
var f;
|
2014-02-07 05:40:58 +02:00
|
|
|
if (typeof str === 'string') {
|
2013-10-10 18:38:30 +03:00
|
|
|
f = str;
|
|
|
|
} else {
|
|
|
|
f = getCurrentFilenameFromField();
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
2014-02-07 05:40:58 +02:00
|
|
|
updateUserProfile({ current_filename: f });
|
2013-10-10 00:35:06 +03:00
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function showHtml() {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// TODO: UPDATE TO SUPPORT FILENAME NOT JUST A RANDOM FILENAME
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var unmd = editor.getSession().getValue();
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function _doneHandler(jqXHR, data, response) {
|
2013-10-10 00:35:06 +03:00
|
|
|
// console.dir(resp)
|
|
|
|
var resp = JSON.parse(response.responseText);
|
|
|
|
$('#myModalBody').text(resp.data);
|
|
|
|
$('#myModal').modal();
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function _failHandler() {
|
2013-10-10 00:35:06 +03:00
|
|
|
alert("Roh-roh. Something went wrong. :(");
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var config = {
|
2013-10-10 00:35:06 +03:00
|
|
|
type: 'POST',
|
|
|
|
data: "unmd=" + encodeURIComponent(unmd),
|
|
|
|
dataType: 'json',
|
|
|
|
url: '/factory/fetch_html_direct',
|
|
|
|
error: _failHandler,
|
|
|
|
success: _doneHandler
|
|
|
|
};
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
$.ajax(config)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function sadPanda() {
|
2013-10-10 00:35:06 +03:00
|
|
|
// TODO: ACTUALLY SHOW A SAD PANDA.
|
|
|
|
alert('Sad Panda - No localStorage for you!')
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function toggleAutoSave() {
|
|
|
|
$autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i> Disable Autosave' : '<i class="icon-ok"></i> Enable Autosave');
|
2013-10-10 00:35:06 +03:00
|
|
|
updateUserProfile({autosave: {enabled: !profile.autosave.enabled }});
|
|
|
|
autoSave();
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function bindPreview() {
|
|
|
|
editor.getSession().on('change', function (e) {
|
2013-10-10 18:38:30 +03:00
|
|
|
previewMd();
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function bindNav() {
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$theme
|
|
|
|
.find('li > a')
|
2014-02-07 05:40:58 +02:00
|
|
|
.bind('click', function (e) {
|
2013-10-10 00:35:06 +03:00
|
|
|
changeTheme(e);
|
|
|
|
return false;
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$('#clear')
|
2014-02-07 05:40:58 +02:00
|
|
|
.on('click', function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
clearSelection();
|
|
|
|
return false;
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$("#autosave")
|
2014-02-07 05:40:58 +02:00
|
|
|
.on('click', function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
toggleAutoSave();
|
|
|
|
return false;
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$('#reset')
|
2014-02-07 05:40:58 +02:00
|
|
|
.on('click', function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
resetProfile();
|
|
|
|
return false;
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
$('#cheat').
|
2014-02-07 05:40:58 +02:00
|
|
|
on('click', function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
window.open("https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet", "_blank");
|
|
|
|
return false;
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
} // end bindNav()
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-12-04 00:28:16 +02:00
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
function bindKeyboard() {
|
2013-10-10 00:35:06 +03:00
|
|
|
// CMD+s TO SAVE DOC
|
2014-02-07 05:40:58 +02:00
|
|
|
key('command+s, ctrl+s', function (e) {
|
2013-10-10 00:35:06 +03:00
|
|
|
saveFile(true);
|
2013-12-04 00:28:16 +02:00
|
|
|
e.preventDefault(); // so we don't save the web page - native browser functionality
|
2013-10-10 00:35:06 +03:00
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var saveCommand = {
|
|
|
|
name: "save",
|
|
|
|
bindKey: {
|
|
|
|
mac: "Command-S",
|
|
|
|
win: "Ctrl-S"
|
|
|
|
},
|
2014-02-07 05:40:58 +02:00
|
|
|
exec: function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
saveFile(true);
|
|
|
|
}
|
|
|
|
};
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
editor.commands.addCommand(saveCommand);
|
|
|
|
}
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
init();
|
|
|
|
});
|
2013-10-01 07:10:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
function getScrollHeight($prevFrame) {
|
|
|
|
// Different browsers attach the scrollHeight of a document to different
|
|
|
|
// elements, so handle that here.
|
|
|
|
if ($prevFrame[0].scrollHeight !== undefined) {
|
|
|
|
return $prevFrame[0].scrollHeight;
|
|
|
|
} else if ($prevFrame.find('html')[0].scrollHeight !== undefined &&
|
2013-10-10 00:35:06 +03:00
|
|
|
$prevFrame.find('html')[0].scrollHeight !== 0) {
|
2013-10-01 07:10:10 +03:00
|
|
|
return $prevFrame.find('html')[0].scrollHeight;
|
|
|
|
} else {
|
|
|
|
return $prevFrame.find('body')[0].scrollHeight;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function syncPreview() {
|
2013-10-10 00:35:06 +03:00
|
|
|
var $ed = window.ace.edit('editor');
|
|
|
|
var $prev = $('#preview');
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var editorScrollRange = ($ed.getSession().getLength());
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
var previewScrollRange = (getScrollHeight($prev));
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// Find how far along the editor is (0 means it is scrolled to the top, 1
|
|
|
|
// means it is at the bottom).
|
|
|
|
var scrollFactor = $ed.getFirstVisibleRow() / editorScrollRange;
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-10-10 00:35:06 +03:00
|
|
|
// Set the scroll position of the preview pane to match. jQuery will
|
|
|
|
// gracefully handle out-of-bounds values.
|
|
|
|
$prev.scrollTop(scrollFactor * previewScrollRange);
|
2013-10-01 07:10:10 +03:00
|
|
|
}
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
window.onload = function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
var $loading = $('#loading');
|
|
|
|
|
2014-02-07 05:40:58 +02:00
|
|
|
if ($.support.transition) {
|
2013-10-10 00:35:06 +03:00
|
|
|
$loading
|
2014-02-07 05:40:58 +02:00
|
|
|
.bind($.support.transitionEnd, function () {
|
2013-10-10 00:35:06 +03:00
|
|
|
$('#main').removeClass('bye');
|
|
|
|
$loading.remove();
|
|
|
|
})
|
|
|
|
.addClass('fade_slow');
|
|
|
|
} else {
|
|
|
|
$('#main').removeClass('bye');
|
|
|
|
$loading.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind synchronization of preview div to editor scroll and change
|
|
|
|
* of editor cursor position.
|
|
|
|
*/
|
|
|
|
window.ace.edit('editor').session.on('changeScrollTop', syncPreview);
|
|
|
|
window.ace.edit('editor').session.selection.on('changeCursor', syncPreview);
|
|
|
|
};
|