dankito / RichTextEditor

Rich text WYSIWYG editor for Android and JavaFX
Apache License 2.0
92 stars 36 forks source link

Getting string from "getCachedHtml()" doesnt match with the same string #28

Closed muneebahmad0600 closed 4 years ago

muneebahmad0600 commented 4 years ago

Firstly the editor doesn't have a "OnTextChnangedListener" which makes it hard to change UI on the basis of text entered. I am trying to check if the editor is empty at a given time. When editor is empty "getChachedHtml()" returns starting and ending tag of "p" but if I compare that with the same string the result is false. I don't know what is wrong if u could help me out or suggest a different way to monitor text change that would be awesome.

knjl34 commented 4 years ago

getCurrentHtmlAsync also gives "" but if I compare that with the same string ""

SO

if (plain.trim().length() <= 1 ) { /// STRING IS EMPTY } else { //STRING HAS SOME VALUE }

:D :+1:

dankito commented 4 years ago

I'm not sure if I understand completely what you mean. Please correct me if I get you wrong.

Yes, you're right, there's so such thing as a "OnTextChangedListener". I once had it, but it was a too big performance issue (in my use case I handle real large HTML documents) so I removed it.

But what may helps in your case is the DidHtmlChangeListener. It's just a bit hidden:

        editor.getJavaScriptExecutor().addDidHtmlChangeListener(new DidHtmlChangeListener() {
            @Override
            public void didHtmlChange(boolean didHtmlChange) {
                if (didHtmlChange) {
                    // user entered some text
                }
                else {
                    // user deleted all text, html is now "<p>\u200B</p>" again
                }
            }
        });

If nothing is entered, getCurrentHtmlAsync() should return "\

\u200B\

". So check against this value to detect if user didn't enter anything into editor:

editor.getCurrentHtmlAsync(new GetCurrentHtmlCallback() {
                        @Override
                        public void htmlRetrieved(@NotNull String html) {
                            boolean isEmpty = "<p>\u200B</p>".equals(html);
                            if (isEmpty) {
                                // ...
                            }
                        }
                    });

(By the way, please do not use getCachedHtml(), it will be removed in the next major release.)

Does this answer your questions?

muneebahmad0600 commented 4 years ago

yes it does answer the question. Thanks a lot. One other thing i have a problem with is that i have set my own font for editor with "editor.setEditorFontFamily("sans-serif-light");" but when I start the activity which contains the editor and start typing, sometimes it uses the default font and sometimes it uses the one i have defined. Could you help me with this also ? PS i don't use "editor.focusEditorAndShowKeyboardDelayed();" so that editor is initialized properly but the issue still remains.

muneebahmad0600 commented 4 years ago

Update : I have currently fixed this issue by doing this : editor.setEditorFontFamily("sans-serif-light"); editor.setHtml(""); Although i have already set the font family when editor is initialized but writing above lines explicitly seem to do the trick.

dankito commented 4 years ago

To make life easier for you in version 2.0.12 I added

Will release this version soon.