This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
T/61: Improved the typing when the whole content is selected #111
Merged
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c3ef549
Improved the typing – when the user held the Backspace or pressed the…
22bc2ab
Added more TCs to manual tests. [skip ci]
3380d32
Improved docs. [skip ci]
6558d59
Simplified the private API of `DeleteCommand#_shouldEntireContentBeRe…
ef6a30e
Simplified the code in order to avoid repeating the code with the eng…
74e77f2
Merge branch 'master' into t/61
60bccf5
Revert: ef6a30e. It was not good idea to follow this way.
bcd3e32
Merge branch 'master' into t/61
291f0db
Removed "getLimitElement()" function because it is already available …
825466a
Merge branch 'master' into t/61
f0c2230
Merge branch 'master' into t/61
723ff29
Merge branch 'master' into t/61
52cde47
Simplified the code (used "Selection#isEntireContentSelected()").
b20e509
Merge branch 'master' into t/61
4b364b9
Code style.
Reinmar 5e1608b
Improved docs.
Reinmar 2b254e7
Code refactoring – fixed semantics.
Reinmar 66e9310
Merge branch 'master' into t/61
Reinmar 45e9beb
Aligned Selection usage to changes in the engine.
Reinmar 54b4ad8
Improved the docs and adjusted to changes in engine (Selection API).
3347a7d
Removed additional options from "getData()" function.
e1d027a
Removed invalid comment. [skip ci]
e798b83
Merge branch 'master' into t/61
Reinmar 7c03010
Code style.
Reinmar 7bf6fed
Fixed tests.
Reinmar 9f57e6f
Improved comments.
Reinmar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
|
||
import Command from '@ckeditor/ckeditor5-core/src/command'; | ||
import Selection from '@ckeditor/ckeditor5-engine/src/model/selection'; | ||
import Element from '@ckeditor/ckeditor5-engine/src/model/element'; | ||
import Position from '@ckeditor/ckeditor5-engine/src/model/position'; | ||
import Range from '@ckeditor/ckeditor5-engine/src/model/range'; | ||
import ChangeBuffer from './changebuffer'; | ||
import count from '@ckeditor/ckeditor5-utils/src/count'; | ||
|
||
|
@@ -54,8 +57,8 @@ export default class DeleteCommand extends Command { | |
* | ||
* @fires execute | ||
* @param {Object} [options] The command options. | ||
* @param {'character'} [options.unit='character'] See {@link module:engine/controller/modifyselection~modifySelection}'s | ||
* options. | ||
* @param {'character'} [options.unit='character'] See {@link module:engine/controller/modifyselection~modifySelection}'s options. | ||
* @param {Number} [options.sequence=1] See the {@link module:engine/view/document~Document#event:delete} event data. | ||
*/ | ||
execute( options = {} ) { | ||
const doc = this.editor.document; | ||
|
@@ -73,6 +76,12 @@ export default class DeleteCommand extends Command { | |
|
||
// If selection is still collapsed, then there's nothing to delete. | ||
if ( selection.isCollapsed ) { | ||
const sequence = options.sequence || 1; | ||
|
||
if ( this._shouldEntireContentBeReplacedWithParagraph( { sequence } ) ) { | ||
this._replaceEntireContentWithParagraph(); | ||
} | ||
|
||
return; | ||
} | ||
|
||
|
@@ -92,4 +101,81 @@ export default class DeleteCommand extends Command { | |
this._buffer.unlock(); | ||
} ); | ||
} | ||
|
||
/** | ||
* If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key, we do nothing because the user can clear | ||
* the whole element without removing them. | ||
* | ||
* But, if the user pressed and released the key, we want to replace the entire content with a paragraph if: | ||
* - the entire content is selected, | ||
* - the paragraph is allowed in the common ancestor, | ||
* - other paragraph does not occur in the editor. | ||
* | ||
* @private | ||
* @param {Object} options | ||
* @param {Number} options.sequence A number that describes which sequence of the same event is fired. | ||
* @returns {Boolean} | ||
*/ | ||
_shouldEntireContentBeReplacedWithParagraph( options ) { | ||
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. It's a private method so just pass the sequence as its direct param. We don't need the options object right now here and since it's private we can do whatever we want with the params in the future. |
||
// Does nothing if user pressed and held the "Backspace" or "Delete" key. | ||
if ( options.sequence > 1 ) { | ||
return false; | ||
} | ||
|
||
const document = this.editor.document; | ||
const selection = document.selection; | ||
const limitElement = getLimitElement( document.schema, selection ); | ||
const limitStartPosition = Position.createAt( limitElement ); | ||
const limitEndPosition = Position.createAt( limitElement, 'end' ); | ||
|
||
if ( | ||
!limitStartPosition.isTouching( selection.getFirstPosition() ) || | ||
!limitEndPosition.isTouching( selection.getLastPosition() ) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( !document.schema.check( { name: 'paragraph', inside: limitElement.name } ) ) { | ||
return false; | ||
} | ||
|
||
// Does nothing if editor contains an empty paragraph. | ||
if ( selection.getFirstRange().getCommonAncestor().name === 'paragraph' ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* The entire content is replaced with the paragraph. Selection is moved inside the paragraph. | ||
* | ||
* @private | ||
*/ | ||
_replaceEntireContentWithParagraph() { | ||
const document = this.editor.document; | ||
const selection = document.selection; | ||
const limitElement = getLimitElement( document.schema, selection ); | ||
const paragraph = new Element( 'paragraph' ); | ||
|
||
this._buffer.batch.remove( Range.createIn( limitElement ) ); | ||
this._buffer.batch.insert( Position.createAt( limitElement ), paragraph ); | ||
|
||
selection.collapse( paragraph ); | ||
} | ||
} | ||
|
||
// Returns the lowest limit element defined in `Schema.limits` for passed selection. | ||
function getLimitElement( schema, selection ) { | ||
let element = selection.getFirstRange().getCommonAncestor(); | ||
|
||
while ( !schema.limits.has( element.name ) ) { | ||
if ( element.parent ) { | ||
element = element.parent; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return element; | ||
} |
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 |
---|---|---|
|
@@ -20,6 +20,14 @@ export default class DeleteObserver extends Observer { | |
constructor( document ) { | ||
super( document ); | ||
|
||
let sequence = 0; | ||
|
||
document.on( 'keyup', ( evt, data ) => { | ||
if ( data.keyCode == keyCodes.delete || data.keyCode == keyCodes.backspace ) { | ||
sequence = 0; | ||
} | ||
} ); | ||
|
||
document.on( 'keydown', ( evt, data ) => { | ||
const deleteData = {}; | ||
|
||
|
@@ -34,6 +42,7 @@ export default class DeleteObserver extends Observer { | |
} | ||
|
||
deleteData.unit = data.altKey ? 'word' : deleteData.unit; | ||
deleteData.sequence = ++sequence; | ||
|
||
document.fire( 'delete', new DomEventData( document, data.domEvent, deleteData ) ); | ||
} ); | ||
|
@@ -55,4 +64,6 @@ export default class DeleteObserver extends Observer { | |
* @param {module:engine/view/observer/domeventdata~DomEventData} data | ||
* @param {'forward'|'delete'} data.direction The direction in which the deletion should happen. | ||
* @param {'character'|'word'} data.unit The "amount" of content that should be deleted. | ||
* @param {Number} data.sequence A number that describes which sequence of the same event is fired. | ||
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. A number describing which subsequent delete event it is without the key being released. If it's 2 or more it means that the key was pressed and hold. |
||
* It helps detect the key was pressed and held. | ||
*/ |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Blank line before a list in MD. And start items from
*
(without additional indentation).