Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for compatability and for accessing Codemirror function from other Mint programs. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions source/CodeMirror.mint
Original file line number Diff line number Diff line change
@@ -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() }})
raguay marked this conversation as resolved.
Show resolved Hide resolved

/* The content to display until the editor is loaded. */
property loadingContent : Html = <></>
property loadingContent : Html = Html.empty()
raguay marked this conversation as resolved.
Show resolved Hide resolved

/* Whether or not show line numbers. */
property lineNumbers : Bool = true
Expand All @@ -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())
}
}

Expand All @@ -50,7 +56,7 @@ component CodeMirror {
}

/* Initializes the editor for the given dom element. */
fun initializeEditor : Void {
fun initializeEditor () : Void {
`
(() => {
if (!this.element) { return }
Expand All @@ -60,6 +66,7 @@ component CodeMirror {
lineNumbers: this.lineNumbers,
theme: this.theme,
mode: this.mode,
lineWrapping: this.wordwrap
})

this.editor.on('change', (value) => {
Expand All @@ -71,6 +78,10 @@ component CodeMirror {
`
}

fun getEditor() : Unit {
`this.editor`
}

/* After an update, update the underlying editor instance. */
fun componentDidUpdate : Void {
`
Expand Down Expand Up @@ -107,7 +118,7 @@ component CodeMirror {
[
<textarea::editor ref={saveReference}/>,
if (`this.editor`) {
<></>
Html.empty()
} else {
loadingContent
}
Expand Down
73 changes: 73 additions & 0 deletions source/CodeMirrorStore.mint
Original file line number Diff line number Diff line change
@@ -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) {
raguay marked this conversation as resolved.
Show resolved Hide resolved
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
}