Files: 98160d45e90af28d4a3c38e17ba614f245725906 / modules_basic / avatar / edit.js
4431 bytesRaw
1 | |
2 | const fs = require('fs') |
3 | const dataurl = require('dataurl-') |
4 | const hyperfile = require('hyperfile') |
5 | const hypercrop = require('hypercrop') |
6 | const hyperlightbox = require('hyperlightbox') |
7 | const h = require('../../h') |
8 | const { |
9 | Value, Array: MutantArray, Dict: MutantObject, |
10 | map, computed, when |
11 | } = require('@mmckegg/mutant') |
12 | const pull = require('pull-stream') |
13 | const getAvatar = require('ssb-avatar') |
14 | const ref = require('ssb-ref') |
15 | const visualize = require('visualize-buffer') |
16 | const self_id = require('../../keys').id |
17 | |
18 | function crop (d, cb) { |
19 | var canvas = hypercrop(h('img', {src: d})) |
20 | |
21 | return h('div.column.avatar_pic', [ |
22 | canvas, |
23 | //canvas.selection, |
24 | h('div.row.avatar_pic__controls', [ |
25 | h('button', 'okay', {'ev-click': () => { |
26 | cb(null, canvas.selection.toDataURL()) |
27 | }}), |
28 | h('button', 'cancel', {'ev-click': () => { |
29 | cb(new Error('canceled')) |
30 | }}) |
31 | ]) |
32 | ]) |
33 | } |
34 | |
35 | exports.needs = { |
36 | message_confirm: 'first', |
37 | sbot_blobs_add: 'first', |
38 | blob_url: 'first', |
39 | sbot_links: 'first', |
40 | avatar_name: 'first' |
41 | } |
42 | |
43 | exports.gives = { |
44 | avatar_edit: true, |
45 | mcss: true |
46 | } |
47 | |
48 | exports.create = function (api) { |
49 | return { |
50 | avatar_edit, |
51 | mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8') |
52 | } |
53 | |
54 | function avatar_edit (id) { |
55 | var img = visualize(new Buffer(id.substring(1), 'base64'), 256) |
56 | |
57 | var proposedAvatar = MutantObject() |
58 | getAvatar({links: api.sbot_links}, self_id, id, (err, avatar) => { |
59 | if (err) return console.error(err) |
60 | //don't show user has already selected an avatar. |
61 | if(proposedAvatar.keys().length) return |
62 | if(ref.isBlob(avatar.image)) |
63 | img.src = api.blob_url(avatar.image) |
64 | }) |
65 | |
66 | var images = MutantArray() |
67 | pull( |
68 | api.sbot_links({dest: id, rel: 'about', values: true}), |
69 | pull.map(e => e.value.content.image), |
70 | pull.filter(e => e && 'string' == typeof e.link), |
71 | pull.unique('link'), |
72 | pull.drain(image => images.push(image) ) |
73 | ) |
74 | |
75 | var lb = hyperlightbox() |
76 | var name = Value(api.avatar_name(id)) |
77 | var proposedName = Value() |
78 | var names = [] //TODO load in name aliases |
79 | var name_input = h('input', {placeholder: ' + another name', 'ev-keyup': (e) => proposedName.set(e.target.value) }) |
80 | var description = '' //TODO load this in, make this editable |
81 | |
82 | var isPossibleUpdate = computed([proposedName, proposedAvatar], (name, image) => { |
83 | console.log('checking', name, image) |
84 | |
85 | return name || Object.keys(image).length |
86 | }) |
87 | |
88 | return h('ProfileEdit', [ |
89 | h('section.lightbox', lb), |
90 | h('section.avatar', [ |
91 | h('section', img), |
92 | h('footer', name), |
93 | ]), |
94 | h('section.description', description), |
95 | h('section.aliases', [ |
96 | h('header', 'Aliases'), |
97 | h('section.avatars', [ |
98 | h('header', 'Avatars'), |
99 | map(images, image => h('img', { |
100 | 'src': api.blob_url(image), |
101 | 'ev-click': changeSelectedImage(image) |
102 | })), |
103 | hyperfile.asDataURL(dataUrlCallback), |
104 | ]), |
105 | h('section.names', [ |
106 | h('header', 'Names'), |
107 | names, |
108 | name_input |
109 | ]), |
110 | when(isPossibleUpdate, h('button.confirm', { 'ev-click': handleUpdateClick }, 'Confirm changes')) |
111 | ]) |
112 | ]) |
113 | |
114 | function changeSelectedImage (image) { |
115 | return () => { |
116 | proposedAvatar.set(image) |
117 | img.src = api.blob_url(image.link || image) |
118 | } |
119 | } |
120 | |
121 | function dataUrlCallback (data) { |
122 | var el = crop(data, (err, data) => { |
123 | if(data) { |
124 | img.src = data |
125 | var _data = dataurl.parse(data) |
126 | pull( |
127 | pull.once(_data.data), |
128 | api.sbot_blobs_add((err, hash) => { |
129 | //TODO. Alerts are EVIL. |
130 | //I use them only in a moment of weakness. |
131 | |
132 | if(err) return alert(err.stack) |
133 | proposedAvatar.set({ |
134 | link: hash, |
135 | size: _data.data.length, |
136 | type: _data.mimetype, |
137 | width: 512, |
138 | height: 512 |
139 | }) |
140 | }) |
141 | ) |
142 | } |
143 | lb.close() |
144 | }) |
145 | lb.show(el) |
146 | } |
147 | |
148 | function handleUpdateClick () { |
149 | const msg = { |
150 | type: 'about', |
151 | about: id, |
152 | name: proposedName(), |
153 | image: proposedAvatar() |
154 | } |
155 | |
156 | api.message_confirm(msg) |
157 | |
158 | if(proposedName) name.set('@'+proposedName()) |
159 | } |
160 | } |
161 | |
162 | } |
163 | |
164 |
Built with git-ssb-web