-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4996 from brave/bsc-revert-dnd
Revert "Merge pull request #4325 from brave/ca-2971"
- Loading branch information
Showing
45 changed files
with
1,374 additions
and
2,119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
// Copyright (c) 2020 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// you can obtain one at http://mozilla.org/MPL/2.0/. | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { bindActionCreators } from 'redux' | ||
import * as newTabActions from '../actions/new_tab_actions' | ||
import * as gridSitesActions from '../actions/grid_sites_actions' | ||
import store from '../store' | ||
|
||
/** | ||
* Get actions from the C++ back-end down to front-end components | ||
*/ | ||
let actions: typeof newTabActions & typeof gridSitesActions | ||
let actions: typeof newTabActions | ||
export default function getActions () { | ||
if (actions) { | ||
return actions | ||
} | ||
const allActions = Object.assign({}, newTabActions, gridSitesActions) | ||
actions = bindActionCreators(allActions, store.dispatch.bind(store)) | ||
actions = bindActionCreators(newTabActions, store.dispatch.bind(store)) | ||
return actions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import getActions from '../getActions' | ||
|
||
/** | ||
* Obtains the URL's bookmark info and calls an action with the result | ||
*/ | ||
export const fetchBookmarkInfo = (url: string) => { | ||
chrome.bookmarks.search(url.replace(/^https?:\/\//, ''), | ||
(bookmarkTreeNodes) => getActions().bookmarkInfoAvailable(url, bookmarkTreeNodes[0] as NewTab.Bookmark) | ||
) | ||
} | ||
|
||
/** | ||
* Updates bookmark info for top sites based on their state | ||
*/ | ||
export const updateBookmarkInfo = (state: NewTab.State, url: string, bookmarkTreeNode?: NewTab.Bookmark) => { | ||
const bookmarks = state.bookmarks | ||
const gridSites = state.gridSites.slice() | ||
const topSites = state.topSites.slice() | ||
const pinnedTopSites = state.pinnedTopSites.slice() | ||
// The default empty object is just to avoid null checks below | ||
const gridSite: Partial<NewTab.Site> = gridSites.find((s) => s.url === url) || {} | ||
const topSite: Partial<NewTab.Site> = topSites.find((s) => s.url === url) || {} | ||
const pinnedTopSite: Partial<NewTab.Site> = pinnedTopSites.find((s) => s.url === url) || {} | ||
|
||
if (bookmarkTreeNode) { | ||
bookmarks[url] = bookmarkTreeNode | ||
gridSite.bookmarked = topSite.bookmarked = pinnedTopSite.bookmarked = bookmarkTreeNode | ||
} else { | ||
delete bookmarks[url] | ||
gridSite.bookmarked = topSite.bookmarked = pinnedTopSite.bookmarked = undefined | ||
} | ||
state = { ...state, bookmarks, gridSites } | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import * as gridAPI from './grid' | ||
|
||
export const onDraggedSite = (state: NewTab.State, url: string, destUrl: string) => { | ||
const gridSitesWithoutPreview = gridAPI.getGridSites(state) | ||
const currentPositionIndex = gridSitesWithoutPreview.findIndex(site => site.url === url) | ||
const finalPositionIndex = gridSitesWithoutPreview.findIndex(site => site.url === destUrl) | ||
let pinnedTopSites = state.pinnedTopSites.slice() | ||
|
||
// A site that is not pinned yet will become pinned | ||
const pinnedMovingSite = pinnedTopSites.find(site => site.url === url) | ||
if (!pinnedMovingSite) { | ||
const movingTopSite = Object.assign({}, gridSitesWithoutPreview.find(site => site.url === url)) | ||
movingTopSite.index = currentPositionIndex | ||
movingTopSite.pinned = true | ||
pinnedTopSites.push(movingTopSite) | ||
} | ||
|
||
pinnedTopSites = pinnedTopSites.map((pinnedTopSite) => { | ||
pinnedTopSite = Object.assign({}, pinnedTopSite) | ||
const currentIndex = pinnedTopSite.index | ||
if (currentIndex === currentPositionIndex) { | ||
pinnedTopSite.index = finalPositionIndex | ||
} else if (currentIndex > currentPositionIndex && pinnedTopSite.index <= finalPositionIndex) { | ||
pinnedTopSite.index = pinnedTopSite.index - 1 | ||
} else if (currentIndex < currentPositionIndex && pinnedTopSite.index >= finalPositionIndex) { | ||
pinnedTopSite.index = pinnedTopSite.index + 1 | ||
} | ||
return pinnedTopSite | ||
}) | ||
state = { ...state, pinnedTopSites } | ||
state = { ...state, gridSites: gridAPI.getGridSites(state) } | ||
return state | ||
} | ||
|
||
export const onDragEnd = (state: NewTab.State) => { | ||
state = { ...state, gridSites: gridAPI.getGridSites(state) } | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
// API | ||
import getActions from '../getActions' | ||
import * as bookmarksAPI from './bookmarks' | ||
import { getCharForSite } from '../../helpers/newTabUtils' | ||
|
||
// Utils | ||
import { debounce } from '../../../common/debounce' | ||
|
||
export const getGridSites = (state: NewTab.State, checkBookmarkInfo?: boolean) => { | ||
const sizeToCount = { large: 18, medium: 12, small: 6 } | ||
const count = sizeToCount[state.gridLayoutSize || 'small'] | ||
const defaultChromeWebStoreUrl = 'https://chrome.google.com/webstore' | ||
|
||
// Start with top sites with filtered out ignored sites and pinned sites | ||
let gridSites = state.topSites.slice() | ||
.filter((site) => | ||
!state.ignoredTopSites.find((ignoredSite) => ignoredSite.url === site.url) && | ||
!state.pinnedTopSites.find((pinnedSite) => pinnedSite.url === site.url) && | ||
// see https://github.com/brave/brave-browser/issues/5376 | ||
!site.url.startsWith(defaultChromeWebStoreUrl) | ||
) | ||
|
||
// Then add in pinned sites at the specified index, these need to be added in the same | ||
// order as the index they are. | ||
const pinnedTopSites = state.pinnedTopSites | ||
.slice() | ||
.sort((x, y) => x.index - y.index) | ||
pinnedTopSites.forEach((pinnedSite) => { | ||
gridSites.splice(pinnedSite.index, 0, pinnedSite) | ||
}) | ||
|
||
gridSites = gridSites.slice(0, count) | ||
gridSites.forEach((gridSite: NewTab.Site) => { | ||
gridSite.letter = getCharForSite(gridSite) | ||
gridSite.thumb = `chrome://thumb/${gridSite.url}` | ||
gridSite.favicon = `chrome://favicon/size/64@1x/${gridSite.url}` | ||
gridSite.bookmarked = state.bookmarks[gridSite.url] | ||
|
||
if (checkBookmarkInfo && !gridSite.bookmarked) { | ||
bookmarksAPI.fetchBookmarkInfo(gridSite.url) | ||
} | ||
}) | ||
return gridSites | ||
} | ||
|
||
/** | ||
* Calculates the top sites grid and calls an action with the results | ||
*/ | ||
export const calculateGridSites = debounce((state: NewTab.State) => { | ||
// TODO(petemill): | ||
// Instead of debouncing at the point of reducing actions to state, | ||
// and having the reducer call this, it may be more understandable | ||
// (and performant) to have this be a selector so that the calculation | ||
// is only performed when the relevant state data is changed. | ||
getActions().gridSitesUpdated(getGridSites(state, true)) | ||
}, 10) |
8 changes: 5 additions & 3 deletions
8
components/brave_new_tab_ui/api/topSites.ts → ...ts/brave_new_tab_ui/api/topSites/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.