textcircle.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This collection stores all the documents.
  2. // Note: not Documents, but this.Documents, because of the editing package (?)
  3. this.Documents = new Mongo.Collection("documents");
  4. // This collection stores sets of users that are editing documents.
  5. EditingUsers = new Mongo.Collection("editingUsers");
  6. if (Meteor.isClient) {
  7. Template.editor.helpers({
  8. // Return the id of the first document you can find.
  9. docid: function () {
  10. const doc = Documents.findOne();
  11. return doc ? doc._id : undefined;
  12. },
  13. // Configure the CodeMirror editor.
  14. config: function () {
  15. return function (editor) {
  16. editor.setOption("lineNumbers", true);
  17. editor.setOption("mode", "html");
  18. // Set a callback that gets triggered whenever the user
  19. // makes a change in the code editing window.
  20. editor.on("change", function (cmEditor /* , info */) {
  21. let $preview = $("#viewer_iframe");
  22. // Send the current code over to the iframe for rendering.
  23. $preview.contents().find("html").html(cmEditor.getValue());
  24. Meteor.call("addEditingUser");
  25. });
  26. };
  27. }
  28. });
  29. Template.editingUsers.helpers({
  30. // Retrieve a set of users that are editing this document.
  31. users: function () {
  32. let doc = Documents.findOne();
  33. if (!doc) {
  34. // Give up.
  35. return null;
  36. }
  37. let eUsers = EditingUsers.findOne({ docId: doc._id });
  38. if (!eUsers) {
  39. // Give up.
  40. return null;
  41. }
  42. let users = new Array();
  43. let i = 0;
  44. for (let userId in eUsers.users) {
  45. users[i] = fixObjectsKeys(eUsers.users[userId]);
  46. users[i]._id = userId;
  47. i++;
  48. }
  49. return users;
  50. }
  51. });
  52. } // end isClient...
  53. if (Meteor.isServer) {
  54. Meteor.startup(function () {
  55. // Insert a document if there isn't one already.
  56. if (!Documents.findOne()) {
  57. Documents.insert({ title: "my new document" });
  58. }
  59. });
  60. }
  61. // Methods that provide write access to the data.
  62. Meteor.methods({
  63. addEditingUser: function () {
  64. let doc = Documents.findOne();
  65. if (!doc) {
  66. // No doc: give up.
  67. return;
  68. }
  69. if (!this.userId) {
  70. // No logged-in user: give up.
  71. return;
  72. }
  73. // Now I have a doc and possibly a user.
  74. let user = Meteor.user().profile;
  75. let eUsers = EditingUsers.findOne({ docId: doc._id });
  76. // No editing users have been stored yet.
  77. if (!eUsers) {
  78. eUsers = {
  79. docId: doc._id,
  80. users: {}
  81. };
  82. }
  83. user.lastEdit = new Date();
  84. eUsers.users[this.userId] = user;
  85. // Upsert: insert or update if filter matches.
  86. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  87. }
  88. });
  89. // This renames object keys by removing hyphens to make them compatible
  90. // with spacebars.
  91. function fixObjectsKeys(obj) {
  92. let newObj = {};
  93. for (let key in obj) {
  94. if (obj.hasOwnProperty(key)) {
  95. const key2 = key.replace("-", "");
  96. newObj[key2] = obj[key];
  97. }
  98. }
  99. return newObj;
  100. }