Skip to content

Commit

Permalink
DEV: Modernizations
Browse files Browse the repository at this point in the history
  • Loading branch information
keegangeorge committed Jan 10, 2025
1 parent d83a2f5 commit e856422
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 113 deletions.
Binary file modified .github/images/usage.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Discourse Copy Post
# 📄 Copy post

This theme component adds a 📄 Copy Button to the post menu control section of every post. Clicking the button will copy the entire contents of the post to the user's clipboard.
This theme component adds a copy button to the post menu control section of every post. Clicking the button will copy the entire contents of the post to the user's clipboard.

Learn more about this component [here](https://meta.discourse.org/t/copy-post-component/218883).

![Example Usage](.github/images/usage.gif)
3 changes: 2 additions & 1 deletion about.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"theme_version": "1.0",
"modifiers": {
"svg_icons": [
"copy"
"far-copy",
"check"
]
}
}
3 changes: 3 additions & 0 deletions common/common.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.post-action-menu__copy-post .d-icon-check {
color: var(--success);
}
29 changes: 13 additions & 16 deletions javascripts/discourse/api-initializers/discourse-copy-post.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { apiInitializer } from "discourse/lib/api";
import CopyPostButton from "../components/copy-post-button";

export default apiInitializer("0.11.1", (api) => {
const user = api.getCurrentUser();
const trustLevelAllUsers = 5;
const minTrustLevel = settings.min_trust_level;
export default apiInitializer("2.0.0", (api) => {
const currentUser = api.getCurrentUser();
const currentUserGroupIds = currentUser.groups.map((group) => group.id);
const allowedGroups = settings.copy_button_allowed_groups;
const allowedGroupIds = allowedGroups.split("|").map(Number);
const userNotAllowed = !allowedGroupIds.some((groupId) =>
currentUserGroupIds.includes(groupId)
);

if (minTrustLevel !== trustLevelAllUsers) {
if (!user) {
return;
}

if (user.trust_level < minTrustLevel) {
return;
}
if (userNotAllowed) {
return;
}

api.decorateWidget("post-menu:before-extra-controls", (helper) => {
const results = [];
results.push(helper.widget.attach("copy-widget", helper));
return results;
api.registerValueTransformer("post-menu-buttons", ({ value: dag }) => {
dag.add("copy-post", CopyPostButton);
});
});
57 changes: 57 additions & 0 deletions javascripts/discourse/components/copy-post-button.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Component from '@glimmer/component';
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import DButton from "discourse/components/d-button";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { clipboardCopy } from "discourse/lib/utilities";
import discourseLater from "discourse-common/lib/later";

export default class CopyPostButton extends Component {
static hidden() {
return false;
}

@tracked icon = "far-copy";

@action
async copyPost() {
const cookedPost = this.args.post.cooked;
const copyType = settings.copy_type;
const postContents = copyType === "html" ? cookedPost : await this.fetchRawPost(this.args.post.id);

if (!postContents) {
return;
}

try {
this.icon = "check";
await clipboardCopy(postContents);
} catch (error) {
popupAjaxError(error);
} finally {
discourseLater(() => {
this.icon = "far-copy";
}, 2000);
}
}

async fetchRawPost(postId) {
try {
const { raw } = await ajax(`/posts/${postId}.json`);
return raw;
} catch (error) {
popupAjaxError(error);
}
}

<template>
<DButton
class="post-action-menu__copy-post btn-flat"
@title={{themePrefix "title"}}
@icon={{this.icon}}
@action={{this.copyPost}}
...attributes
/>
</template>
}
59 changes: 0 additions & 59 deletions javascripts/discourse/widgets/copy-widget.js

This file was deleted.

1 change: 0 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
en:
theme_metadata:
description: "A button in the post menu to copy a post's contents to the clipboard"
copied: "Copied"
title: "Copy the contents of this post"
21 changes: 12 additions & 9 deletions settings.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
copy_raw_html:
type: bool
default: false
description: "If selected the copied contents will be raw HTML instead of the stripped down plain text"
min_trust_level:
type: integer
default: 5
max: 5
description: "Select the minimum trust level required to see the widget. (Select 5 to show to all users)"
copy_type:
type: enum
default: markdown
choices:
- markdown
- html
description: "Select the type of copied text you would like saved to the clipboard."
copy_button_allowed_groups:
type: list
list_type: group
default: "11" # trust_level_1
description: "Select the groups that are allowed to use the copy button."
25 changes: 0 additions & 25 deletions test/acceptance/copy-widget-test.js

This file was deleted.

0 comments on commit e856422

Please sign in to comment.