@backmatter/studio 0.0.23 → 0.0.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/{UiKitApp-DNnsTU4S.js → UiKitApp-BW_SpUgQ.js} +2 -2
- package/dist/chunks/{WorkspaceRoutes-LYgvnqww.js → WorkspaceRoutes-DuwT2y7A.js} +24033 -23697
- package/dist/chunks/{dashboard-DGd2D7_c.js → dashboard-RFDdTeMV.js} +2 -2
- package/dist/chunks/{index-BcnHQg9d.js → index-DXXGgPpc.js} +1 -1
- package/dist/dashboard.js +2 -2
- package/dist/index.js +2 -2
- package/dist/studio.css +53 -1
- package/package.json +2 -2
- package/src/WorkspaceRoutes.tsx +2 -0
- package/src/api.ts +5 -0
- package/src/chrome/ContentExplorerSidebar.tsx +16 -0
- package/src/chrome/explorer-icons.tsx +5 -0
- package/src/define-studio.tsx +12 -1
- package/src/index.ts +1 -0
- package/src/pages/BlockLibrary.tsx +542 -0
- package/src/pages/BlocksLibraryPage.tsx +57 -0
- package/src/pages/BlocksList.tsx +16 -2
- package/src/pages/DocEditorPage.tsx +2 -0
- package/src/pages/WorkspaceLayout.tsx +3 -1
- package/src/pages/block-groups.ts +120 -0
- package/src/workspace-path.ts +8 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// packages/studio/src/pages/block-groups.ts
|
|
2
|
+
// How the "Add block" picker sorts blocks into groups, and whether it opens on
|
|
3
|
+
// the groups or on every block at once.
|
|
4
|
+
import { BLOCK_GROUPS } from "@backmatter/core";
|
|
5
|
+
import type { BlockPickerView, BlockSnapshot } from "../api";
|
|
6
|
+
|
|
7
|
+
/** Site overrides for one block group, from `defineStudio({ blockGroups })`. */
|
|
8
|
+
export type StudioBlockGroupConfig = {
|
|
9
|
+
label?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
/** Cover image: an http(s) URL or a site path like "/block-previews/pricing.png". */
|
|
12
|
+
cover?: string;
|
|
13
|
+
/** Lower comes first. Groups without one keep the default order after these. */
|
|
14
|
+
order?: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type BlockGroupView = {
|
|
18
|
+
/** The `group` key the blocks declare; "" collects blocks without one. */
|
|
19
|
+
name: string;
|
|
20
|
+
label: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
blocks: BlockSnapshot[];
|
|
23
|
+
/** Block drawn as the group's cover when `coverSrc` is unset. */
|
|
24
|
+
cover: BlockSnapshot;
|
|
25
|
+
coverSrc?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Auto view groups once the catalog has more blocks than this. */
|
|
29
|
+
export const AUTO_GROUP_THRESHOLD = 8;
|
|
30
|
+
|
|
31
|
+
const BUILTIN_ORDER = Object.keys(BLOCK_GROUPS);
|
|
32
|
+
|
|
33
|
+
function titleCase(name: string): string {
|
|
34
|
+
return name
|
|
35
|
+
.split("-")
|
|
36
|
+
.filter(Boolean)
|
|
37
|
+
.map((part) => part[0]!.toUpperCase() + part.slice(1))
|
|
38
|
+
.join(" ");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function defaultRank(name: string): number {
|
|
42
|
+
if (name === "") return Number.MAX_SAFE_INTEGER;
|
|
43
|
+
const builtin = BUILTIN_ORDER.indexOf(name);
|
|
44
|
+
return builtin >= 0 ? builtin : BUILTIN_ORDER.length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function groupBlocks(
|
|
48
|
+
catalog: BlockSnapshot[],
|
|
49
|
+
settings: Record<string, StudioBlockGroupConfig> = {},
|
|
50
|
+
): BlockGroupView[] {
|
|
51
|
+
const byName = new Map<string, BlockSnapshot[]>();
|
|
52
|
+
for (const block of catalog) {
|
|
53
|
+
const name = block.group ?? "";
|
|
54
|
+
const list = byName.get(name);
|
|
55
|
+
if (list) list.push(block);
|
|
56
|
+
else byName.set(name, [block]);
|
|
57
|
+
}
|
|
58
|
+
const groups: BlockGroupView[] = [];
|
|
59
|
+
for (const [name, blocks] of byName) {
|
|
60
|
+
const setting = settings[name] ?? {};
|
|
61
|
+
const builtin = (BLOCK_GROUPS as Record<string, { label: string; description: string }>)[name];
|
|
62
|
+
const description = setting.description ?? builtin?.description;
|
|
63
|
+
groups.push({
|
|
64
|
+
name,
|
|
65
|
+
label: setting.label ?? builtin?.label ?? (name ? titleCase(name) : "Other"),
|
|
66
|
+
...(description ? { description } : {}),
|
|
67
|
+
blocks,
|
|
68
|
+
cover: blocks.find((b) => b.preview) ?? blocks[0]!,
|
|
69
|
+
...(setting.cover ? { coverSrc: setting.cover } : {}),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return groups.sort((a, b) => {
|
|
73
|
+
const oa = settings[a.name]?.order;
|
|
74
|
+
const ob = settings[b.name]?.order;
|
|
75
|
+
if (oa !== undefined || ob !== undefined) {
|
|
76
|
+
if (oa === undefined) return 1;
|
|
77
|
+
if (ob === undefined) return -1;
|
|
78
|
+
if (oa !== ob) return oa - ob;
|
|
79
|
+
}
|
|
80
|
+
const ra = defaultRank(a.name);
|
|
81
|
+
const rb = defaultRank(b.name);
|
|
82
|
+
if (ra !== rb) return ra - rb;
|
|
83
|
+
return a.name.localeCompare(b.name);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The picker layout: the blocks field's `picker`, else the site default, else
|
|
89
|
+
* "auto". A catalog in fewer than two groups is always flat.
|
|
90
|
+
*/
|
|
91
|
+
export function resolvePickerView(
|
|
92
|
+
catalog: BlockSnapshot[],
|
|
93
|
+
views: { field?: BlockPickerView; site?: BlockPickerView },
|
|
94
|
+
): "grouped" | "flat" {
|
|
95
|
+
const groups = new Set(catalog.map((b) => b.group ?? ""));
|
|
96
|
+
if (groups.size < 2) return "flat";
|
|
97
|
+
const view = views.field ?? views.site ?? "auto";
|
|
98
|
+
if (view === "auto") return catalog.length > AUTO_GROUP_THRESHOLD ? "grouped" : "flat";
|
|
99
|
+
return view;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Blocks whose name, label, description or group label contain every word of the query. */
|
|
103
|
+
export function searchBlocks(
|
|
104
|
+
groups: BlockGroupView[],
|
|
105
|
+
query: string,
|
|
106
|
+
labelOf: (block: BlockSnapshot) => string = (b) => b.name,
|
|
107
|
+
): BlockSnapshot[] {
|
|
108
|
+
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
109
|
+
if (words.length === 0) return [];
|
|
110
|
+
const hits: BlockSnapshot[] = [];
|
|
111
|
+
for (const group of groups) {
|
|
112
|
+
for (const block of group.blocks) {
|
|
113
|
+
const text = [block.name, labelOf(block), block.description ?? "", group.label]
|
|
114
|
+
.join(" ")
|
|
115
|
+
.toLowerCase();
|
|
116
|
+
if (words.every((w) => text.includes(w))) hits.push(block);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return hits;
|
|
120
|
+
}
|
package/src/workspace-path.ts
CHANGED
|
@@ -23,6 +23,10 @@ export function mediaPath(slug: string) {
|
|
|
23
23
|
return `/${slug}/media`;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
export function blocksPath(slug: string) {
|
|
27
|
+
return `/${slug}/blocks`;
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
export function codeSessionPath(slug: string) {
|
|
27
31
|
return `/${slug}/code`;
|
|
28
32
|
}
|
|
@@ -83,6 +87,10 @@ export function isWorkspaceMediaPath(pathname: string) {
|
|
|
83
87
|
return /\/media\/?$/.test(pathname);
|
|
84
88
|
}
|
|
85
89
|
|
|
90
|
+
export function isWorkspaceBlocksPath(pathname: string) {
|
|
91
|
+
return /\/blocks\/?$/.test(pathname);
|
|
92
|
+
}
|
|
93
|
+
|
|
86
94
|
export function isWorkspaceCodePath(pathname: string) {
|
|
87
95
|
return /\/code\/?$/.test(pathname);
|
|
88
96
|
}
|