Skip to content

Commit 8849f07

Browse files
committed
New actionButton.define command
1 parent cc3aece commit 8849f07

File tree

5 files changed

+69
-12
lines changed

5 files changed

+69
-12
lines changed

Library/Std/Action Button.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#meta
2+
3+
APIs to more conveniently define action buttons (alternative to setting them via regular [[^Library/Std/Config]]).
4+
5+
# API
6+
## actionButton.define(spec)
7+
Keys to define:
8+
9+
* `icon`: [feather icon](https://feathericons.com) to use for your button
10+
* `run`: function to invoke once the button is pushed
11+
* `description` (optional): description of button (appears on hover)
12+
* `priority` (optional): determines priority of button (the higher, the earlier in the list)
13+
* `mobile`: when set to `true` this button will only appear on mobile devices
14+
15+
# Example
16+
```lua
17+
-- Defines a new button that forces your UI into read-only mode
18+
actionButton.define {
19+
icon = "eye",
20+
-- Uncomment the following line to only make this button appear
21+
-- ONLY on mobile devices:
22+
-- mobile = true,
23+
run = function()
24+
editor.setUiOption("forcedROMode", true)
25+
editor.rebuildEditorState()
26+
end
27+
}
28+
```
29+
# Implementation
30+
```space-lua
31+
-- priority: 100
32+
33+
actionButton = actionButton or {}
34+
35+
function actionButton.define(spec)
36+
local actionButtonConfig = config.get("actionButtons", {})
37+
table.insert(actionButtonConfig, spec)
38+
end
39+
```

Library/Std/Config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ config.define("actionButtons", {
144144
type = "string",
145145
description = "Optional description of the action button"
146146
},
147+
priority = {
148+
type = "number",
149+
description = "Optional priority: the higher the earlier the button will appear in the list"
150+
},
147151
mobile = {
148152
type = "boolean",
149153
description = "Optional boolean indicating if the action button is applicable for mobile"
@@ -262,20 +266,23 @@ config.set {
262266
{
263267
icon = "home",
264268
description = "Go to the index page",
269+
priority = 3,
265270
run = function()
266271
editor.invokeCommand("Navigate: Home")
267272
end
268273
},
269274
{
270275
icon = "book",
271276
description = "Open page",
277+
priority = 2,
272278
run = function()
273279
editor.invokeCommand("Navigate: Page Picker")
274280
end
275281
},
276282
{
277283
icon = "terminal",
278284
description = "Run command",
285+
priority = 1,
279286
run = function()
280287
editor.invokeCommand "Open Command Palette"
281288
end,

lib/space_lua/stdlib/table.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
LuaRuntimeError,
99
LuaTable,
1010
type LuaValue,
11+
luaValueToJS,
1112
} from "../runtime.ts";
1213
import { asyncQuickSort } from "../util.ts";
1314

@@ -42,12 +43,14 @@ export const tableApi = new LuaTable({
4243
* @param value - The value to insert.
4344
*/
4445
insert: new LuaBuiltinFunction(
45-
(_sf, tbl: LuaTable | any[], posOrValue: number | any, value?: any) => {
46+
(sf, tbl: LuaTable | any[], posOrValue: number | any, value?: any) => {
4647
if (Array.isArray(tbl)) {
48+
// Since we're inserting/appending to a native JS array, we'll also convert the value to a JS value on the fly
49+
// this seems like a reasonable heuristic
4750
if (value === undefined) {
48-
tbl.push(posOrValue);
51+
tbl.push(luaValueToJS(posOrValue, sf));
4952
} else {
50-
tbl.splice(posOrValue - 1, 0, value);
53+
tbl.splice(posOrValue - 1, 0, luaValueToJS(value, sf));
5154
}
5255
} else if (tbl instanceof LuaTable) {
5356
if (value === undefined) {

web/client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ export class Client {
445445

446446
if (
447447
!this.ui.viewState.unsavedChanges ||
448-
this.ui.viewState.uiOptions.forcedROMode ||
449448
this.clientConfig.readOnly
450449
) {
451450
// No unsaved changes, or read-only mode, not gonna save

web/editor_ui.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export class MainUI {
130130
// Need to dispatch a resize event so that the top_bar can pick it up
131131
globalThis.dispatchEvent(new Event("resize"));
132132
}, [viewState.panels]);
133-
133+
const actionButtons = client.config.get<ActionButton[]>(
134+
"actionButtons",
135+
[],
136+
);
134137
return (
135138
<>
136139
{viewState.showPageNavigator && (
@@ -335,15 +338,20 @@ export class MainUI {
335338
}]
336339
: [],
337340
// Custom action buttons
338-
...client.config.get<ActionButton[]>(
339-
"actionButtons",
340-
[],
341-
).filter((
341+
...actionButtons.filter(( // Filter out buttons without icons (invalid) and mobile buttons when not in mobile mode
342342
button,
343343
) =>
344-
(typeof button.mobile === "undefined") ||
345-
(button.mobile === viewState.isMobile)
344+
button.icon && (
345+
(typeof button.mobile === "undefined") ||
346+
(button.mobile === viewState.isMobile)
347+
)
346348
)
349+
// Then ensure all buttons have a priority set (by default based on array index)
350+
.map((button, index) => ({
351+
...button,
352+
priority: button.priority ?? actionButtons.length - index,
353+
}))
354+
.sort((a, b) => b.priority - a.priority)
347355
.map((button) => {
348356
const mdiIcon = (mdi as any)[kebabToCamel(button.icon)];
349357
let featherIcon =
@@ -356,7 +364,7 @@ export class MainUI {
356364
description: button.description || "",
357365
callback: button.run || (() => {
358366
client.flashNotification(
359-
"Legacy actionButton definition detected, please define a run() callback",
367+
"actionButton did not specify a run() callback",
360368
"error",
361369
);
362370
}),
@@ -427,6 +435,7 @@ type ActionButton = {
427435
icon: string;
428436
description?: string;
429437
mobile?: boolean;
438+
priority?: number;
430439
run: () => void;
431440
};
432441

0 commit comments

Comments
 (0)