Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

custom-pdf-search

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-pdf-search - npm Package Compare versions

Comparing version 3.12.12 to 3.12.13

9

lib/cjs/search.js

@@ -574,2 +574,11 @@ 'use strict';

console.log('Matches found:', arr);
var keywordLowerCase = keyword.keyword.toLowerCase();
var pageTextLowerCase = pageText.toLowerCase();
var isKeywordInText = pageTextLowerCase.includes(keywordLowerCase);
if (isKeywordInText) {
console.log('Keyword found:', keyword);
}
else {
console.log('Keyword not found:', keyword);
}
});

@@ -576,0 +585,0 @@ }

2

lib/cjs/search.min.js

@@ -1,1 +0,1 @@

"use strict";var e=require("@react-pdf-viewer/core");function t(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var r=t(require("react")),n=function(){return r.createElement(e.Icon,{size:16},r.createElement("path",{d:"M0.541,5.627L11.666,18.2c0.183,0.207,0.499,0.226,0.706,0.043c0.015-0.014,0.03-0.028,0.043-0.043\n L23.541,5.627"}))},o=function(){return r.createElement(e.Icon,{size:16},r.createElement("path",{d:"M23.535,18.373L12.409,5.8c-0.183-0.207-0.499-0.226-0.706-0.043C11.688,5.77,11.674,5.785,11.66,5.8\n L0.535,18.373"}))},a=function(){return r.createElement(e.Icon,{ignoreDirection:!0,size:16},r.createElement("path",{d:"M10.5,0.5c5.523,0,10,4.477,10,10s-4.477,10-10,10s-10-4.477-10-10S4.977,0.5,10.5,0.5z\n M23.5,23.5\n l-5.929-5.929"}))},c=function(){return c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},c.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var s=function(e){return{left:"".concat(e.left,"%"),top:"".concat(e.top,"%"),height:"".concat(e.height,"%"),width:"".concat(e.width,"%")}},u=function(t){var n=t.index,o=t.area,a=t.onHighlightKeyword,c=r.useRef();return e.useIsomorphicLayoutEffect((function(){var e=c.current;a&&e&&a({highlightEle:e,keyword:o.keyword})}),[]),r.createElement("div",{className:"rpv-search__highlight","data-index":n,ref:c,style:s(o),title:o.keywordStr.trim()})},i={keyword:"",regExp:new RegExp(" "),wholeWords:!1},l=function(e){var t=e.parentNode;t&&t.removeChild(e)},h=function(e){var t=e.parentNode;if(t){var r=document.createRange();r.selectNodeContents(e),function(e,t){l(e);var r=t.parentNode;r&&r.insertBefore(e,t),l(t)}(r.extractContents(),e),t.normalize()}},d=function(e,t){return e.top<t.top?-1:e.top>t.top?1:e.left<t.left?-1:e.left>t.left?1:0},f=function(t){var n=t.numPages,o=t.pageIndex,a=t.renderHighlights,c=t.store,l=t.onHighlightKeyword,f=r.useRef(),p=r.useCallback((function(e){return r.createElement(r.Fragment,null,e.highlightAreas.map((function(e,t){return r.createElement(u,{index:t,key:t,area:e,onHighlightKeyword:l})})))}),[]),g=a||p,m=r.useState(c.get("matchPosition")),v=m[0],x=m[1],y=r.useState(c.get("keyword")||[i]),w=y[0],E=y[1],b=r.useState({pageIndex:o,scale:1,status:e.LayerRenderStatus.PreRender}),k=b[0],S=b[1],C=r.useRef(null),P=r.useRef([]),_=r.useState([]),I=_[0],T=_[1],M=function(){return!0},R=r.useCallback((function(){return c.get("targetPageFilter")||M}),[c.get("targetPageFilter")]),L=function(e){var t=P.current;if(0===t.length)return[];var r=[],a=[].slice.call(e.querySelectorAll(".rpv-core__text-layer-text")),c=t.map((function(e){return e.char})).join("");return w.forEach((function(s){var u=s.keyword;if(u.trim()){for(var i,l=-1===s.regExp.flags.indexOf("g")?new RegExp(s.regExp,"".concat(s.regExp.flags,"g")):s.regExp,d=[];null!==(i=l.exec(c));)d.push({keyword:l,startIndex:i.index,endIndex:l.lastIndex});d.map((function(e){return{keyword:e.keyword,indexes:t.slice(e.startIndex,e.endIndex)}})).forEach((function(t){var c=t.indexes.reduce((function(e,t){return e[t.spanIndex]=(e[t.spanIndex]||[]).concat([t]),e}),{});Object.values(c).forEach((function(c){if(1!==c.length||""!==c[0].char.trim()){var i=s.wholeWords?c.slice(1,-1):c,l=function(e,t,r,a,c){var s=document.createRange(),u=a.firstChild;if(!u||u.nodeType!==Node.TEXT_NODE)return null;var i=u.textContent.length,l=c[0].charIndexInSpan,d=1===c.length?l:c[c.length-1].charIndexInSpan;if(l>i||d+1>i)return null;s.setStart(u,l),s.setEnd(u,d+1);var f=document.createElement("span");s.surroundContents(f);var p=f.getBoundingClientRect(),g=r.getBoundingClientRect(),m=g.height,v=g.width,x=100*(p.left-g.left)/v,y=100*(p.top-g.top)/m,w=100*p.height/m,E=100*p.width/v;return h(f),{keyword:t,keywordStr:e,numPages:n,pageIndex:o,left:x,top:y,height:w,width:E,pageHeight:m,pageWidth:v}}(u,t.keyword,e,a[i[0].spanIndex],i);l&&r.push(l)}}))}))}})),r.sort(d)},j=function(e){e&&e.length>0&&E(e)},N=function(e){return x(e)},K=function(e){if(e.has(o)){var t=e.get(o);t&&S({ele:t.ele,pageIndex:o,scale:t.scale,status:t.status})}},O=function(){return 0===w.length||1===w.length&&""===w[0].keyword.trim()};return r.useEffect((function(){if(!O()&&k.status===e.LayerRenderStatus.DidRender&&!P.current.length){var t=k.ele,r=[].slice.call(t.querySelectorAll(".rpv-core__text-layer-text")).map((function(e){return e.textContent})).reduce((function(e,t,r){return e.concat(t.split("").map((function(e,t){return{char:e,charIndexInSpan:t,spanIndex:r}})))}),[{char:"",charIndexInSpan:0,spanIndex:0}]).slice(1);P.current=r}}),[w,k.status]),r.useEffect((function(){if(!O()&&k.ele&&k.status===e.LayerRenderStatus.DidRender&&R()({pageIndex:o,numPages:n})){var t=k.ele,r=L(t);T(r)}}),[w,v,k.status,P.current]),r.useEffect((function(){O()&&k.ele&&k.status===e.LayerRenderStatus.DidRender&&T([])}),[w,k.status]),r.useEffect((function(){if(0!==I.length){var t=f.current;if(v.pageIndex===o&&t&&k.status===e.LayerRenderStatus.DidRender){var r=t.querySelector('.rpv-search__highlight[data-index="'.concat(v.matchIndex,'"]'));if(r){var n=function(e,t){for(var r=e.offsetTop,n=e.offsetLeft,o=e.parentElement;o&&o!==t;)r+=o.offsetTop,n+=o.offsetLeft,o=o.parentElement;return{left:n,top:r}}(r,t),a=n.left,s=n.top,u=c.get("jumpToDestination");u&&(u({pageIndex:o,bottomOffset:(t.getBoundingClientRect().height-s)/k.scale,leftOffset:a/k.scale,scaleTo:k.scale}),C.current&&C.current.classList.remove("rpv-search__highlight--current"),C.current=r,r.classList.add("rpv-search__highlight--current"))}}}}),[I,v]),r.useEffect((function(){return c.subscribe("keyword",j),c.subscribe("matchPosition",N),c.subscribe("renderStatus",K),function(){c.unsubscribe("keyword",j),c.unsubscribe("matchPosition",N),c.unsubscribe("renderStatus",K)}}),[]),r.createElement("div",{className:"rpv-search__highlights","data-testid":"search__highlights-".concat(o),ref:f},g({getCssProperties:s,highlightAreas:I}))},p=function(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")},g=function(e){var t=e.wholeWords?"\\b".concat(p(e.keyword),"\\b"):p(e.keyword),r=e.matchCase?"g":"gi";return{keyword:e.keyword,regExp:new RegExp(t,r),wholeWords:e.wholeWords||!1}},m=function(e,t,r){return e instanceof RegExp?{keyword:e.source,regExp:e,wholeWords:r||!1}:"string"==typeof e?""===e?i:g({keyword:e,matchCase:t||!1,wholeWords:r||!1}):(void 0!==t&&(e.matchCase=t),void 0!==r&&(e.wholeWords=r),g(e))},v=function(t){var n,o=t.get("initialKeyword"),a=r.useMemo((function(){if(o&&1===o.length){var e=m(o[0]);return{matchCase:-1===e.regExp.flags.indexOf("i"),wholeWords:e.wholeWords}}return{matchCase:!1,wholeWords:!1}}),[]),s=function(e){var t=r.useRef(e.get("doc")),n=function(e){t.current=e};return r.useEffect((function(){return e.subscribe("doc",n),function(){e.unsubscribe("doc",n)}}),[]),t}(t),u=r.useState(o),l=u[0],h=u[1],d=r.useState([]),f=d[0],p=d[1],g=r.useState(0),v=g[0],x=g[1],y=r.useState(a.matchCase),w=y[0],E=y[1],b=r.useRef([]),k=r.useState(a.wholeWords),S=k[0],C=k[1],P=function(){return!0},_=r.useCallback((function(){return t.get("targetPageFilter")||P}),[t.get("targetPageFilter")]),I=function(e){var t=f.length;if(0===l.length||0===t)return null;var r=e===t+1?1:Math.max(1,Math.min(t,e));return x(r),M(f[r-1])},T=function(e){return h(""===e?[]:[e])},M=function(e){var r=t.get("jumpToPage");return r&&r(e.pageIndex),t.update("matchPosition",{matchIndex:e.matchIndex,pageIndex:e.pageIndex}),e},R=function(r,n,o){var a=s.current;if(!a)return Promise.resolve([]);var u=a.numPages,i=r.map((function(e){return m(e,n,o)}));return console.log("pdf viewer keywords: ",i),t.update("keyword",i),x(0),p([]),new Promise((function(t,r){var n=0===b.current.length?function(){var t=s.current;if(!t)return Promise.resolve([]);var r=Array(t.numPages).fill(0).map((function(r,n){return e.getPage(t,n).then((function(e){return e.getTextContent()})).then((function(e){console.log("package",e);var t=e.items.map((function(e){return""===e.str?c(c({},e),{str:" "}):e})).map((function(e){return e.str||""})).join("");return console.log("package2",t),Promise.resolve({pageContent:t,pageIndex:n})}))}));return Promise.all(r).then((function(e){return e.sort((function(e,t){return e.pageIndex-t.pageIndex})),Promise.resolve(e.map((function(e){return e.pageContent})))}))}().then((function(e){return console.log("getTextContents response: ",e),b.current=e,Promise.resolve(e)})):Promise.resolve(b.current);n.then((function(e){console.log("getTextPromise response: ",e);var r=[];e.forEach((function(e,t){_()({pageIndex:t,numPages:u})&&i.forEach((function(n){for(var o,a=0;null!==(o=n.regExp.exec(e));)r.push({keyword:n.regExp,matchIndex:a,pageIndex:t,pageText:e,startIndex:o.index,endIndex:n.regExp.lastIndex}),a++;console.log("Current page text:",e),console.log("Current keyword:",n.keyword),console.log("Matches found:",r)}))})),p(r),console.log("pdf viewer arr: ",r),r.length>0&&(x(1),M(r[0])),t(r)}))}))};return r.useEffect((function(){b.current=[]}),[s.current]),{clearKeyword:function(){t.update("keyword",[i]),T(""),x(0),p([]),E(!1),C(!1)},changeMatchCase:function(e){E(e),l.length>0&&R(l,e,S)},changeWholeWords:function(e){C(e),l.length>0&&R(l,w,e)},currentMatch:v,jumpToMatch:I,jumpToNextMatch:function(){return I(v+1)},jumpToPreviousMatch:function(){return I(v-1)},keywords:l,matchCase:w,numberOfMatches:f.length,wholeWords:S,search:function(){return R(l,w,S)},searchFor:R,setKeywords:h,keyword:0===l.length?"":(n=l[0],n instanceof RegExp?n.source:"string"==typeof n?n:n.keyword),setKeyword:T,setTargetPages:function(e){t.update("targetPageFilter",e)}}},x=function(e){var t=e.children,n=e.store,o=v(n),a=r.useState(!1),s=a[0],u=a[1],i=function(e){return u(!0)};return r.useEffect((function(){return n.subscribe("doc",i),function(){n.unsubscribe("doc",i)}}),[]),t(c(c({},o),{isDocumentLoaded:s}))},y=function(t){var n=t.containerRef,o=t.store,a=r.useRef(!1),c=function(){a.current=!0},s=function(){a.current=!1},u=function(t){var r=n.current;r&&(t.shiftKey||t.altKey||"f"!==t.key||(e.isMac()?t.metaKey&&!t.ctrlKey:t.ctrlKey)&&(a.current||document.activeElement&&r.contains(document.activeElement))&&(t.preventDefault(),o.update("areShortcutsPressed",!0)))};return r.useEffect((function(){var e=n.current;if(e)return document.addEventListener("keydown",u),e.addEventListener("mouseenter",c),e.addEventListener("mouseleave",s),function(){document.removeEventListener("keydown",u),e.removeEventListener("mouseenter",c),e.removeEventListener("mouseleave",s)}}),[n.current]),r.createElement(r.Fragment,null)},w={left:0,top:8},E=function(t){var a=t.store,c=t.onToggle,s=r.useContext(e.LocalizationContext).l10n,u=r.useContext(e.ThemeContext).direction,i=r.useState(!1),l=i[0],h=i[1],d=r.useState(!1),f=d[0],p=d[1],g=u===e.TextDirection.RightToLeft,m=v(a),x=m.clearKeyword,y=m.changeMatchCase,E=m.changeWholeWords,b=m.currentMatch,k=m.jumpToNextMatch,S=m.jumpToPreviousMatch,C=m.keyword,P=m.matchCase,_=m.numberOfMatches,I=m.wholeWords,T=m.search,M=m.setKeyword,R=function(e){h(!0),T().then((function(t){h(!1),p(!0),e&&e()}))};r.useEffect((function(){var e=a.get("initialKeyword");e&&1===e.length&&C&&R((function(){a.update("initialKeyword",[])}))}),[]);var L=s&&s.search?s.search.enterToSearch:"Enter to search",j=s&&s.search?s.search.previousMatch:"Previous match",N=s&&s.search?s.search.nextMatch:"Next match",K=s&&s.search?s.search.close:"Close";return r.createElement("div",{className:"rpv-search__popover"},r.createElement("div",{className:"rpv-search__popover-input-counter"},r.createElement(e.TextBox,{ariaLabel:L,autoFocus:!0,placeholder:L,type:"text",value:C,onChange:function(e){p(!1),M(e)},onKeyDown:function(e){"Enter"===e.key&&C&&(f?k():R())}}),r.createElement("div",{className:e.classNames({"rpv-search__popover-counter":!0,"rpv-search__popover-counter--ltr":!g,"rpv-search__popover-counter--rtl":g})},l&&r.createElement(e.Spinner,{testId:"search__popover-searching",size:"1rem"}),!l&&r.createElement("span",{"data-testid":"search__popover-num-matches"},b,"/",_))),r.createElement("label",{className:"rpv-search__popover-label"},r.createElement("input",{className:"rpv-search__popover-label-checkbox","data-testid":"search__popover-match-case",checked:P,type:"checkbox",onChange:function(e){p(!1),y(e.target.checked)}})," ",s&&s.search?s.search.matchCase:"Match case"),r.createElement("label",{className:"rpv-search__popover-label"},r.createElement("input",{className:"rpv-search__popover-label-checkbox",checked:I,"data-testid":"search__popover-whole-words",type:"checkbox",onChange:function(e){p(!1),E(e.target.checked)}})," ",s&&s.search?s.search.wholeWords:"Whole words"),r.createElement("div",{className:"rpv-search__popover-footer"},r.createElement("div",{className:"rpv-search__popover-footer-item"},r.createElement(e.Tooltip,{ariaControlsSuffix:"search-previous-match",position:g?e.Position.BottomRight:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaLabel:j,isDisabled:b<=1,onClick:S},r.createElement(o,null)),content:function(){return j},offset:w})),r.createElement("div",{className:"rpv-search__popover-footer-item"},r.createElement(e.Tooltip,{ariaControlsSuffix:"search-next-match",position:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaLabel:N,isDisabled:b>_-1,onClick:k},r.createElement(n,null)),content:function(){return N},offset:w})),r.createElement("div",{className:e.classNames({"rpv-search__popover-footer-button":!0,"rpv-search__popover-footer-button--ltr":!g,"rpv-search__popover-footer-button--rtl":g})},r.createElement(e.Button,{onClick:function(){c(),x()}},K))))},b=function(t){var n=t.children,o=t.onClick,c=r.useContext(e.LocalizationContext).l10n,s=c&&c.search?c.search.search:"Search";return n({icon:r.createElement(a,null),label:s,onClick:o})},k={left:0,top:8},S=function(t){var n=t.enableShortcuts,o=t.store,a=t.onClick,c=n?e.isMac()?"Meta+F":"Ctrl+F":"",s=function(e){e&&a()};return r.useEffect((function(){return o.subscribe("areShortcutsPressed",s),function(){o.unsubscribe("areShortcutsPressed",s)}}),[]),r.createElement(b,{onClick:a},(function(t){return r.createElement(e.Tooltip,{ariaControlsSuffix:"search-popover",position:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaKeyShortcuts:c,ariaLabel:t.label,testId:"search__popover-button",onClick:a},t.icon),content:function(){return t.label},offset:k})}))},C={left:0,top:8},P=function(t){var n=t.children,o=t.enableShortcuts,a=t.store,s=r.useContext(e.ThemeContext).direction===e.TextDirection.RightToLeft?e.Position.BottomRight:e.Position.BottomLeft,u=n||function(e){return r.createElement(S,c({enableShortcuts:o,store:a},e))};return r.createElement(e.Popover,{ariaControlsSuffix:"search",lockScroll:!1,position:s,target:function(e){return u({onClick:e})},content:function(e){return r.createElement(E,{store:a,onToggle:e})},offset:C,closeOnClickOutside:!1,closeOnEscape:!0})},_=function(e){return Array.isArray(e)?e.map((function(e){return m(e)})):[m(e)]};exports.NextIcon=n,exports.PreviousIcon=o,exports.SearchIcon=a,exports.searchPlugin=function(t){var n=r.useMemo((function(){return Object.assign({},{enableShortcuts:!0,onHighlightKeyword:function(){}},t)}),[]),o=r.useMemo((function(){return e.createStore({initialKeyword:t&&t.keyword?Array.isArray(t.keyword)?t.keyword:[t.keyword]:[],keyword:t&&t.keyword?_(t.keyword):[i],matchPosition:{matchIndex:-1,pageIndex:-1},renderStatus:new Map})}),[]),a=v(o),s=a.clearKeyword,u=a.jumpToMatch,l=a.jumpToNextMatch,h=a.jumpToPreviousMatch,d=a.searchFor,p=a.setKeywords,g=a.setTargetPages,m=function(e){return r.createElement(P,c({enableShortcuts:n.enableShortcuts},e,{store:o}))};return{install:function(e){var r=t&&t.keyword?Array.isArray(t.keyword)?t.keyword:[t.keyword]:[],n=t&&t.keyword?_(t.keyword):[i];o.update("initialKeyword",r),o.update("jumpToDestination",e.jumpToDestination),o.update("jumpToPage",e.jumpToPage),o.update("keyword",n)},renderPageLayer:function(e){return r.createElement(f,{key:e.pageIndex,numPages:e.doc.numPages,pageIndex:e.pageIndex,renderHighlights:null==t?void 0:t.renderHighlights,store:o,onHighlightKeyword:n.onHighlightKeyword})},renderViewer:function(e){var t=e.slot;return t.subSlot&&(t.subSlot.children=r.createElement(r.Fragment,null,n.enableShortcuts&&r.createElement(y,{containerRef:e.containerRef,store:o}),t.subSlot.children)),t},uninstall:function(e){var t=o.get("renderStatus");t&&t.clear()},onDocumentLoad:function(e){o.update("doc",e.doc)},onTextLayerRender:function(e){var t=o.get("renderStatus");t&&(t=t.set(e.pageIndex,e),o.update("renderStatus",t))},Search:function(e){return r.createElement(x,c({},e,{store:o}))},ShowSearchPopover:m,ShowSearchPopoverButton:function(){return r.createElement(m,null,(function(e){return r.createElement(S,c({enableShortcuts:n.enableShortcuts,store:o},e))}))},clearHighlights:function(){s()},highlight:function(e){var t=Array.isArray(e)?e:[e];return p(t),d(t)},jumpToMatch:u,jumpToNextMatch:l,jumpToPreviousMatch:h,setTargetPages:g}};
"use strict";var e=require("@react-pdf-viewer/core");function t(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var r=t(require("react")),n=function(){return r.createElement(e.Icon,{size:16},r.createElement("path",{d:"M0.541,5.627L11.666,18.2c0.183,0.207,0.499,0.226,0.706,0.043c0.015-0.014,0.03-0.028,0.043-0.043\n L23.541,5.627"}))},o=function(){return r.createElement(e.Icon,{size:16},r.createElement("path",{d:"M23.535,18.373L12.409,5.8c-0.183-0.207-0.499-0.226-0.706-0.043C11.688,5.77,11.674,5.785,11.66,5.8\n L0.535,18.373"}))},a=function(){return r.createElement(e.Icon,{ignoreDirection:!0,size:16},r.createElement("path",{d:"M10.5,0.5c5.523,0,10,4.477,10,10s-4.477,10-10,10s-10-4.477-10-10S4.977,0.5,10.5,0.5z\n M23.5,23.5\n l-5.929-5.929"}))},c=function(){return c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},c.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var s=function(e){return{left:"".concat(e.left,"%"),top:"".concat(e.top,"%"),height:"".concat(e.height,"%"),width:"".concat(e.width,"%")}},u=function(t){var n=t.index,o=t.area,a=t.onHighlightKeyword,c=r.useRef();return e.useIsomorphicLayoutEffect((function(){var e=c.current;a&&e&&a({highlightEle:e,keyword:o.keyword})}),[]),r.createElement("div",{className:"rpv-search__highlight","data-index":n,ref:c,style:s(o),title:o.keywordStr.trim()})},i={keyword:"",regExp:new RegExp(" "),wholeWords:!1},l=function(e){var t=e.parentNode;t&&t.removeChild(e)},h=function(e){var t=e.parentNode;if(t){var r=document.createRange();r.selectNodeContents(e),function(e,t){l(e);var r=t.parentNode;r&&r.insertBefore(e,t),l(t)}(r.extractContents(),e),t.normalize()}},d=function(e,t){return e.top<t.top?-1:e.top>t.top?1:e.left<t.left?-1:e.left>t.left?1:0},f=function(t){var n=t.numPages,o=t.pageIndex,a=t.renderHighlights,c=t.store,l=t.onHighlightKeyword,f=r.useRef(),p=r.useCallback((function(e){return r.createElement(r.Fragment,null,e.highlightAreas.map((function(e,t){return r.createElement(u,{index:t,key:t,area:e,onHighlightKeyword:l})})))}),[]),g=a||p,m=r.useState(c.get("matchPosition")),v=m[0],x=m[1],y=r.useState(c.get("keyword")||[i]),w=y[0],E=y[1],b=r.useState({pageIndex:o,scale:1,status:e.LayerRenderStatus.PreRender}),k=b[0],S=b[1],C=r.useRef(null),P=r.useRef([]),_=r.useState([]),I=_[0],T=_[1],M=function(){return!0},L=r.useCallback((function(){return c.get("targetPageFilter")||M}),[c.get("targetPageFilter")]),R=function(e){var t=P.current;if(0===t.length)return[];var r=[],a=[].slice.call(e.querySelectorAll(".rpv-core__text-layer-text")),c=t.map((function(e){return e.char})).join("");return w.forEach((function(s){var u=s.keyword;if(u.trim()){for(var i,l=-1===s.regExp.flags.indexOf("g")?new RegExp(s.regExp,"".concat(s.regExp.flags,"g")):s.regExp,d=[];null!==(i=l.exec(c));)d.push({keyword:l,startIndex:i.index,endIndex:l.lastIndex});d.map((function(e){return{keyword:e.keyword,indexes:t.slice(e.startIndex,e.endIndex)}})).forEach((function(t){var c=t.indexes.reduce((function(e,t){return e[t.spanIndex]=(e[t.spanIndex]||[]).concat([t]),e}),{});Object.values(c).forEach((function(c){if(1!==c.length||""!==c[0].char.trim()){var i=s.wholeWords?c.slice(1,-1):c,l=function(e,t,r,a,c){var s=document.createRange(),u=a.firstChild;if(!u||u.nodeType!==Node.TEXT_NODE)return null;var i=u.textContent.length,l=c[0].charIndexInSpan,d=1===c.length?l:c[c.length-1].charIndexInSpan;if(l>i||d+1>i)return null;s.setStart(u,l),s.setEnd(u,d+1);var f=document.createElement("span");s.surroundContents(f);var p=f.getBoundingClientRect(),g=r.getBoundingClientRect(),m=g.height,v=g.width,x=100*(p.left-g.left)/v,y=100*(p.top-g.top)/m,w=100*p.height/m,E=100*p.width/v;return h(f),{keyword:t,keywordStr:e,numPages:n,pageIndex:o,left:x,top:y,height:w,width:E,pageHeight:m,pageWidth:v}}(u,t.keyword,e,a[i[0].spanIndex],i);l&&r.push(l)}}))}))}})),r.sort(d)},j=function(e){e&&e.length>0&&E(e)},K=function(e){return x(e)},N=function(e){if(e.has(o)){var t=e.get(o);t&&S({ele:t.ele,pageIndex:o,scale:t.scale,status:t.status})}},O=function(){return 0===w.length||1===w.length&&""===w[0].keyword.trim()};return r.useEffect((function(){if(!O()&&k.status===e.LayerRenderStatus.DidRender&&!P.current.length){var t=k.ele,r=[].slice.call(t.querySelectorAll(".rpv-core__text-layer-text")).map((function(e){return e.textContent})).reduce((function(e,t,r){return e.concat(t.split("").map((function(e,t){return{char:e,charIndexInSpan:t,spanIndex:r}})))}),[{char:"",charIndexInSpan:0,spanIndex:0}]).slice(1);P.current=r}}),[w,k.status]),r.useEffect((function(){if(!O()&&k.ele&&k.status===e.LayerRenderStatus.DidRender&&L()({pageIndex:o,numPages:n})){var t=k.ele,r=R(t);T(r)}}),[w,v,k.status,P.current]),r.useEffect((function(){O()&&k.ele&&k.status===e.LayerRenderStatus.DidRender&&T([])}),[w,k.status]),r.useEffect((function(){if(0!==I.length){var t=f.current;if(v.pageIndex===o&&t&&k.status===e.LayerRenderStatus.DidRender){var r=t.querySelector('.rpv-search__highlight[data-index="'.concat(v.matchIndex,'"]'));if(r){var n=function(e,t){for(var r=e.offsetTop,n=e.offsetLeft,o=e.parentElement;o&&o!==t;)r+=o.offsetTop,n+=o.offsetLeft,o=o.parentElement;return{left:n,top:r}}(r,t),a=n.left,s=n.top,u=c.get("jumpToDestination");u&&(u({pageIndex:o,bottomOffset:(t.getBoundingClientRect().height-s)/k.scale,leftOffset:a/k.scale,scaleTo:k.scale}),C.current&&C.current.classList.remove("rpv-search__highlight--current"),C.current=r,r.classList.add("rpv-search__highlight--current"))}}}}),[I,v]),r.useEffect((function(){return c.subscribe("keyword",j),c.subscribe("matchPosition",K),c.subscribe("renderStatus",N),function(){c.unsubscribe("keyword",j),c.unsubscribe("matchPosition",K),c.unsubscribe("renderStatus",N)}}),[]),r.createElement("div",{className:"rpv-search__highlights","data-testid":"search__highlights-".concat(o),ref:f},g({getCssProperties:s,highlightAreas:I}))},p=function(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")},g=function(e){var t=e.wholeWords?"\\b".concat(p(e.keyword),"\\b"):p(e.keyword),r=e.matchCase?"g":"gi";return{keyword:e.keyword,regExp:new RegExp(t,r),wholeWords:e.wholeWords||!1}},m=function(e,t,r){return e instanceof RegExp?{keyword:e.source,regExp:e,wholeWords:r||!1}:"string"==typeof e?""===e?i:g({keyword:e,matchCase:t||!1,wholeWords:r||!1}):(void 0!==t&&(e.matchCase=t),void 0!==r&&(e.wholeWords=r),g(e))},v=function(t){var n,o=t.get("initialKeyword"),a=r.useMemo((function(){if(o&&1===o.length){var e=m(o[0]);return{matchCase:-1===e.regExp.flags.indexOf("i"),wholeWords:e.wholeWords}}return{matchCase:!1,wholeWords:!1}}),[]),s=function(e){var t=r.useRef(e.get("doc")),n=function(e){t.current=e};return r.useEffect((function(){return e.subscribe("doc",n),function(){e.unsubscribe("doc",n)}}),[]),t}(t),u=r.useState(o),l=u[0],h=u[1],d=r.useState([]),f=d[0],p=d[1],g=r.useState(0),v=g[0],x=g[1],y=r.useState(a.matchCase),w=y[0],E=y[1],b=r.useRef([]),k=r.useState(a.wholeWords),S=k[0],C=k[1],P=function(){return!0},_=r.useCallback((function(){return t.get("targetPageFilter")||P}),[t.get("targetPageFilter")]),I=function(e){var t=f.length;if(0===l.length||0===t)return null;var r=e===t+1?1:Math.max(1,Math.min(t,e));return x(r),M(f[r-1])},T=function(e){return h(""===e?[]:[e])},M=function(e){var r=t.get("jumpToPage");return r&&r(e.pageIndex),t.update("matchPosition",{matchIndex:e.matchIndex,pageIndex:e.pageIndex}),e},L=function(r,n,o){var a=s.current;if(!a)return Promise.resolve([]);var u=a.numPages,i=r.map((function(e){return m(e,n,o)}));return console.log("pdf viewer keywords: ",i),t.update("keyword",i),x(0),p([]),new Promise((function(t,r){var n=0===b.current.length?function(){var t=s.current;if(!t)return Promise.resolve([]);var r=Array(t.numPages).fill(0).map((function(r,n){return e.getPage(t,n).then((function(e){return e.getTextContent()})).then((function(e){console.log("package",e);var t=e.items.map((function(e){return""===e.str?c(c({},e),{str:" "}):e})).map((function(e){return e.str||""})).join("");return console.log("package2",t),Promise.resolve({pageContent:t,pageIndex:n})}))}));return Promise.all(r).then((function(e){return e.sort((function(e,t){return e.pageIndex-t.pageIndex})),Promise.resolve(e.map((function(e){return e.pageContent})))}))}().then((function(e){return console.log("getTextContents response: ",e),b.current=e,Promise.resolve(e)})):Promise.resolve(b.current);n.then((function(e){console.log("getTextPromise response: ",e);var r=[];e.forEach((function(e,t){_()({pageIndex:t,numPages:u})&&i.forEach((function(n){for(var o,a=0;null!==(o=n.regExp.exec(e));)r.push({keyword:n.regExp,matchIndex:a,pageIndex:t,pageText:e,startIndex:o.index,endIndex:n.regExp.lastIndex}),a++;console.log("Current page text:",e),console.log("Current keyword:",n.keyword),console.log("Matches found:",r);var c=n.keyword.toLowerCase();e.toLowerCase().includes(c)?console.log("Keyword found:",n):console.log("Keyword not found:",n)}))})),p(r),console.log("pdf viewer arr: ",r),r.length>0&&(x(1),M(r[0])),t(r)}))}))};return r.useEffect((function(){b.current=[]}),[s.current]),{clearKeyword:function(){t.update("keyword",[i]),T(""),x(0),p([]),E(!1),C(!1)},changeMatchCase:function(e){E(e),l.length>0&&L(l,e,S)},changeWholeWords:function(e){C(e),l.length>0&&L(l,w,e)},currentMatch:v,jumpToMatch:I,jumpToNextMatch:function(){return I(v+1)},jumpToPreviousMatch:function(){return I(v-1)},keywords:l,matchCase:w,numberOfMatches:f.length,wholeWords:S,search:function(){return L(l,w,S)},searchFor:L,setKeywords:h,keyword:0===l.length?"":(n=l[0],n instanceof RegExp?n.source:"string"==typeof n?n:n.keyword),setKeyword:T,setTargetPages:function(e){t.update("targetPageFilter",e)}}},x=function(e){var t=e.children,n=e.store,o=v(n),a=r.useState(!1),s=a[0],u=a[1],i=function(e){return u(!0)};return r.useEffect((function(){return n.subscribe("doc",i),function(){n.unsubscribe("doc",i)}}),[]),t(c(c({},o),{isDocumentLoaded:s}))},y=function(t){var n=t.containerRef,o=t.store,a=r.useRef(!1),c=function(){a.current=!0},s=function(){a.current=!1},u=function(t){var r=n.current;r&&(t.shiftKey||t.altKey||"f"!==t.key||(e.isMac()?t.metaKey&&!t.ctrlKey:t.ctrlKey)&&(a.current||document.activeElement&&r.contains(document.activeElement))&&(t.preventDefault(),o.update("areShortcutsPressed",!0)))};return r.useEffect((function(){var e=n.current;if(e)return document.addEventListener("keydown",u),e.addEventListener("mouseenter",c),e.addEventListener("mouseleave",s),function(){document.removeEventListener("keydown",u),e.removeEventListener("mouseenter",c),e.removeEventListener("mouseleave",s)}}),[n.current]),r.createElement(r.Fragment,null)},w={left:0,top:8},E=function(t){var a=t.store,c=t.onToggle,s=r.useContext(e.LocalizationContext).l10n,u=r.useContext(e.ThemeContext).direction,i=r.useState(!1),l=i[0],h=i[1],d=r.useState(!1),f=d[0],p=d[1],g=u===e.TextDirection.RightToLeft,m=v(a),x=m.clearKeyword,y=m.changeMatchCase,E=m.changeWholeWords,b=m.currentMatch,k=m.jumpToNextMatch,S=m.jumpToPreviousMatch,C=m.keyword,P=m.matchCase,_=m.numberOfMatches,I=m.wholeWords,T=m.search,M=m.setKeyword,L=function(e){h(!0),T().then((function(t){h(!1),p(!0),e&&e()}))};r.useEffect((function(){var e=a.get("initialKeyword");e&&1===e.length&&C&&L((function(){a.update("initialKeyword",[])}))}),[]);var R=s&&s.search?s.search.enterToSearch:"Enter to search",j=s&&s.search?s.search.previousMatch:"Previous match",K=s&&s.search?s.search.nextMatch:"Next match",N=s&&s.search?s.search.close:"Close";return r.createElement("div",{className:"rpv-search__popover"},r.createElement("div",{className:"rpv-search__popover-input-counter"},r.createElement(e.TextBox,{ariaLabel:R,autoFocus:!0,placeholder:R,type:"text",value:C,onChange:function(e){p(!1),M(e)},onKeyDown:function(e){"Enter"===e.key&&C&&(f?k():L())}}),r.createElement("div",{className:e.classNames({"rpv-search__popover-counter":!0,"rpv-search__popover-counter--ltr":!g,"rpv-search__popover-counter--rtl":g})},l&&r.createElement(e.Spinner,{testId:"search__popover-searching",size:"1rem"}),!l&&r.createElement("span",{"data-testid":"search__popover-num-matches"},b,"/",_))),r.createElement("label",{className:"rpv-search__popover-label"},r.createElement("input",{className:"rpv-search__popover-label-checkbox","data-testid":"search__popover-match-case",checked:P,type:"checkbox",onChange:function(e){p(!1),y(e.target.checked)}})," ",s&&s.search?s.search.matchCase:"Match case"),r.createElement("label",{className:"rpv-search__popover-label"},r.createElement("input",{className:"rpv-search__popover-label-checkbox",checked:I,"data-testid":"search__popover-whole-words",type:"checkbox",onChange:function(e){p(!1),E(e.target.checked)}})," ",s&&s.search?s.search.wholeWords:"Whole words"),r.createElement("div",{className:"rpv-search__popover-footer"},r.createElement("div",{className:"rpv-search__popover-footer-item"},r.createElement(e.Tooltip,{ariaControlsSuffix:"search-previous-match",position:g?e.Position.BottomRight:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaLabel:j,isDisabled:b<=1,onClick:S},r.createElement(o,null)),content:function(){return j},offset:w})),r.createElement("div",{className:"rpv-search__popover-footer-item"},r.createElement(e.Tooltip,{ariaControlsSuffix:"search-next-match",position:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaLabel:K,isDisabled:b>_-1,onClick:k},r.createElement(n,null)),content:function(){return K},offset:w})),r.createElement("div",{className:e.classNames({"rpv-search__popover-footer-button":!0,"rpv-search__popover-footer-button--ltr":!g,"rpv-search__popover-footer-button--rtl":g})},r.createElement(e.Button,{onClick:function(){c(),x()}},N))))},b=function(t){var n=t.children,o=t.onClick,c=r.useContext(e.LocalizationContext).l10n,s=c&&c.search?c.search.search:"Search";return n({icon:r.createElement(a,null),label:s,onClick:o})},k={left:0,top:8},S=function(t){var n=t.enableShortcuts,o=t.store,a=t.onClick,c=n?e.isMac()?"Meta+F":"Ctrl+F":"",s=function(e){e&&a()};return r.useEffect((function(){return o.subscribe("areShortcutsPressed",s),function(){o.unsubscribe("areShortcutsPressed",s)}}),[]),r.createElement(b,{onClick:a},(function(t){return r.createElement(e.Tooltip,{ariaControlsSuffix:"search-popover",position:e.Position.BottomCenter,target:r.createElement(e.MinimalButton,{ariaKeyShortcuts:c,ariaLabel:t.label,testId:"search__popover-button",onClick:a},t.icon),content:function(){return t.label},offset:k})}))},C={left:0,top:8},P=function(t){var n=t.children,o=t.enableShortcuts,a=t.store,s=r.useContext(e.ThemeContext).direction===e.TextDirection.RightToLeft?e.Position.BottomRight:e.Position.BottomLeft,u=n||function(e){return r.createElement(S,c({enableShortcuts:o,store:a},e))};return r.createElement(e.Popover,{ariaControlsSuffix:"search",lockScroll:!1,position:s,target:function(e){return u({onClick:e})},content:function(e){return r.createElement(E,{store:a,onToggle:e})},offset:C,closeOnClickOutside:!1,closeOnEscape:!0})},_=function(e){return Array.isArray(e)?e.map((function(e){return m(e)})):[m(e)]};exports.NextIcon=n,exports.PreviousIcon=o,exports.SearchIcon=a,exports.searchPlugin=function(t){var n=r.useMemo((function(){return Object.assign({},{enableShortcuts:!0,onHighlightKeyword:function(){}},t)}),[]),o=r.useMemo((function(){return e.createStore({initialKeyword:t&&t.keyword?Array.isArray(t.keyword)?t.keyword:[t.keyword]:[],keyword:t&&t.keyword?_(t.keyword):[i],matchPosition:{matchIndex:-1,pageIndex:-1},renderStatus:new Map})}),[]),a=v(o),s=a.clearKeyword,u=a.jumpToMatch,l=a.jumpToNextMatch,h=a.jumpToPreviousMatch,d=a.searchFor,p=a.setKeywords,g=a.setTargetPages,m=function(e){return r.createElement(P,c({enableShortcuts:n.enableShortcuts},e,{store:o}))};return{install:function(e){var r=t&&t.keyword?Array.isArray(t.keyword)?t.keyword:[t.keyword]:[],n=t&&t.keyword?_(t.keyword):[i];o.update("initialKeyword",r),o.update("jumpToDestination",e.jumpToDestination),o.update("jumpToPage",e.jumpToPage),o.update("keyword",n)},renderPageLayer:function(e){return r.createElement(f,{key:e.pageIndex,numPages:e.doc.numPages,pageIndex:e.pageIndex,renderHighlights:null==t?void 0:t.renderHighlights,store:o,onHighlightKeyword:n.onHighlightKeyword})},renderViewer:function(e){var t=e.slot;return t.subSlot&&(t.subSlot.children=r.createElement(r.Fragment,null,n.enableShortcuts&&r.createElement(y,{containerRef:e.containerRef,store:o}),t.subSlot.children)),t},uninstall:function(e){var t=o.get("renderStatus");t&&t.clear()},onDocumentLoad:function(e){o.update("doc",e.doc)},onTextLayerRender:function(e){var t=o.get("renderStatus");t&&(t=t.set(e.pageIndex,e),o.update("renderStatus",t))},Search:function(e){return r.createElement(x,c({},e,{store:o}))},ShowSearchPopover:m,ShowSearchPopoverButton:function(){return r.createElement(m,null,(function(e){return r.createElement(S,c({enableShortcuts:n.enableShortcuts,store:o},e))}))},clearHighlights:function(){s()},highlight:function(e){var t=Array.isArray(e)?e:[e];return p(t),d(t)},jumpToMatch:u,jumpToNextMatch:l,jumpToPreviousMatch:h,setTargetPages:g}};
{
"name": "custom-pdf-search",
"version": "3.12.12",
"version": "3.12.13",
"description": "A React component to view a PDF document",

@@ -5,0 +5,0 @@ "author": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc