Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

303
Visualizações
CKEditor 5-insert html and move the caret outside it

i have a button outside ckeditor, when clicked i want to insert a span element at caret position, this is what i have so far:

var el = document.querySelector('#editor');

ClassicEditor.create(el, {
    licenseKey: ''
}).then( editor => {
    appConfigs.editor = editor; //storing the editor for later usage
}).catch( error => {
    //...
});

when the button is clicked i do:

function clickHandler(){
    var editor = appConfigs.editor;

    editor.model.change( writer => {
        var html = '<span class="special-class-name" data-name="something">special word or button</span>';
        var viewFragment = editor.data.processor.toView(html);
        var modelFragment = editor.data.toModel(viewFragment);

        var insertPosition = editor.model.document.selection.getFirstPosition();

        editor.model.insertContent(modelFragment, insertPosition);
    });
}

the code have the following issues:

1)the inserted span get stripped off all its attributes(class, data-name) which i want to preserve.

2)the editing caret doesn't go back automatically to the editor when the button clicked.

3)when i click the editor to restore caret and start typing, i noticed it edits the text inside inserted span, i want inserted span to be untouched and any further typing goes after it.

i will be so grateful if anyone could help me with those issues.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda