-
Notifications
You must be signed in to change notification settings - Fork 6
/
localfileeditor.js
188 lines (168 loc) · 5.78 KB
/
localfileeditor.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
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
/**
* Copyright (C) 2014 KO GmbH <[email protected]>
*
* @licstart
* This file is part of WebODF.
*
* WebODF is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License (GNU AGPL)
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* WebODF is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with WebODF. If not, see <http://www.gnu.org/licenses/>.
* @licend
*
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
/*global document, window, runtime, FileReader, alert, Uint8Array, Blob, saveAs, Wodo*/
function createEditor() {
"use strict";
var editor = null,
editorOptions,
loadedFilename;
/*jslint emptyblock: true*/
/**
* @return {undefined}
*/
function startEditing() {
}
/*jslint emptyblock: false*/
/**
* extract document url from the url-fragment
*
* @return {?string}
*/
function guessDocUrl() {
var pos, docUrl = String(document.location);
// If the URL has a fragment (#...), try to load the file it represents
pos = docUrl.indexOf('#');
if (pos !== -1) {
docUrl = docUrl.substr(pos + 1);
} else {
docUrl = "welcome.odt";
}
return docUrl || null;
}
function fileSelectHandler(evt) {
var file, files, reader;
files = (evt.target && evt.target.files) ||
(evt.dataTransfer && evt.dataTransfer.files);
function onLoadEnd() {
if (reader.readyState === 2) {
runtime.registerFile(file.name, reader.result);
loadedFilename = file.name;
editor.openDocumentFromUrl(loadedFilename, startEditing);
}
}
if (files && files.length === 1) {
if (!editor.isDocumentModified() ||
window.confirm("There are unsaved changes to the file. Do you want to discard them?")) {
editor.closeDocument(function() {
file = files[0];
reader = new FileReader();
reader.onloadend = onLoadEnd;
reader.readAsArrayBuffer(file);
});
}
} else {
alert("File could not be opened in this browser.");
}
}
function enhanceRuntime() {
var openedFiles = {},
readFile = runtime.readFile;
runtime.readFile = function (path, encoding, callback) {
var array;
if (openedFiles.hasOwnProperty(path)) {
array = new Uint8Array(openedFiles[path]);
callback(undefined, array);
} else {
return readFile(path, encoding, callback);
}
};
runtime.registerFile = function (path, data) {
openedFiles[path] = data;
};
}
function createFileLoadForm() {
var form = document.createElement("form"),
input = document.createElement("input");
function internalHandler(evt) {
if (input.value !== "") {
fileSelectHandler(evt);
}
// reset to "", so selecting the same file next time still trigger the change handler
input.value = "";
}
form.appendChild(input);
form.style.display = "none";
input.id = "fileloader";
input.setAttribute("type", "file");
input.addEventListener("change", internalHandler, false);
document.body.appendChild(form);
}
function load() {
var form = document.getElementById("fileloader");
if (!form) {
enhanceRuntime();
createFileLoadForm();
form = document.getElementById("fileloader");
}
form.click();
}
function save() {
function saveByteArrayLocally(err, data) {
if (err) {
alert(err);
return;
}
// TODO: odfcontainer should have a property mimetype
var mimetype = "application/vnd.oasis.opendocument.text",
filename = loadedFilename || "doc.odt",
blob = new Blob([data.buffer], {type: mimetype});
saveAs(blob, filename);
// TODO: hm, saveAs could fail or be cancelled
editor.setDocumentModified(false);
}
editor.getDocumentAsByteArray(saveByteArrayLocally);
}
editorOptions = {
loadCallback: load,
saveCallback: save,
allFeaturesEnabled: true
};
function onEditorCreated(err, e) {
var docUrl = guessDocUrl();
if (err) {
// something failed unexpectedly
alert(err);
return;
}
editor = e;
editor.setUserData({
fullName: "WebODF-Curious",
color: "black"
});
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "There are unsaved changes to the file.";
if (editor.isDocumentModified()) {
// Gecko + IE
(e || window.event).returnValue = confirmationMessage;
// Webkit, Safari, Chrome etc.
return confirmationMessage;
}
});
if (docUrl) {
loadedFilename = docUrl;
editor.openDocumentFromUrl(docUrl, startEditing);
}
}
Wodo.createTextEditor('editorContainer', editorOptions, onEditorCreated);
}