This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Moves bookmark text calculation into the state
- Loading branch information
Showing
33 changed files
with
1,809 additions
and
312 deletions.
There are no files selected for viewing
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,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 | ||
} |
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
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,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 |
Oops, something went wrong.