diff --git a/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java b/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java index 1944be7c131ff..1724e4a8c831c 100644 --- a/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java +++ b/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecManager.java @@ -5,8 +5,8 @@ import android.support.annotation.Nullable; import android.text.Editable; import android.text.TextWatcher; -import android.view.View; import android.util.Log; +import android.view.View; import com.facebook.infer.annotation.Assertions; import com.facebook.react.bridge.ReactContext; @@ -173,12 +173,14 @@ public void receiveCommand(final ReactAztecText parent, int commandType, @Nullab Assertions.assertNotNull(parent); Assertions.assertNotNull(args); switch (commandType) { - case COMMAND_NOTIFY_APPLY_FORMAT: { - final String format = args.getString(0); - Log.d(TAG, String.format("Apply format: %s", format)); - parent.applyFormat(format); - return; - } + case COMMAND_NOTIFY_APPLY_FORMAT: { + final String format = args.getString(0); + Log.d(TAG, String.format("Apply format: %s", format)); + parent.applyFormat(format); + return; + } + default: + super.receiveCommand(parent, commandType, args); } } diff --git a/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecText.java b/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecText.java index 512a5dc579fd3..b8fa473ff2478 100644 --- a/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecText.java +++ b/android/src/main/java/org/wordpress/mobile/ReactNativeAztec/ReactAztecText.java @@ -12,6 +12,8 @@ import com.facebook.react.views.textinput.ScrollWatcher; import org.wordpress.aztec.AztecText; +import org.wordpress.aztec.AztecTextFormat; +import org.wordpress.aztec.ITextFormat; import org.wordpress.aztec.glideloader.GlideImageLoader; import org.wordpress.aztec.glideloader.GlideVideoThumbnailLoader; import org.wordpress.aztec.plugins.CssUnderlinePlugin; @@ -147,14 +149,49 @@ public void setIsSettingTextFromJS(boolean mIsSettingTextFromJS) { } public void applyFormat(String format) { + ArrayList newFormats = new ArrayList<>(); switch (format) { case ("bold"): + newFormats.add(AztecTextFormat.FORMAT_STRONG); + newFormats.add(AztecTextFormat.FORMAT_BOLD); break; case ("italic"): + newFormats.add(AztecTextFormat.FORMAT_ITALIC); + newFormats.add(AztecTextFormat.FORMAT_CITE); break; case ("strikethrough"): + newFormats.add(AztecTextFormat.FORMAT_STRIKETHROUGH); break; } + + if (newFormats.size() == 0) { + return; + } + + if (!isTextSelected()) { + setSelectedStyles(getNewStylesList(newFormats)); + } else { + toggleFormatting(newFormats.get(0)); + } + } + + // Removes all formats in the list but if none found, applies the first one + private ArrayList getNewStylesList(ArrayList newFormats) { + ArrayList textFormats = new ArrayList<>(); + textFormats.addAll(getSelectedStyles()); + boolean wasRemoved = false; + for (ITextFormat currentFormat : newFormats) { + if (textFormats.contains(currentFormat)) { + wasRemoved = true; + textFormats.remove(currentFormat); + } + } + + if (!wasRemoved) { + textFormats.add(newFormats.get(0)); + } + + return textFormats; } /**