How to use the roosterjs-editor-types.QueryScope.OnSelection function in roosterjs-editor-types

To help you get started, we’ve selected a few roosterjs-editor-types 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-api / lib / format / clearFormat.ts View on Github external
editor.addUndoSnapshot(() => {
        execCommand(editor, DocumentCommand.RemoveFormat);

        editor.queryElements('[class]', QueryScope.OnSelection, node =>
            node.removeAttribute('class')
        );

        const defaultFormat = editor.getDefaultFormat();
        const isDefaultFormatEmpty = Object.keys(defaultFormat).length === 0;
        editor.queryElements('[style]', QueryScope.InSelection, node => {
            STYLES_TO_REMOVE.forEach(style => node.style.removeProperty(style));

            // when default format is empty, keep the HTML minimum by removing style attribute if there's no style
            // (note: because default format is empty, we're not adding style back in)
            if (isDefaultFormatEmpty && node.getAttribute('style') === '') {
                node.removeAttribute('style');
            }
        });

        if (!isDefaultFormatEmpty) {
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / setIndentation.ts View on Github external
editor.addUndoSnapshot(() => {
        editor.focus();
        let listNode = editor.getElementAtCursor('OL,UL');
        let newNode: Node;

        if (listNode) {
            // There is already list node, setIndentation() will increase/decrease the list level,
            // so we need to process the list when change indentation
            newNode = processList(editor, command);
        } else {
            // No existing list node, browser will create <blockquote> node for indentation.
            // We need to set top and bottom margin to 0 to avoid unnecessary spaces
            editor.getDocument().execCommand(command, false, null);
            editor.queryElements('BLOCKQUOTE', QueryScope.OnSelection, node =&gt; {
                newNode = newNode || node;
                node.style.marginTop = '0px';
                node.style.marginBottom = '0px';
            });
        }

        return newNode;
    }, ChangeSource.Format);
}
</blockquote>
github microsoft / roosterjs-react / packages / roosterjs-react-ribbon / lib / components / buttons.tsx View on Github external
onClick: (editor, strings) => {
        editor.saveSelectionRange();
        let link = '';
        try {
            link = (editor.queryElements('a[href]', QueryScope.OnSelection)[0] as HTMLAnchorElement).href;
        } catch (e) {}

        confirm(
            getString('dlgLinkTitle', strings),
            getString('dlgUrlLabel', strings),
            link,
            strings
        ).then(link => {
            if (link) {
                editor.focus();
                createLink(editor, link, link);
            }
        });
    },
};
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / deprecated / queryNodesWithSelection.ts View on Github external
export default function queryNodesWithSelection(
    editor: Editor,
    selector: string,
    nodeContainedByRangeOnly?: boolean,
    forEachCallback?: (node: T) =&gt; void
): T[] {
    return editor.queryElements(
        selector,
        nodeContainedByRangeOnly ? QueryScope.InSelection : QueryScope.OnSelection,
        forEachCallback
    );
}
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / createLink.ts View on Github external
function getAnchorNodeAtCursor(editor: Editor): HTMLAnchorElement {
    return editor.queryElements('a[href]', QueryScope.OnSelection)[0] as HTMLAnchorElement;
}
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / removeLink.ts View on Github external
editor.addUndoSnapshot((start, end) => {
        editor.queryElements('a[href]', QueryScope.OnSelection, unwrap);
        editor.select(start, end);
    }, ChangeSource.Format);
}
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / setImageAltText.ts View on Github external
editor.addUndoSnapshot(() => {
        editor.queryElements('img', QueryScope.OnSelection, node =>
            node.setAttribute('alt', altText)
        );
    }, ChangeSource.Format);
}
github microsoft / roosterjs-react / packages / roosterjs-react-ribbon / lib / components / buttons.tsx View on Github external
onClick: (editor, strings) => {
        editor.saveSelectionRange();
        let node = editor.queryElements('img', QueryScope.OnSelection)[0];
        let alt = (node as HTMLImageElement).alt;
        confirm(getString('dlgAltTextTitle', strings), null, alt, strings).then(alt => {
            editor.focus();
            setImageAltText(editor, alt);
        });
    },
};
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / getFormatState.ts View on Github external
export function getElementBasedFormatState(
    editor: Editor,
    event?: PluginEvent
): ElementBasedFormatState {
    let listTag = getTagOfNode(cacheGetElementAtCursor(editor, event, 'OL,UL'));
    let headerTag = getTagOfNode(cacheGetElementAtCursor(editor, event, 'H1,H2,H3,H4,H5,H6'));

    return {
        isBullet: listTag == 'UL',
        isNumbering: listTag == 'OL',
        headerLevel: (headerTag && parseInt(headerTag[1])) || 0,

        canUnlink: !!editor.queryElements('a[href]', QueryScope.OnSelection)[0],
        canAddImageAltText: !!editor.queryElements('img', QueryScope.OnSelection)[0],
        isBlockQuote: !!editor.queryElements('blockquote', QueryScope.OnSelection)[0],
    };
}
github microsoft / roosterjs / packages / roosterjs-editor-api / lib / format / toggleHeader.ts View on Github external
editor.addUndoSnapshot(() =&gt; {
        editor.focus();

        let wrapped = false;
        editor.queryElements('H1,H2,H3,H4,H5,H6', QueryScope.OnSelection, header =&gt; {
            if (!wrapped) {
                editor.getDocument().execCommand(DocumentCommand.FormatBlock, false, '<div>');
                wrapped = true;
            }

            let div = editor.getDocument().createElement('div');
            while (header.firstChild) {
                div.appendChild(header.firstChild);
            }
            editor.replaceNode(header, div);
        });

        if (level &gt; 0) {
            let traverser = editor.getSelectionTraverser();
            let inlineElement = traverser ? traverser.currentInlineElement : null;
            while (inlineElement) {
</div>