nodebb-plugin-composer-default 10.0.16 → 10.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-composer-default",
3
- "version": "10.0.16",
3
+ "version": "10.0.17",
4
4
  "description": "Default composer for NodeBB",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -2,21 +2,19 @@
2
2
 
3
3
  define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
4
4
  var drafts = {};
5
- var saveThrottleId;
6
5
 
7
6
  drafts.init = function (postContainer, postData) {
8
7
  var draftIconEl = postContainer.find('.draft-icon');
9
- function saveThrottle() {
10
- resetTimeout();
11
-
12
- saveThrottleId = setTimeout(function () {
13
- saveDraft(postContainer, draftIconEl, postData);
14
- }, 1000);
8
+ function doSaveDraft() {
9
+ // Post is modified, save to list of opened drafts
10
+ drafts.updateVisibility('available', postData.save_id, true);
11
+ drafts.updateVisibility('open', postData.save_id, true);
12
+ saveDraft(postContainer, draftIconEl, postData);
15
13
  }
16
14
 
17
- postContainer.on('keyup', 'textarea, input.handle, input.title', saveThrottle);
18
- postContainer.on('click', 'input[type="checkbox"]', saveThrottle);
19
- postContainer.on('thumb.uploaded', saveThrottle);
15
+ postContainer.on('keyup', 'textarea, input.handle, input.title', utils.debounce(doSaveDraft, 1000));
16
+ postContainer.on('click', 'input[type="checkbox"]', utils.debounce(doSaveDraft, 1000));
17
+ postContainer.on('thumb.uploaded', doSaveDraft);
20
18
 
21
19
  draftIconEl.on('animationend', function () {
22
20
  $(this).toggleClass('active', false);
@@ -43,13 +41,6 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
43
41
  drafts.migrateThumbs(...arguments);
44
42
  };
45
43
 
46
- function resetTimeout() {
47
- if (saveThrottleId) {
48
- clearTimeout(saveThrottleId);
49
- saveThrottleId = 0;
50
- }
51
- }
52
-
53
44
  function getStorage(uid) {
54
45
  return parseInt(uid, 10) > 0 ? localStorage : sessionStorage;
55
46
  }
@@ -59,8 +50,9 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
59
50
  var storage = getStorage(uid);
60
51
  var draft = {
61
52
  text: storage.getItem(save_id),
53
+ save_id: save_id,
62
54
  };
63
- ['cid', 'title', 'tags', 'uuid'].forEach(function (key) {
55
+ ['cid', 'title', 'tags', 'uuid', 'timestamp'].forEach(function (key) {
64
56
  const value = storage.getItem(save_id + ':' + key);
65
57
  if (value) {
66
58
  draft[key] = value;
@@ -69,6 +61,9 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
69
61
  if (!parseInt(uid, 10)) {
70
62
  draft.handle = storage.getItem(save_id + ':handle');
71
63
  }
64
+ if (draft.timestamp) {
65
+ draft.timestampISO = utils.toISOString(draft.timestamp);
66
+ }
72
67
 
73
68
  $(window).trigger('action:composer.drafts.get', {
74
69
  save_id: save_id,
@@ -94,6 +89,7 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
94
89
  if (raw.length || (title && title.length)) {
95
90
  storage.setItem(postData.save_id, raw);
96
91
  storage.setItem(`${postData.save_id}:uuid`, postContainer.attr('data-uuid'));
92
+ storage.setItem(`${postData.save_id}:timestamp`, Date.now());
97
93
 
98
94
  if (postData.hasOwnProperty('cid')) {
99
95
  // New topic only
@@ -122,7 +118,7 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
122
118
  if (!save_id) {
123
119
  return;
124
120
  }
125
- resetTimeout();
121
+
126
122
  // Remove save_id from list of open and available drafts
127
123
  drafts.updateVisibility('available', save_id);
128
124
  drafts.updateVisibility('open', save_id);
@@ -130,6 +126,10 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
130
126
  var storage = getStorage(uid);
131
127
  const keys = Object.keys(storage).filter(key => key.startsWith(save_id));
132
128
  keys.forEach(key => storage.removeItem(key));
129
+ $(window).trigger('action:composer.drafts.remove', {
130
+ storage: storage,
131
+ save_id: save_id,
132
+ });
133
133
  };
134
134
 
135
135
  drafts.updateVisibility = function (set, save_id, add) {
@@ -204,6 +204,29 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
204
204
  }
205
205
  };
206
206
 
207
+ drafts.listAvailable = function () {
208
+ try {
209
+ let available = localStorage.getItem('drafts:available');
210
+ available = JSON.parse(available) || [];
211
+ return available.map(drafts.get);
212
+ } catch (e) {
213
+ console.warn('[composer/drafts] Could not read list of available drafts');
214
+ }
215
+ return [];
216
+ };
217
+
218
+ drafts.getAvailableCount = function () {
219
+ return drafts.listAvailable().length;
220
+ };
221
+
222
+ drafts.open = function (save_id) {
223
+ if (!save_id) {
224
+ return;
225
+ }
226
+ const draft = drafts.get(save_id);
227
+ openComposer(save_id, draft);
228
+ };
229
+
207
230
  drafts.loadOpen = function () {
208
231
  if (ajaxify.data.template.login || ajaxify.data.template.register) {
209
232
  return;
@@ -228,10 +251,6 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
228
251
  if (!save_id) {
229
252
  return;
230
253
  }
231
- var saveObj = save_id.split(':');
232
- var uid = saveObj[1];
233
- var type = saveObj[2];
234
- var id = saveObj[3];
235
254
  var draft = drafts.get(save_id);
236
255
 
237
256
  // If draft is already open, do nothing
@@ -239,41 +258,48 @@ define('composer/drafts', ['api', 'alerts'], function (api, alerts) {
239
258
  return;
240
259
  }
241
260
 
242
- // Don't open other peoples' drafts
243
- if (parseInt(app.user.uid, 10) !== parseInt(uid, 10)) {
244
- return;
245
- }
246
-
247
- if (!draft || (draft.text && draft.title && !draft.text.title && !draft.text.length)) {
261
+ if (!draft || (!draft.text && !draft.title)) {
248
262
  // Empty content, remove from list of open drafts
249
263
  drafts.updateVisibility('available', save_id);
250
264
  drafts.updateVisibility('open', save_id);
251
265
  return;
252
266
  }
253
- require(['composer'], function (composer) {
254
- if (type === 'cid') {
255
- composer.newTopic({
256
- cid: id,
257
- handle: app.user && app.user.uid ? undefined : utils.escapeHTML(draft.handle),
258
- title: utils.escapeHTML(draft.title),
259
- body: utils.escapeHTML(draft.text),
260
- tags: String(draft.tags || '').split(','),
261
- });
262
- } else if (type === 'tid') {
263
- api.get('/topics/' + id, {}, function (err, topicObj) {
264
- if (err) {
265
- return alerts.error(err);
266
- }
267
- composer.newReply(id, undefined, topicObj.title, utils.escapeHTML(draft.text));
268
- });
269
- } else if (type === 'pid') {
270
- composer.editPost(id);
271
- }
272
- });
267
+ openComposer(save_id, draft);
273
268
  });
274
269
  }
275
270
  };
276
271
 
272
+ function openComposer(save_id, draft) {
273
+ var saveObj = save_id.split(':');
274
+ var uid = saveObj[1];
275
+ var type = saveObj[2];
276
+ var id = saveObj[3];
277
+ // Don't open other peoples' drafts
278
+ if (parseInt(app.user.uid, 10) !== parseInt(uid, 10)) {
279
+ return;
280
+ }
281
+ require(['composer'], function (composer) {
282
+ if (type === 'cid') {
283
+ composer.newTopic({
284
+ cid: id,
285
+ handle: app.user && app.user.uid ? undefined : utils.escapeHTML(draft.handle),
286
+ title: utils.escapeHTML(draft.title),
287
+ body: utils.escapeHTML(draft.text),
288
+ tags: String(draft.tags || '').split(','),
289
+ });
290
+ } else if (type === 'tid') {
291
+ api.get('/topics/' + id, {}, function (err, topicObj) {
292
+ if (err) {
293
+ return alerts.error(err);
294
+ }
295
+ composer.newReply(id, undefined, topicObj.title, utils.escapeHTML(draft.text));
296
+ });
297
+ } else if (type === 'pid') {
298
+ composer.editPost(id);
299
+ }
300
+ });
301
+ }
302
+
277
303
  // Feature detection courtesy of: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
278
304
  function canSave(type) {
279
305
  var storage;
@@ -334,10 +334,6 @@ define('composer', [
334
334
 
335
335
  postContainer.on('change', 'input, textarea', function () {
336
336
  composer.posts[post_uuid].modified = true;
337
-
338
- // Post is modified, save to list of opened drafts
339
- drafts.updateVisibility('available', composer.posts[post_uuid].save_id, true);
340
- drafts.updateVisibility('open', composer.posts[post_uuid].save_id, true);
341
337
  });
342
338
 
343
339
  submitBtn.on('click', function (e) {
@@ -12,6 +12,4 @@
12
12
  </div>
13
13
  </form>
14
14
 
15
- <button id="save" class="floating-button mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored">
16
- <i class="material-icons">save</i>
17
- </button>
15
+ <!-- IMPORT admin/partials/save_button.tpl -->