问题
I'm implementing a plugin and have a use case need to detect when user select all content. I trying create a select all selection like this const selectAllSelection = editor.model.createSelection(editor.model.document.getRoot(), 'in')
and compare with current model.document.selection
but they are not the same.
回答1:
Those selections are a bit different as the one created with createSelection()
will start directly in <$root>
. Real document selection will start inside the first <paragraph>
. That makes the comparison a bit tricky.
But, it's not that bad:
- check whether the selection has just one range,
- get the first range of document selection (
selection.getFirstRange()
) and usemodel.createRangeIn( editor.model.document.getRoot() )
to create a range inside the root, - the ranges will differ due to what I described, so we can't do
range.isEqual()
. We need to check the start and end positions withisTouching()
.
This would look more or less like this:
function doesSelectAll( selection ) {
if ( selection.rangeCount != 1 ) {
return false;
}
const firstSelectionRange = selection.getFirstRange();
const rootRange = editor.model.createRangeIn( editor.model.document.getRoot() );
return firstSelectionRange.start.isTouching( rootRange.start ) && firstSelectionRange.end.isTouching( rootRange.end );
}
来源:https://stackoverflow.com/questions/62566550/is-there-a-way-to-detect-users-select-all-content