Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public getStartInlineElement(): InlineElement {
if (this.block) {
switch (this.startFrom) {
case ContentPosition.Begin:
case ContentPosition.End:
case ContentPosition.DomEnd:
return getFirstLastInlineElementFromBlockElement(
this.block,
this.startFrom == ContentPosition.Begin
);
case ContentPosition.SelectionStart:
// Get the inline before selection start point, and ensure it falls in the selection block
let startInline = getInlineElementAfter(this.rootNode, this.position);
return startInline && this.block.contains(startInline.getContainerNode())
? startInline
: new EmptyInlineElement(this.position, this.block);
}
}
return null;
}
function handleAutoBulletOrNumbering(identifier: string, editor: Editor) {
let document = editor.getDocument();
let spaceNode = document.createTextNode(' ');
editor.insertNode(spaceNode, {
position: ContentPosition.SelectionStart,
updateCursor: true,
replaceSelection: false,
insertOnNewLine: false,
});
editor.addUndoSnapshot();
editor.runWithoutAddingUndoSnapshot(() => {
// Remove the user input '*', '-' or '1.'
let rangeToDelete: Range = validateAndGetRangeForTextBeforeCursor(
editor,
identifier + ' ',
true,
new CursorData(editor)
);
if (rangeToDelete) {
insertedNode = refNode.insertBefore(node, isBegin ? refNode.firstChild : null);
}
} else {
// No first block, this can happen when editor is empty. Use appendChild to insert the content in contentDiv
insertedNode = contentDiv.appendChild(node);
}
// Final check to see if the inserted node is a block. If not block and the ask is to insert on new line,
// add a DIV wrapping
if (insertedNode && option.insertOnNewLine && !isBlockElement(insertedNode)) {
wrap(insertedNode);
}
break;
case ContentPosition.Range:
case ContentPosition.SelectionStart:
let { range, rangeToRestore } = getInitialRange(core, option);
if (!range) {
return;
}
// if to replace the selection and the selection is not collapsed, remove the the content at selection first
if (option.replaceSelection && !range.collapsed) {
range.deleteContents();
}
let pos = Position.getStart(range);
let blockElement: BlockElement;
if (
option.insertOnNewLine &&
// Remove the user input '*', '-' or '1.'
let rangeToDelete: Range = validateAndGetRangeForTextBeforeCursor(
editor,
identifier + ' ',
true,
new CursorData(editor)
);
if (rangeToDelete) {
rangeToDelete.deleteContents();
}
// If not explicitly insert br, Chrome will operate on the previous line
if (browserData.isChrome) {
let brNode = document.createElement('br');
editor.insertNode(brNode, {
position: ContentPosition.SelectionStart,
updateCursor: true,
replaceSelection: false,
insertOnNewLine: false,
});
}
if (identifier == '*' || identifier == '-') {
toggleBullet(editor);
} else if (identifier == '1.') {
toggleNumbering(editor);
}
});
import ClipboardData from './ClipboardData';
import { Editor } from 'roosterjs-editor-core';
import { ContentPosition, InsertOption } from 'roosterjs-editor-types';
import { processImages } from './PasteUtility';
const PASTE_NODE_INSERT_OPTION: InsertOption = {
position: ContentPosition.SelectionStart,
updateCursor: true,
replaceSelection: true,
insertOnNewLine: false,
};
const InlinePositionStyle = /(<\w+[^>]*style=['"][^>]*)position:[^>;'"]*/gi;
let pasteInterceptor = function preparePasteAndPickupPostPaste(
editor: Editor,
clipboardData: ClipboardData,
completeCallback: (clipboardData: ClipboardData) => void
) {
let [originalSelectionRange, pasteContainer] = preparePasteEnvironment(editor);
setTimeout(function() {
onPostPaste(
editor,
originalSelectionRange,
constructor(props: ApiPaneProps) {
super(props);
this.state = {
content: '',
position: ContentPosition.SelectionStart,
updateCursor: true,
replaceSelection: true,
insertOnNewLine: false,
};
}
reader.onload = (event: ProgressEvent) => {
if (this.editor) {
let image = this.editor.getDocument().createElement('img');
image.src = (event.target as FileReader).result;
this.editor.insertNode(image, {
position: ContentPosition.SelectionStart,
updateCursor: true,
replaceSelection: true,
insertOnNewLine: false,
});
this.editor.addUndoSnapshot();
}
};
reader.readAsDataURL(file);
const getContentTraverser: GetContentTraverser = (
core: EditorCore,
scope: ContentScope,
position: ContentPosition = ContentPosition.SelectionStart
) => {
let range = core.api.getSelectionRange(core, true /*tryGetFromCache*/);
switch (scope) {
case ContentScope.Block:
return (
range &&
ContentTraverser.createSelectionBlockTraverser(core.contentDiv, range, position)
);
case ContentScope.Selection:
return range && ContentTraverser.createSelectionTraverser(core.contentDiv, range);
case ContentScope.Body:
return ContentTraverser.createBodyTraverser(core.contentDiv);
}
return null;
onClick={() => this.setPosition(ContentPosition.SelectionStart)}
/>
export const insertNode: InsertNode = (core: EditorCore, node: Node, option: InsertOption) => {
option = option || {
position: ContentPosition.SelectionStart,
insertOnNewLine: false,
updateCursor: true,
replaceSelection: true,
};
let contentDiv = core.contentDiv;
if (option.updateCursor) {
core.api.focus(core);
}
switch (option.position) {
case ContentPosition.Begin:
case ContentPosition.End: {
let isBegin = option.position == ContentPosition.Begin;
let block = getFirstLastBlockElement(contentDiv, isBegin);
let insertedNode: Node;