dankito / RichTextEditor

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

setAccessibilityDelegate and getSelectionStart() like Edittext #34

Closed seregin-pro closed 4 years ago

seregin-pro commented 4 years ago

Hello.

  1. I need to get cursor position when I touched on text like EditText but a didn't find it.

Example for Edittext

EditText edittext = findViewById(R.id.title); int currentCursor = 0;

editText.setAccessibilityDelegate(new View.AccessibilityDelegate() { public void sendAccessibilityEvent(View view, int eventType) { super.sendAccessibilityEvent(view, eventType); if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED) { currentCursor = editText.getSelectionStart(); } } });

  1. How can I do like this editText.getSelectionStart() and getSelectionEnd() for RichTextEditor
  2. I have dublicates font family, Android 5.1
seregin-pro commented 4 years ago

screem

dankito commented 4 years ago

Hi,

i don't know if i can help you with all your problems.

1) Actually i don't even know what setAccessibilityDelegate() does :). I can only provide the methods of WebView. I think your request (e.g. implementing this manually) is out of scope.

  1. Also here i don't think i can help you as the underlying implementation is done with HTML contenteditable and JavaScript. JavaScript's window.getSelection().getRangeAt(0) would provide a start- and end index, but only relative to the element that contains to selected text. I don't think there's a function in JavaScript to get the selection start- and end index in absolute position (do you know any?).

But depending on your use case i may can help you otherwise. What are trying to archive? E. g. getting selected text would be no problem (at least not as plain text).

  1. Here i think i found the culprit. As newer Android versions use a different system to store font information, there i take care that each font name only gets added once. But as you said you're using an older Android version, there i use a simple ArrayList to collect all installed fonts. Trying to find an Android 5 phone and fix this at the weekend. Let you know if this solves the issue.
seregin-pro commented 4 years ago

I need to get selection text for rotation screen to save it.

For Edittext I did it like this:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); etContentView(R.layout.activity_main);

// get saved data and restore selected text before rotation screen
if (savedInstanceState != null) {
    selectionTextStart = savedInstanceState.getInt("selectionTextStart");
    selectionTextEnd = savedInstanceState.getInt("selectionTextEnd");
}

final EditText editText = findViewById(R.id.editText);

editText.setAccessibilityDelegate(new View.AccessibilityDelegate() {

    // When cursor position changed sendAccessibilityEvent will add new position selected text
    // touch anywhre on text or change position orange text slider
    public void sendAccessibilityEvent(View view, int eventType) {
        super.sendAccessibilityEvent(view, eventType);
        selectionTextStart = editText.getSelectionStart();
        selectionTextStart = editText.getSelectionStart();
    }
});

}

protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState);

outState.putString(Constant.TEXT, text);

outState.putInt("selectionTextStart", selectionTextStart); // put before rotation a screen 
outState.putInt("selectionTextEnd", selectionTextEnd); // put before rotation a screen 

}

I will try to do somethig with WebView

Image 1. I selected text. Image 2. I have this result after rotation screen. I need selected text like Image 1

I hope you understand my English.

seregin-pro commented 4 years ago

Screenshot_2020-02-07-21-47-26 Screenshot_2020-02-07-21-47-53

dankito commented 4 years ago

Just released version 2.0.15 (should be visible at maximum in two hours).

The font bug should be fixed now. Thanks for reporting it to me!

To restoring the selection:

That's not that easy.

I tried saving selected range with var rangeToRestore = window.getSelection().getRangeAt(0). (See https://gist.github.com/dantaex/543e721be845c18d2f92652c0ebe06aa .)

But restoring with window.getSelection().addRange(rangeToRestore) does not work as after setting html it's a new document and therefore backed up anchor element is not valid anymore.

What could work is using the JavaScript library rangy as suggested here: https://stackoverflow.com/questions/39566611/how-can-i-save-and-restore-selection-range-in-javascript

But as i already spent enough time on this, this is something what you would have to do. If you implement it and give me a pull request, i'd be glad to integrate it.

Cheers.

seregin-pro commented 4 years ago

I just added it in AndroidManifest.xml in my It is work but I go on test it yet. <activity android:name=".EditorActivity" android:configChanges="keyboardHidden|orientation|screenSize"> </activity>