Skip to content

Commit

Permalink
Copy whole current line(s) when selection(s) are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Jan 25, 2024
1 parent 91f80ff commit f384579
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
23 changes: 20 additions & 3 deletions src/editor/copy-paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,26 @@ const blockSeparatorRegex = new RegExp(`\\n∞∞∞(${languageTokensMatcher})(-

function copiedRange(state) {
let content = [], ranges = []
for (let range of state.selection.ranges) if (!range.empty) {
content.push(state.sliceDoc(range.from, range.to))
ranges.push(range)
for (let range of state.selection.ranges) {
if (!range.empty) {
content.push(state.sliceDoc(range.from, range.to))
ranges.push(range)
}
}
if (ranges.length == 0) {
// if all ranges are empty, we want to copy each whole (unique) line for each selection
const copiedLines = []
for (let range of state.selection.ranges) {
if (range.empty) {
const line = state.doc.lineAt(range.head)
const lineContent = state.sliceDoc(line.from, line.to)
if (!copiedLines.includes(line.from)) {
content.push(lineContent)
ranges.push(range)
copiedLines.push(line.from)
}
}
}
}
return { text: content.join(state.lineBreak), ranges }
}
Expand Down
65 changes: 59 additions & 6 deletions tests/emacs-clipboard-keys.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { test, expect } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";

let heynotePage
let heynotePage, modifierKey

test.beforeEach(async ({ page }) => {
test.beforeEach(async ({ page, browserName }) => {
heynotePage = new HeynotePage(page)
await heynotePage.goto()
});

modifierKey = heynotePage.isMac ? "Meta" : "Control"

test("test emacs copy/pase/cut key bindings", async ({ page, browserName }) => {
if (browserName !== "chromium") {
// This test only works in Chromium due to accessing the clipboard
test.skip()
Expand All @@ -21,15 +19,18 @@ test("test emacs copy/pase/cut key bindings", async ({ page, browserName }) => {
await page.locator("css=select.metaKey").selectOption("alt")
}
await page.locator("body").press("Escape")
});


test("test emacs copy/pase/cut key bindings", async ({ page }) => {
await page.locator("body").pressSequentially("test")
await page.locator("body").press("Control+Space")
await page.locator("body").press("Control+A")
await page.locator("body").press("Alt+W")
expect(await heynotePage.getBlockContent(0)).toBe("test")
await page.locator("body").press("Control+Y")
expect(await heynotePage.getBlockContent(0)).toBe("testtest")

await page.locator("body").press("Control+E")
await page.locator("body").press("Control+Space")
await page.locator("body").press("Control+A")
Expand All @@ -39,3 +40,55 @@ test("test emacs copy/pase/cut key bindings", async ({ page, browserName }) => {
await page.locator("body").press("Control+Y")
expect(await heynotePage.getBlockContent(0)).toBe("testtesttesttest")
})


// The following tests doesn't really test anything specific to the Emacs key bindings, but triggering
// Copy/Paste by keyboard shortcuts is not possible in Playwright, so we test this functionality using
// the Emacs key bindings

test("copy current line", async ({ page }) => {
await page.locator("body").pressSequentially("test line! ")
await page.keyboard.press("Alt+W")
await page.keyboard.press("Alt+W")
await page.keyboard.press(`${modifierKey}+A`)
await page.keyboard.press(`Backspace`)
expect(await heynotePage.getBlockContent(0)).toBe("")

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50

Check failure on line 55 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line

1) [chromium] › emacs-clipboard-keys.spec.js:49:5 › copy current line ──────────────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "test line! " 53 | await page.keyboard.press(`${modifierKey}+A`) 54 | await page.keyboard.press(`Backspace`) > 55 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 56 | await page.locator("body").press("Control+Y") 57 | await page.locator("body").press("Control+Y") 58 | expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:55:50
await page.locator("body").press("Control+Y")
await page.locator("body").press("Control+Y")
expect(await heynotePage.getBlockContent(0)).toBe("test line! test line! ")
})

test("copy current multiple cursors", async ({ page }) => {
await heynotePage.setContent(`
∞∞∞text
line 1
line 2`)
await page.keyboard.press("Control+A")
await page.keyboard.press(`${modifierKey}+Alt+ArrowUp`)
await page.locator("body").pressSequentially("test")
await page.keyboard.press("Alt+W")
await page.keyboard.press(`${modifierKey}+A`)
await page.keyboard.press(`Backspace`)
expect(await heynotePage.getBlockContent(0)).toBe("")

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50

Check failure on line 72 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors

2) [chromium] › emacs-clipboard-keys.spec.js:61:5 › copy current multiple cursors ──────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "testline 1testline 2" 70 | await page.keyboard.press(`${modifierKey}+A`) 71 | await page.keyboard.press(`Backspace`) > 72 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 73 | await page.keyboard.press("Control+Y") 74 | //await page.waitForTimeout(100); 75 | expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`) at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:72:50
await page.keyboard.press("Control+Y")
//await page.waitForTimeout(100);
expect(await heynotePage.getBlockContent(0)).toBe(`testline 1\ntestline 2`)
})

test("copy current multiple cursors on same line", async ({ page }) => {
await heynotePage.setContent(`
∞∞∞text
test`)
await page.keyboard.press("Control+A")
await page.keyboard.press(`${modifierKey}+Alt+ArrowDown`)
await page.locator("body").pressSequentially("1")
expect(await heynotePage.getBlockContent(0)).toBe("1test1")
await page.keyboard.press("Alt+W")

await page.keyboard.press(`${modifierKey}+A`)
await page.keyboard.press(`Backspace`)
expect(await heynotePage.getBlockContent(0)).toBe("")

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

Check failure on line 90 in tests/emacs-clipboard-keys.spec.js

View workflow job for this annotation

GitHub Actions / Run Playwright Tests

[chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line

3) [chromium] › emacs-clipboard-keys.spec.js:78:5 › copy current multiple cursors on same line ─── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).toBe(expected) // Object.is equality Expected: "" Received: "1test1" 88 | await page.keyboard.press(`${modifierKey}+A`) 89 | await page.keyboard.press(`Backspace`) > 90 | expect(await heynotePage.getBlockContent(0)).toBe("") | ^ 91 | 92 | await page.keyboard.press("Control+Y") 93 | expect(await heynotePage.getBlockContent(0)).toBe("1test1") at /home/runner/work/heynote/heynote/tests/emacs-clipboard-keys.spec.js:90:50

await page.keyboard.press("Control+Y")
expect(await heynotePage.getBlockContent(0)).toBe("1test1")
})

0 comments on commit f384579

Please sign in to comment.