diff --git a/shared/editor/commands/toggleList.ts b/shared/editor/commands/toggleList.ts index 145f66da6a..28e2e5baa7 100644 --- a/shared/editor/commands/toggleList.ts +++ b/shared/editor/commands/toggleList.ts @@ -94,7 +94,7 @@ export default function toggleList( ); dispatch?.(tr); - return false; + return true; } } diff --git a/shared/editor/lib/chainTransactions.test.ts b/shared/editor/lib/chainTransactions.test.ts new file mode 100644 index 0000000000..d2db3ef44a --- /dev/null +++ b/shared/editor/lib/chainTransactions.test.ts @@ -0,0 +1,32 @@ +import type { Command } from "prosemirror-state"; +import { createEditorStateWithSelection, doc, p } from "@shared/test/editor"; +import { chainTransactions } from "./chainTransactions"; + +describe("chainTransactions", () => { + it("can be invoked more than once without losing commands", () => { + const cmd1: Command = (state, dispatch) => { + dispatch?.(state.tr.insertText("a", state.selection.from)); + return true; + }; + const cmd2: Command = (state, dispatch) => { + dispatch?.(state.tr.insertText("b", state.selection.from)); + return true; + }; + const chained = chainTransactions(cmd1, cmd2); + + const runOnce = () => { + const state = createEditorStateWithSelection(doc([p("x")]), 1); + let next = state; + chained(state, (tr) => { + next = next.apply(tr); + }); + return next.doc.textContent; + }; + + const first = runOnce(); + const second = runOnce(); + expect(first).toBe(second); + expect(first).toContain("a"); + expect(first).toContain("b"); + }); +}); diff --git a/shared/editor/lib/chainTransactions.ts b/shared/editor/lib/chainTransactions.ts index b29d8ef6d8..7e50dde155 100644 --- a/shared/editor/lib/chainTransactions.ts +++ b/shared/editor/lib/chainTransactions.ts @@ -19,8 +19,9 @@ export function chainTransactions( dispatch?.(tr); state = view && view.state !== before ? view.state : state.apply(tr); }; - const last = commands.pop(); - commands.map((command) => command?.(state, dispatcher, view)); + const list = [...commands]; + const last = list.pop(); + list.forEach((command) => command?.(state, dispatcher, view)); return last !== undefined && last(state, dispatch, view); }; } diff --git a/shared/editor/lib/emoji.test.ts b/shared/editor/lib/emoji.test.ts index 68a4c94d0e..08ba83a61a 100644 --- a/shared/editor/lib/emoji.test.ts +++ b/shared/editor/lib/emoji.test.ts @@ -4,6 +4,7 @@ import { getEmojiFromName, loadEmojiData, parseReactionShorthand, + snakeCase, } from "./emoji"; beforeAll(async () => { @@ -16,6 +17,14 @@ describe("getNameFromEmoji", () => { }); }); +describe("snakeCase", () => { + it("converts kebab case to snake case for consecutive segments", () => { + expect(snakeCase("a-b-c")).toBe("a_b_c"); + expect(snakeCase("thinking-face")).toBe("thinking_face"); + expect(snakeCase("no-dash")).toBe("no_dash"); + }); +}); + describe("getEmojiFromName", () => { it("returns the correct native character", () => { expect(getEmojiFromName("thinking_face")).toBe("🤔"); diff --git a/shared/editor/lib/emoji.ts b/shared/editor/lib/emoji.ts index 988642dc8d..6cfcd691eb 100644 --- a/shared/editor/lib/emoji.ts +++ b/shared/editor/lib/emoji.ts @@ -13,7 +13,7 @@ export const emojiMartToGemoji: Record = { * @param str The string to convert * @returns The converted string */ -export const snakeCase = (str: string) => str.replace(/(\w)-(\w)/g, "$1_$2"); +export const snakeCase = (str: string) => str.replace(/(\w)-(?=\w)/g, "$1_"); /** * A map of emoji shortcode to emoji character. The shortcode is snake cased diff --git a/shared/editor/queries/table.ts b/shared/editor/queries/table.ts index d991a95aa9..bd290d1b61 100644 --- a/shared/editor/queries/table.ts +++ b/shared/editor/queries/table.ts @@ -143,6 +143,13 @@ export function getCellsInColumn(index: number) { }; } +/** + * Get the positions of the cells in a row, one entry per column. A merged cell + * is repeated for every column it spans, so callers can index by column. + * + * @param index The row index. + * @returns A function that returns the cell positions for the given state. + */ export function getCellsInRow(index: number) { return (state: EditorState): number[] => { if (!isInTable(state)) { @@ -152,17 +159,8 @@ export function getCellsInRow(index: number) { const rect = selectedRect(state); const cells = []; - let previous; for (let i = 0; i < rect.map.width; i += 1) { - const cell = rect.tableStart + rect.map.map[index * rect.map.width + i]; - cells.push(cell); - - // Ensure we don't add the same cell multiple times, this can happen - // if the row is selected and the table column has merged cells. - if (previous === cell) { - continue; - } - previous = cell; + cells.push(rect.tableStart + rect.map.map[index * rect.map.width + i]); } return cells; diff --git a/shared/utils/ProsemirrorHelper.test.ts b/shared/utils/ProsemirrorHelper.test.ts index 28d2394bb3..922c9e917b 100644 --- a/shared/utils/ProsemirrorHelper.test.ts +++ b/shared/utils/ProsemirrorHelper.test.ts @@ -1,4 +1,5 @@ import { Schema } from "prosemirror-model"; +import env from "../env"; import type { ProsemirrorData } from "../types"; import type { CommentMark } from "./ProsemirrorHelper"; import { ProsemirrorHelper } from "./ProsemirrorHelper"; @@ -265,6 +266,40 @@ describe("ProsemirrorHelper", () => { }); }); + describe("attachmentsToAbsoluteUrls", () => { + it("converts every attachment url", () => { + const src = + "/api/attachments.redirect?id=12345678-1234-1234-1234-123456789012"; + const data: ProsemirrorData = { + type: "doc", + content: [ + { type: "image", attrs: { src } }, + { type: "image", attrs: { src } }, + { type: "image", attrs: { src } }, + ], + }; + + const result = ProsemirrorHelper.attachmentsToAbsoluteUrls(data); + expect(result.content?.map((node) => node.attrs?.src)).toEqual([ + env.URL + src, + env.URL + src, + env.URL + src, + ]); + }); + + it("converts uppercase attachment urls", () => { + const src = + "/api/attachments.redirect?id=ABCDEF12-3456-789A-BCDE-F0123456789A"; + const data: ProsemirrorData = { + type: "doc", + content: [{ type: "image", attrs: { src } }], + }; + + const result = ProsemirrorHelper.attachmentsToAbsoluteUrls(data); + expect(result.content?.[0].attrs?.src).toEqual(env.URL + src); + }); + }); + describe("removeMarks", () => { it("should remove specified mark types from text nodes", () => { const doc: ProsemirrorData = { @@ -433,6 +468,17 @@ describe("ProsemirrorHelper", () => { expect(result.childCount).toEqual(2); }); + + it("keeps a titled leaf block at the end", () => { + const result = ProsemirrorHelper.trim( + doc(paragraph({ type: "text", text: "Hello" }), { + type: "video", + attrs: { title: "Clip" }, + }) + ); + + expect(result.childCount).toEqual(2); + }); }); describe("getHeadings", () => { diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index 5aee6f56b6..d8854c832a 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -517,7 +517,8 @@ export class ProsemirrorHelper { * @returns The ProsemirrorData with absolute URLs for attachments */ static attachmentsToAbsoluteUrls(data: ProsemirrorData): ProsemirrorData { - const regex = new RegExp("^" + attachmentRedirectRegex.source); + // No "g" flag, as test() would then keep lastIndex between nodes. + const regex = new RegExp("^" + attachmentRedirectRegex.source, "i"); function replace(node: ProsemirrorData) { if ( @@ -601,6 +602,13 @@ export class ProsemirrorHelper { * text of leaf nodes such as mentions. Stops at the first text found. */ private static hasText(node: Node): boolean { + if (node.type.spec.leafText) { + return !!String(node.type.spec.leafText(node)).trim(); + } + if (node.isText) { + return !!node.text?.trim(); + } + let found = false; node.descendants((child: Node) => { diff --git a/shared/utils/parseCollectionSlug.test.ts b/shared/utils/parseCollectionSlug.test.ts index 15eefd3ead..8e54006d09 100644 --- a/shared/utils/parseCollectionSlug.test.ts +++ b/shared/utils/parseCollectionSlug.test.ts @@ -45,4 +45,9 @@ describe("#parseCollectionSlug", () => { "test-ANzZwgv2RG" ); }); + + it("should return undefined when the collection segment is missing", () => { + expect(parseCollectionSlug("https://example.com/about")).toBeUndefined(); + expect(parseCollectionSlug("/collection/")).toBeUndefined(); + }); }); diff --git a/shared/utils/parseCollectionSlug.ts b/shared/utils/parseCollectionSlug.ts index d37bdf077a..6452a1408d 100644 --- a/shared/utils/parseCollectionSlug.ts +++ b/shared/utils/parseCollectionSlug.ts @@ -21,5 +21,9 @@ export default function parseCollectionSlug(url: string) { const split = parsed.split("/"); const indexOfCollection = split.indexOf("collection"); - return split[indexOfCollection + 1] ?? undefined; + if (indexOfCollection === -1) { + return undefined; + } + const slug = split[indexOfCollection + 1]; + return slug ? slug : undefined; } diff --git a/shared/utils/parseDocumentSlug.test.ts b/shared/utils/parseDocumentSlug.test.ts index 3f63b2ced6..b5f82ab6eb 100644 --- a/shared/utils/parseDocumentSlug.test.ts +++ b/shared/utils/parseDocumentSlug.test.ts @@ -43,4 +43,10 @@ describe("#parseDocumentSlug", () => { "my-doc-y4j4tR4UuV" ); }); + + it("should return undefined when the doc segment is missing", () => { + expect(parseDocumentSlug("https://example.com/about")).toBeUndefined(); + expect(parseDocumentSlug("/about")).toBeUndefined(); + expect(parseDocumentSlug("/doc/")).toBeUndefined(); + }); }); diff --git a/shared/utils/parseDocumentSlug.ts b/shared/utils/parseDocumentSlug.ts index 80aff6cbf0..f58a223afe 100644 --- a/shared/utils/parseDocumentSlug.ts +++ b/shared/utils/parseDocumentSlug.ts @@ -21,5 +21,9 @@ export default function parseDocumentSlug(url: string) { const split = parsed.split("/"); const indexOfDoc = split.indexOf("doc"); - return split[indexOfDoc + 1] ?? undefined; + if (indexOfDoc === -1) { + return undefined; + } + const slug = split[indexOfDoc + 1]; + return slug ? slug : undefined; } diff --git a/shared/utils/urls.test.ts b/shared/utils/urls.test.ts index 04176f3d61..a5d5dca844 100644 --- a/shared/utils/urls.test.ts +++ b/shared/utils/urls.test.ts @@ -646,6 +646,15 @@ describe("#removeUrlPathSuffix", () => { urlsUtils.removeUrlPathSuffix("/doc/my-doc-abc123/edit", "/edit") ).toBe("/doc/my-doc-abc123"); }); + + it("should leave the url unchanged for an empty suffix", () => { + expect( + urlsUtils.removeUrlPathSuffix("https://example.com/doc/my-doc-abc123", "") + ).toBe("https://example.com/doc/my-doc-abc123"); + expect(urlsUtils.removeUrlPathSuffix("/doc/my-doc-abc123", "")).toBe( + "/doc/my-doc-abc123" + ); + }); }); describe("containsUrl", () => { diff --git a/shared/utils/urls.ts b/shared/utils/urls.ts index b57182975c..dfa111fe6c 100644 --- a/shared/utils/urls.ts +++ b/shared/utils/urls.ts @@ -493,6 +493,9 @@ export function removeUrlFragment(url: string): string { * @returns The url with the suffix removed from the end of its pathname. */ export function removeUrlPathSuffix(url: string, suffix: string): string { + if (!suffix) { + return url; + } try { const parsed = new URL(url); if (parsed.pathname.endsWith(suffix)) {