@textbus/platform-node
Version:
Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.
233 lines (221 loc) • 9 kB
JavaScript
var core$1 = require('@textbus/core');
var core = require('@viewfly/core');
var platformBrowser = require('@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;
};
exports.NodeSelectionBridge = class NodeSelectionBridge {
connect() {
//
}
disConnect() {
//
}
restore() {
//
}
getNextLinePositionByCurrent(position) {
return position;
}
getPreviousLinePositionByCurrent(position) {
return position;
}
};
exports.NodeSelectionBridge = __decorate([
core.Injectable()
], exports.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: core$1.NativeSelectionBridge,
useValue: new exports.NodeSelectionBridge()
}, {
provide: core$1.Adapter,
useValue: this.adapter
}]
});
}
}
const adapterError = core$1.makeError('ViewflyHTMLRenderer');
class NodeViewAdapter extends core$1.Adapter {
constructor(components, mount) {
super({
createCompositionNode(compositionState, updateNativeCompositionNode) {
const ref = core.createDynamicRef(node => {
updateNativeCompositionNode(node);
return () => {
updateNativeCompositionNode(null);
};
});
return new core$1.VElement('span', {
style: {
textDecoration: 'underline'
},
ref
}, [
new core$1.VTextNode(compositionState.text)
]);
},
getParentNode(node) {
return node.parent;
},
getChildNodes(parentElement) {
return parentElement.children.slice();
},
isNativeElementNode(node) {
return node instanceof platformBrowser.VDOMElement;
},
getChildByIndex(parentElement, index) {
return parentElement.children[index];
},
getAndUpdateSlotRootNativeElement(vEle, update) {
const currentRef = vEle.attrs.get('ref');
const ref = core.createDynamicRef(nativeNode => {
update(nativeNode);
return () => {
update(null);
};
});
if (currentRef instanceof core.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 = core.createDynamicRef(rootNode => {
this.componentRootElementCaches.set(component, rootNode);
return () => {
this.componentRootElementCaches.remove(component);
};
});
this.componentRefs.set(component, ref);
}
return core.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 core.jsx(vNode.tagName, props, key);
}
}, mount);
Object.defineProperty(this, "onViewUpdated", {
enumerable: true,
configurable: true,
writable: true,
value: new core$1.Subject()
});
Object.defineProperty(this, "host", {
enumerable: true,
configurable: true,
writable: true,
value: new platformBrowser.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 = core.getCurrentInstance();
const textbusComponent = props.component;
const subscription = core$1.merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
if (textbusComponent.changeMarker.dirty) {
comp.markAsDirtied();
}
});
core.onUnmounted(() => {
subscription.unsubscribe();
});
if (isRoot) {
core.onUpdated(() => {
this.onViewUpdated.next();
});
isRoot = false;
}
core.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');
}
}
exports.NodeModule = NodeModule;
exports.NodeViewAdapter = NodeViewAdapter;
;