Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
domPositionToView( domParent, domOffset ) {
if ( this.isBlockFiller( domParent, this.blockFillerMode ) ) {
return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
}
// If position is somewhere inside UIElement - return position before that element.
const viewElement = this.mapDomToView( domParent );
if ( viewElement && viewElement.is( 'uiElement' ) ) {
return ViewPosition._createBefore( viewElement );
}
if ( isText( domParent ) ) {
if ( isInlineFiller( domParent ) ) {
return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
}
const viewParent = this.findCorrespondingViewText( domParent );
let offset = domOffset;
domPositionToView( domParent, domOffset ) {
if ( this.isBlockFiller( domParent, this.blockFillerMode ) ) {
return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
}
// If position is somewhere inside UIElement - return position before that element.
const viewElement = this.mapDomToView( domParent );
if ( viewElement && viewElement.is( 'uiElement' ) ) {
return ViewPosition._createBefore( viewElement );
}
if ( isText( domParent ) ) {
if ( isInlineFiller( domParent ) ) {
return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
}
const viewParent = this.findCorrespondingViewText( domParent );
let offset = domOffset;
if ( !viewParent ) {
return null;
}
if ( startsWithFiller( domParent ) ) {
offset -= INLINE_FILLER_LENGTH;
offset = offset < 0 ? 0 : offset;
}
return new ViewPosition( viewParent, offset );
}
if ( !domBefore ) {
// Position is after a view element that has not been rendered to DOM yet.
return null;
}
domParent = domBefore.parentNode;
domAfter = domBefore.nextSibling;
}
// If there is an inline filler at position return position inside the filler. We should never return
// the position before the inline filler.
if ( isText( domAfter ) && startsWithFiller( domAfter ) ) {
return { parent: domAfter, offset: INLINE_FILLER_LENGTH };
}
const offset = domBefore ? indexOf( domBefore ) + 1 : 0;
return { parent: domParent, offset };
}
}
domRoot.addEventListener( 'mousedown', evt => {
if ( evt.target.classList.contains( 'magic-block' ) ) {
const viewPosition = editingView.domConverter.domPositionToView( evt.target.parentNode, indexOf( evt.target ) );
const modelPosition = editor.editing.mapper.toModelPosition( viewPosition );
editor.model.change( writer => {
const paragraph = writer.createElement( 'paragraph' );
writer.insert( paragraph, modelPosition );
writer.setSelection( paragraph, 0 );
} );
}
} );
} );