A tree table of one model's records β title with folder and page icons, status, last updated β filtered by language and search, and reordered by drag and drop or with each row's move buttons; check several siblings to move them together. It opens in a dialog from a Sort in a table button above each sortable model's own collection, and as the Sort records page in the content sidebar. DatoCMS's own collection view refuses to reorder as soon as it is filtered ("You cannot reorder elements if the collection is filteredβ¦"), which is the view editors work in on a multi-language project such as Bluestep's, with hundreds of pages per language.
It lists every model DatoCMS orders by hand β sortable and tree models
(page first, if there is one). The language picker shows only when the
project has more than one locale; All languages sorts the full list.
DatoCMS orders records by position within one parent. Writing
position: n to a record moves it to n and shifts the siblings in between
by one; its publication status and version are untouched (checked on Bank2
stage: a published page moved and moved back stayed published, with no new
version). Positions have gaps, so a move is computed relative to a neighbour
in the filtered list (src/reorder.ts):
A move is one items.update call per record. Checked siblings move as one
block in their current order (planBlockMove): the first lands next to its
new neighbour, each next one directly after the previous, each write planned
on the result of the last. A move takes the checked records under the
moved record's parent; checked records anywhere can be published together.
The page applies the same shift locally instead of reloading every record,
and reloads if a write fails. Moves are one at a time (the controls are
disabled while one saves).
Positions are absolute, so a move planned on numbers another editor has
since changed would land off. Before each move the page reads back the
current parent and position of every record under the parents involved
(hasMoved, 100 per request); if any differ it reloads and asks for the
move again instead of writing. A move made by someone else between that
check and the writes is not caught.
Dragging works across the whole tree: over a row's top or bottom third the
records land before or after it, over its middle they go into it (as its
last children). Writing parent_id with position moves a record to
another parent in one call (applyMoveTo; again no status or version
change). Nothing can go into itself or below itself. The move buttons stay
among siblings. A parent that isn't in the chosen language but
has children that are is shown, greyed out, so the tree keeps its shape; it
can't be moved from that language's list.
A record's languages are the keys of its localized fields' values, the same thing DatoCMS's "Locales" filter matches.
| File | Responsibility |
|---|---|
src/main.tsx | connect(): the sidebar item and page, the button above each sortable collection, and the dialog it opens. |
src/SorterPage.tsx | The table: model, language and search, the tree of sortable lists (dnd-kit), checkboxes, move buttons, status messages. |
src/records.ts | The CMA: the editor's client, loading a model's records (parent, position, languages, title, slug, status), writing a position, checking for others' moves, publishing. |
src/reorder.ts | The ordering rules: anchors, target positions, DatoCMS's shift, block moves, which records a language and search show, stale-place detection, publish request bodies. No imports; tested by the root pnpm test (src/__tests__/reorder.test.ts). |
src/styles.css | Layout, from the CSS variables Canvas sets from the project's theme. |
Every move works without a mouse: each row has to top, up, down
and to bottom buttons, and the handle supports dnd-kit's keyboard drag
(space to pick up, arrows, space to drop, escape to cancel) with
screen-reader announcements that name the record. A keyboard drag drops
before or after the record it ends over, never into it: its fixed steps
mostly stop mid-row, where "into" would nest a record by accident. Focus stays on the button
that moved a row (or its handle, once that button is disabled), and each
move is announced in a polite live region. Sizes are in rem and rows wrap,
so enlarged text doesn't clip.
As the TinyMCE plugin: it runs in a sandboxed iframe and asks for the
currentUserAccessToken permission, so it reads and writes as the editor
using it β their role decides which records they can move. Whoever can
publish this package can act as an editor; keep publishing in the workflow
below, with 2FA on the npm account.
cd plugins/datocms-record-sorterpnpm install # its own workspace root: never touches the app's lockfilepnpm dev # http://localhost:5173, for a private plugin's entry point URLpnpm build # tsc + vite β dist/To try it before publishing, add a private plugin (Settings β Plugins β Add
new β Create new plugin) with http://localhost:5173 as its entry point,
grant it "Access the current user's API token", and open Sort records in
the content sidebar. Try it on a sandbox environment: moves are real writes.
version in package.json in the PR.npm publish --access public),
then the repository and record-sorter-plugin.yml are added as the
package's trusted publisher on npmjs.com.