tablemocking.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals console, window, document */
  6. import { createTableAsciiArt, modelTable, prepareModelTableInput, prettyFormatModelTableInput } from '../_utils/utils';
  7. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  8. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. import { diffString } from 'json-diff';
  10. import { debounce } from 'lodash-es';
  11. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  12. import TableWalker from '../../src/tablewalker';
  13. import { getSelectionAffectedTableCells } from '../../src/utils/selection';
  14. ClassicEditor
  15. .create( document.querySelector( '#editor' ), {
  16. plugins: [ ArticlePluginSet ],
  17. toolbar: [
  18. 'insertTable', 'undo', 'redo'
  19. ],
  20. table: {
  21. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  22. }
  23. } )
  24. .then( editor => {
  25. window.editor = editor;
  26. const asciiOut = document.getElementById( 'ascii-art' );
  27. const modelData = document.getElementById( 'model-data' );
  28. document.getElementById( 'clear-content' ).addEventListener( 'click', () => {
  29. editor.setData( '' );
  30. } );
  31. document.getElementById( 'set-model-data' ).addEventListener( 'click', () => {
  32. updateInputStatus();
  33. const table = findTable( editor );
  34. const inputModelData = parseModelData( modelData.value );
  35. if ( inputModelData ) {
  36. const element = setModelData._parse( modelTable( inputModelData ), editor.model.schema );
  37. editor.model.change( writer => {
  38. editor.model.insertContent( element, table ? editor.model.createRangeOn( table ) : null );
  39. writer.setSelection( element, 'on' );
  40. } );
  41. editor.editing.view.focus();
  42. }
  43. } );
  44. document.getElementById( 'get-model-data' ).addEventListener( 'click', () => {
  45. updateInputStatus();
  46. const table = findTable( editor, true );
  47. modelData.value = table ? prettyFormatModelTableInput( prepareModelTableInput( editor.model, table ) ) : '';
  48. updateAsciiAndDiff();
  49. } );
  50. document.getElementById( 'renumber-cells' ).addEventListener( 'click', () => {
  51. const table = findTable( editor, true );
  52. if ( !table ) {
  53. return;
  54. }
  55. const useLetters = document.getElementById( 'use-letters' ).checked;
  56. editor.model.change( writer => {
  57. for ( const { row, column, cell } of new TableWalker( table ) ) {
  58. const selection = editor.model.createSelection( cell, 'in' );
  59. editor.model.insertContent( writer.createText( createCellText( row, column, useLetters ) ), selection );
  60. }
  61. } );
  62. updateAsciiAndDiff();
  63. } );
  64. editor.model.document.on( 'change:data', debounce( () => updateAsciiAndDiff(), 100 ) );
  65. updateAsciiAndDiff();
  66. function updateAsciiAndDiff() {
  67. const tables = getAllTables( editor );
  68. if ( !tables.length ) {
  69. asciiOut.innerText = '-- table not found --';
  70. return;
  71. }
  72. const inputModelData = parseModelData( modelData.value );
  73. const currentModelData = prepareModelTableInput( editor.model, tables[ 0 ] );
  74. const diffOutput = inputModelData ? diffString( inputModelData, currentModelData, {
  75. theme: {
  76. ' ': string => string,
  77. '+': string => `<span class="diff-add">${ string }</span>`,
  78. '-': string => `<span class="diff-del">${ string }</span>`
  79. }
  80. } ) : '-- no input --';
  81. const asciiArt = tables
  82. .map( table => createTableAsciiArt( editor.model, table ) )
  83. .join( '\n\n' );
  84. asciiOut.innerHTML = asciiArt + '\n\n' +
  85. 'Diff: input vs post-fixed model (only first table):\n' + ( diffOutput ? diffOutput : '-- no differences --' );
  86. }
  87. function findTable( editor, useAnyTable = false ) {
  88. const selection = editor.model.document.selection;
  89. const tableCells = getSelectionAffectedTableCells( selection );
  90. if ( tableCells.length ) {
  91. return tableCells[ 0 ].findAncestor( 'table' );
  92. }
  93. const element = selection.getSelectedElement();
  94. if ( element && element.is( 'table' ) ) {
  95. return element;
  96. }
  97. if ( useAnyTable ) {
  98. const range = editor.model.createRangeIn( editor.model.document.getRoot() );
  99. for ( const element of range.getItems() ) {
  100. if ( element.is( 'table' ) ) {
  101. return element;
  102. }
  103. }
  104. }
  105. return null;
  106. }
  107. function getAllTables( editor ) {
  108. const range = editor.model.createRangeIn( editor.model.document.getRoot() );
  109. const tables = [];
  110. for ( const element of range.getItems() ) {
  111. if ( element.is( 'table' ) ) {
  112. tables.push( element );
  113. }
  114. }
  115. return tables;
  116. }
  117. function parseModelData( string ) {
  118. if ( !string.trim() ) {
  119. return null;
  120. }
  121. const jsonString = string
  122. .replace( /'/g, '"' )
  123. .replace( /([a-z0-9$_]+)\s*:/gi, '"$1":' );
  124. try {
  125. return JSON.parse( jsonString );
  126. } catch ( error ) {
  127. updateInputStatus( error.message );
  128. }
  129. return null;
  130. }
  131. function updateInputStatus( message = '' ) {
  132. document.getElementById( 'input-status' ).innerText = message;
  133. }
  134. function createCellText( row, column, useLetters ) {
  135. const rowLabel = useLetters ? String.fromCharCode( row + 'a'.charCodeAt( 0 ) ) : row;
  136. const columnLabel = useLetters ? String.fromCharCode( column + 'a'.charCodeAt( 0 ) ) : column;
  137. return `${ rowLabel }${ columnLabel }`;
  138. }
  139. } )
  140. .catch( err => {
  141. console.error( err.stack );
  142. } );