textcircle.js 3.0 KB

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