Skip to content

Commit

Permalink
Ensure entire text range gets sent to Anki card when selecting multiple
Browse files Browse the repository at this point in the history
captions
  • Loading branch information
soamsy committed Feb 3, 2021
1 parent 6c5a0d2 commit 4e49aee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions extension/fg/card_creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,31 @@ class CardCreator {
if (this.isRunning)
return;
var captionElement = document.getElementById(captionId);
var captionText = captionElement.textContent;
const videoElement = document.getElementById('ab-video-element');
const currentTime = videoElement ? videoElement.currentTime : 0;
const metaData = document.getElementById('ab-meta-data');
const audioTrack = metaData ? metaData.getAttribute('data-audio-track') : 0;
const [startNode, endNode] = this.findStartEndElements(captionElement);
this.recordCard(startNode, endNode, captionText, captionId, currentTime, audioTrack);
const [startNode, endNode, text] = this.findElementRange(captionElement);
this.recordCard(startNode, endNode, text, captionId, currentTime, audioTrack);
}

findStartEndElements(clickedCaption) {
findElementRange(clickedCaption) {
const selection = window.getSelection();
if (!selection || !selection.toString())
return [clickedCaption, clickedCaption];
return [clickedCaption, clickedCaption, clickedCaption.textContent];

const [startNode, endNode] = this.captionUtils.getStartEnd(selection);
if (!startNode || !endNode)
return [clickedCaption, clickedCaption];
return [clickedCaption, clickedCaption, clickedCaption.textContent];

const isTimeNode = n => n && n.hasAttribute && n.hasAttribute("data-start") && n.hasAttribute("data-end");
const parentStart = this.captionUtils.findParentMatchingCondition(startNode.parentElement, isTimeNode);
const parentEnd = this.captionUtils.findParentMatchingCondition(endNode.parentElement, isTimeNode);

if (!parentStart || !parentEnd)
return [clickedCaption, clickedCaption];
return [clickedCaption, clickedCaption, clickedCaption.textContent];

return [parentStart, parentEnd];
return [parentStart, parentEnd, selection.toString()];
}

recordCard(startElement, endElement, text, captionId, currentVideoTime, audioTrack) {
Expand Down
2 changes: 1 addition & 1 deletion extension/fg/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function handleKeyDown(e, cardCreator) {
if (!caption || !caption.getAttribute('data-caption-id'))
return;

const [start, end] = cardCreator.findStartEndElements(caption);
const [start, end, text] = cardCreator.findElementRange(caption);
cardCreator.addCard(start.getAttribute('data-caption-id'));
break;
default:
Expand Down

0 comments on commit 4e49aee

Please sign in to comment.