Skip to content

Commit

Permalink
cache onselectstart
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanArmstrong committed Aug 22, 2017
1 parent 791f069 commit 3cd91b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/components/drag-and-drop/with-drag/__spec__.js
Expand Up @@ -74,6 +74,17 @@ describe('WithDrag', () => {
backend.simulateBeginDrag([handlerId]);
expect(BrowserHelper.getDocument().onselectstart()).toBeFalsy();
});

it('caches text selection', () => {
spyOn(BrowserHelper, 'getDocument').and.returnValue({ onselectstart: () => { return 'cache' } });
backend.simulateBeginDrag([handlerId]);

expect(BrowserHelper.getDocument().onselectstart()).toBeFalsy();

backend.simulateEndDrag([handlerId]);

expect(BrowserHelper.getDocument().onselectstart()).toEqual('cache');
});
});

describe('endDrag', () => {
Expand All @@ -83,11 +94,12 @@ describe('WithDrag', () => {
expect(endDragPropSpy).not.toHaveBeenCalled();
expect(endDragContextSpy).toHaveBeenCalled();
});

it('enables text selection', () => {
spyOn(BrowserHelper, 'getDocument').and.returnValue({});
backend.simulateBeginDrag([handlerId]);
backend.simulateEndDrag([handlerId]);
expect(BrowserHelper.getDocument().onselectstart).toBeNull();
expect(BrowserHelper.getDocument().onselectstart).toBeFalsy();
});
});
});
Expand Down
12 changes: 10 additions & 2 deletions src/components/drag-and-drop/with-drag/with-drag.js
Expand Up @@ -52,21 +52,29 @@ class WithDrag extends React.Component {
}
}

var cachedSelectStart = null;
const setSelectStartCache = () => { cachedSelectStart = BrowserHelper.getDocument().onselectstart; }
const getSelectStartCache = () => { return cachedSelectStart; }
const clearSelectStartCache = () => { cachedSelectStart = null; }

const ItemSource = {
canDrag(props, monitor) {
return (props.canDrag) ? props.canDrag(props, monitor) : true;
},

beginDrag(props, monitor, component) {
setSelectStartCache()
// Disable Text selection in Safari to show correct cursor
BrowserHelper.getDocument().onselectstart = function() { return false; };
BrowserHelper.getDocument().onselectstart = () => { return false; };

This comment has been minimized.

Copy link
@paulsturgess

paulsturgess Aug 22, 2017

Now these are a bit more complicated is it worth grouping them together.

Could be worth creating functions: disableTextSelection() and enableTextSelection() calling those from beginDrag and endDrag respectively.

const beginDrag = props.beginDrag || component.context.dragAndDropBeginDrag;
return beginDrag(props, monitor, component);
},

endDrag(props, monitor, component) {
// Enable Text selection in Safari
BrowserHelper.getDocument().onselectstart = null;
BrowserHelper.getDocument().onselectstart = getSelectStartCache();
clearSelectStartCache();

const endDrag = props.endDrag || component.context.dragAndDropEndDrag;
return endDrag(props, monitor, component);
}
Expand Down

0 comments on commit 3cd91b5

Please sign in to comment.