Skip to content

Commit d64bb33

Browse files
Extend file edit options in GitHub to include gitpod (#64)
1 parent 353e8f0 commit d64bb33

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/injectors/github-injector.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ namespace Gitpodify {
88
export const NAV_BTN_ID = "gitpod-btn-nav";
99
export const NAV_BTN_CLASS = "gitpod-nav-btn";
1010
export const NAV_BTN_CLASS_SELECTOR = "." + NAV_BTN_CLASS;
11-
11+
12+
export const EDIT_BTN_ID = "gitpod-btn-file";
13+
1214
export const CSS_REF_BTN_CONTAINER = "gitpod-btn-container";
1315
export const CSS_REF_NO_CONTAINER = "no-container";
1416
}
@@ -23,6 +25,7 @@ export class GitHubInjector extends InjectorBase {
2325
new PullInjector(),
2426
new IssueInjector(),
2527
new FileInjector(),
28+
new EditFileButtonInjector(),
2629
new NavigationInjector(),
2730
new EmptyRepositoryInjector(),
2831
]);
@@ -113,9 +116,13 @@ abstract class ButtonInjectorBase implements ButtonInjector {
113116
const primaryButtons = actionbar.getElementsByClassName("btn-primary");
114117
if (primaryButtons && primaryButtons.length > 1) {
115118
Array.from(primaryButtons)
116-
.slice(0, primaryButtons.length - 1)
117-
.forEach(primaryButton => primaryButton.classList.replace("btn-primary", "btn-secondary"));
119+
.slice(0, primaryButtons.length - 1)
120+
.forEach(primaryButton => primaryButton.classList.replace("btn-primary", "btn-secondary"));
118121
}
122+
123+
// Edit File Menu Options - Open in Gitpod
124+
const editFileButton = document.querySelector('.Box-header .select-menu .SelectMenu-list') as ParentNode;
125+
editFileButton.prepend(this.renderEditButton(currentUrl, openAsPopup));
119126
}
120127

121128
protected renderButton(url: string, openAsPopup: boolean): HTMLElement {
@@ -147,6 +154,20 @@ abstract class ButtonInjectorBase implements ButtonInjector {
147154
protected adjustButton(a: HTMLAnchorElement) {
148155
// do nothing
149156
}
157+
158+
protected renderEditButton(url: string, openAsPopup: boolean): HTMLElement {
159+
const a = document.createElement('a');
160+
a.id = Gitpodify.EDIT_BTN_ID;
161+
a.title = "Edit this file in Gitpod";
162+
a.text = "Open in Gitpod";
163+
a.href = url;
164+
a.target = "_blank";
165+
if (openAsPopup) {
166+
makeOpenInPopup(a);
167+
}
168+
a.className = "SelectMenu-item js-blob-dropdown-click width-full d-flex flex-justify-between color-fg-default f5 text-normal";
169+
return a;
170+
}
150171
}
151172

152173
class PullInjector extends ButtonInjectorBase {
@@ -169,6 +190,16 @@ class IssueInjector extends ButtonInjectorBase {
169190
}
170191
}
171192

193+
class EditFileButtonInjector extends ButtonInjectorBase {
194+
constructor() {
195+
super("", "gitpod-file-edit-btn");
196+
}
197+
198+
isApplicableToCurrentPage(): boolean {
199+
return window.location.pathname.includes("/blob/");
200+
}
201+
}
202+
172203
class FileInjector extends ButtonInjectorBase {
173204
constructor() {
174205
super(".repository-content > div > div > div", "gitpod-file-btn");

0 commit comments

Comments
 (0)