You should look at what @cpennington did with the editor in https://github.com/edx/edx-platform/pull/945/files (legacy.py) and copy that logic instead.