1111 * @since 6.0
1212 */
1313
14- import { Plugin } from "@ckeditor/ckeditor5-core" ;
14+ import { Command , Plugin } from "@ckeditor/ckeditor5-core" ;
1515import type {
1616 DocumentSelection ,
1717 DowncastAttributeEvent ,
@@ -22,6 +22,7 @@ import type {
2222 Selection ,
2323} from "@ckeditor/ckeditor5-engine" ;
2424import { ImageUtils } from "@ckeditor/ckeditor5-image" ;
25+ import { ObservableSetEvent } from "@ckeditor/ckeditor5-utils" ;
2526
2627export 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
154192export default WoltlabImage ;
0 commit comments