Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/6.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyperghost committed Feb 8, 2024
2 parents ac08261 + b5055a9 commit 9085af8
Showing 1 changed file with 39 additions and 1 deletion.
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 @@ -149,6 +154,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;

0 comments on commit 9085af8

Please sign in to comment.