How to prevent PDFTron WebViewer from not setting default styles

121 views
Skip to first unread message

prakhar jaiswal

unread,
Apr 3, 2017, 11:23:03 AM4/3/17
to PDFTron WebViewer
Hi,

Currently the natural behavior in Web Viewer when styling an annotation is: whatever color (stroke-color in our case) is being selected, it becomes the default color for the tool.

Digging into the source, we discovered in AnnotationEdit.js:

function colorSelectedHandler($element, $colorPicker) {
        var annotationProperty = selectedColorPicker;

        var annotation = window.ControlUtils.getSelectedAnnotation();
        if (!annotation) {
            return;
        }

        var color = colorNameToRGB($element.attr('data-color'));
        if (isRemovingColors) {
            var colorName = colorToHex(color);
            if (colorName !== 'transparent') {
                userPrefs.removeToolColor(annotation, annotationProperty, colorName);
                currentToolColors = userPrefs.getToolColors(annotation);
                $element.remove();
            }

        } else {
            deselectColor($colorPicker);
            selectColor($element);
            if (color) {
                annotation[annotationProperty] = color;
                am.updateAnnotation(annotation);
                am.trigger('annotationChanged', [[annotation], 'modify']);
                readerControl.fireEvent('defaultToolValueChanged', [annotation, annotationProperty, color]);
                window.ControlUtils.updateAnnotPreview(annotation);
            }
        }
    }

And the listener in ControlUtils.js listens to the 'defaultToolValueChanged' event:

$(document).on('defaultToolValueChanged', function(e, annotation, property, value) {
        userPreferences.defaultToolValueChanged(annotation, property, value);
    });


Is there a legitimate way to prevent this behavior?

Reason being: we have a color palette tool implemented and the logic of setting default user preference is over-riding the concept of color palette.

The current hack we know of is to listen to 'defaultToolValueChanged' event as well and re-override the tool default attributes with what user has selected as default using our custom palette tool.
Would love to know if there is a better way than this.

Thanks and Best
Prakhar

Justin Jung

unread,
Apr 3, 2017, 6:26:01 PM4/3/17
to PDFTron WebViewer on behalf of prakhar jaiswal
Hello,

You can stop listening to the event by adding the following to config.js
$(document).off('defaultToolValueChanged');
It should be fine as it's the only place that we are listening to the event.

Justin Jung
Software Developer
PDFTron Systems Inc.

pankaj manali

unread,
Apr 5, 2017, 11:48:44 AM4/5/17
to PDFTron WebViewer
Hi Justin Jung,

We stopped listening to an event like the way you said in above comments.But the problem is 'defaultToolValueChanged' also get changed by other values like 'text color' and 'fill color' which we want to persist as per selected from style menu. We don't want 'strokeColor'  to be changed for consecutive annotation of same type when color is changed from style menu. 

Thanks
Pankaj

Justin Jung

unread,
Apr 5, 2017, 7:43:16 PM4/5/17
to PDFTron WebViewer
Hello Pankaj,

To change behaviour of individual properties, you would have to modify ControlUtils.js. For example, if you don't want 'defaultToolValueChanged' to be triggered for 'strokeColor', you can update defaultToolValueChanged handler as follows:

$(document).on('defaultToolValueChanged', function(e, annotation, property, value) {
    if (property === 'StrokeColor') {
        return;
    }
    userPreferences.defaultToolValueChanged(annotation, property, value);
});
Reply all
Reply to author
Forward
0 new messages