Skip to content

Commit 4478cf3

Browse files
committed
Merge pull request #133 from wordpress-mobile/issue/10-format-bar-phone
Issue #10: Format bar (phone)
2 parents 6195fe0 + 50524b9 commit 4478cf3

File tree

155 files changed

+554
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+554
-90
lines changed

WordPressEditor/src/androidTest/java/org/wordpress/android/editor/UtilsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Set;
1313

1414
import static org.junit.Assert.assertEquals;
15+
import static org.wordpress.android.editor.Utils.buildMapFromKeyValuePairs;
1516
import static org.wordpress.android.editor.Utils.getChangeMapFromSets;
1617
import static org.wordpress.android.editor.Utils.splitDelimitedString;
1718

@@ -34,6 +35,40 @@ public void testSplitDelimitedString() {
3435
assertEquals(Collections.emptySet(), splitDelimitedString("", "~"));
3536
}
3637

38+
@Test
39+
public void testBuildMapFromKeyValuePairs() {
40+
Set<String> keyValueSet = new HashSet<>();
41+
Map<String, String> expectedMap = new HashMap<>();
42+
43+
// Test normal usage
44+
keyValueSet.add("id=test");
45+
keyValueSet.add("name=example");
46+
47+
expectedMap.put("id", "test");
48+
expectedMap.put("name", "example");
49+
50+
assertEquals(expectedMap, buildMapFromKeyValuePairs(keyValueSet));
51+
52+
// Test mixed valid and invalid entries
53+
keyValueSet.clear();
54+
keyValueSet.add("test");
55+
keyValueSet.add("name=example");
56+
57+
expectedMap.clear();
58+
expectedMap.put("name", "example");
59+
60+
assertEquals(expectedMap, buildMapFromKeyValuePairs(keyValueSet));
61+
62+
// Test invalid entry
63+
keyValueSet.clear();
64+
keyValueSet.add("test");
65+
66+
assertEquals(Collections.emptyMap(), buildMapFromKeyValuePairs(keyValueSet));
67+
68+
// Test empty sets
69+
assertEquals(Collections.emptyMap(), buildMapFromKeyValuePairs(Collections.<String>emptySet()));
70+
}
71+
3772
@Test
3873
public void testGetChangeMapFromSets() {
3974
Set<String> oldSet = new HashSet<>();

WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java

100644100755
Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,16 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli
3434

3535
private static final String JS_CALLBACK_HANDLER = "nativeCallbackHandler";
3636

37+
private static final String TAG_FORMAT_BAR_BUTTON_MEDIA = "media";
3738
private static final String TAG_FORMAT_BAR_BUTTON_BOLD = "bold";
39+
private static final String TAG_FORMAT_BAR_BUTTON_ITALIC = "italic";
40+
private static final String TAG_FORMAT_BAR_BUTTON_QUOTE = "blockquote";
41+
private static final String TAG_FORMAT_BAR_BUTTON_UL = "unorderedList";
42+
private static final String TAG_FORMAT_BAR_BUTTON_OL = "orderedList";
43+
private static final String TAG_FORMAT_BAR_BUTTON_LINK = "link";
44+
45+
private static final float TOOLBAR_ALPHA_ENABLED = 1;
46+
private static final float TOOLBAR_ALPHA_DISABLED = 0.5f;
3847

3948
private String mParamTitle;
4049
private String mParamContent;
@@ -72,10 +81,34 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7281
mWebView = (EditorWebView) view.findViewById(R.id.webview);
7382
initWebView();
7483

75-
ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.bold);
76-
boldButton.setOnClickListener(this);
84+
ToggleButton mediaButton = (ToggleButton) view.findViewById(R.id.format_bar_button_media);
85+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_MEDIA, mediaButton);
86+
87+
ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.format_bar_button_bold);
7788
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_BOLD, boldButton);
7889

90+
ToggleButton italicButton = (ToggleButton) view.findViewById(R.id.format_bar_button_italic);
91+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_ITALIC, italicButton);
92+
93+
ToggleButton quoteButton = (ToggleButton) view.findViewById(R.id.format_bar_button_quote);
94+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_QUOTE, quoteButton);
95+
96+
ToggleButton ulButton = (ToggleButton) view.findViewById(R.id.format_bar_button_ul);
97+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_UL, ulButton);
98+
99+
ToggleButton olButton = (ToggleButton) view.findViewById(R.id.format_bar_button_ol);
100+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_OL, olButton);
101+
102+
ToggleButton linkButton = (ToggleButton) view.findViewById(R.id.format_bar_button_link);
103+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_LINK, linkButton);
104+
105+
ToggleButton htmlButton = (ToggleButton) view.findViewById(R.id.format_bar_button_html);
106+
htmlButton.setOnClickListener(this);
107+
108+
for (ToggleButton button : mTagToggleButtonMap.values()) {
109+
button.setOnClickListener(this);
110+
}
111+
79112
return view;
80113
}
81114

@@ -120,8 +153,25 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu
120153
@Override
121154
public void onClick(View v) {
122155
int id = v.getId();
123-
if (id == R.id.bold) {
156+
if (id == R.id.format_bar_button_bold) {
124157
mWebView.execJavaScriptFromString("ZSSEditor.setBold();");
158+
} else if (id == R.id.format_bar_button_italic) {
159+
mWebView.execJavaScriptFromString("ZSSEditor.setItalic();");
160+
} else if (id == R.id.format_bar_button_quote) {
161+
mWebView.execJavaScriptFromString("ZSSEditor.setBlockquote();");
162+
} else if (id == R.id.format_bar_button_ul) {
163+
mWebView.execJavaScriptFromString("ZSSEditor.setUnorderedList();");
164+
} else if (id == R.id.format_bar_button_ol) {
165+
mWebView.execJavaScriptFromString("ZSSEditor.setOrderedList();");
166+
} else if (id == R.id.format_bar_button_media) {
167+
// TODO: Handle inserting media
168+
((ToggleButton) v).setChecked(false);
169+
} else if (id == R.id.format_bar_button_link) {
170+
// TODO: Handle inserting a link
171+
((ToggleButton) v).setChecked(false);
172+
} else if (id == R.id.format_bar_button_html) {
173+
// TODO: Handle HTML mode toggling
174+
((ToggleButton) v).setChecked(false);
125175
}
126176
}
127177

@@ -200,4 +250,31 @@ public void run() {
200250
}
201251
});
202252
}
253+
254+
public void onSelectionChanged(final Map<String, String> selectionArgs) {
255+
final String id = selectionArgs.get("id"); // The field currently in focus
256+
mWebView.post(new Runnable() {
257+
@Override
258+
public void run() {
259+
if (!id.isEmpty()) {
260+
switch(id) {
261+
case "zss_field_title":
262+
updateToolbarEnabledState(false);
263+
break;
264+
case "zss_field_content":
265+
updateToolbarEnabledState(true);
266+
break;
267+
}
268+
}
269+
}
270+
});
271+
}
272+
273+
void updateToolbarEnabledState(boolean enabled) {
274+
float alpha = (enabled ? TOOLBAR_ALPHA_ENABLED : TOOLBAR_ALPHA_DISABLED);
275+
for(ToggleButton button : mTagToggleButtonMap.values()) {
276+
button.setEnabled(enabled);
277+
button.setAlpha(alpha);
278+
}
279+
}
203280
}

WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java

100644100755
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ public void executeCallback(String callbackId, String params) {
4848
mPreviousStyleSet = newStyleSet;
4949
break;
5050
case CALLBACK_SELECTION_CHANGED:
51-
// Called when changes are made to selection (includes moving the caret without selecting text)
51+
// Called for changes to the field in current focus and for changes made to selection
52+
// (includes moving the caret without selecting text)
5253
// TODO: Possibly needed for handling WebView scrolling when caret moves (from iOS)
54+
Set<String> selectionKeyValueSet = Utils.splitDelimitedString(params, JS_CALLBACK_DELIMITER);
55+
mListener.onSelectionChanged(Utils.buildMapFromKeyValuePairs(selectionKeyValueSet));
5356
break;
5457
case CALLBACK_INPUT:
5558
// Called on key press

WordPressEditor/src/main/java/org/wordpress/android/editor/LegacyEditorFragment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private WPEditImageSpan createWPEditImageSpanLocal(Context context, MediaFile me
443443
}
444444

445445
private WPEditImageSpan createWPEditImageSpanRemote(Context context, MediaFile mediaFile) {
446-
int drawable = mediaFile.isVideo() ? R.drawable.media_movieclip : R.drawable.dashicon_format_image_big_grey;
446+
int drawable = mediaFile.isVideo() ? R.drawable.media_movieclip : R.drawable.legacy_dashicon_format_image_big_grey;
447447
Uri uri = Uri.parse(mediaFile.getFileURL());
448448
WPEditImageSpan imageSpan = new WPEditImageSpan(context, drawable, uri);
449449
imageSpan.setMediaFile(mediaFile);
@@ -1046,7 +1046,7 @@ public void appendGallery(MediaGallery mediaGallery) {
10461046

10471047
editableText.insert(selectionStart, " ");
10481048
MediaGalleryImageSpan is = new MediaGalleryImageSpan(getActivity(), mediaGallery,
1049-
R.drawable.icon_mediagallery_placeholder);
1049+
R.drawable.legacy_icon_mediagallery_placeholder);
10501050
editableText.setSpan(is, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
10511051
AlignmentSpan.Standard as = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
10521052
editableText.setSpan(as, selectionStart, selectionEnd + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

WordPressEditor/src/main/java/org/wordpress/android/editor/OnJsEditorStateChangedListener.java

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
public interface OnJsEditorStateChangedListener {
66
void onDomLoaded();
7+
void onSelectionChanged(Map<String, String> selectionArgs);
78
void onSelectionStyleChanged(Map<String, Boolean> changeSet);
89
}

WordPressEditor/src/main/java/org/wordpress/android/editor/Utils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ public static Set<String> splitDelimitedString(String string, String delimiter)
6666
return splitString;
6767
}
6868

69+
/**
70+
* Accepts a set of strings, each string being a key-value pair (<code>id=5</code>,
71+
* <code>name=content-filed</code>). Returns a map of all the key-value pairs in the set.
72+
* @param keyValueSet the set of key-value pair strings
73+
*/
74+
public static Map<String, String> buildMapFromKeyValuePairs(Set<String> keyValueSet) {
75+
Map<String, String> selectionArgs = new HashMap<>();
76+
for (String pair : keyValueSet) {
77+
String[] splitString = pair.split("=");
78+
if (splitString.length == 2) {
79+
selectionArgs.put(splitString[0], splitString[1]);
80+
}
81+
}
82+
return selectionArgs;
83+
}
84+
6985
/**
7086
* Compares two <code>Sets</code> and returns a <code>Map</code> of elements not contained in both
7187
* <code>Sets</code>. Elements contained in <code>oldSet</code> but not in <code>newSet</code> will be marked
2.34 KB
2.21 KB
837 Bytes
788 Bytes

0 commit comments

Comments
 (0)