utils.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import {
  6. downcastInsertCell,
  7. downcastInsertRow,
  8. downcastInsertTable,
  9. downcastRemoveRow,
  10. downcastTableHeadingColumnsChange,
  11. downcastTableHeadingRowsChange
  12. } from '../../src/converters/downcast';
  13. import upcastTable, { upcastTableCell } from '../../src/converters/upcasttable';
  14. /**
  15. * Returns a model representation of a table shorthand notation:
  16. *
  17. * modelTable( [
  18. * [ '00' ] // first row
  19. * [ '10' ] // second row
  20. * ] );
  21. *
  22. * will output:
  23. *
  24. * '<table><tableRow><tableCell>00</tableCell></tableRow><tableRow><tableCell>10</tableCell></tableRow></table>'
  25. *
  26. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  27. *
  28. * Passing an object allows to pass additional table cell attributes:
  29. *
  30. * const tableCellData = {
  31. * colspan: 2,
  32. * rowspan: 4,
  33. * contents: 'foo' // text contents of a cell
  34. * };
  35. *
  36. * @param {Array.<String>} tableData
  37. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns`.
  38. *
  39. * @returns {String}
  40. */
  41. export function modelTable( tableData, attributes ) {
  42. const tableRows = makeRows( tableData, {
  43. cellElement: 'tableCell',
  44. rowElement: 'tableRow',
  45. headingElement: 'tableCell',
  46. wrappingElement: 'paragraph',
  47. enforceWrapping: true
  48. } );
  49. return `<table${ formatAttributes( attributes ) }>${ tableRows }</table>`;
  50. }
  51. /**
  52. * Returns a view representation of a table shorthand notation:
  53. *
  54. * viewTable( [
  55. * [ '00', '01' ] // first row
  56. * [ '10', '11' ] // second row
  57. * ] );
  58. *
  59. * will output:
  60. *
  61. * '<table><tbody><tr><td>00</td><td>01<td></tr><tr><td>10</td><td>11<td></tr></tbody></table>'
  62. *
  63. * Each table row passed in `tableData` array is represented as an array of strings or objects. A string defines text contents of a cell.
  64. *
  65. * Passing an object allows to pass additional table cell attributes:
  66. *
  67. * const tableCellData = {
  68. * colspan: 2,
  69. * rowspan: 4,
  70. * isHeading: true, // will render table cell as `<th>` element
  71. * contents: 'foo' // text contents of a cell
  72. * };
  73. *
  74. * @param {Array.<Array.<String|Object>>} tableData The table data array.
  75. * @param {Object} [attributes] Optional table attributes: `headingRows` and `headingColumns` - passing them will properly render rows
  76. * in `<tbody>` or `<thead>` sections.
  77. *
  78. * @returns {String}
  79. */
  80. export function viewTable( tableData, attributes = {} ) {
  81. const headingRows = attributes.headingRows || 0;
  82. const asWidget = !!attributes.asWidget;
  83. const thead = headingRows > 0 ? `<thead>${ makeRows( tableData.slice( 0, headingRows ), {
  84. cellElement: 'th',
  85. rowElement: 'tr',
  86. headingElement: 'th',
  87. wrappingElement: 'p',
  88. asWidget
  89. } ) }</thead>` : '';
  90. const tbody = tableData.length > headingRows ?
  91. `<tbody>${ makeRows( tableData.slice( headingRows ), {
  92. cellElement: 'td',
  93. rowElement: 'tr',
  94. headingElement: 'th',
  95. wrappingElement: 'p',
  96. asWidget
  97. } ) }</tbody>` : '';
  98. const figureAttributes = asWidget ? 'class="ck-widget ck-widget_selectable table" contenteditable="false"' : 'class="table"';
  99. const widgetHandler = '<div class="ck ck-widget__selection-handler"></div>';
  100. return `<figure ${ figureAttributes }>${ asWidget ? widgetHandler : '' }<table>${ thead }${ tbody }</table></figure>`;
  101. }
  102. /**
  103. * Formats model or view table - useful for chai assertions debugging.
  104. *
  105. * @param {String} tableString
  106. * @returns {String}
  107. */
  108. export function formatTable( tableString ) {
  109. return tableString
  110. .replace( /<tableRow>/g, '\n<tableRow>\n ' )
  111. .replace( /<thead>/g, '\n<thead>\n ' )
  112. .replace( /<tbody>/g, '\n<tbody>\n ' )
  113. .replace( /<tr>/g, '\n<tr>\n ' )
  114. .replace( /<\/tableRow>/g, '\n</tableRow>' )
  115. .replace( /<\/thead>/g, '\n</thead>' )
  116. .replace( /<\/tbody>/g, '\n</tbody>' )
  117. .replace( /<\/tr>/g, '\n</tr>' )
  118. .replace( /<\/table>/g, '\n</table>' )
  119. .replace( /<\/figure>/g, '\n</figure>' );
  120. }
  121. /**
  122. * Returns formatted model table string.
  123. *
  124. * @param {Array.<String>} tableData
  125. * @param {Object} [attributes]
  126. * @returns {String}
  127. */
  128. export function formattedModelTable( tableData, attributes ) {
  129. const tableString = modelTable( tableData, attributes );
  130. return formatTable( tableString );
  131. }
  132. /**
  133. * Returns formatted view table string.
  134. *
  135. * @param {Array.<String>} tableData
  136. * @param {Object} [attributes]
  137. * @returns {String}
  138. */
  139. export function formattedViewTable( tableData, attributes ) {
  140. return formatTable( viewTable( tableData, attributes ) );
  141. }
  142. export function defaultSchema( schema ) {
  143. schema.register( 'table', {
  144. allowWhere: '$block',
  145. allowAttributes: [ 'headingRows', 'headingColumns' ],
  146. isObject: true
  147. } );
  148. schema.register( 'tableRow', { allowIn: 'table' } );
  149. schema.register( 'tableCell', {
  150. allowIn: 'tableRow',
  151. allowContentOf: '$block',
  152. allowAttributes: [ 'colspan', 'rowspan' ],
  153. isLimit: true
  154. } );
  155. schema.extend( '$block', { allowIn: 'tableCell' } );
  156. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  157. }
  158. export function defaultConversion( conversion, asWidget = false ) {
  159. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  160. // Table conversion.
  161. conversion.for( 'upcast' ).add( upcastTable() );
  162. conversion.for( 'downcast' ).add( downcastInsertTable( { asWidget } ) );
  163. // Table row conversion.
  164. conversion.for( 'downcast' ).add( downcastInsertRow( { asWidget } ) );
  165. conversion.for( 'downcast' ).add( downcastRemoveRow( { asWidget } ) );
  166. // Table cell conversion.
  167. conversion.for( 'downcast' ).add( downcastInsertCell( { asWidget } ) );
  168. conversion.for( 'upcast' ).add( upcastTableCell( 'td' ) );
  169. conversion.for( 'upcast' ).add( upcastTableCell( 'th' ) );
  170. // Table attributes conversion.
  171. conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
  172. conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );
  173. conversion.for( 'downcast' ).add( downcastTableHeadingColumnsChange( { asWidget } ) );
  174. conversion.for( 'downcast' ).add( downcastTableHeadingRowsChange( { asWidget } ) );
  175. }
  176. // Formats table cell attributes
  177. //
  178. // @param {Object} attributes Attributes of a cell.
  179. function formatAttributes( attributes ) {
  180. let attributesString = '';
  181. if ( attributes ) {
  182. const entries = Object.entries( attributes );
  183. if ( entries.length ) {
  184. attributesString = ' ' + entries.map( entry => `${ entry[ 0 ] }="${ entry[ 1 ] }"` ).join( ' ' );
  185. }
  186. }
  187. return attributesString;
  188. }
  189. // Formats passed table data to a set of table rows.
  190. function makeRows( tableData, options ) {
  191. const { cellElement, rowElement, headingElement, wrappingElement, enforceWrapping, asWidget } = options;
  192. return tableData
  193. .reduce( ( previousRowsString, tableRow ) => {
  194. const tableRowString = tableRow.reduce( ( tableRowString, tableCellData ) => {
  195. const isObject = typeof tableCellData === 'object';
  196. let contents = isObject ? tableCellData.contents : tableCellData;
  197. let resultingCellElement = cellElement;
  198. if ( isObject ) {
  199. // TODO: check...
  200. if ( tableCellData.isHeading ) {
  201. resultingCellElement = headingElement;
  202. }
  203. delete tableCellData.contents;
  204. delete tableCellData.isHeading;
  205. }
  206. const attributes = isObject ? tableCellData : {};
  207. if ( asWidget ) {
  208. attributes.class = 'ck-editor__editable ck-editor__nested-editable';
  209. attributes.contenteditable = 'true';
  210. }
  211. if ( !( contents.replace( '[', '' ).replace( ']', '' ).startsWith( '<' ) ) && enforceWrapping ) {
  212. contents = `<${ wrappingElement }>${ contents }</${ wrappingElement }>`;
  213. }
  214. const formattedAttributes = formatAttributes( attributes );
  215. tableRowString += `<${ resultingCellElement }${ formattedAttributes }>${ contents }</${ resultingCellElement }>`;
  216. return tableRowString;
  217. }, '' );
  218. return `${ previousRowsString }<${ rowElement }>${ tableRowString }</${ rowElement }>`;
  219. }, '' );
  220. }