customText.vue 6.0 KB

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