utils.js 9.8 KB

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