UNPKG

@ray-core/runtime

Version:

Ray 是一个全新的基于 React 的小程序开发框架

144 lines (143 loc) 5.8 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var framework_shared_1 = require("@ray-core/framework-shared"); var Container_1 = __importDefault(require("./Container")); var render_1 = __importDefault(require("./render")); var constants_1 = require("./constants"); var gStore_1 = require("./gStore"); function hash(str) { var hash = 0; for (var i = 0; i < str.length; ++i) { hash += str.charCodeAt(i); hash += hash << 10; hash ^= hash >> 6; } hash += hash << 3; hash ^= hash >> 11; return (((hash + (hash << 15)) & 4294967295) >>> 0).toString(16); } function toNativeComponentConfigCreator(Component, name) { var config = { name: name, options: { styleIsolation: 'apply-shared', multipleSlots: true, }, data: { action: {}, root: { children: [], }, }, attached: function () { // 在组件实例进入页面节点树时执行 if (!this.container) { this.init(); } }, detached: function () { // 在组件实例被从页面节点树移除时执行 this.container.clearUpdate(); (0, render_1.default)(null, this.container); }, properties: { __p__: { type: null, value: null, }, }, observers: { __p__: function () { var _this = this; var k = "$_cb_".concat(_this.data.__instid__, "___p__.onInput"); var cb = gStore_1.gStore.getCallback(k); _this[k] = cb; if (!_this.container) { _this.init(); } else { _this.render(); } } }, methods: { init: function () { this.component = framework_shared_1.RuntimeOptions.get('pluginDriver').onMiniComponent({ component: Component, context: this, }); this.container = new Container_1.default(this); this.container.root.type = 'component-root'; this.render(); }, render: function () { var _a; var _b = this.data, __p__ = _b.__p__, rest = __rest(_b, ["__p__"]); var props = __assign(__assign(__assign({}, rest), __p__), { $scope: this }); var node = gStore_1.gStore.getVNode(rest.__instid__); if (node) { if ((_a = node.children) === null || _a === void 0 ? void 0 : _a.length) { // 计算组件 vNode hash值,作为占位内容,触发组件内部(React)更新 var placeholderKey = hash(JSON.stringify(node.toJSON())); // 给原生组件传递占位符(包含虚拟节点的hash值) props.children = "".concat(constants_1.SLOT_PLACEHOLDER).concat(placeholderKey); // 最新的页面中的子节点(原生组件无法传递子节点,需通过页面桥接) this.container._slot.node = this.container.copyVNode(node); } } this.element = (0, render_1.default)(React.createElement(framework_shared_1.ComponentInstanceContext.Provider, { value: this }, React.createElement(this.component, props)), this.container); }, }, }; return config; } exports.default = toNativeComponentConfigCreator;