Intercept Undo Redo in MobileTextArea

Any ideas on how to intercept undo / redo in a mobile text area so I can implement a custom undo / redo?

There are commands in my app that replaces text (mobiletextarea.text = some string) and this nullifies the built-in undo / redo, so I have to roll my own, and prevent the built-in undo/redo from activating.

You could use declares to UndoManager which should let you manage the undo/redo without destroying state. Every UIResponder has an undoManager property to make this somewhat easy: Apple Developer Documentation

I’m not sure anyone has implemented the declares yet but it should be fairly easy I think.

I haven’t worked with declares at all, so that might be a little over my head.

OK, I found this from jim_mckay, but I have no idea how to update it for iOS in 2022. It kills all undos, so I can roll my own. Does anyone know how to update this Declare?

jim_mckay

Declare Function undoManager lib “Cocoa” selector “undoManager” (obj_ref as ptr) as ptr
Declare Sub removeAllActionsWithTarget lib “Cocoa” selector “removeAllActionsWithTarget:” (obj_ref as ptr,target as Ptr)
Declare Function documentView lib “Cocoa” selector “documentView” (obj_ref as ptr) as Ptr
Declare Function textStorage lib “Cocoa” selector “textStorage” (obj_ref as ptr) as Ptr

dim docView As ptr=documentView(ptr(TextArea1.Handle))
dim textStor As ptr=textStorage(docView)
dim undoMgr as ptr=undoManager(docview)
removeAllActionsWithTarget(undoMgr,textStor)

From this old thread here:

Untested

Declare Function undoManager lib “UIKit” selector “undoManager” (obj_ref as ptr) as ptr
Declare Sub removeAllActionsWithTarget lib “UIKit” selector “removeAllActionsWithTarget:” (obj_ref as ptr,target as Ptr)
Declare Function documentView lib “UIKit” selector “documentView” (obj_ref as ptr) as Ptr
Declare Function textStorage lib “UIKit” selector “textStorage” (obj_ref as ptr) as Ptr

dim docView As ptr=(TextArea1.Handle)
dim textStor As ptr=textStorage(docView)
dim undoMgr as ptr=undoManager(docview)
removeAllActionsWithTarget(undoMgr,textStor)
1 Like

Thanks you Jeremie!