From 75372ce3515b723170df27b0a84e02a16fdde08a Mon Sep 17 00:00:00 2001
From: llj
Date: Sat, 19 Oct 2013 15:29:05 +0800
Subject: [PATCH] [file edit] improved draft autosave
---
media/js/base.js | 10 +-
seahub/templates/file_edit.html | 172 ++++++++++++--------------------
2 files changed, 71 insertions(+), 111 deletions(-)
diff --git a/media/js/base.js b/media/js/base.js
index 3a2a2bb31b..8a17327362 100644
--- a/media/js/base.js
+++ b/media/js/base.js
@@ -57,7 +57,15 @@ $(document).click(function(e) {
// clear repo enc info when log out
$('#logout').click(function() {
if ('localStorage' in window && window['localStorage'] !== null) {
- localStorage.clear();
+ if (localStorage.length > 0) {
+ for (var key in localStorage) {
+ if (key.lastIndexOf('_decrypt_t') == 36 ||
+ key.lastIndexOf('_enc_key') == 36 ||
+ key.lastIndexOf('_enc_iv') == 36) { // key: {{repo_id}}_xx
+ localStorage.removeItem(key);
+ }
+ }
+ }
}
});
if ($.browser.mozilla || $.browser.msie) {
diff --git a/seahub/templates/file_edit.html b/seahub/templates/file_edit.html
index 42e7af6146..e41b33236a 100644
--- a/seahub/templates/file_edit.html
+++ b/seahub/templates/file_edit.html
@@ -90,6 +90,7 @@
{% endfor %}
+
{% trans "Draft saved." %}
{% if filetype == 'Markdown' %}
@@ -153,26 +154,6 @@
 |
 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
{% trans "Extended Syntax for Wiki"%}
@@ -270,13 +251,38 @@
{% trans "Draft Available" %}
{% trans "There's a saved draft for this file, would you like to load it?" %}
-
+
{% endblock %}
{% block extra_script %}
{% if not err and file_content != None %}
+
{% if filetype == 'Sf' %}
@@ -320,106 +326,49 @@ $(window).scroll(function() {
{% endif %}
});
-var browserStorage = {
+if ('localStorage' in window && window['localStorage'] !== null) {
- current_url : location.hostname + location.pathname + location.search,
-
- isAvailable : function() {
- try {
- return localStorage.getItem;
- } catch (e) {
- return false;
- }
- },
-
- set : function(key, value) {
- try {
- localStorage.setItem(key, value + "");
- } catch (e) {
- feedback('{% trans "Failed to save draft, exceeded max quota" %}', 'error');
- }
- },
-
- get : function(key) {
- return localStorage.getItem(key);
- },
-
- remove : function(key) {
- localStorage.removeItem(key);
- }
-
-};
-
-if (browserStorage.isAvailable()) {
-
- var key = browserStorage.current_url;
- var md_key_event_on = true;
-
- function saveDraftByTimeout() {
+ var saved_tip = $('#saved-tip');
+ function saveDraft() {
+ var draft;
{% if filetype == 'Sf' %}
- $('#sf').off('keydown');
+ draft = $('#sf').html();
{% endif %}
{% if filetype == 'Text' %}
- md_key_event_on = false;
+ draft = editor.getValue();
{% endif %}
{% if filetype == 'Markdown' %}
- $('#wmd-input').off('keydown');
+ draft = $('#wmd-input').val();
{% endif %}
- setTimeout(function() {
- var draft;
- {% if filetype == 'Sf' %}
- draft = $('#sf').html();
- $('#sf').on('keydown', saveDraftByTimeout);
- {% endif %}
- {% if filetype == 'Text' %}
- draft = editor.getValue();
- md_key_event_on = true;
- {% endif %}
- {% if filetype == 'Markdown' %}
- draft = $('#wmd-input').val();
- $('#wmd-input').on('keydown', saveDraftByTimeout);
- {% endif %}
- browserStorage.set(key, draft);
- feedback('{% trans "Draft successfully saved" %}', 'success', 1000);
- }, 5000);
+ browserStorage.set(key, draft);
+ saved_tip.removeClass('hide');
}
- $('#file-edit-cancel').click(function() {
- browserStorage.remove(key);
- });
-
- var saved_draft = browserStorage.get(key);
- if (saved_draft !== null) {
- $('#load-draft').modal({
- onClose : function() {
- $.modal.close();
- browserStorage.remove(key);
- }
- });
- $('#simplemodal-container').css({'height':'auto'});
- $('#load-draft-btn').click(function() {
- $.modal.close();
- {% if filetype == 'Sf' %}
- $('#sf').html(saved_draft);
- {% endif %}
- {% if filetype == 'Text' %}
- editor.setValue(saved_draft);
- {% endif %}
- {% if filetype == 'Markdown' %}
- $('#wmd-input').val(saved_draft);
- {% endif %}
- feedback('{% trans "Draft successfully loaded" %}', 'success', 1000);
- browserStorage.set(key, saved_draft);
- });
- }
{% if filetype == 'Sf' %}
- $('#sf').on('keydown', saveDraftByTimeout);
+ $('#sf').on('keyup', saveDraft);
{% endif %}
{% if filetype == 'Markdown' %}
- $('#wmd-input').on('keydown', saveDraftByTimeout);
+ $('#wmd-input').on('keyup', saveDraft);
{% endif %}
+ $('#load-draft-btn').click(function() {
+ $.modal.close();
+ {% if filetype == 'Sf' %}
+ $('#sf').html(saved_draft);
+ {% endif %}
+ {% if filetype == 'Text' %}
+ editor.setValue(saved_draft);
+ {% endif %}
+ {% if filetype == 'Markdown' %}
+ $('#wmd-input').val(saved_draft);
+ {% endif %}
+ feedback('{% trans "Draft successfully loaded" %}', 'success', 1000);
+ });
+
+ $('#discard-draft-btn, #file-edit-cancel').click(function() {
+ browserStorage.remove(key);
+ });
}
{% if filetype == 'Text' %}
@@ -441,8 +390,10 @@ var editor = CodeMirror.fromTextArea($('#docu-view')[0], {
},
autofocus: true,
onKeyEvent: function(instance, event) {
- if (event.type === 'keydown' && md_key_event_on) {
- saveDraftByTimeout();
+ if (event.type == 'keyup') {
+ if ('localStorage' in window && window['localStorage'] !== null) {
+ saveDraft();
+ }
}
}
});
@@ -575,9 +526,10 @@ function editSubmit() {
beforeSend: prepareCSRFToken,
data: {content: content, encoding: $('#file-enc').val()},
success: function(data) {
+ if ('localStorage' in window && window['localStorage'] !== null) {
+ browserStorage.remove(key);
+ }
location.href = data['href'];
- var key = browserStorage.current_url;
- browserStorage.remove(key);
},
error: function(xhr, textStatus, errorThrown) {
if (xhr.responseText) {