-
Notifications
You must be signed in to change notification settings - Fork 6
/
html.js
58 lines (50 loc) · 1.99 KB
/
html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { moveIndex } from '../utils.js';
export function updateList({ prevIds = [], nextIds = [], add, update, element }) {
prevIds.forEach((prevId) => {
if (nextIds.every((nextId) => prevId !== nextId)) {
element.querySelector(`#${prevId}`).remove();
}
});
let prevIdsFiltered = prevIds.filter((prevId) => nextIds.some((nextId) => prevId === nextId));
let current = Array.from(element.querySelectorAll(':scope > *'));
nextIds.forEach((nextId, nextIndex) => {
const prevIndex = prevIdsFiltered.findIndex((prevId) => prevId === nextId);
const prevId = prevIndex !== -1 ? prevIdsFiltered[prevIndex] : undefined;
if (prevId) {
const updatedItem = element.querySelector(`#${nextId}`);
update(updatedItem);
if (prevIndex !== nextIndex && current[nextIndex]) {
element.insertBefore(updatedItem, current[nextIndex]);
prevIdsFiltered = moveIndex(prevIndex, nextIndex, prevIdsFiltered);
}
} else {
const newItem = add(nextId);
if (current[nextIndex]) {
element.insertBefore(newItem, current[nextIndex]);
} else {
element.appendChild(newItem);
}
}
current = Array.from(element.querySelectorAll(':scope > *'));
});
}
export function component(id, template) {
const container = document.createElement('div');
container.innerHTML = template.trim();
const el = container.firstChild;
el.id = id;
return el;
}
const replacements = [
[/\*\*(?<text>[^\n]+)\*\*/, '<strong>$<text></strong>'],
[/\_(?<text>[^\n]+)\_/, '<em>$<text></em>'],
[/\[(?<text>[^\n\]]+)\]\((?<link>[^\)]+)\)/, '<a href="$<link>" target="_blank">$<text></a>'],
];
export function markdownToHtml(text) {
return replacements.reduce((acc, [regex, replacement]) => acc.replace(regex, replacement), text);
}
export function dispatch(detail, el) {
detail.type
? el.dispatchEvent(new CustomEvent('action', { detail, bubbles: true }))
: detail((detail) => el.dispatchEvent(new CustomEvent('action', { detail, bubbles: true })));
}