weex-nuke
Version:
基于 Rax 、Weex 的高性能组件体系 ~~
155 lines (141 loc) • 4.35 kB
JavaScript
/** @jsx createElement */
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
*/
;
/**
* 一个实例中相同的iconfont资源只需被调用一次,如果对iconfont有二次封装会导致调用次数过多在客户端出现闪动
* 单例模式 相同的url算一个单例
*/
const isWeex = typeof callNative === 'function';
const ICON_CACHE_KEY = '_nuke_icon_cache_';
class Iconfont {
init(options) {
const { url, name } = options;
if (!url || !name) return;
let urlFit = url;
if (isWeex) {
if (url.indexOf('//at.alicdn.com') === 0) {
urlFit = `https:${urlFit}`;
}
const domModule = require('@weex-module/dom');
domModule.addRule('fontFace', {
fontFamily: name,
src: `url('${urlFit}')`,
});
} else {
if (isDefined(name, urlFit)) return;
const css = `@font-face {font-family: '${name}';src: url('${urlFit}');}`;
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
cacheDefineResult(name, urlFit);
}
}
}
/**
* check if this font family has been defined
* @param {*} name iconfontName
* @param {*} url iconfontURL
*/
function isDefined(name, url) {
if (isWeex) return false;
const CacheArray = window[ICON_CACHE_KEY] && window[ICON_CACHE_KEY][name] ? window[ICON_CACHE_KEY][name] : null;
if (!CacheArray) return false;
if ('name' in CacheArray && 'url' in CacheArray && CacheArray.name === name && CacheArray.url === url) {
return true;
}
return false;
}
/**
*
* @param {*} name
* @param {*} url
*/
function cacheDefineResult(name, url) {
if (isWeex) return;
window[ICON_CACHE_KEY] = window[ICON_CACHE_KEY] || {};
window[ICON_CACHE_KEY][name] = { name, url };
}
/**
* @param {string/obj} unicode e6DD;\e6dd;
*/
function formatUnicode(unicode) {
if (typeof unicode !== 'string' && process.env.NODE_ENV === 'development') {
console.warn('formatUnicode only support <string> arguments');
}
let tmpUnicode = unicode.raw || unicode;
if (tmpUnicode.indexOf('&#x') === 0) {
tmpUnicode = fromCodePoint(parseInt(tmpUnicode.replace('&#x', '0x').replace(';', ''), 16));
} else if (tmpUnicode.indexOf('\\u') === 0) {
tmpUnicode = unicode;
} else {
tmpUnicode = fromCodePoint(
parseInt(
`0x${tmpUnicode
.replace(/(e[\w\d]{3})/, '$1')
.replace(/\\/, '')
.replace(';', '')}`,
16
)
);
}
return tmpUnicode;
}
function fromCodePoint(_) {
if (String.fromCodePoint) {
return String.fromCodePoint(_);
}
const stringFromCharCode = String.fromCharCode;
const floor = Math.floor;
const MAX_SIZE = 0x4000;
const codeUnits = [];
let highSurrogate;
let lowSurrogate;
let index = -1;
const length = arguments.length;
if (!length) {
return "";
}
let result = "";
while (++index < length) {
let codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError("Invalid code point: " + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
}
function Output(params) {
new Iconfont().init(params);
}
Output.isDefined = isDefined;
Output.cacheDefineResult = cacheDefineResult;
Output.formatUnicode = formatUnicode;
export default Output;