popoverValueRenderer.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { bindPopover } from '@/components/popover/composables/usePopover';
  2. import { classLeft } from '@/constants/commonClassName';
  3. import { getTextWidth } from '@/utils/common/tools';
  4. import { parse } from '@/utils/frontend/dom';
  5. import { GridSettings } from '@sc/handsontable';
  6. let font: string | undefined;
  7. // 首次获取一次就行,提高性能,所有表格字体应是统一的
  8. const getFont = (container: HTMLElement) => {
  9. if (font) return font;
  10. font = getComputedStyle(container).font;
  11. return font;
  12. };
  13. // 如果字符宽度超过列宽,悬浮提示
  14. export default function popoverValueRenderer(
  15. instance: Handsontable,
  16. td: HTMLElement,
  17. row: number,
  18. col: number,
  19. prop: string | number,
  20. value: any,
  21. cellProperties: GridSettings
  22. ): HTMLElement {
  23. // 当前行的源数据
  24. td.innerHTML = ''; // 先清空数据,多次刷新时会出现两行的问题
  25. td.className = (cellProperties.className as string) || classLeft;
  26. if (value) {
  27. const container = parse(`<div>${value}</div>`) as HTMLElement;
  28. td.appendChild(container);
  29. setTimeout(() => {
  30. const colWidth = instance.getColWidth(col) - 4; // 减去padding
  31. const textWidth = getTextWidth(value, getFont(container));
  32. if (textWidth > colWidth) bindPopover(td, container, value);
  33. });
  34. }
  35. return td;
  36. }