Skip to content

Commit

Permalink
selection improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gherkins committed Feb 25, 2024
1 parent 6743428 commit 5a65350
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function App () {
setContents(history.pop())
}

const resetSelection = () => {
const clearSelection = () => {
setSelectionFrom({ x: null, y: null })
setSelectionTo({ x: null, y: null })
}
Expand Down Expand Up @@ -86,15 +86,21 @@ function App () {
return (x + 1) * (y + 1)
}

const clearSelection = () => {
const deleteField = (col, row) => {
if (!contents[row] || !contents[row][col]) {
return false
}
contents[row][col] = ' '
}

const deleteFieldsInSelection = () => {
if (0 === getSelectionSize()) {
return false
}
const selection = getSelection()
for (let row = selection.from.y; row <= selection.to.y; row++) {
for (let col = selection.from.x; col <= selection.to.x; col++) {
contents[row] = contents[row] || {}
contents[row][col] = ' '
deleteField(col, row)
}
}
}
Expand Down Expand Up @@ -174,6 +180,14 @@ function App () {
cursorY = (cursorY + 1 < rows ? cursorY + 1 : rows - 1)
}

const setCursorToSelectionStart = () => {
const selection = getSelection()
if (null !== selection) {
cursorX = selection.from.x
cursorY = selection.from.y
}
}

const copyAllToClipboard = () => {
let text = ''
contents.forEach(row => {
Expand Down Expand Up @@ -205,7 +219,7 @@ function App () {

Mousetrap.bind(['up', 'down', 'left', 'right'], async e => {
e.preventDefault()
resetSelection()
clearSelection()
await handleCursorMovement(e.key)
})

Expand All @@ -225,8 +239,8 @@ function App () {
}
saveState()
copySelectionToBuffer()
deleteFieldsInSelection()
clearSelection()
resetSelection()
const [bufferStartY, bufferStartX] = getBufferStart()
const [bufferEndY, bufferEndX] = getBufferEnd()
const bufferWidth = bufferEndX - bufferStartX
Expand All @@ -243,13 +257,13 @@ function App () {

Mousetrap.bind('tab', e => {
e.preventDefault()
resetSelection()
clearSelection()
cursorX = (cursorX + 4 < cols ? cursorX + 4 : cols - 1)
})

Mousetrap.bind('shift+tab', e => {
e.preventDefault()
resetSelection()
clearSelection()
cursorX = (cursorX - 4 > 0 ? cursorX - 4 : 0)
})

Expand All @@ -267,22 +281,22 @@ function App () {

Mousetrap.bind('space', e => {
e.preventDefault()
resetSelection()
clearSelection()
if (contents[cursorY][cursorX] && contents[cursorY][cursorX] !== ' ') {
saveState()
contents[cursorY][cursorX] = ' '
deleteField(cursorX, cursorY)
}
moveCursorRight()
updateState({})
})

Mousetrap.bind('backspace', e => {
e.preventDefault()
resetSelection()
clearSelection()
moveCursorLeft()
if (contents[cursorY][cursorX] && contents[cursorY][cursorX] !== ' ') {
saveState()
contents[cursorY][cursorX] = ' '
deleteField(cursorX, cursorY)
}
updateState({})
})
Expand All @@ -295,12 +309,17 @@ function App () {
Mousetrap.bind('c', e => {
e.preventDefault()
copySelectionToBuffer()
setCursorToSelectionStart()
clearSelection()
updateState({})
})

Mousetrap.bind('x', e => {
e.preventDefault()
saveState()
copySelectionToBuffer()
deleteFieldsInSelection()
setCursorToSelectionStart()
clearSelection()
updateState({})
})
Expand All @@ -320,7 +339,7 @@ function App () {

Mousetrap.bind('z', e => {
e.preventDefault()
resetSelection()
clearSelection()
undo()
updateState({})
})
Expand Down

0 comments on commit 5a65350

Please sign in to comment.