-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathButtonConfig.ts
More file actions
261 lines (233 loc) · 6.09 KB
/
ButtonConfig.ts
File metadata and controls
261 lines (233 loc) · 6.09 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import type { LinePosition } from 'packages/core/src/config/APIConfigs';
export enum ButtonStyleType {
/**
* Default grey button
*/
DEFAULT = 'default',
/**
* Primary button in the accent color
*/
PRIMARY = 'primary',
/**
* Red button for destructive actions
*/
DESTRUCTIVE = 'destructive',
/**
* Plain button with no background
*/
PLAIN = 'plain',
}
export enum ButtonActionType {
COMMAND = 'command',
JS = 'js',
OPEN = 'open',
INPUT = 'input',
SLEEP = 'sleep',
TEMPLATER_CREATE_NOTE = 'templaterCreateNote',
RUN_TEMPLATER_FILE = 'runTemplaterFile',
UPDATE_METADATA = 'updateMetadata',
CREATE_NOTE = 'createNote',
REPLACE_IN_NOTE = 'replaceInNote',
REGEXP_REPLACE_IN_NOTE = 'regexpReplaceInNote',
REPLACE_SELF = 'replaceSelf',
INSERT_INTO_NOTE = 'insertIntoNote',
INLINE_JS = 'inlineJS',
}
export enum ButtonPaneType {
NewTab = 'tab',
NewSplit = 'split',
NewWindow = 'window',
}
export interface CommandButtonAction {
type: ButtonActionType.COMMAND;
command: string;
}
export interface JSButtonAction {
type: ButtonActionType.JS;
file: string;
args?: Record<string, unknown>;
}
export interface OpenButtonAction {
type: ButtonActionType.OPEN;
link: string;
panetype?: ButtonPaneType; // "tab" | "split" | "window"
}
export interface InputButtonAction {
type: ButtonActionType.INPUT;
str: string;
}
export interface SleepButtonAction {
type: ButtonActionType.SLEEP;
ms: number;
}
export interface TemplaterCreateNoteButtonAction {
type: ButtonActionType.TEMPLATER_CREATE_NOTE;
templateFile: string;
folderPath?: string;
fileName?: string;
openNote?: boolean;
openIfAlreadyExists?: boolean;
}
export interface RunTemplaterFileButtonAction {
type: ButtonActionType.RUN_TEMPLATER_FILE;
templateFile: string;
}
export interface UpdateMetadataButtonAction {
type: ButtonActionType.UPDATE_METADATA;
bindTarget: string;
evaluate: boolean;
value: string;
}
export interface CreateNoteButtonAction {
type: ButtonActionType.CREATE_NOTE;
folderPath?: string;
fileName: string;
openNote?: boolean;
openIfAlreadyExists?: boolean;
}
export interface ReplaceInNoteButtonAction {
type: ButtonActionType.REPLACE_IN_NOTE;
fromLine: number | string;
toLine: number | string;
replacement: string;
templater?: boolean;
}
export interface ReplaceSelfButtonAction {
type: ButtonActionType.REPLACE_SELF;
replacement: string;
templater?: boolean;
}
export interface RegexpReplaceInNoteButtonAction {
type: ButtonActionType.REGEXP_REPLACE_IN_NOTE;
regexp: string;
regexpFlags?: string;
replacement: string;
}
export interface InsertIntoNoteButtonAction {
type: ButtonActionType.INSERT_INTO_NOTE;
line: number | string;
value: string;
templater?: boolean;
}
export interface InlineJSButtonAction {
type: ButtonActionType.INLINE_JS;
code: string;
args?: Record<string, unknown>;
}
/**
* Maps action types to their respective action interfaces.
*/
export interface ButtonActionMap {
[ButtonActionType.COMMAND]: CommandButtonAction;
[ButtonActionType.JS]: JSButtonAction;
[ButtonActionType.OPEN]: OpenButtonAction;
[ButtonActionType.INPUT]: InputButtonAction;
[ButtonActionType.SLEEP]: SleepButtonAction;
[ButtonActionType.TEMPLATER_CREATE_NOTE]: TemplaterCreateNoteButtonAction;
[ButtonActionType.UPDATE_METADATA]: UpdateMetadataButtonAction;
[ButtonActionType.CREATE_NOTE]: CreateNoteButtonAction;
[ButtonActionType.REPLACE_IN_NOTE]: ReplaceInNoteButtonAction;
[ButtonActionType.REPLACE_SELF]: ReplaceSelfButtonAction;
[ButtonActionType.REGEXP_REPLACE_IN_NOTE]: RegexpReplaceInNoteButtonAction;
[ButtonActionType.INSERT_INTO_NOTE]: InsertIntoNoteButtonAction;
[ButtonActionType.INLINE_JS]: InlineJSButtonAction;
[ButtonActionType.RUN_TEMPLATER_FILE]: RunTemplaterFileButtonAction;
}
export type ButtonAction = ButtonActionMap[ButtonActionType];
export interface ButtonConfig {
/**
* The text displayed on the button
*/
label: string;
/**
* Optional icon to display in front of the label
*/
icon?: string;
/**
* The style of the button
*/
style: ButtonStyleType;
/**
* Optional CSS class to add to the button
*/
class?: string;
/**
* Optional CSS styles to add to the button
*/
cssStyle?: string;
/**
* Optional background image to add to the button,
* needed since you can't load images from the vault via pure CSS
*/
backgroundImage?: string;
/**
* Optional tooltip to display when hovering over the button
*/
tooltip?: string;
/**
* Optional ID for use in inline buttons
*/
id?: string;
/**
* Whether the button is hidden
*/
hidden?: boolean;
/**
* A single action to run when the button is clicked
* Mutually exclusive with `actions`
*/
action?: ButtonAction;
/**
* Multiple actions to run when the button is clicked
* Mutually exclusive with `action`
*/
actions?: ButtonAction[];
}
export interface ButtonContext {
position: LinePosition | undefined;
isInGroup: boolean;
isInline: boolean;
}
/**
* Provides information about the button click event.
*/
export class ButtonClickContext {
type: ButtonClickType;
shiftKey: boolean;
ctrlKey: boolean;
altKey: boolean;
constructor(type: ButtonClickType, shiftKey: boolean, ctrlKey: boolean, altKey: boolean) {
this.type = type;
this.shiftKey = shiftKey;
this.ctrlKey = ctrlKey;
this.altKey = altKey;
}
static fromMouseEvent(event: MouseEvent, type: ButtonClickType): ButtonClickContext {
return new ButtonClickContext(type, event.shiftKey, event.ctrlKey, event.altKey);
}
/**
* Whether the click should cause a link to open in a new tab.
* Only applicable when the click is on a link.
*
* @returns
*/
openInNewTab(): boolean {
return this.type === ButtonClickType.MIDDLE || this.ctrlKey;
}
openInNewWindow(): boolean {
return this.type === ButtonClickType.LEFT && this.ctrlKey && this.altKey && this.shiftKey;
}
openInNewSplit(): boolean {
return this.type === ButtonClickType.MIDDLE && this.ctrlKey && this.altKey;
}
}
export enum ButtonClickType {
/**
* The user used the left mouse button to click the button
*/
LEFT = 'left',
/**
* The user used the middle mouse button (also known as scroll wheel click) to click the button
*/
MIDDLE = 'middle',
}