@ray-core/runtime
Version:
Ray 是一个全新的基于 React 的小程序开发框架
115 lines (114 loc) • 4.39 kB
JavaScript
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 __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;
};
import * as React from 'react';
import { RuntimeOptions, ComponentInstanceContext } from '@ray-core/framework-shared';
import Container from './Container';
import render from './render';
import { SLOT_PLACEHOLDER } from './constants';
import { gStore } from './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);
}
export default 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();
render(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.getCallback(k);
_this[k] = cb;
if (!_this.container) {
_this.init();
}
else {
_this.render();
}
}
},
methods: {
init: function () {
this.component = RuntimeOptions.get('pluginDriver').onMiniComponent({
component: Component,
context: this,
});
this.container = new Container(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.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(SLOT_PLACEHOLDER).concat(placeholderKey);
// 最新的页面中的子节点(原生组件无法传递子节点,需通过页面桥接)
this.container._slot.node = this.container.copyVNode(node);
}
}
this.element = render(React.createElement(ComponentInstanceContext.Provider, { value: this }, React.createElement(this.component, props)), this.container);
},
},
};
return config;
}