diff --git a/repo/core/create.js b/repo/core/create.js index fb37b5a..9daff2c 100644 --- a/repo/core/create.js +++ b/repo/core/create.js @@ -52,6 +52,20 @@ module.exports = (store, __exports) => { }, ...rect, }); + electron.session + .fromPartition('persist:notion') + .webRequest.onHeadersReceived((details, callback) => { + callback({ + responseHeaders: Object.assign( + { + 'Content-Security-Policy': [ + "script-src 'self' 'unsafe-inline' 'unsafe-eval' enhancement: https://gist.github.com https://apis.google.com https://api.amplitude.com https://widget.intercom.io https://js.intercomcdn.com https://logs-01.loggly.com https://cdn.segment.com https://analytics.pgncs.notion.so https://checkout.stripe.com https://embed.typeform.com https://admin.typeform.com https://platform.twitter.com https://cdn.syndication.twimg.com; connect-src 'self' https://msgstore.www.notion.so wss://msgstore.www.notion.so https://notion-emojis.s3-us-west-2.amazonaws.com https://s3-us-west-2.amazonaws.com https://s3.us-west-2.amazonaws.com https://notion-production-snapshots-2.s3.us-west-2.amazonaws.com https: http: https://api.amplitude.com https://api.embed.ly https://js.intercomcdn.com https://api-iam.intercom.io wss://nexus-websocket-a.intercom.io https://logs-01.loggly.com https://api.segment.io https://api.pgncs.notion.so https://checkout.stripe.com https://cdn.contentful.com https://preview.contentful.com https://images.ctfassets.net https://api.unsplash.com https://boards-api.greenhouse.io; font-src 'self' data: https://cdnjs.cloudflare.com https://js.intercomcdn.com; img-src 'self' data: blob: https: https://platform.twitter.com https://syndication.twitter.com https://pbs.twimg.com https://ton.twimg.com; style-src 'self' 'unsafe-inline' enhancement: https://cdnjs.cloudflare.com https://github.githubassets.com https://platform.twitter.com https://ton.twimg.com; frame-src https: http:; media-src https: http:", + ], + }, + details.responseHeaders + ), + }); + }); window.once('ready-to-show', function () { if ( !store().openhidden || diff --git a/repo/core/render.js b/repo/core/render.js index dc94206..3b0b494 100644 --- a/repo/core/render.js +++ b/repo/core/render.js @@ -21,6 +21,7 @@ const url = require('url'), module.exports = (store, __exports) => { if (store().tabs) { + let $currentTab; class Index extends React.PureComponent { constructor() { super(...arguments); @@ -29,11 +30,11 @@ module.exports = (store, __exports) => { searching: false, searchingPeekView: false, zoomFactor: 1, - tabs: 2, + tabs: [], }; this.notionElm = []; this.loadedElms = []; - this.reactTabs = []; + this.reactTabs = {}; this.handleNotionRef = (notionElm) => { this.notionElm.push(notionElm); }; @@ -51,13 +52,26 @@ module.exports = (store, __exports) => { } }, 50); }; - window['newtab'] = () => { - this.setState({ tabs: this.state.tabs + 1 }); - setTimeout(() => this.addListeners(), 100); + window['tab'] = (id) => { + if (!id) return; + this.setState({ tabs: [...new Set([...this.state.tabs, id])] }); + setTimeout(() => { + this.addListeners(); + if (document.querySelector(`#tab-${id}`)) { + $currentTab = document.querySelector(`#tab-${id}`); + $currentTab.focus(); + } + }, 100); }; } componentDidMount() { this.addListeners(); + + try { + electron.remote.getCurrentWindow().on('focus', (e) => { + $notion.focus(); + }); + } catch {} } addListeners() { const searchElm = this.searchElm; @@ -214,10 +228,8 @@ module.exports = (store, __exports) => { this.handleReload(); return; } - electronWindow.on('focus', (e) => { - $notion.focus(); - }); $notion.addEventListener('dom-ready', function () { + if ($notion !== $currentTab) return; if (document.activeElement instanceof HTMLElement) { document.activeElement.blur(); } @@ -286,25 +298,26 @@ module.exports = (store, __exports) => { ); } renderNotionContainer() { - this.reactTabs = [ - ...this.reactTabs, - ...new Array(this.state.tabs - this.reactTabs.length) - .fill(0) - .map((i) => - React.createElement('webview', { - className: 'notion', - style: Index.notionWebviewStyle, - ref: this.handleNotionRef, - partition: constants.electronSessionPartition, - preload: path.resolve(`${__notion}/app/renderer/preload.js`), - src: this.props.notionUrl, - }) - ), - ]; + this.reactTabs = Object.fromEntries( + this.state.tabs.map((id) => { + return [ + id, + this.reactTabs[id] || + React.createElement('webview', { + className: 'notion', + style: Index.notionWebviewStyle, + ref: this.handleNotionRef, + partition: constants.electronSessionPartition, + preload: path.resolve(`${__notion}/app/renderer/preload.js`), + src: this.props.notionUrl, + }), + ]; + }) + ); return React.createElement( 'div', { style: this.getNotionContainerStyle() }, - ...this.reactTabs + ...Object.values(this.reactTabs) ); } renderErrorContainer() {