Skip to content

Commit

Permalink
feat: add support for copy_text inline button
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Oct 31, 2024
1 parent d9bab01 commit 4262997
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
"type-fest": "^4.0.0"
},
"peerDependencies": {
"grammy": "^1.19.2"
"grammy": "^1.31.0"
},
"devDependencies": {
"@sindresorhus/tsconfig": "^6.0.0",
"@types/node": "^18.17.12",
"c8": "^10.1.2",
"del-cli": "^6.0.0",
"grammy": "^1.19.2",
"grammy": "^1.31.0",
"typescript": "^5.5.2",
"xo": "^0.59.3"
},
Expand Down
6 changes: 6 additions & 0 deletions source/buttons/basic.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {CopyTextButton} from 'grammy/types';
import type {ActionFunc} from '../action-hive.js';
import type {
ConstOrContextPathFunc,
Expand All @@ -24,6 +25,11 @@ export type ManualButtonOptions<Context> =
& BasicOptions<Context>
& JoinLastRowOption;

export interface CopyTextButtonOptions<Context>
extends SingleButtonOptions<Context> {
readonly copy_text: ConstOrContextPathFunc<Context, Readonly<CopyTextButton>>;
}

export interface UrlButtonOptions<Context>
extends SingleButtonOptions<Context> {
/** Url where this button should be heading */
Expand Down
18 changes: 18 additions & 0 deletions source/menu-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from './action-hive.js';
import type {Body} from './body.js';
import type {
CopyTextButtonOptions,
InteractionOptions,
ManualButtonOptions,
SingleButtonOptions,
Expand Down Expand Up @@ -139,6 +140,23 @@ export class MenuTemplate<Context> {
this.#actions.add(trigger, action, undefined);
}

/** Add a copy_text button to the keyboard
* @example
* menuTemplate.copyText({
* text: 'Copy this',
* copy_text: { text: 'content' },
* });
*/
copyText(options: CopyTextButtonOptions<Context>): void {
const {text, copy_text} = options;
this.manual(async (context, path) => ({
text: typeof text === 'function' ? await text(context, path) : text,
copy_text: typeof copy_text === 'function'
? await copy_text(context, path)
: copy_text,
}), options);
}

/** Add an url button to the keyboard
* @example
* menuTemplate.url({
Expand Down
42 changes: 42 additions & 0 deletions test/menu-template/other-buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,48 @@ import {deepStrictEqual, strictEqual} from 'node:assert';
import {test} from 'node:test';
import {MenuTemplate} from '../../source/menu-template.js';

await test('menu-template other-buttons copy_text', async () => {
const menu = new MenuTemplate('whatever');
menu.copyText({text: 'Button', copy_text: {text: 'content to be copied'}});
const keyboard = await menu.renderKeyboard(undefined, '/');
deepStrictEqual(keyboard, [[{
text: 'Button',
copy_text: {text: 'content to be copied'},
}]]);
});

await test('menu-template other-buttons copy_text functions', async () => {
const menu = new MenuTemplate<string>('whatever');
menu.copyText({
text(context, path) {
strictEqual(context, 'foo');
strictEqual(path, '/');
return 'Button';
},
copy_text(context, path) {
strictEqual(context, 'foo');
strictEqual(path, '/');
return {text: 'content to be copied'};
},
});
const keyboard = await menu.renderKeyboard('foo', '/');
deepStrictEqual(keyboard, [[{
text: 'Button',
copy_text: {text: 'content to be copied'},
}]]);
});

await test('menu-template other-buttons copy_text hidden', async () => {
const menu = new MenuTemplate('whatever');
menu.copyText({
text: 'Button',
copy_text: {text: 'content to be copied'},
hide: () => true,
});
const keyboard = await menu.renderKeyboard(undefined, '/');
deepStrictEqual(keyboard, []);
});

await test('menu-template other-buttons url', async () => {
const menu = new MenuTemplate('whatever');
menu.url({text: 'Button', url: 'https://edjopato.de'});
Expand Down

0 comments on commit 4262997

Please sign in to comment.