Skip to content

Commit

Permalink
Merge pull request #45 from wordpress-mobile/add/format_actions_android
Browse files Browse the repository at this point in the history
Add/format actions to Android
  • Loading branch information
SergioEstevao authored Aug 20, 2018
2 parents 5b0c1fd + 9f8f01a commit 5731b69
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -147,14 +149,49 @@ public void setIsSettingTextFromJS(boolean mIsSettingTextFromJS) {
}

public void applyFormat(String format) {
ArrayList<ITextFormat> 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<ITextFormat> getNewStylesList(ArrayList<ITextFormat> newFormats) {
ArrayList<ITextFormat> 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;
}

/**
Expand Down

0 comments on commit 5731b69

Please sign in to comment.