2020-03-10 22:11:32 +00:00
|
|
|
// class MarkdownView {
|
|
|
|
// constructor(target, content) {
|
|
|
|
// this.textarea = target.appendChild(document.createElement("textarea"))
|
|
|
|
// this.textarea.value = content
|
|
|
|
// }
|
|
|
|
|
|
|
|
// get content() { return this.textarea.value }
|
|
|
|
// focus() { this.textarea.focus() }
|
|
|
|
// destroy() { this.textarea.remove() }
|
|
|
|
// }
|
|
|
|
|
|
|
|
import {EditorView} from "prosemirror-view"
|
2020-03-11 14:59:22 +00:00
|
|
|
import {EditorState} from "prosemirror-state"
|
|
|
|
import {schema, defaultMarkdownParser, defaultMarkdownSerializer} from "prosemirror-markdown"
|
2020-03-10 22:11:32 +00:00
|
|
|
import {exampleSetup} from "prosemirror-example-setup"
|
|
|
|
|
2020-09-09 21:46:47 +00:00
|
|
|
let $title = document.querySelector('#title')
|
|
|
|
let $content = document.querySelector('#content')
|
|
|
|
|
2020-03-10 22:11:32 +00:00
|
|
|
class ProseMirrorView {
|
2020-09-09 14:02:00 +00:00
|
|
|
constructor(target, content) {
|
|
|
|
this.view = new EditorView(target, {
|
|
|
|
state: EditorState.create({
|
2020-09-09 21:46:47 +00:00
|
|
|
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),
|
2020-09-09 14:02:00 +00:00
|
|
|
plugins: exampleSetup({schema})
|
|
|
|
}),
|
|
|
|
dispatchTransaction(transaction) {
|
2020-09-09 21:46:47 +00:00
|
|
|
// 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
|
|
|
|
}());
|
2020-09-09 14:02:00 +00:00
|
|
|
let newState = this.state.apply(transaction)
|
|
|
|
this.updateState(newState)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-10 22:11:32 +00:00
|
|
|
|
2020-09-09 14:02:00 +00:00
|
|
|
get content() {
|
|
|
|
return defaultMarkdownSerializer.serialize(this.view.state.doc)
|
|
|
|
}
|
|
|
|
focus() { this.view.focus() }
|
|
|
|
destroy() { this.view.destroy() }
|
2020-03-10 22:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let place = document.querySelector("#editor")
|
2020-09-09 21:46:47 +00:00
|
|
|
let view = new ProseMirrorView(place, $content.value)
|
2020-03-10 22:11:32 +00:00
|
|
|
|
|
|
|
// document.querySelectorAll("input[type=radio]").forEach(button => {
|
|
|
|
// button.addEventListener("change", () => {
|
|
|
|
// if (!button.checked) return
|
|
|
|
// let View = button.value == "markdown" ? MarkdownView : ProseMirrorView
|
|
|
|
// if (view instanceof View) return
|
|
|
|
// let content = view.content
|
|
|
|
// view.destroy()
|
|
|
|
// view = new View(place, content)
|
|
|
|
// view.focus()
|
|
|
|
// })
|
|
|
|
// })
|