Files
danbooru/app/components/comment_component/comment_component.js
evazion bed5fdafb8 comments: add dtext shortlink option to comment menu.
Add a Shortlink menu option to the comment menu. Clicking this will copy
a DText comment shortlink (e.g. `comment #12345`) to the clipboard. You
can middle-click or right-click the menu option to get the full URL.

The menu option is called Shortlink instead of `comment #1234` because
show the full comment ID in the menu makes the menu look too unbalanced.

Note that the `navigator.clipboard` API can only be used in a https://
environment. It won't work in non-HTTPS development environments. ngrok
can help with this.
2021-01-22 03:55:48 -06:00

50 lines
1.5 KiB
JavaScript

import Utility from "../../javascript/src/javascripts/utility.js";
class CommentComponent {
static initialize() {
if ($("#c-posts #a-show, #c-comments").length) {
$(document).on("click.danbooru.comment", ".edit_comment_link", CommentComponent.showEditForm);
$(document).on("click.danbooru.comment", ".expand-comment-response", CommentComponent.showNewCommentForm);
$(document).on("click.danbooru.comment", ".unhide-comment-link", CommentComponent.unhideComment);
$(document).on("click.danbooru.comment", ".comment-copy-link", CommentComponent.copyLink);
}
}
static showNewCommentForm(e) {
$(e.target).hide();
var $form = $(e.target).closest("div.new-comment").find("form");
$form.show();
$form[0].scrollIntoView(false);
e.preventDefault();
}
static showEditForm(e) {
$(this).closest(".comment").find(".edit_comment").show();
e.preventDefault();
}
static unhideComment(e) {
let $comment = $(this).closest(".comment");
$comment.find(".unhide-comment-link").hide();
$comment.find(".body").show();
e.preventDefault();
}
static async copyLink(e) {
let $comment = $(this).closest(".comment");
let link = `comment #${$comment.data("id")}`;
e.preventDefault();
try {
await navigator.clipboard.writeText(link);
Utility.notice(`Copied ${link} to clipboard.`);
} catch (error) {
Utility.error("Couldn't copy link to clipboard");
}
}
}
$(document).ready(CommentComponent.initialize);
export default CommentComponent;