From 42ce2dffb59dff541232ed60fcddb8ece9a1e91c Mon Sep 17 00:00:00 2001 From: yakov116 <16872793+yakov116@users.noreply.github.com> Date: Tue, 20 Oct 2020 21:12:46 -0400 Subject: Fetch widget for `view-markdown-source` instead of maintaining a copy of it (#3670) Co-authored-by: Federico --- source/features/view-markdown-source.tsx | 79 ++++++-------------------------- 1 file changed, 14 insertions(+), 65 deletions(-) (limited to 'source/features/view-markdown-source.tsx') diff --git a/source/features/view-markdown-source.tsx b/source/features/view-markdown-source.tsx index 44847a77..21926086 100644 --- a/source/features/view-markdown-source.tsx +++ b/source/features/view-markdown-source.tsx @@ -1,5 +1,6 @@ import './view-markdown-source.css'; import React from 'dom-chef'; +import domify from 'doma'; import select from 'select-dom'; import onetime from 'onetime'; import delegate from 'delegate-it'; @@ -7,73 +8,21 @@ import * as pageDetect from 'github-url-detection'; import FileIcon from 'octicon/file.svg'; import CodeIcon from 'octicon/code.svg'; -import KebabHorizontalIcon from 'octicon/kebab-horizontal.svg'; import features from '.'; import fetchDom from '../helpers/fetch-dom'; import GitHubURL from '../github-helpers/github-url'; import {buildRepoURL} from '../github-helpers'; -const lineActions = onetime(() => ( - -)); +const lineActions = onetime(async () => { + // Avoid having to create the entire 60 lines of JSX. The URL is hardcoded to a file we know the DOM exists. + const randomKnownFile = 'https://github.com/sindresorhus/refined-github/blob/b1229bbaeb8cf071f0711bc2ed1b40dd96cd7a05/.editorconfig'; + const html = await browser.runtime.sendMessage({request: randomKnownFile}); + const blobToolbar = domify(html).querySelector('.BlobToolbar')!; + select('#js-view-git-blame', blobToolbar)!.href = new GitHubURL(location.href).assign({route: 'blame'}).href; + select('#js-new-issue', blobToolbar)!.href = buildRepoURL('issues/new'); + return blobToolbar; +}); const buttonBodyMap = new WeakMap>(); @@ -119,9 +68,9 @@ async function showSource(): Promise { sourceButton.classList.add('selected'); renderedButton.classList.remove('selected'); blurButton(sourceButton); - (await source).before(lineActions()); - dispatchEvent(sourceButton, 'rgh:view-markdown-source'); + + (await source).before(await lineActions()); } async function showRendered(): Promise { @@ -137,9 +86,9 @@ async function showRendered(): Promise { sourceButton.classList.remove('selected'); renderedButton.classList.add('selected'); blurButton(renderedButton); - lineActions().remove(); - dispatchEvent(sourceButton, 'rgh:view-markdown-rendered'); + + (await lineActions()).remove(); } async function init(): Promise { -- cgit v1.2.3