Typescript Quilljs:派生自定义属性时发生TypeError
我试图实现一个潜在的重叠属性,如以下问题所示:这个问题: 唯一的区别是我使用的是TypeScript。这是我的代码摘录:Typescript Quilljs:派生自定义属性时发生TypeError,typescript,quill,Typescript,Quill,我试图实现一个潜在的重叠属性,如以下问题所示:这个问题: 唯一的区别是我使用的是TypeScript。这是我的代码摘录: import Quill from "quill"; import Parchment from "parchment"; class Comment extends Parchment.Attributor.Attribute { constructor(attrName = 'comment', keyName = 'data-comment') {
import Quill from "quill";
import Parchment from "parchment";
class Comment extends Parchment.Attributor.Attribute {
constructor(attrName = 'comment', keyName = 'data-comment') {
super(attrName, keyName, { scope: Parchment.Scope.INLINE_ATTRIBUTE })
}
add(node: HTMLElement, value: any) : boolean {
if (this.canAdd(node, value)) {
let ids: string[] = []
if (node.hasAttribute(this.keyName)) {
ids = node.getAttribute(this.keyName)!.split(',')
}
if (ids.indexOf(value) < 0) {
ids.push(value)
}
node.setAttribute(this.keyName, ids.sort().join(','))
return true
}
else
{
return false
}
}
remove(node: HTMLElement) {
node.removeAttribute(this.keyName)
}
value(node: HTMLElement) : string {
return node.getAttribute(this.keyName) || "";
}
}
Quill.register({
'formats/comment': new Comment()
});
let customCommentHandler = () => {
// get the position of the cursor
let range = this.editor.getSelection();
if (range) {
this.editor.formatText(range.index, range.length, "comment", this.counter);
this.counter += 1;
}
};
通过人为地提供contstructor,我至少可以调用它。它可以毫无怨言地编译并工作
不用说,这不是一个非常令人满意的解决方案,因为我失去了TypeScript的好处,比如代码完成和正确的类型检查,我不得不添加恼人的样板代码作为解决方法
我仍然希望能有更好的解决方案。似乎是一个类似的问题,但不是在Typescript中,所以我可以尝试一下。无论OP何时作出响应,这里的答案可能与此问题的答案相同
let Parchment = Quill.import('parchment');
class Comment extends (Parchment.Attributor.Class as { new(attrName: any, keyName: any, options: any): any; } ){
constructor(attrName = 'comment', keyName = 'comment') {
super(attrName, keyName, { scope: Parchment.Scope.INLINE_ATTRIBUTE });
}
// rest of the class goes here
}