简化富文本编辑器的工具栏

master
yutent 2023-09-20 10:34:07 +08:00
parent 98d66014ad
commit d53e7f7d5b
2 changed files with 12 additions and 45 deletions

View File

@ -37,7 +37,7 @@ const ACTTION = {
unordered: 'insertUnorderedList' unordered: 'insertUnorderedList'
} }
const DEFAULT_TOOLS = [ const TOOLBAR = [
'font', 'font',
'color', 'color',
'bold', 'bold',
@ -52,7 +52,8 @@ const DEFAULT_TOOLS = [
'right', 'right',
'link', 'link',
'image', 'image',
'fullscreen' 'fullscreen',
'copy'
] ]
const COLORS = [ const COLORS = [
@ -87,18 +88,6 @@ class Editor extends Component {
static watches = ['value'] static watches = ['value']
static props = { static props = {
toolbar: {
type: String,
default: '',
attribute: false,
observer(v) {
if (v === null) {
this.#toolbar = [...DEFAULT_TOOLS]
} else if (v) {
this.#toolbar = v.split(',').map(it => it.trim())
}
}
},
readonly: { readonly: {
type: Boolean, type: Boolean,
observer(v) { observer(v) {
@ -407,7 +396,6 @@ class Editor extends Component {
` `
] ]
#toolbar = []
#value = '' #value = ''
#cache = { bar: 0, y: 0 } #cache = { bar: 0, y: 0 }
#gridx = 0 #gridx = 0
@ -794,15 +782,10 @@ class Editor extends Component {
} }
render() { render() {
toolbar = [
...(this.#toolbar.length ? this.#toolbar : DEFAULT_TOOLS),
'copy'
]
return html` return html`
<div class="editor"> <div class="editor">
<section class="toolbar" @click=${this.#toolbarClick}> <section class="toolbar" @click=${this.#toolbarClick}>
${toolbar.map( ${TOOLBAR.map(
it => html` it => html`
<span data-act=${it}> <span data-act=${it}>
<svg class="icon" viewBox="0 0 1024 1024"> <svg class="icon" viewBox="0 0 1024 1024">

View File

@ -98,18 +98,8 @@ class MEditor extends Component {
} }
}, },
value: 'str!', value: 'str!',
readonly: { readonly: false,
type: Boolean, disabled: false
observer(v) {
this.#updateStat()
}
},
disabled: {
type: Boolean,
observer(v) {
this.#updateStat()
}
}
} }
static styles = [ static styles = [
@ -472,18 +462,6 @@ class MEditor extends Component {
thumb.style.height = bar + 'px' thumb.style.height = bar + 'px'
} }
#updateStat() {
if (this.$refs.editor) {
if (this.readOnly || this.disabled) {
this.$refs.editor.removeAttribute('contenteditable')
} else {
this.$refs.editor.setAttribute('contenteditable', '')
}
} else {
nextTick(_ => this.#updateStat())
}
}
#fetchScroll(moveY) { #fetchScroll(moveY) {
let { bar } = this.#cache let { bar } = this.#cache
let { outer, thumb, inner } = this.$refs let { outer, thumb, inner } = this.$refs
@ -741,9 +719,15 @@ class MEditor extends Component {
<section class="toolbar"></section> <section class="toolbar"></section>
<div class="editor-outbox"> <div class="editor-outbox">
<textarea <textarea
ref="editor"
class="editor" class="editor"
spellcheck="false" spellcheck="false"
:value=${this.value} :value=${this.value}
:readOnly=${this.readOnly}
:disabled=${this.disabled}
@input=${function (ev) {
this.value = ev.target.value
}}
></textarea> ></textarea>
<wc-markd class="preview"></wc-markd> <wc-markd class="preview"></wc-markd>
</div> </div>