Skip to content

Commit

Permalink
feat(hippy-vue): export parseColor api for HippyVue
Browse files Browse the repository at this point in the history
  • Loading branch information
RonkTsang authored and xuqingkuang committed Jul 2, 2020
1 parent ad5af63 commit a354c94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/hippy-vue/src/renderer/element-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-param-reassign */

import colorParser from '@css-loader/color-parser';
import ViewNode from './view-node';
import { updateChild, updateWithChildren } from './native';
import { getViewMeta, normalizeElementName } from '../elements';
Expand Down Expand Up @@ -132,7 +131,7 @@ class ElementNode extends ViewNode {
break;
case 'caretColor':
case 'caret-color':
this.attributes['caret-color'] = colorParser(value);
this.attributes['caret-color'] = Native.parseColor(value);
break;
default:
this.attributes[key] = tryConvertNumber(value);
Expand Down Expand Up @@ -171,14 +170,14 @@ class ElementNode extends ViewNode {
}
break;
case 'caretColor':
this.attributes['caret-color'] = colorParser(value);
this.attributes['caret-color'] = Native.parseColor(value);
break;
default: {
if (typeof v === 'string') {
v = value.trim();
// Convert inline color style to int
if (property.toLowerCase().indexOf('color') >= 0) {
v = colorParser(v, Native.Platform);
v = Native.parseColor(v);
// Convert inline length style, drop the px unit
} else if (endsWith(v, 'px')) {
v = parseFloat(value.slice(0, value.length - 2));
Expand Down
19 changes: 19 additions & 0 deletions packages/hippy-vue/src/runtime/native.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import colorParser from '@css-loader/color-parser';

import { isDef } from 'shared/util';

import {
HIPPY_VUE_VERSION,
warn,
Expand Down Expand Up @@ -327,6 +330,22 @@ const Native = {
}));
return Promise.race([timeout, measure]);
},

/**
* parse the color to int32Color which native can understand.
* @param { String | Number } color
* @param { {platform: "ios" | "android"} } options
* @returns { Number } int32Color
*/
parseColor(color, options = { platform: Native.Platform }) {
const cache = CACHE.COLOR_PARSER || (CACHE.COLOR_PARSER = Object.create(null));
if (!cache[color]) {
const int32Color = colorParser(color, options);
// cache the calculation result
cache[color] = int32Color;
}
return cache[color];
},
};

// Public export
Expand Down

0 comments on commit a354c94

Please sign in to comment.