8
0

utils.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. redo() {
  144. this._processExecute( 'redo' );
  145. }
  146. _processExecute( commandName, commandArgs ) {
  147. const oldVersion = this.document.version;
  148. this.editor.execute( commandName, commandArgs );
  149. const operations = this.document.history.getOperations( oldVersion );
  150. bufferOperations( Array.from( operations ), this );
  151. }
  152. _getPosition( path, type ) {
  153. if ( !path ) {
  154. return this._getPositionFromSelection( type );
  155. }
  156. return new Position( this.document.getRoot(), path );
  157. }
  158. _getPositionFromSelection( type ) {
  159. const selRange = this.editor.model.document.selection.getFirstRange();
  160. switch ( type ) {
  161. default:
  162. case 'start':
  163. return Position.createFromPosition( selRange.start );
  164. case 'end':
  165. return Position.createFromPosition( selRange.end );
  166. case 'beforeParent':
  167. return Position.createBefore( selRange.start.parent );
  168. }
  169. }
  170. getModelString() {
  171. return getData( this.editor.model, { withoutSelection: true, convertMarkers: true } );
  172. }
  173. destroy() {
  174. clients.delete( this );
  175. return this.editor.destroy();
  176. }
  177. _processAction( name, ...args ) {
  178. const oldVersion = this.document.version;
  179. this.editor.model.change( writer => {
  180. writer[ name ]( ...args );
  181. } );
  182. const operations = Array.from( this.document.history.getOperations( oldVersion ) );
  183. bufferOperations( operations, this );
  184. }
  185. static get( clientName ) {
  186. const client = new Client( clientName );
  187. client.orderNumber = clients.size;
  188. clients.add( client );
  189. return client.init();
  190. }
  191. }
  192. function bufferOperations( operations, client ) {
  193. bufferedOperations.add( { operations, client } );
  194. }
  195. export function syncClients() {
  196. const clientsOperations = {};
  197. // For each client, flatten all buffered operations into one set.
  198. for ( const item of bufferedOperations ) {
  199. const name = item.client.name;
  200. if ( !clientsOperations[ name ] ) {
  201. clientsOperations[ name ] = [];
  202. }
  203. clientsOperations[ name ].push( ...item.operations );
  204. }
  205. for ( const localClient of clients ) {
  206. for ( const remoteClient of clients ) {
  207. if ( remoteClient == localClient ) {
  208. continue;
  209. }
  210. if ( !clientsOperations[ remoteClient.name ] ) {
  211. continue;
  212. }
  213. // Stringify and rebuild operations to simulate sending operations. Set `wasUndone`.
  214. const remoteOperationsJson = clientsOperations[ remoteClient.name ].map( operation => {
  215. operation.wasUndone = remoteClient.document.history.isUndoneOperation( operation );
  216. const json = JSON.stringify( operation );
  217. delete operation.wasUndone;
  218. return json;
  219. } );
  220. const remoteOperations = remoteOperationsJson.map( json => {
  221. const parsedJson = JSON.parse( json );
  222. const operation = OperationFactory.fromJSON( parsedJson, localClient.document );
  223. if ( parsedJson.wasUndone ) {
  224. operation.wasUndone = true;
  225. }
  226. return operation;
  227. } );
  228. const localOperations = Array.from( localClient.document.history.getOperations( localClient.syncedVersion ) );
  229. let remoteOperationsTransformed = null;
  230. const options = {
  231. document: localClient.document,
  232. useRelations: false,
  233. padWithNoOps: true
  234. };
  235. if ( localClient.orderNumber < remoteClient.orderNumber ) {
  236. remoteOperationsTransformed = transformSets( localOperations, remoteOperations, options ).operationsB;
  237. } else {
  238. remoteOperationsTransformed = transformSets( remoteOperations, localOperations, options ).operationsA;
  239. }
  240. localClient.editor.model.enqueueChange( 'transparent', writer => {
  241. for ( const operation of remoteOperationsTransformed ) {
  242. writer.batch.addOperation( operation );
  243. localClient.editor.model.applyOperation( operation );
  244. }
  245. } );
  246. }
  247. localClient.syncedVersion = localClient.document.version;
  248. }
  249. bufferedOperations.clear();
  250. }
  251. export function expectClients( expectedModelString ) {
  252. for ( const client of clients ) {
  253. expect( client.getModelString(), client.name + ' content' ).to.equal( expectedModelString );
  254. }
  255. let syncedVersion = null;
  256. for ( const client of clients ) {
  257. if ( syncedVersion === null ) {
  258. syncedVersion = client.syncedVersion;
  259. continue;
  260. }
  261. expect( client.syncedVersion, client.name + ' version' ).to.equal( syncedVersion );
  262. }
  263. }
  264. export function clearBuffer() {
  265. bufferedOperations.clear();
  266. }