Refactor annotation code and support text form fields

This depends on #2945