@textbus/platform-node
Version:
Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.
230 lines (219 loc) • 8.95 kB
JavaScript
import { NativeSelectionBridge, Adapter, makeError, VElement, VTextNode, Subject, merge } from '@textbus/core';
import { Injectable, createDynamicRef, DynamicRef, jsx, getCurrentInstance, onUnmounted, onUpdated } from '@viewfly/core';
import { VDOMElement } from '@viewfly/platform-browser';
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
function __decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
let NodeSelectionBridge = class NodeSelectionBridge {
connect() {
//
}
disConnect() {
//
}
restore() {
//
}
getNextLinePositionByCurrent(position) {
return position;
}
getPreviousLinePositionByCurrent(position) {
return position;
}
};
NodeSelectionBridge = __decorate([
Injectable()
], NodeSelectionBridge);
class NodeModule {
constructor(adapter) {
Object.defineProperty(this, "adapter", {
enumerable: true,
configurable: true,
writable: true,
value: adapter
});
Object.defineProperty(this, "providers", {
enumerable: true,
configurable: true,
writable: true,
value: [{
provide: NativeSelectionBridge,
useValue: new NodeSelectionBridge()
}, {
provide: Adapter,
useValue: this.adapter
}]
});
}
}
const adapterError = makeError('ViewflyHTMLRenderer');
class NodeViewAdapter extends Adapter {
constructor(components, mount) {
super({
createCompositionNode(compositionState, updateNativeCompositionNode) {
const ref = createDynamicRef(node => {
updateNativeCompositionNode(node);
return () => {
updateNativeCompositionNode(null);
};
});
return new VElement('span', {
style: {
textDecoration: 'underline'
},
ref
}, [
new VTextNode(compositionState.text)
]);
},
getParentNode(node) {
return node.parent;
},
getChildNodes(parentElement) {
return parentElement.children.slice();
},
isNativeElementNode(node) {
return node instanceof VDOMElement;
},
getChildByIndex(parentElement, index) {
return parentElement.children[index];
},
getAndUpdateSlotRootNativeElement(vEle, update) {
const currentRef = vEle.attrs.get('ref');
const ref = createDynamicRef(nativeNode => {
update(nativeNode);
return () => {
update(null);
};
});
if (currentRef instanceof DynamicRef) {
vEle.attrs.set('ref', [currentRef, ref]);
}
else if (Array.isArray(currentRef)) {
currentRef.push(ref);
}
else {
vEle.attrs.set('ref', ref);
}
},
componentRender: (component) => {
const comp = this.components[component.name] || this.components['*'];
if (comp) {
let ref = this.componentRefs.get(component);
if (!ref) {
ref = createDynamicRef(rootNode => {
this.componentRootElementCaches.set(component, rootNode);
return () => {
this.componentRootElementCaches.remove(component);
};
});
this.componentRefs.set(component, ref);
}
return jsx(comp, {
component,
rootRef: ref
}, component.id);
}
throw adapterError(`cannot found view component \`${component.name}\`!`);
},
vElementToViewElement(vNode, children) {
const key = vNode.attrs.get('key');
vNode.attrs.delete('key');
const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
a[b[0]] = 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((a, b) => {
a[b[0]] = b[1];
return a;
}, {});
}
if (children.length) {
props.children = children;
}
return jsx(vNode.tagName, props, key);
}
}, mount);
Object.defineProperty(this, "onViewUpdated", {
enumerable: true,
configurable: true,
writable: true,
value: new Subject()
});
Object.defineProperty(this, "host", {
enumerable: true,
configurable: true,
writable: true,
value: new VDOMElement('body')
});
Object.defineProperty(this, "components", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(this, "componentRefs", {
enumerable: true,
configurable: true,
writable: true,
value: new WeakMap()
});
let isRoot = true;
Object.entries(components).forEach(([key, viewFlyComponent]) => {
this.components[key] = (props) => {
const comp = getCurrentInstance();
const textbusComponent = props.component;
const subscription = merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
if (textbusComponent.changeMarker.dirty) {
comp.markAsDirtied();
}
});
onUnmounted(() => {
subscription.unsubscribe();
});
if (isRoot) {
onUpdated(() => {
this.onViewUpdated.next();
});
isRoot = false;
}
onUpdated(() => {
textbusComponent.changeMarker.rendered();
if (!this.componentRootElementCaches.get(textbusComponent)) {
// eslint-disable-next-line max-len
throw adapterError(`Component \`${textbusComponent.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
}
});
return viewFlyComponent(props);
};
});
}
copy() {
document.execCommand('copy');
}
}
export { NodeModule, NodeSelectionBridge, NodeViewAdapter };