Skip to content

Commit

Permalink
fix: context menu/shortcut trigger clip #61 #62
Browse files Browse the repository at this point in the history
  • Loading branch information
EINDEX committed Mar 2, 2024
1 parent d8e6a10 commit fb3a7a3
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions src/pages/content/QuickCapture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,53 @@ const logseqCopilotPopupId = 'logseq-copilot-popup';
export const zIndex = '2147483647';
const highlights = CSS.highlights;

const capture = () => {
const selection = getSelection();
if (selection !== null) {
const range = selection.getRangeAt(0);
setHighlight(range);
const clonedSelection = range.cloneContents();
const turndownService = buildTurndownService();
selection.empty();
Browser.runtime.sendMessage({
type: 'clip-with-selection',
data: turndownService.turndown(clonedSelection),
});
} else {
clipPage();
}
};

const clipPage = () => {
Browser.runtime.sendMessage({
type: 'clip-page'
})
};

const setHighlight = (range: Range) => {
if(!highlights.has("copilot-highlight")) {
highlights.set('copilot-highlight', new Highlight())
}
const highlight = highlights.get('copilot-highlight');
highlight.add(range);
}


Browser.runtime.onMessage.addListener((request) => {
if (request.type === 'clip-with-selection' || request.type === 'clip') {
capture();
} else if (request.type === 'clip-page') {
clipPage();
}
});

const QuickCapture = () => {
const [position, setPostion] = useState({
x: 0,
y: 0,
});
const [show, setShow] = useState(false);

const setHighlight = (range: Range) => {
if(!highlights.has("copilot-highlight")) {
highlights.set('copilot-highlight', new Highlight())
}
const highlight = highlights.get('copilot-highlight');
highlight.add(range);
}

const capture = () => {
const selection = getSelection();
if (selection !== null) {
const range = selection.getRangeAt(0);
setHighlight(range);
const clonedSelection = range.cloneContents();
const turndownService = buildTurndownService();
selection.empty();
Browser.runtime.sendMessage({
type: 'clip-with-selection',
data: turndownService.turndown(clonedSelection),
});
} else {
clipPage();
}
};

const clipPage = () => {
Browser.runtime.sendMessage({
type: 'clip-page'
})
};


const clicked = (event: MouseEvent) => {
Expand All @@ -69,13 +79,6 @@ const QuickCapture = () => {
useEffect(() => {
document.addEventListener('mouseup', clicked);
document.addEventListener('mousedown', clicked);
Browser.runtime.onMessage.addListener((request) => {
if (request.type === 'clip-with-selection' || request.type === 'clip') {
capture();
} else if (request.type === 'clip-page') {
clipPage();
}
});
}, []);

const styles = (): React.CSSProperties => {
Expand Down

0 comments on commit fb3a7a3

Please sign in to comment.