diff --git a/src/browser/js/app/editor/context-menu.js b/src/browser/js/app/editor/context-menu.js index 44633183..088f940a 100644 --- a/src/browser/js/app/editor/context-menu.js +++ b/src/browser/js/app/editor/context-menu.js @@ -1,5 +1,4 @@ var {updateWidget, incrementWidget} = require('./data-workers'), - {Popup} = require('../ui/utils'), {categories} = require('../widgets/'), widgetManager = require('../managers/widgets'), editor = require('./') @@ -182,28 +181,9 @@ var handleClick = function(event) { if (type === 'widget') { - actions[' Copy'] = ()=>{ - editor.clipboard = JSON.stringify(data) - if (data.length == 1) { - editor.idClipboard = editor.selectedWidgets[0].getProp('id') - } else { - editor.idClipboard =null - } - } + actions[' Copy'] = editor.copyWidget.bind(editor) - actions[' Cut'] = ()=>{ - editor.clipboard = JSON.stringify(data) - if (data.length == 1) { - editor.idClipboard = editor.selectedWidgets[0].getProp('id') - } else { - editor.idClipboard =null - } - - for (var i of index) { - parent.props.widgets.splice(i,1) - } - editor.select(updateWidget(parent, {preventSelect: true})) - } + actions[' Cut'] = editor.cutWidget.bind(editor) actions[' Wrap in'] = {} for (let c of categories.Containers) { @@ -242,66 +222,18 @@ var handleClick = function(event) { if (editor.clipboard !== null) { - var paste = function(increment) { - - var pastedData = JSON.parse(editor.clipboard), - minTop = Infinity, - minLeft = Infinity - - for (var i in pastedData) { - if (increment) pastedData[i] = incrementWidget(pastedData[i]) - if (!isNaN(pastedData[i]).top && pastedData[i].top < minTop) { - minTop = pastedData[i].top - } - if (!isNaN(pastedData[i]).left && pastedData[i].left < minLeft) { - minLeft = pastedData[i].left - } - } - - var keepPosition = eventData.target.classList.contains('tablink') - for (let i in pastedData) { - - if (!keepPosition) { - if (!isNaN(pastedData[i].top)) pastedData[i].top = pastedData[i].top - minTop + clickY - if (!isNaN(pastedData[i].left)) pastedData[i].left = pastedData[i].left - minLeft + clickX - } - - } - - data[0].widgets = data[0].widgets || [] - data[0].widgets = data[0].widgets.concat(pastedData) - - updateWidget(editor.selectedWidgets[0]) - - } - actions[' Paste'] = { ' Paste': ()=>{ - paste() + editor.pasteWidget(eventData) }, ' ID + 1': ()=>{ - paste(true) + editor.pasteWidget(eventData, true) } } if (editor.idClipboard && widgetManager.getWidgetById(editor.idClipboard).length) { actions[' Paste'][' Clone'] = ()=>{ - var clone = {type: 'clone', widgetId: editor.idClipboard}, - pastedData = JSON.parse(editor.clipboard) - - clone.width = pastedData.width - clone.height = pastedData.width - clone.css = pastedData.css - - if (!eventData.target.classList.contains('tablink')) { - clone.top = clickY - clone.left = clickX - } - - data[0].widgets = data[0].widgets || [] - data[0].widgets.push(clone) - - updateWidget(editor.selectedWidgets[0]) + editor.pasteWidgetAsClone(eventData) } } @@ -347,43 +279,7 @@ var handleClick = function(event) { } - actions[' Delete'] = ()=>{ - - var popup = new Popup({ - title: 'Are you sure ?', - content:` -
`, - closable: false, - escKey: true, - enterKey: function(){DOM.get(this.html, '.confirm-delete')[0].click()} - }) - - DOM.get(popup.html, '.confirm-delete')[0].addEventListener('click', ()=>{ - - popup.close() - - if (type === 'widget') { - for (let i of index) { - parent.props.widgets.splice(i,1) - } - } else { - for (let i of index) { - parent.props.tabs.splice(i,1) - } - } - - editor.select(updateWidget(parent, {preventSelect: true})) - - }) - - DOM.get(popup.html, '.cancel-delete')[0].addEventListener('click', function(){ - popup.close() - }) - - } + actions[' Delete'] = editor.deleteWidget.bind(editor) contextMenu.open(eventData, actions) diff --git a/src/browser/js/app/editor/index.js b/src/browser/js/app/editor/index.js index 41dffbda..0685103d 100644 --- a/src/browser/js/app/editor/index.js +++ b/src/browser/js/app/editor/index.js @@ -1,6 +1,8 @@ var {widgets} = require('../widgets/'), + {Popup} = require('../ui/utils'), editField = require('./edit-field'), - {updateWidget} = require('./data-workers') + {updateWidget, incrementWidget} = require('./data-workers'), + keyboardJS = require('keyboardjs') var Editor = class Editor { @@ -30,6 +32,57 @@ var Editor = class Editor { if (this.enabledOnce) return true } + keyboardJS.withContext('editing', ()=>{ + keyboardJS.bind('mod + c', (e)=>{ + if (e.target.classList.contains('input')) return + this.copyWidget() + }) + keyboardJS.bind('mod + x', (e)=>{ + if (e.target.classList.contains('input')) return + this.cutWidget() + }) + keyboardJS.bind('mod + v', (e)=>{ + if (e.target.classList.contains('input')) return + this.pasteWidget(undefined, false) + }) + keyboardJS.bind('mod + shift + v', ()=>{ + if (e.target.classList.contains('input')) return + this.pasteWidget(undefined, true) + }) + keyboardJS.bind('delete', (e)=>{ + if (e.target.classList.contains('input')) return + this.deleteWidget() + }) + keyboardJS.bind(['alt + up', 'alt + down', 'alt + right', 'alt + left'], (e)=>{ + if (e.target.classList.contains('input')) return + + var deltaW = e.key === 'ArrowLeft' ? -GRIDWIDTH : e.key === 'ArrowRight' ? GRIDWIDTH : 0, + deltaH = e.key === 'ArrowUp' ? -GRIDWIDTH : e.key === 'ArrowDown' ? GRIDWIDTH : 0 + + if (e.shiftKey) { + deltaW *= 5 + deltaH *= 5 + } + + this.resizeWidget(deltaW, deltaH) + }) + keyboardJS.bind(['up', 'down', 'right', 'left'], (e)=>{ + if (e.target.classList.contains('input')) return + + var deltaX = e.key === 'ArrowLeft' ? -GRIDWIDTH : e.key === 'ArrowRight' ? GRIDWIDTH : 0, + deltaY = e.key === 'ArrowUp' ? -GRIDWIDTH : e.key === 'ArrowDown' ? GRIDWIDTH : 0 + + if (e.shiftKey) { + deltaX *= 5 + deltaY *= 5 + } + + this.moveWidget(deltaX, deltaY) + }) + + + }) + } enable() { @@ -47,7 +100,7 @@ var Editor = class Editor { document.body.classList.toggle('no-grid', GRIDWIDTH == 1) - GRIDWIDTH = getComputedStyle(document.documentElement).getPropertyValue('--grid-width') + GRIDWIDTH = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--grid-width')) var gridForm = DOM.create(`