follow-input.vue 11 KB

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