Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
동경
话题
提交
ba5d0336
话
话题
项目概览
동경
/
话题
与 Fork 源项目一致
Fork自
inscode / VueJS
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
话
话题
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
ba5d0336
编写于
12月 05, 2023
作者:
W
weixin_43814775
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tue Dec 5 09:26:00 CST 2023 inscode
上级
cc09bb0c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
37 addition
and
30 deletion
+37
-30
src/components/HelloWorld.vue
src/components/HelloWorld.vue
+37
-30
未找到文件。
src/components/HelloWorld.vue
浏览文件 @
ba5d0336
<
script
setup
lang=
"ts"
>
import
{
ref
,
reactive
,
watch
,
watchEffect
,
onMounted
}
from
"
vue
"
;
const
editableDiv
=
ref
<
any
>
(
null
);
const
formState
=
reactive
<
any
>
({
description
:
""
,
});
// 获取可编辑div的innerText内容
const
getContent
=
()
=>
{
return
editableDiv
.
value
.
innerText
;
};
// 设置可编辑div的innerText内容
const
setContent
=
(
content
:
string
)
=>
{
if
(
!
editableDiv
.
value
)
return
;
const
selection
=
window
.
getSelection
();
const
range
=
document
.
createRange
();
range
.
selectNodeContents
(
editableDiv
.
value
);
range
.
collapse
(
false
);
// 光标定位到末尾
selection
?.
removeAllRanges
();
selection
?.
addRange
(
range
);
document
.
execCommand
(
"
insertText
"
,
false
,
content
);
};
// 触发描述框 input事件
const
handleDescInput
=
()
=>
{
const
result
=
document
.
createElement
(
"
p
"
);
const
content
=
editableDiv
.
value
.
textContent
;
const
content
=
getContent
();
if
(
content
)
{
const
result
=
document
.
createElement
(
"
p
"
);
if
(
!
isComposing
)
{
for
(
let
i
=
0
;
i
<
content
.
length
;
i
++
)
{
const
char
=
content
[
i
];
...
...
@@ -48,38 +66,28 @@ const handleDescInput = () => {
result
.
appendChild
(
document
.
createTextNode
(
char
));
}
}
}
editableDiv
.
value
.
focus
();
var
range
=
document
.
createRange
();
range
.
selectNodeContents
(
editableDiv
.
value
);
range
.
collapse
(
false
);
var
sel
=
window
.
getSelection
();
console
.
log
(
'
111
'
,
sel
.
anchorOffset
)
//判断光标位置,如不需要可删除
if
(
sel
.
anchorOffset
!=
0
){
console
.
log
(
'
会来到这里
'
)
formState
.
description
=
result
.
innerHTML
;
return
;
};
sel
.
removeAllRanges
();
sel
.
addRange
(
range
);
formState
.
description
=
result
.
innerHTML
;
formState
.
description
=
result
.
innerHTML
;
}
else
{
formState
.
description
=
""
;
}
};
// 添加paste事件监听器,以便处理粘贴
const
handleDescPaste
=
(
e
)
=>
{
const
handleDescPaste
=
(
e
:
Event
)
=>
{
// 阻止默认粘贴行为
e
.
preventDefault
();
// 获取粘贴的纯文本内容
var
pastedText
=
(
e
.
originalEvent
||
e
).
clipboardData
.
getData
(
"
text/plain
"
);
const
pastedText
=
(
e
as
ClipboardEvent
).
clipboardData
?
.
getData
(
"
text/plain
"
);
// 在粘贴的文本中插入换行符
pastedText
=
pastedText
.
replace
(
/
\s
+/g
,
"
\n
"
);
if
(
pastedText
)
{
// 在粘贴的文本中插入换行符
const
processedText
=
pastedText
.
replace
(
/
\s
+/g
,
"
\n
"
);
// 将处理后的文本插入可编辑div
document
.
execCommand
(
"
insertText
"
,
false
,
pastedText
);
// 将处理后的文本插入可编辑div
setContent
(
processedText
);
}
};
let
isComposing
=
false
;
...
...
@@ -106,7 +114,6 @@ const handleCompositionEnd = () => {
@
paste=
"handleDescPaste"
@
compositionend=
"handleCompositionEnd"
@
compositionstart=
"handleCompositionStart"
v-html=
"formState.description"
></div>
</
template
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录