Skip to content

Commit bbba60a

Browse files
committed
Added partial support for the callback-selection-changed JS callback
- Editor is notified when the focused field (title or content) changes - Handled disabling the format bar when the title is in focus
1 parent bb8c537 commit bbba60a

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

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

100644100755
Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ 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+
private static final String TAG_FORMAT_BAR_BUTTON_HTML = "html";
45+
46+
private static final float TOOLBAR_ALPHA_ENABLED = 1;
47+
private static final float TOOLBAR_ALPHA_DISABLED = 0.5f;
3848

3949
private String mParamTitle;
4050
private String mParamContent;
@@ -72,10 +82,34 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
7282
mWebView = (EditorWebView) view.findViewById(R.id.webview);
7383
initWebView();
7484

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

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

@@ -200,4 +234,31 @@ public void run() {
200234
}
201235
});
202236
}
237+
238+
public void onSelectionChanged(final Map<String, String> selectionArgs) {
239+
final String id = selectionArgs.get("id"); // The field currently in focus
240+
mWebView.post(new Runnable() {
241+
@Override
242+
public void run() {
243+
if (!id.isEmpty()) {
244+
switch(id) {
245+
case "zss_field_title":
246+
updateToolbarEnabledState(false);
247+
break;
248+
case "zss_field_content":
249+
updateToolbarEnabledState(true);
250+
break;
251+
}
252+
}
253+
}
254+
});
255+
}
256+
257+
void updateToolbarEnabledState(boolean enabled) {
258+
float alpha = (enabled ? TOOLBAR_ALPHA_ENABLED : TOOLBAR_ALPHA_DISABLED);
259+
for(ToggleButton button : mTagToggleButtonMap.values()) {
260+
button.setEnabled(enabled);
261+
button.setAlpha(alpha);
262+
}
263+
}
203264
}

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/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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ 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+
selectionArgs.put(splitString[0], splitString[1]);
79+
}
80+
return selectionArgs;
81+
}
82+
6983
/**
7084
* Compares two <code>Sets</code> and returns a <code>Map</code> of elements not contained in both
7185
* <code>Sets</code>. Elements contained in <code>oldSet</code> but not in <code>newSet</code> will be marked

0 commit comments

Comments
 (0)