diff --git a/css/diff.general.css b/css/diff.general.css index dd2e2c2..82c995e 100644 --- a/css/diff.general.css +++ b/css/diff.general.css @@ -46,14 +46,34 @@ } .diff-revision__item { display: table-cell; - padding: 0.3em 1em; + padding-left: 1em; + padding-right: 1em; + white-space: nowrap; +} +.diff-revision__item.diff-revision__item-message { + white-space: normal; +} +.diff-revision__item .more-link { + float: right; + padding-left: 0.2em; } -.diff-revision__item__content.show-less { +.diff-revision__items-group.show-less .diff-revision__item__content { height: 1.538em; - overflow-y: hidden; + overflow: hidden; } -.diff-revision__item__content.show-more { - height: auto; +.diff-revision__items-group.show-less .diff-revision__item-message .diff-revision__item__content { + display: none; +} +.diff-revision__items-group.show-less .diff-revision__item-message .more-link { + float: none; +} +@media screen and (min-width: 480px) { + .diff-revision__items-group.show-less .diff-revision__item-message .diff-revision__item__content { + display: block; + } + .diff-revision__items-group.show-less .diff-revision__item-message .more-link { + float: right; + } } @media screen and (min-width: 920px) { diff --git a/diff.libraries.yml b/diff.libraries.yml index 867d695..21e9c75 100644 --- a/diff.libraries.yml +++ b/diff.libraries.yml @@ -7,6 +7,8 @@ diff.general: js/diff.js: {} dependencies: - core/jquery.once + - core/drupalSettings + - core/drupal.debounce diff.double_column: version: 1.x diff --git a/js/diff.js b/js/diff.js index c4aa950..f9694e3 100644 --- a/js/diff.js +++ b/js/diff.js @@ -1,4 +1,4 @@ -(function ($, Drupal, drupalSettings) { +(function ($, Drupal, debounce, drupalSettings) { 'use strict'; @@ -70,34 +70,71 @@ */ Drupal.behaviors.diffRevisionMessages = { attach: function (context, settings) { - $('.diff-revision__item-message', context).once('diff-message').each(function () { + $('.diff-revision__items', context).once('diff-message').each(function () { var $this = $(this); - var $thisMsg = $this.find('.diff-revision__item__content'); - var height = $thisMsg.height(); - // For line height we are getting px value so we need to convert to int. - var lineHeight = parseInt($thisMsg.css('lineHeight'), 10); - // If height is bigger then line height for revision message then we - // have vertical overflow and we will apply more/less UI logic. - if (height > lineHeight) { - $thisMsg.addClass('show-less'); + // For each message we need to add inner div wrapper so we can calculate + // height of content later properly. + $this.find('.diff-revision__item-message').each(function () { + var $content = $(this).find('.diff-revision__item__content'); + $content.html('
' + $content.html() + '
'); + }); - // Create a more/less link. - var $link = $('' + Drupal.t('more') + ''); - $link.click(function () { - if ($thisMsg.hasClass('show-less')) { - $thisMsg.removeClass('show-less'); - $link.text(Drupal.t('less')); + var attachMoreLess = function ($element) { + $element.find('.diff-revision__items-group').each(function () { + var $group = $(this); + var $thisMsg = $group.find('.diff-revision__item-message .diff-revision__item__content > div'); + if ($thisMsg.length == 0) { + return; } - else { - $thisMsg.addClass('show-less'); - $link.text(Drupal.t('more')); + + // Height of message content. + var height = $thisMsg.height(); + if (!height) { + // If height is 0 or not defined then we don't have nothing to do. + return; + } + + // For line height we are getting px value so we need to convert to int. + var lineHeight = parseInt($thisMsg.css('lineHeight'), 10); + + // If height is bigger then line height for revision message then we + // have vertical overflow and we will apply more/less UI logic. + if (height > lineHeight) { + if ($group.hasClass('show-less')) { + return; + } + $group.addClass('show-less'); + + // Inject more/less link only if we have a message, if not then + // lets not do it - we are still adding show-less modifier to + // enforce one row height. + var $link = $('' + Drupal.t('more') + ''); + $link.click(function () { + if ($group.hasClass('show-less')) { + $group.removeClass('show-less'); + $link.text(Drupal.t('less')); + } + else { + $group.addClass('show-less'); + $link.text(Drupal.t('more')); + } + }); + $link.insertBefore($thisMsg.parent()); + } + else if ($group.hasClass('show-less')) { + $group.removeClass('show-less'); + $group.find('.more-link').remove(); } }); - $link.insertAfter($thisMsg); - } + }; + + attachMoreLess($this); + window.addEventListener('resize', function () { + debounce(attachMoreLess($this), 200); + }); }); } }; -})(jQuery, Drupal, drupalSettings); +})(jQuery, Drupal, Drupal.debounce, drupalSettings); diff --git a/src/DiffLayoutBase.php b/src/DiffLayoutBase.php index 14785d2..039bc30 100644 --- a/src/DiffLayoutBase.php +++ b/src/DiffLayoutBase.php @@ -146,7 +146,7 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface, '#title' => $this->t('Comparing'), '#wrapper_attributes' => ['class' => 'diff-revision'], 'items' => [ - '#prefix' => '
', + '#prefix' => '
', '#suffix' => '
', 'right_revision' => $right_revision, 'left_revision' => $left_revision, @@ -186,8 +186,8 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface, $entity_type_id => $revision->id(), $entity_type_id . '_revision' => $revision->getRevisionId(), ]), - '#prefix' => '
', - '#suffix' => '
', + '#prefix' => '
', + '#suffix' => '
', ]; $revision_link['author'] = [ @@ -196,8 +196,8 @@ abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface, '#url' => Url::fromUri(\Drupal::request()->getUriForPath('/user/' . $user_id)), '#theme' => 'username', '#account' => $revision->getRevisionUser(), - '#prefix' => '
', - '#suffix' => '
', + '#prefix' => '
', + '#suffix' => '
', ]; if ($revision_log) { diff --git a/src/Tests/DiffRevisionTest.php b/src/Tests/DiffRevisionTest.php index 3d6bd86..2068532 100644 --- a/src/Tests/DiffRevisionTest.php +++ b/src/Tests/DiffRevisionTest.php @@ -93,7 +93,7 @@ class DiffRevisionTest extends DiffTestBase { $head = $this->xpath('//thead/tr'); $diff_row = $rows[1]->td; // Assert the revision comment. - $this->assertRaw('diff-revision__item-message">Revision 2 comment'); + $this->assertRaw('diff-revision__item__content">Revision 2 comment'); // Assert changes made to the body, text 1 changed to 2. $this->assertEqual((string) ($diff_row[0]), '1'); $this->assertEqual((string) ($diff_row[1]), '-');