Вопрос

I'm writing a plugin for an existing app and I need to capture the modified text and range affected by the undo and redo action. I am able to access the NSUndoManager and NSTextView the app has created and I am able to register for notifications. Is there a way to leverage these elements to grab the group of text that is undone/redone?

Это было полезно?

Решение

I haven't done this, so I'm only going by the docs/knowledge.

Because you have access to the textview you can become a textview delegate. You'll then receive useful messages...

Before the text changes:

  • textView:shouldChangeTextInRange:replacementString:
  • textView:shouldChangeTextInRanges:replacementStrings:
  • textView:shouldChangeTypingAttributes:toAttributes:

After the text changes:

  • textViewDidChangeTypingAttributes:

I don't know if you'll receive these changes (does UndoManager bypass this stuff?), but you may. In any event you can query selection settings while handling the previous messages.

Before the selection changes:

  • textView:willChangeSelectionFromCharacterRange:toCharacterRange:
  • textView:willChangeSelectionFromCharacterRanges:toCharacterRanges:

After:

  • textViewDidChangeSelection:

UndoManager should tell you that it is in the process of performing an Undo, which mean that you can differentiate ordinary changes from undo-based changes.

This seems like enough to go on, I hope it helps.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top