Skip to content
Snippets Groups Projects
Commit 980f75bd authored by chris's avatar chris
Browse files

fix noteEditor updating main

parent 5f353101
No related branches found
No related tags found
1 merge request!166Fixes
/* eslint react/prop-types: 0 */ /* eslint react/prop-types: 0 */
import React, { useEffect, useRef, useContext } from 'react'; import React, { useEffect, useRef, useContext, useState } from 'react';
import { filter } from 'lodash'; import { filter } from 'lodash';
import { EditorView } from 'prosemirror-view'; import { EditorView } from 'prosemirror-view';
import { EditorState, TextSelection } from 'prosemirror-state'; import { EditorState, TextSelection } from 'prosemirror-state';
...@@ -18,6 +18,8 @@ export default ({ node, view }) => { ...@@ -18,6 +18,8 @@ export default ({ node, view }) => {
const context = useContext(WaxContext); const context = useContext(WaxContext);
const noteId = node.attrs.id; const noteId = node.attrs.id;
let noteView; let noteView;
let updateMainView = true;
useEffect(() => { useEffect(() => {
noteView = new EditorView( noteView = new EditorView(
{ mount: editorRef.current }, { mount: editorRef.current },
...@@ -41,6 +43,9 @@ export default ({ node, view }) => { ...@@ -41,6 +43,9 @@ export default ({ node, view }) => {
transformPasted: slice => { transformPasted: slice => {
return transformPasted(slice, noteView); return transformPasted(slice, noteView);
}, },
handleKeyPress: (noteEditorView, from, to, content) => {
updateMainView = false;
},
}, },
); );
...@@ -80,21 +85,24 @@ export default ({ node, view }) => { ...@@ -80,21 +85,24 @@ export default ({ node, view }) => {
}); });
// TODO Remove timeout and use state to check if noteView has changed // TODO Remove timeout and use state to check if noteView has changed
setTimeout(() => { if (updateMainView) {
context.updateView({}, noteId); setTimeout(() => {
}, 20); context.updateView({}, noteId);
}, 20);
}
if (!tr.getMeta('fromOutside')) { if (!tr.getMeta('fromOutside')) {
const outerTr = view.state.tr; const outerTr = view.state.tr;
const offsetMap = StepMap.offset(noteFound[0].pos + 1); const offsetMap = StepMap.offset(noteFound[0].pos + 1);
for (let i = 0; i < transactions.length; i++) { for (let i = 0; i < transactions.length; i++) {
let steps = transactions[i].steps; let { steps } = transactions[i];
for (let j = 0; j < steps.length; j++) for (let j = 0; j < steps.length; j++)
outerTr.step(steps[j].map(offsetMap)); outerTr.step(steps[j].map(offsetMap));
} }
if (outerTr.docChanged) if (outerTr.docChanged)
view.dispatch(outerTr.setMeta('outsideView', 'notes')); view.dispatch(outerTr.setMeta('outsideView', 'notes'));
updateMainView = true;
} }
}; };
...@@ -115,11 +123,11 @@ export default ({ node, view }) => { ...@@ -115,11 +123,11 @@ export default ({ node, view }) => {
}; };
if (context.view[noteId]) { if (context.view[noteId]) {
let state = context.view[noteId].state; const { state } = context.view[noteId];
let start = node.content.findDiffStart(state.doc.content); const start = node.content.findDiffStart(state.doc.content);
if (start != null) { if (start != null) {
let { a: endA, b: endB } = node.content.findDiffEnd(state.doc.content); let { a: endA, b: endB } = node.content.findDiffEnd(state.doc.content);
let overlap = start - Math.min(endA, endB); const overlap = start - Math.min(endA, endB);
if (overlap > 0) { if (overlap > 0) {
endA += overlap; endA += overlap;
endB += overlap; endB += overlap;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment