Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/extended code blocks: fix an issue

Adorian Doran660a57880461
diff --git a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts
index ae9857f1a4..317b68ea33 100644
--- a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.spec.ts
@@ -8,6 +8,7 @@ import { CODE_VIEW_COMMAND_NAME } from "./content_embed.js";
import ExtendedCodeBlock, {
EXTENDED_CODE_BLOCK_BUTTON, INSERT_EXTENDED_CODE_BLOCK_COMMAND, insertExtendedCodeBlock
} from "./extended_code_block.js";
+import { INSERT_INLINE_EMBED_COMMAND } from "./inline_source.js";
describe("ExtendedCodeBlock", () => {
function createEditor() {
@@ -42,7 +43,7 @@ describe("ExtendedCodeBlock", () => {
expect(editor.commands.get(INSERT_EXTENDED_CODE_BLOCK_COMMAND)?.isEnabled).toBe(false);
});
- it("gives a new block the language that was set last", async () => {
+ it("gives a new block the language that was set last on a code block", async () => {
const editor = await createEditor();
editor.setData(`<pre class="extended-code-block">`
+ `<code class="language-text-plain">x</code></pre>`);
@@ -54,6 +55,7 @@ describe("ExtendedCodeBlock", () => {
editor.model.change((writer) => writer.removeAttribute("language", embed));
setModelData(editor.model, "<paragraph>[]</paragraph>");
+ editor.execute(INSERT_INLINE_EMBED_COMMAND, { language: "mermaid", source: "" });
insertExtendedCodeBlock(editor);
expect(editor.getData()).toContain(`<code class="language-text-x-python"></code>`);
diff --git a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts
index db7c386bfa..29c120ade8 100644
--- a/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/extended_code_block.ts
@@ -1,9 +1,11 @@
import { EXTENDED_CODE_BLOCK_DEFAULT_LANGUAGE } from "@triliumnext/commons";
import bxCodeBlock from "boxicons/svg/regular/bx-code-block.svg?raw";
-import { ButtonView, Command, type DowncastAttributeEvent, type Editor, Plugin } from "ckeditor5";
+import {
+ ButtonView, Command, type DowncastAttributeEvent, type Editor, type ModelElement, Plugin
+} from "ckeditor5";
import ContentEmbed from "./content_embed.js";
-import { INSERT_INLINE_EMBED_COMMAND } from "./inline_source.js";
+import { INSERT_INLINE_EMBED_COMMAND, isExtendedCodeBlock } from "./inline_source.js";
export const INSERT_EXTENDED_CODE_BLOCK_COMMAND = "insertExtendedCodeBlock";
@@ -30,11 +32,12 @@ export default class ExtendedCodeBlock extends Plugin {
const command = new InsertExtendedCodeBlockCommand(editor);
editor.commands.add(INSERT_EXTENDED_CODE_BLOCK_COMMAND, command);
- // A new block takes the language that was set last.
- editor.editing.downcastDispatcher.on<DowncastAttributeEvent>(
+ // A new block takes the language that was set last on a code block.
+ editor.editing.downcastDispatcher.on<DowncastAttributeEvent<ModelElement>>(
"attribute:language:contentEmbed",
(_evt, data) => {
- if (typeof data.attributeNewValue === "string") {
+ if (typeof data.attributeNewValue === "string"
+ && isExtendedCodeBlock(editor, data.item)) {
command.language = data.attributeNewValue;
}
}