Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ckeditor): clear stuck link when deleting empties the block, closes #10613
Elian Dorane0fd4e0bad7f
diff --git a/packages/ckeditor5/src/plugins/remove_format_links.spec.ts b/packages/ckeditor5/src/plugins/remove_format_links.spec.tsindex db61e7b3ba..2e4d772480 100644--- a/packages/ckeditor5/src/plugins/remove_format_links.spec.ts+++ b/packages/ckeditor5/src/plugins/remove_format_links.spec.ts@@ -1,4 +1,4 @@-import { ClassicEditor, Essentials, Link, Paragraph, RemoveFormat } from "ckeditor5";+import { Bold, ClassicEditor, Essentials, Link, Paragraph, RemoveFormat, _getModelData, _setModelData } from "ckeditor5";import { beforeEach, describe, expect, it } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";@@ -8,7 +8,7 @@ describe("RemoveFormatLinksPlugin", () => {let editor: ClassicEditor;beforeEach(async () => {- editor = await createTestEditor([Essentials, Paragraph, Link, RemoveFormat, RemoveFormatLinksPlugin]);+ editor = await createTestEditor([Essentials, Paragraph, Bold, Link, RemoveFormat, RemoveFormatLinksPlugin]);});it("registers itself as a plugin", () => {@@ -22,5 +22,74 @@ describe("RemoveFormatLinksPlugin", () => {it("marks the linkHref attribute as formatting so RemoveFormat strips it", () => {const props = editor.model.schema.getAttributeProperties("linkHref");expect(props.isFormatting).toBe(true);++ _setModelData(editor.model, '<paragraph>[<$text linkHref="https://google.com">link</$text>]</paragraph>');+ editor.execute("removeFormat");++ expect(_getModelData(editor.model)).toBe("<paragraph>[link]</paragraph>");+ });++ describe("clearing the link once a deletion empties the block (#10613)", () => {+ it("drops linkHref when the deleted selection was the whole block", () => {+ _setModelData(+ editor.model,+ '<paragraph>[<$text linkHref="https://google.com">https://google.com</$text>]</paragraph>' ++ "<paragraph>after</paragraph>"+ );++ editor.execute("delete");++ // Neither the selection nor the emptied block may keep the link: without the fix the+ // block retained a stored `selection:linkHref` and typing continued the old link.+ expect(editor.model.document.selection.hasAttribute("linkHref")).toBe(false);+ expect(_getModelData(editor.model)).toBe("<paragraph>[]</paragraph><paragraph>after</paragraph>");+ });++ it("still restores genuine formatting (bold) after the same deletion", () => {+ _setModelData(editor.model, "<paragraph>[<$text bold='true'>bold text</$text>]</paragraph>");++ editor.execute("delete");++ expect(editor.model.document.selection.hasAttribute("bold")).toBe(true);+ });++ it("keeps linkHref when the deletion leaves the block non-empty", () => {+ _setModelData(+ editor.model,+ '<paragraph><$text linkHref="https://google.com">ab[cd]ef</$text></paragraph>'+ );++ editor.execute("delete");++ expect(editor.model.document.selection.hasAttribute("linkHref")).toBe(true);+ });++ it("ignores deletions of unlinked content", () => {+ _setModelData(editor.model, "<paragraph>[plain]</paragraph>");++ editor.execute("delete");++ expect(editor.model.document.selection.hasAttribute("linkHref")).toBe(false);+ expect(_getModelData(editor.model)).toBe("<paragraph>[]</paragraph>");+ });++ it("ignores deleteContent() driven by a selection other than the document one", () => {+ _setModelData(+ editor.model,+ '<paragraph>[<$text linkHref="https://google.com">link</$text>]</paragraph>' ++ "<paragraph>second</paragraph>"+ );++ // A detached selection over the *second* paragraph leaves the document selection+ // expanded over the link, so the handler must not touch it.+ editor.model.change((writer) => {+ const second = editor.model.document.getRoot()?.getChild(1);+ if (second) {+ editor.model.deleteContent(writer.createSelection(second, "in"));+ }+ });++ expect(editor.model.document.selection.hasAttribute("linkHref")).toBe(true);+ });});});diff --git a/packages/ckeditor5/src/plugins/remove_format_links.ts b/packages/ckeditor5/src/plugins/remove_format_links.tsindex 224a8dad2c..6ffb24b03f 100644--- a/packages/ckeditor5/src/plugins/remove_format_links.ts+++ b/packages/ckeditor5/src/plugins/remove_format_links.ts@@ -14,6 +14,41 @@ export default class RemoveFormatLinksPlugin extends Plugin {this.editor.model.schema.setAttributeProperties( 'linkHref', {isFormatting: true});++ this.#clearLinkOnEmptiedBlock();+ }++ /**+ * Drops the `linkHref` attribute from the document selection when a deletion has just+ * emptied the block it sits in.+ *+ * `model.deleteContent()` re-applies the pre-delete *formatting* attributes to the+ * selection whenever the deletion leaves its parent block empty, so that typing carries+ * on in the same style (bold text stays bold). Marking `linkHref` as formatting above —+ * which is what makes "Remove format" strip links — opts it into that restore too, so+ * selecting a line holding only a link and deleting it left the selection (and the+ * block, via the stored `selection:linkHref` attribute) still inside the link. The line+ * looked empty but anything typed next became part of the old link (#10613).+ */+ #clearLinkOnEmptiedBlock() {+ const model = this.editor.model;++ this.listenTo( model, 'deleteContent', () => {+ const selection = model.document.selection;++ if ( !selection.isCollapsed || !selection.hasAttribute( 'linkHref' ) ) {+ return;+ }++ const block = selection.getFirstPosition()?.parent;+ if ( !block?.isEmpty ) {+ return;+ }++ // Nested inside the deletion's own change block, so this stays part of the same+ // undo step: a single undo brings the link back rather than only its attribute.+ model.change( writer => writer.removeSelectionAttribute( 'linkHref' ) );+ }, { priority: 'low' } );}}