11import { buildTurndownService } from '@/utils' ;
2- import { debounce } from '@/utils' ;
32import { useEffect , useState } from 'react' ;
43import { createRoot } from 'react-dom/client' ;
54import Browser from 'webextension-polyfill' ;
@@ -9,68 +8,65 @@ import scssStyles from './index.module.scss';
98const logseqCopilotPopupId = 'logseq-copilot-popup' ;
109export const zIndex = '2147483647' ;
1110
12- const capture = ( ) => {
13- const selection = getSelection ( ) ;
14- if ( selection !== null ) {
15- const range = selection ! . getRangeAt ( 0 ) ;
16- const clonedSelection = range . cloneContents ( ) ;
17- const turndownService = buildTurndownService ( ) ;
18- selection ?. removeAllRanges ( ) ;
19- Browser . runtime . sendMessage ( {
20- type : 'clip-with-selection' ,
21- data : turndownService . turndown ( clonedSelection ) ,
22- } ) ;
23- } else {
24- clipPage ( ) ;
25- }
26- } ;
27-
28- const clipPage = ( ) => {
29- Browser . runtime . sendMessage ( {
30- type : 'clip-page'
31- } )
32- } ;
33-
34- Browser . runtime . onMessage . addListener ( ( request ) => {
35- if ( request . type === 'clip-with-selection' || request . type === 'clip' ) {
36- capture ( ) ;
37- } else if ( request . type === 'clip-page' ) {
38- clipPage ( ) ;
39- }
40- } ) ;
41-
4211const QuickCapture = ( ) => {
4312 const [ position , setPostion ] = useState ( {
4413 x : 0 ,
4514 y : 0 ,
4615 } ) ;
4716 const [ show , setShow ] = useState ( false ) ;
4817
49- const clicked = ( event : MouseEvent ) => {
50- if ( show ) {
51- return ;
52- }
18+ const capture = ( ) => {
5319 const selection = getSelection ( ) ;
54- if ( selection && selection . toString ( ) . trim ( ) !== '' ) {
55- setShow ( true ) ;
56- setPostion ( { x : event . pageX + 10 , y : event . pageY + 10 } ) ;
57- console . log ( 'clicked' ) ;
20+ if ( selection !== null ) {
21+ const range = selection . getRangeAt ( 0 ) ;
22+ const clonedSelection = range . cloneContents ( ) ;
23+ const turndownService = buildTurndownService ( ) ;
24+ selection . empty ( ) ;
25+ Browser . runtime . sendMessage ( {
26+ type : 'clip-with-selection' ,
27+ data : turndownService . turndown ( clonedSelection ) ,
28+ } ) ;
29+ } else {
30+ clipPage ( ) ;
5831 }
5932 } ;
6033
61- const release = ( event : MouseEvent ) => {
62- setShow ( false ) ;
34+ const clipPage = ( ) => {
35+ Browser . runtime . sendMessage ( {
36+ type : 'clip-page'
37+ } )
6338 } ;
6439
65- const quickCapture = ( ) => {
66- setShow ( false ) ;
67- capture ( ) ;
40+
41+ const clicked = ( event : MouseEvent ) => {
42+ const selection = getSelection ( ) ;
43+ const haveSelection = selection && selection . toString ( ) . trim ( ) . length > 0 ;
44+ const isButton = event . target && event . target . className && event . target . className . includes ( "quickCapture" )
45+ if ( isButton ) {
46+ if ( event . type === "mouseup" ) {
47+ setShow ( false ) ;
48+ }
49+ return ;
50+ }
51+ if ( haveSelection && event . type === "mouseup" ) {
52+ setShow ( true ) ;
53+ setPostion ( { x : event . pageX + 10 , y : event . pageY + 10 } ) ;
54+ } else {
55+ setShow ( false )
56+ }
6857 } ;
6958
7059 useEffect ( ( ) => {
71- document . addEventListener ( 'mouseup' , debounce ( clicked , 100 ) ) ;
72- document . addEventListener ( 'mousedown' , debounce ( release , 100 ) ) ;
73- } ) ;
60+ document . addEventListener ( 'mouseup' , clicked ) ;
61+ document . addEventListener ( 'mousedown' , clicked ) ;
62+ Browser . runtime . onMessage . addListener ( ( request ) => {
63+ if ( request . type === 'clip-with-selection' || request . type === 'clip' ) {
64+ capture ( ) ;
65+ } else if ( request . type === 'clip-page' ) {
66+ clipPage ( ) ;
67+ }
68+ } ) ;
69+ } , [ ] ) ;
7470
7571 const styles = ( ) : React . CSSProperties => {
7672 return {
@@ -86,9 +82,9 @@ const QuickCapture = () => {
8682 return (
8783 < div className = { scssStyles . popupButton } style = { styles ( ) } >
8884 < img
89- className = { scssStyles . popupButton }
85+ className = { ` ${ scssStyles . popupButton } quickCapture` }
9086 src = { logo }
91- onClick = { quickCapture }
87+ onClick = { capture }
9288 alt = { 'clip-button' }
9389 />
9490 </ div >
0 commit comments