Skip to content

Commit

Permalink
Add in support for underlining, fix highlighted vs. context, add in t…
Browse files Browse the repository at this point in the history
…est images
  • Loading branch information
dado3212 committed Jul 9, 2024
1 parent 17b2f40 commit bbefec1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 83 deletions.
34 changes: 20 additions & 14 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,18 @@ body {
line-height: 1.3em;
}

.style-0 {
background-color: transparent;
text-decoration: underline 2px red;
}

&.hidden {
display: none;
}

.note {
margin-top: 8px;
font-weight: 500;
}

.info {
Expand Down Expand Up @@ -278,23 +284,23 @@ body {
color: #d6cbb5;
}

&.style-3 mark {
background-color: #82611b;
.style-1 {
background-color: #3c6621;
}

&.style-2 mark {
.style-2 {
background-color: #345190;
}

&.style-1 mark {
background-color: #3c6621;
.style-3 {
background-color: #82611b;
}

&.style-4 mark {
.style-4 {
background-color: #7a3d58;
}

&.style-5 mark {
.style-5 {
background-color: #5e4582;
}

Expand Down Expand Up @@ -369,23 +375,23 @@ body {
.annotation {
border-bottom-color: #eaeaea;

&.style-3 mark {
background-color: #f9df5a;
.style-1 {
background-color: #a6df94;
}

&.style-2 mark {
.style-2 {
background-color: #a0befa;
}

&.style-1 mark {
background-color: #a6df94;
.style-3 {
background-color: #f9df5a;
}

&.style-4 mark {
.style-4 {
background-color: #fda8bd;
}

&.style-5 mark {
.style-5 {
background-color: #cbb9f9;
}

Expand Down
113 changes: 46 additions & 67 deletions js/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function guessName(cfi) {
const regex = /\[([^\]]+)\]/g;
let matches = [];
let match;

while ((match = regex.exec(cfi)) !== null) {
matches.push(match[1]);
}
Expand All @@ -37,6 +37,37 @@ function guessName(cfi) {
}
}

function handleBookmark(bookmark) {
if (!('annotationSelectedText' in bookmark)) {
return;
}
hash = bookmark['annotationAssetID']

if (!(hash in listOfBooks)) {
listOfBooks[hash] = {
'name': hash,
'author': 'Unknown',
'annotations': [],
}
}
// Try and guess the name if it's in the location string
if (listOfBooks[hash]['name'] == hash) {
let possibleName = guessName(bookmark['annotationLocation']);
if (possibleName !== null) {
listOfBooks[hash]['name'] = possibleName;
}
}
listOfBooks[hash]['annotations'].push({
'date': bookmark["annotationCreationDate"],
'selectedText': bookmark['annotationSelectedText'],
'representativeText': ('annotationRepresentativeText' in bookmark) ? bookmark['annotationRepresentativeText'] : null,
'location': bookmark['annotationLocation'],
'style': bookmark['annotationStyle'],
'note': ('annotationNote' in bookmark) ? bookmark['annotationNote'] : null,
'chapter': ('futureProofing5' in bookmark) ? bookmark['futureProofing5'] : null,
});
}

async function startFetch(button) {
let booksData = await ipcRenderer.invoke('read-plist', '/Books/Purchases/Purchases.plist', 'normal');
if (booksData == null) {
Expand Down Expand Up @@ -68,71 +99,14 @@ async function startFetch(button) {
}
}

bookmarks.forEach(bookmark => {
if (!('annotationSelectedText' in bookmark)) {
return;
}
let text = bookmark['annotationSelectedText']
if ('annotationRepresentativeText' in bookmark && bookmark['annotationRepresentativeText'].includes(text)) {
text = bookmark['annotationRepresentativeText']
}
hash = bookmark['annotationAssetID']

if (!(hash in listOfBooks)) {
listOfBooks[hash] = {
'name': hash,
'author': 'Unknown',
'annotations': [],
}
}
// Try and guess the name if it's in the location string
if (listOfBooks[hash]['name'] == hash) {
let possibleName = guessName(bookmark['annotationLocation']);
if (possibleName !== null) {
listOfBooks[hash]['name'] = possibleName;
}
}
listOfBooks[hash]['annotations'].push({
'date': bookmark["annotationCreationDate"],
'text': text,
'location': bookmark['annotationLocation'],
'style': bookmark['annotationStyle'],
'note': ('annotationNote' in bookmark) ? bookmark['annotationNote'] : null,
'chapter': ('futureProofing5' in bookmark) ? bookmark['futureProofing5'] : null,
})
});
bookmarks.forEach(handleBookmark);

// Try and read the older annotations (though we mostly don't have names for these)
let oldData = await ipcRenderer.invoke('read-plist', '/Books/iBooksData2.plist', 'binary');
if (oldData !== null) {
bookmarks = oldData[0]['1.2']['BKBookmark'];

bookmarks.forEach(bookmark => {
if (!('annotationSelectedText' in bookmark)) {
return;
}
let text = bookmark['annotationSelectedText']
if ('annotationRepresentativeText' in bookmark && bookmark['annotationRepresentativeText'].includes(text)) {
text = bookmark['annotationRepresentativeText']
}
hash = bookmark['annotationAssetID']

if (!(hash in listOfBooks)) {
listOfBooks[hash] = {
'name': hash,
'author': 'Unknown',
'annotations': [],
}
}
listOfBooks[hash]['annotations'].push({
'date': bookmark["annotationCreationDate"],
'text': text,
'location': bookmark['annotationLocation'],
'style': bookmark['annotationStyle'],
'note': ('annotationNote' in bookmark) ? bookmark['annotationNote'] : null,
'chapter': ('futureProofing5' in bookmark) ? bookmark['futureProofing5'] : null,
})
});
bookmarks.forEach(handleBookmark);
}

populate(button);
Expand Down Expand Up @@ -208,11 +182,16 @@ async function populate(button) {
booksList.append(bookElement);

listOfBooks[bookHash]['annotations'].forEach(annotation => {
let annotationElement = $(`
<div class="annotation hidden style-${annotation['style']}" data-hash="${bookHash}">
<mark class="text">${annotation['text'].replace("\n", "<br>")}</mark>
</div>
`);
let annotationElement = `<div class="annotation hidden" data-hash="${bookHash}">`;
if (annotation['representativeText'] !== null) {
annotationElement += annotation['representativeText'].replace(
annotation['selectedText'],
`<mark class="style-${annotation['style']}">${annotation['selectedText']}</mark>`
).replace("\n", "<br>");
} else {
annotationElement += `<mark class="style-${annotation['style']}">${annotation['selectedText']}</mark>`.replace("\n", "<br>");
}
annotationElement = $(annotationElement + '</div>');
if (annotation['note'] !== null) {
annotationElement.append($(`<div class="note">${annotation['note']}</div>`));
}
Expand Down Expand Up @@ -295,12 +274,12 @@ $(document).ready(async () => {
searchBar.focus();
searchBar.select();
}
// Hit enter anywhere to index through (or shift+enter to go back)
// Hit enter anywhere to index through (or shift+enter to go back)
} else if (e.keyCode == 13) {
if (currentSearch !== '') {
ipcRenderer.sendSync('find-in-page', currentSearch, { forward: !e.shiftKey, findNext: false, matchCase: false });
}
// Escape to exit the selection
// Escape to exit the selection
} else if (e.code == "Escape") {
searching = false;
document.querySelectorAll('.search span').forEach(a => a.classList.add('hidden'));
Expand Down
2 changes: 0 additions & 2 deletions test-files/files-1/com.apple.ibooks-sync.plist
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@
<string>epubcfi(/6/8[c01]!/4/2[c01]/44/1,:80,:121)</string>
<key>annotationModificationDate</key>
<integer>1720482588</integer>
<key>annotationRepresentativeText</key>
<string>Competition goes against the Mafia ethic. </string>
<key>annotationSelectedText</key>
<string>Competition goes against the Mafia ethic.</string>
<key>annotationStyle</key>
Expand Down
Binary file added test-files/files-1/expected-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-files/files-1/expected-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bbefec1

Please sign in to comment.