diff --git a/src/editor/block/format-code.js b/src/editor/block/format-code.js index f4742dd..23172bc 100644 --- a/src/editor/block/format-code.js +++ b/src/editor/block/format-code.js @@ -40,8 +40,8 @@ export const formatBlockContent = async ({ state, dispatch }) => { plugins: language.prettier.plugins, tabWidth: state.tabSize, }), - cursorOffset: cursorPos == block.content.from ? 0 : content.length, } + formattedContent.cursorOffset = cursorPos == block.content.from ? 0 : formattedContent.formatted.length } else { formattedContent = await prettier.formatWithCursor(content, { cursorOffset: cursorPos - block.content.from, @@ -62,7 +62,7 @@ export const formatBlockContent = async ({ state, dispatch }) => { to: block.content.to, insert: formattedContent.formatted, }, - selection: EditorSelection.cursor(block.content.from + formattedContent.cursorOffset), + selection: EditorSelection.cursor(block.content.from + Math.min(formattedContent.cursorOffset, formattedContent.formatted.length)), }, { userEvent: "input", scrollIntoView: true, diff --git a/tests/formatting.spec.js b/tests/formatting.spec.js new file mode 100644 index 0000000..76d91ae --- /dev/null +++ b/tests/formatting.spec.js @@ -0,0 +1,46 @@ +import { test, expect } from "@playwright/test"; +import { HeynotePage } from "./test-utils.js"; + +let heynotePage + +test.beforeEach(async ({ page }) => { + console.log("beforeEach") + heynotePage = new HeynotePage(page) + await heynotePage.goto() +}) + + +test("JSON formatting", async ({ page }) => { + heynotePage.setContent(` +∞∞∞json + {"test": 1, "key2": "hey!"} +`) + expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON") + await page.locator("css=.status-block.format").click() + await page.waitForTimeout(100) + expect(await heynotePage.getBlockContent(0)).toBe(`{ + "test": 1, + "key2": "hey!" +} +`) +}) + +test("JSON formatting (cursor at start)", async ({ page }) => { + heynotePage.setContent(` +∞∞∞json + {"test": 1, "key2": "hey!"} +`) + expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON") + for (let i=0; i<5; i++) { + await page.locator("body").press("ArrowUp") + } + await page.locator("css=.status-block.format").click() + await page.waitForTimeout(100) + expect(await heynotePage.getBlockContent(0)).toBe(`{ + "test": 1, + "key2": "hey!" +} +`) + const block = (await heynotePage.getBlocks())[0] + expect(await page.evaluate(() => window._heynote_editor.view.state.selection.main.from)).toBe(block.content.from) +}) \ No newline at end of file diff --git a/tests/language-detection.spec.js b/tests/language-detection.spec.js new file mode 100644 index 0000000..6581623 --- /dev/null +++ b/tests/language-detection.spec.js @@ -0,0 +1,41 @@ +import { test, expect } from "@playwright/test"; +import { HeynotePage } from "./test-utils.js"; + +let heynotePage + +test.beforeEach(async ({ page }) => { + console.log("beforeEach") + heynotePage = new HeynotePage(page) + await heynotePage.goto() +}) + + +test("test valid JSON detection", async ({ page }) => { + page.locator("body").pressSequentially(` + {"test": 1, "key2": "hey!"} + `) + await page.waitForTimeout(200); + expect(await page.locator("css=.status .status-block.lang")).toHaveText("JSON (auto)") + const block = (await heynotePage.getBlocks())[0] + expect(block.language.name).toBe("json") + expect(block.language.auto).toBeTruthy() +}) + + +test("python detection", async ({ page }) => { + page.locator("body").pressSequentially(` +# import complex math module +import cmath + +# calculate the discriminant +d = (b**2) - (4*a*c) + +# find two solutions +sol1 = (-b-cmath.sqrt(d))/(2*a) +sol2 = (-b+cmath.sqrt(d))/(2*a) + +print('The solution are {0} and {1}'.format(sol1,sol2)) + `) + await page.waitForTimeout(1000); + expect(await page.locator("css=.status .status-block.lang")).toHaveText("Python (auto)") +}) diff --git a/tests/test-utils.js b/tests/test-utils.js index 005694c..4ef6fe7 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -29,7 +29,7 @@ export class HeynotePage { async getContent() { return await this.page.evaluate(() => window._heynote_editor.getContent()) } - + async setContent(content) { await this.page.evaluate((content) => window._heynote_editor.setContent(content), content) }