我正在使用 Quill 1.0.0-rc.1
。如何制作几种不同类型的链接格式?我制作了三种不同类型的添加data-link-type
属性的链接格式。当我创建链接时,这很好。但是,当我使用 将内容重新加载到编辑器中pasteHTML
时,最后注册的格式获胜,因为tagName
s 都是“A”,因此所有data-link-type
s 都设置为resource
。
所以这:
<p>These are the links:
<a href="http://www.google.com" data-link-type="external">External</a>
<a href="/resources/3" data-link-type="resource">Resource</a>
<a href="/pages/12" data-link-type="internal">Internal</a>
</p>
变成这样:
<p>These are the links:
<a href="http://www.google.com" data-link-type="resource">External</a>
<a href="/resources/3" data-link-type="resource">Resource</a>
<a href="/pages/12" data-link-type="resource">Internal</a>
</p>
我已经包括了一支代码笔:
http://codepen.io/anon/pen/akxNNK
var Inline = Quill.import('blots/inline');
class ExternalLink extends Inline {
static create(value) {
let node = super.create(value);
value = value.trim();
if (!value.match(/^http|mailto/)) {
value = 'http://' + value
}
node.setAttribute('href', value);
node.setAttribute('data-link-type', 'external');
return node;
}
static formats(domNode) {
return domNode.getAttribute('href');
}
}
ExternalLink.blotName = 'external_link';
ExternalLink.tagName = 'A';
class InternalLink extends Inline {
static create(value) {
let node = super.create(value);
if (!value.match(/^\/pages\//)) {
value = '/pages/' + value
}
node.setAttribute('href', value);
node.setAttribute('data-link-type', 'internal');
return node;
}
static formats(domNode) {
return domNode.getAttribute('href');
}
}
InternalLink.blotName = 'internal_link';
InternalLink.tagName = 'A';
class ResourceLink extends Inline {
static create(value) {
let node = super.create(value);
if (!value.match(/^\/resources\//)) {
value = '/resources/' + value
}
node.setAttribute('href', value);
node.setAttribute('data-link-type', 'resource');
return node;
}
static formats(domNode) {
return domNode.getAttribute('href');
}
}
ResourceLink.blotName = 'resource_link';
ResourceLink.tagName = 'A';
Quill.register({
'formats/internal_link': InternalLink,
'formats/external_link': ExternalLink,
'formats/resource_link': ResourceLink
});
var quill = new Quill(
'#editor-container',
{ theme: 'snow' }
);
quill.pasteHTML('<p>These are the links: <a href="http://www.google.com" data-link-type="external">External</a> <a href="/resources/3" data-link-type="resource">Resource</a> <a href="/pages/12" data-link-type="internal">Internal</a></p>');