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

Undo by pressing backspace: AutoFormat #10508

Merged
merged 4 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions packages/ckeditor5-autoformat/src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import { Plugin } from 'ckeditor5/src/core';
import { Delete } from 'ckeditor5/src/typing';

import blockAutoformatEditing from './blockautoformatediting';
import inlineAutoformatEditing from './inlineautoformatediting';
Expand All @@ -21,6 +22,13 @@ import inlineAutoformatEditing from './inlineautoformatediting';
* @extends module:core/plugin~Plugin
*/
export default class Autoformat extends Plugin {
/**
* @inheritdoc
*/
static get requires() {
return [ Delete ];
}

/**
* @inheritDoc
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/ckeditor5-autoformat/src/blockautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export default function blockAutoformatEditing( editor, plugin, pattern, callbac
}
}
range.detach();

editor.model.enqueueChange( () => {
editor.plugins.get( 'Delete' ).requestUndoOnBackspace();
} );
} );
} );
}
4 changes: 4 additions & 0 deletions packages/ckeditor5-autoformat/src/inlineautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ export default function inlineAutoformatEditing( editor, plugin, testRegexpOrCal
for ( const range of rangesToRemove.reverse() ) {
writer.remove( range );
}

model.enqueueChange( () => {
editor.plugins.get( 'Delete' ).requestUndoOnBackspace();
} );
} );
} );
}
Expand Down
127 changes: 103 additions & 24 deletions packages/ckeditor5-autoformat/tests/undointegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,28 @@ import StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethr
import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Delete from '@ckeditor/ckeditor5-typing/src/delete';
import Undo from '@ckeditor/ckeditor5-undo/src/undoediting';

import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';

import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { DomEventData } from '@ckeditor/ckeditor5-engine';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'Autoformat undo integration', () => {
let editor, model, doc;

testUtils.createSinonSandbox();

beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [
Enter,
Undo,
Paragraph,
Autoformat,
ListEditing,
HeadingEditing,
BoldEditing,
ItalicEditing,
CodeEditing,
StrikethroughEditing,
BlockQuoteEditing
]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
} );
} );

afterEach( () => {
return editor.destroy();
} );

describe( 'inline', () => {
beforeEach( createVirtualEditorInstance );

it( 'should undo replacing "**" with bold', () => {
setData( model, '<paragraph>**foobar*[]</paragraph>' );
model.change( writer => {
Expand Down Expand Up @@ -123,6 +104,8 @@ describe( 'Autoformat undo integration', () => {
} );

describe( 'block', () => {
beforeEach( createVirtualEditorInstance );

it( 'should work when replacing asterisk', () => {
setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand Down Expand Up @@ -203,4 +186,100 @@ describe( 'Autoformat undo integration', () => {
expect( getData( model ) ).to.equal( '<paragraph>> []</paragraph>' );
} );
} );

describe( 'by pressing backspace', () => {
let viewDocument, deleteEvent;

beforeEach( async () => {
const newEditor = await ModelTestEditor
.create( {
plugins: [
Autoformat,
Paragraph,
BoldEditing,
ListEditing,
Delete,
Undo
]
} );

editor = newEditor;
model = editor.model;
doc = model.document;
viewDocument = editor.editing.view.document;
deleteEvent = new DomEventData(
viewDocument,
{ preventDefault: sinon.spy() },
{ direction: 'backward', unit: 'codePoint', sequence: 1 }
);
} );

it( 'should undo after inline autoformat', () => {
setData( model, '<paragraph>**foobar*[]</paragraph>' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );

viewDocument.fire( 'delete', deleteEvent );

expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
} );

it( 'should undo after block autoformat', () => {
setData( model, '<paragraph>-[]</paragraph>' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">[]</listItem>' );

viewDocument.fire( 'delete', deleteEvent );

expect( getData( model ) ).to.equal( '<paragraph>- []</paragraph>' );
} );

it( 'should not undo after selection has changed', () => {
setData( model, '<paragraph>**foobar*[]</paragraph>' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to.equal( '<paragraph><$text bold="true">foobar</$text>[]</paragraph>' );

model.change( writer => {
const selection = model.createSelection();
writer.setSelection( selection );
} );

viewDocument.fire( 'delete', deleteEvent );

expect( getData( model, { withoutSelection: true } ) )
.to.equal( '<paragraph><$text bold="true">foobar</$text></paragraph>' );
} );
} );

async function createVirtualEditorInstance() {
const newEditor = await VirtualTestEditor
.create( {
plugins: [
Enter,
Undo,
Paragraph,
Autoformat,
ListEditing,
HeadingEditing,
BoldEditing,
ItalicEditing,
CodeEditing,
StrikethroughEditing,
BlockQuoteEditing
]
} );

editor = newEditor;
model = editor.model;
doc = model.document;
}
} );