Skip to content

Commit b52651a

Browse files
authored
fix(ui): build the build-tool snippets from the code primitives (#1327)
The snippets card drew its own padded box, copy button, fade and status line, and each snippet was a hand-written pre element with coloured tokens. Each snippet is now an MdCopyableCode, which is an MdCodeBlock with a copy action, and the card keeps only the tabs and the loading state. The repository and artifact snippets build their text in one place per build tool. The skeleton widths become width classes. The style gate baselines and allowlists lose the entries this removes, and the two end-to-end specs that read the old markup follow it. Signed-off-by: TheMeinerLP <github@themeinerlp.dev>
1 parent 1aaa866 commit b52651a

11 files changed

Lines changed: 141 additions & 414 deletions

File tree

‎reposilite-frontend/e2e/browse-layout.spec.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,14 @@ test.describe('browse layout', () => {
185185
await page.goto(siteUrl(`/browse/${group}`))
186186
const snippet = page.locator('[data-ingot-component="repository-snippet"]').first()
187187
await expect(snippet, 'the snippet of the group').toBeVisible()
188-
expect(await snippet.evaluate(element => getComputedStyle(element).whiteSpace), 'white-space of the snippet').toBe('pre')
189-
const brokenTokens = await snippet.evaluate((element) => [...element.querySelectorAll('span')]
190-
.filter(span => span.getClientRects().length > 1)
191-
.map(span => span.textContent.trim()))
192-
expect(brokenTokens, 'tokens broken across lines').toEqual([])
188+
const code = snippet.locator('pre')
189+
expect(await code.evaluate(element => getComputedStyle(element).whiteSpace), 'white-space of the snippet').toBe('pre')
190+
// Every line of the text is one line on screen: no line is broken at the edge of the card.
191+
const lines = await code.evaluate(element => ({
192+
shown: Math.round(element.getBoundingClientRect().height / parseFloat(getComputedStyle(element).lineHeight)),
193+
written: (element.textContent ?? '').split('\n').length,
194+
}))
195+
expect(lines.shown, 'lines on screen against lines written').toBe(lines.written)
193196
})
194197

195198
test('aligns the includes block of a group with the listing at 1440 px', async ({ page, seed }) => {

‎reposilite-frontend/e2e/browse-overview.spec.ts‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,28 @@ test.describe('browse overview', () => {
2727
// The snippet is the block that holds the Maven repository element for this repository.
2828
const snippet = page.locator('[data-ingot-component="repository-snippet"]').first()
2929
await expect(snippet, 'the snippet with the repository address').toContainText(`/${repository}`)
30-
const url = snippet.locator('.snippet-url').first()
31-
expect(await url.evaluate(element => element.getClientRects().length), 'lines the url is broken into').toBe(1)
30+
const code = snippet.locator('pre')
31+
const lines = await code.evaluate(element => ({
32+
shown: Math.round(element.getBoundingClientRect().height / parseFloat(getComputedStyle(element).lineHeight)),
33+
written: (element.textContent ?? '').split('\n').length,
34+
}))
35+
expect(lines.shown, 'lines on screen against lines written: the url is not broken').toBe(lines.written)
3236
// The snippet scrolls inside its own region; the page itself never scrolls sideways.
3337
const pageSideways = await page.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth)
3438
expect(pageSideways, 'horizontal overflow of the page in px').toBeLessThanOrEqual(0)
3539
})
3640

37-
test('shows the snippet copy button inside the code field only while the field is hovered or focused', async ({ page, seed }) => {
41+
test('offers the snippet copy button inside the code field and answers a press in the status line', async ({ page, seed }) => {
3842
const repository = await seed.repository('copy-hover-snippet')
3943
await page.goto(siteUrl(`/browse/${repository}`))
4044

4145
// The code field is the box that holds the scrolling code region; the button is its child.
4246
const field = page.getByRole('region', { name: /snippet, scrolls sideways/ }).first().locator('..')
4347
const copy = field.getByRole('button', { name: 'Copy snippet' })
4448
await expect(copy, 'the copy button inside the code field').toBeVisible()
45-
// The button reveals itself through its wrapper, so the wrapper carries the opacity.
46-
const reveal = copy.locator('..')
4749

48-
await expect(reveal, 'hidden while the pointer is elsewhere').toHaveCSS('opacity', '0')
49-
await field.hover()
50-
await expect(reveal, 'shown while the code field is hovered').toHaveCSS('opacity', '1')
51-
52-
await page.mouse.move(0, 0)
53-
await expect(reveal, 'hidden again once the pointer leaves').toHaveCSS('opacity', '0')
54-
await copy.focus()
55-
await expect(reveal, 'shown while the button has keyboard focus').toHaveCSS('opacity', '1')
50+
await copy.click()
51+
await expect(page.getByRole('status').first(), 'the answer to the press, a copy or a selected snippet').not.toBeEmpty()
5652
})
5753

5854
test('labels the repository filter as a filter of the list', async ({ page }) => {

‎reposilite-frontend/src/components/card/ArtifactSnippet.vue‎

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
-->
1616

1717
<script setup lang="ts">
18-
import XmlTag from './XmlTag.vue'
19-
import CodeString from './CodeString.vue'
20-
import CodeBrackets from "./CodeBrackets.vue"
21-
import { computed, ref, type PropType } from "vue"
18+
import MdCopyableCode from '../md/MdCopyableCode.vue'
19+
import { computed, type PropType } from 'vue'
2220
import type { ArtifactSnippetData, SnippetConfiguration } from './snippetTypes'
2321
24-
defineProps({
22+
const props = defineProps({
2523
configuration: {
2624
type: Object as PropType<SnippetConfiguration>,
2725
required: true
@@ -32,46 +30,31 @@ defineProps({
3230
}
3331
})
3432
35-
const preElement = ref<HTMLElement>()
36-
const content = computed(() => preElement?.value?.textContent)
37-
38-
defineExpose({ content })
33+
// The snippet as it is shown and copied: no line break before the first line or after the last.
34+
const text = computed(() => {
35+
const { groupId, artifactId, version } = props.data
36+
const coordinate = `${groupId}:${artifactId}:${version}`
37+
switch (props.configuration.lang) {
38+
case 'xml':
39+
return `<dependency>\n <groupId>${groupId}</groupId>\n <artifactId>${artifactId}</artifactId>\n <version>${version}</version>\n</dependency>`
40+
case 'groovy':
41+
return `implementation "${coordinate}"`
42+
case 'kotlin':
43+
return `implementation("${coordinate}")`
44+
case 'scala':
45+
return `"${groupId}" %% "${artifactId}" %% "${version}"`
46+
default:
47+
return ''
48+
}
49+
})
3950
</script>
4051

4152
<template>
42-
<!--
43-
The closing tag stays on the last line of each snippet: a line break before it would be shown and copied
44-
as an empty line.
45-
-->
46-
<pre
47-
v-if="configuration.lang === 'xml'"
48-
ref="preElement"
49-
data-ingot-component="artifact-snippet"
50-
>
51-
<XmlTag name="dependency">
52-
<XmlTag name="groupId">{{ data.groupId }}</XmlTag>
53-
<XmlTag name="artifactId">{{ data.artifactId }}</XmlTag>
54-
<XmlTag name="version">{{ data.version }}</XmlTag>
55-
</XmlTag></pre>
56-
<pre
57-
v-else-if="configuration.lang === 'groovy'"
58-
ref="preElement"
59-
data-ingot-component="artifact-snippet"
60-
>
61-
implementation <CodeString>{{ `${data.groupId}:${data.artifactId}:${data.version}` }}</CodeString></pre>
62-
<pre
63-
v-else-if="configuration.lang === 'kotlin'"
64-
ref="preElement"
65-
data-ingot-component="artifact-snippet"
66-
>
67-
implementation<CodeBrackets
68-
start="("
69-
end=")"
70-
><CodeString>{{ `${data.groupId}:${data.artifactId}:${data.version}` }}</CodeString></CodeBrackets></pre>
71-
<pre
72-
v-else-if="configuration.lang === 'scala'"
73-
ref="preElement"
74-
data-ingot-component="artifact-snippet"
75-
>
76-
<CodeString>{{ data.groupId }}</CodeString> %% <CodeString>{{ data.artifactId }}</CodeString> %% <CodeString>{{ data.version }}</CodeString></pre>
53+
<div data-ingot-component="artifact-snippet">
54+
<MdCopyableCode
55+
:text="text"
56+
:label="`${configuration.name} snippet, scrolls sideways`"
57+
copy-label="Copy snippet"
58+
/>
59+
</div>
7760
</template>

‎reposilite-frontend/src/components/card/RepositorySnippet.test.ts‎

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect } from 'vitest'
22
import { mount } from '@vue/test-utils'
33
import RepositorySnippet from './RepositorySnippet.vue'
4+
import MdCopyableCode from '../md/MdCopyableCode.vue'
45
import type { RepositorySnippetData } from './snippetTypes'
56

67
const data: RepositorySnippetData = {
@@ -12,9 +13,11 @@ const data: RepositorySnippetData = {
1213

1314
const render = (lang: string) => mount(RepositorySnippet, { props: { configuration: { name: 'Maven', lang }, data } })
1415

16+
const codeText = (wrapper: ReturnType<typeof render>) => wrapper.find('pre').element.textContent
17+
1518
describe('RepositorySnippet Maven text', () => {
1619
it('copies the snippet exactly as written: one element per line, no blank line between them', () => {
17-
const text = render('xml').find('pre').element.textContent
20+
const text = codeText(render('xml'))
1821

1922
expect(text).toBe(
2023
'<repository>\n' +
@@ -24,59 +27,33 @@ describe('RepositorySnippet Maven text', () => {
2427
'</repository>'
2528
)
2629
})
27-
28-
it('keeps the url on one line, without break opportunities inside it', () => {
29-
const pre = render('xml').find('pre').element
30-
31-
expect(pre.querySelectorAll('wbr'), 'the url is not split into pieces').toHaveLength(0)
32-
expect(pre.textContent).toContain(' <url>http://localhost/releases</url>\n')
33-
})
3430
})
3531

3632
describe('RepositorySnippet url', () => {
37-
it.each(['xml', 'groovy', 'kotlin', 'scala'])('marks the url of the %s snippet as its own element, never broken', (lang) => {
38-
const url = render(lang).find('pre').element.querySelector('.snippet-url')!
33+
it.each(['xml', 'groovy', 'kotlin', 'scala'])('keeps the url of the %s snippet whole on one line', (lang) => {
34+
const text = codeText(render(lang))
3935

40-
expect(url, 'the url is wrapped in its own element').not.toBeNull()
41-
expect(url.textContent, 'the wrapped element holds the url and nothing else').toBe('http://localhost/releases')
42-
expect(url.classList, 'the url is not allowed to break at any character').not.toContain('wrap-anywhere')
43-
expect(url.classList, 'the url is not allowed to wrap').not.toContain('whitespace-normal')
44-
})
45-
46-
it('wraps only the url, so the rest of the snippet keeps its line structure', () => {
47-
const pre = render('kotlin').find('pre').element
48-
49-
expect(pre.querySelectorAll('.snippet-url'), 'exactly one wrapped part for one url').toHaveLength(1)
50-
expect(pre.textContent, 'the wrapper adds no character to the copied text').toContain('url = uri("http://localhost/releases")')
36+
expect(text, 'the url is in the text as written').toContain('http://localhost/releases')
37+
expect(text, 'no zero-width space is copied with the url').not.toMatch(/\u200b/)
5138
})
5239

53-
it('adds no zero-width space and no line break to the copied text', () => {
54-
const text = render('xml').find('pre').element.textContent
55-
56-
expect(text, 'no zero-width space is copied with the url').not.toMatch(/\u200b/)
57-
expect(text).toBe(
58-
'<repository>\n' +
59-
' <id>releases-x</id>\n' +
60-
' <name>Ingot Repository</name>\n' +
61-
' <url>http://localhost/releases</url>\n' +
62-
'</repository>'
63-
)
40+
it('writes the Gradle Kotlin url as one call on one line', () => {
41+
expect(codeText(render('kotlin'))).toContain('url = uri("http://localhost/releases")')
6442
})
6543
})
6644

6745
describe('RepositorySnippet block', () => {
68-
// A token is never broken across lines: the block keeps every line whole and the card scrolls sideways instead.
69-
it.each(['xml', 'groovy', 'kotlin', 'scala'])('keeps the %s block on whole lines, so long tokens scroll sideways', (lang) => {
70-
const pre = render(lang).find('pre').element
46+
it.each(['xml', 'groovy', 'kotlin', 'scala'])('draws the %s snippet as one copyable code block under the snippet anchor', (lang) => {
47+
const wrapper = render(lang)
7148

72-
expect(pre.classList, 'no token may break at any character').not.toContain('wrap-anywhere')
73-
expect(pre.classList, 'the block keeps its lines as they are').toContain('whitespace-pre')
49+
expect(wrapper.attributes('data-ingot-component'), 'the anchor stays on the root').toBe('repository-snippet')
50+
expect(wrapper.findAllComponents(MdCopyableCode), 'one copyable code block per snippet').toHaveLength(1)
7451
})
7552
})
7653

7754
describe('RepositorySnippet Gradle and SBT text', () => {
7855
it('indents the Gradle Groovy block by four spaces and keeps its url whole', () => {
79-
const text = render('groovy').find('pre').element.textContent
56+
const text = codeText(render('groovy'))
8057

8158
expect(text).toBe(
8259
'maven {\n' +
@@ -87,7 +64,7 @@ describe('RepositorySnippet Gradle and SBT text', () => {
8764
})
8865

8966
it('writes the SBT resolver as one line, the way it is pasted into build.sbt', () => {
90-
const text = render('scala').find('pre').element.textContent
67+
const text = codeText(render('scala'))
9168

9269
expect(text).toBe('resolvers += "releases-x" at "http://localhost/releases"')
9370
})

‎reposilite-frontend/src/components/card/RepositorySnippet.vue‎

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
-->
1616

1717
<script setup lang="ts">
18-
import XmlTag from './XmlTag.vue'
19-
import CodeString from './CodeString.vue'
20-
import CodeBrackets from "./CodeBrackets.vue"
21-
import { computed, ref, type PropType } from 'vue'
18+
import MdCopyableCode from '../md/MdCopyableCode.vue'
19+
import { computed, type PropType } from 'vue'
2220
import type { RepositorySnippetData, SnippetConfiguration } from './snippetTypes'
2321
2422
const props = defineProps({
@@ -41,62 +39,30 @@ const gradleId = computed(() => {
4139
return gradleIdUppercase.charAt(0).toLowerCase() + gradleIdUppercase.slice(1)
4240
})
4341
44-
const preElement = ref<HTMLElement | null>(null)
45-
const content = computed(() => preElement?.value?.textContent)
46-
47-
defineExpose({ content })
42+
// The snippet as it is shown and copied: no line break before the first line or after the last.
43+
const text = computed(() => {
44+
const { repoId, title, domain } = props.data
45+
switch (props.configuration.lang) {
46+
case 'xml':
47+
return `<repository>\n <id>${repoId}</id>\n <name>${title}</name>\n <url>${domain}</url>\n</repository>`
48+
case 'groovy':
49+
return `maven {\n name "${gradleId.value}"\n url "${domain}"\n}`
50+
case 'kotlin':
51+
return `maven {\n name = "${gradleId.value}"\n url = uri("${domain}")\n}`
52+
case 'scala':
53+
return `resolvers += "${repoId}" at "${domain}"`
54+
default:
55+
return ''
56+
}
57+
})
4858
</script>
4959

5060
<template>
51-
<!--
52-
The closing tag stays on the last line of each snippet: a line break before it would be shown and copied
53-
as an empty line.
54-
-->
55-
<pre
56-
v-if="configuration.lang === 'xml'"
57-
ref="preElement"
58-
class="whitespace-pre"
59-
data-ingot-component="repository-snippet"
60-
>
61-
<XmlTag name="repository">
62-
<XmlTag name="id">{{ data.repoId }}</XmlTag>
63-
<XmlTag name="name">{{ data.title }}</XmlTag>
64-
<XmlTag name="url"><span class="snippet-url">{{ data.domain }}</span></XmlTag>
65-
</XmlTag></pre>
66-
<pre
67-
v-else-if="configuration.lang === 'groovy'"
68-
ref="preElement"
69-
class="whitespace-pre"
70-
data-ingot-component="repository-snippet"
71-
>
72-
maven <CodeBrackets
73-
start="{"
74-
end="}"
75-
>
76-
name <CodeString>{{ gradleId }}</CodeString>
77-
url <CodeString><span class="snippet-url">{{ data.domain }}</span></CodeString>
78-
</CodeBrackets></pre>
79-
<pre
80-
v-else-if="configuration.lang === 'kotlin'"
81-
ref="preElement"
82-
class="whitespace-pre"
83-
data-ingot-component="repository-snippet"
84-
>
85-
maven <CodeBrackets
86-
start="{"
87-
end="}"
88-
>
89-
name = <CodeString>{{ gradleId }}</CodeString>
90-
url = uri<CodeBrackets
91-
start="("
92-
end=")"
93-
><CodeString><span class="snippet-url">{{ data.domain }}</span></CodeString></CodeBrackets>
94-
</CodeBrackets></pre>
95-
<pre
96-
v-else-if="configuration.lang === 'scala'"
97-
ref="preElement"
98-
class="whitespace-pre"
99-
data-ingot-component="repository-snippet"
100-
>
101-
resolvers += <CodeString>{{ data.repoId }}</CodeString> at <CodeString><span class="snippet-url">{{ data.domain }}</span></CodeString></pre>
61+
<div data-ingot-component="repository-snippet">
62+
<MdCopyableCode
63+
:text="text"
64+
:label="`${configuration.name} snippet, scrolls sideways`"
65+
copy-label="Copy snippet"
66+
/>
67+
</div>
10268
</template>

0 commit comments

Comments
 (0)