diff --git a/src/client/app/ipc/callbacks.js b/src/client/app/ipc/callbacks.js index fea964fa..edcecdc7 100644 --- a/src/client/app/ipc/callbacks.js +++ b/src/client/app/ipc/callbacks.js @@ -115,7 +115,7 @@ module.exports = { // disable editor's warning window.onbeforeunload = null - if (!session.session.type) { + if (!session.session) { window.location.href = window.location.href return } @@ -125,7 +125,7 @@ module.exports = { // store session & state backup ipc.send('storeBackup', { backupId: id, - session: session.session, + session: session.session || {}, state: state.get(), history: editor.history, historyState: editor.historyState, diff --git a/src/client/app/managers/session.js b/src/client/app/managers/session.js index 1db3c565..b2d823f5 100644 --- a/src/client/app/managers/session.js +++ b/src/client/app/managers/session.js @@ -14,9 +14,11 @@ var SessionManager = class SessionManager { constructor() { - this.session = [] + this.session = null this.lock = false - this.sessionPath = '' + this.setSessionPath('') + + this.windowTitle = document.title this.allowRemoteSave = false @@ -91,7 +93,7 @@ var SessionManager = class SessionManager { container.classList.add('show') this.lock = false editor.unsavedSession = false - this.sessionPath = '' + this.setSessionPath('') if (callback) callback() }, 25) @@ -101,7 +103,9 @@ var SessionManager = class SessionManager { save(path) { - if (path) this.sessionPath = path + if (!this.session) return + + if (path) this.setSessionPath(path) if (!this.sessionPath) return this.saveAs() @@ -114,6 +118,8 @@ var SessionManager = class SessionManager { saveAs() { + if (!this.session) return + remoteBrowse({extension: 'json', save:true}, (path)=>{ this.save(path) }) @@ -190,7 +196,7 @@ var SessionManager = class SessionManager { open(data) { this.load(data.session, ()=>{ - this.sessionPath = data.path; + this.setSessionPath(data.path) ipc.send('sessionOpened', {path: data.path}) }) @@ -222,6 +228,12 @@ var SessionManager = class SessionManager { }) } + setSessionPath(path) { + + this.sessionPath = path + + } + } var sessionManager = new SessionManager() diff --git a/src/client/app/ui/context-menu.js b/src/client/app/ui/context-menu.js new file mode 100644 index 00000000..526050ff --- /dev/null +++ b/src/client/app/ui/context-menu.js @@ -0,0 +1,132 @@ +var html = require('nanohtml'), + raw = require('nanohtml/raw'), + locales = require('../locales') + +class ContextMenu { + + constructor(){ + + this.menu = null + this.root = document.body + this.clickHandler = (e)=>{ + if (this.menu && !this.menu.contains(event.target)) this.close() + } + + } + + 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) + + if (action.click) { + + item.addEventListener('click', (e)=>{ + e.preventDefault() + action.action() + this.close() + }) + + } else { + + item.addEventListener('fast-click', (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) + }) + + } + + document.addEventListener('fast-click', this.clickHandler, true) + document.addEventListener('fast-right-click', this.clickHandler, true) + + } + + close() { + + if (this.menu) { + + this.menu.parentNode.removeChild(this.menu) + this.menu = null + + } + + document.removeEventListener('fast-click', this.clickHandler, true) + document.removeEventListener('fast-right-click', this.clickHandler, true) + } + + 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' + } + + } + +} + +module.exports = new ContextMenu() diff --git a/src/client/app/ui/utils.js b/src/client/app/ui/utils.js index 2d2c0657..75432004 100644 --- a/src/client/app/ui/utils.js +++ b/src/client/app/ui/utils.js @@ -256,12 +256,21 @@ module.exports = { files = data.files.filter(x=>x.folder || x.name.match(extRe)) - files.sort(function(a, b) { + + function alphaSort(a, b){ // convert to strings and force lowercase a = typeof a.name === 'string' ? a.name.toLowerCase() : a.name.toString(); b = typeof b.name === 'string' ? b.name.toLowerCase() : b.name.toString(); return a.localeCompare(b); - }) + } + + var fo = files.filter(x=>x.folder), + fi = files.filter(x=>!x.folder) + + fo.sort(alphaSort) + fi.sort(alphaSort) + + files = fo.concat(fi) files.unshift({ name: '..', diff --git a/src/server/callbacks.js b/src/server/callbacks.js index 6591f81e..9f610102 100644 --- a/src/server/callbacks.js +++ b/src/server/callbacks.js @@ -132,7 +132,7 @@ module.exports = { return ipc.send('notify', { icon: 'save', - locale: 'session_saveerror', + locale: 'session_save', class: 'error' }, clientId)