8
0

tablepropertiesui.js 10 KB

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