follow-input.vue 10 KB

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