123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- // This collection stores all the documents.
- // Note: not Documents, but this.Documents, because of the editing package (?)
- this.Documents = new Mongo.Collection("documents");
- // This collection stores sets of users that are editing 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("theme", "cobalt");
- editor.setOption("mode", "html");
- // Set a callback that gets triggered whenever the user
- // makes a change in the code editing window.
- editor.on("change", function (cmEditor /* , info */) {
- let $preview = $("#viewer_iframe");
- // Send the current code over to the iframe for rendering.
- $preview.contents().find("html").html(cmEditor.getValue());
- Meteor.call("addEditingUser");
- });
- };
- }
- });
- Template.editingUsers.helpers({
- // Retrieve a set of users that are editing this document.
- users: function () {
- let doc = Documents.findOne();
- if (!doc) {
- // Give up.
- return null;
- }
- let eUsers = EditingUsers.findOne({ docId: doc._id });
- if (!eUsers) {
- // Give up.
- return null;
- }
- let users = new Array();
- let i = 0;
- for (let userId in eUsers.users) {
- users[i] = fixObjectsKeys(eUsers.users[userId]);
- users[i]._id = userId;
- i++;
- }
- return users;
- }
- });
- } // end isClient...
- if (Meteor.isServer) {
- Meteor.startup(function () {
- // Insert a document if there isn't one already.
- if (!Documents.findOne()) {
- Documents.insert({ title: "my new document" });
- }
- });
- }
- // Methods that provide write access to the data.
- Meteor.methods({
- addEditingUser: function () {
- let 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.
- let user = Meteor.user().profile;
- let 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;
- // Upsert: insert or update if filter matches.
- EditingUsers.upsert({ _id: eUsers._id }, eUsers);
- }
- });
- // This renames object keys by removing hyphens to make them compatible
- // with spacebars.
- function fixObjectsKeys(obj) {
- let newObj = {};
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {
- const key2 = key.replace("-", "");
- newObj[key2] = obj[key];
- }
- }
- return newObj;
- }
|