Files
flipper/src/utils/icons.js
Daniel Büchele 771be72b3f remove window dependency
Summary:
Fixes required to be able to run Flipper in node.js:
* Adds checks if the `window`-object exists before using it, to allow running in node.
* Imports from within Flipper should directly reference the file they are requiring instead of `import from 'flipper'`. This was done in most of the places. Fixed a few occurrences where this wasn't the case. This is to prevent cyclic dependencies in node.
* shared packages (React, ReactDOM and Flipper) were exposed on the `window` before, changed this to `global` as this works in browser and node.
* Adds some missing methods to our electron stubs (used for testing and headless Flipper)

Reviewed By: passy

Differential Revision: D13786577

fbshipit-source-id: 145d560f1446e7d0bdec2acd8dd54dae983d7b36
2019-01-25 12:19:07 -08:00

120 lines
2.4 KiB
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
// list of icons that are prefetched in the service worker when launching the app
export const precachedIcons: Array<string> = [
{
name: 'arrow-right',
size: 12,
},
{
name: 'caution-octagon',
},
{
name: 'caution-triangle',
},
{
name: 'info-circle',
},
{
name: 'magic-wand',
size: 20,
},
{
name: 'magnifying-glass',
},
{
name: 'minus-circle',
size: 12,
},
{
name: 'mobile',
size: 12,
},
{
name: 'bug',
size: 12,
},
{
name: 'posts',
size: 20,
},
{
name: 'rocket',
size: 20,
},
{
name: 'tools',
size: 20,
},
{
name: 'triangle-down',
size: 12,
},
{
name: 'triangle-right',
size: 12,
},
{
name: 'chevron-right',
size: 8,
},
{
name: 'chevron-down',
size: 8,
},
].map(icon => getIconUrl(icon.name, icon.size || undefined));
export function getIconUrl(
name: string,
size?: number = 16,
variant?: 'filled' | 'outline' = 'filled',
): string {
if (name.indexOf('/') > -1) {
return name;
}
const AVAILABLE_SIZES = [8, 10, 12, 16, 18, 20, 24, 32];
const SCALE = [1, 1.5, 2, 3, 4];
let requestedSize: number = size;
if (!AVAILABLE_SIZES.includes(size)) {
// find the next largest size
const possibleSize: ?number = AVAILABLE_SIZES.find(size => {
return size > requestedSize;
});
// set to largest size if the real size is larger than what we have
if (possibleSize == null) {
requestedSize = Math.max(...AVAILABLE_SIZES);
} else {
requestedSize = possibleSize;
}
}
let requestedScale: number =
typeof window !== 'undefined' ? window.devicePixelRatio : 1;
if (!SCALE.includes(requestedScale)) {
// find the next largest size
const possibleScale: ?number = SCALE.find(scale => {
return scale > requestedScale;
});
// set to largest size if the real size is larger than what we have
if (possibleScale == null) {
requestedScale = Math.max(...SCALE);
} else {
requestedScale = possibleScale;
}
}
return `https://external.xx.fbcdn.net/assets/?name=${name}&variant=filled&size=${requestedSize}&set=facebook_icons&density=${requestedScale}x${
variant == 'outline' ? '&variant=outline' : ''
}`;
}