Commit e36d3232 authored by Clement Ho's avatar Clement Ho

Add dropdown offset to match input cursor

parent 0e7b8413
...@@ -52,6 +52,10 @@ ...@@ -52,6 +52,10 @@
this.input.setAttribute(DATA_DROPDOWN_TRIGGER, `#${this.listId}`); this.input.setAttribute(DATA_DROPDOWN_TRIGGER, `#${this.listId}`);
} }
setOffset(offset = 0) {
this.dropdown.style.left = `${offset}px`;
}
getCurrentHook() { getCurrentHook() {
return droplab.hooks.filter(h => h.id === this.hookId)[0]; return droplab.hooks.filter(h => h.id === this.hookId)[0];
} }
......
...@@ -99,47 +99,61 @@ ...@@ -99,47 +99,61 @@
loadDropdown(dropdownName = '') { loadDropdown(dropdownName = '') {
dropdownName = dropdownName.toLowerCase(); dropdownName = dropdownName.toLowerCase();
const filterIconPadding = 27;
const match = gl.FilteredSearchTokenKeys.get().filter(value => value.key === dropdownName)[0]; const match = gl.FilteredSearchTokenKeys.get().filter(value => value.key === dropdownName)[0];
const filteredSearch = document.querySelector('.filtered-search');
if (match && this.currentDropdown !== match.key) { if (match && this.currentDropdown !== match.key) {
console.log(`🦄 load ${match.key} dropdown`); console.log(`🦄 load ${match.key} dropdown`);
const dynamicDropdownPadding = 12;
const dropdownOffset = gl.text.getTextWidth(filteredSearch.value) + filterIconPadding + dynamicDropdownPadding;
this.dismissCurrentDropdown(); this.dismissCurrentDropdown();
this.currentDropdown = match.key; this.currentDropdown = match.key;
if (match.key === 'author') { if (match.key === 'author') {
if (!dropdownAuthor) { if (!dropdownAuthor) {
dropdownAuthor = new gl.DropdownAuthor(document.querySelector('#js-dropdown-author'), document.querySelector('.filtered-search')); dropdownAuthor = new gl.DropdownAuthor(document.querySelector('#js-dropdown-author'), filteredSearch);
} }
dropdownAuthor.setOffset(dropdownOffset);
dropdownAuthor.render(); dropdownAuthor.render();
} else if (match.key === 'assignee') { } else if (match.key === 'assignee') {
if (!dropdownAssignee) { if (!dropdownAssignee) {
dropdownAssignee = new gl.DropdownAssignee(document.querySelector('#js-dropdown-assignee'), document.querySelector('.filtered-search')); dropdownAssignee = new gl.DropdownAssignee(document.querySelector('#js-dropdown-assignee'), filteredSearch);
} }
dropdownAssignee.setOffset(dropdownOffset);
dropdownAssignee.render(); dropdownAssignee.render();
} else if (match.key === 'milestone') { } else if (match.key === 'milestone') {
if (!dropdownMilestone) { if (!dropdownMilestone) {
dropdownMilestone = new gl.DropdownMilestone(document.querySelector('#js-dropdown-milestone'), document.querySelector('.filtered-search')); dropdownMilestone = new gl.DropdownMilestone(document.querySelector('#js-dropdown-milestone'), filteredSearch);
} }
dropdownMilestone.setOffset(dropdownOffset);
dropdownMilestone.render(); dropdownMilestone.render();
} else if (match.key === 'label') { } else if (match.key === 'label') {
if (!dropdownLabel) { if (!dropdownLabel) {
dropdownLabel = new gl.DropdownLabel(document.querySelector('#js-dropdown-label'), document.querySelector('.filtered-search')); dropdownLabel = new gl.DropdownLabel(document.querySelector('#js-dropdown-label'), filteredSearch);
} }
dropdownLabel.setOffset(dropdownOffset);
dropdownLabel.render(); dropdownLabel.render();
} }
} else if (!match && this.currentDropdown !== 'hint') { } else if (!match && this.currentDropdown !== 'hint') {
console.log('🦄 load hint dropdown'); console.log('🦄 load hint dropdown');
const dropdownOffset = gl.text.getTextWidth(filteredSearch.value) + filterIconPadding;
this.dismissCurrentDropdown(); this.dismissCurrentDropdown();
this.currentDropdown = 'hint'; this.currentDropdown = 'hint';
if (!dropdownHint) { if (!dropdownHint) {
dropdownHint = new gl.DropdownHint(document.querySelector('#js-dropdown-hint'), document.querySelector('.filtered-search'), this.currentDropdown); dropdownHint = new gl.DropdownHint(document.querySelector('#js-dropdown-hint'), filteredSearch, this.currentDropdown);
} }
dropdownHint.setOffset(dropdownOffset);
dropdownHint.render(); dropdownHint.render();
} }
} }
......
...@@ -17,6 +17,22 @@ ...@@ -17,6 +17,22 @@
gl.text.replaceRange = function(s, start, end, substitute) { gl.text.replaceRange = function(s, start, end, substitute) {
return s.substring(0, start) + substitute + s.substring(end); return s.substring(0, start) + substitute + s.substring(end);
}; };
gl.text.getTextWidth = function(text, font) {
/**
* Uses canvas.measureText to compute and return the width of the given text of given font in pixels.
*
* @param {String} text The text to be rendered.
* @param {String} font The css font descriptor that text is to be rendered with (e.g. "bold 14px verdana").
*
* @see http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393
*/
// re-use canvas object for better performance
var canvas = gl.text.getTextWidth.canvas || (gl.text.getTextWidth.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = font;
var metrics = context.measureText(text);
return metrics.width;
};
gl.text.selectedText = function(text, textarea) { gl.text.selectedText = function(text, textarea) {
return text.substring(textarea.selectionStart, textarea.selectionEnd); return text.substring(textarea.selectionStart, textarea.selectionEnd);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment