Add reorderCards property to guiBrowse, add guiSelectNote

This commit is contained in:
AuroraWright 2024-01-18 22:28:47 +01:00 committed by Alex Yatskov
parent a17c4e42da
commit 29260d6a00
2 changed files with 65 additions and 2 deletions

View File

@ -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*.
<details>
<summary><i>Sample request:</i></summary>
@ -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
```
</details>
#### `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.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "guiSelectNote",
"version": 6,
"params": {
"note": 1494723142483
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": true,
"error": null
}
```
</details>
#### `guiSelectedNotes`
* Finds the open instance of the *Card Browser* dialog and returns an array of identifiers of the notes that are

View File

@ -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):