Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No image size resolving in visitor #33

Closed
Kaned1as opened this issue Apr 4, 2018 · 12 comments
Closed

No image size resolving in visitor #33

Kaned1as opened this issue Apr 4, 2018 · 12 comments
Milestone

Comments

@Kaned1as
Copy link

Kaned1as commented Apr 4, 2018

Hi. Thanks for wonderful Markdown view, was a life-saver to me.

Here: SpannableMarkdownVisitor#visit(org.commonmark.node.Image)

The AsyncDrawable is created without imageSizeResolver and it's rendered veeeery big

I understand that there are no size constraints in native Markdown syntax but at least some level of customization should be possible for images to fit/scale into the textview.

@noties
Copy link
Owner

noties commented Apr 5, 2018

Hey @Adonai !

Yeah, I think that the library should respect TextView canvas size to not exceed it (keeping original ratio of cause). Meanwhile, if it's an option, you could use HTML img tags. Markwon takes into account width and height attributes and allows then reference % and em (font size), so as pixels (actually everything else except % and em is considered to be in pixels). For example:

<!-- takes 33% of canvas width keeping the original ratio -->
<img src="my-super-image.png" width="33%">

@Kaned1as
Copy link
Author

Kaned1as commented Apr 5, 2018

@noties, yep, thanks, I recognized the difference in htmlParser/native markdown.

The problem is that I can't control what users write in the app so for now will come up with small postprocessing hack.

Thanks for quick reply!

@noties noties added this to the 1.0.5 milestone Apr 10, 2018
@noties
Copy link
Owner

noties commented Apr 15, 2018

Hey @Adonai !

I have created a new branch for the next release and pushed there discussed changes, here. I have tested it and it works for me. How it's for you? 😉

@Kaned1as
Copy link
Author

@noties , I'll test it this week and report, thanks!

@Kaned1as
Copy link
Author

Kaned1as commented Apr 19, 2018

Ugh, compiling aar without uploading it is such a pain.
You should have told me about jitpack earlier, bro :)

https://jitpack.io/com/github/noties/Markwon/v1.0.5-SNAPSHOT/

@noties
Copy link
Owner

noties commented Apr 19, 2018

Dafuq is this? 😶 It's not created by me.

While we are on this I can recommend using local maven repo: https://noties.github.io/blog/2018/03/24/local-maven-repo no need to upload anything

@Kaned1as
Copy link
Author

This thing is auto-generating build artifacts for any github repo you request it to do.
See this, very handy

@noties
Copy link
Owner

noties commented Apr 19, 2018

Yeah, I don't like it. Maybe I should create a snapshot myself as development for the next release is mainly in one branch.

@Kaned1as
Copy link
Author

Kaned1as commented Apr 20, 2018

Ok, tested this, works as expected. You may close this once it's released.
Thanks!

@Kaned1as
Copy link
Author

Kaned1as commented Apr 20, 2018

The other problem, though, is that bounds in AsyncDrawable are resolved in setResult() but canvas width detection occurs in draw(). When TextView is just inflated its width and height are zero, thus AsyncDrawable may fail to properly resolve dimensions.

In particular, this happens when you have RecyclerView with TextView items and scroll too fast or reload adapter.

@noties
Copy link
Owner

noties commented Apr 20, 2018

I think if you do something like this it should solve the RecyclerView issue:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyHolder> {

    @Override
    public void onBindViewHolder(@NonNull final MyHolder holder, final int position) {
        whenReady(holder.text, new Runnable() {
            @Override
            public void run() {
                Markwon.setMarkdown(holder.text, getMarkdown(position));
            }
        });
    }
    
    private static void whenReady(@NonNull final View view, @NonNull final Runnable action) {
        if (view.getWidth() == 0) {
            view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    if (view.getWidth() > 0) {
                        view.getViewTreeObserver().removeOnPreDrawListener(this);
                        action.run();
                    }
                    return true;
                }
            });
        } else {
            action.run();
        }
    }
}

@Kaned1as
Copy link
Author

Thanks, I solved it the other but fairly similar way.
Anyway, this issue is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants