customText.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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('autocomplete', 'off');
  95. inputElement.setAttribute('id', id);
  96. return inputElement;
  97. },
  98. getValue() {
  99. let inputs = document.querySelectorAll(
  100. '#' + this.id + ' .templateInput'
  101. );
  102. let data = {};
  103. if (inputs.length) {
  104. inputs.forEach((item) => {
  105. data[item.id] = item.value;
  106. });
  107. }
  108. return {
  109. form: this.$refs[this.id].innerHTML,
  110. valueObj: data,
  111. equation: this.equation
  112. };
  113. },
  114. equationValue({ domId, value }) {
  115. if (domId) {
  116. let dom = document.getElementById(domId);
  117. if (dom) {
  118. this.valueObj[domId] = value;
  119. dom.value = value;
  120. }
  121. }
  122. },
  123. init({ form, valueObj, equation }) {
  124. this.form = form;
  125. this.valueObj = valueObj;
  126. this.equation = equation || {};
  127. this.$nextTick(() => {
  128. if (!this.edit) {
  129. let inputs = document.querySelectorAll('.templateInput');
  130. inputs.forEach((item) => {
  131. item.setAttribute('readonly', 'readonly');
  132. });
  133. }
  134. });
  135. this.objInit();
  136. },
  137. editInputChange(domObj) {
  138. let dom = document.getElementById(this.domId);
  139. dom.style.width = domObj.width + 'px';
  140. if (domObj.readonly == 2) {
  141. dom.setAttribute('readonly', 'readonly');
  142. } else {
  143. dom.removeAttribute('readonly');
  144. }
  145. delete this.valueObj[dom.id];
  146. this.valueObj[domObj.id] = dom.value;
  147. if (domObj.equation) {
  148. this.equation[domObj.id] = domObj.equation;
  149. }
  150. dom.id = domObj.id;
  151. },
  152. onRightClick(PointerEvent) {
  153. this.rightClickShow = true;
  154. this.$nextTick(() => {
  155. let y = PointerEvent.pageY;
  156. let x = PointerEvent.pageX + 10;
  157. if (PointerEvent.screenY >= PointerEvent.view.innerHeight) {
  158. y -= 80;
  159. }
  160. this.$refs.popoverRef.$el.style.top = y + 'px';
  161. this.$refs.popoverRef.$el.style.left = x + 'px';
  162. });
  163. },
  164. inputChange(event) {
  165. if (event.target && event.target.className == 'templateInput') {
  166. this.valueObj[event.target.id] = event.target.value;
  167. this.$emit('calculation');
  168. }
  169. },
  170. inputClick(event) {
  171. if (!this.edit) {
  172. return;
  173. }
  174. if (event.target && event.target.className == 'templateInput') {
  175. this.domId = event.target.id;
  176. this.$emit('editShow', {
  177. templateDivRef: 'customTextRef' + this.id,
  178. domObj: {
  179. width: event.target.offsetWidth,
  180. id: event.target.id,
  181. readonly: event.target.readOnly ? 2 : 1,
  182. equation: this.equation[this.domId]
  183. }
  184. });
  185. }
  186. }
  187. },
  188. mounted() {
  189. this.$nextTick(() => {
  190. document
  191. .getElementById(this.id)
  192. .addEventListener('input', this.inputChange);
  193. document
  194. .getElementById(this.id)
  195. .addEventListener('click', this.inputClick);
  196. });
  197. }
  198. };
  199. </script>
  200. <style lang="scss" scoped>
  201. :deep(.templateInput) {
  202. width: 80px;
  203. border: solid 1px #bfbfbf;
  204. padding: 1px;
  205. margin: 1px;
  206. margin-left: 3px;
  207. text-align: center;
  208. }
  209. .contenteditable {
  210. // padding: 5px;
  211. width: 100%;
  212. min-height: 20px;
  213. padding-left: 10px;
  214. // border: solid 1px #b3b0b0;
  215. }
  216. </style>