@@ -9,43 +9,53 @@ const logseqCopilotPopupId = 'logseq-copilot-popup';
99export const zIndex = '2147483647' ;
1010const highlights = CSS . highlights ;
1111
12+ const capture = ( ) => {
13+ const selection = getSelection ( ) ;
14+ if ( selection !== null ) {
15+ const range = selection . getRangeAt ( 0 ) ;
16+ setHighlight ( range ) ;
17+ const clonedSelection = range . cloneContents ( ) ;
18+ const turndownService = buildTurndownService ( ) ;
19+ selection . empty ( ) ;
20+ Browser . runtime . sendMessage ( {
21+ type : 'clip-with-selection' ,
22+ data : turndownService . turndown ( clonedSelection ) ,
23+ } ) ;
24+ } else {
25+ clipPage ( ) ;
26+ }
27+ } ;
28+
29+ const clipPage = ( ) => {
30+ Browser . runtime . sendMessage ( {
31+ type : 'clip-page'
32+ } )
33+ } ;
34+
35+ const setHighlight = ( range : Range ) => {
36+ if ( ! highlights . has ( "copilot-highlight" ) ) {
37+ highlights . set ( 'copilot-highlight' , new Highlight ( ) )
38+ }
39+ const highlight = highlights . get ( 'copilot-highlight' ) ;
40+ highlight . add ( range ) ;
41+ }
42+
43+
44+ Browser . runtime . onMessage . addListener ( ( request ) => {
45+ if ( request . type === 'clip-with-selection' || request . type === 'clip' ) {
46+ capture ( ) ;
47+ } else if ( request . type === 'clip-page' ) {
48+ clipPage ( ) ;
49+ }
50+ } ) ;
51+
1252const QuickCapture = ( ) => {
1353 const [ position , setPostion ] = useState ( {
1454 x : 0 ,
1555 y : 0 ,
1656 } ) ;
1757 const [ show , setShow ] = useState ( false ) ;
1858
19- const setHighlight = ( range : Range ) => {
20- if ( ! highlights . has ( "copilot-highlight" ) ) {
21- highlights . set ( 'copilot-highlight' , new Highlight ( ) )
22- }
23- const highlight = highlights . get ( 'copilot-highlight' ) ;
24- highlight . add ( range ) ;
25- }
26-
27- const capture = ( ) => {
28- const selection = getSelection ( ) ;
29- if ( selection !== null ) {
30- const range = selection . getRangeAt ( 0 ) ;
31- setHighlight ( range ) ;
32- const clonedSelection = range . cloneContents ( ) ;
33- const turndownService = buildTurndownService ( ) ;
34- selection . empty ( ) ;
35- Browser . runtime . sendMessage ( {
36- type : 'clip-with-selection' ,
37- data : turndownService . turndown ( clonedSelection ) ,
38- } ) ;
39- } else {
40- clipPage ( ) ;
41- }
42- } ;
43-
44- const clipPage = ( ) => {
45- Browser . runtime . sendMessage ( {
46- type : 'clip-page'
47- } )
48- } ;
4959
5060
5161 const clicked = ( event : MouseEvent ) => {
@@ -69,13 +79,6 @@ const QuickCapture = () => {
6979 useEffect ( ( ) => {
7080 document . addEventListener ( 'mouseup' , clicked ) ;
7181 document . addEventListener ( 'mousedown' , clicked ) ;
72- Browser . runtime . onMessage . addListener ( ( request ) => {
73- if ( request . type === 'clip-with-selection' || request . type === 'clip' ) {
74- capture ( ) ;
75- } else if ( request . type === 'clip-page' ) {
76- clipPage ( ) ;
77- }
78- } ) ;
7982 } , [ ] ) ;
8083
8184 const styles = ( ) : React . CSSProperties => {
0 commit comments