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

This commit is contained in:
Joe Fleming 2019-01-28 11:47:57 -07:00 committed by GitHub
parent 4f652d8110
commit 7581d7f8ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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);
}
};