Skip to content

Commit

Permalink
Squashed 'libs/editor/' changes from 72933eb..abfcce5
Browse files Browse the repository at this point in the history
abfcce5 Merge pull request #351 from wordpress-mobile/issue/347-fix-html-mode-content-loss
3152dfa Fixed threading for populating link dialog with selected text
d02a4d1 Extracted LinkDialogFragment building to a standalone method
a917c7f Adjust threading for HTML mode toggle, fixing a race condition on slower devices
6d51447 0.9 version bump

git-subtree-dir: libs/editor
git-subtree-split: abfcce591812a169b7dfa67c00e66a7220f4deac
  • Loading branch information
maxme committed Apr 15, 2016
1 parent c0f0848 commit be4aac0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 47 deletions.
4 changes: 2 additions & 2 deletions WordPressEditor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ android {
buildToolsVersion "23.0.3"

defaultConfig {
versionCode 8
versionName "0.8"
versionCode 9
versionName "0.9"
minSdkVersion 14
targetSdkVersion 23
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,39 @@ private void toggleHtmlMode(final ToggleButton toggleButton) {
updateFormatBarEnabledState(true);

if (toggleButton.isChecked()) {
mSourceViewTitle.setText(getTitle());
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// Update mTitle and mContentHtml with the latest state from the ZSSEditor
getTitle();
getContent();

getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Set HTML mode state
mSourceViewTitle.setText(mTitle);

SpannableString spannableContent = new SpannableString(getContent());
HtmlStyleUtils.styleHtmlForDisplay(spannableContent);
mSourceViewContent.setText(spannableContent);
SpannableString spannableContent = new SpannableString(mContentHtml);
HtmlStyleUtils.styleHtmlForDisplay(spannableContent);
mSourceViewContent.setText(spannableContent);

mWebView.setVisibility(View.GONE);
mSourceView.setVisibility(View.VISIBLE);
mWebView.setVisibility(View.GONE);
mSourceView.setVisibility(View.VISIBLE);

mSourceViewContent.requestFocus();
mSourceViewContent.setSelection(0);
mSourceViewContent.requestFocus();
mSourceViewContent.setSelection(0);

InputMethodManager imm = ((InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE));
imm.showSoftInput(mSourceViewContent, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});

thread.start();

InputMethodManager imm = ((InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE));
imm.showSoftInput(mSourceViewContent, InputMethodManager.SHOW_IMPLICIT);
} else {
mWebView.setVisibility(View.VISIBLE);
mSourceView.setVisibility(View.GONE);
Expand All @@ -479,6 +497,60 @@ private void toggleHtmlMode(final ToggleButton toggleButton) {
}
}

private void displayLinkDialog() {
final LinkDialogFragment linkDialogFragment = new LinkDialogFragment();
linkDialogFragment.setTargetFragment(this, LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD);

final Bundle dialogBundle = new Bundle();

// Pass potential URL from user clipboard
String clipboardUri = Utils.getUrlFromClipboard(getActivity());
if (clipboardUri != null) {
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_URL, clipboardUri);
}

// Pass selected text to dialog
if (mSourceView.getVisibility() == View.VISIBLE) {
// HTML mode
mSelectionStart = mSourceViewContent.getSelectionStart();
mSelectionEnd = mSourceViewContent.getSelectionEnd();

String selectedText = mSourceViewContent.getText().toString().substring(mSelectionStart, mSelectionEnd);
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, selectedText);

linkDialogFragment.setArguments(dialogBundle);
linkDialogFragment.show(getFragmentManager(), LinkDialogFragment.class.getSimpleName());
} else {
// Visual mode
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
mGetSelectedTextCountDownLatch = new CountDownLatch(1);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.execJavaScriptFromString(
"ZSSEditor.execFunctionForResult('getSelectedTextToLinkify');");
}
});

try {
if (mGetSelectedTextCountDownLatch.await(1, TimeUnit.SECONDS)) {
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, mJavaScriptResult);
}
} catch (InterruptedException e) {
AppLog.d(T.EDITOR, "Failed to obtain selected text from JS editor.");
}

linkDialogFragment.setArguments(dialogBundle);
linkDialogFragment.show(getFragmentManager(), LinkDialogFragment.class.getSimpleName());
}
});

thread.start();
}
}

@Override
public void onClick(View v) {
int id = v.getId();
Expand Down Expand Up @@ -507,40 +579,7 @@ public void onClick(View v) {

((ToggleButton) v).setChecked(false);

LinkDialogFragment linkDialogFragment = new LinkDialogFragment();
linkDialogFragment.setTargetFragment(this, LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD);

Bundle dialogBundle = new Bundle();

// Pass potential URL from user clipboard
String clipboardUri = Utils.getUrlFromClipboard(getActivity());
if (clipboardUri != null) {
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_URL, clipboardUri);
}

// Pass selected text to dialog
if (mSourceView.getVisibility() == View.VISIBLE) {
// HTML mode
mSelectionStart = mSourceViewContent.getSelectionStart();
mSelectionEnd = mSourceViewContent.getSelectionEnd();

String selectedText = mSourceViewContent.getText().toString().substring(mSelectionStart, mSelectionEnd);
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, selectedText);
} else {
// Visual mode
mGetSelectedTextCountDownLatch = new CountDownLatch(1);
mWebView.execJavaScriptFromString("ZSSEditor.execFunctionForResult('getSelectedTextToLinkify');");
try {
if (mGetSelectedTextCountDownLatch.await(1, TimeUnit.SECONDS)) {
dialogBundle.putString(LinkDialogFragment.LINK_DIALOG_ARG_TEXT, mJavaScriptResult);
}
} catch (InterruptedException e) {
AppLog.d(T.EDITOR, "Failed to obtain selected text from JS editor.");
}
}

linkDialogFragment.setArguments(dialogBundle);
linkDialogFragment.show(getFragmentManager(), LinkDialogFragment.class.getSimpleName());
displayLinkDialog();
} else {
if (v instanceof ToggleButton) {
onFormattingButtonClicked((ToggleButton) v);
Expand Down

0 comments on commit be4aac0

Please sign in to comment.