From bd6cf0a5b35dacefcbc3bbb69e3e8f6d6029446a Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 18 May 2023 23:09:36 +0800 Subject: [PATCH] chore: deprecate copy file button --- src/components/settings/SettingsBar.tsx | 11 +---- src/platforms/GitHub/DOMHelper.ts | 46 +------------------ .../hooks/useGitHubAttachCopyFileButton.ts | 16 ------- src/platforms/GitHub/index.ts | 4 +- src/utils/config/helper.ts | 3 -- 5 files changed, 3 insertions(+), 77 deletions(-) delete mode 100644 src/platforms/GitHub/hooks/useGitHubAttachCopyFileButton.ts diff --git a/src/components/settings/SettingsBar.tsx b/src/components/settings/SettingsBar.tsx index 50f3356..12f1190 100644 --- a/src/components/settings/SettingsBar.tsx +++ b/src/components/settings/SettingsBar.tsx @@ -20,15 +20,12 @@ export const wikiLinks = { compressSingletonFolder: `${WIKI_HOME_LINK}/Compress-Singleton-Folder`, changeLog: `${WIKI_HOME_LINK}/Change-Log`, codeFolding: `${WIKI_HOME_LINK}/Code-folding`, - copyFileButton: `${WIKI_HOME_LINK}/Copy-file-and-snippet`, copySnippet: `${WIKI_HOME_LINK}/Copy-file-and-snippet`, createAccessToken: `${WIKI_HOME_LINK}/Access-token-for-Gitako`, pjaxMode: `${WIKI_HOME_LINK}/Pjax-Mode`, } -const moreFields: SimpleConfigField< - 'copyFileButton' | 'copySnippetButton' | 'codeFolding' | 'pjaxMode' ->[] = +const moreFields: SimpleConfigField<'copySnippetButton' | 'codeFolding' | 'pjaxMode'>[] = platform === GitHub ? [ { @@ -47,12 +44,6 @@ const moreFields: SimpleConfigField< onChange: checked => (checked ? 'native' : 'pjax-api'), }, }, - { - key: 'copyFileButton', - label: 'Copy file button', - wikiLink: wikiLinks.copyFileButton, - tooltip: `Read more in Gitako's Wiki`, - }, { key: 'copySnippetButton', label: 'Copy snippet button', diff --git a/src/platforms/GitHub/DOMHelper.ts b/src/platforms/GitHub/DOMHelper.ts index 8145b75..9cc48c3 100644 --- a/src/platforms/GitHub/DOMHelper.ts +++ b/src/platforms/GitHub/DOMHelper.ts @@ -3,8 +3,7 @@ import { Clippy, ClippyClassName } from 'components/Clippy' import * as React from 'react' import { $ } from 'utils/$' import { formatClass, parseIntFromElement } from 'utils/DOMHelper' -import { renderReact, run } from 'utils/general' -import { CopyFileButton, copyFileButtonClassName } from './CopyFileButton' +import { renderReact } from 'utils/general' const selectors = { globalNavigation: { @@ -197,49 +196,6 @@ export function getCodeElement() { } } -/** - * add copy file content buttons to button groups - * click these buttons will copy file content to clipboard - */ -export function attachCopyFileBtn() { - const removeButtons = () => { - const buttons = document.querySelectorAll(formatClass(copyFileButtonClassName)) - buttons.forEach(button => { - button.parentElement?.removeChild(button) - }) - } - - if (getCurrentPageType() === PAGE_TYPES.RAW_TEXT) { - let buttonGroup: HTMLElement | null = null - - if (!buttonGroup) { - const rawUrlButtonSelector = '#raw-url' - const $buttonGroup = document.querySelector(rawUrlButtonSelector)?.parentElement - if ($buttonGroup) buttonGroup = $buttonGroup - } - - if (!buttonGroup) { - const buttonGroupSelector = 'main .Box-header .BtnGroup' - const buttonGroups = document.querySelectorAll(buttonGroupSelector) - const $buttonGroup = buttonGroups[buttonGroups.length - 1] - if ($buttonGroup) buttonGroup = $buttonGroup as HTMLElement - } - - run(async () => { - if (!buttonGroup) raiseError(new Error(`No button groups found`)) - else if (!buttonGroup.lastElementChild) return - else { - removeButtons() // prevent duplicated buttons - const button = await renderReact(React.createElement(CopyFileButton)) - if (button instanceof HTMLElement) buttonGroup.appendChild(button) - } - }) - } - - // return callback so that disabling after redirecting from file page to non-page works properly - return removeButtons -} - export function attachCopySnippet() { const readmeSelector = 'main div#readme' return $(readmeSelector, () => { diff --git a/src/platforms/GitHub/hooks/useGitHubAttachCopyFileButton.ts b/src/platforms/GitHub/hooks/useGitHubAttachCopyFileButton.ts deleted file mode 100644 index e2bc04c..0000000 --- a/src/platforms/GitHub/hooks/useGitHubAttachCopyFileButton.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { platform } from 'platforms' -import * as React from 'react' -import { useAfterRedirect } from 'utils/hooks/useFastRedirect' -import * as DOMHelper from '../DOMHelper' -import { GitHub } from '../index' - -export function useGitHubAttachCopyFileButton(copyFileButton: boolean) { - const attachCopyFileButton = React.useCallback( - function attachCopyFileButton() { - if (platform === GitHub && copyFileButton) DOMHelper.attachCopyFileBtn() - }, - [copyFileButton], - ) - React.useEffect(attachCopyFileButton, [attachCopyFileButton]) - useAfterRedirect(attachCopyFileButton) -} diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 834417f..5f1e2c9 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -10,7 +10,6 @@ import * as DOMHelper from './DOMHelper' import { getCommitTreeData } from './getCommitTreeData' import { getPullRequestTreeData } from './getPullRequestTreeData' import { useEnterpriseStatBarStyleFix } from './hooks/useEnterpriseStatBarStyleFix' -import { useGitHubAttachCopyFileButton } from './hooks/useGitHubAttachCopyFileButton' import { useGitHubAttachCopySnippetButton } from './hooks/useGitHubAttachCopySnippetButton' import { useGitHubCodeFold } from './hooks/useGitHubCodeFold' import * as URLHelper from './URLHelper' @@ -196,8 +195,7 @@ export const GitHub: Platform = { return `https://github.com/login/oauth/authorize?${params}` }, usePlatformHooks() { - const { copyFileButton, copySnippetButton, codeFolding } = useConfigs().value - useGitHubAttachCopyFileButton(copyFileButton) + const { copySnippetButton, codeFolding } = useConfigs().value useGitHubAttachCopySnippetButton(copySnippetButton) useGitHubCodeFold(codeFolding) useEnterpriseStatBarStyleFix() diff --git a/src/utils/config/helper.ts b/src/utils/config/helper.ts index 651a61a..fb1b316 100644 --- a/src/utils/config/helper.ts +++ b/src/utils/config/helper.ts @@ -9,7 +9,6 @@ export type Config = { focusSearchInputShortcut: string | undefined // shortcut for focusing search input accessToken: string | undefined compressSingletonFolder: boolean - copyFileButton: boolean copySnippetButton: boolean intelligentToggle: boolean | null // `null` stands for intelligent, boolean for sidebar open state icons: 'rich' | 'dim' | 'native' @@ -33,7 +32,6 @@ enum configKeys { focusSearchInputShortcut = 'focusSearchInputShortcut', accessToken = 'accessToken', compressSingletonFolder = 'compressSingletonFolder', - copyFileButton = 'copyFileButton', copySnippetButton = 'copySnippetButton', intelligentToggle = 'intelligentToggle', icons = 'icons', @@ -59,7 +57,6 @@ export const getDefaultConfigs: () => Config = () => ({ focusSearchInputShortcut: undefined, accessToken: '', compressSingletonFolder: true, - copyFileButton: !isInGitHub, // disable on github.com copySnippetButton: !isInGitHub, // disable on github.com intelligentToggle: null, icons: 'rich',