private void updateVisualEditorFields() { mWebView.execJavaScriptFromString( "ZSSEditor.getField('zss_field_title').setPlainText('" + Utils.escapeHtml(mTitle) + "');"); mWebView.execJavaScriptFromString( "ZSSEditor.getField('zss_field_content').setHTML('" + Utils.escapeHtml(mContentHtml) + "');"); }
private void onFormattingButtonClicked(ToggleButton toggleButton) { String tag = toggleButton.getTag().toString(); if (mWebView.getVisibility() == View.VISIBLE) { mWebView.execJavaScriptFromString("ZSSEditor.set" + StringUtils.capitalize(tag) + "();"); } else { applyFormattingHtmlMode(toggleButton, tag); } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if ((requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD || requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_UPDATE)) { if (resultCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_DELETE) { mWebView.execJavaScriptFromString("ZSSEditor.unlink();"); return; } if (data == null) { return; } Bundle extras = data.getExtras(); if (extras == null) { return; } String linkUrl = extras.getString("linkURL"); String linkText = extras.getString("linkText"); if (linkText == null || linkText.equals("")) { linkText = linkUrl; } if (mSourceView.getVisibility() == View.VISIBLE) { Editable content = mSourceViewContent.getText(); if (content == null) { return; } if (mSelectionStart < mSelectionEnd) { content.delete(mSelectionStart, mSelectionEnd); } String urlHtml = "<a href=\"" + linkUrl + "\">" + linkText + "</a>"; content.insert(mSelectionStart, urlHtml); mSourceViewContent.setSelection(mSelectionStart + urlHtml.length()); } else { String jsMethod; if (requestCode == LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD) { jsMethod = "ZSSEditor.insertLink"; } else { jsMethod = "ZSSEditor.updateLink"; } mWebView.execJavaScriptFromString( jsMethod + "('" + Utils.escapeHtml(linkUrl) + "', '" + Utils.escapeHtml(linkText) + "');"); } } else if (requestCode == ImageSettingsDialogFragment.IMAGE_SETTINGS_DIALOG_REQUEST_CODE) { if (data == null) { return; } Bundle extras = data.getExtras(); if (extras == null) { return; } final String imageMeta = extras.getString("imageMeta"); final int imageRemoteId = extras.getInt("imageRemoteId"); final boolean isFeaturedImage = extras.getBoolean("isFeatured"); mWebView.post( new Runnable() { @Override public void run() { mWebView.execJavaScriptFromString( "ZSSEditor.updateCurrentImageMeta('" + imageMeta + "');"); } }); if (imageRemoteId != 0) { if (isFeaturedImage) { mFeaturedImageId = imageRemoteId; mEditorFragmentListener.onFeaturedImageChanged(mFeaturedImageId); } else { // If this image was unset as featured, clear the featured image id if (mFeaturedImageId == imageRemoteId) { mFeaturedImageId = 0; mEditorFragmentListener.onFeaturedImageChanged(mFeaturedImageId); } } } } }
@Override public void onClick(View v) { int id = v.getId(); if (id == R.id.format_bar_button_html) { // Don't switch to HTML mode if currently uploading media // if (!mUploadingMediaIds.isEmpty()) { // ((ToggleButton) v).setChecked(false); // // if (isAdded()) { // ToastUtils.showToast(getActivity(), // R.string.alert_html_toggle_uploading, ToastUtils.Duration.LONG); // } // return; // } clearFormatBarButtons(); updateFormatBarEnabledState(true); if (((ToggleButton) v).isChecked()) { mSourceViewTitle.setText(getTitle()); SpannableString spannableContent = new SpannableString(getContent()); HtmlStyleUtils.styleHtmlForDisplay(spannableContent); mSourceViewContent.setText(spannableContent); mWebView.setVisibility(View.GONE); mSourceView.setVisibility(View.VISIBLE); mSourceViewContent.requestFocus(); mSourceViewContent.setSelection(0); InputMethodManager imm = ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)); imm.showSoftInput(mSourceViewContent, InputMethodManager.SHOW_IMPLICIT); } else { mWebView.setVisibility(View.VISIBLE); mSourceView.setVisibility(View.GONE); mTitle = mSourceViewTitle.getText().toString(); mContentHtml = mSourceViewContent.getText().toString(); updateVisualEditorFields(); mWebView.execJavaScriptFromString("ZSSEditor.getField('zss_field_content').focus();"); } } else if (id == R.id.format_bar_button_media) { ((ToggleButton) v).setChecked(false); if (mSourceView.getVisibility() == View.VISIBLE) { ToastUtils.showToast( getActivity(), R.string.alert_insert_image_html_mode, ToastUtils.Duration.LONG); } else { mEditorFragmentListener.onAddMediaClicked(); if (isAdded()) { getActivity().openContextMenu(mTagToggleButtonMap.get(TAG_FORMAT_BAR_BUTTON_MEDIA)); } } } else if (id == R.id.format_bar_button_link) { if (!((ToggleButton) v).isChecked()) { // The link button was checked when it was pressed; remove the current link mWebView.execJavaScriptFromString("ZSSEditor.unlink();"); return; } ((ToggleButton) v).setChecked(false); LinkDialogFragment linkDialogFragment = new LinkDialogFragment(); linkDialogFragment.setTargetFragment(this, LinkDialogFragment.LINK_DIALOG_REQUEST_CODE_ADD); Bundle dialogBundle = new Bundle(); // 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("linkText", selectedText); } else { // Visual mode mGetSelectedTextCountDownLatch = new CountDownLatch(1); mWebView.execJavaScriptFromString("ZSSEditor.execFunctionForResult('getSelectedText');"); try { if (mGetSelectedTextCountDownLatch.await(1, TimeUnit.SECONDS)) { dialogBundle.putString("linkText", mJavaScriptResult); } } catch (InterruptedException e) { AppLog.d(AppLog.T.EDITOR, "Failed to obtain selected text from JS editor."); } } linkDialogFragment.setArguments(dialogBundle); linkDialogFragment.show(getFragmentManager(), "LinkDialogFragment"); } else { if (v instanceof ToggleButton) { onFormattingButtonClicked((ToggleButton) v); } } }