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(code-block): keep find results when re-highlighting (closes #6153)
Elian Dorane91b27610e2f
diff --git a/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts b/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.tsindex c5ea206073..ed1a3fd285 100644--- a/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts+++ b/packages/ckeditor5/src/plugins/syntax_highlighting/index.spec.ts@@ -1,4 +1,4 @@-import { _setModelData as setModelData, _getViewData as getViewData, ClassicEditor, CodeBlock, Essentials, GeneralHtmlSupport, HorizontalLine, Paragraph, type EditorConfig } from "ckeditor5";+import { _setModelData as setModelData, _getViewData as getViewData, ClassicEditor, CodeBlock, Essentials, FindAndReplaceEditing, GeneralHtmlSupport, HorizontalLine, Paragraph, type EditorConfig } from "ckeditor5";import { beforeEach, describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../../test/editor-kit.js";@@ -129,6 +129,48 @@ describe("SyntaxHighlighting", () => {expect(getViewData(editor.editing.view)).toContain("hljs-keyword");});+ it("keeps the find results of a code block when replacing one of them re-highlights it", async () => {+ editor = await createEditor(makeConfig(), [ FindAndReplaceEditing ]);++ setModelData(editor.model, '<codeBlock language="javascript">foo bar foo[]</codeBlock>');+ await flushPostFixers(editor);++ const { results } = editor.execute("find", "foo");+ expect(results.length).toBe(2);+ const [ replaced, remaining ] = [ ...results ];++ editor.execute("replace", "baz", replaced);++ expect(remaining.marker?.getRange().isFlat).toBe(true);+ expect(remaining.marker && editor.model.markers.has(remaining.marker)).toBe(true);+ expect(fakeHljs.highlight).toHaveBeenLastCalledWith("baz bar foo", { language: "javascript" });+ expect(getViewData(editor.editing.view)).toContain("hljs-keyword");+ });++ it("keeps the find results of a code block when typing into it during a search", async () => {+ editor = await createEditor(makeConfig(), [ FindAndReplaceEditing ]);++ setModelData(editor.model, '<codeBlock language="javascript">foo bar foo[]</codeBlock>');+ await flushPostFixers(editor);++ const results = editor.plugins.get(FindAndReplaceEditing).find("foo");+ expect(results.length).toBe(2);++ editor.model.change((writer) => {+ const codeBlock = editor?.model.document.getRoot()?.getChild(0);+ if (codeBlock && codeBlock.is("element")) {+ writer.insertText(" foo", writer.createPositionAt(codeBlock, "end"));+ }+ });++ expect(fakeHljs.highlight).toHaveBeenLastCalledWith("foo bar foo foo", { language: "javascript" });+ const markerNames = [ ...results ].map((result) => result.marker?.name);+ expect(markerNames).toHaveLength(3);+ for (const name of markerNames) {+ expect(name && editor.model.markers.has(name)).toBe(true);+ }+ });+it("handles softBreaks (newlines) in code blocks", async () => {editor = await createEditor(makeConfig());diff --git a/packages/ckeditor5/src/plugins/syntax_highlighting/index.ts b/packages/ckeditor5/src/plugins/syntax_highlighting/index.tsindex f8e6186bde..79418c25c4 100644--- a/packages/ckeditor5/src/plugins/syntax_highlighting/index.ts+++ b/packages/ckeditor5/src/plugins/syntax_highlighting/index.ts@@ -162,9 +162,13 @@ export default class SyntaxHighlighting extends Plugin {// Can't invoke addMarker with an already existing marker name,// clear all highlight markers first. Marker names follow the- // pattern hljs:cssClassName:uniqueId, eg hljs:hljs-comment:1+ // pattern hljs:cssClassName:uniqueId, eg hljs:hljs-comment:1.+ // Markers of other features (find results, comments) stay.const codeBlockRange = model.createRangeIn(codeBlock);for (const marker of model.markers.getMarkersIntersectingRange(codeBlockRange)) {+ if (!marker.name.startsWith("hljs:")) {+ continue;+ }dbg("removing marker " + marker.name);writer.removeMarker(marker.name);}