Add TypeScript declarations

This commit is contained in:
Nick Gerleman
2022-12-23 02:43:45 -08:00
parent 3352385fb0
commit aaadf57b19
15 changed files with 576 additions and 649 deletions

190
javascript/sources/YGEnums.d.ts vendored Normal file
View File

@@ -0,0 +1,190 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @generated by enums.py
export const ALIGN_AUTO: 0;
export const ALIGN_FLEX_START: 1;
export const ALIGN_CENTER: 2;
export const ALIGN_FLEX_END: 3;
export const ALIGN_STRETCH: 4;
export const ALIGN_BASELINE: 5;
export const ALIGN_SPACE_BETWEEN: 6;
export const ALIGN_SPACE_AROUND: 7;
export const DIMENSION_WIDTH: 0;
export const DIMENSION_HEIGHT: 1;
export const DIRECTION_INHERIT: 0;
export const DIRECTION_LTR: 1;
export const DIRECTION_RTL: 2;
export const DISPLAY_FLEX: 0;
export const DISPLAY_NONE: 1;
export const EDGE_LEFT: 0;
export const EDGE_TOP: 1;
export const EDGE_RIGHT: 2;
export const EDGE_BOTTOM: 3;
export const EDGE_START: 4;
export const EDGE_END: 5;
export const EDGE_HORIZONTAL: 6;
export const EDGE_VERTICAL: 7;
export const EDGE_ALL: 8;
export const EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: 0;
export const FLEX_DIRECTION_COLUMN: 0;
export const FLEX_DIRECTION_COLUMN_REVERSE: 1;
export const FLEX_DIRECTION_ROW: 2;
export const FLEX_DIRECTION_ROW_REVERSE: 3;
export const GUTTER_COLUMN: 0;
export const GUTTER_ROW: 1;
export const GUTTER_ALL: 2;
export const JUSTIFY_FLEX_START: 0;
export const JUSTIFY_CENTER: 1;
export const JUSTIFY_FLEX_END: 2;
export const JUSTIFY_SPACE_BETWEEN: 3;
export const JUSTIFY_SPACE_AROUND: 4;
export const JUSTIFY_SPACE_EVENLY: 5;
export const LOG_LEVEL_ERROR: 0;
export const LOG_LEVEL_WARN: 1;
export const LOG_LEVEL_INFO: 2;
export const LOG_LEVEL_DEBUG: 3;
export const LOG_LEVEL_VERBOSE: 4;
export const LOG_LEVEL_FATAL: 5;
export const MEASURE_MODE_UNDEFINED: 0;
export const MEASURE_MODE_EXACTLY: 1;
export const MEASURE_MODE_AT_MOST: 2;
export const NODE_TYPE_DEFAULT: 0;
export const NODE_TYPE_TEXT: 1;
export const OVERFLOW_VISIBLE: 0;
export const OVERFLOW_HIDDEN: 1;
export const OVERFLOW_SCROLL: 2;
export const POSITION_TYPE_STATIC: 0;
export const POSITION_TYPE_RELATIVE: 1;
export const POSITION_TYPE_ABSOLUTE: 2;
export const PRINT_OPTIONS_LAYOUT: 1;
export const PRINT_OPTIONS_STYLE: 2;
export const PRINT_OPTIONS_CHILDREN: 4;
export const UNIT_UNDEFINED: 0;
export const UNIT_POINT: 1;
export const UNIT_PERCENT: 2;
export const UNIT_AUTO: 3;
export const WRAP_NO_WRAP: 0;
export const WRAP_WRAP: 1;
export const WRAP_WRAP_REVERSE: 2;
export type Align =
| typeof ALIGN_AUTO
| typeof ALIGN_FLEX_START
| typeof ALIGN_CENTER
| typeof ALIGN_FLEX_END
| typeof ALIGN_STRETCH
| typeof ALIGN_BASELINE
| typeof ALIGN_SPACE_BETWEEN
| typeof ALIGN_SPACE_AROUND;
export type Dimension =
| typeof DIMENSION_WIDTH
| typeof DIMENSION_HEIGHT;
export type Direction =
| typeof DIRECTION_INHERIT
| typeof DIRECTION_LTR
| typeof DIRECTION_RTL;
export type Display =
| typeof DISPLAY_FLEX
| typeof DISPLAY_NONE;
export type Edge =
| typeof EDGE_LEFT
| typeof EDGE_TOP
| typeof EDGE_RIGHT
| typeof EDGE_BOTTOM
| typeof EDGE_START
| typeof EDGE_END
| typeof EDGE_HORIZONTAL
| typeof EDGE_VERTICAL
| typeof EDGE_ALL;
export type ExperimentalFeature =
| typeof EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS;
export type FlexDirection =
| typeof FLEX_DIRECTION_COLUMN
| typeof FLEX_DIRECTION_COLUMN_REVERSE
| typeof FLEX_DIRECTION_ROW
| typeof FLEX_DIRECTION_ROW_REVERSE;
export type Gutter =
| typeof GUTTER_COLUMN
| typeof GUTTER_ROW
| typeof GUTTER_ALL;
export type Justify =
| typeof JUSTIFY_FLEX_START
| typeof JUSTIFY_CENTER
| typeof JUSTIFY_FLEX_END
| typeof JUSTIFY_SPACE_BETWEEN
| typeof JUSTIFY_SPACE_AROUND
| typeof JUSTIFY_SPACE_EVENLY;
export type LogLevel =
| typeof LOG_LEVEL_ERROR
| typeof LOG_LEVEL_WARN
| typeof LOG_LEVEL_INFO
| typeof LOG_LEVEL_DEBUG
| typeof LOG_LEVEL_VERBOSE
| typeof LOG_LEVEL_FATAL;
export type MeasureMode =
| typeof MEASURE_MODE_UNDEFINED
| typeof MEASURE_MODE_EXACTLY
| typeof MEASURE_MODE_AT_MOST;
export type NodeType =
| typeof NODE_TYPE_DEFAULT
| typeof NODE_TYPE_TEXT;
export type Overflow =
| typeof OVERFLOW_VISIBLE
| typeof OVERFLOW_HIDDEN
| typeof OVERFLOW_SCROLL;
export type PositionType =
| typeof POSITION_TYPE_STATIC
| typeof POSITION_TYPE_RELATIVE
| typeof POSITION_TYPE_ABSOLUTE;
export type PrintOptions =
| typeof PRINT_OPTIONS_LAYOUT
| typeof PRINT_OPTIONS_STYLE
| typeof PRINT_OPTIONS_CHILDREN;
export type Unit =
| typeof UNIT_UNDEFINED
| typeof UNIT_POINT
| typeof UNIT_PERCENT
| typeof UNIT_AUTO;
export type Wrap =
| typeof WRAP_NO_WRAP
| typeof WRAP_WRAP
| typeof WRAP_WRAP_REVERSE;

View File

@@ -7,9 +7,7 @@
// @generated by enums.py
// @flow
// @format
const CONSTANTS = {
module.exports = {
ALIGN_COUNT: 8,
ALIGN_AUTO: 0,
ALIGN_FLEX_START: 1,
@@ -109,103 +107,3 @@ const CONSTANTS = {
WRAP_WRAP: 1,
WRAP_WRAP_REVERSE: 2,
};
export type Yoga$Align =
| typeof CONSTANTS.ALIGN_AUTO
| typeof CONSTANTS.ALIGN_FLEX_START
| typeof CONSTANTS.ALIGN_CENTER
| typeof CONSTANTS.ALIGN_FLEX_END
| typeof CONSTANTS.ALIGN_STRETCH
| typeof CONSTANTS.ALIGN_BASELINE
| typeof CONSTANTS.ALIGN_SPACE_BETWEEN
| typeof CONSTANTS.ALIGN_SPACE_AROUND;
export type Yoga$Dimension =
| typeof CONSTANTS.DIMENSION_WIDTH
| typeof CONSTANTS.DIMENSION_HEIGHT;
export type Yoga$Direction =
| typeof CONSTANTS.DIRECTION_INHERIT
| typeof CONSTANTS.DIRECTION_LTR
| typeof CONSTANTS.DIRECTION_RTL;
export type Yoga$Display =
| typeof CONSTANTS.DISPLAY_FLEX
| typeof CONSTANTS.DISPLAY_NONE;
export type Yoga$Edge =
| typeof CONSTANTS.EDGE_LEFT
| typeof CONSTANTS.EDGE_TOP
| typeof CONSTANTS.EDGE_RIGHT
| typeof CONSTANTS.EDGE_BOTTOM
| typeof CONSTANTS.EDGE_START
| typeof CONSTANTS.EDGE_END
| typeof CONSTANTS.EDGE_HORIZONTAL
| typeof CONSTANTS.EDGE_VERTICAL
| typeof CONSTANTS.EDGE_ALL;
export type Yoga$ExperimentalFeature =
| typeof CONSTANTS.EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS;
export type Yoga$FlexDirection =
| typeof CONSTANTS.FLEX_DIRECTION_COLUMN
| typeof CONSTANTS.FLEX_DIRECTION_COLUMN_REVERSE
| typeof CONSTANTS.FLEX_DIRECTION_ROW
| typeof CONSTANTS.FLEX_DIRECTION_ROW_REVERSE;
export type Yoga$Gutter =
| typeof CONSTANTS.GUTTER_COLUMN
| typeof CONSTANTS.GUTTER_ROW
| typeof CONSTANTS.GUTTER_ALL;
export type Yoga$Justify =
| typeof CONSTANTS.JUSTIFY_FLEX_START
| typeof CONSTANTS.JUSTIFY_CENTER
| typeof CONSTANTS.JUSTIFY_FLEX_END
| typeof CONSTANTS.JUSTIFY_SPACE_BETWEEN
| typeof CONSTANTS.JUSTIFY_SPACE_AROUND
| typeof CONSTANTS.JUSTIFY_SPACE_EVENLY;
export type Yoga$LogLevel =
| typeof CONSTANTS.LOG_LEVEL_ERROR
| typeof CONSTANTS.LOG_LEVEL_WARN
| typeof CONSTANTS.LOG_LEVEL_INFO
| typeof CONSTANTS.LOG_LEVEL_DEBUG
| typeof CONSTANTS.LOG_LEVEL_VERBOSE
| typeof CONSTANTS.LOG_LEVEL_FATAL;
export type Yoga$MeasureMode =
| typeof CONSTANTS.MEASURE_MODE_UNDEFINED
| typeof CONSTANTS.MEASURE_MODE_EXACTLY
| typeof CONSTANTS.MEASURE_MODE_AT_MOST;
export type Yoga$NodeType =
| typeof CONSTANTS.NODE_TYPE_DEFAULT
| typeof CONSTANTS.NODE_TYPE_TEXT;
export type Yoga$Overflow =
| typeof CONSTANTS.OVERFLOW_VISIBLE
| typeof CONSTANTS.OVERFLOW_HIDDEN
| typeof CONSTANTS.OVERFLOW_SCROLL;
export type Yoga$PositionType =
| typeof CONSTANTS.POSITION_TYPE_STATIC
| typeof CONSTANTS.POSITION_TYPE_RELATIVE
| typeof CONSTANTS.POSITION_TYPE_ABSOLUTE;
export type Yoga$PrintOptions =
| typeof CONSTANTS.PRINT_OPTIONS_LAYOUT
| typeof CONSTANTS.PRINT_OPTIONS_STYLE
| typeof CONSTANTS.PRINT_OPTIONS_CHILDREN;
export type Yoga$Unit =
| typeof CONSTANTS.UNIT_UNDEFINED
| typeof CONSTANTS.UNIT_POINT
| typeof CONSTANTS.UNIT_PERCENT
| typeof CONSTANTS.UNIT_AUTO;
export type Yoga$Wrap =
| typeof CONSTANTS.WRAP_NO_WRAP
| typeof CONSTANTS.WRAP_WRAP
| typeof CONSTANTS.WRAP_WRAP_REVERSE;
module.exports = CONSTANTS;

View File

@@ -1,379 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const CONSTANTS = require('./YGEnums');
import type {
Yoga$Edge,
Yoga$Wrap,
Yoga$Align,
Yoga$FlexDirection,
Yoga$Gutter,
Yoga$Direction,
Yoga$PositionType,
Yoga$Overflow,
Yoga$Justify,
Yoga$Display,
Yoga$ExperimentalFeature,
} from './YGEnums';
class Layout {
left: number;
right: number;
top: number;
bottom: number;
width: number;
height: number;
constructor(left, right, top, bottom, width, height) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.width = width;
this.height = height;
}
fromJS(expose) {
expose(
this.left,
this.right,
this.top,
this.bottom,
this.width,
this.height,
);
}
toString() {
return `<Layout#${this.left}:${this.right};${this.top}:${this.bottom};${
this.width
}:${this.height}>`;
}
}
class Size {
static fromJS({width, height}) {
return new Size(width, height);
}
width: number;
height: number;
constructor(width, height) {
this.width = width;
this.height = height;
}
fromJS(expose) {
expose(this.width, this.height);
}
toString() {
return `<Size#${this.width}x${this.height}>`;
}
}
class Value {
unit: number;
value: number;
constructor(unit, value) {
this.unit = unit;
this.value = value;
}
fromJS(expose) {
expose(this.unit, this.value);
}
toString() {
switch (this.unit) {
case CONSTANTS.UNIT_POINT:
return String(this.value);
case CONSTANTS.UNIT_PERCENT:
return `${this.value}%`;
case CONSTANTS.UNIT_AUTO:
return 'auto';
default: {
return `${this.value}?`;
}
}
}
valueOf() {
return this.value;
}
}
export type Yoga$Config = {
isExperimentalFeatureEnabled(feature: Yoga$ExperimentalFeature): boolean,
setExperimentalFeatureEnabled(
feature: Yoga$ExperimentalFeature,
enabled: boolean,
): void,
setPointScaleFactor(factor: number): void,
};
export type Yoga$Node = {
calculateLayout(
width?: number,
height?: number,
direction?: Yoga$Direction,
): void,
copyStyle(node: Yoga$Node): void,
free(): void,
freeRecursive(): void,
getAlignContent(): Yoga$Align,
getAlignItems(): Yoga$Align,
getAlignSelf(): Yoga$Align,
getAspectRatio(): number,
getBorder(edge: Yoga$Edge): number,
getChild(index: number): Yoga$Node,
getChildCount(): number,
getComputedBorder(edge: Yoga$Edge): number,
getComputedBottom(): number,
getComputedHeight(): number,
getComputedLayout(): Layout,
getComputedLeft(): number,
getComputedMargin(edge: Yoga$Edge): number,
getComputedPadding(edge: Yoga$Edge): number,
getComputedRight(): number,
getComputedTop(): number,
getComputedWidth(): number,
getDisplay(): Yoga$Display,
getFlexBasis(): number,
getFlexDirection(): Yoga$FlexDirection,
getFlexGrow(): number,
getFlexShrink(): number,
getFlexWrap(): Yoga$Wrap,
getHeight(): Value,
getJustifyContent(): Yoga$Justify,
getGap(gutter: Yoga$Gutter): Value,
getMargin(edge: Yoga$Edge): Value,
getMaxHeight(): Value,
getMaxWidth(): Value,
getMinHeight(): Value,
getMinWidth(): Value,
getOverflow(): Yoga$Overflow,
getPadding(edge: Yoga$Edge): Value,
getParent(): ?Yoga$Node,
getPosition(edge: Yoga$Edge): Value,
getPositionType(): Yoga$PositionType,
getWidth(): Value,
insertChild(child: Yoga$Node, index: number): void,
isDirty(): boolean,
markDirty(): void,
removeChild(child: Yoga$Node): void,
reset(): void,
setAlignContent(alignContent: Yoga$Align): void,
setAlignItems(alignItems: Yoga$Align): void,
setAlignSelf(alignSelf: Yoga$Align): void,
setAspectRatio(aspectRatio: number): void,
setBorder(edge: Yoga$Edge, borderWidth: number): void,
setDisplay(display: Yoga$Display): void,
setFlex(flex: number): void,
setFlexBasis(flexBasis: number | string): void,
setFlexBasisPercent(flexBasis: number): void,
setFlexBasisAuto(): void,
setFlexDirection(flexDirection: Yoga$FlexDirection): void,
setFlexGrow(flexGrow: number): void,
setFlexShrink(flexShrink: number): void,
setFlexWrap(flexWrap: Yoga$Wrap): void,
setHeight(height: number | string): void,
setHeightAuto(): void,
setHeightPercent(height: number): void,
setJustifyContent(justifyContent: Yoga$Justify): void,
setGap(gutter: Yoga$Gutter, gapLength: number): Value,
setMargin(edge: Yoga$Edge, margin: number): void,
setMarginAuto(edge: Yoga$Edge): void,
setMarginPercent(edge: Yoga$Edge, margin: number): void,
setMaxHeight(maxHeight: number | string): void,
setMaxHeightPercent(maxHeight: number): void,
setMaxWidth(maxWidth: number | string): void,
setMaxWidthPercent(maxWidth: number): void,
setMeasureFunc(measureFunc: ?Function): void,
setMinHeight(minHeight: number | string): void,
setMinHeightPercent(minHeight: number): void,
setMinWidth(minWidth: number | string): void,
setMinWidthPercent(minWidth: number): void,
setOverflow(overflow: Yoga$Overflow): void,
setPadding(edge: Yoga$Edge, padding: number | string): void,
setPaddingPercent(edge: Yoga$Edge, padding: number): void,
setPosition(edge: Yoga$Edge, position: number | string): void,
setPositionPercent(edge: Yoga$Edge, position: number): void,
setPositionType(positionType: Yoga$PositionType): void,
setWidth(width: number | string): void,
setWidthAuto(): void,
setWidthPercent(width: number): void,
unsetMeasureFun(): void,
};
type YogaConstructor = {
initialize: () => Promise<Yoga>;
}
type Yoga = {
Config: {
create(): Yoga$Config,
destroy(config: Yoga$Config): any,
},
Node: {
create(): Yoga$Node,
createDefault(): Yoga$Node,
createWithConfig(config: Yoga$Config): Yoga$Node,
destroy(node: Yoga$Node): any,
},
Layout: Layout,
Size: Size,
Value: Value,
getInstanceCount(): number,
...typeof CONSTANTS,
};
module.exports = function wrapLib(lib: any): Yoga {
function patch(prototype, name, fn) {
let original = prototype[name];
prototype[name] = function(...args) {
return fn.call(this, original, ...args);
};
}
for (let fnName of [
'setPosition',
'setMargin',
'setFlexBasis',
'setWidth',
'setHeight',
'setMinWidth',
'setMinHeight',
'setMaxWidth',
'setMaxHeight',
'setPadding',
]) {
let methods = {
[CONSTANTS.UNIT_POINT]: lib.Node.prototype[fnName],
[CONSTANTS.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`],
[CONSTANTS.UNIT_AUTO]: lib.Node.prototype[`${fnName}Auto`],
};
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()) / .setWidth("auto")
let value = args.pop();
let unit, asNumber;
if (value === 'auto') {
unit = CONSTANTS.UNIT_AUTO;
asNumber = undefined;
} else if (value instanceof Value) {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit =
typeof value === 'string' && value.endsWith('%')
? CONSTANTS.UNIT_PERCENT
: CONSTANTS.UNIT_POINT;
asNumber = parseFloat(value);
if (!Number.isNaN(value) && Number.isNaN(asNumber)) {
throw new Error(`Invalid value ${value} for ${fnName}`);
}
}
if (!methods[unit])
throw new Error(
`Failed to execute "${fnName}": Unsupported unit '${value}'`,
);
if (asNumber !== undefined) {
return methods[unit].call(this, ...args, asNumber);
} else {
return methods[unit].call(this, ...args);
}
});
}
function wrapMeasureFunction(measureFunction) {
return lib.MeasureCallback.implement({ measure: measureFunction })
}
patch(lib.Node.prototype, 'setMeasureFunc', function (original, measureFunc) {
original.call(this, wrapMeasureFunction(measureFunc))
})
function wrapDirtiedFunc(dirtiedFunction) {
return lib.DirtiedCallback.implement({ dirtied: dirtiedFunction })
}
patch(lib.Node.prototype, 'setDirtiedFunc', function (original, dirtiedFunc) {
original.call(this, wrapDirtiedFunc(dirtiedFunc))
})
patch(lib.Config.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Config.create),
// we also need to handle the deallocation
lib.Config.destroy(this);
});
patch(lib.Node, 'create', function(_, config) {
// We decide the constructor we want to call depending on the parameters
return config
? lib.Node.createWithConfig(config)
: lib.Node.createDefault();
});
patch(lib.Node.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Node.create),
// we also need to handle the deallocation
lib.Node.destroy(this);
});
patch(lib.Node.prototype, 'freeRecursive', function() {
for (let t = 0, T = this.getChildCount(); t < T; ++t) {
this.getChild(0).freeRecursive();
}
this.free();
});
patch(lib.Node.prototype, 'setMeasureFunc', function(original, measureFunc) {
// This patch is just a convenience patch, since it helps write more
// idiomatic source code (such as .setMeasureFunc(null))
// We also automatically convert the return value of the measureFunc
// to a Size object, so that we can return anything that has .width and
// .height properties
if (measureFunc) {
return original.call(this, (...args) =>
Size.fromJS(measureFunc(...args)),
);
} else {
return this.unsetMeasureFunc();
}
});
patch(lib.Node.prototype, 'calculateLayout', function(
original,
width = NaN,
height = NaN,
direction = CONSTANTS.DIRECTION_LTR,
) {
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
});
return {
Config: lib.Config,
Node: lib.Node,
Layout: Layout,
Size: Size,
Value: Value,
...CONSTANTS,
};
};

View File

@@ -4,18 +4,13 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const wrapLib = require('./entry');
const wrapAsm = require('./wrapAsm');
const loadYoga = require('./asmjs-async');
module.exports = {
load: () => {
return loadYoga().then(wrapLib);
}
load: () => loadYoga().then(wrapAsm),
...require('./YGEnums'),
}
export type * from './YGEnums.js';
export type * from './entry';

15
javascript/sources/index.d.ts vendored Normal file
View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {Yoga} from './wrapAsm';
export * from './YGEnums';
export * from './wrapAsm';
export function load(): Promise<Yoga>;

View File

@@ -4,18 +4,13 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const wrapLib = require('./entry');
const wrapAsm = require('./wrapAsm');
const loadYoga = require('./wasm-async');
module.exports = {
load: () => {
return loadYoga().then(wrapLib);
load: () => loadYoga().then(wrapAsm),
...require('./YGEnums'),
}
}
export type * from './YGEnums.js';
export type * from './entry';

View File

@@ -4,14 +4,10 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const wrapLib = require('./entry');
const wrapAsm = require('./wrapAsm');
const loadYoga = require('./asmjs-sync');
module.exports = wrapLib(loadYoga());
export type * from './YGEnums.js';
export type * from './entry';
module.exports = wrapAsm(loadYoga());

16
javascript/sources/sync.d.ts vendored Normal file
View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {Yoga} from './wrapAsm';
export * from './YGEnums';
export * from './wrapAsm';
declare const yoga: Yoga;
export default yoga;

View File

@@ -4,14 +4,10 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
const wrapLib = require('./entry');
const wrapAsm = require('./wrapAsm');
const loadYoga = require('./wasm-sync');
module.exports = wrapLib(loadYoga());
export type * from './YGEnums.js';
export type * from './entry';
module.exports = wrapAsm(loadYoga());

168
javascript/sources/wrapAsm.d.ts vendored Normal file
View File

@@ -0,0 +1,168 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import type {
Edge,
Wrap,
Align,
FlexDirection,
Gutter,
Direction,
PositionType,
Overflow,
Justify,
Display,
ExperimentalFeature,
} from './YGEnums';
import type * as YGEnums from './YGEnums';
type Layout = {
left: number;
right: number;
top: number;
bottom: number;
width: number;
height: number;
}
type Size = {
width: number;
height: number;
}
type Value = {
unit: number;
value: number;
}
export type Config = {
isExperimentalFeatureEnabled(feature: ExperimentalFeature): boolean,
setExperimentalFeatureEnabled(
feature: ExperimentalFeature,
enabled: boolean,
): void,
setPointScaleFactor(factor: number): void,
};
export type MeasureFunction = (
width: number,
widthMode: number,
height: number,
heightMode: number) => Size;
export type Node = {
calculateLayout(
width?: number,
height?: number,
direction?: Direction,
): void,
copyStyle(node: Node): void,
free(): void,
freeRecursive(): void,
getAlignContent(): Align,
getAlignItems(): Align,
getAlignSelf(): Align,
getAspectRatio(): number,
getBorder(edge: Edge): number,
getChild(index: number): Node,
getChildCount(): number,
getComputedBorder(edge: Edge): number,
getComputedBottom(): number,
getComputedHeight(): number,
getComputedLayout(): Layout,
getComputedLeft(): number,
getComputedMargin(edge: Edge): number,
getComputedPadding(edge: Edge): number,
getComputedRight(): number,
getComputedTop(): number,
getComputedWidth(): number,
getDisplay(): Display,
getFlexBasis(): number,
getFlexDirection(): FlexDirection,
getFlexGrow(): number,
getFlexShrink(): number,
getFlexWrap(): Wrap,
getHeight(): Value,
getJustifyContent(): Justify,
getGap(gutter: Gutter): Value,
getMargin(edge: Edge): Value,
getMaxHeight(): Value,
getMaxWidth(): Value,
getMinHeight(): Value,
getMinWidth(): Value,
getOverflow(): Overflow,
getPadding(edge: Edge): Value,
getParent(): Node | null,
getPosition(edge: Edge): Value,
getPositionType(): PositionType,
getWidth(): Value,
insertChild(child: Node, index: number): void,
isDirty(): boolean,
markDirty(): void,
removeChild(child: Node): void,
reset(): void,
setAlignContent(alignContent: Align): void,
setAlignItems(alignItems: Align): void,
setAlignSelf(alignSelf: Align): void,
setAspectRatio(aspectRatio: number): void,
setBorder(edge: Edge, borderWidth: number): void,
setDisplay(display: Display): void,
setFlex(flex: number): void,
setFlexBasis(flexBasis: number | string): void,
setFlexBasisPercent(flexBasis: number): void,
setFlexBasisAuto(): void,
setFlexDirection(flexDirection: FlexDirection): void,
setFlexGrow(flexGrow: number): void,
setFlexShrink(flexShrink: number): void,
setFlexWrap(flexWrap: Wrap): void,
setHeight(height: number | string): void,
setHeightAuto(): void,
setHeightPercent(height: number): void,
setJustifyContent(justifyContent: Justify): void,
setGap(gutter: Gutter, gapLength: number): Value,
setMargin(edge: Edge, margin: number): void,
setMarginAuto(edge: Edge): void,
setMarginPercent(edge: Edge, margin: number): void,
setMaxHeight(maxHeight: number | string): void,
setMaxHeightPercent(maxHeight: number): void,
setMaxWidth(maxWidth: number | string): void,
setMaxWidthPercent(maxWidth: number): void,
setMeasureFunc(measureFunc: MeasureFunction | null): void,
setMinHeight(minHeight: number | string): void,
setMinHeightPercent(minHeight: number): void,
setMinWidth(minWidth: number | string): void,
setMinWidthPercent(minWidth: number): void,
setOverflow(overflow: Overflow): void,
setPadding(edge: Edge, padding: number | string): void,
setPaddingPercent(edge: Edge, padding: number): void,
setPosition(edge: Edge, position: number | string): void,
setPositionPercent(edge: Edge, position: number): void,
setPositionType(positionType: PositionType): void,
setWidth(width: number | string): void,
setWidthAuto(): void,
setWidthPercent(width: number): void,
unsetMeasureFun(): void,
};
export type Yoga = {
Config: {
create(): Config,
destroy(config: Config): any,
},
Node: {
create(): Node,
createDefault(): Node,
createWithConfig(config: Config): Node,
destroy(node: Node): any,
},
} & typeof YGEnums;
declare const wrapAsm: () => Yoga;
export default wrapAsm;

View File

@@ -0,0 +1,145 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
const CONSTANTS = require('./YGEnums');
module.exports = (lib) => {
function patch(prototype, name, fn) {
let original = prototype[name];
prototype[name] = function(...args) {
return fn.call(this, original, ...args);
};
}
for (let fnName of [
'setPosition',
'setMargin',
'setFlexBasis',
'setWidth',
'setHeight',
'setMinWidth',
'setMinHeight',
'setMaxWidth',
'setMaxHeight',
'setPadding',
]) {
let methods = {
[CONSTANTS.UNIT_POINT]: lib.Node.prototype[fnName],
[CONSTANTS.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`],
[CONSTANTS.UNIT_AUTO]: lib.Node.prototype[`${fnName}Auto`],
};
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()) / .setWidth("auto")
let value = args.pop();
let unit, asNumber;
if (value === 'auto') {
unit = CONSTANTS.UNIT_AUTO;
asNumber = undefined;
} else if (typeof value === 'object') {
unit = value.unit;
asNumber = value.valueOf();
} else {
unit =
typeof value === 'string' && value.endsWith('%')
? CONSTANTS.UNIT_PERCENT
: CONSTANTS.UNIT_POINT;
asNumber = parseFloat(value);
if (!Number.isNaN(value) && Number.isNaN(asNumber)) {
throw new Error(`Invalid value ${value} for ${fnName}`);
}
}
if (!methods[unit])
throw new Error(
`Failed to execute "${fnName}": Unsupported unit '${value}'`,
);
if (asNumber !== undefined) {
return methods[unit].call(this, ...args, asNumber);
} else {
return methods[unit].call(this, ...args);
}
});
}
function wrapMeasureFunction(measureFunction) {
return lib.MeasureCallback.implement({ measure: measureFunction })
}
patch(lib.Node.prototype, 'setMeasureFunc', function (original, measureFunc) {
original.call(this, wrapMeasureFunction(measureFunc))
})
function wrapDirtiedFunc(dirtiedFunction) {
return lib.DirtiedCallback.implement({ dirtied: dirtiedFunction })
}
patch(lib.Node.prototype, 'setDirtiedFunc', function (original, dirtiedFunc) {
original.call(this, wrapDirtiedFunc(dirtiedFunc))
})
patch(lib.Config.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Config.create),
// we also need to handle the deallocation
lib.Config.destroy(this);
});
patch(lib.Node, 'create', function(_, config) {
// We decide the constructor we want to call depending on the parameters
return config
? lib.Node.createWithConfig(config)
: lib.Node.createDefault();
});
patch(lib.Node.prototype, 'free', function() {
// Since we handle the memory allocation ourselves (via lib.Node.create),
// we also need to handle the deallocation
lib.Node.destroy(this);
});
patch(lib.Node.prototype, 'freeRecursive', function() {
for (let t = 0, T = this.getChildCount(); t < T; ++t) {
this.getChild(0).freeRecursive();
}
this.free();
});
patch(lib.Node.prototype, 'setMeasureFunc', function(original, measureFunc) {
// This patch is just a convenience patch, since it helps write more
// idiomatic source code (such as .setMeasureFunc(null))
if (measureFunc) {
return original.call(this, (...args) =>
measureFunc(...args),
);
} else {
return this.unsetMeasureFunc();
}
});
patch(lib.Node.prototype, 'calculateLayout', function(
original,
width = NaN,
height = NaN,
direction = CONSTANTS.DIRECTION_LTR,
) {
// Just a small patch to add support for the function default parameters
return original.call(this, width, height, direction);
});
return {
Config: lib.Config,
Node: lib.Node,
...CONSTANTS,
};
};