@customviews-js/customviews 1.2.0 → 1.4.0
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/dist/custom-views.core.cjs.js +1409 -55
- package/dist/custom-views.core.cjs.js.map +1 -1
- package/dist/custom-views.core.esm.js +1409 -55
- package/dist/custom-views.core.esm.js.map +1 -1
- package/dist/custom-views.esm.js +1409 -55
- package/dist/custom-views.esm.js.map +1 -1
- package/dist/custom-views.js +1409 -55
- package/dist/custom-views.js.map +1 -1
- package/dist/custom-views.min.js +2 -2
- package/dist/custom-views.min.js.map +1 -1
- package/dist/types/core/anchor-engine.d.ts +55 -0
- package/dist/types/core/anchor-engine.d.ts.map +1 -0
- package/dist/types/core/config.d.ts +3 -0
- package/dist/types/core/config.d.ts.map +1 -0
- package/dist/types/core/core.d.ts +9 -1
- package/dist/types/core/core.d.ts.map +1 -1
- package/dist/types/core/custom-elements.d.ts +2 -2
- package/dist/types/core/custom-elements.d.ts.map +1 -1
- package/dist/types/core/focus-manager.d.ts +38 -0
- package/dist/types/core/focus-manager.d.ts.map +1 -0
- package/dist/types/core/share-manager.d.ts +70 -0
- package/dist/types/core/share-manager.d.ts.map +1 -0
- package/dist/types/core/toast-manager.d.ts +12 -0
- package/dist/types/core/toast-manager.d.ts.map +1 -0
- package/dist/types/core/url-state-manager.d.ts +6 -2
- package/dist/types/core/url-state-manager.d.ts.map +1 -1
- package/dist/types/core/widget.d.ts +1 -0
- package/dist/types/core/widget.d.ts.map +1 -1
- package/dist/types/styles/focus-mode-styles.d.ts +8 -0
- package/dist/types/styles/focus-mode-styles.d.ts.map +1 -0
- package/dist/types/styles/share-mode-styles.d.ts +10 -0
- package/dist/types/styles/share-mode-styles.d.ts.map +1 -0
- package/dist/types/styles/toast-styles.d.ts +4 -0
- package/dist/types/styles/toast-styles.d.ts.map +1 -0
- package/dist/types/styles/widget-styles.d.ts +1 -1
- package/dist/types/styles/widget-styles.d.ts.map +1 -1
- package/dist/types/types/types.d.ts +7 -0
- package/dist/types/types/types.d.ts.map +1 -1
- package/dist/types/utils/icons.d.ts +6 -0
- package/dist/types/utils/icons.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/custom-views.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @customviews-js/customviews v1.
|
|
2
|
+
* @customviews-js/customviews v1.4.0
|
|
3
3
|
* (c) 2025 Chan Ger Teck
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -160,7 +160,9 @@
|
|
|
160
160
|
return url.toString();
|
|
161
161
|
}
|
|
162
162
|
/**
|
|
163
|
-
* Encode state into URL-safe string
|
|
163
|
+
* Encode state into URL-safe string
|
|
164
|
+
*
|
|
165
|
+
* (Covers Toggles, Tabs and Focus currently)
|
|
164
166
|
*/
|
|
165
167
|
static encodeState(state) {
|
|
166
168
|
try {
|
|
@@ -174,6 +176,10 @@
|
|
|
174
176
|
if (state.tabs && Object.keys(state.tabs).length > 0) {
|
|
175
177
|
compact.g = Object.entries(state.tabs);
|
|
176
178
|
}
|
|
179
|
+
// Add focus if present
|
|
180
|
+
if (state.focus && state.focus.length > 0) {
|
|
181
|
+
compact.f = state.focus;
|
|
182
|
+
}
|
|
177
183
|
// Convert to JSON and encode
|
|
178
184
|
const json = JSON.stringify(compact);
|
|
179
185
|
let encoded;
|
|
@@ -195,7 +201,9 @@
|
|
|
195
201
|
}
|
|
196
202
|
}
|
|
197
203
|
/**
|
|
198
|
-
* Decode custom state from URL parameter
|
|
204
|
+
* Decode custom state from URL parameter
|
|
205
|
+
*
|
|
206
|
+
* (Covers Toggles, Tabs and Focus currently)
|
|
199
207
|
*/
|
|
200
208
|
static decodeState(encoded) {
|
|
201
209
|
try {
|
|
@@ -234,6 +242,10 @@
|
|
|
234
242
|
}
|
|
235
243
|
}
|
|
236
244
|
}
|
|
245
|
+
// Reconstruct Focus
|
|
246
|
+
if (Array.isArray(compact.f)) {
|
|
247
|
+
state.focus = compact.f;
|
|
248
|
+
}
|
|
237
249
|
return state;
|
|
238
250
|
}
|
|
239
251
|
catch (error) {
|
|
@@ -418,6 +430,20 @@
|
|
|
418
430
|
</svg>
|
|
419
431
|
`.trim();
|
|
420
432
|
}
|
|
433
|
+
function getShareIcon() {
|
|
434
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
|
435
|
+
<path fill="currentColor" d="M18 8h-2a1 1 0 0 0 0 2h2v8H6v-8h2a1 1 0 0 0 0-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8a2 2 0 0 0-2-2z"/>
|
|
436
|
+
<path fill="currentColor" d="M11 6.41V12a1 1 0 0 0 2 0V6.41l1.29 1.3a1 1 0 0 0 1.42 0a1 1 0 0 0 0-1.42l-3-3a1 1 0 0 0-1.42 0l-3 3a1 1 0 1 0 1.42 1.42L11 6.41z"/>
|
|
437
|
+
</svg>`;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* GitHub icon for footer link
|
|
441
|
+
*/
|
|
442
|
+
function getGitHubIcon() {
|
|
443
|
+
return `<svg viewBox="0 0 98 96" width="16" height="16" xmlns="http://www.w3.org/2000/svg" fill="currentColor">
|
|
444
|
+
<path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z"/>
|
|
445
|
+
</svg>`;
|
|
446
|
+
}
|
|
421
447
|
|
|
422
448
|
// Constants for selectors
|
|
423
449
|
const TABGROUP_SELECTOR$1 = 'cv-tabgroup';
|
|
@@ -1382,6 +1408,1077 @@ ${TAB_STYLES}
|
|
|
1382
1408
|
document.head.appendChild(style);
|
|
1383
1409
|
}
|
|
1384
1410
|
|
|
1411
|
+
const TOAST_STYLE_ID = 'cv-toast-styles';
|
|
1412
|
+
const TOAST_CLASS = 'cv-toast-notification';
|
|
1413
|
+
const TOAST_STYLES = `
|
|
1414
|
+
.cv-toast-notification {
|
|
1415
|
+
position: fixed;
|
|
1416
|
+
top: 20px;
|
|
1417
|
+
left: 50%;
|
|
1418
|
+
transform: translateX(-50%);
|
|
1419
|
+
background-color: #323232;
|
|
1420
|
+
color: white;
|
|
1421
|
+
padding: 12px 24px;
|
|
1422
|
+
border-radius: 4px;
|
|
1423
|
+
z-index: 100000;
|
|
1424
|
+
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
|
1425
|
+
opacity: 0;
|
|
1426
|
+
transition: opacity 0.3s ease;
|
|
1427
|
+
pointer-events: none; /* Let clicks pass through if needed, though usually it blocks */
|
|
1428
|
+
font-family: system-ui, -apple-system, sans-serif;
|
|
1429
|
+
font-size: 14px;
|
|
1430
|
+
}
|
|
1431
|
+
`;
|
|
1432
|
+
|
|
1433
|
+
/**
|
|
1434
|
+
* Manages toast notifications for the application.
|
|
1435
|
+
*/
|
|
1436
|
+
class ToastManager {
|
|
1437
|
+
static isStyleInjected = false;
|
|
1438
|
+
static toastEl = null;
|
|
1439
|
+
static navTimeout = null;
|
|
1440
|
+
static fadeTimeout = null;
|
|
1441
|
+
static show(message, duration = 2500) {
|
|
1442
|
+
this.injectStyles();
|
|
1443
|
+
// specific reuse logic
|
|
1444
|
+
if (!this.toastEl) {
|
|
1445
|
+
this.toastEl = document.createElement('div');
|
|
1446
|
+
this.toastEl.className = TOAST_CLASS;
|
|
1447
|
+
document.body.appendChild(this.toastEl);
|
|
1448
|
+
}
|
|
1449
|
+
// Reset state
|
|
1450
|
+
this.toastEl.textContent = message;
|
|
1451
|
+
this.toastEl.style.opacity = '0';
|
|
1452
|
+
this.toastEl.style.display = 'block';
|
|
1453
|
+
// Clear any pending dismissal
|
|
1454
|
+
if (this.navTimeout)
|
|
1455
|
+
clearTimeout(this.navTimeout);
|
|
1456
|
+
if (this.fadeTimeout)
|
|
1457
|
+
clearTimeout(this.fadeTimeout);
|
|
1458
|
+
// Trigger reflow & fade in
|
|
1459
|
+
requestAnimationFrame(() => {
|
|
1460
|
+
if (this.toastEl)
|
|
1461
|
+
this.toastEl.style.opacity = '1';
|
|
1462
|
+
});
|
|
1463
|
+
// Schedule fade out
|
|
1464
|
+
this.navTimeout = setTimeout(() => {
|
|
1465
|
+
if (this.toastEl)
|
|
1466
|
+
this.toastEl.style.opacity = '0';
|
|
1467
|
+
this.fadeTimeout = setTimeout(() => {
|
|
1468
|
+
if (this.toastEl)
|
|
1469
|
+
this.toastEl.style.display = 'none';
|
|
1470
|
+
}, 300);
|
|
1471
|
+
}, duration);
|
|
1472
|
+
}
|
|
1473
|
+
static injectStyles() {
|
|
1474
|
+
if (this.isStyleInjected)
|
|
1475
|
+
return;
|
|
1476
|
+
if (document.getElementById(TOAST_STYLE_ID)) {
|
|
1477
|
+
this.isStyleInjected = true;
|
|
1478
|
+
return;
|
|
1479
|
+
}
|
|
1480
|
+
const style = document.createElement('style');
|
|
1481
|
+
style.id = TOAST_STYLE_ID;
|
|
1482
|
+
style.innerHTML = TOAST_STYLES;
|
|
1483
|
+
document.head.appendChild(style);
|
|
1484
|
+
this.isStyleInjected = true;
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* Engine for generating and resolving robust anchors.
|
|
1490
|
+
*
|
|
1491
|
+
* It implements a simple anchor generation and resolution algorithm that uses a combination of
|
|
1492
|
+
* structural, contextual, and content-based hints to generate a unique anchor for a given DOM element.
|
|
1493
|
+
*
|
|
1494
|
+
* The anchor is generated by first creating an AnchorDescriptor for the element, which contains
|
|
1495
|
+
* information about the element's tag, index, parent ID, and text content. This descriptor is then
|
|
1496
|
+
* serialized into a URL-safe string using a minification algorithm.
|
|
1497
|
+
*
|
|
1498
|
+
* The anchor is then resolved by searching for the element in the DOM using the serialized string.
|
|
1499
|
+
*
|
|
1500
|
+
*/
|
|
1501
|
+
class AnchorEngine {
|
|
1502
|
+
/**
|
|
1503
|
+
* Generates a simple hash code for a string.
|
|
1504
|
+
*
|
|
1505
|
+
* It takes each character's Unicode code point and uses it to update the hash value.
|
|
1506
|
+
*/
|
|
1507
|
+
static hashCode(str) {
|
|
1508
|
+
let hash = 0;
|
|
1509
|
+
if (str.length === 0)
|
|
1510
|
+
return hash;
|
|
1511
|
+
for (let i = 0; i < str.length; i++) {
|
|
1512
|
+
const char = str.charCodeAt(i);
|
|
1513
|
+
hash = ((hash << 5) - hash) + char;
|
|
1514
|
+
hash = hash & hash; // Convert to 32bit integer
|
|
1515
|
+
}
|
|
1516
|
+
return hash;
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* Normalizes text content by removing excessive whitespace.
|
|
1520
|
+
*
|
|
1521
|
+
* It trims leading and trailing whitespace and replaces multiple spaces with a single space.
|
|
1522
|
+
*/
|
|
1523
|
+
static normalizeText(text) {
|
|
1524
|
+
return text.trim().replace(/\s+/g, ' ');
|
|
1525
|
+
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Creates an AnchorDescriptor for a given DOM element.
|
|
1528
|
+
*/
|
|
1529
|
+
static createDescriptor(el) {
|
|
1530
|
+
const tag = el.tagName;
|
|
1531
|
+
const textContent = el.textContent || "";
|
|
1532
|
+
const normalizedText = this.normalizeText(textContent);
|
|
1533
|
+
// Find nearest parent with an ID
|
|
1534
|
+
let parentId;
|
|
1535
|
+
let parent = el.parentElement;
|
|
1536
|
+
while (parent) {
|
|
1537
|
+
if (parent.id) {
|
|
1538
|
+
parentId = parent.id;
|
|
1539
|
+
break;
|
|
1540
|
+
}
|
|
1541
|
+
parent = parent.parentElement;
|
|
1542
|
+
}
|
|
1543
|
+
// Calculate index relative to the container (either the found parent or document.body)
|
|
1544
|
+
const container = parent || document.body;
|
|
1545
|
+
const siblings = Array.from(container.querySelectorAll(tag));
|
|
1546
|
+
// Index is the position of the element in the list of siblings, where siblings are those of the same tag.
|
|
1547
|
+
const index = siblings.indexOf(el);
|
|
1548
|
+
const descriptor = {
|
|
1549
|
+
tag,
|
|
1550
|
+
index: index !== -1 ? index : 0,
|
|
1551
|
+
textSnippet: normalizedText.substring(0, 32),
|
|
1552
|
+
textHash: this.hashCode(normalizedText)
|
|
1553
|
+
};
|
|
1554
|
+
if (parentId) {
|
|
1555
|
+
descriptor.parentId = parentId;
|
|
1556
|
+
}
|
|
1557
|
+
return descriptor;
|
|
1558
|
+
}
|
|
1559
|
+
/**
|
|
1560
|
+
* Serializes a list of AnchorDescriptors into a URL-safe string.
|
|
1561
|
+
*/
|
|
1562
|
+
static serialize(descriptors) {
|
|
1563
|
+
// Minify keys for compactness
|
|
1564
|
+
const minified = descriptors.map(d => ({
|
|
1565
|
+
t: d.tag,
|
|
1566
|
+
i: d.index,
|
|
1567
|
+
p: d.parentId,
|
|
1568
|
+
s: d.textSnippet,
|
|
1569
|
+
h: d.textHash
|
|
1570
|
+
}));
|
|
1571
|
+
const json = JSON.stringify(minified);
|
|
1572
|
+
// Base64 encode
|
|
1573
|
+
return btoa(encodeURIComponent(json));
|
|
1574
|
+
}
|
|
1575
|
+
/**
|
|
1576
|
+
* Deserializes a URL-safe string back into a list of AnchorDescriptors.
|
|
1577
|
+
*/
|
|
1578
|
+
static deserialize(encoded) {
|
|
1579
|
+
try {
|
|
1580
|
+
const json = decodeURIComponent(atob(encoded));
|
|
1581
|
+
const minified = JSON.parse(json);
|
|
1582
|
+
return minified.map((m) => ({
|
|
1583
|
+
tag: m.t,
|
|
1584
|
+
index: m.i,
|
|
1585
|
+
parentId: m.p,
|
|
1586
|
+
textSnippet: m.s,
|
|
1587
|
+
textHash: m.h
|
|
1588
|
+
}));
|
|
1589
|
+
}
|
|
1590
|
+
catch (e) {
|
|
1591
|
+
console.error("Failed to deserialize anchor:", e);
|
|
1592
|
+
return [];
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
/**
|
|
1596
|
+
* Finds the best DOM element match for a descriptor.
|
|
1597
|
+
*/
|
|
1598
|
+
static resolve(root, descriptor) {
|
|
1599
|
+
// 1. Scope
|
|
1600
|
+
let scope = root;
|
|
1601
|
+
if (descriptor.parentId) {
|
|
1602
|
+
const foundParent = root.querySelector(`#${descriptor.parentId}`);
|
|
1603
|
+
if (foundParent instanceof HTMLElement) {
|
|
1604
|
+
scope = foundParent;
|
|
1605
|
+
}
|
|
1606
|
+
else {
|
|
1607
|
+
// Fallback: if parent ID not found, search global root - document.body
|
|
1608
|
+
const globalParent = document.getElementById(descriptor.parentId);
|
|
1609
|
+
if (globalParent) {
|
|
1610
|
+
scope = globalParent;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
// 2. Candidate Search
|
|
1615
|
+
const candidates = Array.from(scope.querySelectorAll(descriptor.tag));
|
|
1616
|
+
// 3. Scoring
|
|
1617
|
+
let bestMatch = null;
|
|
1618
|
+
let highestScore = 0;
|
|
1619
|
+
candidates.forEach((candidate) => {
|
|
1620
|
+
let score = 0;
|
|
1621
|
+
const text = this.normalizeText(candidate.textContent || "");
|
|
1622
|
+
// Exact Text Match (Hash check is faster proxy for full string compare, but let's check hash first)
|
|
1623
|
+
if (this.hashCode(text) === descriptor.textHash) {
|
|
1624
|
+
score += 50;
|
|
1625
|
+
}
|
|
1626
|
+
else if (text.startsWith(descriptor.textSnippet)) {
|
|
1627
|
+
// Fuzzy Text Match (Snippet) - +30 score
|
|
1628
|
+
score += 30;
|
|
1629
|
+
}
|
|
1630
|
+
// Structural Match (Index)
|
|
1631
|
+
// We need to re-calculate index of this candidate to compare with descriptor.index
|
|
1632
|
+
// The descriptor.index is relative to the *found* parentId container.
|
|
1633
|
+
// So we must compare index within the scope we are searching.
|
|
1634
|
+
const siblings = Array.from(scope.querySelectorAll(descriptor.tag));
|
|
1635
|
+
const index = siblings.indexOf(candidate);
|
|
1636
|
+
if (index === descriptor.index) {
|
|
1637
|
+
score += 10;
|
|
1638
|
+
}
|
|
1639
|
+
if (score > highestScore) {
|
|
1640
|
+
highestScore = score;
|
|
1641
|
+
bestMatch = candidate;
|
|
1642
|
+
}
|
|
1643
|
+
});
|
|
1644
|
+
// 4. Winner
|
|
1645
|
+
if (highestScore > 30) {
|
|
1646
|
+
return bestMatch;
|
|
1647
|
+
}
|
|
1648
|
+
return null;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
const SHARE_MODE_STYLE_ID = 'cv-share-mode-styles';
|
|
1653
|
+
const FLOATING_ACTION_BAR_ID = 'cv-floating-action-bar';
|
|
1654
|
+
const HOVER_HELPER_ID = 'cv-hover-helper';
|
|
1655
|
+
const HIGHLIGHT_TARGET_CLASS = 'cv-highlight-target';
|
|
1656
|
+
const SELECTED_CLASS = 'cv-share-selected';
|
|
1657
|
+
/**
|
|
1658
|
+
* CSS styles to be injected during Share Mode.
|
|
1659
|
+
*/
|
|
1660
|
+
const SHARE_MODE_STYLES = `
|
|
1661
|
+
body.cv-share-mode {
|
|
1662
|
+
cursor: default;
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
/* Highlight outlines */
|
|
1666
|
+
.${HIGHLIGHT_TARGET_CLASS} {
|
|
1667
|
+
outline: 2px dashed #0078D4 !important;
|
|
1668
|
+
outline-offset: 2px;
|
|
1669
|
+
cursor: crosshair;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
.${SELECTED_CLASS} {
|
|
1673
|
+
outline: 3px solid #005a9e !important;
|
|
1674
|
+
outline-offset: 2px;
|
|
1675
|
+
background-color: rgba(0, 120, 212, 0.05);
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
/* Floating Action Bar */
|
|
1679
|
+
#${FLOATING_ACTION_BAR_ID} {
|
|
1680
|
+
position: fixed;
|
|
1681
|
+
bottom: 20px;
|
|
1682
|
+
left: 50%;
|
|
1683
|
+
transform: translateX(-50%);
|
|
1684
|
+
background-color: #2c2c2c;
|
|
1685
|
+
color: #f1f1f1;
|
|
1686
|
+
border-radius: 8px;
|
|
1687
|
+
padding: 12px 20px;
|
|
1688
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
|
1689
|
+
display: flex;
|
|
1690
|
+
align-items: center;
|
|
1691
|
+
gap: 16px;
|
|
1692
|
+
z-index: 99999;
|
|
1693
|
+
font-family: system-ui, -apple-system, sans-serif;
|
|
1694
|
+
font-size: 14px;
|
|
1695
|
+
border: 1px solid #4a4a4a;
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button {
|
|
1699
|
+
background-color: #0078D4;
|
|
1700
|
+
color: white;
|
|
1701
|
+
border: none;
|
|
1702
|
+
padding: 8px 14px;
|
|
1703
|
+
border-radius: 5px;
|
|
1704
|
+
cursor: pointer;
|
|
1705
|
+
font-weight: 500;
|
|
1706
|
+
transition: background-color 0.2s;
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button:hover {
|
|
1710
|
+
background-color: #005a9e;
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.clear {
|
|
1714
|
+
background-color: #5a5a5a;
|
|
1715
|
+
}
|
|
1716
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.clear:hover {
|
|
1717
|
+
background-color: #4a4a4a;
|
|
1718
|
+
}
|
|
1719
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.clear:hover {
|
|
1720
|
+
background-color: #4a4a4a;
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.preview {
|
|
1724
|
+
background-color: #106ebe;
|
|
1725
|
+
}
|
|
1726
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.preview:hover {
|
|
1727
|
+
background-color: #005a9e;
|
|
1728
|
+
}
|
|
1729
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.exit {
|
|
1730
|
+
background-color: #d13438;
|
|
1731
|
+
}
|
|
1732
|
+
#${FLOATING_ACTION_BAR_ID} .cv-action-button.exit:hover {
|
|
1733
|
+
background-color: #a42628;
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
/* Hover Helper (Smart Label & Level Up) */
|
|
1737
|
+
#${HOVER_HELPER_ID} {
|
|
1738
|
+
position: fixed;
|
|
1739
|
+
z-index: 99999;
|
|
1740
|
+
background-color: #333;
|
|
1741
|
+
color: white;
|
|
1742
|
+
padding: 4px 8px;
|
|
1743
|
+
border-radius: 4px;
|
|
1744
|
+
font-size: 12px;
|
|
1745
|
+
font-family: monospace;
|
|
1746
|
+
display: none;
|
|
1747
|
+
pointer-events: auto; /* Allow clicking buttons inside */
|
|
1748
|
+
align-items: center;
|
|
1749
|
+
gap: 8px;
|
|
1750
|
+
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
#${HOVER_HELPER_ID} button {
|
|
1754
|
+
background: #555;
|
|
1755
|
+
border: none;
|
|
1756
|
+
color: white;
|
|
1757
|
+
border-radius: 3px;
|
|
1758
|
+
cursor: pointer;
|
|
1759
|
+
padding: 2px 6px;
|
|
1760
|
+
font-size: 14px;
|
|
1761
|
+
line-height: 1;
|
|
1762
|
+
}
|
|
1763
|
+
#${HOVER_HELPER_ID} button:hover {
|
|
1764
|
+
background: #777;
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
`;
|
|
1768
|
+
|
|
1769
|
+
const CV_CUSTOM_ELEMENTS = 'cv-tabgroup, cv-toggle';
|
|
1770
|
+
const SHAREABLE_SELECTOR = 'div, p, blockquote, pre, li, h1, h2, h3, h4, h5, h6, [data-share], ' + CV_CUSTOM_ELEMENTS;
|
|
1771
|
+
/**
|
|
1772
|
+
* Manages the "Share Mode" for creating custom focus links.
|
|
1773
|
+
* Implementing Robust Granular Sharing with "Innermost Wins" and "Level Up" UI.
|
|
1774
|
+
*/
|
|
1775
|
+
class ShareManager {
|
|
1776
|
+
isActive = false;
|
|
1777
|
+
selectedElements = new Set();
|
|
1778
|
+
floatingBarEl = null;
|
|
1779
|
+
helperEl = null;
|
|
1780
|
+
currentHoverTarget = null;
|
|
1781
|
+
excludedTags;
|
|
1782
|
+
excludedIds;
|
|
1783
|
+
boundHandleHover;
|
|
1784
|
+
boundHandleClick;
|
|
1785
|
+
boundHandleKeydown;
|
|
1786
|
+
constructor(options) {
|
|
1787
|
+
this.excludedTags = new Set(options.excludedTags.map(t => t.toUpperCase()));
|
|
1788
|
+
this.excludedIds = new Set(options.excludedIds);
|
|
1789
|
+
this.boundHandleHover = this.handleHover.bind(this);
|
|
1790
|
+
this.boundHandleClick = this.handleClick.bind(this);
|
|
1791
|
+
this.boundHandleKeydown = this.handleKeydown.bind(this);
|
|
1792
|
+
}
|
|
1793
|
+
listeners = [];
|
|
1794
|
+
addStateChangeListener(listener) {
|
|
1795
|
+
this.listeners.push(listener);
|
|
1796
|
+
}
|
|
1797
|
+
removeStateChangeListener(listener) {
|
|
1798
|
+
this.listeners = this.listeners.filter(l => l !== listener);
|
|
1799
|
+
}
|
|
1800
|
+
notifyListeners() {
|
|
1801
|
+
this.listeners.forEach(listener => listener(this.isActive));
|
|
1802
|
+
}
|
|
1803
|
+
toggleShareMode() {
|
|
1804
|
+
this.isActive = !this.isActive;
|
|
1805
|
+
if (this.isActive) {
|
|
1806
|
+
this.activate();
|
|
1807
|
+
}
|
|
1808
|
+
else {
|
|
1809
|
+
this.cleanup();
|
|
1810
|
+
}
|
|
1811
|
+
this.notifyListeners();
|
|
1812
|
+
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Activates the share mode.
|
|
1815
|
+
* Injects styles, creates floating bar, and helper element.
|
|
1816
|
+
* Adds event listeners for hover and click.
|
|
1817
|
+
*/
|
|
1818
|
+
activate() {
|
|
1819
|
+
this.injectStyles();
|
|
1820
|
+
this.createFloatingBar();
|
|
1821
|
+
this.helperEl = this.createHelperPopover();
|
|
1822
|
+
// Event Listeners
|
|
1823
|
+
document.addEventListener('mouseover', this.boundHandleHover, true);
|
|
1824
|
+
document.addEventListener('click', this.boundHandleClick, true);
|
|
1825
|
+
document.addEventListener('keydown', this.boundHandleKeydown, true);
|
|
1826
|
+
}
|
|
1827
|
+
injectStyles() {
|
|
1828
|
+
const styleElement = document.createElement('style');
|
|
1829
|
+
styleElement.id = SHARE_MODE_STYLE_ID;
|
|
1830
|
+
styleElement.innerHTML = SHARE_MODE_STYLES;
|
|
1831
|
+
document.head.appendChild(styleElement);
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Creates the hover helper element that shows up when hovering over a shareable element.
|
|
1835
|
+
*/
|
|
1836
|
+
createHelperPopover() {
|
|
1837
|
+
const div = document.createElement('div');
|
|
1838
|
+
div.id = HOVER_HELPER_ID;
|
|
1839
|
+
div.innerHTML = `
|
|
1840
|
+
<span id="cv-helper-tag">TAG</span>
|
|
1841
|
+
<button id="cv-helper-select-btn" title="Select This Element">✓</button>
|
|
1842
|
+
<button id="cv-helper-up-btn" title="Select Parent">↰</button>
|
|
1843
|
+
`;
|
|
1844
|
+
document.body.appendChild(div);
|
|
1845
|
+
// Select parent button
|
|
1846
|
+
div.querySelector('#cv-helper-up-btn')?.addEventListener('click', (e) => {
|
|
1847
|
+
e.preventDefault();
|
|
1848
|
+
e.stopPropagation();
|
|
1849
|
+
this.handleSelectParent();
|
|
1850
|
+
});
|
|
1851
|
+
// Select element button
|
|
1852
|
+
div.querySelector('#cv-helper-select-btn')?.addEventListener('click', (e) => {
|
|
1853
|
+
e.preventDefault();
|
|
1854
|
+
e.stopPropagation();
|
|
1855
|
+
if (this.currentHoverTarget) {
|
|
1856
|
+
this.toggleSelection(this.currentHoverTarget);
|
|
1857
|
+
}
|
|
1858
|
+
});
|
|
1859
|
+
return div;
|
|
1860
|
+
}
|
|
1861
|
+
/**
|
|
1862
|
+
* Handles mouse hover events.
|
|
1863
|
+
*
|
|
1864
|
+
* This function is called when the user hovers over an element.
|
|
1865
|
+
* It checks if the element is shareable and highlights it.
|
|
1866
|
+
* If a parent element is already selected, it highlights the parent instead,
|
|
1867
|
+
* allowing the helper to remain visible for the selected parent.
|
|
1868
|
+
*
|
|
1869
|
+
* @param e The mouse event triggered by the hover.
|
|
1870
|
+
*/
|
|
1871
|
+
handleHover(e) {
|
|
1872
|
+
if (!this.isActive)
|
|
1873
|
+
return;
|
|
1874
|
+
// Check if we are hovering over the helper itself
|
|
1875
|
+
if (this.helperEl && this.helperEl.contains(e.target)) {
|
|
1876
|
+
return;
|
|
1877
|
+
}
|
|
1878
|
+
const target = e.target;
|
|
1879
|
+
// Exclude by Tag or ID
|
|
1880
|
+
const upperTag = target.tagName.toUpperCase();
|
|
1881
|
+
if (this.excludedTags.has(upperTag) || (target.id && this.excludedIds.has(target.id))) {
|
|
1882
|
+
return;
|
|
1883
|
+
}
|
|
1884
|
+
// Check closest excluded (for nested elements in excluded regions)
|
|
1885
|
+
let ancestor = target.parentElement;
|
|
1886
|
+
while (ancestor) {
|
|
1887
|
+
if (this.excludedTags.has(ancestor.tagName.toUpperCase()) || (ancestor.id && this.excludedIds.has(ancestor.id))) {
|
|
1888
|
+
return;
|
|
1889
|
+
}
|
|
1890
|
+
ancestor = ancestor.parentElement;
|
|
1891
|
+
}
|
|
1892
|
+
// Find closest shareable parent element
|
|
1893
|
+
const shareablePart = target.closest(SHAREABLE_SELECTOR);
|
|
1894
|
+
if (!shareablePart) {
|
|
1895
|
+
this.clearHover();
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
// Cast to HTMLElement
|
|
1899
|
+
const finalTarget = shareablePart;
|
|
1900
|
+
// Check if any ancestor is already selected. If so, do NOT highlight this child.
|
|
1901
|
+
// Instead, highlight (or keep highlighted) the SELECTED PARENT so the user can see the helper for it.
|
|
1902
|
+
let parent = finalTarget.parentElement;
|
|
1903
|
+
let selectedAncestor = null;
|
|
1904
|
+
// Loop outwards until we find a selected parent or reach the top
|
|
1905
|
+
while (parent) {
|
|
1906
|
+
if (this.selectedElements.has(parent)) {
|
|
1907
|
+
selectedAncestor = parent;
|
|
1908
|
+
break;
|
|
1909
|
+
}
|
|
1910
|
+
parent = parent.parentElement;
|
|
1911
|
+
}
|
|
1912
|
+
if (selectedAncestor) {
|
|
1913
|
+
// If we are hovering deep inside a selected block, show the helper for that block
|
|
1914
|
+
this.setNewHoverTarget(selectedAncestor);
|
|
1915
|
+
return;
|
|
1916
|
+
}
|
|
1917
|
+
// stop bubbling to parent
|
|
1918
|
+
// when element found for highlight
|
|
1919
|
+
e.stopPropagation();
|
|
1920
|
+
// If we are already on this target, do nothing (and keep it selected/highlighted)
|
|
1921
|
+
if (this.currentHoverTarget === finalTarget)
|
|
1922
|
+
return;
|
|
1923
|
+
// Highlight
|
|
1924
|
+
this.setNewHoverTarget(finalTarget);
|
|
1925
|
+
}
|
|
1926
|
+
setNewHoverTarget(target) {
|
|
1927
|
+
if (this.currentHoverTarget) {
|
|
1928
|
+
this.currentHoverTarget.classList.remove(HIGHLIGHT_TARGET_CLASS);
|
|
1929
|
+
}
|
|
1930
|
+
this.currentHoverTarget = target;
|
|
1931
|
+
this.currentHoverTarget.classList.add(HIGHLIGHT_TARGET_CLASS);
|
|
1932
|
+
this.positionHelper(target);
|
|
1933
|
+
}
|
|
1934
|
+
positionHelper(target) {
|
|
1935
|
+
if (!this.helperEl)
|
|
1936
|
+
return;
|
|
1937
|
+
const rect = target.getBoundingClientRect();
|
|
1938
|
+
const tagLabel = this.helperEl.querySelector('#cv-helper-tag');
|
|
1939
|
+
const upBtn = this.helperEl.querySelector('#cv-helper-up-btn');
|
|
1940
|
+
if (tagLabel)
|
|
1941
|
+
tagLabel.textContent = target.tagName;
|
|
1942
|
+
// Position at top-right of the element
|
|
1943
|
+
// Prevent going off-screen
|
|
1944
|
+
let top = rect.top - 20;
|
|
1945
|
+
if (top < 0)
|
|
1946
|
+
top = rect.top + 10; // Flip down if too close to top
|
|
1947
|
+
let left = rect.right - 80;
|
|
1948
|
+
if (left < 0)
|
|
1949
|
+
left = 10;
|
|
1950
|
+
this.helperEl.style.display = 'flex';
|
|
1951
|
+
this.helperEl.style.top = `${top}px`;
|
|
1952
|
+
this.helperEl.style.left = `${left}px`;
|
|
1953
|
+
// Update Select Button State (Tick or Cross)
|
|
1954
|
+
const selectBtn = this.helperEl.querySelector('#cv-helper-select-btn');
|
|
1955
|
+
if (selectBtn) {
|
|
1956
|
+
if (this.selectedElements.has(target)) {
|
|
1957
|
+
selectBtn.textContent = '✕';
|
|
1958
|
+
selectBtn.title = 'Deselect This Element';
|
|
1959
|
+
selectBtn.style.backgroundColor = '#d13438'; // Reddish
|
|
1960
|
+
}
|
|
1961
|
+
else {
|
|
1962
|
+
selectBtn.textContent = '✓';
|
|
1963
|
+
selectBtn.title = 'Select This Element';
|
|
1964
|
+
selectBtn.style.backgroundColor = ''; // Reset
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
// Ancestry Check
|
|
1968
|
+
const parent = target.parentElement;
|
|
1969
|
+
const parentIsShareable = parent && parent.matches(SHAREABLE_SELECTOR);
|
|
1970
|
+
if (parentIsShareable) {
|
|
1971
|
+
upBtn.style.display = 'inline-block';
|
|
1972
|
+
}
|
|
1973
|
+
else {
|
|
1974
|
+
upBtn.style.display = 'none';
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
handleSelectParent() {
|
|
1978
|
+
if (this.currentHoverTarget && this.currentHoverTarget.parentElement) {
|
|
1979
|
+
const parent = this.currentHoverTarget.parentElement;
|
|
1980
|
+
if (parent.matches(SHAREABLE_SELECTOR)) {
|
|
1981
|
+
this.setNewHoverTarget(parent);
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
handleClick(e) {
|
|
1986
|
+
if (!this.isActive)
|
|
1987
|
+
return;
|
|
1988
|
+
// If clicking helper
|
|
1989
|
+
if (this.helperEl && this.helperEl.contains(e.target))
|
|
1990
|
+
return;
|
|
1991
|
+
// If clicking floating bar
|
|
1992
|
+
if (this.floatingBarEl && this.floatingBarEl.contains(e.target))
|
|
1993
|
+
return;
|
|
1994
|
+
e.preventDefault();
|
|
1995
|
+
e.stopPropagation();
|
|
1996
|
+
if (this.currentHoverTarget) {
|
|
1997
|
+
this.toggleSelection(this.currentHoverTarget);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
handleKeydown(e) {
|
|
2001
|
+
if (!this.isActive)
|
|
2002
|
+
return;
|
|
2003
|
+
if (e.key === 'Escape') {
|
|
2004
|
+
e.preventDefault();
|
|
2005
|
+
e.stopPropagation();
|
|
2006
|
+
this.toggleShareMode();
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
/**
|
|
2010
|
+
* Toggles the selection state of a given HTML element.
|
|
2011
|
+
* Implements selection logic:
|
|
2012
|
+
* - If an ancestor of the element is already selected, the click is ignored.
|
|
2013
|
+
* - If the element being selected is a parent of already selected elements, those children are deselected.
|
|
2014
|
+
* @param el The HTMLElement to toggle selection for.
|
|
2015
|
+
*/
|
|
2016
|
+
toggleSelection(el) {
|
|
2017
|
+
if (this.selectedElements.has(el)) {
|
|
2018
|
+
this.selectedElements.delete(el);
|
|
2019
|
+
el.classList.remove(SELECTED_CLASS);
|
|
2020
|
+
}
|
|
2021
|
+
else {
|
|
2022
|
+
// Selection Logic
|
|
2023
|
+
// Scenario A: Selecting a Parent -> Remove children selected while selecting parent
|
|
2024
|
+
// Scenario B: Selecting a Child -> Ignore if ancestor selected (or handle)
|
|
2025
|
+
// B. Check if any ancestor is already selected, return if any (Scenario B)
|
|
2026
|
+
let parent = el.parentElement;
|
|
2027
|
+
while (parent) {
|
|
2028
|
+
if (this.selectedElements.has(parent)) {
|
|
2029
|
+
// Ancestor is selected. Ignore click.
|
|
2030
|
+
return;
|
|
2031
|
+
}
|
|
2032
|
+
parent = parent.parentElement;
|
|
2033
|
+
}
|
|
2034
|
+
// A. Check if any children are selected (Scenario A)
|
|
2035
|
+
// We must iterate over currently selected elements
|
|
2036
|
+
const toRemove = [];
|
|
2037
|
+
this.selectedElements.forEach(selected => {
|
|
2038
|
+
if (el.contains(selected) && el !== selected) {
|
|
2039
|
+
toRemove.push(selected);
|
|
2040
|
+
}
|
|
2041
|
+
});
|
|
2042
|
+
toRemove.forEach(child => {
|
|
2043
|
+
this.selectedElements.delete(child);
|
|
2044
|
+
child.classList.remove(SELECTED_CLASS);
|
|
2045
|
+
});
|
|
2046
|
+
// Add new selection
|
|
2047
|
+
this.selectedElements.add(el);
|
|
2048
|
+
el.classList.add(SELECTED_CLASS);
|
|
2049
|
+
}
|
|
2050
|
+
this.updateFloatingBarCount();
|
|
2051
|
+
}
|
|
2052
|
+
createFloatingBar() {
|
|
2053
|
+
const bar = document.createElement('div');
|
|
2054
|
+
bar.id = FLOATING_ACTION_BAR_ID;
|
|
2055
|
+
bar.innerHTML = `
|
|
2056
|
+
<span id="cv-selected-count">0 items selected</span>
|
|
2057
|
+
<button class="cv-action-button clear">Clear All</button>
|
|
2058
|
+
<button class="cv-action-button preview">Preview</button>
|
|
2059
|
+
<button class="cv-action-button generate">Generate Link</button>
|
|
2060
|
+
<button class="cv-action-button exit">Exit</button>
|
|
2061
|
+
`;
|
|
2062
|
+
document.body.appendChild(bar);
|
|
2063
|
+
this.floatingBarEl = bar;
|
|
2064
|
+
bar.querySelector('.clear')?.addEventListener('click', () => this.clearAll());
|
|
2065
|
+
bar.querySelector('.preview')?.addEventListener('click', () => this.previewLink());
|
|
2066
|
+
bar.querySelector('.generate')?.addEventListener('click', () => this.generateLink());
|
|
2067
|
+
bar.querySelector('.exit')?.addEventListener('click', () => this.toggleShareMode());
|
|
2068
|
+
}
|
|
2069
|
+
updateFloatingBarCount() {
|
|
2070
|
+
if (this.floatingBarEl) {
|
|
2071
|
+
const countElement = this.floatingBarEl.querySelector('#cv-selected-count');
|
|
2072
|
+
if (countElement) {
|
|
2073
|
+
const count = this.selectedElements.size;
|
|
2074
|
+
countElement.textContent = `${count} item${count === 1 ? '' : 's'} selected`;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
clearAll() {
|
|
2079
|
+
this.selectedElements.forEach(el => el.classList.remove('cv-share-selected'));
|
|
2080
|
+
this.selectedElements.clear();
|
|
2081
|
+
this.updateFloatingBarCount();
|
|
2082
|
+
}
|
|
2083
|
+
getShareUrl() {
|
|
2084
|
+
if (this.selectedElements.size === 0) {
|
|
2085
|
+
return null;
|
|
2086
|
+
}
|
|
2087
|
+
const descriptors = Array.from(this.selectedElements).map(el => AnchorEngine.createDescriptor(el));
|
|
2088
|
+
const serialized = AnchorEngine.serialize(descriptors);
|
|
2089
|
+
const url = new URL(window.location.href);
|
|
2090
|
+
url.searchParams.set('cv-focus', serialized);
|
|
2091
|
+
return url;
|
|
2092
|
+
}
|
|
2093
|
+
async generateLink() {
|
|
2094
|
+
const url = this.getShareUrl();
|
|
2095
|
+
if (!url) {
|
|
2096
|
+
ToastManager.show('Please select at least one item.');
|
|
2097
|
+
return;
|
|
2098
|
+
}
|
|
2099
|
+
try {
|
|
2100
|
+
await navigator.clipboard.writeText(url.toString());
|
|
2101
|
+
ToastManager.show('Link copied to clipboard!');
|
|
2102
|
+
}
|
|
2103
|
+
catch (e) {
|
|
2104
|
+
console.error('Clipboard failed', e);
|
|
2105
|
+
ToastManager.show('Failed to copy link.');
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
previewLink() {
|
|
2109
|
+
const url = this.getShareUrl();
|
|
2110
|
+
if (!url) {
|
|
2111
|
+
ToastManager.show('Please select at least one item.');
|
|
2112
|
+
return;
|
|
2113
|
+
}
|
|
2114
|
+
window.open(url.toString(), '_blank');
|
|
2115
|
+
}
|
|
2116
|
+
clearHover() {
|
|
2117
|
+
if (this.currentHoverTarget) {
|
|
2118
|
+
this.currentHoverTarget.classList.remove(HIGHLIGHT_TARGET_CLASS);
|
|
2119
|
+
this.currentHoverTarget = null;
|
|
2120
|
+
}
|
|
2121
|
+
if (this.helperEl) {
|
|
2122
|
+
this.helperEl.style.display = 'none';
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
cleanup() {
|
|
2126
|
+
document.body.classList.remove('cv-share-mode');
|
|
2127
|
+
this.clearAll();
|
|
2128
|
+
const style = document.getElementById(SHARE_MODE_STYLE_ID);
|
|
2129
|
+
if (style)
|
|
2130
|
+
document.head.removeChild(style);
|
|
2131
|
+
if (this.floatingBarEl) {
|
|
2132
|
+
document.body.removeChild(this.floatingBarEl);
|
|
2133
|
+
this.floatingBarEl = null;
|
|
2134
|
+
}
|
|
2135
|
+
if (this.helperEl) {
|
|
2136
|
+
document.body.removeChild(this.helperEl);
|
|
2137
|
+
this.helperEl = null;
|
|
2138
|
+
}
|
|
2139
|
+
if (this.currentHoverTarget) {
|
|
2140
|
+
this.currentHoverTarget.classList.remove(HIGHLIGHT_TARGET_CLASS);
|
|
2141
|
+
this.currentHoverTarget = null;
|
|
2142
|
+
}
|
|
2143
|
+
document.removeEventListener('mouseover', this.boundHandleHover, true);
|
|
2144
|
+
document.removeEventListener('click', this.boundHandleClick, true);
|
|
2145
|
+
document.removeEventListener('keydown', this.boundHandleKeydown, true);
|
|
2146
|
+
this.isActive = false;
|
|
2147
|
+
}
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
const FOCUS_MODE_STYLE_ID = 'cv-focus-mode-styles';
|
|
2151
|
+
const BODY_FOCUS_CLASS = 'cv-focus-mode';
|
|
2152
|
+
const HIDDEN_CLASS = 'cv-focus-hidden';
|
|
2153
|
+
const FOCUSED_CLASS = 'cv-focused-element';
|
|
2154
|
+
const DIVIDER_CLASS = 'cv-context-divider';
|
|
2155
|
+
const EXIT_BANNER_ID = 'cv-exit-focus-banner';
|
|
2156
|
+
const styles = `
|
|
2157
|
+
body.${BODY_FOCUS_CLASS} {
|
|
2158
|
+
/* e.g. potentially hide scrollbars or adjust layout */
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
.${HIDDEN_CLASS} {
|
|
2162
|
+
display: none !important;
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
.${FOCUSED_CLASS} {
|
|
2166
|
+
/* No visual style for focused elements, just logic class for now. Can add borders for debugging*/
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
.${DIVIDER_CLASS} {
|
|
2170
|
+
padding: 12px;
|
|
2171
|
+
margin: 16px 0;
|
|
2172
|
+
background-color: #f8f8f8;
|
|
2173
|
+
border-top: 1px dashed #ccc;
|
|
2174
|
+
border-bottom: 1px dashed #ccc;
|
|
2175
|
+
color: #555;
|
|
2176
|
+
text-align: center;
|
|
2177
|
+
cursor: pointer;
|
|
2178
|
+
font-family: system-ui, sans-serif;
|
|
2179
|
+
font-size: 13px;
|
|
2180
|
+
transition: background-color 0.2s;
|
|
2181
|
+
}
|
|
2182
|
+
.${DIVIDER_CLASS}:hover {
|
|
2183
|
+
background-color: #e8e8e8;
|
|
2184
|
+
color: #333;
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
#${EXIT_BANNER_ID} {
|
|
2188
|
+
position: sticky;
|
|
2189
|
+
top: 0;
|
|
2190
|
+
left: 0;
|
|
2191
|
+
right: 0;
|
|
2192
|
+
background-color: #0078D4;
|
|
2193
|
+
color: white;
|
|
2194
|
+
padding: 10px 20px;
|
|
2195
|
+
display: flex;
|
|
2196
|
+
align-items: center;
|
|
2197
|
+
justify-content: center;
|
|
2198
|
+
gap: 16px;
|
|
2199
|
+
z-index: 100000;
|
|
2200
|
+
font-family: system-ui, sans-serif;
|
|
2201
|
+
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
#${EXIT_BANNER_ID} button {
|
|
2205
|
+
background: white;
|
|
2206
|
+
color: #0078D4;
|
|
2207
|
+
border: none;
|
|
2208
|
+
padding: 4px 12px;
|
|
2209
|
+
border-radius: 4px;
|
|
2210
|
+
cursor: pointer;
|
|
2211
|
+
font-weight: 600;
|
|
2212
|
+
}
|
|
2213
|
+
#${EXIT_BANNER_ID} button:hover {
|
|
2214
|
+
background: #f0f0f0;
|
|
2215
|
+
}
|
|
2216
|
+
`;
|
|
2217
|
+
const FOCUS_MODE_STYLES = styles;
|
|
2218
|
+
|
|
2219
|
+
/**
|
|
2220
|
+
* Manages the "Focus Mode" (Presentation View).
|
|
2221
|
+
* Parses the URL for robust anchors, resolves them, hides irrelevant content, and inserts context dividers.
|
|
2222
|
+
*/
|
|
2223
|
+
const FOCUS_PARAM = 'cv-focus';
|
|
2224
|
+
class FocusManager {
|
|
2225
|
+
rootEl;
|
|
2226
|
+
hiddenElements = new Set();
|
|
2227
|
+
dividers = [];
|
|
2228
|
+
exitBanner = null;
|
|
2229
|
+
excludedTags;
|
|
2230
|
+
excludedIds;
|
|
2231
|
+
constructor(rootEl, options) {
|
|
2232
|
+
this.rootEl = rootEl;
|
|
2233
|
+
this.excludedTags = new Set(options.excludedTags.map(t => t.toUpperCase()));
|
|
2234
|
+
this.excludedIds = new Set(options.excludedIds);
|
|
2235
|
+
}
|
|
2236
|
+
/**
|
|
2237
|
+
* Initializes the Focus Manager. Checks URL for focus parameter.
|
|
2238
|
+
*/
|
|
2239
|
+
init() {
|
|
2240
|
+
this.handleUrlChange();
|
|
2241
|
+
}
|
|
2242
|
+
handleUrlChange() {
|
|
2243
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
2244
|
+
const encodedDescriptors = urlParams.get(FOCUS_PARAM);
|
|
2245
|
+
if (encodedDescriptors) {
|
|
2246
|
+
this.applyFocusMode(encodedDescriptors);
|
|
2247
|
+
}
|
|
2248
|
+
else {
|
|
2249
|
+
// encoding missing, ensure we exit focus mode if active
|
|
2250
|
+
if (document.body.classList.contains(BODY_FOCUS_CLASS)) {
|
|
2251
|
+
this.exitFocusMode();
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Applies Focus Mode based on encoded descriptors.
|
|
2257
|
+
*/
|
|
2258
|
+
applyFocusMode(encodedDescriptors) {
|
|
2259
|
+
const descriptors = AnchorEngine.deserialize(encodedDescriptors);
|
|
2260
|
+
if (!descriptors || descriptors.length === 0)
|
|
2261
|
+
return;
|
|
2262
|
+
// Resolve anchors to DOM elements
|
|
2263
|
+
const targets = [];
|
|
2264
|
+
descriptors.forEach(desc => {
|
|
2265
|
+
const el = AnchorEngine.resolve(this.rootEl, desc);
|
|
2266
|
+
if (el) {
|
|
2267
|
+
targets.push(el);
|
|
2268
|
+
}
|
|
2269
|
+
});
|
|
2270
|
+
if (targets.length === 0) {
|
|
2271
|
+
ToastManager.show("Some shared sections could not be found.");
|
|
2272
|
+
return;
|
|
2273
|
+
}
|
|
2274
|
+
if (targets.length < descriptors.length) {
|
|
2275
|
+
ToastManager.show("Some shared sections could not be found.");
|
|
2276
|
+
}
|
|
2277
|
+
this.injectStyles();
|
|
2278
|
+
document.body.classList.add(BODY_FOCUS_CLASS);
|
|
2279
|
+
this.renderFocusedView(targets);
|
|
2280
|
+
this.showExitBanner();
|
|
2281
|
+
}
|
|
2282
|
+
injectStyles() {
|
|
2283
|
+
if (document.getElementById(FOCUS_MODE_STYLE_ID))
|
|
2284
|
+
return;
|
|
2285
|
+
const style = document.createElement('style');
|
|
2286
|
+
style.id = FOCUS_MODE_STYLE_ID;
|
|
2287
|
+
style.textContent = FOCUS_MODE_STYLES;
|
|
2288
|
+
document.head.appendChild(style);
|
|
2289
|
+
}
|
|
2290
|
+
/**
|
|
2291
|
+
* Hides irrelevant content and adds dividers.
|
|
2292
|
+
*/
|
|
2293
|
+
renderFocusedView(targets) {
|
|
2294
|
+
// 1. Mark targets
|
|
2295
|
+
targets.forEach(t => t.classList.add(FOCUSED_CLASS));
|
|
2296
|
+
// 2. We need to hide siblings of targets (and their ancestors up to root generally,
|
|
2297
|
+
// but "siblings between focused zones" suggests we are mostly looking at a flat list or specific nesting).
|
|
2298
|
+
//
|
|
2299
|
+
// "All sibling elements between the focused zones are collapsed and hidden."
|
|
2300
|
+
// "If a user selects a parent element, all of its child elements must be visible."
|
|
2301
|
+
//
|
|
2302
|
+
// Algorithm:
|
|
2303
|
+
// Walk up from each target to finding the common container?
|
|
2304
|
+
// Or just assume targets are somewhat related.
|
|
2305
|
+
//
|
|
2306
|
+
// Let's implement a robust "Hide Siblings" approach.
|
|
2307
|
+
// For every target, we ensure it is visible.
|
|
2308
|
+
// We look at its siblings. If a sibling is NOT a target AND NOT an ancestor of a target, we hide it.
|
|
2309
|
+
// We need to identify all "Keep Visible" elements (targets + ancestors)
|
|
2310
|
+
const keepVisible = new Set();
|
|
2311
|
+
targets.forEach(t => {
|
|
2312
|
+
let curr = t;
|
|
2313
|
+
while (curr && curr !== document.body && curr !== document.documentElement) {
|
|
2314
|
+
keepVisible.add(curr);
|
|
2315
|
+
curr = curr.parentElement;
|
|
2316
|
+
}
|
|
2317
|
+
});
|
|
2318
|
+
// Now iterate through siblings of "Keep Visible" elements?
|
|
2319
|
+
// Actually, we can just walk the tree or iterate siblings of targets/ancestors?
|
|
2320
|
+
//
|
|
2321
|
+
// Improved Algorithm:
|
|
2322
|
+
// 1. Collect all direct siblings of every element in keepVisible set.
|
|
2323
|
+
// 2. If a sibling is NOT in keepVisible, hide it.
|
|
2324
|
+
// To avoid processing the entire DOM, we start from targets and walk up.
|
|
2325
|
+
keepVisible.forEach(el => {
|
|
2326
|
+
if (el === document.body)
|
|
2327
|
+
return; // Don't hide siblings of body (scripts etc) unless we are sure.
|
|
2328
|
+
// Actually usually we want to hide siblings of the content container.
|
|
2329
|
+
const parent = el.parentElement;
|
|
2330
|
+
if (!parent)
|
|
2331
|
+
return;
|
|
2332
|
+
// FIX: "Parent Dominance"
|
|
2333
|
+
// If the parent itself is a target (or we otherwise decided its whole content is meaningful),
|
|
2334
|
+
// then we should NOT hide anything inside it.
|
|
2335
|
+
// We check if 'parent' is one of the explicitly resolved targets.
|
|
2336
|
+
// We can check if it has the FOCUSED_CLASS class, since we added it in step 1.
|
|
2337
|
+
if (parent.classList.contains(FOCUSED_CLASS)) {
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
// Using children because we want element nodes
|
|
2341
|
+
Array.from(parent.children).forEach(child => {
|
|
2342
|
+
if (child instanceof HTMLElement && !keepVisible.has(child)) {
|
|
2343
|
+
this.hideElement(child);
|
|
2344
|
+
}
|
|
2345
|
+
});
|
|
2346
|
+
});
|
|
2347
|
+
// 3. Insert Dividers
|
|
2348
|
+
// We process each container that has hidden elements
|
|
2349
|
+
const processedContainers = new Set();
|
|
2350
|
+
keepVisible.forEach(el => {
|
|
2351
|
+
const parent = el.parentElement;
|
|
2352
|
+
if (parent && !processedContainers.has(parent)) {
|
|
2353
|
+
this.insertDividersForContainer(parent);
|
|
2354
|
+
processedContainers.add(parent);
|
|
2355
|
+
}
|
|
2356
|
+
});
|
|
2357
|
+
}
|
|
2358
|
+
hideElement(el) {
|
|
2359
|
+
if (this.hiddenElements.has(el))
|
|
2360
|
+
return; // Already hidden
|
|
2361
|
+
// Exclude by Tag
|
|
2362
|
+
if (this.excludedTags.has(el.tagName.toUpperCase()))
|
|
2363
|
+
return;
|
|
2364
|
+
// Exclude by ID (if strictly matching)
|
|
2365
|
+
if (el.id && this.excludedIds.has(el.id))
|
|
2366
|
+
return;
|
|
2367
|
+
// Also don't hide things that are aria-hidden
|
|
2368
|
+
if (el.getAttribute('aria-hidden') === 'true')
|
|
2369
|
+
return;
|
|
2370
|
+
// Exclude Toast Notification
|
|
2371
|
+
if (el.classList.contains(TOAST_CLASS))
|
|
2372
|
+
return;
|
|
2373
|
+
// We check if it is already hidden (e.g. by previous focus mode run? No, isActive check handles that)
|
|
2374
|
+
// Just mark it.
|
|
2375
|
+
el.classList.add(HIDDEN_CLASS);
|
|
2376
|
+
this.hiddenElements.add(el);
|
|
2377
|
+
}
|
|
2378
|
+
insertDividersForContainer(container) {
|
|
2379
|
+
const children = Array.from(container.children);
|
|
2380
|
+
let hiddenCount = 0;
|
|
2381
|
+
let hiddenGroupStart = null;
|
|
2382
|
+
children.forEach((child) => {
|
|
2383
|
+
if (child.classList.contains(HIDDEN_CLASS)) {
|
|
2384
|
+
if (hiddenCount === 0)
|
|
2385
|
+
hiddenGroupStart = child;
|
|
2386
|
+
hiddenCount++;
|
|
2387
|
+
}
|
|
2388
|
+
else {
|
|
2389
|
+
// Found a visible element. Was there a hidden group before this?
|
|
2390
|
+
if (hiddenCount > 0 && hiddenGroupStart) {
|
|
2391
|
+
this.createDivider(container, hiddenGroupStart, hiddenCount);
|
|
2392
|
+
hiddenCount = 0;
|
|
2393
|
+
hiddenGroupStart = null;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
});
|
|
2397
|
+
// Trailing hidden group
|
|
2398
|
+
if (hiddenCount > 0 && hiddenGroupStart) {
|
|
2399
|
+
this.createDivider(container, hiddenGroupStart, hiddenCount);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
createDivider(container, insertBeforeEl, count) {
|
|
2403
|
+
const divider = document.createElement('div');
|
|
2404
|
+
divider.className = DIVIDER_CLASS;
|
|
2405
|
+
divider.textContent = `... ${count} section${count > 1 ? 's' : ''} hidden (Click to expand) ...`;
|
|
2406
|
+
divider.onclick = () => this.expandContext(insertBeforeEl, count, divider);
|
|
2407
|
+
container.insertBefore(divider, insertBeforeEl);
|
|
2408
|
+
this.dividers.push(divider);
|
|
2409
|
+
}
|
|
2410
|
+
expandContext(firstHidden, count, divider) {
|
|
2411
|
+
// Divider is inserted BEFORE firstHidden.
|
|
2412
|
+
// So firstHidden is the first element to reveal.
|
|
2413
|
+
let curr = firstHidden;
|
|
2414
|
+
let expanded = 0;
|
|
2415
|
+
while (curr && expanded < count) {
|
|
2416
|
+
if (curr instanceof HTMLElement && curr.classList.contains(HIDDEN_CLASS)) {
|
|
2417
|
+
curr.classList.remove(HIDDEN_CLASS);
|
|
2418
|
+
this.hiddenElements.delete(curr);
|
|
2419
|
+
}
|
|
2420
|
+
curr = curr.nextElementSibling;
|
|
2421
|
+
// Note: If nested dividers or other elements exist, they shouldn't count?
|
|
2422
|
+
// "Children" iteration in insertDividers covered direct children.
|
|
2423
|
+
// sibling iteration also covers direct children.
|
|
2424
|
+
// We assume contiguous hidden siblings.
|
|
2425
|
+
expanded++;
|
|
2426
|
+
}
|
|
2427
|
+
divider.remove();
|
|
2428
|
+
const idx = this.dividers.indexOf(divider);
|
|
2429
|
+
if (idx > -1)
|
|
2430
|
+
this.dividers.splice(idx, 1);
|
|
2431
|
+
// If no more hidden elements, remove the banner
|
|
2432
|
+
if (this.hiddenElements.size === 0) {
|
|
2433
|
+
this.removeExitBanner();
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
removeExitBanner() {
|
|
2437
|
+
if (this.exitBanner) {
|
|
2438
|
+
this.exitBanner.remove();
|
|
2439
|
+
this.exitBanner = null;
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Override of renderFocusedView with robust logic
|
|
2444
|
+
*/
|
|
2445
|
+
// (We use the class method `renderFocusedView` and internal helpers)
|
|
2446
|
+
showExitBanner() {
|
|
2447
|
+
if (document.getElementById(EXIT_BANNER_ID))
|
|
2448
|
+
return;
|
|
2449
|
+
const banner = document.createElement('div');
|
|
2450
|
+
banner.id = EXIT_BANNER_ID;
|
|
2451
|
+
banner.innerHTML = `
|
|
2452
|
+
<span>You are viewing a focused selection.</span>
|
|
2453
|
+
<button id="cv-exit-focus-btn">Show Full Page</button>
|
|
2454
|
+
`;
|
|
2455
|
+
document.body.prepend(banner); // Top of body
|
|
2456
|
+
banner.querySelector('button')?.addEventListener('click', () => this.exitFocusMode());
|
|
2457
|
+
this.exitBanner = banner;
|
|
2458
|
+
}
|
|
2459
|
+
exitFocusMode() {
|
|
2460
|
+
document.body.classList.remove(BODY_FOCUS_CLASS);
|
|
2461
|
+
// Show all hidden elements
|
|
2462
|
+
this.hiddenElements.forEach(el => el.classList.remove(HIDDEN_CLASS));
|
|
2463
|
+
this.hiddenElements.clear();
|
|
2464
|
+
// Remove dividers
|
|
2465
|
+
this.dividers.forEach(d => d.remove());
|
|
2466
|
+
this.dividers = [];
|
|
2467
|
+
// Remove styling from targets
|
|
2468
|
+
const targets = document.querySelectorAll(`.${FOCUSED_CLASS}`);
|
|
2469
|
+
targets.forEach(t => t.classList.remove(FOCUSED_CLASS));
|
|
2470
|
+
// Remove banner
|
|
2471
|
+
this.removeExitBanner();
|
|
2472
|
+
// Update URL
|
|
2473
|
+
const url = new URL(window.location.href);
|
|
2474
|
+
url.searchParams.delete(FOCUS_PARAM);
|
|
2475
|
+
window.history.pushState({}, '', url.toString());
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
const DEFAULT_EXCLUDED_TAGS = ['HEADER', 'NAV', 'FOOTER', 'SCRIPT', 'STYLE'];
|
|
2480
|
+
const DEFAULT_EXCLUDED_IDS = ['cv-floating-action-bar', 'cv-hover-helper', 'cv-toast-notification'];
|
|
2481
|
+
|
|
1385
2482
|
const TOGGLE_SELECTOR = "[data-cv-toggle], [data-customviews-toggle], cv-toggle";
|
|
1386
2483
|
const TABGROUP_SELECTOR = 'cv-tabgroup';
|
|
1387
2484
|
class CustomViewsCore {
|
|
@@ -1390,6 +2487,8 @@ ${TAB_STYLES}
|
|
|
1390
2487
|
persistenceManager;
|
|
1391
2488
|
visibilityManager;
|
|
1392
2489
|
observer = null;
|
|
2490
|
+
shareManager;
|
|
2491
|
+
focusManager;
|
|
1393
2492
|
componentRegistry = {
|
|
1394
2493
|
toggles: new Set(),
|
|
1395
2494
|
tabGroups: new Set(),
|
|
@@ -1406,6 +2505,21 @@ ${TAB_STYLES}
|
|
|
1406
2505
|
this.visibilityManager = new VisibilityManager();
|
|
1407
2506
|
this.showUrlEnabled = opt.showUrl ?? false;
|
|
1408
2507
|
this.lastAppliedState = this.cloneState(this.getComputedDefaultState());
|
|
2508
|
+
// Resolve Exclusions
|
|
2509
|
+
const excludedTags = [...DEFAULT_EXCLUDED_TAGS, ...(this.config.shareExclusions?.tags || [])];
|
|
2510
|
+
const excludedIds = [...DEFAULT_EXCLUDED_IDS, ...(this.config.shareExclusions?.ids || [])];
|
|
2511
|
+
const commonOptions = { excludedTags, excludedIds };
|
|
2512
|
+
this.shareManager = new ShareManager(commonOptions);
|
|
2513
|
+
this.focusManager = new FocusManager(this.rootEl, commonOptions);
|
|
2514
|
+
}
|
|
2515
|
+
getShareManager() {
|
|
2516
|
+
return this.shareManager;
|
|
2517
|
+
}
|
|
2518
|
+
/**
|
|
2519
|
+
* Toggles the share mode on or off.
|
|
2520
|
+
*/
|
|
2521
|
+
toggleShareMode() {
|
|
2522
|
+
this.shareManager.toggleShareMode();
|
|
1409
2523
|
}
|
|
1410
2524
|
/**
|
|
1411
2525
|
* Scan the given element for toggles and tab groups, register them
|
|
@@ -1438,7 +2552,7 @@ ${TAB_STYLES}
|
|
|
1438
2552
|
return newComponentsFound;
|
|
1439
2553
|
}
|
|
1440
2554
|
/**
|
|
1441
|
-
* Unscan the given element for toggles and tab groups, de-register them
|
|
2555
|
+
* Unscan the given element for toggles and tab groups, de-register them from registry
|
|
1442
2556
|
*/
|
|
1443
2557
|
unscan(element) {
|
|
1444
2558
|
// Unscan for toggles
|
|
@@ -1542,8 +2656,10 @@ ${TAB_STYLES}
|
|
|
1542
2656
|
// For session history, clicks on back/forward button
|
|
1543
2657
|
window.addEventListener("popstate", () => {
|
|
1544
2658
|
this.loadAndCallApplyState();
|
|
2659
|
+
this.focusManager.handleUrlChange();
|
|
1545
2660
|
});
|
|
1546
2661
|
this.loadAndCallApplyState();
|
|
2662
|
+
this.focusManager.init();
|
|
1547
2663
|
this.initObserver();
|
|
1548
2664
|
}
|
|
1549
2665
|
initializeNewComponents() {
|
|
@@ -1565,7 +2681,7 @@ ${TAB_STYLES}
|
|
|
1565
2681
|
const currentToggles = this.getCurrentActiveToggles();
|
|
1566
2682
|
const newState = {
|
|
1567
2683
|
toggles: currentToggles,
|
|
1568
|
-
tabs: currentTabs
|
|
2684
|
+
tabs: currentTabs,
|
|
1569
2685
|
};
|
|
1570
2686
|
// 2. Apply state with scroll anchor information
|
|
1571
2687
|
this.applyState(newState, {
|
|
@@ -1833,10 +2949,11 @@ ${TAB_STYLES}
|
|
|
1833
2949
|
}
|
|
1834
2950
|
|
|
1835
2951
|
/**
|
|
1836
|
-
*
|
|
2952
|
+
* Defines the custom elements used by CustomViews.
|
|
1837
2953
|
*/
|
|
1838
2954
|
/**
|
|
1839
|
-
*
|
|
2955
|
+
* `<cv-tab>`: A custom element representing a single tab panel within a tab group.
|
|
2956
|
+
* Its content is displayed when the corresponding tab is active.
|
|
1840
2957
|
*/
|
|
1841
2958
|
class CVTab extends HTMLElement {
|
|
1842
2959
|
connectedCallback() {
|
|
@@ -1844,7 +2961,8 @@ ${TAB_STYLES}
|
|
|
1844
2961
|
}
|
|
1845
2962
|
}
|
|
1846
2963
|
/**
|
|
1847
|
-
*
|
|
2964
|
+
* `<cv-tabgroup>`: A custom element that encapsulates a set of tabs (`<cv-tab>`).
|
|
2965
|
+
* It manages the tab navigation and content visibility for the group.
|
|
1848
2966
|
*/
|
|
1849
2967
|
class CVTabgroup extends HTMLElement {
|
|
1850
2968
|
connectedCallback() {
|
|
@@ -1860,7 +2978,7 @@ ${TAB_STYLES}
|
|
|
1860
2978
|
}
|
|
1861
2979
|
}
|
|
1862
2980
|
/**
|
|
1863
|
-
*
|
|
2981
|
+
* `<cv-toggle>`: A custom element for creating a toggleable content block.
|
|
1864
2982
|
*/
|
|
1865
2983
|
class CVToggle extends HTMLElement {
|
|
1866
2984
|
connectedCallback() {
|
|
@@ -1868,8 +2986,8 @@ ${TAB_STYLES}
|
|
|
1868
2986
|
}
|
|
1869
2987
|
}
|
|
1870
2988
|
/**
|
|
1871
|
-
*
|
|
1872
|
-
*
|
|
2989
|
+
* `<cv-tab-header>`: A semantic container for a tab's header content.
|
|
2990
|
+
* The content of this element is used to create the navigation link for the tab.
|
|
1873
2991
|
*/
|
|
1874
2992
|
class CVTabHeader extends HTMLElement {
|
|
1875
2993
|
connectedCallback() {
|
|
@@ -1877,8 +2995,7 @@ ${TAB_STYLES}
|
|
|
1877
2995
|
}
|
|
1878
2996
|
}
|
|
1879
2997
|
/**
|
|
1880
|
-
*
|
|
1881
|
-
* Semantic container for tab panel content
|
|
2998
|
+
* `<cv-tab-body>`: A semantic container for the main content of a tab panel.
|
|
1882
2999
|
*/
|
|
1883
3000
|
class CVTabBody extends HTMLElement {
|
|
1884
3001
|
connectedCallback() {
|
|
@@ -1886,7 +3003,7 @@ ${TAB_STYLES}
|
|
|
1886
3003
|
}
|
|
1887
3004
|
}
|
|
1888
3005
|
/**
|
|
1889
|
-
*
|
|
3006
|
+
* Registers all CustomViews custom elements with the CustomElementRegistry.
|
|
1890
3007
|
*/
|
|
1891
3008
|
function registerCustomElements() {
|
|
1892
3009
|
// Only register if not already defined
|
|
@@ -2851,6 +3968,33 @@ ${TAB_STYLES}
|
|
|
2851
3968
|
padding: 0.75rem;
|
|
2852
3969
|
border-top: 1px solid rgba(0, 0, 0, 0.1);
|
|
2853
3970
|
}
|
|
3971
|
+
|
|
3972
|
+
.cv-footer-link {
|
|
3973
|
+
display: flex;
|
|
3974
|
+
align-items: center;
|
|
3975
|
+
justify-content: center;
|
|
3976
|
+
gap: 0.5rem;
|
|
3977
|
+
font-size: 0.75rem;
|
|
3978
|
+
color: rgba(0, 0, 0, 0.5);
|
|
3979
|
+
text-decoration: none;
|
|
3980
|
+
transition: color 0.2s ease;
|
|
3981
|
+
}
|
|
3982
|
+
|
|
3983
|
+
.cv-footer-link:hover {
|
|
3984
|
+
color: #3e84f4;
|
|
3985
|
+
}
|
|
3986
|
+
|
|
3987
|
+
.cv-footer-link svg {
|
|
3988
|
+
opacity: 0.8;
|
|
3989
|
+
}
|
|
3990
|
+
|
|
3991
|
+
.cv-widget-theme-dark .cv-footer-link {
|
|
3992
|
+
color: rgba(255, 255, 255, 0.4);
|
|
3993
|
+
}
|
|
3994
|
+
|
|
3995
|
+
.cv-widget-theme-dark .cv-footer-link:hover {
|
|
3996
|
+
color: #60a5fa;
|
|
3997
|
+
}
|
|
2854
3998
|
|
|
2855
3999
|
.cv-reset-btn,
|
|
2856
4000
|
.cv-share-btn {
|
|
@@ -3042,6 +4186,154 @@ ${TAB_STYLES}
|
|
|
3042
4186
|
display: none !important;
|
|
3043
4187
|
}
|
|
3044
4188
|
}
|
|
4189
|
+
/* Widget Modal Tabs */
|
|
4190
|
+
.cv-modal-tabs {
|
|
4191
|
+
display: flex;
|
|
4192
|
+
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
|
|
4193
|
+
margin-bottom: 0.5rem;
|
|
4194
|
+
}
|
|
4195
|
+
|
|
4196
|
+
.cv-tab-content > .cv-content-section + .cv-content-section {
|
|
4197
|
+
margin-top: 1.5rem;
|
|
4198
|
+
}
|
|
4199
|
+
|
|
4200
|
+
.cv-modal-tab {
|
|
4201
|
+
padding: 0.75rem 1.5rem;
|
|
4202
|
+
font-size: 0.875rem;
|
|
4203
|
+
font-weight: 500;
|
|
4204
|
+
color: rgba(0, 0, 0, 0.6);
|
|
4205
|
+
background: none;
|
|
4206
|
+
border: none;
|
|
4207
|
+
border-bottom: 2px solid transparent;
|
|
4208
|
+
cursor: pointer;
|
|
4209
|
+
transition: all 0.2s ease;
|
|
4210
|
+
}
|
|
4211
|
+
|
|
4212
|
+
.cv-modal-tab:hover {
|
|
4213
|
+
color: rgba(0, 0, 0, 0.9);
|
|
4214
|
+
}
|
|
4215
|
+
|
|
4216
|
+
.cv-modal-tab.active {
|
|
4217
|
+
color: #3e84f4;
|
|
4218
|
+
border-bottom-color: #3e84f4;
|
|
4219
|
+
}
|
|
4220
|
+
|
|
4221
|
+
.cv-tab-content {
|
|
4222
|
+
display: none;
|
|
4223
|
+
animation: fadeIn 0.3s ease;
|
|
4224
|
+
}
|
|
4225
|
+
|
|
4226
|
+
.cv-tab-content.active {
|
|
4227
|
+
display: block;
|
|
4228
|
+
}
|
|
4229
|
+
|
|
4230
|
+
/* Share Tab Content */
|
|
4231
|
+
.cv-share-content {
|
|
4232
|
+
display: flex;
|
|
4233
|
+
flex-direction: column;
|
|
4234
|
+
gap: 1rem;
|
|
4235
|
+
padding: 1rem 0;
|
|
4236
|
+
align-items: center;
|
|
4237
|
+
text-align: center;
|
|
4238
|
+
}
|
|
4239
|
+
|
|
4240
|
+
.cv-share-instruction {
|
|
4241
|
+
font-size: 0.9rem;
|
|
4242
|
+
color: rgba(0, 0, 0, 0.7);
|
|
4243
|
+
margin-bottom: 1rem;
|
|
4244
|
+
}
|
|
4245
|
+
|
|
4246
|
+
.cv-share-action-btn {
|
|
4247
|
+
display: flex;
|
|
4248
|
+
align-items: center;
|
|
4249
|
+
justify-content: center;
|
|
4250
|
+
gap: 0.5rem;
|
|
4251
|
+
width: 100%;
|
|
4252
|
+
padding: 12px 16px;
|
|
4253
|
+
background: white;
|
|
4254
|
+
color: #333;
|
|
4255
|
+
border: 1px solid rgba(0, 0, 0, 0.15);
|
|
4256
|
+
border-radius: 6px;
|
|
4257
|
+
cursor: pointer;
|
|
4258
|
+
font-size: 0.9rem;
|
|
4259
|
+
font-weight: 500;
|
|
4260
|
+
transition: all 0.2s ease;
|
|
4261
|
+
}
|
|
4262
|
+
|
|
4263
|
+
.cv-share-action-btn:hover {
|
|
4264
|
+
background: #f8f9fa;
|
|
4265
|
+
border-color: rgba(0, 0, 0, 0.25);
|
|
4266
|
+
transform: translateY(-1px);
|
|
4267
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
|
4268
|
+
}
|
|
4269
|
+
|
|
4270
|
+
.cv-share-action-btn.primary {
|
|
4271
|
+
background: #3e84f4;
|
|
4272
|
+
color: white;
|
|
4273
|
+
border-color: #3e84f4;
|
|
4274
|
+
}
|
|
4275
|
+
|
|
4276
|
+
.cv-share-action-btn.primary:hover {
|
|
4277
|
+
background: #2b74e6;
|
|
4278
|
+
border-color: #2b74e6;
|
|
4279
|
+
}
|
|
4280
|
+
|
|
4281
|
+
.cv-done-btn {
|
|
4282
|
+
padding: 0.375rem 1rem;
|
|
4283
|
+
background: #3e84f4;
|
|
4284
|
+
color: white;
|
|
4285
|
+
border: none;
|
|
4286
|
+
border-radius: 0.5rem;
|
|
4287
|
+
font-weight: 600;
|
|
4288
|
+
font-size: 0.875rem;
|
|
4289
|
+
cursor: pointer;
|
|
4290
|
+
transition: all 0.2s ease;
|
|
4291
|
+
}
|
|
4292
|
+
|
|
4293
|
+
.cv-done-btn:hover {
|
|
4294
|
+
background: #2b74e6;
|
|
4295
|
+
}
|
|
4296
|
+
|
|
4297
|
+
/* Dark Theme Adjustments */
|
|
4298
|
+
.cv-widget-theme-dark .cv-modal-tabs {
|
|
4299
|
+
border-color: rgba(255, 255, 255, 0.1);
|
|
4300
|
+
}
|
|
4301
|
+
|
|
4302
|
+
.cv-widget-theme-dark .cv-modal-tab {
|
|
4303
|
+
color: rgba(255, 255, 255, 0.6);
|
|
4304
|
+
}
|
|
4305
|
+
|
|
4306
|
+
.cv-widget-theme-dark .cv-modal-tab:hover {
|
|
4307
|
+
color: rgba(255, 255, 255, 0.9);
|
|
4308
|
+
}
|
|
4309
|
+
|
|
4310
|
+
.cv-widget-theme-dark .cv-modal-tab.active {
|
|
4311
|
+
color: #60a5fa;
|
|
4312
|
+
border-bottom-color: #60a5fa;
|
|
4313
|
+
}
|
|
4314
|
+
|
|
4315
|
+
.cv-widget-theme-dark .cv-share-instruction {
|
|
4316
|
+
color: rgba(255, 255, 255, 0.7);
|
|
4317
|
+
}
|
|
4318
|
+
|
|
4319
|
+
.cv-widget-theme-dark .cv-share-action-btn {
|
|
4320
|
+
background: #1a202c;
|
|
4321
|
+
color: white;
|
|
4322
|
+
border-color: rgba(255, 255, 255, 0.15);
|
|
4323
|
+
}
|
|
4324
|
+
|
|
4325
|
+
.cv-widget-theme-dark .cv-share-action-btn:hover {
|
|
4326
|
+
background: #2d3748;
|
|
4327
|
+
}
|
|
4328
|
+
|
|
4329
|
+
.cv-widget-theme-dark .cv-share-action-btn.primary {
|
|
4330
|
+
background: #3e84f4;
|
|
4331
|
+
border-color: #3e84f4;
|
|
4332
|
+
}
|
|
4333
|
+
|
|
4334
|
+
.cv-widget-theme-dark .cv-share-action-btn.primary:hover {
|
|
4335
|
+
background: #2b74e6;
|
|
4336
|
+
}
|
|
3045
4337
|
`;
|
|
3046
4338
|
/**
|
|
3047
4339
|
* Inject widget styles into the document head
|
|
@@ -3064,6 +4356,7 @@ ${TAB_STYLES}
|
|
|
3064
4356
|
_hasVisibleConfig = false;
|
|
3065
4357
|
pageToggleIds = new Set();
|
|
3066
4358
|
pageTabIds = new Set();
|
|
4359
|
+
currentTab = 'customize';
|
|
3067
4360
|
// Modal state
|
|
3068
4361
|
stateModal = null;
|
|
3069
4362
|
constructor(options) {
|
|
@@ -3239,12 +4532,12 @@ ${TAB_STYLES}
|
|
|
3239
4532
|
</div>
|
|
3240
4533
|
<div class="cv-tabgroup-info">
|
|
3241
4534
|
<div class="cv-tabgroup-title-container">
|
|
3242
|
-
<p class="cv-tabgroup-title">
|
|
4535
|
+
<p class="cv-tabgroup-title">Show only the selected tab</p>
|
|
3243
4536
|
</div>
|
|
3244
|
-
<p class="cv-tabgroup-description">
|
|
4537
|
+
<p class="cv-tabgroup-description">Hide the navigation headers</p>
|
|
3245
4538
|
</div>
|
|
3246
4539
|
<label class="cv-toggle-switch cv-nav-toggle">
|
|
3247
|
-
<input class="cv-nav-pref-input" type="checkbox" ${initialNavsVisible ? '
|
|
4540
|
+
<input class="cv-nav-pref-input" type="checkbox" ${initialNavsVisible ? '' : 'checked'} aria-label="Show only the selected tab" />
|
|
3248
4541
|
<span class="cv-switch-bg"></span>
|
|
3249
4542
|
<span class="cv-switch-knob"></span>
|
|
3250
4543
|
</label>
|
|
@@ -3280,36 +4573,64 @@ ${TAB_STYLES}
|
|
|
3280
4573
|
<main class="cv-modal-main">
|
|
3281
4574
|
${this.options.description ? `<p class="cv-modal-description">${this.options.description}</p>` : ''}
|
|
3282
4575
|
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
<
|
|
3286
|
-
|
|
3287
|
-
|
|
4576
|
+
<div class="cv-modal-tabs">
|
|
4577
|
+
<button class="cv-modal-tab ${this.currentTab === 'customize' ? 'active' : ''}" data-tab="customize">Customize</button>
|
|
4578
|
+
<button class="cv-modal-tab ${this.currentTab === 'share' ? 'active' : ''}" data-tab="share">Share</button>
|
|
4579
|
+
</div>
|
|
4580
|
+
|
|
4581
|
+
<div class="cv-tab-content ${this.currentTab === 'customize' ? 'active' : ''}" data-content="customize">
|
|
4582
|
+
${visibleToggles.length ? `
|
|
4583
|
+
<div class="cv-content-section">
|
|
4584
|
+
<div class="cv-section-heading">Toggles</div>
|
|
4585
|
+
<div class="cv-toggles-container">
|
|
4586
|
+
${toggleControlsHtml}
|
|
4587
|
+
</div>
|
|
4588
|
+
</div>
|
|
4589
|
+
` : ''}
|
|
4590
|
+
|
|
4591
|
+
${this.options.showTabGroups && tabGroups && tabGroups.length > 0 ? `
|
|
4592
|
+
<div class="cv-content-section">
|
|
4593
|
+
<div class="cv-section-heading">Tab Groups</div>
|
|
4594
|
+
<div class="cv-tabgroups-container">
|
|
4595
|
+
${tabGroupControlsHTML}
|
|
4596
|
+
</div>
|
|
3288
4597
|
</div>
|
|
4598
|
+
` : ''}
|
|
3289
4599
|
</div>
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
4600
|
+
|
|
4601
|
+
<div class="cv-tab-content ${this.currentTab === 'share' ? 'active' : ''}" data-content="share">
|
|
4602
|
+
<div class="cv-share-content">
|
|
4603
|
+
<div class="cv-share-instruction">
|
|
4604
|
+
Create a shareable link for your current customization, or select specific parts of the page to share.
|
|
4605
|
+
</div>
|
|
4606
|
+
|
|
4607
|
+
<button class="cv-share-action-btn primary cv-start-share-btn">
|
|
4608
|
+
<span class="cv-btn-icon">${getShareIcon()}</span>
|
|
4609
|
+
<span>Select elements to share</span>
|
|
4610
|
+
</button>
|
|
4611
|
+
|
|
4612
|
+
<button class="cv-share-action-btn cv-copy-url-btn">
|
|
4613
|
+
<span class="cv-btn-icon">${getCopyIcon()}</span>
|
|
4614
|
+
<span>Copy Shareable URL of Settings</span>
|
|
4615
|
+
</button>
|
|
3297
4616
|
</div>
|
|
3298
4617
|
</div>
|
|
3299
|
-
` : ''}
|
|
3300
4618
|
</main>
|
|
3301
4619
|
|
|
3302
4620
|
<footer class="cv-modal-footer">
|
|
3303
4621
|
${this.options.showReset ? `
|
|
3304
|
-
<button class="cv-reset-btn">
|
|
4622
|
+
<button class="cv-reset-btn" title="Reset to Default">
|
|
3305
4623
|
<span class="cv-reset-btn-icon">${getResetIcon()}</span>
|
|
3306
|
-
<span>Reset
|
|
3307
|
-
</button>
|
|
3308
|
-
` : ''}
|
|
3309
|
-
<button class="cv-share-btn">
|
|
3310
|
-
<span>Copy Shareable URL</span>
|
|
3311
|
-
<span class="cv-share-btn-icon">${getCopyIcon()}</span>
|
|
4624
|
+
<span>Reset</span>
|
|
3312
4625
|
</button>
|
|
4626
|
+
` : '<div></div>'}
|
|
4627
|
+
|
|
4628
|
+
<a href="https://github.com/customviews-js/customviews" target="_blank" class="cv-footer-link">
|
|
4629
|
+
${getGitHubIcon()}
|
|
4630
|
+
<span>View on GitHub</span>
|
|
4631
|
+
</a>
|
|
4632
|
+
|
|
4633
|
+
<button class="cv-done-btn">Done</button>
|
|
3313
4634
|
</footer>
|
|
3314
4635
|
</div>
|
|
3315
4636
|
`;
|
|
@@ -3330,20 +4651,6 @@ ${TAB_STYLES}
|
|
|
3330
4651
|
this.closeModal();
|
|
3331
4652
|
return;
|
|
3332
4653
|
}
|
|
3333
|
-
// Copy URL button
|
|
3334
|
-
if (target.closest('.cv-share-btn')) {
|
|
3335
|
-
this.copyShareableURL();
|
|
3336
|
-
const copyUrlBtn = target.closest('.cv-share-btn');
|
|
3337
|
-
const iconContainer = copyUrlBtn?.querySelector('.cv-share-btn-icon');
|
|
3338
|
-
if (iconContainer) {
|
|
3339
|
-
const originalIcon = iconContainer.innerHTML;
|
|
3340
|
-
iconContainer.innerHTML = getTickIcon();
|
|
3341
|
-
setTimeout(() => {
|
|
3342
|
-
iconContainer.innerHTML = originalIcon;
|
|
3343
|
-
}, 3000);
|
|
3344
|
-
}
|
|
3345
|
-
return;
|
|
3346
|
-
}
|
|
3347
4654
|
// Reset to default button
|
|
3348
4655
|
if (target.closest('.cv-reset-btn')) {
|
|
3349
4656
|
const resetBtn = target.closest('.cv-reset-btn');
|
|
@@ -3360,6 +4667,11 @@ ${TAB_STYLES}
|
|
|
3360
4667
|
}, 600);
|
|
3361
4668
|
return;
|
|
3362
4669
|
}
|
|
4670
|
+
// Done button
|
|
4671
|
+
if (target.closest('.cv-done-btn')) {
|
|
4672
|
+
this.closeModal();
|
|
4673
|
+
return;
|
|
4674
|
+
}
|
|
3363
4675
|
// Overlay click to close
|
|
3364
4676
|
if (e.target === this.stateModal) {
|
|
3365
4677
|
this.closeModal();
|
|
@@ -3420,10 +4732,10 @@ ${TAB_STYLES}
|
|
|
3420
4732
|
navIcon.innerHTML = isVisible ? getNavHeadingOnIcon() : getNavHeadingOffIcon();
|
|
3421
4733
|
}
|
|
3422
4734
|
};
|
|
3423
|
-
navHeaderCard.addEventListener('mouseenter', () => updateIcon(tabNavToggle.checked, true));
|
|
3424
|
-
navHeaderCard.addEventListener('mouseleave', () => updateIcon(tabNavToggle.checked, false));
|
|
4735
|
+
navHeaderCard.addEventListener('mouseenter', () => updateIcon(!tabNavToggle.checked, true));
|
|
4736
|
+
navHeaderCard.addEventListener('mouseleave', () => updateIcon(!tabNavToggle.checked, false));
|
|
3425
4737
|
tabNavToggle.addEventListener('change', () => {
|
|
3426
|
-
const visible = tabNavToggle.checked;
|
|
4738
|
+
const visible = !tabNavToggle.checked;
|
|
3427
4739
|
updateIcon(visible, false);
|
|
3428
4740
|
this.core.persistTabNavVisibility(visible);
|
|
3429
4741
|
try {
|
|
@@ -3434,6 +4746,48 @@ ${TAB_STYLES}
|
|
|
3434
4746
|
}
|
|
3435
4747
|
});
|
|
3436
4748
|
}
|
|
4749
|
+
// Tab switching
|
|
4750
|
+
const tabs = this.stateModal.querySelectorAll('.cv-modal-tab');
|
|
4751
|
+
tabs.forEach(tab => {
|
|
4752
|
+
tab.addEventListener('click', () => {
|
|
4753
|
+
const tabId = tab.dataset.tab;
|
|
4754
|
+
if (tabId === 'customize' || tabId === 'share') {
|
|
4755
|
+
this.currentTab = tabId;
|
|
4756
|
+
// Update UI without full re-render
|
|
4757
|
+
tabs.forEach(t => t.classList.remove('active'));
|
|
4758
|
+
tab.classList.add('active');
|
|
4759
|
+
const contents = this.stateModal?.querySelectorAll('.cv-tab-content');
|
|
4760
|
+
contents?.forEach(c => {
|
|
4761
|
+
c.classList.remove('active');
|
|
4762
|
+
if (c.dataset.content === tabId) {
|
|
4763
|
+
c.classList.add('active');
|
|
4764
|
+
}
|
|
4765
|
+
});
|
|
4766
|
+
}
|
|
4767
|
+
});
|
|
4768
|
+
});
|
|
4769
|
+
// Share buttons (inside content)
|
|
4770
|
+
const startShareBtn = this.stateModal.querySelector('.cv-start-share-btn');
|
|
4771
|
+
if (startShareBtn) {
|
|
4772
|
+
startShareBtn.addEventListener('click', () => {
|
|
4773
|
+
this.closeModal();
|
|
4774
|
+
this.core.toggleShareMode();
|
|
4775
|
+
});
|
|
4776
|
+
}
|
|
4777
|
+
const copyUrlBtn = this.stateModal.querySelector('.cv-copy-url-btn');
|
|
4778
|
+
if (copyUrlBtn) {
|
|
4779
|
+
copyUrlBtn.addEventListener('click', () => {
|
|
4780
|
+
this.copyShareableURL();
|
|
4781
|
+
const iconContainer = copyUrlBtn.querySelector('.cv-btn-icon');
|
|
4782
|
+
if (iconContainer) {
|
|
4783
|
+
const originalIcon = iconContainer.innerHTML;
|
|
4784
|
+
iconContainer.innerHTML = getTickIcon();
|
|
4785
|
+
setTimeout(() => {
|
|
4786
|
+
iconContainer.innerHTML = originalIcon;
|
|
4787
|
+
}, 2000);
|
|
4788
|
+
}
|
|
4789
|
+
});
|
|
4790
|
+
}
|
|
3437
4791
|
}
|
|
3438
4792
|
/**
|
|
3439
4793
|
* Apply theme class to the modal overlay based on options
|
|
@@ -3529,7 +4883,7 @@ ${TAB_STYLES}
|
|
|
3529
4883
|
const tabNavToggle = this.stateModal.querySelector('.cv-nav-pref-input');
|
|
3530
4884
|
const navIcon = this.stateModal?.querySelector('#cv-nav-icon');
|
|
3531
4885
|
if (tabNavToggle) {
|
|
3532
|
-
tabNavToggle.checked = navPref;
|
|
4886
|
+
tabNavToggle.checked = !navPref;
|
|
3533
4887
|
// Ensure UI matches actual visibility
|
|
3534
4888
|
TabManager.setNavsVisibility(document.body, navPref);
|
|
3535
4889
|
// Update the nav icon to reflect the current state
|