-
Notifications
You must be signed in to change notification settings - Fork 6
/
__init__.py
119 lines (94 loc) · 3.76 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# https://github.com/sartak/anki-editor-autocomplete
# -*- coding: utf8 -*-
from aqt import mw
from aqt import editor
from aqt.fields import FieldDialog
from anki.hooks import wrap
from anki.utils import splitFields, stripHTMLMedia
from anki.utils import json
import urllib.request, urllib.error, urllib.parse
noAutocompleteFields = [ ]
def mySetup(self, note, hide=True, focus=False):
self.prevAutocomplete = ""
# only initialize the autocompleter on Add Cards not in browser
if self.note and self.addMode:
self.web.eval("""
document.styleSheets[0].addRule('.autocomplete', 'margin: 0.3em 0 1.0em 0; color: blue; text-decoration: underline; cursor: pointer;');
// every second send the current state over
setInterval(function () {
if (currentField) {
var r = {
text: currentField.innerHTML,
};
pycmd("autocomplete:" + JSON.stringify(r));
}
}, 1000);
""")
def myBridge(self, cmd, _old=None):
if cmd.startswith("autocomplete"):
(type, jsonText) = cmd.split(":", 1)
result = json.loads(jsonText)
text = self.mungeHTML(result['text'])
if self.currentField is None:
return
# bail out if the user hasn't actually changed the field
previous = "%d:%s" % (self.currentField, text)
if self.prevAutocomplete == previous:
return
self.prevAutocomplete = previous
if text == "" or len(text) > 500 or self.note is None:
self.web.eval("$('.autocomplete').remove();");
return
field = self.note.model()['flds'][self.currentField]
if field['name'] in noAutocompleteFields:
field['no_autocomplete'] = True
if 'no_autocomplete' in list(field.keys()) and field['no_autocomplete']:
return
# find a value from the same model and field whose
# prefix is what the user typed so far
query = "'note:%s' '%s:%s*'" % (
self.note.model()['name'],
field['name'],
text)
col = self.note.col
res = col.findCards(query, order=True)
if len(res) == 0:
self.web.eval("$('.autocomplete').remove();");
return
# pull out the full value
value = col.getCard(res[0]).note().fields[self.currentField]
escaped = json.dumps(value)
self.web.eval("""
$('.autocomplete').remove();
if (currentField) {
$('<div class="autocomplete">' + %s + '</div>').click({field: currentField}, updateField).insertAfter(currentField)
}
function updateField(event){
currentField = event.data.field;
currentField.innerHTML = %s;
saveField("key");
focusField(currentFieldOrdinal());
caretToEnd();
}
"""
% (escaped, escaped))
else:
_old(self, cmd)
# XXX must figure out how to add noAutocomplete checkbox to form
def myLoadField(self, idx):
fld = self.model['flds'][idx]
f = self.form
if 'no_autocomplete' in list(fld.keys()):
f.noAutocomplete.setChecked(fld['no_autocomplete'])
def mySaveField(self):
# not initialized yet?
if self.currentIdx is None:
return
idx = self.currentIdx
fld = self.model['flds'][idx]
f = self.form
fld['no_autocomplete'] = f.noAutocomplete.isChecked()
editor.Editor.onBridgeCmd = wrap(editor.Editor.onBridgeCmd, myBridge, 'around')
editor.Editor.setNote = wrap(editor.Editor.setNote, mySetup, 'after')
#FieldDialog.loadField = wrap(FieldDialog.loadField, myLoadField, 'after')
#FieldDialog.saveField = wrap(FieldDialog.saveField, mySaveField, 'after')