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

T/86 InputCommand should accept range instead of position as a parameter. #88

Merged
merged 7 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
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
22 changes: 22 additions & 0 deletions tests/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,28 @@ describe( 'Input feature', () => {
expect( getViewData( view ) ).to.equal( '<p>Foo house{}</p>' );
} );

it( 'should place non-collapsed selection after changing single character (composition)', () => {
// This test case emulates spellchecker correction.
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
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>
28 changes: 28 additions & 0 deletions tests/manual/86/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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 Enter from '@ckeditor/ckeditor5-enter/src/enter';
Copy link
Member

Choose a reason for hiding this comment

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

Use the essential preset + paragraph instead of these 4 plugins.

Copy link
Member

Choose a reason for hiding this comment

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

You can also update all other manual tests.

import Typing from '../../../src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
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 ],
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).

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)_.