This repository was archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathios.js
158 lines (143 loc) · 3.64 KB
/
ios.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
/* global window, document, fetch */
const MAXFILESIZE = 1024 * 1024 * 1024 * 2;
const EventEmitter = require('events');
const emitter = new EventEmitter();
function dom(tagName, attributes, children = []) {
const node = document.createElement(tagName);
for (const name in attributes) {
if (name.indexOf('on') === 0) {
node[name] = attributes[name];
} else if (name === 'htmlFor') {
node.htmlFor = attributes.htmlFor;
} else if (name === 'className') {
node.className = attributes.className;
} else {
node.setAttribute(name, attributes[name]);
}
}
if (!(children instanceof Array)) {
children = [children];
}
for (let child of children) {
if (typeof child === 'string') {
child = document.createTextNode(child);
}
node.appendChild(child);
}
return node;
}
function uploadComplete(file) {
document.body.innerHTML = '';
const input = dom('input', { id: 'url', value: file.url });
const copy = dom(
'button',
{
id: 'copy-button',
className: 'button',
onclick: () => {
window.webkit.messageHandlers['copy'].postMessage(input.value);
copy.textContent = 'Copied!';
setTimeout(function() {
copy.textContent = 'Copy to clipboard';
}, 2000);
}
},
'Copy to clipboard'
);
const node = dom(
'div',
{ id: 'striped' },
dom('div', { id: 'white' }, [
input,
copy,
dom(
'button',
{ id: 'send-another', className: 'button', onclick: render },
'Send another file'
)
])
);
document.body.appendChild(node);
}
const state = {
storage: {
files: [],
remove: function(fileId) {
console.log('REMOVE FILEID', fileId);
},
writeFile: function(file) {
console.log('WRITEFILE', file);
},
addFile: uploadComplete,
totalUploads: 0
},
transfer: null,
uploading: false,
settingPassword: false,
passwordSetError: null,
route: '/'
};
function upload(event) {
console.log('UPLOAD');
event.preventDefault();
const target = event.target;
const file = target.files[0];
if (file.size === 0) {
return;
}
if (file.size > MAXFILESIZE) {
console.log('file too big (no bigger than ' + MAXFILESIZE + ')');
return;
}
emitter.emit('upload', { file: file, type: 'click' });
}
function render() {
document.body.innerHTML = '';
const striped = dom(
'div',
{ id: 'striped' },
dom('div', { id: 'white' }, [
dom('label', { id: 'label', htmlFor: 'input' }, 'Choose file'),
dom('input', {
id: 'input',
type: 'file',
name: 'input',
onchange: upload
})
])
);
document.body.appendChild(striped);
}
emitter.on('render', function() {
document.body.innerHTML = '';
const percent =
(state.transfer.progress[0] / state.transfer.progress[1]) * 100;
const node = dom(
'div',
{ style: 'background-color: white; width: 100%' },
dom('span', {
style: `display: inline-block; width: ${percent}%; background-color: blue`
})
);
document.body.appendChild(node);
});
emitter.on('pushState', function(path) {
console.log('pushState ' + path + ' ' + JSON.stringify(state));
});
const controller = require('../app/controller').default;
try {
controller(state, emitter);
} catch (e) {
console.error('error' + e);
console.error(e);
}
function sendBase64EncodedFromSwift(encoded) {
fetch(encoded)
.then(res => res.blob())
.then(blob => {
emitter.emit('upload', { file: blob, type: 'share' });
});
}
window.sendBase64EncodedFromSwift = sendBase64EncodedFromSwift;
render();
window.webkit.messageHandlers['loaded'].postMessage('');