Altered the positioning for the pickers.

This commit is contained in:
flash 2024-06-27 18:11:51 +00:00
parent b0ad0108f8
commit 74f116bfac
3 changed files with 35 additions and 21 deletions

View file

@ -79,8 +79,8 @@ const MamiColourPicker = function(options) {
if(typeof pos !== 'object') if(typeof pos !== 'object')
throw 'pos must be an object'; throw 'pos must be an object';
html.style.bottom = 'y' in pos && pos.y >= 0 ? `${pos.y}px` : null; for(const attr of ['top', 'left', 'right', 'bottom'])
html.style.left = 'x' in pos && pos.x >= 0 ? `${pos.x}px` : null; html.style[attr] = typeof pos[attr] === 'number' ? `${pos[attr]}px` : null;
}; };
const tabs = new MamiTabsControl({ const tabs = new MamiTabsControl({
@ -166,20 +166,23 @@ const MamiColourPicker = function(options) {
get colour() { return colour; }, get colour() { return colour; },
set colour(value) { setColour(value); }, set colour(value) { setColour(value); },
setPosition: setPosition, setPosition: setPosition,
close: close, close: runReject,
dialog: pos => { dialog: pos => {
html.classList.remove('hidden'); html.classList.remove('hidden');
if(pos instanceof MouseEvent) { if(pos instanceof MouseEvent) {
const ev = pos;
pos = {};
const mbb = html.getBoundingClientRect(); const mbb = html.getBoundingClientRect();
const bbb = ev.target.getBoundingClientRect();
const pbb = html.parentNode.getBoundingClientRect(); const pbb = html.parentNode.getBoundingClientRect();
let x = pos.clientX;
let y = pbb.height - pos.clientY;
if(x > (pbb.width - mbb.width)) pos.left = bbb.left;
x = 10; pos.bottom = pbb.height - bbb.top;
pos = { x: x, y: y }; if(pos.left + mbb.width >= pbb.width)
pos.left = 0;
} }
if(pos !== undefined) if(pos !== undefined)

View file

@ -100,8 +100,8 @@ const MamiEmotePicker = function(args) {
if(typeof pos !== 'object') if(typeof pos !== 'object')
throw 'pos must be an object'; throw 'pos must be an object';
html.style.bottom = 'y' in pos && pos.y >= 0 ? `${pos.y}px` : null; for(const attr of ['top', 'left', 'right', 'bottom'])
html.style.right = 'x' in pos && pos.x >= 0 ? `${pos.x}px` : null; html.style[attr] = typeof pos[attr] === 'number' ? `${pos[attr]}px` : null;
}; };
return { return {
@ -117,15 +117,22 @@ const MamiEmotePicker = function(args) {
html.classList.remove('hidden'); html.classList.remove('hidden');
if(pos instanceof MouseEvent) { if(pos instanceof MouseEvent) {
const ev = pos;
pos = {};
const mbb = html.getBoundingClientRect(); const mbb = html.getBoundingClientRect();
const bbb = ev.target.getBoundingClientRect();
const pbb = html.parentNode.getBoundingClientRect(); const pbb = html.parentNode.getBoundingClientRect();
let x = pbb.width - pos.clientX;
let y = pbb.height - pos.clientY;
if(x > mbb.width) pos.right = pbb.width - bbb.left;
x -= mbb.width; pos.bottom = pbb.height - bbb.top;
pos = { x: x, y: y }; if(pos.right + mbb.width > pbb.width)
pos.right = 0;
else if(pos.right > mbb.width)
pos.right -= mbb.width;
else
pos.right -= bbb.width;
} }
if(pos !== undefined) if(pos !== undefined)

View file

@ -21,17 +21,21 @@ Umi.UI.Markup = (function() {
let picker, pickerVisible = false; let picker, pickerVisible = false;
const insert = function(ev) { const insert = function(ev) {
if(this.dataset.umiTagName === 'color' && !pickerVisible) { if(this.dataset.umiTagName === 'color') {
pickerVisible = true;
if(picker === undefined) { if(picker === undefined) {
picker = new MamiColourPicker({ presets: futami.get('colours') }); picker = new MamiColourPicker({ presets: futami.get('colours') });
pickerTarget.appendChild(picker.element); pickerTarget.appendChild(picker.element);
} }
if(pickerVisible) {
picker.close();
} else {
pickerVisible = true;
picker.dialog(ev) picker.dialog(ev)
.then(colour => insertRaw(`[color=${MamiColour.hex(colour)}]`, '[/color]')) .then(colour => insertRaw(`[color=${MamiColour.hex(colour)}]`, '[/color]'))
.catch(() => {}) // noop so the console stops screaming
.finally(() => pickerVisible = false); .finally(() => pickerVisible = false);
}
} else } else
insertRaw( insertRaw(
this.dataset.umiBeforeCursor, this.dataset.umiBeforeCursor,