/* * notion-enhancer: global block links * (c) 2021 admiraldus (https://github.com/admiraldus) * (c) 2021 dragonwocky (https://dragonwocky.me/) * (https://notion-enhancer.github.io/) under the MIT license */ export default async function ({ web, components, notion }, db) { const topbarShareSelector = '.notion-topbar-share-menu', blockActionSelector = '.notion-overlay-container .notion-scroller.vertical .notion-focusable > div > div > [style*="text-overflow: ellipsis;"]', hoveredActionSelector = '.notion-overlay-container .notion-scroller.vertical .notion-focusable[style*="background:"]', topbarCopyClass = 'global_block_links--topbar_copy', blockCopyClass = 'global_block_links--block_copy', hiddenClass = 'global_block_links--hidden'; const topbarCopyIcon = await db.get(['topbar_copy_icon']), topbarCopyText = await db.get(['topbar_copy_text']); if (topbarCopyIcon || topbarCopyText) { const $topbarCopyTemplate = web.render( web.html`
`, topbarCopyIcon ? web.html` ` : '', topbarCopyText ? web.html` Copy link Link copied! ` : '' ); const insertTopbarCopy = () => { const $btns = document.querySelectorAll(topbarShareSelector); $btns.forEach(($btn) => { if (!$btn.previousElementSibling?.classList?.contains?.(topbarCopyClass)) { const $copy = $topbarCopyTemplate.cloneNode(true); components.tooltip($copy, '**Copy page link**'); $btn.before($copy); let resetButtonDelay; $copy.addEventListener('click', () => { if (topbarCopyText) { const $copyText = $copy.querySelector('[data-copy]'), $copiedText = $copy.querySelector('[data-copied]'); $copyText.classList.add(hiddenClass); $copiedText.classList.remove(hiddenClass); clearTimeout(resetButtonDelay); resetButtonDelay = setTimeout(() => { $copyText.classList.remove(hiddenClass); $copiedText.classList.add(hiddenClass); }, 1250); } web.copyToClipboard(`https://notion.so/${notion.getPageID().replace(/-/g, '')}`); }); } }); }; insertTopbarCopy(); web.addDocumentObserver(insertTopbarCopy, [topbarShareSelector]); } const $blockCopyTemplate = web.html`
${await components.feather('globe')} Global link
`; const getLinkButtons = () => [...document.querySelectorAll(blockActionSelector)] .filter(($action) => ['Copy link', '링크 복사', 'リンクをコピー'].includes($action.textContent) ) .map(($action) => $action.closest('.notion-focusable')), insertBlockCopy = () => { const $btns = getLinkButtons(); $btns.forEach(($btn) => { if (!$btn.previousElementSibling?.classList?.contains?.(blockCopyClass)) { const $copy = $blockCopyTemplate.cloneNode(true); $btn.before($copy); $copy.addEventListener('mouseover', () => { document.querySelectorAll(hoveredActionSelector).forEach(($action) => { $action.style.background = ''; }); }); $copy.addEventListener('click', async () => { $btn.click(); const link = await web.readFromClipboard(), id = link.replace(/.+#(?=\w+)/, ''); web.copyToClipboard(id.length === 32 ? `https://notion.so/${id}` : link); }); } }); }; insertBlockCopy(); web.addDocumentObserver(insertBlockCopy, [blockActionSelector]); }