forked from dat-ecosystem-archive/dat-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable-title-field.js
215 lines (190 loc) · 5.21 KB
/
table-title-field.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var Nanocomponent = require('nanocomponent')
var nanomorph = require('nanomorph')
var html = require('choo/html')
var css = require('sheetify')
var icon = require('./icon')
var button = require('./button')
var overlay = css`
:host {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background-color: rgba(0,0,0,.2);
}
`
var editableField = css`
:host {
position: relative;
h2 {
position: relative;
}
.indicator {
position: absolute;
display: none;
top: .25rem;
right: 0;
width: .75rem;
}
&:hover, &:focus {
h2 {
color: var(--color-blue);
}
.indicator {
display: block;
}
}
}
`
module.exports = TitleField
// Creates an input field with an explicit save button.
// There's 2 modes: active and inactive.
// Only dats that you can write to can have an active input field.
// Inactive becomes active by clicking on the input field.
// Active becomes inactive by:
// - clicking anywhere outside the field
// - pressing escape
// - pressing enter (saving)
// - clicking the save button (saving)
function TitleField () {
if (!(this instanceof TitleField)) return new TitleField()
Nanocomponent.call(this)
this.props = null
this.state = resetState()
function resetState () {
return {
isEditing: false,
editTarget: null,
editValue: '',
title: null,
key: null
}
}
}
TitleField.prototype = Object.create(Nanocomponent.prototype)
TitleField.prototype.createElement = function (props) {
var dat = props.dat
this.props = props
var state = this.state
if (dat) {
state.writable = dat.writable
state.key = dat.key.toString('hex')
state.title = dat.metadata.title || ''
state.placeholderTitle = '#' + state.key
}
return (state.isEditing && state.writable)
? this.renderActive()
: this.renderInactive()
}
TitleField.prototype.renderInactive = function () {
var self = this
var state = this.state
state.editValue = ''
return state.writable
? html`
<div class=${editableField}>
<h2 class="f6 f5-l normal truncate pr3" onclick=${onclick}>
${state.title || state.placeholderTitle}
${icon('edit', { class: 'absolute top-0 bottom-0 right-0 color-neutral-30 indicator' })}
</h2>
</div>
`
: html`
<div>
<h2 class="f6 f5-l normal truncate pr3">
${state.title || state.placeholderTitle}
</h2>
</div>
`
function onclick (e) {
e.stopPropagation()
e.preventDefault()
state.isEditing = true
state.editValue = state.title
state.update = true
self.props.emit('render')
}
}
TitleField.prototype.renderActive = function () {
var self = this
setTimeout(function () {
var input = self.element.querySelector('input')
input.focus()
input.select()
}, 0)
var state = this.state
return html`
<div>
<div class=${overlay} onclick=${deactivate}></div>
<div class="${editableField} bg-white nt1 nb1 nl1 pl1 shadow-1 flex justify-between">
<input class="bn f6 normal w-100"
value=${state.editValue} onkeyup=${handleKeypress} />
${renderButton()}
</div>
</div>
`
function handleKeypress (e) {
var oldValue = state.editValue
var newValue = e.target.value
state.editValue = newValue
e.stopPropagation()
if (e.code === 'Escape') {
e.preventDefault()
deactivate()
} else if (e.code === 'Enter') {
e.preventDefault()
handleSave()
} else if (oldValue !== newValue) {
nanomorph(self.element.querySelector('button'), renderButton())
}
}
function renderButton () {
if (state.editValue === state.title) {
return button('Save', { onclick: deactivate })
} else {
return button.green('Save', { onclick: handleSave })
}
}
function handleSave (e) {
if (e) {
e.stopPropagation()
e.preventDefault()
}
self.props.emit('dats:update-title', { key: self.state.key, title: self.state.editValue })
deactivate()
}
function deactivate (e) {
if (e) e.stopPropagation()
state.isEditing = false
state.update = true
self.props.emit('render')
}
}
TitleField.prototype.update = function ({ dat }, external) {
if (this.state.update) {
if (!external) {
// `.update` is usually called (and part of) the `.render` method
// meaning that setting "state.update" once will result in exactly
// one rendering. However, since the table-row is a complex component
// and the table row uses `.update` to see if any of the child
// components would need an update. If the call comes from
// the parent (or externally) we do not reset the update property
// to make sure that `.state.update` stays true until actually
// rendering the component
this.state.update = false
}
return true
}
if (dat.writable !== this.state.writable) {
return true
}
if (dat.key.toString('hex') !== this.state.key) {
return true
}
var oldTitle = (dat.metadata.title || '#' + this.state.key)
if (this.state.title !== oldTitle) {
return true
}
return false
}