Skip to content

Commit b7be242

Browse files
committed
Added handling for the "callback-selection-style" JS callback
- Added support for toggling styles in the editor using the format bar, and handling the callback to update the highlighted status of format bar buttons
1 parent e9c6401 commit b7be242

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,23 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.io.InputStreamReader;
30+
import java.util.HashMap;
31+
import java.util.Map;
3032

3133
public class EditorFragment extends EditorFragmentAbstract implements View.OnClickListener, JsCallbackListener {
3234
private static final String ARG_PARAM_TITLE = "param_title";
3335
private static final String ARG_PARAM_CONTENT = "param_content";
3436

3537
private static final String JS_CALLBACK_HANDLER = "nativeCallbackHandler";
3638

39+
private static final String TAG_FORMAT_BAR_BUTTON_BOLD = "bold";
40+
3741
private String mParamTitle;
3842
private String mParamContent;
3943
private WebView mWebView;
4044

41-
private ToggleButton mBoldButton;
45+
private final Map<String, ToggleButton> mTagToggleButtonMap = new HashMap<>();
46+
4247
public static EditorFragment newInstance(String title, String content) {
4348
EditorFragment fragment = new EditorFragment();
4449
Bundle args = new Bundle();
@@ -66,8 +71,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
6671
mWebView = (WebView) view.findViewById(R.id.webview);
6772
initWebView();
6873

69-
mBoldButton = (ToggleButton) view.findViewById(R.id.bold);
70-
mBoldButton.setOnClickListener(this);
74+
ToggleButton boldButton = (ToggleButton) view.findViewById(R.id.bold);
75+
boldButton.setOnClickListener(this);
76+
mTagToggleButtonMap.put(TAG_FORMAT_BAR_BUTTON_BOLD, boldButton);
7177

7278
return view;
7379
}
@@ -110,6 +116,14 @@ public boolean onJsAlert(WebView view, String url, String message, JsResult resu
110116
enableWebDebugging(true);
111117
}
112118

119+
@Override
120+
public void onClick(View v) {
121+
int id = v.getId();
122+
if (id == R.id.bold) {
123+
execJavaScriptFromString("ZSSEditor.setBold();");
124+
}
125+
}
126+
113127
private String getStringFromAsset(String filename) throws IOException {
114128
if (!isAdded()) {
115129
return null;
@@ -202,4 +216,18 @@ public void run() {
202216
}
203217
});
204218
}
219+
220+
public void onSelectionStyleChanged(final Map<String, Boolean> changeMap) {
221+
mWebView.post(new Runnable() {
222+
public void run() {
223+
for (Map.Entry<String, Boolean> entry : changeMap.entrySet()) {
224+
// Handle toggling format bar style buttons
225+
ToggleButton button = mTagToggleButtonMap.get(entry.getKey());
226+
if (button != null) {
227+
button.setChecked(entry.getValue());
228+
}
229+
}
230+
}
231+
});
232+
}
205233
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
import org.wordpress.android.util.AppLog;
66

7+
import java.util.HashSet;
8+
import java.util.Set;
9+
710
public class JsCallbackHandler {
11+
private static final String JS_CALLBACK_DELIMITER = "~";
12+
813
private static final String CALLBACK_LOG = "callback-log";
914

1015
private static final String CALLBACK_DOM_LOADED = "callback-dom-loaded";
@@ -24,6 +29,8 @@ public class JsCallbackHandler {
2429

2530
private final JsCallbackListener mJsCallbackListener;
2631

32+
private Set<String> mPreviousStyleSet = new HashSet<>();
33+
2734
public JsCallbackHandler(EditorFragmentAbstract editorFragmentAbstract) {
2835
mJsCallbackListener = (JsCallbackListener) editorFragmentAbstract;
2936
}
@@ -34,6 +41,12 @@ public void executeCallback(String callbackId, String params) {
3441
case CALLBACK_DOM_LOADED:
3542
mJsCallbackListener.onDomLoaded();
3643
break;
44+
case CALLBACK_SELECTION_STYLE:
45+
// Compare the new styles to the previous ones, and notify the JsCallbackListener of the changeset
46+
Set<String> newStyleSet = Utils.splitDelimitedString(params, JS_CALLBACK_DELIMITER);
47+
mJsCallbackListener.onSelectionStyleChanged(Utils.getChangeMapFromSets(mPreviousStyleSet, newStyleSet));
48+
mPreviousStyleSet = newStyleSet;
49+
break;
3750
case CALLBACK_LOG:
3851
// Strip 'msg=' from beginning of string
3952
AppLog.d(AppLog.T.EDITOR, callbackId + ": " + params.substring(4));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.wordpress.android.editor;
22

3+
import java.util.Map;
4+
35
public interface JsCallbackListener {
46
void onDomLoaded();
7+
void onSelectionStyleChanged(Map<String, Boolean> changeSet);
58
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package org.wordpress.android.editor;
22

3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.StringTokenizer;
8+
39
public class Utils {
410
public static String escapeHtml(String html) {
511
html = html.replace("\\", "\\\\");
@@ -9,4 +15,50 @@ public static String escapeHtml(String html) {
915
html = html.replace("\n", "\\n");
1016
return html;
1117
}
18+
19+
/**
20+
* Splits a delimited string into a set of strings.
21+
* @param string the delimited string to split
22+
* @param delimiter the string delimiter
23+
*/
24+
public static Set<String> splitDelimitedString(String string, String delimiter) {
25+
Set<String> splitString = new HashSet<>();
26+
27+
StringTokenizer stringTokenizer = new StringTokenizer(string, delimiter);
28+
while (stringTokenizer.hasMoreTokens()) {
29+
splitString.add(stringTokenizer.nextToken());
30+
}
31+
32+
return splitString;
33+
}
34+
35+
/**
36+
* Compares two <code>Sets</code> and returns a <code>Map</code> of elements not contained in both
37+
* <code>Sets</code>. Elements contained in <code>oldSet</code> but not in <code>newSet</code> will be marked
38+
* <code>false</code> in the returned map; the converse will be marked <code>true</code>.
39+
* @param oldSet the older of the two <code>Sets</code>
40+
* @param newSet the newer of the two <code>Sets</code>
41+
* @param <E> type of element stored in the <code>Sets</code>
42+
* @return a <code>Map</code> containing the difference between <code>oldSet</code> and <code>newSet</code>, and whether the
43+
* element was added (<code>true</code>) or removed (<code>false</code>) in <code>newSet</code>
44+
*/
45+
public static <E> Map<E, Boolean> getChangeMapFromSets(Set<E> oldSet, Set<E> newSet) {
46+
Map<E, Boolean> changeMap = new HashMap<>();
47+
48+
Set<E> additions = new HashSet<>(newSet);
49+
additions.removeAll(oldSet);
50+
51+
Set<E> removals = new HashSet<>(oldSet);
52+
removals.removeAll(newSet);
53+
54+
for (E s : additions) {
55+
changeMap.put(s, true);
56+
}
57+
58+
for (E s : removals) {
59+
changeMap.put(s, false);
60+
}
61+
62+
return changeMap;
63+
}
1264
}

0 commit comments

Comments
 (0)