mizzao / meteor-sharejs

Meteor smart package for transparently adding ShareJS editors to an app
MIT License
225 stars 53 forks source link

How can I get the value of the text area (sounds basic, but i've tried everything)? #43

Closed charford closed 8 years ago

charford commented 9 years ago

Seems like a really basic question, but I can't figure out how to programmatically get the value of the code mirror editor. Is this possible?

I'm also looking to attach a listening event to the CodeMirror editor, so whenever changes are made, I would get the content of the editor, and render it in a separate div. But to do that, I need to some how have access to the CodeMirror object I think.

cllunsford commented 9 years ago

Regarding your second question: I'd love to know if there is a better way, but I'm able to grab the codemirror object by creating a file-scoped variable in my client-side js and assigning the codemirror object to it with the onRender hook.

client.html

{{> sharejsCM docid=_id id="editor" onRender=renderEditor }}

client.js

var myEditor;

Template.body.helpers({
  renderEditor: function () {
    return function(editor) {
      myEditor = editor;
    }
  }
});

I'm able to use myEditor in my event functions after this.

philipbeadle commented 8 years ago

I'm in the same boat, I want to render the contents in a {{markdown}} template but cant figure oit how to get the current content.

mizzao commented 8 years ago

I'm pretty sure you guys should be able to get the contents from the Ace API or the CodeMirror API depending on what you are using.

johannbotha commented 8 years ago

For Ace

myEditor = null

Template.myTemplate.events
  'click .do-something': () ->
    console.log(myEditor.getValue())    

Template.myTemplate.config = () ->
  (editor) ->
    editor.setTheme('ace/theme/monokai')
    editor.getSession().setMode('ace/mode/javascript')
    myEditor = editor