Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing choosing the insert order when creating a new entry. You can… #404

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export namespace Type {
export function contains(type: Type): Array<string | Type> {
return getType(type).contains ?? []
}
export function insertOrder(type: Type): 'top' | 'bottom' | 'free' {
return getType(type).insertOrder ?? 'free'
}

export function isHidden(type: Type): boolean {
return Boolean(getType(type).hidden)
Expand Down Expand Up @@ -160,6 +163,9 @@ export interface TypeConfig<Definition> {
/** A React component used to view a thumbnail of this type in the dashboard */
summaryThumb?: View<SummaryProps>

/** The position where new children will be inserted */
insertOrder?: 'top' | 'bottom' | 'free'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go for 'first' | 'last' ...


entryUrl?: (meta: EntryUrlMeta) => string
}

Expand Down
53 changes: 51 additions & 2 deletions src/dashboard/view/entry/NewEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ const parentData = {
parents: {},
select: Entry.path
},
childrenIndex: {
firstChildIndex: {
first: true as const,
children: {},
select: Entry.index,
orderBy: {asc: Entry.index, caseSensitive: true}
},
lastChildIndex: {
first: true as const,
children: {},
select: Entry.index,
orderBy: {desc: Entry.index, caseSensitive: true}
}
}

Expand Down Expand Up @@ -133,6 +139,8 @@ function NewEntryForm({parentId}: NewEntryProps) {
}

const typeField = useMemo(() => {
console.log('Config', config)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cleanup needed :)

console.log('Parent', parent)
const typeField: SelectField<string> = select('Select type', {
options: {}
}) as any
Expand All @@ -155,6 +163,35 @@ function NewEntryForm({parentId}: NewEntryProps) {
})
}, [])

const insertOrderField = useMemo(() => {
const insertOrderField: SelectField<'top' | 'bottom'> = select(
'Insert order',
{
initialValue: 'top',
options: {
top: 'At the top of the list',
bottom: 'At the bottom of the list'
}
}
)
return track.options(insertOrderField, async get => {
const selectedParent = get(parentField)
const parentId = selectedParent?.[EntryReference.entry]
const parent = await graph.get({
select: {
type: Entry.type
},
id: parentId,
status: 'preferDraft'
})
const parentType = parent && config.schema[parent.type]
const parentInsertOrder = Type.insertOrder(parentType)
return {
hidden: parentInsertOrder !== 'free'
}
})
}, [])

const copyFromField = useMemo(() => {
const copyFromField = entry('Copy content from')
return track.options(copyFromField, get => {
Expand Down Expand Up @@ -183,6 +220,7 @@ function NewEntryForm({parentId}: NewEntryProps) {
parent: parentField,
title: titleField,
type: typeField,
order: insertOrderField,
copyFrom: copyFromField
}
}),
Expand Down Expand Up @@ -229,6 +267,7 @@ function NewEntryForm({parentId}: NewEntryProps) {
status: 'preferPublished'
})
: null
const parentType = parent && config.schema[parent.type]
const parentPaths = parent ? parent.parentPaths.concat(parent.path) : []
const filePath = entryFilepath(config, data, parentPaths)
const childrenDir = entryChildrenDir(config, data, parentPaths)
Expand All @@ -243,6 +282,16 @@ function NewEntryForm({parentId}: NewEntryProps) {
status: 'preferPublished'
})
: Type.initialValue(entryType)

const parentInsertOrder = parentType ? Type.insertOrder(parentType) : 'free'
let index = generateKeyBetween(null, parent?.firstChildIndex || null)
if (
parentInsertOrder === 'bottom' ||
(parentInsertOrder === 'free' && form.data().order === 'bottom')
) {
index = generateKeyBetween(parent?.lastChildIndex || null, null)
}

const entry = await createEntryRow(config, {
id: id,
...data,
Expand All @@ -251,7 +300,7 @@ function NewEntryForm({parentId}: NewEntryProps) {
path,
title,
url,
index: generateKeyBetween(null, parent?.childrenIndex || null),
index,
parentId: parent?.id ?? null,
seeded: null,
level: parent ? parent.level + 1 : 0,
Expand Down
Loading