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

View File

@ -127,17 +127,6 @@ var H = {
}
$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) {
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 updateWordCount = function() {
var words = 0;
var val = $content.el.innerText.trim();
var val = $content.el.value.trim();
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" : "");
};
@ -110,7 +110,7 @@
$btnPublish.el.className = 'disabled';
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';
} else {
$btnPublish.el.className = '';
@ -118,7 +118,7 @@
};
{{if .Post.Id}}var draftDoc = 'draft{{.Post.Id}}';
var origDoc = '{{.Post.Content}}';{{else}}var draftDoc = 'lastDoc';{{end}}
H.loadText($content, draftDoc, true);
H.load($content, draftDoc, true);
updateWordCount();
var typingTimer;
@ -261,8 +261,8 @@
});
$btnPublish.on('click', function(e) {
e.preventDefault();
if (!publishing && $content.el.innerText) {
var content = $content.el.innerText;
if (!publishing && $content.el.value) {
var content = $content.el.value;
publish(content, selectedFont);
}
});
@ -345,13 +345,13 @@
}
var doneTyping = function() {
if (draftDoc == 'lastDoc' || $content.el.innerText != origDoc) {
H.saveText($content, draftDoc);
if (draftDoc == 'lastDoc' || $content.el.value != origDoc) {
H.save($content, draftDoc);
updateWordCount();
}
};
window.addEventListener('beforeunload', function(e) {
if (draftDoc != 'lastDoc' && $content.el.innerText == origDoc) {
if (draftDoc != 'lastDoc' && $content.el.value == origDoc) {
H.remove(draftDoc);
} else if (!justPublished) {
doneTyping();