notion-enhancer/repo/always-on-top/button.mjs

46 lines
4.2 KiB
JavaScript

/**
* notion-enhancer: always on top
* (c) 2021 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
export const createButton = async ({ electron, web, components }, db) => {
let pinIcon =
(await db.get(['pin_icon'])) ||
`<svg viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" clip-rule="evenodd" d="M14.5036 1.13488C14.3765 0.925602 14.2483 0.707495 14.1174 0.479601L14.9536 0C15.6901 1.28291 16.3103 2.18846 17.0583 2.94838C17.8041 3.70619 18.6966 4.33897 20 5.04819L19.5391 5.89451C19.3035 5.76631 19.0792 5.63964 18.865 5.5134L15.0521 9.80084C15.4553 11.0798 15.495 12.4878 15.0464 13.8871C14.727 14.8833 14.1631 15.8645 13.3206 16.7832C13.1407 16.9793 12.8358 16.9926 12.6396 16.8128C12.6328 16.8065 12.6262 16.8001 12.6198 16.7936C12.6184 16.7923 12.6171 16.7909 12.6158 16.7896L12.6157 16.7896L8.83994 13.0156L0.819221 19.8752C0.357424 20.2701 -0.27004 19.6423 0.124825 19.1811L6.98768 11.1642L3.21189 7.39021L3.21188 7.39019C3.21074 7.38905 3.2096 7.38791 3.20847 7.38676C3.19722 7.37583 3.18639 7.36425 3.17604 7.35202C3.00408 7.1489 3.02943 6.84489 3.23266 6.67301C3.87295 6.13148 4.53111 5.70968 5.19581 5.39767C6.91511 4.59063 8.64205 4.5275 10.1724 4.98295L14.5036 1.13488ZM15.0286 1.95762L11.1853 5.37229C12.6878 6.08669 13.9221 7.31677 14.6512 8.80124L16.2331 7.02243L14.6084 5.39847C14.4202 5.21031 14.4202 4.90525 14.6084 4.7171C14.7967 4.52895 15.1019 4.52895 15.2901 4.7171L16.8747 6.30098L18.0377 4.99329C17.4012 4.56407 16.861 4.12205 16.371 3.62414C15.8839 3.12926 15.4531 2.58595 15.0286 1.95762ZM4.26612 7.08121L7.97786 10.7912C7.97786 10.7912 7.97787 10.7912 7.97788 10.7912C8.15977 10.973 8.171 11.264 8.00385 11.4593L8.00384 11.4593L4.79816 15.204L8.54467 11.9999C8.54468 11.9999 8.54468 11.9999 8.54468 11.9999C8.74012 11.8328 9.03128 11.8441 9.21312 12.0258L12.9392 15.7502C13.5101 15.0445 13.8964 14.3165 14.1283 13.5931C15.6562 8.82761 10.5772 3.93621 5.60561 6.26987C5.15973 6.47917 4.71155 6.74739 4.26612 7.08121ZM0.193019 19.1425C0.19283 19.1427 0.192641 19.1428 0.192451 19.143L0.499031 19.5011L0.491164 19.4944L0.499031 19.5011L0.505838 19.5091L0.499032 19.5011L0.857328 19.8076C0.85747 19.8074 0.857613 19.8072 0.857755 19.8071L0.857333 19.8076L0.499031 19.5011L0.192456 19.143L0.193019 19.1425ZM0.499031 19.5011H0.499031L0.499031 19.5011H0.499032L0.499031 19.5011Z"/>
</svg>`,
unpinIcon =
(await db.get(['unpin_icon'])) ||
`<svg viewBox="0 0 20 20" fill="currentColor">
<path d="M14.5036 1.13488C14.3765 0.925602 14.2483 0.707495 14.1174 0.479601L14.9536 0C15.6901 1.28291 16.3103 2.18846 17.0583 2.94839C17.8041 3.70619 18.6966 4.33897 20 5.04819L19.5391 5.89451C19.3035 5.76631 19.0792 5.63965 18.865 5.5134L15.0521 9.80084C15.4553 11.0798 15.495 12.4878 15.0464 13.8871C14.727 14.8833 14.1631 15.8645 13.3206 16.7832C13.1407 16.9793 12.8358 16.9926 12.6396 16.8128C12.6328 16.8065 12.6262 16.8001 12.6198 16.7936L12.6158 16.7896L8.83994 13.0156L0.819221 19.8752C0.357424 20.2701 -0.27004 19.6423 0.124825 19.1811L6.98768 11.1642L3.21189 7.39021L3.20847 7.38676C3.19722 7.37583 3.18639 7.36425 3.17604 7.35203C3.00408 7.1489 3.02943 6.84489 3.23266 6.67301C3.87295 6.13148 4.53111 5.70968 5.19581 5.39767C6.91511 4.59063 8.64205 4.5275 10.1724 4.98295L14.5036 1.13488Z"/>
</svg>`;
pinIcon = pinIcon.trim();
unpinIcon = unpinIcon.trim();
pinIcon =
pinIcon.startsWith('<svg') && pinIcon.endsWith('</svg>') ? pinIcon : web.escape(pinIcon);
unpinIcon =
unpinIcon.startsWith('<svg') && unpinIcon.endsWith('</svg>')
? unpinIcon
: web.escape(unpinIcon);
const $button = web.html`<div class="always_on_top--button"></div>`,
$pin = web.html`<button id="always_on_top--pin">${pinIcon}</button>`,
$unpin = web.html`<button id="always_on_top--unpin">${unpinIcon}</button>`;
components.addTooltip($pin, '**Pin window to top**');
components.addTooltip($unpin, '**Unpin window from top**');
web.render($button, $pin);
$pin.addEventListener('click', () => {
$pin.replaceWith($unpin);
electron.browser.setAlwaysOnTop(true);
});
$unpin.addEventListener('click', () => {
$unpin.replaceWith($pin);
electron.browser.setAlwaysOnTop(false);
});
return $button;
};