Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
node &&
node.nodeType == NodeType.Text &&
node.nodeValue == ZERO_WIDTH_SPACE &&
getTagOfNode(node.parentNode) == 'SPAN';
if (!isZWSNode) {
editor.addUndoSnapshot();
// Create a new text node to hold the selection.
// Some content is needed to position selection into the span
// for here, we inject ZWS - zero width space
node = editor.getDocument().createTextNode(ZERO_WIDTH_SPACE);
range.insertNode(node);
}
applyTextStyle(node, callback);
editor.select(node, PositionType.End);
}
} else {
// This is start and end node that get the style. The start and end needs to be recorded so that selection
// can be re-applied post-applying style
editor.addUndoSnapshot(() => {
let firstNode: Node;
let lastNode: Node;
let contentTraverser = editor.getSelectionTraverser();
let inlineElement = contentTraverser && contentTraverser.currentInlineElement;
while (inlineElement) {
let nextInlineElement = contentTraverser.getNextInlineElement();
inlineElement.applyStyle((element, isInnerNode) => {
callback(element, isInnerNode);
firstNode = firstNode || element;
lastNode = element;
});
styler: (element: HTMLElement) => void
): void {
// let's just be simple to create a new span to hold the style
// TODO: maybe we should be a bit smarter to see if we're in a span, and apply the style in parent span
let element = core.document.createElement('SPAN');
// Some content is needed to position selection into the span
// for here, we inject ZWS - zero width space
element.innerHTML = ZERO_WIDTH_SPACE;
styler(element);
selectionRange.insertNode(element);
// reset selection to be after the ZWS (rather than selecting it)
// This is needed so that the cursor still looks blinking inside editor
// This also means an extra ZWS will be in editor
// TODO: somewhere in returning content to consumer, we may need to do a cleanup for ZWS
core.api.select(core, element, PositionType.End);
}
public getEndPosition(): NodePosition {
// For a position, we always want it to point to a leaf node
// We should try to go get the lowest last child node from the container
return new Position(this.containerNode, PositionType.End).normalize();
}
export default function applyTextStyle(
container: Node,
styler: (node: HTMLElement) => any,
from: NodePosition = new Position(container, PositionType.Begin).normalize(),
to: NodePosition = new Position(container, PositionType.End).normalize()
) {
let formatNodes: Node[] = [];
while (from && to && to.isAfter(from)) {
let formatNode = from.node;
let parentTag = getTagOfNode(formatNode.parentNode);
// The code below modifies DOM. Need to get the next sibling first otherwise you won't be able to reliably get a good next sibling node
let nextNode = getNextLeafSibling(container, formatNode);
if (formatNode.nodeType == NodeType.Text && ['TR', 'TABLE'].indexOf(parentTag) < 0) {
if (formatNode == to.node && !to.isAtEnd) {
formatNode = splitTextNode(formatNode, to.offset, true /*returnFirstPart*/);
}
if (from.offset > 0) {
public applyStyle(styler: (element: HTMLElement, isInnerNode?: boolean) => any) {
let from = this.getStartPosition().normalize();
let to = this.getEndPosition().normalize();
let container = this.getContainerNode();
if (from.isAtEnd) {
let nextNode = getNextLeafSibling(container, from.node);
from = nextNode ? new Position(nextNode, PositionType.Begin) : null;
}
if (to.offset == 0) {
let previousNode = getPreviousLeafSibling(container, to.node);
to = previousNode ? new Position(previousNode, PositionType.End) : null;
}
applyTextStyle(container, styler, from, to);
}
}
private onMouseOver = (block: BlockElement) => {
this.props
.getEditor()
.select(block.getStartNode(), 0, block.getEndNode(), PositionType.End);
};
}
this.editor.performAutoComplete(() => {
matchingRange.deleteContents();
matchingRange.insertNode(nodeToInsert);
this.editor.select(nodeToInsert, PositionType.End);
});
}