poptip.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. var __rest = (this && this.__rest) || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  5. t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  7. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  9. t[p[i]] = s[p[i]];
  10. }
  11. return t;
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. exports.Poptip = void 0;
  15. const g_1 = require("@antv/g");
  16. const string_1 = require("../utils/string");
  17. const helper_1 = require("../utils/helper");
  18. function dom(tag, children, style) {
  19. return `<${tag} style="${Object.entries(style)
  20. .map(([key, value]) => `${(0, string_1.kebabCase)(key)}:${value}`)
  21. .join(';')}">${children}</${tag}>`;
  22. }
  23. const defaultTipStyle = {
  24. backgroundColor: 'rgba(0,0,0,0.75)',
  25. color: '#fff',
  26. width: 'max-content',
  27. padding: '1px 4px',
  28. fontSize: '12px',
  29. borderRadius: '2.5px',
  30. boxShadow: '0 3px 6px -4px rgba(0,0,0,0.12), 0 6px 16px 0 rgba(0,0,0,0.08), 0 9px 28px 8px rgba(0,0,0,0.05)',
  31. };
  32. function isTipText(element) {
  33. if (element.nodeName !== 'text')
  34. return false;
  35. if (element.isOverflowing())
  36. return true;
  37. return false;
  38. }
  39. function Poptip(_a) {
  40. var { offsetX = 8, offsetY = 8 } = _a, style = __rest(_a, ["offsetX", "offsetY"]);
  41. return (context) => {
  42. const { container } = context;
  43. const [x0, y0] = container.getBounds().min;
  44. const tipStyle = (0, helper_1.subObject)(style, 'tip');
  45. const tips = new Set();
  46. const pointerover = (e) => {
  47. const { target } = e;
  48. if (!isTipText(target)) {
  49. e.stopPropagation();
  50. return;
  51. }
  52. const { offsetX: mouseX, offsetY: mouseY } = e;
  53. const x = mouseX + offsetX - x0;
  54. const y = mouseY + offsetY - y0;
  55. if (target.tip) {
  56. target.tip.style.x = x;
  57. target.tip.style.y = y;
  58. return;
  59. }
  60. const { text } = target.style;
  61. const tipELement = new g_1.HTML({
  62. className: 'poptip',
  63. style: {
  64. innerHTML: dom('div', text, Object.assign(Object.assign({}, defaultTipStyle), tipStyle)),
  65. x,
  66. y,
  67. },
  68. });
  69. container.appendChild(tipELement);
  70. target.tip = tipELement;
  71. tips.add(tipELement);
  72. };
  73. const pointerout = (e) => {
  74. const { target } = e;
  75. if (!isTipText(target)) {
  76. e.stopPropagation();
  77. return;
  78. }
  79. if (!target.tip)
  80. return;
  81. target.tip.remove();
  82. target.tip = null;
  83. tips.delete(target.tip);
  84. };
  85. container.addEventListener('pointerover', pointerover);
  86. container.addEventListener('pointerout', pointerout);
  87. return () => {
  88. container.removeEventListener('pointerover', pointerover);
  89. container.removeEventListener('pointerout', pointerout);
  90. tips.forEach((tip) => tip.remove());
  91. };
  92. };
  93. }
  94. exports.Poptip = Poptip;
  95. //# sourceMappingURL=poptip.js.map