Hi all,
I 've ported this @stevel05 B4J CodeMirror wrapper to B4A
https://www.b4x.com/android/forum/threads/codemirror-wrapper-and-example.125775/#post-785682
Now I need to get values back from Javascript but I've some problems.
Steve in his B4J code implemented itself a Javascript interface on a JavaFX WebView, ExecuteJavaScript command can execute JS and return back an object so it can back with just one call.
B4J CodeMirrorWrapper to get editor code do this:
In my code I use WebViewExtras and ExecuteJavascript method do not return values, so I just created a JS function to get a value and then JS call a B4A sub to pass back it.
The problem here is that this require some time and I do not know how to wait a value.
It always return non last value assigned to a global variable (from previous call).
I've even tried to reset it and wait with while loop (not good option, just for test) while the value change, but as expected it enter in an infinite loop.
Even I do not like that require a global variable.
Here my attemp:
I need to do it on a lots of methods (in the wrapper class).
Wait For I think cannot help here because the sub is called by JS and not B4A, right ?
Please can someone help me to know how to do it in an elegant way ?
Thanks
I 've ported this @stevel05 B4J CodeMirror wrapper to B4A
https://www.b4x.com/android/forum/threads/codemirror-wrapper-and-example.125775/#post-785682
Now I need to get values back from Javascript but I've some problems.
Steve in his B4J code implemented itself a Javascript interface on a JavaFX WebView, ExecuteJavaScript command can execute JS and return back an object so it can back with just one call.
B4X:
'Executes a script in the context of the current page.
Public Sub ExecuteScript(Script As String) As Object
Return TJO.RunMethod("executeScript",Array As Object(Script))
End Sub
B4J CodeMirrorWrapper to get editor code do this:
B4X:
'Get the current code editor text code
Public Sub GetCode() As String
Return ExecuteScript("editor.getValue();") ' A single call to inject JS and get back return value to B4J calling module that request it (Main or class module)
End Sub
In my code I use WebViewExtras and ExecuteJavascript method do not return values, so I just created a JS function to get a value and then JS call a B4A sub to pass back it.
The problem here is that this require some time and I do not know how to wait a value.
It always return non last value assigned to a global variable (from previous call).
I've even tried to reset it and wait with while loop (not good option, just for test) while the value change, but as expected it enter in an infinite loop.
Even I do not like that require a global variable.
Here my attemp:
B4X:
'Get the current text on this CodeEditor.
Public Sub GetCode As String
' Code = "" ' Code is a String global variable
wve.executeJavascript(mWebView, "getValue();")
' Return wve.executeJavascript(mWebView, "editor.getValue();" ' This cannot be used because ExecuteJavascript
' method return nothing
Return Code
End Sub
Public Sub codemirrortext(Value As String) ' Called by JS to return back the value
Code = Value
Log(">>> CodeMirror text from JS: " & CRLF & Code)
End Sub
JS Code:
function GetValue() {
var text = editor.getValue();
B4A.CallSub('codemirrortext', true, text);
}
I need to do it on a lots of methods (in the wrapper class).
Wait For I think cannot help here because the sub is called by JS and not B4A, right ?
Please can someone help me to know how to do it in an elegant way ?
Thanks
Last edited: