textcircle.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. EditingUsers = new Mongo.Collection("editingUsers");
  5. if (Meteor.isClient) {
  6. Template.editor.helpers({
  7. // Return the id of the first document you can find.
  8. docid: function () {
  9. const doc = Documents.findOne();
  10. return doc ? doc._id : undefined;
  11. },
  12. // Configure the CodeMirror editor.
  13. config: function () {
  14. return function (editor) {
  15. editor.setOption("lineNumbers", true);
  16. editor.setOption("mode", "html");
  17. editor.on("change", function (cmEditor, info) {
  18. let $preview = $("#viewer_iframe");
  19. $preview.contents().find("html").html(cmEditor.getValue());
  20. Meteor.call("addEditingUser");
  21. });
  22. };
  23. },
  24. });
  25. }
  26. if (Meteor.isServer) {
  27. Meteor.startup(function () {
  28. // Insert a document if there isn't one already.
  29. if (!Documents.findOne()) {
  30. Documents.insert({ title: "my new document" });
  31. }
  32. });
  33. }
  34. Meteor.methods({
  35. addEditingUser: function () {
  36. var doc, user, eUsers;
  37. doc = Documents.findOne();
  38. if (!doc) {
  39. // No doc: give up.
  40. return;
  41. }
  42. if (!this.userId) {
  43. // No logged-in user: give up.
  44. return;
  45. }
  46. // Now I have a doc and possibly a user.
  47. user = Meteor.user().profile;
  48. eUsers = EditingUsers.findOne({ docId: doc._id });
  49. // No editing users have been stored yet.
  50. if (!eUsers) {
  51. eUsers = {
  52. docId: doc._id,
  53. users: {}
  54. };
  55. }
  56. user.lastEdit = new Date();
  57. eUsers.users[this.userId] = user;
  58. EditingUsers.upsert({ _id: eUsers._id }, eUsers);
  59. }
  60. });