|
|
@@ -1,28 +1,36 @@
|
|
|
<template>
|
|
|
<section>
|
|
|
- <div
|
|
|
+ <component
|
|
|
+ ref="element"
|
|
|
+ :is="tagName"
|
|
|
contenteditable
|
|
|
@keydown.stop="handleKeyDown"
|
|
|
- :v-html="innerContent"
|
|
|
- ></div>
|
|
|
- 输入的内容为:{{ innerContent }}
|
|
|
+ @input="checkBlockType"
|
|
|
+ >
|
|
|
+ </component>
|
|
|
+ 输入的内容为:{{ content }}
|
|
|
</section>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
-import { CREATE_ITEM_AFTER } from '../store/types/editor-types'
|
|
|
+import {
|
|
|
+ CREATE_ITEM_AFTER,
|
|
|
+ SET_TAG_NAME,
|
|
|
+ CHANGE_CONTENT
|
|
|
+} from '../store/types/editor-types.js'
|
|
|
+import blockTypeRegs from '@/utils/block-type-regs.js'
|
|
|
export default {
|
|
|
name: 'editable-item',
|
|
|
- props: ['id', 'tagName', 'content'],
|
|
|
+ props: ['id', 'tagName', 'content', 'isFocus'],
|
|
|
data: function() {
|
|
|
- return {
|
|
|
- innerContent: this.content
|
|
|
- }
|
|
|
+ return {}
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.$refs.element.innerHTML = this.content
|
|
|
+ this.isFocus ? this.$refs.element.focus() : this.$refs.element.blur()
|
|
|
},
|
|
|
methods: {
|
|
|
handleKeyDown(e) {
|
|
|
- console.log(e.key)
|
|
|
-
|
|
|
switch (e.key) {
|
|
|
case 'Enter':
|
|
|
e.preventDefault()
|
|
|
@@ -31,10 +39,34 @@ export default {
|
|
|
|
|
|
default:
|
|
|
requestAnimationFrame(() => {
|
|
|
- this.innerContent = e.target.innerText
|
|
|
+ this.$store.commit(CHANGE_CONTENT, {
|
|
|
+ id: this.id,
|
|
|
+ content: e.target.innerText
|
|
|
+ })
|
|
|
})
|
|
|
break
|
|
|
}
|
|
|
+ },
|
|
|
+ checkBlockType(e) {
|
|
|
+ this.tagName === 'div' &&
|
|
|
+ Object.entries(blockTypeRegs)
|
|
|
+ .map(array => ({ tagName: array[0], reg: array[1] }))
|
|
|
+ .some(({ tagName, reg }) => {
|
|
|
+ if (reg.test(this.content)) {
|
|
|
+ this.$store.commit(SET_TAG_NAME, {
|
|
|
+ id: this.id,
|
|
|
+ tagName: tagName
|
|
|
+ })
|
|
|
+ this.$store.commit(CHANGE_CONTENT, {
|
|
|
+ id: this.id,
|
|
|
+ content: this.content.replace(reg, '')
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ changeTagName(newTagName) {
|
|
|
+ this.tagName !== newTagName &&
|
|
|
+ this.$store.commit(SET_TAG_NAME, { id: this.id, tagName: newTagName })
|
|
|
}
|
|
|
}
|
|
|
}
|