@@ -19,7 +19,7 @@ async function initUI() {
19
19
const renderLock = Zotero . Promise . defer ( ) ;
20
20
if ( ! isWindowAlive ( addon . data . prefs . window ) ) return ;
21
21
addon . data . prefs . tableHelper = new ztoolkit . VirtualizedTable (
22
- addon . data . prefs . window ! ,
22
+ addon . data . prefs . window !
23
23
)
24
24
. setContainerId ( `${ config . addonRef } -table-container` )
25
25
. setProp ( {
@@ -34,19 +34,20 @@ async function initUI() {
34
34
. setProp ( "getRowData" , getRowData as any )
35
35
// Update selected key when selection changes
36
36
. setProp ( "onSelectionChange" , ( selection ) => {
37
- const switchButtons =
38
- addon . data . prefs . window ?. document . querySelectorAll ( ".action-selection" ) ;
39
- for ( let i = 0 ; i < addon . data . actions . cachedKeys . length ; i ++ ) {
40
- if ( selection . isSelected ( i ) ) {
41
- addon . data . actions . selectedKey = addon . data . actions . cachedKeys [ i ] ;
42
- switchButtons &&
43
- switchButtons . forEach ( ( e ) => e . removeAttribute ( "disabled" ) ) ;
44
- return ;
45
- }
46
- }
47
- addon . data . actions . selectedKey = undefined ;
48
- switchButtons &&
49
- switchButtons . forEach ( ( e ) => e . setAttribute ( "disabled" , "true" ) ) ;
37
+ const selectedKeys = getSelection ( ) ;
38
+ addon . data . actions . selectedKey = selectedKeys [ 0 ] ;
39
+
40
+ addon . data . prefs . window ?. document
41
+ . querySelectorAll ( ".action-selection" )
42
+ ?. forEach ( ( e ) =>
43
+ setButtonDisabled ( e as XUL . Button , selectedKeys . length === 0 )
44
+ ) ;
45
+
46
+ addon . data . prefs . window ?. document
47
+ . querySelectorAll ( ".action-selection-single" )
48
+ ?. forEach ( ( e ) =>
49
+ setButtonDisabled ( e as XUL . Button , selectedKeys . length !== 1 )
50
+ ) ;
50
51
} )
51
52
// When pressing delete, delete selected line and refresh table.
52
53
// Returning false to prevent default event.
@@ -96,7 +97,7 @@ function initEvents() {
96
97
. querySelector ( `#${ config . addonRef } -action-add` )
97
98
?. addEventListener ( "command" , ( e ) => {
98
99
const key = addon . api . actionManager . updateAction (
99
- Object . assign ( { } , emptyAction ) ,
100
+ Object . assign ( { } , emptyAction )
100
101
) ;
101
102
updateUI ( ) ;
102
103
editAndUpdate ( key ) ;
@@ -117,6 +118,32 @@ function initEvents() {
117
118
await editAndUpdate ( ) ;
118
119
} ) ;
119
120
121
+ doc
122
+ . querySelector ( `#${ config . addonRef } -action-duplicate` )
123
+ ?. addEventListener ( "command" , async ( e ) => {
124
+ const newAction = Object . assign (
125
+ { } ,
126
+ addon . data . actions . map . get ( addon . data . actions . selectedKey ! )
127
+ ) ;
128
+ if ( newAction . name ) {
129
+ // Add (1) (2) ... to the end of the name
130
+ if ( newAction . name . match ( / \( \d + \) $ / ) ) {
131
+ newAction . name = newAction . name . replace (
132
+ / \( ( \d + ) \) $ / ,
133
+ ( _ , num ) => `(${ parseInt ( num ) + 1 } )`
134
+ ) ;
135
+ } else {
136
+ newAction . name += " (1)" ;
137
+ }
138
+ } else {
139
+ // Use time as name
140
+ newAction . name = new Date ( ) . toLocaleString ( ) ;
141
+ }
142
+ const key = addon . api . actionManager . updateAction ( newAction ) ;
143
+ updateUI ( ) ;
144
+ await editAndUpdate ( key ) ;
145
+ } ) ;
146
+
120
147
doc
121
148
. querySelector ( `#${ config . addonRef } -action-export` )
122
149
?. addEventListener ( "command" , async ( e ) => {
@@ -154,7 +181,7 @@ function getRowData(index: number) {
154
181
return {
155
182
event : getString ( `prefs-action-event-${ ActionEventTypes [ action . event ] } ` ) ,
156
183
operation : getString (
157
- `prefs-action-operation-${ ActionOperationTypes [ action . operation ] } ` ,
184
+ `prefs-action-operation-${ ActionOperationTypes [ action . operation ] } `
158
185
) ,
159
186
data : action . data ,
160
187
shortcut : action . shortcut ,
@@ -172,3 +199,11 @@ function getSelection() {
172
199
const keys = addon . data . actions . cachedKeys ;
173
200
return Array . from ( indices ) . map ( ( i ) => keys [ i ] ) ;
174
201
}
202
+
203
+ function setButtonDisabled ( button : XUL . Button , disabled : boolean = true ) {
204
+ if ( disabled ) {
205
+ button . setAttribute ( "disabled" , "true" ) ;
206
+ } else {
207
+ button . removeAttribute ( "disabled" ) ;
208
+ }
209
+ }
0 commit comments