Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove underscore from _updateEditorData method name and make it public. #13784

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions packages/ckeditor5-source-editing/src/sourceediting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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';
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class SourceEditing extends Plugin {
/**
* @inheritDoc
*/
public static get requires() {
public static get requires(): ContextPluginDependencies {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static get requires(): ContextPluginDependencies {
public static get requires() {

return [ PendingActions ] as const;
}

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