remote-files: almost there

This commit is contained in:
jean-emmanuel 2019-03-31 17:27:48 +02:00
parent 3ab8ed92d4
commit 05ed6c21aa
5 changed files with 163 additions and 10 deletions

View file

@ -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,

View file

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

View file

@ -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`<div class="context-menu"></div>`
for (let action of actions) {
if (Array.isArray(action.action)) {
let item = html`<div class="item has-sub" tabIndex="1">${raw(action.label)}</div>`
menu.appendChild(item)
this.open(e, action.action, item)
} else {
let item = html`<div class="item">${raw(action.label)}</div>`
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()

View file

@ -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: '..',