editor: add snap-to-grid feature, set grid's width via theming vars

This commit is contained in:
jean-emmanuel 2016-12-11 20:11:52 +01:00
parent b90e839590
commit 4f69a73d0f
6 changed files with 43 additions and 13 deletions

View file

@ -116,10 +116,11 @@ module.exports = {
},
editorEnable: function(){
GRIDWIDTH = getComputedStyle(document.documentElement).getPropertyValue("--grid-width")
$('.editor-root').attr('data-tab','#container').removeClass('disabled')
$('.enable-editor').addClass('on')
$('.disable-editor').removeClass('on')
$('body').addClass('editor-enabled')
EDITING = true
},
editorDisable: function(){
@ -130,7 +131,7 @@ module.exports = {
$('.editor-container').remove()
$('.disable-editor').addClass('on')
$('.enable-editor').removeClass('on')
$('body').removeClass('editor-enabled')
EDITING = false
},

View file

@ -121,6 +121,9 @@ var callbacks = module.exports = {
setTimeout(()=>{
$('canvas').trigger('resize')
},100)
GRIDWIDTH = getComputedStyle(document.documentElement).getPropertyValue("--grid-width")
PXSCALE = getComputedStyle(document.documentElement).getPropertyValue("--pixel-scale")
INITIALZOOM = PXSCALE
}
}

View file

@ -212,17 +212,26 @@ var editObject = function(container, data, refresh){
start: function(event, ui){
handleTarget = $(event.originalEvent.target)
},
resize: function(event, ui) {
ui.size.height = Math.round(ui.size.height / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE
ui.size.width = Math.round(ui.size.width / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE
},
stop: function( event, ui ) {
if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-s')) data.height = Math.max(ui.size.height,30)/PXSCALE
if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-e')) data.width = Math.max(ui.size.width,30)/PXSCALE
if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-s')) data.height = Math.round((Math.max(ui.size.height,30)/PXSCALE) / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
if (handleTarget.hasClass('ui-resizable-se') || handleTarget.hasClass('ui-resizable-e')) data.width = Math.round((Math.max(ui.size.width,30)/PXSCALE) / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
updateDom(container,data)
}
},
grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE]
})
}
if (data.hasOwnProperty('top')) {
container.draggable({
cursor:'-webkit-grabbing',
drag: function(event, ui) {
ui.position.top = Math.round(ui.position.top / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE
ui.position.left = Math.round(ui.position.left / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH * PXSCALE
},
stop: function( event, ui ) {
event.preventDefault()
data.top = (ui.helper.position().top + container.parent().scrollTop())/PXSCALE
@ -231,8 +240,7 @@ var editObject = function(container, data, refresh){
updateDom(container,data)
},
handle:'.ui-draggable-handle, > .label',
snap:'.widget',
snapTolerance:5,
grid: [GRIDWIDTH * PXSCALE, GRIDWIDTH * PXSCALE],
helper:function(){return $('<div class="ui-helper"></div>').css({height:container.outerHeight(),width:container.outerWidth()})}
}).append('<div class="ui-draggable-handle"></div>')
}

View file

@ -81,8 +81,8 @@ var init = function(){
if (!target.attr('data-tab')) {
newData.top = d.offsetY
newData.left= d.offsetX
newData.top = Math.round(d.offsetY / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
newData.left= Math.round(d.offsetX / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
} else {
delete newData.top
delete newData.left
@ -95,8 +95,8 @@ var init = function(){
data.widgets = data.widgets || []
var newData = JSON.parse(JSON.stringify(CLIPBOARD))
if (!target.attr('data-tab')) {
newData.top = d.offsetY
newData.left= d.offsetX
newData.top = Math.round(d.offsetY / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
newData.left= Math.round(d.offsetX / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
} else {
delete newData.top
delete newData.left
@ -113,8 +113,8 @@ var init = function(){
data.widgets = data.widgets || []
var newData = {type:widgetType}
if (!target.attr('data-tab')) {
newData.top = d.offsetY
newData.left= d.offsetX
newData.top = Math.round(d.offsetY / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
newData.left= Math.round(d.offsetX / (GRIDWIDTH * PXSCALE)) * GRIDWIDTH
}
data.widgets.push(newData)
updateDom(container,data)

View file

@ -288,3 +288,18 @@
cursor:-webkit-grabbing;
}
}
// GRID
.editor-enabled {
.tab:not(.has-tabs), .panel {
background-image: linear-gradient(to bottom, var(--color-grid) 1rem, transparent 1rem),
linear-gradient(to right, var(--color-grid) 1rem, transparent 1rem);
--size: calc(var(--grid-width) * calc(var(--grid-width) - 1) / calc(var(--grid-width) - 1) * 1rem);
background-size: var(--size) var(--size);
background-attachment: local;
background-position: -1rem -1rem;
}
}

View file

@ -12,6 +12,7 @@
--color-accent:#528bff;
--color-custom:var(--color-accent);
--color-grid: transparentize(#528bff, 0.9);
@ -21,6 +22,8 @@
--pixel-scale:1;
--grid-width:10;
/* Unset variables by default
--display-input:none; // Hides widgets' numeric inputs