dataease/frontend/src/components/canvas/custom-component/VText.vue

171 lines
3.9 KiB
Vue
Raw Normal View History

2021-03-25 19:16:32 +08:00
<template>
<div v-if="editMode == 'edit'" class="v-text" @keydown="handleKeydown" @keyup="handleKeyup">
<!-- tabindex >= 0 使得双击时聚集该元素 -->
<div
v-if="canEdit"
ref="text"
:contenteditable="canEdit"
:class="{ canEdit }"
:tabindex="element.id"
:style="{ verticalAlign: element.style.verticalAlign }"
@dblclick="setEdit"
@paste="clearStyle"
@mousedown="handleMousedown"
@blur="handleBlur"
@input="handleInput"
v-html="element.propValue"
/>
<div v-if="!canEdit" :style="{ verticalAlign: element.style.verticalAlign }" @dblclick="setEdit" v-html="element.propValue" />
</div>
<div v-else class="v-text">
2021-10-14 16:44:51 +08:00
<div :style="{ verticalAlign: element.style.verticalAlign }" v-html="textInfo" />
</div>
2021-03-25 19:16:32 +08:00
</template>
<script>
2021-03-30 15:38:32 +08:00
import { keycodes } from '@/components/canvas/utils/shortcutKey.js'
2021-03-25 19:16:32 +08:00
export default {
props: {
2021-06-24 14:31:01 +08:00
// eslint-disable-next-line vue/require-default-prop
propValue: {
type: String,
require: true
2021-03-25 19:16:32 +08:00
},
2021-06-24 14:31:01 +08:00
// eslint-disable-next-line vue/require-default-prop
element: {
type: Object
2021-03-25 19:16:32 +08:00
},
editMode: {
type: String,
require: false,
default: 'preview'
2021-03-25 19:16:32 +08:00
},
active: {
type: Boolean,
require: false,
default: false
}
},
data() {
return {
canEdit: false,
ctrlKey: 17,
isCtrlDown: false
}
},
computed: {
2021-10-14 16:44:51 +08:00
textInfo() {
if (this.element && this.element.hyperlinks && this.element.hyperlinks.enable) {
return "<a title='" + this.element.hyperlinks.content + "' target='" + this.element.hyperlinks.openMode + "' href='" + this.element.hyperlinks.content + "'>" + this.element.propValue + '</a>'
2021-10-14 16:44:51 +08:00
} else {
return this.element.propValue
}
}
},
watch: {
active: {
handler(newVal, oldVla) {
this.removeSelectText()
},
deep: true
}
},
methods: {
handleInput(e) {
this.$emit('input', this.element, e.target.innerHTML)
this.$store.commit('recordStyleChange')
},
handleKeydown(e) {
2021-06-24 14:31:01 +08:00
if (e.keyCode === this.ctrlKey) {
this.isCtrlDown = true
} else if (this.isCtrlDown && this.canEdit && keycodes.includes(e.keyCode)) {
e.stopPropagation()
2021-06-24 14:31:01 +08:00
} else if (e.keyCode === 46) { // deleteKey
e.stopPropagation()
}
},
handleKeyup(e) {
2021-06-24 14:31:01 +08:00
if (e.keyCode === this.ctrlKey) {
this.isCtrlDown = false
}
},
handleMousedown(e) {
if (this.canEdit) {
e.stopPropagation()
}
2021-03-25 19:16:32 +08:00
},
clearStyle(e) {
e.preventDefault()
const clp = e.clipboardData
const text = clp.getData('text/plain') || ''
if (text !== '') {
document.execCommand('insertText', false, text)
}
this.$emit('input', this.element, e.target.innerHTML)
},
handleBlur(e) {
this.element.propValue = e.target.innerHTML || '&nbsp;'
this.canEdit = false
},
setEdit() {
this.canEdit = true
// // // 聚焦到单元格
// setTimeout(() => {
// this.$refs['text'].focus()
// }, 500)
// 全选
this.selectText(this.$refs.text)
},
selectText(element) {
const selection = window.getSelection()
const range = document.createRange()
2021-10-14 16:44:51 +08:00
// range.selectNodeContents(element)
// selection.removeAllRanges()
// selection.addRange(range)
},
removeSelectText() {
const selection = window.getSelection()
selection.removeAllRanges()
}
}
2021-03-25 19:16:32 +08:00
}
</script>
<style lang="scss" scoped>
.v-text {
width: 100%;
height: 100%;
display: table;
div {
display: table-cell;
width: 100%;
height: 100%;
outline: none;
2021-03-25 19:16:32 +08:00
}
.canEdit {
cursor: text;
height: 100%;
}
}
::v-deep a:hover {
text-decoration: underline!important;
color: blue!important;
}
2021-03-30 15:38:32 +08:00
</style>