Capture TextEditor


I’ve created a new TextEditor view, I’d like to have set/get control over its contents.

@exampleEditor = new TextEditor
          tabLength: 2
          softTabs: true
          softWrapped: true
          placeholderText: 'Text above'
@subview 'exampleEditor', new TextEditorView(editor: @exampleEditor)

I can set the contents using @exampleEditor.setText('Hello World'), but these contents are not visible in the editor view. It remains empty, or can accept a user input.

I can get the contents using @exampleEditor.getText(), but this just grabs the ‘Hello World’ text, and not the user inputted text.

How can I set and get the actual Editor text as seen in the view?


To open a new editor, you should use the function. Most of the Atom API classes are designed to only be instantiated by the framework, not client code.

Additionally, as a design note, the TextEditor class (and the other classes publicly exposed by the Atom API) represents the model in the standard MV* architecture. A while back, the Atom team chose to remove the view classes from public accessibility (as much as anything can be private in JavaScript) to simplify the API.


I should mention, what I’m trying to do is use a multi-line mini editor as an input. For example, as a small markdown editor comment box. I’m using the TextEditorView from atom-space-pen-views.


I believe in that case you’re supposed to set the text via the TextEditorView class:


Thanks for the help, I appreciate it. Unfortunately, I still have the same issue

If I type the word “test” into the text field, the following result happens:
getText() returns nothing.
setText('hello') doesn’t change the input field
getText() after setText('hello') returns the value ‘hello’.

There is no way to get the intended result, "“test”.

Also, there is no “getModel” method on the TextEditorView, which I expected.


Sorry, I never used the space-pen stuff that extensively. You can always use jQuery, space-pen is built from it.


I tried a second method. Adding a <atom-text-editor> tag with a class attached to it, then console.log’ing the element. But still no way to grab the text. Any ideas?


As I said, you can always use jQuery:


Actually, looking into the Settings View which uses a ton of mini editors … I realized that mini editors have gotten really complicated since I last looked :laughing: The above probably won’t work either.

I think I’ll leave this to someone who has worked with space-pen more recently.


I assumed that would work too, but it returns an empty string and ignores the input.


When I needed a big edit box for my bug-report package, I just opened a new regular editor and used that.


Great package! I’ve found it very useful.

In this case, the input needs to be smaller, but still multi-line. I have two of them within the same panel.

Another possible hint: Running the following:

atom.workspace.observeTextEditors (editor) ->
  editor.insertText('Hello World')

Does not add to my new text editor fields, but adds “Hello World” to all open text editor windows.


Issue Resolved

I was loading the Editor in the Initialize phase. Moving it to @content at the top causes it to work.


The way I’ve always created a text editor is

textEditorView = document.createElement 'atom-text-editor'
@textEditor = textEditorView.getModel()

And I’ve never had a problem with it. And no need to require the space pen views :smile: