@velocity-astro/cms-admin 0.1.0-beta.1 → 0.1.0-beta.11
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/app-dist/assets/{RichTextEditor-Ci93Tlgw.js → RichTextEditor-UA2kIIOw.js} +1 -1
- package/app-dist/assets/index-DkDHhCG6.css +2 -0
- package/app-dist/assets/index-DovlPZTn.js +80 -0
- package/app-dist/index.html +16 -2
- package/dist/app/AdminApp.d.ts.map +1 -1
- package/dist/app/AdminApp.js +4 -1
- package/dist/app/AdminApp.js.map +1 -1
- package/dist/app/AdminLayout.d.ts.map +1 -1
- package/dist/app/AdminLayout.js +108 -53
- package/dist/app/AdminLayout.js.map +1 -1
- package/dist/app/RequestState.d.ts.map +1 -1
- package/dist/app/RequestState.js +4 -0
- package/dist/app/RequestState.js.map +1 -1
- package/dist/app/RouteError.d.ts +9 -0
- package/dist/app/RouteError.d.ts.map +1 -0
- package/dist/app/RouteError.js +16 -0
- package/dist/app/RouteError.js.map +1 -0
- package/dist/app/paths.d.ts +9 -0
- package/dist/app/paths.d.ts.map +1 -1
- package/dist/app/paths.js +8 -0
- package/dist/app/paths.js.map +1 -1
- package/dist/app/routes.d.ts.map +1 -1
- package/dist/app/routes.js +32 -17
- package/dist/app/routes.js.map +1 -1
- package/dist/client/admin-client.d.ts +22 -1
- package/dist/client/admin-client.d.ts.map +1 -1
- package/dist/client/http-client.d.ts +12 -1
- package/dist/client/http-client.d.ts.map +1 -1
- package/dist/client/http-client.js +33 -0
- package/dist/client/http-client.js.map +1 -1
- package/dist/client/queries.d.ts +20 -1
- package/dist/client/queries.d.ts.map +1 -1
- package/dist/client/queries.js +115 -7
- package/dist/client/queries.js.map +1 -1
- package/dist/fields/BlocksControl.d.ts +6 -0
- package/dist/fields/BlocksControl.d.ts.map +1 -1
- package/dist/fields/BlocksControl.js +353 -31
- package/dist/fields/BlocksControl.js.map +1 -1
- package/dist/fields/FieldGrid.d.ts +14 -2
- package/dist/fields/FieldGrid.d.ts.map +1 -1
- package/dist/fields/FieldGrid.js +16 -10
- package/dist/fields/FieldGrid.js.map +1 -1
- package/dist/fields/FieldSetEditor.js +1 -1
- package/dist/fields/FieldSetEditor.js.map +1 -1
- package/dist/fields/SiteImagePicker.d.ts +22 -0
- package/dist/fields/SiteImagePicker.d.ts.map +1 -0
- package/dist/fields/SiteImagePicker.js +42 -0
- package/dist/fields/SiteImagePicker.js.map +1 -0
- package/dist/fields/block-icons.d.ts.map +1 -1
- package/dist/fields/block-icons.js +3 -1
- package/dist/fields/block-icons.js.map +1 -1
- package/dist/fields/carry.d.ts +19 -0
- package/dist/fields/carry.d.ts.map +1 -0
- package/dist/fields/carry.js +61 -0
- package/dist/fields/carry.js.map +1 -0
- package/dist/fields/controls.d.ts.map +1 -1
- package/dist/fields/controls.js +117 -46
- package/dist/fields/controls.js.map +1 -1
- package/dist/fields/field-control.d.ts +98 -0
- package/dist/fields/field-control.d.ts.map +1 -1
- package/dist/fields/field-control.js.map +1 -1
- package/dist/fields/page-focus.d.ts +61 -2
- package/dist/fields/page-focus.d.ts.map +1 -1
- package/dist/fields/page-focus.js +92 -2
- package/dist/fields/page-focus.js.map +1 -1
- package/dist/fields/settings.d.ts.map +1 -1
- package/dist/fields/settings.js +7 -0
- package/dist/fields/settings.js.map +1 -1
- package/dist/lib/boot.d.ts +25 -0
- package/dist/lib/boot.d.ts.map +1 -0
- package/dist/lib/boot.js +87 -0
- package/dist/lib/boot.js.map +1 -0
- package/dist/lib/load-fresh.d.ts +8 -0
- package/dist/lib/load-fresh.d.ts.map +1 -0
- package/dist/lib/load-fresh.js +51 -0
- package/dist/lib/load-fresh.js.map +1 -0
- package/dist/lib/open-site.d.ts +6 -0
- package/dist/lib/open-site.d.ts.map +1 -0
- package/dist/lib/open-site.js +8 -0
- package/dist/lib/open-site.js.map +1 -0
- package/dist/lib/prepare-image.d.ts.map +1 -1
- package/dist/lib/prepare-image.js +1 -3
- package/dist/lib/prepare-image.js.map +1 -1
- package/dist/lib/rich-from-html.d.ts +9 -0
- package/dist/lib/rich-from-html.d.ts.map +1 -0
- package/dist/lib/rich-from-html.js +129 -0
- package/dist/lib/rich-from-html.js.map +1 -0
- package/dist/lib/sittings.d.ts +16 -0
- package/dist/lib/sittings.d.ts.map +1 -0
- package/dist/lib/sittings.js +29 -0
- package/dist/lib/sittings.js.map +1 -0
- package/dist/lib/small-image.d.ts +7 -0
- package/dist/lib/small-image.d.ts.map +1 -0
- package/dist/lib/small-image.js +6 -0
- package/dist/lib/small-image.js.map +1 -0
- package/dist/lib/use-command-key.d.ts +16 -0
- package/dist/lib/use-command-key.d.ts.map +1 -0
- package/dist/lib/use-command-key.js +30 -0
- package/dist/lib/use-command-key.js.map +1 -0
- package/dist/lib/use-escapes.d.ts +7 -0
- package/dist/lib/use-escapes.d.ts.map +1 -0
- package/dist/lib/use-escapes.js +19 -0
- package/dist/lib/use-escapes.js.map +1 -0
- package/dist/lib/use-history.d.ts +24 -0
- package/dist/lib/use-history.d.ts.map +1 -0
- package/dist/lib/use-history.js +48 -0
- package/dist/lib/use-history.js.map +1 -0
- package/dist/lib/use-narrow.d.ts +5 -0
- package/dist/lib/use-narrow.d.ts.map +1 -0
- package/dist/lib/use-narrow.js +19 -0
- package/dist/lib/use-narrow.js.map +1 -0
- package/dist/lib/use-panel-sizes.d.ts +7 -0
- package/dist/lib/use-panel-sizes.d.ts.map +1 -0
- package/dist/lib/use-panel-sizes.js +33 -0
- package/dist/lib/use-panel-sizes.js.map +1 -0
- package/dist/lib/use-save-key.d.ts +15 -0
- package/dist/lib/use-save-key.d.ts.map +1 -0
- package/dist/lib/use-save-key.js +27 -0
- package/dist/lib/use-save-key.js.map +1 -0
- package/dist/lib/use-undo-keys.d.ts +14 -0
- package/dist/lib/use-undo-keys.d.ts.map +1 -0
- package/dist/lib/use-undo-keys.js +46 -0
- package/dist/lib/use-undo-keys.js.map +1 -0
- package/dist/messages/en.d.ts +269 -3
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +242 -3
- package/dist/messages/en.js.map +1 -1
- package/dist/page-bridge/index.d.ts +7 -0
- package/dist/page-bridge/index.d.ts.map +1 -0
- package/dist/page-bridge/index.js +7 -0
- package/dist/page-bridge/index.js.map +1 -0
- package/dist/page-bridge/preview-bridge.d.ts +35 -0
- package/dist/page-bridge/preview-bridge.d.ts.map +1 -0
- package/dist/page-bridge/preview-bridge.js +1821 -0
- package/dist/page-bridge/preview-bridge.js.map +1 -0
- package/dist/plugins/plugin-editor.d.ts +2 -0
- package/dist/plugins/plugin-editor.d.ts.map +1 -1
- package/dist/plugins/plugin-editor.js.map +1 -1
- package/dist/screens/Contact/ContactDeals.js +1 -1
- package/dist/screens/Contact/ContactDeals.js.map +1 -1
- package/dist/screens/Contact/ContactScreen.js +14 -2
- package/dist/screens/Contact/ContactScreen.js.map +1 -1
- package/dist/screens/Contacts/ContactsScreen.js +16 -10
- package/dist/screens/Contacts/ContactsScreen.js.map +1 -1
- package/dist/screens/Contacts/StagesDialog.d.ts.map +1 -1
- package/dist/screens/Contacts/StagesDialog.js +4 -1
- package/dist/screens/Contacts/StagesDialog.js.map +1 -1
- package/dist/screens/ContentList/ContentListScreen.d.ts.map +1 -1
- package/dist/screens/ContentList/ContentListScreen.js +13 -6
- package/dist/screens/ContentList/ContentListScreen.js.map +1 -1
- package/dist/screens/ContentType/ContentTypeScreen.js +10 -3
- package/dist/screens/ContentType/ContentTypeScreen.js.map +1 -1
- package/dist/screens/ContentTypes/ContentTypesScreen.js +3 -3
- package/dist/screens/ContentTypes/ContentTypesScreen.js.map +1 -1
- package/dist/screens/Deals/DealsScreen.d.ts.map +1 -1
- package/dist/screens/Deals/DealsScreen.js +28 -4
- package/dist/screens/Deals/DealsScreen.js.map +1 -1
- package/dist/screens/EntryEditor/EntryEditorScreen.d.ts.map +1 -1
- package/dist/screens/EntryEditor/EntryEditorScreen.js +8 -3
- package/dist/screens/EntryEditor/EntryEditorScreen.js.map +1 -1
- package/dist/screens/EntryEditor/EntryForm.d.ts +5 -1
- package/dist/screens/EntryEditor/EntryForm.d.ts.map +1 -1
- package/dist/screens/EntryEditor/EntryForm.js +226 -25
- package/dist/screens/EntryEditor/EntryForm.js.map +1 -1
- package/dist/screens/EntryEditor/HistoryDialog.d.ts +2 -1
- package/dist/screens/EntryEditor/HistoryDialog.d.ts.map +1 -1
- package/dist/screens/EntryEditor/HistoryDialog.js +24 -5
- package/dist/screens/EntryEditor/HistoryDialog.js.map +1 -1
- package/dist/screens/EntryEditor/PagePane.d.ts +32 -2
- package/dist/screens/EntryEditor/PagePane.d.ts.map +1 -1
- package/dist/screens/EntryEditor/PagePane.js +63 -6
- package/dist/screens/EntryEditor/PagePane.js.map +1 -1
- package/dist/screens/EntryEditor/SectionOutline.d.ts +11 -0
- package/dist/screens/EntryEditor/SectionOutline.d.ts.map +1 -0
- package/dist/screens/EntryEditor/SectionOutline.js +46 -0
- package/dist/screens/EntryEditor/SectionOutline.js.map +1 -0
- package/dist/screens/EntryEditor/ThemeCard.d.ts +32 -0
- package/dist/screens/EntryEditor/ThemeCard.d.ts.map +1 -0
- package/dist/screens/EntryEditor/ThemeCard.js +71 -0
- package/dist/screens/EntryEditor/ThemeCard.js.map +1 -0
- package/dist/screens/Form/FormScreen.js +10 -2
- package/dist/screens/Form/FormScreen.js.map +1 -1
- package/dist/screens/Form/Submissions.js +1 -1
- package/dist/screens/Form/Submissions.js.map +1 -1
- package/dist/screens/Forms/FormsScreen.d.ts.map +1 -1
- package/dist/screens/Forms/FormsScreen.js +6 -3
- package/dist/screens/Forms/FormsScreen.js.map +1 -1
- package/dist/screens/Home/FollowUps.d.ts.map +1 -1
- package/dist/screens/Home/FollowUps.js +3 -3
- package/dist/screens/Home/FollowUps.js.map +1 -1
- package/dist/screens/Home/HomeScreen.d.ts +1 -1
- package/dist/screens/Home/HomeScreen.d.ts.map +1 -1
- package/dist/screens/Home/HomeScreen.js +111 -22
- package/dist/screens/Home/HomeScreen.js.map +1 -1
- package/dist/screens/Media/MediaScreen.d.ts.map +1 -1
- package/dist/screens/Media/MediaScreen.js +3 -2
- package/dist/screens/Media/MediaScreen.js.map +1 -1
- package/dist/screens/People/PeopleScreen.js +1 -1
- package/dist/screens/People/PeopleScreen.js.map +1 -1
- package/dist/screens/Settings/EmailSettings.d.ts +3 -0
- package/dist/screens/Settings/EmailSettings.d.ts.map +1 -0
- package/dist/screens/Settings/EmailSettings.js +76 -0
- package/dist/screens/Settings/EmailSettings.js.map +1 -0
- package/dist/screens/Settings/FormsSettings.d.ts +3 -0
- package/dist/screens/Settings/FormsSettings.d.ts.map +1 -0
- package/dist/screens/Settings/FormsSettings.js +91 -0
- package/dist/screens/Settings/FormsSettings.js.map +1 -0
- package/dist/screens/Settings/GeneralSettings.d.ts +3 -0
- package/dist/screens/Settings/GeneralSettings.d.ts.map +1 -0
- package/dist/screens/Settings/GeneralSettings.js +42 -0
- package/dist/screens/Settings/GeneralSettings.js.map +1 -0
- package/dist/screens/Settings/SettingsPage.d.ts +9 -0
- package/dist/screens/Settings/SettingsPage.d.ts.map +1 -0
- package/dist/screens/Settings/SettingsPage.js +42 -0
- package/dist/screens/Settings/SettingsPage.js.map +1 -0
- package/dist/screens/Settings/SettingsScreen.d.ts +7 -0
- package/dist/screens/Settings/SettingsScreen.d.ts.map +1 -0
- package/dist/screens/Settings/SettingsScreen.js +53 -0
- package/dist/screens/Settings/SettingsScreen.js.map +1 -0
- package/dist/screens/Settings/StorageSettings.d.ts +3 -0
- package/dist/screens/Settings/StorageSettings.d.ts.map +1 -0
- package/dist/screens/Settings/StorageSettings.js +82 -0
- package/dist/screens/Settings/StorageSettings.js.map +1 -0
- package/dist/screens/Trash/TrashScreen.js +2 -2
- package/dist/screens/Trash/TrashScreen.js.map +1 -1
- package/dist/testing/admin-client-contract.d.ts.map +1 -1
- package/dist/testing/admin-client-contract.js +44 -0
- package/dist/testing/admin-client-contract.js.map +1 -1
- package/dist/testing/http-server.d.ts.map +1 -1
- package/dist/testing/http-server.js +5 -0
- package/dist/testing/http-server.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +1 -1
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/memory-client.d.ts +25 -1
- package/dist/testing/memory-client.d.ts.map +1 -1
- package/dist/testing/memory-client.js +49 -1
- package/dist/testing/memory-client.js.map +1 -1
- package/dist/testing/memory-mail.d.ts +48 -0
- package/dist/testing/memory-mail.d.ts.map +1 -0
- package/dist/testing/memory-mail.js +74 -0
- package/dist/testing/memory-mail.js.map +1 -0
- package/dist/testing/sample-content.d.ts +6 -0
- package/dist/testing/sample-content.d.ts.map +1 -1
- package/dist/testing/sample-content.js +33 -0
- package/dist/testing/sample-content.js.map +1 -1
- package/package.json +9 -5
- package/app-dist/assets/index-CczBKhBz.css +0 -2
- package/app-dist/assets/index-DOIcFJow.js +0 -80
|
@@ -0,0 +1,1821 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the page beside the editor's form runs: the page's side of the conversation with the
|
|
3
|
+
* editor. It is sent to the browser as text (`PREVIEW_BRIDGE_SCRIPT`), so the function must stand
|
|
4
|
+
* alone: its argument only, no imports and nothing from this file.
|
|
5
|
+
*
|
|
6
|
+
* It is in parts, each a few functions that close over the same page:
|
|
7
|
+
*
|
|
8
|
+
* - messaging: what the page and the editor say to each other, and to nobody else
|
|
9
|
+
* - marks: which section, and which field of it, an element of the page belongs to
|
|
10
|
+
* - layer: outlines drawn above the page, so nothing of the template's own markup is changed
|
|
11
|
+
* - tools: a small bar on the section the pointer is in (move, copy, remove), a button at each
|
|
12
|
+
* of its edges that adds a section there, and a button on a picture or a link the editor can
|
|
13
|
+
* change, all in the layer and all said to the editor, which makes the change
|
|
14
|
+
* - events: what a pointer over the page and a click on it mean
|
|
15
|
+
* - typing: words typed straight into a field on the page, said to the editor as they are typed
|
|
16
|
+
* - scroll: the page stays where it was when it is drawn again
|
|
17
|
+
*
|
|
18
|
+
* A template says what is what with two attributes: `data-cms-section` holds a section's id, and
|
|
19
|
+
* `data-cms-field` inside it holds a field's place in the section, as keys (`title`,
|
|
20
|
+
* `items.0.text`). A page with no field marks still works: a section that says what it holds
|
|
21
|
+
* (`data-cms-values`, a list of place and words) has its fields found by what they show, so a
|
|
22
|
+
* template whose blocks hand their words to components of its own needs no mark on each. A mark
|
|
23
|
+
* the template wrote always comes first, and a click that finds no field chooses the section.
|
|
24
|
+
*
|
|
25
|
+
* Words can be typed on the page only where the editor says a field holds plain words and the page
|
|
26
|
+
* shows them exactly as they are held: the one run of text that shows them is changed, and nothing
|
|
27
|
+
* else of the element can be.
|
|
28
|
+
*
|
|
29
|
+
* The layer's look is set from script, which a page's content rules allow where a style written
|
|
30
|
+
* into the page would not be.
|
|
31
|
+
*/
|
|
32
|
+
export function previewBridge(page) {
|
|
33
|
+
if (page.parent === page)
|
|
34
|
+
return;
|
|
35
|
+
const doc = page.document;
|
|
36
|
+
const origin = page.location.origin;
|
|
37
|
+
const SOURCE = 'velocity-cms';
|
|
38
|
+
// Messaging.
|
|
39
|
+
const say = (message) => page.parent.postMessage({ source: SOURCE, ...message }, origin);
|
|
40
|
+
const isElement = (target) => typeof target === 'object' && target !== null && target.nodeType === 1;
|
|
41
|
+
// What a section's fields were found to be by what they show: nothing is written on the page.
|
|
42
|
+
const guessed = new WeakMap();
|
|
43
|
+
const guesses = new WeakMap();
|
|
44
|
+
// The run of text that shows a field's words in an element, where there is exactly such a run.
|
|
45
|
+
const runOf = new WeakMap();
|
|
46
|
+
// Every run that shows a field, by section and field, so all of them follow what is typed in one.
|
|
47
|
+
const shownIn = new WeakMap();
|
|
48
|
+
const squeeze = (text) => (text ?? '').replace(/\s+/g, '');
|
|
49
|
+
// What a section holds itself: a section inside it (a column's) answers for its own.
|
|
50
|
+
const within = (section, selector) => [...section.querySelectorAll(selector)].filter((element) => element.closest('[data-cms-section]') === section);
|
|
51
|
+
const guess = (section) => {
|
|
52
|
+
const known = guesses.get(section);
|
|
53
|
+
if (known)
|
|
54
|
+
return known;
|
|
55
|
+
const first = new Map();
|
|
56
|
+
guesses.set(section, first);
|
|
57
|
+
const shows = new Map();
|
|
58
|
+
shownIn.set(section, shows);
|
|
59
|
+
let values;
|
|
60
|
+
try {
|
|
61
|
+
values = JSON.parse(section.dataset['cmsValues'] ?? '[]');
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return first;
|
|
65
|
+
}
|
|
66
|
+
if (!Array.isArray(values))
|
|
67
|
+
return first;
|
|
68
|
+
// Each element by all it says (the innermost of those that say the same), and by each run of words in it.
|
|
69
|
+
const whole = new Map();
|
|
70
|
+
const runs = new Map();
|
|
71
|
+
const pictures = [];
|
|
72
|
+
const add = (into, text, element) => {
|
|
73
|
+
if (text === '')
|
|
74
|
+
return;
|
|
75
|
+
const list = into.get(text) ?? [];
|
|
76
|
+
const last = list[list.length - 1];
|
|
77
|
+
if (last === element)
|
|
78
|
+
return;
|
|
79
|
+
if (last?.contains(element))
|
|
80
|
+
list[list.length - 1] = element;
|
|
81
|
+
else
|
|
82
|
+
list.push(element);
|
|
83
|
+
into.set(text, list);
|
|
84
|
+
};
|
|
85
|
+
for (const element of within(section, '*')) {
|
|
86
|
+
if (element.closest('script, style, template, noscript'))
|
|
87
|
+
continue;
|
|
88
|
+
if (element.tagName === 'IMG')
|
|
89
|
+
pictures.push(element);
|
|
90
|
+
add(whole, squeeze(element.textContent), element);
|
|
91
|
+
for (const node of element.childNodes)
|
|
92
|
+
if (node.nodeType === 3)
|
|
93
|
+
add(runs, squeeze(node.nodeValue), element);
|
|
94
|
+
}
|
|
95
|
+
// Fields that say the same words share the elements that show them, in order.
|
|
96
|
+
const wanted = new Map();
|
|
97
|
+
const sources = [];
|
|
98
|
+
// Fields held as plain words, by what they say: only those can be typed on the page.
|
|
99
|
+
const plain = new Map();
|
|
100
|
+
for (const value of values) {
|
|
101
|
+
if (!Array.isArray(value) || typeof value[0] !== 'string' || typeof value[1] !== 'string')
|
|
102
|
+
continue;
|
|
103
|
+
const path = value[0];
|
|
104
|
+
if (within(section, '[data-cms-field]').some((marked) => marked.dataset['cmsField'] === path))
|
|
105
|
+
continue;
|
|
106
|
+
let text = value[1];
|
|
107
|
+
if (value[2] === 'html') {
|
|
108
|
+
const holder = doc.createElement('template');
|
|
109
|
+
holder.innerHTML = text;
|
|
110
|
+
text = holder.content.textContent ?? '';
|
|
111
|
+
}
|
|
112
|
+
else if (text.length > 1 && !/\s/.test(text)) {
|
|
113
|
+
sources.push([path, text]);
|
|
114
|
+
}
|
|
115
|
+
const words = squeeze(text);
|
|
116
|
+
if (words !== '')
|
|
117
|
+
wanted.set(words, [...(wanted.get(words) ?? []), path]);
|
|
118
|
+
if (value[2] !== 'html')
|
|
119
|
+
plain.set(path, words);
|
|
120
|
+
}
|
|
121
|
+
const claim = (element, path) => {
|
|
122
|
+
if (!first.has(path))
|
|
123
|
+
first.set(path, element);
|
|
124
|
+
if (guessed.has(element))
|
|
125
|
+
return false;
|
|
126
|
+
guessed.set(element, path);
|
|
127
|
+
return true;
|
|
128
|
+
};
|
|
129
|
+
for (const [words, paths] of wanted) {
|
|
130
|
+
const elements = whole.get(words) ?? runs.get(words) ?? [];
|
|
131
|
+
elements.forEach((element, at) => {
|
|
132
|
+
const path = paths[Math.min(paths.length - 1, Math.floor((at * paths.length) / elements.length))] ?? '';
|
|
133
|
+
if (!claim(element, path) || plain.get(path) !== words)
|
|
134
|
+
return;
|
|
135
|
+
const held = [...element.childNodes].filter((node) => node.nodeType === 3 && squeeze(node.nodeValue) === words);
|
|
136
|
+
const run = held.length === 1 ? held[0] : undefined;
|
|
137
|
+
if (!run)
|
|
138
|
+
return;
|
|
139
|
+
runOf.set(element, run);
|
|
140
|
+
shows.set(path, [...(shows.get(path) ?? []), run]);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
// A picture is found by its name in its address: a whole part of it, not letters inside a longer name.
|
|
144
|
+
const names = (address) => {
|
|
145
|
+
try {
|
|
146
|
+
return decodeURIComponent(address);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return address;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
for (const [path, name] of sources) {
|
|
153
|
+
if (first.has(path))
|
|
154
|
+
continue;
|
|
155
|
+
for (const picture of pictures) {
|
|
156
|
+
const address = names(picture.getAttribute('src') ?? '');
|
|
157
|
+
const at = address.indexOf(name);
|
|
158
|
+
if (at < 0)
|
|
159
|
+
continue;
|
|
160
|
+
const before = name.startsWith('/') || at === 0 ? '/' : (address[at - 1] ?? '');
|
|
161
|
+
const after = address[at + name.length] ?? '';
|
|
162
|
+
if ((before === '/' || before === '=') && (after === '' || './?&'.includes(after)))
|
|
163
|
+
claim(picture, path);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return first;
|
|
167
|
+
};
|
|
168
|
+
const markOf = (target) => {
|
|
169
|
+
if (!isElement(target))
|
|
170
|
+
return null;
|
|
171
|
+
const section = target.closest('[data-cms-section]');
|
|
172
|
+
const id = section?.dataset['cmsSection'];
|
|
173
|
+
if (!section || !id)
|
|
174
|
+
return null;
|
|
175
|
+
// Everything inside text being written is that text, whatever was written into it since.
|
|
176
|
+
if (writing?.mark.element.contains(target))
|
|
177
|
+
return writing.mark;
|
|
178
|
+
guess(section);
|
|
179
|
+
// A field counts only inside the section the pointer is in, and the nearest one is meant.
|
|
180
|
+
for (let element = target; element && element !== section; element = element.parentElement) {
|
|
181
|
+
const path = element.dataset?.['cmsField'] ?? guessed.get(element);
|
|
182
|
+
if (path)
|
|
183
|
+
return { element: element, section: id, field: path };
|
|
184
|
+
}
|
|
185
|
+
return { element: section, section: id };
|
|
186
|
+
};
|
|
187
|
+
const find = (section, field) => {
|
|
188
|
+
const found = [...doc.querySelectorAll('[data-cms-section]')].find((candidate) => candidate.dataset['cmsSection'] === section);
|
|
189
|
+
if (!found)
|
|
190
|
+
return null;
|
|
191
|
+
if (field !== undefined) {
|
|
192
|
+
const inner = within(found, '[data-cms-field]').find((candidate) => candidate.dataset['cmsField'] === field) ?? guess(found).get(field);
|
|
193
|
+
if (inner)
|
|
194
|
+
return { element: inner, section, field };
|
|
195
|
+
}
|
|
196
|
+
return { element: found, section };
|
|
197
|
+
};
|
|
198
|
+
// Layer.
|
|
199
|
+
// The editor's own orange and the ink that goes on it (`--brand-500` and `--brand-ink` in the design
|
|
200
|
+
// system's tokens), written out because the bridge can import nothing. What the editor draws on a
|
|
201
|
+
// page looks like the editor, whatever the page's own colours are.
|
|
202
|
+
const ACCENT = 'oklch(62.5% 0.22 38)';
|
|
203
|
+
const ACCENT_INK = 'oklch(14% 0 0)';
|
|
204
|
+
// The editor may show the page smaller than it is, so a wide page fits a narrow pane. What the
|
|
205
|
+
// editor draws for a pointer (bars and buttons) is then drawn that much larger, so it is the
|
|
206
|
+
// size it always is on the person's screen. `grow` is how much, and a place for such a thing is
|
|
207
|
+
// written with `at`, since the growing takes its place along.
|
|
208
|
+
let grow = 1;
|
|
209
|
+
const at = (px) => `${px / grow}px`;
|
|
210
|
+
const grown = [];
|
|
211
|
+
const grows = (element) => {
|
|
212
|
+
grown.push(element);
|
|
213
|
+
if (grow !== 1)
|
|
214
|
+
element.style.setProperty('zoom', String(grow));
|
|
215
|
+
return element;
|
|
216
|
+
};
|
|
217
|
+
const layer = doc.createElement('div');
|
|
218
|
+
layer.setAttribute('data-cms-layer', '');
|
|
219
|
+
layer.setAttribute('aria-hidden', 'true');
|
|
220
|
+
Object.assign(layer.style, { position: 'fixed', inset: '0', pointerEvents: 'none', zIndex: '2147483647' });
|
|
221
|
+
const outline = (name, width) => {
|
|
222
|
+
const box = doc.createElement('div');
|
|
223
|
+
box.setAttribute('data-cms-outline', name);
|
|
224
|
+
Object.assign(box.style, { position: 'absolute', display: 'none', boxSizing: 'border-box', border: `${width} solid ${ACCENT}`, borderRadius: '4px' });
|
|
225
|
+
layer.append(box);
|
|
226
|
+
return box;
|
|
227
|
+
};
|
|
228
|
+
const boxes = { over: outline('over', '1px'), chosen: outline('chosen', '2px') };
|
|
229
|
+
const marks = { over: null, chosen: null };
|
|
230
|
+
const place = (box, mark) => {
|
|
231
|
+
// A mark whose element has left the page, or has no size, is not drawn.
|
|
232
|
+
const rect = mark?.element.isConnected ? mark.element.getBoundingClientRect() : null;
|
|
233
|
+
if (!rect || (rect.width === 0 && rect.height === 0)) {
|
|
234
|
+
box.style.display = 'none';
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
Object.assign(box.style, { display: 'block', top: `${rect.top}px`, left: `${rect.left}px`, width: `${rect.width}px`, height: `${rect.height}px` });
|
|
238
|
+
};
|
|
239
|
+
let placeTools = () => undefined;
|
|
240
|
+
const draw = () => {
|
|
241
|
+
place(boxes.chosen, marks.chosen);
|
|
242
|
+
// One outline is enough where the pointer is over what is chosen.
|
|
243
|
+
place(boxes.over, marks.over && marks.over.element === marks.chosen?.element ? null : marks.over);
|
|
244
|
+
placeTools();
|
|
245
|
+
};
|
|
246
|
+
let waiting = false;
|
|
247
|
+
const redraw = () => {
|
|
248
|
+
if (waiting)
|
|
249
|
+
return;
|
|
250
|
+
waiting = true;
|
|
251
|
+
page.requestAnimationFrame(() => {
|
|
252
|
+
waiting = false;
|
|
253
|
+
draw();
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
doc.documentElement.append(layer);
|
|
257
|
+
page.addEventListener('scroll', redraw, { capture: true, passive: true });
|
|
258
|
+
page.addEventListener('resize', redraw);
|
|
259
|
+
// Pictures arrive and fonts change after the page is drawn, and move what is outlined.
|
|
260
|
+
if (typeof page.ResizeObserver === 'function')
|
|
261
|
+
new page.ResizeObserver(redraw).observe(doc.documentElement);
|
|
262
|
+
// Typing.
|
|
263
|
+
let typable = {};
|
|
264
|
+
let typing = null;
|
|
265
|
+
/** The run of text words can be typed into for a mark, where the editor and the page both allow it. */
|
|
266
|
+
const runFor = (mark) => {
|
|
267
|
+
if (!mark || mark.field === undefined || mark.element.hasAttribute('data-cms-field'))
|
|
268
|
+
return null;
|
|
269
|
+
const places = typable[mark.section];
|
|
270
|
+
if (!Array.isArray(places) || !places.includes(mark.field))
|
|
271
|
+
return null;
|
|
272
|
+
// A button takes a space for a press, and a field of a form is the visitor's to fill.
|
|
273
|
+
if (mark.element.closest('button, input, textarea, select, [contenteditable="false"]'))
|
|
274
|
+
return null;
|
|
275
|
+
const run = runOf.get(mark.element);
|
|
276
|
+
return run?.isConnected ? run : null;
|
|
277
|
+
};
|
|
278
|
+
// Where the words are in the run being typed into: the space a template leaves around them (before the next words of a heading) is not theirs.
|
|
279
|
+
const wordsAt = (now) => [Math.min(now.lead, now.run.length), Math.max(now.lead, now.run.length - now.trail)];
|
|
280
|
+
// Spaces in a row show as one, so they are held as one; a new line the field already had is kept.
|
|
281
|
+
const wordsOf = (run) => (run.nodeValue ?? '').replace(/[ \t\u00a0]+/g, ' ').trim();
|
|
282
|
+
const tellWords = (now) => {
|
|
283
|
+
const words = wordsOf(now.run);
|
|
284
|
+
const section = now.mark.element.closest('[data-cms-section]');
|
|
285
|
+
// The same field shown elsewhere in the section says the same.
|
|
286
|
+
for (const other of (section && shownIn.get(section)?.get(now.mark.field ?? '')) ?? [])
|
|
287
|
+
if (other !== now.run && other.isConnected)
|
|
288
|
+
other.nodeValue = words;
|
|
289
|
+
say({ type: 'words', id: now.mark.section, field: now.mark.field, words });
|
|
290
|
+
redraw();
|
|
291
|
+
};
|
|
292
|
+
const stopTyping = () => {
|
|
293
|
+
if (!typing)
|
|
294
|
+
return;
|
|
295
|
+
const { mark, was, outline } = typing;
|
|
296
|
+
typing = null;
|
|
297
|
+
mark.element.style.outline = outline;
|
|
298
|
+
if (was === null)
|
|
299
|
+
mark.element.removeAttribute('contenteditable');
|
|
300
|
+
else
|
|
301
|
+
mark.element.setAttribute('contenteditable', was);
|
|
302
|
+
redraw();
|
|
303
|
+
};
|
|
304
|
+
const startTyping = (mark, run) => {
|
|
305
|
+
if (typing?.mark.element === mark.element)
|
|
306
|
+
return;
|
|
307
|
+
stopTyping();
|
|
308
|
+
stopWriting();
|
|
309
|
+
const text = run.nodeValue ?? '';
|
|
310
|
+
const lead = /^[ \t\n\r]*/.exec(text)?.[0].length ?? 0;
|
|
311
|
+
const trail = lead === text.length ? 0 : (/[ \t\n\r]*$/.exec(text)?.[0].length ?? 0);
|
|
312
|
+
typing = { mark, run, before: text, was: mark.element.getAttribute('contenteditable'), outline: mark.element.style.outline, lead, trail };
|
|
313
|
+
mark.element.setAttribute('contenteditable', 'plaintext-only');
|
|
314
|
+
// A browser that does not know plain text only is given the ordinary kind; what is pasted is made plain below.
|
|
315
|
+
if (mark.element.contentEditable !== 'plaintext-only')
|
|
316
|
+
mark.element.setAttribute('contenteditable', 'true');
|
|
317
|
+
// The layer's outline says where the typing goes: the browser's own would sit under it.
|
|
318
|
+
mark.element.style.outline = 'none';
|
|
319
|
+
redraw();
|
|
320
|
+
};
|
|
321
|
+
// The element is made ready as the pointer goes down, so the caret lands where it was pressed.
|
|
322
|
+
doc.addEventListener('mousedown', (event) => {
|
|
323
|
+
const mark = markOf(event.target);
|
|
324
|
+
const run = runFor(mark);
|
|
325
|
+
if (mark && run)
|
|
326
|
+
startTyping(mark, run);
|
|
327
|
+
else if (typing && !(isElement(event.target) && typing.mark.element.contains(event.target)))
|
|
328
|
+
stopTyping();
|
|
329
|
+
}, true);
|
|
330
|
+
// The caret stays in the run: an arrow or End that would carry it into other words of the element stops at the run's edge.
|
|
331
|
+
doc.addEventListener('selectionchange', () => {
|
|
332
|
+
const selection = page.getSelection();
|
|
333
|
+
if (!typing || !selection || selection.rangeCount === 0 || !typing.run.isConnected)
|
|
334
|
+
return;
|
|
335
|
+
const { run } = typing;
|
|
336
|
+
const range = selection.getRangeAt(0);
|
|
337
|
+
if (!typing.mark.element.contains(range.commonAncestorContainer))
|
|
338
|
+
return;
|
|
339
|
+
const [from, to] = wordsAt(typing);
|
|
340
|
+
const edge = (node, offset) => {
|
|
341
|
+
if (node === run)
|
|
342
|
+
return offset < from ? from : offset > to ? to : null;
|
|
343
|
+
const point = doc.createRange();
|
|
344
|
+
point.setStart(node, offset);
|
|
345
|
+
return point.comparePoint(run, 0) === -1 ? to : from;
|
|
346
|
+
};
|
|
347
|
+
const start = edge(range.startContainer, range.startOffset);
|
|
348
|
+
const end = edge(range.endContainer, range.endOffset);
|
|
349
|
+
if (start === null && end === null)
|
|
350
|
+
return;
|
|
351
|
+
selection.setBaseAndExtent(run, start ?? range.startOffset, run, end ?? range.endOffset);
|
|
352
|
+
});
|
|
353
|
+
doc.addEventListener('focusout', (event) => {
|
|
354
|
+
if (typing && event.target === typing.mark.element)
|
|
355
|
+
stopTyping();
|
|
356
|
+
});
|
|
357
|
+
// Only the run of text that shows the field may change: what else the element holds (an icon, other words) stays.
|
|
358
|
+
doc.addEventListener('beforeinput', (event) => {
|
|
359
|
+
if (!typing || !isElement(event.target) || !typing.mark.element.contains(event.target))
|
|
360
|
+
return;
|
|
361
|
+
const { run } = typing;
|
|
362
|
+
const kind = event.inputType;
|
|
363
|
+
if (kind === 'insertParagraph' || kind === 'insertLineBreak' || kind.startsWith('format') || kind.startsWith('history') || kind === 'insertFromDrop') {
|
|
364
|
+
event.preventDefault();
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
// What the change would touch: as the browser says, or else what is selected.
|
|
368
|
+
const selection = page.getSelection();
|
|
369
|
+
const said = event.getTargetRanges();
|
|
370
|
+
const ranges = said.length > 0 ? said : selection && selection.rangeCount > 0 ? [selection.getRangeAt(0)] : [];
|
|
371
|
+
if (ranges.length !== 1 || !ranges[0]) {
|
|
372
|
+
event.preventDefault();
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// The part of the words inside it. A change that would reach outside them is kept to them, and one
|
|
376
|
+
// wholly outside (a key pressed before the caret was brought back) is made at their nearest end.
|
|
377
|
+
const [from, to] = wordsAt(typing);
|
|
378
|
+
const within = (node, offset) => {
|
|
379
|
+
if (node === run)
|
|
380
|
+
return Math.min(to, Math.max(from, offset));
|
|
381
|
+
const point = doc.createRange();
|
|
382
|
+
point.setStart(node, offset);
|
|
383
|
+
return point.comparePoint(run, 0) === -1 ? to : from;
|
|
384
|
+
};
|
|
385
|
+
let start = within(ranges[0].startContainer, ranges[0].startOffset);
|
|
386
|
+
let end = within(ranges[0].endContainer, ranges[0].endOffset);
|
|
387
|
+
// A browser that does not say how much a key would delete says only where the caret is: the reach is worked out here.
|
|
388
|
+
if (start === end && kind.startsWith('delete')) {
|
|
389
|
+
const text = run.data;
|
|
390
|
+
const pair = (at) => (at >= 0 && at < text.length - 1 && text.charCodeAt(at) >= 0xd800 && text.charCodeAt(at) <= 0xdbff ? 2 : 1);
|
|
391
|
+
if (kind.endsWith('Backward')) {
|
|
392
|
+
const word = text.slice(from, start).search(/\S+\s*$/);
|
|
393
|
+
start = kind.includes('Line') ? from : kind.includes('Word') ? from + Math.max(0, word) : Math.max(from, start - pair(start - 2));
|
|
394
|
+
}
|
|
395
|
+
else if (kind.endsWith('Forward')) {
|
|
396
|
+
const word = /^\s*\S+/.exec(text.slice(end, to))?.[0].length ?? 0;
|
|
397
|
+
end = kind.includes('Line') ? to : kind.includes('Word') ? end + word : Math.min(to, end + pair(end));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// The change is made here and never by the browser, which would tidy the spaces around the words and take an emptied run away.
|
|
401
|
+
event.preventDefault();
|
|
402
|
+
// What is pasted, or put in place of a word, goes in as plain words on one line.
|
|
403
|
+
const data = kind.startsWith('delete') ? '' : (event.data ?? event.dataTransfer?.getData('text/plain').replace(/\s+/g, ' '));
|
|
404
|
+
if (typeof data !== 'string' || (start === end && data === ''))
|
|
405
|
+
return;
|
|
406
|
+
// A space typed at either end of the words would be lost among the template's own, so it is one that shows.
|
|
407
|
+
const added = data === ' ' && (start === from || end === to) ? '\u00a0' : data;
|
|
408
|
+
run.replaceData(start, end - start, added);
|
|
409
|
+
selection?.collapse(run, start + added.length);
|
|
410
|
+
tellWords(typing);
|
|
411
|
+
}, true);
|
|
412
|
+
doc.addEventListener('input', (event) => {
|
|
413
|
+
if (typing && isElement(event.target) && typing.mark.element.contains(event.target) && typing.run.isConnected)
|
|
414
|
+
tellWords(typing);
|
|
415
|
+
}, true);
|
|
416
|
+
doc.addEventListener('keydown', (event) => {
|
|
417
|
+
if (!typing || event.isComposing)
|
|
418
|
+
return;
|
|
419
|
+
if (event.key !== 'Enter' && event.key !== 'Escape')
|
|
420
|
+
return;
|
|
421
|
+
event.preventDefault();
|
|
422
|
+
const now = typing;
|
|
423
|
+
// Escape puts back what the field said when the typing began.
|
|
424
|
+
if (event.key === 'Escape' && now.run.isConnected && now.run.nodeValue !== now.before) {
|
|
425
|
+
now.run.nodeValue = now.before;
|
|
426
|
+
tellWords(now);
|
|
427
|
+
}
|
|
428
|
+
now.mark.element.blur();
|
|
429
|
+
stopTyping();
|
|
430
|
+
}, true);
|
|
431
|
+
let writing = null;
|
|
432
|
+
// The words a link's address is being asked for, while the editor asks.
|
|
433
|
+
let asking = null;
|
|
434
|
+
// A link as the field holds it: without what a preview adds to links that stay on the site.
|
|
435
|
+
const withoutPreview = (href) => /^[a-z][a-z0-9+.-]*:/i.test(href) ? href : href.replace(/([?&])preview=[^&#]*(&)?/, (_all, lead, more) => (more ? lead : ''));
|
|
436
|
+
const htmlOf = (element) => {
|
|
437
|
+
const copy = element.cloneNode(true);
|
|
438
|
+
for (const anchor of copy.querySelectorAll('a[href]'))
|
|
439
|
+
anchor.setAttribute('href', withoutPreview(anchor.getAttribute('href') ?? ''));
|
|
440
|
+
return copy.innerHTML;
|
|
441
|
+
};
|
|
442
|
+
/** The element that shows a field's formatted text exactly as it is held, where the editor lets it be written on the page. */
|
|
443
|
+
const holderFor = (mark) => {
|
|
444
|
+
if (!mark || mark.field === undefined || mark.element.hasAttribute('data-cms-field'))
|
|
445
|
+
return null;
|
|
446
|
+
const places = typable[mark.section];
|
|
447
|
+
if (!Array.isArray(places) || !places.includes(mark.field))
|
|
448
|
+
return null;
|
|
449
|
+
const section = mark.element.closest('[data-cms-section]');
|
|
450
|
+
if (!section)
|
|
451
|
+
return null;
|
|
452
|
+
let held = null;
|
|
453
|
+
try {
|
|
454
|
+
const values = JSON.parse(section.dataset['cmsValues'] ?? '[]');
|
|
455
|
+
const value = (Array.isArray(values) ? values : []).find((candidate) => Array.isArray(candidate) && candidate[0] === mark.field && candidate[2] === 'html');
|
|
456
|
+
if (typeof value?.[1] === 'string') {
|
|
457
|
+
// Read as a browser reads it, and never drawn: nothing in it runs.
|
|
458
|
+
const read = doc.createElement('template');
|
|
459
|
+
read.innerHTML = value[1];
|
|
460
|
+
held = read.innerHTML;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
catch {
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
if (held === null)
|
|
467
|
+
return null;
|
|
468
|
+
for (let element = mark.element; element && element !== section; element = element.parentElement) {
|
|
469
|
+
if (htmlOf(element) === held)
|
|
470
|
+
return element.closest('button, input, textarea, select, [contenteditable="false"]') ? null : element;
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
};
|
|
474
|
+
let placeFormat = () => undefined;
|
|
475
|
+
const tellWriting = (now) => {
|
|
476
|
+
say({ type: 'words', id: now.mark.section, field: now.mark.field, words: htmlOf(now.mark.element) });
|
|
477
|
+
redraw();
|
|
478
|
+
};
|
|
479
|
+
const stopWriting = () => {
|
|
480
|
+
if (!writing)
|
|
481
|
+
return;
|
|
482
|
+
const { mark, was, outline } = writing;
|
|
483
|
+
writing = null;
|
|
484
|
+
asking = null;
|
|
485
|
+
mark.element.style.outline = outline;
|
|
486
|
+
if (was === null)
|
|
487
|
+
mark.element.removeAttribute('contenteditable');
|
|
488
|
+
else
|
|
489
|
+
mark.element.setAttribute('contenteditable', was);
|
|
490
|
+
redraw();
|
|
491
|
+
};
|
|
492
|
+
const startWriting = (mark) => {
|
|
493
|
+
if (writing?.mark.element === mark.element)
|
|
494
|
+
return;
|
|
495
|
+
stopTyping();
|
|
496
|
+
stopWriting();
|
|
497
|
+
writing = { mark, before: mark.element.innerHTML, was: mark.element.getAttribute('contenteditable'), outline: mark.element.style.outline };
|
|
498
|
+
mark.element.setAttribute('contenteditable', 'true');
|
|
499
|
+
mark.element.style.outline = 'none';
|
|
500
|
+
marks.chosen = mark;
|
|
501
|
+
redraw();
|
|
502
|
+
};
|
|
503
|
+
doc.addEventListener('mousedown', (event) => {
|
|
504
|
+
if (isElement(event.target) && (layer.contains(event.target) || writing?.mark.element.contains(event.target)))
|
|
505
|
+
return;
|
|
506
|
+
const mark = markOf(event.target);
|
|
507
|
+
const holder = holderFor(mark);
|
|
508
|
+
if (mark && holder && mark.field !== undefined)
|
|
509
|
+
startWriting({ element: holder, section: mark.section, field: mark.field });
|
|
510
|
+
else
|
|
511
|
+
stopWriting();
|
|
512
|
+
}, true);
|
|
513
|
+
doc.addEventListener('focusout', (event) => {
|
|
514
|
+
// While the editor asks where a link leads, the page is left and come back to.
|
|
515
|
+
if (writing && !asking && event.target === writing.mark.element)
|
|
516
|
+
stopWriting();
|
|
517
|
+
});
|
|
518
|
+
doc.addEventListener('beforeinput', (event) => {
|
|
519
|
+
// Nothing is dragged into the text from elsewhere on the page.
|
|
520
|
+
if (writing && isElement(event.target) && writing.mark.element.contains(event.target) && event.inputType === 'insertFromDrop')
|
|
521
|
+
event.preventDefault();
|
|
522
|
+
}, true);
|
|
523
|
+
doc.addEventListener('paste', (event) => {
|
|
524
|
+
if (!writing || !isElement(event.target) || !writing.mark.element.contains(event.target))
|
|
525
|
+
return;
|
|
526
|
+
// What is pasted goes in as words alone: the markup of wherever it came from stays there.
|
|
527
|
+
event.preventDefault();
|
|
528
|
+
const words = event.clipboardData?.getData('text/plain') ?? '';
|
|
529
|
+
if (words !== '')
|
|
530
|
+
doc.execCommand('insertText', false, words);
|
|
531
|
+
}, true);
|
|
532
|
+
doc.addEventListener('input', (event) => {
|
|
533
|
+
if (writing && isElement(event.target) && writing.mark.element.contains(event.target))
|
|
534
|
+
tellWriting(writing);
|
|
535
|
+
}, true);
|
|
536
|
+
doc.addEventListener('keydown', (event) => {
|
|
537
|
+
if (!writing || event.isComposing || event.key !== 'Escape')
|
|
538
|
+
return;
|
|
539
|
+
event.preventDefault();
|
|
540
|
+
const now = writing;
|
|
541
|
+
// Escape puts back what the field said when the writing began.
|
|
542
|
+
if (now.mark.element.innerHTML !== now.before) {
|
|
543
|
+
now.mark.element.innerHTML = now.before;
|
|
544
|
+
tellWriting(now);
|
|
545
|
+
}
|
|
546
|
+
stopWriting();
|
|
547
|
+
now.mark.element.blur();
|
|
548
|
+
}, true);
|
|
549
|
+
doc.addEventListener('selectionchange', () => placeFormat());
|
|
550
|
+
// Undo and redo are the form's: the page says the keys were pressed and takes nothing back
|
|
551
|
+
// itself. Formatted text being written is the browser's to take back, as it is to write.
|
|
552
|
+
doc.addEventListener('keydown', (event) => {
|
|
553
|
+
if (writing || event.isComposing || event.altKey || !(event.metaKey || event.ctrlKey))
|
|
554
|
+
return;
|
|
555
|
+
const key = event.key.toLowerCase();
|
|
556
|
+
const asked = key === 'z' ? (event.shiftKey ? 'redo' : 'undo') : key === 'y' && event.ctrlKey && !event.shiftKey ? 'redo' : null;
|
|
557
|
+
if (asked === null)
|
|
558
|
+
return;
|
|
559
|
+
// A field of a form on the page is the visitor's, and the browser's to take back.
|
|
560
|
+
if (!typing && isElement(event.target) && event.target.closest('input, textarea, select, [contenteditable]'))
|
|
561
|
+
return;
|
|
562
|
+
event.preventDefault();
|
|
563
|
+
if (typing) {
|
|
564
|
+
typing.mark.element.blur();
|
|
565
|
+
stopTyping();
|
|
566
|
+
}
|
|
567
|
+
say({ type: asked });
|
|
568
|
+
}, true);
|
|
569
|
+
// Saving is the form's too: the page says the keys were pressed, and the browser's own answer
|
|
570
|
+
// to them (saving the page as a file) is stopped. What is being typed is already in the form.
|
|
571
|
+
doc.addEventListener('keydown', (event) => {
|
|
572
|
+
if (event.isComposing || event.altKey || event.shiftKey || !(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== 's')
|
|
573
|
+
return;
|
|
574
|
+
event.preventDefault();
|
|
575
|
+
say({ type: 'save' });
|
|
576
|
+
}, true);
|
|
577
|
+
let tools = null;
|
|
578
|
+
// The section the bar is for: the one the pointer was last in, and it stays while the pointer is on the bar itself.
|
|
579
|
+
let tooled = null;
|
|
580
|
+
// Lucide's drawings, as the lines they are made of: the bridge can import nothing.
|
|
581
|
+
const DRAWN = {
|
|
582
|
+
up: ['m5 12 7-7 7 7', 'M12 19V5'],
|
|
583
|
+
down: ['M12 5v14', 'm19 12-7 7-7-7'],
|
|
584
|
+
duplicate: ['M8 8h12a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H10a2 2 0 0 1-2-2z', 'M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2'],
|
|
585
|
+
remove: ['M3 6h18', 'M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6', 'M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2', 'M10 11v6', 'M14 11v6'],
|
|
586
|
+
add: ['M5 12h14', 'M12 5v14'],
|
|
587
|
+
picture: ['M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z', 'M11 9a2 2 0 1 1-4 0 2 2 0 0 1 4 0z', 'm21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21'],
|
|
588
|
+
link: ['M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71', 'M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71'],
|
|
589
|
+
bold: ['M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8'],
|
|
590
|
+
italic: ['M19 4h-9', 'M14 20H5', 'M15 4 9 20'],
|
|
591
|
+
reorder: ['m21 16-4 4-4-4', 'M17 20V4', 'm3 8 4-4 4 4', 'M7 4v16'],
|
|
592
|
+
drag: ['M9 5h.01', 'M9 12h.01', 'M9 19h.01', 'M15 5h.01', 'M15 12h.01', 'M15 19h.01'],
|
|
593
|
+
};
|
|
594
|
+
const drawing = (name) => {
|
|
595
|
+
const svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
596
|
+
for (const [key, value] of Object.entries({ viewBox: '0 0 24 24', width: '16', height: '16', fill: 'none', stroke: 'currentColor', 'stroke-width': '2', 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }))
|
|
597
|
+
svg.setAttribute(key, value);
|
|
598
|
+
for (const line of DRAWN[name] ?? []) {
|
|
599
|
+
const path = doc.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
600
|
+
path.setAttribute('d', line);
|
|
601
|
+
svg.append(path);
|
|
602
|
+
}
|
|
603
|
+
return svg;
|
|
604
|
+
};
|
|
605
|
+
const FONT = 'system-ui, sans-serif';
|
|
606
|
+
/** Where what a site keeps at the top of its window ends (a header that stays as the page scrolls), or 0. */
|
|
607
|
+
const kept = () => {
|
|
608
|
+
if (typeof doc.elementFromPoint !== 'function')
|
|
609
|
+
return 0;
|
|
610
|
+
for (let element = doc.elementFromPoint(page.innerWidth / 2, 2); element && element !== doc.body && element !== doc.documentElement; element = element.parentElement) {
|
|
611
|
+
const position = page.getComputedStyle(element).position;
|
|
612
|
+
if (position !== 'fixed' && position !== 'sticky')
|
|
613
|
+
continue;
|
|
614
|
+
const rect = element.getBoundingClientRect();
|
|
615
|
+
// Only a strip at the top counts: something that covers the window is no header.
|
|
616
|
+
return rect.top <= 2 && rect.bottom < page.innerHeight / 3 ? rect.bottom : 0;
|
|
617
|
+
}
|
|
618
|
+
return 0;
|
|
619
|
+
};
|
|
620
|
+
const bar = grows(doc.createElement('div'));
|
|
621
|
+
bar.setAttribute('data-cms-tools', '');
|
|
622
|
+
Object.assign(bar.style, {
|
|
623
|
+
position: 'absolute',
|
|
624
|
+
display: 'none',
|
|
625
|
+
alignItems: 'center',
|
|
626
|
+
gap: '2px',
|
|
627
|
+
padding: '4px',
|
|
628
|
+
borderRadius: '8px',
|
|
629
|
+
background: 'CanvasText',
|
|
630
|
+
color: 'Canvas',
|
|
631
|
+
font: `500 12px/1 ${FONT}`,
|
|
632
|
+
boxShadow: '0 4px 12px rgb(0 0 0 / 0.25)',
|
|
633
|
+
pointerEvents: 'auto',
|
|
634
|
+
});
|
|
635
|
+
const grip = doc.createElement('span');
|
|
636
|
+
grip.setAttribute('data-cms-tool', 'drag');
|
|
637
|
+
Object.assign(grip.style, { display: 'grid', placeItems: 'center', width: '20px', height: '28px', cursor: 'grab' });
|
|
638
|
+
grip.append(drawing('drag'));
|
|
639
|
+
bar.append(grip);
|
|
640
|
+
const named = doc.createElement('span');
|
|
641
|
+
Object.assign(named.style, { padding: '0 8px 0 2px', cursor: 'grab', maxWidth: '12rem', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' });
|
|
642
|
+
bar.append(named);
|
|
643
|
+
const press = (button, action) => {
|
|
644
|
+
// The pointer going down must not take the caret from the page or start a selection.
|
|
645
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
646
|
+
button.addEventListener('click', () => {
|
|
647
|
+
const id = tooled?.dataset['cmsSection'];
|
|
648
|
+
if (id && !button.disabled)
|
|
649
|
+
say({ type: 'act', id, action });
|
|
650
|
+
});
|
|
651
|
+
};
|
|
652
|
+
const tool = (action) => {
|
|
653
|
+
const button = doc.createElement('button');
|
|
654
|
+
button.type = 'button';
|
|
655
|
+
// The bar is for a pointer: the same things are in the form for a keyboard and a screen reader.
|
|
656
|
+
button.tabIndex = -1;
|
|
657
|
+
button.setAttribute('data-cms-tool', action);
|
|
658
|
+
Object.assign(button.style, { display: 'grid', placeItems: 'center', width: '28px', height: '28px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', cursor: 'pointer' });
|
|
659
|
+
button.append(drawing(action));
|
|
660
|
+
press(button, action);
|
|
661
|
+
bar.append(button);
|
|
662
|
+
return button;
|
|
663
|
+
};
|
|
664
|
+
const buttons = { up: tool('up'), down: tool('down'), duplicate: tool('duplicate'), remove: tool('remove') };
|
|
665
|
+
// Adding is on the bar too, for a section whose edges are off the page.
|
|
666
|
+
const more = doc.createElement('button');
|
|
667
|
+
more.type = 'button';
|
|
668
|
+
more.tabIndex = -1;
|
|
669
|
+
more.setAttribute('data-cms-tool', 'addBelow');
|
|
670
|
+
Object.assign(more.style, { display: 'grid', placeItems: 'center', width: '28px', height: '28px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', cursor: 'pointer' });
|
|
671
|
+
more.append(drawing('add'));
|
|
672
|
+
press(more, 'addAfter');
|
|
673
|
+
bar.append(more);
|
|
674
|
+
// The page's sections by name, to put in another order.
|
|
675
|
+
const sorter = doc.createElement('button');
|
|
676
|
+
sorter.type = 'button';
|
|
677
|
+
sorter.tabIndex = -1;
|
|
678
|
+
sorter.setAttribute('data-cms-tool', 'reorder');
|
|
679
|
+
Object.assign(sorter.style, { display: 'none', placeItems: 'center', width: '28px', height: '28px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', cursor: 'pointer' });
|
|
680
|
+
sorter.append(drawing('reorder'));
|
|
681
|
+
sorter.addEventListener('mousedown', (event) => event.preventDefault());
|
|
682
|
+
sorter.addEventListener('click', () => openStack(null, true, 0));
|
|
683
|
+
bar.append(sorter);
|
|
684
|
+
// Swatches: a look of the section chosen among a few of the site's colours has a dot for each
|
|
685
|
+
// choice on the bar. The page changes nothing: it says which was pressed.
|
|
686
|
+
const dots = doc.createElement('span');
|
|
687
|
+
dots.setAttribute('data-cms-swatches', '');
|
|
688
|
+
Object.assign(dots.style, { display: 'none', alignItems: 'center', gap: '7px', padding: '0 6px 0 10px', marginLeft: '4px', borderLeft: '1px solid color-mix(in srgb, currentColor 25%, transparent)' });
|
|
689
|
+
bar.append(dots);
|
|
690
|
+
// One bar for everything: what can be done to the thing the pointer was last on (a picture, a
|
|
691
|
+
// link, an item of a list) is at its end, in the editor's orange, as the thing's own outline is.
|
|
692
|
+
const forThing = doc.createElement('span');
|
|
693
|
+
forThing.setAttribute('data-cms-about', '');
|
|
694
|
+
Object.assign(forThing.style, { display: 'none', alignItems: 'center', gap: '4px', paddingLeft: '8px', marginLeft: '4px', borderLeft: '1px solid color-mix(in srgb, currentColor 25%, transparent)' });
|
|
695
|
+
bar.append(forThing);
|
|
696
|
+
// A colour and nothing else: a sample is never an address to fetch.
|
|
697
|
+
const COLOUR = /^(?:#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|(?:oklch|oklab|rgb|hsl)\([0-9.%,/ -]+\))$/i;
|
|
698
|
+
let dotted = '';
|
|
699
|
+
const drawDots = (about, id) => {
|
|
700
|
+
const sets = Array.isArray(about.swatches) ? about.swatches : [];
|
|
701
|
+
const key = `${id}:${JSON.stringify(sets)}`;
|
|
702
|
+
if (key === dotted)
|
|
703
|
+
return;
|
|
704
|
+
dotted = key;
|
|
705
|
+
dots.replaceChildren();
|
|
706
|
+
for (const set of sets) {
|
|
707
|
+
if (typeof set.field !== 'string' || !Array.isArray(set.choices))
|
|
708
|
+
continue;
|
|
709
|
+
for (const choice of set.choices) {
|
|
710
|
+
if (typeof choice.value !== 'string' || typeof choice.colour !== 'string' || choice.colour.length > 64 || !COLOUR.test(choice.colour))
|
|
711
|
+
continue;
|
|
712
|
+
const { field } = set;
|
|
713
|
+
const { value } = choice;
|
|
714
|
+
const dot = doc.createElement('button');
|
|
715
|
+
dot.type = 'button';
|
|
716
|
+
dot.tabIndex = -1;
|
|
717
|
+
dot.setAttribute('data-cms-swatch', `${field}:${value}`);
|
|
718
|
+
dot.title = [word(set.name), word(choice.name)].filter(Boolean).join(': ');
|
|
719
|
+
const isChosen = set.chosen === value;
|
|
720
|
+
if (isChosen)
|
|
721
|
+
dot.setAttribute('data-cms-chosen', '');
|
|
722
|
+
Object.assign(dot.style, {
|
|
723
|
+
boxSizing: 'border-box',
|
|
724
|
+
width: '18px',
|
|
725
|
+
height: '18px',
|
|
726
|
+
padding: '0',
|
|
727
|
+
borderRadius: '50%',
|
|
728
|
+
border: '1px solid color-mix(in srgb, currentColor 45%, transparent)',
|
|
729
|
+
background: choice.colour,
|
|
730
|
+
outline: isChosen ? `2px solid ${ACCENT}` : 'none',
|
|
731
|
+
outlineOffset: '1px',
|
|
732
|
+
cursor: isChosen ? 'default' : 'pointer',
|
|
733
|
+
});
|
|
734
|
+
dot.addEventListener('mousedown', (event) => event.preventDefault());
|
|
735
|
+
dot.addEventListener('click', () => {
|
|
736
|
+
if (!isChosen)
|
|
737
|
+
say({ type: 'swatch', id, field, value });
|
|
738
|
+
});
|
|
739
|
+
dots.append(dot);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
dots.style.display = dots.childElementCount > 0 ? 'flex' : 'none';
|
|
743
|
+
};
|
|
744
|
+
const adder = (action, acts = true) => {
|
|
745
|
+
const button = grows(doc.createElement('button'));
|
|
746
|
+
button.type = 'button';
|
|
747
|
+
button.tabIndex = -1;
|
|
748
|
+
button.setAttribute('data-cms-tool', action);
|
|
749
|
+
Object.assign(button.style, {
|
|
750
|
+
position: 'absolute',
|
|
751
|
+
display: 'none',
|
|
752
|
+
alignItems: 'center',
|
|
753
|
+
gap: '4px',
|
|
754
|
+
height: '28px',
|
|
755
|
+
padding: '0 12px 0 8px',
|
|
756
|
+
border: '0',
|
|
757
|
+
borderRadius: '14px',
|
|
758
|
+
background: ACCENT,
|
|
759
|
+
color: ACCENT_INK,
|
|
760
|
+
font: `600 12px/1 ${FONT}`,
|
|
761
|
+
boxShadow: '0 4px 12px rgb(0 0 0 / 0.25)',
|
|
762
|
+
transform: 'translate(-50%, -50%)',
|
|
763
|
+
whiteSpace: 'nowrap',
|
|
764
|
+
cursor: 'pointer',
|
|
765
|
+
pointerEvents: 'auto',
|
|
766
|
+
});
|
|
767
|
+
button.append(drawing('add'), doc.createElement('span'));
|
|
768
|
+
if (acts)
|
|
769
|
+
press(button, action);
|
|
770
|
+
else
|
|
771
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
772
|
+
layer.append(button);
|
|
773
|
+
return button;
|
|
774
|
+
};
|
|
775
|
+
// A line along each edge of the section the bar is on, with the button that adds a section there.
|
|
776
|
+
const seam = () => {
|
|
777
|
+
const drawn = doc.createElement('div');
|
|
778
|
+
drawn.setAttribute('data-cms-seam', '');
|
|
779
|
+
Object.assign(drawn.style, { position: 'absolute', display: 'none', height: '2px', marginTop: '-1px', background: ACCENT });
|
|
780
|
+
layer.append(drawn);
|
|
781
|
+
return drawn;
|
|
782
|
+
};
|
|
783
|
+
const seams = { addBefore: seam(), addAfter: seam() };
|
|
784
|
+
const adders = { addBefore: adder('addBefore'), addAfter: adder('addAfter') };
|
|
785
|
+
// An empty place inside a section (a column with nothing in it yet) has a button that adds the first.
|
|
786
|
+
const slotOf = (element) => element.dataset['cmsSlot'] ?? '';
|
|
787
|
+
const fillers = new Map();
|
|
788
|
+
const placeFillers = (isShown) => {
|
|
789
|
+
const wanted = new Map();
|
|
790
|
+
if (isShown && tools?.slots) {
|
|
791
|
+
for (const slot of doc.querySelectorAll('[data-cms-slot]')) {
|
|
792
|
+
const about = tools.slots[slotOf(slot)];
|
|
793
|
+
const rect = slot.getBoundingClientRect();
|
|
794
|
+
if (about?.empty === true && about.full !== true && rect.bottom > 14 && rect.top < page.innerHeight - 14 && rect.width > 0)
|
|
795
|
+
wanted.set(slotOf(slot), rect);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
for (const [key, button] of fillers) {
|
|
799
|
+
if (wanted.has(key))
|
|
800
|
+
continue;
|
|
801
|
+
button.remove();
|
|
802
|
+
fillers.delete(key);
|
|
803
|
+
}
|
|
804
|
+
for (const [key, rect] of wanted) {
|
|
805
|
+
let button = fillers.get(key);
|
|
806
|
+
if (!button) {
|
|
807
|
+
button = adder('fill', false);
|
|
808
|
+
button.setAttribute('data-cms-fill', key);
|
|
809
|
+
button.addEventListener('click', () => {
|
|
810
|
+
const at = key.indexOf(':');
|
|
811
|
+
say({ type: 'slot', id: key.slice(0, at), field: key.slice(at + 1) });
|
|
812
|
+
});
|
|
813
|
+
fillers.set(key, button);
|
|
814
|
+
}
|
|
815
|
+
if (button.lastElementChild)
|
|
816
|
+
button.lastElementChild.textContent = word(tools?.labels['add']);
|
|
817
|
+
const middle = Math.min(Math.max(rect.top + rect.height / 2, 16), page.innerHeight - 16);
|
|
818
|
+
Object.assign(button.style, { display: 'flex', top: at(middle), left: at(rect.left + rect.width / 2) });
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
layer.append(bar);
|
|
822
|
+
const word = (value) => (typeof value === 'string' ? value : '');
|
|
823
|
+
// What the editor can change from the page has a button of its own on the bar once the pointer
|
|
824
|
+
// has been on it: a picture, and a link or a button. The pointer may be on something the
|
|
825
|
+
// template lays over it, so it is found by where it is.
|
|
826
|
+
const chip = (name) => {
|
|
827
|
+
const button = doc.createElement('button');
|
|
828
|
+
button.type = 'button';
|
|
829
|
+
button.tabIndex = -1;
|
|
830
|
+
button.setAttribute('data-cms-tool', name);
|
|
831
|
+
Object.assign(button.style, {
|
|
832
|
+
display: 'none',
|
|
833
|
+
alignItems: 'center',
|
|
834
|
+
gap: '6px',
|
|
835
|
+
height: '28px',
|
|
836
|
+
padding: '0 10px 0 8px',
|
|
837
|
+
border: '0',
|
|
838
|
+
borderRadius: '6px',
|
|
839
|
+
background: ACCENT,
|
|
840
|
+
color: ACCENT_INK,
|
|
841
|
+
font: `600 12px/1 ${FONT}`,
|
|
842
|
+
whiteSpace: 'nowrap',
|
|
843
|
+
cursor: 'pointer',
|
|
844
|
+
});
|
|
845
|
+
button.append(drawing(name), doc.createElement('span'));
|
|
846
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
847
|
+
forThing.append(button);
|
|
848
|
+
return button;
|
|
849
|
+
};
|
|
850
|
+
let pictured = null;
|
|
851
|
+
const pictureBox = outline('picture', '2px');
|
|
852
|
+
const linkBox = outline('link', '2px');
|
|
853
|
+
const changer = chip('picture');
|
|
854
|
+
changer.addEventListener('click', () => {
|
|
855
|
+
if (pictured?.field !== undefined)
|
|
856
|
+
say({ type: 'picture', id: pictured.section, field: pictured.field });
|
|
857
|
+
});
|
|
858
|
+
let linked = null;
|
|
859
|
+
const linker = chip('link');
|
|
860
|
+
linker.addEventListener('click', () => {
|
|
861
|
+
if (linked?.field !== undefined)
|
|
862
|
+
say({ type: 'link', id: linked.section, field: linked.field });
|
|
863
|
+
});
|
|
864
|
+
// Of those that lie over one another, the smallest is the one meant.
|
|
865
|
+
const smallestAt = (marks, x, y) => {
|
|
866
|
+
let found = null;
|
|
867
|
+
let least = Infinity;
|
|
868
|
+
for (const mark of marks) {
|
|
869
|
+
const rect = mark.element.getBoundingClientRect();
|
|
870
|
+
if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom)
|
|
871
|
+
continue;
|
|
872
|
+
const size = rect.width * rect.height;
|
|
873
|
+
if (size < least)
|
|
874
|
+
[found, least] = [mark, size];
|
|
875
|
+
}
|
|
876
|
+
return found;
|
|
877
|
+
};
|
|
878
|
+
const pictureAt = (x, y) => {
|
|
879
|
+
const id = tooled?.dataset['cmsSection'] ?? '';
|
|
880
|
+
const paths = tools?.sections[id]?.pictures;
|
|
881
|
+
if (!tooled || !Array.isArray(paths))
|
|
882
|
+
return null;
|
|
883
|
+
return smallestAt(paths.flatMap((path) => {
|
|
884
|
+
const mark = typeof path === 'string' ? find(id, path) : null;
|
|
885
|
+
return mark?.field === undefined ? [] : [mark];
|
|
886
|
+
}), x, y);
|
|
887
|
+
};
|
|
888
|
+
// A link shows no words of its own: it is found by where it leads. Two addresses that lead to
|
|
889
|
+
// one place read the same here, whatever a preview added to one and however it ends.
|
|
890
|
+
const leadsTo = (href) => {
|
|
891
|
+
if (typeof href !== 'string' || href === '')
|
|
892
|
+
return null;
|
|
893
|
+
try {
|
|
894
|
+
const to = new URL(href, page.location.href);
|
|
895
|
+
to.searchParams.delete('preview');
|
|
896
|
+
return `${to.origin}${to.pathname.replace(/\/$/, '')}${to.search}${to.hash}`;
|
|
897
|
+
}
|
|
898
|
+
catch {
|
|
899
|
+
return null;
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
// Where the site found each link to an entry to lead, by `<type>:<entry>`: it says so beside this script.
|
|
903
|
+
let leads = null;
|
|
904
|
+
const linksIn = new WeakMap();
|
|
905
|
+
const linksOf = (section, id, paths) => {
|
|
906
|
+
const said = paths.join('\n');
|
|
907
|
+
const known = linksIn.get(section);
|
|
908
|
+
if (known?.said === said)
|
|
909
|
+
return known.found;
|
|
910
|
+
const found = [];
|
|
911
|
+
linksIn.set(section, { said, found });
|
|
912
|
+
const held = new Map();
|
|
913
|
+
try {
|
|
914
|
+
if (leads === null) {
|
|
915
|
+
const table = JSON.parse(doc.querySelector('script[data-cms-links]')?.textContent ?? '{}');
|
|
916
|
+
leads = typeof table === 'object' && table !== null ? table : {};
|
|
917
|
+
}
|
|
918
|
+
const values = JSON.parse(section.dataset['cmsValues'] ?? '[]');
|
|
919
|
+
for (const value of Array.isArray(values) ? values : []) {
|
|
920
|
+
if (Array.isArray(value) && typeof value[0] === 'string' && typeof value[1] === 'string')
|
|
921
|
+
held.set(value[0], value[1]);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
catch {
|
|
925
|
+
return found;
|
|
926
|
+
}
|
|
927
|
+
// Fields that lead to the same place share the links that go there, in order.
|
|
928
|
+
const wanted = new Map();
|
|
929
|
+
for (const path of paths) {
|
|
930
|
+
if (typeof path !== 'string')
|
|
931
|
+
continue;
|
|
932
|
+
const entry = held.get(`${path}.kind`) === 'entry' ? `${held.get(`${path}.type`) ?? ''}:${held.get(`${path}.entry`) ?? ''}` : null;
|
|
933
|
+
const to = leadsTo(entry === null ? (held.get(`${path}.href`) ?? held.get(path)) : leads?.[entry]);
|
|
934
|
+
if (to !== null)
|
|
935
|
+
wanted.set(to, [...(wanted.get(to) ?? []), path]);
|
|
936
|
+
}
|
|
937
|
+
const going = new Map();
|
|
938
|
+
for (const anchor of within(section, 'a[href]')) {
|
|
939
|
+
const to = leadsTo(anchor.getAttribute('href'));
|
|
940
|
+
if (to !== null && wanted.has(to))
|
|
941
|
+
going.set(to, [...(going.get(to) ?? []), anchor]);
|
|
942
|
+
}
|
|
943
|
+
for (const [to, fields] of wanted) {
|
|
944
|
+
const anchors = going.get(to) ?? [];
|
|
945
|
+
anchors.forEach((element, at) => {
|
|
946
|
+
const field = fields[Math.min(fields.length - 1, Math.floor((at * fields.length) / anchors.length))];
|
|
947
|
+
if (field !== undefined)
|
|
948
|
+
found.push({ element, section: id, field });
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
return found;
|
|
952
|
+
};
|
|
953
|
+
const linkAt = (x, y) => {
|
|
954
|
+
const id = tooled?.dataset['cmsSection'] ?? '';
|
|
955
|
+
const paths = tools?.sections[id]?.links;
|
|
956
|
+
return tooled && Array.isArray(paths) ? smallestAt(linksOf(tooled, id, paths), x, y) : null;
|
|
957
|
+
};
|
|
958
|
+
// Words chosen in text being written have a bar of their own: bold, italic, link. The browser
|
|
959
|
+
// shapes the words; where a link leads is asked of the editor, which has the words to ask with.
|
|
960
|
+
const formatBar = grows(doc.createElement('div'));
|
|
961
|
+
formatBar.setAttribute('data-cms-format', '');
|
|
962
|
+
Object.assign(formatBar.style, {
|
|
963
|
+
position: 'absolute',
|
|
964
|
+
display: 'none',
|
|
965
|
+
alignItems: 'center',
|
|
966
|
+
gap: '2px',
|
|
967
|
+
padding: '2px',
|
|
968
|
+
borderRadius: '8px',
|
|
969
|
+
background: 'CanvasText',
|
|
970
|
+
color: 'Canvas',
|
|
971
|
+
boxShadow: '0 4px 12px rgb(0 0 0 / 0.25)',
|
|
972
|
+
pointerEvents: 'auto',
|
|
973
|
+
});
|
|
974
|
+
const chosenWords = () => {
|
|
975
|
+
const selection = page.getSelection();
|
|
976
|
+
if (!writing || !selection || selection.rangeCount === 0 || selection.isCollapsed)
|
|
977
|
+
return null;
|
|
978
|
+
const range = selection.getRangeAt(0);
|
|
979
|
+
return writing.mark.element.contains(range.commonAncestorContainer) ? range : null;
|
|
980
|
+
};
|
|
981
|
+
const formatTool = (name, label, press) => {
|
|
982
|
+
const button = doc.createElement('button');
|
|
983
|
+
button.type = 'button';
|
|
984
|
+
button.tabIndex = -1;
|
|
985
|
+
button.setAttribute('data-cms-format-tool', name);
|
|
986
|
+
button.setAttribute('data-cms-label', label);
|
|
987
|
+
Object.assign(button.style, { display: 'grid', placeItems: 'center', width: '28px', height: '28px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', cursor: 'pointer' });
|
|
988
|
+
button.append(drawing(name));
|
|
989
|
+
// The pointer going down must not take the chosen words away.
|
|
990
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
991
|
+
button.addEventListener('click', () => {
|
|
992
|
+
const range = chosenWords();
|
|
993
|
+
if (writing && range)
|
|
994
|
+
press(writing, range);
|
|
995
|
+
});
|
|
996
|
+
formatBar.append(button);
|
|
997
|
+
return button;
|
|
998
|
+
};
|
|
999
|
+
const shape = (command, value) => {
|
|
1000
|
+
if (!writing)
|
|
1001
|
+
return;
|
|
1002
|
+
doc.execCommand(command, false, value);
|
|
1003
|
+
tellWriting(writing);
|
|
1004
|
+
};
|
|
1005
|
+
formatTool('bold', 'bold', () => shape('bold'));
|
|
1006
|
+
formatTool('italic', 'italic', () => shape('italic'));
|
|
1007
|
+
formatTool('link', 'textLink', (now, range) => {
|
|
1008
|
+
const start = isElement(range.startContainer) ? range.startContainer : range.startContainer.parentElement;
|
|
1009
|
+
const anchor = start?.closest('a[href]');
|
|
1010
|
+
asking = range.cloneRange();
|
|
1011
|
+
say({ type: 'linkAsk', id: now.mark.section, field: now.mark.field, href: anchor && now.mark.element.contains(anchor) ? withoutPreview(anchor.getAttribute('href') ?? '') : '' });
|
|
1012
|
+
placeFormat();
|
|
1013
|
+
});
|
|
1014
|
+
layer.append(formatBar);
|
|
1015
|
+
/** The editor's answer to where a link leads: an address, nothing to take the link away, or `null` to leave it. */
|
|
1016
|
+
const answerLink = (href) => {
|
|
1017
|
+
const range = asking;
|
|
1018
|
+
asking = null;
|
|
1019
|
+
if (!writing || !range)
|
|
1020
|
+
return;
|
|
1021
|
+
writing.mark.element.focus();
|
|
1022
|
+
const selection = page.getSelection();
|
|
1023
|
+
selection?.removeAllRanges();
|
|
1024
|
+
selection?.addRange(range);
|
|
1025
|
+
if (href === '')
|
|
1026
|
+
shape('unlink');
|
|
1027
|
+
else if (typeof href === 'string')
|
|
1028
|
+
shape('createLink', href);
|
|
1029
|
+
placeFormat();
|
|
1030
|
+
};
|
|
1031
|
+
placeFormat = () => {
|
|
1032
|
+
const range = asking ? null : chosenWords();
|
|
1033
|
+
if (!range) {
|
|
1034
|
+
formatBar.style.display = 'none';
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
for (const button of formatBar.querySelectorAll('button'))
|
|
1038
|
+
button.title = word(tools?.labels[button.getAttribute('data-cms-label') ?? '']);
|
|
1039
|
+
const rect = typeof range.getBoundingClientRect === 'function' ? range.getBoundingClientRect() : (writing?.mark.element.getBoundingClientRect() ?? null);
|
|
1040
|
+
if (!rect)
|
|
1041
|
+
return;
|
|
1042
|
+
// Above the chosen words, and under them where the page ends above.
|
|
1043
|
+
Object.assign(formatBar.style, { display: 'flex', top: at(rect.top >= 40 * grow ? rect.top - 36 * grow : rect.bottom + 6), left: at(Math.max(rect.left, 4)) });
|
|
1044
|
+
};
|
|
1045
|
+
const itemsIn = new WeakMap();
|
|
1046
|
+
const itemsOf = (section, id) => {
|
|
1047
|
+
const about = tools?.sections[id];
|
|
1048
|
+
const lists = Array.isArray(about?.lists) ? about.lists : [];
|
|
1049
|
+
const links = Array.isArray(about?.links) ? about.links : [];
|
|
1050
|
+
const said = JSON.stringify([lists, links]);
|
|
1051
|
+
const known = itemsIn.get(section);
|
|
1052
|
+
if (known?.said === said)
|
|
1053
|
+
return known.found;
|
|
1054
|
+
const found = [];
|
|
1055
|
+
itemsIn.set(section, { said, found });
|
|
1056
|
+
// Every element known to show a field, by the field's place.
|
|
1057
|
+
const shown = [...guess(section)];
|
|
1058
|
+
for (const marked of within(section, '[data-cms-field]'))
|
|
1059
|
+
shown.push([marked.dataset['cmsField'] ?? '', marked]);
|
|
1060
|
+
for (const link of linksOf(section, id, links))
|
|
1061
|
+
shown.push([link.field ?? '', link.element]);
|
|
1062
|
+
for (const list of lists) {
|
|
1063
|
+
if (typeof list !== 'object' || list === null)
|
|
1064
|
+
continue;
|
|
1065
|
+
const { field, count, full } = list;
|
|
1066
|
+
if (typeof field !== 'string' || typeof count !== 'number')
|
|
1067
|
+
continue;
|
|
1068
|
+
const held = new Map();
|
|
1069
|
+
for (const [path, element] of shown) {
|
|
1070
|
+
if (!path.startsWith(`${field}.`))
|
|
1071
|
+
continue;
|
|
1072
|
+
const at = Number(path.slice(field.length + 1).split('.')[0]);
|
|
1073
|
+
if (Number.isInteger(at) && at >= 0 && at < count)
|
|
1074
|
+
held.set(at, [...(held.get(at) ?? []), element]);
|
|
1075
|
+
}
|
|
1076
|
+
for (const [at, elements] of held) {
|
|
1077
|
+
let element = elements[0];
|
|
1078
|
+
if (!element)
|
|
1079
|
+
continue;
|
|
1080
|
+
while (element !== section && !elements.every((inner) => element?.contains(inner)) && element.parentElement)
|
|
1081
|
+
element = element.parentElement;
|
|
1082
|
+
// As far out as it goes without taking in another item, so the whole card is the item.
|
|
1083
|
+
const others = [...held].filter(([other]) => other !== at).flatMap(([, theirs]) => theirs);
|
|
1084
|
+
if (others.some((other) => element?.contains(other)))
|
|
1085
|
+
continue;
|
|
1086
|
+
while (others.length > 0 && element.parentElement && element.parentElement !== section && !others.some((other) => element?.parentElement?.contains(other)))
|
|
1087
|
+
element = element.parentElement;
|
|
1088
|
+
if (element !== section)
|
|
1089
|
+
found.push({ element, section: id, field: `${field}.${at}`, list: field, at, count, full: full === true });
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
return found;
|
|
1093
|
+
};
|
|
1094
|
+
let itemed = null;
|
|
1095
|
+
/** Where the item the bar is about will be on the page drawn next. */
|
|
1096
|
+
let next;
|
|
1097
|
+
/** What the bar was on when the page was last drawn: it is on it again, with no move of the pointer. */
|
|
1098
|
+
let again = null;
|
|
1099
|
+
let forget = () => undefined;
|
|
1100
|
+
const itemAt = (x, y) => {
|
|
1101
|
+
const id = tooled?.dataset['cmsSection'] ?? '';
|
|
1102
|
+
if (!tooled || !tools)
|
|
1103
|
+
return null;
|
|
1104
|
+
const items = itemsOf(tooled, id);
|
|
1105
|
+
const mark = smallestAt(items, x, y);
|
|
1106
|
+
return items.find((item) => item.element === mark?.element && item.field === mark.field) ?? null;
|
|
1107
|
+
};
|
|
1108
|
+
const itemBox = outline('item', '2px');
|
|
1109
|
+
itemBox.style.borderStyle = 'dashed';
|
|
1110
|
+
const itemBar = doc.createElement('div');
|
|
1111
|
+
itemBar.setAttribute('data-cms-item-tools', '');
|
|
1112
|
+
Object.assign(itemBar.style, { display: 'none', alignItems: 'center', gap: '0', padding: '0 2px', height: '28px', boxSizing: 'border-box', borderRadius: '6px', background: ACCENT, color: ACCENT_INK });
|
|
1113
|
+
const itemTool = (action) => {
|
|
1114
|
+
const button = doc.createElement('button');
|
|
1115
|
+
button.type = 'button';
|
|
1116
|
+
button.tabIndex = -1;
|
|
1117
|
+
button.setAttribute('data-cms-item-tool', action);
|
|
1118
|
+
Object.assign(button.style, { display: 'grid', placeItems: 'center', width: '26px', height: '26px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', cursor: 'pointer' });
|
|
1119
|
+
button.append(drawing(action));
|
|
1120
|
+
button.addEventListener('mousedown', (event) => event.preventDefault());
|
|
1121
|
+
button.addEventListener('click', () => {
|
|
1122
|
+
if (!itemed || button.disabled)
|
|
1123
|
+
return;
|
|
1124
|
+
// Where the item will be once the page is drawn again, so the bar is still about it.
|
|
1125
|
+
next = action === 'remove' ? undefined : itemed.at + (action === 'up' ? -1 : action === 'down' ? 1 : 0);
|
|
1126
|
+
say({ type: 'item', id: itemed.section, field: itemed.list, at: itemed.at, action });
|
|
1127
|
+
});
|
|
1128
|
+
itemBar.append(button);
|
|
1129
|
+
return button;
|
|
1130
|
+
};
|
|
1131
|
+
const itemButtons = { up: itemTool('up'), down: itemTool('down'), duplicate: itemTool('duplicate'), remove: itemTool('remove') };
|
|
1132
|
+
forThing.append(itemBar);
|
|
1133
|
+
const placeItem = () => {
|
|
1134
|
+
const rect = shownRect(itemed);
|
|
1135
|
+
if (!rect || !tools || !itemed) {
|
|
1136
|
+
itemBar.style.display = 'none';
|
|
1137
|
+
itemBox.style.display = 'none';
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
place(itemBox, itemed);
|
|
1141
|
+
// Items side by side are moved sideways, and the arrows say so.
|
|
1142
|
+
const beside = itemsOf(tooled ?? itemed.element, itemed.section).find((other) => other.list === itemed?.list && other.at !== itemed.at);
|
|
1143
|
+
const sideways = beside !== undefined && Math.abs(beside.element.getBoundingClientRect().top - rect.top) < rect.height / 2;
|
|
1144
|
+
const able = { up: itemed.at > 0, down: itemed.at < itemed.count - 1, duplicate: !itemed.full, remove: true };
|
|
1145
|
+
for (const action of ['up', 'down', 'duplicate', 'remove']) {
|
|
1146
|
+
const button = itemButtons[action];
|
|
1147
|
+
button.title = word(tools.labels[`item${action.charAt(0).toUpperCase()}${action.slice(1)}`]);
|
|
1148
|
+
button.disabled = !able[action];
|
|
1149
|
+
button.style.opacity = able[action] ? '1' : '0.4';
|
|
1150
|
+
button.style.cursor = able[action] ? 'pointer' : 'default';
|
|
1151
|
+
if (action === 'up' || action === 'down')
|
|
1152
|
+
button.firstElementChild?.style.setProperty('transform', sideways ? 'rotate(-90deg)' : 'none');
|
|
1153
|
+
}
|
|
1154
|
+
itemBar.style.display = 'flex';
|
|
1155
|
+
};
|
|
1156
|
+
const shownRect = (mark) => {
|
|
1157
|
+
const rect = !typing && !writing && !drag?.moved && mark?.element.isConnected ? mark.element.getBoundingClientRect() : null;
|
|
1158
|
+
return !rect || !tools || rect.bottom <= 0 || rect.top >= page.innerHeight || (rect.width === 0 && rect.height === 0) ? null : rect;
|
|
1159
|
+
};
|
|
1160
|
+
const placeChips = () => {
|
|
1161
|
+
const picture = shownRect(pictured);
|
|
1162
|
+
changer.style.display = picture && tools ? 'flex' : 'none';
|
|
1163
|
+
if (picture && tools && changer.lastElementChild)
|
|
1164
|
+
changer.lastElementChild.textContent = word(tools.labels['picture']);
|
|
1165
|
+
place(pictureBox, picture ? pictured : null);
|
|
1166
|
+
const link = shownRect(linked);
|
|
1167
|
+
linker.style.display = link && tools ? 'flex' : 'none';
|
|
1168
|
+
if (link && tools && linker.lastElementChild)
|
|
1169
|
+
linker.lastElementChild.textContent = word(tools.labels['link']);
|
|
1170
|
+
place(linkBox, link ? linked : null);
|
|
1171
|
+
placeItem();
|
|
1172
|
+
forThing.style.display = changer.style.display === 'none' && linker.style.display === 'none' && itemBar.style.display === 'none' ? 'none' : 'flex';
|
|
1173
|
+
placeFormat();
|
|
1174
|
+
};
|
|
1175
|
+
// What the bar is about stays as it was while the pointer travels: over nothing, and across
|
|
1176
|
+
// other things on its way to the bar. It turns to another thing once the pointer rests on it.
|
|
1177
|
+
const REST = 140;
|
|
1178
|
+
let resting;
|
|
1179
|
+
const sameThing = (one, other) => one?.element === other?.element && one?.field === other?.field;
|
|
1180
|
+
const pointed = (x, y, isNow) => {
|
|
1181
|
+
page.clearTimeout(resting);
|
|
1182
|
+
resting = undefined;
|
|
1183
|
+
const picture = pictureAt(x, y);
|
|
1184
|
+
const link = linkAt(x, y);
|
|
1185
|
+
const item = itemAt(x, y);
|
|
1186
|
+
if (!picture && !link && !item)
|
|
1187
|
+
return;
|
|
1188
|
+
// Less of the same thing (the card, off its picture) is no reason to take a button away.
|
|
1189
|
+
if ((!picture || sameThing(picture, pictured)) && (!link || sameThing(link, linked)) && (!item || sameThing(item, itemed)))
|
|
1190
|
+
return;
|
|
1191
|
+
const turn = () => {
|
|
1192
|
+
resting = undefined;
|
|
1193
|
+
pictured = picture;
|
|
1194
|
+
linked = link;
|
|
1195
|
+
itemed = item;
|
|
1196
|
+
placeTools();
|
|
1197
|
+
};
|
|
1198
|
+
if (isNow || (!pictured && !linked && !itemed))
|
|
1199
|
+
turn();
|
|
1200
|
+
else
|
|
1201
|
+
resting = page.setTimeout(turn, REST);
|
|
1202
|
+
};
|
|
1203
|
+
forget = () => {
|
|
1204
|
+
page.clearTimeout(resting);
|
|
1205
|
+
resting = undefined;
|
|
1206
|
+
pictured = null;
|
|
1207
|
+
linked = null;
|
|
1208
|
+
itemed = null;
|
|
1209
|
+
};
|
|
1210
|
+
doc.addEventListener('mousemove', (event) => {
|
|
1211
|
+
// On the layer's own buttons, what they are for stays as it was.
|
|
1212
|
+
if (isElement(event.target) && layer.contains(event.target)) {
|
|
1213
|
+
page.clearTimeout(resting);
|
|
1214
|
+
resting = undefined;
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
pointed(event.clientX, event.clientY, false);
|
|
1218
|
+
}, true);
|
|
1219
|
+
placeTools = () => {
|
|
1220
|
+
placeChips();
|
|
1221
|
+
placeFillers(!typing && !writing && !drag?.moved);
|
|
1222
|
+
const hide = () => {
|
|
1223
|
+
bar.style.display = 'none';
|
|
1224
|
+
adders.addBefore.style.display = 'none';
|
|
1225
|
+
adders.addAfter.style.display = 'none';
|
|
1226
|
+
seams.addBefore.style.display = 'none';
|
|
1227
|
+
seams.addAfter.style.display = 'none';
|
|
1228
|
+
};
|
|
1229
|
+
// While words are typed, nothing sits over them.
|
|
1230
|
+
const section = typing || writing || drag?.moved ? null : tooled?.isConnected ? tooled : null;
|
|
1231
|
+
const about = section && tools?.sections[section.dataset['cmsSection'] ?? ''];
|
|
1232
|
+
if (!section || !tools || !about)
|
|
1233
|
+
return hide();
|
|
1234
|
+
const rect = section.getBoundingClientRect();
|
|
1235
|
+
if (rect.bottom <= 0 || rect.top >= page.innerHeight || (rect.width === 0 && rect.height === 0))
|
|
1236
|
+
return hide();
|
|
1237
|
+
named.textContent = word(about.name);
|
|
1238
|
+
drawDots(about, section.dataset['cmsSection'] ?? '');
|
|
1239
|
+
const able = { up: about.up === true, down: about.down === true, duplicate: about.copy === true, remove: true };
|
|
1240
|
+
for (const action of ['up', 'down', 'duplicate', 'remove']) {
|
|
1241
|
+
const button = buttons[action];
|
|
1242
|
+
button.title = word(tools.labels[action]);
|
|
1243
|
+
button.disabled = !able[action];
|
|
1244
|
+
button.style.opacity = able[action] ? '1' : '0.4';
|
|
1245
|
+
button.style.cursor = able[action] ? 'pointer' : 'default';
|
|
1246
|
+
}
|
|
1247
|
+
// At the section's top corner, and at the top of the page while the section's own top is above it:
|
|
1248
|
+
// under what the site keeps there, so the bar never covers the site's own header.
|
|
1249
|
+
sorter.title = word(tools.labels['reorder']);
|
|
1250
|
+
sorter.style.display = word(about.list) === '' && pageOrder().length > 1 ? 'grid' : 'none';
|
|
1251
|
+
more.title = word(tools.labels['addAfter']);
|
|
1252
|
+
more.style.display = about.add === true ? 'grid' : 'none';
|
|
1253
|
+
Object.assign(bar.style, { display: 'flex', top: at(Math.min(Math.max(rect.top, kept()) + 12 * grow, Math.max(rect.bottom - 44 * grow, 0))), left: 'auto', right: at(Math.max(page.innerWidth - rect.right, 0) + 12 * grow) });
|
|
1254
|
+
// A bar wider than the room to its left (a narrow column) starts at the page's edge instead.
|
|
1255
|
+
if (bar.offsetWidth > 0 && bar.getBoundingClientRect().left < 4)
|
|
1256
|
+
Object.assign(bar.style, { left: at(4), right: 'auto' });
|
|
1257
|
+
for (const [action, edge] of [['addBefore', rect.top], ['addAfter', rect.bottom]]) {
|
|
1258
|
+
const button = adders[action];
|
|
1259
|
+
// An edge that is off the page, or a page that takes no more sections, has no button.
|
|
1260
|
+
const shown = about.add === true && edge >= 14 && edge <= page.innerHeight - 14;
|
|
1261
|
+
button.style.display = shown ? 'flex' : 'none';
|
|
1262
|
+
Object.assign(seams[action].style, { display: shown ? 'block' : 'none', top: `${edge}px`, left: `${rect.left}px`, width: `${rect.width}px` });
|
|
1263
|
+
if (!shown)
|
|
1264
|
+
continue;
|
|
1265
|
+
if (button.lastElementChild)
|
|
1266
|
+
button.lastElementChild.textContent = word(tools.labels['add']);
|
|
1267
|
+
button.title = word(tools.labels[action]);
|
|
1268
|
+
Object.assign(button.style, { top: at(edge), left: at(rect.left + rect.width / 2) });
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
// Dragging a section by its bar: a line shows where it would land, and letting go says so to the editor.
|
|
1272
|
+
const line = doc.createElement('div');
|
|
1273
|
+
line.setAttribute('data-cms-drop', '');
|
|
1274
|
+
Object.assign(line.style, { position: 'absolute', display: 'none', left: '0', right: '0', height: '4px', marginTop: '-2px', borderRadius: '2px', background: ACCENT, boxShadow: '0 0 0 1px Canvas' });
|
|
1275
|
+
layer.append(line);
|
|
1276
|
+
let drag = null;
|
|
1277
|
+
const movable = () => [...doc.querySelectorAll('[data-cms-section]')].filter((section) => tools?.sections[section.dataset['cmsSection'] ?? ''] !== undefined);
|
|
1278
|
+
const aim = () => {
|
|
1279
|
+
if (!drag)
|
|
1280
|
+
return;
|
|
1281
|
+
const { id, x, y } = drag;
|
|
1282
|
+
const all = movable();
|
|
1283
|
+
const listOf = (section) => word(tools?.sections[section.dataset['cmsSection'] ?? '']?.list);
|
|
1284
|
+
const home = word(tools?.sections[id]?.list);
|
|
1285
|
+
const group = word(tools?.sections[id]?.group);
|
|
1286
|
+
const dragged = all.find((section) => section.dataset['cmsSection'] === id);
|
|
1287
|
+
// It lands among the sections it is with, or in another place the pointer is over that takes
|
|
1288
|
+
// the same blocks: the smallest such place, and never one inside the section itself.
|
|
1289
|
+
let into = home;
|
|
1290
|
+
let place;
|
|
1291
|
+
for (const slot of doc.querySelectorAll('[data-cms-slot]')) {
|
|
1292
|
+
const about = tools?.slots?.[slotOf(slot)];
|
|
1293
|
+
if (!about || about.group !== group || dragged?.contains(slot) || (slotOf(slot) !== home && about.full === true))
|
|
1294
|
+
continue;
|
|
1295
|
+
const rect = slot.getBoundingClientRect();
|
|
1296
|
+
if (x < rect.left || x > rect.right || y < rect.top || y > rect.bottom)
|
|
1297
|
+
continue;
|
|
1298
|
+
if (place && place.width * place.height <= rect.width * rect.height)
|
|
1299
|
+
continue;
|
|
1300
|
+
into = slotOf(slot);
|
|
1301
|
+
place = rect;
|
|
1302
|
+
}
|
|
1303
|
+
const sections = all.filter((section) => listOf(section) === into);
|
|
1304
|
+
const at = sections.findIndex((section) => {
|
|
1305
|
+
const rect = section.getBoundingClientRect();
|
|
1306
|
+
return y < rect.top + rect.height / 2;
|
|
1307
|
+
});
|
|
1308
|
+
const target = at < 0 ? undefined : sections[at];
|
|
1309
|
+
const own = sections.findIndex((section) => section.dataset['cmsSection'] === id);
|
|
1310
|
+
const to = at < 0 ? sections.length : at;
|
|
1311
|
+
// Before itself, or before the one after it, is where it is already.
|
|
1312
|
+
const stays = into === home && (own < 0 || to === own || to === own + 1);
|
|
1313
|
+
drag.before = stays ? undefined : (target?.dataset['cmsSection'] ?? null);
|
|
1314
|
+
drag.into = into === home ? undefined : into;
|
|
1315
|
+
const last = sections[sections.length - 1];
|
|
1316
|
+
const beside = target?.getBoundingClientRect() ?? last?.getBoundingClientRect() ?? place;
|
|
1317
|
+
const edge = target ? target.getBoundingClientRect().top : (last?.getBoundingClientRect().bottom ?? (place ? place.top + 2 : 0));
|
|
1318
|
+
// Across the page for the page's own sections, and as wide as its neighbours inside a section.
|
|
1319
|
+
const across = into === '' || !beside ? { left: '0', right: '0', width: 'auto' } : { left: `${beside.left}px`, right: 'auto', width: `${beside.width}px` };
|
|
1320
|
+
Object.assign(line.style, { display: stays ? 'none' : 'block', top: `${Math.min(Math.max(edge, 2), page.innerHeight - 2)}px`, ...across });
|
|
1321
|
+
};
|
|
1322
|
+
const endDrag = (drop) => {
|
|
1323
|
+
if (!drag)
|
|
1324
|
+
return;
|
|
1325
|
+
const { id, before, into, moved } = drag;
|
|
1326
|
+
drag = null;
|
|
1327
|
+
line.style.display = 'none';
|
|
1328
|
+
Object.assign(layer.style, { pointerEvents: 'none', cursor: '' });
|
|
1329
|
+
if (drop && moved && before !== undefined)
|
|
1330
|
+
say({ type: 'move', id, before, ...(into !== undefined && { into }) });
|
|
1331
|
+
draw();
|
|
1332
|
+
};
|
|
1333
|
+
// Near the top or the bottom of the page, it scrolls to bring the rest within reach.
|
|
1334
|
+
const creep = () => {
|
|
1335
|
+
if (!drag)
|
|
1336
|
+
return;
|
|
1337
|
+
const by = drag.y < 72 ? -14 : drag.y > page.innerHeight - 72 ? 14 : 0;
|
|
1338
|
+
if (by !== 0 && drag.moved) {
|
|
1339
|
+
page.scrollBy(0, by);
|
|
1340
|
+
aim();
|
|
1341
|
+
}
|
|
1342
|
+
page.setTimeout(creep, 16);
|
|
1343
|
+
};
|
|
1344
|
+
for (const handle of [grip, named]) {
|
|
1345
|
+
handle.addEventListener('mousedown', (event) => {
|
|
1346
|
+
const id = tooled?.dataset['cmsSection'];
|
|
1347
|
+
if (event.button !== 0 || !id || movable().length < 2)
|
|
1348
|
+
return;
|
|
1349
|
+
event.preventDefault();
|
|
1350
|
+
drag = { id, from: event.clientY, x: event.clientX, y: event.clientY, into: undefined, before: undefined, moved: false };
|
|
1351
|
+
// The layer takes the pointer while it drags, so nothing on the page answers to it.
|
|
1352
|
+
Object.assign(layer.style, { pointerEvents: 'auto', cursor: 'grabbing' });
|
|
1353
|
+
page.setTimeout(creep, 16);
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
doc.addEventListener('mousemove', (event) => {
|
|
1357
|
+
if (!drag)
|
|
1358
|
+
return;
|
|
1359
|
+
drag.x = event.clientX;
|
|
1360
|
+
drag.y = event.clientY;
|
|
1361
|
+
// A few pixels of give, so a press that was only a press moves nothing.
|
|
1362
|
+
if (Math.abs(drag.y - drag.from) > 4)
|
|
1363
|
+
drag.moved = true;
|
|
1364
|
+
// A section of the page itself is moved among the page's sections by name.
|
|
1365
|
+
if (drag.moved && word(tools?.sections[drag.id]?.list) === '') {
|
|
1366
|
+
const { id } = drag;
|
|
1367
|
+
endDrag(false);
|
|
1368
|
+
openStack(id, false, event.clientY);
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
if (drag.moved)
|
|
1372
|
+
aim();
|
|
1373
|
+
}, true);
|
|
1374
|
+
doc.addEventListener('mouseup', () => endDrag(true), true);
|
|
1375
|
+
doc.documentElement.addEventListener('mouseleave', () => endDrag(false));
|
|
1376
|
+
doc.addEventListener('keydown', (event) => {
|
|
1377
|
+
if (drag && event.key === 'Escape')
|
|
1378
|
+
endDrag(false);
|
|
1379
|
+
}, true);
|
|
1380
|
+
const ROW = 44;
|
|
1381
|
+
const STEP = 50;
|
|
1382
|
+
let stacked = null;
|
|
1383
|
+
let stackWords = { names: {}, title: '', done: '', up: '', down: '' };
|
|
1384
|
+
let shift = 0;
|
|
1385
|
+
const stackKey = `velocity-cms:stack:${page.location.pathname}`;
|
|
1386
|
+
const pageOrder = () => movable().filter((section) => word(tools?.sections[section.dataset['cmsSection'] ?? '']?.list) === '').map((section) => section.dataset['cmsSection'] ?? '');
|
|
1387
|
+
const stack = doc.createElement('div');
|
|
1388
|
+
stack.setAttribute('data-cms-stack', '');
|
|
1389
|
+
Object.assign(stack.style, { position: 'absolute', inset: '0', display: 'none', overflow: 'hidden', background: 'rgb(14 14 16 / 0.82)', backdropFilter: 'blur(6px)', color: '#fff', font: `500 14px/1 ${FONT}`, pointerEvents: 'auto', userSelect: 'none' });
|
|
1390
|
+
// The rows are as large on the person's screen as the bar is, so their places are counted in their own size.
|
|
1391
|
+
const column = grows(doc.createElement('div'));
|
|
1392
|
+
Object.assign(column.style, { position: 'absolute', top: '0', left: '50%', width: 'min(26rem, calc(100% - 32px))' });
|
|
1393
|
+
stack.append(column);
|
|
1394
|
+
layer.append(stack);
|
|
1395
|
+
const headOf = (now) => (now.stays ? 56 : 0);
|
|
1396
|
+
/** Which row the held one would land before: as many as there are rows for after the last. */
|
|
1397
|
+
const landing = (now) => Math.min(Math.max(Math.round(((now.y - shift) / grow - headOf(now)) / STEP), 0), now.order.length);
|
|
1398
|
+
const drawStack = () => {
|
|
1399
|
+
const now = stacked;
|
|
1400
|
+
stack.style.display = now ? 'block' : 'none';
|
|
1401
|
+
if (!now)
|
|
1402
|
+
return;
|
|
1403
|
+
const head = headOf(now);
|
|
1404
|
+
const dragging = now.held !== null && now.moved;
|
|
1405
|
+
const own = now.held === null ? -1 : now.order.indexOf(now.held);
|
|
1406
|
+
const to = landing(now);
|
|
1407
|
+
column.replaceChildren();
|
|
1408
|
+
Object.assign(column.style, { height: `${head + now.order.length * STEP}px`, transform: `translate(-50%, ${shift / grow}px)` });
|
|
1409
|
+
if (now.stays) {
|
|
1410
|
+
const top = doc.createElement('div');
|
|
1411
|
+
Object.assign(top.style, { position: 'absolute', top: '0', left: '0', right: '0', height: '40px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', font: `600 15px/1 ${FONT}` });
|
|
1412
|
+
const title = doc.createElement('span');
|
|
1413
|
+
title.textContent = stackWords.title;
|
|
1414
|
+
const done = doc.createElement('button');
|
|
1415
|
+
done.type = 'button';
|
|
1416
|
+
done.tabIndex = -1;
|
|
1417
|
+
done.setAttribute('data-cms-stack-done', '');
|
|
1418
|
+
done.textContent = stackWords.done;
|
|
1419
|
+
Object.assign(done.style, { height: '32px', padding: '0 14px', border: '0', borderRadius: '8px', background: ACCENT, color: ACCENT_INK, font: `600 13px/1 ${FONT}`, cursor: 'pointer' });
|
|
1420
|
+
done.addEventListener('click', () => closeStack());
|
|
1421
|
+
top.append(title, done);
|
|
1422
|
+
column.append(top);
|
|
1423
|
+
}
|
|
1424
|
+
const rowLook = { position: 'absolute', left: '0', right: '0', height: `${ROW}px`, boxSizing: 'border-box', display: 'flex', alignItems: 'center', gap: '10px', padding: '0 8px 0 12px', borderRadius: '10px' };
|
|
1425
|
+
now.order.forEach((id, at) => {
|
|
1426
|
+
const row = doc.createElement('div');
|
|
1427
|
+
row.setAttribute('data-cms-stack-row', id);
|
|
1428
|
+
const isHeld = dragging && id === now.held;
|
|
1429
|
+
Object.assign(row.style, rowLook, {
|
|
1430
|
+
top: `${head + at * STEP}px`,
|
|
1431
|
+
background: isHeld ? 'transparent' : 'rgb(255 255 255 / 0.1)',
|
|
1432
|
+
border: isHeld ? '1px dashed rgb(255 255 255 / 0.35)' : '1px solid rgb(255 255 255 / 0.14)',
|
|
1433
|
+
color: isHeld ? 'rgb(255 255 255 / 0.35)' : '#fff',
|
|
1434
|
+
cursor: now.stays ? 'grab' : 'grabbing',
|
|
1435
|
+
});
|
|
1436
|
+
const count = doc.createElement('span');
|
|
1437
|
+
count.textContent = String(at + 1);
|
|
1438
|
+
Object.assign(count.style, { width: '20px', textAlign: 'right', opacity: '0.55', font: `500 12px/1 ${FONT}` });
|
|
1439
|
+
const name = doc.createElement('span');
|
|
1440
|
+
name.textContent = stackWords.names[id] ?? '';
|
|
1441
|
+
Object.assign(name.style, { flex: '1', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' });
|
|
1442
|
+
row.append(drawing('drag'), count, name);
|
|
1443
|
+
if (now.stays) {
|
|
1444
|
+
for (const [way, label, able] of [['up', stackWords.up, at > 0], ['down', stackWords.down, at < now.order.length - 1]]) {
|
|
1445
|
+
const nudge = doc.createElement('button');
|
|
1446
|
+
nudge.type = 'button';
|
|
1447
|
+
nudge.tabIndex = -1;
|
|
1448
|
+
nudge.title = label;
|
|
1449
|
+
nudge.disabled = !able;
|
|
1450
|
+
nudge.setAttribute('data-cms-stack-nudge', way);
|
|
1451
|
+
Object.assign(nudge.style, { display: 'grid', placeItems: 'center', width: '30px', height: '30px', padding: '0', border: '0', borderRadius: '6px', background: 'transparent', color: 'inherit', opacity: able ? '1' : '0.3', cursor: able ? 'pointer' : 'default' });
|
|
1452
|
+
nudge.append(drawing(way));
|
|
1453
|
+
nudge.addEventListener('mousedown', (event) => event.stopPropagation());
|
|
1454
|
+
nudge.addEventListener('click', () => {
|
|
1455
|
+
if (able)
|
|
1456
|
+
land(id, way === 'up' ? at - 1 : at + 2);
|
|
1457
|
+
});
|
|
1458
|
+
row.append(nudge);
|
|
1459
|
+
}
|
|
1460
|
+
row.addEventListener('mousedown', (event) => {
|
|
1461
|
+
if (event.button !== 0)
|
|
1462
|
+
return;
|
|
1463
|
+
event.preventDefault();
|
|
1464
|
+
Object.assign(now, { held: id, from: event.clientY, y: event.clientY, moved: false });
|
|
1465
|
+
});
|
|
1466
|
+
}
|
|
1467
|
+
column.append(row);
|
|
1468
|
+
});
|
|
1469
|
+
if (!dragging || now.held === null)
|
|
1470
|
+
return;
|
|
1471
|
+
// Where it would land: nowhere new before itself or before the one after it.
|
|
1472
|
+
if (to !== own && to !== own + 1) {
|
|
1473
|
+
const mark = doc.createElement('div');
|
|
1474
|
+
mark.setAttribute('data-cms-stack-line', String(to));
|
|
1475
|
+
Object.assign(mark.style, { position: 'absolute', left: '-10px', right: '-10px', top: `${head + to * STEP - 6}px`, height: '6px', borderRadius: '3px', background: ACCENT, boxShadow: `0 0 0 3px color-mix(in srgb, ${ACCENT} 30%, transparent)` });
|
|
1476
|
+
column.append(mark);
|
|
1477
|
+
}
|
|
1478
|
+
const ghost = doc.createElement('div');
|
|
1479
|
+
ghost.setAttribute('data-cms-stack-held', now.held);
|
|
1480
|
+
Object.assign(ghost.style, rowLook, { top: `${(now.y - shift) / grow - ROW / 2}px`, left: '40px', right: '-40px', background: ACCENT, color: ACCENT_INK, font: `600 14px/1 ${FONT}`, boxShadow: '0 12px 32px rgb(0 0 0 / 0.45)', transform: 'rotate(-1deg)', pointerEvents: 'none', cursor: 'grabbing' });
|
|
1481
|
+
const held = doc.createElement('span');
|
|
1482
|
+
held.textContent = stackWords.names[now.held] ?? '';
|
|
1483
|
+
Object.assign(held.style, { flex: '1', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' });
|
|
1484
|
+
ghost.append(drawing('drag'), held);
|
|
1485
|
+
column.append(ghost);
|
|
1486
|
+
};
|
|
1487
|
+
const wordsNow = () => {
|
|
1488
|
+
if (!tools)
|
|
1489
|
+
return;
|
|
1490
|
+
const { labels, sections } = tools;
|
|
1491
|
+
stackWords = { names: Object.fromEntries(pageOrder().map((id) => [id, word(sections[id]?.name)])), title: word(labels['reorder']), done: word(labels['done']), up: word(labels['up']), down: word(labels['down']) };
|
|
1492
|
+
};
|
|
1493
|
+
const keepStack = () => {
|
|
1494
|
+
try {
|
|
1495
|
+
if (stacked?.stays)
|
|
1496
|
+
page.sessionStorage.setItem(stackKey, JSON.stringify({ order: stacked.order, words: stackWords }));
|
|
1497
|
+
else
|
|
1498
|
+
page.sessionStorage.removeItem(stackKey);
|
|
1499
|
+
}
|
|
1500
|
+
catch {
|
|
1501
|
+
// A page that may keep nothing opens the rows again from the bar.
|
|
1502
|
+
}
|
|
1503
|
+
};
|
|
1504
|
+
const closeStack = () => {
|
|
1505
|
+
stacked = null;
|
|
1506
|
+
keepStack();
|
|
1507
|
+
draw();
|
|
1508
|
+
drawStack();
|
|
1509
|
+
};
|
|
1510
|
+
/** The held section goes before the row at `to`, and the editor is told; the rows stay if they were opened to stay. */
|
|
1511
|
+
const land = (id, to) => {
|
|
1512
|
+
const now = stacked;
|
|
1513
|
+
if (!now)
|
|
1514
|
+
return false;
|
|
1515
|
+
const own = now.order.indexOf(id);
|
|
1516
|
+
if (own < 0 || to === own || to === own + 1)
|
|
1517
|
+
return false;
|
|
1518
|
+
const before = now.order[to] ?? null;
|
|
1519
|
+
const rest = now.order.filter((other) => other !== id);
|
|
1520
|
+
rest.splice(before === null ? rest.length : rest.indexOf(before), 0, id);
|
|
1521
|
+
now.order = rest;
|
|
1522
|
+
if (now.stays)
|
|
1523
|
+
keepStack();
|
|
1524
|
+
say({ type: 'move', id, before });
|
|
1525
|
+
drawStack();
|
|
1526
|
+
return true;
|
|
1527
|
+
};
|
|
1528
|
+
const openStack = (held, stays, y, order = pageOrder()) => {
|
|
1529
|
+
if (order.length < 2)
|
|
1530
|
+
return;
|
|
1531
|
+
wordsNow();
|
|
1532
|
+
stacked = { order, stays, held, from: y, y, moved: held !== null };
|
|
1533
|
+
const total = (headOf(stacked) + order.length * STEP) * grow;
|
|
1534
|
+
// Opened by a drag, the held section's own row is under the pointer; opened from the bar, the rows are in the middle.
|
|
1535
|
+
shift = held === null ? Math.max((page.innerHeight - total) / 2, 24) : y - (order.indexOf(held) * STEP + ROW / 2) * grow;
|
|
1536
|
+
tooled = null;
|
|
1537
|
+
forget();
|
|
1538
|
+
draw();
|
|
1539
|
+
drawStack();
|
|
1540
|
+
if (held !== null)
|
|
1541
|
+
page.setTimeout(creepStack, 16);
|
|
1542
|
+
};
|
|
1543
|
+
// Near the top or the bottom, rows out of sight come within reach.
|
|
1544
|
+
const creepStack = () => {
|
|
1545
|
+
const now = stacked;
|
|
1546
|
+
if (!now || now.held === null)
|
|
1547
|
+
return;
|
|
1548
|
+
const total = (headOf(now) + now.order.length * STEP) * grow;
|
|
1549
|
+
const by = now.y < 72 && shift < 24 ? 12 : now.y > page.innerHeight - 72 && shift + total > page.innerHeight - 24 ? -12 : 0;
|
|
1550
|
+
if (by !== 0 && now.moved) {
|
|
1551
|
+
shift += by;
|
|
1552
|
+
drawStack();
|
|
1553
|
+
}
|
|
1554
|
+
page.setTimeout(creepStack, 16);
|
|
1555
|
+
};
|
|
1556
|
+
doc.addEventListener('mousemove', (event) => {
|
|
1557
|
+
const now = stacked;
|
|
1558
|
+
if (!now || now.held === null)
|
|
1559
|
+
return;
|
|
1560
|
+
now.y = event.clientY;
|
|
1561
|
+
if (!now.moved && Math.abs(now.y - now.from) > 4) {
|
|
1562
|
+
now.moved = true;
|
|
1563
|
+
page.setTimeout(creepStack, 16);
|
|
1564
|
+
}
|
|
1565
|
+
if (now.moved)
|
|
1566
|
+
drawStack();
|
|
1567
|
+
}, true);
|
|
1568
|
+
const letGo = (drop) => {
|
|
1569
|
+
const now = stacked;
|
|
1570
|
+
if (!now || now.held === null)
|
|
1571
|
+
return;
|
|
1572
|
+
const { held, moved } = now;
|
|
1573
|
+
const to = landing(now);
|
|
1574
|
+
now.held = null;
|
|
1575
|
+
now.moved = false;
|
|
1576
|
+
const landed = drop && moved && land(held, to);
|
|
1577
|
+
if (now.stays)
|
|
1578
|
+
drawStack();
|
|
1579
|
+
// Dropped, the rows stay in their new order until the page drawn from it takes their place.
|
|
1580
|
+
else if (landed)
|
|
1581
|
+
page.setTimeout(() => stacked === now && closeStack(), 4000);
|
|
1582
|
+
else
|
|
1583
|
+
closeStack();
|
|
1584
|
+
};
|
|
1585
|
+
doc.addEventListener('mouseup', () => letGo(true), true);
|
|
1586
|
+
doc.documentElement.addEventListener('mouseleave', () => letGo(false));
|
|
1587
|
+
doc.addEventListener('keydown', (event) => {
|
|
1588
|
+
if (!stacked || event.key !== 'Escape')
|
|
1589
|
+
return;
|
|
1590
|
+
event.stopPropagation();
|
|
1591
|
+
if (stacked.held !== null && stacked.stays)
|
|
1592
|
+
letGo(false);
|
|
1593
|
+
else
|
|
1594
|
+
closeStack();
|
|
1595
|
+
}, true);
|
|
1596
|
+
// A press beside the rows closes them.
|
|
1597
|
+
stack.addEventListener('mousedown', (event) => {
|
|
1598
|
+
if (event.target === stack && stacked?.stays)
|
|
1599
|
+
closeStack();
|
|
1600
|
+
});
|
|
1601
|
+
stack.addEventListener('wheel', (event) => {
|
|
1602
|
+
const now = stacked;
|
|
1603
|
+
if (!now)
|
|
1604
|
+
return;
|
|
1605
|
+
event.preventDefault();
|
|
1606
|
+
const total = (headOf(now) + now.order.length * STEP) * grow;
|
|
1607
|
+
shift = Math.min(Math.max(shift - event.deltaY, Math.min(page.innerHeight - total - 24, 24)), 24);
|
|
1608
|
+
drawStack();
|
|
1609
|
+
}, { passive: false });
|
|
1610
|
+
// Rows that were open when a move drew the page again are open on it at once, in the new order.
|
|
1611
|
+
try {
|
|
1612
|
+
const read = JSON.parse(page.sessionStorage.getItem(stackKey) ?? 'null');
|
|
1613
|
+
const was = typeof read === 'object' && read !== null ? read : {};
|
|
1614
|
+
if (Array.isArray(was.order) && was.order.every((id) => typeof id === 'string') && typeof was.words === 'object' && was.words !== null) {
|
|
1615
|
+
const words = was.words;
|
|
1616
|
+
const names = typeof words.names === 'object' && words.names !== null ? words.names : {};
|
|
1617
|
+
stackWords = { names: Object.fromEntries(Object.entries(names).map(([id, name]) => [id, word(name)])), title: word(words.title), done: word(words.done), up: word(words.up), down: word(words.down) };
|
|
1618
|
+
const order = was.order;
|
|
1619
|
+
if (order.length > 1) {
|
|
1620
|
+
stacked = { order, stays: true, held: null, from: 0, y: 0, moved: false };
|
|
1621
|
+
shift = Math.max((page.innerHeight - (56 + order.length * STEP) * grow) / 2, 24);
|
|
1622
|
+
drawStack();
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
catch {
|
|
1627
|
+
// Nothing kept, or nothing that can be read: the rows are opened from the bar.
|
|
1628
|
+
}
|
|
1629
|
+
// Events.
|
|
1630
|
+
doc.addEventListener('mouseover', (event) => {
|
|
1631
|
+
// Over the layer's own bar and buttons, what they are for stays as it was.
|
|
1632
|
+
if (isElement(event.target) && layer.contains(event.target))
|
|
1633
|
+
return;
|
|
1634
|
+
marks.over = markOf(event.target);
|
|
1635
|
+
if (marks.over)
|
|
1636
|
+
marks.over.element.style.cursor = runFor(marks.over) ? 'text' : 'pointer';
|
|
1637
|
+
// Over what is no section (a header, a gap), the bar stays with the section it was on.
|
|
1638
|
+
const section = isElement(event.target) ? event.target.closest('[data-cms-section]') : null;
|
|
1639
|
+
if (section && section !== tooled) {
|
|
1640
|
+
tooled = section;
|
|
1641
|
+
forget();
|
|
1642
|
+
}
|
|
1643
|
+
draw();
|
|
1644
|
+
});
|
|
1645
|
+
doc.documentElement.addEventListener('mouseleave', () => {
|
|
1646
|
+
marks.over = null;
|
|
1647
|
+
tooled = null;
|
|
1648
|
+
forget();
|
|
1649
|
+
draw();
|
|
1650
|
+
});
|
|
1651
|
+
// Nothing on the page goes anywhere: a click chooses what it is on.
|
|
1652
|
+
doc.addEventListener('click', (event) => {
|
|
1653
|
+
event.preventDefault();
|
|
1654
|
+
// A press on the layer's own bar is that button's business.
|
|
1655
|
+
if (isElement(event.target) && layer.contains(event.target))
|
|
1656
|
+
return;
|
|
1657
|
+
// A press, or a tap where there is no pointer to rest, says at once what the bar is about.
|
|
1658
|
+
pointed(event.clientX, event.clientY, true);
|
|
1659
|
+
const mark = markOf(event.target);
|
|
1660
|
+
if (!mark)
|
|
1661
|
+
return;
|
|
1662
|
+
marks.chosen = mark;
|
|
1663
|
+
draw();
|
|
1664
|
+
// A field that is not being typed into here, where it is, is one for the form.
|
|
1665
|
+
const here = typing?.mark.element === mark.element || writing?.mark.element === mark.element;
|
|
1666
|
+
say({ type: 'section', id: mark.section, ...(mark.field !== undefined && { field: mark.field, ...(!here && { form: true }) }) });
|
|
1667
|
+
}, true);
|
|
1668
|
+
doc.addEventListener('submit', (event) => event.preventDefault(), true);
|
|
1669
|
+
// True once the page is where it should be: the person scrolled it, or the editor asked for a part of it.
|
|
1670
|
+
let settled = false;
|
|
1671
|
+
page.addEventListener('message', (event) => {
|
|
1672
|
+
if (event.origin !== origin || event.source !== page.parent || event.data?.source !== SOURCE)
|
|
1673
|
+
return;
|
|
1674
|
+
if (event.data.type === 'typable') {
|
|
1675
|
+
const places = event.data.places;
|
|
1676
|
+
typable = typeof places === 'object' && places !== null ? places : {};
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
if (event.data.type === 'escape') {
|
|
1680
|
+
// Escape pressed in the editor around the page: the page takes it as its own, so a drag is dropped
|
|
1681
|
+
// and the list of sections closed wherever the keyboard was.
|
|
1682
|
+
doc.dispatchEvent(new page.KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
if (event.data.type === 'linkAnswer') {
|
|
1686
|
+
answerLink(event.data.href);
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
if (event.data.type === 'zoom') {
|
|
1690
|
+
// How much smaller than life the editor shows the page: between a tenth and life size.
|
|
1691
|
+
const scale = event.data.scale;
|
|
1692
|
+
const next = typeof scale === 'number' && scale >= 0.1 && scale < 1 ? 1 / scale : 1;
|
|
1693
|
+
if (next !== grow) {
|
|
1694
|
+
grow = next;
|
|
1695
|
+
for (const element of grown) {
|
|
1696
|
+
if (grow === 1)
|
|
1697
|
+
element.style.removeProperty('zoom');
|
|
1698
|
+
else
|
|
1699
|
+
element.style.setProperty('zoom', String(grow));
|
|
1700
|
+
}
|
|
1701
|
+
draw();
|
|
1702
|
+
placeFormat();
|
|
1703
|
+
}
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
if (event.data.type === 'tools') {
|
|
1707
|
+
const said = event.data;
|
|
1708
|
+
const isRecord = (value) => typeof value === 'object' && value !== null;
|
|
1709
|
+
const slots = said.slots;
|
|
1710
|
+
tools = isRecord(said.labels) && isRecord(said.sections) ? { labels: said.labels, sections: said.sections, ...(isRecord(slots) && { slots: slots }) } : null;
|
|
1711
|
+
if (again && tools && !tooled) {
|
|
1712
|
+
const was = again;
|
|
1713
|
+
tooled = [...doc.querySelectorAll('[data-cms-section]')].find((section) => section.dataset['cmsSection'] === was.id) ?? null;
|
|
1714
|
+
if (tooled && was.list !== undefined)
|
|
1715
|
+
itemed = itemsOf(tooled, was.id).find((item) => item.list === was.list && item.at === was.at) ?? null;
|
|
1716
|
+
}
|
|
1717
|
+
if (tools)
|
|
1718
|
+
again = null;
|
|
1719
|
+
if (stacked && stacked.held === null && tools) {
|
|
1720
|
+
// The page as it is now says the order and the names.
|
|
1721
|
+
const order = pageOrder();
|
|
1722
|
+
if (order.length > 1)
|
|
1723
|
+
stacked.order = order;
|
|
1724
|
+
wordsNow();
|
|
1725
|
+
drawStack();
|
|
1726
|
+
}
|
|
1727
|
+
draw();
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
if (event.data.type !== 'show' || typeof event.data.id !== 'string')
|
|
1731
|
+
return;
|
|
1732
|
+
// While words are typed into a section, the outline stays on the field they go into.
|
|
1733
|
+
if (typing?.mark.section === event.data.id || writing?.mark.section === event.data.id)
|
|
1734
|
+
return;
|
|
1735
|
+
const mark = find(event.data.id, typeof event.data.field === 'string' ? event.data.field : undefined);
|
|
1736
|
+
if (!mark)
|
|
1737
|
+
return;
|
|
1738
|
+
marks.chosen = mark;
|
|
1739
|
+
const rect = mark.element.getBoundingClientRect();
|
|
1740
|
+
// A section asked for is brought to the top of the window, unless its top is there already (in the
|
|
1741
|
+
// upper half): the page does not jump for what is in view. Nor does a page drawn again, which is
|
|
1742
|
+
// where the person left it: the editor says so (`still`).
|
|
1743
|
+
if (event.data.still !== true && (rect.top < 0 || rect.top >= page.innerHeight / 2)) {
|
|
1744
|
+
// Room for a header that stays at the top of the page.
|
|
1745
|
+
mark.element.style.scrollMarginTop = '6rem';
|
|
1746
|
+
settled = true;
|
|
1747
|
+
mark.element.scrollIntoView({ block: 'start' });
|
|
1748
|
+
}
|
|
1749
|
+
draw();
|
|
1750
|
+
});
|
|
1751
|
+
// Scroll.
|
|
1752
|
+
// A page drawn again is where it was: by the section that was in the window and where in it, so
|
|
1753
|
+
// it stays put even when something above is taller or shorter now, and else by how far down it was.
|
|
1754
|
+
const key = `velocity-cms:scroll:${page.location.pathname}`;
|
|
1755
|
+
try {
|
|
1756
|
+
let kept = {};
|
|
1757
|
+
try {
|
|
1758
|
+
const read = JSON.parse(page.sessionStorage.getItem(key) ?? 'null');
|
|
1759
|
+
kept = typeof read === 'number' ? { y: read } : typeof read === 'object' && read !== null ? read : {};
|
|
1760
|
+
}
|
|
1761
|
+
catch {
|
|
1762
|
+
// What was kept another way is as good as nothing kept.
|
|
1763
|
+
}
|
|
1764
|
+
if (kept.on === true && typeof kept.id === 'string')
|
|
1765
|
+
again = { id: kept.id, ...(typeof kept.list === 'string' && typeof kept.at === 'number' && { list: kept.list, at: kept.at }) };
|
|
1766
|
+
const back = () => {
|
|
1767
|
+
if (typeof kept.y !== 'number' || !(kept.y > 0))
|
|
1768
|
+
return;
|
|
1769
|
+
const section = [...doc.querySelectorAll('[data-cms-section]')].find((candidate) => candidate.dataset['cmsSection'] === kept.id);
|
|
1770
|
+
const to = section && typeof kept.top === 'number' ? page.scrollY + section.getBoundingClientRect().top - kept.top : kept.y;
|
|
1771
|
+
if (Math.abs(page.scrollY - to) > 1)
|
|
1772
|
+
page.scrollTo({ top: Math.max(to, 0), behavior: 'instant' });
|
|
1773
|
+
};
|
|
1774
|
+
back();
|
|
1775
|
+
// A page that is not as tall yet as it will be (photos, fonts) is put back again as it settles,
|
|
1776
|
+
// until the person scrolls it or the editor asks for another part of it.
|
|
1777
|
+
for (const sign of ['wheel', 'touchmove', 'keydown', 'mousedown'])
|
|
1778
|
+
page.addEventListener(sign, () => (settled = true), { once: true, capture: true, passive: true });
|
|
1779
|
+
let tries = 0;
|
|
1780
|
+
const settle = () => {
|
|
1781
|
+
if (settled)
|
|
1782
|
+
return;
|
|
1783
|
+
back();
|
|
1784
|
+
if (++tries < 20)
|
|
1785
|
+
page.setTimeout(settle, 100);
|
|
1786
|
+
};
|
|
1787
|
+
page.setTimeout(settle, 100);
|
|
1788
|
+
page.addEventListener('load', () => {
|
|
1789
|
+
if (!settled)
|
|
1790
|
+
back();
|
|
1791
|
+
});
|
|
1792
|
+
page.addEventListener('pagehide', () => {
|
|
1793
|
+
// The section being worked on, or else the first one in the window.
|
|
1794
|
+
const inView = (section) => {
|
|
1795
|
+
const rect = section?.isConnected ? section.getBoundingClientRect() : null;
|
|
1796
|
+
return rect !== null && rect.bottom > 0 && rect.top < page.innerHeight;
|
|
1797
|
+
};
|
|
1798
|
+
const section = inView(tooled) ? tooled : [...doc.querySelectorAll('[data-cms-section]')].find(inView);
|
|
1799
|
+
const item = section === tooled && itemed ? { list: itemed.list, at: next ?? itemed.at } : undefined;
|
|
1800
|
+
page.sessionStorage.setItem(key, JSON.stringify({ y: page.scrollY, ...(section && { id: section.dataset['cmsSection'], top: section.getBoundingClientRect().top, on: section === tooled, ...item }) }));
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
catch {
|
|
1804
|
+
// A page that may not keep anything starts at the top.
|
|
1805
|
+
}
|
|
1806
|
+
// The page is drawn again in place: a template's fade from one page to the next is for visitors.
|
|
1807
|
+
// The turn itself is kept, since it holds the page as it was on screen until this one is ready,
|
|
1808
|
+
// and is made to take no time. The rule is the bridge's own sheet, not a change to the page's.
|
|
1809
|
+
try {
|
|
1810
|
+
const sheet = new page.CSSStyleSheet();
|
|
1811
|
+
sheet.replaceSync('::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }');
|
|
1812
|
+
doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, sheet];
|
|
1813
|
+
}
|
|
1814
|
+
catch {
|
|
1815
|
+
// A browser without such sheets shows the template's own turn.
|
|
1816
|
+
}
|
|
1817
|
+
say({ type: 'ready' });
|
|
1818
|
+
}
|
|
1819
|
+
/** The bridge as the page is sent it. */
|
|
1820
|
+
export const PREVIEW_BRIDGE_SCRIPT = `(${previewBridge.toString()})(window);\n`;
|
|
1821
|
+
//# sourceMappingURL=preview-bridge.js.map
|