我在我的项目中同时使用 Wagtail Review 和 Annotator.js。我想在注释中添加一个删除按钮(红色圆圈内的“X”),当单击时,它应该删除相应的注释。我已在注释控件内添加了删除按钮,但我很难正确触发删除。
annotator-outer annotator-viewer
)。.annotator-controls
范围内。Annotator.js
删除函数,或者我是否在Wagtail Review中正确处理交互。.annotator-outer.annotator-viewer
) 悬停时,我在 .annotator-controls
内动态添加“X”删除按钮。document.addEventListener('DOMContentLoaded', (event) => {
function deleteAnnotation() {
console.log('Delete function triggered!');
// Assuming annotator.js method is accessible to delete the annotation
if (window.annotator && window.annotator.deleteSelected) {
window.annotator.deleteSelected();
console.log('Annotation deleted');
} else {
console.log('annotator.js method not found.');
}
}
function injectTextAndAddEvent(span) {
span.textContent = '×'; // Add delete button content
span.addEventListener('click', deleteAnnotation); // Attach click event
}
document.querySelectorAll('.annotator-outer.annotator-viewer').forEach(parentDiv => {
parentDiv.addEventListener('mouseenter', function() {
const span = parentDiv.querySelector('.annotator-controls');
if (span) {
injectTextAndAddEvent(span);
}
});
});
});
Annotator.js
方法,或者是否有更好的方法与 Wagtail Review 框架内的注释进行交互。我希望熟悉 Annotator.js 和 Wagtail Review 的人提供任何见解。
修改
deleteAnnotation
以获取注释对象并使用removeAnnotation
将其删除。
document.addEventListener('DOMContentLoaded', () => {
function deleteAnnotation(annotation) {
window.annotator?.removeAnnotation(annotation);
}
function injectDeleteButton(span, annotation) {
span.textContent = '×';
span.onclick = () => deleteAnnotation(annotation);
}
document.querySelectorAll('.annotator-outer.annotator-viewer').forEach(parent => {
parent.onmouseenter = () => {
const span = parent.querySelector('.annotator-controls');
const annotation = /* fetch the annotation */;
span && injectDeleteButton(span, annotation);
};
});
});