markdown preserved between edit/load/save

This commit is contained in:
Rob Loranger 2020-03-11 07:59:22 -07:00
parent ee712bbfaa
commit b3a541ab09
No known key found for this signature in database
GPG Key ID: D6F1633A4F0903B8
5 changed files with 14 additions and 26 deletions

File diff suppressed because one or more lines are too long

View File

@ -10,9 +10,8 @@
// } // }
import {EditorView} from "prosemirror-view" import {EditorView} from "prosemirror-view"
import {EditorState, Plugin} from "prosemirror-state" import {EditorState} from "prosemirror-state"
import {schema, defaultMarkdownParser, import {schema, defaultMarkdownParser, defaultMarkdownSerializer} from "prosemirror-markdown"
defaultMarkdownSerializer} from "prosemirror-markdown"
import {exampleSetup} from "prosemirror-example-setup" import {exampleSetup} from "prosemirror-example-setup"
class ProseMirrorView { class ProseMirrorView {
@ -22,7 +21,7 @@ class ProseMirrorView {
doc: defaultMarkdownParser.parse(content), doc: defaultMarkdownParser.parse(content),
plugins: exampleSetup({schema}) plugins: exampleSetup({schema})
}), dispatchTransaction(transaction) { }), dispatchTransaction(transaction) {
document.querySelector('#content').innerText = defaultMarkdownSerializer.serialize(transaction.doc) document.querySelector('#content').value = defaultMarkdownSerializer.serialize(transaction.doc)
let newState = this.state.apply(transaction) let newState = this.state.apply(transaction)
this.updateState(newState) this.updateState(newState)
} }

View File

@ -127,17 +127,6 @@ var H = {
} }
$el.el.value = val; $el.el.value = val;
}, },
saveText: function($el, key) {
localStorage.setItem(key, $el.el.innerText);
},
loadText: function($el, key, onlyLoadPopulated) {
var val = localStorage.getItem(key);
if (onlyLoadPopulated && val == null) {
// Do nothing
return;
}
$el.el.innerText = val;
},
set: function(key, value) { set: function(key, value) {
localStorage.setItem(key, value); localStorage.setItem(key, value);
}, },

File diff suppressed because one or more lines are too long

View File

@ -99,9 +99,9 @@
var $wc = H.getEl("wc"); var $wc = H.getEl("wc");
var updateWordCount = function() { var updateWordCount = function() {
var words = 0; var words = 0;
var val = $content.el.innerText.trim(); var val = $content.el.value.trim();
if (val != '') { if (val != '') {
words = $content.el.innerText.trim().replace(/\s+/gi, ' ').split(' ').length; words = $content.el.value.trim().replace(/\s+/gi, ' ').split(' ').length;
} }
$wc.el.innerText = words + " word" + (words != 1 ? "s" : ""); $wc.el.innerText = words + " word" + (words != 1 ? "s" : "");
}; };
@ -110,7 +110,7 @@
$btnPublish.el.className = 'disabled'; $btnPublish.el.className = 'disabled';
return; return;
} }
if ($content.el.innerText.length === 0 || (draftDoc != 'lastDoc' && $content.el.innerText == origDoc)) { if ($content.el.value.length === 0 || (draftDoc != 'lastDoc' && $content.el.value == origDoc)) {
$btnPublish.el.className = 'disabled'; $btnPublish.el.className = 'disabled';
} else { } else {
$btnPublish.el.className = ''; $btnPublish.el.className = '';
@ -118,7 +118,7 @@
}; };
{{if .Post.Id}}var draftDoc = 'draft{{.Post.Id}}'; {{if .Post.Id}}var draftDoc = 'draft{{.Post.Id}}';
var origDoc = '{{.Post.Content}}';{{else}}var draftDoc = 'lastDoc';{{end}} var origDoc = '{{.Post.Content}}';{{else}}var draftDoc = 'lastDoc';{{end}}
H.loadText($content, draftDoc, true); H.load($content, draftDoc, true);
updateWordCount(); updateWordCount();
var typingTimer; var typingTimer;
@ -261,8 +261,8 @@
}); });
$btnPublish.on('click', function(e) { $btnPublish.on('click', function(e) {
e.preventDefault(); e.preventDefault();
if (!publishing && $content.el.innerText) { if (!publishing && $content.el.value) {
var content = $content.el.innerText; var content = $content.el.value;
publish(content, selectedFont); publish(content, selectedFont);
} }
}); });
@ -345,13 +345,13 @@
} }
var doneTyping = function() { var doneTyping = function() {
if (draftDoc == 'lastDoc' || $content.el.innerText != origDoc) { if (draftDoc == 'lastDoc' || $content.el.value != origDoc) {
H.saveText($content, draftDoc); H.save($content, draftDoc);
updateWordCount(); updateWordCount();
} }
}; };
window.addEventListener('beforeunload', function(e) { window.addEventListener('beforeunload', function(e) {
if (draftDoc != 'lastDoc' && $content.el.innerText == origDoc) { if (draftDoc != 'lastDoc' && $content.el.value == origDoc) {
H.remove(draftDoc); H.remove(draftDoc);
} else if (!justPublished) { } else if (!justPublished) {
doneTyping(); doneTyping();