utils.js 8.5 KB

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