Skip to content

Commit

Permalink
Update page api, update init command
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Oct 23, 2024
1 parent 3e3899a commit f31aa57
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 87 deletions.
24 changes: 12 additions & 12 deletions src/backend/Database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {reportHalt} from 'alinea/cli/util/Report'
import {Config} from 'alinea/core/Config'
import {SyncResponse, Syncable} from 'alinea/core/Connection'
import {EntryRecord, createRecord, parseRecord} from 'alinea/core/EntryRecord'
import {createId} from 'alinea/core/Id'
import {Mutation, MutationType} from 'alinea/core/Mutation'
import {PageSeed} from 'alinea/core/Page'
import {Page} from 'alinea/core/Page'
import {Resolver} from 'alinea/core/Resolver'
import {Root} from 'alinea/core/Root'
import {Schema} from 'alinea/core/Schema'
Expand Down Expand Up @@ -47,7 +48,7 @@ interface Seed {
workspace: string
root: string
filePath: string
page: PageSeed
page: Page
}

function seedKey(workspace: string, root: string, filePath: string) {
Expand Down Expand Up @@ -522,7 +523,7 @@ export class Database implements Syncable {
}

const pathData = entryPath === 'index' ? '' : entryPath
const seedData = seed ? PageSeed.data(seed.page).partial : {}
const seedData = seed ? Page.data(seed.page).fields : {}
const title = record.title ?? seedData?.title ?? ''
const entryData = {
...seedData,
Expand Down Expand Up @@ -569,13 +570,13 @@ export class Database implements Syncable {
const {i18n} = Root.data(root)
const locales = i18n?.locales ?? [undefined]
for (const locale of locales) {
const pages: Array<readonly [string, PageSeed]> = entries(root)
const pages: Array<readonly [string, Page]> = entries(root)
const target = locale ? `/${locale}` : '/'
while (pages.length > 0) {
const [pagePath, page] = pages.shift()!
const path = pagePath.split('/').map(slugify).join('/')
if (!PageSeed.isPageSeed(page)) continue
const {type} = PageSeed.data(page)
if (!Page.isPage(page)) continue
const {type} = Page.data(page)
const filePath = paths.join(target, path) + '.json'
const typeName = typeNames.get(type)
if (!typeName) continue
Expand All @@ -589,7 +590,7 @@ export class Database implements Syncable {
})
const children = entries(page).map(
([childPath, child]) =>
[paths.join(path, childPath), child as PageSeed] as const
[paths.join(path, childPath), child as Page] as const
)
pages.push(...children)
}
Expand Down Expand Up @@ -691,8 +692,7 @@ export class Database implements Syncable {
seenVersions.push(`${entry.id}.${entry.phase}`)
inserted.push(`${entry.id}.${entry.phase}`)
} catch (e: any) {
console.info(`> skipped ${file.filePath}${e.message}`)
console.error(e)
reportHalt(`${e.message} @ ${file.filePath}`)
process.exit(1)
}
}
Expand All @@ -705,7 +705,7 @@ export class Database implements Syncable {
for (const seed of this.seed.values()) {
const key = seedKey(seed.workspace, seed.root, seed.filePath)
if (seenSeeds.has(key)) continue
const {type, partial} = PageSeed.data(seed.page)
const {type, fields} = Page.data(seed.page)
const typeName = typeNames.get(type)
if (!typeName) continue
const root = this.config.workspaces[seed.workspace][seed.root]
Expand All @@ -724,8 +724,8 @@ export class Database implements Syncable {
type: typeName,
index: 'a0',
seeded: seed.filePath,
title: partial.title ?? '',
data: partial
title: fields.title ?? '',
data: fields
}),
seed,
seed
Expand Down
62 changes: 35 additions & 27 deletions src/backend/test/Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,15 @@ export function createExample() {
pages: root('Pages', {
contains: ['Page', 'Container'],
entries: {
entry1: page(Page, {title: 'Test title'}),
entry2: page(
Container,
{title: 'Entry 2'},
{
entry3: page(Page, {title: 'Entry 3'})
entry1: page({type: Page, fields: {title: 'Test title'}}),
entry2: page({
type: Container,
fields: {title: 'Entry 2'},
children: {
entry3: page({type: Page, fields: {title: 'Entry 3'}})
}
),
container1: page(Container, {title: 'Container 1'})
}),
container1: page({type: Container, fields: {title: 'Container 1'}})
}
}),
multiLanguage: root('Multi language', {
Expand All @@ -190,29 +190,37 @@ export function createExample() {
locales: ['en', 'fr']
},
entries: {
localised1: page(Page, {title: 'Test title'}),
localised2: page(
Container,
{title: 'Entry 2'},
{
localised3: page(Page, {title: 'Entry 3'})
localised1: page({type: Page, fields: {title: 'Test title'}}),
localised2: page({
type: Container,
fields: {title: 'Entry 2'},
children: {
localised3: page({type: Page, fields: {title: 'Entry 3'}})
}
)
})
}
}),
media: createMediaRoot({
dir: page(MediaLibrary, {title: 'Media folder'}),
'file1.png': page(MediaFile, {
title: 'File 1',
path: 'file1.png',
extension: '.png',
size: 1000,
width: 120,
height: 120,
hash: 'hash1',
preview: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAIAAAC2BqGFAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHIklEQVR4nO2bz2sTTxTAZ2aTLCYhaWstUlqlHlSwVm0UlaKCPxAhFg9FD3qp1Iul5KoeelD6N0g9FKWK3qLSgqKCB1F6SNEmxYqChRyqptJaZVs3uzsehuTbb6Cb7SZ5E+37HMIWJtO3n32ZnXm7QznnBKk8THYAawUUDQSKBgJFA4GigUDRQKBoIFA0ECgaCBQNBIoGAkUDgaKBQNFAoGggUDQQKBoIFA0EigYCRQOBooFA0UCgaCBQNBAoGggUDQSKBgJFA4GigUDRQKBoIFA0ECgaCBQNBIoGAkUDgaKB8MgOoBDLsiilnHPxSQjRdX12dnZubk7XdcaY3+/fsGFDTU0NpZQQIlqKA0IIpVT8uVLnnHPGmE2bClF1ovO+0un0o0ePnj9/Pj4+vrCwsLi4aJomIcTn8/n9/sbGxoMHD54+ffrEiROKoiiKIr67sLCwtLS0UueWZRFCwuFwIBCAOqEcvGoQ6Waa5sePH/v6+urr60XqCfLHhBDxp6IoXq83EokMDw9ns1nDMLLZ7JUrV2pXpq6ubv369SMjI/BnVy2ihab5+flr164Fg0HnP21KqaIohw4dSqVSuq7HYrGiX4nH4/AnWC1DB2MskUhcunQpmUyKzOXOtvRyzimlr1+/Pnbs2J07dyodp3vgr20BpmlalvX48WMxVrg4BZH+jLFwONzR0VG0/RrNaM75w4cPz58/r+s6d7Uxnefun5qmvXnzptwBlgeZosWlfvXqVU9Pj7DsTnS+N9M0S+mhosgUbZrmzMxMT0/Pz58/SS4xXSOmblWLtJUh59yyr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4eHhpqYmxliJW0tEh+5e/AVAZljintbS0hKPxzdu3Fj6mFu1lkmVbH/btWvXkydPtm7d6vouJ65ZY2NjZ2dnBQIsA/JFixFjx44dL1++7OrqUlV1VSOAeG2MUtrW1vbixYtNmzZVNFrXyBct4JzX1dXdu3fv9u3b27Ztc35v5JyL18aePXvW3NxctaOH/K0VAnEzJIScPXv21KlT8Xj85s2biUQiv27Mr/2Wb4Lz+/1dXV2XL19ub28X16apqcmm7sE5VxQlHA6DnNP/kLAEtydvlnOuadrU1NTY2NinT5++fv2qaZqiKDU1Nc3Nza2trZFIRExXCkZ2myFeTMPzFxWSqhOdj2clX3zZ7uX8ZmYnXyzooWwRO6PqRP+rVOmt498DRQOBooFA0UCgaCBQNBAoGggUDQSKBgJFA4GigUDRQKBoIFA0ECgaCBQNBIoGAkUDgaKBQNFAoGggUDQQKBoIFA0EigYCRQOBooFA0UCgaCBQNBAoGggUDQSKBgJFA4GigUDRQKBoIP4AP2+to8vlbgYAAAAASUVORK5CYII=`,
averageColor: '#ffffff',
focus: {x: 0.5, y: 0.5}
dir: page({
type: MediaLibrary,
fields: {title: 'Media folder'},
children: {
'file1.png': page({
type: MediaFile,
fields: {
title: 'File 1',
path: 'file1.png',
extension: '.png',
size: 1000,
width: 120,
height: 120,
hash: 'hash1',
preview: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAIAAAC2BqGFAAAACXBIWXMAAAsTAAALEwEAmpwYAAAHIklEQVR4nO2bz2sTTxTAZ2aTLCYhaWstUlqlHlSwVm0UlaKCPxAhFg9FD3qp1Iul5KoeelD6N0g9FKWK3qLSgqKCB1F6SNEmxYqChRyqptJaZVs3uzsehuTbb6Cb7SZ5E+37HMIWJtO3n32ZnXm7QznnBKk8THYAawUUDQSKBgJFA4GigUDRQKBoIFA0ECgaCBQNBIoGAkUDgaKBQNFAoGggUDQQKBoIFA0EigYCRQOBooFA0UCgaCBQNBAoGggUDQSKBgJFA4GigUDRQKBoIFA0ECg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ff',
focus: {x: 0.5, y: 0.5}
}
})
}
})
})
}
Expand Down
12 changes: 5 additions & 7 deletions src/cli/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ export async function init(options: InitOptions) {
path.join(cwd, 'content/pages/welcome.json'),
JSON.stringify(
{
id: createId(),
type: 'Page',
title: 'Welcome',
alinea: {
index: 'a0',
seeded: 'welcome.json'
}
_id: createId(),
_type: 'Page',
_index: 'a0',
_seeded: 'welcome.json',
title: 'Welcome'
},
null,
2
Expand Down
9 changes: 5 additions & 4 deletions src/cli/static/init/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export const cms = createCMS({
pages: Config.root('Example project', {
contains: ['Page'],
entries: {
welcome: Config.page(
Page({
welcome: Config.page({
type: Page,
fields: {
title: 'Welcome'
})
)
}
})
}
}),
media: Config.media()
Expand Down
2 changes: 1 addition & 1 deletion src/core/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {IcRoundDescription} from 'alinea/ui/icons/IcRoundDescription'
import {IcRoundShare} from 'alinea/ui/icons/IcRoundShare'
import {FieldsDefinition, Type, TypeConfig, type} from './Type.js'

export interface Document {
export type Document = {
title: TextField
path: PathField
metadata: MetadataField
Expand Down
4 changes: 2 additions & 2 deletions src/core/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {Config} from './Config.js'
import {EntryFields} from './EntryFields.js'
import {Expr} from './Expr.js'
import {Filter} from './Filter.js'
import {PageSeed} from './Page.js'
import {Page} from './Page.js'
import {PreviewRequest} from './Preview.js'
import {Resolver} from './Resolver.js'
import {Type} from './Type.js'
import {hasExact} from './util/Checks.js'
import {Expand} from './util/Types.js'

export type Location = Root | Workspace | PageSeed | Array<string>
export type Location = Root | Workspace | Page | Array<string>
type EmptyObject = Record<PropertyKey, never>

type FieldsOf<Types> = Types extends Type<infer V>
Expand Down
49 changes: 23 additions & 26 deletions src/core/Page.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import {Type} from './Type.js'
import {FieldsDefinition, Type} from './Type.js'

export interface PageSeedData {
export interface PageData {
type: Type
partial: Record<string, any>
fields: Record<string, any>
}

export type PageSeed<
Definition = object,
Children extends Record<string, PageSeed> = Record<string, PageSeed<any, any>>
export type Page<
Children extends Record<string, Page> = Record<string, never>
> = Children & {
[PageSeed.Data]: PageSeedData
[Page.Data]: PageData
}

export namespace PageSeed {
export namespace Page {
export const Data = Symbol.for('@alinea/Page.Data')

export function data(page: PageSeed): PageSeedData {
return page[PageSeed.Data]
export function data(page: Page): PageData {
return page[Page.Data]
}

export function isPageSeed(page: any): page is PageSeed {
return Boolean(page && page[PageSeed.Data])
export function isPage(page: any): page is Page {
return Boolean(page && page[Page.Data])
}
}

export function page<
Definition,
Children extends Record<string, PageSeed<any, any>>
>(
type: Type<Definition>,
partial: Partial<Type.Infer<Definition>> = {},
export interface PageConfig<Definition, Children> {
type: Type<Definition>
fields?: Partial<Type.Infer<Definition>>
children?: Children
): PageSeed<Definition> {
children = children ?? ({} as Children)
}

export function page<
Fields extends FieldsDefinition,
Children extends Record<string, Page>
>(config: PageConfig<Fields, Children>): Page<Children> {
return {
...children,
[PageSeed.Data]: {
type: type,
partial: partial
}
}
...config.children,
[Page.Data]: config
} as any
}
4 changes: 2 additions & 2 deletions src/core/Root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as cito from 'cito'
import type {ComponentType} from 'react'
import {getRoot, hasRoot, HasRoot, internalRoot} from './Internal.js'
import {Label} from './Label.js'
import {PageSeed} from './Page.js'
import {Page} from './Page.js'
import {Preview} from './Preview.js'
import {Schema} from './Schema.js'
import {Type} from './Type.js'
Expand All @@ -23,7 +23,7 @@ export interface RootMeta {
}

export interface EntriesDefinition {
[key: string]: PageSeed
[key: string]: Page
}

export interface RootData extends RootMeta {
Expand Down
4 changes: 2 additions & 2 deletions src/core/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {Config} from './Config.js'
import {Expr} from './Expr.js'
import {Field} from './Field.js'
import {getExpr, hasExpr} from './Internal.js'
import {PageSeed} from './Page.js'
import {Page} from './Page.js'
import {Root} from './Root.js'
import {Type} from './Type.js'
import {entries} from './util/Objects.js'

const scopes = new WeakMap()
type Entity = Workspace | Root | Type | Field | Expr | PageSeed
type Entity = Workspace | Root | Type | Field | Expr | Page
const ENTITY_KEY = '@alinea.Entity'
const EXPR_KEY = '@alinea.Expr'

Expand Down
7 changes: 3 additions & 4 deletions src/core/media/MediaRoot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {IcRoundPermMedia} from 'alinea/ui/icons/IcRoundPermMedia'
import {PageSeed} from '../Page.js'
import {Page} from '../Page.js'
import {Root, root} from '../Root.js'

export type MediaRoot<Children extends Record<string, PageSeed>> =
Root<Children>
export type MediaRoot<Children extends Record<string, Page>> = Root<Children>

export function createMediaRoot<Children extends Record<string, PageSeed>>(
export function createMediaRoot<Children extends Record<string, Page>>(
children: Children = {} as Children
) {
return root('Media', {
Expand Down

0 comments on commit f31aa57

Please sign in to comment.