-
-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathImport.js
More file actions
150 lines (133 loc) · 4.15 KB
/
Import.js
File metadata and controls
150 lines (133 loc) · 4.15 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
MD.Import = function () {
const workarea = document.getElementById("workarea");
const $importInput = $('#tool_import input');
const $openInput = $('#tool_open input');
$importInput.on("change", importImage);
$openInput.on("change", openImage);
function importImage(e) {
$('#menu_bar').removeClass('active')
if (!window.FileReader) return;
//e.stopPropagation();
//e.preventDefault();
workarea.removeAttribute("style");
$('#main_menu').hide();
var file = null;
if (e.type === "drop") file = e.dataTransfer.files[0]
else file = this.files[0];
if (!file) return $.alert("File not found");
if (file.type.indexOf("image") === -1) return $.alert("File is not image");
importFile(file);
//editor.saveCanvas();
}
function importFile(file, then) {
//svg handing
if (file.type.indexOf("svg") != -1) {
var reader = new FileReader();
reader.onloadend = function (e) {
svgCanvas.importSvgString(e.target.result, true);
//svgCanvas.ungroupSelectedElement();
svgCanvas.alignSelectedElements("m", "page");
svgCanvas.alignSelectedElements("c", "page");
if (typeof then === 'function') {
then();
}
};
reader.readAsText(file);
}
//image handling
else {
var reader = new FileReader();
reader.onloadend = function (e) {
// lets insert the new image until we know its dimensions
insertNewImage = function (img_width, img_height) {
var newImage = svgCanvas.addSvgElementFromJson({
"element": "image",
"attr": {
"x": 0,
"y": 0,
"width": img_width,
"height": img_height,
"id": svgCanvas.getNextId(),
"style": "pointer-events:inherit"
}
});
svgCanvas.setHref(newImage, e.target.result);
svgCanvas.selectOnly([newImage])
svgCanvas.alignSelectedElements("m", "page")
svgCanvas.alignSelectedElements("c", "page")
editor.panel.updateContextPanel();
if (typeof then === 'function') {
then();
}
}
// put a placeholder img so we know the default dimensions
var img_width = 100;
var img_height = 100;
var img = new Image()
img.src = e.target.result
document.body.appendChild(img);
img.onload = function () {
img_width = img.offsetWidth
img_height = img.offsetHeight
insertNewImage(img_width, img_height);
document.body.removeChild(img);
}
};
reader.readAsDataURL(file)
}
}
function loadSvgString(str, callback) {
var success = svgCanvas.setSvgString(str) !== false;
callback = callback || $.noop;
if (success) {
callback(true);
editor.saveCanvas();
state.set("canvasTitle", svgCanvas.getDocumentTitle());
} else {
$.alert("Error: Unable to load SVG data", function () {
callback(false);
});
}
}
function openImage(e) {
$('#menu_bar').removeClass('active')
const f = this;
if (f.files.length === 1) {
svgCanvas.clear();
var reader = new FileReader();
reader.onloadend = function (e) {
loadSvgString(e.target.result);
editor.canvas.update(true);
};
reader.readAsText(f.files[0]);
}
}
function onDragEnter(e) {
e.stopPropagation();
e.preventDefault();
workarea.style.transform = "scale(1.1)";
}
function onDragOver(e) {
e.stopPropagation();
e.preventDefault();
}
function onDragLeave(e) {
workarea.removeAttribute("style");
e.stopPropagation();
e.preventDefault();
}
function place() {
$importInput.trigger("click");
}
function open() {
$openInput.trigger("click");
}
workarea.addEventListener('dragenter', onDragEnter, false);
workarea.addEventListener('dragover', onDragOver, false);
workarea.addEventListener('dragleave', onDragLeave, false);
workarea.addEventListener('drop', importImage, false);
this.place = place;
this.open = open;
this.loadSvgString = loadSvgString;
this.importFile = importFile;
}