1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-04-06 10:46:57 +00:00

feat: write to a tiddler

This commit is contained in:
lin onetwo 2025-03-25 22:32:23 +08:00
parent d262030cf4
commit 348427733b
3 changed files with 25 additions and 13 deletions

View File

@ -0,0 +1,12 @@
title: $:/plugins/tiddlywiki/prosemirror/example
# asdf
# asdf
* This is an unordered list
* It has two items
# This is a numbered list
## With a subitem
## With second subitem
# And a third item

View File

@ -2,4 +2,6 @@ title: $:/plugins/tiddlywiki/prosemirror/readme
Test `<$prosemirror />` widget below, see console for content JSON.
<$prosemirror />
<$edit-prosemirror tiddler="$:/plugins/tiddlywiki/prosemirror/example"/>
<$edit-text tiddler="$:/plugins/tiddlywiki/prosemirror/example" class="tc-edit-texteditor"/>

View File

@ -42,6 +42,14 @@ ProsemirrorWidget.prototype.render = function(parent,nextSibling) {
this.parentDomNode = parent;
this.computeAttributes();
this.execute();
var tiddler = this.getAttribute("tiddler");
var initialText = this.wiki.getTiddlerText(tiddler, "");
var initialWikiAst = $tw.wiki.parseText(null, initialText).tree;
var doc = wikiAstToProseMirrorAst(initialWikiAst);
// DEBUG: console doc
console.log(`initial doc`, doc);
var container = $tw.utils.domMaker('div', {
class: 'tc-prosemirror-container',
});
@ -56,18 +64,6 @@ ProsemirrorWidget.prototype.render = function(parent,nextSibling) {
var listPlugins = createListPlugins({ schema })
var self = this;
var wikiAst = $tw.wiki.parseText(null, `# asdf
# asdf
* This is an unordered list
* It has two items
# This is a numbered list
## With a subitem
# And a third item`).tree;
var doc = wikiAstToProseMirrorAst(wikiAst);
// DEBUG: console doc
console.log(`initial doc`, doc);
this.view = new EditorView(container, {
state: EditorState.create({
// doc: schema.node("doc", null, [schema.node("paragraph")]),
@ -97,6 +93,8 @@ ProsemirrorWidget.prototype.saveEditorContent = function() {
console.log(`WikiAST: ${JSON.stringify(wikiast)}`, wikiast);
var wikiText = $tw.utils.serializeParseTree(wikiast);
console.log(`WikiText: ${wikiText}`);
var tiddler = this.getAttribute("tiddler");
this.wiki.setText(tiddler, "text", undefined, wikiText);
}
// Debounced save function for performance