Skip to content

kie-issues:129: Revisit 'Open in KIE Sandbox' button #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/chrome-extension/src/app/Dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export class Dependencies {
};

public readonly openRepoInExternalEditor = {
buttonContainerOnRepoHome: () => {
return document.querySelector("#repository-details-container .pagehead-actions") as HTMLElement | null;
},
buttonContainerOnRepoFilesList: () => {
return document.querySelector(".d-flex.gap-2")?.parentElement as HTMLElement | null;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import * as React from "react";
import * as ReactDOM from "react-dom";
import { Globals, Main } from "../common/Main";
import { createAndGetMainContainer, openRepoInExternalEditorContainer, removeAllChildren } from "../../utils";
import {
createAndGetMainContainer,
openRepoInExternalEditorContainer,
openRepoInExternalEditorContainerFromRepositoryHome,
removeAllChildren,
} from "../../utils";
import { OpenInExternalEditorButton } from "./OpenInExternalEditorButton";
import { GitHubPageType } from "../../github/GitHubPageType";
import {
KOGITO_IFRAME_CONTAINER_PR_CLASS,
KOGITO_OPEN_REPO_IN_EXTERNAL_EDITOR_CONTAINER_CLASS,
KOGITO_TOOLBAR_CONTAINER_PR_CLASS,
KOGITO_VIEW_ORIGINAL_LINK_CONTAINER_PR_CLASS,
} from "../../constants";
import { KOGITO_OPEN_REPO_IN_EXTERNAL_EDITOR_CONTAINER_CLASS } from "../../constants";

export function renderOpenRepoInExternalEditorApp(
args: Globals & { className: string; pageType: GitHubPageType; container: () => HTMLElement }
Expand All @@ -50,7 +50,9 @@ export function renderOpenRepoInExternalEditorApp(
>
{ReactDOM.createPortal(
<OpenInExternalEditorButton className={args.className} pageType={args.pageType} />,
openRepoInExternalEditorContainer(args.id, args.container())
GitHubPageType.REPO_HOME === args.pageType
? openRepoInExternalEditorContainerFromRepositoryHome(args.id, args.container())
: openRepoInExternalEditorContainer(args.id, args.container())
)}
</Main>,
createAndGetMainContainer(args.id, args.dependencies.all.body()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
extractOpenFileExtension,
extractOpenFilePath,
iframeFullscreenContainer,
openRepoInExternalEditorContainer,
removeAllChildren,
waitForElementToBeReady,
} from "../../utils";
Expand All @@ -35,6 +36,8 @@ import { KOGITO_IFRAME_CONTAINER_CLASS, KOGITO_TOOLBAR_CONTAINER_CLASS } from ".
import { fetchFile } from "../../github/api";
import { useGitHubApi } from "../common/GitHubContext";
import { useGlobals } from "../common/GlobalContext";
import { OpenInExternalEditorButton } from "../openRepoInExternalEditor/OpenInExternalEditorButton";
import { GitHubPageType } from "../../github/GitHubPageType";

export interface FileInfo {
repo: string;
Expand All @@ -43,7 +46,9 @@ export interface FileInfo {
gitRef: string;
}

export async function renderSingleEditorReadonlyApp(args: Globals & { fileInfo: FileInfo }) {
export async function renderSingleEditorReadonlyApp(
args: Globals & { className: string; pageType: GitHubPageType; container: () => HTMLElement; fileInfo: FileInfo }
) {
// wait for the dom element to be ready before rendering
await waitForElementToBeReady("textarea[id='read-only-cursor-text-area']");
// Checking whether this text editor exists is a good way to determine if the page is "ready",
Expand Down Expand Up @@ -80,6 +85,10 @@ export async function renderSingleEditorReadonlyApp(args: Globals & { fileInfo:
resourceContentServiceFactory={args.resourceContentServiceFactory}
externalEditorManager={args.externalEditorManager}
>
{ReactDOM.createPortal(
<OpenInExternalEditorButton className={args.className} pageType={args.pageType} />,
openRepoInExternalEditorContainer(args.id, args.container())
)}
<SingleEditorViewApp fileInfo={args.fileInfo} openFileExtension={openFileExtension} />
</Main>,
createAndGetMainContainer(args.id, args.dependencies.all.body()!),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export enum GitHubPageType {
VIEW,
EDIT,
PR_FILES_OR_COMMITS,
CAN_OPEN_REPO_IN_EXTERNAL_EDITOR,
ANY,
PR_HOME,
REPO_HOME,
}
13 changes: 13 additions & 0 deletions packages/chrome-extension/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ export function openRepoInExternalEditorContainer(id: string, container: HTMLEle
return element();
}

export function openRepoInExternalEditorContainerFromRepositoryHome(id: string, container: HTMLElement) {
const element = () => document.querySelector(`.${KOGITO_OPEN_REPO_IN_EXTERNAL_EDITOR_CONTAINER_CLASS}.${id}`)!;

if (!element()) {
container.insertAdjacentHTML(
"beforeend",
`<li><div class="${KOGITO_OPEN_REPO_IN_EXTERNAL_EDITOR_CONTAINER_CLASS} ${id}"></div></li>`
);
}

return element();
}

export function extractOpenFileExtension(url: string) {
return url
.split(".")
Expand Down
20 changes: 14 additions & 6 deletions packages/chrome-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ function init(globals: Globals) {
if (pageType === GitHubPageType.EDIT) {
renderSingleEditorApp({ ...globals, fileInfo });
} else if (pageType === GitHubPageType.VIEW) {
renderSingleEditorReadonlyApp({ ...globals, fileInfo });
renderSingleEditorReadonlyApp({
...globals,
pageType,
className: "btn ml-2 d-none d-md-block",
container: () => globals.dependencies.openRepoInExternalEditor.buttonContainerOnRepoFilesList()!,
fileInfo,
});
} else if (pageType === GitHubPageType.PR_FILES_OR_COMMITS) {
renderPrEditorsApp({ ...globals });
} else if (pageType === GitHubPageType.PR_HOME) {
Expand All @@ -109,12 +115,12 @@ function init(globals: Globals) {
className: "btn btn-sm",
container: () => globals.dependencies.openRepoInExternalEditor.buttonContainerOnPrs()!,
});
} else if (pageType === GitHubPageType.CAN_OPEN_REPO_IN_EXTERNAL_EDITOR) {
} else if (pageType === GitHubPageType.REPO_HOME) {
renderOpenRepoInExternalEditorApp({
...globals,
pageType,
className: "btn ml-2 d-none d-md-block",
container: () => globals.dependencies.openRepoInExternalEditor.buttonContainerOnRepoFilesList()!,
className: "btn btn-sm",
container: () => globals.dependencies.openRepoInExternalEditor.buttonContainerOnRepoHome()!,
});
} else {
throw new Error(`Unknown GitHubPageType ${pageType}`);
Expand Down Expand Up @@ -172,9 +178,11 @@ export function discoverCurrentGitHubPageType() {
}

const isOrgSlashRepo = window.location.pathname.split("/").length === 3;
const isOrgSlashRepoSlashTreeSlashName =
window.location.pathname.split("/tree/").length === 2 && !window.location.pathname.split("/tree/")[1].includes("/");

if (pathnameMatches(`/.*/.*/tree/.*`) || isOrgSlashRepo) {
return GitHubPageType.CAN_OPEN_REPO_IN_EXTERNAL_EDITOR;
if (isOrgSlashRepo || isOrgSlashRepoSlashTreeSlashName) {
return GitHubPageType.REPO_HOME;
}

if (pathnameMatches(`.*/.*/pull/[0-9]+/files.*`)) {
Expand Down
24 changes: 12 additions & 12 deletions packages/chrome-extension/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ describe("discoverCurrentGitHubPageType", () => {
expect(type).toStrictEqual(GitHubPageType.EDIT);
});

test("repo home", async () => {
setWindowLocationPathname("github.com/organization/repositoryName");
const type = index.discoverCurrentGitHubPageType();
expect(type).toStrictEqual(GitHubPageType.REPO_HOME);
});

test("repo home with branch", async () => {
setWindowLocationPathname("github.com/organization/repositoryName/tree/main");
const type = index.discoverCurrentGitHubPageType();
expect(type).toStrictEqual(GitHubPageType.REPO_HOME);
});

test("pr home", async () => {
setWindowLocationPathname("/org/repo/pull/1");
const type = index.discoverCurrentGitHubPageType();
Expand All @@ -71,18 +83,6 @@ describe("discoverCurrentGitHubPageType", () => {
expect(type).toStrictEqual(GitHubPageType.PR_FILES_OR_COMMITS);
});

test("tree repo", async () => {
setWindowLocationPathname("/user/repo/tree/some_ref");
const type = index.discoverCurrentGitHubPageType();
expect(type).toStrictEqual(GitHubPageType.CAN_OPEN_REPO_IN_EXTERNAL_EDITOR);
});

test("tree repo root", async () => {
setWindowLocationPathname("/user/repo");
const type = index.discoverCurrentGitHubPageType();
expect(type).toStrictEqual(GitHubPageType.CAN_OPEN_REPO_IN_EXTERNAL_EDITOR);
});

test("any", async () => {
setWindowLocationPathname("/");
const type = index.discoverCurrentGitHubPageType();
Expand Down