@textbus/adapter-viewfly
Version:
Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.
467 lines (463 loc) • 18.9 kB
JavaScript
import { makeError, merge, Adapter } from '@textbus/core';
import { getCurrentInstance, onUnmounted, onUpdated, jsx, createDynamicRef, ReflectiveInjector } from '@viewfly/core';
import { DomAdapter } from '@textbus/platform-browser';
import { NodeViewAdapter } from '@textbus/platform-node';
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_with_holes(arr) {
if (Array.isArray(arr)) return arr;
}
function _assert_this_initialized$1(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super$1(_this, derived, args) {
derived = _get_prototype_of$1(derived);
return _possible_constructor_return$1(_this, _is_native_reflect_construct$1() ? Reflect.construct(derived, args || [], _get_prototype_of$1(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check$1(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties$1(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class$1(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
return Constructor;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _get$1(target, property, receiver) {
if (typeof Reflect !== "undefined" && Reflect.get) {
_get$1 = Reflect.get;
} else {
_get$1 = function get(target, property, receiver) {
var base = _super_prop_base$1(target, property);
if (!base) return;
var desc = Object.getOwnPropertyDescriptor(base, property);
if (desc.get) {
return desc.get.call(receiver || target);
}
return desc.value;
};
}
return _get$1(target, property, receiver || target);
}
function _get_prototype_of$1(o) {
_get_prototype_of$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of$1(o);
}
function _inherits$1(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of$1(subClass, superClass);
}
function _instanceof(left, right) {
"@swc/helpers - instanceof";
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _iterable_to_array_limit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _non_iterable_rest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _possible_constructor_return$1(self, call) {
if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized$1(self);
}
function _set_prototype_of$1(o, p) {
_set_prototype_of$1 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of$1(o, p);
}
function _sliced_to_array(arr, i) {
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _super_prop_base$1(object, property) {
while(!Object.prototype.hasOwnProperty.call(object, property)){
object = _get_prototype_of$1(object);
if (object === null) break;
}
return object;
}
function _type_of$1(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
function _is_native_reflect_construct$1() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct$1 = function() {
return !!result;
})();
}
var adapterError = makeError('ViewflyDOMRenderer');
var ViewflyAdapter = /*#__PURE__*/ function(DomAdapter1) {
_inherits$1(ViewflyAdapter, DomAdapter1);
function ViewflyAdapter(components, mount) {
_class_call_check$1(this, ViewflyAdapter);
var _this;
_this = _call_super$1(this, ViewflyAdapter, [
{
createCompositionNode: function createCompositionNode(compositionState, updateNativeCompositionNode) {
var ref = createDynamicRef(function(node) {
updateNativeCompositionNode(node);
return function() {
updateNativeCompositionNode(null);
};
});
return jsx('span', {
style: {
textDecoration: 'underline'
},
ref: ref,
children: [
compositionState.text
]
}, compositionState.slot.id);
},
getParentNode: function getParentNode(node) {
return node.parentNode;
},
getChildNodes: function getChildNodes(parentElement) {
return Array.from(parentElement.childNodes);
},
isNativeElementNode: function isNativeElementNode(node) {
return _instanceof(node, Element);
},
getChildByIndex: function getChildByIndex(parentElement, index) {
return parentElement.childNodes[index];
},
getAndUpdateSlotRootNativeElement: function getAndUpdateSlotRootNativeElement(vEle, update) {
var currentRef = vEle.attrs.get('ref');
var ref = createDynamicRef(function(nativeNode) {
update(nativeNode);
return function() {
update(null);
};
});
if (Array.isArray(currentRef)) {
currentRef.push(ref);
} else if (typeof currentRef === 'function' || (typeof currentRef === "undefined" ? "undefined" : _type_of$1(currentRef)) === 'object') {
// 两个判断用于兼容 viewfly 2/3
vEle.attrs.set('ref', [
currentRef,
ref
]);
} else {
vEle.attrs.set('ref', ref);
}
},
componentRender: function componentRender(component) {
var comp = _assert_this_initialized$1(_this).components[component.name] || _assert_this_initialized$1(_this).components['*'];
if (comp) {
var ref = _assert_this_initialized$1(_this).componentRefs.get(component);
if (!ref) {
ref = createDynamicRef(function(rootNode) {
_assert_this_initialized$1(_this).componentRootElementCaches.set(component, rootNode);
return function() {
// 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点
if (_assert_this_initialized$1(_this).componentRootElementCaches.get(component) === rootNode) {
_assert_this_initialized$1(_this).componentRootElementCaches.remove(component);
}
};
});
_assert_this_initialized$1(_this).componentRefs.set(component, ref);
}
return jsx(comp, {
component: component,
rootRef: ref
}, component.id);
}
throw adapterError("cannot found view component `".concat(component.name, "`!"));
},
vElementToViewElement: function vElementToViewElement(vNode, children) {
var key;
var props = Array.from(vNode.attrs).reduce(function(a, b) {
var propName = b[0];
if (propName === 'key') {
key = b[1];
return a;
}
a[propName] = b[1];
return a;
}, {});
if (vNode.classes.size) {
props.class = Array.from(vNode.classes).join(' ');
}
if (vNode.styles) {
props.style = Array.from(vNode.styles).reduce(function(a, b) {
a[b[0]] = b[1];
return a;
}, {});
}
if (children.length) {
props.children = children;
}
return jsx(vNode.tagName, props, key);
}
},
mount
]), _define_property(_this, "components", {}), _define_property(_this, "componentRefs", new WeakMap());
var isRoot = true;
Object.entries(components).forEach(function(param) {
var _param = _sliced_to_array(param, 2), key = _param[0], viewFlyComponent = _param[1];
_this.components[key] = function(props) {
var comp = getCurrentInstance();
var textbusComponent = props.component;
var subscription = merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(function() {
if (textbusComponent.changeMarker.dirty) {
comp.markAsDirtied();
}
});
onUnmounted(function() {
subscription.unsubscribe();
});
if (isRoot) {
onUpdated(function() {
_this.onViewUpdated.next();
});
isRoot = false;
}
onUpdated(function() {
textbusComponent.changeMarker.rendered();
if (!_this.componentRootElementCaches.get(textbusComponent)) {
// eslint-disable-next-line max-len
throw adapterError("Component `".concat(textbusComponent.name, "` is not bound to rootRef, you must bind rootRef to the root element node of the component view."));
}
});
return viewFlyComponent(props);
};
});
return _this;
}
_create_class$1(ViewflyAdapter, [
{
key: "render",
value: function render(rootComponent, injector) {
var childInjector = new ReflectiveInjector(injector, [
{
provide: Adapter,
useValue: this
},
{
provide: DomAdapter,
useValue: this
},
{
provide: ViewflyAdapter,
useValue: this
}
]);
return _get$1(_get_prototype_of$1(ViewflyAdapter.prototype), "render", this).call(this, rootComponent, childInjector);
}
},
{
key: "copy",
value: function copy() {
document.execCommand('copy');
}
}
]);
return ViewflyAdapter;
}(DomAdapter);
function _assert_this_initialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _call_super(_this, derived, args) {
derived = _get_prototype_of(derived);
return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
}
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for(var i = 0; i < props.length; i++){
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
return Constructor;
}
function _get(target, property, receiver) {
if (typeof Reflect !== "undefined" && Reflect.get) {
_get = Reflect.get;
} else {
_get = function get(target, property, receiver) {
var base = _super_prop_base(target, property);
if (!base) return;
var desc = Object.getOwnPropertyDescriptor(base, property);
if (desc.get) {
return desc.get.call(receiver || target);
}
return desc.value;
};
}
return _get(target, property, receiver || target);
}
function _get_prototype_of(o) {
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _get_prototype_of(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _set_prototype_of(subClass, superClass);
}
function _possible_constructor_return(self, call) {
if (call && (_type_of(call) === "object" || typeof call === "function")) {
return call;
}
return _assert_this_initialized(self);
}
function _set_prototype_of(o, p) {
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _set_prototype_of(o, p);
}
function _super_prop_base(object, property) {
while(!Object.prototype.hasOwnProperty.call(object, property)){
object = _get_prototype_of(object);
if (object === null) break;
}
return object;
}
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
function _is_native_reflect_construct() {
try {
var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
} catch (_) {}
return (_is_native_reflect_construct = function() {
return !!result;
})();
}
var ViewflyVDomAdapter = /*#__PURE__*/ function(NodeViewAdapter) {
_inherits(ViewflyVDomAdapter, NodeViewAdapter);
function ViewflyVDomAdapter() {
_class_call_check(this, ViewflyVDomAdapter);
return _call_super(this, ViewflyVDomAdapter, arguments);
}
_create_class(ViewflyVDomAdapter, [
{
key: "render",
value: function render(rootComponent, injector) {
var childInjector = new ReflectiveInjector(injector, [
{
provide: Adapter,
useValue: this
},
{
provide: DomAdapter,
useValue: this
},
{
provide: ViewflyAdapter,
useValue: this
}
]);
return _get(_get_prototype_of(ViewflyVDomAdapter.prototype), "render", this).call(this, rootComponent, childInjector);
}
}
]);
return ViewflyVDomAdapter;
}(NodeViewAdapter);
export { ViewflyAdapter, ViewflyVDomAdapter };
//# sourceMappingURL=index.esm.js.map