Skip to content

Commit

Permalink
Added partial support for the callback-selection-changed JS callback
Browse files Browse the repository at this point in the history
- Editor is notified when the focused field (title or content) changes
- Handled disabling the format bar when the title is in focus
  • Loading branch information
aforcier committed Apr 17, 2015
1 parent bb8c537 commit bbba60a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
65 changes: 63 additions & 2 deletions WordPressEditor/src/main/java/org/wordpress/android/editor/EditorFragment.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli

private static final String JS_CALLBACK_HANDLER = "nativeCallbackHandler";

private static final String TAG_FORMAT_BAR_BUTTON_MEDIA = "media";
private static final String TAG_FORMAT_BAR_BUTTON_BOLD = "bold";
private static final String TAG_FORMAT_BAR_BUTTON_ITALIC = "italic";
private static final String TAG_FORMAT_BAR_BUTTON_QUOTE = "blockquote";
private static final String TAG_FORMAT_BAR_BUTTON_UL = "unorderedList";
private static final String TAG_FORMAT_BAR_BUTTON_OL = "orderedList";
private static final String TAG_FORMAT_BAR_BUTTON_LINK = "link";
private static final String TAG_FORMAT_BAR_BUTTON_HTML = "html";

private static final float TOOLBAR_ALPHA_ENABLED = 1;
private static final float TOOLBAR_ALPHA_DISABLED = 0.5f;

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

ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.bold);
boldButton.setOnClickListener(this);
ToggleButton mediaButton = (ToggleButton) view.findViewById(R.id.format_bar_button_media);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_MEDIA, mediaButton);

ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.format_bar_button_bold);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_BOLD, boldButton);

ToggleButton italicButton = (ToggleButton) view.findViewById(R.id.format_bar_button_italic);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_ITALIC, italicButton);

ToggleButton quoteButton = (ToggleButton) view.findViewById(R.id.format_bar_button_quote);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_QUOTE, quoteButton);

ToggleButton ulButton = (ToggleButton) view.findViewById(R.id.format_bar_button_ul);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_UL, ulButton);

ToggleButton olButton = (ToggleButton) view.findViewById(R.id.format_bar_button_ol);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_OL, olButton);

ToggleButton linkButton = (ToggleButton) view.findViewById(R.id.format_bar_button_link);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_LINK, linkButton);

ToggleButton htmlButton = (ToggleButton) view.findViewById(R.id.format_bar_button_html);
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_HTML, htmlButton);

for (ToggleButton button : mTagToggleButtonMap.values()) {
button.setOnClickListener(this);
}

return view;
}

Expand Down Expand Up @@ -200,4 +234,31 @@ public void run() {
}
});
}

public void onSelectionChanged(final Map<String, String> selectionArgs) {
final String id = selectionArgs.get("id"); // The field currently in focus
mWebView.post(new Runnable() {
@Override
public void run() {
if (!id.isEmpty()) {
switch(id) {
case "zss_field_title":
updateToolbarEnabledState(false);
break;
case "zss_field_content":
updateToolbarEnabledState(true);
break;
}
}
}
});
}

void updateToolbarEnabledState(boolean enabled) {
float alpha = (enabled ? TOOLBAR_ALPHA_ENABLED : TOOLBAR_ALPHA_DISABLED);
for(ToggleButton button : mTagToggleButtonMap.values()) {
button.setEnabled(enabled);
button.setAlpha(alpha);
}
}
}
5 changes: 4 additions & 1 deletion WordPressEditor/src/main/java/org/wordpress/android/editor/JsCallbackReceiver.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public void executeCallback(String callbackId, String params) {
mPreviousStyleSet = newStyleSet;
break;
case CALLBACK_SELECTION_CHANGED:
// Called when changes are made to selection (includes moving the caret without selecting text)
// Called for changes to the field in current focus and for changes made to selection
// (includes moving the caret without selecting text)
// TODO: Possibly needed for handling WebView scrolling when caret moves (from iOS)
Set<String> selectionKeyValueSet = Utils.splitDelimitedString(params, JS_CALLBACK_DELIMITER);
mListener.onSelectionChanged(Utils.buildMapFromKeyValuePairs(selectionKeyValueSet));
break;
case CALLBACK_INPUT:
// Called on key press
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

public interface OnJsEditorStateChangedListener {
void onDomLoaded();
void onSelectionChanged(Map<String, String> selectionArgs);
void onSelectionStyleChanged(Map<String, Boolean> changeSet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public static Set<String> splitDelimitedString(String string, String delimiter)
return splitString;
}

/**
* Accepts a set of strings, each string being a key-value pair (<code>id=5</code>,
* <code>name=content-filed</code>). Returns a map of all the key-value pairs in the set.
* @param keyValueSet the set of key-value pair strings
*/
public static Map<String, String> buildMapFromKeyValuePairs(Set<String> keyValueSet) {
Map<String, String> selectionArgs = new HashMap<>();
for (String pair : keyValueSet) {
String[] splitString = pair.split("=");
selectionArgs.put(splitString[0], splitString[1]);
}
return selectionArgs;
}

/**
* Compares two <code>Sets</code> and returns a <code>Map</code> of elements not contained in both
* <code>Sets</code>. Elements contained in <code>oldSet</code> but not in <code>newSet</code> will be marked
Expand Down

0 comments on commit bbba60a

Please sign in to comment.