8
0

tableui.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @license Copyright (c) 2003-2019, 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/tableui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import { addListToDropdown, createDropdown } from '@ckeditor/ckeditor5-ui/src/dropdown/utils';
  10. import Model from '@ckeditor/ckeditor5-ui/src/model';
  11. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  12. import InsertTableView from './ui/inserttableview';
  13. import tableIcon from './../theme/icons/table.svg';
  14. import tableColumnIcon from './../theme/icons/table-column.svg';
  15. import tableRowIcon from './../theme/icons/table-row.svg';
  16. import tableMergeCellIcon from './../theme/icons/table-merge-cell.svg';
  17. /**
  18. * The table UI plugin. It introduces:
  19. *
  20. * * The `'insertTable'` dropdown,
  21. * * The `'tableColumn'` dropdown,
  22. * * The `'tableRow'` dropdown,
  23. * * The `'mergeTableCells'` dropdown.
  24. *
  25. * The `'tableColumn'`, `'tableRow'`, `'mergeTableCells'` dropdowns work best with {@link module:table/tabletoolbar~TableToolbar}.
  26. *
  27. * @extends module:core/plugin~Plugin
  28. */
  29. export default class TableUI extends Plugin {
  30. /**
  31. * @inheritDoc
  32. */
  33. init() {
  34. const editor = this.editor;
  35. const t = this.editor.t;
  36. const contentLanguageDirection = editor.locale.contentLanguageDirection;
  37. const isContentLtr = contentLanguageDirection === 'ltr';
  38. editor.ui.componentFactory.add( 'insertTable', locale => {
  39. const command = editor.commands.get( 'insertTable' );
  40. const dropdownView = createDropdown( locale );
  41. dropdownView.bind( 'isEnabled' ).to( command );
  42. // Decorate dropdown's button.
  43. dropdownView.buttonView.set( {
  44. icon: tableIcon,
  45. label: t( 'Insert table' ),
  46. tooltip: true
  47. } );
  48. // Prepare custom view for dropdown's panel.
  49. const insertTableView = new InsertTableView( locale );
  50. dropdownView.panelView.children.add( insertTableView );
  51. insertTableView.delegate( 'execute' ).to( dropdownView );
  52. dropdownView.buttonView.on( 'open', () => {
  53. // Reset the chooser before showing it to the user.
  54. insertTableView.rows = 0;
  55. insertTableView.columns = 0;
  56. } );
  57. dropdownView.on( 'execute', () => {
  58. editor.execute( 'insertTable', { rows: insertTableView.rows, columns: insertTableView.columns } );
  59. editor.editing.view.focus();
  60. } );
  61. return dropdownView;
  62. } );
  63. editor.ui.componentFactory.add( 'tableColumn', locale => {
  64. const options = [
  65. {
  66. type: 'switchbutton',
  67. model: {
  68. commandName: 'setTableColumnHeader',
  69. label: t( 'Header column' ),
  70. bindIsOn: true
  71. }
  72. },
  73. { type: 'separator' },
  74. {
  75. type: 'button',
  76. model: {
  77. commandName: isContentLtr ? 'insertTableColumnLeft' : 'insertTableColumnRight',
  78. label: t( 'Insert column left' )
  79. }
  80. },
  81. {
  82. type: 'button',
  83. model: {
  84. commandName: isContentLtr ? 'insertTableColumnRight' : 'insertTableColumnLeft',
  85. label: t( 'Insert column right' )
  86. }
  87. },
  88. {
  89. type: 'button',
  90. model: {
  91. commandName: 'removeTableColumn',
  92. label: t( 'Delete column' )
  93. }
  94. }
  95. ];
  96. return this._prepareDropdown( t( 'Column' ), tableColumnIcon, options, locale );
  97. } );
  98. editor.ui.componentFactory.add( 'tableRow', locale => {
  99. const options = [
  100. {
  101. type: 'switchbutton',
  102. model: {
  103. commandName: 'setTableRowHeader',
  104. label: t( 'Header row' ),
  105. bindIsOn: true
  106. }
  107. },
  108. { type: 'separator' },
  109. {
  110. type: 'button',
  111. model: {
  112. commandName: 'insertTableRowBelow',
  113. label: t( 'Insert row below' )
  114. }
  115. },
  116. {
  117. type: 'button',
  118. model: {
  119. commandName: 'insertTableRowAbove',
  120. label: t( 'Insert row above' )
  121. }
  122. },
  123. {
  124. type: 'button',
  125. model: {
  126. commandName: 'removeTableRow',
  127. label: t( 'Delete row' )
  128. }
  129. }
  130. ];
  131. return this._prepareDropdown( t( 'Row' ), tableRowIcon, options, locale );
  132. } );
  133. editor.ui.componentFactory.add( 'mergeTableCells', locale => {
  134. const options = [
  135. {
  136. type: 'button',
  137. model: {
  138. commandName: 'mergeTableCellUp',
  139. label: t( 'Merge cell up' )
  140. }
  141. },
  142. {
  143. type: 'button',
  144. model: {
  145. commandName: isContentLtr ? 'mergeTableCellRight' : 'mergeTableCellLeft',
  146. label: t( 'Merge cell right' )
  147. }
  148. },
  149. {
  150. type: 'button',
  151. model: {
  152. commandName: 'mergeTableCellDown',
  153. label: t( 'Merge cell down' )
  154. }
  155. },
  156. {
  157. type: 'button',
  158. model: {
  159. commandName: isContentLtr ? 'mergeTableCellLeft' : 'mergeTableCellRight',
  160. label: t( 'Merge cell left' )
  161. }
  162. },
  163. { type: 'separator' },
  164. {
  165. type: 'button',
  166. model: {
  167. commandName: 'splitTableCellVertically',
  168. label: t( 'Split cell vertically' )
  169. }
  170. },
  171. {
  172. type: 'button',
  173. model: {
  174. commandName: 'splitTableCellHorizontally',
  175. label: t( 'Split cell horizontally' )
  176. }
  177. }
  178. ];
  179. return this._prepareDropdown( t( 'Merge cells' ), tableMergeCellIcon, options, locale );
  180. } );
  181. }
  182. /**
  183. * Creates a dropdown view from the set of options.
  184. *
  185. * @private
  186. * @param {String} label The dropdown button label.
  187. * @param {String} icon An icon for the dropdown button.
  188. * @param {Array.<module:ui/dropdown/utils~ListDropdownItemDefinition>} options The list of options for the dropdown.
  189. * @param {module:utils/locale~Locale} locale
  190. * @returns {module:ui/dropdown/dropdownview~DropdownView}
  191. */
  192. _prepareDropdown( label, icon, options, locale ) {
  193. const editor = this.editor;
  194. const dropdownView = createDropdown( locale );
  195. const commands = [];
  196. // Prepare dropdown list items for list dropdown.
  197. const itemDefinitions = new Collection();
  198. for ( const option of options ) {
  199. addListOption( option, editor, commands, itemDefinitions );
  200. }
  201. addListToDropdown( dropdownView, itemDefinitions, editor.ui.componentFactory );
  202. // Decorate dropdown's button.
  203. dropdownView.buttonView.set( {
  204. label,
  205. icon,
  206. tooltip: true
  207. } );
  208. // Make dropdown button disabled when all options are disabled.
  209. dropdownView.bind( 'isEnabled' ).toMany( commands, 'isEnabled', ( ...areEnabled ) => {
  210. return areEnabled.some( isEnabled => isEnabled );
  211. } );
  212. this.listenTo( dropdownView, 'execute', evt => {
  213. editor.execute( evt.source.commandName );
  214. editor.editing.view.focus();
  215. } );
  216. return dropdownView;
  217. }
  218. }
  219. // Adds an option to a list view.
  220. //
  221. // @param {module:table/tableui~DropdownOption} option Configuration option.
  222. // @param {module:core/editor/editor~Editor} editor
  223. // @param {Array.<module:core/command~Command>} commands List of commands to update.
  224. // @param {Iterable.<module:ui/dropdown/utils~ListDropdownItemDefinition>} itemDefinitions
  225. // Collection of dropdown items to update with given option.
  226. function addListOption( option, editor, commands, itemDefinitions ) {
  227. const model = option.model = new Model( option.model );
  228. const { commandName, bindIsOn } = option.model;
  229. if ( option.type === 'button' || option.type === 'switchbutton' ) {
  230. const command = editor.commands.get( commandName );
  231. commands.push( command );
  232. model.set( { commandName } );
  233. model.bind( 'isEnabled' ).to( command );
  234. if ( bindIsOn ) {
  235. model.bind( 'isOn' ).to( command, 'value' );
  236. }
  237. }
  238. model.set( {
  239. withText: true
  240. } );
  241. itemDefinitions.add( option );
  242. }