Is there a way to detect users select all content?

无人久伴 提交于 2021-02-10 14:53:59

问题


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 use model.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 with isTouching().

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!