electron-findbar
Version:
Chrome-like findbar for your Electron app.
1 lines • 8.66 kB
JavaScript
(()=>{"use strict";var e={927(e,t,i){var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};const s=i(288),o=i(339),r=n(i(896)),a=n(i(928));class d{constructor(e,t){var i;if(this.followVisibilityEventsFlag=!0,this.matches={active:0,total:0},this.windowHandler=d.defaultWindowHandler,this.boundsHandler=d.defaultBoundsHandler,this.lastText="",this.theme=d.defaultTheme,this.matchCaseFlag=!1,this.isMovableFlag=!1,h(e)?(this.findableContents=e,e=d.getBaseWindowFromWebContents(this.findableContents)):this.findableContents=null!==(i=t)&&void 0!==i?i:d.retrieveWebContents(e),!this.findableContents)throw new Error("There are no searchable web contents.");d.setRef(this.findableContents,this),this.findableContents.prependOnceListener("destroyed",()=>{this.detach()}),this.findableContents.prependListener("found-in-page",(e,t)=>{this.sendMatchesCount(t.activeMatchOrdinal,t.matches)}),this.updateParentWindow(e)}open(){var e,t;if(this.window)return void this.focusWindowAndHighlightInput();this.parent||this.updateParentWindow();const i=d.mergeStandardOptions(this.customOptions,this.parent);this.isMovableFlag=null!==(e=i.movable)&&void 0!==e&&e,this.window=new s.BrowserWindow(i),this.registerListeners(),null===(t=this.windowHandler)||void 0===t||t.call(this,this.window),this.window.loadFile(d.assetPaths.html),d.setRef(this.window.webContents,this)}close(){this.window&&!this.window.isDestroyed()&&this.window.close()}detach(){this.close(),d.setRef(this.findableContents,void 0),this.window&&d.setRef(this.window.webContents,void 0)}updateParentWindow(e){var t;this.parent!==e&&(this.close(),this.parent=null!==(t=e)&&void 0!==t?t:d.getBaseWindowFromWebContents(this.findableContents))}getLastState(){return{text:this.lastText,matchCase:this.matchCaseFlag,movable:this.isMovableFlag,theme:this.theme}}startFind(e,t){var i;t||null===(i=this.window)||void 0===i||i.webContents.send("electron-findbar/text-change",e),(this.lastText=e)?this.isOpen()&&this.findInContent({findNext:!0}):this.stopFind()}matchCase(e,t){var i;this.matchCaseFlag!==e&&(this.matchCaseFlag=e,t||null===(i=this.window)||void 0===i||i.webContents.send("electron-findbar/match-case-change",this.matchCaseFlag),this.stopFindInContent(),this.startFind(this.lastText,t))}findPrevious(){this.matches.total<2||(1===this.matches.active&&(this.fixMove=!1),this.isOpen()&&this.findInContent({forward:!1}))}findNext(){this.matches.total<2||(this.matches.active===this.matches.total&&(this.fixMove=!0),this.isOpen()&&this.findInContent({forward:!0}))}stopFind(){this.isOpen()&&this.sendMatchesCount(0,0),this.findableContents.isDestroyed()||this.stopFindInContent()}isOpen(){return!!this.window}isFocused(){var e;return!!(null===(e=this.window)||void 0===e?void 0:e.isFocused())}isVisible(){var e;return!!(null===(e=this.window)||void 0===e?void 0:e.isVisible())}setWindowOptions(e){this.customOptions=e}setWindowHandler(e){this.windowHandler=e}setBoundsHandler(e){this.boundsHandler=e}followVisibilityEvents(e){this.followVisibilityEventsFlag=e}getTheme(){return this.theme}updateTheme(e=d.defaultTheme){this.theme=e,this.window&&!this.window.isDestroyed()&&this.window.webContents.send("electron-findbar/force-theme",e)}static getDefaultTheme(){return d.defaultTheme}static setDefaultTheme(e){d.defaultTheme=e}static setDefaultWindowHandler(e){d.defaultWindowHandler=e}static setDefaultBoundsHandler(e){d.defaultBoundsHandler=e}registerKeyboardShortcuts(e,t){if(t.meta||t.control||t.alt)return;const i=t.key.toLowerCase();"enter"!==i?"escape"===i&&this.isOpen()&&setTimeout(()=>this.close(),0):t.shift?this.findPrevious():this.findNext()}findInContent(e){e.matchCase=this.matchCaseFlag,this.findableContents.findInPage(this.lastText,e)}stopFindInContent(){this.findableContents.stopFindInPage("clearSelection")}registerListeners(){const e=()=>{this.window.isVisible()||this.window.show()},t=()=>{this.window.isVisible()&&this.window.hide()},i=()=>{this.close(),this.parent=void 0},n=()=>{const e=this.window.getBounds(),t=this.boundsHandler(this.parent.getBounds(),e);t.width||(t.width=e.width),t.height||(t.height=e.height),this.window.setBounds(t,!1)};this.parent&&!this.parent.isDestroyed()&&(n(),this.followVisibilityEventsFlag&&(this.parent.prependListener("show",e),this.parent.prependListener("hide",t)),this.parent.prependListener("resize",n),this.parent.prependListener("move",n),this.parent.prependOnceListener("closed",i)),this.window.once("closed",()=>{this.parent&&!this.parent.isDestroyed()&&(this.followVisibilityEventsFlag&&(this.parent.off("show",e),this.parent.off("hide",t)),this.parent.off("resize",n),this.parent.off("move",n),this.parent.off("closed",i)),this.window=void 0,this.stopFind()}),this.window.prependOnceListener("ready-to-show",()=>{this.window.show()}),this.window.webContents.prependListener("before-input-event",(e,t)=>{this.registerKeyboardShortcuts(e,t)})}sendMatchesCount(e,t){void 0!==this.fixMove&&(this.fixMove?this.findNext():this.findPrevious(),this.fixMove=void 0),this.matches.active=e,this.matches.total=t,this.window.webContents.send("electron-findbar/matches",this.matches)}focusWindowAndHighlightInput(){this.window.focus(),this.window.webContents.send("electron-findbar/input-focus")}static retrieveWebContents(e){var t,i,n;return null!==(t=e.webContents)&&void 0!==t?t:null===(n=null===(i=e.contentView)||void 0===i?void 0:i.children[0])||void 0===n?void 0:n.webContents}static getBaseWindowFromWebContents(e){return s.BaseWindow.getAllWindows().find(t=>{var i;return t.webContents===e||(null===(i=t.contentView)||void 0===i?void 0:i.children.some(t=>t.webContents===e))})}static setDefaultPosition(e,t){return{x:e.x+e.width-t.width-20,y:e.y-(t.height/4|0),width:t.width,height:t.height}}static mergeStandardOptions(e,t){var i,n,s,o,r,a;return e||(e={}),e.width=null!==(i=e.width)&&void 0!==i?i:372,e.height=null!==(n=e.height)&&void 0!==n?n:52,e.resizable=null!==(s=e.resizable)&&void 0!==s&&s,e.movable=null!==(o=e.movable)&&void 0!==o&&o,e.acceptFirstMouse=null===(r=e.acceptFirstMouse)||void 0===r||r,e.parent=t,e.show=!1,e.frame=!1,e.roundedCorners=!0,e.transparent="linux"===process.platform,e.maximizable=!1,e.minimizable=!1,e.skipTaskbar=!0,e.fullscreenable=!1,e.autoHideMenuBar=!0,e.backgroundColor="#222222",e.webPreferences||(e.webPreferences={}),e.webPreferences.nodeIntegration=!1,e.webPreferences.contextIsolation=!0,e.webPreferences.preload=null!==(a=e.webPreferences.preload)&&void 0!==a?a:d.assetPaths.preload,e}static buildAssetPaths(){const e="findbar.html";try{const{app:t}=i(288),n=(0,o.createRequire)(a.default.join(t.getAppPath(),"_")),s=a.default.dirname(n.resolve("electron-findbar"));if(r.default.existsSync(a.default.join(s,e)))return{html:a.default.join(s,e),preload:a.default.join(s,"preload.js")}}catch(e){}return{html:a.default.join(__dirname,e),preload:a.default.join(__dirname,"preload.js")}}static setRef(e,t){Object.defineProperty(e,"__findbar__",{value:t,configurable:!0,writable:!1})}static from(e,t){const i=h(e)?e:null!=t?t:d.retrieveWebContents(e);if(!i)throw new Error("[Findbar] There are no searchable web contents.");return i.__findbar__||new d(e,t)}static fromIfExists(e){const t=h(e)?e:d.retrieveWebContents(e);if(!t)throw new Error("[Findbar] There are no searchable web contents.");return t.__findbar__}}d.assetPaths=d.buildAssetPaths(),d.defaultTheme="system",d.defaultBoundsHandler=d.setDefaultPosition;const h=e=>e&&"function"==typeof e.findInPage&&"function"==typeof e.stopFindInPage;s.ipcMain.handle("electron-findbar/last-state",e=>{var t;return null===(t=d.fromIfExists(e.sender))||void 0===t?void 0:t.getLastState()}),s.ipcMain.on("electron-findbar/input-change",(e,t,i)=>{var n;return null===(n=d.fromIfExists(e.sender))||void 0===n?void 0:n.startFind(t,i)}),s.ipcMain.on("electron-findbar/match-case",(e,t,i)=>{var n;return null===(n=d.fromIfExists(e.sender))||void 0===n?void 0:n.matchCase(t,i)}),s.ipcMain.on("electron-findbar/previous",e=>{var t;return null===(t=d.fromIfExists(e.sender))||void 0===t?void 0:t.findPrevious()}),s.ipcMain.on("electron-findbar/next",e=>{var t;return null===(t=d.fromIfExists(e.sender))||void 0===t?void 0:t.findNext()}),s.ipcMain.on("electron-findbar/open",e=>d.from(e.sender).open()),s.ipcMain.on("electron-findbar/close",e=>{var t;return null===(t=d.fromIfExists(e.sender))||void 0===t?void 0:t.close()}),e.exports=d},288(e){e.exports=require("electron")},896(e){e.exports=require("fs")},339(e){e.exports=require("module")},928(e){e.exports=require("path")}},t={},i=function i(n){var s=t[n];if(void 0!==s)return s.exports;var o=t[n]={exports:{}};return e[n].call(o.exports,o,o.exports,i),o.exports}(927);module.exports=i})();