tablemocking.js 5.4 KB

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