Steve
Steve

Reputation: 1386

CKEditor5: prevent attributes being copied to new paragraph on enter

I am working on a project that allows users to inject content into the editable. I have added a contentid attribute to the paragraph so I can keep track of what has been added and where it is.

<p contentid="1">some default content</p> 

When enter is pressed from within that paragraph, the next paragraph that is entered also has the contentid="1" attribute. I want the new paragraph to not have the contentid attribute.

I'm trying to determine how/where best to achieve this. I have been hacking around with listening to the enter key and schema.addChildCheck but not making much progress.

Upvotes: 2

Views: 630

Answers (1)

Steve
Steve

Reputation: 1386

This seems to do the trick. Maybe a little hacky.. but realized that the selection is always in the newly created element. So I didn't need to find a reference to it in a callback, I could just grab the new paragraph from the current selection.

editor.commands.get( 'enter' ).on( 'afterExecute', () => {
  const block = first( editor.model.document.selection.getSelectedBlocks() );
  editor.model.change( writer => {
    writer.removeAttribute( 'contentid', block );
  });
});

Upvotes: 1

Related Questions