Search in sources :

Example 16 with QuoteSpan

use of android.text.style.QuoteSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method afterTextChanged.

@Override
public void afterTextChanged(Editable s) {
    int position = Selection.getSelectionStart(mContentEditText.getText());
    if ((mIsBackspace && position != 1) || mLastPosition == position || !mIsLocalDraft)
        return;
    if (position < 0) {
        position = 0;
    }
    mLastPosition = position;
    if (position > 0) {
        if (mStyleStart > position) {
            mStyleStart = position - 1;
        }
        boolean shouldBold = mBoldToggleButton.isChecked();
        boolean shouldEm = mEmToggleButton.isChecked();
        boolean shouldUnderline = mUnderlineToggleButton.isChecked();
        boolean shouldStrike = mStrikeToggleButton.isChecked();
        boolean shouldQuote = mBquoteToggleButton.isChecked();
        Object[] allSpans = s.getSpans(mStyleStart, position, Object.class);
        for (Object span : allSpans) {
            if (span instanceof StyleSpan) {
                StyleSpan styleSpan = (StyleSpan) span;
                if (styleSpan.getStyle() == Typeface.BOLD)
                    shouldBold = false;
                else if (styleSpan.getStyle() == Typeface.ITALIC)
                    shouldEm = false;
            } else if (span instanceof WPUnderlineSpan) {
                shouldUnderline = false;
            } else if (span instanceof StrikethroughSpan) {
                shouldStrike = false;
            } else if (span instanceof QuoteSpan) {
                shouldQuote = false;
            }
        }
        if (shouldBold)
            s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), mStyleStart, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        if (shouldEm)
            s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), mStyleStart, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        if (shouldUnderline)
            s.setSpan(new WPUnderlineSpan(), mStyleStart, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        if (shouldStrike)
            s.setSpan(new StrikethroughSpan(), mStyleStart, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        if (shouldQuote)
            s.setSpan(new QuoteSpan(), mStyleStart, position, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }
}
Also used : WPUnderlineSpan(org.wordpress.android.util.helpers.WPUnderlineSpan) StyleSpan(android.text.style.StyleSpan) QuoteSpan(android.text.style.QuoteSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 17 with QuoteSpan

use of android.text.style.QuoteSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method onSelectionChanged.

@Override
public void onSelectionChanged() {
    if (!mIsLocalDraft) {
        return;
    }
    final Spannable s = mContentEditText.getText();
    if (s == null)
        return;
    // set toggle buttons if cursor is inside of a matching span
    mStyleStart = mContentEditText.getSelectionStart();
    Object[] spans = s.getSpans(mContentEditText.getSelectionStart(), mContentEditText.getSelectionStart(), Object.class);
    mBoldToggleButton.setChecked(false);
    mEmToggleButton.setChecked(false);
    mBquoteToggleButton.setChecked(false);
    mUnderlineToggleButton.setChecked(false);
    mStrikeToggleButton.setChecked(false);
    for (Object span : spans) {
        if (span instanceof StyleSpan) {
            StyleSpan ss = (StyleSpan) span;
            if (ss.getStyle() == android.graphics.Typeface.BOLD) {
                mBoldToggleButton.setChecked(true);
            }
            if (ss.getStyle() == android.graphics.Typeface.ITALIC) {
                mEmToggleButton.setChecked(true);
            }
        }
        if (span instanceof QuoteSpan) {
            mBquoteToggleButton.setChecked(true);
        }
        if (span instanceof WPUnderlineSpan) {
            mUnderlineToggleButton.setChecked(true);
        }
        if (span instanceof StrikethroughSpan) {
            mStrikeToggleButton.setChecked(true);
        }
    }
}
Also used : WPUnderlineSpan(org.wordpress.android.util.helpers.WPUnderlineSpan) StyleSpan(android.text.style.StyleSpan) QuoteSpan(android.text.style.QuoteSpan) Spannable(android.text.Spannable) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 18 with QuoteSpan

use of android.text.style.QuoteSpan in project WordPress-Android by wordpress-mobile.

the class WPHtml method withinDiv.

@SuppressWarnings("unused")
private static void withinDiv(StringBuilder out, Spanned text, int start, int end) {
    int next;
    for (int i = start; i < end; i = next) {
        next = text.nextSpanTransition(i, end, QuoteSpan.class);
        QuoteSpan[] quotes = text.getSpans(i, next, QuoteSpan.class);
        for (QuoteSpan quote : quotes) {
            out.append("<blockquote>");
        }
        withinBlockquote(out, text, i, next);
        for (QuoteSpan quote : quotes) {
            out.append("</blockquote>\n");
        }
    }
}
Also used : QuoteSpan(android.text.style.QuoteSpan)

Example 19 with QuoteSpan

use of android.text.style.QuoteSpan in project WordPress-Android by wordpress-mobile.

the class HtmlToSpannedConverter method handleEndTag.

private void handleEndTag(String tag) {
    if (mPost != null) {
        if (!mPost.isLocalDraft())
            return;
    }
    if (!mysteryTagFound) {
        if (tag.equalsIgnoreCase("br")) {
            handleBr(mSpannableStringBuilder);
        } else if (tag.equalsIgnoreCase("p")) {
            handleP(mSpannableStringBuilder);
        } else if (tag.equalsIgnoreCase("div")) {
            handleP(mSpannableStringBuilder);
        } else if (tag.equalsIgnoreCase("em")) {
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC));
        } else if (tag.equalsIgnoreCase("b")) {
            end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD));
        } else if (tag.equalsIgnoreCase("strong")) {
            end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD));
        } else if (tag.equalsIgnoreCase("cite")) {
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC));
        } else if (tag.equalsIgnoreCase("dfn")) {
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC));
        } else if (tag.equalsIgnoreCase("i")) {
            end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC));
        } else if (tag.equalsIgnoreCase("big")) {
            end(mSpannableStringBuilder, Big.class, new RelativeSizeSpan(1.25f));
        } else if (tag.equalsIgnoreCase("small")) {
            end(mSpannableStringBuilder, Small.class, new RelativeSizeSpan(0.8f));
        } else if (tag.equalsIgnoreCase("font")) {
            endFont(mSpannableStringBuilder);
        } else if (tag.equalsIgnoreCase("blockquote")) {
            handleP(mSpannableStringBuilder);
            end(mSpannableStringBuilder, Blockquote.class, new QuoteSpan());
        } else if (tag.equalsIgnoreCase("tt")) {
            end(mSpannableStringBuilder, Monospace.class, new TypefaceSpan("monospace"));
        } else if (tag.equalsIgnoreCase("a")) {
            endA(mSpannableStringBuilder);
        } else if (tag.equalsIgnoreCase("u")) {
            end(mSpannableStringBuilder, Underline.class, new WPUnderlineSpan());
        } else if (tag.equalsIgnoreCase("sup")) {
            end(mSpannableStringBuilder, Super.class, new SuperscriptSpan());
        } else if (tag.equalsIgnoreCase("sub")) {
            end(mSpannableStringBuilder, Sub.class, new SubscriptSpan());
        } else if (tag.equalsIgnoreCase("strike")) {
            end(mSpannableStringBuilder, Strike.class, new StrikethroughSpan());
        } else if (tag.length() == 2 && Character.toLowerCase(tag.charAt(0)) == 'h' && tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {
            handleP(mSpannableStringBuilder);
            endHeader(mSpannableStringBuilder);
        }
    } else {
        if (tag.equalsIgnoreCase("html") || tag.equalsIgnoreCase("body")) {
            return;
        }
        if (mysteryTagName.equals(tag)) {
            mysteryTagFound = false;
            mSpannableStringBuilder.append(mysteryTagContent);
        }
    // mTagHandler.handleTag(false, tag, mSpannableStringBuilder,
    // mReader,
    // mysteryTagContent);
    }
}
Also used : SuperscriptSpan(android.text.style.SuperscriptSpan) WPUnderlineSpan(org.wordpress.android.util.helpers.WPUnderlineSpan) RelativeSizeSpan(android.text.style.RelativeSizeSpan) QuoteSpan(android.text.style.QuoteSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) TypefaceSpan(android.text.style.TypefaceSpan) StrikethroughSpan(android.text.style.StrikethroughSpan)

Example 20 with QuoteSpan

use of android.text.style.QuoteSpan in project WordPress-Android by wordpress-mobile.

the class HtmlUtils method fromHtml.

/**
     * An alternative to Html.fromHtml() supporting {@code <ul>}, {@code <ol>}, {@code <blockquote>}
     * tags and replacing EmoticonsUtils with Emojis
     * @param  source
     * @param  wpImageGetter
     */
public static SpannableStringBuilder fromHtml(String source, WPImageGetter wpImageGetter) {
    SpannableStringBuilder html;
    try {
        html = (SpannableStringBuilder) Html.fromHtml(source, wpImageGetter, new WPHtmlTagHandler());
    } catch (RuntimeException runtimeException) {
        // In case our tag handler fails
        html = (SpannableStringBuilder) Html.fromHtml(source, wpImageGetter, null);
    }
    EmoticonsUtils.replaceEmoticonsWithEmoji(html);
    QuoteSpan[] spans = html.getSpans(0, html.length(), QuoteSpan.class);
    for (QuoteSpan span : spans) {
        html.setSpan(new WPQuoteSpan(), html.getSpanStart(span), html.getSpanEnd(span), html.getSpanFlags(span));
        html.setSpan(new ForegroundColorSpan(0xFF666666), html.getSpanStart(span), html.getSpanEnd(span), html.getSpanFlags(span));
        html.removeSpan(span);
    }
    return html;
}
Also used : WPHtmlTagHandler(org.wordpress.android.util.helpers.WPHtmlTagHandler) WPQuoteSpan(org.wordpress.android.util.helpers.WPQuoteSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) QuoteSpan(android.text.style.QuoteSpan) WPQuoteSpan(org.wordpress.android.util.helpers.WPQuoteSpan) SpannableStringBuilder(android.text.SpannableStringBuilder)

Aggregations

QuoteSpan (android.text.style.QuoteSpan)24 StyleSpan (android.text.style.StyleSpan)5 RelativeSizeSpan (android.text.style.RelativeSizeSpan)3 StrikethroughSpan (android.text.style.StrikethroughSpan)3 SubscriptSpan (android.text.style.SubscriptSpan)3 SuperscriptSpan (android.text.style.SuperscriptSpan)3 TypefaceSpan (android.text.style.TypefaceSpan)3 WPUnderlineSpan (org.wordpress.android.util.helpers.WPUnderlineSpan)3 UnderlineSpan (android.text.style.UnderlineSpan)2 SmallTest (android.test.suitebuilder.annotation.SmallTest)1 Spannable (android.text.Spannable)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 BulletSpan (android.text.style.BulletSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 URLSpan (android.text.style.URLSpan)1 WPHtmlTagHandler (org.wordpress.android.util.helpers.WPHtmlTagHandler)1 WPQuoteSpan (org.wordpress.android.util.helpers.WPQuoteSpan)1