UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

86 lines (79 loc) 1.71 kB
/** @jsx createElement */ 'use strict'; import { createElement, Component, findDOMNode, setNativeProps, PropTypes } from 'rax'; const isWeb = typeof callNative !== 'function'; function createEle(type, opts = {}, children) { const el = document.createElement(type); if (children) { if (!Array.isArray(children)) { children = [children]; } children.forEach((child) => { child && el.appendChild(child); }); } setNativeProps(el, opts); return el; } function append(ele) { document.body.appendChild(ele); } function ready(callback) { if (callback) { callbacks = callbacks || []; callbacks.push(callback); } if (document.body) { clearInterval(timer); timer = null; while (callbacks.length) { callbacks.shift()(); } } else if (!timer) { timer = setInterval(bodyReady, 16); } } function getRect(ref) { return new Promise((resolve, reject) => { const res = { width: null, height: null, isSuccess: false, }; if (!ref) { reject(res); } const domEl = findDOMNode(ref); if (domEl) { if (isWeb) { const size = domEl.getBoundingClientRect(); resolve({ isSuccess: true, ...size, }); } } else { try { const dom = require('@weex-module/dom'); dom.getComponentRect(domEl.ref, (e) => { if (e && e.size) { resolve({ isSuccess: true, ...e.size, }); } else { reject(res); } }); } catch (e) { reject(res); } } }); } export default { createEle, append, ready, getRect, };