From 894a355f0a87d1c11d3431a76937def7f70272c7 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 7 Apr 2020 14:27:44 +0200 Subject: [PATCH] Merge filepicker and share iframes --- customize.dist/src/outer.css | 2 +- www/code/inner.js | 1 + www/common/common-interface.js | 17 ++- www/common/common-ui-elements.js | 50 +++---- www/common/onlyoffice/inner.js | 25 ++-- www/common/sframe-app-framework.js | 21 ++- www/common/sframe-common-outer.js | 95 +++++-------- www/common/sframe-common.js | 1 - www/filepicker/main.js | 134 ------------------ www/poll/inner.js | 18 +-- .../app-secure.less} | 14 +- www/{filepicker => secureiframe}/inner.html | 4 +- www/{filepicker => secureiframe}/inner.js | 127 +++++++++++------ www/{share => secureiframe}/main.js | 29 ++-- www/share/app-share.less | 17 --- www/share/index.html | 30 ---- www/share/inner.html | 29 ---- www/share/inner.js | 87 ------------ 18 files changed, 212 insertions(+), 489 deletions(-) delete mode 100644 www/filepicker/main.js rename www/{filepicker/app-filepicker.less => secureiframe/app-secure.less} (84%) rename www/{filepicker => secureiframe}/inner.html (73%) rename www/{filepicker => secureiframe}/inner.js (63%) rename www/{share => secureiframe}/main.js (87%) delete mode 100644 www/share/app-share.less delete mode 100644 www/share/index.html delete mode 100644 www/share/inner.html delete mode 100644 www/share/inner.js diff --git a/customize.dist/src/outer.css b/customize.dist/src/outer.css index b36fb2c10..75f449e61 100644 --- a/customize.dist/src/outer.css +++ b/customize.dist/src/outer.css @@ -2,7 +2,7 @@ html, body { margin: 0px; padding: 0px; } -#sbox-iframe, #sbox-share-iframe, #sbox-filePicker-iframe { +#sbox-iframe, #sbox-secure-iframe { position: fixed; top:0; left:0; bottom:0; right:0; diff --git a/www/code/inner.js b/www/code/inner.js index 5b517c74c..28b90b07c 100644 --- a/www/code/inner.js +++ b/www/code/inner.js @@ -278,6 +278,7 @@ define([ if (MEDIA_TAG_MODES.indexOf(mode) !== -1) { // Embedding is endabled framework.setMediaTagEmbedder(function (mt) { + editor.focus(); editor.replaceSelection($(mt)[0].outerHTML); }); } else { diff --git a/www/common/common-interface.js b/www/common/common-interface.js index 4c4518434..2b3939370 100644 --- a/www/common/common-interface.js +++ b/www/common/common-interface.js @@ -464,16 +464,23 @@ define([ UI.createModal = function (cfg) { var $body = cfg.$body || $('body'); - var $blockContainer = $body.find('#'+cfg.id); - if (!$blockContainer.length) { - $blockContainer = $(h('div.cp-modal-container#'+cfg.id, { + var $blockContainer = cfg.id && $body.find('#'+cfg.id); + if (!$blockContainer || !$blockContainer.length) { + var id = ''; + if (cfg.id) { id = '#'+cfg.id; } + $blockContainer = $(h('div.cp-modal-container'+id, { tabindex: 1 })); } + var deleted = false; var hide = function () { - if (cfg.onClose) { return void cfg.onClose(); } + if (deleted) { return; } $blockContainer.hide(); - if (cfg.onClosed) { cfg.onClosed(); } + if (!cfg.id) { + deleted = true; + $blockContainer.remove(); + } + if (cfg.onClose) { cfg.onClose(); } }; $blockContainer.html('').appendTo($body); var $block = $(h('div.cp-modal')).appendTo($blockContainer); diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index a9c682afc..2729be386 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -2628,17 +2628,13 @@ define([ }); }; - UIElements.initFilePicker = function (common, cfg) { - var onSelect = cfg.onSelect || $.noop; + UIElements.openFilePicker = function (common, types, cb) { var sframeChan = common.getSframeChannel(); - sframeChan.on("EV_FILE_PICKED", function (data) { - onSelect(data); + sframeChan.query("Q_FILE_PICKER_OPEN", types, function (err, data) { + if (err) { return; } + cb(data); }); }; - UIElements.openFilePicker = function (common, types) { - var sframeChan = common.getSframeChannel(); - sframeChan.event("EV_FILE_PICKER_OPEN", types); - }; UIElements.openTemplatePicker = function (common, force) { var metadataMgr = common.getMetadataMgr(); @@ -2661,29 +2657,25 @@ define([ return; } delete pickerCfg.hidden; - common.openFilePicker(pickerCfg); var first = true; // We can only pick a template once (for a new document) - var fileDialogCfg = { - onSelect: function (data) { - if (data.type === type && first) { - UI.addLoadingScreen({hideTips: true}); - var chatChan = common.getPadChat(); - var cursorChan = common.getCursorChannel(); - sframeChan.query('Q_TEMPLATE_USE', { - href: data.href, - chat: chatChan, - cursor: cursorChan - }, function () { - first = false; - UI.removeLoadingScreen(); - Feedback.send('TEMPLATE_USED'); - }); - if (focus) { focus.focus(); } - return; - } + common.openFilePicker(pickerCfg, function (data) { + if (data.type === type && first) { + UI.addLoadingScreen({hideTips: true}); + var chatChan = common.getPadChat(); + var cursorChan = common.getCursorChannel(); + sframeChan.query('Q_TEMPLATE_USE', { + href: data.href, + chat: chatChan, + cursor: cursorChan + }, function () { + first = false; + UI.removeLoadingScreen(); + Feedback.send('TEMPLATE_USED'); + }); + if (focus) { focus.focus(); } + return; } - }; - common.initFilePicker(fileDialogCfg); + }); }; sframeChan.query("Q_TEMPLATE_EXIST", type, function (err, data) { diff --git a/www/common/onlyoffice/inner.js b/www/common/onlyoffice/inner.js index 7515c908d..a99b9744d 100644 --- a/www/common/onlyoffice/inner.js +++ b/www/common/onlyoffice/inner.js @@ -941,8 +941,16 @@ define([ if (ifr) { ifr.remove(); } }; - common.initFilePicker({ - onSelect: function (data) { + APP.AddImage = function(cb1, cb2) { + APP.AddImageSuccessCallback = cb1; + APP.AddImageErrorCallback = cb2; + common.openFilePicker({ + types: ['file'], + where: ['root'], + filter: { + fileType: ['image/'] + } + }, function (data) { if (data.type !== 'file') { debug("Unexpected data type picked " + data.type); return; @@ -963,19 +971,6 @@ define([ }); }); }); - } - }); - - APP.AddImage = function(cb1, cb2) { - APP.AddImageSuccessCallback = cb1; - APP.AddImageErrorCallback = cb2; - common.openFilePicker({ - types: ['file'], - where: ['root'], - filter: { - fileType: ['image/'] - } - }); }; diff --git a/www/common/sframe-app-framework.js b/www/common/sframe-app-framework.js index 171c58b0e..fc149b2cc 100644 --- a/www/common/sframe-app-framework.js +++ b/www/common/sframe-app-framework.js @@ -503,8 +503,13 @@ define([ var createFilePicker = function () { if (!common.isLoggedIn()) { return; } - common.initFilePicker({ - onSelect: function (data) { + $embedButton = common.createButton('mediatag', true).click(function () { + var cfg = { + types: ['file'], + where: ['root'] + }; + if ($embedButton.data('filter')) { cfg.filter = $embedButton.data('filter'); } + common.openFilePicker(cfg, function (data) { if (data.type !== 'file') { console.log("Unexpected data type picked " + data.type); return; @@ -516,17 +521,7 @@ define([ var src = data.src = data.src.slice(0,1) === '/' ? origin + data.src : data.src; mediaTagEmbedder($(''), data); - } - }); - $embedButton = common.createButton('mediatag', true).click(function () { - var cfg = { - types: ['file'], - where: ['root'] - }; - if ($embedButton.data('filter')) { - cfg.filter = $embedButton.data('filter'); - } - common.openFilePicker(cfg); + }); }).appendTo(toolbar.$rightside).hide(); }; var setMediaTagEmbedder = function (mte, filter) { diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index ea4cc7b8b..62491ffad 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -18,8 +18,7 @@ define([ var Cryptget; var SFrameChannel; var sframeChan; - var FilePicker; - var Share; + var SecureIframe; var Messaging; var Notifier; var Utils = { @@ -44,8 +43,7 @@ define([ '/bower_components/chainpad-crypto/crypto.js', '/common/cryptget.js', '/common/outer/worker-channel.js', - '/filepicker/main.js', - '/share/main.js', + '/secureiframe/main.js', '/common/common-messaging.js', '/common/common-notifier.js', '/common/common-hash.js', @@ -58,15 +56,14 @@ define([ '/common/test.js', '/common/userObject.js', ], waitFor(function (_CpNfOuter, _Cryptpad, _Crypto, _Cryptget, _SFrameChannel, - _FilePicker, _Share, _Messaging, _Notifier, _Hash, _Util, _Realtime, + _SecureIframe, _Messaging, _Notifier, _Hash, _Util, _Realtime, _Constants, _Feedback, _LocalStore, _AppConfig, _Test, _UserObject) { CpNfOuter = _CpNfOuter; Cryptpad = _Cryptpad; Crypto = Utils.Crypto = _Crypto; Cryptget = _Cryptget; SFrameChannel = _SFrameChannel; - FilePicker = _FilePicker; - Share = _Share; + SecureIframe = _SecureIframe; Messaging = _Messaging; Notifier = _Notifier; Utils.Hash = _Hash; @@ -976,81 +973,55 @@ define([ onFileUpload(sframeChan, data, cb); }); - // File picker - var FP = {}; - var initFilePicker = function (cfg) { - // cfg.hidden means pre-loading the filepicker while keeping it hidden. + // Secure modal + var SecureModal = {}; + // Create or display the iframe and modal + var initSecureModal = function (type, cfg, cb) { + cfg.modal = type; + SecureModal.cb = cb; + // cfg.hidden means pre-loading the iframe while keeping it hidden. // if cfg.hidden is true and the iframe already exists, do nothing - if (!FP.$iframe) { + if (!SecureModal.$iframe) { var config = {}; - config.onFilePicked = function (data) { - sframeChan.event('EV_FILE_PICKED', data); + config.onAction = function (data) { + if (typeof(SecureModal.cb) !== "function") { return; } + SecureModal.cb(data); + SecureModal.$iframe.hide(); }; config.onClose = function () { - FP.$iframe.hide(); + SecureModal.$iframe.hide(); }; - config.onFileUpload = onFileUpload; - config.types = cfg; - config.addCommonRpc = addCommonRpc; - config.modules = { - Cryptpad: Cryptpad, - SFrameChannel: SFrameChannel, - Utils: Utils - }; - FP.$iframe = $('