sharejs.js 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3. var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. /* eslint-disable
  6. camelcase,
  7. max-len,
  8. no-class-assign,
  9. no-return-assign,
  10. no-undef,
  11. no-unused-vars,
  12. no-use-before-define,
  13. standard/object-curly-even-spacing,
  14. */
  15. // TODO: This file was created by bulk-decaffeinate.
  16. // Fix any style issues and re-enable lint.
  17. /*
  18. * decaffeinate suggestions:
  19. * DS101: Remove unnecessary use of Array.from
  20. * DS102: Remove unnecessary code created because of implicit returns
  21. * DS205: Consider reworking code to avoid use of IIFEs
  22. * DS207: Consider shorter variations of null checks
  23. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  24. */
  25. define(['ace/ace','libs/sha1'], function () {
  26. var append = void 0,
  27. bootstrapTransform = void 0,
  28. exports = void 0,
  29. transformComponent = void 0;
  30. var WEB = true;
  31. window.sharejs = exports = {};
  32. var types = exports.types = {};
  33. // These methods let you build a transform function from a transformComponent function
  34. // for OT types like text and JSON in which operations are lists of components
  35. // and transforming them requires N^2 work.
  36. // Add transform and transformX functions for an OT type which has transformComponent defined.
  37. // transformComponent(destination array, component, other component, side)
  38. exports['_bt'] = bootstrapTransform = function bootstrapTransform(type, transformComponent, checkValidOp, append) {
  39. var _transformX = void 0;
  40. var transformComponentX = function transformComponentX(left, right, destLeft, destRight) {
  41. transformComponent(destLeft, left, right, 'left');
  42. return transformComponent(destRight, right, left, 'right');
  43. };
  44. // Transforms rightOp by leftOp. Returns ['rightOp', clientOp']
  45. type.transformX = type['transformX'] = _transformX = function transformX(leftOp, rightOp) {
  46. checkValidOp(leftOp);
  47. checkValidOp(rightOp);
  48. var newRightOp = [];
  49. var _iteratorNormalCompletion = true;
  50. var _didIteratorError = false;
  51. var _iteratorError = undefined;
  52. try {
  53. for (var _iterator = Array.from(rightOp)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  54. var rightComponent = _step.value;
  55. // Generate newLeftOp by composing leftOp by rightComponent
  56. var newLeftOp = [];
  57. var k = 0;
  58. while (k < leftOp.length) {
  59. var l;
  60. var nextC = [];
  61. transformComponentX(leftOp[k], rightComponent, newLeftOp, nextC);
  62. k++;
  63. if (nextC.length === 1) {
  64. rightComponent = nextC[0];
  65. } else if (nextC.length === 0) {
  66. var _iteratorNormalCompletion2 = true;
  67. var _didIteratorError2 = false;
  68. var _iteratorError2 = undefined;
  69. try {
  70. for (var _iterator2 = Array.from(leftOp.slice(k))[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
  71. l = _step2.value;
  72. append(newLeftOp, l);
  73. }
  74. } catch (err) {
  75. _didIteratorError2 = true;
  76. _iteratorError2 = err;
  77. } finally {
  78. try {
  79. if (!_iteratorNormalCompletion2 && _iterator2.return) {
  80. _iterator2.return();
  81. }
  82. } finally {
  83. if (_didIteratorError2) {
  84. throw _iteratorError2;
  85. }
  86. }
  87. }
  88. rightComponent = null;
  89. break;
  90. } else {
  91. // Recurse.
  92. var _Array$from = Array.from(_transformX(leftOp.slice(k), nextC)),
  93. _Array$from2 = _slicedToArray(_Array$from, 2),
  94. l_ = _Array$from2[0],
  95. r_ = _Array$from2[1];
  96. var _iteratorNormalCompletion3 = true;
  97. var _didIteratorError3 = false;
  98. var _iteratorError3 = undefined;
  99. try {
  100. for (var _iterator3 = Array.from(l_)[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
  101. l = _step3.value;
  102. append(newLeftOp, l);
  103. }
  104. } catch (err) {
  105. _didIteratorError3 = true;
  106. _iteratorError3 = err;
  107. } finally {
  108. try {
  109. if (!_iteratorNormalCompletion3 && _iterator3.return) {
  110. _iterator3.return();
  111. }
  112. } finally {
  113. if (_didIteratorError3) {
  114. throw _iteratorError3;
  115. }
  116. }
  117. }
  118. var _iteratorNormalCompletion4 = true;
  119. var _didIteratorError4 = false;
  120. var _iteratorError4 = undefined;
  121. try {
  122. for (var _iterator4 = Array.from(r_)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
  123. var r = _step4.value;
  124. append(newRightOp, r);
  125. }
  126. } catch (err) {
  127. _didIteratorError4 = true;
  128. _iteratorError4 = err;
  129. } finally {
  130. try {
  131. if (!_iteratorNormalCompletion4 && _iterator4.return) {
  132. _iterator4.return();
  133. }
  134. } finally {
  135. if (_didIteratorError4) {
  136. throw _iteratorError4;
  137. }
  138. }
  139. }
  140. rightComponent = null;
  141. break;
  142. }
  143. }
  144. if (rightComponent != null) {
  145. append(newRightOp, rightComponent);
  146. }
  147. leftOp = newLeftOp;
  148. }
  149. } catch (err) {
  150. _didIteratorError = true;
  151. _iteratorError = err;
  152. } finally {
  153. try {
  154. if (!_iteratorNormalCompletion && _iterator.return) {
  155. _iterator.return();
  156. }
  157. } finally {
  158. if (_didIteratorError) {
  159. throw _iteratorError;
  160. }
  161. }
  162. }
  163. return [leftOp, newRightOp];
  164. };
  165. // Transforms op with specified type ('left' or 'right') by otherOp.
  166. return type.transform = type['transform'] = function (op, otherOp, type) {
  167. var _ = void 0;
  168. if (type !== 'left' && type !== 'right') {
  169. throw new Error("type must be 'left' or 'right'");
  170. }
  171. if (otherOp.length === 0) {
  172. return op;
  173. }
  174. // TODO: Benchmark with and without this line. I _think_ it'll make a big difference...?
  175. if (op.length === 1 && otherOp.length === 1) {
  176. return transformComponent([], op[0], otherOp[0], type);
  177. }
  178. if (type === 'left') {
  179. var left = void 0;
  180. var _Array$from3 = Array.from(_transformX(op, otherOp));
  181. var _Array$from4 = _slicedToArray(_Array$from3, 2);
  182. left = _Array$from4[0];
  183. _ = _Array$from4[1];
  184. return left;
  185. } else {
  186. var right = void 0;
  187. var _Array$from5 = Array.from(_transformX(otherOp, op));
  188. var _Array$from6 = _slicedToArray(_Array$from5, 2);
  189. _ = _Array$from6[0];
  190. right = _Array$from6[1];
  191. return right;
  192. }
  193. };
  194. };
  195. if (typeof WEB === 'undefined') {
  196. exports.bootstrapTransform = bootstrapTransform;
  197. }
  198. // A simple text implementation
  199. //
  200. // Operations are lists of components.
  201. // Each component either inserts or deletes at a specified position in the document.
  202. //
  203. // Components are either:
  204. // {i:'str', p:100}: Insert 'str' at position 100 in the document
  205. // {d:'str', p:100}: Delete 'str' at position 100 in the document
  206. //
  207. // Components in an operation are executed sequentially, so the position of components
  208. // assumes previous components have already executed.
  209. //
  210. // Eg: This op:
  211. // [{i:'abc', p:0}]
  212. // is equivalent to this op:
  213. // [{i:'a', p:0}, {i:'b', p:1}, {i:'c', p:2}]
  214. // NOTE: The global scope here is shared with other sharejs files when built with closure.
  215. // Be careful what ends up in your namespace.
  216. var text = {};
  217. text.name = 'text';
  218. text.create = function () {
  219. return '';
  220. };
  221. var strInject = function strInject(s1, pos, s2) {
  222. return s1.slice(0, pos) + s2 + s1.slice(pos);
  223. };
  224. var checkValidComponent = function checkValidComponent(c) {
  225. if (typeof c.p !== 'number') {
  226. throw new Error('component missing position field');
  227. }
  228. var i_type = _typeof(c.i);
  229. var d_type = _typeof(c.d);
  230. var c_type = _typeof(c.c);
  231. if (!(i_type === 'string' ^ d_type === 'string' ^ c_type === 'string')) {
  232. throw new Error('component needs an i, d or c field');
  233. }
  234. if (!(c.p >= 0)) {
  235. throw new Error('position cannot be negative');
  236. }
  237. };
  238. var checkValidOp = function checkValidOp(op) {
  239. var _iteratorNormalCompletion5 = true;
  240. var _didIteratorError5 = false;
  241. var _iteratorError5 = undefined;
  242. try {
  243. for (var _iterator5 = Array.from(op)[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
  244. var c = _step5.value;
  245. checkValidComponent(c);
  246. }
  247. } catch (err) {
  248. _didIteratorError5 = true;
  249. _iteratorError5 = err;
  250. } finally {
  251. try {
  252. if (!_iteratorNormalCompletion5 && _iterator5.return) {
  253. _iterator5.return();
  254. }
  255. } finally {
  256. if (_didIteratorError5) {
  257. throw _iteratorError5;
  258. }
  259. }
  260. }
  261. return true;
  262. };
  263. text.apply = function (snapshot, op) {
  264. checkValidOp(op);
  265. var _iteratorNormalCompletion6 = true;
  266. var _didIteratorError6 = false;
  267. var _iteratorError6 = undefined;
  268. try {
  269. for (var _iterator6 = Array.from(op)[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
  270. var component = _step6.value;
  271. if (component.i != null) {
  272. snapshot = strInject(snapshot, component.p, component.i);
  273. } else if (component.d != null) {
  274. var deleted = snapshot.slice(component.p, component.p + component.d.length);
  275. if (component.d !== deleted) {
  276. throw new Error('Delete component \'' + component.d + '\' does not match deleted text \'' + deleted + '\'');
  277. }
  278. snapshot = snapshot.slice(0, component.p) + snapshot.slice(component.p + component.d.length);
  279. } else if (component.c != null) {
  280. var comment = snapshot.slice(component.p, component.p + component.c.length);
  281. if (component.c !== comment) {
  282. throw new Error('Comment component \'' + component.c + '\' does not match commented text \'' + comment + '\'');
  283. }
  284. } else {
  285. throw new Error('Unknown op type');
  286. }
  287. }
  288. } catch (err) {
  289. _didIteratorError6 = true;
  290. _iteratorError6 = err;
  291. } finally {
  292. try {
  293. if (!_iteratorNormalCompletion6 && _iterator6.return) {
  294. _iterator6.return();
  295. }
  296. } finally {
  297. if (_didIteratorError6) {
  298. throw _iteratorError6;
  299. }
  300. }
  301. }
  302. return snapshot;
  303. };
  304. var cloneAndModify = function cloneAndModify(op, modifications) {
  305. var v = void 0;
  306. var newOp = {};
  307. for (var k in op) {
  308. v = op[k];
  309. newOp[k] = v;
  310. }
  311. for (k in modifications) {
  312. v = modifications[k];
  313. newOp[k] = v;
  314. }
  315. return newOp;
  316. };
  317. // Exported for use by the random op generator.
  318. //
  319. // For simplicity, this version of append does not compress adjacent inserts and deletes of
  320. // the same text. It would be nice to change that at some stage.
  321. text._append = append = function append(newOp, c) {
  322. if (c.i === '' || c.d === '') {
  323. return;
  324. }
  325. if (newOp.length === 0) {
  326. return newOp.push(c);
  327. } else {
  328. var last = newOp[newOp.length - 1];
  329. // Compose the insert into the previous insert if possible
  330. if (last.i != null && c.i != null && last.p <= c.p && c.p <= last.p + last.i.length && last.u === c.u) {
  331. return newOp[newOp.length - 1] = cloneAndModify(last, { i: strInject(last.i, c.p - last.p, c.i) });
  332. } else if (last.d != null && c.d != null && c.p <= last.p && last.p <= c.p + c.d.length && last.u === c.u) {
  333. return newOp[newOp.length - 1] = cloneAndModify(last, { d: strInject(c.d, last.p - c.p, last.d), p: c.p });
  334. } else {
  335. return newOp.push(c);
  336. }
  337. }
  338. };
  339. text.compose = function (op1, op2) {
  340. checkValidOp(op1);
  341. checkValidOp(op2);
  342. var newOp = op1.slice();
  343. var _iteratorNormalCompletion7 = true;
  344. var _didIteratorError7 = false;
  345. var _iteratorError7 = undefined;
  346. try {
  347. for (var _iterator7 = Array.from(op2)[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
  348. var c = _step7.value;
  349. append(newOp, c);
  350. }
  351. } catch (err) {
  352. _didIteratorError7 = true;
  353. _iteratorError7 = err;
  354. } finally {
  355. try {
  356. if (!_iteratorNormalCompletion7 && _iterator7.return) {
  357. _iterator7.return();
  358. }
  359. } finally {
  360. if (_didIteratorError7) {
  361. throw _iteratorError7;
  362. }
  363. }
  364. }
  365. return newOp;
  366. };
  367. // Attempt to compress the op components together 'as much as possible'.
  368. // This implementation preserves order and preserves create/delete pairs.
  369. text.compress = function (op) {
  370. return text.compose([], op);
  371. };
  372. text.normalize = function (op) {
  373. var newOp = [];
  374. // Normalize should allow ops which are a single (unwrapped) component:
  375. // {i:'asdf', p:23}.
  376. // There's no good way to test if something is an array:
  377. // http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
  378. // so this is probably the least bad solution.
  379. if (op.i != null || op.p != null) {
  380. op = [op];
  381. }
  382. var _iteratorNormalCompletion8 = true;
  383. var _didIteratorError8 = false;
  384. var _iteratorError8 = undefined;
  385. try {
  386. for (var _iterator8 = Array.from(op)[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
  387. var c = _step8.value;
  388. if (c.p == null) {
  389. c.p = 0;
  390. }
  391. append(newOp, c);
  392. }
  393. } catch (err) {
  394. _didIteratorError8 = true;
  395. _iteratorError8 = err;
  396. } finally {
  397. try {
  398. if (!_iteratorNormalCompletion8 && _iterator8.return) {
  399. _iterator8.return();
  400. }
  401. } finally {
  402. if (_didIteratorError8) {
  403. throw _iteratorError8;
  404. }
  405. }
  406. }
  407. return newOp;
  408. };
  409. // This helper method transforms a position by an op component.
  410. //
  411. // If c is an insert, insertAfter specifies whether the transform
  412. // is pushed after the insert (true) or before it (false).
  413. //
  414. // insertAfter is optional for deletes.
  415. var transformPosition = function transformPosition(pos, c, insertAfter) {
  416. if (c.i != null) {
  417. if (c.p < pos || c.p === pos && insertAfter) {
  418. return pos + c.i.length;
  419. } else {
  420. return pos;
  421. }
  422. } else if (c.d != null) {
  423. // I think this could also be written as: Math.min(c.p, Math.min(c.p - otherC.p, otherC.d.length))
  424. // but I think its harder to read that way, and it compiles using ternary operators anyway
  425. // so its no slower written like this.
  426. if (pos <= c.p) {
  427. return pos;
  428. } else if (pos <= c.p + c.d.length) {
  429. return c.p;
  430. } else {
  431. return pos - c.d.length;
  432. }
  433. } else if (c.c != null) {
  434. return pos;
  435. } else {
  436. throw new Error('unknown op type');
  437. }
  438. };
  439. // Helper method to transform a cursor position as a result of an op.
  440. //
  441. // Like transformPosition above, if c is an insert, insertAfter specifies whether the cursor position
  442. // is pushed after an insert (true) or before it (false).
  443. text.transformCursor = function (position, op, side) {
  444. var insertAfter = side === 'right';
  445. var _iteratorNormalCompletion9 = true;
  446. var _didIteratorError9 = false;
  447. var _iteratorError9 = undefined;
  448. try {
  449. for (var _iterator9 = Array.from(op)[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
  450. var c = _step9.value;
  451. position = transformPosition(position, c, insertAfter);
  452. }
  453. } catch (err) {
  454. _didIteratorError9 = true;
  455. _iteratorError9 = err;
  456. } finally {
  457. try {
  458. if (!_iteratorNormalCompletion9 && _iterator9.return) {
  459. _iterator9.return();
  460. }
  461. } finally {
  462. if (_didIteratorError9) {
  463. throw _iteratorError9;
  464. }
  465. }
  466. }
  467. return position;
  468. };
  469. // Transform an op component by another op component. Asymmetric.
  470. // The result will be appended to destination.
  471. //
  472. // exported for use in JSON type
  473. text._tc = transformComponent = function transformComponent(dest, c, otherC, side) {
  474. var cIntersect = void 0,
  475. intersectEnd = void 0,
  476. intersectStart = void 0,
  477. newC = void 0,
  478. otherIntersect = void 0;
  479. checkValidOp([c]);
  480. checkValidOp([otherC]);
  481. if (c.i != null) {
  482. append(dest, cloneAndModify(c, { p: transformPosition(c.p, otherC, side === 'right') }));
  483. } else if (c.d != null) {
  484. // Delete
  485. if (otherC.i != null) {
  486. // delete vs insert
  487. var s = c.d;
  488. if (c.p < otherC.p) {
  489. append(dest, cloneAndModify(c, { d: s.slice(0, otherC.p - c.p) }));
  490. s = s.slice(otherC.p - c.p);
  491. }
  492. if (s !== '') {
  493. append(dest, cloneAndModify(c, { d: s, p: c.p + otherC.i.length }));
  494. }
  495. } else if (otherC.d != null) {
  496. // Delete vs delete
  497. if (c.p >= otherC.p + otherC.d.length) {
  498. append(dest, cloneAndModify(c, { p: c.p - otherC.d.length }));
  499. } else if (c.p + c.d.length <= otherC.p) {
  500. append(dest, c);
  501. } else {
  502. // They overlap somewhere.
  503. newC = cloneAndModify(c, { d: '' });
  504. if (c.p < otherC.p) {
  505. newC.d = c.d.slice(0, otherC.p - c.p);
  506. }
  507. if (c.p + c.d.length > otherC.p + otherC.d.length) {
  508. newC.d += c.d.slice(otherC.p + otherC.d.length - c.p);
  509. }
  510. // This is entirely optional - just for a check that the deleted
  511. // text in the two ops matches
  512. intersectStart = Math.max(c.p, otherC.p);
  513. intersectEnd = Math.min(c.p + c.d.length, otherC.p + otherC.d.length);
  514. cIntersect = c.d.slice(intersectStart - c.p, intersectEnd - c.p);
  515. otherIntersect = otherC.d.slice(intersectStart - otherC.p, intersectEnd - otherC.p);
  516. if (cIntersect !== otherIntersect) {
  517. throw new Error('Delete ops delete different text in the same region of the document');
  518. }
  519. if (newC.d !== '') {
  520. // This could be rewritten similarly to insert v delete, above.
  521. newC.p = transformPosition(newC.p, otherC);
  522. append(dest, newC);
  523. }
  524. }
  525. } else if (otherC.c != null) {
  526. append(dest, c);
  527. } else {
  528. throw new Error('unknown op type');
  529. }
  530. } else if (c.c != null) {
  531. // Comment
  532. if (otherC.i != null) {
  533. if (c.p < otherC.p && otherC.p < c.p + c.c.length) {
  534. var offset = otherC.p - c.p;
  535. var new_c = c.c.slice(0, +(offset - 1) + 1 || undefined) + otherC.i + c.c.slice(offset);
  536. append(dest, cloneAndModify(c, { c: new_c }));
  537. } else {
  538. append(dest, cloneAndModify(c, { p: transformPosition(c.p, otherC, true) }));
  539. }
  540. } else if (otherC.d != null) {
  541. if (c.p >= otherC.p + otherC.d.length) {
  542. append(dest, cloneAndModify(c, { p: c.p - otherC.d.length }));
  543. } else if (c.p + c.c.length <= otherC.p) {
  544. append(dest, c);
  545. } else {
  546. // Delete overlaps comment
  547. // They overlap somewhere.
  548. newC = cloneAndModify(c, { c: '' });
  549. if (c.p < otherC.p) {
  550. newC.c = c.c.slice(0, otherC.p - c.p);
  551. }
  552. if (c.p + c.c.length > otherC.p + otherC.d.length) {
  553. newC.c += c.c.slice(otherC.p + otherC.d.length - c.p);
  554. }
  555. // This is entirely optional - just for a check that the deleted
  556. // text in the two ops matches
  557. intersectStart = Math.max(c.p, otherC.p);
  558. intersectEnd = Math.min(c.p + c.c.length, otherC.p + otherC.d.length);
  559. cIntersect = c.c.slice(intersectStart - c.p, intersectEnd - c.p);
  560. otherIntersect = otherC.d.slice(intersectStart - otherC.p, intersectEnd - otherC.p);
  561. if (cIntersect !== otherIntersect) {
  562. throw new Error('Delete ops delete different text in the same region of the document');
  563. }
  564. newC.p = transformPosition(newC.p, otherC);
  565. append(dest, newC);
  566. }
  567. } else if (otherC.c != null) {
  568. append(dest, c);
  569. } else {
  570. throw new Error('unknown op type');
  571. }
  572. }
  573. return dest;
  574. };
  575. var invertComponent = function invertComponent(c) {
  576. if (c.i != null) {
  577. return { d: c.i, p: c.p };
  578. } else {
  579. return { i: c.d, p: c.p };
  580. }
  581. };
  582. // No need to use append for invert, because the components won't be able to
  583. // cancel with one another.
  584. text.invert = function (op) {
  585. return Array.from(op.slice().reverse()).map(function (c) {
  586. return invertComponent(c);
  587. });
  588. };
  589. if (WEB != null) {
  590. if (!exports.types) {
  591. exports.types = {};
  592. }
  593. // This is kind of awful - come up with a better way to hook this helper code up.
  594. bootstrapTransform(text, transformComponent, checkValidOp, append);
  595. // [] is used to prevent closure from renaming types.text
  596. exports.types.text = text;
  597. } else {
  598. module.exports = text;
  599. // The text type really shouldn't need this - it should be possible to define
  600. // an efficient transform function by making a sort of transform map and passing each
  601. // op component through it.
  602. require('./helpers').bootstrapTransform(text, transformComponent, checkValidOp, append);
  603. }
  604. // Text document API for text
  605. if (typeof WEB === 'undefined') {
  606. text = require('./text');
  607. }
  608. text.api = {
  609. provides: { text: true },
  610. // The number of characters in the string
  611. getLength: function getLength() {
  612. return this.snapshot.length;
  613. },
  614. // Get the text contents of a document
  615. getText: function getText() {
  616. return this.snapshot;
  617. },
  618. insert: function insert(pos, text, fromUndo, callback) {
  619. var op = { p: pos, i: text };
  620. if (fromUndo) {
  621. op.u = true;
  622. }
  623. op = [op];
  624. this.submitOp(op, callback);
  625. return op;
  626. },
  627. del: function del(pos, length, fromUndo, callback) {
  628. var op = { p: pos, d: this.snapshot.slice(pos, pos + length) };
  629. if (fromUndo) {
  630. op.u = true;
  631. }
  632. op = [op];
  633. this.submitOp(op, callback);
  634. return op;
  635. },
  636. _register: function _register() {
  637. return this.on('remoteop', function (op) {
  638. var _this = this;
  639. return function () {
  640. var result = [];
  641. var _iteratorNormalCompletion10 = true;
  642. var _didIteratorError10 = false;
  643. var _iteratorError10 = undefined;
  644. try {
  645. for (var _iterator10 = Array.from(op)[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
  646. var component = _step10.value;
  647. if (component.i !== undefined) {
  648. result.push(_this.emit('insert', component.p, component.i));
  649. } else if (component.d !== undefined) {
  650. result.push(_this.emit('delete', component.p, component.d));
  651. } else {
  652. result.push(undefined);
  653. }
  654. }
  655. } catch (err) {
  656. _didIteratorError10 = true;
  657. _iteratorError10 = err;
  658. } finally {
  659. try {
  660. if (!_iteratorNormalCompletion10 && _iterator10.return) {
  661. _iterator10.return();
  662. }
  663. } finally {
  664. if (_didIteratorError10) {
  665. throw _iteratorError10;
  666. }
  667. }
  668. }
  669. return result;
  670. }();
  671. });
  672. }
  673. };
  674. // This is a simple port of microevent.js to Coffeescript. I've changed the
  675. // function names to be consistent with node.js EventEmitter.
  676. //
  677. // microevent.js is copyright Jerome Etienne, and licensed under the MIT license:
  678. // https://github.com/jeromeetienne/microevent.js
  679. var nextTick = WEB != null ? function (fn) {
  680. return setTimeout(fn, 0);
  681. } : process['nextTick'];
  682. var MicroEvent = function () {
  683. function MicroEvent() {
  684. _classCallCheck(this, MicroEvent);
  685. }
  686. _createClass(MicroEvent, [{
  687. key: 'on',
  688. value: function on(event, fct) {
  689. if (!this._events) {
  690. this._events = {};
  691. }
  692. if (!this._events[event]) {
  693. this._events[event] = [];
  694. }
  695. this._events[event].push(fct);
  696. return this;
  697. }
  698. }, {
  699. key: 'removeListener',
  700. value: function removeListener(event, fct) {
  701. var _this2 = this;
  702. if (!this._events) {
  703. this._events = {};
  704. }
  705. var listeners = this._events[event] || (this._events[event] = []);
  706. // Sadly, there's no IE8- support for indexOf.
  707. var i = 0;
  708. while (i < listeners.length) {
  709. if (listeners[i] === fct) {
  710. listeners[i] = undefined;
  711. }
  712. i++;
  713. }
  714. nextTick(function () {
  715. return _this2._events[event] = Array.from(_this2._events[event]).filter(function (x) {
  716. return x;
  717. });
  718. });
  719. return this;
  720. }
  721. }, {
  722. key: 'emit',
  723. value: function emit(event) {
  724. if (!(this._events != null ? this._events[event] : undefined)) {
  725. return this;
  726. }
  727. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  728. args[_key - 1] = arguments[_key];
  729. }
  730. var _iteratorNormalCompletion11 = true;
  731. var _didIteratorError11 = false;
  732. var _iteratorError11 = undefined;
  733. try {
  734. for (var _iterator11 = Array.from(this._events[event])[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
  735. var fn = _step11.value;
  736. if (fn) {
  737. fn.apply(this, args);
  738. }
  739. }
  740. } catch (err) {
  741. _didIteratorError11 = true;
  742. _iteratorError11 = err;
  743. } finally {
  744. try {
  745. if (!_iteratorNormalCompletion11 && _iterator11.return) {
  746. _iterator11.return();
  747. }
  748. } finally {
  749. if (_didIteratorError11) {
  750. throw _iteratorError11;
  751. }
  752. }
  753. }
  754. return this;
  755. }
  756. }]);
  757. return MicroEvent;
  758. }();
  759. // mixin will delegate all MicroEvent.js function in the destination object
  760. MicroEvent.mixin = function (obj) {
  761. var proto = obj.prototype || obj;
  762. // Damn closure compiler :/
  763. proto.on = MicroEvent.prototype.on;
  764. proto.removeListener = MicroEvent.prototype.removeListener;
  765. proto.emit = MicroEvent.prototype.emit;
  766. return obj;
  767. };
  768. if (WEB == null) {
  769. module.exports = MicroEvent;
  770. }
  771. if (WEB == null) {
  772. types = require('../types');
  773. }
  774. if (WEB != null) {
  775. exports.extendDoc = function (name, fn) {
  776. return Doc.prototype[name] = fn;
  777. };
  778. }
  779. // A Doc is a client's view on a sharejs document.
  780. //
  781. // Documents are created by calling Connection.open().
  782. //
  783. // Documents are event emitters - use doc.on(eventname, fn) to subscribe.
  784. //
  785. // Documents get mixed in with their type's API methods. So, you can .insert('foo', 0) into
  786. // a text document and stuff like that.
  787. //
  788. // Events:
  789. // - remoteop (op)
  790. // - changed (op)
  791. // - acknowledge (op)
  792. // - error
  793. // - open, closing, closed. 'closing' is not guaranteed to fire before closed.
  794. var Doc = function () {
  795. // connection is a Connection object.
  796. // name is the documents' docName.
  797. // data can optionally contain known document data, and initial open() call arguments:
  798. // {v[erson], snapshot={...}, type, create=true/false/undefined}
  799. // callback will be called once the document is first opened.
  800. function Doc(connection, name, openData) {
  801. _classCallCheck(this, Doc);
  802. // Any of these can be null / undefined at this stage.
  803. this.flush = this.flush.bind(this);
  804. this.setFlushDelay = this.setFlushDelay.bind(this);
  805. this.shout = this.shout.bind(this);
  806. this.connection = connection;
  807. this.name = name;
  808. if (!openData) {
  809. openData = {};
  810. }
  811. this.version = openData.v;
  812. this.snapshot = openData.snaphot;
  813. if (openData.type) {
  814. this._setType(openData.type);
  815. }
  816. this.state = 'closed';
  817. this.autoOpen = false;
  818. // Has the document already been created?
  819. this._create = openData.create;
  820. // The op that is currently roundtripping to the server, or null.
  821. //
  822. // When the connection reconnects, the inflight op is resubmitted.
  823. this.inflightOp = null;
  824. this.inflightCallbacks = [];
  825. // The auth ids which the client has previously used to attempt to send inflightOp. This is
  826. // usually empty.
  827. this.inflightSubmittedIds = [];
  828. // All ops that are waiting for the server to acknowledge @inflightOp
  829. this.pendingOp = null;
  830. this.pendingCallbacks = [];
  831. // Some recent ops, incase submitOp is called with an old op version number.
  832. this.serverOps = {};
  833. }
  834. // Transform a server op by a client op, and vice versa.
  835. _createClass(Doc, [{
  836. key: '_xf',
  837. value: function _xf(client, server) {
  838. if (this.type.transformX) {
  839. return this.type.transformX(client, server);
  840. } else {
  841. var client_ = this.type.transform(client, server, 'left');
  842. var server_ = this.type.transform(server, client, 'right');
  843. return [client_, server_];
  844. }
  845. }
  846. }, {
  847. key: '_otApply',
  848. value: function _otApply(docOp, isRemote, msg) {
  849. var oldSnapshot = this.snapshot;
  850. this.snapshot = this.type.apply(this.snapshot, docOp);
  851. // Its important that these event handlers are called with oldSnapshot.
  852. // The reason is that the OT type APIs might need to access the snapshots to
  853. // determine information about the received op.
  854. this.emit('change', docOp, oldSnapshot, msg);
  855. if (isRemote) {
  856. return this.emit('remoteop', docOp, oldSnapshot, msg);
  857. }
  858. }
  859. }, {
  860. key: '_connectionStateChanged',
  861. value: function _connectionStateChanged(state, data) {
  862. switch (state) {
  863. case 'disconnected':
  864. this.state = 'closed';
  865. // This is used by the server to make sure that when an op is resubmitted it
  866. // doesn't end up getting applied twice.
  867. if (this.inflightOp) {
  868. this.inflightSubmittedIds.push(this.connection.id);
  869. }
  870. this.emit('closed');
  871. break;
  872. case 'ok':
  873. // Might be able to do this when we're connecting... that would save a roundtrip.
  874. if (this.autoOpen) {
  875. this.open();
  876. }
  877. break;
  878. case 'stopped':
  879. if (typeof this._openCallback === 'function') {
  880. this._openCallback(data);
  881. }
  882. break;
  883. }
  884. return this.emit(state, data);
  885. }
  886. }, {
  887. key: '_setType',
  888. value: function _setType(type) {
  889. if (typeof type === 'string') {
  890. type = types[type];
  891. }
  892. if (!type || !type.compose) {
  893. throw new Error('Support for types without compose() is not implemented');
  894. }
  895. this.type = type;
  896. if (type.api) {
  897. for (var k in type.api) {
  898. var v = type.api[k];this[k] = v;
  899. }
  900. return typeof this._register === 'function' ? this._register() : undefined;
  901. } else {
  902. return this.provides = {};
  903. }
  904. }
  905. }, {
  906. key: '_onMessage',
  907. value: function _onMessage(msg) {
  908. // console.warn 's->c', msg
  909. if (msg.open === true) {
  910. // The document has been successfully opened.
  911. this.state = 'open';
  912. this._create = false; // Don't try and create the document again next time open() is called.
  913. if (this.created == null) {
  914. this.created = !!msg.create;
  915. }
  916. if (msg.type) {
  917. this._setType(msg.type);
  918. }
  919. if (msg.create) {
  920. this.created = true;
  921. this.snapshot = this.type.create();
  922. } else {
  923. if (this.created !== true) {
  924. this.created = false;
  925. }
  926. if (msg.snapshot !== undefined) {
  927. this.snapshot = msg.snapshot;
  928. }
  929. }
  930. if (msg.v != null) {
  931. this.version = msg.v;
  932. }
  933. // Resend any previously queued operation.
  934. if (this.inflightOp) {
  935. var response = {
  936. doc: this.name,
  937. op: this.inflightOp,
  938. v: this.version
  939. };
  940. if (this.inflightSubmittedIds.length) {
  941. response.dupIfSource = this.inflightSubmittedIds;
  942. }
  943. this.connection.send(response);
  944. } else {
  945. this.flush();
  946. }
  947. this.emit('open');
  948. return typeof this._openCallback === 'function' ? this._openCallback(null) : undefined;
  949. } else if (msg.open === false) {
  950. // The document has either been closed, or an open request has failed.
  951. if (msg.error) {
  952. // An error occurred opening the document.
  953. if (typeof console !== 'undefined' && console !== null) {
  954. console.error('Could not open document: ' + msg.error);
  955. }
  956. this.emit('error', msg.error);
  957. if (typeof this._openCallback === 'function') {
  958. this._openCallback(msg.error);
  959. }
  960. }
  961. this.state = 'closed';
  962. this.emit('closed');
  963. if (typeof this._closeCallback === 'function') {
  964. this._closeCallback();
  965. }
  966. return this._closeCallback = null;
  967. } else if (msg.op === null && error === 'Op already submitted') {
  968. // We've tried to resend an op to the server, which has already been received successfully. Do nothing.
  969. // The op will be confirmed normally when we get the op itself was echoed back from the server
  970. // (handled below).
  971. } else if (msg.op === undefined && msg.v !== undefined || msg.op && Array.from(this.inflightSubmittedIds).includes(msg.meta.source)) {
  972. // Our inflight op has been acknowledged.
  973. var callback = void 0;
  974. var oldInflightOp = this.inflightOp;
  975. this.inflightOp = null;
  976. this.inflightSubmittedIds.length = 0;
  977. if (this.pendingOp === null) {
  978. // All ops are acked
  979. this.emit('saved');
  980. }
  981. var error = msg.error;
  982. if (error) {
  983. // The server has rejected an op from the client for some reason.
  984. // We'll send the error message to the user and roll back the change.
  985. //
  986. // If the server isn't going to allow edits anyway, we should probably
  987. // figure out some way to flag that (readonly:true in the open request?)
  988. if (this.type.invert) {
  989. var undo = this.type.invert(oldInflightOp);
  990. // Now we have to transform the undo operation by any server ops & pending ops
  991. if (this.pendingOp) {
  992. var _Array$from7 = Array.from(this._xf(this.pendingOp, undo));
  993. var _Array$from8 = _slicedToArray(_Array$from7, 2);
  994. this.pendingOp = _Array$from8[0];
  995. undo = _Array$from8[1];
  996. }
  997. // ... and apply it locally, reverting the changes.
  998. //
  999. // This call will also call @emit 'remoteop'. I'm still not 100% sure about this
  1000. // functionality, because its really a local op. Basically, the problem is that
  1001. // if the client's op is rejected by the server, the editor window should update
  1002. // to reflect the undo.
  1003. this._otApply(undo, true, msg);
  1004. } else {
  1005. this.emit('error', 'Op apply failed (' + error + ') and the op could not be reverted');
  1006. }
  1007. var _iteratorNormalCompletion12 = true;
  1008. var _didIteratorError12 = false;
  1009. var _iteratorError12 = undefined;
  1010. try {
  1011. for (var _iterator12 = Array.from(this.inflightCallbacks)[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
  1012. callback = _step12.value;
  1013. callback(error);
  1014. }
  1015. } catch (err) {
  1016. _didIteratorError12 = true;
  1017. _iteratorError12 = err;
  1018. } finally {
  1019. try {
  1020. if (!_iteratorNormalCompletion12 && _iterator12.return) {
  1021. _iterator12.return();
  1022. }
  1023. } finally {
  1024. if (_didIteratorError12) {
  1025. throw _iteratorError12;
  1026. }
  1027. }
  1028. }
  1029. } else {
  1030. // The op applied successfully.
  1031. // We may get multiple acks of the same message if we retried it,
  1032. // so its ok if we receive an ack for a version that we've already gone past.
  1033. // If so, just ignore it
  1034. if (msg.v < this.version) {
  1035. return;
  1036. }
  1037. if (msg.v !== this.version) {
  1038. throw new Error('Invalid version from server');
  1039. }
  1040. this.serverOps[this.version] = oldInflightOp;
  1041. this.version++;
  1042. this.emit('acknowledge', oldInflightOp);
  1043. var _iteratorNormalCompletion13 = true;
  1044. var _didIteratorError13 = false;
  1045. var _iteratorError13 = undefined;
  1046. try {
  1047. for (var _iterator13 = Array.from(this.inflightCallbacks)[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
  1048. callback = _step13.value;
  1049. callback(null, oldInflightOp);
  1050. }
  1051. } catch (err) {
  1052. _didIteratorError13 = true;
  1053. _iteratorError13 = err;
  1054. } finally {
  1055. try {
  1056. if (!_iteratorNormalCompletion13 && _iterator13.return) {
  1057. _iterator13.return();
  1058. }
  1059. } finally {
  1060. if (_didIteratorError13) {
  1061. throw _iteratorError13;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. // Send the next op.
  1067. return this.delayedFlush();
  1068. } else if (msg.op) {
  1069. // We got a new op from the server.
  1070. // msg is {doc:, op:, v:}
  1071. // There is a bug in socket.io (produced on firefox 3.6) which causes messages
  1072. // to be duplicated sometimes.
  1073. // We'll just silently drop subsequent messages.
  1074. if (msg.v < this.version) {
  1075. return;
  1076. }
  1077. if (msg.doc !== this.name) {
  1078. return this.emit('error', 'Expected docName \'' + this.name + '\' but got ' + msg.doc);
  1079. }
  1080. if (msg.v !== this.version) {
  1081. return this.emit('error', 'Expected version ' + this.version + ' but got ' + msg.v);
  1082. }
  1083. // p "if: #{i @inflightOp} pending: #{i @pendingOp} doc '#{@snapshot}' op: #{i msg.op}"
  1084. var op = msg.op;
  1085. this.serverOps[this.version] = op;
  1086. var docOp = op;
  1087. if (this.inflightOp !== null) {
  1088. var _Array$from9 = Array.from(this._xf(this.inflightOp, docOp));
  1089. var _Array$from10 = _slicedToArray(_Array$from9, 2);
  1090. this.inflightOp = _Array$from10[0];
  1091. docOp = _Array$from10[1];
  1092. }
  1093. if (this.pendingOp !== null) {
  1094. var _Array$from11 = Array.from(this._xf(this.pendingOp, docOp));
  1095. var _Array$from12 = _slicedToArray(_Array$from11, 2);
  1096. this.pendingOp = _Array$from12[0];
  1097. docOp = _Array$from12[1];
  1098. }
  1099. this.version++;
  1100. // Finally, apply the op to @snapshot and trigger any event listeners
  1101. return this._otApply(docOp, true, msg);
  1102. } else if (msg.meta) {
  1103. var _msg$meta = msg.meta,
  1104. path = _msg$meta.path,
  1105. value = _msg$meta.value;
  1106. switch (path != null ? path[0] : undefined) {
  1107. case 'shout':
  1108. return this.emit('shout', value);
  1109. default:
  1110. return typeof console !== 'undefined' && console !== null ? console.warn('Unhandled meta op:', msg) : undefined;
  1111. }
  1112. } else {
  1113. return typeof console !== 'undefined' && console !== null ? console.warn('Unhandled document message:', msg) : undefined;
  1114. }
  1115. }
  1116. // Send ops to the server, if appropriate.
  1117. //
  1118. // Only one op can be in-flight at a time, so if an op is already on its way then
  1119. // this method does nothing.
  1120. }, {
  1121. key: 'flush',
  1122. value: function flush() {
  1123. this.flushTimeout = null;
  1124. // console.log "CALLED FLUSH"
  1125. if (this.connection.state !== 'ok' || this.inflightOp !== null || this.pendingOp === null) {
  1126. return;
  1127. }
  1128. // Rotate null -> pending -> inflight
  1129. this.inflightOp = this.pendingOp;
  1130. this.inflightCallbacks = this.pendingCallbacks;
  1131. this.pendingOp = null;
  1132. this.pendingCallbacks = [];
  1133. this.emit('flipped_pending_to_inflight');
  1134. if (window.useShareJsHash || window.sl_debugging) {
  1135. var now = Date.now()
  1136. var age = this.__lastSubmitTimestamp && (now - this.__lastSubmitTimestamp)
  1137. var RECOMPUTE_HASH_INTERVAL = 5000
  1138. // check the document hash regularly (but not if we have checked in the last 5 seconds)
  1139. var needToRecomputeHash = !this.__lastSubmitTimestamp || (age > RECOMPUTE_HASH_INTERVAL) || (age < 0)
  1140. if (needToRecomputeHash || window.sl_debugging) {
  1141. // send git hash of current snapshot
  1142. var sha1 = CryptoJS.SHA1("blob " + this.snapshot.length + "\x00" + this.snapshot).toString()
  1143. this.__lastSubmitTimestamp = now;
  1144. }
  1145. }
  1146. // console.log "SENDING OP TO SERVER", @inflightOp, @version
  1147. var lastVersion = this.__lastVersion;
  1148. this.__lastVersion = this.version;
  1149. return this.connection.send({ doc: this.name, op: this.inflightOp, v: this.version, lastV: lastVersion, hash: sha1});
  1150. }
  1151. // Submit an op to the server. The op maybe held for a little while before being sent, as only one
  1152. // op can be inflight at any time.
  1153. }, {
  1154. key: 'submitOp',
  1155. value: function submitOp(op, callback) {
  1156. if (this.type.normalize != null) {
  1157. op = this.type.normalize(op);
  1158. }
  1159. var oldSnapshot = this.snapshot;
  1160. // If this throws an exception, no changes should have been made to the doc
  1161. this.snapshot = this.type.apply(this.snapshot, op);
  1162. if (this.pendingOp !== null) {
  1163. this.pendingOp = this.type.compose(this.pendingOp, op);
  1164. } else {
  1165. this.pendingOp = op;
  1166. }
  1167. if (callback) {
  1168. this.pendingCallbacks.push(callback);
  1169. }
  1170. this.emit('change', op, oldSnapshot);
  1171. return this.delayedFlush();
  1172. }
  1173. }, {
  1174. key: 'delayedFlush',
  1175. value: function delayedFlush() {
  1176. if (this.flushTimeout == null) {
  1177. return this.flushTimeout = setTimeout(this.flush, this._flushDelay || 0);
  1178. }
  1179. }
  1180. }, {
  1181. key: 'setFlushDelay',
  1182. value: function setFlushDelay(delay) {
  1183. return this._flushDelay = delay;
  1184. }
  1185. }, {
  1186. key: 'shout',
  1187. value: function shout(msg) {
  1188. // Meta ops don't have to queue, they can go direct. Good/bad idea?
  1189. return this.connection.send({ doc: this.name, meta: { path: ['shout'], value: msg } });
  1190. }
  1191. // Open a document. The document starts closed.
  1192. }, {
  1193. key: 'open',
  1194. value: function open(callback) {
  1195. var _this3 = this;
  1196. this.autoOpen = true;
  1197. if (this.state !== 'closed') {
  1198. return;
  1199. }
  1200. var message = {
  1201. doc: this.name,
  1202. open: true
  1203. };
  1204. if (this.snapshot === undefined) {
  1205. message.snapshot = null;
  1206. }
  1207. if (this.type) {
  1208. message.type = this.type.name;
  1209. }
  1210. if (this.version != null) {
  1211. message.v = this.version;
  1212. }
  1213. if (this._create) {
  1214. message.create = true;
  1215. }
  1216. this.connection.send(message);
  1217. this.state = 'opening';
  1218. return this._openCallback = function (error) {
  1219. _this3._openCallback = null;
  1220. return typeof callback === 'function' ? callback(error) : undefined;
  1221. };
  1222. }
  1223. // Close a document.
  1224. }, {
  1225. key: 'close',
  1226. value: function close(callback) {
  1227. this.autoOpen = false;
  1228. if (this.state === 'closed') {
  1229. return typeof callback === 'function' ? callback() : undefined;
  1230. }
  1231. this.connection.send({ doc: this.name, open: false });
  1232. // Should this happen immediately or when we get open:false back from the server?
  1233. this.state = 'closed';
  1234. this.emit('closing');
  1235. return this._closeCallback = callback;
  1236. }
  1237. }]);
  1238. return Doc;
  1239. }();
  1240. // Make documents event emitters
  1241. if (WEB == null) {
  1242. MicroEvent = require('./microevent');
  1243. }
  1244. MicroEvent.mixin(Doc);
  1245. exports.Doc = Doc;
  1246. // This is some utility code to connect an ace editor to a sharejs document.
  1247. var _ace$require = ace.require('ace/range'),
  1248. Range = _ace$require.Range;
  1249. // Convert an ace delta into an op understood by share.js
  1250. var applyAceToShareJS = function applyAceToShareJS(editorDoc, delta, doc, fromUndo) {
  1251. // Get the start position of the range, in no. of characters
  1252. var getStartOffsetPosition = function getStartOffsetPosition(start) {
  1253. // This is quite inefficient - getLines makes a copy of the entire
  1254. // lines array in the document. It would be nice if we could just
  1255. // access them directly.
  1256. var lines = editorDoc.getLines(0, start.row);
  1257. var offset = 0;
  1258. for (var i = 0; i < lines.length; i++) {
  1259. var line = lines[i];
  1260. offset += i < start.row ? line.length : start.column;
  1261. }
  1262. // Add the row number to include newlines.
  1263. return offset + start.row;
  1264. };
  1265. var pos = getStartOffsetPosition(delta.start);
  1266. switch (delta.action) {
  1267. case 'insert':
  1268. text = delta.lines.join('\n');
  1269. doc.insert(pos, text, fromUndo);
  1270. break;
  1271. case 'remove':
  1272. text = delta.lines.join('\n');
  1273. doc.del(pos, text.length, fromUndo);
  1274. break;
  1275. default:
  1276. throw new Error('unknown action: ' + delta.action);
  1277. }
  1278. };
  1279. // Attach an ace editor to the document. The editor's contents are replaced
  1280. // with the document's contents unless keepEditorContents is true. (In which case the document's
  1281. // contents are nuked and replaced with the editor's).
  1282. window.sharejs.extendDoc('attach_ace', function (editor, keepEditorContents, maxDocLength) {
  1283. if (!this.provides['text']) {
  1284. throw new Error('Only text documents can be attached to ace');
  1285. }
  1286. var doc = this;
  1287. var editorDoc = editor.getSession().getDocument();
  1288. editorDoc.setNewLineMode('unix');
  1289. var check = function check() {
  1290. return window.setTimeout(function () {
  1291. var editorText = editorDoc.getValue();
  1292. var otText = doc.getText();
  1293. if (editorText !== otText) {
  1294. doc.emit('error','Text does not match in ace')
  1295. console.error('Text does not match!');
  1296. console.error('editor: ' + editorText);
  1297. return console.error('ot: ' + otText);
  1298. }
  1299. }
  1300. // Should probably also replace the editor text with the doc snapshot.
  1301. , 0);
  1302. };
  1303. if (keepEditorContents) {
  1304. doc.del(0, doc.getText().length);
  1305. doc.insert(0, editorDoc.getValue());
  1306. } else {
  1307. editorDoc.setValue(doc.getText());
  1308. }
  1309. check();
  1310. // When we apply ops from sharejs, ace emits edit events. We need to ignore those
  1311. // to prevent an infinite typing loop.
  1312. var suppress = false;
  1313. // Listen for edits in ace
  1314. var editorListener = function editorListener(change) {
  1315. if (suppress) {
  1316. return;
  1317. }
  1318. if (maxDocLength != null && editorDoc.getValue().length > maxDocLength) {
  1319. doc.emit('error', new Error('document length is greater than maxDocLength'));
  1320. return;
  1321. }
  1322. var fromUndo = !!(editor.getSession().$fromUndo || editor.getSession().$fromReject);
  1323. applyAceToShareJS(editorDoc, change, doc, fromUndo);
  1324. return check();
  1325. };
  1326. editorDoc.on('change', editorListener);
  1327. // Horribly inefficient.
  1328. var offsetToPos = function offsetToPos(offset) {
  1329. // Again, very inefficient.
  1330. var lines = editorDoc.getAllLines();
  1331. var row = 0;
  1332. for (row = 0; row < lines.length; row++) {
  1333. var line = lines[row];
  1334. if (offset <= line.length) {
  1335. break;
  1336. }
  1337. // +1 for the newline.
  1338. offset -= lines[row].length + 1;
  1339. }
  1340. return { row: row, column: offset };
  1341. };
  1342. // We want to insert a remote:true into the delta if the op comes from the
  1343. // underlying sharejs doc (which means it is from a remote op), so we have to do
  1344. // the work of editorDoc.insert and editorDoc.remove manually. These methods are
  1345. // copied from ace.js doc#insert and #remove, and then inject the remote:true
  1346. // flag into the delta.
  1347. var onInsert = function onInsert(pos, text) {
  1348. if (editorDoc.getLength() <= 1) {
  1349. editorDoc.$detectNewLine(text);
  1350. }
  1351. var lines = editorDoc.$split(text);
  1352. var position = offsetToPos(pos);
  1353. var start = editorDoc.clippedPos(position.row, position.column);
  1354. var end = {
  1355. row: start.row + lines.length - 1,
  1356. column: (lines.length === 1 ? start.column : 0) + lines[lines.length - 1].length
  1357. };
  1358. suppress = true;
  1359. editorDoc.applyDelta({
  1360. start: start,
  1361. end: end,
  1362. action: 'insert',
  1363. lines: lines,
  1364. remote: true
  1365. });
  1366. suppress = false;
  1367. return check();
  1368. };
  1369. var onDelete = function onDelete(pos, text) {
  1370. var range = Range.fromPoints(offsetToPos(pos), offsetToPos(pos + text.length));
  1371. var start = editorDoc.clippedPos(range.start.row, range.start.column);
  1372. var end = editorDoc.clippedPos(range.end.row, range.end.column);
  1373. suppress = true;
  1374. editorDoc.applyDelta({
  1375. start: start,
  1376. end: end,
  1377. action: 'remove',
  1378. lines: editorDoc.getLinesForRange({ start: start, end: end }),
  1379. remote: true
  1380. });
  1381. suppress = false;
  1382. return check();
  1383. };
  1384. doc.on('insert', onInsert);
  1385. doc.on('delete', onDelete);
  1386. doc.detach_ace = function () {
  1387. doc.removeListener('insert', onInsert);
  1388. doc.removeListener('delete', onDelete);
  1389. editorDoc.removeListener('change', editorListener);
  1390. return delete doc.detach_ace;
  1391. };
  1392. });
  1393. // This is some utility code to connect a CodeMirror editor
  1394. // to a sharejs document.
  1395. // It is heavily inspired from the Ace editor hook.
  1396. // Convert a CodeMirror delta into an op understood by share.js
  1397. var applyCMToShareJS = function applyCMToShareJS(editorDoc, delta, doc) {
  1398. // CodeMirror deltas give a text replacement.
  1399. // I tuned this operation a little bit, for speed.
  1400. var startPos = 0; // Get character position from # of chars in each line.
  1401. var i = 0; // i goes through all lines.
  1402. // Compute the position from the shareJS snapshot because we are in the CodeMirror
  1403. // change event, where the change has already been applied to the editorDoc
  1404. var docLines = doc.snapshot.split('\n', delta.from.line) // only split the document as far as we need to
  1405. while (i < delta.from.line) {
  1406. startPos += docLines[i].length + 1; // Add 1 for '\n'
  1407. i++;
  1408. }
  1409. startPos += delta.from.ch;
  1410. if (delta.removed) {
  1411. doc.del(startPos, delta.removed.join('\n').length);
  1412. }
  1413. if (delta.text) {
  1414. return doc.insert(startPos, delta.text.join('\n'));
  1415. }
  1416. };
  1417. // Attach a CodeMirror editor to the document. The editor's contents are replaced
  1418. // with the document's contents unless keepEditorContents is true. (In which case
  1419. // the document's contents are nuked and replaced with the editor's).
  1420. window.sharejs.extendDoc('attach_cm', function (editor, keepEditorContents) {
  1421. if (!this.provides.text) {
  1422. throw new Error('Only text documents can be attached to CodeMirror2');
  1423. }
  1424. var sharedoc = this;
  1425. var editorDoc = editor.getDoc();
  1426. var check = function check() {
  1427. return window.setTimeout(function () {
  1428. var editorText = editor.getValue();
  1429. var otText = sharedoc.getText();
  1430. if (editorText !== otText) {
  1431. sharedoc.emit('error','Text does not match in CodeMirror')
  1432. console.error('Text does not match!');
  1433. console.error('editor: ' + editorText);
  1434. return console.error('ot: ' + otText);
  1435. }
  1436. }
  1437. // Removed editor.setValue here as it would cause recursive loops if
  1438. // consistency check failed - because setting the value would trigger
  1439. // the change event
  1440. , 0);
  1441. };
  1442. if (keepEditorContents) {
  1443. this.del(0, sharedoc.getText().length);
  1444. this.insert(0, editor.getValue());
  1445. } else {
  1446. editor.setValue(sharedoc.getText());
  1447. }
  1448. check();
  1449. // When we apply ops from sharejs, CodeMirror emits edit events.
  1450. // We need to ignore those to prevent an infinite typing loop.
  1451. var suppress = false;
  1452. // Listen for edits in CodeMirror.
  1453. var editorListener = function editorListener(ed, change) {
  1454. if (suppress) {
  1455. return;
  1456. }
  1457. applyCMToShareJS(editorDoc, change, sharedoc);
  1458. return check();
  1459. };
  1460. editorDoc.on('change', editorListener);
  1461. var onInsert = function onInsert(pos, text) {
  1462. suppress = true;
  1463. // All the primitives we need are already in CM's API.
  1464. editor.replaceRange(text, editor.posFromIndex(pos));
  1465. // Clear CM's undo/redo history on remote edit. This prevents issues where
  1466. // a user can accidentally remove another user's edits
  1467. editor.clearHistory();
  1468. suppress = false;
  1469. return check();
  1470. };
  1471. var onDelete = function onDelete(pos, text) {
  1472. suppress = true;
  1473. var from = editor.posFromIndex(pos);
  1474. var to = editor.posFromIndex(pos + text.length);
  1475. editor.replaceRange('', from, to);
  1476. // Clear CM's undo/redo history on remote edit. This prevents issues where
  1477. // a user can accidentally remove another user's edits
  1478. editor.clearHistory()
  1479. suppress = false;
  1480. return check();
  1481. };
  1482. this.on('insert', onInsert);
  1483. this.on('delete', onDelete);
  1484. this.detach_cm = function () {
  1485. this.removeListener('insert', onInsert);
  1486. this.removeListener('delete', onDelete);
  1487. editorDoc.off('change', editorListener);
  1488. return delete this.detach_cm;
  1489. };
  1490. });
  1491. return window.sharejs;
  1492. });