更改模型后刷新CKEditor5小部件

时间:2018-07-13 06:56:07

标签: widget ckeditor5

我有一个带有文本属性节点的自定义架构元素,该节点呈现为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 );
}

通过某些外部事件(例如,来自配置小部件的外部模式的结果),使用model.change更新了属性。

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

现在,由于其属性已更改,所以我希望该窗口小部件可以重新呈现,但是事实并非如此。如何手动触发刷新以确保它是最新的?

1 个答案:

答案 0 :(得分:1)

有关模型更改的视图小部件的刷新由向下转换处理。我看到您想将模型属性转换为<div>的文本内容。我假设“属性”属性是<div>的元素属性(例如data-attribute)。在这种情况下,您将需要一组转换器:

  1. 上流
  2. 垂头丧气
  3. 双向转换器,将涵盖简单的attribute to attribute转换(上载和下载)。

ps .:我使用“ downcast”而不是“ editingDowncast”来分别定义“ editingDowncast”和“ dataDowncast”的downcast转换。

// 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 );
}