@seamlet/astro 0.5.0 → 0.5.1
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/README.md +1 -1
- package/dist/bridge.js +46 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -164,7 +164,7 @@ A reference to a record removed since is kept, with the issue `missing-record`:
|
|
|
164
164
|
|
|
165
165
|
## What the page hides
|
|
166
166
|
|
|
167
|
-
A field is edited where it is seen, so the editor opens what hides it when a person chooses it in the list of fields. A native `<details>` needs nothing: a click on its summary opens it in the editor too. A part the site hides and shows by itself (its own accordion, a step of a quiz, a dialog, a tab) gets the attribute `data-cms-reveal` (`REVEAL_ATTRIBUTE`). When a field inside it is chosen, the bridge sets `data-cms-revealed` on it and sends it the event `cms:reveal`, which bubbles; the site shows the part with CSS or with its own script:
|
|
167
|
+
A field is edited where it is seen, so the editor opens what hides it when a person chooses it in the list of fields. A native `<details>` needs nothing: a click on its summary opens it in the editor too. Neither do tabs, filters and toggles made of radio buttons and checkboxes and shown with CSS: a click on one or its label flips it in the editor, and the page keeps it flipped after every edit. A part the site hides and shows by itself (its own accordion, a step of a quiz, a dialog, a tab) gets the attribute `data-cms-reveal` (`REVEAL_ATTRIBUTE`). When a field inside it is chosen, the bridge sets `data-cms-revealed` on it and sends it the event `cms:reveal`, which bubbles; the site shows the part with CSS or with its own script:
|
|
168
168
|
|
|
169
169
|
```css
|
|
170
170
|
[data-cms-reveal][data-cms-revealed] {
|
package/dist/bridge.js
CHANGED
|
@@ -62,6 +62,7 @@ function asElement(value) {
|
|
|
62
62
|
// src/bridge/page-reveals.ts
|
|
63
63
|
var DETAILS_SELECTOR = "details";
|
|
64
64
|
var REVEAL_SELECTOR = `[${REVEAL_ATTRIBUTE}]`;
|
|
65
|
+
var SWITCH_SELECTOR = 'input[type="radio"], input[type="checkbox"]';
|
|
65
66
|
function revealAround(element) {
|
|
66
67
|
for (let part = element; part !== null; ) {
|
|
67
68
|
if (isDetails(part)) part.open = true;
|
|
@@ -78,14 +79,28 @@ function toggleDetailsAt(target, isOnField) {
|
|
|
78
79
|
}
|
|
79
80
|
if (!isOnField) details.open = false;
|
|
80
81
|
}
|
|
82
|
+
function flipSwitchAt(target, isOnField) {
|
|
83
|
+
const control = switchAround(target);
|
|
84
|
+
if (control === void 0) return;
|
|
85
|
+
const isRadio = control.type === "radio";
|
|
86
|
+
const isFlipped = isRadio ? !control.checked : !isOnField;
|
|
87
|
+
if (!isFlipped) return;
|
|
88
|
+
control.checked = isRadio || !control.checked;
|
|
89
|
+
announceSwitch(control);
|
|
90
|
+
}
|
|
81
91
|
function revealedPartsOf(document2) {
|
|
82
92
|
const details = [...document2.querySelectorAll(DETAILS_SELECTOR)];
|
|
83
93
|
const parts = [...document2.querySelectorAll(REVEAL_SELECTOR)];
|
|
94
|
+
const switches = [...document2.querySelectorAll(SWITCH_SELECTOR)];
|
|
84
95
|
return {
|
|
85
96
|
openDetails: indexesWhere(details, (part) => isDetails(part) && part.open),
|
|
86
97
|
revealed: indexesWhere(
|
|
87
98
|
parts,
|
|
88
99
|
(part) => part.hasAttribute(REVEALED_ATTRIBUTE)
|
|
100
|
+
),
|
|
101
|
+
flippedSwitches: indexesWhere(
|
|
102
|
+
switches,
|
|
103
|
+
(control) => isSwitch(control) && control.checked !== control.defaultChecked
|
|
89
104
|
)
|
|
90
105
|
};
|
|
91
106
|
}
|
|
@@ -100,6 +115,26 @@ function restoreRevealedParts(document2, revealed) {
|
|
|
100
115
|
const part = parts[index];
|
|
101
116
|
if (part !== void 0) reveal(part);
|
|
102
117
|
}
|
|
118
|
+
restoreSwitches(document2, revealed.flippedSwitches);
|
|
119
|
+
}
|
|
120
|
+
function restoreSwitches(document2, flipped) {
|
|
121
|
+
const switches = [...document2.querySelectorAll(SWITCH_SELECTOR)];
|
|
122
|
+
for (const index of flipped) {
|
|
123
|
+
const control = switches[index];
|
|
124
|
+
if (control === void 0 || !isSwitch(control)) continue;
|
|
125
|
+
control.checked = !control.defaultChecked;
|
|
126
|
+
announceSwitch(control);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function announceSwitch(control) {
|
|
130
|
+
control.dispatchEvent(new Event("input", { bubbles: true }));
|
|
131
|
+
control.dispatchEvent(new Event("change", { bubbles: true }));
|
|
132
|
+
}
|
|
133
|
+
function switchAround(target) {
|
|
134
|
+
if (!isElement(target)) return void 0;
|
|
135
|
+
const label = target.closest("label");
|
|
136
|
+
const control = isLabel(label) ? label.control : target;
|
|
137
|
+
return control !== null && isSwitch(control) ? control : void 0;
|
|
103
138
|
}
|
|
104
139
|
function reveal(part) {
|
|
105
140
|
if (part.hasAttribute(REVEALED_ATTRIBUTE)) return;
|
|
@@ -120,6 +155,12 @@ function indexesWhere(elements, isPicked) {
|
|
|
120
155
|
function isDetails(element) {
|
|
121
156
|
return element.localName === "details";
|
|
122
157
|
}
|
|
158
|
+
function isLabel(element) {
|
|
159
|
+
return element?.localName === "label";
|
|
160
|
+
}
|
|
161
|
+
function isSwitch(element) {
|
|
162
|
+
return element.matches(SWITCH_SELECTOR);
|
|
163
|
+
}
|
|
123
164
|
function isElement(value) {
|
|
124
165
|
return value !== null && "nodeType" in value && value.nodeType === Node.ELEMENT_NODE;
|
|
125
166
|
}
|
|
@@ -147,7 +188,9 @@ function listenToPage(document2, listeners) {
|
|
|
147
188
|
return;
|
|
148
189
|
}
|
|
149
190
|
const marked = markedElementAround(event.target);
|
|
150
|
-
|
|
191
|
+
const isOnField = marked !== void 0;
|
|
192
|
+
toggleDetailsAt(event.target, isOnField);
|
|
193
|
+
flipSwitchAt(event.target, isOnField);
|
|
151
194
|
listeners.onClick(
|
|
152
195
|
marked === void 0 ? void 0 : { ...marked, point: { x: event.clientX, y: event.clientY } }
|
|
153
196
|
);
|
|
@@ -194,6 +237,8 @@ function listenToPage(document2, listeners) {
|
|
|
194
237
|
["scroll", listeners.onLayoutChange],
|
|
195
238
|
// An opened or closed `<details>` moves what is below it; toggle does not bubble.
|
|
196
239
|
["toggle", listeners.onLayoutChange],
|
|
240
|
+
// A flipped switch shows and hides parts of the page.
|
|
241
|
+
["change", listeners.onLayoutChange],
|
|
197
242
|
// Images load after the page is shown and move what is below them; load does not bubble.
|
|
198
243
|
["load", listeners.onLayoutChange]
|
|
199
244
|
];
|
package/package.json
CHANGED