fix: remove text selection on writeable change (#28887)

This commit is contained in:
Joe Fleming 2019-01-28 07:48:00 -07:00 committed by Robert Monfera
parent e3b8e56d14
commit 7eba079e1f

View file

@ -6,9 +6,13 @@
import { duplicatePage } from '../actions/pages';
import { fetchRenderable } from '../actions/elements';
import { getPages } from '../selectors/workpad';
import { setWriteable } from '../actions/workpad';
import { getPages, isWriteable } from '../selectors/workpad';
import { getWindow } from '../../lib/get_window';
export const workpadUpdate = ({ dispatch, getState }) => next => action => {
const oldIsWriteable = isWriteable(getState());
next(action);
// This middleware fetches all of the renderable elements on new, duplicate page
@ -20,4 +24,15 @@ export const workpadUpdate = ({ dispatch, getState }) => next => action => {
// For each element on that page, dispatch the action to update it
return newPage.elements.forEach(element => dispatch(fetchRenderable(element)));
}
// This middleware clears any page selection when the writeable mode changes
if (action.type === setWriteable.toString() && oldIsWriteable !== isWriteable(getState())) {
const win = getWindow();
if (typeof win.getSelection !== 'function') {
return;
}
win.getSelection().collapse(document.querySelector('body'), 0);
}
};