T
T
Tsuintora2020-03-23 22:36:09
JavaScript
Tsuintora, 2020-03-23 22:36:09

Alternative to document.execCommand?

thank you for your time.

Is there an alternative or alternative implementation of document.execCommand? The problem is that in each browser execCommand behaves differently, there is no way to set classes, identifiers and other attributes for elements. Plus, I would like to have semantic correctness (for example, for bold only strong in all browsers, and not span & b).

I paid attention to the implementation of this moment in other major visual editors, for example, TinyMCE & WordPress Gutenberg do not use this method at all (only contenteditable is used from the entire arsenal).

Example:

<p contenteditable = "true">
    <a href = "321">
        <strong>
            <em>
                Олимпиада
            </em>
        </strong>
    </a>
</p>

If I remove the letters "MP", then the following should come out:
<p contenteditable = "true">
    <a href = "321">
        <strong>
            <em>
                Оли
            </em>
        </strong>
    </a>
    МП
    <a href = "321">
        <strong>
            <em>
                иада
            </em>
        </strong>
    </a>
</p>


Many thanks in advance for your help and again for your time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
Hi-Pyncho, 2020-11-07
@Hi-Pyncho

You can use the Clipboard API, as MDN itself warns against using document.execCommand
. For example, you have a "Copy" button that will copy the contents of the desired element to the clipboard.

const copyButton = document.querySelector('.button')

You hang an event handler on this button and write the desired piece of text to the Clipboard through the Navigator object
copyButton.addEventListener('click', () => {
  window.navigator.clipboard.writeText(input.value)
})

And now the desired text in the clipboard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question