-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
126 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,61 @@ | ||
export default function flairPickerLoader(element: HTMLElement): void { | ||
const parent = $(element).parent(); | ||
const close = () => element.removeAttribute('open'); | ||
import { frag } from 'common'; | ||
|
||
export default async function flairPickerLoader(element: HTMLElement): Promise<void> { | ||
const selectEl = element.querySelector('select')!; | ||
const pickerEl = element.querySelector('.flair-picker') as HTMLElement; | ||
const removeEl = element.querySelector('.emoji-remove') as HTMLElement; | ||
const isOpen = () => !pickerEl.classList.contains('none'); | ||
|
||
const toggle = () => { | ||
if (isOpen() && (pickerEl.contains(document.activeElement) || document.activeElement === removeEl)) | ||
selectEl.focus(); | ||
pickerEl.classList.toggle('none'); | ||
}; | ||
|
||
const onEmojiSelect = (i?: { id: string; src: string }) => { | ||
parent.find('input[name="flair"]').val(i?.id ?? ''); | ||
parent.find('.uflair').remove(); | ||
if (i) parent.find('.flair-container').append('<img class="uflair" src="' + i.src + '" />'); | ||
close(); | ||
element.querySelector('.emoji-popup-button option')?.remove(); | ||
if (i?.id) selectEl.append(frag('<option value="' + i.id + '" selected></option>')); | ||
element.querySelector<HTMLImageElement>('.emoji-popup-button img')!.src = i?.src ?? ''; | ||
toggle(); | ||
}; | ||
parent.find('.emoji-remove').on('click', e => { | ||
|
||
const onClick = async (e: Event) => { | ||
if (e instanceof KeyboardEvent && e.key !== 'Enter' && e.key !== ' ') return; | ||
e.preventDefault(); | ||
toggle(); | ||
selectEl.focus(); | ||
}; | ||
|
||
await Promise.all([ | ||
site.asset.loadCssPath('bits.flairPicker'), | ||
site.asset.loadEsm('bits.flairPicker', { | ||
init: { | ||
element: element.querySelector('.flair-picker')!, | ||
onEmojiSelect, | ||
close: (e: PointerEvent) => { | ||
if (!isOpen() || selectEl.contains(e.target as Node)) return; | ||
toggle(); | ||
}, | ||
}, | ||
}), | ||
]); | ||
|
||
['mousedown', 'keydown'].forEach(t => selectEl.addEventListener(t, onClick)); | ||
removeEl.addEventListener('click', e => { | ||
e.preventDefault(); | ||
onEmojiSelect(); | ||
$(e.target).remove(); | ||
}); | ||
$(element).on('toggle', () => | ||
Promise.all([ | ||
site.asset.loadCssPath('bits.flairPicker'), | ||
site.asset.loadEsm('bits.flairPicker', { | ||
init: { | ||
element: element.querySelector('.flair-picker')!, | ||
close, | ||
onEmojiSelect, | ||
}, | ||
}), | ||
]), | ||
); | ||
|
||
element.closest('.dialog-content')?.addEventListener('click', (e: PointerEvent) => { | ||
// em's onClickOutside callback does not trigger inside modal dialog, so do it manually | ||
if (!isOpen() || [selectEl, pickerEl].some(el => el.contains(e.target as Node))) return; | ||
e.preventDefault(); | ||
toggle(); | ||
}); | ||
|
||
if (!CSS.supports('selector(:has(option))')) { | ||
// let old browsers set and remove flairs | ||
element.querySelector('img')!.style.display = 'block'; | ||
element.querySelector<HTMLElement>('.emoji-remove')!.style.display = 'block'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters