Fix blob viewer (new UI) (#521)
* Fix blob viewer (new UI) Fixes #371 * Add step test and update snaps * Better selector
This commit is contained in:
@ -44,20 +44,10 @@ async function injectBlob(
|
||||
filename: string,
|
||||
document: Document
|
||||
) {
|
||||
let classicUi = false
|
||||
// React UI (as of 2023-06-23, for signed-in users only)
|
||||
const childWithProperClass = document.querySelector<HTMLElement>(
|
||||
'.react-blob-view-header-sticky'
|
||||
)
|
||||
let element = childWithProperClass?.parentElement
|
||||
if (!element) {
|
||||
// Classic UI
|
||||
const childWithProperClass =
|
||||
document.querySelector<HTMLElement>('.js-blob-header')
|
||||
element = childWithProperClass?.parentElement
|
||||
classicUi = !!element
|
||||
}
|
||||
|
||||
if (!element) {
|
||||
throw Error("Couldn't find blob html element to inject")
|
||||
}
|
||||
@ -70,7 +60,6 @@ async function injectBlob(
|
||||
repo,
|
||||
sha,
|
||||
filename,
|
||||
classicUi,
|
||||
colorMode,
|
||||
})
|
||||
root.render(cadBlobPage)
|
||||
|
@ -13,14 +13,12 @@ function CadBlobPortal({
|
||||
repo,
|
||||
sha,
|
||||
filename,
|
||||
classicUi,
|
||||
}: {
|
||||
element: HTMLElement
|
||||
owner: string
|
||||
repo: string
|
||||
sha: string
|
||||
filename: string
|
||||
classicUi: boolean
|
||||
}): React.ReactElement {
|
||||
const [richBlob, setRichBlob] = useState<FileBlob>()
|
||||
const [richSelected, setRichSelected] = useState(true)
|
||||
@ -29,23 +27,14 @@ function CadBlobPortal({
|
||||
const [sourceElements, setSourceElements] = useState<HTMLElement[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
let existingToggle: HTMLElement | undefined | null
|
||||
let toolbar: HTMLElement | undefined | null
|
||||
let blob: HTMLElement | undefined | null
|
||||
if (classicUi) {
|
||||
// no existing toggle
|
||||
toolbar = element.querySelector<HTMLElement>('.js-blob-header')
|
||||
blob = element.querySelector<HTMLElement>('.blob-wrapper')
|
||||
} else {
|
||||
existingToggle = element.querySelector<HTMLElement>(
|
||||
'ul[class*=SegmentedControl]'
|
||||
const existingToggle = element.querySelector<HTMLElement>(
|
||||
'ul[class*=SegmentedControl]'
|
||||
)
|
||||
const toolbar = existingToggle?.parentElement
|
||||
let blob: HTMLElement | undefined | null =
|
||||
element.querySelector<HTMLElement>(
|
||||
'section[aria-labelledby*=file-name-id]'
|
||||
)
|
||||
toolbar = existingToggle?.parentElement
|
||||
blob = element.querySelector<HTMLElement>(
|
||||
'section[aria-labelledby="file-name-id"]'
|
||||
)
|
||||
}
|
||||
|
||||
const isPreviewAlreadyEnabled =
|
||||
existingToggle && existingToggle.childElementCount > 2 // Preview, Code, Blame
|
||||
if (isPreviewAlreadyEnabled) {
|
||||
@ -93,7 +82,7 @@ function CadBlobPortal({
|
||||
{toolbarContainer &&
|
||||
createPortal(
|
||||
<SegmentedControl
|
||||
sx={{ mr: classicUi ? 2 : 0, order: -1 }} // prepend in flex
|
||||
sx={{ mr: 0, order: -1 }} // prepend in flex
|
||||
aria-label="File view"
|
||||
onChange={(index: number) => {
|
||||
if (index < 2) {
|
||||
@ -114,11 +103,7 @@ function CadBlobPortal({
|
||||
<SegmentedControl.Button selected={!richSelected}>
|
||||
Code
|
||||
</SegmentedControl.Button>
|
||||
{!classicUi && (
|
||||
<SegmentedControl.Button>
|
||||
Blame
|
||||
</SegmentedControl.Button>
|
||||
)}
|
||||
<SegmentedControl.Button>Blame</SegmentedControl.Button>
|
||||
</SegmentedControl>,
|
||||
toolbarContainer
|
||||
)}
|
||||
@ -148,7 +133,6 @@ export type CadBlobPageProps = {
|
||||
repo: string
|
||||
sha: string
|
||||
filename: string
|
||||
classicUi: boolean
|
||||
colorMode: ColorModeWithAuto
|
||||
}
|
||||
|
||||
@ -158,7 +142,6 @@ export function CadBlobPage({
|
||||
repo,
|
||||
sha,
|
||||
filename,
|
||||
classicUi,
|
||||
colorMode,
|
||||
}: CadBlobPageProps): React.ReactElement {
|
||||
return (
|
||||
@ -169,7 +152,6 @@ export function CadBlobPage({
|
||||
repo={repo}
|
||||
sha={sha}
|
||||
filename={filename}
|
||||
classicUi={classicUi}
|
||||
/>
|
||||
</ThemeProvider>
|
||||
)
|
||||
|
@ -1,11 +1,6 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import '@react-three/fiber'
|
||||
import {
|
||||
Box,
|
||||
useTheme,
|
||||
TabNav,
|
||||
Octicon,
|
||||
} from '@primer/react'
|
||||
import { Box, useTheme, TabNav, Octicon } from '@primer/react'
|
||||
import { FileDiff } from '../../chrome/types'
|
||||
import { Viewer3D } from './Viewer3D'
|
||||
import { BufferGeometry, Sphere } from 'three'
|
||||
|
@ -124,17 +124,28 @@ test('commit diff with an added .step file', async ({
|
||||
// expect(screenshot2).toMatchSnapshot()
|
||||
// })
|
||||
|
||||
// TODO: re-enable when new blob page is fixed
|
||||
// test('blob preview with an .obj file', async ({
|
||||
// page,
|
||||
// authorizedBackground,
|
||||
// }) => {
|
||||
// const url =
|
||||
// 'https://github.com/KittyCAD/diff-samples/blob/fd9eec79f0464833686ea6b5b34ea07145e32734/models/box.obj'
|
||||
// const element = await getBlobPreviewElement(page, url)
|
||||
// const screenshot = await element.screenshot()
|
||||
// expect(screenshot).toMatchSnapshot()
|
||||
// })
|
||||
test('blob preview with an .obj file', async ({
|
||||
page,
|
||||
authorizedBackground,
|
||||
}) => {
|
||||
const url =
|
||||
'https://github.com/KittyCAD/diff-samples/blob/fd9eec79f0464833686ea6b5b34ea07145e32734/models/box.obj'
|
||||
const element = await getBlobPreviewElement(page, url)
|
||||
const screenshot = await element.screenshot()
|
||||
expect(screenshot).toMatchSnapshot()
|
||||
})
|
||||
|
||||
|
||||
test('blob preview with a .step file', async ({
|
||||
page,
|
||||
authorizedBackground,
|
||||
}) => {
|
||||
const url =
|
||||
'https://github.com/KittyCAD/diff-samples/blob/fd9eec79f0464833686ea6b5b34ea07145e32734/models/box.step'
|
||||
const element = await getBlobPreviewElement(page, url)
|
||||
const screenshot = await element.screenshot()
|
||||
expect(screenshot).toMatchSnapshot()
|
||||
})
|
||||
|
||||
test('blob preview with an .stl file', async ({
|
||||
page,
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
Binary file not shown.
Before Width: | Height: | Size: 140 KiB After Width: | Height: | Size: 130 KiB |
Reference in New Issue
Block a user