UNPKG

1.5 kBJavaScriptView Raw
1// import { isObject } from './utils';
2
3/**
4 * 计算图片宽高
5 * @param {number} width
6 * @param {number} height
7 * @param {number} containerWidth
8 * @return {object<widht: number, height: number>}
9 */
10export function computedImageSize(width, height, containerWidth) {
11 if (width <= containerWidth) {
12 return { width, height };
13 } else {
14 return {
15 width: containerWidth,
16 height: parseInt((height / width) * containerWidth, 10)
17 };
18 }
19}
20
21/**
22 * 获得文本块最后一行,并移除
23 * @parma {array<object>} 文本块
24 * @return {array<object>} 最后一行
25 */
26export function helperGetLastLIne(textList) {
27 let results = [];
28
29 for (let i = textList.length - 1; i >= 0; i--) {
30 let inlineText = textList[i].content;
31 let match = inlineText.match(/\n(.*?)$/);
32 if (match) {
33 let { 1: matched, index } = match;
34
35 // 后置文本清理
36 textList[i].content = inlineText.substr(0, index + 1);
37 results.push({ ...textList[i], content: matched });
38
39 break;
40 } else {
41 results.push(textList[i]);
42 // 清理textList末尾
43 textList.pop();
44 }
45 }
46 return results.reverse();
47}
48
49/**
50 * 事件绑定
51 * @param {HTMLElement} el 绑定dom
52 * @param {string} event 事件名
53 * @param {function} func 回调函数
54 * @return {function} 解绑函数
55 */
56export function addEvent(el, event, func) {
57 const callback = e => func(e);
58 el.addEventListener(event, callback);
59 return () => el.removeEventListener(event, callback);
60}