diff --git a/CHANGELOG.md b/CHANGELOG.md index 478a4600..e70a3462 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,17 @@ # Changelog # $nap; + +#### Added +* `core` - `MovementMethodPlugin.none()`, `MovementMethodPlugin.link()` factory methods +* `core` - `CorePlugin` `hasExplicitMovementMethodPlugin` configuration method to **not** add implicit `LinkMovementMethod` on `afterSetText` + +#### Changed * `html` - `SimpleTagHandler` visits children tags if supplied tag is block one ([#235]) +#### Deprecated +* `core` - `MovementMethodPlugin.create()` use explicit `MovementMethodPlugin.link()` instead + [#235]: https://github.com/noties/Markwon/issues/235 # 4.4.0 diff --git a/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java b/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java index 1c576e7f..d9c2a3dd 100644 --- a/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java +++ b/markwon-core/src/main/java/io/noties/markwon/core/CorePlugin.java @@ -115,9 +115,22 @@ public class CorePlugin extends AbstractMarkwonPlugin { // @since 4.0.0 private final List onTextAddedListeners = new ArrayList<>(0); + // @since $nap; + private boolean hasExplicitMovementMethodPlugin; + protected CorePlugin() { } + /** + * @since $nap; + */ + @SuppressWarnings("UnusedReturnValue") + @NonNull + public CorePlugin hasExplicitMovementMethodPlugin(boolean hasExplicitMovementMethodPlugin) { + this.hasExplicitMovementMethodPlugin = hasExplicitMovementMethodPlugin; + return this; + } + /** * Can be useful to post-process text added. For example for auto-linking capabilities. * @@ -188,7 +201,8 @@ public class CorePlugin extends AbstractMarkwonPlugin { // let's ensure that there is a movement method applied // we do it `afterSetText` so any user-defined movement method won't be // replaced (it should be done in `beforeSetText` or manually on a TextView) - if (textView.getMovementMethod() == null) { + // @since $nap; we additionally check if we should apply _implicit_ movement method + if (!hasExplicitMovementMethodPlugin && textView.getMovementMethod() == null) { textView.setMovementMethod(LinkMovementMethod.getInstance()); } } diff --git a/markwon-core/src/main/java/io/noties/markwon/movement/MovementMethodPlugin.java b/markwon-core/src/main/java/io/noties/markwon/movement/MovementMethodPlugin.java index 0f9a7e10..b16af95f 100644 --- a/markwon-core/src/main/java/io/noties/markwon/movement/MovementMethodPlugin.java +++ b/markwon-core/src/main/java/io/noties/markwon/movement/MovementMethodPlugin.java @@ -6,8 +6,10 @@ import android.text.method.MovementMethod; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.core.CorePlugin; /** * @since 3.0.0 @@ -19,26 +21,62 @@ public class MovementMethodPlugin extends AbstractMarkwonPlugin { * Uses Android system LinkMovementMethod as default * * @see #create(MovementMethod) + * @see #link() + * @deprecated $nap; use {@link #link()} */ @NonNull + @Deprecated public static MovementMethodPlugin create() { return create(LinkMovementMethod.getInstance()); } + /** + * @since $nap; + */ + @NonNull + public static MovementMethodPlugin link() { + return create(LinkMovementMethod.getInstance()); + } + + /** + * Special {@link MovementMethodPlugin} that is not applying a MovementMethod on a TextView + * implicitly + * + * @since $nap; + */ + @NonNull + public static MovementMethodPlugin none() { + return new MovementMethodPlugin(null); + } + @NonNull public static MovementMethodPlugin create(@NonNull MovementMethod movementMethod) { return new MovementMethodPlugin(movementMethod); } + @Nullable private final MovementMethod movementMethod; + /** + * Since $nap; change to be nullable + */ @SuppressWarnings("WeakerAccess") - MovementMethodPlugin(@NonNull MovementMethod movementMethod) { + MovementMethodPlugin(@Nullable MovementMethod movementMethod) { this.movementMethod = movementMethod; } + @Override + public void configure(@NonNull Registry registry) { + registry.require(CorePlugin.class) + .hasExplicitMovementMethodPlugin(true); + } + @Override public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { - textView.setMovementMethod(movementMethod); + // @since $nap; check for equality + final MovementMethod current = textView.getMovementMethod(); + if (current != movementMethod) { + textView.setMovementMethod(movementMethod); + } } } diff --git a/sample/src/main/java/io/noties/markwon/sample/recycler/RecyclerActivity.java b/sample/src/main/java/io/noties/markwon/sample/recycler/RecyclerActivity.java index 50bcd696..c1424e6d 100644 --- a/sample/src/main/java/io/noties/markwon/sample/recycler/RecyclerActivity.java +++ b/sample/src/main/java/io/noties/markwon/sample/recycler/RecyclerActivity.java @@ -39,6 +39,7 @@ import io.noties.markwon.image.destination.ImageDestinationProcessorRelativeToAb import io.noties.markwon.image.file.FileSchemeHandler; import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler; import io.noties.markwon.image.svg.SvgMediaDecoder; +import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.SimpleEntry; import io.noties.markwon.recycler.table.TableEntry; @@ -82,7 +83,6 @@ public class RecyclerActivity extends Activity { @NonNull private static Markwon markwon(@NonNull Context context) { return Markwon.builder(context) - .usePlugin(CorePlugin.create()) .usePlugin(ImagesPlugin.create(plugin -> { plugin .addSchemeHandler(FileSchemeHandler.createWithAssets(context))