This commit is contained in:
bordun 2020-03-05 22:32:38 +01:00
parent 75be4b7d71
commit c2ea1e0073
5 changed files with 149 additions and 114 deletions

View file

@ -1,106 +1,132 @@
var html = require('nanohtml'),
raw = require('nanohtml/raw'),
keyboardJS = require('keyboardjs'),
UiWidget = require('./ui-widget'),
mod = (navigator.platform || '').match('Mac') ? 'cmd' : 'ctrl',
MENU_CONTAINER
class ContextMenu extends UiWidget {
constructor(options){
constructor(options={}){
super(options)
if (!MENU_CONTAINER) MENU_CONTAINER = DOM.get('osc-workspace')[0]
this.container = null
this.container = html`<div class="context-menu"></div>`
this.clickHandler = (e)=>{
if (this.container && !this.container.contains(e.target)) this.close()
if (this.container && !this.container.contains(e.target)) {
this.close()
}
}
this.position = options.position
if (this.position) {
this.container.style.top = this.position[0] + 'rem'
this.container.style.left = this.position[1] + 'rem'
}
this.container.addEventListener('click', (e)=>{
e.preventDefault()
if (e.target._action){
e.target._action()
this.close()
}
})
this.hoverNode = null
this.container.addEventListener('mousemove', (e)=>{
if (this.hoverNode === e.target) return
this.hoverNode = e.target
if (!e.target.classList.contains('focus')) {
DOM.each(e.target.parentNode, '.focus', (el)=>{
el.classList.remove('focus')
})
e.target.classList.add('focus')
}
})
this.container.addEventListener('touchstart', (e)=>{
if (!e.target.classList.contains('focus')) {
DOM.each(e.target.parentNode, '.focus', (el)=>{
el.classList.remove('focus')
})
e.target.classList.add('focus')
} else {
e.target.classList.remove('focus')
DOM.each(e.target, '.focus', (el)=>{
el.classList.remove('focus')
})
}
})
}
open(e, actions, parent) {
var menu = html`<div class="context-menu"></div>`
parse(actions, node) {
for (let action of actions) {
if (action.separator) {
menu.appendChild(html`<div class="separator"></div>`)
node.appendChild(html`<div class="separator"></div>`)
} else if (Array.isArray(action.action)) {
let label = typeof action.label === 'function' ? action.label() : action.label,
let submenu = html`<div class="context-menu"></div>`,
label = typeof action.label === 'function' ? action.label() : action.label,
iclass = typeof action.class === 'function' ? action .class() : action.class,
item = html`<div class="item has-sub ${iclass}" tabIndex="1">${raw(label)}</div>`
item = html`<div class="item has-sub ${iclass || ''}" tabIndex="1">${raw(label)}</div>`
menu.appendChild(item)
item.appendChild(submenu)
node.appendChild(item)
this.open(e, action.action, item)
this.parse(action.action, submenu)
} else {
let label = typeof action.label === 'function' ? action.label() : action.label,
iclass = typeof action.class === 'function' ? action.class() : action.class,
item = html`<div class="item ${iclass}">${raw(label)}</div>`
item = html`<div class="item ${iclass || ''}">${raw(label)}</div>`
menu.appendChild(item)
item._action = action.action
node.appendChild(item)
// if (action.click) {
if (action.shortcut) {
item.addEventListener('click', (e)=>{
e.preventDefault()
action.action()
this.close()
})
item.appendChild(html`<div class="shortcut">${action.shortcut.replace('mod', mod)}</div>`)
// } else {
//
// item.addEventListener('fast-click', (e)=>{
// e.detail.preventOriginalEvent = true
// action.action()
// this.close()
// })
//
// }
}
}
}
if (parent) parent.appendChild(menu)
}
if (!parent) {
open(e, actions, parent) {
this.container = menu
this.container.innerHTML = ''
this.parse(actions, this.container)
MENU_CONTAINER.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 / PXSCALE + 'rem'
menu.style.left = e.pageX / PXSCALE + 'rem'
this.correctPosition(menu)
DOM.each(menu, '.context-menu', (m)=>{
this.correctPosition(m, m.parentNode)
})
MENU_CONTAINER.appendChild(this.container)
if (!this.position) {
this.container.style.top = e.pageY / PXSCALE + 'rem'
this.container.style.left = e.pageX / PXSCALE + 'rem'
}
this.correctPosition(this.container)
DOM.each(this.container, '.context-menu', (m)=>{
this.correctPosition(m, m.parentNode)
})
document.addEventListener('fast-click', this.clickHandler, true)
document.addEventListener('fast-right-click', this.clickHandler, true)
@ -113,13 +139,14 @@ class ContextMenu extends UiWidget {
if (this.container) {
this.container.parentNode.removeChild(this.container)
this.container = null
}
document.removeEventListener('fast-click', this.clickHandler, true)
document.removeEventListener('fast-right-click', this.clickHandler, true)
this.hoverNode = null
this.trigger('close')
}
@ -136,7 +163,7 @@ class ContextMenu extends UiWidget {
if (position.left - width > 0) {
menu.style.right = parent ? '100%' : '0'
menu.style.left = 'auto'
menu.style.marginRight = '1rem'
menu.style.marginRight = '-0.5rem'
} else {
menu.style.left = (parent.offsetWidth + (totalWidth - width - position.left)) / PXSCALE + 'rem'
}
@ -149,6 +176,27 @@ class ContextMenu extends UiWidget {
}
bindShortcuts(actions) {
for (let action of actions) {
if (Array.isArray(action.action)) {
this.bindShortcuts(action.action)
} else if (action.shortcut && action.action) {
keyboardJS.bind(action.shortcut, (e)=>{
e.preventDefault()
action.action()
})
}
}
}
}
module.exports = ContextMenu

View file

@ -59,7 +59,7 @@ class UiSidePanel extends UiWidget {
disable() {
this.container.classList.add('disabled')
this.disabled = false
this.disabled = true
this.close()
@ -70,7 +70,8 @@ class UiSidePanel extends UiWidget {
if (this.minimized) return
this.minimized = true
this.close()
if (!this.disabled) this.close()
}
@ -80,7 +81,8 @@ class UiSidePanel extends UiWidget {
if (!this.minimized) return
this.minimized = false
this.open()
if (!this.disabled) this.open()
}

View file

@ -13,6 +13,7 @@ var UiWidget = require('./ui-widget'),
stateManager = require('../managers/state'),
uiSidePanel = require('./ui-sidepanel')
var recentSessions = []
var menuEntries = [
@ -28,7 +29,8 @@ var menuEntries = [
},
{
label: locales('file_open'),
action: sessionManager.browse.bind(sessionManager)
action: sessionManager.browse.bind(sessionManager),
shortcut: 'mod + o'
},
{
label: locales('file_open_recent'),
@ -39,11 +41,13 @@ var menuEntries = [
},
{
label: locales('file_save'),
action: sessionManager.save.bind(sessionManager)
action: sessionManager.save.bind(sessionManager),
shortcut: 'mod + s'
},
{
label: locales('file_save_as'),
action: sessionManager.saveAs.bind(sessionManager)
action: sessionManager.saveAs.bind(sessionManager),
shortcut: 'mod + shift + s'
},
{
separator: true
@ -120,7 +124,8 @@ var menuEntries = [
action: ()=>{
if (editor.enabled) editor.disable()
else editor.enable()
}
},
shortcut: 'mod + e'
},
{
separator: true
@ -128,13 +133,15 @@ var menuEntries = [
{
label: locales('editor_grid'),
class: ()=>{return 'toggle ' + (editor.grid ? 'on' : 'off')},
action: editor.toggleGrid.bind(editor)
action: editor.toggleGrid.bind(editor),
// shortcut: 'mod + g'
},
{
label: locales('editor_tree'),
class: ()=>{return 'toggle ' + (uiSidePanel.left.minimized ? 'off' : 'on')},
action: ()=>{return uiSidePanel.left.minimized ? uiSidePanel.left.restore() : uiSidePanel.left.minimize()},
// shortcut: 'mod + t'
},
@ -142,6 +149,7 @@ var menuEntries = [
label: locales('editor_inspector'),
class: ()=>{return 'toggle ' + (uiSidePanel.right.minimized ? 'off' : 'on')},
action: ()=>{return uiSidePanel.right.minimized ? uiSidePanel.right.restore() : uiSidePanel.right.minimize()},
// shortcut: 'mod + i'
}
]
},
@ -153,7 +161,8 @@ var menuEntries = [
class: ()=>{return 'toggle ' + (fullscreen.isFullscreen ? 'on' : 'off')},
action: ()=>{
if (fullscreen.isEnabled) fullscreen.toggle()
}
},
shortcut: 'f11'
},
]
@ -180,16 +189,17 @@ if (navigator.userAgent.match(/Android|iPhone|iPad|iPod/i)) {
class UiToolbar extends UiWidget {
constructor(options) {
super(options)
this.menu = new ContextMenu()
this.menu = new ContextMenu({
position: [39, 1]
})
this.opened = false
this.menu.bindShortcuts(menuEntries)
ipc.on('sessionList', (data)=>{
if (recentSessions.length) {
@ -221,11 +231,7 @@ class UiToolbar extends UiWidget {
if (this.opened) return
this.menu.open({
target: e.detail.target,
pageX: e.detail.pageX - e.detail.offsetX + PXSCALE,
pageY: e.detail.pageY - e.detail.offsetY + e.detail.target.offsetHeight + 5 * PXSCALE
}, menuEntries)
this.menu.open(e, menuEntries)
this.opened = true
this.toggleState()