ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

How to add custom class when using writer.insertText

Open crying2812 opened this issue 5 years ago • 4 comments

Provide a description of the task

I using insertText() to create new paragraph, how can i add custom class for new element?

What steps should be taken to fulfill the task? Here is my code: `js const docFrag = editor.model.change(writer => {

            const p1 = writer.createElement('paragraph', {class: 'flashback'});

            const docFrag = writer.createDocumentFragment();

            writer.append(p1, docFrag);

            writer.insertText('※   ※   ※', p1);

            return docFrag;
        });

`

  • Browser: Chrome
  • OS: Window
  • CKEditor version: 5

crying2812 avatar Dec 11 '20 03:12 crying2812

Hi, thanks for the report. Did you allow class attribute on the paragraph element in the schema and provide proper conversion for this attribute? The simplified implementation could look like this:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class AllowClassAttribute extends Plugin {
    init() {
        const editor = this.editor;

        editor.model.schema.extend( 'paragraph', { allowAttributes: [ 'class' ] } );

        editor.conversion.attributeToAttribute( { model: 'class', view: 'class' } );
    }
}

Mgsy avatar Dec 14 '20 08:12 Mgsy

Hi, thanks for the report. Did you allow class attribute on the paragraph element in the schema and provide proper conversion for this attribute? The simplified implementation could look like this:

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class AllowClassAttribute extends Plugin {
    init() {
        const editor = this.editor;

        editor.model.schema.extend( 'paragraph', { allowAttributes: [ 'class' ] } );

        editor.conversion.attributeToAttribute( { model: 'class', view: 'class' } );
    }
}

Thanks @Mgsy, it worked.

crying2812 avatar Dec 15 '20 02:12 crying2812

I am running into this same issue. I have added this after the ...}).then( ckeditor => {...:

    ckeditor.model.schema.extend( 'paragraph', { allowAttributes: [ 'class', 'contenteditable' ] } );
    ckeditor.conversion.attributeToAttribute( { model: 'class', view: 'class' } );
    ckeditor.conversion.attributeToAttribute( { model: 'contenteditable', view: 'contenteditable' } );

However, this only works for changes after the model has been loaded. I don't really understand which file I need to edit to include the import and export as mentioned above. Is this done before build? If so, which files do I need to edit before build?

dustinormond avatar Jan 26 '21 23:01 dustinormond

Hey @Mgsy Can you please help me I want to use this code in my .vue file as there is already export default where to use it

I have a separate function where I am inserting text in CKEditor I want to assign the class to those texts

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class AllowClassAttribute extends Plugin { init() { const editor = this.editor;

    editor.model.schema.extend( 'paragraph', { allowAttributes: [ 'class' ] } );

    editor.conversion.attributeToAttribute( { model: 'class', view: 'class' } );
}

}

Here is my code :

addPlaceHolder(bodyText) { this.editorInstance.model.change((writer) => { const insertPosition = this.editorInstance.model.document.selection.getFirstPosition(); if (bodyText == "{{ticket.url}}") { writer.insertText( bodyText, { linkHref: bodyText, blockQuote: true }, insertPosition ); } else { //writer.insertText(bodyText, insertPosition);

      const root = this.editorInstance.model.document.getRoot();
      const blockQuote = writer.createElement("blockQuote");
      const messageParagraph = writer.createElement("paragraph");

      // Inserts the message.
      writer.insertText("Testing meesage for blockquote", messageParagraph);
      writer.append(messageParagraph, blockQuote);
      // Appends the block quote to editor.
      writer.append(blockQuote, root);
    }

neelam-maro231 avatar Oct 21 '22 12:10 neelam-maro231