// This collection stores all the documents. // Note: not Documents, but this.Documents, because of the editing package (?) this.Documents = new Mongo.Collection("documents"); EditingUsers = new Mongo.Collection("editingUsers"); if (Meteor.isClient) { Template.editor.helpers({ // Return the id of the first document you can find. docid: function () { const doc = Documents.findOne(); return doc ? doc._id : undefined; }, // Configure the CodeMirror editor. config: function () { return function (editor) { editor.setOption("lineNumbers", true); editor.setOption("mode", "html"); editor.on("change", function (cmEditor, info) { let $preview = $("#viewer_iframe"); $preview.contents().find("html").html(cmEditor.getValue()); Meteor.call("addEditingUser"); }); }; }, }); } if (Meteor.isServer) { Meteor.startup(function () { // Insert a document if there isn't one already. if (!Documents.findOne()) { Documents.insert({ title: "my new document" }); } }); } Meteor.methods({ addEditingUser: function () { var doc, user, eUsers; doc = Documents.findOne(); if (!doc) { // No doc: give up. return; } if (!this.userId) { // No logged-in user: give up. return; } // Now I have a doc and possibly a user. user = Meteor.user().profile; eUsers = EditingUsers.findOne({ docId: doc._id }); // No editing users have been stored yet. if (!eUsers) { eUsers = { docId: doc._id, users: {} }; } user.lastEdit = new Date(); eUsers.users[this.userId] = user; EditingUsers.upsert({ _id: eUsers._id }, eUsers); } });