split tabs into separate mod, add opt for default page, load default settings when store()-ing another mod id

This commit is contained in:
dragonwocky 2020-10-12 21:44:12 +11:00
parent d82dda1ff8
commit 9b49e6034b
Signed by: dragonwocky
GPG Key ID: C7A48B7846AA706D
8 changed files with 142 additions and 51 deletions

View File

@ -17,6 +17,8 @@ a flexibility update.
- new: in-page columns disabled/wrapped and wider pages when the window is narrower than 600px
for improved responsiveness.
- new: relaunch button in tray menu.
- new: a core mod option for a default page id (all new windows will load it instead of the
normal "most recent" page).
- improved: a core mod option to make transitions snappy/0s.
- improved: menu will now respect integrated titlebar setting.
- improved: use keyup listeners instead of a globalShortcut for the enhancements menu toggle.

View File

@ -15,7 +15,7 @@ module.exports = (store) => {
buttons = {
element: helpers.createElement('<div class="window-buttons-area"></div>'),
insert: [
...((store('mods', {})['72886371-dada-49a7-9afc-9f275ecf29d3'] || {})
...((store('mods')['72886371-dada-49a7-9afc-9f275ecf29d3'] || {})
.enabled
? ['alwaysontop']
: []),

View File

@ -19,12 +19,16 @@ module.exports = (store, __exports) => {
// additional hotkeys
document.defaultView.addEventListener('keyup', (event) => {
if (event.code === 'F5') location.reload();
// open menu on hotkey toggle
const hotkey = toKeyEvent(store().menu_toggle);
let triggered = true;
for (let prop in hotkey)
if (hotkey[prop] !== event[prop]) triggered = false;
if (triggered) electron.ipcRenderer.send('enhancer:open-menu');
if (
!(store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled
) {
// open menu on hotkey toggle
const hotkey = toKeyEvent(store().menu_toggle);
let triggered = true;
for (let prop in hotkey)
if (hotkey[prop] !== event[prop]) triggered = false;
if (triggered) electron.ipcRenderer.send('enhancer:open-menu');
}
});
const attempt_interval = setInterval(enhance, 500);
@ -43,7 +47,11 @@ module.exports = (store, __exports) => {
document.body.classList.add('snappy-transitions');
// frameless
if (store().frameless && !store().tiling_mode && !store().tabs) {
if (
store().frameless &&
!store().tiling_mode &&
!(store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled
) {
document.body.classList.add('frameless');
// draggable area
document
@ -56,7 +64,9 @@ module.exports = (store, __exports) => {
}
// window buttons
if (!store().tabs) {
if (
!(store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled
) {
const buttons = require('./buttons.js')(store);
document
.querySelector('.notion-topbar > div[style*="display: flex"]')
@ -153,7 +163,9 @@ module.exports = (store, __exports) => {
'--theme--code_inline-background',
].map((rule) => [rule, getStyle(rule)])
);
if (store().tabs) {
if (
(store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled
) {
electron.ipcRenderer.sendToHost(
'enhancer:set-tab-theme',
[
@ -179,7 +191,7 @@ module.exports = (store, __exports) => {
);
electron.ipcRenderer.on('enhancer:get-menu-theme', setThemeVars);
if (store().tabs) {
if ((store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled) {
let tab_title = '';
__electronApi.setWindowTitle = (title) => {
if (tab_title !== title) {

View File

@ -426,11 +426,13 @@ window['__start'] = async () => {
};
mod.elem = helpers.createElement(`
<section class="${
mod.tags.includes('core') || enabled ? 'enabled' : 'disabled'
mod.id === '0f0bf8b6-eae6-4273-b307-8fc43f2ee082' || enabled
? 'enabled'
: 'disabled'
}" id="${mod.id}">
<div class="meta">
<h3 ${
mod.tags.includes('core')
mod.id === '0f0bf8b6-eae6-4273-b307-8fc43f2ee082'
? `>${mod.name}`
: `class="toggle">
<input type="checkbox" id="enable_${mod.id}"

View File

@ -14,12 +14,6 @@ module.exports = {
version: require('../../package.json').version,
author: 'dragonwocky',
options: [
{
key: 'tabs',
label: 'tabbable windows',
type: 'toggle',
value: false,
},
{
key: 'openhidden',
label: 'hide app on open',
@ -80,6 +74,12 @@ module.exports = {
type: 'input',
value: 'Alt+E',
},
{
key: 'default_page',
label: 'open to default page id:',
type: 'input',
value: '',
},
],
hacks: {
'main/main.js': require('./tray.js'),

View File

@ -18,7 +18,8 @@ const url = require('url'),
koMessages = require(`${__notion}/app/i18n/ko_KR/messages.json`),
schemeHelpers = require(`${__notion}/app/shared/schemeHelpers.js`),
React = require(`${__notion}/app/node_modules/react/index.js`),
ReactDOM = require(`${__notion}/app/node_modules/react-dom/index.js`);
ReactDOM = require(`${__notion}/app/node_modules/react-dom/index.js`),
{ toKeyEvent } = require('keyboardevent-from-electron-accelerator');
const insertCSP = `
const csp = document.createElement('meta');
@ -28,7 +29,7 @@ const insertCSP = `
`;
module.exports = (store, __exports) => {
if (store().tabs) {
if ((store('mods')['e1692c29-475e-437b-b7ff-3eee872e1a42'] || {}).enabled) {
class Index extends React.PureComponent {
constructor() {
super(...arguments);
@ -156,22 +157,44 @@ module.exports = (store, __exports) => {
const list = new Map(this.state.tabs);
while (this.state.tabs.get(id)) id++;
list.delete(id);
if (this.views.html[id]) {
this.views.html[id].style.opacity = '0';
let unhide;
unhide = () => {
this.views.html[id].style.opacity = '';
this.views.html[id].removeEventListener('did-stop-loading', unhide);
};
this.views.html[id].addEventListener('did-stop-loading', unhide);
this.views.html[id].loadURL(this.views.current.$el().src);
}
this.openTab(id, list);
console.log(
store().default_page
? `notion://www.notion.so/${store().default_page}`
: this.views.current.$el().src
);
this.openTab(id, list, true);
}
openTab(id, state = new Map(this.state.tabs)) {
openTab(id, state = new Map(this.state.tabs), load) {
if (!id && id !== 0) return;
this.views.current.id = id;
this.setState({ tabs: state.set(id, true) }, this.focusTab.bind(this));
this.setState({ tabs: state.set(id, true) }, async () => {
this.focusTab();
if (load) {
await new Promise((res, rej) => {
let attempt;
attempt = setInterval(() => {
if (!document.body.contains(this.views.html[id])) return;
clearInterval(attempt);
res();
}, 50);
});
this.views.html[id].style.opacity = '0';
let unhide;
unhide = () => {
this.views.html[id].style.opacity = '';
this.views.html[id].removeEventListener(
'did-stop-loading',
unhide
);
};
this.views.html[id].addEventListener('did-stop-loading', unhide);
this.views.html[id].loadURL(
store().default_page
? `notion://www.notion.so/${store().default_page}`
: this.views.current.$el().src
);
}
});
}
closeTab(id) {
if ((!id && id !== 0) || !this.state.tabs.get(id)) return;
@ -733,16 +756,28 @@ module.exports = (store, __exports) => {
window['__start'] = () => {
document.head.innerHTML += `<link rel="stylesheet" href="${__dirname}/css/tabs.css" />`;
// open menu on hotkey toggle
document.addEventListener('keyup', (event) => {
const hotkey = toKeyEvent(store().menu_toggle);
let triggered = true;
for (let prop in hotkey)
if (hotkey[prop] !== event[prop]) triggered = false;
if (triggered) electron.ipcRenderer.send('enhancer:open-menu');
});
const parsed = url.parse(window.location.href, true),
notionUrl =
parsed.query.path ||
schemeHelpers.getSchemeUrl({
httpUrl: config.default.baseURL,
protocol: config.default.protocol,
});
notionUrl = store().default_page
? `notion://www.notion.so/${store().default_page}`
: parsed.query.path ||
schemeHelpers.getSchemeUrl({
httpUrl: config.default.baseURL,
protocol: config.default.protocol,
});
delete parsed.search;
delete parsed.query;
const plainUrl = url.format(parsed);
const plainUrl = store().default_page
? `notion://www.notion.so/${store().default_page}`
: url.format(parsed);
window.history.replaceState(undefined, undefined, plainUrl);
document.title = localizationHelper
@ -769,6 +804,26 @@ module.exports = (store, __exports) => {
const __start = window['__start'];
window['__start'] = () => {
__start();
if (store().default_page) {
new Promise((res, rej) => {
let attempt;
attempt = setInterval(() => {
if (
!document.getElementById('notion') ||
!document.getElementById('notion').loadURL
)
return;
clearInterval(attempt);
res();
}, 50);
}).then(() => {
document
.getElementById('notion')
.loadURL(`notion://www.notion.so/${store().default_page}`);
});
}
const dragarea = document.querySelector(
'#root [style*="-webkit-app-region: drag"]'
),

19
mods/tabs/mod.js Normal file
View File

@ -0,0 +1,19 @@
/*
* tabs
* (c) 2020 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* under the MIT license
*/
'use strict';
// this is just a pseudo mod to "separate" the options
// from the core module - the core still handles actually
// making it work.
module.exports = {
id: 'e1692c29-475e-437b-b7ff-3eee872e1a42',
tags: ['core', 'extension'],
name: 'tabs',
desc: 'have multiple notion pages open in a single window.',
version: '0.1.0',
author: 'dragonwocky',
};

View File

@ -60,7 +60,7 @@ module.exports = function (__file, __exports) {
...modules.loaded.filter((m) => !m.tags.includes('core')).reverse(),
]) {
if (
(mod.tags || []).includes('core') ||
mod.id === '0f0bf8b6-eae6-4273-b307-8fc43f2ee082' ||
store('mods', { [mod.id]: { enabled: false } })[mod.id].enabled
) {
if (
@ -84,15 +84,16 @@ module.exports = function (__file, __exports) {
});
}
if (mod.hacks && mod.hacks[__file]) {
mod.hacks[__file](
(...args) =>
!args.length
? store(mod.id, mod.defaults)
: args.length === 1 && typeof args[0] === 'object'
? store(mod.id, { ...mod.defaults, ...args[0] })
: store(args[0], { ...mod.defaults, ...args[1] }),
__exports
);
mod.hacks[__file]((...args) => {
if (!args.length) return store(mod.id, mod.defaults);
if (args.length === 1 && typeof args[0] === 'object')
return store(mod.id, { ...mod.defaults, ...args[0] });
const other_mod = modules.loaded.find((m) => m.id === args[0]);
return store(args[0], {
...(other_mod ? other_mod.defaults : {}),
...args[1],
});
}, __exports);
}
}
}