1+ import { start } from "repl" ;
12import { SETTING } from "./settings" ;
23import { ZoteroItem } from "./zotero-item" ;
34
5+ import { CompletionContext , startCompletion } from "@codemirror/autocomplete" ;
6+
47interface ChangePos {
58 line : any ;
69 ch : any ;
@@ -14,69 +17,34 @@ interface Change {
1417const COMMAND = 'z@' ;
1518
1619export class ZoteroQuery {
17- codeMirror : any ;
18- cm : any ;
19- context : any ;
2020 start : ChangePos ;
2121 change : Change ;
2222 zoteroItems : ZoteroItem [ ] = [ ] ;
2323 filteredItems : ZoteroItem [ ] = [ ] ;
2424 timeout : NodeJS . Timeout | undefined ;
2525
26- constructor ( codeMirror , context , cm ) {
27- this . codeMirror = codeMirror ;
28- this . context = context ;
29- this . cm = cm
30- }
31-
32- getInputReadHandler ( ) {
33- const that = this ;
34- return async function ( cm , change ) {
35- if ( ! cm . state . completionActive && that . cm . getTokenAt ( cm . getCursor ( ) ) . string === COMMAND ) {
36- that . zoteroItems = await that . loadData ( ) ;
37- that . filteredItems = that . zoteroItems ;
38-
39- that . change = change ;
40- that . start = {
41- line : change . from . line ,
42- ch : change . from . ch + 1
43- } ;
44-
45- setTimeout ( ( ) => {
46- that . codeMirror . showHint ( that . cm , that . getPrepareHints ( ) , {
47- completeSingle : false ,
48- closeOnUnfocus : true ,
49- async : true ,
50- closeCharacters : / [ ( ) \[ \] { } ; : > , ] /
51- } ) ;
52- } , 10 ) ;
53- }
54- }
26+ constructor ( private context , private cm ) {
27+ this . loadData ( ) . then ( items => this . zoteroItems = items ) ;
5528 }
5629
57- getPrepareHints ( ) {
30+ getCompletions ( ) {
5831 const that = this ;
59-
60- return async function ( cm , callback ) {
61- const cursor = cm . getCursor ( ) ;
62- let prefix = cm . getRange ( that . start , cursor ) || '' ;
63-
64- const hints = await that . query ( prefix ) ;
65- callback ( {
66- list : hints ,
67- from : {
68- line : that . change . from . line ,
69- ch : that . change . from . ch + 1 ,
70- } ,
71- to : {
72- line : that . change . to . line ,
73- ch : that . change . to . ch + 1 ,
74- } ,
75- } ) ;
32+ return ( context : CompletionContext ) => {
33+ // reload data in background
34+ that . loadData ( ) . then ( items => that . zoteroItems = items ) ;
35+ let word = context . matchBefore ( / \S + / ) ;
36+
37+ if ( ! word || word . from == word . to || ! word . text . startsWith ( 'z@' ) )
38+ return null ;
39+
40+ return {
41+ from : word . from + 2 ,
42+ options : that . zoteroItems . map ( item => item . getHint ( ) )
43+ }
7644 }
77- } ;
45+ }
7846
79- async query ( query : string ) {
47+ query ( query : string ) {
8048 if ( ! this . zoteroItems ) {
8149 return [ ] ;
8250 }
@@ -99,12 +67,10 @@ export class ZoteroQuery {
9967 data = await this . tryZotServer ( settings . port ) ;
10068 }
10169
102- console . log ( data ) ;
103-
10470 if ( ! ! data ) {
10571 data = data . filter ( item => item . itemType !== 'attachment' && item . itemType !== 'note' )
10672 . map ( item => new ZoteroItem ( item ) ) ;
107- console . log ( 'items' , data ) ;
73+
10874 data . sort ( ( a : ZoteroItem , b : ZoteroItem ) => a . title . localeCompare ( b . title ) ) ;
10975 return data ;
11076 } else {
0 commit comments