Files: 93c43d5bdd6ca578eeb9cc8fd147cbd099c08175 / modules / avatar-edit.js
2196 bytesRaw
1 | var dataurl = require('dataurl') |
2 | |
3 | var hyperfile = require('hyperfile') |
4 | |
5 | var hypercrop = require('hypercrop') |
6 | var hyperlightbox = require('hyperlightbox') |
7 | var h = require('hyperscript') |
8 | |
9 | function crop (d, cb) { |
10 | var data |
11 | var canvas = hypercrop(h('img', {src: d})) |
12 | |
13 | return h('div.avatar_pic', |
14 | canvas, canvas.selection, |
15 | h('div.avatar_pic__controls', |
16 | h('button', 'okay', {onclick: function () { |
17 | cb(null, canvas.selection.toDataURL()) |
18 | }}), |
19 | h('button', 'cancel', {onclick: function () { |
20 | cb(new Error('canceled')) |
21 | }}) |
22 | ) |
23 | ) |
24 | } |
25 | |
26 | var plugs = require('../plugs') |
27 | var confirm = plugs.first(exports.message_confirm = []) |
28 | var sbot_blobs_add = plugs.first(exports.sbot_blobs_add = []) |
29 | |
30 | var pull = require('pull-stream') |
31 | |
32 | exports.avatar_edit = function (id) { |
33 | var lb = hyperlightbox() |
34 | var img = h('img', {src: ''}) //TODO, show current image. |
35 | var name = h('input', {placeholder: 'rename'}) |
36 | |
37 | var selected = null |
38 | |
39 | return h('div', |
40 | lb, |
41 | img, |
42 | name, |
43 | hyperfile.asDataURL(function (data) { |
44 | var el = crop(data, function (err, data) { |
45 | if(data) { |
46 | img.src = data |
47 | selected = dataurl.parse(data) |
48 | } |
49 | lb.close() |
50 | }) |
51 | lb.show(el) |
52 | }), |
53 | h('button', 'update', {onclick: function () { |
54 | if(selected) { |
55 | pull( |
56 | pull.once(selected.data), |
57 | sbot_blobs_add(function (err, hash) { |
58 | //TODO. Alerts are EVIL. |
59 | //I use them only in a moment of weakness. |
60 | if(err) return alert(err.stack) |
61 | confirm({ |
62 | type: 'about', |
63 | about: id, |
64 | name: name.value || undefined, |
65 | image: { |
66 | link: hash, |
67 | size: selected.data.length, |
68 | type: selected.mimetype, |
69 | width: 512, |
70 | height: 512 |
71 | } |
72 | }) |
73 | }) |
74 | ) |
75 | } |
76 | else if(input.value) //name only |
77 | confirm({ |
78 | type: 'about', |
79 | about: id, |
80 | name: name.value || undefined, |
81 | }) |
82 | else |
83 | //another moment of weakness |
84 | alert('must select a name or image') |
85 | }}) |
86 | ) |
87 | } |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 |
Built with git-ssb-web