Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function insertImage( writer, model, attributes = {} ) {
const imageElement = writer.createElement( 'image', attributes );
const insertAtSelection = findOptimalInsertionPosition( model.document.selection, model );
model.insertContent( imageElement, insertAtSelection );
// Inserting an image might've failed due to schema regulations.
if ( imageElement.parent ) {
writer.setSelection( imageElement, 'on' );
}
}
function getInsertImageParent( selection, model ) {
const insertAt = findOptimalInsertionPosition( selection, model );
const parent = insertAt.parent;
if ( parent.isEmpty && !parent.is( '$root' ) ) {
return parent.parent;
}
return parent;
}
execute( url ) {
const model = this.editor.model;
const selection = model.document.selection;
const selectedMedia = getSelectedMediaModelWidget( selection );
if ( selectedMedia ) {
model.change( writer => {
writer.setAttribute( 'url', url, selectedMedia );
} );
} else {
const insertPosition = findOptimalInsertionPosition( selection, model );
insertMedia( model, url, insertPosition );
}
}
}
execute( options = {} ) {
const model = this.editor.model;
const selection = model.document.selection;
const tableUtils = this.editor.plugins.get( 'TableUtils' );
const rows = parseInt( options.rows ) || 2;
const columns = parseInt( options.columns ) || 2;
const insertPosition = findOptimalInsertionPosition( selection, model );
model.change( writer => {
const table = tableUtils.createTable( writer, rows, columns );
model.insertContent( table, insertPosition );
writer.setSelection( writer.createPositionAt( table.getNodeByPath( [ 0, 0, 0 ] ), 0 ) );
} );
}
}