Skip to content

Commit

Permalink
Merge pull request #5013 from dodona-edu/fix/copy-code
Browse files Browse the repository at this point in the history
Fix copying selected code without extra newlines
  • Loading branch information
jorg-vr authored Sep 28, 2023
2 parents f172203 + 2c3a4f0 commit 8402009
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/assets/javascripts/code_listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ function initAnnotateButtons(): void {
// make the whole document a valid target for dropping the create annotation button
document.addEventListener("dragover", e => e.preventDefault());
document.addEventListener("drop", e => e.preventDefault());

// copy only the selected code, this avoids copying the line numbers or extra whitespace from the complex html
document.addEventListener("copy", event => {
const selection = userAnnotationState.selectedRange;
if (!selection) {
return; // if there is no code selection, let the browser handle the copy event
}

const selectedCode = submissionState.code.split("\n").slice(selection.row - 1, selection.row + selection.rows - 1);
// on the first and last line, selection might only cover part of the line
// only copy the selected columns/characters
selectedCode[0] = selectedCode[0].slice(selection.column);
selectedCode[selectedCode.length - 1] = selectedCode[selectedCode.length - 1].slice(0, selection.columns);
event.clipboardData.setData("text/plain", selectedCode.join("\n"));
event.preventDefault();
});
}

function loadUserAnnotations(): void {
Expand Down

0 comments on commit 8402009

Please sign in to comment.