diff --git a/app/browser/reducers/sitesReducer.js b/app/browser/reducers/sitesReducer.js index fefb6768717..17dde85f0fb 100644 --- a/app/browser/reducers/sitesReducer.js +++ b/app/browser/reducers/sitesReducer.js @@ -96,7 +96,7 @@ const sitesReducer = (state, action, immutableAction) => { if (closestKey != null) { const sourceKey = siteUtil.getSiteKey(site) - state = siteUtil.moveSite(state, sourceKey, closestKey, false, false, true) + state = siteUtil.moveSite(state, sourceKey, closestKey, !!action.isLeftSide, false, true) } if (isSyncEnabled) { diff --git a/app/renderer/components/bookmarks/bookmarksToolbar.js b/app/renderer/components/bookmarks/bookmarksToolbar.js index 5ec443ffa8a..485a668282c 100644 --- a/app/renderer/components/bookmarks/bookmarksToolbar.js +++ b/app/renderer/components/bookmarks/bookmarksToolbar.js @@ -48,15 +48,18 @@ class BookmarksToolbar extends React.Component { onDrop (e) { e.preventDefault() - const bookmark = dnd.prepareBookmarkDataFromCompatible(e.dataTransfer) - if (bookmark) { - // Figure out the droppedOn element filtering out the source drag item - let droppedOn = dnd.closestFromXOffset(this.bookmarkRefs.filter((bookmarkRef) => { + const getClosestFromPos = (clientX, sourceKey) => + dnd.closestFromXOffset(this.bookmarkRefs.filter((bookmarkRef) => { if (!bookmarkRef) { return false } - return bookmarkRef.props.bookmarkKey !== bookmark.get('bookmarkKey') + return bookmarkRef.props.bookmarkKey !== sourceKey }), e.clientX) + const bookmark = dnd.prepareBookmarkDataFromCompatible(e.dataTransfer) + if (bookmark) { + // Figure out the droppedOn element filtering out the source drag item + const bookmarkKey = bookmark.get('bookmarkKey') + const droppedOn = getClosestFromPos(e.clientX, bookmarkKey) if (droppedOn.selectedRef) { const isLeftSide = dnd.isLeftSide(ReactDOM.findDOMNode(droppedOn.selectedRef), e.clientX) const droppedOnKey = droppedOn.selectedRef.props.bookmarkKey @@ -66,6 +69,15 @@ class BookmarksToolbar extends React.Component { } return } + + const droppedOn = getClosestFromPos(e.clientX, undefined) + let isLeftSide = false + let closestKey + if (droppedOn.selectedRef) { + closestKey = droppedOn.selectedRef.props.bookmarkKey + isLeftSide = dnd.isLeftSide(ReactDOM.findDOMNode(droppedOn.selectedRef), e.clientX) + } + const droppedHTML = e.dataTransfer.getData('text/html') if (droppedHTML) { const parser = new window.DOMParser() @@ -75,14 +87,14 @@ class BookmarksToolbar extends React.Component { appActions.addBookmark({ title: a.innerText, location: e.dataTransfer.getData('text/plain') - }, siteTags.BOOKMARK) + }, siteTags.BOOKMARK, closestKey, isLeftSide) return } } if (e.dataTransfer.files.length > 0) { Array.from(e.dataTransfer.items).forEach((item) => { - item.getAsString((name) => appActions.addBookmark({ location: item.type, title: name }, siteTags.BOOKMARK)) + item.getAsString((name) => appActions.addBookmark({ location: item.type, title: name }, siteTags.BOOKMARK, closestKey, isLeftSide)) }) return } @@ -92,7 +104,7 @@ class BookmarksToolbar extends React.Component { .map((x) => x.trim()) .filter((x) => !x.startsWith('#') && x.length > 0) .forEach((url) => - appActions.addBookmark({ location: url }, siteTags.BOOKMARK)) + appActions.addBookmark({ location: url }, siteTags.BOOKMARK, closestKey, isLeftSide)) } onDragEnter (e) { diff --git a/js/actions/appActions.js b/js/actions/appActions.js index a1cc4660b4c..130820c22e2 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -1517,12 +1517,13 @@ const appActions = { }) }, - addBookmark: function (siteDetail, tag, closestKey) { + addBookmark: function (siteDetail, tag, closestKey, isLeftSide = false) { dispatch({ actionType: appConstants.APP_ADD_BOOKMARK, siteDetail, tag, - closestKey + closestKey, + isLeftSide }) }, diff --git a/test/unit/app/browser/reducers/sitesReducerTest.js b/test/unit/app/browser/reducers/sitesReducerTest.js index c63d752b73c..6a63a2b1336 100644 --- a/test/unit/app/browser/reducers/sitesReducerTest.js +++ b/test/unit/app/browser/reducers/sitesReducerTest.js @@ -332,6 +332,92 @@ describe('sitesReducerTest', function () { const result = sitesReducer(state, action) assert.deepEqual(result.get('sites').toJS(), newSites) }) + + it('add a bookmark with a close bookmark prepending', function () { + const state = initState.set('sites', makeImmutable({ + 'https://www.clifton.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 0, + location: 'https://www.brave.com', + parentFolderId: 0 + }, + 'https://www.bbondy.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 1, + location: 'https://www.bbondy.io', + parentFolderId: 0 + }, + 'https://www.bridiver.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 2, + location: 'https://www.bridiver.io', + parentFolderId: 0 + } + })) + + const action = { + actionType: appConstants.APP_ADD_BOOKMARK, + siteDetail: makeImmutable({ + parentFolderId: 0, + title: 'Brave', + location: 'https://www.brave.com' + }), + tag: siteTags.BOOKMARK, + closestKey: 'https://www.bbondy.io|0|0', + isLeftSide: true + } + + const newSites = { + 'https://www.clifton.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 0, + location: 'https://www.brave.com', + parentFolderId: 0 + }, + 'https://www.bbondy.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 2, + location: 'https://www.bbondy.io', + parentFolderId: 0 + }, + 'https://www.brave.com|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 1, + location: 'https://www.brave.com', + parentFolderId: 0 + }, + 'https://www.bridiver.io|0|0': { + lastAccessedTime: 0, + tags: [siteTags.BOOKMARK], + objectId: undefined, + title: 'Brave', + order: 3, + location: 'https://www.bridiver.io', + parentFolderId: 0 + } + } + + const result = sitesReducer(state, action) + assert.deepEqual(result.get('sites').toJS(), newSites) + }) }) describe('APP_EDIT_BOOKMARK', function () {