2016-12-05 30 views
3

我正在嘗試使用purescript-puxCodeMirror這個靜態代碼編輯器編寫應用程序。如何從外部模塊生成Pux操作?

CodeMirror從text-area生成編輯器,但事件處理方式不同,因此我無法使用Pux.Html.Events中的onChange函數。

要處理的CodeMirror編輯器輸入的變化,應該做這種方式:

CodemirrorInstance.on('change',function(cMirror){ 
    // get value right from instance 
    var newValue = cMirror.getValue(); 
}); 

我能夠做出Codemirror實例,它是一些瑣碎的工作。我遇到的問題是如何讓pux意識到事件。

我有我訂閱了Channel我用處理WebSocket的消息,但我真的不知道我怎麼能送的東西在那邊國外JavaScript文件。

回答

0

你可能只需要在你的外部模塊上設置一個信號(purescript-signal)。

也許是這樣的:

function CMChangeSignal(constant) { 
    /** take initial value here if it possible and pass it to constan fnt */ 
    var out = constant(); 
    // listen to it 
    CodemirrorInstance.on('change',function(cMirror){ 
     // get value right from instance 
     var newValue = cMirror.getValue(); 
     out.set(newValue); 
    }); 
    return function() { 
     return out; 
    } 
} 

然後在你的purescript文件:

// type it, read the CodeMirror Docs. 
type CMChange = 
    { from :: CMCoordinate 
     to :: CMCoordinate 
     text :: Array String 
     removed :: String 
     origin :: ?? 
    } 

foreign import _CMChangeSignal :: forall eff change. (change -> Signal change) -> Eff (dom :: DOM | eff) (Signal CMChange) 

cmChanged :: forall eff. Eff (dom :: DOM | eff) (Signal CMChange) 
cmChanged = _CMChangeSignal constant 

main :: Eff (CoreEffects (dom :: DOM)) Unit 
main = do 
    urlSignal <- sampleUrl 
    let routeSignal = urlSignal ~> (PageView <<< match) 
    -- create signal then map it to your Action 
    cmSignal <- cmChanged 
    let cmChangeSignal = cmSignal ~> CodeMirrorChangedAction 

    app <- start 
    { initialState: init 
    , update: update 
    , view: view 
    , inputs: [routeSignal, cmChangeSignal] 
    } 

    renderToDOM "#app" app.html 

constant處於 「purescript信號」 是PUX取決於定義的函數。