From 172c9e822f76891dfbc55262871bcdb064d584ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Sun, 2 Feb 2020 22:55:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0markdown=E7=BB=84=E4=BB=B6;?= =?UTF-8?q?=E4=BC=98=E5=8C=96code=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/code/index.wc | 44 +++-- src/{marked/index.js => markd/core.js} | 44 +---- src/markd/index.wc | 253 +++++++++++++++++++++++++ src/scroll/index.wc | 2 +- 4 files changed, 287 insertions(+), 56 deletions(-) rename src/{marked/index.js => markd/core.js} (96%) create mode 100644 src/markd/index.wc diff --git a/src/code/index.wc b/src/code/index.wc index ffbf5f5..184349a 100644 --- a/src/code/index.wc +++ b/src/code/index.wc @@ -2,6 +2,7 @@
+ @@ -138,23 +139,42 @@ import $ from '../utils' export default class Code { props = { dark: '', - inline: '', - content: '' + lang: '' } __init__() { /* render */ + + // .code-box var elem = this.root.children[1] + var header = elem.children[0] + this.__CODE__ = elem.children[1] - this.__RUN__ = elem.children[0].children[1].firstElementChild - this.__CP__ = elem.children[0].children[1].lastElementChild + + this.__LANG__ = header.children[1] + this.__RUN__ = header.children[2].firstElementChild + this.__CP__ = header.children[2].lastElementChild } get value() { return this.props.content } + set value(txt) { + this.props.content = txt + txt = txt + .replace(//g, '>') + .split('\n') + txt = txt.map(s => `

${s}

`).join('') + this.__CODE__.innerHTML = txt + } + mounted() { + var txt = this.innerHTML || this.textContent + this.value = txt.replace(/^[\r\n]|\s{2,}$/g, '') + this.textContent = '' + this._cpFN = $.bind(this.__CP__, 'click', ev => { try { navigator.clipboard.writeText(this.value) @@ -180,18 +200,14 @@ export default class Code { watch() { switch (name) { - case 'content': - this.props.content = val - val = val - .replace(//g, '>') - .split('\n') - val = val.map(s => `

${s}

`).join('') - this.__CODE__.innerHTML = val - this.removeAttribute('content') + case 'lang': + this.props.lang = val.toLowerCase() + this.__LANG__.textContent = this.props.lang break - default: + case 'value': + this.value = val + this.removeAttribute('value') break } } diff --git a/src/marked/index.js b/src/markd/core.js similarity index 96% rename from src/marked/index.js rename to src/markd/core.js index b2fb2c5..c9e09cd 100644 --- a/src/marked/index.js +++ b/src/markd/core.js @@ -826,48 +826,12 @@ Renderer.prototype.code = function(code, lang, escaped) { code = code .replace(/<script([^&]*?)>/g, '') .replace(/<\/script>/g, '') - if (this.options.highlight) { - var out = this.options.highlight(code, lang) - if (out != null && out !== code) { - escaped = true - code = out - } - } //转义特殊字符 - code = escaped ? code : escape(code, true) - lang = this.options.langPrefix + escape(lang, true) + code = escape(code, true) + lang = escape(lang, true) - var codes = code.split('\n'), - ln = codes.length, - idx = 0, - output = '', - multiCom = false - - while (idx++ < ln) { - //处理多行注释 - if (!multiCom) { - if ( - /^\s*?$/.test(codes[idx - 1]) - ) { - multiCom = true - codes[idx - 1] += '' - } - } else { - if (!/<\/span>$/.test(codes[idx - 1])) { - codes[idx - 1] = - '' + codes[idx - 1] + '' - } else { - codes[idx - 1] = '' + codes[idx - 1] - multiCom = false - } - } - - output += '' + codes[idx - 1] + '\n' //加\n为了避免空行时无法显示 - } - - return '
' + output + '
' + return '' + code + '' } Renderer.prototype.blockquote = function(quote) { @@ -1399,6 +1363,4 @@ marked.safe = function(txt) { return marked(txt) } -window.marked = marked - export default marked diff --git a/src/markd/index.wc b/src/markd/index.wc new file mode 100644 index 0000000..626053a --- /dev/null +++ b/src/markd/index.wc @@ -0,0 +1,253 @@ + + + + + diff --git a/src/scroll/index.wc b/src/scroll/index.wc index 1422886..079e01c 100644 --- a/src/scroll/index.wc +++ b/src/scroll/index.wc @@ -219,13 +219,13 @@ export default class Scroll { // 鼠标滚动事件 this._wheelFn = $.catch(this.__BOX__, 'wheel', ev => { - ev.preventDefault() var { sh, oh, yh, sw, ow, xw } = this.props // x轴 y轴 都为0时, 不作任何处理 if (!xw && !yh) { return } + ev.preventDefault() //校正兼容苹果鼠标在 chrome和FF下滚动的精度 var deltaX