tooltip.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. let offset = parseInt(window.getComputedStyle(this.root).marginTop);
  8. this.quill.root.addEventListener('scroll', () => {
  9. this.root.style.marginTop = (-1*this.quill.root.scrollTop) + offset + 'px';
  10. this.checkBounds();
  11. });
  12. this.hide();
  13. }
  14. checkBounds() {
  15. this.root.classList.toggle('ql-out-top', this.root.offsetTop <= 0);
  16. this.root.classList.remove('ql-out-bottom');
  17. this.root.classList.toggle('ql-out-bottom', this.root.offsetTop + this.root.offsetHeight >= this.quill.root.offsetHeight);
  18. }
  19. hide() {
  20. this.root.classList.add('ql-hidden');
  21. }
  22. position(reference) {
  23. let left = reference.left + reference.width/2 - this.root.offsetWidth/2;
  24. let top = reference.bottom + this.quill.root.scrollTop;
  25. this.root.style.left = left + 'px';
  26. this.root.style.top = top + 'px';
  27. let containerBounds = this.boundsContainer.getBoundingClientRect();
  28. let rootBounds = this.root.getBoundingClientRect();
  29. let shift = 0;
  30. if (rootBounds.right > containerBounds.right) {
  31. shift = containerBounds.right - rootBounds.right;
  32. this.root.style.left = (left + shift) + 'px';
  33. }
  34. if (rootBounds.left < containerBounds.left) {
  35. shift = containerBounds.left - rootBounds.left;
  36. this.root.style.left = (left + shift) + 'px';
  37. }
  38. this.checkBounds();
  39. return shift;
  40. }
  41. show() {
  42. this.root.classList.remove('ql-editing');
  43. this.root.classList.remove('ql-hidden');
  44. }
  45. }
  46. export default Tooltip;