UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

61 lines (60 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderHandlers = void 0; const event_handlers_1 = require("../../../helpers/event-handlers"); const component_1 = require("../component"); const src_generator_1 = require("../src-generator"); const IIF_START = '(() => {'; const IIF_END = '})()'; function extractJSBlock(binding) { if (typeof binding == 'string') { if (binding.startsWith('{') && binding.endsWith('}') && !binding.startsWith('{"') && !binding.endsWith('"}')) { return binding.substring(1, binding.length - 2); } else if (binding.startsWith(IIF_START) && binding.endsWith(IIF_END)) { return binding.substring(IIF_START.length, binding.length - IIF_END.length - 1); } } return null; } function renderHandlers(file, componentName, children) { let id = 0; const map = new Map(); const nodes = [...children]; while (nodes.length) { const node = nodes.shift(); const bindings = node.bindings; for (const key in bindings) { if (Object.prototype.hasOwnProperty.call(bindings, key)) { const { code: binding } = bindings[key]; if (binding != null) { if (isEventName(key)) { let block = extractJSBlock(binding) || binding; const symbol = `${componentName}_${key}_${id++}`; map.set(binding, symbol); renderHandler(file, symbol, block); } } } } nodes.push(...node.children); } return map; } exports.renderHandlers = renderHandlers; function renderHandler(file, symbol, code) { const body = [code]; const shouldRenderStateRestore = code.indexOf('state') !== -1; if (shouldRenderStateRestore) { body.unshift((0, component_1.renderUseLexicalScope)(file)); } file.exportConst(symbol, function () { this.emit([(0, src_generator_1.arrowFnBlock)(['event'], body)]); }); } function isEventName(name) { return (0, event_handlers_1.checkIsEvent)(name) && name.charAt(2).toUpperCase() == name.charAt(2); }