const { Annotations, Tools, annotManager, docViewer } = instance;
// shared helper function that creates caret annotation at the end of the selection
const createCaretAnnotation = (quads, data) => {
const lastQuad = quads[quads.length - 1];
const quadHeight = Math.abs(lastQuad.y2 - lastQuad.y3);
const caretSize = quadHeight / 2;
const caret = new Annotations.CaretAnnotation();
caret.PageNumber = data.pageNumber;
// position center of caret at the edge of strikeout
caret.X = lastQuad.x3 - (caretSize / 2);
// center relative to the strikeout
caret.Y = lastQuad.y2 - caretSize;
caret.Width = caretSize;
caret.Height = caretSize;
caret.Author = annotManager.getCurrentUser();
caret.StrokeColor = new Annotations.Color(0, 0, 255);
annotManager.addAnnotation(caret);
if (data.annotation) {
annotManager.groupAnnotations(data.annotation, [caret]);
}
annotManager.redrawAnnotation(caret);
return caret;
};
// Replace Text Tool
const ReplaceTextTool = function() {
Tools.TextStrikeoutCreateTool.apply(this, arguments);
};
ReplaceTextTool.prototype = new Tools.TextStrikeoutCreateTool();
const replaceToolName = 'ReplaceTextTool';
const replaceTextTool = new ReplaceTextTool(docViewer);
replaceTextTool.on('annotationAdded', function(annotation) {
// adds the caret annotation when the strikeout is added with the custom tool
createCaretAnnotation(annotation.Quads, {
annotation,
pageNumber: annotation.PageNumber
});
instance.focusNote(annotation.Id);
});
instance.registerTool({
toolName: replaceToolName,
toolObject: replaceTextTool,
'<path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"/>' +
'<path d="M0 0h24v24H0z" fill="none"/>' +
'</svg>',
buttonName: 'replaceTextToolButton',
tooltip: 'Replace Text'
}, Annotations.CaretAnnotation);
instance.setHeaderItems(function(header) {
const replaceTextButton = {
type: 'toolButton',
toolName: replaceToolName
};
header.push(replaceTextButton);
});
// Insert Text Tool
const InsertTextTool = function() {
Tools.TextSelectTool.apply(this, arguments);
};
InsertTextTool.prototype = new Tools.TextSelectTool();
InsertTextTool.prototype.switchIn = function() {
Tools.TextSelectTool.prototype.switchIn.apply(this, arguments);
Tools.Tool.ENABLE_AUTO_SWITCH = false;
};
InsertTextTool.prototype.switchOut = function() {
Tools.TextSelectTool.prototype.switchOut.apply(this, arguments);
Tools.Tool.ENABLE_AUTO_SWITCH = true;
};
const insertToolName = 'InsertTextTool';
const insertTextTool = new InsertTextTool(docViewer);
insertTextTool.on('selectionComplete', (startLocation, allQuads) => {
const selectedPageNumbers = Object.keys(allQuads);
const pageNumber = selectedPageNumbers[selectedPageNumbers.length - 1];
const caret = createCaretAnnotation(allQuads[pageNumber], { pageNumber });
instance.focusNote(caret.Id);
});
instance.registerTool({
toolName: insertToolName,
toolObject: insertTextTool,
'<path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"/>' +
'<path d="M0 0h24v24H0z" fill="none"/>' +
'</svg>',
buttonName: 'insertTextToolButton',
tooltip: 'Insert Text'
}, Annotations.CaretAnnotation);
instance.setHeaderItems(function(header) {
const insertTextButton = {
type: 'toolButton',
toolName: insertToolName
};
header.push(insertTextButton);
});