@xiee/utils 1.6.1 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/js/tabsets.js +35 -11
- package/js/tabsets.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -134,7 +134,7 @@ the em-dash.
|
|
|
134
134
|
## tabsets.js
|
|
135
135
|
|
|
136
136
|
Create tabsets from section headings and their content. See [this
|
|
137
|
-
post](https://yihui.org/en/2023/10/
|
|
137
|
+
post](https://yihui.org/en/2023/10/section-tabsets/) for documentation.
|
|
138
138
|
|
|
139
139
|
## toc.js
|
|
140
140
|
|
package/js/tabsets.js
CHANGED
|
@@ -1,19 +1,39 @@
|
|
|
1
|
-
// find an element with class `tabset` and convert its subsequent headings to tabs
|
|
1
|
+
// find an element with class `tabset` and convert its subsequent bullet list or headings to tabs;
|
|
2
|
+
// see documentation at: https://yihui.org/en/2023/10/section-tabsets/
|
|
2
3
|
document.querySelectorAll('.tabset').forEach(h => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// using headings to create a tabset later if it's empty
|
|
6
|
-
if (links.length === 0) {
|
|
7
|
-
links = []; panes = [];
|
|
8
|
-
}
|
|
4
|
+
const links = [...h.querySelectorAll(':scope > .tab-link')],
|
|
5
|
+
panes = [...h.querySelectorAll(':scope > .tab-pane')];
|
|
9
6
|
function activate(i) {
|
|
10
7
|
function a(x, i) {
|
|
11
8
|
x.forEach((el, k) => el.classList[k === i ? 'add' : 'remove']('active'));
|
|
12
9
|
}
|
|
13
10
|
a(links, i); a(panes, i);
|
|
14
11
|
}
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
function newEl(tag, cls) {
|
|
13
|
+
const el = document.createElement(tag);
|
|
14
|
+
el.className = cls;
|
|
15
|
+
return el;
|
|
16
|
+
}
|
|
17
|
+
let n = -1, el = h.nextElementSibling, p;
|
|
18
|
+
// if the first sibling is <ul>, try to convert it to tabset
|
|
19
|
+
if (links.length === 0 && el.tagName === 'UL') {
|
|
20
|
+
[...el.children].forEach(li => {
|
|
21
|
+
const l = li.firstElementChild;
|
|
22
|
+
if (!l) return;
|
|
23
|
+
const l2 = newEl('div', 'tab-link');
|
|
24
|
+
l2.append(l);
|
|
25
|
+
l.outerHTML = l.innerHTML;
|
|
26
|
+
el.before(l2);
|
|
27
|
+
const p = newEl('div', 'tab-pane');
|
|
28
|
+
l2.after(p);
|
|
29
|
+
[...li.children].forEach(l => p.append(l));
|
|
30
|
+
links.push(l2); panes.push(p);
|
|
31
|
+
});
|
|
32
|
+
el.remove();
|
|
33
|
+
}
|
|
34
|
+
// create a tabset using headings if the above didn't work
|
|
35
|
+
if (links.length === 0) while (el) {
|
|
36
|
+
// convert headings to tabs
|
|
17
37
|
if (el.nodeName === '#comment' && el.nodeValue.trim() === `tabset:${h.id}`)
|
|
18
38
|
break; // quit after <!--tabset:id-->
|
|
19
39
|
const t = el.tagName;
|
|
@@ -23,8 +43,7 @@ document.querySelectorAll('.tabset').forEach(h => {
|
|
|
23
43
|
if (n < 0) n = n2 - 1;
|
|
24
44
|
// find the next lower-level heading and start creating a tab
|
|
25
45
|
if (n2 === n + 1) {
|
|
26
|
-
p =
|
|
27
|
-
p.className = 'tab-pane';
|
|
46
|
+
p = newEl('div', 'tab-pane');
|
|
28
47
|
el.after(p);
|
|
29
48
|
el.classList.add('tab-link');
|
|
30
49
|
el.querySelector('.anchor')?.remove();
|
|
@@ -41,6 +60,11 @@ document.querySelectorAll('.tabset').forEach(h => {
|
|
|
41
60
|
}
|
|
42
61
|
el = el.nextSibling;
|
|
43
62
|
}
|
|
63
|
+
// if the initial tabset container is empty, move links and panes into it
|
|
64
|
+
if (h.innerText.trim() == '') {
|
|
65
|
+
links.forEach(l => h.append(l));
|
|
66
|
+
panes.forEach(p => h.append(p));
|
|
67
|
+
}
|
|
44
68
|
// activate one tab initially if none is active
|
|
45
69
|
let init = 0;
|
|
46
70
|
links.forEach((l, i) => {
|
package/js/tabsets.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
document.querySelectorAll(".tabset").forEach((e=>{
|
|
1
|
+
document.querySelectorAll(".tabset").forEach((e=>{const t=[...e.querySelectorAll(":scope > .tab-link")],n=[...e.querySelectorAll(":scope > .tab-pane")];function a(e){function a(e,t){e.forEach(((e,n)=>e.classList[n===t?"add":"remove"]("active")))}a(t,e),a(n,e)}function r(e,t){const n=document.createElement(e);return n.className=t,n}let c,i=-1,o=e.nextElementSibling;if(0===t.length&&"UL"===o.tagName&&([...o.children].forEach((e=>{const a=e.firstElementChild;if(!a)return;const c=r("div","tab-link");c.append(a),a.outerHTML=a.innerHTML,o.before(c);const i=r("div","tab-pane");c.after(i),[...e.children].forEach((e=>i.append(e))),t.push(c),n.push(i)})),o.remove()),0===t.length)for(;o&&("#comment"!==o.nodeName||o.nodeValue.trim()!==`tabset:${e.id}`);){const e=o.tagName;if(/^H[1-6]$/.test(e)){const a=+e.replace("H","");if(a<=i)break;if(i<0&&(i=a-1),a===i+1){c=r("div","tab-pane"),o.after(c),o.classList.add("tab-link"),o.querySelector(".anchor")?.remove(),o.outerHTML=o.outerHTML.replace(/^<h[1-6](.*)h[1-6]>$/,"<div$1div>"),o=c.previousElementSibling,t.push(o),n.push(c),o=c.nextSibling;continue}}c&&(c.append(o),o=c),o=o.nextSibling}""==e.innerText.trim()&&(t.forEach((t=>e.append(t))),n.forEach((t=>e.append(t))));let l=0;t.forEach(((e,n)=>{n>0&&t[n-1].after(e),e.onclick=()=>a(n),e.classList.contains("active")&&(l=n)})),a(l)}));
|