Load fonts using FontFaceObserver

This commit is contained in:
Ken-Håvard Lieng 2017-05-12 10:51:37 +02:00
parent 4ee035766b
commit 9bc518fbe9
8 changed files with 69 additions and 63 deletions

File diff suppressed because one or more lines are too long

View File

@ -41,6 +41,7 @@
"autolinker": "^1.4.3", "autolinker": "^1.4.3",
"backo": "^1.1.0", "backo": "^1.1.0",
"base64-arraybuffer": "^0.1.5", "base64-arraybuffer": "^0.1.5",
"fontfaceobserver": "^2.0.9",
"history": "4.5.1", "history": "4.5.1",
"immutable": "^3.8.1", "immutable": "^3.8.1",
"lodash": "^4.17.4", "lodash": "^4.17.4",

View File

@ -74,12 +74,12 @@ export function fetchMessages() {
}; };
} }
export function updateMessageHeight() { export function updateMessageHeight(wrapWidth, charWidth) {
return (dispatch, getState) => dispatch({ return {
type: actions.UPDATE_MESSAGE_HEIGHT, type: actions.UPDATE_MESSAGE_HEIGHT,
wrapWidth: getState().environment.get('wrapWidth'), wrapWidth,
charWidth: getState().environment.get('charWidth') charWidth
}); };
} }
export function sendMessage(content, to, server) { export function sendMessage(content, to, server) {

View File

@ -12,27 +12,14 @@ import { part } from '../actions/channel';
import { openPrivateChat, closePrivateChat } from '../actions/privateChat'; import { openPrivateChat, closePrivateChat } from '../actions/privateChat';
import { searchMessages, toggleSearch } from '../actions/search'; import { searchMessages, toggleSearch } from '../actions/search';
import { select } from '../actions/tab'; import { select } from '../actions/tab';
import { runCommand, sendMessage, updateMessageHeight, fetchMessages } from '../actions/message'; import { runCommand, sendMessage, fetchMessages } from '../actions/message';
import { disconnect } from '../actions/server'; import { disconnect } from '../actions/server';
import { setWrapWidth, setCharWidth } from '../actions/environment';
import { stringWidth } from '../util';
import { toggleUserList } from '../actions/ui'; import { toggleUserList } from '../actions/ui';
import * as inputHistoryActions from '../actions/inputHistory'; import * as inputHistoryActions from '../actions/inputHistory';
import { getSelectedTab } from '../reducers/tab'; import { getSelectedTab } from '../reducers/tab';
import { getSelectedMessages } from '../reducers/messages'; import { getSelectedMessages } from '../reducers/messages';
function updateCharWidth() {
const charWidth = stringWidth(' ', '16px Roboto Mono, monospace');
window.messageIndent = 6 * charWidth;
return setCharWidth(charWidth);
}
class Chat extends PureComponent { class Chat extends PureComponent {
componentWillMount() {
const { dispatch } = this.props;
setTimeout(() => dispatch(updateCharWidth()), 1000);
}
handleSearch = phrase => { handleSearch = phrase => {
const { dispatch, tab } = this.props; const { dispatch, tab } = this.props;
if (tab.isChannel()) { if (tab.isChannel()) {
@ -82,8 +69,6 @@ class Chat extends PureComponent {
messages={messages} messages={messages}
hasMoreMessages={hasMoreMessages} hasMoreMessages={hasMoreMessages}
tab={tab} tab={tab}
setWrapWidth={this.props.setWrapWidth}
updateMessageHeight={this.props.updateMessageHeight}
onNickClick={this.handleMessageNickClick} onNickClick={this.handleMessageNickClick}
onFetchMore={this.handleFetchMore} onFetchMore={this.handleFetchMore}
/> />
@ -169,9 +154,7 @@ function mapDispatchToProps(dispatch) {
part, part,
disconnect, disconnect,
openPrivateChat, openPrivateChat,
closePrivateChat, closePrivateChat
setWrapWidth,
updateMessageHeight
}, dispatch), }, dispatch),
inputActions: bindActionCreators(inputHistoryActions, dispatch) inputActions: bindActionCreators(inputHistoryActions, dispatch)
}; };

View File

@ -49,12 +49,12 @@ if (env.users) {
}); });
} }
initWidthUpdates(store); initWidthUpdates(store, () => {
if (env.messages) {
if (env.messages) { const { messages, server, to, next } = env.messages;
const { messages, server, to, next } = env.messages; store.dispatch(addMessages(messages, server, to, false, next));
store.dispatch(addMessages(messages, server, to, false, next)); }
} });
const renderRoot = () => { const renderRoot = () => {
render( render(

View File

@ -5,5 +5,11 @@ import * as actions from '../actions';
export default createReducer(Map(), { export default createReducer(Map(), {
[actions.SET_ENVIRONMENT](state, action) { [actions.SET_ENVIRONMENT](state, action) {
return state.set(action.key, action.value); return state.set(action.key, action.value);
},
[actions.UPDATE_MESSAGE_HEIGHT](state, action) {
return state
.set('wrapWidth', action.wrapWidth)
.set('charWidth', action.charWidth);
} }
}); });

View File

@ -1,6 +1,6 @@
import FontFaceObserver from 'fontfaceobserver';
import { stringWidth, measureScrollBarWidth } from './index'; import { stringWidth, measureScrollBarWidth } from './index';
import { updateMessageHeight } from '../actions/message'; import { updateMessageHeight } from '../actions/message';
import { setCharWidth, setWrapWidth } from '../actions/environment';
const lineHeight = 24; const lineHeight = 24;
const menuWidth = 200; const menuWidth = 200;
@ -9,16 +9,12 @@ const messagePadding = 30;
const smallScreen = 600; const smallScreen = 600;
let windowWidth; let windowWidth;
export function initWidthUpdates(store) { function init(store, charWidth, done) {
const scrollBarWidth = measureScrollBarWidth();
const charWidth = stringWidth(' ', '16px Roboto Mono, monospace');
window.messageIndent = 6 * charWidth; window.messageIndent = 6 * charWidth;
store.dispatch(setCharWidth(charWidth)); const scrollBarWidth = measureScrollBarWidth();
let prevWrapWidth; let prevWrapWidth;
function updateWidth(delta, first) { function updateWidth() {
windowWidth = window.innerWidth; windowWidth = window.innerWidth;
let wrapWidth = windowWidth - scrollBarWidth - messagePadding; let wrapWidth = windowWidth - scrollBarWidth - messagePadding;
if (windowWidth > smallScreen) { if (windowWidth > smallScreen) {
@ -27,11 +23,7 @@ export function initWidthUpdates(store) {
if (wrapWidth !== prevWrapWidth) { if (wrapWidth !== prevWrapWidth) {
prevWrapWidth = wrapWidth; prevWrapWidth = wrapWidth;
store.dispatch(updateMessageHeight(wrapWidth, charWidth));
store.dispatch(setWrapWidth(wrapWidth));
if (!first) {
store.dispatch(updateMessageHeight());
}
} }
} }
@ -44,10 +36,30 @@ export function initWidthUpdates(store) {
resizeRAF = window.requestAnimationFrame(updateWidth); resizeRAF = window.requestAnimationFrame(updateWidth);
} }
updateWidth(0, true); updateWidth();
done();
window.addEventListener('resize', resize); window.addEventListener('resize', resize);
} }
export function initWidthUpdates(store, done) {
let charWidth = localStorage.charWidth;
if (charWidth) {
init(store, parseFloat(charWidth), done);
}
new FontFaceObserver('Roboto Mono').load().then(() => {
if (!charWidth) {
charWidth = stringWidth(' ', '16px Roboto Mono');
init(store, charWidth, done);
localStorage.charWidth = charWidth;
}
});
new FontFaceObserver('Montserrat').load();
new FontFaceObserver('Montserrat', { weight: 700 }).load();
new FontFaceObserver('Roboto Mono', { weight: 700 }).load();
}
export function findBreakpoints(text) { export function findBreakpoints(text) {
const breakpoints = []; const breakpoints = [];

View File

@ -2377,6 +2377,10 @@ flatten@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782" resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
fontfaceobserver@^2.0.9:
version "2.0.9"
resolved "https://registry.yarnpkg.com/fontfaceobserver/-/fontfaceobserver-2.0.9.tgz#fb61e1053bd28d38cd4750a05b30f5130b32e97a"
for-in@^1.0.1: for-in@^1.0.1:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"