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#9611

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Aug 7, 2017
1 parent 1bab9a7 commit a2fc1f6
Show file tree
Hide file tree
Showing 6 changed files with 18,169 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/browser/reducers/bookmarksReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ const bookmarksReducer = (state, action, immutableAction) => {
state = bookmarksState.removeBookmark(state, bookmarkKey)
}
state = bookmarkUtil.updateActiveTabBookmarked(state)
break
}
case appConstants.APP_ON_BOOKMARK_TEXT_CALC:
{
const values = action.get('values') || Immutable.List()

if (values == null) {
break
}

values.forEach((item) => {
state = bookmarksState.setTextWidth(state, item.get('key'), item.get('width'))
})

break
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/common/state/bookmarksState.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ const bookmarksState = {

const cache = bookmarkOrderCache.getBookmarksByParentId(state, folderKey)
return cache.map((item) => bookmarksState.getBookmark(state, item.get('key')))
},

setTextWidth: (state, bookmarkKey, width) => {
let bookmark = bookmarksState.getBookmark(state, bookmarkKey)

if (bookmark.isEmpty()) {
return state
}

return state.setIn([STATE_SITES.BOOKMARKS, bookmarkKey, 'width'], width)

}
}

Expand Down
11 changes: 11 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,17 @@ const appActions = {
destinationKey,
prepend
})
},

/**
* Dispatches a message that saves new text width values
* @param values{Array} - Contains objects with key and width
*/
onBookmarkTextCalc: function (values) {
dispatch({
actionType: appConstants.APP_ON_BOOKMARK_TEXT_CALC,
values
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ const appConstants = {
APP_REMOVE_BOOKMARK_FOLDER: _,
APP_REMOVE_BOOKMARK: _,
APP_MOVE_BOOKMARK_FOLDER: _,
APP_MOVE_BOOKMARK: _
APP_MOVE_BOOKMARK: _,
APP_ON_BOOKMARK_TEXT_CALC: _
}

module.exports = mapValuesByKeys(appConstants)
40 changes: 40 additions & 0 deletions js/stores/windowStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const windowActions = require('../actions/windowActions')
const bookmarkFoldersState = require('../../app/common/state/bookmarkFoldersState')
const bookmarksState = require('../../app/common/state/bookmarksState')
const bookmarkUtil = require('../../app/common/lib/bookmarkUtil')
const textCalculator = require('../lib/textCalculator')

let windowState = Immutable.fromJS({
activeFrameKey: null,
Expand Down Expand Up @@ -816,6 +817,45 @@ const doAction = (action) => {
}
break
}
case appConstants.APP_ADD_BOOKMARK:
case appConstants.APP_EDIT_BOOKMARK:
{
const bookmark = action.siteDetail
let result = Immutable.List()

if (bookmark == null) {
break
}

if (Immutable.List.isList(bookmark)) {
action.get('siteDetail', Immutable.List()).forEach((item) => {
if (item.get('parentId', 0) === 0) {
const key = bookmarkUtil.getKey(item)
result = result.push({
key: key,
width: textCalculator.calculateTextWidth(bookmark.get('title') || bookmark.get('location'))
})
}
})
} else {
if (bookmark.get('parentId', 0) === 0) {
const key = bookmarkUtil.getKey(bookmark)
result = result.push({
key: key,
width: textCalculator.calculateTextWidth(bookmark.get('title') || bookmark.get('location'))
})
}
}

appActions.onBookmarkTextCalc(result)
break
}
case appConstants.APP_ADD_BOOKMARK_FOLDER:
case appConstants.APP_EDIT_BOOKMARK_FOLDER:
{
console.log(textCalculator.calculateTextWidth('test'))
break
}
default:
break
}
Expand Down
Loading

0 comments on commit a2fc1f6

Please sign in to comment.