chinalwb / Android-Rich-text-Editor

Android Rich Text Editor With customized spans - 富文本编辑器 - Don't miss this one :)
Apache License 2.0
844 stars 166 forks source link

Few Fixes #75

Open rishabhk669 opened 5 years ago

rishabhk669 commented 5 years ago

Hi, At very outset, thanks a lot for this awesome work. It helped me a lot. I found many other projects but they are using WebView instead of EditText which creates a lot of stuff block. Your Span idea is great. While I was working with code I faced few problems and I fixed them. If I took your updated code then I think below fixes may improve it little bit. Please consider these fixes and commit to project if you think it is useful.

Please mark it resolve and accept my apologize if you found it not useful. And if you found it useful then please implement it. And a upgradation to your project with undo-redo functionality will be appreciable.

1) Font Size is not updating for selected text. It just update font for text which is going to type. So I made changes and now it is updating font for selected text.

//File: ARE_FontSize.java //declare global variable: private int selStart=0, selEnd=0; //onClick before showFontsizePickerWindow() if (null != mEditText) { selStart=mEditText.getSelectionStart(); selEnd=mEditText.getSelectionEnd(); } //in method onFontSizeChange() //replace assignment of start, end variable as: int start = selStart; int end = selEnd;

//Another File: ARE_Style_FontSize //declare global variable: private int selStart=0, selEnd=0;

//onClick before showFontsizePickerWindow() if (null != mEditText) { selStart=mEditText.getSelectionStart(); selEnd=mEditText.getSelectionEnd(); } //in method onFontSizeChange() //replace assignment of start, end variable as: int start = selStart; int end = selEnd-1;

2) If we change font size of a text( for Ex: "Chinalwb is a wonderful project.") now if we try to change the font size of partial text( for Ex: "Chinalwb is a wonderful") then remaining text( i.e. " project.") will reset to default size.

//File: ARE_ABS_Dynamic_Style.java // Changes in method applyNewStyle(Editable editable, int ..............) // I am coping whole changed method.

protected void applyNewStyle(Editable editable, int start, int end, int currentStyle) { E startSpan = null; int startSpanStart = Integer.MAX_VALUE; E endSpan = null; int endSpanStart = -1; int endSpanEnd = -1;

    int detectStart = start;
    if (start > 0) {
        detectStart = start - 1;
    }
    int detectEnd = end;
    if (end < editable.length()) {
        detectEnd = end + 1;
    }
    E[] existingSpans = editable.getSpans(detectStart, detectEnd, clazzE);
    if (existingSpans != null && existingSpans.length > 0) {
        for (E span : existingSpans) {
            int spanStart = editable.getSpanStart(span);

            if (spanStart < startSpanStart) {
                startSpanStart = spanStart;
                startSpan = span;
            }

            if (spanStart >= endSpanStart) {
                endSpanStart = spanStart;
                endSpan = span;
                int thisSpanEnd = editable.getSpanEnd(span);
                if (thisSpanEnd > endSpanEnd) {
                    endSpanEnd = thisSpanEnd;
                }
            }
        } // End for

        if (startSpan == null || endSpan == null) {
            Util.log("[ARE_ABS_Dynamic_Style#applyNewStyle] >>>>>>>>>>>>>>> ERROR!! startSpan or endSpan is null");
            return;
        }

        if (end > endSpanEnd) {
            Util.log("This should never happen! TAKE CARE!");
            endSpanEnd = end;
        }
        if (startSpanStart > start) {
            Util.log("This should never happen! TAKE CARE!");
            startSpanStart = start;
        }

        for (E span : existingSpans) {
            editable.removeSpan(span);
        }

        int startSpanFeature = startSpan.getDynamicFeature();
        int endSpanFeature = endSpan.getDynamicFeature();

        if (startSpanFeature == currentStyle && endSpanFeature == currentStyle) {
            editable.setSpan(newSpan(), startSpanStart, endSpanEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        } else if (startSpanFeature == currentStyle) {
           editable.setSpan(newSpan(startSpanFeature), startSpanStart, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            editable.setSpan(newSpan(endSpanFeature), end, endSpanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        } else if (endSpanFeature == currentStyle) {
            editable.setSpan(newSpan(startSpanFeature), startSpanStart, start, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            editable.setSpan(newSpan(endSpanFeature), start, endSpanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        } else {
            if(startSpanStart<start) {
                editable.setSpan(newSpan(startSpanFeature), startSpanStart, start, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            if (endSpanEnd > end) {
                editable.setSpan(newSpan(endSpanFeature), end, endSpanEnd, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            }
            editable.setSpan(newSpan(), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
    } else {
        editable.setSpan(newSpan(), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }
}

////////////////////////////End for Font size////////////////////////

3) It is important one. As per functionality, for new text, previous character style should applied. But here it is not capturing previous character style instead last applied style is taken.

//File: AREditText // in afterTextChanged comment below lines: /for (IARE_Style style : sStylesList) { style.applyStyle(s, startPos, endPos); }/

// in method setToolbar() add below lines in method end this.sToolbar= (ARE_Toolbar) toolbar; this.sToolbar.setEditText(this);

// onSelectionChanged() comment below lines: /if (mToolbar == null) { return; } List toolItems = mToolbar.getToolItems(); for (IARE_ToolItem toolItem : toolItems) { toolItem.onSelectionChanged(selStart, selEnd); }/ // and add below lines in condition( if (sToolbar == null) ) in onSelectionChanged() sToolbar=ARE_Toolbar.getInstance(); if(sToolbar==null)

4) multi-line indent, multi-line bullet and numbering and quote is not happening. Even after below change some fix is require, but will do it later.

//Files: wherever getCurrentCursorLine(EditText) is getting use // replace Util.getCurrentCursorLine(EditText) with Util.getCurrentSelectionLines(EditText) // get start, end as follows: int start = Util.getThisLineStart(editText, currentLine[0]); int end = Util.getThisLineEnd(editText, currentLine[1]);

/***/ Thanks for your time.

chinalwb commented 5 years ago

@rishabhk669 Thanks so much for the fixing codes. I can merge them, however why not make a Pull Request, I believe that's a more common way to contribute on Github.

Let me know if you have any problem about creating a PR.

Thanks.

rishabhk669 commented 5 years ago

Hi @chinalwb I will try to make PR and if stand out with any issue, I will ping you. Thanks for consideration.

Best Regards

chinalwb commented 5 years ago

Sure. Thank you.

Best Regards.

frankyxcs commented 5 years ago

Hi @chinalwb I will try to make PR and if stand out with any issue, I will ping you. Thanks for consideration.

Best Regards

Hi rishabhk669

very nice fixes !! I appreciate this. Will you make more fixed or have you already done some additional fixes ? Would be nice to share it or here or make a complete Pull Request

Greets Frank

frankyxcs commented 5 years ago

hi rishabhk669 multi-line indent, multi-line bullet and numbering and quote is not happening. Even after below change some fix is require, but will do it later. //Files: wherever getCurrentCursorLine(EditText) is getting use // replace Util.getCurrentCursorLine(EditText) with Util.getCurrentSelectionLines(EditText) // get start, end as follows: int start = Util.getThisLineStart(editText, currentLine[0]); int end = Util.getThisLineEnd(editText, currentLine[1]);

did you change this only in the file ARE_Quote.java ??

Also Util.getCurrentSelectionLines is an array and not an int. you forgot to mention this

greets frank

rishabhk669 commented 5 years ago

Hi @chinalwb , @frankyxcs ,

I am trying to take checkout of project in Android Studio. Are you guys using Android Studio? Folder structure is completely different.

I have Migrated project to AppCompat, AndroidX in Android Studio. Would you like to get it or you guys are using some other IDE.