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'])
content = data.get('data')
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:
return redirect(url_for('wiki.create', name=cname))
@ -121,7 +121,6 @@ def page(name):
return redirect(url_for('wiki.page', name=cname))
data = g.current_wiki.get_page(cname)
if data:
return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials'))
else:

View file

@ -4,13 +4,13 @@
$(function () {
var url_prefix = "";
var $theme = $('#theme-list')
, $preview = $('#preview')
, $autosave = $('#autosave')
, $wordcount = $('#wordcount')
, $wordcounter = $('#wordcounter')
, $pagename = $("#page-name");
var sha = $("#sha").text();
var $theme = $('#theme-list');
var $preview = $('#preview');
var $autosave = $('#autosave');
var $wordcount = $('#wordcount');
var $wordcounter = $('#wordcounter');
var $pagename = $("#page-name");
var $entry_markdown_header = $("#entry-markdown-header");
var $entry_preview_header = $("#entry-preview-header");
@ -27,24 +27,26 @@ $(function () {
});
var editor
, autoInterval
, profile =
{
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()
var editor;
var autoInterval;
var profile = {
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()
};
// Feature detect ish
var dillinger = 'dillinger'
, dillingerElem = document.createElement(dillinger)
, dillingerStyle = dillingerElem.style
, domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
var dillinger = 'dillinger';
var dillingerElem = document.createElement(dillinger);
var dillingerStyle = dillingerElem.style;
var domPrefixes = 'Webkit Moz O ms Khtml'.split(' ');
/// UTILS =================
/**
* Utility method to async load a JavaScript file.
*
@ -69,45 +71,23 @@ $(function () {
}(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.
*
* @return {Void}
*/
function getUserProfile() {
var p;
try {
p = JSON.parse(localStorage.profile);
// 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);
} catch (e) {
p = profile
}
if (p.filename != $pagename.val()) {
localforage.getItem('profile', function(p) {
profile = $.extend(true, profile, p);
if (profile.filename != $pagename.val()) {
setEditorValue("");
updateUserProfile({ filename: $pagename.val(), currentMd: "" });
} else {
if (profile.currentMd) {
setEditorValue(profile.currentMd);
}
profile = p;
}
});
}
/**
@ -117,8 +97,8 @@ $(function () {
* @return {Void}
*/
function updateUserProfile(obj) {
localStorage.clear();
localStorage.profile = JSON.stringify($.extend(true, profile, obj));
localforage.clear();
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.
* The script recursively traverses all text nodes, and returns a
@ -252,19 +212,13 @@ $(function () {
* @return {Void}
*/
function init() {
if (!hasLocalStorage()) {
sadPanda()
}
else {
// Attach to jQuery support object for later use.
$.support.transitionEnd = normalizeTransitionEnd();
getUserProfile();
initAce();
getUserProfile();
initUi();
bindPreview();
@ -274,9 +228,6 @@ $(function () {
bindKeyboard();
autoSave();
}
}
function initAce() {
@ -300,8 +251,7 @@ $(function () {
editor.renderer.setShowInvisibles(true);
editor.renderer.setShowGutter(false);
editor.getSession().setMode('ace/mode/markdown');
editor.getSession().setValue(profile.currentMd || editor.getSession().getValue());
setEditorValue(profile.currentMd || editor.getSession().getValue());
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');
$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();
/* END RE-ARCH STUFF */
}
function clearSelection() {
editor.getSession().setValue("");
setEditorValue("");
previewMd();
}
function saveFile(isManual) {
updateUserProfile({currentMd: editor.getSession().getValue()});
if (isManual) {
@ -348,7 +292,6 @@ $(function () {
if (profile.autosave.enabled) {
autoInterval = setInterval(function() {
// firefox barfs if I don't pass in anon func to setTimeout.
saveFile();
}, profile.autosave.interval);
@ -365,14 +308,13 @@ $(function () {
function resetProfile() {
// For some reason, clear() is not working in Chrome.
localStorage.clear();
localforage.clear();
// Let's turn off autosave
profile.autosave.enabled = false
// Delete the property altogether --> need ; for JSHint bug.
;
delete localStorage.profile;
// Now reload the page to start fresh
profile.autosave.enabled = false;
localforage.removeItem('profile', function() {
window.location.reload();
});
}
function changeTheme(e) {
@ -409,15 +351,12 @@ $(function () {
// document.body.style.backgroundColor = bgColors[name]
}
function setEditorValue(str) {
editor.getSession().setValue(str);
}
function previewMd() {
var unmd = editor.getSession().getValue()
, md = MDR.convert(unmd, true);
$preview
.html('') // unnecessary?
.html(md);
//refreshWordCount();
$preview.html(MDR.convert(editor.getSession().getValue(), true));
}
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() {
$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 }});

View file

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

View file

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