editor.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <div class="editor-wrapper">
  3. <div class="top">
  4. <div class="title">
  5. Enter Link to Embed in Tweet
  6. </div>
  7. <div class="search-wrapper">
  8. <input class="input" type="text" v-model="siteUrl" placeholder="Enter link">
  9. <div class="btn" @click="searchHandler">
  10. <img :src="require('@/assets/svg/icon-tool-box-search-arrow.svg')" />
  11. </div>
  12. </div>
  13. <div class="desc">
  14. <img :src="linkInputDescImage" alt="">
  15. </div>
  16. </div>
  17. <div class="bottom">
  18. <div class="content">
  19. <div class="cate-item history-wrapper" v-if="historyList.length">
  20. <div class="cate">
  21. <img :src="require('@/assets/svg/icon-tool-app-history.svg')" />
  22. </div>
  23. <div class="app-list">
  24. <div class="app" v-for="(app, idx) in historyList" :key="idx"
  25. @click="clickHistoryAppHandler(app)">
  26. <div class="img-wrapper">
  27. <img class="img" :class="{'small-img': !app.appId}" :src="app.iconPath" :onerror="imgOnError" />
  28. </div>
  29. <div class="name">
  30. {{ app.name }}
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="cate-item" v-for="(item) in appList" :key="item.cateId">
  36. <div class="cate">
  37. <img :src="item.iconPath">
  38. </div>
  39. <div class="app-list">
  40. <div class="app" v-for="(app, idx) in item.apps" :key="idx" @click="clickAppHandler(app)">
  41. <img class="app-img" :src="app.iconPath" alt="">
  42. <div class="name">
  43. {{ app.name }}
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </template>
  52. <script setup>
  53. import { ref, defineProps, defineEmits, onMounted } from "vue";
  54. import axios from 'axios';
  55. import { message } from "ant-design-vue";
  56. import { convertUrl, getAllPostEditorAppData } from "@/http/toolBoxApi";
  57. import { getChromeStorage } from "@/uilts/chromeExtension"
  58. import { checkURL } from "@/uilts/help"
  59. const props = defineProps({
  60. linkInputDescImage: {
  61. type: String,
  62. default: '',
  63. },
  64. });
  65. let siteUrl = ref('');
  66. let selectAppGuideData = {};
  67. let openWindowList = [];
  68. let historyList = ref([])
  69. let appList = ref();
  70. const emits = defineEmits(["changeShowCom"]);
  71. const searchHandler = async () => {
  72. let siteTitle = '', favicon = '';
  73. if(!siteUrl.value) {
  74. return;
  75. }
  76. siteUrl.value = siteUrl.value.trim();
  77. if(!checkURL(siteUrl.value)) {
  78. message.info('Incorrect URL entered');
  79. //提示
  80. return;
  81. }
  82. const loadingHide = message.loading('loading...', 0);
  83. let siteRes = await axios.get(siteUrl.value);
  84. if(siteRes) {
  85. if(siteRes.headers['content-type'].indexOf('text/html') < 0 || siteRes.request.status > 399) {
  86. // 提示
  87. return;
  88. }
  89. let urlObj = new URL(siteUrl.value);
  90. if(siteRes.data) {
  91. siteTitle = getTitleByHtmlStr(siteRes.data);
  92. if(!siteTitle) {
  93. siteTitle = urlObj.hostname;
  94. }
  95. console.log(siteTitle)
  96. }
  97. favicon = urlObj.origin + '/favicon.ico';
  98. }
  99. let currentApp = {
  100. appId: '',
  101. cateId: '',
  102. createType: '',
  103. defaultUrl: siteUrl.value,
  104. guideData: '',
  105. iconPath: favicon,
  106. interactType: '',
  107. linkImagePath: "",
  108. name: siteTitle,
  109. }
  110. let convertRes = await convertUrl({params: {originUrl: siteUrl.value}});
  111. let params = { convertUrl: siteUrl.value, originUrl: siteUrl.value, appId: '', currentApp };
  112. loadingHide();
  113. if(convertRes && convertRes.code == 0) {
  114. let {convertUrl} = convertRes.data || {};
  115. params.convertUrl = convertUrl;
  116. }
  117. emits('changeShowCom', params)
  118. }
  119. const getTitleByHtmlStr = (str) => {
  120. let index1 = str.indexOf('<title>') + 7;
  121. let index2 = str.indexOf('</title>');
  122. return str.substring(index1, index2) || '';
  123. };
  124. const clickHistoryAppHandler = (params) => {
  125. if(params.appId) {
  126. clickAppHandler(params);
  127. } else {
  128. siteUrl.value = params.defaultUrl;
  129. searchHandler();
  130. }
  131. };
  132. const clickAppHandler = (params) => {
  133. let { createType, defaultUrl, appId, linkImagePath } = params;
  134. switch (createType) {
  135. case 1:
  136. emits('changeShowCom', { convertUrl: defaultUrl, originUrl: defaultUrl, appId, linkImagePath, currentApp: params })
  137. break;
  138. case 2:
  139. openWindow(params);
  140. break;
  141. }
  142. }
  143. const openWindow = (params) => {
  144. chrome.windows.getCurrent({},
  145. function(window) {
  146. if(window && window.state == "fullscreen") {
  147. chrome.windows.update(window.id,{
  148. state: 'normal'
  149. }, function() {
  150. setTimeout(() => {
  151. createGuideWindow(params, true);
  152. }, 1000)
  153. })
  154. } else {
  155. createGuideWindow(params);
  156. }
  157. })
  158. };
  159. const createGuideWindow = (params, isUpdate = false) => {
  160. openWindowList = [];
  161. selectAppGuideData = {};
  162. let windowWith = window.screen.width - 500;
  163. let guideUrl = chrome.runtime.getURL('/iframe/tool-box-guide.html');
  164. chrome.windows.create({
  165. width: windowWith,
  166. type: 'normal',
  167. url: params.defaultUrl,
  168. state: 'normal'
  169. }, function (window) {
  170. openWindowList.push(window);
  171. })
  172. chrome.windows.create({
  173. width: 500,
  174. type: 'popup',
  175. url: guideUrl,
  176. left: windowWith,
  177. state: 'normal'
  178. }, function (window) {
  179. openWindowList.push(window);
  180. })
  181. if(params.guideData) {
  182. selectAppGuideData = JSON.parse(params.guideData);
  183. }
  184. }
  185. const getAppList = () => {
  186. getAllPostEditorAppData({params: {}}).then(res => {
  187. if(res.code == 0) {
  188. appList.value = res.data || [];
  189. }
  190. })
  191. }
  192. const onRuntimeMsg = () => {
  193. chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
  194. sendResponse('ok')
  195. switch (req.actionType) {
  196. case 'CONTENT_GET_GUIDE_DATA':
  197. chrome.runtime.sendMessage({
  198. actionType: "CONTENT_EDIT_SEND_GUIDE_DATA",
  199. data: {
  200. guideData: selectAppGuideData,
  201. windowData: openWindowList
  202. }
  203. },(response) => {});
  204. break;
  205. case 'CONTENT_GUIDE_APPLY_APP':
  206. siteUrl.value = req.data.siteUrl;
  207. searchHandler();
  208. break;
  209. }
  210. })
  211. }
  212. const getHistoryList = async () => {
  213. let {list = []} = await getChromeStorage('toolBoxAppHistoryData') || {};
  214. historyList.value = list;
  215. };
  216. const imgOnError = (e) => {
  217. let img = e.srcElement;
  218. img.src = require('@/assets/img/icon-default-app-logo.png');
  219. img.onerror = null;
  220. }
  221. onMounted(() => {
  222. getHistoryList();
  223. getAppList();
  224. onRuntimeMsg();
  225. })
  226. </script>
  227. <style lang="scss" scoped>
  228. .editor-wrapper {
  229. width: 100%;
  230. height: 100%;
  231. .top {
  232. padding: 25px 40px 30px 40px;
  233. border-bottom: 0.5px solid #D1D9DD;
  234. box-sizing: border-box;
  235. .title {
  236. font-weight: 500;
  237. font-size: 20px;
  238. }
  239. .search-wrapper {
  240. margin: 20px 0;
  241. border-radius: 8px;
  242. width: 100%;
  243. height: 49px;
  244. display: flex;
  245. align-items: center;
  246. .input {
  247. background: #F1F3F4;
  248. border: none;
  249. outline: none;
  250. font-weight: 400;
  251. font-size: 16px;
  252. color: #636363;
  253. height: 100%;
  254. width: calc(100% - 49px);
  255. padding-left: 20px;
  256. border-bottom-left-radius: 8px;
  257. border-top-left-radius: 8px;
  258. }
  259. .btn {
  260. width: 49px;
  261. height: 49px;
  262. background: #1D9BF0;
  263. border-bottom-right-radius: 8px;
  264. border-top-right-radius: 8px;
  265. display: flex;
  266. align-items: center;
  267. justify-content: center;
  268. cursor: pointer;
  269. }
  270. }
  271. .desc {
  272. img {
  273. width: 315px;
  274. height: 14px;
  275. object-fit: cover;
  276. }
  277. }
  278. }
  279. .bottom {
  280. width: 100%;
  281. height: calc(100% - 185px);
  282. overflow-y: auto;
  283. .content {
  284. width: 100%;
  285. padding: 36px 30px 20px 50px;
  286. box-sizing: border-box;
  287. .history-wrapper {
  288. .app-list {
  289. .img-wrapper {
  290. width: 60px;
  291. height: 60px;
  292. border-radius: 10px;
  293. margin-bottom: 10px;
  294. border: 1px solid #E5E5E5;
  295. box-sizing: border-box;
  296. display: flex;
  297. align-items: center;
  298. justify-content: center;
  299. .img {
  300. width: 100%;
  301. height: 100%;
  302. border-radius: 10px;
  303. }
  304. .small-img {
  305. width: 25px;
  306. height: 25px;
  307. }
  308. }
  309. }
  310. }
  311. .cate-item {
  312. min-height: 110px;
  313. display: flex;
  314. margin-bottom: 12px;
  315. .cate {
  316. width: 20px;
  317. height: 110px;
  318. margin-right: 26px;
  319. display: flex;
  320. align-items: center;
  321. margin-top: -10px;
  322. img {
  323. width: 20px;
  324. height: 20px;
  325. }
  326. }
  327. .app-list {
  328. display: flex;
  329. align-content: center;
  330. flex-wrap: wrap;
  331. .app {
  332. display: flex;
  333. flex-direction: column;
  334. justify-content: center;
  335. align-items: center;
  336. width: 110px;
  337. height: 110px;
  338. cursor: pointer;
  339. .app-img {
  340. width: 60px;
  341. height: 60px;
  342. border-radius: 10px;
  343. margin-bottom: 10px;
  344. border: 1px solid #E5E5E5;
  345. box-sizing: border-box;
  346. }
  347. .name {
  348. font-weight: 500;
  349. font-size: 12px;
  350. color: #636363;
  351. width: 100%;
  352. height: 15px;
  353. text-overflow: ellipsis;
  354. white-space: nowrap;
  355. overflow: hidden;
  356. text-align: center;
  357. box-sizing: border-box;
  358. padding: 0 5px;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. </style>