follow-input.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <template>
  2. <!-- 发布器-@关注人 输入组件 -->
  3. <div class="follow-input-wrapper">
  4. <div class="at-user-item" v-for="(item, index) in pageAtUserList" :key="index">
  5. <img :src="require('@/assets/svg/icon-del-follows-user.svg')"
  6. class="icon-del"
  7. @click="delUser(item, index)">
  8. <pre :id='"pre" + index' class="at-user-input-placeholder">{{item.name}}</pre>
  9. @<input class="at-user-input"
  10. :id='"input" + index'
  11. v-model="item.name"
  12. @change="onIptChange(item, index)"
  13. @input="onInput(item, index)"
  14. @keydown="onKeydown(item, index)"
  15. @keyup="onKeyup(item, index)"
  16. @blur="onblur(item, index)"
  17. @focus="onfocus(item, index)"
  18. @mouseenter="iptMouseenter"/>
  19. <template v-if="currentIptIndex == index && userList.length">
  20. <div class="user-list-wrapper">
  21. <div class="item"
  22. v-for="(item, index) in userList"
  23. :key="index"
  24. :class="{'active': index === currentUserIndex}"
  25. @mouseenter="onUserMouseEnter(item, index)"
  26. @mouseleave="onUserMouseLeave(item, index)"
  27. @click="selectedUser(item, index)">
  28. <div class="following" v-if="item.following">
  29. <img :src="require('@/assets/svg/icon-following-user.svg')">following
  30. </div>
  31. <div class="content">
  32. <img class="avatar" :src="item.avatarlUrl">
  33. <div>
  34. <div class="name">{{item.name}}</div>
  35. <div class="screenName">@{{item.screenName}}</div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </template>
  41. </div>
  42. <div class="icon-add-wrapper" v-if="pageAtUserList.length < 5"
  43. @click="addInput"
  44. @mouseenter="handleMouseEn"
  45. @mouseleave="handleMouseLe">
  46. <img :src="require('@/assets/svg/icon-add-user-default.svg')"
  47. class="icon-add"
  48. v-if="!isActiveAddBtn">
  49. <img :src="require('@/assets/svg/icon-add-user-active.svg')"
  50. class="icon-add"
  51. v-else>
  52. </div>
  53. </div>
  54. </template>
  55. <script setup>
  56. /* eslint-disable */
  57. import { reactive, ref, onMounted, defineEmits, defineProps, watch } from "vue";
  58. import {searchTwitterUser} from "@/http/publishApi";
  59. import {getChromeStorage} from "@/uilts/chromeExtension"
  60. import {debounce} from "@/uilts/help";
  61. const props = defineProps({
  62. atUserList: {
  63. type: Array,
  64. default: () => {
  65. return []
  66. }
  67. },
  68. isAddSelf: {
  69. type: Boolean,
  70. default: true
  71. }
  72. })
  73. let currentIptIndex = ref(-1);
  74. let isActiveAddBtn = ref(false);
  75. let currentUserIndex = ref(-1);
  76. let domStyle = ref({
  77. left: 0,
  78. top: 0
  79. })
  80. let userList = ref([]);
  81. let userInfo = reactive({});
  82. let pageAtUserList = ref(props.atUserList);
  83. const emits = defineEmits(["addUser", "setUser", "delUser"]);
  84. onMounted(() => {
  85. if(!pageAtUserList.value.length) {
  86. getUserInfo((info) => {
  87. if(info && info.nickName && props.isAddSelf) {
  88. addUser(info.nickName);
  89. setTimeout(() => {
  90. setIptWidth(0);
  91. }, 600)
  92. }
  93. });
  94. } else {
  95. for(let i = 0; i < pageAtUserList.value.length; i++ ) {
  96. setTimeout(() => {
  97. setIptWidth(i);
  98. })
  99. }
  100. }
  101. document.addEventListener('click',e => {
  102. let dom = document.querySelector('.user-list-wrapper');
  103. if (dom) {
  104. if (!dom.contains(e.target)) {
  105. userList.value = [];
  106. } else {
  107. }
  108. }
  109. })
  110. })
  111. watch(
  112. () => props.atUserList,
  113. (newVal) => {
  114. console.log(newVal, '222')
  115. },
  116. {
  117. deep: true
  118. }
  119. );
  120. const getUserInfo = (cb) => {
  121. getChromeStorage('userInfo', (res) => {
  122. if(res) {
  123. userInfo.value = res;
  124. }
  125. cb && cb(res);
  126. })
  127. }
  128. const addUser = (name = '') => {
  129. emits('addUser', {
  130. name
  131. })
  132. }
  133. const addInput = () => {
  134. addUser('');
  135. setTimeout(() => {
  136. let index = props.atUserList.length - 1;
  137. setIptWidth(index);
  138. let iptDom = document.getElementById('input'+index);
  139. iptDom.focus();
  140. })
  141. }
  142. const delUser = (params, index) => {
  143. emits("delUser", {index})
  144. }
  145. const onfocus = (params, index) => {
  146. console.log(params, index)
  147. }
  148. const onblur = (params, index) => {
  149. console.log(params, index)
  150. }
  151. const onInput = debounce(function(params, index) {
  152. currentIptIndex.value = index;
  153. emits("setUser", {index: index, name: params.name})
  154. getTwitterUsers(params.name)
  155. }, 500)
  156. const onKeydown = (params, index) => {
  157. // setIptWidth(index);
  158. }
  159. const onKeyup = (params, index) => {
  160. setIptWidth(index);
  161. }
  162. const setIptWidth = (index) => {
  163. let iptDom = document.getElementById('input'+index);
  164. if(iptDom) {
  165. iptDom.style.width = document.getElementById('pre'+index).offsetWidth + 'px'
  166. } else {
  167. setTimeout(() => {
  168. let iptDom = document.getElementById('input'+index);
  169. iptDom.style.width = document.getElementById('pre'+index).offsetWidth + 'px'
  170. console.log('iptDom1',iptDom,document.getElementById('pre'+index).offsetWidth);
  171. }, 500)
  172. }
  173. }
  174. const onIptChange = (params, index) => {
  175. console.log(index)
  176. }
  177. const handleMouseEn = () => {
  178. isActiveAddBtn.value = true;
  179. }
  180. const handleMouseLe = () => {
  181. isActiveAddBtn.value = false;
  182. }
  183. const iptMouseenter = (e) => {
  184. rectClick(e);
  185. }
  186. const rectClick = (event) => {
  187. //获取相对于当前所指向对象的位置坐标
  188. console.log(event,'x:' + event.clientX + " y:" + event.clientY);
  189. let dom = event.target;
  190. domStyle.value = {
  191. left : (dom.getBoundingClientRect().left / 2) - 140,
  192. top: dom.getBoundingClientRect().top + 20
  193. }
  194. }
  195. const getTwitterUsers = (query, cb) => {
  196. searchTwitterUser({
  197. params: {
  198. name : query
  199. }
  200. }).then(res => {
  201. if(res.code == 0) {
  202. userList.value = res.data;
  203. }
  204. cb && cb(res);
  205. console.log('searchTwitterUser',res)
  206. })
  207. }
  208. const selectedUser = (params, index) => {
  209. emits("setUser", {index: currentIptIndex.value, name: params.screenName});
  210. setTimeout(() => {
  211. setIptWidth(currentIptIndex.value);
  212. currentIptIndex.value = -1;
  213. })
  214. }
  215. const onUserMouseEnter = (params, index) => {
  216. currentUserIndex.value = index;
  217. }
  218. const onUserMouseLeave = (params, index) => {
  219. currentUserIndex.value = -1;
  220. }
  221. </script>
  222. <style lang="scss" scoped>
  223. .follow-input-wrapper {
  224. width: 100%;
  225. display: flex;
  226. flex-wrap: wrap;
  227. padding: 0px 0 10px 18px;
  228. box-sizing: border-box;
  229. .icon-add{
  230. width: 24px;
  231. height: 24px;
  232. margin-left: 10px;
  233. cursor: pointer;
  234. }
  235. .at-user-item {
  236. display: flex;
  237. align-items: center;
  238. border: 1px solid #ECECEC;
  239. box-sizing: border-box;
  240. border-radius: 100px;
  241. color: #1D9BF0;
  242. font-weight: 500;
  243. font-size: 15px;
  244. padding: 4px 12px;
  245. position: relative;
  246. margin-right: 10px;
  247. background-color: #fff;
  248. margin-top: 10px;
  249. .icon-del {
  250. width: 18px;
  251. height: 18px;
  252. position: absolute;
  253. right: -7px;
  254. top: -6px;
  255. cursor: pointer;
  256. }
  257. .at-user-input-placeholder {
  258. position: absolute;
  259. top: -1000px;
  260. font-size: 13px;
  261. min-width: 12px;
  262. max-width: 128px;
  263. }
  264. .at-user-input {
  265. color: #1D9BF0;
  266. border: none;
  267. outline: none;
  268. }
  269. .user-list-wrapper {
  270. width: 284px;
  271. max-height: 430px;
  272. position: absolute;
  273. box-shadow: 0px 4px 20px 0px #0000004D;
  274. overflow-y: scroll;
  275. background-color: #fff;
  276. top: 30px;
  277. left: -150px;
  278. z-index: 1000;
  279. border-radius: 10px;
  280. .item {
  281. width: 100%;
  282. height: auto;
  283. box-sizing: border-box;
  284. padding: 8px 16px;
  285. cursor: pointer;
  286. .following {
  287. font-weight: 500;
  288. font-size: 14px;
  289. color: #566370;
  290. display: flex;
  291. align-items: center;
  292. margin-left: 20px;
  293. img {
  294. margin-right: 6px;
  295. }
  296. }
  297. .content {
  298. display: flex;
  299. align-items: center;
  300. box-sizing: border-box;
  301. margin: 5px 0;
  302. height: 100%;
  303. .avatar {
  304. width: 40px;
  305. height: 40px;
  306. border-radius: 50%;
  307. margin-right: 10px;
  308. }
  309. div {
  310. width: calc(100% - 50px);
  311. .name {
  312. font-weight: 600;
  313. font-size: 16px;
  314. margin-bottom: 2px;
  315. color: #000;
  316. overflow: hidden;
  317. text-overflow: ellipsis;
  318. white-space: nowrap;
  319. }
  320. .screenName {
  321. font-size: 15px;
  322. color: #566370;
  323. }
  324. }
  325. }
  326. }
  327. .active {
  328. background-color: #F7F9F9 !important;
  329. }
  330. }
  331. }
  332. .icon-add-wrapper {
  333. display: flex;
  334. align-items: center;
  335. margin-top: 14px;
  336. }
  337. }
  338. </style>