From 29260d6a00c36ec79f877f30ebe8e209f9ac5192 Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Thu, 18 Jan 2024 22:28:47 +0100 Subject: [PATCH] Add reorderCards property to guiBrowse, add guiSelectNote --- README.md | 40 +++++++++++++++++++++++++++++++++++++++- plugin/__init__.py | 27 ++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c1f1f2c..4a71179 100644 --- a/README.md +++ b/README.md @@ -1226,6 +1226,10 @@ Search parameters are passed to Anki, check the docs for more information: https * Invokes the *Card Browser* dialog and searches for a given query. Returns an array of identifiers of the cards that were found. Query syntax is [documented here](https://docs.ankiweb.net/searching.html). + Optionally, the `reorderCards` property can be provided to reorder the cards shown in the *Card Browser*. + This is an array including the `order` and `columnId` objects. `order` can be either `ascending` or `descending` while `columnId` can be one of several column identifiers (as documented in the [Anki source code](https://github.com/ankitects/anki/blob/main/rslib/src/browser_table.rs)). + The specified column needs to be visible in the *Card Browser*. +
Sample request: @@ -1234,7 +1238,11 @@ Search parameters are passed to Anki, check the docs for more information: https "action": "guiBrowse", "version": 6, "params": { - "query": "deck:current" + "query": "deck:current", + "reorderCards": { + "order": "descending", + "columnId": "noteCrt" + } } } ``` @@ -1251,6 +1259,36 @@ Search parameters are passed to Anki, check the docs for more information: https ```
+#### `guiSelectNote` + +* Finds the open instance of the *Card Browser* dialog and selects a note given a note identifier. + Returns `True` if the *Card Browser* is open, `False` otherwise. + +
+ Sample request: + + ```json + { + "action": "guiSelectNote", + "version": 6, + "params": { + "note": 1494723142483 + } + } + ``` +
+ +
+ Sample result: + + ```json + { + "result": true, + "error": null + } + ``` +
+ #### `guiSelectedNotes` * Finds the open instance of the *Card Browser* dialog and returns an array of identifiers of the notes that are diff --git a/plugin/__init__.py b/plugin/__init__.py index 8f95923..b464921 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1669,7 +1669,7 @@ class AnkiConnect: @util.api() - def guiBrowse(self, query=None): + def guiBrowse(self, query=None, reorderCards=None): browser = aqt.dialogs.open('Browser', self.window()) browser.activateWindow() @@ -1680,6 +1680,23 @@ class AnkiConnect: else: browser.onSearchActivated() + if reorderCards is not None: + if not isinstance(reorderCards, dict): + raise Exception('reorderCards should be a dict: {}'.format(reorderCards)) + if not ('columnId' in reorderCards and 'order' in reorderCards): + raise Exception('Must provide a "columnId" and a "order" property"') + + cardOrder = reorderCards['order'] + if cardOrder not in ('ascending', 'descending'): + raise Exception('invalid card order: {}'.format(reorderCards['order'])) + + cardOrder = Qt.SortOrder.DescendingOrder if cardOrder == 'descending' else Qt.SortOrder.AscendingOrder + columnId = browser.table._model.active_column_index(reorderCards['columnId']) + if columnId == None: + raise Exception('invalid columnId: {}'.format(reorderCards['columnId'])) + + browser.table._on_sort_column_changed(columnId, cardOrder) + return self.findCards(query) @@ -1687,6 +1704,14 @@ class AnkiConnect: def guiEditNote(self, note): Edit.open_dialog_and_show_note_with_id(note) + @util.api() + def guiSelectNote(self, note): + (creator, instance) = aqt.dialogs._dialogs['Browser'] + if instance is None: + return False + instance.table.clear_selection() + instance.table.select_single_card(note) + return True @util.api() def guiSelectedNotes(self):