Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure selection mark tags are always cleaned up #97

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {map} from 'immutable-array-methods';
import {setIn} from 'immutable-object-methods';
import objectAssign from 'object-assign';
import patchDom from './patch-dom';
import {diffChars} from 'diff';

function parseEmbed (type) {
return (elm) => {
Expand Down Expand Up @@ -93,9 +94,15 @@ const setup = ({customTextFormattings, parseFigureProps, customCaption} = {}) =>
nextElement[id] = next;
}

return itemsHasUpdated ||
const shouldUpdate = itemsHasUpdated ||
props.selections !== nextProps.selections ||
props.contenteditable !== nextProps.contenteditable;

if (!shouldUpdate) {
// To clean up selection mark tags
restoreSelection(currentElement[id]);
}
return shouldUpdate;
},
afterRender: ({props: {items, selections = true}, id}, oldArticleElm) => {
currentElement[id] = oldArticleElm;
Expand All @@ -105,11 +112,17 @@ const setup = ({customTextFormattings, parseFigureProps, customCaption} = {}) =>
assert(newArticleElm, 'newArticleElm must exists');

if (oldArticleElm.innerHTML !== newArticleElm.innerHTML) {
console.log('PATCHED:');
diffChars(oldArticleElm.innerHTML, newArticleElm.innerHTML).forEach((part) => {
if (part.added || part.removed) {
console.log(part.added ? 'added: ' : 'removed: ', part.value);
}
});
patchDom({oldArticleElm, newArticleElm});
}

if (selections) {
restoreSelection(oldArticleElm);
}
if (selections) {
restoreSelection(oldArticleElm);
}
}
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"article-json-html-render": "^2.5.0",
"deku": "^1.0.0",
"diff": "^3.2.0",
"dift": "^0.1.12",
"embeds": "^2.5.1",
"get-selection-range-from-elm": "^2.0.1",
Expand Down
16 changes: 14 additions & 2 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ if (process.browser) {
markClass: 'selection-end'
}]
}];

const app = tree(<ArticleJsonToContenteditable items={initialItems} onUpdate={onUpdate}/>);
const container = renderAppInContainer(app);
const secondParagraph = container.querySelectorAll('article p')[1];
Expand All @@ -339,11 +340,22 @@ if (process.browser) {
t.deepEqual(items, expected);
}

t.is(container.querySelector('mark.selection-start'), null, 'No selection tag in dom after render');
t.is(container.querySelector('mark.selection-end'), null, 'No selection tag in dom after render');

setSelection(secondParagraph, 0, secondParagraph, 1);
container.querySelector('article').dispatchEvent(mouseup());
t.ok(onUpdateCalled, 'onUpdate was called');
app.unmount();
t.end();

app.mount(<ArticleJsonToContenteditable items={initialItems} onUpdate={onUpdate}/>);

process.nextTick(() => {
t.is(container.querySelector('mark.selection-start'), null, 'No selection tag in dom after update');
t.is(container.querySelector('mark.selection-end'), null, 'No selection tag in dom after update');

app.unmount();
t.end();
});
});

test('<ArticleJsonToContenteditable> no saved selections on blur', t => {
Expand Down