Simplification
This commit is contained in:
@@ -1,7 +1,50 @@
|
||||
(() => {
|
||||
const on = (selector, handler) => document.querySelectorAll(selector).forEach(handler);
|
||||
const each = (selector, handler) => document.querySelectorAll(selector).forEach(handler);
|
||||
|
||||
on('[data-copy-text]', (button) => {
|
||||
|
||||
const siteHeader = document.querySelector('.site-header');
|
||||
const mobileMenu = document.querySelector('[data-mobile-menu]');
|
||||
const mobileMenuOpen = document.querySelector('[data-mobile-menu-open]');
|
||||
|
||||
if (siteHeader && mobileMenu && mobileMenuOpen) {
|
||||
const setMobileMenuState = (isOpen) => {
|
||||
mobileMenu.classList.toggle('is-open', isOpen);
|
||||
siteHeader.classList.toggle('is-mobile-menu-open', isOpen);
|
||||
mobileMenuOpen.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
|
||||
};
|
||||
|
||||
mobileMenuOpen.addEventListener('click', () => {
|
||||
setMobileMenuState(true);
|
||||
});
|
||||
|
||||
each('[data-mobile-menu-close]', (button) => {
|
||||
button.addEventListener('click', () => {
|
||||
setMobileMenuState(false);
|
||||
});
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Escape' && mobileMenu.classList.contains('is-open')) {
|
||||
setMobileMenuState(false);
|
||||
mobileMenuOpen.focus();
|
||||
}
|
||||
});
|
||||
|
||||
const mediaQuery = window.matchMedia('(min-width: 721px)');
|
||||
const handleViewportChange = (event) => {
|
||||
if (event.matches) {
|
||||
setMobileMenuState(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof mediaQuery.addEventListener === 'function') {
|
||||
mediaQuery.addEventListener('change', handleViewportChange);
|
||||
} else if (typeof mediaQuery.addListener === 'function') {
|
||||
mediaQuery.addListener(handleViewportChange);
|
||||
}
|
||||
}
|
||||
|
||||
each('[data-copy-text]', (button) => {
|
||||
button.addEventListener('click', async () => {
|
||||
const text = button.getAttribute('data-copy-text') || '';
|
||||
if (!text) {
|
||||
@@ -21,7 +64,7 @@
|
||||
});
|
||||
});
|
||||
|
||||
on('[data-confirm]', (form) => {
|
||||
each('[data-confirm]', (form) => {
|
||||
form.addEventListener('submit', (event) => {
|
||||
const message = form.getAttribute('data-confirm') || 'Confirmer cette action ?';
|
||||
if (!window.confirm(message)) {
|
||||
@@ -30,7 +73,7 @@
|
||||
});
|
||||
});
|
||||
|
||||
on('[data-char-count]', (field) => {
|
||||
each('[data-char-count]', (field) => {
|
||||
const counter = field.parentElement?.querySelector('[data-char-count-value]');
|
||||
if (!counter) {
|
||||
return;
|
||||
@@ -45,23 +88,19 @@
|
||||
});
|
||||
|
||||
const editor = document.querySelector('[data-markdown-editor]');
|
||||
const picker = document.querySelector('[data-media-picker]');
|
||||
const editorLayout = document.querySelector('[data-editor-layout]');
|
||||
const pickerClose = document.querySelector('[data-media-picker-close]');
|
||||
|
||||
const focusEditor = () => editor?.focus();
|
||||
|
||||
if (editor) {
|
||||
const focusEditor = () => editor.focus();
|
||||
|
||||
const replaceSelection = (before, after = '', placeholder = '') => {
|
||||
const start = editor.selectionStart;
|
||||
const end = editor.selectionEnd;
|
||||
const selected = editor.value.slice(start, end);
|
||||
const content = selected || placeholder;
|
||||
const selection = editor.value.slice(start, end);
|
||||
const content = selection || placeholder;
|
||||
const insertion = before + content + after;
|
||||
|
||||
editor.setRangeText(insertion, start, end, 'end');
|
||||
|
||||
if (!selected && placeholder) {
|
||||
if (!selection && placeholder) {
|
||||
const cursorStart = start + before.length;
|
||||
editor.setSelectionRange(cursorStart, cursorStart + placeholder.length);
|
||||
}
|
||||
@@ -72,17 +111,17 @@
|
||||
const prefixLines = (prefix, placeholder) => {
|
||||
const start = editor.selectionStart;
|
||||
const end = editor.selectionEnd;
|
||||
const selected = editor.value.slice(start, end) || placeholder;
|
||||
const prefixed = selected
|
||||
const selection = editor.value.slice(start, end) || placeholder;
|
||||
const value = selection
|
||||
.split('\n')
|
||||
.map((line) => (line ? prefix + line : prefix.trimEnd()))
|
||||
.join('\n');
|
||||
|
||||
editor.setRangeText(prefixed, start, end, 'end');
|
||||
editor.setRangeText(value, start, end, 'end');
|
||||
focusEditor();
|
||||
};
|
||||
|
||||
on('[data-md-action]', (button) => {
|
||||
each('[data-md-action]', (button) => {
|
||||
button.addEventListener('click', () => {
|
||||
switch (button.getAttribute('data-md-action')) {
|
||||
case 'bold':
|
||||
@@ -104,12 +143,8 @@
|
||||
replaceSelection('[', '](https://)', 'texte');
|
||||
break;
|
||||
case 'code': {
|
||||
const selected = editor.value.slice(editor.selectionStart, editor.selectionEnd);
|
||||
replaceSelection(
|
||||
selected.includes('\n') ? '```\n' : '`',
|
||||
selected.includes('\n') ? '\n```' : '`',
|
||||
'code'
|
||||
);
|
||||
const selection = editor.value.slice(editor.selectionStart, editor.selectionEnd);
|
||||
replaceSelection(selection.includes('\n') ? '```\n' : '`', selection.includes('\n') ? '\n```' : '`', 'code');
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -117,45 +152,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
const togglePicker = (open) => {
|
||||
if (!picker) {
|
||||
return;
|
||||
}
|
||||
|
||||
picker.classList.toggle('is-hidden', !open);
|
||||
editorLayout?.classList.toggle('is-picker-open', open);
|
||||
|
||||
if (!open) {
|
||||
focusEditor();
|
||||
return;
|
||||
}
|
||||
|
||||
picker.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
};
|
||||
|
||||
on('[data-media-picker-open]', (button) => {
|
||||
button.addEventListener('click', () => togglePicker(true));
|
||||
});
|
||||
|
||||
pickerClose?.addEventListener('click', () => togglePicker(false));
|
||||
|
||||
on('[data-media-picker-select]', (button) => {
|
||||
button.addEventListener('click', () => {
|
||||
const markdown = button.getAttribute('data-media-markdown') || '';
|
||||
if (!editor || !markdown) {
|
||||
return;
|
||||
}
|
||||
|
||||
const start = editor.selectionStart;
|
||||
const end = editor.selectionEnd;
|
||||
const prefix = start > 0 && !editor.value.slice(0, start).endsWith('\n\n') ? '\n\n' : '';
|
||||
const suffix = end < editor.value.length && !editor.value.slice(end).startsWith('\n\n') ? '\n\n' : '';
|
||||
editor.setRangeText(prefix + markdown + suffix, start, end, 'end');
|
||||
togglePicker(false);
|
||||
});
|
||||
});
|
||||
|
||||
on('[data-alt-input]', (input) => {
|
||||
each('[data-alt-input]', (input) => {
|
||||
const card = input.closest('.card');
|
||||
const button = card?.querySelector('[data-markdown-template]');
|
||||
if (!button) {
|
||||
@@ -164,8 +161,7 @@
|
||||
|
||||
const update = () => {
|
||||
const template = button.getAttribute('data-markdown-template') || '';
|
||||
const alt = input.value;
|
||||
button.setAttribute('data-copy-text', template.replace(');
|
||||
button.setAttribute('data-copy-text', template.replace(');
|
||||
};
|
||||
|
||||
input.addEventListener('input', update);
|
||||
|
||||
Reference in New Issue
Block a user