Skip to content

Commit 8525fcd

Browse files
committed
Add a set of functions to handle user interactions
1 parent cf91f9a commit 8525fcd

File tree

3 files changed

+132
-11
lines changed

3 files changed

+132
-11
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Add `markedit-api` to your (TypeScript) project's devDependencies:
1818
```json
1919
{
2020
"devDependencies": {
21-
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.5.0"
21+
"markedit-api": "https://github.com/MarkEdit-app/MarkEdit-api#v0.6.0"
2222
}
2323
}
2424
```
@@ -42,13 +42,21 @@ interface MarkEdit {
4242
// Lezer modules used by MarkEdit.
4343
lezer: { common, highlight, markdown, lr };
4444
// Get notified when the editor is initialized.
45-
onEditorReady: (listener: (editorView: EditorView) => void) => void;
45+
onEditorReady(listener: (editorView: EditorView) => void): void;
4646
// Add an extension to MarkEdit.
47-
addExtension: (extension: Extension) => void;
47+
addExtension(extension: Extension): void;
4848
// Add a Markdown config to MarkEdit.
49-
addMarkdownConfig: (config: MarkdownConfig) => void;
49+
addMarkdownConfig(config: MarkdownConfig): void;
5050
// Add a language to be highlighted (in code blocks) to MarkEdit.
51-
addCodeLanguage: (language: LanguageDescription) => void;
51+
addCodeLanguage(language: LanguageDescription): void;
52+
// Add a menu item to the status bar.
53+
addMainMenuItem(item: MenuItem | MenuItem[]): void;
54+
// Present a contextual menu to receive user input.
55+
showContextMenu(items: MenuItem[], location?: Point): void;
56+
// Present an alert to receive user input.
57+
showAlert(alert: Alert): Promise<number>;
58+
// Present a text box to receive user input.
59+
showTextBox(textBox?: TextBox): Promise<string | undefined>;
5260
}
5361
```
5462

@@ -81,6 +89,14 @@ MarkEdit.addCodeLanguage(language);
8189

8290
> While extensions and configs can theoretically be added at any time, it is recommended that they be added immediately after loading the script.
8391
92+
## Handling User Input
93+
94+
While you can certainly build user interfaces with JavaScript and CSS, leveraging native UI components might be a better option.
95+
96+
To create UI entries for your features, you can use the [addMainMenuItem](https://github.com/search?q=repo%3AMarkEdit-app%2FMarkEdit-api+addMainMenuItem&type=code) function, which adds an item to the "Extensions" submenu of the main menu, with keyboard shortcuts support.
97+
98+
To request user input, try using [showContextMenu](https://github.com/search?q=repo%3AMarkEdit-app%2FMarkEdit-api+showContextMenu&type=code), [showAlert](https://github.com/search?q=repo%3AMarkEdit-app%2FMarkEdit-api+showAlert&type=code), and [showTextBox](https://github.com/search?q=repo%3AMarkEdit-app%2FMarkEdit-api+showTextBox&type=code).
99+
84100
## Building
85101

86102
In your build configuration, mark used MarkEdit and CodeMirror dependencies as `external`.
@@ -147,3 +163,5 @@ const editorAPI = MarkEdit.editorAPI;
147163
----
148164

149165
For complete examples, refer to [Example: Markdown Table Editor](https://github.com/MarkEdit-app/MarkEdit-mte), [Example: Text Highlight](https://github.com/MarkEdit-app/MarkEdit-highlight) and [Example: Vue Language Package](https://github.com/MarkEdit-app/MarkEdit-lang-vue).
166+
167+
Also, [markedit.d.ts](./markedit.d.ts) is fully typed and documented, use it as the API reference.

markedit.d.ts

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,52 @@ export interface MarkEdit {
9595
* Get notified when the editor is initialized.
9696
* @param listener The callback function with the initialized editor instance.
9797
*/
98-
onEditorReady: (listener: (editorView: EditorView) => void) => void;
98+
onEditorReady(listener: (editorView: EditorView) => void): void;
9999

100100
/**
101101
* Add an extension to MarkEdit.
102102
* @param extension CodeMirror extension.
103103
*/
104-
addExtension: (extension: Extension) => void;
104+
addExtension(extension: Extension): void;
105105

106106
/**
107107
* Add a Markdown config to MarkEdit.
108108
* @param config Markdown config.
109109
*/
110-
addMarkdownConfig: (config: MarkdownConfig) => void;
110+
addMarkdownConfig(config: MarkdownConfig): void;
111111

112112
/**
113113
* Add a language to be highlighted (in code blocks) to MarkEdit.
114114
* @param language The language description.
115115
*/
116-
addCodeLanguage: (language: LanguageDescription) => void;
116+
addCodeLanguage(language: LanguageDescription): void;
117+
118+
/**
119+
* Add a menu item to the status bar.
120+
* @param item The menu item to add, which will be placed under the "Extensions" submenu. It can either perform an action or contain a submenu.
121+
*/
122+
addMainMenuItem(item: MenuItem | MenuItem[]): void;
123+
124+
/**
125+
* Present a contextual menu to receive user input.
126+
* @param items The items in the menu.
127+
* @param location The location to show the menu, with the default value set to the caret position.
128+
*/
129+
showContextMenu(items: MenuItem[], location?: Point): void;
130+
131+
/**
132+
* Present an alert to receive user input.
133+
* @param alert The alert to present.
134+
* @returns The index of the chosen button, 0-indexed.
135+
*/
136+
showAlert(alert: Alert): Promise<number>;
137+
138+
/**
139+
* Present a text box to receive user input.
140+
* @param textBox The text box to present.
141+
* @returns The input string, or undefined if no value is provided.
142+
*/
143+
showTextBox(textBox?: TextBox): Promise<string | undefined>;
117144
}
118145

119146
/**
@@ -183,7 +210,83 @@ export interface TextEditable {
183210
redo(): void;
184211
}
185212

213+
/**
214+
* Represents a portion of text.
215+
*/
186216
export type TextRange = {
187217
from: number;
188218
to: number;
189-
}
219+
};
220+
221+
/**
222+
* Represents a menu item in native menus.
223+
*/
224+
export type MenuItem = {
225+
title?: string;
226+
action?: () => void;
227+
228+
/**
229+
* Whether an item is a separator used to logically group other items.
230+
*/
231+
separator?: boolean;
232+
233+
/**
234+
* Key equivalent of the action.
235+
*/
236+
key?: string;
237+
238+
/**
239+
* Key modifiers of the action. Valid values are `Shift`, `Control`, `Command`, and `Option`.
240+
*
241+
* The order does not matter but they are case sensitive.
242+
*
243+
* When `key` is uppercase, `Shift` is automatically enabled.
244+
*/
245+
modifiers?: ('Shift' | 'Control' | 'Command' | 'Option')[];
246+
247+
/**
248+
* Child items to build a submenu.
249+
*/
250+
children?: MenuItem[];
251+
};
252+
253+
/**
254+
* Represents a location on the screen.
255+
*/
256+
export type Point = {
257+
x: number;
258+
y: number;
259+
};
260+
261+
/**
262+
* Represents a native alert to present.
263+
*
264+
* If it's a string, it will be used as the title.
265+
*/
266+
export type Alert = {
267+
/**
268+
* The title that is displayed prominently in the alert.
269+
*/
270+
title?: string;
271+
272+
/**
273+
* The informative message for more details.
274+
*/
275+
message?: string;
276+
277+
/**
278+
* The button titles.
279+
*/
280+
buttons?: string[];
281+
} | string;
282+
283+
/**
284+
* Represents a text box to receive input.
285+
*
286+
* If it's a string, it will be used as the title.
287+
*/
288+
export type TextBox = {
289+
title?: string;
290+
placeholder?: string;
291+
defaultValue?: string;
292+
} | string;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markedit-api",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "Type definitions for the latest MakrEdit API.",
55
"main": "main.ts",
66
"types": "markedit.d.ts",

0 commit comments

Comments
 (0)