From 594d9956acb6bb2f6dade4b00dbe126cce111923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Wed, 29 Jul 2020 17:46:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=8C=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/neditor/index.wc | 87 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 2d658da..345101d 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@cc0/wcui", + "name": "@bd/wcui", "version": "2.0.0", "description": "基于wc开发的一套UI库, 面向未来, 面向electron", "main": "index.js", diff --git a/src/neditor/index.wc b/src/neditor/index.wc index c17069c..25d2915 100644 --- a/src/neditor/index.wc +++ b/src/neditor/index.wc @@ -121,6 +121,13 @@ wc-scroll { } } +:host([disabled]) { + .neditor { + cursor: not-allowed; + opacity: 0.6; + } +} + .font-layer, .color-layer, .link-layer { @@ -273,7 +280,9 @@ function renderToolbar(list) { export default class Neditor { props = { toolbar: null, - value: '' + value: '', + readonly: false, + disabled: false } __init__() { @@ -289,6 +298,57 @@ export default class Neditor { this.__LINK_BTN__ = this.__LINK__.querySelector('wc-button') } + get readOnly() { + return this.props.readonly + } + + set readOnly(val) { + var type = typeof val + + if (val === this.props.readonly) { + return + } + + if ((type === 'boolean' && val) || type !== 'boolean') { + this.props.readonly = true + this.setAttribute('readonly', '') + this.__EDITOR__.removeAttribute('contenteditable') + } else { + this.props.readonly = false + this.removeAttribute('readonly') + this.__EDITOR__.setAttribute('contenteditable', true) + } + } + + get disabled() { + return this.props.disabled + } + + set disabled(val) { + var type = typeof val + + if (val === this.props.disabled) { + return + } + + log( + this.__EDITOR__, + '------', + val, + (type === 'boolean' && val) || type !== 'boolean' + ) + + if ((type === 'boolean' && val) || type !== 'boolean') { + this.props.disabled = true + this.setAttribute('disabled', '') + this.__EDITOR__.removeAttribute('contenteditable') + } else { + this.props.disabled = false + this.removeAttribute('disabled') + this.__EDITOR__.setAttribute('contenteditable', true) + } + } + get value() { var html = this.__EDITOR__.innerHTML if (~html.indexOf('')) { @@ -379,17 +439,26 @@ export default class Neditor { /* ------------------------------ */ // 工具栏点击事件 - this._toolFn = $.bind(this.__TOOLBAR__, 'click', ev => { + this._toolFn = $.catch(this.__TOOLBAR__, 'click', ev => { + var target = ev.target + var act, val + this.restoreSelection() + if (ev.target === ev.currentTarget) { return } - let target = ev.target + + if (this.props.readonly || this.props.disabled) { + return ev.preventDefault() + } + while (target.tagName !== 'SPAN') { target = target.parentNode } - var act = target.dataset.act - var val = '' + + act = target.dataset.act + val = '' switch (act) { case 'font': @@ -504,6 +573,8 @@ export default class Neditor { '' ) .replace(/<(?!a|img)([\w\-]+)[^>]*>/g, '<$1>') + .replace(/]*?>[\w\W]*?<\/xml>/g, '') + .replace(/