Skip to content

Commit

Permalink
Fix sorting issues caused by inconsistent sorting and duplicate fract…
Browse files Browse the repository at this point in the history
…ional indexes
  • Loading branch information
dmerckx committed Jun 20, 2024
1 parent dc85369 commit b4288eb
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 17 deletions.
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/en/root-folder-a.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2U6iOBNZTvULq6hB4WYaoqMDRYb",
"_type": "Folder",
"_index": "a0",
"_index": "a2",
"_i18nId": "2U6iOGsMb7ORnRwQi9ZPhvV9vel",
"_root": "pages",
"title": "Root folder A"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/en/root-folder-b.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2U6ifs4lY2IKDaUlm6yawnv8Hkn",
"_type": "Folder",
"_index": "a0",
"_index": "a3",
"_i18nId": "2U6ifmP32fhM3IfRt9s3r4xxTDh",
"_root": "pages",
"title": "Root folder B"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/en/seededpath.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2dgecbJqTltGUOJZXpmkVTtLfnL",
"_type": "Page",
"_index": "a0",
"_index": "Zz",
"_i18nId": "2dgeccRswr67Tr7c5S4cphuEz6I",
"_seeded": "/en/seededpath.json",
"_root": "pages"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/fr/root-page-2.draft.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2U6jHZqpDp6Frzlvr6EnFHmXOsV",
"_type": "Page",
"_index": "a0",
"_index": "a4",
"_i18nId": "2U6jEk8GPCcaQXQhCHGPgppet6w",
"_root": "pages",
"title": "Root page 2 FR"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/fr/seededpath.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2dgecbFuUPezSbPzm7ho8Vu5nHW",
"_type": "Page",
"_index": "a0",
"_index": "Zz",
"_i18nId": "2dgeccRswr67Tr7c5S4cphuEz6I",
"_seeded": "/fr/seededpath.json",
"_root": "pages"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/nl/root-folder-nl.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2clUBd0nHqL3ny8ocC4pUOstRkk",
"_type": "Folder",
"_index": "a0",
"_index": "a2",
"_i18nId": "2U6iOGsMb7ORnRwQi9ZPhvV9vel",
"_root": "pages",
"title": "Root folder A"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/nl/root-page-2.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2U6jEli79o6HPNiKFlLw8oLIi4S",
"_type": "Page",
"_index": "a0",
"_index": "a4",
"_i18nId": "2U6jEk8GPCcaQXQhCHGPgppet6w",
"_root": "pages",
"title": "Root page 2"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/nl/seededpath.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2dgecaB3gLGWYiPJiN9W7fSLGaC",
"_type": "Page",
"_index": "a0",
"_index": "Zz",
"_i18nId": "2dgeccRswr67Tr7c5S4cphuEz6I",
"_seeded": "/nl/seededpath.json",
"_root": "pages"
Expand Down
2 changes: 1 addition & 1 deletion apps/dev/content/primary/pages/nl/unicộde-tést.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": "2b01fKO0WytNOYxXx2hvaf3k0MK",
"_type": "Page",
"_index": "a0",
"_index": "a5",
"_i18nId": "2b01fFETBoAaydHPddiZ22CljH9",
"_root": "pages",
"title": "Unicộde tést",
Expand Down
3 changes: 2 additions & 1 deletion src/backend/resolver/EntryResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ export class EntryResolver {

orderBy(ctx: ResolveContext, orderBy: Array<pages.OrderBy>): Array<OrderBy> {
return orderBy.map(({expr, order}) => {
const e = new Expr(this.expr(ctx, expr)).collate('NOCASE')
// TODO: reintroduce .collate('NOCASE') when sorting on title instead of index
const e = new Expr(this.expr(ctx, expr))
return {
expr: e[Expr.Data],
order: order === 'Desc' ? OrderDirection.Desc : OrderDirection.Asc
Expand Down
47 changes: 40 additions & 7 deletions src/dashboard/atoms/EntryAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {Mutation, MutationType} from 'alinea/core/Mutation'
import {Type} from 'alinea/core/Type'
import {Projection} from 'alinea/core/pages/Projection'
import {entryFileName} from 'alinea/core/util/EntryFilenames'
import {generateKeyBetween} from 'alinea/core/util/FractionalIndexing'
import {
generateKeyBetween,
generateNKeysBetween
} from 'alinea/core/util/FractionalIndexing'
import {entries} from 'alinea/core/util/Objects'
import DataLoader from 'dataloader'
import {atom, useAtomValue} from 'jotai'
Expand Down Expand Up @@ -206,19 +209,49 @@ export function useEntryTreeProvider(): AsyncTreeDataLoader<EntryTreeItem> & {
console.log('Todo: move entries')
return
}
const previous = parent.getChildren()[childIndex - 1]
const next = parent.getChildren()[childIndex]
const previousIndex = previous?.getItemData()?.index ?? null
const nextIndex = next?.getItemData()?.index ?? null
const children = parent.getChildren()
const previous = children[childIndex - 1]
const previousIndexKey = previous?.getItemData()?.index ?? null
const next = children
.slice(childIndex)
.find(
entry => !entry || entry?.getItemData().index !== previousIndexKey
)
const nextChildIndex = next ? children.indexOf(next) : undefined
const nextIndexKey = next?.getItemData()?.index ?? null

try {
const newIndex = generateKeyBetween(previousIndex, nextIndex)
const brokenChildren = children.slice(childIndex, nextChildIndex)
const newIndexKey = generateKeyBetween(previousIndexKey, nextIndexKey)
const mutations: Array<Mutation> = []

if (brokenChildren.length > 0) {
// Start by generating new, clean keys for broken children (children with duplicate keys)
const newKeys = generateNKeysBetween(
newIndexKey,
nextIndexKey,
brokenChildren.length
)
for (let i = 0; i < brokenChildren.length; i++) {
const child = brokenChildren[i]
const correctedIndexKey = newKeys[i]
for (const entry of child.getItemData().entries) {
mutations.push({
type: MutationType.Order,
entryId: entry.entryId,
file: entryFileName(config, entry, entry.parentPaths),
index: correctedIndexKey
})
}
}
}

for (const entry of dropping.getItemData().entries) {
mutations.push({
type: MutationType.Order,
entryId: entry.entryId,
file: entryFileName(config, entry, entry.parentPaths),
index: newIndex
index: newIndexKey
})
}
mutate(mutations, true)
Expand Down

0 comments on commit b4288eb

Please sign in to comment.