Skip to content

Commit 4ec46a2

Browse files
committed
chore(client): add some documentation
1 parent db6f948 commit 4ec46a2

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

apps/client/src/services/options.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ class Options {
7777
await server.put(`options`, payload);
7878
}
7979

80+
/**
81+
* Saves multiple options at once, by supplying a record where the keys are the option names and the values represent the stringified value to set.
82+
* @param newValues the record of keys and values.
83+
*/
8084
async saveMany<T extends OptionNames>(newValues: Record<T, OptionValue>) {
8185
await server.put<void>("options", newValues);
8286
}

apps/client/src/services/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ async function openInAppHelp($button: JQuery<HTMLElement>) {
378378
}
379379
}
380380

381+
/**
382+
* Opens the in-app help at the given page in a split note. If there already is a split note open with a help page, it will be replaced by this one.
383+
*
384+
* @param inAppHelpPage the ID of the help note (excluding the `_help_` prefix).
385+
* @returns a promise that resolves once the help has been opened.
386+
*/
381387
export async function openInAppHelpFromUrl(inAppHelpPage: string) {
382388
// Dynamic import to avoid import issues in tests.
383389
const appContext = (await import("../components/app_context.js")).default;
@@ -738,6 +744,14 @@ function isLaunchBarConfig(noteId: string) {
738744
return ["_lbRoot", "_lbAvailableLaunchers", "_lbVisibleLaunchers", "_lbMobileRoot", "_lbMobileAvailableLaunchers", "_lbMobileVisibleLaunchers"].includes(noteId);
739745
}
740746

747+
/**
748+
* Adds a class to the <body> of the page, where the class name is formed via a prefix and a value.
749+
* Useful for configurable options such as `heading-style-markdown`, where `heading-style` is the prefix and `markdown` is the dynamic value.
750+
* There is no separator between the prefix and the value, if needed it has to be supplied manually to the prefix.
751+
*
752+
* @param prefix the prefix.
753+
* @param value the value to be appended to the prefix.
754+
*/
741755
export function toggleBodyClass(prefix: string, value: string) {
742756
const $body = $("body");
743757
for (const clazz of Array.from($body[0].classList)) {
@@ -750,6 +764,13 @@ export function toggleBodyClass(prefix: string, value: string) {
750764
$body.addClass(prefix + value);
751765
}
752766

767+
/**
768+
* Basic comparison for equality between the two arrays. The values are strictly checked via `===`.
769+
*
770+
* @param a the first array to compare.
771+
* @param b the second array to compare.
772+
* @returns `true` if both arrays are equals, `false` otherwise.
773+
*/
753774
export function arrayEqual<T>(a: T[], b: T[]) {
754775
if (a === b) {
755776
return true;

apps/client/src/widgets/react/hooks.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ export function useTriliumOption(name: OptionNames, needsRefresh?: boolean): [st
145145
]
146146
}
147147

148+
/**
149+
* Similar to {@link useTriliumOption}, but the value is converted to and from a boolean instead of a string.
150+
*
151+
* @param name the name of the option to listen for.
152+
* @param needsRefresh whether to reload the frontend whenever the value is changed.
153+
* @returns an array where the first value is the current option value and the second value is the setter.
154+
*/
148155
export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean): [boolean, (newValue: boolean) => Promise<void>] {
149156
const [ value, setValue ] = useTriliumOption(name, needsRefresh);
150157
return [
@@ -153,6 +160,13 @@ export function useTriliumOptionBool(name: OptionNames, needsRefresh?: boolean):
153160
]
154161
}
155162

163+
/**
164+
* Similar to {@link useTriliumOption}, but the value is converted to and from a int instead of a string.
165+
*
166+
* @param name the name of the option to listen for.
167+
* @param needsRefresh whether to reload the frontend whenever the value is changed.
168+
* @returns an array where the first value is the current option value and the second value is the setter.
169+
*/
156170
export function useTriliumOptionInt(name: OptionNames): [number, (newValue: number) => Promise<void>] {
157171
const [ value, setValue ] = useTriliumOption(name);
158172
return [
@@ -161,6 +175,12 @@ export function useTriliumOptionInt(name: OptionNames): [number, (newValue: numb
161175
]
162176
}
163177

178+
/**
179+
* Similar to {@link useTriliumOption}, but the object value is parsed to and from a JSON instead of a string.
180+
*
181+
* @param name the name of the option to listen for.
182+
* @returns an array where the first value is the current option value and the second value is the setter.
183+
*/
164184
export function useTriliumOptionJson<T>(name: OptionNames): [ T, (newValue: T) => Promise<void> ] {
165185
const [ value, setValue ] = useTriliumOption(name);
166186
return [
@@ -169,6 +189,12 @@ export function useTriliumOptionJson<T>(name: OptionNames): [ T, (newValue: T) =
169189
];
170190
}
171191

192+
/**
193+
* Similar to {@link useTriliumOption}, but operates with multiple options at once.
194+
*
195+
* @param names the name of the option to listen for.
196+
* @returns an array where the first value is a map where the keys are the option names and the values, and the second value is the setter which takes in the same type of map and saves them all at once.
197+
*/
172198
export function useTriliumOptions<T extends OptionNames>(...names: T[]) {
173199
const values: Record<string, string> = {};
174200
for (const name of names) {

0 commit comments

Comments
 (0)