Refactors FindController dependencies.

This commit is contained in:
Yury Delendik 2016-04-13 14:39:29 -05:00
parent 3d49879211
commit 81fc46e666
5 changed files with 60 additions and 43 deletions

View File

@ -30,6 +30,7 @@ PDFJS.workerSrc = '../../build/pdf.worker.js';
// PDFJS.cMapPacked = true; // PDFJS.cMapPacked = true;
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf'; var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
var SEARCH_FOR = ''; // try 'Mozilla';
var container = document.getElementById('viewerContainer'); var container = document.getElementById('viewerContainer');
@ -42,9 +43,19 @@ var pdfViewer = new PDFJS.PDFViewer({
}); });
pdfLinkService.setViewer(pdfViewer); pdfLinkService.setViewer(pdfViewer);
// (Optionally) enable find controller.
var pdfFindController = new PDFJS.PDFFindController({
pdfViewer: pdfViewer
});
pdfViewer.setFindController(pdfFindController);
container.addEventListener('pagesinit', function () { container.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale. // We can use pdfViewer now, e.g. let's change default scale.
pdfViewer.currentScaleValue = 'page-width'; pdfViewer.currentScaleValue = 'page-width';
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', {query: SEARCH_FOR});
}
}); });
// Loading document. // Loading document.

View File

@ -179,6 +179,7 @@ target.components = function() {
var bundleFiles = [ var bundleFiles = [
'pdf_viewer.js', 'pdf_viewer.js',
'pdf_history.js', 'pdf_history.js',
'pdf_find_controller.js',
'download_manager.js' 'download_manager.js'
]; ];
bundle('pdf_viewer.component.js', ROOT_DIR + TMP_PDF_VIEWER, bundleFiles, bundle('pdf_viewer.component.js', ROOT_DIR + TMP_PDF_VIEWER, bundleFiles,

View File

@ -204,9 +204,24 @@ var PDFViewerApplication = {
pdfLinkService.setHistory(this.pdfHistory); pdfLinkService.setHistory(this.pdfHistory);
this.findController = new PDFFindController({ this.findController = new PDFFindController({
pdfViewer: this.pdfViewer, pdfViewer: this.pdfViewer
integratedFind: this.supportsIntegratedFind
}); });
this.findController.onUpdateResultsCount = function (matchCount) {
if (this.supportsIntegratedFind) {
return;
}
this.findBar.updateResultsCount(matchCount);
}.bind(this);
this.findController.onUpdateState = function (state, previous, matchCount) {
if (this.supportsIntegratedFind) {
FirefoxCom.request('updateFindControlState',
{result: state, findPrevious: previous});
} else {
this.findBar.updateUIState(state, previous, matchCount);
}
}.bind(this);
this.findController.listenWindowEvents();
this.pdfViewer.setFindController(this.findController); this.pdfViewer.setFindController(this.findController);
// FIXME better PDFFindBar constructor parameters // FIXME better PDFFindBar constructor parameters
@ -214,8 +229,6 @@ var PDFViewerApplication = {
findBarConfig.findController = this.findController; findBarConfig.findController = this.findController;
this.findBar = new PDFFindBar(findBarConfig); this.findBar = new PDFFindBar(findBarConfig);
this.findController.setFindBar(this.findBar);
this.overlayManager = OverlayManager; this.overlayManager = OverlayManager;
this.handTool = new HandTool({ this.handTool = new HandTool({

View File

@ -17,18 +17,16 @@
(function (root, factory) { (function (root, factory) {
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
define('pdfjs-web/pdf_find_controller', ['exports', define('pdfjs-web/pdf_find_controller', ['exports', 'pdfjs-web/ui_utils'],
'pdfjs-web/ui_utils', 'pdfjs-web/firefoxcom'], factory); factory);
} else if (typeof exports !== 'undefined') { } else if (typeof exports !== 'undefined') {
factory(exports, require('./ui_utils.js'), require('./firefoxcom.js')); factory(exports, require('./ui_utils.js'));
} else { } else {
factory((root.pdfjsWebPDFFindController = {}), root.pdfjsWebUIUtils, factory((root.pdfjsWebPDFFindController = {}), root.pdfjsWebUIUtils);
root.pdfjsWebFirefoxCom);
} }
}(this, function (exports, uiUtils, firefoxCom) { }(this, function (exports, uiUtils, firefoxCom) {
var scrollIntoView = uiUtils.scrollIntoView; var scrollIntoView = uiUtils.scrollIntoView;
var FirefoxCom = firefoxCom.FirefoxCom;
var FindStates = { var FindStates = {
FIND_FOUND: 0, FIND_FOUND: 0,
@ -61,31 +59,32 @@ var CHARACTERS_TO_NORMALIZE = {
var PDFFindController = (function PDFFindControllerClosure() { var PDFFindController = (function PDFFindControllerClosure() {
function PDFFindController(options) { function PDFFindController(options) {
this.pdfViewer = options.pdfViewer || null; this.pdfViewer = options.pdfViewer || null;
this.integratedFind = options.integratedFind || false;
this.findBar = options.findBar || null; this.onUpdateResultsCount = null;
this.onUpdateState = null;
this.reset(); this.reset();
// Compile the regular expression for text normalization once. // Compile the regular expression for text normalization once.
var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join(''); var replace = Object.keys(CHARACTERS_TO_NORMALIZE).join('');
this.normalizationRegex = new RegExp('[' + replace + ']', 'g'); this.normalizationRegex = new RegExp('[' + replace + ']', 'g');
var events = [
'find',
'findagain',
'findhighlightallchange',
'findcasesensitivitychange'
];
this.handleEvent = this.handleEvent.bind(this);
for (var i = 0, len = events.length; i < len; i++) {
window.addEventListener(events[i], this.handleEvent);
}
} }
PDFFindController.prototype = { PDFFindController.prototype = {
setFindBar: function PDFFindController_setFindBar(findBar) { listenWindowEvents: function PDFFindController_listenWindowEvents() {
this.findBar = findBar; var events = [
'find',
'findagain',
'findhighlightallchange',
'findcasesensitivitychange'
];
var handleEvent = function (e) {
this.executeCommand(e.type, e.detail);
}.bind(this);
for (var i = 0, len = events.length; i < len; i++) {
window.addEventListener(events[i], handleEvent);
}
}, },
reset: function PDFFindController_reset() { reset: function PDFFindController_reset() {
@ -199,18 +198,18 @@ var PDFFindController = (function PDFFindControllerClosure() {
extractPageText(0); extractPageText(0);
}, },
handleEvent: function PDFFindController_handleEvent(e) { executeCommand: function PDFFindController_executeCommand(cmd, state) {
if (this.state === null || e.type !== 'findagain') { if (this.state === null || cmd !== 'findagain') {
this.dirtyMatch = true; this.dirtyMatch = true;
} }
this.state = e.detail; this.state = state;
this.updateUIState(FindStates.FIND_PENDING); this.updateUIState(FindStates.FIND_PENDING);
this.firstPagePromise.then(function() { this.firstPagePromise.then(function() {
this.extractText(); this.extractText();
clearTimeout(this.findTimeout); clearTimeout(this.findTimeout);
if (e.type === 'find') { if (cmd === 'find') {
// Only trigger the find action after 250ms of silence. // Only trigger the find action after 250ms of silence.
this.findTimeout = setTimeout(this.nextMatch.bind(this), 250); this.findTimeout = setTimeout(this.nextMatch.bind(this), 250);
} else { } else {
@ -408,24 +407,15 @@ var PDFFindController = (function PDFFindControllerClosure() {
updateUIResultsCount: updateUIResultsCount:
function PDFFindController_updateUIResultsCount() { function PDFFindController_updateUIResultsCount() {
if (this.findBar === null) { if (this.onUpdateResultsCount) {
throw new Error('PDFFindController is not initialized with a ' + this.onUpdateResultsCount(this.matchCount);
'PDFFindBar instance.');
} }
this.findBar.updateResultsCount(this.matchCount);
}, },
updateUIState: function PDFFindController_updateUIState(state, previous) { updateUIState: function PDFFindController_updateUIState(state, previous) {
if (this.integratedFind) { if (this.onUpdateState) {
FirefoxCom.request('updateFindControlState', this.onUpdateState(state, previous, this.matchCount);
{ result: state, findPrevious: previous });
return;
} }
if (this.findBar === null) {
throw new Error('PDFFindController is not initialized with a ' +
'PDFFindBar instance.');
}
this.findBar.updateUIState(state, previous, this.matchCount);
} }
}; };
return PDFFindController; return PDFFindController;

View File

@ -50,6 +50,8 @@
PDFJS.DefaultAnnotationLayerFactory = PDFJS.DefaultAnnotationLayerFactory =
pdfViewerLibs.pdfjsWebAnnotationLayerBuilder.DefaultAnnotationLayerFactory; pdfViewerLibs.pdfjsWebAnnotationLayerBuilder.DefaultAnnotationLayerFactory;
PDFJS.PDFHistory = pdfViewerLibs.pdfjsWebPDFHistory.PDFHistory; PDFJS.PDFHistory = pdfViewerLibs.pdfjsWebPDFHistory.PDFHistory;
PDFJS.PDFFindController =
pdfViewerLibs.pdfjsWebPDFFindController.PDFFindController;
PDFJS.DownloadManager = pdfViewerLibs.pdfjsWebDownloadManager.DownloadManager; PDFJS.DownloadManager = pdfViewerLibs.pdfjsWebDownloadManager.DownloadManager;
PDFJS.ProgressBar = pdfViewerLibs.pdfjsWebUIUtils.ProgressBar; PDFJS.ProgressBar = pdfViewerLibs.pdfjsWebUIUtils.ProgressBar;