Problem/Motivation

#3510940: JS code is executed multiple times. seems to have introduced another bug where the code is now wrapped multiple times:

Screenshot of markup wrapped multiple times with the same wrapper div

Steps to reproduce

Proposed resolution

Simply have a check added if there is already a wrapper before adding it

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

norman.lol created an issue. See original summary.

norman.lol’s picture

Status: Active » Needs review
norman.lol’s picture

Status: Needs review » Needs work

Damn, it's still there..

It's not the wrapping..

It's ClipboardJS that creates these duplicates..

norman.lol’s picture

Ahhhh now I get it..

Look:

First we run this: once('highlight_js', 'code', context).forEach(function (element) {

And inside there we run this: $('code').each(function (i, block) {

So, there's always going to be exactly that amount of wrappers and highlighting attempts as there are <code> occurrences on the page.

norman.lol’s picture

Status: Needs work » Needs review
norman.lol’s picture

Title: Prevent code to be wrapped multiple times » Prevent code to be executed multiple times

a.dmitriiev made their first commit to this issue’s fork.

a.dmitriiev’s picture

I have update MR, please check. I have also included the change from this issue #3550427: The highlight js code runs on all code blocks

norman.lol’s picture

Status: Needs review » Needs work
StatusFileSize
new1.03 MB

This looks much nicer 🤩 Much better to remove the duplicate foreach 👍🏻

But..

..now we again run highlightAll multiple times:

once('highlight-js', 'pre code', context).forEach(function (block, i) {
  
  if (typeof hljs !== 'undefined') {
    hljs.highlightAll();
   }

which again results in browser console full of this:

Screenshot of browser console full of error messages saying that elements already got highlighted

norman.lol’s picture

So either we move hljs.highlightAll outside of the loop.

Or we hightlight each block separately via hljs.highlightBlock(block).

Seen on https://stackoverflow.com/a/15177245/2199525:

   $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
a.dmitriiev’s picture

Status: Needs work » Needs review

I checked the highlightBlock and it turned out that it is deprecated and highlightElement should be used instead. I have updated MR, please check

norman.lol’s picture

Status: Needs review » Needs work

highlightElement is working fine but now the copy functionality is missing. Looks like as if element.wrap('<div class="code-container"></div>'); is not executed, checking..

norman.lol’s picture

Ah, it is working, but only for <pre>, not for <code>..

norman.lol’s picture

Status: Needs work » Needs review

Okay, I think I now managed to maintain the existing logic:

  • Wrap all <code> elements for the copy functionality to work
  • Only overflow-x on the <pre><code> elements (inline <code> elements without <pre> parent must not have it)
  • Only syntax highlight on the <pre><code> elements (inline <code> elements without <pre> parent must not have it)
a.dmitriiev’s picture

StatusFileSize
new429.57 KB

@norman.lol, are you sure there is need to wrap code only tags in container and to have the copy button? Because this ends up in situations like this:

Standalone code tag

The code tag is not inline anymore and the copy button is far away from the code itself.

a.dmitriiev’s picture

Status: Needs review » Needs work
norman.lol’s picture

Status: Needs work » Needs review
StatusFileSize
new91.85 KB

Our designers provided a nice design for the inline copy button.

I implemented this now (to all code blocks, not just the inline ones).

I needed to add two new config options success_bg_transparent and success_bg_color for it, and added backwards compatible values in an update hook.

On the way I also added the missing config install and schema.

Screenshot of the copy code tooltip looking like a formalized speech bubble

norman.lol’s picture

It would be really easy now to also add an option inline_enable (default TRUE) to let people completely disable highlighting and copy functionality on inline elements.. We can then simply return early in the JS..

norman.lol’s picture

(I've left the success message icon out for now. Thinking it's maybe a bit too opinionated.)

norman.lol’s picture

(Or just we maybe add a textarea option in the config form allowing people to paste SVG markup? 🤔)

a.dmitriiev’s picture

StatusFileSize
new64.76 KB

Minor issue with the "Copied" message, see the screenshot below:

when copied

a.dmitriiev’s picture

Status: Needs review » Needs work
norman.lol’s picture

Status: Needs work » Needs review

I added a CSS rule that simply hides the .copy-btn when the .copy-message is not empty. Since .copy-message always only gets populated after the button click and then emptied again after 2 seconds, that looks like a solid fix.

a.dmitriiev’s picture

Status: Needs review » Needs work

The inline code looks better now, but I am not sure that for the pre code this tooltip is the best solution, maybe let's leave it for now how it was before, just in top right corner?

norman.lol’s picture

Status: Needs work » Needs review

Latest feedback included: Inside big code blocks the button is inside the code block in the top right corner again.

I also managed to get the font-family inherited from the body tag instead of from the pre element (monospace).

norman.lol’s picture

Green lights from @f0ns over from the other issue:

https://www.drupal.org/project/highlight_js/issues/3550427#comment-16346547

a.dmitriiev’s picture

Status: Needs review » Fixed

I made a small adjustment to the update hook and merged! Thank you all!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.