utils.js 9.7 KB

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