From 74cdb12ded740b154c8eef7d080a50754a34447b Mon Sep 17 00:00:00 2001 From: l-lejman Date: Wed, 8 Jan 2025 14:14:26 +0100 Subject: [PATCH 1/4] Disable source editing mode when CommentsOnly plugin is enabled. --- .../src/sourceediting.ts | 78 +++++++++++++------ 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/packages/ckeditor5-source-editing/src/sourceediting.ts b/packages/ckeditor5-source-editing/src/sourceediting.ts index 5bf63d880a9..bbc6f8bd0eb 100644 --- a/packages/ckeditor5-source-editing/src/sourceediting.ts +++ b/packages/ckeditor5-source-editing/src/sourceediting.ts @@ -10,6 +10,7 @@ /* global console */ import { type Editor, Plugin, PendingActions } from 'ckeditor5/src/core.js'; +import { CommentsOnly } from '@ckeditor/ckeditor5-comments'; import { ButtonView, MenuBarMenuListItemButtonView, type Dialog } from 'ckeditor5/src/ui.js'; import { CKEditorError, createElement, ElementReplacer } from 'ckeditor5/src/utils.js'; import { formatHtml } from './utils/formathtml.js'; @@ -135,6 +136,14 @@ export default class SourceEditing extends Plugin { this.on( 'change:isEnabled', ( evt, name, isEnabled ) => this._handleReadOnlyMode( !isEnabled ) ); this.listenTo( editor, 'change:isReadOnly', ( evt, name, isReadOnly ) => this._handleReadOnlyMode( isReadOnly ) ); + + if ( editor.plugins.has( CommentsOnly ) ) { + this.listenTo( editor.plugins.get( CommentsOnly ), 'change:isEnabled', ( evt, name, isCommentsOnlyEnabled ) => { + if ( isCommentsOnlyEnabled ) { + this.isSourceEditingMode = false; + } + } ); + } } // Update the editor data while calling editor.getData() in the source editing mode. @@ -411,31 +420,22 @@ export default class SourceEditing extends Plugin { buttonView.bind( 'isOn' ).to( this, 'isSourceEditingMode' ); - // The button should be disabled if one of the following conditions is met: - buttonView.bind( 'isEnabled' ).to( - this, 'isEnabled', - editor, 'isReadOnly', - editor.plugins.get( PendingActions ), 'hasAny', - ( isEnabled, isEditorReadOnly, hasAnyPendingActions ) => { - // (1) The plugin itself is disabled. - if ( !isEnabled ) { - return false; - } - - // (2) The editor is in read-only mode. - if ( isEditorReadOnly ) { - return false; - } - - // (3) Any pending action is scheduled. It may change the model, so modifying the document source should be prevented - // until the model is finally set. - if ( hasAnyPendingActions ) { - return false; - } - - return true; - } - ); + if ( editor.plugins.has( CommentsOnly ) ) { + buttonView.bind( 'isEnabled' ).to( + this, 'isEnabled', + editor, 'isReadOnly', + editor.plugins.get( PendingActions ), 'hasAny', + editor.plugins.get( CommentsOnly ), 'isEnabled', + this._isSourceEditingButtonEnabled + ); + } else { + buttonView.bind( 'isEnabled' ).to( + this, 'isEnabled', + editor, 'isReadOnly', + editor.plugins.get( PendingActions ), 'hasAny', + this._isSourceEditingButtonEnabled + ); + } this.listenTo( buttonView, 'execute', () => { this.isSourceEditingMode = !this.isSourceEditingMode; @@ -443,6 +443,34 @@ export default class SourceEditing extends Plugin { return buttonView; } + + // The button should be disabled if one of the following conditions is met: + private _isSourceEditingButtonEnabled( + isEnabled: boolean, isEditorReadOnly: boolean, hasAnyPendingActions: boolean, isCommentsOnlyEnabled?: boolean + ): boolean { + // (1) The plugin itself is disabled. + if ( !isEnabled ) { + return false; + } + + // (2) The editor is in read-only mode. + if ( isEditorReadOnly ) { + return false; + } + + // (3) Any pending action is scheduled. It may change the model, so modifying the document source should be prevented + // until the model is finally set. + if ( hasAnyPendingActions ) { + return false; + } + + // (4) The ediotr is in comments-only mode + if ( isCommentsOnlyEnabled ) { + return false; + } + + return true; + } } /** From 900e3b08c136ed4f3746739571e49c81a0d9c521 Mon Sep 17 00:00:00 2001 From: l-lejman Date: Wed, 8 Jan 2025 16:27:58 +0100 Subject: [PATCH 2/4] Close SourceEditing mode when mode is disabled. --- .../src/sourceediting.ts | 84 +++++++------------ 1 file changed, 30 insertions(+), 54 deletions(-) diff --git a/packages/ckeditor5-source-editing/src/sourceediting.ts b/packages/ckeditor5-source-editing/src/sourceediting.ts index bbc6f8bd0eb..71ed1514069 100644 --- a/packages/ckeditor5-source-editing/src/sourceediting.ts +++ b/packages/ckeditor5-source-editing/src/sourceediting.ts @@ -10,7 +10,6 @@ /* global console */ import { type Editor, Plugin, PendingActions } from 'ckeditor5/src/core.js'; -import { CommentsOnly } from '@ckeditor/ckeditor5-comments'; import { ButtonView, MenuBarMenuListItemButtonView, type Dialog } from 'ckeditor5/src/ui.js'; import { CKEditorError, createElement, ElementReplacer } from 'ckeditor5/src/utils.js'; import { formatHtml } from './utils/formathtml.js'; @@ -133,17 +132,13 @@ export default class SourceEditing extends Plugin { } } ); - this.on( 'change:isEnabled', ( evt, name, isEnabled ) => this._handleReadOnlyMode( !isEnabled ) ); + this.on( 'change:isEnabled', ( evt, name, isEnabled ) => { + if ( !isEnabled && this.isSourceEditingMode ) { + this.isSourceEditingMode = isEnabled; + } + } ); this.listenTo( editor, 'change:isReadOnly', ( evt, name, isReadOnly ) => this._handleReadOnlyMode( isReadOnly ) ); - - if ( editor.plugins.has( CommentsOnly ) ) { - this.listenTo( editor.plugins.get( CommentsOnly ), 'change:isEnabled', ( evt, name, isCommentsOnlyEnabled ) => { - if ( isCommentsOnlyEnabled ) { - this.isSourceEditingMode = false; - } - } ); - } } // Update the editor data while calling editor.getData() in the source editing mode. @@ -420,22 +415,31 @@ export default class SourceEditing extends Plugin { buttonView.bind( 'isOn' ).to( this, 'isSourceEditingMode' ); - if ( editor.plugins.has( CommentsOnly ) ) { - buttonView.bind( 'isEnabled' ).to( - this, 'isEnabled', - editor, 'isReadOnly', - editor.plugins.get( PendingActions ), 'hasAny', - editor.plugins.get( CommentsOnly ), 'isEnabled', - this._isSourceEditingButtonEnabled - ); - } else { - buttonView.bind( 'isEnabled' ).to( - this, 'isEnabled', - editor, 'isReadOnly', - editor.plugins.get( PendingActions ), 'hasAny', - this._isSourceEditingButtonEnabled - ); - } + // The button should be disabled if one of the following conditions is met: + buttonView.bind( 'isEnabled' ).to( + this, 'isEnabled', + editor, 'isReadOnly', + editor.plugins.get( PendingActions ), 'hasAny', + ( isEnabled, isEditorReadOnly, hasAnyPendingActions ) => { + // (1) The plugin itself is disabled. + if ( !isEnabled ) { + return false; + } + + // (2) The editor is in read-only mode. + if ( isEditorReadOnly ) { + return false; + } + + // (3) Any pending action is scheduled. It may change the model, so modifying the document source should be prevented + // until the model is finally set. + if ( hasAnyPendingActions ) { + return false; + } + + return true; + } + ); this.listenTo( buttonView, 'execute', () => { this.isSourceEditingMode = !this.isSourceEditingMode; @@ -443,34 +447,6 @@ export default class SourceEditing extends Plugin { return buttonView; } - - // The button should be disabled if one of the following conditions is met: - private _isSourceEditingButtonEnabled( - isEnabled: boolean, isEditorReadOnly: boolean, hasAnyPendingActions: boolean, isCommentsOnlyEnabled?: boolean - ): boolean { - // (1) The plugin itself is disabled. - if ( !isEnabled ) { - return false; - } - - // (2) The editor is in read-only mode. - if ( isEditorReadOnly ) { - return false; - } - - // (3) Any pending action is scheduled. It may change the model, so modifying the document source should be prevented - // until the model is finally set. - if ( hasAnyPendingActions ) { - return false; - } - - // (4) The ediotr is in comments-only mode - if ( isCommentsOnlyEnabled ) { - return false; - } - - return true; - } } /** From 30680777b3066d20162c44d0711bc0601f120c17 Mon Sep 17 00:00:00 2001 From: l-lejman Date: Wed, 8 Jan 2025 16:31:04 +0100 Subject: [PATCH 3/4] Add tests for close SourceEditing mode when mode is disabled. --- packages/ckeditor5-source-editing/tests/sourceediting.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/ckeditor5-source-editing/tests/sourceediting.js b/packages/ckeditor5-source-editing/tests/sourceediting.js index adf1e9d29c0..87b6156a774 100644 --- a/packages/ckeditor5-source-editing/tests/sourceediting.js +++ b/packages/ckeditor5-source-editing/tests/sourceediting.js @@ -425,16 +425,11 @@ describe( 'SourceEditing', () => { it( 'should disable textarea if plugin is disabled', () => { button.fire( 'execute' ); - const domRoot = editor.editing.view.getDomRoot(); - const textarea = domRoot.nextSibling.children[ 0 ]; - plugin.forceDisabled( 'disablePlugin' ); - expect( textarea.readOnly ).to.be.true; + expect( plugin.isSourceEditingMode ).to.be.false; plugin.clearForceDisabled( 'disablePlugin' ); - - expect( textarea.readOnly ).to.be.false; } ); it( 'should remember replaced roots', () => { From c829472869322b14d987e113fef0b71d93b25bd5 Mon Sep 17 00:00:00 2001 From: l-lejman Date: Wed, 8 Jan 2025 16:35:06 +0100 Subject: [PATCH 4/4] Add tests for close SourceEditing mode when mode is disabled. --- packages/ckeditor5-source-editing/tests/sourceediting.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ckeditor5-source-editing/tests/sourceediting.js b/packages/ckeditor5-source-editing/tests/sourceediting.js index 87b6156a774..ddcd1ad6661 100644 --- a/packages/ckeditor5-source-editing/tests/sourceediting.js +++ b/packages/ckeditor5-source-editing/tests/sourceediting.js @@ -422,7 +422,7 @@ describe( 'SourceEditing', () => { expect( textarea.readOnly ).to.be.false; } ); - it( 'should disable textarea if plugin is disabled', () => { + it( 'should close source editing mode if plugin is disabled', () => { button.fire( 'execute' ); plugin.forceDisabled( 'disablePlugin' );