Skip to content

Commit

Permalink
copy/paste
Browse files Browse the repository at this point in the history
  • Loading branch information
gherkins committed Feb 21, 2024
1 parent 37d74f0 commit eec1f22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ up to 10 characters.

- Shift + Arrow keys: select blocks
- <del>Alt + Arrow keys: move selected block</del>
- <del>C: copy selected block</del>
- <del>X: cut selected block</del>
- <del>V: paste selected block</del>
- C: copy selected block
- X: cut selected block
- V: paste selected block

--------------------------------------
---
Expand Down
36 changes: 32 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ function App () {
}
}

const getSelectionSize = (selection) => {
const getSelectionSize = () => {
const selection = getSelection()
let x = selection.to.x - selection.from.x
let y = selection.to.y - selection.from.y
if (x > 0 || y > 0) {
Expand All @@ -60,11 +61,20 @@ function App () {
return x * y
}

const clearSelection = (block) => {

const clearSelection = () => {
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] = ' '
}
}
}

const copySelectionToBuffer = (block) => {
const copySelectionToBuffer = () => {
if (0 === getSelectionSize()) {
return false
}
buffer = {}
const selection = getSelection()
for (let row = selection.from.y; row <= selection.to.y; row++) {
Expand All @@ -77,7 +87,25 @@ function App () {
}

const applyBuffer = () => {
if (0 === getBufferSize()) {
return false
}
const bufferStartY = parseInt(Object.keys(buffer)[0])
const bufferStartX = parseInt(Object.keys(buffer[bufferStartY])[0])

const offsetX = cursorX - bufferStartX
const offsetY = cursorY - bufferStartY

for (let row in buffer) {
for (let col in buffer[row]) {
const newRow = parseInt(row) + offsetY
const newCol = parseInt(col) + offsetX
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
contents[newRow][newCol] = buffer[row][col]
}
}
}
updateState({})
}

const clearBuffer = () => {
Expand Down

0 comments on commit eec1f22

Please sign in to comment.