How to use the roosterjs-editor-dom.Position function in roosterjs-editor-dom

To help you get started, we’ve selected a few roosterjs-editor-dom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github microsoft / roosterjs / packages / roosterjs-editor-plugins / lib / ContentEdit / features / insertLineBeforeStructuredNodeFeature.ts View on Github external
editor.addUndoSnapshot(() => {
            element.parentNode.insertBefore(div, element);
            // Select the new line when we are in table. This is the same behavior with Word
            if (getTagOfNode(element) == 'TABLE') {
                editor.select(new Position(div, PositionType.Begin).normalize());
            }
        });
        event.rawEvent.preventDefault();
github microsoft / roosterjs / packages / roosterjs-editor-core / lib / corePlugins / TypeInContainerPlugin.ts View on Github external
// Only reason we don't get the selection block is that we have an empty content div
            // which can happen when users removes everything (i.e. select all and DEL, or backspace from very end to begin)
            // The fix is to add a DIV wrapping, apply default format and move cursor over
            formatNode = fromHtml(
                Browser.isEdge ? '<div><span><br></span></div>' : '<div><br></div>',
                this.editor.getDocument()
            )[0] as HTMLElement;
            this.editor.insertNode(formatNode, {
                position: ContentPosition.End,
                updateCursor: false,
                replaceSelection: false,
                insertOnNewLine: false,
            });

            // element points to a wrapping node we added "<div><br></div>". We should move the selection left to <br>
            result = new Position(formatNode.firstChild, PositionType.Begin);
        }

        if (formatNode) {
            applyFormat(formatNode, this.editor.getDefaultFormat(), this.editor.isDarkMode());
        }

        return result;
    }
github microsoft / roosterjs / packages / roosterjs-editor-core / lib / corePlugins / FirefoxTypeAfterLink.ts View on Github external
onPluginEvent(event: PluginEvent) {
        if (event.eventType == PluginEventType.KeyPress) {
            let range = this.editor.getSelectionRange();
            if (range && range.collapsed && this.editor.getElementAtCursor('A[href]')) {
                let searcher = cacheGetContentSearcher(event, this.editor);
                let inlineElement = searcher.getInlineElementBefore();
                if (inlineElement instanceof LinkInlineElement) {
                    this.editor.select(
                        new Position(inlineElement.getContainerNode(), PositionType.After)
                    );
                }
            }
        }
    }
}
github microsoft / roosterjs / packages / roosterjs-editor-core / lib / coreAPI / insertNode.ts View on Github external
if (
                option.insertOnNewLine &&
                (blockElement = getBlockElementAtNode(contentDiv, pos.normalize().node))
            ) {
                pos = new Position(blockElement.getEndNode(), PositionType.After);
            } else {
                pos = adjustNodeInsertPosition(contentDiv, node, pos);
            }

            let nodeForCursor = node.nodeType == NodeType.DocumentFragment ? node.lastChild : node;
            range = createRange(pos);
            range.insertNode(node);
            if (option.updateCursor && nodeForCursor) {
                rangeToRestore = createRange(
                    new Position(nodeForCursor, PositionType.After).normalize()
                );
            }
            core.api.selectRange(core, rangeToRestore);

            break;
        case ContentPosition.Outside:
            core.contentDiv.parentNode.insertBefore(node, contentDiv.nextSibling);
            break;
    }

    return true;
};
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / table / insertTable.ts View on Github external
        editor.runAsync(() => editor.select(new Position(table, PositionType.Begin).normalize()));
    }, ChangeSource.Format);
github microsoft / roosterjs / packages / roosterjs-editor-plugins / lib / ContentEdit / features / tableFeatures.ts View on Github external
editor.runAsync(() => {
            let newContainer = editor.getElementAtCursor();
            if (
                contains(vtable.table, newContainer) &&
                !contains(td, newContainer, true /*treatSameNodeAsContain*/)
            ) {
                let newPos = targetTd
                    ? new Position(targetTd, PositionType.Begin)
                    : new Position(vtable.table, isUp ? PositionType.Before : PositionType.After);
                if (hasShiftKey) {
                    newPos =
                        newPos.node.nodeType == NodeType.Element && isVoidHtmlElement(newPos.node)
                            ? new Position(
                                  newPos.node,
                                  newPos.isAtEnd ? PositionType.After : PositionType.Before
                              )
                            : newPos;
                    editor
                        .getSelection()
                        .setBaseAndExtent(anchorNode, anchorOffset, newPos.node, newPos.offset);
                } else {
                    editor.select(newPos);
                }
            }
github microsoft / roosterjs / packages / roosterjs-editor-plugins / lib / ContentEdit / features / tableFeatures.ts View on Github external
editor.runAsync(() => {
            let newContainer = editor.getElementAtCursor();
            if (
                contains(vtable.table, newContainer) &&
                !contains(td, newContainer, true /*treatSameNodeAsContain*/)
            ) {
                let newPos = targetTd
                    ? new Position(targetTd, PositionType.Begin)
                    : new Position(vtable.table, isUp ? PositionType.Before : PositionType.After);
                if (hasShiftKey) {
                    newPos =
                        newPos.node.nodeType == NodeType.Element && isVoidHtmlElement(newPos.node)
                            ? new Position(
                                  newPos.node,
                                  newPos.isAtEnd ? PositionType.After : PositionType.Before
                              )
                            : newPos;
                    editor
                        .getSelection()
                        .setBaseAndExtent(anchorNode, anchorOffset, newPos.node, newPos.offset);
                } else {
                    editor.select(newPos);
                }
            }
        });
    },