SvgComponents.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <component :is="svgtype" :svg_color="svg_color" ref="textsss"></component>
  3. </template>
  4. <script>
  5. let importComponents = {};
  6. export default {
  7. props: [
  8. "svgInfoData",
  9. "svg_color",
  10. "svgtype",
  11. "text",
  12. "size",
  13. "fontSize",
  14. "height",
  15. "strokeWidth",
  16. ],
  17. data() {
  18. return {
  19. componentTag: "",
  20. };
  21. },
  22. components: importComponents,
  23. watch: {
  24. text(text) {
  25. this.editText(text);
  26. },
  27. fontSize(fontSize) {
  28. this.font_Size(fontSize);
  29. },
  30. strokeWidth(width) {
  31. this.stroke_Width(width);
  32. },
  33. height(height) {
  34. this.edit_height(height);
  35. },
  36. },
  37. created() {
  38. this.svgInfoData.forEach((f) => {
  39. let componentInfo = {
  40. template: f.template,
  41. props: f.props,
  42. };
  43. if (f.type === this.svgtype) {
  44. importComponents[f.type] = componentInfo;
  45. }
  46. });
  47. },
  48. mounted() {
  49. this.$nextTick(() => {
  50. this.editText(this.text);
  51. this.font_Size(this.fontSize);
  52. this.stroke_Width(this.strokeWidth);
  53. this.edit_height(this.height);
  54. });
  55. },
  56. methods: {
  57. editText(val) {
  58. var el = this.$refs.textsss.$el;
  59. if (el != undefined) {
  60. // console.log(this.$refs.textsss);
  61. if (el.nodeName === "text") {
  62. el.innerHTML = val;
  63. this.font_Size(this.fontSize);
  64. }
  65. }
  66. },
  67. font_Size(val) {
  68. var el = this.$refs.textsss.$el;
  69. if (el != undefined) {
  70. if (el.nodeName === "text") {
  71. var len = el.innerHTML.length;
  72. el.setAttribute("x", -(val / 2) * len);
  73. el.setAttribute("y", val / 2);
  74. el.setAttribute("font-size", val);
  75. }
  76. }
  77. },
  78. edit_height(val) {
  79. var el = this.$refs.textsss.$el;
  80. if (el != undefined) {
  81. var dom = document.getElementById(el.parentNode.id);
  82. var title = dom.getAttribute("title");
  83. let line = dom.getElementsByTagName("line")[0];
  84. if (val != null && val != "" && val != undefined) {
  85. if (title === "电线") {
  86. line.setAttribute("y1", -val);
  87. line.setAttribute("y2", val);
  88. } else if (title === "横线") {
  89. line.setAttribute("x1", -val);
  90. line.setAttribute("x2", val);
  91. }
  92. }
  93. }
  94. },
  95. stroke_Width(val) {
  96. var el = this.$refs.textsss.$el;
  97. if (el != undefined) {
  98. var dom = document.getElementById(el.parentNode.id);
  99. var title = dom.getAttribute("title");
  100. let line = dom.getElementsByTagName("line")[0];
  101. if (val != null && val != "" && val != undefined) {
  102. if (title === "电线") {
  103. line.setAttribute("stroke-width", val);
  104. } else if (title === "横线") {
  105. line.setAttribute("stroke-width", val);
  106. }
  107. }
  108. }
  109. },
  110. },
  111. };
  112. </script>
  113. <style scoped>
  114. </style>