follow-input.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. import { reactive, ref, onMounted, defineEmits, defineProps, watch } from "vue";
  57. import {searchTwitterUser} from "@/http/publishApi";
  58. import {getChromeStorage} from "@/uilts/chromeExtension"
  59. import {debounce} from "@/uilts/help";
  60. const props = defineProps({
  61. atUserList: {
  62. type: Array,
  63. default: () => {
  64. return []
  65. }
  66. },
  67. isAddSelf: {
  68. type: Boolean,
  69. default: true
  70. }
  71. })
  72. let currentIptIndex = ref(-1);
  73. let isActiveAddBtn = ref(false);
  74. let currentUserIndex = ref(-1);
  75. let domStyle = ref({
  76. left: 0,
  77. top: 0
  78. })
  79. let userList = ref([]);
  80. let userInfo = reactive({});
  81. let pageAtUserList = ref(props.atUserList);
  82. const emits = defineEmits(["addUser", "setUser", "delUser"]);
  83. onMounted(() => {
  84. if(!pageAtUserList.value.length) {
  85. getUserInfo((info) => {
  86. if(info && info.nickName && props.isAddSelf) {
  87. addUser(info.nickName);
  88. setTimeout(() => {
  89. setIptWidth(0);
  90. }, 600)
  91. }
  92. });
  93. } else {
  94. for(let i = 0; i < pageAtUserList.value.length; i++ ) {
  95. setTimeout(() => {
  96. setIptWidth(i);
  97. })
  98. }
  99. }
  100. document.addEventListener('click',e => {
  101. let dom = document.querySelector('.user-list-wrapper');
  102. if (dom) {
  103. if (!dom.contains(e.target)) {
  104. userList.value = [];
  105. } else {
  106. }
  107. }
  108. })
  109. })
  110. watch(
  111. () => props.atUserList,
  112. (newVal) => {
  113. console.log(newVal, '222')
  114. },
  115. {
  116. deep: true
  117. }
  118. );
  119. const getUserInfo = (cb) => {
  120. getChromeStorage('userInfo', (res) => {
  121. if(res) {
  122. userInfo.value = res;
  123. }
  124. cb && cb(res);
  125. })
  126. }
  127. const addUser = (name = '') => {
  128. emits('addUser', {
  129. name
  130. })
  131. }
  132. const addInput = () => {
  133. addUser('');
  134. setTimeout(() => {
  135. let index = props.atUserList.length - 1;
  136. setIptWidth(index);
  137. let iptDom = document.getElementById('input'+index);
  138. iptDom.focus();
  139. })
  140. }
  141. const delUser = (params, index) => {
  142. emits("delUser", {index})
  143. }
  144. const onfocus = (params, index) => {
  145. console.log(params, index)
  146. }
  147. const onblur = (params, index) => {
  148. console.log(params, index)
  149. }
  150. const onInput = (params, index) => {
  151. currentIptIndex.value = index;
  152. emits("setUser", {index: index, name: params.name})
  153. getTwitterUsers(params.name)
  154. }
  155. const onKeydown = (params, index) => {
  156. // setIptWidth(index);
  157. }
  158. const onKeyup = (params, index) => {
  159. setIptWidth(index);
  160. }
  161. const setIptWidth = (index) => {
  162. let iptDom = document.getElementById('input'+index);
  163. if(iptDom) {
  164. iptDom.style.width = document.getElementById('pre'+index).offsetWidth + 'px'
  165. } else {
  166. setTimeout(() => {
  167. let iptDom = document.getElementById('input'+index);
  168. iptDom.style.width = document.getElementById('pre'+index).offsetWidth + 'px'
  169. console.log('iptDom1',iptDom,document.getElementById('pre'+index).offsetWidth);
  170. }, 500)
  171. }
  172. }
  173. const onIptChange = (params, index) => {
  174. console.log(index)
  175. }
  176. const handleMouseEn = () => {
  177. isActiveAddBtn.value = true;
  178. }
  179. const handleMouseLe = () => {
  180. isActiveAddBtn.value = false;
  181. }
  182. const iptMouseenter = (e) => {
  183. rectClick(e);
  184. }
  185. const rectClick = (event) => {
  186. //获取相对于当前所指向对象的位置坐标
  187. console.log(event,'x:' + event.clientX + " y:" + event.clientY);
  188. let dom = event.target;
  189. domStyle.value = {
  190. left : (dom.getBoundingClientRect().left / 2) - 140,
  191. top: dom.getBoundingClientRect().top + 20
  192. }
  193. }
  194. const getTwitterUsers = debounce(function(query, cb) {
  195. query = query.replace("@", "");
  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. }, 500)
  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 4px 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: 4px;
  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. min-width: 12px;
  261. max-width: 128px;
  262. }
  263. .at-user-input {
  264. color: #1D9BF0;
  265. border: none;
  266. outline: none;
  267. }
  268. .user-list-wrapper {
  269. width: 284px;
  270. max-height: 430px;
  271. position: absolute;
  272. box-shadow: 0px 4px 20px 0px #0000004D;
  273. overflow-y: auto;
  274. background-color: #fff;
  275. top: 30px;
  276. left: -150px;
  277. z-index: 1000;
  278. border-radius: 10px;
  279. .item {
  280. width: 100%;
  281. height: auto;
  282. box-sizing: border-box;
  283. padding: 8px 16px;
  284. cursor: pointer;
  285. .following {
  286. font-weight: 500;
  287. font-size: 14px;
  288. color: #566370;
  289. display: flex;
  290. align-items: center;
  291. margin-left: 20px;
  292. img {
  293. margin-right: 6px;
  294. }
  295. }
  296. .content {
  297. display: flex;
  298. align-items: center;
  299. box-sizing: border-box;
  300. margin: 5px 0;
  301. height: 100%;
  302. .avatar {
  303. width: 40px;
  304. height: 40px;
  305. border-radius: 50%;
  306. margin-right: 10px;
  307. }
  308. div {
  309. width: calc(100% - 50px);
  310. .name {
  311. font-weight: 600;
  312. font-size: 16px;
  313. margin-bottom: 2px;
  314. color: #000;
  315. overflow: hidden;
  316. text-overflow: ellipsis;
  317. white-space: nowrap;
  318. }
  319. .screenName {
  320. font-size: 15px;
  321. color: #566370;
  322. }
  323. }
  324. }
  325. }
  326. .active {
  327. background-color: #F7F9F9 !important;
  328. }
  329. }
  330. }
  331. .icon-add-wrapper {
  332. display: flex;
  333. align-items: center;
  334. margin-top: 10px;
  335. }
  336. }
  337. </style>