Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Respect bookmark position when adding bookmarks
Browse files Browse the repository at this point in the history
Fix #11177
  • Loading branch information
bbondy committed Sep 27, 2017
1 parent 82edb7f commit 8c5cde4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/browser/reducers/sitesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 20 additions & 8 deletions app/renderer/components/bookmarks/bookmarksToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
},

Expand Down
86 changes: 86 additions & 0 deletions test/unit/app/browser/reducers/sitesReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 8c5cde4

Please sign in to comment.