diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index 62c4cbcab00c..08ab3c6d64e1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -559,6 +559,9 @@ public void run() { } private void purgeMediaToPostAssociationsIfNotInPostAnymore() { + boolean useAztec = AppPrefs.isAztecEditorEnabled(); + boolean useGutenberg = AppPrefs.isGutenbergEditorEnabled(); + ArrayList allMedia = new ArrayList<>(); allMedia.addAll(mUploadStore.getFailedMediaForPost(mPost)); allMedia.addAll(mUploadStore.getCompletedMediaForPost(mPost)); @@ -567,8 +570,16 @@ private void purgeMediaToPostAssociationsIfNotInPostAnymore() { if (!allMedia.isEmpty()) { HashSet mediaToDeleteAssociationFor = new HashSet<>(); for (MediaModel media : allMedia) { - if (!AztecEditorFragment.isMediaInPostBody(this, mPost.getContent(), String.valueOf(media.getId()))) { - mediaToDeleteAssociationFor.add(media); + if (useAztec) { + if (!AztecEditorFragment.isMediaInPostBody(this, + mPost.getContent(), String.valueOf(media.getId()))) { + mediaToDeleteAssociationFor.add(media); + } + } else if (useGutenberg) { + if (!PostUtils.isMediaInGutenbergPostBody( + mPost.getContent(), String.valueOf(media.getId()))) { + mediaToDeleteAssociationFor.add(media); + } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java index 045530bbc208..71b517bb1ec8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostUtils.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.posts; import android.content.Context; +import android.support.annotation.NonNull; import android.support.v4.app.FragmentManager; import android.text.TextUtils; import android.text.format.DateUtils; @@ -9,6 +10,7 @@ import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.analytics.AnalyticsTracker; +import org.wordpress.android.editor.Utils; import org.wordpress.android.fluxc.model.MediaModel; import org.wordpress.android.fluxc.model.PostModel; import org.wordpress.android.fluxc.model.SiteModel; @@ -21,6 +23,7 @@ import org.wordpress.android.util.HtmlUtils; import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.analytics.AnalyticsUtils; +import org.wordpress.android.util.helpers.MediaFile; import java.text.BreakIterator; import java.text.SimpleDateFormat; @@ -40,6 +43,9 @@ public class PostUtils { private static final HashSet SHORTCODE_TABLE = new HashSet<>(); private static final String GUTENBERG_BLOCK_START = ""; + private static final String GB_IMG_BLOCK_CLASS_PLACEHOLDER = "class=\"wp-image-%s\""; /* * collapses shortcodes in the passed post content, stripping anything between the @@ -378,6 +384,55 @@ public static boolean shouldShowGutenbergEditor(boolean isNewPost, PostModel pos || TextUtils.isEmpty(post.getContent())); } + public static String replaceMediaFileWithUrlInGutenbergPost(@NonNull String postContent, + String localMediaId, MediaFile mediaFile) { + if (mediaFile != null && contentContainsGutenbergBlocks(postContent)) { + String remoteUrl = org.wordpress.android.util.StringUtils + .notNullStr(Utils.escapeQuotes(mediaFile.getFileURL())); + // TODO: replace the URL + if (!mediaFile.isVideo()) { + // replace gutenberg block id holder with serverMediaId, and url_holder with remoteUrl + String oldImgBlockHeader = String.format(GB_IMG_BLOCK_HEADER_PLACEHOLDER, localMediaId); + String newImgBlockHeader = String.format(GB_IMG_BLOCK_HEADER_PLACEHOLDER, mediaFile.getMediaId()); + postContent = postContent.replace(oldImgBlockHeader, newImgBlockHeader); + + // replace class wp-image-id with serverMediaId, and url_holder with remoteUrl + String oldImgClass = String.format(GB_IMG_BLOCK_CLASS_PLACEHOLDER, localMediaId); + String newImgClass = String.format(GB_IMG_BLOCK_CLASS_PLACEHOLDER, mediaFile.getMediaId()); + postContent = postContent.replace(oldImgClass, newImgClass); + + // let's first find this occurrence and keep note of the position, as we need to replace the + // immediate `src` value before + int iStartOfWpImageClassAttribute = postContent.indexOf(newImgClass); + if (iStartOfWpImageClassAttribute != -1) { + // now search negatively, for the src attribute appearing right before + int iStartOfImgTag = postContent.lastIndexOf("]*src=[\\\"']([^\\\"^']*)"); + Matcher m = p.matcher(postContent.substring(iStartOfImgTag)); + if (m.find()) { + String src = m.group(); + int startIndex = src.indexOf("src=") + SRC_ATTRIBUTE_LENGTH_PLUS_ONE; + String srcTag = src.substring(startIndex, src.length()); + // now replace the url + postContent = postContent.replace(srcTag, remoteUrl); + } + } + } + } else { + // TODO replace in GB Video block? + } + } + return postContent; + } + + public static boolean isMediaInGutenbergPostBody(@NonNull String postContent, + String localMediaId) { + // check if media is in Gutenberg Post + String imgBlockHeaderToSearchFor = String.format("", localMediaId); + return postContent.indexOf(imgBlockHeaderToSearchFor) != -1; + } + public static boolean isPostInConflictWithRemote(PostModel post) { // at this point we know there's a potential version conflict (the post has been modified // both locally and on the remote) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadReadyProcessor.java b/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadReadyProcessor.java index 1b9f6bc976c4..b18bbcd1d83f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadReadyProcessor.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/uploads/MediaUploadReadyProcessor.java @@ -7,6 +7,7 @@ import org.wordpress.android.editor.EditorFragment; import org.wordpress.android.fluxc.model.PostModel; import org.wordpress.android.ui.media.services.MediaUploadReadyListener; +import org.wordpress.android.ui.posts.PostUtils; import org.wordpress.android.ui.prefs.AppPrefs; import org.wordpress.android.util.helpers.MediaFile; @@ -17,8 +18,12 @@ public PostModel replaceMediaFileWithUrlInPost(@Nullable PostModel post, String if (post != null) { boolean showAztecEditor = AppPrefs.isAztecEditorEnabled(); boolean showNewEditor = AppPrefs.isVisualEditorEnabled(); + boolean showGutenbergEditor = AppPrefs.isGutenbergEditorEnabled(); - if (showAztecEditor) { + if (showGutenbergEditor) { + post.setContent( + PostUtils.replaceMediaFileWithUrlInGutenbergPost(post.getContent(), localMediaId, mediaFile)); + } else if (showAztecEditor) { post.setContent(AztecEditorFragment.replaceMediaFileWithUrl(WordPress.getContext(), post.getContent(), localMediaId, mediaFile)); } else if (showNewEditor) { @@ -36,8 +41,11 @@ public PostModel markMediaUploadFailedInPost(@Nullable PostModel post, String lo if (post != null) { boolean showAztecEditor = AppPrefs.isAztecEditorEnabled(); boolean showNewEditor = AppPrefs.isVisualEditorEnabled(); + boolean showGutenbergEditor = AppPrefs.isGutenbergEditorEnabled(); - if (showAztecEditor) { + if (showGutenbergEditor) { + // TODO check if anything needs be done in Gutenberg + } else if (showAztecEditor) { post.setContent(AztecEditorFragment.markMediaFailed(WordPress.getContext(), post.getContent(), localMediaId, mediaFile)); } else if (showNewEditor) { diff --git a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java index 106a2c754697..4bdf34aca937 100644 --- a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java +++ b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java @@ -45,7 +45,6 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements EditorMediaUploadListener, IHistoryListener { private static final String KEY_HTML_MODE_ENABLED = "KEY_HTML_MODE_ENABLED"; - private static final String GUTENBERG_BLOCK_START = "