Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion plugins/ckeditor5-woltlab-image/src/woltlabimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @since 6.0
*/

import { Plugin } from "@ckeditor/ckeditor5-core";
import { Command, Plugin } from "@ckeditor/ckeditor5-core";
import type {
DocumentSelection,
DowncastAttributeEvent,
Expand All @@ -22,6 +22,7 @@ import type {
Selection,
} from "@ckeditor/ckeditor5-engine";
import { ImageUtils } from "@ckeditor/ckeditor5-image";
import { ObservableSetEvent } from "@ckeditor/ckeditor5-utils";

export class WoltlabImage extends Plugin {
static get pluginName() {
Expand All @@ -33,6 +34,10 @@ export class WoltlabImage extends Plugin {
}

init() {
this.#decorateCommand(this.editor.commands.get("uploadImage")!);
this.#decorateCommand(this.editor.commands.get("insertImage")!);
this.#decorateCommand(this.editor.commands.get("replaceImageSource")!);

const { conversion } = this.editor;

const imageTypes = ["imageBlock", "imageInline"] as const;
Expand Down Expand Up @@ -146,6 +151,39 @@ export class WoltlabImage extends Plugin {
imageUtils.findViewImgElement(viewElement)!,
);
}

#decorateCommand(command: Command) {
const imageUtils: ImageUtils = this.editor.plugins.get("ImageUtils");
command.on<ObservableSetEvent<boolean>>(
"set:isEnabled",
(evt, _, value) => {
if (!value) {
return;
}
const selection = this.editor.model.document.selection;
const element = imageUtils.getClosestSelectedImageElement(selection);
if (!element) {
return;
}
evt.return = !(this.#isAttachment(element) || this.#isMedia(element));
},
{ priority: "high" },
);
}

#isAttachment(element: Element): boolean {
return (
element.getAttribute("classList") === "woltlabAttachment" ||
element.hasAttribute("attachmentId")
);
}

#isMedia(element: Element): boolean {
return (
element.getAttribute("classList") === "woltlabSuiteMedia" ||
element.hasAttribute("mediaId")
);
}
}

export default WoltlabImage;