| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Feature from '../core/feature.js';
- import ListEngine from './listengine.js';
- import ButtonView from '../ui/button/buttonview.js';
- import { parseKeystroke } from '../utils/keyboard.js';
- /**
- * The lists feature. It introduces the `numberedList` and `bulletedList` buttons which
- * allows to convert paragraphs to/from list items and indent/outdent them.
- *
- * See also {@link list.ListEngine}.
- *
- * @memberOf list
- * @extends core.Feature
- */
- export default class List extends Feature {
- /**
- * @inheritDoc
- */
- static get requires() {
- return [ ListEngine ];
- }
- /**
- * @inheritDoc
- */
- init() {
- // Create two buttons and link them with numberedList and bulletedList commands.
- const t = this.editor.t;
- this._addButton( 'numberedList', t( 'Numbered List' ) );
- this._addButton( 'bulletedList', t( 'Bulleted List' ) );
- // Overwrite default enter key behavior.
- // If enter key is pressed with selection collapsed in empty list item, outdent it instead of breaking it.
- this.listenTo( this.editor.editing.view, 'enter', ( evt, data ) => {
- const doc = this.editor.document;
- const positionParent = doc.selection.getLastPosition().parent;
- if ( doc.selection.isCollapsed && positionParent.name == 'listItem' && positionParent.isEmpty ) {
- this.editor.execute( 'outdentList' );
- data.preventDefault();
- evt.stop();
- }
- } );
- // Add tab key support.
- // When in list item, pressing tab should indent list item, if possible.
- // Pressing shift + tab shout outdent list item.
- this.listenTo( this.editor.editing.view, 'keydown', ( evt, data ) => {
- let commandName = null;
- if ( data.keystroke == parseKeystroke( 'tab' ) ) {
- commandName = 'indentList';
- } else if ( data.keystroke == parseKeystroke( 'Shift+tab' ) ) {
- commandName = 'outdentList';
- }
- if ( commandName ) {
- const command = this.editor.commands.get( commandName );
- if ( command.isEnabled ) {
- this.editor.execute( commandName );
- data.preventDefault();
- evt.stop();
- }
- }
- } );
- }
- /**
- * Helper method for initializing a button and linking it with an appropriate command.
- *
- * @private
- * @param {String} commandName Name of the command.
- * @param {Object} label Button label.
- */
- _addButton( commandName, label ) {
- const editor = this.editor;
- const command = editor.commands.get( commandName );
- // Add button to feature components.
- editor.ui.featureComponents.add( commandName, ( locale ) => {
- const buttonView = new ButtonView( locale );
- buttonView.set( {
- label: label,
- icon: commandName.toLowerCase()
- } );
- // Bind button model to command.
- buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
- // Execute command.
- this.listenTo( buttonView, 'execute', () => editor.execute( commandName ) );
- return buttonView;
- } );
- }
- }
|