tablemocking.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ClassicEditor
  14. .create( document.querySelector( '#editor' ), {
  15. plugins: [ ArticlePluginSet ],
  16. toolbar: [
  17. 'insertTable', 'undo', 'redo'
  18. ],
  19. table: {
  20. contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
  21. }
  22. } )
  23. .then( editor => {
  24. window.editor = editor;
  25. const asciiOut = document.getElementById( 'ascii-art' );
  26. const modelData = document.getElementById( 'model-data' );
  27. document.getElementById( 'clear-content' ).addEventListener( 'click', () => {
  28. editor.setData( '' );
  29. } );
  30. document.getElementById( 'set-model-data' ).addEventListener( 'click', () => {
  31. updateInputStatus();
  32. const inputModelData = parseModelData( modelData.value );
  33. setModelData( editor.model, inputModelData ? modelTable( inputModelData ) : '' );
  34. } );
  35. document.getElementById( 'get-model-data' ).addEventListener( 'click', () => {
  36. updateInputStatus();
  37. const table = findTable( editor );
  38. modelData.value = table ? prettyFormatModelTableInput( prepareModelTableInput( editor.model, table ) ) : '';
  39. updateAsciiAndDiff();
  40. } );
  41. document.getElementById( 'renumber-cells' ).addEventListener( 'click', () => {
  42. const table = findTable( editor );
  43. if ( !table ) {
  44. return;
  45. }
  46. editor.model.change( writer => {
  47. for ( const { row, column, cell } of new TableWalker( table ) ) {
  48. const selection = editor.model.createSelection( cell, 'in' );
  49. editor.model.insertContent( writer.createText( `${ row }${ column }` ), selection );
  50. }
  51. } );
  52. updateAsciiAndDiff();
  53. } );
  54. editor.model.document.on( 'change:data', debounce( () => updateAsciiAndDiff(), 100 ) );
  55. updateAsciiAndDiff();
  56. function updateAsciiAndDiff() {
  57. const table = findTable( editor );
  58. if ( !table ) {
  59. asciiOut.innerText = '-- table not found --';
  60. return;
  61. }
  62. const inputModelData = parseModelData( modelData.value );
  63. const currentModelData = prepareModelTableInput( editor.model, table );
  64. const diffOutput = inputModelData ? diffString( inputModelData, currentModelData, {
  65. theme: {
  66. ' ': string => string,
  67. '+': string => `<span class="diff-add">${ string }</span>`,
  68. '-': string => `<span class="diff-del">${ string }</span>`
  69. }
  70. } ) : '-- no input --';
  71. asciiOut.innerHTML = createTableAsciiArt( editor.model, table ) + '\n\n' +
  72. 'Diff: input vs post-fixed model:\n' + ( diffOutput ? diffOutput : '-- no differences --' );
  73. }
  74. function findTable( editor ) {
  75. const range = editor.model.createRangeIn( editor.model.document.getRoot() );
  76. for ( const element of range.getItems() ) {
  77. if ( element.is( 'table' ) ) {
  78. return element;
  79. }
  80. }
  81. return null;
  82. }
  83. function parseModelData( string ) {
  84. if ( !string.trim() ) {
  85. return null;
  86. }
  87. const jsonString = string
  88. .replace( /'/g, '"' )
  89. .replace( /([a-z0-9$_]+)\s*:/gi, '"$1":' );
  90. try {
  91. return JSON.parse( jsonString );
  92. } catch ( error ) {
  93. updateInputStatus( error.message );
  94. }
  95. return null;
  96. }
  97. function updateInputStatus( message = '' ) {
  98. document.getElementById( 'input-status' ).innerText = message;
  99. }
  100. } )
  101. .catch( err => {
  102. console.error( err.stack );
  103. } );