Add auto-save to WYSIWYG editor
This commit is contained in:
parent
a7190795f7
commit
a96d4474ef
|
@ -14,15 +14,40 @@ import {EditorState} from "prosemirror-state"
|
||||||
import {schema, defaultMarkdownParser, defaultMarkdownSerializer} from "prosemirror-markdown"
|
import {schema, defaultMarkdownParser, defaultMarkdownSerializer} from "prosemirror-markdown"
|
||||||
import {exampleSetup} from "prosemirror-example-setup"
|
import {exampleSetup} from "prosemirror-example-setup"
|
||||||
|
|
||||||
|
let $title = document.querySelector('#title')
|
||||||
|
let $content = document.querySelector('#content')
|
||||||
|
|
||||||
class ProseMirrorView {
|
class ProseMirrorView {
|
||||||
constructor(target, content) {
|
constructor(target, content) {
|
||||||
this.view = new EditorView(target, {
|
this.view = new EditorView(target, {
|
||||||
state: EditorState.create({
|
state: EditorState.create({
|
||||||
doc: defaultMarkdownParser.parse(content),
|
doc: function(content) {
|
||||||
|
// console.log('loading '+window.draftKey)
|
||||||
|
let localDraft = localStorage.getItem(window.draftKey);
|
||||||
|
if (localDraft != null) {
|
||||||
|
content = localDraft
|
||||||
|
}
|
||||||
|
if (content.indexOf("# ") === 0) {
|
||||||
|
let eol = content.indexOf("\n");
|
||||||
|
let title = content.substring("# ".length, eol);
|
||||||
|
content = content.substring(eol+"\n\n".length);
|
||||||
|
$title.value = title;
|
||||||
|
}
|
||||||
|
return defaultMarkdownParser.parse(content)
|
||||||
|
}(content),
|
||||||
plugins: exampleSetup({schema})
|
plugins: exampleSetup({schema})
|
||||||
}),
|
}),
|
||||||
dispatchTransaction(transaction) {
|
dispatchTransaction(transaction) {
|
||||||
document.querySelector('#content').value = defaultMarkdownSerializer.serialize(transaction.doc)
|
// console.log('saving to '+window.draftKey)
|
||||||
|
$content.value = defaultMarkdownSerializer.serialize(transaction.doc)
|
||||||
|
localStorage.setItem(window.draftKey, function() {
|
||||||
|
let draft = "";
|
||||||
|
if ($title.value != null && $title.value !== "") {
|
||||||
|
draft = "# "+$title.value+"\n\n"
|
||||||
|
}
|
||||||
|
draft += $content.value
|
||||||
|
return draft
|
||||||
|
}());
|
||||||
let newState = this.state.apply(transaction)
|
let newState = this.state.apply(transaction)
|
||||||
this.updateState(newState)
|
this.updateState(newState)
|
||||||
}
|
}
|
||||||
|
@ -37,7 +62,7 @@ class ProseMirrorView {
|
||||||
}
|
}
|
||||||
|
|
||||||
let place = document.querySelector("#editor")
|
let place = document.querySelector("#editor")
|
||||||
let view = new ProseMirrorView(place, document.querySelector('#content').value)
|
let view = new ProseMirrorView(place, $content.value)
|
||||||
|
|
||||||
// document.querySelectorAll("input[type=radio]").forEach(button => {
|
// document.querySelectorAll("input[type=radio]").forEach(button => {
|
||||||
// button.addEventListener("change", () => {
|
// button.addEventListener("change", () => {
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -70,7 +70,6 @@
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<script src="/js/prose.bundle.js"></script>
|
|
||||||
<script src="/js/h.js"></script>
|
<script src="/js/h.js"></script>
|
||||||
<script>
|
<script>
|
||||||
function toggleTheme() {
|
function toggleTheme() {
|
||||||
|
@ -124,7 +123,11 @@
|
||||||
};
|
};
|
||||||
{{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.loadClassic($title, $writer, draftDoc, true);
|
|
||||||
|
// ProseMirror editor
|
||||||
|
window.draftKey = draftDoc;
|
||||||
|
|
||||||
|
// H.loadClassic($title, $writer, draftDoc, true);
|
||||||
updateWordCount();
|
updateWordCount();
|
||||||
|
|
||||||
var typingTimer;
|
var typingTimer;
|
||||||
|
@ -254,11 +257,6 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
setButtonStates();
|
setButtonStates();
|
||||||
$title.on('keyup input', function() {
|
|
||||||
setButtonStates();
|
|
||||||
clearTimeout(typingTimer);
|
|
||||||
typingTimer = setTimeout(doneTyping, doneTypingInterval);
|
|
||||||
}, false);
|
|
||||||
$title.on('keydown', function(e) {
|
$title.on('keydown', function(e) {
|
||||||
if (e.keyCode == 13) {
|
if (e.keyCode == 13) {
|
||||||
if (e.metaKey || e.ctrlKey) {
|
if (e.metaKey || e.ctrlKey) {
|
||||||
|
@ -269,6 +267,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
/*
|
||||||
$writer.on('keyup input', function() {
|
$writer.on('keyup input', function() {
|
||||||
setButtonStates();
|
setButtonStates();
|
||||||
clearTimeout(typingTimer);
|
clearTimeout(typingTimer);
|
||||||
|
@ -280,6 +279,7 @@
|
||||||
$btnPublish.el.click();
|
$btnPublish.el.click();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
$btnPublish.on('click', function(e) {
|
$btnPublish.on('click', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (!publishing && $content.el.value) {
|
if (!publishing && $content.el.value) {
|
||||||
|
@ -394,6 +394,7 @@
|
||||||
// whatevs
|
// whatevs
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
<script src="/js/prose.bundle.js"></script>
|
||||||
<link href="/css/icons.css" rel="stylesheet">
|
<link href="/css/icons.css" rel="stylesheet">
|
||||||
</body>
|
</body>
|
||||||
</html>{{end}}
|
</html>{{end}}
|
||||||
|
|
Loading…
Reference in New Issue