B4J Question HTMLeditor - save text without HTML

ThRuST

Well-Known Member
Licensed User
Longtime User
Is it possible to save text in the HTMLeditor control as plain text without all HTML tags, how to do that?
I want to copy only the text to the clipboard in this case. I've tried jsoup v0.15 library to parse the html string with the 'parse_HTML' command but still there's HTML surrounding the text which is not what I want.
Thanks
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Thanks Steve, that worked. However it is not totally reliable so I would rather want the text to be selected automatically and copied to the clipboard from the HTMLeditor control. As a workaround I can just let the user copy text manually with CTRL+C and then store whatever string that is currently in the clipboard to the database.
Perhaps AWTRobot could be useful or to access Javaobject and handle HTMLeditors own functions to automatically set focus, select all text and copy it to the clipboard to get the text from the HTMLeditor control. This way there's no need to depend on parsing the HTML string. However I have not yet managed to get AWTrobot to perform these tasks in the HTMLeditor control so I'd appreciate a solution with a JavaObject, or if there's a better way.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I found something in nativa JavaFX that could work to select all text in the HTMLEditor control. You can find it Here
I need help to be able to use it in B4J. Thanks
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
That looks interesting, but it will probably take alot of time to implement for such a simple thing so I'll just go with the option to require users to copy text manually into the clipboard and then be able to save that into the database. I am working on giving your floatingmenus programmable slots so 30x4 slots will be programmable for quick access of documentation or code. These floatingmenus use a separate database (SQLite) so they can be customized and exhanged for a streamlined workflow. The code from CodeArea is copied automatically so that's allready solved. It will also be possible to insert an empty line into the floatingmenus so they will be easy to work with.
Work remains to bind this all together but in about two, three month time I think I will have a beta version of Mindcraft ready. Working slowly because I suffer from a burnout because of years and latest 6 month of stress/pressure. So take care and make sure you rest between extreme sessions. Burnout gives scary side effects so I have to take it slow. I will be able to complete Mindcraft because it will be a great tool for everyone. For everyone who does not know what it's all about I am working on a knowledge management software were experts like Steve05, Roycefer, Cableguy among others have contributed to move the project forward. It sums up years of programming experience and is developed in B4J. It will put Erel on the map and all of you who contributed. Everybody will know about Anywhere Software and B4X once it's done.
With a knowledge management software running on every platform, you just can't go wrong.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Here is the converted function, I don't know how to select all text programatically, perhaps you can do that with awtrobot, or look for another solution.
 

Attachments

  • WVTest.zip
    2.4 KB · Views: 251
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Nice. maybe it can be combined with this example provided by Erel. It sets focus to the HTMLeditor control and puts the cursor at the end of the text.
It can be a part of the solution to select all text in the HTMLeditor control and copy the text, probably with AWTRobot. Autoselection is better than to do this manually because it might get bothersome in the long run. Luckily this works well with CodeArea but it's the HTMLeditor that needs to be tamed. We'll solve this :)

B4X:
Sub SetHTMLFocus
   
   Dim jo2 As JavaObject = HTMLEditor
   Dim r2 As Reflector
   r2.Target = jo2.RunMethodJO("getSkin", Null)
   jo2 = r2.GetField("webView")
   jo2.RunMethod("requestFocus", Null)
   jo2 = jo2.RunMethod("getEngine", Null)
   jo2.RunMethod("executeScript", Array("document.body.focus()"))
   jo2.RunMethod("executeScript", Array($"
   var el = document.body;
if (typeof window.getSelection != "undefined"
  && typeof document.createRange != "undefined") {
   var range = document.createRange();
   range.selectNodeContents(el);
   range.collapse(false);
   var sel = window.getSelection();
   sel.removeAllRanges();
   sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
   var textRange = document.body.createTextRange();
  textRange.moveToElementText(el);
   textRange.collapse(false);
   textRange.select();
}
   "$))
  
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Here's Steve's code example WVTest.zip in code for easy overview. "Do Not judge Me by my successes, judge Me by how many times I fell down And got back up again." - Nelson Rolihlahla Mandela :) I can agree to that.

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim HTMLEditor1 As HTMLEditor
    Private Button1 As Button
    Private SELECT_TEXTFunc As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
   
    SELECT_TEXTFunc = $"(function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
         text = document.selection.createRange().text;
    }
    if (window.getSelection) {
        if (window.getSelection().empty) {  // Chrome
            window.getSelection().empty();
        } else if (window.getSelection().removeAllRanges) {  // Firefox
            window.getSelection().removeAllRanges();
        }
    } else if (document.selection) {  //IE?
        document.selection.empty();
     }
         return text;
    })();"$
   
    Dim HTML As String =  $"<p><em>"Do Not judge Me by my successes, judge Me by how many times I fell down And got back up again."</em></p>&nbsp;&nbsp&nbsp&nbsp;-&nbspNelson Rolihlahla Mandela"$
   
    HTMLEditor1.HtmlText = HTML
   
   
   
   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub



Sub Button1_Action
    Dim HTMLWVJO As JavaObject = HTMLEditor1
    Dim WV As JavaObject = HTMLWVJO.RunMethod("lookup",Array("WebView"))
    If WV.IsInitialized Then
        Dim WE As JavaObject = WV.RunMethod("getEngine",Null)
        Dim Selection As Object = WE.RunMethod("executeScript",Array(SELECT_TEXTFunc))
        If Selection Is String Then
            Log(Selection)
        End If
    End If
End Sub
 
Upvote 0
Top