Skip to content

Commit

Permalink
Merge pull request #13784 from ckeditor/ck/11008-allow-saving-data-in…
Browse files Browse the repository at this point in the history
…-source-mode

Feature (source-editing): Make `SourceEditing.updateEditorData` method public to allow saving source editing data independently of the UI. Closes #11008.
  • Loading branch information
arkflpc authored Apr 3, 2023
2 parents 90445f8 + 70de37f commit acc02f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
50 changes: 25 additions & 25 deletions packages/ckeditor5-source-editing/src/sourceediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } );
}
Expand Down Expand Up @@ -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<string, string> = {};

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.
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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<string, string> = {};

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.
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/ckeditor5-source-editing/tests/sourceediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<paragraph>Foo</paragraph>' );
plugin.updateEditorData();
expect( getData( editor.model, { withoutSelection: true } ) ).to.equal( '<paragraph>bar</paragraph>' );
} );
} );

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 => {
Expand Down

0 comments on commit acc02f4

Please sign in to comment.