Skip to content

Commit 9085af8

Browse files
committed
Merge remote-tracking branch 'origin/6.0'
2 parents ac08261 + b5055a9 commit 9085af8

1 file changed

Lines changed: 39 additions & 1 deletion

File tree

plugins/ckeditor5-woltlab-image/src/woltlabimage.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @since 6.0
1212
*/
1313

14-
import { Plugin } from "@ckeditor/ckeditor5-core";
14+
import { Command, Plugin } from "@ckeditor/ckeditor5-core";
1515
import type {
1616
DocumentSelection,
1717
DowncastAttributeEvent,
@@ -22,6 +22,7 @@ import type {
2222
Selection,
2323
} from "@ckeditor/ckeditor5-engine";
2424
import { ImageUtils } from "@ckeditor/ckeditor5-image";
25+
import { ObservableSetEvent } from "@ckeditor/ckeditor5-utils";
2526

2627
export class WoltlabImage extends Plugin {
2728
static get pluginName() {
@@ -33,6 +34,10 @@ export class WoltlabImage extends Plugin {
3334
}
3435

3536
init() {
37+
this.#decorateCommand(this.editor.commands.get("uploadImage")!);
38+
this.#decorateCommand(this.editor.commands.get("insertImage")!);
39+
this.#decorateCommand(this.editor.commands.get("replaceImageSource")!);
40+
3641
const { conversion } = this.editor;
3742

3843
const imageTypes = ["imageBlock", "imageInline"] as const;
@@ -149,6 +154,39 @@ export class WoltlabImage extends Plugin {
149154
imageUtils.findViewImgElement(viewElement)!,
150155
);
151156
}
157+
158+
#decorateCommand(command: Command) {
159+
const imageUtils: ImageUtils = this.editor.plugins.get("ImageUtils");
160+
command.on<ObservableSetEvent<boolean>>(
161+
"set:isEnabled",
162+
(evt, _, value) => {
163+
if (!value) {
164+
return;
165+
}
166+
const selection = this.editor.model.document.selection;
167+
const element = imageUtils.getClosestSelectedImageElement(selection);
168+
if (!element) {
169+
return;
170+
}
171+
evt.return = !(this.#isAttachment(element) || this.#isMedia(element));
172+
},
173+
{ priority: "high" },
174+
);
175+
}
176+
177+
#isAttachment(element: Element): boolean {
178+
return (
179+
element.getAttribute("classList") === "woltlabAttachment" ||
180+
element.hasAttribute("attachmentId")
181+
);
182+
}
183+
184+
#isMedia(element: Element): boolean {
185+
return (
186+
element.getAttribute("classList") === "woltlabSuiteMedia" ||
187+
element.hasAttribute("mediaId")
188+
);
189+
}
152190
}
153191

154192
export default WoltlabImage;

0 commit comments

Comments
 (0)