How to add custom class when using writer.insertText
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
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' } );
}
}
Hi, thanks for the report. Did you allow
classattribute on theparagraphelement 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.
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?
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);
}