-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: API allow post actions to optionally provide visual feedback
post action feedback is the mechanism in which we provide visual feedback to the user when a post action is clicked, in cases where the action is a background (hidden to user) for example: copying text to the clipboard Core uses this to share post links, but other plugins (for example: AI) use this to share post transcripts via the clipboard. This adds a proper plugin API to consume this functionality `addPostMenuButton` can provide a builder that specified a function as the action. This function will be called with an object that has both the current post and a method for showing feedback.
- Loading branch information
1 parent
f5380bb
commit c6cb319
Showing
8 changed files
with
216 additions
and
97 deletions.
There are no files selected for viewing
64 changes: 0 additions & 64 deletions
64
app/assets/javascripts/discourse/app/lib/copy-post-link.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/assets/javascripts/discourse/app/lib/post-action-feedback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { SVG_NAMESPACE } from "discourse-common/lib/icon-library"; | ||
import I18n from "discourse-i18n"; | ||
|
||
export default function postActionFeedback({ | ||
postId, | ||
actionClass, | ||
messageKey, | ||
actionCallback, | ||
errorCallback, | ||
}) { | ||
if (recentlyCopied(postId, actionClass)) { | ||
return; | ||
} | ||
|
||
const maybePromise = actionCallback(); | ||
|
||
if (maybePromise && maybePromise.then) { | ||
maybePromise | ||
.then(() => { | ||
showAlert(postId, actionClass, messageKey); | ||
}) | ||
.catch(() => { | ||
if (errorCallback) { | ||
errorCallback(); | ||
} | ||
}); | ||
} else { | ||
showAlert(postId, actionClass, messageKey); | ||
} | ||
} | ||
|
||
export function recentlyCopied(postId, actionClass) { | ||
return document.querySelector( | ||
`article[data-post-id='${postId}'] .${actionClass} .${actionClass}-checkmark` | ||
); | ||
} | ||
|
||
export function showAlert(postId, actionClass, messageKey) { | ||
const postSelector = `article[data-post-id='${postId}']`; | ||
const actionBtn = document.querySelector(`${postSelector} .${actionClass}`); | ||
|
||
actionBtn?.classList.add("post-action-feedback-button"); | ||
|
||
createAlert(I18n.t(messageKey), postId, actionBtn); | ||
createCheckmark(actionBtn, actionClass, postId); | ||
styleBtn(actionBtn); | ||
} | ||
|
||
function createAlert(message, postId, actionBtn) { | ||
if (!actionBtn) { | ||
return; | ||
} | ||
|
||
let alertDiv = document.createElement("div"); | ||
alertDiv.className = "post-action-feedback-alert -success"; | ||
alertDiv.textContent = message; | ||
|
||
actionBtn.appendChild(alertDiv); | ||
|
||
setTimeout(() => alertDiv.classList.add("slide-out"), 1000); | ||
setTimeout(() => removeElement(alertDiv), 2500); | ||
} | ||
|
||
function createCheckmark(btn, actionClass, postId) { | ||
const svgId = `svg_${actionClass}_${postId}`; | ||
const checkmark = makeCheckmarkSvg(postId, actionClass, svgId); | ||
btn.appendChild(checkmark.content); | ||
|
||
setTimeout(() => checkmark.classList.remove("is-visible"), 3000); | ||
setTimeout(() => removeElement(document.getElementById(svgId)), 3500); | ||
} | ||
|
||
function styleBtn(btn) { | ||
btn.classList.add("is-copied"); | ||
setTimeout(() => btn.classList.remove("is-copied"), 3200); | ||
} | ||
|
||
function makeCheckmarkSvg(postId, actionClass, svgId) { | ||
const svgElement = document.createElement("template"); | ||
svgElement.innerHTML = ` | ||
<svg class="${actionClass}-checkmark post-action-feedback-svg is-visible" id="${svgId}" xmlns="${SVG_NAMESPACE}" viewBox="0 0 52 52"> | ||
<path class="checkmark__check" fill="none" d="M13 26 l10 10 20 -20"/> | ||
</svg> | ||
`; | ||
return svgElement; | ||
} | ||
|
||
function removeElement(element) { | ||
element?.parentNode?.removeChild(element); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.