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. const left =
  19. reference.left + reference.width / 2 - this.root.offsetWidth / 2;
  20. // root.scrollTop should be 0 if scrollContainer !== root
  21. const top = reference.bottom + this.quill.root.scrollTop;
  22. this.root.style.left = `${left}px`;
  23. this.root.style.top = `${top}px`;
  24. this.root.classList.remove('ql-flip');
  25. const containerBounds = this.boundsContainer.getBoundingClientRect();
  26. const rootBounds = this.root.getBoundingClientRect();
  27. let shift = 0;
  28. if (rootBounds.right > containerBounds.right) {
  29. shift = containerBounds.right - rootBounds.right;
  30. this.root.style.left = `${left + shift}px`;
  31. }
  32. if (rootBounds.left < containerBounds.left) {
  33. shift = containerBounds.left - rootBounds.left;
  34. this.root.style.left = `${left + shift}px`;
  35. }
  36. if (rootBounds.bottom > containerBounds.bottom) {
  37. const height = rootBounds.bottom - rootBounds.top;
  38. const verticalShift = reference.bottom - reference.top + height;
  39. this.root.style.top = `${top - verticalShift}px`;
  40. this.root.classList.add('ql-flip');
  41. }
  42. return shift;
  43. }
  44. show() {
  45. this.root.classList.remove('ql-editing');
  46. this.root.classList.remove('ql-hidden');
  47. }
  48. }
  49. export default Tooltip;