с.Есть несколько незначительных проблем:
- Процесс преобразования может быть упрощен для двустороннего преобразователя
conversion.attributeToAttribute()
. - Вы должны разрешить атрибут
id
для всех заголовков либо путем расширения их определений , либо путем добавления проверки атрибута к схеме. - Обеспечение поддержки состояния модели должно выполняться путем добавления документа моделиpost-fixer .
class HeadingIdAttribute extends Plugin {
init() {
const editor = this.editor;
const model = editor.model;
const conversion = editor.conversion;
// Allow 'id' attribute on heading* elements:
// Either by extending each heading definition:
// editor.model.schema.extend( 'heading1', { allowAttributes: [ 'id' ] } );
// editor.model.schema.extend( 'heading2', { allowAttributes: [ 'id' ] } );
// editor.model.schema.extend( 'heading3', { allowAttributes: [ 'id' ] } );
// or by adding a more general attribute check:
model.schema.addAttributeCheck( ( schemaContext, attribute ) => {
if ( attribute == 'id' && isHeading( schemaContext.last.name ) ) {
return true;
}
} );
// Then the conversion might be a two way attribute-to-attribute:
conversion.attributeToAttribute( {
model: 'id',
view: 'id'
} );
// Register a model post-fixer to add missing id attribute
// to the heading* element.
model.document.registerPostFixer( writer => {
let wasChanged = false;
// Get changes
const changes = model.document.differ.getChanges();
for ( const change of changes ) {
// Check heading nodes on insert.
if ( change.type == 'insert' && isHeading( change.name ) ) {
const heading = change.position.nodeAfter;
// Set 'id' attribute when it is missing in the model.
if ( !heading.hasAttribute( 'id' ) ) {
writer.setAttribute( 'id', uid(), heading );
// Return true to notify that model was altered.
wasChanged = true;
}
}
}
return wasChanged;
} );
// Helper method for checking if name is any heading element.
// Detects default headings: 'heading1', 'heading2', ... 'heading6'.
function isHeading( name ) {
return name.slice( 0, -1 ) == 'heading';
}
}
}
И добавьте этот плагин в ваш редактор (не забудьте также добавить другие плагины):
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Undo, Heading, Paragraph, HeadingIdAttribute ],
toolbar: [ 'heading', '|', 'undo', 'redo' ]
} )
.then( editor => {
window.editor = editor;
} )
.catch( err => {
console.error( err.stack );
} );
Он создаст атрибут idдля просмотра:
![Paragraph changed to heading3](https://i.stack.imgur.com/OLtI5.png)