如何在 Quill 编辑器中使用 http 预填充链接? [英] How can I prefill links with http in a Quill editor?
问题描述
使用 Quill 编辑器添加链接时,我必须包含协议,否则链接将被视为相对链接.
When adding links with the Quill editor I must include the protocol or the link is treated as a relative link.
当有人点击添加链接时,我希望用 http://
预填充该字段,这样当用户输入 google.com
时,它会创建一个链接到 http://google.com
而不是 http://myapp.net/something/google.com
.
When someone clicks to add a link I would like to have the field prepopulate with http://
so when a user types google.com
it will create a link to http://google.com
instead of http://myapp.net/something/google.com
.
堆栈溢出这样做...
推荐答案
当您尝试保存现有链接时,上述解决方案将不起作用.它还忽略其他协议,例如 (mailto, tel, https)
The above solution wont work when you try to save an existing link. Also it ignores other protocols such as ( mailto, tel, https )
这是一个更好的解决方案:
Here is a better solution:
let Link = window.Quill.import('formats/link');
class CustomLink extends Link {
static sanitize(url) {
let value = super.sanitize(url);
if(value)
{
for(let i=0;i<CustomLink.PROTOCOL_WHITELIST.length;i++)
if(value.startsWith(CustomLink.PROTOCOL_WHITELIST[i]))
return value;
return `http://${value}`
}
return value;
}
}
Quill.register(CustomLink);
这篇关于如何在 Quill 编辑器中使用 http 预填充链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!