|
| 1 | +import _ from 'lodash'; |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { createRoot } from 'react-dom/client'; |
| 4 | +import TurndownService from 'turndown'; |
| 5 | +import Browser from 'webextension-polyfill'; |
| 6 | +import logo from '../../assets/img/logo.png'; |
| 7 | +import scssStyles from './index.module.scss'; |
| 8 | + |
| 9 | +const logseqCopilotPopupId = 'logseq-copilot-popup'; |
| 10 | +export const zIndex = '2147483647'; |
| 11 | + |
| 12 | +const connect = Browser.runtime.connect(); |
| 13 | + |
| 14 | +const QuickCapture = () => { |
| 15 | + const [position, setPostion] = useState({ |
| 16 | + x: 0, |
| 17 | + y: 0, |
| 18 | + }); |
| 19 | + const [show, setShow] = useState(false); |
| 20 | + |
| 21 | + const clicked = (event: MouseEvent) => { |
| 22 | + if (show) { |
| 23 | + return; |
| 24 | + } |
| 25 | + const selection = getSelection(); |
| 26 | + if (selection && selection.toString().trim() !== '') { |
| 27 | + setShow(true); |
| 28 | + setPostion({ x: event.pageX + 10, y: event.pageY + 10 }); |
| 29 | + console.log('clicked'); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const release = (event: MouseEvent) => { |
| 34 | + setShow(false); |
| 35 | + }; |
| 36 | + |
| 37 | + // const capture = async (data: string) => { |
| 38 | + // const tab = await Browser.tabs.query({ active: true, currentWindow: true }); |
| 39 | + // if (!tab) return; |
| 40 | + // const activeTab = tab[0]; |
| 41 | + // window.open( |
| 42 | + // `logseq://x-callback-url/quickCapture?title=${ |
| 43 | + // activeTab.title |
| 44 | + // }&url=${encodeURIComponent(activeTab.url)}&content=${data}`, |
| 45 | + // ); |
| 46 | + // }; |
| 47 | + |
| 48 | + const quickCapture = () => { |
| 49 | + setShow(false); |
| 50 | + const selection = getSelection(); |
| 51 | + const range = selection!.getRangeAt(0); |
| 52 | + const clonedSelection = range.cloneContents(); |
| 53 | + const turndownService = new TurndownService(); |
| 54 | + selection?.removeAllRanges(); |
| 55 | + connect.postMessage({ |
| 56 | + type: 'quick-capture', |
| 57 | + data: turndownService.turndown(clonedSelection), |
| 58 | + }); |
| 59 | + console.log('quick-capture'); |
| 60 | + }; |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + document.addEventListener('mouseup', _.debounce(clicked, 100)); |
| 64 | + document.addEventListener('mousedown', _.debounce(release, 100)); |
| 65 | + }); |
| 66 | + |
| 67 | + const styles = (): React.CSSProperties => { |
| 68 | + return { |
| 69 | + display: show ? 'block' : 'none', |
| 70 | + position: 'absolute', |
| 71 | + left: `${position.x}px`, |
| 72 | + top: `${position.y}px`, |
| 73 | + zIndex: zIndex, |
| 74 | + background: '#fff', |
| 75 | + }; |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className={scssStyles.popupButton} style={styles()}> |
| 80 | + <img |
| 81 | + className={scssStyles.popupButton} |
| 82 | + src={logo} |
| 83 | + onClick={quickCapture} |
| 84 | + alt={'logseq-quickcapture-button'} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + ); |
| 88 | +}; |
| 89 | + |
| 90 | +const mountQuickCapture = () => { |
| 91 | + const contrainer = document.createElement('div'); |
| 92 | + contrainer.id = logseqCopilotPopupId; |
| 93 | + document.getElementsByTagName('body')[0].appendChild(contrainer); |
| 94 | + const root = createRoot(contrainer); |
| 95 | + |
| 96 | + root.render(<QuickCapture />); |
| 97 | +}; |
| 98 | + |
| 99 | +export default mountQuickCapture; |
0 commit comments