Skip to content

Commit 8bed3a1

Browse files
feat(editor): show notification toast when code formatting fails
1 parent 010a4af commit 8bed3a1

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

packages/ckeditor5/src/plugins/format_codeblock/format_codeblock_button.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { ButtonView, Plugin } from "ckeditor5";
1+
import { ButtonView, Notification, Plugin } from "ckeditor5";
22
import formatIcon from "../../icons/format-codeblock.svg?raw";
33
import { FormatterRegistry } from "./code_formatter";
44
import { FormatCodeblockCommand } from "./format_codeblock_command";
55
import { PrettierFormatter } from "./prettier_formatter";
66

77
export default class FormatCodeblockButton extends Plugin {
8+
static get requires() {
9+
return [Notification];
10+
}
11+
812
public init() {
913
const editor = this.editor;
1014

packages/ckeditor5/src/plugins/format_codeblock/format_codeblock_command.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Command, ModelElement } from "ckeditor5";
1+
import { Command, ModelElement, Notification } from "ckeditor5";
22
import { FormatterRegistry } from "./code_formatter";
33

44
export class FormatCodeblockCommand extends Command {
@@ -25,6 +25,8 @@ export class FormatCodeblockCommand extends Command {
2525
const editor = this.editor;
2626
const model = editor.model;
2727
const selection = model.document.selection;
28+
const notification = editor.plugins.get(Notification);
29+
const t = editor.locale.t;
2830

2931
const language = this.value;
3032
if (!language) {
@@ -41,7 +43,12 @@ export class FormatCodeblockCommand extends Command {
4143
.getFirstPosition()
4244
?.findAncestor("codeBlock");
4345
if (!codeBlockEl) {
44-
console.warn("Unable to find code block element to format.");
46+
notification.showWarning(
47+
t("Unable to find code block element to format."),
48+
{
49+
namespace: "formatCodeblock",
50+
},
51+
);
4552
return;
4653
}
4754

@@ -73,10 +80,12 @@ export class FormatCodeblockCommand extends Command {
7380
});
7481
})
7582
.catch((err) => {
76-
console.error(
77-
`Failed to format code block with ${formatter.name}:`,
78-
err,
79-
);
83+
notification.showWarning(err.message || String(err), {
84+
title: t(
85+
`Failed to format code block with ${formatter.name}`,
86+
),
87+
namespace: "formatCodeblock",
88+
});
8089
});
8190
}
8291

0 commit comments

Comments
 (0)