From 242c9a99e30f274d0cdf05f326045b3f4e8ec647 Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 22 Jun 2016 17:00:07 +0200 Subject: [PATCH] implement import and export in /pad/ --- www/common/toolbar.js | 180 ++++++++++++++++++++++++++---------------- www/pad/main.js | 57 ++++++++++++- 2 files changed, 170 insertions(+), 67 deletions(-) diff --git a/www/common/toolbar.js b/www/common/toolbar.js index 134bb7afb..e24bec338 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -15,6 +15,7 @@ define([ var LAG_ELEM_CLS = 'rtwysiwyg-lag'; var SAVE_ELEMENT_CLS = 'cryptpad-saveContent'; + var LOAD_ELEMENT_CLS = 'cryptpad-loadContent'; /** The toolbar class which contains the user list, debug link and lag. */ var TOOLBAR_CLS = 'rtwysiwyg-toolbar'; @@ -38,70 +39,106 @@ define([ '' ); var toolbar = $container.find('#'+id); - toolbar.append([ - '' - ].join('\n')); + + var swap = function (str, dict) { + return str.replace(/\{\{(.*?)\}\}/g, function (all, block) { + //console.log(block); + return dict[block] || block; + }); + }; + + var css = swap(function(){/* + + */}.toString().slice(14,-3), { + TOOLBAR_CLS: TOOLBAR_CLS, + SAVE_ELEMENT_CLS: SAVE_ELEMENT_CLS, + LOAD_ELEMENT_CLS: LOAD_ELEMENT_CLS, + USERNAME_BUTTON_GROUP: USERNAME_BUTTON_GROUP, + DEBUG_LINK_CLS: DEBUG_LINK_CLS, + }).trim(); + + toolbar.append(css); return toolbar; }; @@ -182,7 +219,12 @@ define([ }; var createSaveElement = function (id, $container) { - $container.append(''); + $container.append(''); + return $container.find('#'+id)[0]; + }; + + var createLoadElement = function (id, $container) { + $container.append(''); return $container.find('#'+id)[0]; }; @@ -212,8 +254,10 @@ define([ var lagElement = createLagElement(toolbar.find('.rtwysiwyg-toolbar-rightside')); var userData = config.userData; var changeNameID = config.changeNameID; - var saveContentID = config.saveContentID; + var saveContentID = config.saveContentID || config.exportContentID; + var loadContentID = config.loadContentID || config.importContentID; var saveElement; + var loadElement; // Check if the user is allowed to change his name if(changeNameID) { @@ -225,6 +269,10 @@ define([ saveElement = createSaveElement(saveContentID, toolbar.find('.rtwysiwyg-toolbar-rightside')); } + if (loadContentID) { + loadElement = createLoadElement(loadContentID, toolbar.find('.rtwysiwyg-toolbar-rightside')); + } + var connected = false; userList.onChange = function(newUserData) { diff --git a/www/pad/main.js b/www/pad/main.js index 186079949..02b32957a 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -12,6 +12,7 @@ define([ 'json.sortify', '/bower_components/textpatcher/TextPatcher.amd.js', '/common/cryptpad-common.js', + '/bower_components/file-saver/FileSaver.min.js', '/bower_components/diff-dom/diffDOM.js', '/bower_components/jquery/dist/jquery.min.js', '/customize/pad.js' @@ -19,6 +20,7 @@ define([ Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher, Cryptpad) { var $ = window.jQuery; + var saveAs = window.saveAs; var ifrw = $('#pad-iframe')[0].contentWindow; var Ckeditor; // to be initialized later... var DiffDom = window.diffDOM; @@ -98,6 +100,13 @@ define([ var diffOptions = { preDiffApply: function (info) { + if (info.node && info.node.tagName === 'BODY') { + if (info.diff.action === 'removeAttribute' && + ['class', 'spellcheck'].indexOf(info.diff.name) !== -1) { + return true; + } + } + /* DiffDOM will filter out magicline plugin elements in practice this will make it impossible to use it while someone else is typing, which could be annoying. @@ -312,16 +321,62 @@ define([ } }; + var getHTML = function (Dom) { + var data = inner.innerHTML; + Dom = Dom || (new DOMParser()).parseFromString(data,"text/html"); + return ('\n' + + '\n' + + Dom.head.outerHTML + '\n' + + Dom.body.outerHTML); + }; + + var domFromHTML = function (html) { + return new DOMParser().parseFromString(html, 'text/html'); + }; + + var exportFile = function () { + var html = getHTML(); + console.log(html === getHTML(domFromHTML(html))); + + console.log(html); + var filename = window.prompt('What would you like to name your file?', "MyPad.html"); + if (filename) { + var blob = new Blob([html], {type: "text/html;charset=utf-8"}); + saveAs(blob, filename); + } + }; + var onInit = realtimeOptions.onInit = function (info) { var $bar = $('#pad-iframe')[0].contentWindow.$('#cke_1_toolbox'); toolbarList = info.userList; var config = { userData: userList, - changeNameID: 'cryptpad-changeName' + changeNameID: 'cryptpad-changeName', + 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); + 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'); + }); + }); + // set the hash window.location.hash = info.channel + secret.key; Cryptpad.rememberPad();