proton icon indicating copy to clipboard operation
proton copied to clipboard

Ask framework if selected range is a list

Open YoX89 opened this issue 3 years ago • 2 comments

Hi,

Is there a way to ask the framework, for example the ListTextProcessor if the selected range is a list?

This would be good if you have a control to toggle lists on/off.

Thanks for a great framework! Jonathan

YoX89 avatar Oct 22 '20 09:10 YoX89

thanks, Jonathan.

ListTextProcessor does not provide a mechanism to see if the selected range is list. However, you can check for that via attributedString using either of the following approaches:

editor.attributedText.attribute(.listItem, at: location, effectiveRange: nil) != nil

The location is the index of character in editor that you wish to test - or in your case, range.location.

If you have non-continuous range, you may also use:

 editor.attributedText.enumerateAttribute(.listItem, in: range, options: .longestEffectiveRangeNotRequired) { (_, listRange, _) in
            // your code here
      }

longestEffectiveRangeNotRequired will give you ranges when any of the attributes change i..e font, color etc. If you omit that, you will get longest continuous range that has .listItem attribute with the same value. In other words, if this range is equal to your original range, it tells that the entire range is a list range.

rajdeep avatar Oct 22 '20 10:10 rajdeep

Thanks for the quick answer. The problem I'm having with that approach is when I do the following.

  1. Start with an empty EditorView.
  2. Insert a list.
  3. Tap return/enter to get a second list line.

When doing this the didChangeSelectionAt delegate call returns a range that is (2, 0).

At the same time, the editor.attributedText.string.count returns 2. So the range is out of bounds of the text. I think this might have to do with blankLineFiller.

I'm using 0.5 Semi release btw.

Edit: This can be reproduced with CommandsExampleViewController

YoX89 avatar Oct 22 '20 11:10 YoX89

@YoX89, please let me know if this issue is still relevant. If not, I'd go ahead and close this.

rajdeep avatar Apr 11 '23 04:04 rajdeep