Improve JS bindings

Summary:
- Fix a few things related to how npm should publish the package (sources were in the gitignore file, so I think it would have break the build during install - I fixed this by adding a npmignore that should override the gitignore rules)

- The enumerations values are now generated directly from `enums.py`

- I added percent unit support (#258) to the bindings (`.setWidthPercent` is currently exposed, but I've also added a very very little parsing to also support `.setWidth("100%")` and `.setWidth(.getWidth())`), added the missing tests, and fixed Travis.
Closes https://github.com/facebook/yoga/pull/314

Reviewed By: mikearmstrong001

Differential Revision: D4377198

Pulled By: emilsjolander

fbshipit-source-id: 774dfafd416f5421f3be59a1d181eb7056487abe
This commit is contained in:
Maël Nison
2017-01-04 04:33:39 -08:00
committed by Facebook Github Bot
parent 663a93912b
commit 7108454455
12 changed files with 1344 additions and 110 deletions

View File

@@ -19,57 +19,11 @@ function patch(prototype, name, fn) {
module.exports = function (bind, lib) {
let constants = {};
let constants = Object.assign({
constants.ALIGN_AUTO = 0;
constants.ALIGN_CENTER = 2;
constants.ALIGN_FLEX_END = 3;
constants.ALIGN_FLEX_START = 1;
constants.ALIGN_STRETCH = 4;
UNDEFINED: NaN
constants.DIRECTION_INHERIT = 0;
constants.DIRECTION_LTR = 1;
constants.DIRECTION_RTL = 2;
constants.EDGE_ALL = 8;
constants.EDGE_BOTTOM = 3;
constants.EDGE_END = 5;
constants.EDGE_HORIZONTAL = 6;
constants.EDGE_LEFT = 0;
constants.EDGE_RIGHT = 2;
constants.EDGE_START = 4;
constants.EDGE_TOP = 1;
constants.EDGE_VERTICAL = 7;
constants.FEATURE_ROUNDING = 0;
constants.FEATURE_WEB_FLEX_BASIS = 1;
constants.FLEX_DIRECTION_COLUMN = 0;
constants.FLEX_DIRECTION_COLUMN_REVERSE = 1;
constants.FLEX_DIRECTION_ROW = 2;
constants.FLEX_DIRECTION_ROW_REVERSE = 3;
constants.JUSTIFY_CENTER = 1;
constants.JUSTIFY_FLEX_END = 2;
constants.JUSTIFY_FLEX_START = 0;
constants.JUSTIFY_SPACE_AROUND = 4;
constants.JUSTIFY_SPACE_BETWEEN = 3;
constants.MEASURE_MODE_UNDEFINED = 0;
constants.MEASURE_MODE_EXACTLY = 1;
constants.MEASURE_MODE_AT_MOST = 2;
constants.OVERFLOW_HIDDEN = 1;
constants.OVERFLOW_VISIBLE = 0;
constants.OVERFLOW_SCROLL = 2;
constants.POSITION_TYPE_ABSOLUTE = 1;
constants.POSITION_TYPE_RELATIVE = 0;
constants.WRAP_NO_WRAP = 0;
constants.WRAP_WRAP = 1;
constants.UNDEFINED = NaN;
}, require(`./YGEnums`));
class Layout {
@@ -129,6 +83,80 @@ module.exports = function (bind, lib) {
}
class Value {
constructor(unit, value) {
this.unit = unit;
this.value = value;
}
fromJS(expose) {
expose(this.unit, this.value);
}
toString() {
switch (this.unit) {
case constants.UNIT_PIXEL:
return `${this.value}`;
case constants.UNIT_PERCENT:
return `${this.value}%`;
default: {
return `${this.value}?`;
}
}
}
valueOf() {
return this.value;
}
}
for (let fnName of [ `setPosition`, `setMargin`, `setFlexBasis`, `setWidth`, `setHeight`, `setMinWidth`, `setMinHeight`, `setMaxWidth`, `setMaxHeight`, `setPadding` ]) {
let methods = { [constants.UNIT_PIXEL]: lib.Node.prototype[fnName], [constants.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`] };
if (Object.keys(methods).some(method => methods[method] == null))
throw new Error(`Assertion failed; some unit derivates of ${fnName} seem missing`);
patch(lib.Node.prototype, fnName, function (original, ... args) {
// We patch all these functions to add support for the following calls:
// .setWidth(100) / .setWidth("100%") / .setWidth(.getWidth())
let value = args.pop();
let unit, asNumber;
if (value instanceof Value) {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit = typeof value === `string` && value.endsWith(`%`) ? constants.UNIT_PERCENT : constants.UNIT_PIXEL;
asNumber = parseFloat(value);
}
return methods[unit].call(this, ... args, asNumber);
});
}
patch(lib.Node.prototype, `free`, function () {
// Since we handle the memory allocation ourselves (via lib.Node.create), we also need to handle the deallocation
@@ -187,6 +215,7 @@ module.exports = function (bind, lib) {
bind(`Layout`, Layout);
bind(`Size`, Size);
bind(`Value`, Value);
return Object.assign({
@@ -194,6 +223,7 @@ module.exports = function (bind, lib) {
Layout,
Size,
Value,
setExperimentalFeatureEnabled,
isExperimentalFeatureEnabled,