Search in sources :

Example 6 with WPImageSpan

use of org.wordpress.android.util.helpers.WPImageSpan in project WordPress-Android by wordpress-mobile.

the class EditPostActivity method updatePostContent.

// TODO: Replace with contents of the updatePostContentNewEditor() method when legacy editor is dropped
/**
     * Updates post object with content of this fragment
     */
public void updatePostContent(boolean isAutoSave) throws IllegalEditorStateException {
    if (mPost == null) {
        return;
    }
    String title = StringUtils.notNullStr((String) mEditorFragment.getTitle());
    SpannableStringBuilder postContent;
    if (mEditorFragment.getSpannedContent() != null) {
        // needed by the legacy editor to save local drafts
        try {
            postContent = new SpannableStringBuilder(mEditorFragment.getSpannedContent());
        } catch (IndexOutOfBoundsException e) {
            // A core android bug might cause an out of bounds exception, if so we'll just use the current editable
            // See https://code.google.com/p/android/issues/detail?id=5164
            postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
        }
    } else {
        postContent = new SpannableStringBuilder(StringUtils.notNullStr((String) mEditorFragment.getContent()));
    }
    String content;
    if (mPost.isLocalDraft()) {
        // remove suggestion spans, they cause craziness in WPHtml.toHTML().
        CharacterStyle[] characterStyles = postContent.getSpans(0, postContent.length(), CharacterStyle.class);
        for (CharacterStyle characterStyle : characterStyles) {
            if (characterStyle instanceof SuggestionSpan) {
                postContent.removeSpan(characterStyle);
            }
        }
        content = WPHtml.toHtml(postContent);
        // replace duplicate <p> tags so there's not duplicates, trac #86
        content = content.replace("<p><p>", "<p>");
        content = content.replace("</p></p>", "</p>");
        content = content.replace("<br><br>", "<br>");
        // sometimes the editor creates extra tags
        content = content.replace("</strong><strong>", "").replace("</em><em>", "").replace("</u><u>", "").replace("</strike><strike>", "").replace("</blockquote><blockquote>", "");
    } else {
        if (!isAutoSave) {
            // Add gallery shortcode
            MediaGalleryImageSpan[] gallerySpans = postContent.getSpans(0, postContent.length(), MediaGalleryImageSpan.class);
            for (MediaGalleryImageSpan gallerySpan : gallerySpans) {
                int start = postContent.getSpanStart(gallerySpan);
                postContent.removeSpan(gallerySpan);
                postContent.insert(start, WPHtml.getGalleryShortcode(gallerySpan));
            }
        }
        WPImageSpan[] imageSpans = postContent.getSpans(0, postContent.length(), WPImageSpan.class);
        if (imageSpans.length != 0) {
            for (WPImageSpan wpIS : imageSpans) {
                MediaFile mediaFile = wpIS.getMediaFile();
                if (mediaFile == null) {
                    continue;
                }
                if (mediaFile.getMediaId() != null) {
                    updateMediaFileOnServer(mediaFile);
                } else {
                    mediaFile.setFileName(wpIS.getImageSource().toString());
                    mediaFile.setFilePath(wpIS.getImageSource().toString());
                }
                int tagStart = postContent.getSpanStart(wpIS);
                if (!isAutoSave) {
                    postContent.removeSpan(wpIS);
                    // network image has a mediaId
                    if (mediaFile.getMediaId() != null && mediaFile.getMediaId().length() > 0) {
                        postContent.insert(tagStart, WPHtml.getContent(wpIS));
                    } else {
                        // local image for upload
                        postContent.insert(tagStart, "<img android-uri=\"" + wpIS.getImageSource().toString() + "\" />");
                    }
                }
            }
        }
        content = postContent.toString();
    }
    mPost.setTitle(title);
    mPost.setContent(content);
    if (!mPost.isLocalDraft()) {
        mPost.setIsLocallyChanged(true);
    }
}
Also used : MediaFile(org.wordpress.android.util.helpers.MediaFile) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) SuggestionSpan(android.text.style.SuggestionSpan) MediaGalleryImageSpan(org.wordpress.android.util.helpers.MediaGalleryImageSpan) SpannableStringBuilder(android.text.SpannableStringBuilder) CharacterStyle(android.text.style.CharacterStyle)

Example 7 with WPImageSpan

use of org.wordpress.android.util.helpers.WPImageSpan in project WordPress-Android by wordpress-mobile.

the class LegacyEditorFragment method showImageSettings.

/**
     * Rich Text Editor
     */
public void showImageSettings(final View alertView, final EditText titleText, final EditText caption, final EditText imageWidthText, final CheckBox featuredCheckBox, final CheckBox featuredInPostCheckBox, final int maxWidth, final Spinner alignmentSpinner, final WPImageSpan imageSpan) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(getString(R.string.image_settings));
    builder.setView(alertView);
    builder.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            String title = (titleText.getText() != null) ? titleText.getText().toString() : "";
            MediaFile mediaFile = imageSpan.getMediaFile();
            if (mediaFile == null) {
                return;
            }
            mediaFile.setTitle(title);
            mediaFile.setHorizontalAlignment(alignmentSpinner.getSelectedItemPosition());
            mediaFile.setWidth(getEditTextIntegerClamped(imageWidthText, 10, maxWidth));
            String captionText = (caption.getText() != null) ? caption.getText().toString() : "";
            mediaFile.setCaption(captionText);
            mediaFile.setFeatured(featuredCheckBox.isChecked());
            if (featuredCheckBox.isChecked()) {
                // remove featured flag from all other images
                Spannable contentSpannable = mContentEditText.getText();
                WPImageSpan[] imageSpans = contentSpannable.getSpans(0, contentSpannable.length(), WPImageSpan.class);
                if (imageSpans.length > 1) {
                    for (WPImageSpan postImageSpan : imageSpans) {
                        if (postImageSpan != imageSpan) {
                            MediaFile postMediaFile = postImageSpan.getMediaFile();
                            postMediaFile.setFeatured(false);
                            postMediaFile.setFeaturedInPost(false);
                            // TODO: remove this
                            mEditorFragmentListener.saveMediaFile(postMediaFile);
                        }
                    }
                }
            }
            mediaFile.setFeaturedInPost(featuredInPostCheckBox.isChecked());
            // TODO: remove this
            mEditorFragmentListener.saveMediaFile(mediaFile);
        }
    });
    builder.setNegativeButton(getString(android.R.string.cancel), new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    alertDialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) MediaFile(org.wordpress.android.util.helpers.MediaFile) DialogInterface(android.content.DialogInterface) WPImageSpan(org.wordpress.android.util.helpers.WPImageSpan) Spannable(android.text.Spannable)

Aggregations

WPImageSpan (org.wordpress.android.util.helpers.WPImageSpan)7 MediaFile (org.wordpress.android.util.helpers.MediaFile)5 MediaGalleryImageSpan (org.wordpress.android.util.helpers.MediaGalleryImageSpan)3 WPEditText (org.wordpress.android.util.widgets.WPEditText)3 Bitmap (android.graphics.Bitmap)2 ActionBar (android.support.v7.app.ActionBar)2 Spannable (android.text.Spannable)2 CharacterStyle (android.text.style.CharacterStyle)2 KeyEvent (android.view.KeyEvent)2 View (android.view.View)2 CompoundButton (android.widget.CompoundButton)2 EditText (android.widget.EditText)2 TextView (android.widget.TextView)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 Parcelable (android.os.Parcelable)1 Editable (android.text.Editable)1