text-block.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.TextBlock = void 0;
  15. const x6_common_1 = require("@antv/x6-common");
  16. const registry_1 = require("../registry");
  17. const base_1 = require("./base");
  18. exports.TextBlock = base_1.Base.define({
  19. shape: 'text-block',
  20. markup: [
  21. {
  22. tagName: 'rect',
  23. selector: 'body',
  24. },
  25. x6_common_1.Platform.SUPPORT_FOREIGNOBJECT
  26. ? {
  27. tagName: 'foreignObject',
  28. selector: 'foreignObject',
  29. children: [
  30. {
  31. tagName: 'div',
  32. ns: x6_common_1.Dom.ns.xhtml,
  33. selector: 'label',
  34. style: {
  35. width: '100%',
  36. height: '100%',
  37. position: 'static',
  38. backgroundColor: 'transparent',
  39. textAlign: 'center',
  40. margin: 0,
  41. padding: '0px 5px',
  42. boxSizing: 'border-box',
  43. display: 'flex',
  44. alignItems: 'center',
  45. justifyContent: 'center',
  46. },
  47. },
  48. ],
  49. }
  50. : {
  51. tagName: 'text',
  52. selector: 'label',
  53. attrs: {
  54. textAnchor: 'middle',
  55. },
  56. },
  57. ],
  58. attrs: {
  59. body: Object.assign(Object.assign({}, base_1.Base.bodyAttr), { refWidth: '100%', refHeight: '100%' }),
  60. foreignObject: {
  61. refWidth: '100%',
  62. refHeight: '100%',
  63. },
  64. label: {
  65. style: {
  66. fontSize: 14,
  67. },
  68. },
  69. },
  70. propHooks(metadata) {
  71. const { text } = metadata, others = __rest(metadata, ["text"]);
  72. if (text) {
  73. x6_common_1.ObjectExt.setByPath(others, 'attrs/label/text', text);
  74. }
  75. return others;
  76. },
  77. attrHooks: {
  78. text: {
  79. set(text, { cell, view, refBBox, elem, attrs }) {
  80. if (elem instanceof HTMLElement) {
  81. elem.textContent = text;
  82. }
  83. else {
  84. // No foreign object
  85. const style = attrs.style || {};
  86. const wrapValue = { text, width: -5, height: '100%' };
  87. const wrapAttrs = Object.assign({ textVerticalAnchor: 'middle' }, style);
  88. const textWrap = registry_1.Attr.presets.textWrap;
  89. x6_common_1.FunctionExt.call(textWrap.set, this, wrapValue, {
  90. cell,
  91. view,
  92. elem,
  93. refBBox,
  94. attrs: wrapAttrs,
  95. });
  96. return { fill: style.color || null };
  97. }
  98. },
  99. position(text, { refBBox, elem }) {
  100. if (elem instanceof SVGElement) {
  101. return refBBox.getCenter();
  102. }
  103. },
  104. },
  105. },
  106. });
  107. //# sourceMappingURL=text-block.js.map