$('#flask-pagedown-pagedown').on('paste', function(event) {
const textarea = $(this)[0];
var editor = $('#flask-pagedown-pagedown');
// 阻止默认行为,防止粘贴内容直接插入输入框
event.preventDefault();
// 获取剪贴板数据
var clipboardData = event.originalEvent.clipboardData || window.clipboardData;
var pastedData = clipboardData.getData('Text');
const selectionStart = textarea.selectionStart;
const selectionEnd = textarea.selectionEnd;
var oldText = editor.val();
var newText = oldText;
if(isURL(pastedData)) {
var url_content = pastedData;
$.ajax({
url: '/get_url_title',
method: 'GET',
data: {
url: url_content // 你想要获取标题的网页
},
success: function (data) {
var markdown_url = "["+data['title'] + "](" + url_content + ")";
newText = oldText.substring(0, selectionStart) + markdown_url + oldText.substring(selectionStart);
editor.val(newText);
},
error: function () {
alert('Failed to get the title');
var markdown_url = "[title](" + url_content + ")";
newText = oldText.substring(0, selectionStart) + markdown_url + oldText.substring(selectionStart);
editor.val(newText);
}
});
} else {
newText = oldText.substring(0, selectionStart) + pastedData + oldText.substring(selectionStart);
editor.val(newText);
}
editor.trigger('keyup');
// 如果需要,可以将数据设置到其他元素
// $('#output').text(pastedData);
});
jQuery 编辑器在光标处插入内容,自动解析网址生成markdown格式
jQuery相关文章
最近热门
最常浏览
- 016 推荐系统 | 排序学习(LTR - Learning To Rank)
- 偏微分符号
- i.i.d(又称IID)
- 利普希茨连续条件(Lipschitz continuity)
- (error) MOVED 原因和解决方案
- TextCNN详解
- 找不到com.google.protobuf.GeneratedMessageV3的类文件
- Deployment failed: repository element was not specified in the POM inside distributionManagement
- cannot access com.google.protobuf.GeneratedMessageV3 解决方案
- CLUSTERDOWN Hash slot not served 问题原因和解决办法
×