tooltip.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. class Tooltip {
  2. constructor(quill, boundsContainer) {
  3. this.quill = quill;
  4. this.boundsContainer = boundsContainer || document.body;
  5. this.root = quill.addContainer('ql-tooltip');
  6. this.root.innerHTML = this.constructor.TEMPLATE;
  7. if (this.quill.root === this.quill.scrollingContainer) {
  8. this.quill.root.addEventListener('scroll', () => {
  9. this.root.style.marginTop = (-1*this.quill.root.scrollTop) + 'px';
  10. });
  11. }
  12. this.hide();
  13. }
  14. hide() {
  15. this.root.classList.add('ql-hidden');
  16. }
  17. position(reference) {
  18. let left = reference.left + reference.width/2 - this.root.offsetWidth/2;
  19. // root.scrollTop should be 0 if scrollContainer !== root
  20. let top = reference.bottom + this.quill.root.scrollTop;
  21. this.root.style.left = left + 'px';
  22. this.root.style.top = top + 'px';
  23. this.root.classList.remove('ql-flip');
  24. let containerBounds = this.boundsContainer.getBoundingClientRect();
  25. let rootBounds = this.root.getBoundingClientRect();
  26. let shift = 0;
  27. if (rootBounds.right > containerBounds.right) {
  28. shift = containerBounds.right - rootBounds.right;
  29. this.root.style.left = (left + shift) + 'px';
  30. }
  31. if (rootBounds.left < containerBounds.left) {
  32. shift = containerBounds.left - rootBounds.left;
  33. this.root.style.left = (left + shift) + 'px';
  34. }
  35. if (rootBounds.bottom > containerBounds.bottom) {
  36. let height = rootBounds.bottom - rootBounds.top;
  37. let verticalShift = reference.bottom - reference.top + height;
  38. this.root.style.top = (top - verticalShift) + 'px';
  39. this.root.classList.add('ql-flip');
  40. }
  41. return shift;
  42. }
  43. show() {
  44. this.root.classList.remove('ql-editing');
  45. this.root.classList.remove('ql-hidden');
  46. }
  47. }
  48. export default Tooltip;