add-html-label.js 817 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import util from '../util'
  2. function addHtmlLabel (root, node) {
  3. const fo = root
  4. .append('foreignObject')
  5. .attr('width', '100000')
  6. const div = fo
  7. .append('xhtml:div')
  8. div.attr('xmlns', 'http://www.w3.org/1999/xhtml')
  9. const label = node.label
  10. switch (typeof label) {
  11. case 'function':
  12. div.insert(label)
  13. break
  14. case 'object':
  15. // Currently we assume this is a DOM object.
  16. div.insert(function () { return label })
  17. break
  18. default: div.html(label)
  19. }
  20. util.applyStyle(div, node.labelStyle)
  21. div.style('display', 'inline-block')
  22. // Fix for firefox
  23. div.style('white-space', 'nowrap')
  24. const client = div.node().getBoundingClientRect()
  25. fo
  26. .attr('width', client.width)
  27. .attr('height', client.height)
  28. return fo
  29. }
  30. export default addHtmlLabel