Skip to content

Commit 5638d31

Browse files
authored
Merge pull request #690 from JunoLab/sp/objecturlplot
use object URLs instead of data urls to support objects > 2mb
2 parents 14a797f + 6506b39 commit 5638d31

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lib/runtime/plots.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ function consoleLog (e) {
1717
log(e.message, `\nat ${e.sourceID}:${e.line}`)
1818
}
1919

20+
// https://stackoverflow.com/a/5100158/12113178
21+
function dataURItoBlob (dataURI) {
22+
// convert base64/URLEncoded data component to raw binary data held in a string
23+
let byteString
24+
if (dataURI.split(',')[0].indexOf('base64') >= 0)
25+
byteString = atob(dataURI.split(',')[1])
26+
else
27+
byteString = unescape(dataURI.split(',')[1])
28+
29+
// separate out the mime component
30+
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
31+
32+
// write the bytes of the string to a typed array
33+
var ia = new Uint8Array(byteString.length)
34+
for (var i = 0; i < byteString.length; i++) {
35+
ia[i] = byteString.charCodeAt(i)
36+
}
37+
38+
return new Blob([ia], {type:mimeString})
39+
},
40+
2041
export default {
2142
activate () {
2243
client.handle({
@@ -76,14 +97,24 @@ export default {
7697
},
7798

7899
webview (url) {
100+
const isDataURI = url.startsWith('data')
101+
if (isDataURI) {
102+
const object = dataURItoBlob(url)
103+
url = URL.createObjectURL(object)
104+
}
105+
79106
const v = views.render(webview({
80107
class: 'blinkjl',
81108
src: url,
82109
style: 'width: 100%; height: 100%'
83-
})
84-
)
110+
}))
85111
v.classList.add('native-key-bindings')
86112
v.addEventListener('console-message', e => consoleLog(e))
113+
if (isDataURI) {
114+
v.addEventListener('dom-ready', e => {
115+
URL.revokeObjectURL(url)
116+
})
117+
}
87118
return v
88119
},
89120

0 commit comments

Comments
 (0)