screenshare.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const decodeCells = xmlString => {
  2. const parsedXml = new DOMParser().parseFromString(xmlString, "text/xml").firstElementChild;
  3. const codec = new mxCodec();
  4. return codec.decode(parsedXml);
  5. };
  6. const encodeCells = cells => {
  7. const codec = new mxCodec();
  8. const encoded = codec.encode(cells);
  9. return new XMLSerializer().serializeToString(encoded);
  10. };
  11. class ScreenShare {
  12. constructor(client, graph, confirm, alert) {
  13. this.graph = graph;
  14. this.sharingWith = {};
  15. this.confirm = confirm;
  16. this.alert = alert;
  17. const shareEvent = serializer => {
  18. // return new event listener for mxEventSource:
  19. return (source, eventObj) => {
  20. const props = serializer(eventObj.properties);
  21. if (props !== undefined) {
  22. Object.keys(this.sharingWith).forEach(peer => {
  23. this.p2p.send(peer, "push_edit", {
  24. event: eventObj.name,
  25. props,
  26. }, (err, data) => {
  27. if (err) {}
  28. else {}
  29. });
  30. })
  31. }
  32. };
  33. };
  34. // We do not use these events because they are fired in the middle of an edit transaction (instead of at the end)
  35. // wrong: CELLS_ADDED(cells: Array[mxCell], parent: mxCell, index: int, absolute: bool)
  36. // wrong: CELLS_MOVED(cells: Array[mxCell], dx, dy)
  37. // wrong: CELLS_REMOVED(cells: Array[mxCell])
  38. // right: MOVE_CELLS(cells: Array{mxCell], clone: bool, dx, dy, target: mxCell, event: PointerEvent})
  39. this.graph.addListener(mxEvent.MOVE_CELLS, shareEvent(({cells, target, clone, dx, dy}) => {
  40. const data = {
  41. targetId: target ? target.id : null,
  42. clone, dx, dy,
  43. }
  44. if (clone) {
  45. // this is also true when a new cell was added
  46. return {
  47. ...data,
  48. cellsXml: encodeCells(cells),
  49. }
  50. } else {
  51. return {
  52. ...data,
  53. cellIds: cells.map(cell => cell.id),
  54. }
  55. }
  56. }))
  57. this.graph.addListener(mxEvent.REMOVE_CELLS, shareEvent(({cells, includeEdges}) => {
  58. if (cells.length > 0) {
  59. // for some reason moving cells causes REMOVE_CELLS to be fired with 0 cells - do not share the event in this case :)
  60. return {
  61. cellIds: cells.map(cell => cell.id),
  62. includeEdges,
  63. }
  64. }
  65. }))
  66. this.graph.addListener(mxEvent.RESIZE_CELLS, shareEvent(({cells, bounds, previous}) => {
  67. return {
  68. cellIds: cells.map(cell => cell.id),
  69. boundsXml: encodeCells(bounds),
  70. }
  71. }))
  72. // edge: mxCell
  73. // source: boolean (whether source of edge (=directed) was connected, or target)
  74. // terminal: mxCell to which connect happened (potentially undefined)
  75. // previous: mxCell from which disconnect happened (potentially undefined)
  76. this.graph.addListener(mxEvent.CONNECT_CELL, shareEvent(({edge, source, terminal, previous}) => {
  77. return {
  78. edgeId: edge.id,
  79. edgeGeometryXml: encodeCells(edge.geometry),
  80. source,
  81. terminalId: terminal ? terminal.id : null,
  82. previousId: previous ? previous.id : null,
  83. }
  84. }))
  85. // CELLS_RESIZED(cells: .., bounds: mxRectangle{x,y,width,height}, previous: Array[mxGeometry{x,y,width,height, ...}])
  86. // CELL_CONNECTED(edge, mxCell, source: bool, [terminal: mxCell,] [previous: mxCell])
  87. // source: whether source of edge connected
  88. // terminal: cell connected to
  89. // previous: cell disconnected from
  90. // Handler for incoming requests from other peers
  91. this.p2p = new PeerToPeer(client, {
  92. // incoming request from another peer
  93. "push_edit": (from, {event, props}, reply) => {
  94. if (this.sharingWith[from]) {
  95. // received edit from other peer
  96. this.graph.setEventsEnabled(false);
  97. ({
  98. [mxEvent.MOVE_CELLS]: ({cellsXml, cellIds, targetId, clone, dx, dy}) => {
  99. const target = targetId !== null ? this.graph.model.cells[targetId] : undefined;
  100. if (clone) {
  101. const cells = decodeCells(cellsXml);
  102. const result = this.graph.moveCells(cells,
  103. 0, 0, // dx, dy - the cells themselves already contain the correct offset. using dx and dy would position them at twice their position-vector
  104. false, // clone - not necessary because decodeCells already created the cells, we want to simply "move" them to the target. if true, the cells would be given new ids
  105. target);
  106. } else {
  107. const cells = cellIds.map(id => this.graph.model.cells[id]);
  108. this.graph.moveCells(cells, dx, dy, clone, target);
  109. }
  110. },
  111. [mxEvent.REMOVE_CELLS]: ({cellIds, includeEdges}) => {
  112. const cells = cellIds.map(id => this.graph.model.cells[id]);
  113. this.graph.removeCells(cells, includeEdges);
  114. },
  115. [mxEvent.RESIZE_CELLS]: ({cellIds, boundsXml}) => {
  116. const cells = cellIds.map(id => this.graph.model.cells[id]);
  117. const bounds = decodeCells(boundsXml);
  118. this.graph.resizeCells(cells, bounds,
  119. false); // recurse
  120. },
  121. [mxEvent.CONNECT_CELL]: ({edgeId, edgeGeometryXml, source, terminalId, previousId}) => {
  122. const edge = this.graph.model.cells[edgeId];
  123. const geometry = decodeCells(edgeGeometryXml);
  124. const terminal = terminalId !== null ? this.graph.model.cells[terminalId] : undefined;
  125. const previous = previousId !== null ? this.graph.model.cells[previousId] : undefined;
  126. edge.setGeometry(geometry);
  127. this.graph.connectCell(edge, terminal, source,
  128. null); // "constraint". the docs say: optional <mxConnectionConstraint>. are we missing something here?
  129. },
  130. }[event])(props);
  131. this.graph.setEventsEnabled(true);
  132. reply(); // acknowledge
  133. }
  134. },
  135. "init_screenshare": (from, graphSerialized, reply) => {
  136. const yes = () => {
  137. const doc = mxUtils.parseXml(graphSerialized);
  138. const codec = new mxCodec(doc);
  139. codec.decode(doc.documentElement, graph.model);
  140. this.sharingWith[from] = true;
  141. reply(); // acknowledge
  142. this.alert("You are now <b>screen sharing</b> with " + shortUUID(from));
  143. };
  144. const no = () => {
  145. reply("denied")
  146. };
  147. this.confirm(`Peer ${shortUUID(from)} wants to <b>screen share</b>.<br />Your diagram will be erased and replaced by his/hers.<br /><br />Accept?`, yes, no);
  148. },
  149. });
  150. }
  151. initshare(peer) {
  152. const codec = new mxCodec();
  153. const graphSerialized = mxUtils.getXml(codec.encode(this.graph.model));
  154. this.p2p.send(peer, "init_screenshare", graphSerialized, (err, data) => {
  155. if (err) {
  156. this.alert(err)
  157. }
  158. else {
  159. this.alert("Accepted: You are now <b>screen sharing</b> with " + shortUUID(peer));
  160. this.sharingWith[peer] = true;
  161. }
  162. });
  163. }
  164. }