2015-11-12 31 views
5

我正在做一個簡單的筆記應用程序並使用GhostDown Markdown編輯器。這很不錯,我喜歡,但我試圖以編程方式設置它的值。如何設置GhostDown Markdown編輯器的值

我可以很容易地得出數值。 $(「入門降價內容textarea的」)。VAL()

設置但它是另一回事... :(

什麼我工作的原型可以在http://potusnotes.com

可以看出
+0

你在此[幽靈降價編輯器(https://開頭github上。 com/timsayshey/Ghost-Markdown-Editor)版本? – Blag

+0

@Blag不確定,但如果你提供最新版本的答案,我會確保在必要時進行更新 – Serhiy

+0

它比版本更關注fork(因爲我沒有找到GhostDown Markdown編輯器的官方網站) ;我看了一下源代碼,但有點混亂<_>;它似乎有'getMarkdown'和'getHtml',但沒有setter ...;當他們使用覆蓋,如果你沒有找到正確的方法,這是毫無意義的 – Blag

回答

3

對於編輯部分,幽靈降價編輯器使用CodeMirror editor。 因此,設定值編程方式,我們稱之爲CodeMirror的情況下,做

editor.setValue(txt); 

但如何獲取該CM實例?它由創建Ghost-Markdown-Editor的小部件創建。見jquery.ghostdown.js文件:

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

} 

作爲窗口小部件是用jQuery的Widget工廠生產,一個小部件實例保持它被用來在對象的inside .data("plugin-name")元素。

因此,我們可以這樣訪問控件實例並設置編輯值:

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

或者乾脆

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

太好了,非常感謝你,這正是我所期待的。我認爲這會是這樣的事情,但無法自己搞清楚。 – Serhiy

+0

很高興能幫到你!幸運的是它是CodeMirror的底層。 – Vaviloff

相關問題