customText.vue 6.2 KB

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