-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render error overlay when themeFileSystem has upload errors
- Loading branch information
1 parent
ac22581
commit d659886
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/theme/src/cli/utilities/theme-environment/hot-reload/error-overlay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const OVERLAY_STYLES = { | ||
container: ` | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
z-index: 9999; | ||
`, | ||
dialog: ` | ||
background: rgba(200, 200, 200, 0.9); | ||
backdrop-filter: blur(10px); | ||
border-radius: 10px; | ||
padding: 20px; | ||
font-family: system-ui, -apple-system, sans-serif; | ||
max-width: 80%; | ||
max-height: 80%; | ||
box-shadow: 0px 0px 10px rgba(0,0,0,0.5); | ||
position: relative; | ||
overflow-y: auto; | ||
`, | ||
closeButton: ` | ||
position: absolute; | ||
top: 10px; | ||
right: 10px; | ||
background: transparent; | ||
border: none; | ||
font-size: 16px; | ||
cursor: pointer; | ||
`, | ||
errorItem: ` | ||
margin-bottom: 16px; | ||
text-align: left; | ||
`, | ||
errorMessage: ` | ||
margin: 8px 0; | ||
white-space: normal; | ||
word-wrap: break-word; | ||
`, | ||
} | ||
|
||
export function getErrorOverlay(errors: Map<string, string[]>): string { | ||
const errorContent = Array.from(errors.entries()) | ||
.map( | ||
([fileKey, messages]) => ` | ||
<div style="${OVERLAY_STYLES.errorItem}"> | ||
<strong>${fileKey}</strong> | ||
${messages.map((msg) => `<pre style="${OVERLAY_STYLES.errorMessage}">- ${msg}</pre>`).join('')} | ||
</div> | ||
`, | ||
) | ||
.join('') | ||
|
||
return ` | ||
<div id="section-error-overlay" style="${OVERLAY_STYLES.container}"> | ||
<div style="${OVERLAY_STYLES.dialog}"> | ||
${errorContent} | ||
<button | ||
style="${OVERLAY_STYLES.closeButton}" | ||
onclick="document.getElementById('section-error-overlay').style.display='none';" | ||
> | ||
× | ||
</button> | ||
</div> | ||
</div> | ||
` | ||
} | ||
|
||
export function injectErrorIntoHtml(html: string, errors: Map<string, string[]>): string { | ||
return html + getErrorOverlay(errors) | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/theme/src/cli/utilities/theme-environment/html.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {getErrorOverlay, injectErrorIntoHtml} from './hot-reload/error-overlay.js' | ||
import {describe, expect, test} from 'vitest' | ||
|
||
describe('html', () => { | ||
test('injects error overlay into HTML with body tag', () => { | ||
// Given | ||
const html = '<html><head></head><body><div>content</div></body></html>' | ||
const errors = new Map([ | ||
['assets/theme.css', ['Syntax error in line 10']], | ||
['sections/header.liquid', ['Missing end tag', 'Invalid liquid syntax']], | ||
]) | ||
|
||
// When | ||
const result = injectErrorIntoHtml(html, errors) | ||
|
||
// Then | ||
const overlay = getErrorOverlay(errors) | ||
expect(result).toBe(html + overlay) | ||
expect(result).toContain('assets/theme.css') | ||
expect(result).toContain('Syntax error in line 10') | ||
expect(result).toContain('sections/header.liquid') | ||
expect(result).toContain('Missing end tag') | ||
expect(result).toContain('Invalid liquid syntax') | ||
}) | ||
|
||
test('preserves original HTML content', () => { | ||
// Given | ||
const html = '<html><head><title>My Store</title></head><body><div>content</div></body></html>' | ||
const errors = new Map([['file.css', ['Error']]]) | ||
|
||
// When | ||
const result = injectErrorIntoHtml(html, errors) | ||
|
||
// Then | ||
const overlay = getErrorOverlay(errors) | ||
expect(result).toBe(html + overlay) | ||
expect(result).toContain('<title>My Store</title>') | ||
expect(result).toContain('<div>content</div>') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters