work a bit more on import/export and code highlighting

pull/1/head
ansuz 9 years ago
parent 242c9a99e3
commit 31aa09d6f6

@ -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"));
});
};
});

@ -8,6 +8,8 @@
<link rel="stylesheet" href="codemirror-5.13.2/addon/dialog/dialog.css">
<link rel="stylesheet" href="codemirror-5.13.2/addon/fold/foldgutter.css" />
<script src="codemirror-5.13.2/mode/javascript/javascript.js"></script>
<script src="codemirror-5.13.2/mode/loadmode.js"></script>
<script src="codemirror-5.13.2/mode/meta.js"></script>
<script src="codemirror-5.13.2/addon/edit/closebrackets.js"></script>
<script src="codemirror-5.13.2/addon/edit/matchbrackets.js"></script>
<script src="codemirror-5.13.2/addon/edit/trailingspace.js"></script>

@ -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();
};

@ -45,5 +45,17 @@ define([
localStorage[storageKey] = JSON.stringify(out);
};
var importContent = common.importContent = function (type, f) {
return function () {
var $files = $('<input type="file">').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;
});

@ -360,22 +360,12 @@ define([
var $saver = $bar.find('#cryptpad-saveContent').click(exportFile);
var $loader = $bar.find('#cryptpad-loadContent').click(function () {
var $files = $('<input type="file">').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));
$bar.find('#cryptpad-loadContent')
.click(Cryptpad.importContent('text/plain', function (content) {
var shjson = stringify(Hyperjson.fromDOM(domFromHTML(content).body));
applyHjson(shjson);
onLocal();
};
reader.readAsText(file, 'text/plain');
});
});
}));
// set the hash
window.location.hash = info.channel + secret.key;

Loading…
Cancel
Save