-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimg-live.js
More file actions
188 lines (159 loc) · 5.83 KB
/
img-live.js
File metadata and controls
188 lines (159 loc) · 5.83 KB
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
// Braid-Blob Live Images
// requires client.js
;(function() {
var subscriptions = new Map() // base_url -> sub
function sync(img) {
if (!img._img_live_base_url) img._img_live_base_url = img.src
var base_url = img._img_live_base_url
var sub = subscriptions.get(base_url)
if (sub) {
// Cancel any pending teardown
clearTimeout(sub.teardown_timer)
sub.teardown_timer = null
} else {
// Create cache-bust helper for this base URL
var param = 'img-live'
var u = new URL(base_url)
while (u.searchParams.has(param)) param = '-' + param
function cache_bust() {
u.searchParams.set(param, Math.random().toString(36).slice(2))
return u.toString()
}
subscriptions.set(base_url, sub = {
imgs: new Set(),
ac: new AbortController(),
current_src: null,
teardown_timer: null
})
function set_src(src) {
sub.current_src = src
sub.imgs.forEach(img => img.src = sub.current_src)
}
var client_p = (async () => {
var res = await braid_fetch(cache_bust(), {
method: 'HEAD',
headers: { "Merge-Type": "aww" },
subscribe: true,
retry: () => true,
signal: sub.ac.signal
})
return braid_blob_client(cache_bust(), {
signal: sub.ac.signal,
parents: res.version,
on_update: (body, content_type, version, from_local_update) =>
set_src(!from_local_update ? cache_bust() :
URL.createObjectURL(new Blob(
[body], { type: content_type || 'image/png' }))),
on_delete: () => set_src(cache_bust()),
on_error: (error) =>
console.error('Live image error for', base_url, error)
})
})()
sub.update = async (body, content_type) => {
await (await client_p).update(body, content_type)
set_src(cache_bust())
}
}
sub.imgs.add(img)
// Immediately set to the most recent known src
if (sub.current_src) img.src = sub.current_src
if (img.hasAttribute('droppable')) {
if (!img.hasAttribute('tabindex')) img.setAttribute('tabindex', '0')
img.addEventListener('dragenter', function() {
img.style.outline = '3px dashed #007bff'
img.style.outlineOffset = '3px'
})
img.addEventListener('dragleave', function() {
img.style.outline = ''
img.style.outlineOffset = ''
})
img.addEventListener('dragover', function(e) {
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
})
img.addEventListener('drop', function(e) {
e.preventDefault()
img.style.outline = ''
img.style.outlineOffset = ''
var file = e.dataTransfer.files[0]
if (!file || !file.type.startsWith('image/')) return
var reader = new FileReader()
reader.onload = () => sub.update(reader.result, file.type)
reader.readAsArrayBuffer(file)
})
img.addEventListener('click', function() {
img.focus()
})
img.addEventListener('focus', function() {
img.style.outline = '3px dashed #007bff'
img.style.outlineOffset = '3px'
document.addEventListener('paste', on_paste)
})
img.addEventListener('blur', function() {
img.style.outline = ''
img.style.outlineOffset = ''
document.removeEventListener('paste', on_paste)
})
function on_paste(e) {
var items = e.clipboardData.items
for (var i = 0; i < items.length; i++) {
if (items[i].type.startsWith('image/')) {
e.preventDefault()
var file = items[i].getAsFile()
var reader = new FileReader()
reader.onload = () => sub.update(reader.result, file.type)
reader.readAsArrayBuffer(file)
break
}
}
}
}
}
function unsync(img) {
var base_url = img._img_live_base_url
var sub = subscriptions.get(base_url)
if (sub?.imgs.delete(img) && !sub.imgs.size) {
sub.teardown_timer = setTimeout(() => {
sub.ac.abort()
subscriptions.delete(base_url)
}, 5000)
}
}
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) {
if (node.tagName === 'IMG' && node.hasAttribute('live'))
sync(node)
node.querySelectorAll('img[live]').forEach(sync)
}
})
mutation.removedNodes.forEach(function(node) {
if (node.nodeType === 1) {
if (node.tagName === 'IMG' && node.hasAttribute('live'))
unsync(node)
node.querySelectorAll('img[live]').forEach(unsync)
}
})
if (mutation.type === 'attributes' && mutation.target.tagName === 'IMG') {
if (mutation.target.hasAttribute('live'))
sync(mutation.target)
else if (mutation.attributeName === 'live')
unsync(mutation.target)
}
})
})
if (document.readyState === 'loading')
document.addEventListener('DOMContentLoaded', init)
else
init()
function init() {
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['live', 'droppable']
})
document.querySelectorAll('img[live]').forEach(sync)
}
})()