@syncfusion/ej2-richtexteditor
Version:
Essential JS 2 RichTextEditor component
117 lines (114 loc) • 4.72 kB
JavaScript
import * as CONSTANT from './../base/constant';
import { InsertHtml } from './inserthtml';
import * as EVENTS from './../../common/constant';
import { closest } from '@syncfusion/ej2-base';
/**
* Selection EXEC internal component
*
* @hidden
*/
var InsertHtmlExec = /** @class */ (function () {
/**
* Constructor for creating the Formats plugin
*
* @param {EditorManager} parent - sepcifies the parent element
* @hidden
*/
function InsertHtmlExec(parent) {
this.parent = parent;
this.addEventListener();
}
InsertHtmlExec.prototype.addEventListener = function () {
this.parent.observer.on(CONSTANT.INSERTHTML_TYPE, this.applyHtml, this);
this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
};
InsertHtmlExec.prototype.removeEventListener = function () {
this.parent.observer.off(CONSTANT.INSERTHTML_TYPE, this.applyHtml);
this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
};
InsertHtmlExec.prototype.applyHtml = function (e) {
var selectionRange = this.getSelectionRange();
var element = e.value;
var firstChild = element.firstChild;
if (firstChild && firstChild.nodeName === 'A' && selectionRange.startOffset !== selectionRange.endOffset) {
var isTextNode = selectionRange.startContainer.nodeName === '#text';
var notifyType = isTextNode ? CONSTANT.LINK : CONSTANT.IMAGE;
var actionType = isTextNode ? 'CreateLink' : 'InsertLink';
this.parent.observer.notify(notifyType, {
value: actionType,
item: this.extractHyperlinkDetails(e),
callBack: e.callBack
});
}
else {
InsertHtml.Insert(this.parent.currentDocument, e.value, this.parent.editableElement, true, e.enterAction, this.parent);
}
if (e.subCommand === 'pasteCleanup') {
var pastedElements = this.parent.editableElement.querySelectorAll('.pasteContent_RTE');
var allPastedElements = [].slice.call(pastedElements);
var imgElements = this.parent.editableElement.querySelectorAll('.pasteContent_Img');
var allImgElm = [].slice.call(imgElements);
e.callBack({
requestType: e.subCommand,
editorMode: 'HTML',
elements: allPastedElements,
imgElem: allImgElm
});
}
else {
if (e.callBack) {
e.callBack({
requestType: e.subCommand,
editorMode: 'HTML',
event: e.event,
range: this.parent.nodeSelection.getRange(this.parent.currentDocument),
elements: this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)
});
}
}
};
InsertHtmlExec.prototype.extractHyperlinkDetails = function (e) {
var selectedText;
var selectionRange = this.getSelectionRange();
var selection = this.parent.nodeSelection;
var parentNodes = selection.getParentNodeCollection(selectionRange);
var anchor = e.value.querySelector('a');
var anchorElement = this.findAnchorElement(parentNodes);
if (anchorElement && anchorElement.nodeName === 'A') {
var rangeText = selectionRange.toString();
var anchorText = anchorElement.innerText;
selectedText = (rangeText.length < anchorText.length) ? anchorText : rangeText;
}
else {
selectedText = selectionRange.toString();
}
return {
url: anchor.href || '',
text: selectedText,
title: anchor.title || '',
target: anchor.target || '',
ariaLabel: anchor.ariaLabel || '',
selection: this.parent.nodeSelection,
selectNode: Array.from(this.getSelectionRange().startContainer.childNodes),
selectParent: parentNodes
};
};
InsertHtmlExec.prototype.getSelectionRange = function () {
return this.parent.nodeSelection.getRange(this.parent.currentDocument);
};
InsertHtmlExec.prototype.findAnchorElement = function (nodes) {
for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
var node = nodes_1[_i];
var anchor = closest(node, 'a');
if (anchor) {
return anchor;
}
}
return nodes[0];
};
InsertHtmlExec.prototype.destroy = function () {
this.removeEventListener();
};
return InsertHtmlExec;
}());
export { InsertHtmlExec };