8
0

tablecellpropertiesui.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/tablecellproperties/tablecellpropertiesui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import { getTableWidgetAncestor } from '../utils';
  11. import clickOutsideHandler from '@ckeditor/ckeditor5-ui/src/bindings/clickoutsidehandler';
  12. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  13. import TableCellPropertiesView from './ui/tablecellpropertiesview';
  14. import tableCellProperties from './../../theme/icons/table-cell-properties.svg';
  15. import {
  16. colorFieldValidator,
  17. getBalloonCellPositionData,
  18. getLocalizedColorErrorText,
  19. getLocalizedLengthErrorText,
  20. defaultColors,
  21. lengthFieldValidator,
  22. lineWidthFieldValidator,
  23. repositionContextualBalloon
  24. } from '../ui/utils';
  25. import {
  26. getLocalizedColorOptions,
  27. normalizeColorOptions
  28. } from '@ckeditor/ckeditor5-ui/src/colorgrid/utils';
  29. import { debounce } from 'lodash-es';
  30. const ERROR_TEXT_TIMEOUT = 500;
  31. /**
  32. * The table cell properties UI plugin. It introduces the `'tableCellProperties'` button
  33. * that opens a form allowing to specify visual styling of a table cell.
  34. *
  35. * It uses the
  36. * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
  37. *
  38. * @extends module:core/plugin~Plugin
  39. */
  40. export default class TableCellPropertiesUI extends Plugin {
  41. /**
  42. * @inheritDoc
  43. */
  44. static get requires() {
  45. return [ ContextualBalloon ];
  46. }
  47. /**
  48. * @inheritDoc
  49. */
  50. static get pluginName() {
  51. return 'TableCellPropertiesUI';
  52. }
  53. /**
  54. * @inheritDoc
  55. */
  56. constructor( editor ) {
  57. super( editor );
  58. editor.config.define( 'table.tableCellProperties', {
  59. borderColors: defaultColors,
  60. backgroundColors: defaultColors
  61. } );
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. init() {
  67. const editor = this.editor;
  68. const t = editor.t;
  69. /**
  70. * The contextual balloon plugin instance.
  71. *
  72. * @private
  73. * @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
  74. */
  75. this._balloon = editor.plugins.get( ContextualBalloon );
  76. /**
  77. * The cell properties form view displayed inside the balloon.
  78. *
  79. * @member {module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView}
  80. */
  81. this.view = this._createPropertiesView();
  82. /**
  83. * The batch used to undo all changes made by the form (which are live, as the user types)
  84. * when "Cancel" was pressed. Each time the view is shown, a new batch is created.
  85. *
  86. * @protected
  87. * @member {module:engine/model/batch~Batch}
  88. */
  89. this._undoStepBatch = null;
  90. editor.ui.componentFactory.add( 'tableCellProperties', locale => {
  91. const view = new ButtonView( locale );
  92. view.set( {
  93. label: t( 'Cell properties' ),
  94. icon: tableCellProperties,
  95. tooltip: true
  96. } );
  97. this.listenTo( view, 'execute', () => this._showView() );
  98. return view;
  99. } );
  100. }
  101. /**
  102. * @inheritDoc
  103. */
  104. destroy() {
  105. super.destroy();
  106. // Destroy created UI components as they are not automatically destroyed.
  107. // See https://github.com/ckeditor/ckeditor5/issues/1341.
  108. this.view.destroy();
  109. }
  110. /**
  111. * Creates the {@link module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView} instance.
  112. *
  113. * @private
  114. * @returns {module:table/tablecellproperties/ui/tablecellpropertiesview~TableCellPropertiesView} The cell
  115. * properties form view instance.
  116. */
  117. _createPropertiesView() {
  118. const editor = this.editor;
  119. const viewDocument = editor.editing.view.document;
  120. const config = editor.config.get( 'table.tableCellProperties' );
  121. const borderColorsConfig = normalizeColorOptions( config.borderColors );
  122. const localizedBorderColors = getLocalizedColorOptions( editor.locale, borderColorsConfig );
  123. const backgroundColorsConfig = normalizeColorOptions( config.backgroundColors );
  124. const localizedBackgroundColors = getLocalizedColorOptions( editor.locale, backgroundColorsConfig );
  125. const view = new TableCellPropertiesView( editor.locale, {
  126. borderColors: localizedBorderColors,
  127. backgroundColors: localizedBackgroundColors
  128. } );
  129. const t = editor.t;
  130. // Render the view so its #element is available for the clickOutsideHandler.
  131. view.render();
  132. this.listenTo( view, 'submit', () => {
  133. this._hideView();
  134. } );
  135. this.listenTo( view, 'cancel', () => {
  136. // https://github.com/ckeditor/ckeditor5/issues/6180
  137. if ( this._undoStepBatch.operations.length ) {
  138. editor.execute( 'undo', this._undoStepBatch );
  139. }
  140. this._hideView();
  141. } );
  142. // Close the balloon on Esc key press.
  143. view.keystrokes.set( 'Esc', ( data, cancel ) => {
  144. this._hideView();
  145. cancel();
  146. } );
  147. // Reposition the balloon or hide the form if a table cell is no longer selected.
  148. this.listenTo( editor.ui, 'update', () => {
  149. if ( !getTableWidgetAncestor( viewDocument.selection ) ) {
  150. this._hideView();
  151. } else if ( this._isViewVisible ) {
  152. repositionContextualBalloon( editor, 'cell' );
  153. }
  154. } );
  155. // Close on click outside of balloon panel element.
  156. clickOutsideHandler( {
  157. emitter: view,
  158. activator: () => this._isViewInBalloon,
  159. contextElements: [ this._balloon.view.element ],
  160. callback: () => this._hideView()
  161. } );
  162. const colorErrorText = getLocalizedColorErrorText( t );
  163. const lengthErrorText = getLocalizedLengthErrorText( t );
  164. // Create the "UI -> editor data" binding.
  165. // These listeners update the editor data (via table commands) when any observable
  166. // property of the view has changed. They also validate the value and display errors in the UI
  167. // when necessary. This makes the view live, which means the changes are
  168. // visible in the editing as soon as the user types or changes fields' values.
  169. view.on( 'change:borderStyle', this._getPropertyChangeCallback( 'tableCellBorderStyle' ) );
  170. view.on( 'change:borderColor', this._getValidatedPropertyChangeCallback( {
  171. viewField: view.borderColorInput,
  172. commandName: 'tableCellBorderColor',
  173. errorText: colorErrorText,
  174. validator: colorFieldValidator
  175. } ) );
  176. view.on( 'change:borderWidth', this._getValidatedPropertyChangeCallback( {
  177. viewField: view.borderWidthInput,
  178. commandName: 'tableCellBorderWidth',
  179. errorText: lengthErrorText,
  180. validator: lineWidthFieldValidator
  181. } ) );
  182. view.on( 'change:padding', this._getValidatedPropertyChangeCallback( {
  183. viewField: view.paddingInput,
  184. commandName: 'tableCellPadding',
  185. errorText: lengthErrorText,
  186. validator: lengthFieldValidator
  187. } ) );
  188. view.on( 'change:width', this._getValidatedPropertyChangeCallback( {
  189. viewField: view.widthInput,
  190. commandName: 'tableCellWidth',
  191. errorText: lengthErrorText,
  192. validator: lengthFieldValidator
  193. } ) );
  194. view.on( 'change:height', this._getValidatedPropertyChangeCallback( {
  195. viewField: view.heightInput,
  196. commandName: 'tableCellHeight',
  197. errorText: lengthErrorText,
  198. validator: lengthFieldValidator
  199. } ) );
  200. view.on( 'change:backgroundColor', this._getValidatedPropertyChangeCallback( {
  201. viewField: view.backgroundInput,
  202. commandName: 'tableCellBackgroundColor',
  203. errorText: colorErrorText,
  204. validator: colorFieldValidator
  205. } ) );
  206. view.on( 'change:horizontalAlignment', this._getPropertyChangeCallback( 'tableCellHorizontalAlignment' ) );
  207. view.on( 'change:verticalAlignment', this._getPropertyChangeCallback( 'tableCellVerticalAlignment' ) );
  208. return view;
  209. }
  210. /**
  211. * In this method the "editor data -> UI" binding is happening.
  212. *
  213. * When executed, this method obtains selected cell property values from various table commands
  214. * and passes them to the {@link #view}.
  215. *
  216. * This way, the UI stays up–to–date with the editor data.
  217. *
  218. * @private
  219. */
  220. _fillViewFormFromCommandValues() {
  221. const commands = this.editor.commands;
  222. this.view.set( {
  223. borderStyle: commands.get( 'tableCellBorderStyle' ).value || '',
  224. borderColor: commands.get( 'tableCellBorderColor' ).value || '',
  225. borderWidth: commands.get( 'tableCellBorderWidth' ).value || '',
  226. width: commands.get( 'tableCellWidth' ).value || '',
  227. height: commands.get( 'tableCellHeight' ).value || '',
  228. padding: commands.get( 'tableCellPadding' ).value || '',
  229. backgroundColor: commands.get( 'tableCellBackgroundColor' ).value || '',
  230. horizontalAlignment: commands.get( 'tableCellHorizontalAlignment' ).value || '',
  231. verticalAlignment: commands.get( 'tableCellVerticalAlignment' ).value || ''
  232. } );
  233. }
  234. /**
  235. * Shows the {@link #view} in the {@link #_balloon}.
  236. *
  237. * **Note**: Each time a view is shown, the new {@link #_undoStepBatch} is created that contains
  238. * all changes made to the document when the view is visible, allowing a single undo step
  239. * for all of them.
  240. *
  241. * @protected
  242. */
  243. _showView() {
  244. const editor = this.editor;
  245. this._balloon.add( {
  246. view: this.view,
  247. position: getBalloonCellPositionData( editor )
  248. } );
  249. // Create a new batch. Clicking "Cancel" will undo this batch.
  250. this._undoStepBatch = editor.model.createBatch();
  251. // Update the view with the model values.
  252. this._fillViewFormFromCommandValues();
  253. // Basic a11y.
  254. this.view.focus();
  255. }
  256. /**
  257. * Removes the {@link #view} from the {@link #_balloon}.
  258. *
  259. * @protected
  260. */
  261. _hideView() {
  262. if ( !this._isViewInBalloon ) {
  263. return;
  264. }
  265. const editor = this.editor;
  266. this.stopListening( editor.ui, 'update' );
  267. // Blur any input element before removing it from DOM to prevent issues in some browsers.
  268. // See https://github.com/ckeditor/ckeditor5/issues/1501.
  269. this.view.saveButtonView.focus();
  270. this._balloon.remove( this.view );
  271. // Make sure the focus is not lost in the process by putting it directly
  272. // into the editing view.
  273. this.editor.editing.view.focus();
  274. }
  275. /**
  276. * Returns `true` when the {@link #view} is the visible in the {@link #_balloon}.
  277. *
  278. * @private
  279. * @type {Boolean}
  280. */
  281. get _isViewVisible() {
  282. return this._balloon.visibleView === this.view;
  283. }
  284. /**
  285. * Returns `true` when the {@link #view} is in the {@link #_balloon}.
  286. *
  287. * @private
  288. * @type {Boolean}
  289. */
  290. get _isViewInBalloon() {
  291. return this._balloon.hasView( this.view );
  292. }
  293. /**
  294. * Creates a callback that when executed upon {@link #view view's} property change
  295. * executes a related editor command with the new property value.
  296. *
  297. * @private
  298. * @param {String} commandName
  299. * @returns {Function}
  300. */
  301. _getPropertyChangeCallback( commandName ) {
  302. return ( evt, propertyName, newValue ) => {
  303. this.editor.execute( commandName, {
  304. value: newValue,
  305. batch: this._undoStepBatch
  306. } );
  307. };
  308. }
  309. /**
  310. * Creates a callback that when executed upon {@link #view view's} property change:
  311. * * executes a related editor command with the new property value if the value is valid,
  312. * * or sets the error text next to the invalid field, if the value did not pass the validation.
  313. *
  314. * @private
  315. * @param {Object} options
  316. * @param {String} options.commandName
  317. * @param {module:ui/view~View} options.viewField
  318. * @param {Function} options.validator
  319. * @param {String} options.errorText
  320. * @returns {Function}
  321. */
  322. _getValidatedPropertyChangeCallback( { commandName, viewField, validator, errorText } ) {
  323. const setErrorTextDebounced = debounce( () => {
  324. viewField.errorText = errorText;
  325. }, ERROR_TEXT_TIMEOUT );
  326. return ( evt, propertyName, newValue ) => {
  327. setErrorTextDebounced.cancel();
  328. if ( validator( newValue ) ) {
  329. this.editor.execute( commandName, {
  330. value: newValue,
  331. batch: this._undoStepBatch
  332. } );
  333. viewField.errorText = null;
  334. } else {
  335. setErrorTextDebounced();
  336. }
  337. };
  338. }
  339. }