Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #88 from ckeditor/t/86
Browse files Browse the repository at this point in the history
Fix: `InputCommand` now accepts `Range` instead of `Position` as a parameter. Closes #86. Closes #54.
BREAKING CHANGE: `InputCommand` `options.resultPosition` was replaced with `options.resultRange`.
  • Loading branch information
f1ames authored Mar 13, 2017
2 parents 49cfe9c + a7c56f8 commit 0766407
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@ckeditor/ckeditor5-heading": "^0.8.0",
"@ckeditor/ckeditor5-paragraph": "^0.6.1",
"@ckeditor/ckeditor5-undo": "^0.7.1",
"@ckeditor/ckeditor5-presets": "^0.1.1",
"gulp": "^3.9.0",
"guppy-pre-commit": "^0.4.0"
},
Expand Down
6 changes: 3 additions & 3 deletions src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ class MutationHandler {
}

// Try setting new model selection according to passed view selection.
let modelSelectionPosition = null;
let modelSelectionRange = null;

if ( viewSelection ) {
modelSelectionPosition = this.editing.mapper.toModelPosition( viewSelection.anchor );
modelSelectionRange = this.editing.mapper.toModelRange( viewSelection.getFirstRange() );
}

// Get the position in view and model where the changes will happen.
Expand All @@ -199,7 +199,7 @@ class MutationHandler {
this.editor.execute( 'input', {
text: insertText,
range: removeRange,
resultPosition: modelSelectionPosition
resultRange: modelSelectionRange
} );
}

Expand Down
8 changes: 4 additions & 4 deletions src/inputcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class InputCommand extends Command {
* @param {String} [options.text=''] Text to be inserted.
* @param {module:engine/model/range~Range} [options.range] Range in which the text is inserted. Defaults
* to the first range in the current selection.
* @param {module:engine/model/position~Position} [options.resultPosition] Position at which the selection
* @param {module:engine/model/range~Range} [options.resultRange] Range at which the selection
* should be placed after the insertion. If not specified, the selection will be placed right after
* the inserted text.
*/
Expand All @@ -73,7 +73,7 @@ export default class InputCommand extends Command {
const text = options.text || '';
const textInsertions = text.length;
const range = options.range || doc.selection.getFirstRange();
const resultPosition = options.resultPosition;
const resultRange = options.resultRange;

doc.enqueueChanges( () => {
const isCollapsedRange = range.isCollapsed;
Expand All @@ -86,8 +86,8 @@ export default class InputCommand extends Command {

this._buffer.batch.weakInsert( range.start, text );

if ( resultPosition ) {
this.editor.data.model.selection.collapse( resultPosition );
if ( resultRange ) {
this.editor.data.model.selection.setRanges( [ resultRange ] );
} else if ( isCollapsedRange ) {
// If range was collapsed just shift the selection by the number of inserted characters.
this.editor.data.model.selection.collapse( range.start.getShiftedBy( textInsertions ) );
Expand Down
21 changes: 21 additions & 0 deletions tests/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,27 @@ describe( 'Input feature', () => {
expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
} );

it( 'should place non-collapsed selection after changing single character (composition)', () => {
editor.setData( '<p>Foo house</p>' );

const viewSelection = new ViewSelection();
viewSelection.collapse( viewRoot.getChild( 0 ).getChild( 0 ), 8 );
viewSelection.setFocus( viewRoot.getChild( 0 ).getChild( 0 ), 9 );

view.fire( 'mutations',
[ {
type: 'text',
oldText: 'Foo house',
newText: 'Foo housa',
node: viewRoot.getChild( 0 ).getChild( 0 )
} ],
viewSelection
);

expect( getModelData( model ) ).to.equal( '<paragraph>Foo hous[a]</paragraph>' );
expect( getViewData( view ) ).to.equal( '<p>Foo hous{a}</p>' );
} );

it( 'should replace last &nbsp; with space', () => {
model.enqueueChanges( () => {
model.selection.setRanges( [
Expand Down
26 changes: 26 additions & 0 deletions tests/inputcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtest
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import Range from '@ckeditor/ckeditor5-engine/src/model/range';
import Position from '@ckeditor/ckeditor5-engine/src/model/position';
import InputCommand from '../src/inputcommand';
import ChangeBuffer from '../src/changebuffer';
import Input from '../src/input';
Expand Down Expand Up @@ -168,6 +170,30 @@ describe( 'InputCommand', () => {
expect( buffer.size ).to.be.equal( 0 );
} );

it( 'should set selection according to passed resultRange (collapsed)', () => {
setData( doc, '<p>[foo]bar</p>' );

editor.execute( 'input', {
text: 'new',
resultRange: new Range( new Position( doc.getRoot(), [ 0, 5 ] ) )
} );

expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>newba[]r</p>' );
expect( buffer.size ).to.be.equal( 3 );
} );

it( 'should set selection according to passed resultRange (non-collapsed)', () => {
setData( doc, '<p>[foo]bar</p>' );

editor.execute( 'input', {
text: 'new',
resultRange: new Range( new Position( doc.getRoot(), [ 0, 3 ] ), new Position( doc.getRoot(), [ 0, 6 ] ) )
} );

expect( getData( doc, { selection: true } ) ).to.be.equal( '<p>new[bar]</p>' );
expect( buffer.size ).to.be.equal( 3 );
} );

it( 'only removes content when no text given (with default non-collapsed range)', () => {
setData( doc, '<p>[fo]obar</p>' );

Expand Down
6 changes: 2 additions & 4 deletions tests/manual/20/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
6 changes: 2 additions & 4 deletions tests/manual/21/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
6 changes: 2 additions & 4 deletions tests/manual/82/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

window.setInterval( function() {
console.log( getData( window.editor.document ) );
}, 3000 );

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Heading ],
plugins: [ EssentialsPreset, Paragraph, Heading ],
toolbar: [ 'headings', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
3 changes: 3 additions & 0 deletions tests/manual/86/1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="editor">
<p>This is an editor instance.</p>
</div>
26 changes: 26 additions & 0 deletions tests/manual/86/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

window.setInterval( function() {
console.log( getData( window.editor.document ) );
}, 3000 );

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ EssentialsPreset, Paragraph ],
toolbar: [ 'undo', 'redo' ]
} )
.then( editor => {
window.editor = editor;
} )
.catch( err => {
console.error( err.stack );
} );
5 changes: 5 additions & 0 deletions tests/manual/86/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Typing - MacOS accent balloon

It is possible to navigate with arrow keys inside MacOS balloon panel and insert a selected accent
(long "a" press to activate accent balloon).

6 changes: 2 additions & 4 deletions tests/manual/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

window.getData = getData;
Expand All @@ -22,7 +20,7 @@ window.setInterval( function() {
}, 3000 );

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
6 changes: 2 additions & 4 deletions tests/manual/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

window.setInterval( function() {
console.log( getData( window.editor.document ) );
}, 3000 );

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
6 changes: 2 additions & 4 deletions tests/manual/rtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

const config = {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
};

Expand Down
6 changes: 2 additions & 4 deletions tests/manual/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
/* globals console, document, window */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold ],
plugins: [ EssentialsPreset, Paragraph, Bold ],
toolbar: [ 'bold' ]
} )
.then( editor => {
Expand Down
6 changes: 2 additions & 4 deletions tests/manual/spellchecking.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
/* globals console, window, document */

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classic';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '../../src/typing';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import EssentialsPreset from '@ckeditor/ckeditor5-presets/src/essentials';
import { getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

window.setInterval( function() {
console.log( getData( window.editor.document ) );
}, 3000 );

ClassicEditor.create( document.querySelector( '#editor' ), {
plugins: [ Enter, Typing, Paragraph, Undo, Bold, Italic, Heading ],
plugins: [ EssentialsPreset, Paragraph, Bold, Italic, Heading ],
toolbar: [ 'headings', 'bold', 'italic', 'undo', 'redo' ]
} )
.then( editor => {
Expand Down
3 changes: 1 addition & 2 deletions tests/manual/spellchecking.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ Try to correct all misspelled words using native spell checking mechanism in the

* Words should be corrected and selection placed after corrected word.

_In Safari selection is placed at the beginning of the corrected word
(this is a known issue [ckeditor5-typing/#54](https://github.com/ckeditor/ckeditor5-typing/issues/54))_.
_In Safari selection contains whole corrected word (same as in native contenteditable)_.

0 comments on commit 0766407

Please sign in to comment.