diff --git a/www/code/codemirror-5.13.2/mode/loadmode.js b/www/code/codemirror-5.13.2/mode/loadmode.js new file mode 100644 index 000000000..10117ec22 --- /dev/null +++ b/www/code/codemirror-5.13.2/mode/loadmode.js @@ -0,0 +1,64 @@ +// CodeMirror, copyright (c) by Marijn Haverbeke and others +// Distributed under an MIT license: http://codemirror.net/LICENSE + +(function(mod) { + if (typeof exports == "object" && typeof module == "object") // CommonJS + mod(require("../../lib/codemirror"), "cjs"); + else if (typeof define == "function" && define.amd) // AMD + define(["../../lib/codemirror"], function(CM) { mod(CM, "amd"); }); + else // Plain browser env + mod(CodeMirror, "plain"); +})(function(CodeMirror, env) { + if (!CodeMirror.modeURL) CodeMirror.modeURL = "../mode/%N/%N.js"; + + var loading = {}; + function splitCallback(cont, n) { + var countDown = n; + return function() { if (--countDown == 0) cont(); }; + } + function ensureDeps(mode, cont) { + var deps = CodeMirror.modes[mode].dependencies; + if (!deps) return cont(); + var missing = []; + for (var i = 0; i < deps.length; ++i) { + if (!CodeMirror.modes.hasOwnProperty(deps[i])) + missing.push(deps[i]); + } + if (!missing.length) return cont(); + var split = splitCallback(cont, missing.length); + for (var i = 0; i < missing.length; ++i) + CodeMirror.requireMode(missing[i], split); + } + + CodeMirror.requireMode = function(mode, cont) { + if (typeof mode != "string") mode = mode.name; + if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont); + if (loading.hasOwnProperty(mode)) return loading[mode].push(cont); + + var file = CodeMirror.modeURL.replace(/%N/g, mode); + if (env == "plain") { + var script = document.createElement("script"); + script.src = file; + var others = document.getElementsByTagName("script")[0]; + var list = loading[mode] = [cont]; + CodeMirror.on(script, "load", function() { + ensureDeps(mode, function() { + for (var i = 0; i < list.length; ++i) list[i](); + }); + }); + others.parentNode.insertBefore(script, others); + } else if (env == "cjs") { + require(file); + cont(); + } else if (env == "amd") { + requirejs([file], cont); + } + }; + + CodeMirror.autoLoadMode = function(instance, mode) { + if (!CodeMirror.modes.hasOwnProperty(mode)) + CodeMirror.requireMode(mode, function() { + instance.setOption("mode", instance.getOption("mode")); + }); + }; +}); diff --git a/www/code/inner.html b/www/code/inner.html index c031930ae..81c97fb0e 100644 --- a/www/code/inner.html +++ b/www/code/inner.html @@ -8,6 +8,8 @@ + + diff --git a/www/code/main.js b/www/code/main.js index 65ad0afe7..064a1ab63 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -28,6 +28,10 @@ define([ var secret = Cryptpad.getSecrets(); var andThen = function (CMeditor) { + var CodeMirror = module.CodeMirror = CMeditor; + CodeMirror.modeURL = "/code/codemirror-5.13.2/mode/%N/%N.js"; + + var $pad = $('#pad-iframe'); var $textarea = $pad.contents().find('#editor1'); @@ -47,6 +51,11 @@ define([ readOnly: true }); + var setMode = module.setMode = function (mode) { + CodeMirror.autoLoadMode(editor, mode); + editor.setOption('mode', mode); + }; + var setEditable = module.setEditable = function (bool) { editor.setOption('readOnly', !bool); }; @@ -138,12 +147,19 @@ define([ var config = { userData: userList, changeNameID: 'cryptpad-changeName', - saveContentID: 'cryptpad-saveContent', + exportContentID: 'cryptpad-saveContent', + importContentID: 'cryptpad-loadContent', }; toolbar = info.realtime.toolbar = Toolbar.create($bar, info.myID, info.realtime, info.getLag, info.userList, config); createChangeName('cryptpad-changeName', $bar); $bar.find('#cryptpad-saveContent').click(exportText); + $bar.find('#cryptpad-loadContent') + .click(Cryptpad.importContent('text/plain', function (content) { + editor.setValue(content); + config.onLocal(); + })); + window.location.hash = info.channel + secret.key; Cryptpad.rememberPad(); }; diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index f0b5367a0..53506f8f5 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -45,5 +45,17 @@ define([ localStorage[storageKey] = JSON.stringify(out); }; + var importContent = common.importContent = function (type, f) { + return function () { + var $files = $('').click() + $files.on('change', function (e) { + var file = e.target.files[0]; + var reader = new FileReader(); + reader.onload = function (e) { f(e.target.result); }; + reader.readAsText(file, type); + }); + }; + }; + return common; }); diff --git a/www/pad/main.js b/www/pad/main.js index 02b32957a..a7681bb66 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -360,22 +360,12 @@ define([ var $saver = $bar.find('#cryptpad-saveContent').click(exportFile); - var $loader = $bar.find('#cryptpad-loadContent').click(function () { - var $files = $('').click() - $files.on('change', function (e) { - var file = e.target.files[0]; - var reader = new FileReader(); - - reader.onload = function (e) { - var result = e.target.result; - console.log(result); - var shjson = stringify(Hyperjson.fromDOM(domFromHTML(result).body)); - applyHjson(shjson); - onLocal(); - }; - reader.readAsText(file, 'text/plain'); - }); - }); + $bar.find('#cryptpad-loadContent') + .click(Cryptpad.importContent('text/plain', function (content) { + var shjson = stringify(Hyperjson.fromDOM(domFromHTML(content).body)); + applyHjson(shjson); + onLocal(); + })); // set the hash window.location.hash = info.channel + secret.key;