How to set the value of the GhostDown Markdown editor

I am working on a simple note taking application and using the GhostDown Markdown editor. This is very nice, I like it, but I'm stuck trying to set its value programmatically.

I can easily get the values. $ ('. entry-markdown-content textarea'). val ()

Customization, however, is another story ... :(

A prototype of what I'm working on can be seen at http://potusnotes.com

+5
source share
1 answer

For the editor part, the Ghost-Markdown-Editor uses the CodeMirror editor . Thus, to set the value programmatically, we call an instance of CodeMirror and do

editor.setValue(txt); 

But how to get this CM instance? It was created by widgets with which the Ghost-Markdown-Editor was created. See the jquery.ghostdown.js file:

 $.widget( "b4m.ghostDown", { editor: null, // ... _create: function() { // ... this.editor = CodeMirror.fromTextArea(this.element.find('textarea')[0], { mode: 'markdown', tabMode: 'indent', lineWrapping: true }); } } 

Since the widget was created using the jQuery Widget factory, the widget instance is stored inside the .data ("plugin-name") element of the object on which it was used.

Thus, we can access the widget instance and set the editor value as follows:

 var ghostdown = $(".editor").data("b4m-ghostDown"); ghostdown.editor.setValue("# Hello, SO"); 

Or simply

 $(".editor").data("b4m-ghostDown").editor.setValue("# Hello, SO"); 
+3
source

Source: https://habr.com/ru/post/1235802/


All Articles