Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas
/
scrm-micro-activity-center
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 09c47d8c
authored
Jun 27, 2023
by
杨明
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fixed
1 parent
33814a88
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
10 deletions
src/components/verbal/WTextarea.vue
src/components/verbal/WTextarea.vue
View file @
09c47d8
<
template
>
<div
class=
"w-textarea"
ref=
"wTextarea"
>
<div
contenteditable=
"true"
class=
"w-textarea_input"
ref=
"wTextareaContent"
:id=
"contentId"
@
click=
"inputClick($event)"
@
focus=
"onFocus"
@
blur=
"onBlur"
@
keydown
.
delete=
"handleDelete($even
t)"
@
input=
"handleInput($event.target)"
></div>
<div
contenteditable=
"true"
class=
"w-textarea_input"
ref=
"wTextareaContent"
:id=
"contentId"
@
keydown=
"textareaKeydown($event)"
@
click=
"inputClick($event)"
@
focus=
"onFocus"
@
blur=
"onBlur"
@
keydown
.
delete=
"handleDelete($event)"
@
compositionend=
"handleInput($event.targe
t)"
@
input=
"handleInput($event.target)"
></div>
</div>
</
template
>
<
script
>
...
...
@@ -64,7 +64,8 @@ export default {
// 创建模版标签的style
this
.
createStyle
()
// 每次光标变化的时候,保存 range
document
.
addEventListener
(
'selectionchange'
,
this
.
selectHandler
)
document
.
querySelector
(
'.w-textarea_input'
).
addEventListener
(
'keyup'
,
this
.
selectHandler
)
document
.
querySelector
(
'.w-textarea_input'
).
addEventListener
(
'click'
,
this
.
selectHandler
)
},
beforeDestroy
()
{
// 卸载事件
...
...
@@ -95,10 +96,7 @@ export default {
},
textareaKeydown
(
event
)
{
if
(
event
.
keyCode
==
13
)
{
this
.
insertHtml
(
'<br>'
)
let
nn
=
document
.
createElement
(
'br'
)
document
.
querySelector
(
'.w-textarea_input'
).
appendChild
(
nn
)
return
false
this
.
insertContent
(
'<br>'
)
}
},
/*换行*/
...
...
@@ -146,7 +144,7 @@ export default {
node
.
innerText
=
text
// 添加id便于删除
node
.
id
=
this
.
getGuid
()
this
.
insert
Node
(
node
)
this
.
insert
Content
(
node
)
},
addNode
()
{
let
target
=
this
.
$refs
.
wTextareaContent
...
...
@@ -165,7 +163,7 @@ export default {
let
node
=
document
.
createElement
(
'img'
)
node
.
src
=
url
// node.setAttribute('ondragstart', 'return false;')
this
.
insert
Node
(
node
)
this
.
insert
Content
(
node
)
},
onFocus
()
{
this
.
isLocked
=
true
...
...
@@ -174,6 +172,44 @@ export default {
onBlur
()
{
this
.
isLocked
=
false
},
insertContent
(
el
)
{
let
sel
=
null
if
(
document
.
selection
)
{
sel
=
document
.
selection
sel
.
createRange
().
pasteHTML
(
el
)
}
else
{
sel
=
window
.
getSelection
()
// 同上考虑到标签插入时光标会在之前插入过的标签内或者不在DIV上,这里作一下判断
// 光标在div的text上时后层组件是div,在插入标签时后两层是div,这样就不会插入到div外面或者出现标签内插入标签。
console
.
log
(
sel
)
if
(
sel
.
rangeCount
>
0
)
{
console
.
log
(
888
)
const
frag
=
document
.
createDocumentFragment
()
const
node
=
el
const
lastNode
=
frag
.
appendChild
(
node
)
this
.
savedRange
.
insertNode
(
frag
)
const
contentRange
=
this
.
savedRange
.
cloneRange
()
contentRange
.
setStartAfter
(
lastNode
)
contentRange
.
collapse
(
true
)
sel
.
removeAllRanges
()
sel
.
addRange
(
contentRange
)
}
}
},
insertNode
(
node
)
{
// 在内容中插入标签
// 删掉选中的内容(如有)
...
...
@@ -276,6 +312,7 @@ export default {
if
(
range
&&
range
.
commonAncestorContainer
.
ownerDocument
.
activeElement
.
id
===
this
.
contentId
)
{
this
.
savedRange
=
range
}
console
.
log
(
this
.
savedRange
)
}
},
watch
:
{
...
...
@@ -291,7 +328,7 @@ export default {
<
style
lang=
"scss"
>
.w-textarea
{
wise
{
user-select
:
none
;
//
user-select
:
none
;
border-radius
:
2px
;
background-color
:
transparent
;
font-size
:
14px
;
...
...
@@ -302,7 +339,7 @@ export default {
color
:
#26a2ff
;
//
padding
:
0
1px
;
white-space
:
nowrap
;
user-select
:
none
;
user-select
:
auto
!important
;
cursor
:
default
;
-webkit-user-modify
:
read-only
!important
;
}
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment