|
@@ -1,7 +1,7 @@
|
|
|
<template>
|
|
<template>
|
|
|
<component
|
|
<component
|
|
|
ref="element"
|
|
ref="element"
|
|
|
- :is="item.type"
|
|
|
|
|
|
|
+ :is="tagName"
|
|
|
:id="item.id"
|
|
:id="item.id"
|
|
|
@compositionstart.stop="handleCompositionStart"
|
|
@compositionstart.stop="handleCompositionStart"
|
|
|
@compositionend.stop="handleCompositionEnd"
|
|
@compositionend.stop="handleCompositionEnd"
|
|
@@ -29,29 +29,50 @@ import defaultPlaceholder from '../../utils/default-placeholder'
|
|
|
import defaultMarkdownFagment from '../../utils/default-markdown-fagment'
|
|
import defaultMarkdownFagment from '../../utils/default-markdown-fagment'
|
|
|
export default {
|
|
export default {
|
|
|
name: 'editable-item',
|
|
name: 'editable-item',
|
|
|
- props: ['item', 'pageId'],
|
|
|
|
|
|
|
+ props: ['item'],
|
|
|
data: function() {
|
|
data: function() {
|
|
|
return {
|
|
return {
|
|
|
isCompositionStarted: false,
|
|
isCompositionStarted: false,
|
|
|
range: null,
|
|
range: null,
|
|
|
- contentForShow:
|
|
|
|
|
- this.item.type === 'div'
|
|
|
|
|
- ? this.item.content
|
|
|
|
|
- : this.item.content.replace(blockTypeRegs[this.item.type], '')
|
|
|
|
|
|
|
+ tagName: 'div',
|
|
|
|
|
+ contentForShow: ''
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
|
|
+ mounted() {
|
|
|
|
|
+ this.init()
|
|
|
|
|
+ },
|
|
|
|
|
+ updated() {
|
|
|
|
|
+ this.init()
|
|
|
|
|
+ },
|
|
|
methods: {
|
|
methods: {
|
|
|
|
|
+ init() {
|
|
|
|
|
+ this.tagName = this.matchTagName()
|
|
|
|
|
+ this.contentForShow =
|
|
|
|
|
+ this.tagName === 'div'
|
|
|
|
|
+ ? this.item.content
|
|
|
|
|
+ : this.item.content.replace(blockTypeRegs[this.tagName], '')
|
|
|
|
|
+ },
|
|
|
|
|
+ matchTagName() {
|
|
|
|
|
+ let res = 'div'
|
|
|
|
|
+ Object.entries(blockTypeRegs)
|
|
|
|
|
+ .map(array => ({ tagName: array[0], reg: array[1] }))
|
|
|
|
|
+ .some(({ tagName, reg }) => {
|
|
|
|
|
+ if (reg.test(this.item.content)) {
|
|
|
|
|
+ res = tagName
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ return res
|
|
|
|
|
+ },
|
|
|
handleKeyDown(e) {
|
|
handleKeyDown(e) {
|
|
|
if (!this.isCompositionStarted) {
|
|
if (!this.isCompositionStarted) {
|
|
|
switch (e.key) {
|
|
switch (e.key) {
|
|
|
case 'Enter':
|
|
case 'Enter':
|
|
|
e.preventDefault()
|
|
e.preventDefault()
|
|
|
this.$store.commit(ADD_ITEM_AFTER, {
|
|
this.$store.commit(ADD_ITEM_AFTER, {
|
|
|
- pageId: this.pageId,
|
|
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ id: this.item.id
|
|
|
})
|
|
})
|
|
|
this.$store.commit(SET_FOCUS_AFTER, {
|
|
this.$store.commit(SET_FOCUS_AFTER, {
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ focusId: this.item.id
|
|
|
})
|
|
})
|
|
|
break
|
|
break
|
|
|
|
|
|
|
@@ -59,11 +80,10 @@ export default {
|
|
|
if (!this.item.content) {
|
|
if (!this.item.content) {
|
|
|
e.preventDefault()
|
|
e.preventDefault()
|
|
|
this.$store.commit(SET_FOCUS_BEFORE, {
|
|
this.$store.commit(SET_FOCUS_BEFORE, {
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ focusId: this.item.id
|
|
|
})
|
|
})
|
|
|
this.$store.commit(DELETE_ITEM, {
|
|
this.$store.commit(DELETE_ITEM, {
|
|
|
- pageId: this.pageId,
|
|
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ id: this.item.id
|
|
|
})
|
|
})
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
@@ -74,36 +94,32 @@ export default {
|
|
|
if (
|
|
if (
|
|
|
offset === 0 &&
|
|
offset === 0 &&
|
|
|
this.range.collapsed &&
|
|
this.range.collapsed &&
|
|
|
- this.item.type !== 'div'
|
|
|
|
|
|
|
+ this.tagName !== 'div'
|
|
|
) {
|
|
) {
|
|
|
this.$store.commit(MODIFY_ITEM, {
|
|
this.$store.commit(MODIFY_ITEM, {
|
|
|
- pageId: this.pageId,
|
|
|
|
|
- itemId: this.item.id,
|
|
|
|
|
|
|
+ id: this.item.id,
|
|
|
diff: {
|
|
diff: {
|
|
|
- type: 'div',
|
|
|
|
|
placeholder: defaultPlaceholder['div'],
|
|
placeholder: defaultPlaceholder['div'],
|
|
|
content: this.contentForShow
|
|
content: this.contentForShow
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
this.$store.commit(SET_FOCUS, {
|
|
this.$store.commit(SET_FOCUS, {
|
|
|
- focusItemId: this.item.id,
|
|
|
|
|
- focusItemOffset: offset
|
|
|
|
|
|
|
+ diff: { focusId: this.item.id, offset }
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
break
|
|
break
|
|
|
|
|
|
|
|
case 'ArrowUp':
|
|
case 'ArrowUp':
|
|
|
e.preventDefault()
|
|
e.preventDefault()
|
|
|
this.$store.commit(SET_FOCUS_BEFORE, {
|
|
this.$store.commit(SET_FOCUS_BEFORE, {
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ focusId: this.item.id
|
|
|
})
|
|
})
|
|
|
break
|
|
break
|
|
|
|
|
|
|
|
case 'ArrowDown':
|
|
case 'ArrowDown':
|
|
|
e.preventDefault()
|
|
e.preventDefault()
|
|
|
this.$store.commit(SET_FOCUS_AFTER, {
|
|
this.$store.commit(SET_FOCUS_AFTER, {
|
|
|
- itemId: this.item.id
|
|
|
|
|
|
|
+ focusId: this.item.id
|
|
|
})
|
|
})
|
|
|
break
|
|
break
|
|
|
|
|
|
|
@@ -124,31 +140,21 @@ export default {
|
|
|
},
|
|
},
|
|
|
handleInputValue(e) {
|
|
handleInputValue(e) {
|
|
|
const diff = {
|
|
const diff = {
|
|
|
- content: defaultMarkdownFagment[this.item.type] + e.target.textContent
|
|
|
|
|
|
|
+ content: defaultMarkdownFagment[this.tagName] + e.target.textContent
|
|
|
|
|
+ }
|
|
|
|
|
+ if (this.tagName === 'div' && this.matchTagName() !== this.tagName) {
|
|
|
|
|
+ diff.placeholder = defaultPlaceholder[this.matchTagName()]
|
|
|
}
|
|
}
|
|
|
- this.item.type === 'div' &&
|
|
|
|
|
- Object.entries(blockTypeRegs)
|
|
|
|
|
- .map(array => ({ tagName: array[0], reg: array[1] }))
|
|
|
|
|
- .some(({ tagName, reg }) => {
|
|
|
|
|
- if (reg.test(e.target.textContent)) {
|
|
|
|
|
- diff.type = tagName
|
|
|
|
|
- diff.placeholder = defaultPlaceholder[tagName]
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
|
|
|
|
|
this.$store.commit(MODIFY_ITEM, {
|
|
this.$store.commit(MODIFY_ITEM, {
|
|
|
- pageId: this.pageId,
|
|
|
|
|
- itemId: this.item.id,
|
|
|
|
|
|
|
+ id: this.item.id,
|
|
|
diff
|
|
diff
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
this.range = window.getSelection().getRangeAt(0)
|
|
this.range = window.getSelection().getRangeAt(0)
|
|
|
const offset = this.range.startOffset
|
|
const offset = this.range.startOffset
|
|
|
|
|
|
|
|
- this.$store.commit(SET_FOCUS, {
|
|
|
|
|
- focusItemId: this.item.id,
|
|
|
|
|
- focusItemOffset: offset
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ this.$store.commit(SET_FOCUS, { diff: { focusId: this.item.id, offset } })
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|