本文介绍了有没有一种方法可以检测用户选择了所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个插件,并且有一个用例需要检测用户何时选择所有内容.我试图创建一个全选的选择,例如 const selectAllSelection = editor.model.createSelection(editor.model.document.getRoot(),'in')并与当前的 model.document比较.选择,但它们不相同.

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.

推荐答案

这些选择有些不同,因为使用 createSelection()创建的选择将直接在< $ root>中开始..真正的文档选择将在第一个< paragraph> 内部开始.这使得比较有点棘手.

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.

但是,还不错:

  • 检查所选内容是否只有一个范围,
  • 获取文档选择的第一个范围( selection.getFirstRange())并使用 model.createRangeIn(editor.model.document.getRoot())创建一个在根部范围内
  • 由于我所描述的原因,范围会有所不同,因此我们无法执行 range.isEqual().我们需要使用 isTouching()检查开始和结束位置.
  • 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 );
}

这篇关于有没有一种方法可以检测用户选择了所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:19