From 2d8b118157706073ec9374a7056cc0ab969d1696 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 31 Mar 2019 15:56:39 +0200 Subject: [PATCH] remote-files: continue wip, add context menu for import/export/save-as actions --- src/client/app/editor/context-menu.js | 123 +------------------------- src/client/app/locales/en.js | 7 +- src/client/app/locales/fr.js | 8 +- src/client/app/managers/session.js | 29 ++++-- src/client/app/ui/sidepanel.js | 80 +++++++++++------ src/client/app/ui/utils.js | 74 ++++++++++++---- src/scss/index.scss | 12 +++ src/scss/sidepanel.scss | 10 ++- src/server/callbacks.js | 101 ++++++++++++--------- 9 files changed, 228 insertions(+), 216 deletions(-) diff --git a/src/client/app/editor/context-menu.js b/src/client/app/editor/context-menu.js index 7264ea96..48d23c66 100644 --- a/src/client/app/editor/context-menu.js +++ b/src/client/app/editor/context-menu.js @@ -1,133 +1,18 @@ var {updateWidget} = require('./data-workers'), {categories} = require('../widgets/'), widgetManager = require('../managers/widgets'), - html = require('nanohtml'), - raw = require('nanohtml/raw'), {icon} = require('../ui/utils'), editor = require('./'), - locales = require('../locales') + locales = require('../locales'), + contextMenu = require('../ui/context-menu') var multiSelectKey = (navigator.platform || '').match('Mac') ? 'metaKey' : 'ctrlKey' - -class ContextMenu { - - constructor(){ - - this.menu = null - this.event = 'fast-click' - this.root = document.body - - } - - open(e, actions, parent) { - - var menu = html`
` - - for (let action of actions) { - - if (Array.isArray(action.action)) { - - let item = html`
${raw(action.label)}
` - - menu.appendChild(item) - - this.open(e, action.action, item) - - - } else { - - let item = html`
${raw(action.label)}
` - - menu.appendChild(item) - - item.addEventListener(this.event, (e)=>{ - e.detail.preventOriginalEvent = true - action.action() - this.close() - }) - - } - - } - - if (parent) parent.appendChild(menu) - - if (!parent) { - - this.menu = menu - - this.root.appendChild(menu) - - DOM.each(menu, '.item', (item)=>{ - item.addEventListener('mouseenter', ()=>{ - DOM.each(item.parentNode, '.focus', (focused)=>{ - focused.classList.remove('focus') - }) - item.classList.add('focus') - }) - item.addEventListener('mouseleave', ()=>{ - if (!item.classList.contains('has-sub')) item.classList.remove('focus') - }) - }) - - menu.style.top = e.pageY + 'px' - menu.style.left = e.pageX + 'px' - - this.correctPosition(menu) - - DOM.each(menu, '.context-menu', (m)=>{ - this.correctPosition(m, m.parentNode) - }) - - } - - - } - - close() { - - if (this.menu) { - - this.menu.parentNode.removeChild(this.menu) - this.menu = null - - } - - } - - correctPosition(menu, parent) { - - var position = DOM.offset(menu), - width = menu.offsetWidth, - height = menu.offsetHeight, - totalWidth = this.root.offsetWidth, - totalHeight = this.root.offsetHeight - - if (width + position.left > totalWidth) { - menu.style.right = parent ? '100%' : '0' - menu.style.left = 'auto' - menu.style.marginRight = '2rem' - } - - if (height + position.top > totalHeight) { - menu.style.top = 'auto' - menu.style.bottom = '2rem' - } - - } - -} - -var contextMenu = new ContextMenu() - var handleClick = function(event) { if (!EDITING) return - if (contextMenu.menu && !contextMenu.menu.contains(event.target)) contextMenu.close() - if (!event.detail[multiSelectKey] && event.type !== 'fast-right-click' && ( event.target.classList.contains('ui-resizable-handle') || event.target.classList.contains('ui-draggable-handle') || @@ -361,5 +246,5 @@ var handleClick = function(event) { } -document.addEventListener('fast-right-click', handleClick, true) -document.addEventListener('fast-click', handleClick, true) +document.body.addEventListener('fast-right-click', handleClick, true) +document.body.addEventListener('fast-click', handleClick, true) diff --git a/src/client/app/locales/en.js b/src/client/app/locales/en.js index 52a43128..236e2662 100644 --- a/src/client/app/locales/en.js +++ b/src/client/app/locales/en.js @@ -4,9 +4,12 @@ module.exports = { editor_on: 'On', editor_off: 'Off', editor_root: 'Root', - editor_load: 'Load', + editor_load: 'Open', editor_save: 'Save', - editor_save_as: 'Export', + editor_save_as: 'Save as...', + editor_import: 'Import', + editor_export: 'Export', + // editor context-menu editor_editparent: 'Edit parent', diff --git a/src/client/app/locales/fr.js b/src/client/app/locales/fr.js index 794d8c44..4137d735 100644 --- a/src/client/app/locales/fr.js +++ b/src/client/app/locales/fr.js @@ -4,9 +4,11 @@ module.exports = { editor_on: 'On', editor_off: 'Off', editor_root: 'Root', - editor_load: 'Charger', - editor_save: 'Sauver', - editor_save_as: 'Exporter', + editor_load: 'Ouvrir', + editor_save: 'Enregistrer', + editor_save_as: 'Enregistrer sous...', + editor_import: 'Importer', + editor_export: 'Exporter', // editor context-menu editor_editparent: 'Éditer parent', diff --git a/src/client/app/managers/session.js b/src/client/app/managers/session.js index 7a7405f2..1db3c565 100644 --- a/src/client/app/managers/session.js +++ b/src/client/app/managers/session.js @@ -99,14 +99,29 @@ var SessionManager = class SessionManager { } - save() { + save(path) { - ipc.send('sessionSave', JSON.stringify(this.session, null, ' ')) + if (path) this.sessionPath = path + + if (!this.sessionPath) return this.saveAs() + + ipc.send('sessionSave', { + session: JSON.stringify(this.session, null, ' '), + path: this.sessionPath + }) } saveAs() { + remoteBrowse({extension: 'json', save:true}, (path)=>{ + this.save(path) + }) + + } + + export() { + var sessionfile = JSON.stringify(this.session,null,' '), blob = new Blob([sessionfile],{type : 'application/json'}) @@ -175,7 +190,7 @@ var SessionManager = class SessionManager { open(data) { this.load(data.session, ()=>{ - this.sessionPath = data.path + this.sessionPath = data.path; ipc.send('sessionOpened', {path: data.path}) }) @@ -183,10 +198,14 @@ var SessionManager = class SessionManager { browse() { - remoteBrowse('json', (path)=>{ + remoteBrowse({extension: 'json'}, (path)=>{ ipc.send('sessionOpen',{path:path}) }) - return + + } + + import() { + upload('.json', (path, result)=>{ ipc.send('sessionOpen',{file:result, path:path}) }, ()=>{ diff --git a/src/client/app/ui/sidepanel.js b/src/client/app/ui/sidepanel.js index 7420f1d4..e3b1095a 100644 --- a/src/client/app/ui/sidepanel.js +++ b/src/client/app/ui/sidepanel.js @@ -7,13 +7,14 @@ var state = require('../managers/state'), {enableTraversingGestures, disableTraversingGestures} = require('../events/drag'), locales = require('../locales'), html = require('nanohtml'), - raw = require('nanohtml/raw') + raw = require('nanohtml/raw'), + contextMenu = require('./context-menu') var sidepanelData = [ { actions: [ { - title: locales('sidepanel_fs') + (ELECTRON_FULLSCREEN ? ' (F11)' : ''), + label: locales('sidepanel_fs') + (ELECTRON_FULLSCREEN ? ' (F11)' : ''), action:()=>{ if (fullscreen.enabled) fullscreen.toggle() }, @@ -23,10 +24,10 @@ var sidepanelData = [ ] }, { - title: icon('sliders-h') + '  ' + locales('sidepanel_state'), + label: icon('sliders-h') + '  ' + locales('sidepanel_state'), actions: [ { - title: locales('state_store'), + label: locales('state_store'), action:()=>{ state.quickSave() DOM.get('.quickload')[0].classList.remove('disabled') @@ -37,7 +38,7 @@ var sidepanelData = [ } }, { - title: locales('state_recall'), + label: locales('state_recall'), action: ()=>{ state.quickLoad() notifications.add({ @@ -48,7 +49,7 @@ var sidepanelData = [ class:'disabled quickload' }, { - title: locales('state_send'), + label: locales('state_send'), action: ()=>{ state.send() notifications.add({ @@ -58,20 +59,20 @@ var sidepanelData = [ } }, { - title: locales('state_import'), + label: locales('state_import'), action:state.load.bind(state) }, { - title: locales('state_export'), + label: locales('state_export'), action:state.save.bind(state) } ] }, { - title: icon('magic') + '  ' + locales('sidepanel_traversing'), + label: icon('magic') + '  ' + locales('sidepanel_traversing'), actions: [ { - title: locales('traversing_on'), + label: locales('traversing_on'), action:(el)=>{ DOM.each(el.parentNode, 'a', (el)=>{ el.classList.remove('on') @@ -84,7 +85,7 @@ var sidepanelData = [ class:'traversingEnable' }, { - title: locales('traversing_smart'), + label: locales('traversing_smart'), action:(el)=>{ DOM.each(el.parentNode, 'a', (el)=>{ el.classList.remove('on') @@ -97,7 +98,7 @@ var sidepanelData = [ class:'traversingSmart' }, { - title: locales('traversing_off'), + label: locales('traversing_off'), action:(el)=>{ DOM.each(el.parentNode, 'a', (el)=>{ el.classList.remove('on') @@ -110,34 +111,55 @@ var sidepanelData = [ ] }, { - title: icon('edit') + '  ' + locales('sidepanel_editor'), + label: icon('edit') + '  ' + locales('sidepanel_editor'), class:'editor-menu', actions: [ { - title: locales('editor_on'), + label: locales('editor_on'), action:editor.enable.bind(editor), class:'enable-editor' }, { - title: locales('editor_off'), + label: locales('editor_off'), action:editor.disable.bind(editor), class:'on disable-editor' }, { - title: locales('editor_root'), - class:'editor-root disabled' + label: raw(icon('vector-square')), + class:'editor-root disabled narrow', + title: locales('editor_root') }, { - title: locales('editor_load'), + label: locales('editor_load'), action:session.browse.bind(session) }, { - title: locales('editor_save'), - action:session.save.bind(session) + label: locales('editor_save'), + action:()=>{ + session.save() + } }, { - title: locales('editor_save_as'), - action:session.saveAs.bind(session) + label: raw(icon('ellipsis-h')), + class: 'narrow', + menu: [ + { + label: locales('editor_save_as'), + action:()=>{ + session.saveAs() + } + }, + { + label: locales('editor_import'), + click: true, + action: session.import + }, + { + label: locales('editor_export'), + click: true, + action: session.export + } + ] } ] }, @@ -151,7 +173,7 @@ if (navigator.userAgent.match(/Android|iPhone|iPad|iPod/i)) { noSleepState = false sidepanelData[0].actions.push({ - title: locales('sidepanel_nosleep'), + label: locales('sidepanel_nosleep'), class: 'toggle', action:(el)=>{ noSleepState = el.classList.toggle('on') @@ -176,7 +198,7 @@ for (let i in sidepanelData) {
  • - ${data.title ? html`
    ${raw(data.title)}
    ` : ''} + ${data.label ? html`
    ${raw(data.label)}
    ` : ''}
  • @@ -186,10 +208,18 @@ for (let i in sidepanelData) { for (let j in data.actions) { let actionData = data.actions[j], - element = html`${actionData.title}` + element = html`${actionData.label}` if (actionData.action) element.addEventListener('click', ()=>{actionData.action(element)}) + if (actionData.menu) { + + element.addEventListener('fast-click', (e)=>{ + contextMenu.open(event.detail, actionData.menu) + }) + + } + wrapper.appendChild(element) } diff --git a/src/client/app/ui/utils.js b/src/client/app/ui/utils.js index 12dba3e6..2d2c0657 100644 --- a/src/client/app/ui/utils.js +++ b/src/client/app/ui/utils.js @@ -143,11 +143,14 @@ module.exports = { uploadSingleton.click() }, - remoteBrowse: function(ext='json', callback) { + remoteBrowse: function(options, callback) { + + var save = options.save, + saveInputFocus = undefined var popup = new module.exports.Popup({ closable: true, - title: `Open file`, + title: save ? `Save file` : `Open file`, hide: true }) @@ -155,16 +158,40 @@ module.exports = { browser = html`
    `, ariane = html`
    `, list = html`
    `, + saveInput = html``, actions = html`
    + ${ + save ? saveInput : '' + }
    ${raw(module.exports.icon('times'))} Cancel
    -
    ${raw(module.exports.icon('folder-open'))} Open
    + ${ + save ? html`
    ${raw(module.exports.icon('save'))} Save
    ` : + html`
    ${raw(module.exports.icon('folder-open'))} Open
    ` + }
    ` var files = [], path = '' + var extRe = options.extension ? new RegExp('.*\\.' + options.extension) : /.*/ + + if (save) { + saveInput.addEventListener('change', ()=>{ + if (!saveInput.value.match(extRe)) saveInput.value += '.' + options.extension + }) + saveInput.addEventListener('focus', ()=>{ + saveInputFocus = true + }) + list.addEventListener('change', ()=>{ + saveInputFocus = false + var choice = DOM.get(list, ':checked')[0] + if (choice && choice.classList.contains('file')) { + saveInput.value = choice.value + } + }) + } doubleTap(list, submit) @@ -172,30 +199,41 @@ module.exports = { var choice = DOM.get(list, ':checked')[0] - if (!choice) return + if (!choice && (!save || !saveInput.value)) return - if (choice.classList.contains('folder')) { + if (choice && choice.classList.contains('folder') && (!save || !saveInputFocus)) { ipc.send('listDir', {path: [path, choice.value]}) } else { - callback([path, choice.value]) - popup.close() + if (save) { + if (files.some(x=>x.name===saveInput.value)) { + if (!confirm('Overwrite ?')) return + } + callback([path, saveInput.value]) + popup.close() + } else { + callback([path, choice.value]) + popup.close() + } } } actions.addEventListener('click', (e)=>{ - if (e.target.classList.contains('open')) { + if (e.target.classList.contains('submit')) { submit() - } else { + } else if ((e.target.classList.contains('cancel'))){ popup.close() } }) function keyHandler(e){ + if (e.keyCode === 13) { - submit() + setTimeout(submit) + } else if (e.target === saveInput) { + return } else if (e.keyCode === 8) { - list.childNodes[0].firstElementChild.click() + list.childNodes[0].firstElementChild.checked = true submit() } else if (e.key.length === 1) { var letter = e.key.toLowerCase() @@ -216,11 +254,7 @@ module.exports = { ariane.innerHTML = path = data.path - if (ext) { - if (!Array.isArray(ext)) ext = [ext] - var filter = new RegExp('.*\\.(' + ext.join('|') + ')') - files = data.files.filter(x=>x.folder || x.name.match(filter)) - } + files = data.files.filter(x=>x.folder || x.name.match(extRe)) files.sort(function(a, b) { // convert to strings and force lowercase @@ -239,7 +273,7 @@ module.exports = { var file = files[i] list.appendChild(html`
    - +
    ${raw(module.exports.icon(file.folder ? 'folder' : 'file-alt'))} ${file.name}
    `) @@ -254,7 +288,11 @@ module.exports = { popup.open() - list.childNodes[0].firstElementChild.focus() + if (save && saveInputFocus === undefined) { + saveInput.focus() + } else { + list.childNodes[0].firstElementChild.focus() + } }, {context: popup}) diff --git a/src/scss/index.scss b/src/scss/index.scss index 05217c66..dfac05eb 100644 --- a/src/scss/index.scss +++ b/src/scss/index.scss @@ -670,9 +670,21 @@ body { div { margin-left: 10rem; text-transform: uppercase; + text-overflow: clip; } i { pointer-events: none; } + .save-as { + flex:1; + height: 40rem; + line-height: 40rem; + border: 0; + padding: 0 10rem; + font-size: 120%; + top: -1rem; + position: relative; + background: var(--color-bg); + } } } diff --git a/src/scss/sidepanel.scss b/src/scss/sidepanel.scss index 0be5da73..6752e370 100644 --- a/src/scss/sidepanel.scss +++ b/src/scss/sidepanel.scss @@ -98,7 +98,9 @@ } a { - flex: 4 1 32%; + // flex: 4 1 32%; + flex:1; + min-width: 32%; padding:13rem 10rem; display: block; text-align: center; @@ -106,6 +108,12 @@ color:var(--color-text); overflow: visible; margin: 0.5rem; + &.narrow { + width: auto; + min-width: 40rem; + flex:0; + } + &.on { z-index: 0; diff --git a/src/server/callbacks.js b/src/server/callbacks.js index 7ad897ec..6591f81e 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -68,6 +68,14 @@ module.exports = { } }, + sessionSetPath: function(data, clientId) { + + if (Array.isArray(data.path)) path = path.resolve(...data.path) + + ipc.clients[clientId].sessionPath = data.path + + }, + sessionOpen: function(data,clientId) { if (Array.isArray(data.path)) data.path = path.resolve(...data.path) @@ -95,6 +103,8 @@ module.exports = { sessionOpened: function(data, clientId) { + if (Array.isArray(data.path)) data.path = path.resolve(...data.path) + if (settings.read('stateFile')) { var send = true for (var id in ipc.clients) { @@ -112,59 +122,64 @@ module.exports = { ipc.send('stateSend', null, null, clientId) - module.exports.sessionSetPath(data, clientId) - - }, - - sessionSetPath: function(data, clientId) { - - ipc.clients[clientId].sessionPath = '' - - if (!data.path || settings.read('remoteSaving') && !settings.read('remoteSaving').test(ipc.clients[clientId].address)) { - return - } - - fs.lstat(data.path, (err, stats)=>{ - if (err || !stats.isFile()) return - ipc.clients[clientId].sessionPath = data.path - if (!settings.read('readOnly')) { - this.sessionAddToHistory(data.path) - } - }) + module.exports.sessionSetPath({path: data.path}, clientId) }, sessionSave: function(data, clientId) { - var path = ipc.clients[clientId].sessionPath, - error + if (!data.path || settings.read('remoteSaving') && !settings.read('remoteSaving').test(ipc.clients[clientId].address)) { - if (path) { - - fs.writeFile(path, data, function(err, data) { - if (err) return error = err - console.log('Session file saved in '+ path) - ipc.send('sessionSaved', clientId) - ipc.send('notify', { - icon: 'save', - locale: 'session_savesuccess' - }, clientId) - for (var id in ipc.clients) { - if (id !== clientId && ipc.clients[id].sessionPath === path) { - module.exports.sessionOpen({path: path}, id) - } - } - }) + return ipc.send('notify', { + icon: 'save', + locale: 'session_saveerror', + class: 'error' + }, clientId) } - if (!path || error) { + if (data.path) { - ipc.send('notify', { - class: 'error', - locale: 'session_saveerror', - message: error - }, clientId) + if (Array.isArray(data.path)) data.path = path.resolve(...data.path) + + try { + JSON.parse(data.session) + } catch(e) { + throw 'Could not save: invalid session file' + return + } + + fs.writeFile(data.path, data.session, function(err, fdata) { + + if (err) return ipc.send('notify', { + class: 'error', + locale: 'session_saveerror', + message: err + }, clientId) + + + console.log('Session file saved in '+ data.path) + ipc.send('sessionSaved', clientId) + ipc.send('notify', { + icon: 'save', + locale: 'session_savesuccess', + message: ' ('+ data.path +')' + }, clientId) + + // reload session in other clients + for (var id in ipc.clients) { + if (id !== clientId && ipc.clients[id].sessionPath === data.path) { + module.exports.sessionOpen({path: data.path}, id) + } + } + + if (!settings.read('readOnly')) { + module.exports.sessionAddToHistory(data.path) + } + + module.exports.sessionSetPath({path: data.path}, clientId) + + }) }