我正在尝试添加一个扩展来替换媒体编辑器中现有的链接功能,以允许我拥有一个文件列表和其他自定义位。问题是,一旦我显示模态并且您对其进行了一些更改,原始选择就会丢失。插入代码的示例,看起来与主脚本使用的内容相同,我对其进行了编辑,仅允许将选择对象作为第二个参数传入。
我所做的是单击按钮显示模态,设置一些输入,从基础中获取选择(与 window.getSelection() 相同)将其保存到扩展对象。然后单击模态中的保存按钮,我使用代码插入示例中的函数,传递选择。但是选择已经改变,所以我猜它是一个参考或每次都计算,所以我不确定如何使它工作。
这是代码,我删除了不相关的位:
function MediumLink() {
var self = this;
this.parent = true;
//...button init
this.modalSubmit.addEventListener('click', function () {
//the linkSel is no longer what I set it to here
console.log(self.linkSel);
self.insertHtmlAtCaret('<a href="' + self.modalHref.value + '">' + self.modalName.value + '</a>', self.linkSel);
//... hide and reset modal
}, false);
//https://github.com/jillix/medium-editor-custom-html
this.insertHtmlAtCaret = function (html, sel) {
if (sel === undefined) {
sel = window.getSelection();
}
//..rest of this function is unchanged from example
};
}
MediumLink.prototype.onClick = function () {
var sel = this.base.selection;
if (sel.type === 'Range') { //trying to keep sel from changing since an empty selection would have a different type
//... set inputs in modal based on the selection
// sel is what I want at this point, what should be passed
console.log(sel);
this.linkSel = sel;
}
};