@terrastack/ink
Version:
React for CLI. Temporary fork of https://github.com/vadimdemedes/ink
114 lines (103 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactReconciler = _interopRequireDefault(require("react-reconciler"));
var _dom = require("./dom");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = onRender => {
const rootHostContext = {};
const childHostContext = {};
const hostConfig = {
now: Date.now,
getRootHostContext: () => rootHostContext,
prepareForCommit: () => {},
resetAfterCommit: () => {},
getChildHostContext: () => childHostContext,
shouldSetTextContent: (type, props) => {
return typeof props.children === 'string' || typeof props.children === 'number';
},
createInstance: (type, newProps) => {
const node = (0, _dom.createNode)(type);
for (const [key, value] of Object.entries(newProps)) {
if (key === 'children') {
if (typeof value === 'string' || typeof value === 'number') {
// Text node must be wrapped in another node, so that text can be aligned within container
const textElement = (0, _dom.createNode)('span');
textElement.textContent = value;
(0, _dom.appendChildNode)(node, textElement);
}
} else if (key === 'style') {
Object.assign(node.style, value);
} else if (key === 'unstable__transformChildren') {
node.unstable__transformChildren = value; // eslint-disable-line camelcase
} else if (key === 'static') {
node.static = true;
} else {
(0, _dom.setAttribute)(node, key, value);
}
}
return node;
},
createTextInstance: _dom.createTextNode,
resetTextContent: node => {
if (node.textContent) {
node.textContent = '';
}
if (node.childNodes.length > 0) {
for (const childNode of node.childNodes) {
childNode.yogaNode.free();
(0, _dom.removeChildNode)(node, childNode);
}
}
},
getPublicInstance: instance => instance,
appendInitialChild: _dom.appendChildNode,
appendChild: _dom.appendChildNode,
insertBefore: _dom.insertBeforeNode,
finalizeInitialChildren: () => {},
supportsMutation: true,
appendChildToContainer: (parentNode, childNode) => {
(0, _dom.appendChildNode)(parentNode, childNode);
onRender();
},
removeChildFromContainer: (parentNode, childNode) => {
(0, _dom.removeChildNode)(parentNode, childNode);
onRender();
},
prepareUpdate: () => true,
commitUpdate: (node, updatePayload, type, oldProps, newProps) => {
for (const [key, value] of Object.entries(newProps)) {
if (key === 'children') {
if (typeof value === 'string' || typeof value === 'number') {
node.childNodes[0].textContent = value;
}
} else if (key === 'style') {
Object.assign(node.style, value);
} else if (key === 'unstable__transformChildren') {
node.unstable__transformChildren = value; // eslint-disable-line camelcase
} else if (key === 'static') {
node.static = true;
} else {
(0, _dom.setAttribute)(node, key, value);
}
}
onRender();
},
commitTextUpdate: (node, oldText, newText) => {
if (node.nodeName === '#text') {
node.nodeValue = newText;
} else {
node.textContent = newText;
}
onRender();
},
removeChild: (parentNode, childNode) => {
(0, _dom.removeChildNode)(parentNode, childNode);
onRender();
}
};
return (0, _reactReconciler.default)(hostConfig); // eslint-disable-line new-cap
};
exports.default = _default;