diff --git a/wax-prosemirror-schema/src/nodes/footNoteNode.js b/wax-prosemirror-schema/src/nodes/footNoteNode.js index 6c4667922125c8b551d4526c49744b159d9b50b0..3a1f74070bdafd5820607647f96692637acfeb67 100644 --- a/wax-prosemirror-schema/src/nodes/footNoteNode.js +++ b/wax-prosemirror-schema/src/nodes/footNoteNode.js @@ -3,8 +3,22 @@ const footnote = { content: "inline*", inline: true, atom: true, - toDOM: () => ["footnote", 0], - parseDOM: [{ tag: "footnote" }] + attrs: { + id: { default: "" } + }, + toDOM: node => { + return ["footnote", node.attrs, 0]; + }, + parseDOM: [ + { + tag: "footnote", + getAttrs(dom) { + return { + id: dom.getAttribute("id") + }; + } + } + ] }; export default footnote; diff --git a/wax-prosemirror-services/src/NoteService/Editor.js b/wax-prosemirror-services/src/NoteService/Editor.js index 1ab70ad29368b1a1450c3eb3199f49566eb415bc..2f2964ed247389d7723c0a95de63fbd5d96385f8 100644 --- a/wax-prosemirror-services/src/NoteService/Editor.js +++ b/wax-prosemirror-services/src/NoteService/Editor.js @@ -8,10 +8,12 @@ import { undo, redo } from "prosemirror-history"; import { WaxContext } from "wax-prosemirror-core/src/ioc-react"; import { Commands } from "wax-prosemirror-utilities"; import { NoteEditorContainer } from "wax-prosemirror-components"; +import { filter } from "lodash"; -export default ({ node, view, pos }) => { +export default ({ node, view, allNotes }) => { const editorRef = useRef(); const context = useContext(WaxContext); + const noteId = node.attrs.id; useEffect(() => { const noteView = new EditorView( @@ -26,9 +28,13 @@ export default ({ node, view, pos }) => { let { state, transactions } = noteView.state.applyTransaction(tr); noteView.updateState(state); + const noteFound = filter(allNotes, { + node: { attrs: { id: noteId } } + }); + if (!tr.getMeta("fromOutside")) { let outerTr = view.state.tr, - offsetMap = StepMap.offset(pos + 1); + offsetMap = StepMap.offset(noteFound[0].pos + 1); for (let i = 0; i < transactions.length; i++) { let steps = transactions[i].steps; for (let j = 0; j < steps.length; j++) @@ -48,8 +54,10 @@ export default ({ node, view, pos }) => { } } ); - // noteView.focus(); - context.updateView({ [pos]: noteView }); + + //Set Each note into Wax's Context + context.updateView({ [noteId]: noteView }); + if (context.view[noteId]) context.view[noteId].focus(); }, []); const createKeyBindings = () => { @@ -71,8 +79,8 @@ export default ({ node, view, pos }) => { }; }; - if (context.view[pos]) { - let state = context.view[pos].state; + if (context.view[noteId]) { + let state = context.view[noteId].state; let start = node.content.findDiffStart(state.doc.content); if (start != null) { let { a: endA, b: endB } = node.content.findDiffEnd(state.doc.content); @@ -81,7 +89,7 @@ export default ({ node, view, pos }) => { endA += overlap; endB += overlap; } - context.view[pos].dispatch( + context.view[noteId].dispatch( state.tr .replace(start, endB, node.slice(start, endA)) .setMeta("fromOutside", true) diff --git a/wax-prosemirror-services/src/NoteService/Note.js b/wax-prosemirror-services/src/NoteService/Note.js index 699bbde0557479c48ad5067ca3627a469243c742..3d948d20f3f36eb2c41c653b212bbf300402f640 100644 --- a/wax-prosemirror-services/src/NoteService/Note.js +++ b/wax-prosemirror-services/src/NoteService/Note.js @@ -2,6 +2,7 @@ import Tools from "../lib/Tools"; import { injectable } from "inversify"; import { icons } from "wax-prosemirror-components"; import { Fragment } from "prosemirror-model"; +import { v4 as uuid } from "uuid"; @injectable() export default class Note extends Tools { @@ -17,7 +18,10 @@ export default class Note extends Tools { $from.parentOffset, $to.parentOffset ); - const footnote = state.config.schema.nodes.footnote.create(null, content); + const footnote = state.config.schema.nodes.footnote.create( + { id: uuid() }, + content + ); dispatch(state.tr.replaceSelectionWith(footnote)); }; } diff --git a/wax-prosemirror-services/src/NoteService/NoteEditor.js b/wax-prosemirror-services/src/NoteService/NoteEditor.js index 0e8f197c01e4d8e24a60d72dec6387df1357a293..1c519a09d1fcb52fc38b72d0578cd7aca030ca9c 100644 --- a/wax-prosemirror-services/src/NoteService/NoteEditor.js +++ b/wax-prosemirror-services/src/NoteService/NoteEditor.js @@ -6,9 +6,9 @@ export default ({ notes, view }) => { <div> {notes.map(note => ( <Editor - key={`editor-${note.pos}`} + key={note.node.attrs.id} node={note.node} - pos={note.pos} + allNotes={notes} view={view} /> ))}