You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

6 years ago
6 years ago
  1. define([
  2. '/api/config?cb=' + Math.random().toString(16).substring(2),
  3. '/pad/realtime-wysiwyg.js',
  4. '/common/messages.js',
  5. '/bower_components/jquery/dist/jquery.min.js',
  6. '/bower_components/ckeditor/ckeditor.js',
  7. '/bower_components/tweetnacl/nacl-fast.min.js',
  8. ], function (Config, RTWysiwyg, Messages) {
  9. var Ckeditor = window.CKEDITOR;
  10. var Nacl = window.nacl;
  11. var $ = jQuery;
  12. var module = { exports: {} };
  13. var parseKey = function (str) {
  14. var array = Nacl.util.decodeBase64(str);
  15. var hash = Nacl.hash(array);
  16. return { lookupKey: hash.subarray(32), cryptKey: hash.subarray(0,32) };
  17. };
  18. var genKey = function () {
  19. return Nacl.util.encodeBase64(Nacl.randomBytes(18));
  20. };
  21. var userName = function () {
  22. return Nacl.util.encodeBase64(Nacl.randomBytes(8));
  23. };
  24. $(function () {
  25. $(window).on('hashchange', function() {
  26. window.location.reload();
  27. });
  28. if (window.location.href.indexOf('#') === -1) {
  29. window.location.href = window.location.href + '#' + genKey();
  30. return;
  31. }
  32. var key = parseKey(window.location.hash.substring(1));
  33. var editor = Ckeditor.replace('editor1', {
  34. removeButtons: 'Source,Maximize',
  35. // This plugin inserts html crap into the document which is not part of the document
  36. // itself and causes problems when it's sent across the wire and reflected back.
  37. removePlugins: 'magicline'
  38. });
  39. editor.on('instanceReady', function () {
  40. editor.execCommand('maximize');
  41. var ifr = window.ifr = $('iframe')[0];
  42. ifr.contentDocument.body.innerHTML = Messages.initialState;
  43. var rtw =
  44. RTWysiwyg.start(Config.websocketURL,
  45. userName(),
  46. Nacl.util.encodeBase64(key.lookupKey).substring(0,10),
  47. key.cryptKey);
  48. editor.on('change', function () { rtw.onEvent(); });
  49. });
  50. });
  51. });