@builder.io/mitosis
Version:
Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io
60 lines (59 loc) • 2.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeReactiveState = exports.addCodeToOnInit = exports.addCodeToOnUpdate = exports.addCodeNgAfterViewInit = void 0;
const addCodeNgAfterViewInit = (json, code) => {
if (!json.compileContext) {
json.compileContext = {
angular: {
hooks: {
ngAfterViewInit: {
code: '',
},
},
},
};
}
json.compileContext.angular.hooks.ngAfterViewInit.code += code;
};
exports.addCodeNgAfterViewInit = addCodeNgAfterViewInit;
/**
* Adds code to the `onUpdate` hook of a MitosisComponent.
*
* @param {MitosisComponent} root - The root MitosisComponent.
* @param {string} code - The code to be added to the `onUpdate` hook.
*/
const addCodeToOnUpdate = (root, code) => {
root.hooks.onUpdate = root.hooks.onUpdate || [];
root.hooks.onUpdate.push({
code,
});
};
exports.addCodeToOnUpdate = addCodeToOnUpdate;
/**
* Adds code to the `onInit` hook of a MitosisComponent.
*
* @param {MitosisComponent} root - The root MitosisComponent.
* @param {string} code - The code to be added to the `onInit` hook.
*/
const addCodeToOnInit = (root, code) => {
var _a;
if (!((_a = root.hooks.onInit) === null || _a === void 0 ? void 0 : _a.code)) {
root.hooks.onInit = { code: '' };
}
root.hooks.onInit.code += `\n${code};`;
};
exports.addCodeToOnInit = addCodeToOnInit;
/**
* Creates a reactive state in Angular.
* Initializes the state with `null` because we cannot access `state.` or `props.` properties before the component is initialized.
* Adds the code (init/re-init code) to the `onInit` and `onUpdate` hooks.
* @param root The root MitosisComponent.
* @param stateName The name of the reactive state.
* @param code The code to be added to the onInit and onUpdate hooks.
*/
const makeReactiveState = (root, stateName, code) => {
root.state[stateName] = { code: 'null', type: 'property' };
(0, exports.addCodeToOnInit)(root, code);
(0, exports.addCodeToOnUpdate)(root, code);
};
exports.makeReactiveState = makeReactiveState;
;