Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

t/ckeditor5-widget/60: Aligned to the new WidgetToolbarRepository API #72

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/mediaembedcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import Command from '@ckeditor/ckeditor5-core/src/command';
import { findOptimalInsertionPosition } from '@ckeditor/ckeditor5-widget/src/utils';
import { getSelectedMediaElement, insertMedia } from './utils';
import { getSelectedMediaModelWidget, insertMedia } from './utils';

/**
* The insert media command.
Expand All @@ -31,7 +31,7 @@ export default class MediaEmbedCommand extends Command {
const selection = model.document.selection;
const schema = model.schema;
const position = selection.getFirstPosition();
const selectedMedia = getSelectedMediaElement( selection );
const selectedMedia = getSelectedMediaModelWidget( selection );

let parent = position.parent;

Expand All @@ -55,7 +55,7 @@ export default class MediaEmbedCommand extends Command {
execute( url ) {
const model = this.editor.model;
const selection = model.document.selection;
const selectedMedia = getSelectedMediaElement( selection );
const selectedMedia = getSelectedMediaModelWidget( selection );

if ( selectedMedia ) {
model.change( writer => {
Expand Down
4 changes: 2 additions & 2 deletions src/mediaembedtoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import WidgetToolbarRepository from '@ckeditor/ckeditor5-widget/src/widgettoolbarrepository';
import { isMediaWidgetSelected } from './utils';
import { getSelectedMediaViewWidget } from './utils';

/**
* The media embed toolbar plugin. It creates a toolbar for media embed that shows up when the media element is selected.
Expand Down Expand Up @@ -42,7 +42,7 @@ export default class MediaEmbedToolbar extends Plugin {

widgetToolbarRepository.register( 'mediaEmbed', {
items: editor.config.get( 'mediaEmbed.toolbar' ) || [],
visibleWhen: isMediaWidgetSelected,
getRelatedElement: getSelectedMediaViewWidget,
} );
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,28 @@ export function toMediaWidget( viewElement, writer, label ) {
return toWidget( viewElement, writer, { label } );
}

export function isMediaWidgetSelected( viewSelection ) {
const viewElement = viewSelection.getSelectedElement();
/**
* Returns a media widget editing view element if one is selected.
*
* @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} selection
* @returns {module:engine/view/element~Element|null}
*/
export function getSelectedMediaViewWidget( selection ) {
const viewElement = selection.getSelectedElement();

if ( viewElement && isMediaWidget( viewElement ) ) {
return viewElement;
}

return !!( viewElement && isMediaWidget( viewElement ) );
return null;
}

/**
* Checks if a given view element is a media widget.
*
* @param {module:engine/view/element~Element} viewElement
* @returns {Boolean}
*/
export function isMediaWidget( viewElement ) {
return !!viewElement.getCustomProperty( mediaSymbol ) && isWidget( viewElement );
}
Expand Down Expand Up @@ -78,7 +94,7 @@ export function createMediaFigureElement( writer, registry, url, options ) {
* @param {module:engine/model/selection~Selection} selection
* @returns {module:engine/model/element~Element|null}
*/
export function getSelectedMediaElement( selection ) {
export function getSelectedMediaModelWidget( selection ) {
const selectedElement = selection.getSelectedElement();

if ( selectedElement && selectedElement.is( 'media' ) ) {
Expand Down
4 changes: 2 additions & 2 deletions tests/mediaembedtoolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe( 'MediaEmbedToolbar', () => {
editor = _editor;
model = editor.model;
widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
toolbar = widgetToolbarRepository._toolbars.get( 'mediaEmbed' ).view;
toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ).view;
balloon = editor.plugins.get( 'ContextualBalloon' );
} );
} );
Expand Down Expand Up @@ -184,7 +184,7 @@ describe( 'MediaEmbedToolbar - integration with BalloonEditor', () => {
editor = _editor;
model = editor.model;
widgetToolbarRepository = editor.plugins.get( 'WidgetToolbarRepository' );
toolbar = widgetToolbarRepository._toolbars.get( 'mediaEmbed' ).view;
toolbar = widgetToolbarRepository._toolbarDefinitions.get( 'mediaEmbed' ).view;
balloon = editor.plugins.get( 'ContextualBalloon' );
balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
} );
Expand Down