utils.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
  7. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  8. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  9. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  10. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  11. import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
  12. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  13. import { getData, parse } from '../../../../src/dev-utils/model';
  14. import { transformSets } from '../../../../src/model/operation/transform';
  15. import Position from '../../../../src/model/position';
  16. import Range from '../../../../src/model/range';
  17. import OperationFactory from '../../../../src/model/operation/operationfactory';
  18. const clients = new Set();
  19. const bufferedOperations = new Set();
  20. export class Client {
  21. constructor( name ) {
  22. this.editor = null;
  23. this.document = null;
  24. this.syncedVersion = 0;
  25. this.orderNumber = null;
  26. this.name = name;
  27. }
  28. init() {
  29. return ModelTestEditor.create( {
  30. // Typing is needed for delete command.
  31. // UndoEditing is needed for undo command.
  32. // Block plugins are needed for proper data serializing.
  33. // BoldEditing is needed for bold command.
  34. plugins: [ Typing, Paragraph, ListEditing, UndoEditing, BlockQuoteEditing, HeadingEditing, BoldEditing ]
  35. } ).then( editor => {
  36. this.editor = editor;
  37. this.document = editor.model.document;
  38. return this;
  39. } );
  40. }
  41. setData( initModelString ) {
  42. const model = this.editor.model;
  43. const modelRoot = model.document.getRoot();
  44. // Parse data string to model.
  45. const parsedResult = parse( initModelString, model.schema, { context: [ modelRoot.name ] } );
  46. // Retrieve DocumentFragment and Selection from parsed model.
  47. const modelDocumentFragment = parsedResult.model;
  48. const selection = parsedResult.selection;
  49. model.change( writer => {
  50. // Replace existing model in document by new one.
  51. writer.remove( writer.createRangeIn( modelRoot ) );
  52. writer.insert( modelDocumentFragment, modelRoot );
  53. } );
  54. const ranges = [];
  55. for ( const range of selection.getRanges() ) {
  56. const start = new Position( modelRoot, range.start.path );
  57. const end = new Position( modelRoot, range.end.path );
  58. ranges.push( new Range( start, end ) );
  59. }
  60. model.document.selection._setTo( ranges );
  61. // Purify graveyard so there are no artifact nodes there remaining after setting new data.
  62. // Because of those old nodes some tests may pass even though they fail in real scenarios (those tests
  63. // involve bringing back elements from graveyard, like wrap or split).
  64. model.document.graveyard._removeChildren( 0, model.document.graveyard.childCount );
  65. this.syncedVersion = this.document.version;
  66. }
  67. setSelection( start, end ) {
  68. if ( !end ) {
  69. end = start.slice();
  70. }
  71. const startPos = this._getPosition( start );
  72. const endPos = this._getPosition( end );
  73. this.editor.model.document.selection._setTo( new Range( startPos, endPos ) );
  74. }
  75. insert( itemString, path ) {
  76. const item = parse( itemString, this.editor.model.schema );
  77. const position = this._getPosition( path, 'start' );
  78. this._processAction( 'insert', item, position );
  79. }
  80. type( text, attributes, path ) {
  81. const position = this._getPosition( path, 'start' );
  82. this._processAction( 'insertText', text, attributes, position );
  83. }
  84. remove( start, end ) {
  85. const startPos = this._getPosition( start, 'start' );
  86. const endPos = this._getPosition( end, 'end' );
  87. this._processAction( 'remove', new Range( startPos, endPos ) );
  88. }
  89. delete() {
  90. this._processExecute( 'delete' );
  91. }
  92. move( target, start, end ) {
  93. const targetPos = this._getPosition( target );
  94. const startPos = this._getPosition( start, 'start' );
  95. const endPos = this._getPosition( end, 'end' );
  96. this._processAction( 'move', new Range( startPos, endPos ), targetPos );
  97. }
  98. rename( newName, path ) {
  99. const pos = this._getPosition( path, 'beforeParent' );
  100. const element = pos.nodeAfter;
  101. this._processAction( 'rename', element, newName );
  102. }
  103. setAttribute( key, value, start, end ) {
  104. const startPos = this._getPosition( start, 'start' );
  105. const endPos = this._getPosition( end, 'end' );
  106. this._processAction( 'setAttribute', key, value, new Range( startPos, endPos ) );
  107. }
  108. removeAttribute( key, start, end ) {
  109. const startPos = this._getPosition( start, 'start' );
  110. const endPos = this._getPosition( end, 'end' );
  111. this._processAction( 'removeAttribute', key, new Range( startPos, endPos ) );
  112. }
  113. setMarker( name, start, end ) {
  114. let actionName;
  115. const startPos = this._getPosition( start, 'start' );
  116. const endPos = this._getPosition( end, 'end' );
  117. if ( this.editor.model.markers.has( name ) ) {
  118. actionName = 'updateMarker';
  119. } else {
  120. actionName = 'addMarker';
  121. }
  122. const range = new Range( startPos, endPos );
  123. this._processAction( actionName, name, { range, usingOperation: true } );
  124. }
  125. removeMarker( name ) {
  126. this._processAction( 'removeMarker', name );
  127. }
  128. wrap( elementName, start, end ) {
  129. const startPos = this._getPosition( start, 'start' );
  130. const endPos = this._getPosition( end, 'end' );
  131. this._processAction( 'wrap', new Range( startPos, endPos ), elementName );
  132. }
  133. unwrap( path ) {
  134. const pos = this._getPosition( path, 'beforeParent' );
  135. const element = pos.nodeAfter;
  136. this._processAction( 'unwrap', element );
  137. }
  138. merge( path ) {
  139. const pos = this._getPosition( path, 'start' );
  140. this._processAction( 'merge', pos );
  141. }
  142. split( path ) {
  143. const pos = this._getPosition( path, 'start' );
  144. this._processAction( 'split', pos );
  145. }
  146. undo() {
  147. this._processExecute( 'undo' );
  148. }
  149. redo() {
  150. this._processExecute( 'redo' );
  151. }
  152. _processExecute( commandName, commandArgs ) {
  153. const oldVersion = this.document.version;
  154. this.editor.execute( commandName, commandArgs );
  155. const operations = this.document.history.getOperations( oldVersion );
  156. bufferOperations( Array.from( operations ), this );
  157. }
  158. _getPosition( path, type ) {
  159. if ( !path ) {
  160. return this._getPositionFromSelection( type );
  161. }
  162. return new Position( this.document.getRoot(), path );
  163. }
  164. _getPositionFromSelection( type ) {
  165. const selRange = this.editor.model.document.selection.getFirstRange();
  166. switch ( type ) {
  167. default:
  168. case 'start':
  169. return selRange.start.clone();
  170. case 'end':
  171. return selRange.end.clone();
  172. case 'beforeParent':
  173. return Position._createBefore( selRange.start.parent );
  174. }
  175. }
  176. getModelString() {
  177. return getData( this.editor.model, { withoutSelection: true, convertMarkers: true } );
  178. }
  179. destroy() {
  180. clients.delete( this );
  181. return this.editor.destroy();
  182. }
  183. _processAction( name, ...args ) {
  184. const oldVersion = this.document.version;
  185. this.editor.model.change( writer => {
  186. writer[ name ]( ...args );
  187. } );
  188. const operations = Array.from( this.document.history.getOperations( oldVersion ) );
  189. bufferOperations( operations, this );
  190. }
  191. static get( clientName ) {
  192. const client = new Client( clientName );
  193. client.orderNumber = clients.size;
  194. clients.add( client );
  195. return client.init();
  196. }
  197. }
  198. function bufferOperations( operations, client ) {
  199. bufferedOperations.add( { operations, client } );
  200. }
  201. export function syncClients() {
  202. const clientsOperations = {};
  203. // For each client, flatten all buffered operations into one set.
  204. for ( const item of bufferedOperations ) {
  205. const name = item.client.name;
  206. if ( !clientsOperations[ name ] ) {
  207. clientsOperations[ name ] = [];
  208. }
  209. clientsOperations[ name ].push( ...item.operations );
  210. }
  211. for ( const localClient of clients ) {
  212. for ( const remoteClient of clients ) {
  213. if ( remoteClient == localClient ) {
  214. continue;
  215. }
  216. if ( !clientsOperations[ remoteClient.name ] ) {
  217. continue;
  218. }
  219. // Stringify and rebuild operations to simulate sending operations. Set `wasUndone`.
  220. const remoteOperationsJson = clientsOperations[ remoteClient.name ].map( operation => {
  221. operation.wasUndone = remoteClient.document.history.isUndoneOperation( operation );
  222. const json = JSON.stringify( operation );
  223. delete operation.wasUndone;
  224. return json;
  225. } );
  226. const remoteOperations = remoteOperationsJson.map( json => {
  227. const parsedJson = JSON.parse( json );
  228. const operation = OperationFactory.fromJSON( parsedJson, localClient.document );
  229. if ( parsedJson.wasUndone ) {
  230. operation.wasUndone = true;
  231. }
  232. return operation;
  233. } );
  234. const localOperations = Array.from( localClient.document.history.getOperations( localClient.syncedVersion ) );
  235. let remoteOperationsTransformed = null;
  236. const options = {
  237. document: localClient.document,
  238. useRelations: false,
  239. padWithNoOps: true
  240. };
  241. if ( localClient.orderNumber < remoteClient.orderNumber ) {
  242. remoteOperationsTransformed = transformSets( localOperations, remoteOperations, options ).operationsB;
  243. } else {
  244. remoteOperationsTransformed = transformSets( remoteOperations, localOperations, options ).operationsA;
  245. }
  246. localClient.editor.model.enqueueChange( 'transparent', writer => {
  247. for ( const operation of remoteOperationsTransformed ) {
  248. writer.batch.addOperation( operation );
  249. localClient.editor.model.applyOperation( operation );
  250. }
  251. } );
  252. }
  253. localClient.syncedVersion = localClient.document.version;
  254. }
  255. bufferedOperations.clear();
  256. }
  257. export function expectClients( expectedModelString ) {
  258. for ( const client of clients ) {
  259. expect( client.getModelString(), client.name + ' content' ).to.equal( expectedModelString );
  260. }
  261. let syncedVersion = null;
  262. for ( const client of clients ) {
  263. if ( syncedVersion === null ) {
  264. syncedVersion = client.syncedVersion;
  265. continue;
  266. }
  267. expect( client.syncedVersion, client.name + ' version' ).to.equal( syncedVersion );
  268. }
  269. }
  270. export function clearBuffer() {
  271. bufferedOperations.clear();
  272. }