[go: nahoru, domu]

Skip to content

Commit

Permalink
Squashed 'libs/editor/' changes from bed2a4e..f1f6d10
Browse files Browse the repository at this point in the history
f1f6d10 Merge pull request #325 from wordpress-mobile/issue/fix-rare-crash-when-editorfragment-is-not-attached
fd11532 Merge pull request #327 from wordpress-mobile/sync-wpandroid
c1991d7 Adjusted expected callbacks in ZssEditorTest to account for different null handling between the two callback methods
2e3b075 Fixed integration tests to listen for the iframe callback method for API<17
8caccda Make placeholder replacements for android-editor.html in integration tests
f2bb3f0 Fix unit tests broken by unimplemented EditorFragmentAbstract method
c5dbd53 Merge commit '9b0b5fab24db9f435b278ad0b9e77d5895135700' into develop
512d940 Merge pull request #3897 from wordpress-mobile/issue/260editor-clear-failed-images-on-upload
ed89332 Null check getParentRangeOfFocusedNode/setRange in onMutationObserved - in case editor is not in focus
414e55f Make sure fragment is added
33215ad New EditorFragment method to removeAllFailedMediaUploads()
6564195 New JS function ZSSEditor.removeAllFailedMediaUploads

git-subtree-dir: libs/editor
git-subtree-split: f1f6d10e2381ccd4b4d7aabe277ec4b3f41fad9b
  • Loading branch information
aforcier committed Mar 24, 2016
1 parent 9b0b5fa commit c6efe0a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Instrumentation;
import android.os.Build;
import android.test.ActivityInstrumentationTestCase2;
import android.webkit.JavascriptInterface;

Expand Down Expand Up @@ -46,13 +47,31 @@ protected void setUp() throws Exception {

mSetUpLatch.countDown();

final String htmlEditor = Utils.getHtmlFromFile(activity, "android-editor.html");
String htmlEditor = Utils.getHtmlFromFile(activity, "android-editor.html");

if (htmlEditor != null) {
htmlEditor = htmlEditor.replace("%%TITLE%%", getActivity().getString(R.string.visual_editor));
htmlEditor = htmlEditor.replace("%%ANDROID_API_LEVEL%%", String.valueOf(Build.VERSION.SDK_INT));
htmlEditor = htmlEditor.replace("%%LOCALIZED_STRING_INIT%%",
"nativeState.localizedStringEdit = '" + getActivity().getString(R.string.edit) + "';\n" +
"nativeState.localizedStringUploading = '" + getActivity().getString(R.string.uploading) + "';\n" +
"nativeState.localizedStringUploadingGallery = '" +
getActivity().getString(R.string.uploading_gallery_placeholder) + "';\n");
}

final String finalHtmlEditor = htmlEditor;

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView = new EditorWebView(mInstrumentation.getContext(), null);
mWebView.addJavascriptInterface(new MockJsCallbackReceiver(), JS_CALLBACK_HANDLER);
mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", "");
if (Build.VERSION.SDK_INT < 17) {
mWebView.setJsCallbackReceiver(new MockJsCallbackReceiver(new EditorFragmentForTests()));
} else {
mWebView.addJavascriptInterface(new MockJsCallbackReceiver(new EditorFragmentForTests()),
JS_CALLBACK_HANDLER);
}
mWebView.loadDataWithBaseURL("file:///android_asset/", finalHtmlEditor, "text/html", "utf-8", "");
mSetUpLatch.countDown();
}
});
Expand All @@ -74,12 +93,16 @@ public void testInitialization() throws InterruptedException {
Set<String> expectedSet = new HashSet<>();
expectedSet.add("callback-new-field:id=zss_field_title");
expectedSet.add("callback-new-field:id=zss_field_content");
expectedSet.add("callback-dom-loaded:undefined");
expectedSet.add("callback-dom-loaded:");

assertEquals(expectedSet, mCallbackSet);
}

private class MockJsCallbackReceiver {
private class MockJsCallbackReceiver extends JsCallbackReceiver {
public MockJsCallbackReceiver(EditorFragmentAbstract editorFragmentAbstract) {
super(editorFragmentAbstract);
}

@JavascriptInterface
public void executeCallback(String callbackId, String params) {
if (callbackId.equals("callback-dom-loaded")) {
Expand All @@ -90,10 +113,12 @@ public void executeCallback(String callbackId, String params) {
// Handle callbacks and count down latches according to the currently running test
switch(mTestMethod) {
case INIT:
if (callbackId.equals("callback-new-field") || callbackId.equals("callback-dom-loaded")) {
if (callbackId.equals("callback-dom-loaded")) {
mCallbackSet.add(callbackId + ":");
} else if (callbackId.equals("callback-new-field")) {
mCallbackSet.add(callbackId + ":" + params);
mCallbackLatch.countDown();
}
mCallbackLatch.countDown();
break;
default:
throw(new RuntimeException("Unknown calling method"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,11 @@ public boolean hasFailedMediaUploads() {
return (mFailedMediaIds.size() > 0);
}

@Override
public void removeAllFailedMediaUploads() {
mWebView.execJavaScriptFromString("ZSSEditor.removeAllFailedMediaUploads();");
}

@Override
public Spanned getSpannedContent() {
return null;
Expand Down Expand Up @@ -929,6 +934,10 @@ public void onDomLoaded() {

mWebView.post(new Runnable() {
public void run() {
if (!isAdded()) {
return;
}

mDomHasLoaded = true;

mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').setMultiline('true');");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class EditorFragmentAbstract extends Fragment {
public abstract void setUrlForVideoPressId(String videoPressId, String url, String posterUrl);
public abstract boolean isUploadingMedia();
public abstract boolean hasFailedMediaUploads();
public abstract void removeAllFailedMediaUploads();
public abstract void setTitlePlaceholder(CharSequence text);
public abstract void setContentPlaceholder(CharSequence text);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,9 @@ public boolean hasFailedMediaUploads() {
return false;
}

@Override
public void removeAllFailedMediaUploads() {}

@Override
public void setTitlePlaceholder(CharSequence text) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public boolean hasFailedMediaUploads() {
return false;
}

@Override
public void removeAllFailedMediaUploads() {

}

@Override
public void setTitlePlaceholder(CharSequence text) {

Expand Down
46 changes: 33 additions & 13 deletions libs/editor-common/assets/ZSSRichTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,18 @@ ZSSEditor. {
var mediaIdentifier = removedNode.attributes.getNamedItem("data-wpid").value;
var parentRange = ZSSEditor.getParentRangeOfFocusedNode();
ZSSEditor.removeImage(mediaIdentifier);
ZSSEditor.setRange(parentRange);
if (parentRange != null) {
ZSSEditor.setRange(parentRange);
}
ZSSEditor.sendMediaRemovedCallback(mediaIdentifier);
} else if (removedNode.attributes.getNamedItem("data-video_wpid")) {
// An uploading or failed video was deleted manually - remove its container and send the callback
var mediaIdentifier = removedNode.attributes.getNamedItem("data-video_wpid").value;
var parentRange = ZSSEditor.getParentRangeOfFocusedNode();
ZSSEditor.removeVideo(mediaIdentifier);
ZSSEditor.setRange(parentRange);
if (parentRange != null) {
ZSSEditor.setRange(parentRange);
}
ZSSEditor.sendMediaRemovedCallback(mediaIdentifier);
}
}
Expand Down Expand Up @@ -957,34 +961,39 @@ ZSSEditor.markAllUploadingMediaAsFailed = function(message) {
}
};

/**
* @brief Sends a callback with a list of failed images
*/
ZSSEditor.getFailedMedia = function() {
ZSSEditor.getFailedMediaIdArray = function() {
var html = ZSSEditor.getField("zss_field_content").getHTML();
var tmp = document.createElement( "div" );
var tmpDom = $( tmp ).html( html );
var matches = tmpDom.find("img.failed");

var functionArgument = "function=getFailedMedia";
var mediaIdArray = [];

for (var i = 0; i < matches.size(); i++) {
var mediaId;
var mediaId = null;
if (matches[i].hasAttribute("data-wpid")) {
mediaId = matches[i].getAttribute("data-wpid");
} else if (matches[i].hasAttribute("data-video_wpid")) {
mediaId = matches[i].getAttribute("data-video_wpid");
}

// Track pre-existing failed media nodes for manual deletion events
ZSSEditor.trackNodeForMutation(this.getMediaContainerNodeWithIdentifier(mediaId));

if (mediaId.length > 0) {
if (mediaId !== null) {
mediaIdArray.push(mediaId);
}
}
return mediaIdArray;
};

/**
* @brief Sends a callback with a list of failed images
*/
ZSSEditor.getFailedMedia = function() {
var mediaIdArray = ZSSEditor.getFailedMediaIdArray();
for (var i = 0; i < mediaIdArray.length; i++) {
// Track pre-existing failed media nodes for manual deletion events
ZSSEditor.trackNodeForMutation(this.getMediaContainerNodeWithIdentifier(mediaIdArray[i]));
}

var functionArgument = "function=getFailedMedia";
var joinedArguments = functionArgument + defaultCallbackSeparator + "ids=" + mediaIdArray.toString();
ZSSEditor.callback('callback-response-string', joinedArguments);
};
Expand Down Expand Up @@ -1299,6 +1308,14 @@ ZSSEditor.removeImage = function(imageNodeIdentifier) {
}
};

ZSSEditor.removeAllFailedMediaUploads = function() {
console.log("Remove all failed media");
var failedMediaArray = ZSSEditor.getFailedMediaIdArray();
for (var i = 0; i < failedMediaArray.length; i++) {
ZSSEditor.removeImage(failedMediaArray[i]);
}
}

/**
* @brief Inserts a video tag using the videoURL as source and posterURL as the
* image to show while video is loading.
Expand Down Expand Up @@ -2826,6 +2843,9 @@ ZSSEditor.parentTags = function() {

ZSSEditor.getParentRangeOfFocusedNode = function() {
var selection = window.getSelection();
if (selection.focusNode == null) {
return null;
}
return selection.getRangeAt(selection.focusNode.parentNode);
};

Expand Down

0 comments on commit c6efe0a

Please sign in to comment.