Ckeditor5: How to duplicated Content on shortcut

Created on 19 Oct 2019  ·  1Comment  ·  Source: ckeditor/ckeditor5

📝 Provide detailed reproduction steps (if any)

I try to implement a duplicate line shortcut.

I tried alot of stuff I can reach it.

editor.keystrokes.set( 'Ctrl+D', ( evt, stop ) => {
      stop();
      const block = first( this.editor.model.document.selection.getSelectedBlocks() );
      editor.model.change( writer => {
        writer.insert( block, block, 'after' );
      } );
    } );

This is my code.

Any help.

✔️ Expected result

I Expect that on Ctrl+D my line is duplicate just under, of my selected Element is duplicated just after the one that is selected.
Event multiple Lines are duplicated under

❌ Actual result

I have an empty block

question

Most helpful comment

Hi! Basically, you are on the right track, however, you should take care of more things, like creating a new element, inserting data and preserving attributes. I've modified your code, see it below.

Please, keep in mind that this is a basic implementation which will work for a single block and probably will fail in some cases. Treat it as a starting point for your further development.

editor.keystrokes.set( 'Ctrl+D', ( evt, stop ) => {
    stop();
    const blocks = editor.model.document.selection.getSelectedBlocks();

     for ( const block of blocks ) {
          editor.model.change( writer => {
               const element = writer.createElement( block.name );

                for ( const child of block.getChildren() ) {
                     const text = writer.createText( child.data )

                     for ( const attr of child.getAttributes() ) {
                         writer.setAttribute( attr[ 0 ], attr[ 1 ], text );
                     };

                    writer.append( text, element );
               }

               writer.insert( element, block, 'after' );
           } );
      }
} );

>All comments

Hi! Basically, you are on the right track, however, you should take care of more things, like creating a new element, inserting data and preserving attributes. I've modified your code, see it below.

Please, keep in mind that this is a basic implementation which will work for a single block and probably will fail in some cases. Treat it as a starting point for your further development.

editor.keystrokes.set( 'Ctrl+D', ( evt, stop ) => {
    stop();
    const blocks = editor.model.document.selection.getSelectedBlocks();

     for ( const block of blocks ) {
          editor.model.change( writer => {
               const element = writer.createElement( block.name );

                for ( const child of block.getChildren() ) {
                     const text = writer.createText( child.data )

                     for ( const attr of child.getAttributes() ) {
                         writer.setAttribute( attr[ 0 ], attr[ 1 ], text );
                     };

                    writer.append( text, element );
               }

               writer.insert( element, block, 'after' );
           } );
      }
} );

Was this page helpful?
0 / 5 - 0 ratings