diff --git a/src/base.ts b/src/base.ts new file mode 100644 index 0000000..c61d4fa --- /dev/null +++ b/src/base.ts @@ -0,0 +1,25 @@ +import {clipboard, HeadlessCommand, toast} from "@kksh/api/headless"; + +export class BaseExt extends HeadlessCommand { + _func: Function; + + constructor(func: (text: string) => string) { + super(); + + this._func = func; + } + + async load() { + if (!await clipboard.hasText()) { + await toast.error("Clipboard is empty"); + return; + } + + const clipboardText = await clipboard.readText(); + const convertedText = this._func(clipboardText); + + await clipboard.writeText(convertedText); + await toast.success(`Copied "${convertedText}"`); + return; + } +} diff --git a/src/camel-case.ts b/src/camel-case.ts index c0286e7..7cb39e7 100644 --- a/src/camel-case.ts +++ b/src/camel-case.ts @@ -1,16 +1,10 @@ -import { clipboard, expose, HeadlessCommand, toast } from "@kksh/api/headless" -import camelCase from "lodash/camelcase" +import {expose} from "@kksh/api/headless"; +import camelCase from "lodash/camelcase"; +import {BaseExt} from "./base"; -class CamelCaseExt extends HeadlessCommand { - async load() { - if (!await clipboard.hasText()) return; - - const clipboardText = await clipboard.readText(); - const convertedText = camelCase(clipboardText); - - await clipboard.writeText(convertedText); - await toast.success(`Copied: "${convertedText}"`); - return +class CamelCaseExt extends BaseExt { + constructor() { + super(camelCase); } }