Skip to content

Commit a36b764

Browse files
committed
Add a toolbar in cms ui
1 parent dd18da7 commit a36b764

2 files changed

Lines changed: 118 additions & 2 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { createDOMNodeFromHTML } from '../common.js'
2+
3+
const template = ({ tools }) => {
4+
const __ = '#toolbar-panel'
5+
6+
return (`
7+
<div id="toolbar-panel">
8+
<style>
9+
${__} {
10+
position: fixed;
11+
top: 1em;
12+
left: 50%;
13+
translate: -50% 0;
14+
display: flex;
15+
align-items: flex-end;
16+
gap: .15em;
17+
padding: .3em .5em;
18+
border: 1px solid #fff6;
19+
border-radius: 1em;
20+
font-family: helvetica, sans-serif;
21+
background: #f5f5f5cc;
22+
backdrop-filter: blur(20px) saturate(1.8);
23+
box-shadow:
24+
0 2px 8px #0002,
25+
0 0 0 .5px #0001,
26+
inset 0 .5px 0 #fff8;
27+
z-index: 1;
28+
}
29+
30+
${__} .toolbar-tool {
31+
display: flex;
32+
flex-direction: column;
33+
align-items: center;
34+
gap: .1em;
35+
padding: .25em .5em .15em;
36+
border: none;
37+
border-radius: .5em;
38+
font-family: inherit;
39+
background: transparent;
40+
cursor: pointer;
41+
transition: transform .2s ease;
42+
transform-origin: bottom center;
43+
}
44+
45+
${__} .toolbar-tool:hover {
46+
transform: scale(1.25);
47+
}
48+
49+
${__} .toolbar-tool:active {
50+
transform: scale(1.1);
51+
}
52+
53+
${__} .toolbar-tool-icon {
54+
font-size: 1.6em;
55+
line-height: 1;
56+
}
57+
58+
${__} .toolbar-tool-label {
59+
font-size: 1em;
60+
color: #444;
61+
}
62+
</style>
63+
64+
${tools.map(tool => `
65+
<button
66+
class="toolbar-tool"
67+
data-tool="${tool.name}"
68+
type="button"
69+
>
70+
<span class="toolbar-tool-icon">${tool.icon}</span>
71+
<span class="toolbar-tool-label">${tool.label}</span>
72+
</button>
73+
`).join('')}
74+
</div>
75+
`)
76+
}
77+
78+
const toolbarPanel = ({ tools }) => {
79+
const html = template({ tools })
80+
const $el = createDOMNodeFromHTML(html)
81+
82+
tools.forEach(tool => {
83+
const $btn = $el.querySelector(
84+
`[data-tool="${tool.name}"]`
85+
)
86+
$btn.addEventListener('click', () => {
87+
tool.action()
88+
})
89+
})
90+
91+
return {
92+
$el
93+
}
94+
}
95+
96+
export default toolbarPanel

src/cms/server/public/app/editProject.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import api from '../api.js'
2-
import { setIframeSrc } from './common.js'
2+
import { query, setIframeSrc } from './common.js'
33
import dialog from './components/dialog.js'
4-
import selectContentTypesForm from './components/selectContentTypesForm.js'
4+
import toolbarPanel from './components/toolbarPanel.js'
5+
6+
const initToolbar = () => {
7+
const { $el: $toolbar } = toolbarPanel({
8+
tools: [
9+
{
10+
name: 'hello',
11+
icon: '👋',
12+
label: 'Hello',
13+
action: () => {
14+
dialog
15+
.html('<p>Hello from the toolbar!</p>')
16+
.show()
17+
}
18+
}
19+
]
20+
})
21+
const container = query('#panels-container')
22+
container.prepend($toolbar)
23+
}
524

625
const editProject = async ({ ssgOptions }) => {
726
console.log('starting editor with ssgOptions', ssgOptions)
827
await api.ssg.watch(ssgOptions)
928
setIframeSrc()
29+
initToolbar()
1030

1131
const contentTypes = await api.contentTypes.get()
1232
if (contentTypes.length) {

0 commit comments

Comments
 (0)