Skip to content

Commit b5055a9

Browse files
authored
Merge pull request #115 from WoltLab/disable-replace-image-buttons
Disable replace image buttons
2 parents 02583b1 + e89b5d3 commit b5055a9

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;
@@ -146,6 +151,39 @@ export class WoltlabImage extends Plugin {
146151
imageUtils.findViewImgElement(viewElement)!,
147152
);
148153
}
154+
155+
#decorateCommand(command: Command) {
156+
const imageUtils: ImageUtils = this.editor.plugins.get("ImageUtils");
157+
command.on<ObservableSetEvent<boolean>>(
158+
"set:isEnabled",
159+
(evt, _, value) => {
160+
if (!value) {
161+
return;
162+
}
163+
const selection = this.editor.model.document.selection;
164+
const element = imageUtils.getClosestSelectedImageElement(selection);
165+
if (!element) {
166+
return;
167+
}
168+
evt.return = !(this.#isAttachment(element) || this.#isMedia(element));
169+
},
170+
{ priority: "high" },
171+
);
172+
}
173+
174+
#isAttachment(element: Element): boolean {
175+
return (
176+
element.getAttribute("classList") === "woltlabAttachment" ||
177+
element.hasAttribute("attachmentId")
178+
);
179+
}
180+
181+
#isMedia(element: Element): boolean {
182+
return (
183+
element.getAttribute("classList") === "woltlabSuiteMedia" ||
184+
element.hasAttribute("mediaId")
185+
);
186+
}
149187
}
150188

151189
export default WoltlabImage;

0 commit comments

Comments
 (0)