/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import ModelTestEditor from '/tests/core/_utils/modeltesteditor.js'; import Range from '/ckeditor5/engine/model/range.js'; import Position from '/ckeditor5/engine/model/position.js'; import UndoEngine from '/ckeditor5/undo/undoengine.js'; import { setData, getData } from '/tests/engine/_utils/model.js'; import deleteContents from '/ckeditor5/engine/model/composer/deletecontents.js'; let editor, doc, root; beforeEach( () => { return ModelTestEditor.create( { features: [ UndoEngine ] } ) .then( newEditor => { editor = newEditor; doc = editor.document; root = doc.getRoot(); } ); } ); function setSelection( pathA, pathB ) { doc.selection.setRanges( [ new Range( new Position( root, pathA ), new Position( root, pathB ) ) ] ); } function input( input ) { setData( doc, input ); } function output( output ) { expect( getData( doc ) ).to.equal( output ); } function undoDisabled() { expect( editor.commands.get( 'undo' ).isEnabled ).to.be.false; } describe( 'UndoEngine integration', () => { describe( 'adding and removing content', () => { it( 'add and undo', () => { input( '
fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); editor.execute( 'undo' ); output( 'fo
bar
' ); undoDisabled(); } ); it( 'multiple adding and undo', () => { input( 'fo
bar
' ); doc.batch() .insert( doc.selection.getFirstPosition(), 'zzz' ) .insert( new Position( root, [ 1, 0 ] ), 'xxx' ); output( 'fozzz
xxxbar
' ); setSelection( [ 1, 0 ], [ 1, 0 ] ); doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' ); output( 'fozzzo
yyy
fozzzo
fo
bar
' ); undoDisabled(); } ); it( 'multiple adding mixed with undo', () => { input( 'fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); setSelection( [ 1, 0 ], [ 1, 0 ] ); doc.batch().insert( doc.selection.getFirstPosition(), 'yyy' ); output( 'fozzzo
yyy
fozzzo
xxx
bar
' ); editor.execute( 'undo' ); output( 'bar
' ); editor.execute( 'undo' ); output( 'fo
bar
' ); undoDisabled(); } ); it( 'multiple remove and undo', () => { input( 'bar
' ); doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) ); output( 'bar
' ); setSelection( [ 1, 1 ], [ 1, 1 ] ); doc.batch().remove( Range.createFromPositionAndShift( doc.selection.getFirstPosition(), 2 ) ); output( 'o
b
o
b
bar
' ); undoDisabled(); } ); it( 'add and remove different parts and undo', () => { input( 'fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); setSelection( [ 1, 2 ], [ 1, 2 ] ); doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 1, 1 ] ) , 1 ) ); output( 'fozzzo
b
fozzzo
ba
fo
bar
' ); undoDisabled(); } ); it( 'add and remove same part and undo', () => { input( 'fo
bar
' ); doc.batch().insert( doc.selection.getFirstPosition(), 'zzz' ); output( 'fozzz
bar
' ); doc.batch().remove( Range.createFromPositionAndShift( new Position( root, [ 0, 2 ] ) , 3 ) ); output( 'fo
bar
' ); editor.execute( 'undo' ); output( 'fozzz
bar
' ); editor.execute( 'undo' ); output( 'fo
bar
' ); undoDisabled(); } ); } ); describe( 'moving', () => { it( 'move same content twice then undo', () => { input( 'f
bar
' ); doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) ); output( 'fz
fz
bar
' ); editor.execute( 'undo' ); output( 'fz
f
bar
' ); undoDisabled(); } ); it( 'move content and new parent then undo', () => { input( 'f
bar
' ); doc.batch().move( doc.selection.getFirstRange(), new Position( root, [ 1, 0 ] ) ); output( 'fz
obar
fz
' ); editor.execute( 'undo' ); output( 'fz
obar
f
bar
' ); undoDisabled(); } ); } ); describe( 'attributes with other', () => { it( 'attributes then insert inside then undo', () => { input( 'fo
fo
fo<$text bold=true>o$text>zzz
fo<$text bold=true>o
fo
zb
zb
b
ar' ); editor.execute( 'undo' ); output( 'fofoo
foo
foo
it ends up in graveyard. We have to manually move it to correct node. setSelection( [ 0, 3 ], [ 0, 3 ] ); output( '
foo
foo
foo
it ends up in wrong node. We have to manually move it to correct node. setSelection( [ 1, 0 ], [ 1, 0 ] ); output( '
foo
foo
fo
bar
' ); deleteContents( doc.batch(), doc.selection, { merge: true } ); output( 'fo
fo
bar
' ); } ); } ); } );