Skip to content

Commit

Permalink
Moves bookmark text calculation into the state
Browse files Browse the repository at this point in the history
Resolves brave#9517

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Aug 15, 2017
1 parent 0a599c6 commit 59405bb
Show file tree
Hide file tree
Showing 28 changed files with 1,653 additions and 269 deletions.
2 changes: 2 additions & 0 deletions app/browser/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,10 @@ const doAction = (state, action) => {
}
case appConstants.APP_ADD_BOOKMARK:
case appConstants.APP_EDIT_BOOKMARK:
case appConstants.APP_MOVE_BOOKMARK:
case appConstants.APP_REMOVE_BOOKMARK:
case appConstants.APP_ADD_BOOKMARK_FOLDER:
case appConstants.APP_MOVE_BOOKMARK_FOLDER:
case appConstants.APP_EDIT_BOOKMARK_FOLDER:
case appConstants.APP_REMOVE_BOOKMARK_FOLDER:
createMenu(state)
Expand Down
48 changes: 41 additions & 7 deletions app/browser/reducers/bookmarkFoldersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ const Immutable = require('immutable')

// State
const bookmarkFoldersState = require('../../common/state/bookmarkFoldersState')
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const siteTags = require('../../../js/constants/siteTags')
const {STATE_SITES} = require('../../../js/constants/stateConstants')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const syncUtil = require('../../../js/state/syncUtil')
const textCalcUtil = require('../../common/lib/textCalcUtil')
const bookmarkFolderUtil = require('../../common/lib/bookmarkFoldersUtil')

const bookmarkFoldersReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
Expand All @@ -29,12 +33,16 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(folder)) {
action.get('folderDetails', Immutable.List()).forEach((folder) => {
state = bookmarkFoldersState.addFolder(state, folder, closestKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
const folderDetails = bookmarkFolderUtil.buildFolder(folder, bookmarkFoldersState.getFolders(state))
state = bookmarkFoldersState.addFolder(state, folderDetails, closestKey)
state = syncUtil.updateObjectCache(state, folderDetails, STATE_SITES.BOOKMARK_FOLDERS)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
})
} else {
state = bookmarkFoldersState.addFolder(state, folder, closestKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
const folderDetails = bookmarkFolderUtil.buildFolder(folder, bookmarkFoldersState.getFolders(state))
state = bookmarkFoldersState.addFolder(state, folderDetails, closestKey)
state = syncUtil.updateObjectCache(state, folderDetails, STATE_SITES.BOOKMARK_FOLDERS)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
}
break
}
Expand All @@ -49,7 +57,8 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

state = bookmarkFoldersState.editFolder(state, key, folder)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)

const folderDetails = bookmarkFoldersState.getFolder(state, key)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
break
}
case appConstants.APP_MOVE_BOOKMARK_FOLDER:
Expand All @@ -70,6 +79,9 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

const destinationDetail = bookmarkFoldersState.getFolder(state, action.get('destinationKey'))
state = syncUtil.updateObjectCache(state, destinationDetail, STATE_SITES.BOOKMARK_FOLDERS)
if (destinationDetail.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
case appConstants.APP_REMOVE_BOOKMARK_FOLDER:
Expand All @@ -82,16 +94,38 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(folderKey)) {
action.get('folderKey', Immutable.List()).forEach((key) => {
const folder = state.getIn([STATE_SITES.BOOKMARK_FOLDERS, key])
const folder = bookmarkFoldersState.getFolder(state, key)
state = bookmarkFoldersState.removeFolder(state, key)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
})
state = bookmarkToolbarState.setToolbars(state)
} else {
const folder = state.getIn([STATE_SITES.BOOKMARK_FOLDERS, folderKey])
const folder = bookmarkFoldersState.getFolder(state, folderKey)
state = bookmarkFoldersState.removeFolder(state, folderKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
if (folder.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
}
break
}
case appConstants.APP_ON_BOOKMARK_FOLDER_WIDTH_CHANGED:
{
if (action.get('folderList', Immutable.List()).isEmpty()) {
break
}

let updateToolbar = false
action.get('folderList').forEach(item => {
state = bookmarkFoldersState.setWidth(state, item.get('key'), item.get('width'))
if (item.get('parentFolderId') === 0) {
updateToolbar = true
}
})

if (updateToolbar) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
}
Expand Down
37 changes: 37 additions & 0 deletions app/browser/reducers/bookmarkToolbarReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* 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/. */

// Constants
const appConstants = require('../../../js/constants/appConstants')

// State
const bookmarksState = require('../../common/state/bookmarksState')
const bookmarkFoldersState = require('../../common/state/bookmarkFoldersState')

// Util
const {makeImmutable} = require('../../common/state/immutableUtil')
const textCalcUtil = require('../../common/lib/textCalcUtil')

const tabsReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_SET_STATE:
{
// update session for 0.21.x version
const bookmarks = bookmarksState.getBookmarks(state)
if (bookmarks.first() && !bookmarks.first().has('width')) {
textCalcUtil.calcTextList(bookmarks.toList())
}

const bookmarkFolders = bookmarkFoldersState.getFolders(state)
if (bookmarkFolders.first() && !bookmarkFolders.first().has('width')) {
textCalcUtil.calcTextList(bookmarkFolders.toList())
}
}
break
}
return state
}

module.exports = tabsReducer
52 changes: 47 additions & 5 deletions app/browser/reducers/bookmarksReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ const Immutable = require('immutable')

// State
const bookmarksState = require('../../common/state/bookmarksState')
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const siteTags = require('../../../js/constants/siteTags')
const {STATE_SITES} = require('../../../js/constants/stateConstants')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const syncUtil = require('../../../js/state/syncUtil')
const bookmarkUtil = require('../../common/lib/bookmarkUtil')
const bookmarkLocationCache = require('../../common/cache/bookmarkLocationCache')
const textCalcUtil = require('../../common/lib/textCalcUtil')

const bookmarksReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
Expand All @@ -34,12 +37,16 @@ const bookmarksReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(bookmark)) {
action.get('siteDetail', Immutable.List()).forEach((bookmark) => {
state = bookmarksState.addBookmark(state, bookmark, closestKey)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
const bookmarkDetail = bookmarkUtil.buildBookmark(state, bookmark)
state = bookmarksState.addBookmark(state, bookmarkDetail, closestKey)
state = syncUtil.updateObjectCache(state, bookmarkDetail, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)
})
} else {
state = bookmarksState.addBookmark(state, bookmark, closestKey)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
const bookmarkDetail = bookmarkUtil.buildBookmark(state, bookmark)
state = bookmarksState.addBookmark(state, bookmarkDetail, closestKey)
state = syncUtil.updateObjectCache(state, bookmarkDetail, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)
}

state = bookmarkUtil.updateActiveTabBookmarked(state)
Expand All @@ -54,8 +61,15 @@ const bookmarksReducer = (state, action, immutableAction) => {
break
}

state = bookmarksState.editBookmark(state, key, bookmark)
const oldBookmark = bookmarksState.getBookmark(state, key)
if (oldBookmark.isEmpty()) {
break
}

const bookmarkDetail = bookmarkUtil.buildEditBookmark(oldBookmark, bookmark)
state = bookmarksState.editBookmark(state, oldBookmark, bookmarkDetail)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)

state = bookmarkUtil.updateActiveTabBookmarked(state)
break
Expand All @@ -78,6 +92,10 @@ const bookmarksReducer = (state, action, immutableAction) => {

const destinationDetail = bookmarksState.getBookmark(state, action.get('destinationKey'))
state = syncUtil.updateObjectCache(state, destinationDetail, STATE_SITES.BOOKMARKS)

if (destinationDetail.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
case appConstants.APP_REMOVE_BOOKMARK:
Expand All @@ -91,12 +109,36 @@ const bookmarksReducer = (state, action, immutableAction) => {
action.get('bookmarkKey', Immutable.List()).forEach((key) => {
state = bookmarksState.removeBookmark(state, key)
})
state = bookmarkToolbarState.setToolbars(state)
} else {
const bookmark = bookmarksState.getBookmark(state, bookmarkKey)
if (bookmark.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
state = bookmarksState.removeBookmark(state, bookmarkKey)
}
state = bookmarkUtil.updateActiveTabBookmarked(state)
break
}
case appConstants.APP_ON_BOOKMARK_WIDTH_CHANGED:
{
if (action.get('bookmarkList', Immutable.List()).isEmpty()) {
break
}

let updateToolbar = false
action.get('bookmarkList').forEach(item => {
state = bookmarksState.setWidth(state, item.get('key'), item.get('width'))
if (item.get('parentFolderId') === 0) {
updateToolbar = true
}
})

if (updateToolbar) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
}

return state
Expand Down
18 changes: 15 additions & 3 deletions app/browser/reducers/windowsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
const path = require('path')
const assert = require('assert')
const Immutable = require('immutable')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')

// State
const windowState = require('../../common/state/windowState')

// Utils
const windows = require('../windows')
const sessionStoreShutdown = require('../../sessionStoreShutdown')
const {makeImmutable, isImmutable} = require('../../common/state/immutableUtil')
Expand Down Expand Up @@ -305,12 +311,18 @@ const windowsReducer = (state, action, immutableAction) => {
state = windowState.removeWindow(state, action)
sessionStoreShutdown.removeWindowFromCache(action.getIn(['windowValue', 'windowId']))
break
case appConstants.APP_WINDOW_CREATED:
state = windowState.maybeCreateWindow(state, action)
break
case appConstants.APP_WINDOW_UPDATED:
state = windowState.maybeCreateWindow(state, action)
break
case appConstants.APP_WINDOW_CREATED:
case appConstants.APP_WINDOW_RESIZED:
{
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')
state = windowState.maybeCreateWindow(state, action)
const windowId = action.getIn(['windowValue', 'windowId'], windowState.WINDOW_ID_NONE)
state = bookmarkToolbarState.setToolbar(state, windowId)
break
}
case appConstants.APP_DEFAULT_WINDOW_PARAMS_CHANGED:
if (action.get('size')) {
state = state.setIn(['defaultWindowParams', 'width'], action.getIn(['size', 0]))
Expand Down
8 changes: 5 additions & 3 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,19 @@ const api = {
})
},

executeScriptInBackground: (script, cb) => {
executeScriptInBackground: (script, cb, debug = false) => {
const win = new BrowserWindow({
show: false,
show: debug,
webPreferences: {
partition: 'default'
}
})
win.webContents.on('did-finish-load', (e) => {
win.webContents.executeScriptInTab(config.braveExtensionId, script, {}, (err, url, result) => {
cb(err, url, result)
setImmediate(() => win.close())
if (!debug) {
setImmediate(() => win.close())
}
})
})
win.loadURL('about:blank')
Expand Down
10 changes: 9 additions & 1 deletion app/browser/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ const updateWindow = (windowId) => {
}
}

const onWindowResize = (windowId) => {
const windowValue = getWindowValue(windowId)
if (windowValue) {
appActions.onWindowResize(windowValue)
}
}

const updatePinnedTabs = (win) => {
if (win.webContents.browserWindowOptions.disposition === 'new-popup') {
return
Expand Down Expand Up @@ -115,6 +122,7 @@ const api = {
let windowId = -1
const updateWindowMove = debounce(updateWindow, 100)
const updateWindowDebounce = debounce(updateWindow, 5)
const onWindowResizeDebounce = debounce(onWindowResize, 5)

win.once('initialized', () => {
windowId = win.id
Expand Down Expand Up @@ -220,7 +228,7 @@ const api = {
updateWindowDebounce(windowId)
})
win.on('resize', () => {
updateWindowDebounce(windowId)
onWindowResizeDebounce(windowId)
const size = win.getSize()
const position = win.getPosition()
// NOTE: the default window size is whatever the last window resize was
Expand Down
26 changes: 24 additions & 2 deletions app/common/lib/bookmarkFoldersUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const siteTags = require('../../../js/constants/siteTags')
const {makeImmutable} = require('../state/immutableUtil')

const isFolderNameValid = (title) => {
return title != null && title.trim().length > 0
Expand All @@ -26,7 +27,7 @@ const getNextFolderIdItem = (folders) => {
}

const getNextFolderId = (folders) => {
const defaultFolderId = 0
const defaultFolderId = 1
if (!folders) {
return defaultFolderId
}
Expand Down Expand Up @@ -117,11 +118,32 @@ const isMoveAllowed = (folderList, sourceDetail, destinationDetail) => {
return true
}

const buildFolder = (folderDetails, folders) => {
folderDetails = makeImmutable(folderDetails)
let key = folderDetails.get('folderId')

if (!folderDetails.has('folderId')) {
key = getNextFolderId(folders)
}

return makeImmutable({
title: folderDetails.get('title'),
folderId: ~~key,
key: key.toString(),
parentFolderId: ~~folderDetails.get('parentFolderId', 0),
partitionNumber: ~~folderDetails.get('partitionNumber', 0),
objectId: folderDetails.get('objectId', null),
type: siteTags.BOOKMARK_FOLDER,
skipSync: folderDetails.get('skipSync', null)
})
}

module.exports = {
isFolderNameValid,
getNextFolderId,
getNextFolderName,
isFolder,
getKey,
isMoveAllowed
isMoveAllowed,
buildFolder
}
Loading

0 comments on commit 59405bb

Please sign in to comment.