editor.vue 9.7 KB

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