Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keyboard shortcuts to add rows and columns on grid cell hover #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/components/area/AreaEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
:grayed="!isActive"
:focused="isFocused(section)"
@pointerdown="selectionEl.cellDown($event)"
@overcell="onOverCell"
@overgridcell="onOverGridCell"
@leavegridcell="onLeaveGridCell"
/>
<GridTrack
v-for="track in gridTracks"
Expand Down Expand Up @@ -354,6 +355,15 @@ function onOverCell({ row, col }) {
overArea.value = props.area
}

function onOverGridCell({ row, col }) {
onOverCell({ row, col })
if (overArea.value.display === 'grid') overArea.value.grid.hover = { row, col }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change this to be something like overArea: { area, cell: { row, coll } }. If you check the other hover or focused states, they don't modify the design state because that is included in undo/redo automatically. So all this focus info needs to be out of the mainArea state tree.

}

function onLeaveGridCell() {
if (overArea.value.display === 'grid') overArea.value.grid.hover = null
}

function isFocused(section) {
const c = currentHover.value
return c && c.on === 'cell' && c.grid === grid.value && c.row === section.row.start && c.col === section.col.start
Expand Down
5 changes: 3 additions & 2 deletions src/components/grid/GridCell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
}"
class="grid-section"
@pointerdown="$emit('pointerdown', $event)"
@mouseover="$emit('overcell', { col: section.col.start, row: section.row.start })"
@mouseover="$emit('overgridcell', { col: section.col.start, row: section.row.start })"
@mouseleave="$emit('leavegridcell')"
/>
</template>

Expand Down Expand Up @@ -71,7 +72,7 @@ const props = defineProps({
grayed: { type: Boolean, default: false },
focused: { type: Boolean, default: false },
})
defineEmit(['pointerdown', 'overcell'])
defineEmit(['pointerdown', 'overgridcell', 'overgridcell'])

import { computed } from 'vue'

Expand Down
4 changes: 2 additions & 2 deletions src/components/props/AreaGridTemplateProps.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="items">
<div class="items-header" title="Defines the track sizing functions of the grid columns.">
<h2>grid-template-columns</h2>
<OptionsButton class="add-button" @click="addCol(grid, '1fr')">add<span>+</span></OptionsButton>
<OptionsButton class="add-button" @click="addCol(props.area, '1fr')">add<span>+</span></OptionsButton>
</div>
<div v-for="column in colsNumber" :key="column" class="area-size">
<div
Expand Down Expand Up @@ -57,7 +57,7 @@
<div class="items">
<div class="items-header" title="Defines track sizing functions of the grid rows.">
<h2>grid-template-rows</h2>
<OptionsButton class="add-button" @click="addRow(grid, '1fr')">add<span>+</span></OptionsButton>
<OptionsButton class="add-button" @click="addRow(props.area, '1fr')">add<span>+</span></OptionsButton>
</div>
<div v-for="row in rowsNumber" :key="row" class="area-size">
<div
Expand Down
30 changes: 24 additions & 6 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,35 @@ export const isValidGapSize = isValidTrackSize

// This should go in grid.js, we need to check again if we can use sync:pre in the history management before

export function addToDimension(dimension, val) {
dimension.sizes.push(val)
export function addToDimension(dimension, index, val) {
dimension.sizes.splice(index, 0, val)
dimension.lineNames.push({ active: false, name: '' })
}

export function addCol(grid, colStr) {
addToDimension(grid.col, colStr)
export function addCol(area, colStr) {
const colIndex = area.grid.hover?.col || -1
addToDimension(area.grid.col, colIndex, colStr)
if (colIndex > 0)
area.children.forEach((child) => {
const [startRow, startCol, endRow, endCol] = child.gridArea.split(' / ')
if (colIndex < Number(startCol))
child.gridArea = `${startRow} / ${Number(startCol) + 1} / ${endRow} / ${Number(endCol) + 1}`
else if (colIndex < Number(endCol))
child.gridArea = `${startRow} / ${startCol} / ${endRow} / ${Number(endCol) + 1}`
})
}

export function addRow(grid, rowStr) {
addToDimension(grid.row, rowStr)
export function addRow(area, rowStr) {
const rowIndex = area.grid.hover?.row || -1
addToDimension(area.grid.row, rowIndex, rowStr)
if (rowIndex > 0)
area.children.forEach((child) => {
const [startRow, startCol, endRow, endCol] = child.gridArea.split(' / ')
if (rowIndex < Number(startRow))
child.gridArea = `${Number(startRow) + 1} / ${startCol} / ${Number(endRow) + 1} / ${endCol}`
else if (rowIndex < Number(endRow))
child.gridArea = `${startRow} / ${startCol} / ${Number(endRow) + 1} / ${endCol}`
})
}

function reduceLimit(l) {
Expand Down
10 changes: 4 additions & 6 deletions src/utils/keyMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
canRedo,
canUndo,
currentArea,
overArea,
deselectCurrentArea,
mainArea,
redo,
Expand Down Expand Up @@ -37,6 +38,7 @@ function ctrlMetaKeyHandler(event) {
}

function keyHandler(event) {
const targetArea = overArea?.value || currentArea.value
switch (event.key.toLowerCase()) {
case 'backspace':
case 'delete':
Expand All @@ -50,14 +52,10 @@ function keyHandler(event) {
}
break
case 'r':
if (currentArea?.value?.grid) {
addRow(currentArea.value.grid, '1fr')
}
if (targetArea.display === 'grid') addRow(targetArea, '1fr')
break
case 'c':
if (currentArea?.value?.grid) {
addCol(currentArea.value.grid, '1fr')
}
if (targetArea.display === 'grid') addCol(targetArea, '1fr')
break
case 'escape':
if (currentArea.value !== mainArea.value) {
Expand Down