From b2fc6919631e96e07369ffd02b82486ab93bf25f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Remiszewski?= Date: Wed, 29 Mar 2023 09:54:14 +0200 Subject: [PATCH 1/3] Remove underscore from _updateEditorData method name and make it public. --- .../src/sourceediting.ts | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/ckeditor5-source-editing/src/sourceediting.ts b/packages/ckeditor5-source-editing/src/sourceediting.ts index b654f8d5b37..1b07f0554f2 100644 --- a/packages/ckeditor5-source-editing/src/sourceediting.ts +++ b/packages/ckeditor5-source-editing/src/sourceediting.ts @@ -9,7 +9,7 @@ /* global console */ -import { type Editor, Plugin, PendingActions } from 'ckeditor5/src/core'; +import { type ContextPluginDependencies, type Editor, Plugin, PendingActions } from 'ckeditor5/src/core'; import { ButtonView } from 'ckeditor5/src/ui'; import { createElement, ElementReplacer } from 'ckeditor5/src/utils'; import { formatHtml } from './utils/formathtml'; @@ -38,7 +38,7 @@ export default class SourceEditing extends Plugin { /** * @inheritDoc */ - public static get requires() { + public static get requires(): ContextPluginDependencies { return [ PendingActions ] as const; } @@ -150,7 +150,7 @@ export default class SourceEditing extends Plugin { // Update the editor data while calling editor.getData() in the source editing mode. editor.data.on( 'get', () => { if ( this.isSourceEditingMode ) { - this._updateEditorData(); + this.updateEditorData(); } }, { priority: 'high' } ); } @@ -187,6 +187,29 @@ export default class SourceEditing extends Plugin { } } + /** + * Updates the source data in all hidden editing roots. + */ + public updateEditorData(): void { + const editor = this.editor; + const data: Record = {}; + + for ( const [ rootName, domSourceEditingElementWrapper ] of this._replacedRoots ) { + const oldData = this._dataFromRoots.get( rootName ); + const newData = domSourceEditingElementWrapper.dataset.value!; + + // Do not set the data unless some changes have been made in the meantime. + // This prevents empty undo steps after switching to the normal editor. + if ( oldData !== newData ) { + data[ rootName ] = newData; + } + } + + if ( Object.keys( data ).length ) { + editor.data.set( data, { batchType: { isUndoable: true } } ); + } + } + /** * Creates source editing wrappers that replace each editing root. Each wrapper contains the document source from the corresponding * root. @@ -262,7 +285,7 @@ export default class SourceEditing extends Plugin { const editor = this.editor; const editingView = editor.editing.view; - this._updateEditorData(); + this.updateEditorData(); editingView.change( writer => { for ( const [ rootName ] of this._replacedRoots ) { @@ -278,29 +301,6 @@ export default class SourceEditing extends Plugin { editingView.focus(); } - /** - * Updates the source data in all hidden editing roots. - */ - private _updateEditorData(): void { - const editor = this.editor; - const data: Record = {}; - - for ( const [ rootName, domSourceEditingElementWrapper ] of this._replacedRoots ) { - const oldData = this._dataFromRoots.get( rootName ); - const newData = domSourceEditingElementWrapper.dataset.value!; - - // Do not set the data unless some changes have been made in the meantime. - // This prevents empty undo steps after switching to the normal editor. - if ( oldData !== newData ) { - data[ rootName ] = newData; - } - } - - if ( Object.keys( data ).length ) { - editor.data.set( data, { batchType: { isUndoable: true } } ); - } - } - /** * Focuses the textarea containing document source from the first editing root. */ From a8503fcb4223ebf095fddb9ced559961adc12e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Remiszewski?= Date: Fri, 31 Mar 2023 09:17:46 +0200 Subject: [PATCH 2/3] Add test for updateEditorData. --- .../tests/sourceediting.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/ckeditor5-source-editing/tests/sourceediting.js b/packages/ckeditor5-source-editing/tests/sourceediting.js index 870dd6e997a..c967e2d7c68 100644 --- a/packages/ckeditor5-source-editing/tests/sourceediting.js +++ b/packages/ckeditor5-source-editing/tests/sourceediting.js @@ -514,6 +514,22 @@ describe( 'SourceEditing', () => { } ); } ); + describe( 'updateEditorData', () => { + it( 'should update editor model when called', () => { + button.fire( 'execute' ); + + const domRoot = editor.editing.view.getDomRoot(); + const textarea = domRoot.nextSibling.children[ 0 ]; + + textarea.value = 'bar'; + textarea.dispatchEvent( new Event( 'input' ) ); + + expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( 'Foo' ); + plugin.updateEditorData(); + expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( 'bar' ); + } ); + } ); + describe( 'integration with undo', () => { it( 'should preserve the undo/redo stacks when no changes has been in the source editing mode', () => { editor.model.change( writer => { From 70de37f7c59f0ade9f3898559d0422156e5625e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Remiszewski?= Date: Fri, 31 Mar 2023 13:34:40 +0200 Subject: [PATCH 3/3] Remove return type on `requires`. --- packages/ckeditor5-source-editing/src/sourceediting.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ckeditor5-source-editing/src/sourceediting.ts b/packages/ckeditor5-source-editing/src/sourceediting.ts index 1b07f0554f2..3ef946870d0 100644 --- a/packages/ckeditor5-source-editing/src/sourceediting.ts +++ b/packages/ckeditor5-source-editing/src/sourceediting.ts @@ -9,7 +9,7 @@ /* global console */ -import { type ContextPluginDependencies, type Editor, Plugin, PendingActions } from 'ckeditor5/src/core'; +import { type Editor, Plugin, PendingActions } from 'ckeditor5/src/core'; import { ButtonView } from 'ckeditor5/src/ui'; import { createElement, ElementReplacer } from 'ckeditor5/src/utils'; import { formatHtml } from './utils/formathtml'; @@ -38,7 +38,7 @@ export default class SourceEditing extends Plugin { /** * @inheritDoc */ - public static get requires(): ContextPluginDependencies { + public static get requires() { return [ PendingActions ] as const; }