unsavedEdits.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. Meteor.subscribe('unsaved-edits');
  2. // `UnsavedEdits` is a global key-value store used to save drafts of user
  3. // inputs. We used to have the notion of a `cachedValue` that was local to a
  4. // component but the global store has multiple advantages:
  5. // 1. When the component is unmounted (ie, destroyed) the draft isn't lost
  6. // 2. The drafts are synced across multiple computers
  7. // 3. The drafts are synced across multiple browser tabs
  8. // XXX This currently doesn't work in purely offline mode since the sync is
  9. // handled with the DDP connection to the server. To solve this, we could use
  10. // something like GroundDB that syncs using localstorage.
  11. //
  12. // The key is a dictionary composed of two fields:
  13. // * a `fieldName` which identifies the particular field. Since this is a global
  14. // identifier a good practice would be to compose it with the collection name
  15. // and the document field, eg. `boardTitle`, `cardDescription`.
  16. // * a `docId` which identifies the appropriate document. In general we use
  17. // MongoDB `_id` field.
  18. //
  19. // The value is a string containing the draft.
  20. UnsavedEdits = {
  21. // XXX Wanted to have the collection has an instance variable, but
  22. // unfortunately the collection isn't defined yet at this point. We need ES6
  23. // modules to solve the file order issue!
  24. //
  25. // _collection: UnsavedEditCollection,
  26. get({ fieldName, docId }, defaultTo = '') {
  27. const unsavedValue = this._getCollectionDocument(fieldName, docId);
  28. if (unsavedValue) {
  29. return unsavedValue.value;
  30. } else {
  31. return defaultTo;
  32. }
  33. },
  34. has({ fieldName, docId }) {
  35. return Boolean(this.get({fieldName, docId}));
  36. },
  37. set({ fieldName, docId }, value) {
  38. const currentDoc = this._getCollectionDocument(fieldName, docId);
  39. if (currentDoc) {
  40. UnsavedEditCollection.update(currentDoc._id, { $set: { value }});
  41. } else {
  42. UnsavedEditCollection.insert({
  43. fieldName,
  44. docId,
  45. value,
  46. });
  47. }
  48. },
  49. reset({ fieldName, docId }) {
  50. const currentDoc = this._getCollectionDocument(fieldName, docId);
  51. if (currentDoc) {
  52. UnsavedEditCollection.remove(currentDoc._id);
  53. }
  54. },
  55. _getCollectionDocument(fieldName, docId) {
  56. return UnsavedEditCollection.findOne({fieldName, docId});
  57. },
  58. };
  59. Blaze.registerHelper('getUnsavedValue', (fieldName, docId, defaultTo) => {
  60. // Workaround some blaze feature that ass a list of keywords arguments as the
  61. // last parameter (even if the caller didn't specify any).
  62. if (!_.isString(defaultTo)) {
  63. defaultTo = '';
  64. }
  65. return UnsavedEdits.get({ fieldName, docId }, defaultTo);
  66. });
  67. Blaze.registerHelper('hasUnsavedValue', (fieldName, docId) => {
  68. return UnsavedEdits.has({ fieldName, docId });
  69. });