3 回答

TA貢獻1839條經驗 獲得超15個贊
不幸的是,接受的答案并沒有完全解決我的問題。
我正在尋找一種添加/保留多個類的可能方法。
這是最終的解決方案:
const Inline = Quill.import("blots/inline");
const ATTRIBUTES = ["href", "rel", "target", "class"];
class InternalLink extends Inline {
static create(value) {
let node = super.create(value);
node.setAttribute("href", value.href);
if (value.rel) node.setAttribute("rel", value.rel);
if (value.target) node.setAttribute("target", value.target);
if (value.class) node.setAttribute("class", value.class);
return node;
}
static formats(domNode) {
return ATTRIBUTES.reduce((formats, attribute) => {
if (domNode.hasAttribute(attribute)) {
formats[attribute] = domNode.getAttribute(attribute);
}
return formats;
}, {});
}
}
InternalLink.blotName = "internal_link";
InternalLink.tagName = "A";
Quill.register({
"formats/link": InternalLink
});

TA貢獻1780條經驗 獲得超4個贊
您還需要使用該類將該元素添加到 Quill 實例的一側Inline。
這是一個例子:
const Inline = Quill.import("blots/inline");
InternalLink.blotName = "internal_link";
InternalLink.className = "btn";
InternalLink.tagName = "A";
Quill.register({
"attributors/class/link": LinkClass,
"formats/internal_link": InternalLink
});

TA貢獻1804條經驗 獲得超8個贊
此版本將保留所有屬性(在我的初步測試中)并且不需要將它們顯式定義為印跡。
const Inline = Quill.import("blots/inline");
class FixedLink extends Inline {
? ? static create(value) {
? ? ? ? let node = super.create(value);
? ? ? ? for (const key in value){
? ? ? ? ? ? node.setAttribute(key, value[key]);
? ? ? ? }
? ? ? ? return node;
? ? }
? ? static formats(domNode) {
? ? ? ? const attributes = {};
? ? ? ? for (const attr of domNode.attributes){
? ? ? ? ? ? attributes[attr.name] = attr.value;
? ? ? ? }
? ? ? ? return attributes;
? ? }
}
FixedLink.blotName = "fixed_link";
FixedLink.tagName = "A";
添加回答
舉報