verso-cms 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +288 -2
- package/bin/verso.js +16 -0
- package/editor/door.js +139 -0
- package/editor/editor.js +1880 -0
- package/editor/elevate.css +2158 -0
- package/editor/ext-attrs.js +87 -0
- package/editor/ext-blocks.js +422 -0
- package/editor/ext-builder.js +819 -0
- package/editor/ext-channels.js +574 -0
- package/editor/ext-components.js +160 -0
- package/editor/ext-history.js +242 -0
- package/editor/ext-palette.js +307 -0
- package/editor/ext-people.js +345 -0
- package/editor/ext-repeaters.js +143 -0
- package/editor/ext-screens.js +94 -0
- package/editor/ext-site-settings.js +318 -0
- package/editor/ext-site-view.js +632 -0
- package/editor/ext-tokens.js +1166 -0
- package/editor/new-page.js +252 -0
- package/editor/rich.js +23 -0
- package/package.json +69 -4
- package/src/blocks.js +269 -0
- package/src/box.js +339 -0
- package/src/build.js +210 -0
- package/src/cli.js +161 -0
- package/src/color.js +137 -0
- package/src/components.js +223 -0
- package/src/elevate.js +74 -0
- package/src/errors.js +46 -0
- package/src/extensions/README.md +70 -0
- package/src/extensions/blocks.js +294 -0
- package/src/extensions/builder.js +734 -0
- package/src/extensions/channels.js +341 -0
- package/src/extensions/components.js +141 -0
- package/src/extensions/history.js +169 -0
- package/src/extensions/palette.js +31 -0
- package/src/extensions/people.js +113 -0
- package/src/extensions/site-settings.js +251 -0
- package/src/extensions/site-view.js +381 -0
- package/src/extensions/tags.js +15 -0
- package/src/extensions/tokens.js +587 -0
- package/src/fonts.js +139 -0
- package/src/git.js +419 -0
- package/src/html-edit.js +942 -0
- package/src/images.js +116 -0
- package/src/people.js +286 -0
- package/src/rich.js +124 -0
- package/src/scaffold.js +185 -0
- package/src/screens.js +65 -0
- package/src/server.js +693 -0
- package/src/site.js +618 -0
- package/src/template.js +915 -0
- package/src/video.js +29 -0
- package/starter/Dockerfile +20 -0
- package/starter/README.md +49 -0
- package/starter/blocks/call-to-action.html +6 -0
- package/starter/blocks/features.html +15 -0
- package/starter/blocks/hero.html +8 -0
- package/starter/blocks/image-and-text.html +8 -0
- package/starter/blocks/latest-posts.html +20 -0
- package/starter/blocks/post-index.html +30 -0
- package/starter/blocks/quote.html +9 -0
- package/starter/blocks/site-footer.html +8 -0
- package/starter/blocks/site-nav.html +8 -0
- package/starter/blocks/tag-list.html +10 -0
- package/starter/blocks/tagged-posts.html +19 -0
- package/starter/blocks/team.html +15 -0
- package/starter/blocks/text.html +4 -0
- package/starter/content/404/page.json +21 -0
- package/starter/content/about/page.json +65 -0
- package/starter/content/journal/designing-for-the-morning-shift/body.html +22 -0
- package/starter/content/journal/designing-for-the-morning-shift/checklist.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/cover.jpg +0 -0
- package/starter/content/journal/designing-for-the-morning-shift/page.json +17 -0
- package/starter/content/journal/page.json +19 -0
- package/starter/content/journal/the-first-week/body.html +17 -0
- package/starter/content/journal/the-first-week/cover.jpg +0 -0
- package/starter/content/journal/the-first-week/page.json +17 -0
- package/starter/content/journal/the-first-week/wall-of-cards.jpg +0 -0
- package/starter/content/page.json +94 -0
- package/starter/content/site.json +88 -0
- package/starter/content/studio-table.jpg +0 -0
- package/starter/content/topics/page.json +27 -0
- package/starter/dockerignore +5 -0
- package/starter/elevate/config/design.ts +30 -0
- package/starter/elevate/config/elevate.ts +16 -0
- package/starter/elevate/config/syntax.ts +6 -0
- package/starter/elevate/design/site.ts +9 -0
- package/starter/elevate/design/tokens.json +20 -0
- package/starter/elevate/extend/fonts.css +1 -0
- package/starter/gitignore +6 -0
- package/starter/netlify.toml +5 -0
- package/starter/parts/.gitkeep +0 -0
- package/starter/public/favicon.svg +1 -0
- package/starter/templates/page.html +20 -0
- package/starter/templates/page.layout.json +23 -0
- package/starter/templates/post.html +65 -0
- package/starter/verso.defaults.json +33 -0
- package/starter-blank/blocks/site-footer.html +3 -0
- package/starter-blank/blocks/site-nav.html +8 -0
- package/starter-blank/blocks/text.html +4 -0
- package/starter-blank/content/404/page.json +18 -0
- package/starter-blank/content/page.json +18 -0
- package/starter-blank/content/site.json +36 -0
- package/starter-blank/elevate/design/tokens.json +10 -0
- package/starter-blank/templates/page.html +17 -0
- package/starter-blank/verso.defaults.json +26 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Editor extensions
|
|
2
|
+
|
|
3
|
+
Every part of the editor beyond editing words — page types, channels, the
|
|
4
|
+
builder, design, history, site view — is an extension. Each one is two files:
|
|
5
|
+
a server half here, and an editor half in `editor/ext-<name>.js`.
|
|
6
|
+
|
|
7
|
+
## The server half
|
|
8
|
+
|
|
9
|
+
Each `.js` file here is loaded when `verso edit` starts, in name order. Export:
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
export async function setup(ctx) {
|
|
13
|
+
return {
|
|
14
|
+
// Answer a request. Return true if you did.
|
|
15
|
+
async handle(req, res, url) { … },
|
|
16
|
+
// Extra properties merged into the page's editor data (verso.data).
|
|
17
|
+
editorData(site, page, template) { return {}; },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`ctx` has:
|
|
23
|
+
|
|
24
|
+
| Name | What it is |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `root` | The site folder |
|
|
27
|
+
| `broadcast(event)` | Tell every open editor tab (e.g. `{ type: 'reload' }`) |
|
|
28
|
+
| `written` | Map of files Verso just wrote, so the file watcher ignores them |
|
|
29
|
+
| `hash(data)` | A short hash, for checking a file hasn't changed under you |
|
|
30
|
+
| `relPath(file)` | A path relative to the site folder |
|
|
31
|
+
| `send(res, status, body, type)`, `sendJSON(res, status, value)` | Answer a request |
|
|
32
|
+
| `readBody(req)`, `readRaw(req)` | Read a request as JSON or as bytes |
|
|
33
|
+
| `compile({ reload })` | Rebuild the CSS |
|
|
34
|
+
| `renderUrl(url)` | The HTML of any page, as the editor shows it |
|
|
35
|
+
| `css`, `cssError` | The current CSS, and the last CSS error |
|
|
36
|
+
| `box` | On a served site (`verso serve`), the box: its `people`, and `rebuild()` for the published pages. `null` when editing on your own computer |
|
|
37
|
+
|
|
38
|
+
On a served site, every request that reaches `handle` is from someone signed in: `req.me` is `{ id, name, email, role, initials }`, and `role` is `'owner'` or `'editor'`. Anything only owners should change belongs in `OWNER_ONLY` in `src/box.js`.
|
|
39
|
+
|
|
40
|
+
Name endpoints `/__verso/<name>/…` so they can't clash with pages. Throw a
|
|
41
|
+
`VersoError` (from `src/errors.js`) and the editor gets
|
|
42
|
+
`{ ok: false, message }` with your message, written for the person using it.
|
|
43
|
+
|
|
44
|
+
## The editor half
|
|
45
|
+
|
|
46
|
+
`editor/ext-<name>.js` exports `init(verso)`, called once on every page. The
|
|
47
|
+
editor's interface lives in a shadow root, so its classes don't touch the
|
|
48
|
+
site. Write class strings as `ui("…")` and run `npm run build:ui` after
|
|
49
|
+
changing them.
|
|
50
|
+
|
|
51
|
+
On `verso`:
|
|
52
|
+
|
|
53
|
+
| Name | What it does |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `data` | The page's editor data, including what `editorData` added |
|
|
56
|
+
| `root` | The editor's shadow root |
|
|
57
|
+
| `menuItems.push({ label, icon, run })` | Adds to the page menu and the command palette |
|
|
58
|
+
| `paletteItems.push({ label, run })` | Adds to the command palette only. Create it first: `verso.paletteItems = verso.paletteItems \|\| []` |
|
|
59
|
+
| `addBarButton({ id, title, svg, onClick })` | Adds a bar button on wider screens; on phones it's in the page menu |
|
|
60
|
+
| `settingsSections.push({ html, bind })` | Adds a section to Page settings: `html()` returns markup, `bind(panel)` wires it up |
|
|
61
|
+
| `openSettings()` | Opens Page settings |
|
|
62
|
+
| `toast(message, kind, action)` | Shows a message. `kind` is `'ok'` or `'error'`; `action` is `{ label, run }` |
|
|
63
|
+
| `queue(changes)`, `flush()` | Save field changes: queue them, then save now (returns a promise) |
|
|
64
|
+
| `reload()` | Reload the page when it's safe to |
|
|
65
|
+
| `refreshStatus()` | Redraw the bar's save and problem status |
|
|
66
|
+
| `flash(el)` | Outline an element for a moment, to show where something landed |
|
|
67
|
+
| `ui`, `icon`, `esc`, `show`, `hide`, `values` | Small helpers the built-in extensions use |
|
|
68
|
+
|
|
69
|
+
Extensions can also offer things to each other: the builder sets
|
|
70
|
+
`verso.builder.open(name)`, which opens a block in the builder.
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Blocks in the editor: live previews for the library, and adding, moving,
|
|
2
|
+
// copying and removing block instances on a page.
|
|
3
|
+
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import { loadSite, readPage, writePageAndSite, withSiteBlocks, dirForUrl, PAGE_FILE } from '../site.js';
|
|
7
|
+
import { renderPage, humanize } from '../template.js';
|
|
8
|
+
import { imageSize } from '../images.js';
|
|
9
|
+
import { VersoError } from '../errors.js';
|
|
10
|
+
import { blockSurround, newId, findBlock, slotList, cloneWithNewIds, walkBlocks, allows } from '../blocks.js';
|
|
11
|
+
import { findPart, findNode, nextKey, topBlocks, writeComponent, walkTree } from '../components.js';
|
|
12
|
+
|
|
13
|
+
export async function setup(ctx) {
|
|
14
|
+
const { root, written, hash, send, sendJSON, readBody, broadcast } = ctx;
|
|
15
|
+
|
|
16
|
+
const openPage = (url) => {
|
|
17
|
+
const dir = dirForUrl(root, String(url || ''));
|
|
18
|
+
if (!dir || !fs.existsSync(path.join(dir, PAGE_FILE))) throw new VersoError('That page no longer exists.');
|
|
19
|
+
const site = loadSite(root);
|
|
20
|
+
// Blocks in site slots ({{ site.header:blocks }}) are worked on with the
|
|
21
|
+
// page's own, and go back to site.json when saved.
|
|
22
|
+
const own = readPage(root, dir);
|
|
23
|
+
const template = site.templates.get(own.template);
|
|
24
|
+
const page = withSiteBlocks(own, site.siteData, template);
|
|
25
|
+
if (!template) throw new VersoError('This page\'s template can\'t be read right now.');
|
|
26
|
+
return { site, page, template };
|
|
27
|
+
};
|
|
28
|
+
const save = (page) => {
|
|
29
|
+
const files = writePageAndSite(root, page);
|
|
30
|
+
Object.entries(files).forEach(([rel, text]) => written.set(rel, hash(Buffer.from(text, 'utf8'))));
|
|
31
|
+
};
|
|
32
|
+
const saveComponent = (comp) => {
|
|
33
|
+
const out = writeComponent(root, comp);
|
|
34
|
+
written.set(out.rel, hash(Buffer.from(out.text, 'utf8')));
|
|
35
|
+
};
|
|
36
|
+
const label = (inst, site) => {
|
|
37
|
+
if (inst.component) return `${site.blocks.components?.get(inst.component)?.label || humanize(inst.component)} component`;
|
|
38
|
+
// An item of a {{ list }}: "an item from Team".
|
|
39
|
+
const of = site.blocks.get(inst.block)?.repeaterOf;
|
|
40
|
+
return of ? `an item from ${of.label}` : humanize(inst.block);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// A slot inside a component's block ("<instance>-<key>:<slot>") belongs to
|
|
44
|
+
// the component: changing it changes every page that uses it.
|
|
45
|
+
const componentSlot = (page, site, target) => {
|
|
46
|
+
const [owner, slot] = String(target || '').split(':');
|
|
47
|
+
const part = owner && findPart(page, owner, site.blocks.components);
|
|
48
|
+
if (!part) return null;
|
|
49
|
+
const field = site.blocks.get(part.node.block)?.fields.get(slot);
|
|
50
|
+
if (!field || field.type !== 'blocks') throw new VersoError('That place on the page no longer takes blocks.');
|
|
51
|
+
part.node.blocks = part.node.blocks || {};
|
|
52
|
+
part.node.blocks[slot] = Array.isArray(part.node.blocks[slot]) ? part.node.blocks[slot] : [];
|
|
53
|
+
return { part, field, list: part.node.blocks[slot] };
|
|
54
|
+
};
|
|
55
|
+
const checkAllowed = (field, names) => {
|
|
56
|
+
names.forEach((name) => {
|
|
57
|
+
if (!allows(field, name)) throw new VersoError(`${humanize(name)} can't go there. That spot takes ${field.tools.map(humanize).join(', ')}.`);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
const slotFieldFor = (page, template, site, target) => {
|
|
61
|
+
const [owner, slot] = String(target || '').split(':');
|
|
62
|
+
if (!owner) return template.fields.get(slot);
|
|
63
|
+
const found = findBlock(page, owner);
|
|
64
|
+
return found ? site.blocks.get(found.inst.block)?.fields.get(slot) : null;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// A block on its own, with placeholders, inside the site's first template's
|
|
68
|
+
// <head>, so the library shows it with the site's real CSS and fonts.
|
|
69
|
+
const preview = (name) => {
|
|
70
|
+
const site = loadSite(root);
|
|
71
|
+
const block = site.blocks.get(name);
|
|
72
|
+
if (!block) return null;
|
|
73
|
+
const host = [...site.templates.values()][0];
|
|
74
|
+
const head = host ? host.source.slice(0, host.headEnd) : '<!DOCTYPE html><html><head><meta charset="utf-8">';
|
|
75
|
+
const css = host && host.linksElevate ? '' : '<link rel="stylesheet" href="/elevate.css">';
|
|
76
|
+
const page = { url: '/', dir: root, label: 'Preview', title: '', description: '', date: '', fields: {}, rich: {}, blocks: {} };
|
|
77
|
+
const inst = { id: 'preview', block: name, fields: {}, blocks: {} };
|
|
78
|
+
// Show one example of the first allowed block in each nested slot.
|
|
79
|
+
block.fields.forEach((f) => {
|
|
80
|
+
if (f.type !== 'blocks') return;
|
|
81
|
+
const first = f.tools[0] || [...site.blocks.keys()].find((n) => n !== name);
|
|
82
|
+
if (first && site.blocks.has(first)) inst.blocks[f.name] = [{ id: 'preview-child', block: first, fields: {}, blocks: {} }];
|
|
83
|
+
});
|
|
84
|
+
const body = renderPage(block, page, { mode: 'editor', instance: inst, prefix: '@preview.', pages: site.pages, rich: site.config.rich, locale: site.config.locale, imageSize, blocks: site.blocks });
|
|
85
|
+
const bodyClass = host ? (/<body[^>]*class="([^"]*)"/.exec(host.source)?.[1] || '') : '';
|
|
86
|
+
const around = blockSurround(site, name);
|
|
87
|
+
return `${head}${css}<style>body{margin:0}</style></head><body class="${bodyClass}">${around.open}${body}${around.close}</body></html>`;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const routes = {
|
|
91
|
+
// { url, slot: "<owner>:<slot>", index, block }
|
|
92
|
+
insert(body) {
|
|
93
|
+
const { site, page, template } = openPage(body.url);
|
|
94
|
+
if (body.component) {
|
|
95
|
+
const comp = site.blocks.components?.get(String(body.component));
|
|
96
|
+
if (!comp) throw new VersoError(`There's no component called "${body.component}".`);
|
|
97
|
+
if (componentSlot(page, site, body.slot)) throw new VersoError('A component can\'t go inside another component.');
|
|
98
|
+
const list = slotList(page, template, site.blocks, body.slot, null);
|
|
99
|
+
checkAllowed(slotFieldFor(page, template, site, body.slot), topBlocks(comp));
|
|
100
|
+
const inst = { id: newId(), component: comp.name, fields: {} };
|
|
101
|
+
list.splice(clampIndex(body.index, list.length), 0, inst);
|
|
102
|
+
save(page);
|
|
103
|
+
return { id: inst.id };
|
|
104
|
+
}
|
|
105
|
+
const name = String(body.block || '');
|
|
106
|
+
if (!site.blocks.has(name)) throw new VersoError(`There's no block called "${name}".`);
|
|
107
|
+
const inner = componentSlot(page, site, body.slot);
|
|
108
|
+
if (inner) {
|
|
109
|
+
checkAllowed(inner.field, [name]);
|
|
110
|
+
const node = { block: name, key: nextKey(inner.part.comp) };
|
|
111
|
+
inner.list.splice(clampIndex(body.index, inner.list.length), 0, node);
|
|
112
|
+
saveComponent(inner.part.comp);
|
|
113
|
+
return { component: inner.part.comp.label };
|
|
114
|
+
}
|
|
115
|
+
const list = slotList(page, template, site.blocks, body.slot, name);
|
|
116
|
+
const inst = { id: newId(), block: name, fields: {} };
|
|
117
|
+
list.splice(clampIndex(body.index, list.length), 0, inst);
|
|
118
|
+
save(page);
|
|
119
|
+
return { id: inst.id };
|
|
120
|
+
},
|
|
121
|
+
// Puts back a block or component removed a moment ago: { url, slot, index, inst }
|
|
122
|
+
restore(body) {
|
|
123
|
+
const { site, page, template } = openPage(body.url);
|
|
124
|
+
const inst = body.inst;
|
|
125
|
+
if (!inst || typeof inst !== 'object' || (typeof inst.block !== 'string' && typeof inst.component !== 'string')) throw new VersoError('There\'s nothing to put back.');
|
|
126
|
+
if (body.componentName) {
|
|
127
|
+
const comp = site.blocks.components?.get(String(body.componentName));
|
|
128
|
+
const parent = comp && (body.parentKey ? findNode(comp, String(body.parentKey))?.node : null);
|
|
129
|
+
if (!comp || !parent) throw new VersoError('That component has changed, so the block can\'t be put back.');
|
|
130
|
+
parent.blocks = parent.blocks || {};
|
|
131
|
+
const list = (parent.blocks[body.slotName] = parent.blocks[body.slotName] || []);
|
|
132
|
+
list.splice(clampIndex(body.index, list.length), 0, inst);
|
|
133
|
+
saveComponent(comp);
|
|
134
|
+
return {};
|
|
135
|
+
}
|
|
136
|
+
if (findBlock(page, inst.id)) throw new VersoError('That block is already back on the page.');
|
|
137
|
+
const list = slotList(page, template, site.blocks, body.slot, inst.block || null);
|
|
138
|
+
if (inst.component) {
|
|
139
|
+
const comp = site.blocks.components?.get(inst.component);
|
|
140
|
+
if (comp) checkAllowed(slotFieldFor(page, template, site, body.slot), topBlocks(comp));
|
|
141
|
+
}
|
|
142
|
+
list.splice(clampIndex(body.index, list.length), 0, inst);
|
|
143
|
+
save(page);
|
|
144
|
+
return { id: inst.id };
|
|
145
|
+
},
|
|
146
|
+
// { url, id, slot, index }: index is where it goes in the target list as it is now
|
|
147
|
+
move(body) {
|
|
148
|
+
const { site, page, template } = openPage(body.url);
|
|
149
|
+
const part = !findBlock(page, String(body.id || '')) && findPart(page, String(body.id || ''), site.blocks.components);
|
|
150
|
+
if (part) {
|
|
151
|
+
const inner = componentSlot(page, site, body.slot);
|
|
152
|
+
if (!inner || inner.part.comp !== part.comp) throw new VersoError('Blocks in a component can only move within that component.');
|
|
153
|
+
let inside = inner.part.key === part.key;
|
|
154
|
+
walkTree([part.node], (n) => { if (n.key === inner.part.key) inside = true; });
|
|
155
|
+
if (inside) throw new VersoError('A block can\'t go inside itself.');
|
|
156
|
+
checkAllowed(inner.field, [part.node.block]);
|
|
157
|
+
let index = clampIndex(body.index, inner.list.length);
|
|
158
|
+
if (inner.list === part.list && part.index < index) index -= 1;
|
|
159
|
+
part.list.splice(part.index, 1);
|
|
160
|
+
inner.list.splice(Math.min(index, inner.list.length), 0, part.node);
|
|
161
|
+
saveComponent(part.comp);
|
|
162
|
+
return { component: part.comp.label };
|
|
163
|
+
}
|
|
164
|
+
const found = findBlock(page, String(body.id || ''));
|
|
165
|
+
if (!found) throw new VersoError('That block is no longer on the page.');
|
|
166
|
+
if (found.inst.component) {
|
|
167
|
+
if (componentSlot(page, site, body.slot)) throw new VersoError('A component can\'t go inside another component.');
|
|
168
|
+
const comp = site.blocks.components?.get(found.inst.component);
|
|
169
|
+
const target = slotList(page, template, site.blocks, body.slot, null);
|
|
170
|
+
if (comp) checkAllowed(slotFieldFor(page, template, site, body.slot), topBlocks(comp));
|
|
171
|
+
let index = clampIndex(body.index, target.length);
|
|
172
|
+
if (target === found.list && found.index < index) index -= 1;
|
|
173
|
+
found.list.splice(found.index, 1);
|
|
174
|
+
target.splice(Math.min(index, target.length), 0, found.inst);
|
|
175
|
+
save(page);
|
|
176
|
+
return { id: found.inst.id };
|
|
177
|
+
}
|
|
178
|
+
const [owner] = String(body.slot || '').split(':');
|
|
179
|
+
let inside = owner === found.inst.id;
|
|
180
|
+
walkBlocks(found.inst.blocks, (b) => { if (b.id === owner) inside = true; });
|
|
181
|
+
if (inside) throw new VersoError('A block can\'t go inside itself.');
|
|
182
|
+
const target = slotList(page, template, site.blocks, body.slot, found.inst.block);
|
|
183
|
+
let index = clampIndex(body.index, target.length);
|
|
184
|
+
if (target === found.list && found.index < index) index -= 1;
|
|
185
|
+
found.list.splice(found.index, 1);
|
|
186
|
+
target.splice(Math.min(index, target.length), 0, found.inst);
|
|
187
|
+
save(page);
|
|
188
|
+
return { id: found.inst.id };
|
|
189
|
+
},
|
|
190
|
+
// { url, id, by: -1 | 1 }
|
|
191
|
+
step(body) {
|
|
192
|
+
const { site, page } = openPage(body.url);
|
|
193
|
+
const part = !findBlock(page, String(body.id || '')) && findPart(page, String(body.id || ''), site.blocks.components);
|
|
194
|
+
if (part) {
|
|
195
|
+
const to = part.index + (Number(body.by) < 0 ? -1 : 1);
|
|
196
|
+
if (to < 0 || to >= part.list.length) return {};
|
|
197
|
+
part.list.splice(part.index, 1);
|
|
198
|
+
part.list.splice(to, 0, part.node);
|
|
199
|
+
saveComponent(part.comp);
|
|
200
|
+
return { component: part.comp.label };
|
|
201
|
+
}
|
|
202
|
+
const found = findBlock(page, String(body.id || ''));
|
|
203
|
+
if (!found) throw new VersoError('That block is no longer on the page.');
|
|
204
|
+
const to = found.index + (Number(body.by) < 0 ? -1 : 1);
|
|
205
|
+
if (to < 0 || to >= found.list.length) return { id: found.inst.id };
|
|
206
|
+
found.list.splice(found.index, 1);
|
|
207
|
+
found.list.splice(to, 0, found.inst);
|
|
208
|
+
save(page);
|
|
209
|
+
return { id: found.inst.id };
|
|
210
|
+
},
|
|
211
|
+
duplicate(body) {
|
|
212
|
+
const { site, page } = openPage(body.url);
|
|
213
|
+
const part = !findBlock(page, String(body.id || '')) && findPart(page, String(body.id || ''), site.blocks.components);
|
|
214
|
+
if (part) {
|
|
215
|
+
const copy = JSON.parse(JSON.stringify(part.node));
|
|
216
|
+
let n = Number(nextKey(part.comp).slice(1));
|
|
217
|
+
const renamed = {};
|
|
218
|
+
walkTree([copy], (node) => { renamed[node.key] = `k${n}`; node.key = `k${n}`; n += 1; });
|
|
219
|
+
// The copy starts with the same defaults and shared content.
|
|
220
|
+
['defaults', 'shared'].forEach((bag) => Object.entries(part.comp[bag] || {}).forEach(([k, v]) => {
|
|
221
|
+
const [key, field] = [k.slice(0, k.indexOf('.')), k.slice(k.indexOf('.') + 1)];
|
|
222
|
+
if (renamed[key]) part.comp[bag][`${renamed[key]}.${field}`] = v;
|
|
223
|
+
}));
|
|
224
|
+
part.list.splice(part.index + 1, 0, copy);
|
|
225
|
+
saveComponent(part.comp);
|
|
226
|
+
return { component: part.comp.label };
|
|
227
|
+
}
|
|
228
|
+
const found = findBlock(page, String(body.id || ''));
|
|
229
|
+
if (!found) throw new VersoError('That block is no longer on the page.');
|
|
230
|
+
const copy = cloneWithNewIds(found.inst);
|
|
231
|
+
found.list.splice(found.index + 1, 0, copy);
|
|
232
|
+
save(page);
|
|
233
|
+
return { id: copy.id };
|
|
234
|
+
},
|
|
235
|
+
remove(body) {
|
|
236
|
+
const { site, page } = openPage(body.url);
|
|
237
|
+
const part = !findBlock(page, String(body.id || '')) && findPart(page, String(body.id || ''), site.blocks.components);
|
|
238
|
+
if (part) {
|
|
239
|
+
if (!part.parent) throw new VersoError('That\'s the outside of the component. To take it off this page, remove the component itself.');
|
|
240
|
+
part.list.splice(part.index, 1);
|
|
241
|
+
saveComponent(part.comp);
|
|
242
|
+
return {
|
|
243
|
+
removed: humanize(part.node.block), component: part.comp.label,
|
|
244
|
+
restore: { componentName: part.comp.name, parentKey: part.parent.key, slotName: part.slot, index: part.index, inst: part.node },
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
const found = findBlock(page, String(body.id || ''));
|
|
248
|
+
if (!found) throw new VersoError('That block is no longer on the page.');
|
|
249
|
+
found.list.splice(found.index, 1);
|
|
250
|
+
prune(page.blocks);
|
|
251
|
+
save(page);
|
|
252
|
+
return { removed: label(found.inst, site), restore: { slot: `${found.owner}:${found.slot}`, index: found.index, inst: found.inst } };
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
return {
|
|
257
|
+
async handle(req, res, url) {
|
|
258
|
+
const p = url.pathname;
|
|
259
|
+
if (p.startsWith('/__verso/block-preview/') && req.method === 'GET') {
|
|
260
|
+
const html = preview(decodeURIComponent(p.slice('/__verso/block-preview/'.length).replace(/\/$/, '')));
|
|
261
|
+
if (html) send(res, 200, html);
|
|
262
|
+
else send(res, 404, 'Not found', 'text/plain');
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
const m = /^\/__verso\/blocks\/(insert|move|step|duplicate|remove|restore)$/.exec(p);
|
|
266
|
+
if (!m || req.method !== 'POST') return false;
|
|
267
|
+
try {
|
|
268
|
+
const out = routes[m[1]](await readBody(req));
|
|
269
|
+
broadcast({ type: 'status' });
|
|
270
|
+
sendJSON(res, 200, { ok: true, ...out });
|
|
271
|
+
} catch (err) {
|
|
272
|
+
if (!(err instanceof VersoError)) throw err;
|
|
273
|
+
sendJSON(res, 400, { ok: false, message: err.message });
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
},
|
|
277
|
+
editorData(site) {
|
|
278
|
+
return { blockTypes: [...site.blocks.entries()].map(([name, b]) => ({ name, label: b?.repeaterOf ? b.repeaterOf.label : humanize(name), repeater: !!b?.repeaterOf })) };
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const clampIndex = (i, length) => {
|
|
284
|
+
const n = Number.isInteger(Number(i)) ? Number(i) : length;
|
|
285
|
+
return Math.max(0, Math.min(n, length));
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// Empty slot lists are dropped, so page.json stays tidy.
|
|
289
|
+
function prune(blocks) {
|
|
290
|
+
Object.keys(blocks || {}).forEach((slot) => {
|
|
291
|
+
if (!Array.isArray(blocks[slot]) || !blocks[slot].length) delete blocks[slot];
|
|
292
|
+
else blocks[slot].forEach((b) => { if (b.blocks) { prune(b.blocks); if (!Object.keys(b.blocks).length) delete b.blocks; } });
|
|
293
|
+
});
|
|
294
|
+
}
|