A platform for community discussion. Free, open, simple.
UX: Modernize footnote implementation and enable in preview (#33928)
David Taylore508ba7291c4
diff --git a/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs b/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjsnew file mode 100644index 00000000000..18aa97dff7d--- /dev/null+++ b/plugins/footnote/assets/javascripts/api-initializers/inline-footnotes.gjs@@ -0,0 +1,66 @@+import { htmlSafe } from "@ember/template";+import { apiInitializer } from "discourse/lib/api";+import { iconHTML } from "discourse/lib/icon-library";+import DTooltipInstance from "float-kit/lib/d-tooltip-instance";++const TooltipContentComponent = <template>+ {{htmlSafe @data.contentHtml}}+</template>;++export default apiInitializer((api) => {+ function onFootnoteClick(event) {+ event.preventDefault();++ const tooltipService = api.container.lookup("service:tooltip");++ const instance = new DTooltipInstance(api.container, {+ identifier: "inline-footnote",+ interactive: true,+ closeOnScroll: false,+ closeOnClickOutside: true,+ component: TooltipContentComponent,+ data: {+ contentHtml: event.target.dataset.footnoteContent,+ },+ });+ instance.trigger = event.target;+ instance.detachedTrigger = true;++ tooltipService.show(instance);+ }++ api.decorateCookedElement((elem) => {+ if (+ !api.container.lookup("service:site-settings").display_footnotes_inline+ ) {+ return;+ }++ const footnoteRefs = elem.querySelectorAll("sup.footnote-ref");++ footnoteRefs.forEach((footnoteRef) => {+ const refLink = footnoteRef.querySelector("a");+ if (!refLink) {+ return;+ }++ const footnoteId = refLink.getAttribute("href");+ const footnoteContent = elem.querySelector(footnoteId)?.innerHTML;++ const expandableFootnote = document.createElement("a");+ expandableFootnote.classList.add("expand-footnote");+ expandableFootnote.innerHTML = iconHTML("ellipsis");+ expandableFootnote.href = "";+ expandableFootnote.role = "button";+ expandableFootnote.dataset.footnoteId = footnoteId;+ expandableFootnote.dataset.footnoteContent = footnoteContent;+ expandableFootnote.addEventListener("click", onFootnoteClick);++ footnoteRef.after(expandableFootnote);+ });++ if (footnoteRefs.length) {+ elem.classList.add("inline-footnotes");+ }+ });+});diff --git a/plugins/footnote/assets/javascripts/initializers/inline-footnotes.js b/plugins/footnote/assets/javascripts/initializers/inline-footnotes.jsdeleted file mode 100644index 934ad5d5815..00000000000--- a/plugins/footnote/assets/javascripts/initializers/inline-footnotes.js+++ /dev/null@@ -1,139 +0,0 @@-import { createPopper } from "@popperjs/core";-import { iconHTML } from "discourse/lib/icon-library";-import { withPluginApi } from "discourse/lib/plugin-api";--let inlineFootnotePopper;--function applyInlineFootnotes(elem) {- const footnoteRefs = elem.querySelectorAll("sup.footnote-ref");-- footnoteRefs.forEach((footnoteRef) => {- const refLink = footnoteRef.querySelector("a");- if (!refLink) {- return;- }-- const expandableFootnote = document.createElement("a");- expandableFootnote.classList.add("expand-footnote");- expandableFootnote.innerHTML = iconHTML("ellipsis");- expandableFootnote.href = "";- expandableFootnote.role = "button";- expandableFootnote.dataset.footnoteId = refLink.getAttribute("href");-- footnoteRef.after(expandableFootnote);- });-- if (footnoteRefs.length) {- elem.classList.add("inline-footnotes");- }-}--function buildTooltip() {- const template = document.createElement("template");- template.innerHTML = `- <div id="footnote-tooltip" role="tooltip">- <div class="footnote-tooltip-content"></div>- <div id="arrow" data-popper-arrow></div>- </div>- `.trim();-- return template.content.firstChild;-}--function footnoteEventHandler(event) {- const tooltip = document.getElementById("footnote-tooltip");- const displayedFootnoteId = tooltip?.dataset.footnoteId;- const expandableFootnote = event.target;- const footnoteId = expandableFootnote.dataset.footnoteId;-- inlineFootnotePopper?.destroy();- tooltip?.removeAttribute("data-show");- tooltip?.removeAttribute("data-footnote-id");-- if (!event.target.classList.contains("expand-footnote")) {- // dismissing the tooltip by clicking outside- return;- }-- event.preventDefault();- event.stopPropagation();-- if (displayedFootnoteId === footnoteId) {- // dismissing the tooltip by clicking the footnote button- return;- }-- // append footnote to tooltip body- const footnoteContent = tooltip.querySelector(".footnote-tooltip-content");- let cooked = expandableFootnote.closest(".cooked");- if (cooked.dataset.refPostId != null) {- // For full screen tables, redirect- cooked = document.querySelector(- `article[data-post-id="${cooked.dataset.refPostId}"] .cooked`- );- }- const newContent = cooked.querySelector(footnoteId);- footnoteContent.innerHTML = newContent.innerHTML;-- // display tooltip- tooltip.dataset.show = "";- tooltip.dataset.footnoteId = footnoteId;-- // setup popper- inlineFootnotePopper?.destroy();- inlineFootnotePopper = createPopper(expandableFootnote, tooltip, {- modifiers: [- {- name: "arrow",- options: { element: tooltip.querySelector("#arrow") },- },- {- name: "preventOverflow",- options: {- altAxis: true,- padding: 5,- },- },- {- name: "offset",- options: {- offset: [0, 12],- },- },- ],- });-}--export default {- name: "inline-footnotes",-- initialize(container) {- if (!container.lookup("service:site-settings").display_footnotes_inline) {- return;- }-- document.body.append(buildTooltip());- window.addEventListener("click", footnoteEventHandler, true);-- withPluginApi((api) => {- api.decorateCookedElement((elem) => applyInlineFootnotes(elem), {- onlyStream: true,- id: "inline-footnotes",- });-- api.onPageChange(() => {- inlineFootnotePopper?.destroy();-- const tooltip = document.getElementById("footnote-tooltip");- tooltip?.removeAttribute("data-show");- tooltip?.removeAttribute("data-footnote-id");- });- });- },-- teardown() {- inlineFootnotePopper?.destroy();- window.removeEventListener("click", footnoteEventHandler);- document.getElementById("footnote-tooltip")?.remove();- },-};diff --git a/plugins/footnote/assets/stylesheets/footnotes.scss b/plugins/footnote/assets/stylesheets/footnotes.scssindex 3c07028e952..76189c99655 100644--- a/plugins/footnote/assets/stylesheets/footnotes.scss+++ b/plugins/footnote/assets/stylesheets/footnotes.scss@@ -38,78 +38,22 @@}}-#footnote-tooltip {- background-color: var(--primary-low);- color: var(--primary);- padding: 0.5em;- font-size: var(--font-down-1);- border-radius: 3px;- display: none;- z-index: z("modal", "tooltip");- max-width: 400px;+.fk-d-tooltip__content[data-identifier="inline-footnote"] {overflow-wrap: break-word;- box-sizing: border-box;+ font-size: var(--font-down-1);- .mobile-view & {- // tooltips are positioned 5px from the left- // - 10px accounts for this and gives 5px space on the right- max-width: calc(100dvw - 10px);+ .footnote-backref {+ display: none;}- .footnote-tooltip-content {- overflow: hidden;-- .footnote-backref {- display: none;- }-- img {- object-fit: cover;- max-width: 100%;- }-- p {- margin: 0;- }+ img {+ object-fit: cover;+ max-width: 100%;}-}-#footnote-tooltip[data-show] {- display: block;-}--#arrow,-#arrow::before {- position: absolute;- width: 10px;- height: 10px;- background: inherit;-}--#arrow {- visibility: hidden;-}--#arrow::before {- visibility: visible;- content: "";- transform: rotate(45deg);-}--#footnote-tooltip[data-popper-placement^="top"] > #arrow {- bottom: -4px;-}--#footnote-tooltip[data-popper-placement^="bottom"] > #arrow {- top: -4px;-}--#footnote-tooltip[data-popper-placement^="left"] > #arrow {- right: -4px;-}--#footnote-tooltip[data-popper-placement^="right"] > #arrow {- left: -4px;+ p {+ margin: 0;+ }}.ProseMirror {diff --git a/plugins/footnote/test/javascripts/acceptance/footnote-test.js b/plugins/footnote/test/javascripts/acceptance/footnote-test.jsindex 9af93cead27..8867b7083e6 100644--- a/plugins/footnote/test/javascripts/acceptance/footnote-test.js+++ b/plugins/footnote/test/javascripts/acceptance/footnote-test.js@@ -1,9 +1,12 @@-import { click, visit } from "@ember/test-helpers";+import { click, triggerEvent, visit } from "@ember/test-helpers";import { test } from "qunit";import { cloneJSON } from "discourse/lib/object";import topicFixtures from "discourse/tests/fixtures/topic";import { acceptance } from "discourse/tests/helpers/qunit-helpers";+const TOOLTIP_SELECTOR =+ ".fk-d-tooltip__content[data-identifier='inline-footnote']";+acceptance("Discourse Footnote Plugin", function (needs) {needs.settings({display_footnotes_inline: true,@@ -29,44 +32,24 @@ acceptance("Discourse Footnote Plugin", function (needs) {test("displays the footnote on click", async function (assert) {await visit("/t/-/45");- assert.dom("#footnote-tooltip", document.body).exists();-// openawait click(".expand-footnote");- assert- .dom(".footnote-tooltip-content", document.body)- .hasText("consectetur adipiscing elit ↩︎");- assert.dom("#footnote-tooltip", document.body).hasAttribute("data-show");++ assert.dom(TOOLTIP_SELECTOR).hasText("consectetur adipiscing elit ↩︎");// close by clicking outside- await click(document.body);- assert- .dom("#footnote-tooltip", document.body)- .doesNotHaveAttribute("data-show");+ await triggerEvent(".d-header", "pointerdown");+ assert.dom(TOOLTIP_SELECTOR).doesNotExist();// open againawait click(".expand-footnote");- assert- .dom(".footnote-tooltip-content", document.body)- .hasText("consectetur adipiscing elit ↩︎");- assert.dom("#footnote-tooltip", document.body).hasAttribute("data-show");-- // close by clicking the button- await click(".expand-footnote");- assert- .dom("#footnote-tooltip", document.body)- .doesNotHaveAttribute("data-show");+ assert.dom(TOOLTIP_SELECTOR).hasText("consectetur adipiscing elit ↩︎");});test("clicking a second footnote with same name works", async function (assert) {await visit("/t/-/45");- assert.dom("#footnote-tooltip", document.body).exists();-await click(".second .expand-footnote");- assert- .dom(".footnote-tooltip-content", document.body)- .hasText("consectetur adipiscing elit ↩︎");- assert.dom("#footnote-tooltip", document.body).hasAttribute("data-show");+ assert.dom(TOOLTIP_SELECTOR).hasText("consectetur adipiscing elit ↩︎");});});