|
|
@@ -8,8 +8,8 @@
|
|
|
@input.stop="handleInput"
|
|
|
@keydown.stop="handleKeyDown"
|
|
|
contenteditable
|
|
|
- v-html="item.content"
|
|
|
- :class="item.content ? '' : 'placeholder'"
|
|
|
+ v-html="contentForShow"
|
|
|
+ :class="contentForShow ? '' : 'placeholder'"
|
|
|
:placeholder="item.placeholder"
|
|
|
>
|
|
|
</component>
|
|
|
@@ -25,13 +25,19 @@ import {
|
|
|
SET_FOCUS_BEFORE,
|
|
|
SET_FOCUS
|
|
|
} from '../../store/types/editor-types'
|
|
|
+import defaultPlaceholder from '../../utils/default-placeholder'
|
|
|
+import defaultMarkdownFagment from '../../utils/default-markdown-fagment'
|
|
|
export default {
|
|
|
name: 'editable-item',
|
|
|
props: ['item', 'pageId'],
|
|
|
data: function() {
|
|
|
return {
|
|
|
isCompositionStarted: false,
|
|
|
- range: null
|
|
|
+ range: null,
|
|
|
+ contentForShow:
|
|
|
+ this.item.type === 'div'
|
|
|
+ ? this.item.content
|
|
|
+ : this.item.content.replace(blockTypeRegs[this.item.type], '')
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
@@ -59,7 +65,32 @@ export default {
|
|
|
pageId: this.pageId,
|
|
|
itemId: this.item.id
|
|
|
})
|
|
|
+ break
|
|
|
}
|
|
|
+
|
|
|
+ this.range = window.getSelection().getRangeAt(0)
|
|
|
+ const offset = this.range.startOffset
|
|
|
+
|
|
|
+ if (
|
|
|
+ offset === 0 &&
|
|
|
+ this.range.collapsed &&
|
|
|
+ this.item.type !== 'div'
|
|
|
+ ) {
|
|
|
+ this.$store.commit(MODIFY_ITEM, {
|
|
|
+ pageId: this.pageId,
|
|
|
+ itemId: this.item.id,
|
|
|
+ diff: {
|
|
|
+ type: 'div',
|
|
|
+ placeholder: defaultPlaceholder['div'],
|
|
|
+ content: this.contentForShow
|
|
|
+ }
|
|
|
+ })
|
|
|
+ this.$store.commit(SET_FOCUS, {
|
|
|
+ focusItemId: this.item.id,
|
|
|
+ focusItemOffset: offset
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
break
|
|
|
|
|
|
case 'ArrowUp':
|
|
|
@@ -92,13 +123,16 @@ export default {
|
|
|
!this.isCompositionStarted && this.handleInputValue(e)
|
|
|
},
|
|
|
handleInputValue(e) {
|
|
|
- const diff = { content: e.target.textContent }
|
|
|
+ const diff = {
|
|
|
+ content: defaultMarkdownFagment[this.item.type] + e.target.textContent
|
|
|
+ }
|
|
|
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]
|
|
|
}
|
|
|
})
|
|
|
|
|
|
@@ -134,14 +168,9 @@ export default {
|
|
|
background: #f9fafc;
|
|
|
}
|
|
|
|
|
|
- &:empty:focus:before {
|
|
|
+ &:empty:before {
|
|
|
content: attr(placeholder);
|
|
|
color: #bbb;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-h1:empty:before {
|
|
|
- content: attr(placeholder);
|
|
|
- color: #bbb;
|
|
|
-}
|
|
|
</style>
|