-
Notifications
You must be signed in to change notification settings - Fork 40
T/ckeditor5/1243: Unify widget insertion #1545
Changes from 8 commits
7cd3f5e
97cb888
4c233e5
b91dc3b
e0ba244
f18462a
b00ca26
dcb0c54
6163265
abfff1f
259bd57
6e779db
63e4f18
aa31f0f
e95e695
3cb0b59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -298,13 +298,29 @@ export default class Model { | |
* | ||
* editor.model.insertContent( modelFragment, editor.model.document.selection ); | ||
* | ||
* If an instance of {module:engine/model/selection~Selection} is passed as `selectable` it will be modified | ||
* to the insertion selection (equal to a range to be selected after insertion). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd propose something like:
|
||
* | ||
* // Insert text replacing given selection instance. | ||
* const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 5 ] ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's maybe use a more human readable method, like – Also, I don't think that const selection = new Selection( paragraph, 'in' ); |
||
* | ||
* editor.model.change( ( writer ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No parenthesis around |
||
* editor.model.insertContent( model, writer.createText( 'x' ), selection ); | ||
* | ||
* // insertContent() modifies the passed selection instance so it can be used to set the document selection. | ||
* // Note: This is not necessary when you passed document selection to insertContent(). | ||
* writer.setSelection( selection ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* } ); | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sample with range/position is missing. |
||
* @fires insertContent | ||
* @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert. | ||
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection | ||
* Selection into which the content should be inserted. | ||
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection| | ||
* module:engine/model/position~Position|module:engine/model/element~Element| | ||
* Iterable.<module:engine/model/range~Range>|module:engine/model/range~Range|null} [selectable] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't it supposed to default to the document selection? If yes, make sure to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you made this change. So you also need to update these docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still missing the default value in the documentation:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bonus – I think that we should define a typedef called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah - I though about this also - I can make it here though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* Selection into which the content should be inserted. If not provided the current model document selection will be used. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can add a note that if you use a selection object, it will be modified and you will be able to get the insertion selection. |
||
*/ | ||
insertContent( content, selection ) { | ||
insertContent( this, content, selection ); | ||
insertContent( content, selectable ) { | ||
insertContent( this, content, selectable ); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import Selection from '../../../src/model/selection'; | |
import Position from '../../../src/model/position'; | ||
|
||
import { setData, getData, parse } from '../../../src/dev-utils/model'; | ||
import Range from '../../../src/model/range'; | ||
|
||
describe( 'DataController utils', () => { | ||
let model, doc; | ||
|
@@ -26,7 +27,7 @@ describe( 'DataController utils', () => { | |
setData( model, 'x[]x' ); | ||
|
||
model.change( writer => { | ||
insertContent( model, new Text( 'a' ), doc.selection ); | ||
insertContent( model, new Text( 'a' ) ); | ||
expect( writer.batch.operations ).to.length( 1 ); | ||
} ); | ||
} ); | ||
|
@@ -47,6 +48,89 @@ describe( 'DataController utils', () => { | |
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests should check if the selection was modified. |
||
} ); | ||
|
||
it( 'should modify passed selection instance', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
doc.createRoot(); | ||
|
||
model.schema.extend( '$text', { allowIn: '$root' } ); | ||
setData( model, 'a[]bc' ); | ||
|
||
const selection = new Selection( new Position( doc.getRoot(), [ 2 ] ) ); | ||
const selectionCopy = new Selection( new Position( doc.getRoot(), [ 2 ] ) ); | ||
|
||
expect( selection.isEqual( selectionCopy ) ).to.be.true; | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ), selection ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the writer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest I've copied tests from other cases so should we change all the tests then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm... I think so. Just to make sure it's going to work with how other people will use it and to avoid confusing people who may see our tests. |
||
} ); | ||
|
||
expect( selection.isEqual( selectionCopy ) ).to.be.false; | ||
|
||
const insertionSelection = new Selection( new Position( doc.getRoot(), [ 3 ] ) ); | ||
expect( selection.isEqual( insertionSelection ) ).to.be.true; | ||
} ); | ||
|
||
it( 'should be able to insert content at custom position', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
doc.createRoot(); | ||
|
||
model.schema.extend( '$text', { allowIn: '$root' } ); | ||
setData( model, 'a[]bc' ); | ||
|
||
const position = new Position( doc.getRoot(), [ 2 ] ); | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ), position ); | ||
expect( getData( model ) ).to.equal( 'a[]bxc' ); | ||
} ); | ||
} ); | ||
|
||
it( 'should be able to insert content at custom range', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
doc.createRoot(); | ||
|
||
model.schema.extend( '$text', { allowIn: '$root' } ); | ||
setData( model, 'a[]bc' ); | ||
|
||
const range = new Range( new Position( doc.getRoot(), [ 2 ] ), new Position( doc.getRoot(), [ 3 ] ) ); | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ), range ); | ||
expect( getData( model ) ).to.equal( 'a[]bx' ); | ||
} ); | ||
} ); | ||
|
||
it( 'should be able to insert content at model selection if document selection is passed', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
doc.createRoot(); | ||
|
||
model.schema.extend( '$text', { allowIn: '$root' } ); | ||
setData( model, 'a[]bc' ); | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ), model.document.selection ); | ||
expect( getData( model ) ).to.equal( 'ax[]bc' ); | ||
} ); | ||
} ); | ||
|
||
it( 'should be able to insert content at model selection if none passed', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
doc.createRoot(); | ||
|
||
model.schema.extend( '$text', { allowIn: '$root' } ); | ||
setData( model, 'a[]bc' ); | ||
|
||
model.change( () => { | ||
insertContent( model, new Text( 'x' ) ); | ||
expect( getData( model ) ).to.equal( 'ax[]bc' ); | ||
} ); | ||
} ); | ||
|
||
it( 'accepts DocumentFragment', () => { | ||
model = new Model(); | ||
doc = model.document; | ||
|
@@ -56,7 +140,7 @@ describe( 'DataController utils', () => { | |
|
||
setData( model, 'x[]x' ); | ||
|
||
insertContent( model, new DocumentFragment( [ new Text( 'a' ) ] ), doc.selection ); | ||
insertContent( model, new DocumentFragment( [ new Text( 'a' ) ] ) ); | ||
|
||
expect( getData( model ) ).to.equal( 'xa[]x' ); | ||
} ); | ||
|
@@ -70,7 +154,7 @@ describe( 'DataController utils', () => { | |
|
||
setData( model, 'x[]x' ); | ||
|
||
insertContent( model, new Text( 'a' ), doc.selection ); | ||
insertContent( model, new Text( 'a' ) ); | ||
|
||
expect( getData( model ) ).to.equal( 'xa[]x' ); | ||
} ); | ||
|
@@ -90,7 +174,7 @@ describe( 'DataController utils', () => { | |
|
||
setData( model, '<paragraph>foo[]</paragraph>' ); | ||
|
||
insertContent( model, content, doc.selection ); | ||
insertContent( model, content ); | ||
|
||
expect( doc.getRoot().getChild( 0 ).getChild( 1 ) ).to.equal( content ); | ||
} ); | ||
|
@@ -303,7 +387,7 @@ describe( 'DataController utils', () => { | |
] ); | ||
|
||
setData( model, '[<heading2>foo</heading2>]' ); | ||
insertContent( model, content, doc.selection ); | ||
insertContent( model, content ); | ||
expect( getData( model ) ).to.equal( '<heading1>bar[]</heading1>' ); | ||
} ); | ||
|
||
|
@@ -854,6 +938,6 @@ describe( 'DataController utils', () => { | |
} ); | ||
} | ||
|
||
insertContent( model, content, doc.selection ); | ||
insertContent( model, content ); | ||
} | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
editor.model.document.selection
should be removed, I believe.