Skip to content

Commit 9730601

Browse files
committed
partial #206
1 parent dc95fcf commit 9730601

File tree

15 files changed

+375
-18
lines changed

15 files changed

+375
-18
lines changed

exampleVault/Replace Buttons.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
### Two buttons that replace the same text
2+
3+
some
4+
text
5+
wow
6+
7+
```meta-bind-button
8+
label: Replace in Note
9+
hidden: false
10+
class: ""
11+
tooltip: ""
12+
id: ""
13+
style: default
14+
action:
15+
type: "replaceInNote"
16+
fromLine: 3
17+
toLine: 5
18+
replacement: "some\ntext\nwow"
19+
```
20+
21+
```meta-bind-button
22+
label: Replace Other in Note
23+
hidden: false
24+
class: ""
25+
tooltip: ""
26+
id: ""
27+
style: default
28+
action:
29+
type: "replaceInNote"
30+
fromLine: 3
31+
toLine: 5
32+
replacement: "other\ntext\ntada"
33+
```
34+
35+
### Inserting text into the file
36+
37+
Insert below this line:
38+
39+
```meta-bind-button
40+
label: Insert Text
41+
hidden: false
42+
class: ""
43+
tooltip: ""
44+
id: ""
45+
style: default
46+
action:
47+
type: "insertIntoNote"
48+
line: 38
49+
value: "i am inserted"
50+
```
51+
52+
```meta-bind-button
53+
label: Remove inserted lines
54+
hidden: false
55+
class: ""
56+
tooltip: ""
57+
id: ""
58+
style: default
59+
action:
60+
type: "regexpReplaceInNote"
61+
regexp: "Insert below this line:\n(i am inserted\n)+"
62+
replacement: "Insert below this line:\n"
63+
```
64+
65+
### A button replacing itself
66+
67+
```meta-bind-button
68+
label: Replace Self
69+
hidden: false
70+
class: ""
71+
tooltip: ""
72+
id: ""
73+
style: default
74+
action:
75+
type: "replaceSelf"
76+
replacement: "i am no longer a button\n\nnice"
77+
```
78+
79+
### A button reconstructing the button above with regexp replacement
80+
81+
```meta-bind-button
82+
label: Recreate Button with Regexp
83+
hidden: false
84+
class: ""
85+
tooltip: ""
86+
id: ""
87+
style: default
88+
action:
89+
type: "regexpReplaceInNote"
90+
regexp: "i am no longer a button\\s+nice\n"
91+
replacement: |
92+
```meta-bind-button
93+
label: Replace Self
94+
hidden: false
95+
class: ""
96+
tooltip: ""
97+
id: ""
98+
style: default
99+
action:
100+
type: "replaceSelf"
101+
replacement: "i am no longer a button\n\nnice"
102+
```
103+
```
104+

packages/core/src/api/API.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ export interface InlineButtonOptions {
7474

7575
export interface ButtonOptions {
7676
declaration: ButtonConfig | string;
77+
position: NotePosition | undefined;
7778
isPreview: boolean;
7879
}
7980

81+
export interface NotePosition {
82+
lineStart: number;
83+
lineEnd: number;
84+
}
85+
8086
export interface EmbedOptions {
8187
depth: number;
8288
content: string;
@@ -344,7 +350,7 @@ export abstract class API<Plugin extends IPlugin> {
344350
declaration = this.buttonParser.validateSimpleButtonConfig(options.declaration);
345351
}
346352

347-
return new ButtonBase(this.plugin, uuid, filePath, declaration, options.isPreview);
353+
return new ButtonBase(this.plugin, uuid, filePath, declaration, options.position, options.isPreview);
348354
}
349355

350356
public createEmbedBase(filePath: string, options: EmbedOptions): EmbedBase {

packages/core/src/api/InternalAPI.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ export abstract class InternalAPI<Plugin extends IPlugin> {
221221
*/
222222
abstract readFilePath(filePath: string): Promise<string>;
223223

224+
abstract writeFilePath(filePath: string, content: string): Promise<void>;
225+
224226
abstract createFile(folderPath: string, fileName: string, extension: string, open?: boolean): Promise<string>;
225227

226228
abstract createContextMenu(items: ContextMenuItemDefinition[]): IContextMenu;

packages/core/src/config/ButtonConfig.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export enum ButtonActionType {
1414
TEMPLATER_CREATE_NOTE = 'templaterCreateNote',
1515
UPDATE_METADATA = 'updateMetadata',
1616
CREATE_NOTE = 'createNote',
17+
REPLACE_IN_NOTE = 'replaceInNote',
18+
REGEXP_REPLACE_IN_NOTE = 'regexpReplaceInNote',
19+
REPLACE_SELF = 'replaceSelf',
20+
INSERT_INTO_NOTE = 'insertIntoNote',
1721
}
1822

1923
export interface CommandButtonAction {
@@ -65,6 +69,30 @@ export interface CreateNoteButtonAction {
6569
openNote?: boolean;
6670
}
6771

72+
export interface ReplaceInNoteButtonAction {
73+
type: ButtonActionType.REPLACE_IN_NOTE;
74+
fromLine: number;
75+
toLine: number;
76+
replacement: string;
77+
}
78+
79+
export interface ReplaceSelfButtonAction {
80+
type: ButtonActionType.REPLACE_SELF;
81+
replacement: string;
82+
}
83+
84+
export interface RegexpReplaceInNoteButtonAction {
85+
type: ButtonActionType.REGEXP_REPLACE_IN_NOTE;
86+
regexp: string;
87+
replacement: string;
88+
}
89+
90+
export interface InsertIntoNoteButtonAction {
91+
type: ButtonActionType.INSERT_INTO_NOTE;
92+
line: number;
93+
value: string;
94+
}
95+
6896
export type ButtonAction =
6997
| CommandButtonAction
7098
| JSButtonAction
@@ -73,7 +101,11 @@ export type ButtonAction =
73101
| SleepButtonAction
74102
| TemplaterCreateNoteButtonAction
75103
| UpdateMetadataButtonAction
76-
| CreateNoteButtonAction;
104+
| CreateNoteButtonAction
105+
| ReplaceInNoteButtonAction
106+
| ReplaceSelfButtonAction
107+
| RegexpReplaceInNoteButtonAction
108+
| InsertIntoNoteButtonAction;
77109

78110
export interface ButtonConfig {
79111
label: string;

packages/core/src/config/ButtonConfigValidators.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import {
77
type CreateNoteButtonAction,
88
type InputButtonAction,
99
type JSButtonAction,
10+
type ReplaceInNoteButtonAction,
1011
type OpenButtonAction,
1112
type SleepButtonAction,
1213
type TemplaterCreateNoteButtonAction,
1314
type UpdateMetadataButtonAction,
15+
type ReplaceSelfButtonAction,
16+
type RegexpReplaceInNoteButtonAction,
17+
type InsertIntoNoteButtonAction,
1418
} from 'packages/core/src/config/ButtonConfig';
1519
import { oneOf, schemaForType } from 'packages/core/src/utils/ZodUtils';
1620
import { z } from 'zod';
@@ -79,6 +83,38 @@ export const V_CreateNoteButtonAction = schemaForType<CreateNoteButtonAction>()(
7983
}),
8084
);
8185

86+
export const V_ReplaceInNoteButtonAction = schemaForType<ReplaceInNoteButtonAction>()(
87+
z.object({
88+
type: z.literal(ButtonActionType.REPLACE_IN_NOTE),
89+
fromLine: z.number(),
90+
toLine: z.number(),
91+
replacement: z.string(),
92+
}),
93+
);
94+
95+
export const V_ReplaceSelfButtonAction = schemaForType<ReplaceSelfButtonAction>()(
96+
z.object({
97+
type: z.literal(ButtonActionType.REPLACE_SELF),
98+
replacement: z.string(),
99+
}),
100+
);
101+
102+
export const V_RegexpReplaceInNoteButtonAction = schemaForType<RegexpReplaceInNoteButtonAction>()(
103+
z.object({
104+
type: z.literal(ButtonActionType.REGEXP_REPLACE_IN_NOTE),
105+
regexp: z.string(),
106+
replacement: z.string(),
107+
}),
108+
);
109+
110+
export const V_InsertIntoNoteButtonAction = schemaForType<InsertIntoNoteButtonAction>()(
111+
z.object({
112+
type: z.literal(ButtonActionType.INSERT_INTO_NOTE),
113+
line: z.number(),
114+
value: z.string(),
115+
}),
116+
);
117+
82118
export const V_ButtonAction = schemaForType<ButtonAction>()(
83119
z.union([
84120
V_CommandButtonAction,
@@ -89,6 +125,10 @@ export const V_ButtonAction = schemaForType<ButtonAction>()(
89125
V_TemplaterCreateNoteButtonAction,
90126
V_UpdateMetadataButtonAction,
91127
V_CreateNoteButtonAction,
128+
V_ReplaceInNoteButtonAction,
129+
V_ReplaceSelfButtonAction,
130+
V_RegexpReplaceInNoteButtonAction,
131+
V_InsertIntoNoteButtonAction,
92132
]),
93133
);
94134

0 commit comments

Comments
 (0)