editor.vue 10.0 KB

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