ClearHighLightCommand

Aug 30, 2010 at 8:43 AM
Edited Aug 30, 2010 at 8:46 AM

I need to allow users to clear only their own highlights(annotations), and not another users highlights.

In my implementation each annotation has only one Author.

As far as I know : it seems there is a logic which maps text selection to actual Highlight annotation object, and decides to remove or change StartTextIndex andEndtextIndex.

How can I implement this? Seems the code inside ClearHighlightCommand is the key!

 

Coordinator
Aug 30, 2010 at 9:29 AM
Edited Aug 31, 2010 at 9:29 AM

The below snippet removes all highlights currently selected. It should be easy to add your own logic

 

public void RemoveHighlightsForSelection(AnnotationService service)
{
  if (!service.TextContainer.Selection.IsEmpty) {
    // ensure AnnotationStore doesn't raise a zillion Changed events
    service.Store.BeginEdit();

    RemoveHighlights(service.Store, service.TextContainer.Selection);

    // raises the Changed event one time
    service.Store.EndEdit();
  }
}

private void RemoveHighlights(AnnotationStore store, TextRange range)
{
  // removes all highlights in the given range
  foreach (var highlight in store.OfType<Highlight>().ToArray()) {
    if (highlight.TextRanges.Clip(range)) {
      // remove entire highlight when its text range collection is empty.
      if (highlight.TextRanges.IsEmpty()) {
        store.Remove(highlight);
      }
    }
  }
}

 

 

Aug 31, 2010 at 2:51 AM

I implemented the OnExecute metohd of the derived  ClearHighlightcommand, and it worked fine.

Ive fidex this,Just FYI : foreach block and store.Remove lines throw an exception : "Collection was modified; enumeration operation may not execute."

Thanks for your help

Coordinator
Aug 31, 2010 at 9:29 AM

You are correct, I've modified the sample code so it no longer throws.