/
FileReader.js
44 lines (32 loc) · 1.5 KB
/
FileReader.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
import {AbstractBackend, addBackend} from "./AbstractBackend";
export var FileReaderBackend = Object.create(AbstractBackend);
FileReaderBackend.init = function (container, _) {
AbstractBackend.init.call(this, container, "sozi-editor-backend-FileReader-input", _('Load an SVG file from your computer (<i class="fa fa-warning"></i> read-only)'));
$(container).append('<input style="display:none;" id="sozi-editor-backend-FileReader-file" type="file" accept="image/svg+xml">');
$("#sozi-editor-backend-FileReader-input").click(this.openFileChooser.bind(this));
// Load the SVG document selected in the file input
$("#sozi-editor-backend-FileReader-file").change(evt => {
if (evt.target.files.length) {
this.load(evt.target.files[0]);
}
});
return this;
};
FileReaderBackend.openFileChooser = function () {
$("#sozi-editor-backend-FileReader-file").click();
};
FileReaderBackend.getName = function (fileDescriptor) {
return fileDescriptor.name;
};
FileReaderBackend.load = function (fileDescriptor) {
var reader = new FileReader();
reader.readAsText(fileDescriptor, "utf8");
reader.onload = () => {
this.emit("load", fileDescriptor, reader.result, reader.error && reader.error.name);
};
};
addBackend(FileReaderBackend);