tablecellborderstylecommand.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module table/tablecellproperties/commands/tablecellborderstylecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import { findAncestor, getSingleValue } from '../../commands/utils';
  10. /**
  11. * The table cell border style command.
  12. *
  13. * The command is registered by {@link module:table/tablecellproperties/tablecellpropertiesediting~TableCellPropertiesEditing} as
  14. * `'tableCellBorderStyle'` editor command.
  15. *
  16. * To change cell border style of the selected cell, execute the command:
  17. *
  18. * editor.execute( 'tableCellBorderStyle', {
  19. * value: '5px'
  20. * } );
  21. *
  22. * @extends module:core/command~Command
  23. */
  24. export default class TableCellBorderStyleCommand extends Command {
  25. constructor( editor ) {
  26. super( editor );
  27. this.attributeName = 'borderStyle';
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. refresh() {
  33. const editor = this.editor;
  34. const selection = editor.model.document.selection;
  35. const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
  36. this.isEnabled = !!tableCell;
  37. this.value = this._getValue( tableCell );
  38. }
  39. _getValue( tableCell ) {
  40. if ( !tableCell ) {
  41. return;
  42. }
  43. return getSingleValue( tableCell.getAttribute( this.attributeName ) );
  44. }
  45. /**
  46. * Executes the command.
  47. *
  48. * @fires execute
  49. * @param {Object} [options]
  50. * @param {Boolean} [options.value] If set the command will set border style.
  51. * If border style is not set the command will remove the attribute.
  52. */
  53. execute( options = {} ) {
  54. const model = this.editor.model;
  55. const selection = model.document.selection;
  56. const { value } = options;
  57. const tableCells = Array.from( selection.getSelectedBlocks() )
  58. .map( element => findAncestor( 'tableCell', model.createPositionAt( element, 0 ) ) );
  59. model.change( writer => {
  60. if ( value ) {
  61. tableCells.forEach( tableCell => writer.setAttribute( this.attributeName, value, tableCell ) );
  62. } else {
  63. tableCells.forEach( tableCell => writer.removeAttribute( this.attributeName, tableCell ) );
  64. }
  65. } );
  66. }
  67. }