add sha to dom

This commit is contained in:
Matthew Scragg 2014-09-29 20:33:09 -05:00
parent 2895308667
commit ec551ac09d
4 changed files with 65 additions and 130 deletions

View file

@ -74,7 +74,7 @@ def edit(name):
name = remove_ext(data['name']) name = remove_ext(data['name'])
content = data.get('data') content = data.get('data')
g.assets['js'].append('editor.js') g.assets['js'].append('editor.js')
return render_template('wiki/edit.html', name=name, content=content, partials=data.get('partials')) return render_template('wiki/edit.html', name=name, content=content, sha=data.get('sha'), partials=data.get('partials'))
else: else:
return redirect(url_for('wiki.create', name=cname)) return redirect(url_for('wiki.create', name=cname))
@ -121,7 +121,6 @@ def page(name):
return redirect(url_for('wiki.page', name=cname)) return redirect(url_for('wiki.page', name=cname))
data = g.current_wiki.get_page(cname) data = g.current_wiki.get_page(cname)
if data: if data:
return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials')) return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials'))
else: else:

View file

@ -4,13 +4,13 @@
$(function () { $(function () {
var url_prefix = ""; var url_prefix = "";
var sha = $("#sha").text();
var $theme = $('#theme-list') var $theme = $('#theme-list');
, $preview = $('#preview') var $preview = $('#preview');
, $autosave = $('#autosave') var $autosave = $('#autosave');
, $wordcount = $('#wordcount') var $wordcount = $('#wordcount');
, $wordcounter = $('#wordcounter') var $wordcounter = $('#wordcounter');
, $pagename = $("#page-name"); var $pagename = $("#page-name");
var $entry_markdown_header = $("#entry-markdown-header"); var $entry_markdown_header = $("#entry-markdown-header");
var $entry_preview_header = $("#entry-preview-header"); var $entry_preview_header = $("#entry-preview-header");
@ -27,24 +27,26 @@ $(function () {
}); });
var editor var editor;
, autoInterval var autoInterval;
, profile = var profile = {
{ theme: 'ace/theme/idle_fingers',
theme: 'ace/theme/idle_fingers', currentMd: '', autosave: { currentMd: '',
enabled: true, interval: 3000 // might be too aggressive; don't want to block UI for large saves. autosave: {
}, current_filename: $pagename.val() enabled: true,
}; interval: 3000 // might be too aggressive; don't want to block UI for large saves.
},
current_filename: $pagename.val()
};
// Feature detect ish // Feature detect ish
var dillinger = 'dillinger' var dillinger = 'dillinger';
, dillingerElem = document.createElement(dillinger) var dillingerElem = document.createElement(dillinger);
, dillingerStyle = dillingerElem.style var dillingerStyle = dillingerElem.style;
, domPrefixes = 'Webkit Moz O ms Khtml'.split(' '); var domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
/// UTILS ================= /// UTILS =================
/** /**
* Utility method to async load a JavaScript file. * Utility method to async load a JavaScript file.
* *
@ -69,45 +71,23 @@ $(function () {
}(document, 'script')); }(document, 'script'));
} }
/**
* Utility method to determin if localStorage is supported or not.
*
* @return {Boolean}
*/
function hasLocalStorage() {
// http://mathiasbynens.be/notes/localstorage-pattern
var storage;
try {
if (localStorage.getItem) {
storage = localStorage
}
} catch (e) {
}
return storage;
}
/** /**
* Grab the user's profile from localStorage and stash in "profile" variable. * Grab the user's profile from localStorage and stash in "profile" variable.
* *
* @return {Void} * @return {Void}
*/ */
function getUserProfile() { function getUserProfile() {
localforage.getItem('profile', function(p) {
var p; profile = $.extend(true, profile, p);
if (profile.filename != $pagename.val()) {
try { setEditorValue("");
p = JSON.parse(localStorage.profile); updateUserProfile({ filename: $pagename.val(), currentMd: "" });
// Need to merge in any undefined/new properties from last release } else {
// Meaning, if we add new features they may not have them in profile if (profile.currentMd) {
p = $.extend(true, profile, p); setEditorValue(profile.currentMd);
} catch (e) { }
p = profile }
} });
if (p.filename != $pagename.val()) {
updateUserProfile({ filename: $pagename.val(), currentMd: "" });
}
profile = p;
} }
/** /**
@ -117,8 +97,8 @@ $(function () {
* @return {Void} * @return {Void}
*/ */
function updateUserProfile(obj) { function updateUserProfile(obj) {
localStorage.clear(); localforage.clear();
localStorage.profile = JSON.stringify($.extend(true, profile, obj)); localforage.setItem('profile', $.extend(true, profile, obj));
} }
/** /**
@ -189,26 +169,6 @@ $(function () {
} }
/**
* Get current filename from contenteditable field.
*
* @return {String}
*/
function getCurrentFilenameFromField() {
return $('#filename > span[contenteditable="true"]').text()
}
/**
* Set current filename from profile.
*
* @param {String} Optional string to force set the value.
* @return {String}
*/
function setCurrentFilenameField(str) {
$('#filename > span[contenteditable="true"]').text(str || profile.current_filename || "Untitled Document")
}
/** /**
* Returns the full text of an element and all its children. * Returns the full text of an element and all its children.
* The script recursively traverses all text nodes, and returns a * The script recursively traverses all text nodes, and returns a
@ -252,31 +212,22 @@ $(function () {
* @return {Void} * @return {Void}
*/ */
function init() { function init() {
// Attach to jQuery support object for later use.
$.support.transitionEnd = normalizeTransitionEnd();
if (!hasLocalStorage()) { initAce();
sadPanda()
}
else {
// Attach to jQuery support object for later use. getUserProfile();
$.support.transitionEnd = normalizeTransitionEnd();
getUserProfile(); initUi();
initAce(); bindPreview();
initUi(); bindNav();
bindPreview(); bindKeyboard();
bindNav();
bindKeyboard();
autoSave();
}
autoSave();
} }
function initAce() { function initAce() {
@ -300,8 +251,7 @@ $(function () {
editor.renderer.setShowInvisibles(true); editor.renderer.setShowInvisibles(true);
editor.renderer.setShowGutter(false); editor.renderer.setShowGutter(false);
editor.getSession().setMode('ace/mode/markdown'); editor.getSession().setMode('ace/mode/markdown');
setEditorValue(profile.currentMd || editor.getSession().getValue());
editor.getSession().setValue(profile.currentMd || editor.getSession().getValue());
previewMd(); previewMd();
}); });
@ -310,23 +260,17 @@ $(function () {
$autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i>&nbsp;Disable Autosave' : '<i class="icon-ok"></i>&nbsp;Enable Autosave'); $autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i>&nbsp;Disable Autosave' : '<i class="icon-ok"></i>&nbsp;Enable Autosave');
$wordcount.html(!profile.wordcount ? '<i class="icon-remove"></i>&nbsp;Disabled Word Count' : '<i class="icon-ok"></i>&nbsp;Enabled Word Count'); $wordcount.html(!profile.wordcount ? '<i class="icon-remove"></i>&nbsp;Disabled Word Count' : '<i class="icon-ok"></i>&nbsp;Enabled Word Count');
setCurrentFilenameField();
/* BEGIN RE-ARCH STUFF */
$('.dropdown-toggle').dropdown(); $('.dropdown-toggle').dropdown();
/* END RE-ARCH STUFF */
} }
function clearSelection() { function clearSelection() {
editor.getSession().setValue(""); setEditorValue("");
previewMd(); previewMd();
} }
function saveFile(isManual) { function saveFile(isManual) {
updateUserProfile({currentMd: editor.getSession().getValue()}); updateUserProfile({currentMd: editor.getSession().getValue()});
if (isManual) { if (isManual) {
@ -337,7 +281,7 @@ $(function () {
message: $("#page-message").val(), message: $("#page-message").val(),
content: editor.getSession().getValue() content: editor.getSession().getValue()
}; };
$.post(window.location, data, function () { $.post(window.location, data, function() {
location.href = url_prefix + '/' + data['name']; location.href = url_prefix + '/' + data['name'];
}); });
} }
@ -347,8 +291,7 @@ $(function () {
function autoSave() { function autoSave() {
if (profile.autosave.enabled) { if (profile.autosave.enabled) {
autoInterval = setInterval(function () { autoInterval = setInterval(function() {
// firefox barfs if I don't pass in anon func to setTimeout.
saveFile(); saveFile();
}, profile.autosave.interval); }, profile.autosave.interval);
@ -358,21 +301,20 @@ $(function () {
} }
$("#save-native").on('click', function () { $("#save-native").on('click', function() {
saveFile(true); saveFile(true);
}); });
function resetProfile() { function resetProfile() {
// For some reason, clear() is not working in Chrome. // For some reason, clear() is not working in Chrome.
localStorage.clear(); localforage.clear();
// Let's turn off autosave // Let's turn off autosave
profile.autosave.enabled = false profile.autosave.enabled = false;
// Delete the property altogether --> need ; for JSHint bug. localforage.removeItem('profile', function() {
; window.location.reload();
delete localStorage.profile; });
// Now reload the page to start fresh
window.location.reload();
} }
function changeTheme(e) { function changeTheme(e) {
@ -409,15 +351,12 @@ $(function () {
// document.body.style.backgroundColor = bgColors[name] // document.body.style.backgroundColor = bgColors[name]
} }
function setEditorValue(str) {
editor.getSession().setValue(str);
}
function previewMd() { function previewMd() {
$preview.html(MDR.convert(editor.getSession().getValue(), true));
var unmd = editor.getSession().getValue()
, md = MDR.convert(unmd, true);
$preview
.html('') // unnecessary?
.html(md);
//refreshWordCount();
} }
function updateFilename(str) { function updateFilename(str) {
@ -462,11 +401,6 @@ $(function () {
} }
function sadPanda() {
// TODO: ACTUALLY SHOW A SAD PANDA.
alert('Sad Panda - No localStorage for you!')
}
function toggleAutoSave() { function toggleAutoSave() {
$autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i>&nbsp;Disable Autosave' : '<i class="icon-ok"></i>&nbsp;Enable Autosave'); $autosave.html(profile.autosave.enabled ? '<i class="icon-remove"></i>&nbsp;Disable Autosave' : '<i class="icon-ok"></i>&nbsp;Enable Autosave');
updateUserProfile({autosave: {enabled: !profile.autosave.enabled }}); updateUserProfile({autosave: {enabled: !profile.autosave.enabled }});

View file

@ -127,6 +127,8 @@
</section> </section>
</section> </section>
<input id="sha" type="hidden" name="sha" value="{{ sha }}" />
</div> </div>
{% endblock %} {% endblock %}

View file

@ -8,7 +8,7 @@ with open('README.md') as f:
with open('requirements.txt') as f: with open('requirements.txt') as f:
required = f.read().splitlines() required = f.read().splitlines()
VERSION = '0.2.1' VERSION = '0.2.2'
CLASSIFIERS = [ CLASSIFIERS = [
'Intended Audience :: Developers', 'Intended Audience :: Developers',