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