Вы можете использовать метод DropdownView.on () для прослушивания события execute
.
И, используйте свойство EventInfo.source для получения объекта по щелчку, а затем используйте его свойство, например id
или label
, чтобы идентифицировать его.
Например:
const items = new Collection();
items.add( {
type: 'button',
model: new Model({
id: 'en', // id
withText: true,
label: 'English',
})
} );
items.add( {
type: 'button',
model: new Model({
id: 'es', // id
withText: true,
label: 'Spanish'
})
} );
addListToDropdown(dropdownView, items);
dropdownView.on('execute', (eventInfo) => {
const { id, label } = eventInfo.source;
if ( id === 'en' ) {
console.log('Object (en):', label);
} else if ( id === 'es' ) {
console.log('Object (es):', label);
}
});
Вот полный рабочий пример с ClassicEditor
(проверено) :
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Model from '@ckeditor/ckeditor5-ui/src/model';
import Collection from '@ckeditor/ckeditor5-utils/src/collection';
import { createDropdown, addListToDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
const Translate = 'translate';
class Translation extends Plugin {
init() {
console.log('Translation initialized!');
this.editor.ui.componentFactory.add(Translate, (locale) => {
const dropdownView = createDropdown(locale);
dropdownView.buttonView.set({
label: 'Translate',
withText: true,
});
const items = new Collection();
items.add( {
type: 'button',
model: new Model({
id: 'en',
withText: true,
label: 'English',
})
} );
items.add( {
type: 'button',
model: new Model({
id: 'es',
withText: true,
label: 'Spanish'
})
} );
addListToDropdown(dropdownView, items);
dropdownView.on('execute', (eventInfo) => {
const { id, label } = eventInfo.source;
if ( id === 'en' ) {
console.log('Object (en):', label);
} else if ( id === 'es' ) {
console.log('Object (es):', label);
}
});
return dropdownView;
});
}
}
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ Essentials, Translation ],
toolbar: [ Translate ]
} )
.then( editor => {
console.log( 'Editor was initialized', editor );
} )
.catch( error => {
console.error( error.stack );
} );
Вывод на консоль после нажатия на оба элемента:
Object (en): English
Object (es): Spanish