I want to move the Facebook and Twitter share buttons to post menu

AI-generated summary

In the Dev category, stevenpslade continues a discussion on customizing share buttons by adding Facebook and Twitter options to the post menu. While initially struggling to reuse existing share logic, the user eventually implemented a “hacky” solution using api.attachWidgetAction to open share URLs in popups stevenpslade. This approach constructs URLs dynamically using the current topic link, username, and title fetched via $.getJSON. A key improvement noted involves adding data properties (such as share-url and post-number) to the button configuration, which automatically populates the HTML attributes without needing extra JavaScript to extract the post number stevenpslade. The popup dimensions are hardcoded based on the specific social platform, mirroring values found in sharing-sources.js.es6. Despite the functional result, stevenpslade expresses a desire for a cleaner method that extends the core sharing logic rather than manually wiring actions, inviting the community to suggest better alternatives.

Continuing the discussion from How can I custom share button?:

I have added new post menu items for sharing to Facebook and Twitter.

They do not have any functionality yet though. I was looking to hopefully piggy back on action of the original share actions for facebook and twitter but I am struggling with doing so.

I was also looking at the api documentation for adding a new share source, but it has not helped.

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/sharing.js.es6#L7

What do I need to do to copy the share action of the current facebook and twitter buttons and place them on the post menu items I have created.

For anyone interested in the super hacky way I completed this:

  • I added two post menu buttons as per the screen shot in my original post (one for twitter and one for facebook).

  • using api.attachWidgetAction I wired the button up to link to the appropriate share url, opening in a popup

        api.attachWidgetAction('post-menu', 'facebook-share', () => {
          const link = window.location.href + '?u=' + currentUser.username;
          const url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(link) + '&t=' + encodeURIComponent(title);
          window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=600,height=' + 315);
        });
    
        api.attachWidgetAction('post-menu', 'twitter-share', () => {
          const link = window.location.href + '?u=' + currentUser.username;
          const url = "http://twitter.com/intent/tweet?url=" + encodeURIComponent(link) + "&text=" + encodeURIComponent(title);
          window.open(url, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=600,height=' + 265);
        });
    

So here I set the link and url. The link is the current topic a user is on (I will need to add some JS to include the specific post number a user tries to share).
UPDATE: No hacky JS needed to get the post number. When you make the post menu button add data as a property like so:

api.addPostMenuButton('twitter-share', (attrs) => {
        return {
          action: 'twitter-share',
          icon: 'twitter',
          className: 'twitter-share',
          title: I18n.t('share.twitter'),
          data: {
            'share-url': attrs.shareUrl,
            'post-number': attrs.post_number
          },
          position: 'first'  // can be `first`, `second`, `last` or `second-last-hidden`
        };
      });

This will result in the follow html for the button tag:

data-share-url="/t/privacy-policy/6?u=stevenpslade" data-post-number="1"

The url is the twitter or facebook share url, which is taken from discourse/app/assets/javascripts/discourse/initializers/sharing-sources.js.es6.
I also set the title by getting the .json of the current topic:

var title;
      $.getJSON(window.location.href + '.json', function (data) { 
        var json = data;
        title = json.title;
      });

The urls open in a pop up, and the size of the pop up is dependant on if it is Facebook or Twitter (again taken from sharing-sources.js.es6).

The reason I share this hacky method is in the hopes that someone has a way, way better idea. Ideally, I would want to just extend the current sharing logic and place it on the new post menu buttons :thinking: