From 9c63599053c44458b90545d0d75c11c298e55129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=92=83=E7=99=BD?= <18511759309@163.com> Date: Tue, 29 Jun 2021 14:53:14 +0800 Subject: [PATCH] fix --- src/App.vue | 1 + src/components/content/md-preview.vue | 36 -------------------------- src/components/content/md-textarea.vue | 33 ++++++++++++----------- src/components/header/md-header.vue | 17 +++++++++--- 4 files changed, 33 insertions(+), 54 deletions(-) diff --git a/src/App.vue b/src/App.vue index 7c9766f..714fa78 100644 --- a/src/App.vue +++ b/src/App.vue @@ -48,6 +48,7 @@ :rows="rows" :html.sync="html" :id="textareaId" + :ref="'md_textarea' + id" @tab="$refs['md_header' + id].tab()" @submit="submit" v-else diff --git a/src/components/content/md-preview.vue b/src/components/content/md-preview.vue index 50631cb..eff6eee 100644 --- a/src/components/content/md-preview.vue +++ b/src/components/content/md-preview.vue @@ -31,9 +31,6 @@ export default { } }, mounted() { - setTimeout(() => { - // this.resetMinHeight(); - }, 0); this.addClass(); }, updated() { @@ -46,39 +43,6 @@ export default { if (!previewDom) return; previewDom.className = "md_hljs"; }, 0); - }, - transferMarkdown(val) { - marked.setOptions({ - highlight: function(code, lang, callback) { - const html = require("highlight.js").highlightAuto(code).value; - return html; - } - }); - const str = val + ""; - // if (!str.trim()) return; - const html = marked(str); - this.$emit("update:html", html); - }, - resetMinHeight() { - console.log("resetHeight"); - - const textEl = document.getElementById(this.id); - if (!textEl) return; - const height = textEl.offsetHeight; - console.log("编辑区高度", height); - - this.htmlMinHeight = height; - } - }, - watch: { - text: { - immediate: true, - handler: function(val) { - setTimeout(() => { - // this.resetMinHeight(); - }, 0); - // this.transferMarkdown(val); - } } } }; diff --git a/src/components/content/md-textarea.vue b/src/components/content/md-textarea.vue index 2c62744..9563070 100644 --- a/src/components/content/md-textarea.vue +++ b/src/components/content/md-textarea.vue @@ -88,11 +88,16 @@ export default { document.addEventListener("mouseup", this.checkSelection); }, mounted() { - setTimeout(() => { - this.resetMinHeight(); - }, 0); + this.resetPreviewMinHeight(); }, watch: { + isFocus: { + handler: function(val) { + if (val) { + this.resetPreviewMinHeight(); + } + } + }, text: { immediate: true, handler: function(val) { @@ -111,7 +116,7 @@ export default { document.body.style.overflow = "auto"; } setTimeout(() => { - this.reSizeHeight(); + this.reSizeTextareaHeight(); }, 0); } }, @@ -120,7 +125,7 @@ export default { handler: function() { setTimeout(() => { if (!this.autoSize) return; - this.reSizeHeight(); + this.reSizeTextareaHeight(); }, 0); } } @@ -139,15 +144,13 @@ export default { } }, methods: { - resetMinHeight() { - console.log("resetHeight"); - - const textEl = document.getElementById(this.id); - if (!textEl) return; - const height = textEl.offsetHeight; - console.log("编辑区高度", height); - this.$emit("update:htmlMinHeight", height); - // this.htmlMinHeight = height; + resetPreviewMinHeight() { + setTimeout(() => { + const textEl = document.getElementById(this.id); + if (!textEl) return; + const height = textEl.offsetHeight; + this.$emit("update:htmlMinHeight", height); + }, 0); }, transferMarkdown(val) { marked.setOptions({ @@ -165,7 +168,7 @@ export default { this.$emit("update:textLength", this.textContent.length); this.emitText(); }, - reSizeHeight() { + reSizeTextareaHeight() { const textEl = document.getElementById(this.id); if (!textEl) return; const fontSize = getComputedStyle(textEl).getPropertyValue("font-size"); diff --git a/src/components/header/md-header.vue b/src/components/header/md-header.vue index 0f4bc84..8b86890 100644 --- a/src/components/header/md-header.vue +++ b/src/components/header/md-header.vue @@ -205,10 +205,21 @@ export default { const startStr = " "; const endStr = ""; const originalText = this.text; - const selectionInfo = this.selectionInfo; + const cursorPoint = getPosition(this.id); + console.log(cursorPoint); + + const selectionInfo = { + selectionStart: cursorPoint, + selectionEnd: cursorPoint + }; const newText = formatText(originalText, selectionInfo, startStr, endStr); const len = newText.length - originalText.length; // const len = 0; + this.$emit("update:selectionInfo", { + selectorId: "", + selectionStart: cursorPoint.selectionStart + len, + selectionEnd: cursorPoint.selectionEnd + len + }); this.updateText(newText, len); }, setShowPreview(val) { @@ -222,8 +233,8 @@ export default { this.$emit("update:selectionInfo", { selectorId: "", - selectionStart: cursorPoint, - selectionEnd: cursorPoint + selectionStart: "", + selectionEnd: "" }); console.log(cursorPoint); -- GitLab