patchcompose-drafts
Advanced tools
+72
| var h = require('hyperscript') | ||
| var isMsg = require('ssb-ref').isMsg | ||
| exports.gives = { | ||
| compose: {insert: true} | ||
| } | ||
| function get(str) { | ||
| try { | ||
| return JSON.parse(localStorage['drafts:'+str]) | ||
| } catch (err) { } | ||
| } | ||
| function set (key, value) { | ||
| var _key = 'drafts:'+key | ||
| var _value = localStorage[_key] | ||
| if(value == null || value == '') { | ||
| localStorage.removeItem(_key) | ||
| } | ||
| else | ||
| localStorage[_key] = | ||
| JSON.stringify({ts: Date.now(), value: value}) | ||
| var ev = new CustomEvent('storage', {}) | ||
| ev.key = _key | ||
| ev.newValue = localStorage[_key] | ||
| ev.oldValue = _value | ||
| window.dispatchEvent(ev) | ||
| } | ||
| exports.create = function (api) { | ||
| for(var k in localStorage) { | ||
| if(isMsg(k)) { | ||
| var value = localStorage[k] | ||
| localStorage.removeItem(k) | ||
| set(k, value) | ||
| } | ||
| } | ||
| return { | ||
| compose: { | ||
| insert: function (ta, meta, context) { | ||
| if(!context) return | ||
| var timer | ||
| var button = h('button', 'save', {onclick: onSave}) | ||
| function onSave () { | ||
| clearTimeout(timer) | ||
| set(context.path, ta.value) | ||
| button.textContent = 'saved!' | ||
| } | ||
| //load from drafts, if defined. | ||
| var data = get(context.path) | ||
| if(data && data.value) ta.value = data.value | ||
| ta.onchange = | ||
| ta.oninput = function () { | ||
| clearTimeout(timer) | ||
| if(ta.value == '') { | ||
| set(context.path, undefined) | ||
| button.disabled = true | ||
| return | ||
| } | ||
| button.disabled = false | ||
| button.textContent = 'save' | ||
| timer = setTimeout(onSave, 3000) | ||
| } | ||
| return button | ||
| } | ||
| } | ||
| } | ||
| } | ||
+85
| var h = require('hyperscript') | ||
| var isMsg = require('ssb-ref').isMsg | ||
| var pull = require('pull-stream') | ||
| var friendly = require('base64-url') | ||
| exports.gives = { | ||
| app: { | ||
| view: true, | ||
| menu: true | ||
| } | ||
| } | ||
| exports.needs = { | ||
| message: {layout: 'first'}, | ||
| sbot: { get: 'first' } | ||
| } | ||
| function prepend(container, el) { | ||
| if(container.firstChild) | ||
| container.insertBefore(el, container.firstChild) | ||
| else | ||
| container.appendChild(el) | ||
| } | ||
| exports.create = function (api) { | ||
| return { | ||
| app: { | ||
| view: function (src) { | ||
| if(src !== '/drafts') return | ||
| var a = [], id | ||
| for(var k in localStorage) { | ||
| if(/^drafts:/.test(k) && isMsg(id = k.substring(7))) | ||
| try { | ||
| a.push({ts: JSON.parse(localStorage[k]).ts, id: id}) | ||
| } | ||
| catch (err) {} | ||
| } | ||
| var content = h('div.content') | ||
| pull( | ||
| pull.values(a.sort(function (a, b) { | ||
| return b.ts - a.ts | ||
| })), | ||
| pull.asyncMap(function (e, cb) { | ||
| api.sbot.get(e.id, function (err, value) { | ||
| cb(err, {key:e.id, value: value}) | ||
| }) | ||
| }), | ||
| pull.drain(function (msg) { | ||
| var el = api.message.layout(msg) | ||
| el.id = friendly.encode(msg.key) | ||
| content.appendChild(el) | ||
| }) | ||
| ) | ||
| window.addEventListener('storage', function (ev) { | ||
| var id | ||
| if(/^drafts:/.test(ev.key) && isMsg(id = ev.key.substring(7))) { | ||
| var el = content.querySelector('#'+friendly.encode(id)) | ||
| if(el && !ev.newValue) { | ||
| content.removeChild(el) | ||
| } | ||
| else if(el) { | ||
| //bring to start of list | ||
| prepend(content, el) | ||
| } | ||
| else | ||
| api.sbot.get(id, function (err, msg) { | ||
| var el = api.message.layout({key: id, value: msg}) | ||
| el.id = friendly.encode(id) | ||
| prepend(content, el) | ||
| }) | ||
| } | ||
| }) | ||
| return content | ||
| }, | ||
| menu: function () { | ||
| return '/drafts' | ||
| } | ||
| } | ||
| } | ||
| } | ||
+5
-39
@@ -1,40 +0,6 @@ | ||
| var h = require('hyperscript') | ||
| exports.gives = { | ||
| compose: {insert: true} | ||
| module.exports = { | ||
| patchdrafts: { | ||
| view: require('./view'), | ||
| compose: require('./compose') | ||
| } | ||
| } | ||
| exports.create = function (api) { | ||
| return { compose: { insert: function (ta, meta, context) { | ||
| if(!context) return | ||
| var timer | ||
| var button = h('button', 'save', {onclick: onSave}) | ||
| function onSave () { | ||
| clearTimeout(timer) | ||
| localStorage[context.path] = ta.value | ||
| button.textContent = 'saved!' | ||
| } | ||
| //load from drafts, if defined. | ||
| if(localStorage[context.path]) | ||
| ta.value = localStorage[context.path] | ||
| ta.oninput = function () { | ||
| clearTimeout(timer) | ||
| if(ta.value == '') { | ||
| localStorage.removeItem(context.path) | ||
| button.disabled = true | ||
| return | ||
| } | ||
| button.disabled = false | ||
| button.textContent = 'save' | ||
| timer = setTimeout(onSave, 3000) | ||
| } | ||
| return button | ||
| }}} | ||
| } | ||
+5
-2
| { | ||
| "name": "patchcompose-drafts", | ||
| "description": "", | ||
| "version": "1.0.1", | ||
| "version": "2.0.0", | ||
| "homepage": "https://github.com/dominictarr/patchcompose-drafts", | ||
@@ -11,3 +11,6 @@ "repository": { | ||
| "dependencies": { | ||
| "hyperscript": "^2.0.2" | ||
| "base64-url": "^2.1.0", | ||
| "hyperscript": "^2.0.2", | ||
| "pull-stream": "^3.6.7", | ||
| "ssb-ref": "^2.9.1" | ||
| }, | ||
@@ -14,0 +17,0 @@ "devDependencies": {}, |
6016
106.1%7
40%146
370.97%4
300%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added