Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
highlighting v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fonol committed Apr 4, 2020
1 parent 8bb3d75 commit a4a459c
Show file tree
Hide file tree
Showing 12 changed files with 631 additions and 213 deletions.
6 changes: 6 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ def insert_scripts():
script.src = 'http://127.0.0.1:{port}/_addons/{addon_id}/web/pdfjs/pdf.min.js';
document.body.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://127.0.0.1:{port}/_addons/{addon_id}/web/pdf_highlighting.js';
document.body.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://127.0.0.1:{port}/_addons/{addon_id}/web/pdf_reader.js';
Expand Down
48 changes: 48 additions & 0 deletions src/command_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,54 @@ def expanded_on_bridge_cmd(handled, cmd, self):
notes = get_all_unread_pdf_notes()
index.ui.reading_modal.sidebar.print(notes)


#
# highlights
#

elif cmd.startswith("siac-hl-clicked "):
# highlight btn clicked -> store current highlight color in reading modal
id = int(cmd.split()[1])
color = " ".join(cmd.split()[2:])
index.ui.reading_modal.highlight_color = color
index.ui.reading_modal.highlight_type = id

elif cmd.startswith("siac-pdf-page-loaded "):
# page loaded, so load highlights from db
page = int(cmd.split()[1])
index.ui.reading_modal.show_highlights_for_page(page)

elif cmd.startswith("siac-hl-new "):
# highlights created, save to db
# order is page group type [x0,y0,x1,y1]+ # text
page = int(cmd.split(" ")[1])
group = int(cmd.split(" ")[2])
type = int(cmd.split(" ")[3])
nid = index.ui.reading_modal.note_id
all = []
# [(nid,page,group,type,text,x0,y0,x1,y1)]
text = cmd[cmd.index("#") + 1:]
for ix, i in enumerate(cmd.split(" ")[4:]):
if i == "#":
break
if ix % 4 == 0:
x0 = float(i[:10])
elif ix % 4 == 1:
y0 = float(i[:10])
elif ix % 4 == 2:
x1 = float(i[:10])
else:
y1 = float(i[:10])
all.append((nid,page,group,type,text,x0,y0,x1,y1))
insert_highlights(all)
index.ui.reading_modal.show_highlights_for_page(page)

elif cmd.startswith("siac-hl-del "):
id = int(cmd.split()[1])
delete_highlight(id)



#
# Checkboxes
#
Expand Down
62 changes: 59 additions & 3 deletions src/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def create_db_file_if_not_exists():
else:
conn = sqlite3.connect(file_path)

info_from_data_json = get_notes_info()
if info_from_data_json is not None and "db_last_checked" in info_from_data_json and len(info_from_data_json["db_last_checked"]) > 0:
return
# disable for now
# info_from_data_json = get_notes_info()
# if info_from_data_json is not None and "db_last_checked" in info_from_data_json and len(info_from_data_json["db_last_checked"]) > 0:
# return

conn.execute("""
create table if not exists read
Expand Down Expand Up @@ -119,6 +120,22 @@ def create_db_file_if_not_exists():
created TEXT
);
""")
conn.execute("""
create table if not exists highlights
(
nid INTEGER,
page INTEGER,
type INTEGER,
grouping INTEGER,
x0 REAL,
y0 REAL,
x1 REAL,
y1 REAL,
text TEXT,
data TEXT,
created TEXT
);
""")

# temporary, drop fk
if not _table_exists("_queue_prio_log_old"):
Expand Down Expand Up @@ -548,6 +565,38 @@ def get_read_stats(nid):
conn.close()
return res

#
# highlights
#

def insert_highlights(highlights):
"""
[(nid,page,group,type,text,x0,y0,x1,y1)]
"""
dt = _date_now_str()
highlights = [h + (dt,) for h in highlights]
conn = _get_connection()

res = conn.executemany("insert into highlights(nid, page, grouping, type, text, x0, y0, x1, y1, created) values (?,?,?,?,?,?,?,?,?,?)", highlights)
conn.commit()
conn.close()

def delete_highlight(id):
conn = _get_connection()
conn.execute(f"delete from highlights where rowid = {id}")
conn.commit()
conn.close()

def get_highlights(nid, page):
conn = _get_connection()
res = conn.execute(f"select rowid, * from highlights where nid = {nid} and page = {page}").fetchall()
conn.close()
return res

#
# end highlights
#


def get_all_tags():
"""
Expand Down Expand Up @@ -671,6 +720,7 @@ def delete_note(id):
delete from marks where nid ={id};
delete from notes where id={id};
delete from queue_prio_log where nid={id};
delete from highlights where nid={id};
""")
conn.commit()
conn.close()
Expand Down Expand Up @@ -984,6 +1034,12 @@ def insert_pages_total(nid, pages_total):
conn.commit()
conn.close()


#
# helpers
#


def _to_notes(db_list, pinned=[]):
notes = list()
for tup in db_list:
Expand Down
7 changes: 2 additions & 5 deletions src/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self):

# todo: move to text utils
self.SEP_END = re.compile(r'</div>\u001f$')
self.SOUND_TAG = re.compile(r'sound[a-zA-Z0-9]*mp')
self.EXCLUDE_KEYWORDS = re.compile(r'(?:sound|mp3|c[0-9]+)')

#
# state
Expand Down Expand Up @@ -354,9 +354,6 @@ def _js(self, js, editor):
else:
editor.web.eval(js)




def sortByDate(self, mode):
"""
Rerenders the last search results, but sorted by creation date.
Expand Down Expand Up @@ -500,7 +497,7 @@ def _most_common_words(self, text):
text = utility.text.clean(text, self.stopwords)
counts = {}
for token in text.split():
if token == "" or len(token) == 1 or self.SOUND_TAG.match(token):
if token == "" or len(token) == 1 or self.EXCLUDE_KEYWORDS.match(token):
continue
if token.lower() in counts:
counts[token.lower()][1] += 1
Expand Down
16 changes: 15 additions & 1 deletion src/web/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,18 @@ def get_pdf_viewer_html(nid, source, title):
{search_sources}
</div>
</div>
<div style='position: absolute; left: 0; bottom: 150px; text-align: center;'>
<div style='background: #e65100;' data-id="1" data-color="#e65100" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-color-btn'></div>
<div style='background: #558b2f;' data-id="2" data-color="#558b2f" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-color-btn'></div>
<div style='background: #2196f3;' data-id="3" data-color="#2196f3" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-color-btn'></div>
<div style='background: #ffee58;' data-id="4" data-color="#ffee58" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-color-btn'></div>
<div style='background: #ab47bc;' data-id="5" data-color="#ab47bc" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-color-btn'></div>
<br>
<div style='background: #e65100;' data-id="6" data-color="#e65100" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-ul-btn'></div>
<div style='background: #558b2f;' data-id="7" data-color="#558b2f" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-ul-btn'></div>
<div style='background: #2196f3;' data-id="8" data-color="#2196f3" onclick="Highlighting.onColorBtn(this)" class='siac-pdf-ul-btn'></div>
</div>
<div class='siac-btn siac-btn-dark' id='siac-pdf-search-btn' onclick='$(this).toggleClass("expanded"); onPDFSearchBtnClicked(this);'><img src='{pdf_search_img_src}' style='width: 16px; height: 16px;'/>
<div id='siac-pdf-search-btn-inner' class='expanded-hidden white-hover' style='margin: 0 2px 0 5px; color: lightgrey; text-align: center;'>
<input style='width: 200px; border:none; background-color: #2f2f31; color: lightgrey; padding-left: 2px;' onclick='event.stopPropagation();' onkeyup='onPDFSearchInput(this.value, event);'/>
Expand All @@ -984,7 +996,7 @@ def get_pdf_viewer_html(nid, source, title):
</div>
</div>
<canvas id="siac-pdf-canvas" style='z-index: 99999; display:inline-block;'></canvas>
<div id="text-layer" onmouseup='pdfKeyup();' onclick='if (!window.getSelection().toString().length) {{$("#siac-pdf-tooltip").hide();}}' class="textLayer"></div>
<div id="text-layer" onmouseup='pdfKeyup(event);' onclick='textlayerClicked(event, this);' class="textLayer"></div>
</div>
<iframe id='siac-iframe' sandbox='allow-scripts'></iframe>
<div class='siac-reading-modal-button-bar-wrapper' style="">
Expand Down Expand Up @@ -1034,6 +1046,8 @@ def get_pdf_viewer_html(nid, source, title):
}} else {{
$('#siac-pdf-tooltip-toggle').removeClass('active');
}}
$('.siac-pdf-color-btn[data-id=' + Highlighting.colorSelected.id + ']').addClass('active');
$('.siac-pdf-ul-btn[data-id=' + Highlighting.colorSelected.id + ']').addClass('active');
</script>
""".format_map(dict(nid = nid, pdf_title = title, pdf_path = source, quick_sched_btn=quick_sched, search_sources=search_sources, marks_img_src=marks_img_src, marks_grey_img_src=marks_grey_img_src, pdf_search_img_src=pdf_search_img_src))
return html
Expand Down
32 changes: 26 additions & 6 deletions src/web/reading_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from ..tag_find import get_most_active_tags
from ..state import get_index, check_index, set_deck_map
from ..notes import get_note, _get_priority_list, get_all_tags, get_read_pages, get_pdf_marks, insert_pages_total, get_read_today_count
from ..notes import *
from .html import *
from .note_templates import *
from ..internals import js, requires_index_loaded, perf_time
Expand All @@ -48,6 +48,9 @@ def __init__(self):
self.note = None
self._editor = None

self.highlight_color = "#e65100"
self.highlight_type = 1

self.sidebar = ReadingModalSidebar()

self.sep_color = mw.addonManager.getConfig(__name__)["styling"]["general"]["windowColumnSeparatorColor"]
Expand Down Expand Up @@ -564,15 +567,32 @@ def jump_to_first_unread_page(self, nid):
return """
if (pdfDisplayed) {
for (var i = 1; i < pdfDisplayed.numPages + 1; i++) {
if (!pagesRead || pagesRead.indexOf(i) === -1) {
pdfDisplayedCurrentPage = i;
rerenderPDFPage(pdfDisplayedCurrentPage, false, true);
break;
}
if (!pagesRead || pagesRead.indexOf(i) === -1) {
pdfDisplayedCurrentPage = i;
rerenderPDFPage(pdfDisplayedCurrentPage, false, true);
break;
}
}
}
"""

#
# highlights
#

def show_highlights_for_page(self, page):
highlights = get_highlights(self.note_id, page)
if highlights is not None and len(highlights) > 0:
js = ""
for h in highlights:
# [(rowid, nid, page, type, grouping, x0, y0, x1, y1, text, data, created)]
js = f"{js},[{h[5]},{h[6]},{h[7]},{h[8]},{h[3]},{h[0]}]"
js = js[1:]
print(js)
self._editor.web.eval("Highlighting.current = [%s]; Highlighting.displayHighlights();" % js)





class ReadingModalSidebar():
Expand Down
8 changes: 4 additions & 4 deletions user_files/data.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"index": {
"timestamp": "31/03/2020 14:09:53",
"size": 63506,
"timestamp": "02/04/2020 08:58:06",
"size": 11124,
"decks": [],
"shouldRebuild": false,
"lastInsertedId": -1,
"fieldsToExclude": {},
"stopwordsSize": 123,
"type": "SQLite FTS4"
"type": "SQLite FTS5"
},
"synsets": [],
"notes": {
"db_last_checked": "2020-03-14-07-03-05"
"db_last_checked": "2020-04-04-17-07-52"
}
}
Loading

0 comments on commit a4a459c

Please sign in to comment.