memos.html 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta
  6. name="viewport"
  7. content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"
  8. />
  9. <title>memos记录</title>
  10. <script src="js/util.js"></script>
  11. <script src="js/login.js"></script>
  12. <link rel="stylesheet" href="memos.css"/>
  13. <script src="js/vue.min.js"></script>
  14. <script src="elementUI/index.js"></script>
  15. <link rel="stylesheet" href="elementUI/index.css"/>
  16. <link rel="stylesheet" href="css/vditor.css"/>
  17. <script src="js/vditor.js"></script>
  18. </head>
  19. <body>
  20. <div id="app">
  21. <div id="all" class="main">
  22. <div class="myTop">
  23. <div class="main">
  24. <el-link href="https://memos.tianyunperfect.cn/">memos主页</el-link>
  25. <el-button @click="page = page - 1;search()" :disabled="page === 1">上一页</el-button>
  26. <el-input
  27. type="number"
  28. style="width: 80px"
  29. v-model="page"
  30. @change="search"
  31. ></el-input>
  32. <el-button @click="page = page + 1;search()">下一页</el-button>
  33. <el-select
  34. v-model="size"
  35. placeholder="请选择"
  36. style="width: 80px"
  37. @change="search"
  38. >
  39. <el-option
  40. v-for="item in [10, 20, 50, 100]"
  41. :key="item"
  42. :label="item"
  43. :value="item"
  44. >
  45. </el-option>
  46. </el-select>
  47. <!--输入框,绑定回车事件,触发查询-->
  48. <el-input
  49. style="margin-left: 30px; width: 20%; padding-left: 10px"
  50. placeholder="关键字"
  51. v-model="searchStr"
  52. @change="searchStrChange"
  53. ></el-input>
  54. <el-input
  55. style="margin-left: 30px; width: 30%; padding-left: 10px"
  56. placeholder="AI 问答"
  57. v-model="searchStr2"
  58. @change="searchStrChange2"
  59. ></el-input>
  60. <br/><br/>
  61. <div></div>
  62. <div style="display: flex; align-items: stretch; width: 90%">
  63. <!-- 创建一个用于编辑的容器 -->
  64. <el-input
  65. @paste.native="handlePaste"
  66. @keydown.enter.native="keyDown"
  67. type="textarea"
  68. :autosize="{minRows:2}"
  69. placeholder="记录内容"
  70. @input="localStorage.setItem('contentStr', contentStr)"
  71. v-model="contentStr"
  72. >
  73. </el-input>
  74. <!-- 添加发送按钮 -->
  75. <el-button
  76. style="margin-left: 20px; width: 10%; font-size: 2rem"
  77. @click="sendData"
  78. :disabled="!sendBtnAble"
  79. >记录
  80. </el-button
  81. >
  82. <el-button
  83. style="margin-left: 20px; width: 10%; font-size: 1rem"
  84. @click="mergeByIds"
  85. :disabled="selectedIds.length<=1"
  86. >合并
  87. </el-button
  88. >
  89. <div
  90. @click="dialogVisible2=true"
  91. style="font-size: 0.3em; margin-left: 10px;margin-top: 10px"
  92. >
  93. 替换
  94. </div>
  95. </div>
  96. <div style="display: flex">
  97. <div v-for="item in tmpFileIds">
  98. <el-image
  99. v-if="item['type'].startsWith('image')"
  100. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  101. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  102. class="img"
  103. ></el-image>
  104. <audio controls v-if="item['type'].startsWith('audio')">
  105. <source
  106. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  107. type="audio/ogg"
  108. />
  109. <source
  110. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  111. type="audio/mpeg"
  112. />
  113. 您的浏览器不支持 audio 元素。
  114. </audio>
  115. <video
  116. width="320"
  117. height="240"
  118. controls
  119. v-if="item['type'].startsWith('video')"
  120. >
  121. <source
  122. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  123. type="video/mp4"
  124. />
  125. <source
  126. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  127. type="video/ogg"
  128. />
  129. 您的浏览器不支持 HTML5 video 元素。
  130. </video>
  131. <el-link
  132. v-if="item['type'].startsWith('application')"
  133. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  134. target="_blank"
  135. >{{item['filename']}}
  136. </el-link
  137. >
  138. <div @click="removeTmp(item)">删除</div>
  139. </div>
  140. </div>
  141. <!-- 添加多选按钮 -->
  142. <div style="display: flex">
  143. <div style="max-width: 55vw; height: 2.5em; display: flex;align-items: center">
  144. <el-checkbox-group v-model="tagChecked" @change="checkChange1()">
  145. <el-checkbox-button
  146. v-for="tag in allTags"
  147. :label="tag"
  148. ></el-checkbox-button>
  149. </el-checkbox-group>
  150. </div>
  151. <div style="margin-left: 20px; display: flex; align-items: center">
  152. <el-switch
  153. @change="duoxuanChange"
  154. v-model="duoxuan"
  155. active-text="多选"
  156. inactive-text="单选"
  157. >
  158. </el-switch>
  159. </div>
  160. </div>
  161. <div id="tags"></div>
  162. </div>
  163. </div>
  164. <div style="margin-top: 180px"></div>
  165. <div v-if="searchStr2" style="margin: 10px; padding: 10px">
  166. <el-input
  167. v-model="searchResStr"
  168. type="textarea"
  169. :autosize="{minRows: 1}"
  170. readonly="true"
  171. ></el-input>
  172. </div>
  173. <div>
  174. <div v-for="item in contentList" class="myLine">
  175. <el-row style="margin: 10px; padding: 10px" class="border">
  176. <el-col :span="18" class="contentLine">
  177. <div style="display: flex">
  178. <!-- 日期字符串-->
  179. <div style="font-family: ui-sans-serif, system-ui; display: flex;align-items: center;color: grey;font-size: 0.9em">
  180. {{ item.created_ts.replace('T', ' ')}}
  181. </div>
  182. <div style="margin-left: 20px">
  183. <el-checkbox-button
  184. v-for="tag in commonTags"
  185. :label="tag"
  186. :key="item"
  187. @change="checked=>checkChange(checked,item,tag)"
  188. :checked="isChooseTag(item,tag)"
  189. >{{tag}}
  190. </el-checkbox-button>
  191. </div>
  192. </div>
  193. <div class="myText" @dblclick="edit(item)">
  194. <!-- <el-input v-model="item.content" type="textarea" :autosize="{minRows: 1}" readonly="true"></el-input>-->
  195. <div :id="item.resource_name"></div>
  196. </div>
  197. <div style="display: flex">
  198. <div v-for="item in resourceMap[item.id]">
  199. <el-image
  200. v-if="item['type'].startsWith('image')"
  201. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  202. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  203. fit="cover"
  204. class="img"
  205. >
  206. </el-image>
  207. <audio controls v-if="item['type'].startsWith('audio')">
  208. <source
  209. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  210. type="audio/ogg"
  211. />
  212. <source
  213. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  214. type="audio/mpeg"
  215. />
  216. 您的浏览器不支持 audio 元素。
  217. </audio>
  218. <video
  219. width="320"
  220. height="240"
  221. controls
  222. v-if="item['type'].startsWith('video')"
  223. >
  224. <source
  225. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  226. type="video/mp4"
  227. />
  228. <source
  229. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  230. type="video/ogg"
  231. />
  232. 您的浏览器不支持 HTML5 video 元素。
  233. </video>
  234. <el-link
  235. v-if="item['type'].startsWith('application')"
  236. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  237. target="_blank"
  238. >{{item['filename']}}
  239. </el-link
  240. >
  241. </div>
  242. </div>
  243. </el-col>
  244. <el-col :span="5" style="display: flex; justify-content: flex-end">
  245. <div style="display: flex; flex-direction: column; gap: 8px">
  246. <el-button
  247. size="mini"
  248. type="info"
  249. icon="el-icon-link"
  250. @click="window.open('https://memos.tianyunperfect.cn/m/' + item.resource_name)"
  251. style="width: 80px"
  252. >跳转
  253. </el-button>
  254. <el-button
  255. size="mini"
  256. type="info"
  257. icon="el-icon-folder-opened"
  258. @click="archive(item)"
  259. style="width: 80px; margin-left: 0"
  260. >归档
  261. </el-button>
  262. <el-button
  263. size="mini"
  264. :type="item.pendingDelete ? 'success' : 'danger'"
  265. :icon="item.pendingDelete ? 'el-icon-refresh' : 'el-icon-delete'"
  266. @click="del(item)"
  267. style="width: 80px; margin-left: 0"
  268. >
  269. {{ item.pendingDelete ? `(${item.countdown}s)` : '删除' }}
  270. </el-button>
  271. </div>
  272. <el-checkbox
  273. :value="selectedIds.includes(item.id)"
  274. @change="handleCheckboxChange(item.id, $event)"
  275. style="margin-top: 6px; margin-left: 20px"
  276. >合并
  277. </el-checkbox>
  278. </el-col>
  279. </el-row>
  280. </div>
  281. </div>
  282. </div>
  283. <el-dialog title="提示" :visible.sync="dialogVisible" width="60%">
  284. <div v-if="tmpItem.id" class="contentLine">
  285. <div>
  286. <el-date-picker
  287. v-model="tmpItem.created_ts"
  288. type="datetime"
  289. value-format="yyyy-MM-ddTHH:mm:ss"
  290. placeholder="选择日期时间"
  291. >
  292. </el-date-picker>
  293. <!-- el-input 只处理粘贴事件 -->
  294. <el-input
  295. @paste.native="handlePaste2"
  296. placeholder="粘贴图片"
  297. style="width: 50%"
  298. >
  299. </el-input>
  300. </div>
  301. <div>
  302. <div id="vditor"></div>
  303. <!-- <el-input
  304. ref="contentEdit"
  305. v-model="tmpItem.content"
  306. type="textarea"
  307. :autosize="{minRows: 2}"
  308. ></el-input> -->
  309. </div>
  310. <div style="display: flex">
  311. <div v-for="item in cloneFileIds">
  312. <el-image
  313. v-if="item['type'].startsWith('image')"
  314. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=150'"
  315. :preview-src-list="['https://memos_assert.tianyunperfect.cn/' + item['internal_path'] + '?width=750']"
  316. class="img"
  317. >
  318. </el-image>
  319. <audio controls v-if="item['type'].startsWith('audio')">
  320. <source
  321. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  322. type="audio/ogg"
  323. />
  324. <source
  325. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  326. type="audio/mpeg"
  327. />
  328. 您的浏览器不支持 audio 元素。
  329. </audio>
  330. <video
  331. width="320"
  332. height="240"
  333. controls
  334. v-if="item['type'].startsWith('video')"
  335. >
  336. <source
  337. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  338. type="video/mp4"
  339. />
  340. <source
  341. :src="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  342. type="video/ogg"
  343. />
  344. 您的浏览器不支持 HTML5 video 元素。
  345. </video>
  346. <el-link
  347. v-if="item['type'].startsWith('application')"
  348. :href="'https://memos_assert.tianyunperfect.cn/' + item['internal_path']"
  349. target="_blank"
  350. >{{item['filename']}}
  351. </el-link
  352. >
  353. <div @click="delPic(item)">删除</div>
  354. </div>
  355. </div>
  356. </div>
  357. <span slot="footer" class="dialog-footer">
  358. <el-button @click="dialogVisible = false">取 消</el-button>
  359. <el-button type="primary" @click="confirmSubmit">确 定</el-button>
  360. </span>
  361. </el-dialog>
  362. <el-dialog
  363. title="替换(危险操作,不可撤销)"
  364. :visible.sync="dialogVisible2"
  365. width="60%"
  366. >
  367. <div class="contentLine">
  368. <div>
  369. <el-input v-model="old_text" placeholder="old"></el-input>
  370. <el-input v-model="new_text" placeholder="new"></el-input>
  371. </div>
  372. </div>
  373. <span slot="footer" class="dialog-footer">
  374. <el-button @click="dialogVisible2 = false">取 消</el-button>
  375. <el-button type="primary" @click="confirmSubmit2">确 定</el-button>
  376. </span>
  377. </el-dialog>
  378. </div>
  379. <!--异步请求示例:requestUtil.sync('https://jsonplaceholder.typicode.com/posts/1', 'post', data, headers) .then(data => console.log(data))-->
  380. <script>
  381. let myHeaders = {
  382. "Content-type": "application/json",
  383. Authorization: "",
  384. };
  385. function getRandomId() {
  386. return Math.random().toString(36).substr(2);
  387. }
  388. let vm = new Vue({
  389. el: "#app",
  390. data: {
  391. contentEditor: "", // vditor实例
  392. dialogVisible2: false,
  393. old_text: "",
  394. new_text: "",
  395. searchStr2: "",
  396. searchResStr: "",
  397. selectedIds: [],
  398. duoxuan: false,
  399. tmpItem: {},
  400. dialogVisible: false,
  401. page: 1,
  402. size: 20,
  403. searchStr: "",
  404. contentStr: "",
  405. tagChecked: [],
  406. allTags: [],
  407. contentList: [],
  408. sendBtnAble: true,
  409. resourceMap: {}, // 资源map,
  410. commonTags: ["todo"],
  411. upload_url: "https://memos.tianyunperfect.cn/api/v1/resource/blob",
  412. tmpFileIds: [],
  413. cloneFileIds: [],
  414. },
  415. watch: {
  416. page: function () {
  417. this.search();
  418. },
  419. size: function () {
  420. this.search();
  421. },
  422. contentList: function () {
  423. console.log(1);
  424. this.$nextTick(() => {
  425. console.log(2);
  426. this.contentList.forEach((item) => {
  427. this.resetView(item);
  428. });
  429. });
  430. },
  431. },
  432. mounted() {
  433. if (!checkLogin()) {
  434. // 警告并刷新页面
  435. alert("登录失败,请先登录");
  436. location.reload();
  437. }
  438. myHeaders.Authorization = "bearer " + getCookie("token");
  439. if (this.getQueryStr()) {
  440. this.searchStr = this.getQueryStr();
  441. }
  442. this.duoxuan = this.getDuoXuan();
  443. // 初始化contentStr,如果本地有缓存
  444. let item = localStorage.getItem("contentStr");
  445. if (item) {
  446. this.contentStr = item;
  447. }
  448. let queryStr = this.getQueryStr();
  449. if (queryStr) {
  450. // queryStr 解码
  451. queryStr = decodeURIComponent(queryStr);
  452. this.searchStr = queryStr;
  453. }
  454. // 解码
  455. let tagsStr = decodeURIComponent(getQueryString("tags"));
  456. if (tagsStr) {
  457. this.tagChecked = tagsStr.split(",");
  458. }
  459. // this.getAllTags();
  460. this.search();
  461. requestUtil
  462. .async(
  463. "https://web_history.tianyunperfect.cn/memos/getCustomMemoTags",
  464. "get"
  465. )
  466. .then((res) => {
  467. this.commonTags = JSON.parse(res["res"][0]["content"]);
  468. this.allTags = this.commonTags;
  469. });
  470. },
  471. methods: {
  472. highlightHashTags(element) {
  473. // 获取目标元素
  474. if (!element) return;
  475. // 使用正则表达式匹配所有以#开头的标签
  476. const regex = /#(\S+)/g; // 匹配#后跟非空格字符的组合
  477. // 替换匹配项为带样式的span元素
  478. element.innerHTML = element.innerHTML.replace(regex,
  479. '<span style="color: #679fe0;">$&</span>'
  480. );
  481. },
  482. resetView(item) {
  483. let elementById = document.getElementById(item.resource_name);
  484. Vditor.preview(
  485. elementById,
  486. item.content,
  487. {
  488. cdn: "https://ld246.com/js/lib/vditor",
  489. speech: {
  490. enable: false,
  491. },
  492. anchor: 1,
  493. after() {
  494. vm.highlightHashTags(document.getElementById(item.resource_name));
  495. },
  496. },
  497. );
  498. // elementById 的html里面,如果有文本是 #文本 的话,就替换给加一个颜色
  499. },
  500. mergeByIds() {
  501. if (this.selectedIds.length < 2) {
  502. return;
  503. }
  504. requestUtil.sync(
  505. "https://web_history.tianyunperfect.cn/memos/mergeMemo",
  506. "post",
  507. {ids: this.selectedIds},
  508. {}
  509. );
  510. location.reload();
  511. },
  512. handleCheckboxChange(id, checked) {
  513. if (checked) {
  514. this.selectedIds.push(id);
  515. } else {
  516. this.selectedIds = this.selectedIds.filter(
  517. (selectedId) => selectedId !== id
  518. );
  519. }
  520. console.log(this.selectedIds);
  521. },
  522. searchStrChange() {
  523. let queryStr = this.searchStr;
  524. // url编码
  525. queryStr = encodeURIComponent(queryStr);
  526. location.href = requestUtil.buildUrl(location.href, {
  527. queryStr: queryStr,
  528. });
  529. },
  530. searchStrChange2() {
  531. this.searchResStr = "";
  532. requestUtil
  533. .async(
  534. "https://wx.tianyunperfect.cn/full_search?",
  535. "post",
  536. {search: this.searchStr2},
  537. {}
  538. )
  539. .then((res) => {
  540. if (res["list"]) {
  541. let resList = res["list"];
  542. this.searchResStr = res["res"];
  543. this.contentList = resList;
  544. this.searchByContent();
  545. }
  546. });
  547. },
  548. duoxuanChange() {
  549. if (this.duoxuan) {
  550. this.setDuoXuan("ok");
  551. } else {
  552. this.setDuoXuan("");
  553. }
  554. },
  555. checkChange1(newVal) {
  556. console.log(this.tagChecked);
  557. if (!this.getDuoXuan()) {
  558. if (this.tagChecked.length > 1) {
  559. // 保留最后一个
  560. this.tagChecked = [this.tagChecked[this.tagChecked.length - 1]];
  561. }
  562. }
  563. location.href = requestUtil.buildUrl(location.href, {
  564. tags: this.tagChecked.join(","),
  565. });
  566. },
  567. getQueryStr() {
  568. return getQueryString("queryStr");
  569. },
  570. getDuoXuan() {
  571. return getQueryString("duoxuan") == "ok";
  572. },
  573. setDuoXuan(ok) {
  574. location.href = requestUtil.buildUrl(location.href, {
  575. duoxuan: ok,
  576. });
  577. },
  578. removeTmp(item) {
  579. // tmpFileIds 根据id删除
  580. let index = this.tmpFileIds.findIndex(
  581. (item) => item.id === item.id
  582. );
  583. if (index !== -1) {
  584. this.tmpFileIds.splice(index, 1);
  585. }
  586. },
  587. delPic(img) {
  588. // 根据id从 cloneFileIds 删除
  589. let index = this.cloneFileIds.findIndex(
  590. (item) => item.id === img.id
  591. );
  592. if (index !== -1) {
  593. this.cloneFileIds.splice(index, 1);
  594. }
  595. },
  596. getCloneResourceIds(id) {
  597. let resourceMapElement = this.resourceMap[id];
  598. if (resourceMapElement) {
  599. return JSON.parse(JSON.stringify(resourceMapElement));
  600. }
  601. return [];
  602. },
  603. uploadFile(url, file, headers = {}) {
  604. return new Promise((resolve, reject) => {
  605. const xhr = new XMLHttpRequest();
  606. xhr.open("POST", url, true); // 使用异步的方式
  607. const formData = new FormData();
  608. formData.append("file", file);
  609. for (const [key, value] of Object.entries(headers)) {
  610. xhr.setRequestHeader(key, value);
  611. }
  612. xhr.onload = function () {
  613. if (xhr.status === 200) {
  614. resolve(JSON.parse(xhr.responseText));
  615. } else {
  616. reject(new Error(xhr.statusText));
  617. }
  618. };
  619. xhr.onerror = function () {
  620. reject(new Error("Network Error"));
  621. };
  622. xhr.send(formData);
  623. });
  624. },
  625. getFileFromPaste(e) {
  626. let clipboardData = e.clipboardData; // IE
  627. if (!clipboardData) {
  628. //chrome
  629. clipboardData = e.originalEvent.clipboardData;
  630. }
  631. let items = "";
  632. items = (e.clipboardData || window.clipboardData).items;
  633. let file = null;
  634. if (!items || items.length === 0) {
  635. this.$message.error(
  636. "当前浏览器不支持粘贴本地图片,请打开图片复制后再粘贴!"
  637. );
  638. return;
  639. }
  640. // 搜索剪切板items
  641. for (let i = 0; i < items.length; i++) {
  642. // 限制上传文件类型
  643. if (items[i].type.indexOf("image") !== -1) {
  644. file = items[i].getAsFile();
  645. break;
  646. }
  647. }
  648. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  649. if (file) {
  650. return file;
  651. }
  652. },
  653. handlePaste(e) {
  654. let file = this.getFileFromPaste(e);
  655. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  656. if (file) {
  657. this.uploadFile(this.upload_url, file, {
  658. Authorization: myHeaders["Authorization"],
  659. }).then((res) => {
  660. let fileId = res["id"];
  661. if (fileId) {
  662. let sourceRes = requestUtil.sync(
  663. "https://web_history.tianyunperfect.cn/memos/resourceById",
  664. "post",
  665. {ids: fileId},
  666. {}
  667. );
  668. let sourceRe = sourceRes["res"];
  669. this.tmpFileIds.push(...sourceRe);
  670. }
  671. });
  672. }
  673. },
  674. handlePaste2(e) {
  675. let file = this.getFileFromPaste(e);
  676. // 对复制黏贴的类型进行判断,若是非文件类型,比如复制黏贴的文字,则不会调用上传文件的函数
  677. if (file) {
  678. this.uploadFile(this.upload_url, file, {
  679. Authorization: myHeaders["Authorization"],
  680. }).then((res) => {
  681. let fileId = res["id"];
  682. if (fileId) {
  683. let sourceRes = requestUtil.sync(
  684. "https://web_history.tianyunperfect.cn/memos/resourceById",
  685. "post",
  686. {ids: fileId},
  687. {}
  688. );
  689. let sourceRe = sourceRes["res"];
  690. this.cloneFileIds.push(...sourceRe);
  691. }
  692. });
  693. }
  694. },
  695. keyDown() {
  696. // 如果是ctrl + enter \ meta + enter 则发送
  697. if (event.ctrlKey || event.metaKey) {
  698. this.sendData();
  699. }
  700. },
  701. msg(content) {
  702. this.$message({
  703. message: content,
  704. type: "success",
  705. });
  706. },
  707. isChooseTag(item, tag) {
  708. return (item.content.indexOf("#" + tag + " ") !== -1) || (item.content.endsWith("#" + tag));
  709. },
  710. checkChange(val, item, tag) {
  711. if (val) {
  712. this.addTag(item, tag);
  713. } else {
  714. this.delTag(item, tag);
  715. }
  716. this.resetView(item);
  717. },
  718. addTag(item, cTag) {
  719. let content = item.content;
  720. // 如果已经有了,就不添加
  721. let tmpTag = "#" + cTag + " ";
  722. if (content.indexOf(tmpTag) !== -1) {
  723. return;
  724. }
  725. item.content = tmpTag + content;
  726. // 提交记录
  727. let res = requestUtil.sync(
  728. "https://web_history.tianyunperfect.cn/memos/updateContent",
  729. "post",
  730. {
  731. id: item.id,
  732. content: item.content,
  733. created_ts: item.created_ts,
  734. },
  735. {}
  736. );
  737. if (res.code === 200) {
  738. this.msg("更新成功");
  739. // this.search();
  740. }
  741. },
  742. delTag(item, cTag) {
  743. let content = item.content;
  744. // 如果没有,就不删除
  745. if (content.indexOf(cTag) === -1) {
  746. return;
  747. }
  748. item.content = content.replaceAll("#" + cTag + " ", "");
  749. // 最少有一个标签
  750. if (!item.content.includes("#")) {
  751. item.content = "#todo " + item.content;
  752. }
  753. // 提交记录
  754. let res = requestUtil.sync(
  755. "https://web_history.tianyunperfect.cn/memos/updateContent",
  756. "post",
  757. {
  758. id: item.id,
  759. content: item.content,
  760. created_ts: item.created_ts,
  761. },
  762. {}
  763. );
  764. if (res.code === 200) {
  765. this.msg("更新成功");
  766. // this.search();
  767. }
  768. },
  769. // 修正后的删除逻辑,总计时3秒支持撤销
  770. del(item) {
  771. // 如果正在等待删除,则取消删除
  772. if (item.pendingDelete) {
  773. clearInterval(item.countdownInterval);
  774. this.$set(item, "pendingDelete", false);
  775. this.$set(item, "countdown", 0);
  776. this.$notify({
  777. type: "info",
  778. title: "已取消",
  779. message: "已取消删除操作",
  780. duration: 1500,
  781. });
  782. return;
  783. }
  784. // 开始删除倒计时
  785. this.$set(item, "pendingDelete", true);
  786. this.$set(item, "countdown", 5);
  787. // 设置倒计时通知
  788. item.countdownInterval = setInterval(() => {
  789. this.$set(item, "countdown", item.countdown - 1);
  790. if (item.countdown <= 0) {
  791. clearInterval(item.countdownInterval);
  792. // 执行删除请求
  793. requestUtil
  794. .async(
  795. `https://web_history.tianyunperfect.cn/memos/delete`,
  796. "post",
  797. {id: item.id},
  798. {}
  799. )
  800. .then((res) => {
  801. if (res.code === 200) {
  802. this.msg("删除成功");
  803. this.contentList = this.contentList.filter(
  804. (it) => it.id !== item.id
  805. );
  806. }
  807. });
  808. }
  809. }, 1000);
  810. },
  811. archive(item) {
  812. requestUtil
  813. .async(
  814. `https://web_history.tianyunperfect.cn/memos/archive`,
  815. "post",
  816. {id: item.id},
  817. {}
  818. )
  819. .then((res) => {
  820. this.msg("归档成功");
  821. // 本地缓存更新
  822. this.contentList = this.contentList.filter(
  823. (it) => it.id !== item.id
  824. );
  825. });
  826. },
  827. handleClose(done) {
  828. this.dialogVisible = false;
  829. done();
  830. },
  831. confirmSubmit(done) {
  832. // 提交记录
  833. let res = requestUtil.sync(
  834. "https://web_history.tianyunperfect.cn/memos/update",
  835. "post",
  836. {
  837. id: this.tmpItem.id,
  838. content: this.contentEditor.getValue(),
  839. created_ts: this.tmpItem.created_ts,
  840. resourceIdList: this.cloneFileIds.map((it) => it.id),
  841. },
  842. {}
  843. );
  844. if (res.code === 200) {
  845. this.msg("更新成功");
  846. this.tmpItem.content = this.contentEditor.getValue();
  847. this.resetView(this.tmpItem);
  848. // 更新主页面的图片
  849. this.resourceMap[this.tmpItem.id] = this.cloneFileIds;
  850. // this.search();
  851. }
  852. this.dialogVisible = false;
  853. done();
  854. },
  855. edit(item) {
  856. this.tmpItem = item;
  857. this.cloneFileIds = this.getCloneResourceIds(item.id);
  858. this.dialogVisible = true;
  859. this.$nextTick(() => {
  860. this.contentEditor = new Vditor("vditor", {
  861. height: 360,
  862. toolbarConfig: {
  863. // pin: true,
  864. },
  865. cache: {
  866. enable: false,
  867. },
  868. mode: "ir",
  869. // 其他配置...
  870. after: () => {
  871. this.contentEditor.setValue(this.tmpItem.content);
  872. this.contentEditor.focus();
  873. },
  874. });
  875. });
  876. },
  877. isDisabled(item) {
  878. // 默认不可编辑,这个时候 没有disable属性
  879. return item["disable"] !== false;
  880. },
  881. sendData: function () {
  882. this.sendBtnAble = false;
  883. let content = this.contentStr;
  884. let categories = this.tagChecked;
  885. // 循环拼接类似 #tag #tag2 content
  886. let updatedContent = content;
  887. if (categories.length > 0) {
  888. // 循环拼接类似 #tag #tag2 content
  889. updatedContent =
  890. categories.map((tag) => `#${tag}`).join(" ") + " " + content;
  891. }
  892. // updatedContent 去除尾部的\n,可能有多个
  893. updatedContent = updatedContent.replace(/\n+$/, "");
  894. // updatedContent 最少有一个标签,如果没有就添加一个标签 #todo,检测是否有 #标签
  895. if (!updatedContent.includes("#")) {
  896. updatedContent = "#todo " + updatedContent;
  897. }
  898. let res = requestUtil.sync(
  899. "https://memos.tianyunperfect.cn/api/v1/memo",
  900. "post",
  901. {
  902. content: updatedContent,
  903. resourceIdList: this.tmpFileIds.map((it) => it.id),
  904. },
  905. myHeaders
  906. );
  907. if (res["name"]) {
  908. // window.location.href = `https://memos.tianyunperfect.cn/m/${res['name']}`;
  909. this.msg("记录成功");
  910. localStorage.setItem("contentStr", "");
  911. this.contentStr = "";
  912. this.tmpFileIds = [];
  913. this.search();
  914. }
  915. this.sendBtnAble = true;
  916. },
  917. search: function () {
  918. document.documentElement.scrollTo(0, 0);
  919. console.log(this.tagChecked);
  920. let params = {
  921. search: this.searchStr,
  922. tag_str: this.tagChecked.join(","),
  923. page: this.page,
  924. page_size: this.size,
  925. };
  926. let url = "https://web_history.tianyunperfect.cn/memos/list";
  927. let urlWithParams = requestUtil.buildUrl(url, params);
  928. let res = requestUtil.sync(urlWithParams, "get", null, myHeaders);
  929. if (res.code !== 200) {
  930. return;
  931. }
  932. this.contentList = res.res;
  933. this.searchByContent();
  934. },
  935. getAllTags: function () {
  936. this.allTags = requestUtil
  937. .async(
  938. "https://memos.tianyunperfect.cn/api/v1/tag",
  939. "get",
  940. null,
  941. myHeaders
  942. )
  943. .then((res) => {
  944. this.allTags = res;
  945. });
  946. },
  947. confirmSubmit2() {
  948. res = requestUtil.sync(
  949. "https://web_history.tianyunperfect.cn/memos/replace",
  950. "post",
  951. {
  952. old_text: this.old_text,
  953. new_text: this.new_text,
  954. },
  955. {}
  956. );
  957. if (res.code === 200) {
  958. this.msg("替换成功");
  959. this.search();
  960. }
  961. this.dialogVisible2 = false;
  962. },
  963. searchByContent: function () {
  964. this.resourceMap = {};
  965. // contentList 循环获取里面所有的id数组
  966. let idArr = this.contentList.map((item) => item["id"]);
  967. // 获取资源
  968. let ids = idArr.join(",");
  969. if (!ids) {
  970. return;
  971. }
  972. let sourceRes = requestUtil.sync(
  973. "https://web_history.tianyunperfect.cn/memos/resource",
  974. "post",
  975. {ids: ids},
  976. {}
  977. );
  978. // {'code': 200, 'res': [{'memo_id': 1, 'resource_name': 'EfdmvRsodBviDQWRmDiTaV'}]}
  979. // 循环遍历,以memo_id为key,resource_name数组为value
  980. sourceRes["res"].forEach((item) => {
  981. if (this.resourceMap[item["memo_id"]]) {
  982. this.resourceMap[item["memo_id"]].push(item);
  983. } else {
  984. this.resourceMap[item["memo_id"]] = [item];
  985. }
  986. });
  987. },
  988. },
  989. });
  990. // 如果处于编辑状态,则执行esc取消
  991. window.onkeydown = function (event) {
  992. let edit = vm.dialogVisible;
  993. if (!edit) {
  994. // 如果是右箭头,则翻页
  995. if (event.keyCode === 39) {
  996. vm.page++;
  997. // 提示翻页
  998. vm.$message({
  999. message: "翻页",
  1000. type: "success",
  1001. });
  1002. }
  1003. // 如果是左箭头,则翻页
  1004. if (event.keyCode === 37 && vm.page > 1) {
  1005. vm.page--;
  1006. vm.$message({
  1007. message: "翻页",
  1008. type: "success",
  1009. });
  1010. }
  1011. return;
  1012. }
  1013. // esc
  1014. if (event.keyCode === 27) {
  1015. vm.dialogVisible = false;
  1016. }
  1017. // 如果是meta + enter 则保存
  1018. if (event.metaKey && event.keyCode === 13) {
  1019. vm.confirmSubmit();
  1020. }
  1021. // 如果是ctrl + enter 则保存
  1022. if (event.ctrlKey && event.keyCode === 13) {
  1023. vm.confirmSubmit();
  1024. }
  1025. };
  1026. </script>
  1027. </body>
  1028. </html>