From a75529627338eb3207ed0cf3df27fed049df1321 Mon Sep 17 00:00:00 2001 From: Richard Guay Date: Wed, 19 Sep 2018 12:09:23 +0700 Subject: [PATCH] Changes for compatability and for accessing Codemirror function from any Mint program. --- source/CodeMirror.mint | 27 ++++++++++---- source/CodeMirrorStore.mint | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 source/CodeMirrorStore.mint diff --git a/source/CodeMirror.mint b/source/CodeMirror.mint index d1fa29c..2d03ef3 100644 --- a/source/CodeMirror.mint +++ b/source/CodeMirror.mint @@ -1,22 +1,22 @@ /* A component that integrates the CodeMirror editor. */ component CodeMirror { + connect CodeMirror.Store exposing { addEditor } + /* The JavaScript files of Codemirror to load, either locally or from a CDN. */ property javascripts : Array(String) = [ - "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.0" \ - "/codemirror.min.js" + "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.0/codemirror.min.js" ] /* The CSS files of Codemirror to load, either locally or from a CDN. */ property styles : Array(String) = [ - "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.0" \ - "/codemirror.min.css" + "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.39.0/codemirror.min.css" ] /* Handler for the change event. */ - property onChange : Function(String, a) = ((value : String) : Void => { void }) + property onChange : Function(String, Promise(Never, Void)) = ((value : String) : Promise(Never, Void) => { sequence { Promise.never() }}) /* The content to display until the editor is loaded. */ - property loadingContent : Html = <> + property loadingContent : Html = Html.empty() /* Whether or not show line numbers. */ property lineNumbers : Bool = true @@ -30,12 +30,18 @@ component CodeMirror { /* The mode of the editor. */ property mode : String = "" + /* Set wordwrap or not. */ + property wordwrap : Bool = true + + property name : String = "" + /* Loads all assets when the components mounts. */ fun componentDidMount : Promise(Never, Void) { sequence { AssetLoader.loadAll(AssetLoader.loadScript, javascripts) AssetLoader.loadAll(AssetLoader.loadStyle, styles) initializeEditor() + addEditor(name,getEditor()) } } @@ -50,7 +56,7 @@ component CodeMirror { } /* Initializes the editor for the given dom element. */ - fun initializeEditor : Void { + fun initializeEditor () : Void { ` (() => { if (!this.element) { return } @@ -60,6 +66,7 @@ component CodeMirror { lineNumbers: this.lineNumbers, theme: this.theme, mode: this.mode, + lineWrapping: this.wordwrap }) this.editor.on('change', (value) => { @@ -71,6 +78,10 @@ component CodeMirror { ` } + fun getEditor() : Unit { + `this.editor` + } + /* After an update, update the underlying editor instance. */ fun componentDidUpdate : Void { ` @@ -107,7 +118,7 @@ component CodeMirror { [ , if (`this.editor`) { - <> + Html.empty() } else { loadingContent } diff --git a/source/CodeMirrorStore.mint b/source/CodeMirrorStore.mint new file mode 100644 index 0000000..ae6729c --- /dev/null +++ b/source/CodeMirrorStore.mint @@ -0,0 +1,73 @@ +store CodeMirror.Store { + state editors : Array(EditorRecord) = [] + + fun addEditor(nameNew : String, editorNew : Unit) : Promise(Never, Void) { + next { + editors = Array.push({ name = nameNew, editor = editorNew }, editors) + } + } + + fun getEditor(name : String) : EditorRecord { + Array.firstWithDefault({name = "", editor=`{}`},Array.select((thing : EditorRecord) : Bool => { + if(thing.name == name) { + true + } else { + false + } + }, editors)) + } + + fun listSelections(editor : EditorRecord) : Array(EditorSelections) { + `editor.editor.listSelections()` + } + + fun setSelections(editor : EditorRecord, selections : Array(EditorSelections)) : Void { + `editor.editor.setSelections(selections)` + } + + fun setValue(editor : EditorRecord, text : String) : Void { + `editor.editor.setValue(text)` + } + + fun getSelection(editor : EditorRecord) : String { + `editor.editor.getDoc().getSelection()` + } + + fun saveSelection(editor: EditorRecord, text : String) : Void { + `editor.editor.getDoc().replaceSelection(text)` + } + + fun insertAtCursor(editor : EditorRecord, text : String) : Void { + `(()=>{ + var cursor = editor.editor.getDoc().getCursor(); + editor.editor.getDoc().replaceRange(text,cursor); + })()` + } + + fun getCursor(editor : EditorRecord) : Cursor { + `editor.editor.getDoc().getCursor()` + } + + fun setCursor(editor : EditorRecord, cursor : Cursor) : Void { + `editor.editor.getDoc().setCursor(cursor)` + } + + fun isSelection(editor : EditorRecord) : Bool { + `editor.editor.getDoc().somethingSelected()` + } +} + +record EditorRecord { + name : String, + editor : Unit +} + +record Cursor { + line : Number, + ch : Number +} + +record EditorSelections { + anchor : Cursor, + head : Cursor +} \ No newline at end of file