Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
feat: add preliminary page inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydw committed Dec 5, 2021
1 parent fd6d5ea commit 8853068
Show file tree
Hide file tree
Showing 13 changed files with 1,624 additions and 4 deletions.
3 changes: 3 additions & 0 deletions example/content/pages/index.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
partials:
- partial: hero
title: Hello World 1!
image:
url: 'data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7'
alt: 'Alt text'
- partial: hero
title: Hello World 2!
3 changes: 3 additions & 0 deletions example/views/partials/hero.njk
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<div class="hero">
<h1>{{partial.title}}</h1>
{% if partial.image %}
<img src="{{partial.image.url}}" alt="{{partial.image.alt}}">
{% endif %}
</div>
2 changes: 1 addition & 1 deletion gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ gulp.task('build:templates', async () => {
.pipe(gulp.dest(ENTRIES.templates.out));
})

gulp.task('watch', gulp.parallel('watch:js', 'watch:templates'));
gulp.task('build', gulp.parallel('build:js', 'build:templates'));
gulp.task('watch', gulp.parallel('build', 'watch:js', 'watch:templates'));
gulp.task('default', gulp.series('watch'));
1,173 changes: 1,173 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"@amagaki/amagaki": "^1.7.0",
"@blinkk/degu": "^2.0.0",
"@types/gulp": "^4.0.9",
"@types/js-beautify": "^1.13.3",
"@types/node": "^13.11.1",
Expand Down
3 changes: 3 additions & 0 deletions src/page-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test('PageBuilder', async (t: ExecutionContext) => {
<page-module>
<div class="hero">
<h1>Hello World 1!</h1>
<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" alt="Alt text">
</div>
</page-module>
<page-module>
Expand Down Expand Up @@ -98,6 +99,7 @@ test('PageBuilder dev', async (t: ExecutionContext) => {
<page-module-inspector partial="hero"></page-module-inspector>
<div class="hero">
<h1>Hello World 1!</h1>
<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub//ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcppV0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" alt="Alt text">
</div>
</page-module>
<page-module>
Expand All @@ -108,6 +110,7 @@ test('PageBuilder dev', async (t: ExecutionContext) => {
</page-module>
</main>
</div>
<page-inspector></page-inspector>
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions src/page-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class PageBuilder {
? await this.buildExtraElements(this.options.body?.extra)
: ''
}
${this.enableInspector ? html`<page-inspector></page-inspector>` : ''}
</body>
</html>
`;
Expand Down
5 changes: 3 additions & 2 deletions src/partial-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ test('PatialPreview', async (t: ExecutionContext) => {
<page-module>
<div class="partial-preview-gallery">
<ul>
<li>/views/partials/header.njk
<li>/views/partials/hero.njk
<li><a href="/preview/header/">header</a>
<li><a href="/preview/hero/">hero</a>
</ul>
</div>
</page-module>
</main>
</div>
<page-inspector></page-inspector>
</body>
</html>
Expand Down
94 changes: 94 additions & 0 deletions src/ui/attribute-highlighter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import * as dom from '@blinkk/degu/lib/dom/dom';
import * as func from '@blinkk/degu/lib/func/func';

import {LitElement, css, html} from 'lit';

import {AttributeHighlighter as DeguAttributeHighlighter} from '@blinkk/degu/lib/ui/attribute-highlighter';
import {customElement} from 'lit/decorators.js';

// Adds required styles to page only ever once.
const addStylesToPage = func.runOnlyOnce(() => {
dom.addStylesToPage(`
.attribute-highlighter {
align-items: left;
color: #FFF;
background: #000;
border-radius: 3px;
box-shadow: none !important;
display: flex;
flex-direction: column;
font-size: 11px;
height: auto;
justify-content: center;
left: var(--left);
margin: 0 !important;
max-width: var(--max-width);
padding: 0px 8px;
position: fixed !important;
// Slight off center.
top: calc(var(--center) + 10px);
transform: translateX(-50%) translateY(0%);
transition: transform 0.3s ease;
transform-origin: top center;
z-index: 9999;
cursor: pointer;
}
.attribute-highlighter span {
display: block;
}
.attribute-highlighter.active,
.attribute-highlighter:active, {
z-index: 10000;
transform: translateX(-50%) translateY(0%) scale(1.2);
}
.attribute-highlighter:hover {
cursor: pointer;
z-index: 10000;
}
.attribute-highlighter.up {
top: calc(var(--center) - 10px + (var(--height) * -1));
}
`);
});

@customElement('attribute-highlighter')
export class AttributeHighlighter extends LitElement {
private attributeHighlighter?: DeguAttributeHighlighter;
private show = false;
connectedCallback() {
super.connectedCallback();

// Add styles to page.
addStylesToPage();

document.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 'a') {
this.toggleA11yHighlight();
}
});
}

private toggleA11yHighlight() {
this.show = !this.show;
if (this.show) {
this.attributeHighlighter = new DeguAttributeHighlighter({
cssClassName: 'attribute-highlighter',
scopeQuerySelector: 'page-module',
attributes: ['alt', 'aria-label'],
warnMissingAttributes: [
{
attribute: 'alt',
querySelector: 'img',
},
],
});
this.attributeHighlighter.refresh();
} else {
this.attributeHighlighter?.dispose();
}
}

createRenderRoot() {
return this;
}
}
182 changes: 182 additions & 0 deletions src/ui/grid-inspector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import {LitElement, css, html} from 'lit';

import {customElement} from 'lit/decorators.js';

@customElement('grid-inspector')
export class GridInspector extends LitElement {
connectedCallback() {
super.connectedCallback();
// Enable toggling the grid overlay using `ctrl+g` (similar to Figma).
document.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.ctrlKey && e.key === 'g') {
this.toggleGridOverlay();
}
});
}

private toggleGridOverlay() {
this.style.display = this.style.display === 'none' ? 'block' : 'none';
}

static get styles() {
return css`
:host {
display:none
}
.page-grid-overlay {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji';
font-size: 12px;
font-weight: 500;
color:#000;
font-optical-sizing:none;
font-size:14px;
font-weight:500;
height:100vh;
left:0;
letter-spacing:.25px;
line-height:20px;
pointer-events:none;
position:fixed;
top:0;
width:100%;
z-index:2000;
}
.page-grid-overlay * {
box-sizing:border-box
}
.page-grid-overlay .page-grid__label {
display:none;
margin-top:42px;
margin-left:8px;
padding:0 8px;
background:#ebffff;
position:absolute;
text-align:left
}
.page-grid-overlay .opt--mobile::before {
content:"Mobile (320 - 599px)"
}
@media(max-width: 599px) {
.page-grid-overlay .opt--mobile {
display:block
}
}
.page-grid-overlay .opt--ds-tablet::before {
content:"Tablet (600 - 1023px)"
}
@media(min-width: 600px)and (max-width: 1023px) {
.page-grid-overlay .opt--ds-tablet {
display:block
}
}
.page-grid-overlay .opt--ds-laptop::before {
content:"Laptop (1024 - 1439px)"
}
@media(min-width: 1024px)and (max-width: 1439px) {
.page-grid-overlay .opt--ds-laptop {
display:block
}
}
.page-grid-overlay .opt--ds-desktop::before {
content:"Desktop (1440px)"
}
@media(min-width: 1440px) {
.page-grid-overlay .opt--ds-desktop {
display:block
}
}
.page-grid-overlay .page-grid {
display:grid;
-webkit-column-gap:24px;
-moz-column-gap:24px;
column-gap:24px;
counter-reset:column;
height:100%
}
@media(max-width: 599px) {
.page-grid-overlay .page-grid {
grid-template-columns:repeat(4, 1fr);
padding:0 16px
}
}
@media(min-width: 600px)and (max-width: 1023px) {
.page-grid-overlay .page-grid {
grid-template-columns:repeat(12, 1fr);
margin-left:auto;
margin-right:auto;
max-width:600px;
padding:0 24px
}
}
@media(min-width: 1024px)and (max-width: 1439px) {
.page-grid-overlay .page-grid {
grid-template-columns:repeat(12, 1fr);
padding:0 24px
}
}
@media(min-width: 1440px) {
.page-grid-overlay .page-grid {
grid-template-columns:repeat(12, 1fr);
margin-left:auto;
margin-right:auto;
max-width:1440px;
padding:0 24px
}
}
@media(max-width: 599px) {
.page-grid-overlay .page-grid>* {
grid-column-end:span 4
}
}
@media(min-width: 600px) {
.page-grid-overlay .page-grid>* {
grid-column-end:span 12
}
}
[page-grid-overlay=true] .page-grid-overlay .page-grid>* {
box-shadow:inset 0 0 0 1px orchid
}
.page-grid-overlay .page-grid__col {
grid-column-end:span 1;
background-color:rgba(0,255,255,.08);
height:100%;
padding-top:12px;
text-align:center
}
.page-grid-overlay .page-grid__col::before {
counter-increment:column;
content:counter(column)
}
@media(max-width: 599px) {
.page-grid-overlay .page-grid__col:nth-child(5),.page-grid-overlay .page-grid__col:nth-child(6),.page-grid-overlay .page-grid__col:nth-child(7),.page-grid-overlay .page-grid__col:nth-child(8),.page-grid-overlay .page-grid__col:nth-child(9),.page-grid-overlay .page-grid__col:nth-child(10),.page-grid-overlay .page-grid__col:nth-child(11),.page-grid-overlay .page-grid__col:nth-child(12) {
display:none
}
}
`;
}

render() {
return html`
<div class="page-grid-overlay">
<div class="page-grid">
<div class="page-grid__label opt--mobile"></div>
<div class="page-grid__label opt--ds-tablet"></div>
<div class="page-grid__label opt--ds-laptop"></div>
<div class="page-grid__label opt--ds-desktop"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
<div class="page-grid__col"></div>
</div>
</div>
`;
}
}
Loading

0 comments on commit 8853068

Please sign in to comment.