How to use the roosterjs-editor-dom.getFirstLeafNode 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-core / lib / coreAPI / selection.ts View on Github external
function setSelectionToBegin(core: EditorCore) {
    let range: Range;
    let firstNode = getFirstLeafNode(core.contentDiv);
    if (firstNode) {
        if (firstNode.nodeType == NodeType.Text) {
            // First node is text, move range to the begin
            range = core.document.createRange();
            range.setStart(firstNode, 0);
        } else if (firstNode.nodeType == NodeType.Element) {
            if (isVoidHtmlElement(firstNode as HTMLElement)) {
                // First node is a html void element (void elements cannot have child nodes), move range before it
                range = core.document.createRange();
                range.setStartBefore(firstNode);
            } else {
                // Other html element, move range inside it
                range = core.document.createRange();
                range.setStart(firstNode, 0);
            }
        }
github microsoft / roosterjs / packages / roosterjs-editor-core / lib / coreAPI / focus.ts View on Github external
export const focus: Focus = (core: EditorCore) => {
    if (!core.api.hasFocus(core) || !core.api.getSelectionRange(core, false /*tryGetFromCache*/)) {
        // Focus (document.activeElement indicates) and selection are mostly in sync, but could be out of sync in some extreme cases.
        // i.e. if you programmatically change window selection to point to a non-focusable DOM element (i.e. tabindex=-1 etc.).
        // On Chrome/Firefox, it does not change document.activeElement. On Edge/IE, it change document.activeElement to be body
        // Although on Chrome/Firefox, document.activeElement points to editor, you cannot really type which we don't want (no cursor).
        // So here we always do a live selection pull on DOM and make it point in Editor. The pitfall is, the cursor could be reset
        // to very begin to of editor since we don't really have last saved selection (created on blur which does not fire in this case).
        // It should be better than the case you cannot type
        if (
            !core.cachedSelectionRange ||
            !core.api.selectRange(core, core.cachedSelectionRange, true /*skipSameRange*/)
        ) {
            let node = getFirstLeafNode(core.contentDiv) || core.contentDiv;
            core.api.selectRange(
                core,
                createRange(node, PositionType.Begin),
                true /*skipSameRange*/
            );
        }
    }

    // remember to clear cachedSelectionRange
    core.cachedSelectionRange = null;

    // This is more a fallback to ensure editor gets focus if it didn't manage to move focus to editor
    if (!core.api.hasFocus(core)) {
        core.contentDiv.focus();
    }
};
github microsoft / roosterjs / packages / roosterjs-editor-plugins / lib / Paste / officeOnlineConverter / convertPastedContentFromWordOnline.ts View on Github external
function getListItemBlocks(doc: HTMLDocument): ListItemBlock[] {
    const listElements = doc.getElementsByClassName(LIST_CONTAINER_ELEMENT_CLASS_NAME);
    const result: ListItemBlock[] = [];
    let curListItemBlock: ListItemBlock;
    for (let i = 0; i < listElements.length; i++) {
        let curItem = listElements[i];
        if (!curListItemBlock) {
            curListItemBlock = createListItemBlock(curItem)
        } else {
            const { listItemContainers } = curListItemBlock;
            const lastItemInCurBlock = listItemContainers[listItemContainers.length - 1];
            if (curItem == lastItemInCurBlock.nextSibling
                || getFirstLeafNode(curItem) == getNextLeafSibling(doc.body, lastItemInCurBlock)) {
                listItemContainers.push(curItem);
                curListItemBlock.endElement = curItem
            } else {
                curListItemBlock.endElement = lastItemInCurBlock;
                result.push(curListItemBlock);
                curListItemBlock = createListItemBlock(curItem);
            }
        }
    }

    if (curListItemBlock.listItemContainers.length > 0) {
        result.push(curListItemBlock);
    }

    return result;
}