Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a CKEditor5 widget upon model changes

I have a custom schema element with a text attribute node that renders as a widget for the editingDowncast:

conversion.for( 'editingDowncast' )
  .add( downcastElementToElement({
    model: 'myModelElement',
    view: (modelElement, viewWriter) => {
      return createMyModelWidget( modelElement, viewWriter, 'Label' );
    }
  } )
);

function createMyModelWidget( modelElement, writer, label ) {
  const content = modelElement.getAttribute('content')
  const placeholder = writer.createText( content );
  const container = writer.createContainerElement( 'div', { class: 'my-model-element--widget' } );

  writer.insert( ViewPosition.createAt( container ), placeholder );
  return toWidget( container, writer, label );
}

Through some external event (e.g., result from an external modal that configures the widget), the attribute is updated using model.change.

model.change(writer => {
  writer.setAttribute( 'attribute', 'whatever', widget );
  writer.setAttribute( 'content', 'new content', widget );
});

Now since its attribute changed, I would expect the widget to be re-rendered, however that is not the case. How can I manually trigger a refresh to ensure it is up-to-date?

like image 806
oliverguenther Avatar asked Nov 01 '25 03:11

oliverguenther


1 Answers

The refreshing of an view widget on model changes is handled by downcast conversion. I can see that you want convert a model attribute to a text contents of a <div>. I assume that the "attribute" attribute is a <div>s element attribute (like data-attribute). In such case you'll need set of converters:

  1. Upcast
    • a single element to element upcast converter that will create myModelElement model element instance.
  2. Downcast
    • an element to element downcast converter that will create div for newly inserted myModelElment into a model
    • a custom attribute converter that will update div contents on attribute content changes
  3. A two-way converter that will cover simple attribute to attribute conversion (both upcast & downcast).

ps.: I've used "downcast" instead of "editingDowncast" to not define the downcast conversion for "editingDowncast" and "dataDowncast" separately.

// Required imports:
// import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
// import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
// import { upcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/upcast-converters';
// import ViewPosition from '@ckeditor/ckeditor5-engine/src/view/position';
// import ViewRange from '@ckeditor/ckeditor5-engine/src/view/range';

const conversion = editor.conversion;
const model = editor.model;
const schema = model.schema;

// Define model element and allow attributes on it.
schema.register( 'myModelElement', {
    allowWhere: '$block',
    allowAttributes: [ 'content', 'attribute' ],
    isObject: true
} );

// Simple attribute to attribute converter two-way converter
// - for upcast it will read "data-attribute" property of div and put it into model's "attribute" attribute
// - for downcast it will update "data-attribute" on changes in "attribute".
conversion.attributeToAttribute( {
    model: {
        name: 'myModelElement',
        key: 'attribute'
    },
    view: {
        key: 'data-attribute'
    }
} );

// Define upcast conversion:
conversion.for( 'upcast' ).add( upcastElementToElement( {
    model: ( viewElement, modelWriter ) => {
        const firstChild = viewElement.getChild( 0 );
        const text = firstChild.is( 'text' ) ? firstChild.data : '(empty)';

        return modelWriter.createElement( 'myModelElement', { content: text } );
    },
    view: {
        name: 'div',
        classes: 'my-model-element--widget'
    }
} ) );

// Define downcast conversion:
conversion.for( 'downcast' )
    .add( downcastElementToElement( {
        model: 'myModelElement',
        view: ( modelElement, viewWriter ) => {
            return createMyModelWidget( modelElement, viewWriter, 'Label' );
        }
    } ) )
    // Special conversion of attribute "content":
    .add( dispatcher => dispatcher.on( 'attribute:content', ( evt, data, conversionApi ) => {
        const myModelElement = data.item;

        // Mark element as consumed by conversion.
        conversionApi.consumable.consume( data.item, evt.name );

        // Get mapped view element to update.
        const viewElement = conversionApi.mapper.toViewElement( myModelElement );

        // Remove current <div> element contents.
        conversionApi.writer.remove( ViewRange.createOn( viewElement.getChild( 0 ) ) );

        // Set current content
        setContent( conversionApi.writer, data.attributeNewValue, viewElement );
    } ) );

function createMyModelWidget( modelElement, writer, label ) {
    const content = modelElement.getAttribute( 'content' );
    const container = writer.createContainerElement( 'div', { class: 'my-model-element--widget' } );

    setContent( writer, content, container );

    return toWidget( container, writer, label );
}

function setContent( writer, content, container ) {
    const placeholder = writer.createText( content || '' );
    writer.insert( ViewPosition.createAt( container ), placeholder );
}
like image 50
jodator Avatar answered Nov 03 '25 06:11

jodator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!