From 0e5b44baad038dfd99ac832211bd2133cb435d81 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 19 Mar 2024 03:15:19 +0900 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=87=AA=E5=8A=A8=E8=AF=86=E5=88=ABmd?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manage/components/ChatInput/index.vue | 19 ++++++++++-- resources/assets/js/store/markdown.js | 29 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/resources/assets/js/pages/manage/components/ChatInput/index.vue b/resources/assets/js/pages/manage/components/ChatInput/index.vue index 786254a75..f308089b9 100755 --- a/resources/assets/js/pages/manage/components/ChatInput/index.vue +++ b/resources/assets/js/pages/manage/components/ChatInput/index.vue @@ -156,7 +156,11 @@
- {{$L('Markdown 格式发送')}} + {{$L('MD 格式发送')}} +
+
+ + {{$L('正常发送')}}
@@ -214,6 +218,7 @@ import touchmouse from "../../../../directives/touchmouse"; import TransferDom from "../../../../directives/transfer-dom"; import clickoutside from "../../../../directives/clickoutside"; import longpress from "../../../../directives/longpress"; +import {isMarkdownFormat} from "../../../../store/markdown"; import {Store} from "le5le-store"; export default { @@ -356,6 +361,7 @@ export default { }, beforeDestroy() { if (this.quill) { + this.quill.getModule("mention")?.hideMentionList(); this.quill = null } if (this.recordRec) { @@ -1036,7 +1042,7 @@ export default { this.showMenu = true; }, - onSend(type) { + onSend(type = 'auto') { this.emojiTimer && clearTimeout(this.emojiTimer) this.emojiQuickShow = false; // @@ -1047,6 +1053,13 @@ export default { this.hidePopover('send') this.rangeIndex = 0 this.clearSearchKey() + // + if (type === 'auto') { + type = isMarkdownFormat(this.value) ? 'md' : '' + } + if (type === 'normal') { + type = '' + } if (type) { this.$emit('on-send', null, type) } else { @@ -1286,7 +1299,7 @@ export default { if (this.userId === data.userid || this.quoteData.userid !== data.userid) { return } - if (new RegExp(`]*?class="mention"[^>]*?data-id="${data.userid}"[^>]*?>`).test(this.$refs.editor.firstChild.innerHTML)) { + if (new RegExp(`]+?class="mention"[^>]+?data-id="${data.userid}"[^>]*?>`).test(this.$refs.editor.firstChild.innerHTML)) { return } this.addMention({ diff --git a/resources/assets/js/store/markdown.js b/resources/assets/js/store/markdown.js index ca178e0f3..9f2572744 100644 --- a/resources/assets/js/store/markdown.js +++ b/resources/assets/js/store/markdown.js @@ -51,3 +51,32 @@ export function MarkdownPreview(text) { } return MarkdownUtils.mds.render(text) } + +export function isMarkdownFormat(html) { + if (html === '') { + return false + } + if (/<\/(strong|s|em|u|ol|ul|li|blockquote|pre|img|a)>/i.test(html)) { + return false + } + if (/]+?class="mention"[^>]*?>/i.test(html)) { + return false + } + // + const text = html.replace(/<[^>]+?>/g, '') + if ( + /(^|\s+)#+\s(.*)$/m.test(text) // 标题 + || /\*\*(.*)\*\*/m.test(text) // 粗体 + || /__(.*)__/m.test(text) // 粗体 + || /\*(.*)\*/m.test(text) // 斜体 + || /_(.*)_/m.test(text) // 斜体 + || /~~(.*)~~/m.test(text) // 删除线 + || /\[(.*?)\]\((.*?)\)/m.test(text) // 链接 + || /!\[(.*?)\]\((.*?)\)/m.test(text) // 图片 + || /`(.*?)`/m.test(text) // 行内代码 + || /```([\s\S]*?)```/m.test(text) // 代码块 + ) { + return true + } + return false +}