-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7237 from ckeditor/i/6775
Internal (widget): Integrated the `WidgetTypeAround` plugin with the read–only mode. Closes #6775. Internal (widget): Integrated the `WidgetTypeAround` plugin with the `RestrictedEditingMode` plugin (see #6775). Feature (paragraph): Implemented the `InsertParagraphCommand` registered as `'insertParagraph'` in the editor. Closes #6823. Closes #7229. Docs (engine): Improved `Model#insertContent` API documentation (see #6775).
- Loading branch information
Showing
12 changed files
with
295 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/ckeditor5-paragraph/src/insertparagraphcommand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
/** | ||
* @module paragraph/insertparagraphcommand | ||
*/ | ||
|
||
import Command from '@ckeditor/ckeditor5-core/src/command'; | ||
|
||
/** | ||
* The insert paragraph command. It inserts a new paragraph at a specific | ||
* {@link module:engine/model/position~Position document position}. | ||
* | ||
* // Insert a new paragraph before an element in the document. | ||
* editor.execute( 'insertParagraph', { | ||
* position: editor.model.createPositionBefore( element ) | ||
* } ); | ||
* | ||
* **Note**: This command moves the selection to the inserted paragraph. | ||
* | ||
* @extends module:core/command~Command | ||
*/ | ||
export default class InsertParagraphCommand extends Command { | ||
/** | ||
* Executes the command. | ||
* | ||
* @param {Object} options Options for the executed command. | ||
* @param {module:engine/model/position~Position} options.position The model position at which | ||
* the new paragraph will be inserted. | ||
* @fires execute | ||
*/ | ||
execute( options ) { | ||
const model = this.editor.model; | ||
|
||
if ( !model.schema.checkChild( options.position, 'paragraph' ) ) { | ||
return; | ||
} | ||
|
||
model.change( writer => { | ||
const paragraph = writer.createElement( 'paragraph' ); | ||
|
||
model.insertContent( paragraph, options.position ); | ||
|
||
writer.setSelection( paragraph, 'in' ); | ||
} ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/ckeditor5-paragraph/tests/insertparagraphcommand.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license | ||
*/ | ||
|
||
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; | ||
import InsertParagraphCommand from '../src/insertparagraphcommand'; | ||
|
||
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; | ||
|
||
describe( 'InsertParagraphCommand', () => { | ||
let editor, model, document, command, root, schema; | ||
|
||
beforeEach( () => { | ||
return ModelTestEditor.create().then( newEditor => { | ||
editor = newEditor; | ||
model = editor.model; | ||
document = model.document; | ||
schema = model.schema; | ||
command = new InsertParagraphCommand( editor ); | ||
root = document.getRoot(); | ||
|
||
editor.commands.add( 'insertParagraph', command ); | ||
schema.register( 'paragraph', { inheritAllFrom: '$block' } ); | ||
schema.register( 'heading1', { inheritAllFrom: '$block', allowIn: 'headersOnly' } ); | ||
schema.register( 'headersOnly', { inheritAllFrom: '$block' } ); | ||
} ); | ||
} ); | ||
|
||
afterEach( () => { | ||
command.destroy(); | ||
} ); | ||
|
||
describe( 'execute()', () => { | ||
it( 'should insert a paragraph at a specific document position and anchor the selection inside of it', () => { | ||
setData( model, '<heading1>foo[]</heading1>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionBefore( root.getChild( 0 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><heading1>foo</heading1>' ); | ||
} ); | ||
|
||
it( 'should do nothing if the paragraph is not allowed at the provided position', () => { | ||
setData( model, '<headersOnly><heading1>foo[]</heading1></headersOnly>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionBefore( root.getChild( 0 ).getChild( 0 ) ) | ||
} ); | ||
|
||
command.execute( { | ||
position: model.createPositionAfter( root.getChild( 0 ).getChild( 0 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<headersOnly><heading1>foo[]</heading1></headersOnly>' ); | ||
} ); | ||
|
||
describe( 'interation with existing paragraphs in the content', () => { | ||
it( 'should insert a paragraph before another paragraph', () => { | ||
setData( model, '<paragraph>foo[]</paragraph>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionBefore( root.getChild( 0 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' ); | ||
} ); | ||
|
||
it( 'should insert a paragraph after another paragraph', () => { | ||
setData( model, '<paragraph>foo[]</paragraph>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionAfter( root.getChild( 0 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' ); | ||
} ); | ||
|
||
it( 'should not merge with a paragraph that precedes the position at which a new paragraph is inserted', () => { | ||
setData( model, '<paragraph>bar</paragraph><heading1>foo[]</heading1>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionBefore( root.getChild( 1 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<paragraph>bar</paragraph><paragraph>[]</paragraph><heading1>foo</heading1>' ); | ||
} ); | ||
|
||
it( 'should not merge with a paragraph that follows the position at which a new paragraph is inserted', () => { | ||
setData( model, '<heading1>foo[]</heading1><paragraph>bar</paragraph>' ); | ||
|
||
command.execute( { | ||
position: model.createPositionAfter( root.getChild( 0 ) ) | ||
} ); | ||
|
||
expect( getData( model ) ).to.equal( '<heading1>foo</heading1><paragraph>[]</paragraph><paragraph>bar</paragraph>' ); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.