Browse Source

Highlights: toggle long context

imwald
Nuša Pukšič 4 days ago
parent
commit
81b72b1b01
  1. 19
      assets/controllers/utility/show_more_controller.js
  2. 40
      templates/pages/article.html.twig

19
assets/controllers/utility/show_more_controller.js

@ -0,0 +1,19 @@
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static targets = ["preview", "full", "button"];
toggle(event) {
event?.preventDefault?.();
if (this.fullTarget.style.display === "none") {
this.fullTarget.style.display = "";
this.previewTarget.style.display = "none";
this.buttonTarget.textContent = "Show less";
} else {
this.fullTarget.style.display = "none";
this.previewTarget.style.display = "";
this.buttonTarget.textContent = "Show more";
}
}
}

40
templates/pages/article.html.twig

@ -234,21 +234,37 @@
{{ highlight.created_at|date('M j') }} {{ highlight.created_at|date('M j') }}
</small> </small>
</div> </div>
<div class="highlight-content-compact">
{% if highlight.context %} {% if highlight.context %}
{% set htmlContext = highlight.context|markdown_to_html %} {% set htmlContext = highlight.context|markdown_to_html %}
{% if highlight.content in highlight.context %} {% if highlight.content in highlight.context %}
{% set wrapper = '<mark class="highlight-mark-compact">' ~ highlight.content ~ '</mark>' %} {% set wrapper = '<mark class="highlight-mark-compact">' ~ highlight.content ~ '</mark>' %}
{% set rendered = htmlContext|replace({ (highlight.content): wrapper }) %} {% set rendered = htmlContext|replace({ (highlight.content): wrapper }) %}
{{ rendered|raw }} {{ rendered|raw }}
{% else %}
<div class="context-text-compact">{{ htmlContext|raw }}</div>
<mark class="highlight-mark-compact">{{ highlight.content }}</mark>
{% endif %}
{% else %} {% else %}
<div class="context-text-compact">{{ htmlContext|raw }}</div>
<mark class="highlight-mark-compact">{{ highlight.content }}</mark> <mark class="highlight-mark-compact">{{ highlight.content }}</mark>
{% endif %} {% endif %}
</div> {% else %}
{% set charLimit = 450 %}
{% set content = highlight.content %}
{% if content|length > charLimit %}
<div data-utility--show-more-target="preview">
{{ content|slice(0, charLimit) ~ '…' }}
</div>
<div data-utility--show-more-target="full" style="display:none;">
{{ content }}
</div>
<button type="button"
class="btn btn-link btn-sm p-0 mt-1"
data-utility--show-more-target="button"
data-action="utility--show-more#toggle">
Show more
</button>
{% else %}
<mark class="highlight-mark-compact">{{ content }}</mark>
{% endif %}
{% endif %}
</div> </div>
{% endfor %} {% endfor %}
</div> </div>

Loading…
Cancel
Save