follow-input.vue 11 KB

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