Skip to content

Commit

Permalink
Show visual selection on a text when the contextual balloon is displa…
Browse files Browse the repository at this point in the history
…yed.

As asked for link in ckeditor/ckeditor5#4721.
  • Loading branch information
Aeness committed Apr 20, 2021
1 parent 112f282 commit 1811a11
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/insertimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import ImageInsertCommand from '@ckeditor/ckeditor5-image/src/image/imageinsertc

import InsertImageForm from './insertimageform';

import { createFakeVisualSelection, renderFakeVisualSelection } from './utils';

export default class InsertImage extends Plugin {
// Call before SubPlugin init
constructor( editor ) {
Expand Down Expand Up @@ -57,7 +59,10 @@ export default class InsertImage extends Plugin {

// Show the panel on button click.
// this listen to 'excute" on button (backbone)
this.listenTo( button, 'execute', () => this._form.swapUI() );
this.listenTo( button, 'execute', () => {
createFakeVisualSelection(this.editor.model);
this._form.swapUI()
});

// Active the button when images are allowed
button.bind( 'isEnabled' ).to( this._command, 'isEnabled' );
Expand All @@ -69,6 +74,8 @@ export default class InsertImage extends Plugin {
});
});

renderFakeVisualSelection(editor)

return button;
} );
}
Expand Down Expand Up @@ -99,4 +106,4 @@ export default class InsertImage extends Plugin {
callback: () => this._form.hideUI()
} );
}
}
}
3 changes: 3 additions & 0 deletions src/insertimageform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextu
import InsertImageFormView from './ui/insertimageformview';

import { getSelectedImageWidget } from '@ckeditor/ckeditor5-image/src/image/utils';
import { hideFakeVisualSelection } from './utils';

/**
* Use the "main" stack of the ContextualBalloon with only one view (added and removed by this
Expand Down Expand Up @@ -120,6 +121,8 @@ export default class InsertImageForm extends Plugin {
// Because the form has an input which has focus, the focus must be brought back
// to the editor. Otherwise, it would be lost.
this.editor.editing.view.focus();

hideFakeVisualSelection(this.editor.model);
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export const VISUAL_SELECTION_MARKER_NAME = 'aeness-insert-image';

/**
* Create a fake visual selection when the contextual balloon is displayed.
*
* This adds a 'aeness-insert-image' marker into the document that is rendered as a highlight on selected text fragment.
*/
export function createFakeVisualSelection(model) {
model.change( writer => {
const range = model.document.selection.getFirstRange();

if ( model.markers.has( VISUAL_SELECTION_MARKER_NAME ) ) {
writer.updateMarker( VISUAL_SELECTION_MARKER_NAME, { range } );
} else {
writer.addMarker( VISUAL_SELECTION_MARKER_NAME, {
usingOperation: false,
affectsData: false,
range
} );
}
} );
}

/**
* Render the fake visual selection created in {@link #createFakeVisualSelection}.
*/
export function renderFakeVisualSelection(editor) {
// Renders a fake visual selection marker on an expanded selection.
editor.conversion.for( 'editingDowncast' ).markerToHighlight( {
model: VISUAL_SELECTION_MARKER_NAME,
view: {
classes: [ 'ck-fake-insert-image-aeness-selection' ]
}
} );

// Renders a fake visual selection marker on a collapsed selection.
editor.conversion.for( 'editingDowncast' ).markerToElement( {
model: VISUAL_SELECTION_MARKER_NAME,
view: {
name: 'span',
classes: [ 'ck-fake-insert-image-aeness-selection', 'ck-fake-insert-image-aeness-selection_collapsed' ]
}
} );
}


/**
* Hides the fake visual selection created in {@link #createFakeVisualSelection}.
*/
export function hideFakeVisualSelection(model) {

if ( model.markers.has( VISUAL_SELECTION_MARKER_NAME ) ) {
model.change( writer => {
writer.removeMarker( VISUAL_SELECTION_MARKER_NAME );
} );
}
}
16 changes: 16 additions & 0 deletions theme/insertimageform.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,20 @@

[dir=ltr] .ck.ck-insert-image-form>:not(:first-child), [dir=rtl] .ck.ck-insert-image-form>:not(:last-child) {
margin-left: var(--ck-spacing-standard);
}


/*
* Classes used by the "fake visual selection" displayed in the content when the balloon is focused.
*/
.ck .ck-fake-insert-image-aeness-selection {
background: var(--ck-color-link-fake-selection);
}

/* A collapsed fake visual selection. */
.ck .ck-fake-insert-image-aeness-selection_collapsed {
height: 100%;
border-right: 1px solid var(--ck-color-base-text);
margin-right: -1px;
outline: solid 1px hsla(0, 0%, 100%, .5);
}

0 comments on commit 1811a11

Please sign in to comment.