@alstar/studio 0.0.0-beta.16 → 0.0.0-beta.18
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/api/block.ts +0 -14
- package/components/AdminPanel.ts +11 -5
- package/components/BlockFieldRenderer.ts +25 -19
- package/components/BlockRenderer.ts +4 -4
- package/components/Entries.ts +1 -1
- package/components/Entry.ts +13 -7
- package/components/FieldRenderer.ts +8 -7
- package/components/LivePreview.ts +37 -0
- package/components/Render.ts +2 -2
- package/components/SiteLayout.ts +1 -4
- package/components/fields/Markdown.ts +10 -3
- package/components/fields/Reference.ts +11 -7
- package/components/fields/Slug.ts +6 -6
- package/components/fields/Text.ts +13 -8
- package/components/icons.ts +3 -0
- package/components/settings/ApiKeys.ts +4 -4
- package/components/settings/Backup.ts +3 -3
- package/components/settings/Users.ts +1 -1
- package/index.ts +11 -10
- package/package.json +4 -5
- package/pages/entry/[id].ts +7 -1
- package/pages/error.ts +7 -6
- package/pages/login.ts +1 -1
- package/pages/register.ts +2 -2
- package/public/studio/css/admin-panel.css +27 -9
- package/public/studio/css/blocks-field.css +25 -0
- package/public/studio/css/entry-page.css +4 -0
- package/public/studio/css/entry.css +35 -0
- package/public/studio/css/field.css +14 -0
- package/public/studio/css/live-preview.css +25 -0
- package/public/studio/css/settings.css +4 -0
- package/public/studio/js/live-preview.js +26 -0
- package/public/studio/js/markdown-editor.js +6 -0
- package/public/studio/js/sortable-list.js +6 -4
- package/public/studio/main.css +11 -13
- package/public/studio/main.js +1 -0
- package/queries/block.ts +127 -105
- package/queries/index.ts +3 -2
- package/types.ts +39 -69
- package/utils/define.ts +3 -1
- package/utils/refresher.ts +56 -0
- package/utils/renderSSE.ts +8 -3
- package/utils/startup-log.ts +4 -4
- package/queries/block-2.ts +0 -339
- package/queries/db-types.ts +0 -15
- package/queries/getBlockTrees-2.ts +0 -71
- package/queries/getBlocks.ts +0 -214
- package/queries/structure-types.ts +0 -97
- package/utils/buildBlocksTree.ts +0 -44
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { structure } from "../index.ts";
|
|
2
|
-
|
|
3
|
-
export const defineField = <
|
|
4
|
-
F extends Record<string, any> | undefined = undefined,
|
|
5
|
-
C extends Record<string, any> | undefined = undefined
|
|
6
|
-
>(config: {
|
|
7
|
-
type: string;
|
|
8
|
-
label: string;
|
|
9
|
-
fields?: F;
|
|
10
|
-
children?: C;
|
|
11
|
-
}) =>
|
|
12
|
-
({
|
|
13
|
-
kind: 'field',
|
|
14
|
-
type: config.type,
|
|
15
|
-
label: config.label,
|
|
16
|
-
fields: (config.fields ?? {}) as F,
|
|
17
|
-
children: (config.children ?? {}) as C,
|
|
18
|
-
} as const);
|
|
19
|
-
|
|
20
|
-
export const defineBlock = <
|
|
21
|
-
F extends Record<string, any> = {},
|
|
22
|
-
C extends Record<string, any> = {}
|
|
23
|
-
>(config: {
|
|
24
|
-
type: string;
|
|
25
|
-
label: string;
|
|
26
|
-
fields?: F;
|
|
27
|
-
children?: C;
|
|
28
|
-
}) =>
|
|
29
|
-
({
|
|
30
|
-
kind: 'block',
|
|
31
|
-
type: config.type,
|
|
32
|
-
label: config.label,
|
|
33
|
-
fields: (config.fields ?? {}) as F,
|
|
34
|
-
children: (config.children ?? {}) as C,
|
|
35
|
-
} as const);
|
|
36
|
-
|
|
37
|
-
type InferBlock<T> =
|
|
38
|
-
// A Block has fields + children
|
|
39
|
-
T extends { kind: 'block'; fields: infer F; children: infer C }
|
|
40
|
-
? {
|
|
41
|
-
fields: { [K in keyof F]: InferBlock<F[K]> };
|
|
42
|
-
children: { [K in keyof C]: InferBlock<C[K]> }[keyof C][];
|
|
43
|
-
}
|
|
44
|
-
// A Field without children
|
|
45
|
-
: T extends { kind: 'field'; children: infer C; fields: infer F }
|
|
46
|
-
? keyof C extends never
|
|
47
|
-
? { value: string | null; type: string; label: string }
|
|
48
|
-
// A Field with children (like "blocks")
|
|
49
|
-
: {
|
|
50
|
-
fields: { [K in keyof F]: InferBlock<F[K]> };
|
|
51
|
-
children: { [K in keyof C]: InferBlock<C[K]> }[keyof C][];
|
|
52
|
-
type: string;
|
|
53
|
-
label: string;
|
|
54
|
-
}
|
|
55
|
-
: never;
|
|
56
|
-
|
|
57
|
-
// Top-level inference for the structure object
|
|
58
|
-
type InferStructure<S> = {
|
|
59
|
-
[K in keyof S]: InferBlock<S[K]>;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
type AppStructure = typeof structure;
|
|
63
|
-
type AppBlocks = InferStructure<AppStructure>;
|
|
64
|
-
|
|
65
|
-
// Example: AppBlocks["page"] is the typed shape for a page block
|
|
66
|
-
export function getBlockTrees<T extends keyof AppBlocks>(
|
|
67
|
-
rootType: T
|
|
68
|
-
): AppBlocks[T][] {
|
|
69
|
-
// Your SQL + builder code goes here
|
|
70
|
-
return [] as any;
|
|
71
|
-
}
|
package/queries/getBlocks.ts
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { DatabaseSync } from "node:sqlite";
|
|
2
|
-
import { sql } from "../utils/sql.ts";
|
|
3
|
-
|
|
4
|
-
// --- Field & block definitions ---
|
|
5
|
-
type FieldType = 'text' | 'slug' | 'markdown' | 'blocks' | 'image';
|
|
6
|
-
|
|
7
|
-
interface BaseField {
|
|
8
|
-
label: string;
|
|
9
|
-
type: FieldType;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface TextField extends BaseField {
|
|
13
|
-
type: 'text' | 'slug' | 'markdown';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
interface ImageField extends BaseField {
|
|
17
|
-
type: 'image';
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
interface BlocksField extends BaseField {
|
|
21
|
-
type: 'blocks';
|
|
22
|
-
children: Record<string, BlockDef>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type FieldDef = TextField | ImageField | BlocksField;
|
|
26
|
-
|
|
27
|
-
interface BlockDef {
|
|
28
|
-
label: string;
|
|
29
|
-
type: string;
|
|
30
|
-
fields: Record<string, FieldDef>;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// --- Identity helpers (preserve literal types) ---
|
|
34
|
-
export function defineField<T extends FieldDef>(field: T) {
|
|
35
|
-
return field;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function defineBlock<T extends BlockDef>(block: T) {
|
|
39
|
-
return block;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function defineStructure<T extends Record<string, BlockDef>>(structure: T) {
|
|
43
|
-
return structure;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// --- Type mapping from structure to data ---
|
|
47
|
-
type FieldToType<F extends FieldDef> =
|
|
48
|
-
F['type'] extends 'text' | 'slug' | 'markdown' ? string :
|
|
49
|
-
F['type'] extends 'image' ? string :
|
|
50
|
-
F['type'] extends 'blocks'
|
|
51
|
-
? {
|
|
52
|
-
[K in keyof F['children']]: {
|
|
53
|
-
type: K;
|
|
54
|
-
data: BlockData<F['children'][K]>;
|
|
55
|
-
}
|
|
56
|
-
}[keyof F['children']][]
|
|
57
|
-
: never;
|
|
58
|
-
|
|
59
|
-
type BlockData<B extends BlockDef> = {
|
|
60
|
-
[K in keyof B['fields']]: FieldToType<B['fields'][K]>;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
type StructureData<S extends Record<string, BlockDef>> = {
|
|
64
|
-
[K in keyof S]: BlockData<S[K]>;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// This will be inferred after you define your structure
|
|
68
|
-
export type CMSData<S extends Record<string, BlockDef>> = StructureData<S>;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
interface DBBase {
|
|
72
|
-
id: number;
|
|
73
|
-
created_at: string;
|
|
74
|
-
updated_at: string;
|
|
75
|
-
name: string;
|
|
76
|
-
label: string;
|
|
77
|
-
type: string;
|
|
78
|
-
sort_order: number;
|
|
79
|
-
value: string | null;
|
|
80
|
-
options: any;
|
|
81
|
-
status: string;
|
|
82
|
-
parent_id: number | null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function buildTypedForest<
|
|
86
|
-
S extends Record<string, BlockDef>,
|
|
87
|
-
K extends keyof S
|
|
88
|
-
>(
|
|
89
|
-
rows: DBBase[],
|
|
90
|
-
rootDef: S[K]
|
|
91
|
-
): CMSData<S>[K][] {
|
|
92
|
-
const map = new Map<number, DBBase & { children: DBBase[] }>();
|
|
93
|
-
|
|
94
|
-
// Initialize with children arrays
|
|
95
|
-
for (const r of rows) {
|
|
96
|
-
map.set(r.id, { ...r, children: [] });
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Link children to parents
|
|
100
|
-
const roots: (DBBase & { children: DBBase[] })[] = [];
|
|
101
|
-
for (const r of rows) {
|
|
102
|
-
const node = map.get(r.id)!;
|
|
103
|
-
if (r.parent_id === null) {
|
|
104
|
-
roots.push(node);
|
|
105
|
-
} else {
|
|
106
|
-
const parent = map.get(r.parent_id);
|
|
107
|
-
if (parent) parent.children.push(node);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Recursive transformer: maps a DB row into typed data
|
|
112
|
-
function transformNode<D extends FieldDef | BlockDef>(
|
|
113
|
-
node: DBBase & DBBase['type'] extends 'blocks' ? { children: DBBase[] } : {},
|
|
114
|
-
def: D
|
|
115
|
-
): any {
|
|
116
|
-
if ('fields' in def) {
|
|
117
|
-
// It's a BlockDef
|
|
118
|
-
const result: any = {};
|
|
119
|
-
for (const key in def.fields) {
|
|
120
|
-
const fieldDef = def.fields[key];
|
|
121
|
-
const childNode = node.children.find(c => c.name === key);
|
|
122
|
-
result[key] = childNode
|
|
123
|
-
? transformNode(childNode, fieldDef)
|
|
124
|
-
: getDefaultValue(fieldDef);
|
|
125
|
-
}
|
|
126
|
-
return result;
|
|
127
|
-
} else {
|
|
128
|
-
// It's a FieldDef
|
|
129
|
-
if (def.type === 'text' || def.type === 'slug' || def.type === 'markdown') {
|
|
130
|
-
return node.value ?? '';
|
|
131
|
-
}
|
|
132
|
-
if (def.type === 'image') {
|
|
133
|
-
return node.value ?? '';
|
|
134
|
-
}
|
|
135
|
-
if (def.type === 'blocks') {
|
|
136
|
-
return node.children.map(child => {
|
|
137
|
-
const childDef = def.children[child.name as keyof typeof def.children];
|
|
138
|
-
return {
|
|
139
|
-
type: child.name as keyof typeof def.children,
|
|
140
|
-
data: transformNode(child, childDef)
|
|
141
|
-
};
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Provide safe defaults for missing fields
|
|
148
|
-
function getDefaultValue(fieldDef: FieldDef): any {
|
|
149
|
-
if (fieldDef.type === 'blocks') return [];
|
|
150
|
-
return '';
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// Map all root nodes into typed data
|
|
154
|
-
return roots.map(root => transformNode(root, rootDef));
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
function rootQuery(filterSql: string, depthLimit?: number) {
|
|
159
|
-
const depthLimitClause =
|
|
160
|
-
depthLimit !== undefined ? `WHERE d.depth + 1 <= ${depthLimit}` : '';
|
|
161
|
-
|
|
162
|
-
return sql`
|
|
163
|
-
with recursive
|
|
164
|
-
ancestors as (
|
|
165
|
-
select
|
|
166
|
-
id, created_at, updated_at, name, label, type, sort_order, value, options, status, parent_id,
|
|
167
|
-
0 as depth
|
|
168
|
-
from blocks
|
|
169
|
-
where ${filterSql}
|
|
170
|
-
union all
|
|
171
|
-
select
|
|
172
|
-
b.id, b.created_at, b.updated_at, b.name, b.label, b.type, b.sort_order, b.value, b.options, b.status, b.parent_id,
|
|
173
|
-
a.depth + 1
|
|
174
|
-
from blocks b
|
|
175
|
-
inner join ancestors a on b.id = a.parent_id
|
|
176
|
-
),
|
|
177
|
-
roots as (
|
|
178
|
-
select * from ancestors where parent_id is null
|
|
179
|
-
),
|
|
180
|
-
descendants as (
|
|
181
|
-
select * from roots
|
|
182
|
-
union all
|
|
183
|
-
select
|
|
184
|
-
b.id, b.created_at, b.updated_at, b.name, b.label, b.type, b.sort_order, b.value, b.options, b.status, b.parent_id,
|
|
185
|
-
d.depth + 1
|
|
186
|
-
from blocks b
|
|
187
|
-
inner join descendants d on b.parent_id = d.id ${depthLimitClause}
|
|
188
|
-
)
|
|
189
|
-
select * from descendants
|
|
190
|
-
order by parent_id, sort_order
|
|
191
|
-
`;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export function queryTypedRoot<
|
|
195
|
-
S extends Record<string, BlockDef>,
|
|
196
|
-
K extends keyof S
|
|
197
|
-
>(
|
|
198
|
-
db: DatabaseSync,
|
|
199
|
-
structure: S,
|
|
200
|
-
blockType: K,
|
|
201
|
-
opts: { id?: number; depthLimit?: number }
|
|
202
|
-
): CMSData<S>[K][] {
|
|
203
|
-
let filterSql: string;
|
|
204
|
-
if (opts.id !== undefined) {
|
|
205
|
-
filterSql = `id = ${opts.id}`;
|
|
206
|
-
} else {
|
|
207
|
-
filterSql = `type = '${String(structure[blockType].type)}'`;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const sql = rootQuery(filterSql, opts.depthLimit);
|
|
211
|
-
const rows = db.prepare(sql).all() as unknown as DBBase[];
|
|
212
|
-
|
|
213
|
-
return buildTypedForest(rows, structure[blockType]);
|
|
214
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// structure-types.ts
|
|
2
|
-
import { type DBBase } from "./db-types.ts";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Extract helpers
|
|
6
|
-
*/
|
|
7
|
-
type ArrayElement<T> = T extends readonly (infer U)[] ? U : never;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Field definition shape inferred from defineField(...) (the runtime helper)
|
|
11
|
-
* We keep it generic as "any" shape but with the important properties present
|
|
12
|
-
*/
|
|
13
|
-
type FieldDef = {
|
|
14
|
-
readonly name: string;
|
|
15
|
-
readonly type: string;
|
|
16
|
-
readonly fields?: readonly any[]; // only present when type === 'blocks'
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Block definition shape inferred from defineBlock(...) (the runtime helper)
|
|
21
|
-
*/
|
|
22
|
-
type BlockDef = {
|
|
23
|
-
readonly name: string;
|
|
24
|
-
readonly type: string;
|
|
25
|
-
readonly fields?: readonly FieldDef[];
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Primitive field node (non-'blocks'): DBBase + kept fields but no children
|
|
30
|
-
*/
|
|
31
|
-
type PrimitiveFieldNode<TField extends FieldDef> =
|
|
32
|
-
DBBase & {
|
|
33
|
-
readonly name: TField["name"];
|
|
34
|
-
readonly type: TField["type"];
|
|
35
|
-
// no children (leaf), no nested fields
|
|
36
|
-
readonly children?: [];
|
|
37
|
-
readonly fields?: {}; // empty object for leaf
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* For 'blocks' typed field, we need:
|
|
42
|
-
* - the block node representing the 'blocks' wrapper (has DBBase props)
|
|
43
|
-
* - its 'children' are an array of BlockNodes corresponding to nested block defs supplied in the field's 'fields' array
|
|
44
|
-
* - its 'fields' property is the mapping of its own child-field names (can be empty)
|
|
45
|
-
*/
|
|
46
|
-
type BlocksFieldNode<
|
|
47
|
-
TField extends FieldDef,
|
|
48
|
-
TFieldDefs extends readonly BlockDef[]
|
|
49
|
-
> = DBBase & {
|
|
50
|
-
readonly name: TField["name"]; // e.g. "blocks" or "images"
|
|
51
|
-
readonly type: "blocks"; // literally 'blocks'
|
|
52
|
-
readonly children: BlockNodeFromBlockDefs<TFieldDefs>[]; // children are instances of the nested blocks
|
|
53
|
-
readonly fields: FieldsFromFieldDefs<TFieldDefs[number]["fields"]>; // the blocks-wrapper's own fields mapping (if any)
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Build the 'fields' object for a set of FieldDef[].
|
|
58
|
-
* Maps each field name -> either PrimitiveFieldNode or BlocksFieldNode recursively.
|
|
59
|
-
*/
|
|
60
|
-
type FieldsFromFieldDefs<TDefs> =
|
|
61
|
-
// If no fields
|
|
62
|
-
TDefs extends readonly any[]
|
|
63
|
-
? {
|
|
64
|
-
// For each field F in TDefs, map F['name'] -> node type
|
|
65
|
-
[F in ArrayElement<TDefs> as F extends { name: infer N extends string } ? N : never]:
|
|
66
|
-
F extends { type: "blocks"; fields: readonly BlockDef[] }
|
|
67
|
-
? BlocksFieldNode<F, F["fields"]>
|
|
68
|
-
: PrimitiveFieldNode<F>;
|
|
69
|
-
}
|
|
70
|
-
: {};
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* A Block node type for a particular BlockDef.
|
|
74
|
-
* - fields: mapping derived from the block's declared fields
|
|
75
|
-
* - children: by default [], because in our final shape all immediate children are placed under 'fields' of the parent.
|
|
76
|
-
* BUT for nodes that are themselves 'blocks' wrappers (i.e. appear as a Block instance of a nested block def),
|
|
77
|
-
* their 'children' will contain actual child blocks (these are handled via BlocksFieldNode above).
|
|
78
|
-
*/
|
|
79
|
-
export type BlockNode<T extends BlockDef> = DBBase & {
|
|
80
|
-
readonly name: T["name"];
|
|
81
|
-
readonly type: T["type"];
|
|
82
|
-
readonly fields: FieldsFromFieldDefs<T["fields"]>;
|
|
83
|
-
// for regular block nodes, children will usually be [] (top-level parent's children moved into fields)
|
|
84
|
-
readonly children: [];
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Construct BlockNode unions for a set of block defs (used when blocks field has multiple block subdefs)
|
|
89
|
-
*/
|
|
90
|
-
type BlockNodeFromBlockDefs<TDefs extends readonly BlockDef[]> =
|
|
91
|
-
ArrayElement<TDefs> extends infer B ? (B extends BlockDef ? BlockNode<B> : never) : never;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* The top-level forest return type when you pass a structure: it's an array of BlockNode of any top-level BlockDef
|
|
95
|
-
*/
|
|
96
|
-
export type BlockTreeFromStructure<TStructure extends readonly BlockDef[]> =
|
|
97
|
-
BlockNodeFromBlockDefs<TStructure>;
|
package/utils/buildBlocksTree.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
type Block = {
|
|
2
|
-
id: number
|
|
3
|
-
name: string
|
|
4
|
-
label: string
|
|
5
|
-
type: string
|
|
6
|
-
sort_order: number
|
|
7
|
-
value: string | null
|
|
8
|
-
options: any // JSON-parsed if necessary
|
|
9
|
-
parent_id: number | null
|
|
10
|
-
depth: number
|
|
11
|
-
// ... you can add other fields if needed
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type BlockWithChildren = Block & { fields: BlockWithChildren[] }
|
|
15
|
-
|
|
16
|
-
export function buildBlockTree(blocks: Block[]): BlockWithChildren {
|
|
17
|
-
const blockMap = new Map<number, BlockWithChildren>()
|
|
18
|
-
|
|
19
|
-
// Initialize map with all blocks and empty `fields` array
|
|
20
|
-
for (const block of blocks) {
|
|
21
|
-
blockMap.set(block.id, { ...block, fields: [] })
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const tree: BlockWithChildren[] = []
|
|
25
|
-
|
|
26
|
-
for (const block of blocks) {
|
|
27
|
-
const current = blockMap.get(block.id)!
|
|
28
|
-
|
|
29
|
-
if (block.parent_id != null) {
|
|
30
|
-
const parent = blockMap.get(block.parent_id)
|
|
31
|
-
if (parent) {
|
|
32
|
-
parent.fields.push(current)
|
|
33
|
-
} else {
|
|
34
|
-
console.warn(
|
|
35
|
-
`Parent with id ${block.parent_id} not found for block ${block.id}`,
|
|
36
|
-
)
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
tree.push(current) // top-level (root) blocks
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return tree[0]
|
|
44
|
-
}
|