/** * In HTML mode, applies formatting to selected text, or inserts formatting tag at current cursor * position * * @param toggleButton format bar button which was clicked * @param tag identifier tag */ private void applyFormattingHtmlMode(ToggleButton toggleButton, String tag) { if (mSourceViewContent == null) { return; } // Replace style tags with their proper HTML tags String htmlTag; if (tag.equals(getString(R.string.format_bar_tag_bold))) { htmlTag = "b"; } else if (tag.equals(getString(R.string.format_bar_tag_italic))) { htmlTag = "i"; } else if (tag.equals(getString(R.string.format_bar_tag_strikethrough))) { htmlTag = "del"; } else if (tag.equals(getString(R.string.format_bar_tag_unorderedList))) { htmlTag = "ul"; } else if (tag.equals(getString(R.string.format_bar_tag_orderedList))) { htmlTag = "ol"; } else { htmlTag = tag; } int selectionStart = mSourceViewContent.getSelectionStart(); int selectionEnd = mSourceViewContent.getSelectionEnd(); if (selectionStart > selectionEnd) { int temp = selectionEnd; selectionEnd = selectionStart; selectionStart = temp; } boolean textIsSelected = selectionEnd > selectionStart; String startTag = "<" + htmlTag + ">"; String endTag = "</" + htmlTag + ">"; // Add li tags together with ul and ol tags if (htmlTag.equals("ul") || htmlTag.equals("ol")) { startTag = startTag + "\n\t<li>"; endTag = "</li>\n" + endTag; } Editable content = mSourceViewContent.getText(); if (textIsSelected) { // Surround selected text with opening and closing tags content.insert(selectionStart, startTag); content.insert(selectionEnd + startTag.length(), endTag); toggleButton.setChecked(false); mSourceViewContent.setSelection(selectionEnd + startTag.length() + endTag.length()); } else if (toggleButton.isChecked()) { // Insert opening tag content.insert(selectionStart, startTag); mSourceViewContent.setSelection(selectionEnd + startTag.length()); } else { // Insert closing tag content.insert(selectionEnd, endTag); mSourceViewContent.setSelection(selectionEnd + endTag.length()); } }
@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); } } }