diff --git a/config.json b/config.json index cbe2bb65..6199ef06 100644 --- a/config.json +++ b/config.json @@ -104,6 +104,15 @@ "notes.sidebar.visible": false, "notes.showSource": true, "searchpane.zoom" : 1.0, -"switchLeftRight": false +"switchLeftRight": false, +"highlighting": true, +"pdf.onOpen.autoFillTagsWithPDFsTags" : true, +"pdf.onOpen.autoFillFieldsWithPDFName": [], +"pdf.import.folders_to_search" : [], +"notes.queue.priorityScaleFactor": 5, +"notes.queue.missedNotesHandling": "remove-schedule", +"notes.queue.scheduleDialogOnDoneUnscheduledNotes": false, +"notes.queue.intervalSchedulesStartToday": true, +"pdf.theme": "pdf_reader.css" -} \ No newline at end of file +} diff --git a/src/__init__.py b/src/__init__.py index 4710e018..0969eb4e 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -35,11 +35,14 @@ import time as t import webbrowser import functools +import typing +from typing import Dict, Any, List, Tuple, Optional, Callable import sys sys.path.insert(0, os.path.dirname(__file__)) import utility.tags import utility.misc +import state from .state import check_index, get_index, corpus_is_loaded, set_corpus, set_edit, get_edit from .index.indexing import build_index, get_notes_in_collection @@ -54,26 +57,20 @@ from .config import get_config_value_or_default from .command_parsing import expanded_on_bridge_cmd, toggleAddon, rerenderNote, rerender_info, add_note_to_index - - - config = mw.addonManager.getConfig(__name__) def init_addon(): + """ Executed once on Anki startup. """ global origEditorContextMenuEvt - # wrap js -> py bridge to include the add-ons commands, see command_parsing.py - Editor.onBridgeCmd = wrap(Editor.onBridgeCmd, expanded_on_bridge_cmd, "around") - #todo: Find out if there is a better moment to start index creation + gui_hooks.webview_did_receive_js_message.append(expanded_on_bridge_cmd) - create_db_file_if_not_exists() + #todo: Find out if there is a better moment to start index creation + state.db_file_existed = create_db_file_if_not_exists() gui_hooks.profile_did_open.append(build_index) gui_hooks.profile_did_open.append(insert_scripts) gui_hooks.profile_did_open.append(recalculate_priority_queue) - #disabled for now, to be able to use Occlude Image in the context menu - #origEditorContextMenuEvt = EditorWebView.contextMenuEvent - #EditorWebView.contextMenuEvent = editorContextMenuEventWrapper if get_config_value_or_default("searchOnTagEntry", True): TagEdit.keyPressEvent = wrap(TagEdit.keyPressEvent, tag_edit_keypress, "around") @@ -81,12 +78,15 @@ def init_addon(): setup_tagedit_timer() # add new notes to search index when adding - AddCards.addNote = wrap(AddCards.addNote, add_note_and_update_index, "around") + gui_hooks.add_cards_did_add_note.append(add_note_to_index) + # update notes in index when changed through the "Edit" button EditDialog.saveAndClose = wrap(EditDialog.saveAndClose, editor_save_with_index_update, "around") # shortcut to toggle add-on pane gui_hooks.editor_did_init_shortcuts.append(add_hide_show_shortcut) + # reset state after the add/edit dialog is opened + gui_hooks.editor_did_init_shortcuts.append(reset_state) # add-on internal hooks setup_hooks() @@ -97,24 +97,14 @@ def init_addon(): document.addEventListener("keydown", function (e) {globalKeydown(e); }, false); """ - typing_delay = max(500, config['delayWhileTyping']) #this inserts all the javascript functions in scripts.js into the editor webview - aqt.editor._html += getScriptPlatformSpecific(typing_delay) + aqt.editor._html += getScriptPlatformSpecific() + #when a note is loaded (i.e. the add cards dialog is opened), we have to insert our html for the search ui gui_hooks.editor_did_load_note.append(on_load_note) - - -def add_note_and_update_index(dialog, note, _old): - """ - Wrapper around the note adding method, to update the index with the new created note. - """ - res = _old(dialog, note) - add_note_to_index(note) - return res - -def editor_save_with_index_update(dialog, _old): +def editor_save_with_index_update(dialog: EditDialog, _old: Callable): _old(dialog) # update index index = get_index() @@ -126,7 +116,7 @@ def editor_save_with_index_update(dialog, _old): index.ui.edited[str(dialog.editor.note.id)] = t.time() -def on_load_note(editor): +def on_load_note(editor: Editor): """ Executed everytime a note is created/loaded in the add cards dialog. Wraps the normal editor html in a flex layout to render a second column for the searching ui. @@ -134,18 +124,26 @@ def on_load_note(editor): #only display in add cards dialog or in the review edit dialog (if enabled) if editor.addMode or (get_config_value_or_default("useInEdit", False) and isinstance(editor.parentWindow, EditCurrent)): - index = get_index() - zoom = get_config_value_or_default("searchpane.zoom", 1.0) - show_tag_info_on_hover = "true" if get_config_value_or_default("showTagInfoOnHover", True) and get_config_value_or_default("noteScale", 1.0) == 1.0 and zoom == 1.0 else "false" + index = get_index() + zoom = get_config_value_or_default("searchpane.zoom", 1.0) + typing_delay = max(500, get_config_value_or_default('delayWhileTyping', 1000)) + show_tag_info_on_hover = "true" if get_config_value_or_default("showTagInfoOnHover", True) and get_config_value_or_default("noteScale", 1.0) == 1.0 and zoom == 1.0 else "false" + editor.web.eval(f""" - var showTagInfoOnHover = {show_tag_info_on_hover}; - tagHoverTimeout = {get_config_value_or_default("tagHoverDelayInMiliSec", 1000)}; + var showTagInfoOnHover = {show_tag_info_on_hover}; + tagHoverTimeout = {get_config_value_or_default("tagHoverDelayInMiliSec", 1000)}; + var delayWhileTyping = {typing_delay}; """) def cb(was_already_rendered): if was_already_rendered: return + + if index is not None and index.ui is not None: + index.ui.set_editor(editor) + index.ui._loadPlotJsIfNotLoaded() + if index is not None: setup_ui_after_index_built(editor, index) @@ -153,47 +151,36 @@ def cb(was_already_rendered): fillDeckSelect(editor) if index is not None and index.lastSearch is None: - printStartingInfo(editor) + print_starting_info(editor) if not corpus_is_loaded(): corpus = get_notes_in_collection() set_corpus(corpus) - if index is not None and index.ui is not None: - index.ui.set_editor(editor) - index.ui._loadPlotJsIfNotLoaded() - - # render the right side (search area) of the editor # (the script checks if it has been rendered already) editor.web.evalWithCallback(right_side_html(index is not None), cb) - if get_edit() is None and editor is not None: set_edit(editor) +def on_add_cards_init(add_cards: AddCards): + if get_index() is not None and add_cards.editor is not None: + get_index().ui.set_editor(add_cards.editor) -def editorContextMenuEventWrapper(view, evt): - global contextEvt - win = aqt.mw.app.activeWindow() - if isinstance(win, Browser): - origEditorContextMenuEvt(view, evt) - return - contextEvt = evt - pos = evt.pos() - determineClickTarget(pos) - #origEditorContextMenuEvt(view, evt) - def insert_scripts(): """ Expose the scripts on the internal web server. styles.css and pdf_reader.css are not included that way, because they are processed ($$ placeholders are replaced) and inserted via