Browse Source

ISSUE 5703: Color picker offset relative to editor container (#5704) Closes #5703

pull/5693/head^2
Brian Ernesto 3 years ago
committed by GitHub
parent
commit
f1754b8860
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/canvas/model/Frame.ts
  2. 66
      src/utils/ColorPicker.ts

2
src/canvas/model/Frame.ts

@ -226,6 +226,8 @@ export default class Frame extends ModuleModel<CanvasModule> {
_emitUpdated(data = {}) { _emitUpdated(data = {}) {
this.em.trigger('frame:updated', { frame: this, ...data }); this.em.trigger('frame:updated', { frame: this, ...data });
// When the frame is updated, the canvas should be refreshed. This may be better up stream from here.
this.em.refreshCanvas();
} }
hasAutoHeight() { hasAutoHeight() {

66
src/utils/ColorPicker.ts

@ -982,34 +982,48 @@ export default function ($, undefined?: any) {
/** /**
* checkOffset - get the offset below/above and left/right element depending on screen position * checkOffset - get the offset below/above and left/right element depending on screen position
* Thanks https://github.com/jquery/jquery-ui/blob/master/ui/jquery.ui.datepicker.js
*/ */
function getOffset(picker, input) { function getOffset(picker, input) {
var extraY = 0; // Get editor container
var dpWidth = picker.outerWidth(); const rootElm = document.querySelector('.gjs-editor-cont');
var dpHeight = picker.outerHeight();
var inputHeight = input.outerHeight(); // Ensure DOM elements are not a collections
var doc = picker[0].ownerDocument; let offsetElm = input.length ? input[0] : input;
var docElem = doc.documentElement; picker = picker.length ? picker[0] : picker;
var cW = docElem.clientWidth;
var cH = docElem.clientHeight; // Get the input position
var scL = $(doc).scrollLeft(); let rect = offsetElm.getBoundingClientRect();
var scT = $(doc).scrollTop();
var viewWidth = cW + scL; // Accuumulate the input offset
var viewHeight = cH + scT; let offset = { top: 0, left: 0, width: offsetElm.offsetWidth, height: offsetElm.offsetHeight };
var offset = input.offset(); while (offsetElm) {
// Accumulate offsets
offset.top += inputHeight; offset.top += offsetElm.offsetTop - offsetElm.scrollTop;
offset.left += offsetElm.offsetLeft - offsetElm.scrollLeft;
offset.left -= Math.min(
offset.left, // Check if the current element in our root, or if the next offset parent is outside of the root
offset.left + dpWidth > viewWidth && viewWidth > dpWidth ? Math.abs(offset.left + dpWidth - viewWidth) : 0 if (offsetElm === rootElm || !rootElm.contains(offsetElm.offsetParent)) {
); break; // Exit the loop if we are at root element
}
offset.top -= Math.min(
offset.top, // Move to the next offset parent
offset.top + dpHeight > viewHeight && viewHeight > dpHeight ? Math.abs(dpHeight + inputHeight - extraY) : extraY offsetElm = offsetElm.offsetParent;
); }
// Input is to close to the right side of the screen to show picker
if (rect.right + picker.offsetWidth > window.innerWidth - window.scrollX && rect.right - picker.offsetWidth > 0) {
// Right align the picker to the input
offset.left -= picker.offsetWidth - offset.width;
}
// Input is to close to the bottom of the screen to show picker above
if (rect.bottom + picker.offsetHeight < window.innerHeight - window.scrollY) {
// Bottom align the picker to the input
offset.top += offset.height;
} else {
// Top align the picker to the input
offset.top -= picker.offsetHeight;
}
return offset; return offset;
} }

Loading…
Cancel
Save