editor.vue 10 KB

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