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

Commit

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

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Sep 26, 2017
1 parent 4f9d9b3 commit 1a7ed9c
Show file tree
Hide file tree
Showing 33 changed files with 1,809 additions and 312 deletions.
142 changes: 142 additions & 0 deletions app/browser/api/textCalc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* 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/. */

const Immutable = require('immutable')

// Actions
const appActions = require('../../../js/actions/appActions')

// Constant
const siteTags = require('../../../js/constants/siteTags')

// Utils
const tabs = require('../../browser/tabs')
const {makeImmutable} = require('../../common/state/immutableUtil')

// Styles
const globalStyles = require('../../renderer/components/styles/global')

const fontSize = globalStyles.spacing.bookmarksItemFontSize
const fontFamily = globalStyles.defaultFontFamily

const calcText = (item, type) => {
const title = type === siteTags.BOOKMARK
? item.get('title') || item.get('location')
: item.get('title')

if (title && title.length === 0) {
return
}

const param = `
(function() {
let ctx = document.createElement('canvas').getContext('2d')
ctx.font = '${fontSize} ${fontFamily}'
const width = ctx.measureText('${title}').width
return width
})()
`

tabs.executeScriptInBackground(param, (err, url, result) => {
if (err) {
throw err
}

if (type === siteTags.BOOKMARK) {
appActions.onBookmarkWidthChanged(Immutable.fromJS([
{
key: item.get('key'),
parentFolderId: item.get('parentFolderId'),
width: result[0]
}
]))
} else {
appActions.onBookmarkFolderWidthChanged(Immutable.fromJS([
{
key: item.get('key'),
parentFolderId: item.get('parentFolderId'),
width: result[0]
}
]))
}
})
}

const calcTextList = (list) => {
const take = 500
list = makeImmutable(list)

if (list.size === 0) {
return
}

let paramList = JSON.stringify(list.take(take))
.replace(/'/g, '!')
.replace(/\\"/g, '!')
.replace(/\\\\/g, '//')

const param = `
(function() {
const ctx = document.createElement('canvas').getContext('2d')
ctx.font = '${fontSize} ${fontFamily}'
const bookmarks = []
const folders = []
const list = JSON.parse('${paramList}')
list.forEach(item => {
if (item.type === '${siteTags.BOOKMARK}') {
bookmarks.push({
key: item.key,
parentFolderId: item.parentFolderId,
width: ctx.measureText(item.title || item.location).width
})
} else {
folders.push({
key: item.key,
parentFolderId: item.parentFolderId,
width: ctx.measureText(item.title).width
})
}
})
const result = {
bookmarks: bookmarks,
folders: folders
}
return JSON.stringify(result)
})()
`

tabs.executeScriptInBackground(param, (err, url, result) => {
if (err) {
console.error('Error in executeScriptInBackground (textCalcUtil.js)')
}

if (result[0]) {
const data = JSON.parse(result[0])
if (data.bookmarks.length > 0) {
appActions.onBookmarkWidthChanged(Immutable.fromJS(data.bookmarks))
}

if (data.folders.length > 0) {
appActions.onBookmarkFolderWidthChanged(Immutable.fromJS(data.folders))
}
} else {
console.error('Error, cant parse bookmarks in executeScriptInBackground')
}

list = list.skip(take)

if (list.size > 0) {
calcTextList(list)
}
})
}

module.exports = {
calcText,
calcTextList
}
2 changes: 2 additions & 0 deletions app/browser/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,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
50 changes: 43 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 textCalc = require('../../browser/api/textCalc')
const bookmarkFolderUtil = require('../../common/lib/bookmarkFoldersUtil')

const bookmarkFoldersReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
Expand All @@ -28,13 +32,19 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {
}

if (Immutable.List.isList(folder)) {
let folderList = Immutable.List()
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)
folderList = folderList.push(folderDetails)
})
textCalc.calcTextList(folderList)
} 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)
textCalc.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
}
break
}
Expand All @@ -49,7 +59,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)
textCalc.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
break
}
case appConstants.APP_MOVE_BOOKMARK_FOLDER:
Expand All @@ -70,6 +81,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 || action.get('destinationKey') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
case appConstants.APP_REMOVE_BOOKMARK_FOLDER:
Expand All @@ -82,16 +96,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 textCalc = require('../../browser/api/textCalc')

const bookmarkToolbarReducer = (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')) {
textCalc.calcTextList(bookmarks.toList())
}

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

module.exports = bookmarkToolbarReducer
Loading

0 comments on commit 1a7ed9c

Please sign in to comment.