Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Block corruption when deleting block content using deleteLine command #134

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/editor/block/delete-line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { EditorSelection } from "@codemirror/state"
import { getActiveNoteBlock } from "./block"

function updateSel(sel, by) {
return EditorSelection.create(sel.ranges.map(by), sel.mainIndex);
}

function selectedLineBlocks(state) {
let blocks = [], upto = -1
for (let range of state.selection.ranges) {
let startLine = state.doc.lineAt(range.from), endLine = state.doc.lineAt(range.to)
if (!range.empty && range.to == endLine.from) endLine = state.doc.lineAt(range.to - 1)
if (upto >= startLine.number) {
let prev = blocks[blocks.length - 1]
prev.to = endLine.to
prev.ranges.push(range)
} else {
blocks.push({ from: startLine.from, to: endLine.to, ranges: [range] })
}
upto = endLine.number + 1
}
return blocks
}

export const deleteLine = (view) => {
if (view.state.readOnly)
return false

const { state } = view

const block = getActiveNoteBlock(view.state)
const selectedLines = selectedLineBlocks(state)

const changes = state.changes(selectedLines.map(({ from, to }) => {
if(from !== block.content.from || to !== block.content.to) {
if (from > 0) from--
else if (to < state.doc.length) to++
}
return { from, to }
}))

const selection = updateSel(state.selection, range => view.moveVertically(range, true)).map(changes)
view.dispatch({ changes, selection, scrollIntoView: true, userEvent: "delete.line" })

return true;
}
2 changes: 2 additions & 0 deletions src/editor/keymap.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from "./block/commands.js"

import { formatBlockContent } from "./block/format-code.js"
import { deleteLine } from "./block/delete-line.js"


export function keymapFromSpec(specs) {
Expand Down Expand Up @@ -51,6 +52,7 @@ export function heynoteKeymap(editor) {
["Alt-Shift-f", formatBlockContent],
["Mod-Alt-ArrowDown", newCursorBelow],
["Mod-Alt-ArrowUp", newCursorAbove],
["Mod-Shift-k", deleteLine],
{key:"Mod-ArrowUp", run:gotoPreviousBlock, shift:selectPreviousBlock},
{key:"Mod-ArrowDown", run:gotoNextBlock, shift:selectNextBlock},
{key:"Ctrl-ArrowUp", run:gotoPreviousParagraph, shift:selectPreviousParagraph},
Expand Down
50 changes: 50 additions & 0 deletions tests/delete-line.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect, test } from "@playwright/test";
import { HeynotePage } from "./test-utils.js";

let heynotePage

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

expect((await heynotePage.getBlocks()).length).toBe(1)
await heynotePage.setContent(`
∞∞∞text
Block A
text content 1
text content 2
text content 3
text content 4
∞∞∞text
Block B
∞∞∞text
Block C`)

// check that blocks are created
expect((await heynotePage.getBlocks()).length).toBe(3)

// check that visual block layers are created
await expect(page.locator("css=.heynote-blocks-layer > div")).toHaveCount(3)
});

test("delete line on single line in Block A", async ({ page }) => {
for (let i = 0; i < 7; i++) {
await page.locator("body").press("ArrowUp")
}
for (let i = 0; i < 4; i++) {
await page.locator("body").press(`${heynotePage.isMac ? "Meta" : "Control"}+Shift+k`)
}
expect(await heynotePage.getBlockContent(0)).toBe("\ntext content 4");
});

test("delete line on selection in Block B", async ({ page }) => {
for (let i = 0; i < 1; i++) {
await page.locator("body").press("ArrowUp")
}
await page.locator("body").press(`${heynotePage.isMac ? "Meta" : "Control"}+A`)
await page.locator("body").press(`${heynotePage.isMac ? "Meta" : "Control"}+Shift+k`)
expect(await heynotePage.getBlockContent(1)).toBe('');
});



Loading