ui icon indicating copy to clipboard operation
ui copied to clipboard

please enhance caption to v3 level

Open hanscees opened this issue 2 years ago • 4 comments

Hi,

I have nice pages here, where version three alas has a nasty bug (won't load beyond fourth image when swiping.) https://www.bomengids.nl/2022/species/Tulpenboom__Liriodendron_tulipifera__Tulip_tree__Amerikanischer_Tulpenbaum__Tulipier_de_Virginie.html

Fancybox four has no such bug, but alas only has

caption= "some text"

and not the much more elaborate <lightbox_caption>

lots of html here

</lightbox_caption>

Just to let you know the version three option is much easier to work with. Any workaround would be nice, or better enhancement.

hanscees avatar Aug 02 '22 20:08 hanscees

Hi,

Sorry, but I do not understand your comment about captions. In my opinion, it is very simple to use it, example:

Fancybox.bind('[data-fancybox="gallery"]', {
  caption: function (fancybox, carousel, slide) {
    return (
      `${slide.index + 1} / ${carousel.slides.length} <br />` + slide.caption
    );
  },
});

https://fancyapps.com/playground/GQ

fancyapps avatar Aug 03 '22 07:08 fancyapps

Hi,

Sorry, but I do not understand your comment about captions. In my opinion, it is very simple to use it, example:

Fancybox.bind('[data-fancybox="gallery"]', {
  caption: function (fancybox, carousel, slide) {
    return (
      `${slide.index + 1} / ${carousel.slides.length} <br />` + slide.caption
    );
  },
});

https://fancyapps.com/playground/GQ

The problem I have is that I want a complex html structure as caption. In this version I cannot add this easily to a slide because the only option to fill slide.caption via html is by doing caption="Xxx".

I have for instance in the caption four languages all in different div structures. A page button determines what language you see, and what languages are hidden. And also I have a url in the caption. In version three you could load any html into slide.caption by wrapping it inside a html structure like <figurecaption> lots of html </figurecaption>

hanscees avatar Aug 03 '22 13:08 hanscees

Well, it could be as simple as this -

Fancybox.bind('[data-fancybox="gallery"]', {
  caption: function (fancybox, carousel, slide) {
    const figurecaption = slide.$trigger.querySelector("figurecaption");

    return figurecaption ? figurecaption.innerHTML : slide.caption;
  },
});

https://fancyapps.com/playground/3n9

fancyapps avatar Aug 03 '22 13:08 fancyapps

that is fantastic, works like a dream: https://www.bomengids.nl/2022/2022bugtest/fs4.html

thanx a million!

hanscees avatar Aug 03 '22 15:08 hanscees

Well, it could be as simple as this -

Fancybox.bind('[data-fancybox="gallery"]', {
  caption: function (fancybox, carousel, slide) {
    const figurecaption = slide.$trigger.querySelector("figurecaption");

    return figurecaption ? figurecaption.innerHTML : slide.caption;
  },
});

https://fancyapps.com/playground/3n9

Hi, does this also work in v5?

gyurmey2 avatar Mar 05 '23 07:03 gyurmey2

@gyurmey2 Yes, but it will be like this:

Fancybox.bind('[data-fancybox="gallery"]', {
  caption: function (fancybox, slide) {
    const figurecaption = slide.triggerEl?.querySelector(
      "figurecaption"
    );

    return figurecaption ? figurecaption.innerHTML : slide.caption || "";
  },
});

https://jsfiddle.net/tzfjmy0k/

fancyapps avatar Mar 05 '23 09:03 fancyapps

I've also created a new section in the documentation - https://fancyapps.com/fancybox/captions/

fancyapps avatar Mar 05 '23 10:03 fancyapps

@fancyapps, thanks a lot!

Unfortunately, WordPress generates the code below. Is there any way to do this?

<figure>
  <a href="full-size-image.jpg">
    <img src="thumb-image.jpg">
  </a>
  <figcaption>Example caption</figcaption>
</figure>

gyurmey2 avatar Mar 05 '23 16:03 gyurmey2

Simply replace selector here -

slide.triggerEl?.querySelector("figurecaption")

with the one that works for you, like slide.triggerEl?.parentElement.querySelector("figurecaption").

Basically, slide.triggerEl will be your link and then you use that to find your source for the caption.

fancyapps avatar Mar 05 '23 16:03 fancyapps

Unfortunately it doesn't work. Could the reason be that figcaption is not nested in a?

Fancybox.bind('figure a', {

   caption: function (fancybox, slide) {
   const figurecaption = slide.triggerEl?.querySelector('figcaption');
   return figurecaption ? figurecaption.innerHTML : slide.caption || '';

   },
}

gyurmey2 avatar Mar 05 '23 16:03 gyurmey2

I somehow hoped that you will figure it out that you simply need to add parentElement.

On Sun, 5 Mar 2023 at 18:50, Marius @.***> wrote:

Unfortunately it doesn't work. Could the reason be that figcaption is not nested in a?

Fancybox.bind('figure a', {

caption: function (fancybox, slide) { const figurecaption = slide.triggerEl?.querySelector('figcaption'); return figurecaption ? figurecaption.innerHTML : slide.caption || '';

}, }

— Reply to this email directly, view it on GitHub https://github.com/fancyapps/ui/issues/328#issuecomment-1455143606, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIBWZOJPHX3XCCTJSVGULDW2S737ANCNFSM55MOSJPA . You are receiving this because you were mentioned.Message ID: @.***>

fancyapps avatar Mar 05 '23 16:03 fancyapps

I can't handle it, but thank you for your help.

gyurmey2 avatar Mar 05 '23 17:03 gyurmey2

Here is a working example with your HTML structure:

Fancybox.bind('figure a', {
  groupAll: true,
  caption: function (_fancybox, slide) {
    const figcaption = slide.triggerEl?.parentElement.querySelector(
      "figcaption"
    );

    return figcaption ? figcaption.innerHTML : slide.caption || "";
  },
});

https://jsfiddle.net/vgeb1Lwj/

fancyapps avatar Mar 05 '23 18:03 fancyapps

You are very helpful. Thank you very much!

gyurmey2 avatar Mar 05 '23 21:03 gyurmey2

@fancyapps, sorry to bother you, but is it possible to get the attribute caption from an image? I try with below code snippet but it doesn't work.

caption: function (fancybox, slide) {
   const atrr = slide.triggerEl?.querySelector('img').getAttribute('title');
   return atrr ? atrr.innerHTML : slide.caption || '';
}, 

gyurmey2 avatar Mar 06 '23 07:03 gyurmey2

Title attribute is a string, not HTML element :)

caption: function (_fancybox, slide) {
  const title = slide.triggerEl?.querySelector('img').getAttribute('title');
  return title || slide.caption || '';
},

https://jsfiddle.net/qu6f8roy/

fancyapps avatar Mar 06 '23 07:03 fancyapps

@fancyapps, thank you again!

gyurmey2 avatar Mar 06 '23 08:03 gyurmey2