Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
동경
话题
提交
cc09bb0c
话
话题
项目概览
동경
/
话题
与 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看板
提交
cc09bb0c
编写于
12月 04, 2023
作者:
W
weixin_43814775
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Mon Dec 4 22:24:00 CST 2023 inscode
上级
7c434981
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
23 addition
and
34 deletion
+23
-34
src/components/HelloWorld.vue
src/components/HelloWorld.vue
+23
-34
未找到文件。
src/components/HelloWorld.vue
浏览文件 @
cc09bb0c
...
...
@@ -5,29 +5,11 @@ const formState = reactive<any>({
description
:
""
,
});
// 保存光标位置
let
savedSelection
:
Selection
|
null
=
null
;
// 获取当前光标位置的 Range 对象
const
getCurrentRange
=
()
=>
{
const
selection
=
window
.
getSelection
();
if
(
selection
&&
selection
.
rangeCount
>
0
)
{
return
selection
.
getRangeAt
(
0
);
}
return
null
;
};
// 恢复光标位置
const
restoreSelection
=
()
=>
{
if
(
savedSelection
)
{
const
selection
=
window
.
getSelection
();
selection
.
removeAllRanges
();
selection
.
addRange
(
savedSelection
);
}
};
// 触发描述框 input 事件
// 触发描述框 input事件
const
handleDescInput
=
()
=>
{
const
result
=
document
.
createElement
(
"
p
"
);
const
content
=
editableDiv
.
value
.
textContent
;
...
...
@@ -50,12 +32,12 @@ const handleDescInput = () => {
}
if
(
hashtag
.
length
>
1
)
{
// 创建
span
元素并设置样式和内容
// 创建
span
元素并设置样式和内容
var
span
=
document
.
createElement
(
"
span
"
);
span
.
className
=
"
hashtag
"
;
span
.
innerText
=
hashtag
;
console
.
log
(
"
hashtag
"
,
hashtag
);
// 将
span 元素添加到可编辑 div
中
// 将
span元素添加到可编辑div
中
result
.
appendChild
(
span
);
}
else
{
result
.
appendChild
(
document
.
createTextNode
(
char
));
...
...
@@ -67,15 +49,26 @@ const handleDescInput = () => {
}
}
}
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
);
// 保存当前光标位置
savedSelection
=
getCurrentRange
();
formState
.
description
=
result
.
innerHTML
;
// 恢复光标位置
restoreSelection
();
};
// 添加
paste
事件监听器,以便处理粘贴
// 添加
paste
事件监听器,以便处理粘贴
const
handleDescPaste
=
(
e
)
=>
{
// 阻止默认粘贴行为
e
.
preventDefault
();
...
...
@@ -85,12 +78,8 @@ const handleDescPaste = (e) => {
// 在粘贴的文本中插入换行符
pastedText
=
pastedText
.
replace
(
/
\s
+/g
,
"
\n
"
);
// 保存当前光标位置
savedSelection
=
getCurrentRange
();
// 将处理后的文本插入可编辑 div
// 将处理后的文本插入可编辑div
document
.
execCommand
(
"
insertText
"
,
false
,
pastedText
);
// 恢复光标位置
restoreSelection
();
};
let
isComposing
=
false
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录