Skip to content

Fancybox now used for image handling

Announcements
  • Seeing as fancybox is such a great and efficient library, Iโ€™ve decided to implement it here. Being super lightweight, thereโ€™s absolutely zero impact to the overall speed of the Sudonix platform, Iโ€™ve included it here. In fact, itโ€™s a fork of the same extension I wrote for the Flarum project which is still very much in use ๐Ÿ™‚

    Iโ€™ve streamlined the code and the required functions. Images only are shown, and the function will ignore avatars and emojis etc as displaying these as part of an image carousel is pointless in my view.

    Enjoy.

  • @phenomlab

    Great ๐Ÿ˜‰
    Possible to have the code or a tutorial for implemented it on nodeBB ?

    because itโ€™s very practical when users put large images for example!

  • @DownPW Sure. Here goesโ€ฆ

    1. In /admin/appearance/customise#custom-header add the below code
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0/dist/fancybox.css"/>
    <script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@4.0/dist/fancybox.umd.js"></script>
    
    1. In /admin/appearance/customise#custom-js
    // Fancybox wrapper
    
    if (top.location.pathname !== '/login') {
        $(window).on('action:ajaxify.end', function (data) {
            this.$('img').not('.forum-logo').each(function () {
                // Strip out the images contained inside blockquotes as this looks nasty :)
                $('blockquote img').remove();
                Fancybox.bind(
                    'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]',
                    {
                        groupAll: true,
                    }
                );
            });
        });
    }
    
  • @phenomlab

    • Possible to host the script locally?
    • Possibility to have the images all the same size in the posts

    For example here, I have two images of different sizes, they should appear identically in the posts like on sudonix ?

    638a003a-c839-4890-bfe5-bf79a2b36403-image.png

  • @DownPW possible, yes, 100%. Youโ€™d just need to download the files locally and host them on your own server. Finally, you obviously need to change the target path

    The image size is simple CSS.

  • @phenomlab

    ha yes I have it:

    @media (min-width: 1200px) {
    .topic .posts .content .img-responsive {
        max-width: what you want %;
        width: auto;
        padding: 20px;
    }
    }
    
  • On this site, Iโ€™ve added an animation (essentially, a link underline) that meant I needed to modify the Fancybox function. The new animation I mentioned above has an annoying artefact where it also applies on images where the fancybox class exists.

    This is not surprising, as the fancybox attribute adds a a href to all img tags. Based on this, it is necessary to add a noanimate class so that the link animation is not being applied. However, this isnโ€™t such a simple affair. Due to the Lazy Load feature that NodeBB leverages, the images arenโ€™t in the DOM until that specific chunk of data is loaded as the user scrolls through the post. To allow for this, we need to fire an event that selects each target image extension and then appends the existing classes with noanimate as desired. For this to work, we have to create a duplicate each function that uses the preexisting hook of action: posts.loaded.

    In usual cases of an entire page load, this could be quite greedy and have a significant impact on CPU cycles. Thankfully, the cost is in fact negated by the limited amount of data being pulled in each Ajax request using the post loaded feature.

    Below is the code I developed for that

    if (top.location.pathname !== '/login') {
        $(window).on('action:posts.loaded', function(data) {
            console.log("Loaded");
            $(document).ready(function() {
                $('a').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() {
                    $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate");
                });
            });
        });
    }
    
    if (top.location.pathname !== '/login') {
        $(document).ready(function() {
            $(window).on('action:ajaxify.end', function(data) {
                this.$('a').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() {
                    $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate");
                    data.preventDefault()
                    // Strip out the images contained inside blockquotes as this looks nasty :)
                    $('blockquote img').remove();
                });
                Fancybox.bind(
                    'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]', {
                        groupAll: true,
                    }
                );
            });
        });
    }
    

    The specific CSS that defines the animated underline (which you can see for example by hovering over the usernames in each thread) is shown below - note how we exempt noanimate using the :not() class. Basically, apply this css to all a href except those that carry the appended class noanimate.

    Itโ€™s a bit of a bulldozer to break an egg, but itโ€™s still efficient nonetheless.

    .content p a {
        position:relative;
    }
    
    .content p a:not(.noanimate):after {    
      background: none repeat scroll 0 0 transparent;
      bottom: 0;
      content: "";
      display: block;
      height: 1px;
      left: 0%;
      position: absolute;
      background: var(--link);
      transition: width 0.3s ease 0s, left 0.3s ease 0s;
      width: 0;
    }
    .content p a:hover:after { 
      width: 100%; 
      left: 0; 
    }
    
  • Thanks @phenomlab, are you still using Fancybox@4.0 on your site? I see v5.0 is available.

  • @dave1904 Yes, currently, but will be upgrading soon!

    EDIT - I am using 5 ๐Ÿ™‚

  • Canโ€™t see it working yet but I guess I have to rebuild/restart. Will do it later when less users are online. Already rebuilt and restarted a short time ago after deactivating the light box. ๐Ÿ˜„

    Iโ€™ll keep you updated.

  • @dave1904 No need. Can you provide your Custom Header ?

  • @phenomlab Sure!

    <script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.umd.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0/dist/fancybox/fancybox.css"/>
    

    Edit: I should activate the header to get this working. ๐Ÿคฆ Thanks.

  • @dave1904 Working now?

  • Yes it is. ๐Ÿ™‚

    no more need for the light box plugin if using this solution.

  • @dave1904 Yes, I put this together some time ago because itโ€™s so much simpler.

  • And it seems to be less conflicting!


Related Topics
  • ANNOUNCEMENT: Social Login Changes

    Announcements
    4
    6 Votes
    4 Posts
    470 Views

    @DownPW Always looking for ways to improve the overall experience.

  • IMPORTANT: Theme / Swatch changes

    Announcements
    4
    6 Votes
    4 Posts
    293 Views

    @cagatay these changes arenโ€™t published anywhere presently, so nothing for you to do.

  • OGProxy - a replacement for iFramely

    Announcements
    35
    34 Votes
    35 Posts
    3k Views

    @DownPW sorry for the delay here - real life is getting in the way, but the latest release will be pushed out soon.

  • Issues with Imgur images in Fancybox chat module

    Bugs
    12
    2 Votes
    12 Posts
    717 Views

    @DownPW looks good to me.

  • [SOLVED] Fancybox doesn't work in chat

    Bugs
    3
    4 Votes
    3 Posts
    360 Views

    EDIT: Sorry all, I found a bug that causes Fancybox to be bound twice.

    Please remove the original functions I provided (the original post has been updated for anyone who did not use the original code and is new here) and replace with just this block

    // Chat fancybox - fires when chat module loaded and AJAX calls new chat $(document).ready(function() { $(window).on('action:chat.loaded', function(data) { this.$('img').not('.forum-logo').not(".avatar").not(".emoji").not(".bmac-noanimate").each(function() { var newHref = $(this).attr("src"); $(this).wrap("<a class='fancybox' href='" + newHref + "'/>"); $('a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]').addClass("noanimate"); data.preventDefault(); // Strip out the images contained inside blockquotes as this looks nasty :) $('blockquote img').remove(); }); Fancybox.bind( 'a[href*=".jpg"], a[href*=".jpeg"], a[href*=".png"], a[href*=".gif"], a[href*=".webp"]', { groupAll: true, } ); }); });
  • Clustering for NodeBB enabled

    Announcements
    22
    16 Votes
    22 Posts
    616 Views

    @Madchatthew True. I think this is the reason as to why most Open Source projects are abandoned because they are not sustainable in the long-term.

  • Testing out Webdock.io

    Moved Announcements
    2
    5 Votes
    2 Posts
    548 Views

    Just coming back to this thread for review (as I often do), and it looks like Webdock have increased their available offerings - some are extremely powerful, yet very competitive from the pricing perspective.

    image.png

    10 CPU cores, plus 20Gb RAM? Well worth a look (and the asking price) - thereโ€™s also a fixed IP which is hugely beneficial.

    Clearly, this is well beyond what most people will want to spend - itโ€™s more of an example (but interestingly, Sudonix runs on something not too different from the above).

    However, not all that glitters is gold ๐Ÿ˜• - just have a walk through the benchmark report I found below and youโ€™ll see a huge difference between Heztner and Webdock

    https://www.vpsbenchmarks.com/compare/hetzner_vs_webdock

    That being said, the amount of HTTP requests that Webdock handles in relation to Hetzner is superior - @DownPW you might want to have a look at this - thereโ€™s a free 24 hour trialโ€ฆ ๐Ÿ™‚

    5203639b-2f62-47e6-b87b-37580ce5deae-image.png

  • New Twitter handle

    Announcements
    4
    5 Votes
    4 Posts
    433 Views

    Off course ๐Ÿ˜‰