Skip to content

Commit

Permalink
Merge pull request #403 from ryohey/fix-selection-move
Browse files Browse the repository at this point in the history
Fix selection copy/paste issue
  • Loading branch information
ryohey authored Sep 10, 2024
2 parents 45ff781 + bb1c1fd commit 1cadf7d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
46 changes: 26 additions & 20 deletions app/src/actions/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
PianoNotesClipboardData,
isPianoNotesClipboardData,
} from "../clipboard/clipboardTypes"
import { MaxNoteNumber } from "../Constants"
import { Rect } from "../entities/geometry/Rect"
import { Selection } from "../entities/selection/Selection"
import { NotePoint } from "../entities/transform/NotePoint"
Expand Down Expand Up @@ -96,6 +97,29 @@ export const moveSelectionBy =
return
}

const movedNotes = selectedNoteIds
.map((id) => {
const n = selectedTrack.getEventById(id)
if (n == undefined || !isNoteEvent(n)) {
return null
}
return {
id,
tick: n.tick + delta.tick,
noteNumber: n.noteNumber + delta.noteNumber,
}
})
.filter(isNotNull)

if (
movedNotes.some(
(n) => n.tick < 0 || n.noteNumber < 0 || n.noteNumber > MaxNoteNumber,
)
) {
// Do not move the note when it tries to go out of the screen
return
}

if (selection !== null) {
const movedSelection = Selection.moved(
selection,
Expand All @@ -110,24 +134,7 @@ export const moveSelectionBy =
pianoRollStore.selection = clampedSelection
}

selectedTrack.updateEvents(
selectedNoteIds
.map((id) => {
const n = selectedTrack.getEventById(id)
if (n == undefined || !isNoteEvent(n)) {
return null
}
const pos = NotePoint.clamp({
tick: n.tick + delta.tick,
noteNumber: n.noteNumber + delta.noteNumber,
})
return {
id,
...pos,
}
})
.filter(isNotNull),
)
selectedTrack.updateEvents(movedNotes)
}

export const updateSelectedNotes =
Expand Down Expand Up @@ -284,7 +291,6 @@ export const pasteSelection =
if (selectedTrack === undefined) {
return
}
// 現在位置にコピーしたノートをペースト
// Paste notes copied to the current position
const text = clipboard.readText()
if (!text || text.length === 0) {
Expand All @@ -299,7 +305,7 @@ export const pasteSelection =

const notes = obj.notes.map((note) => ({
...note,
tick: note.tick + player.position,
tick: Math.max(0, note.tick + player.position),
}))
selectedTrack.addEvents(notes)
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/track/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ export const validateMidiEvent = (e: TrackEvent) => {
}
}
})
if (e.tick < 0) {
console.warn("minus tick is not allowed in MIDI", e)
}
}

0 comments on commit 1cadf7d

Please sign in to comment.