customText.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!-- 搜索表单 -->
  2. <template>
  3. <div>
  4. <el-popover
  5. style="position: fixed; z-index: 2000"
  6. width="130"
  7. ref="popoverRef"
  8. v-model="rightClickShow"
  9. @click.native="rightClickShow = false"
  10. >
  11. <div>
  12. <el-button type="primary" @click="addHtml">插入输入框</el-button>
  13. </div>
  14. </el-popover>
  15. <div
  16. v-html="form"
  17. :contenteditable="edit"
  18. class="contenteditable"
  19. :ref="id"
  20. :id="id"
  21. @contextmenu.prevent="onRightClick($event)"
  22. >
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import dictMixins from '@/mixins/dictMixins';
  28. import { generateRandomString } from '@/utils/util';
  29. export default {
  30. props: {},
  31. mixins: [dictMixins],
  32. props: {
  33. id: '',
  34. edit: {
  35. default: true,
  36. type: Boolean
  37. }
  38. },
  39. data() {
  40. return {
  41. form: null,
  42. valueObj: {},
  43. equation: {},
  44. domId: '',
  45. rightClickShow: false
  46. };
  47. },
  48. methods: {
  49. addHtml() {
  50. this.insertInput();
  51. },
  52. objInit() {
  53. if (Object.keys(this.valueObj).length) {
  54. for (let key in this.valueObj) {
  55. this.$nextTick(() => {
  56. let dom = document.getElementById(key);
  57. dom.value = this.valueObj[key];
  58. });
  59. }
  60. }
  61. },
  62. insertInput() {
  63. const selection = window.getSelection();
  64. if (selection.rangeCount > 0) {
  65. const range = selection.getRangeAt(0);
  66. this.getParentID(range.commonAncestorContainer);
  67. if (this.getParentID(range.commonAncestorContainer)) {
  68. range.setStart(selection.anchorNode, selection.anchorOffset);
  69. range.setEnd(selection.focusNode, selection.focusOffset);
  70. range.deleteContents(); // 删除选中的内容(如果需要)
  71. range.insertNode(this.getInput()); // 插入input元素
  72. // 可以设置光标位置在input之后
  73. range.selectNodeContents(this.getInput());
  74. range.collapse(false); // 光标放在input之后
  75. selection.removeAllRanges();
  76. selection.addRange(range);
  77. }
  78. // return
  79. }
  80. },
  81. getParentID(data) {
  82. if (data.id == this.id) {
  83. return true;
  84. }
  85. if (data.parentElement) {
  86. return this.getParentID(data.parentElement);
  87. }
  88. },
  89. getInput() {
  90. let id = generateRandomString();
  91. const inputElement = document.createElement('input');
  92. inputElement.setAttribute('type', 'text');
  93. inputElement.setAttribute('class', 'templateInput');
  94. inputElement.setAttribute('id', id);
  95. return inputElement;
  96. },
  97. getValue() {
  98. let inputs = document.querySelectorAll(
  99. '#' + this.id + ' .templateInput'
  100. );
  101. let data = {};
  102. if (inputs.length) {
  103. inputs.forEach((item) => {
  104. data[item.id] = item.value;
  105. });
  106. }
  107. return {
  108. form: this.$refs[this.id].innerHTML,
  109. valueObj: data,
  110. equation: this.equation
  111. };
  112. },
  113. equationValue({ domId, value }) {
  114. let dom = document.getElementById(domId);
  115. this.valueObj[domId] = value;
  116. dom.value = value;
  117. },
  118. init({ form, valueObj, equation }) {
  119. this.form = form;
  120. this.valueObj = valueObj;
  121. this.equation = equation||{};
  122. this.$nextTick(() => {
  123. if (!this.edit) {
  124. let inputs = document.querySelectorAll('.templateInput');
  125. inputs.forEach((item) => {
  126. item.setAttribute('readonly', 'readonly');
  127. });
  128. }
  129. });
  130. this.objInit();
  131. },
  132. editInputChange(domObj) {
  133. let dom = document.getElementById(this.domId);
  134. dom.style.width = domObj.width + 'px';
  135. if (domObj.readonly == 2) {
  136. dom.setAttribute('readonly', 'readonly');
  137. } else {
  138. dom.removeAttribute('readonly');
  139. }
  140. delete this.valueObj[dom.id];
  141. this.valueObj[domObj.id] = dom.value;
  142. if (domObj.equation) {
  143. this.equation[domObj.id] = domObj.equation;
  144. }
  145. dom.id = domObj.id;
  146. },
  147. onRightClick(PointerEvent) {
  148. this.rightClickShow = true;
  149. this.$nextTick(() => {
  150. let y = PointerEvent.pageY;
  151. let x = PointerEvent.pageX + 10;
  152. if (PointerEvent.screenY >= PointerEvent.view.innerHeight) {
  153. y -= 80;
  154. }
  155. this.$refs.popoverRef.$el.style.top = y + 'px';
  156. this.$refs.popoverRef.$el.style.left = x + 'px';
  157. });
  158. },
  159. inputChange(event) {
  160. if (event.target && event.target.className == 'templateInput') {
  161. this.valueObj[event.target.id] = event.target.value;
  162. this.$emit('calculation');
  163. }
  164. },
  165. inputClick(event) {
  166. if (!this.edit) {
  167. return;
  168. }
  169. if (event.target && event.target.className == 'templateInput') {
  170. this.domId = event.target.id;
  171. this.$emit('editShow', {
  172. templateDivRef: 'customTextRef' + this.id,
  173. domObj: {
  174. width: event.target.offsetWidth,
  175. id: event.target.id,
  176. readonly: event.target.readOnly ? 2 : 1,
  177. equation: this.equation[this.domId]
  178. }
  179. });
  180. }
  181. }
  182. },
  183. mounted() {
  184. this.$nextTick(() => {
  185. console.log(document.getElementById(this.id), 'dsd');
  186. document
  187. .getElementById(this.id)
  188. .addEventListener('change', this.inputChange);
  189. document
  190. .getElementById(this.id)
  191. .addEventListener('click', this.inputClick);
  192. });
  193. }
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. :deep(.templateInput) {
  198. width: 80px;
  199. border: solid 1px #bfbfbf;
  200. padding: 1px;
  201. margin: 1px;
  202. margin-left: 3px;
  203. text-align: center;
  204. }
  205. .contenteditable {
  206. // padding: 5px;
  207. width: 100%;
  208. min-height: 20px;
  209. padding-left: 10px;
  210. // border: solid 1px #b3b0b0;
  211. }
  212. </style>