localfileeditor.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (C) 2014 KO GmbH <copyright@kogmbh.com>
  3. *
  4. * @licstart
  5. * This file is part of WebODF.
  6. *
  7. * WebODF is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU Affero General Public License (GNU AGPL)
  9. * as published by the Free Software Foundation, either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * WebODF is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with WebODF. If not, see <http://www.gnu.org/licenses/>.
  19. * @licend
  20. *
  21. * @source: http://www.webodf.org/
  22. * @source: https://github.com/kogmbh/WebODF/
  23. */
  24. function createEditor(path) {
  25. "use strict";
  26. var editor = null,
  27. editorOptions,
  28. loadedFilename;
  29. /**
  30. * @return {undefined}
  31. */
  32. function startEditing() {
  33. }
  34. /**
  35. * extract document url from the url-fragment
  36. *
  37. * @return {?string}
  38. */
  39. function guessDocUrl() {
  40. var pos, docUrl = String(document.location);
  41. // If the URL has a fragment (#...), try to load the file it represents
  42. pos = docUrl.indexOf('#');
  43. if (pos !== -1) {
  44. docUrl = docUrl.substr(pos + 1);
  45. } else if (path) {
  46. docUrl = path;
  47. }else {
  48. docUrl = "welcome.odt";
  49. }
  50. return docUrl || null;
  51. }
  52. function fileSelectHandler(evt) {
  53. var file, files, reader;
  54. files = (evt.target && evt.target.files) ||
  55. (evt.dataTransfer && evt.dataTransfer.files);
  56. function onLoadEnd() {
  57. if (reader.readyState === 2) {
  58. runtime.registerFile(file.name, reader.result);
  59. loadedFilename = file.name;
  60. editor.openDocumentFromUrl(loadedFilename, startEditing);
  61. }
  62. }
  63. if (files && files.length === 1) {
  64. editor.closeDocument(function() {
  65. file = files[0];
  66. reader = new FileReader();
  67. reader.onloadend = onLoadEnd;
  68. reader.readAsArrayBuffer(file);
  69. });
  70. } else {
  71. alert("File could not be opened in this browser.");
  72. }
  73. }
  74. function enhanceRuntime() {
  75. var openedFiles = {},
  76. read = runtime.read,
  77. getFileSize = runtime.getFileSize;
  78. runtime.read = function (path, offset, length, callback) {
  79. var array;
  80. if (openedFiles.hasOwnProperty(path)) {
  81. array = new Uint8Array(openedFiles[path], offset, length);
  82. callback(undefined, array);
  83. } else {
  84. return read(path, offset, length, callback);
  85. }
  86. };
  87. runtime.getFileSize = function (path, callback) {
  88. if (openedFiles.hasOwnProperty(path)) {
  89. return callback(openedFiles[path].byteLength);
  90. } else {
  91. return getFileSize(path, callback);
  92. }
  93. };
  94. runtime.registerFile = function (path, data) {
  95. openedFiles[path] = data;
  96. };
  97. }
  98. function createFileLoadForm() {
  99. var form = document.createElement("form"),
  100. input = document.createElement("input");
  101. function internalHandler(evt) {
  102. if (input.value !== "") {
  103. fileSelectHandler(evt);
  104. }
  105. // reset to "", so selecting the same file next time still trigger the change handler
  106. input.value = "";
  107. }
  108. form.appendChild(input);
  109. form.style.display = "none";
  110. input.id = "fileloader";
  111. input.setAttribute("type", "file");
  112. input.addEventListener("change", internalHandler, false);
  113. document.body.appendChild(form);
  114. }
  115. function load() {
  116. var form = document.getElementById("fileloader");
  117. if (!form) {
  118. enhanceRuntime();
  119. createFileLoadForm();
  120. form = document.getElementById("fileloader");
  121. }
  122. form.click();
  123. }
  124. function save() {
  125. function saveByteArrayLocally(err, data) {
  126. if (err) {
  127. alert(err);
  128. return;
  129. }
  130. // TODO: odfcontainer should have a property mimetype
  131. var mimetype = "application/vnd.oasis.opendocument.text",
  132. filename = loadedFilename || "doc.odt",
  133. blob = new Blob([data.buffer], {type: mimetype});
  134. saveAs(blob, filename);
  135. }
  136. editor.getDocumentAsByteArray(saveByteArrayLocally);
  137. }
  138. editorOptions = {
  139. loadCallback: load,
  140. saveCallback: save,
  141. allFeaturesEnabled: true
  142. };
  143. function onEditorCreated(err, e) {
  144. var docUrl = guessDocUrl();
  145. console.log(docUrl);
  146. if (err) {
  147. // something failed unexpectedly
  148. alert(err);
  149. return;
  150. }
  151. editor = e;
  152. editor.setUserData({
  153. fullName: "WebODF-Curious",
  154. color: "black"
  155. });
  156. if (docUrl) {
  157. loadedFilename = docUrl;
  158. editor.openDocumentFromUrl(docUrl, startEditing);
  159. }
  160. }
  161. Wodo.createTextEditor('editorContainer', editorOptions, onEditorCreated);
  162. }