update.eternae.ink/assets/ts/message_icons.ts

27 lines
866 B
TypeScript
Raw Normal View History

2020-04-23 18:07:39 +02:00
import feather from "feather-icons";
document.addEventListener('DOMContentLoaded', () => {
2020-09-23 13:30:48 +02:00
const messageTypeToIcon: { [p: string]: string } = {
2020-04-23 18:07:39 +02:00
info: 'info',
success: 'check',
warning: 'alert-triangle',
error: 'x-circle',
question: 'help-circle',
};
2020-09-23 13:30:48 +02:00
document.querySelectorAll<HTMLElement>('.message').forEach(el => {
2020-04-23 18:07:39 +02:00
const icon = el.querySelector('.icon');
2020-09-23 13:30:48 +02:00
const type = el.dataset['type'];
if (!icon || !type) return;
if (!messageTypeToIcon[type]) throw new Error(`No icon for type ${type}`);
2020-04-23 18:07:39 +02:00
const svgContainer = document.createElement('div');
svgContainer.innerHTML = feather.icons[messageTypeToIcon[type]].toSvg();
2020-09-23 13:30:48 +02:00
if (svgContainer.firstChild) el.insertBefore(svgContainer.firstChild, icon);
2020-04-23 18:07:39 +02:00
icon.remove();
});
feather.replace();
2020-09-23 13:30:48 +02:00
});