Git Product home page Git Product logo

orderable-document-list's Introduction

@sanity/orderable-document-list

Drag-and-drop Document Ordering without leaving the Editing surface.

2022-04-26 12 23 39

This plugin aims to be OS-like in that you can select and move multiple documents by holding shift and clicking a second item, and toggling on/off selections by holding command/control.

Requirements

A Sanity Studio with Desk Structure configured.

Installation

sanity install @sanity/orderable-document-list

1. Import the Document List into your Desk Structure

The config parameter requires type and also accepts title and icon.

// ./src/desk-structure/index.js (or similar)

import S from '@sanity/desk-tool/structure-builder'
import {orderableDocumentListDeskItem} from '@sanity/orderable-document-list'

export default () =>
  S.list()
    .title('Content')
    .items([
      // Minimum required configuration
      orderableDocumentListDeskItem({type: 'category'}),

      // Optional configuration
      orderableDocumentListDeskItem({
        type: 'project',
        title: 'Projects',
        icon: Paint
        // See notes on adding a `filter` below
        filter: `__i18n_lang == $lang`,
        params: {
          lang: 'en_US'
        },
      }),

      // ... all other desk items

** Caution: Adding a filter **

By default, the plugin will display all documents of the same type. However, you may wish to add a filter to reduce this down to a subset of documents. A typical usecase is for internationalized document schema to order documents of just the base language version.

However, order ranks are still computed based on all documents of the same type. Creating multiple lists with different filter settings could produce unexpected results.

2. Add the orderRank field to your schema(s).

You must pass in the type of the schema, to create an initialValue value.

Additionally, pass in overrides for the field, such as making it visible by passing hidden: false.

You cannot override the name, type or initialValue attributes.

Example:

// ./src/schema/category.js (or similar)

import {
  orderRankField,
  orderRankOrdering,
} from '@sanity/orderable-document-list';

export default {
  name: 'category',
  title: 'Category',
  type: 'document',
  // Optional: The plugin also exports a set of 'orderings' for use in other Document Lists
  orderings: [orderRankOrdering],
  fields: [
    // Minimum required configuration
    orderRankField({ type: 'category' }),

    // OR you can override _some_ of the field settings
    orderRankField({ type: 'category', hidden: false }),

    // ...all other fields

3. Generate initial Ranks

On first load, your Document list will not have any Order. You can select "Reset Order" from the menu in the top right of the list. You can also re-run this at any time.

The orderRankField will query the last Document to set an initialValue to come after it. New Documents always start at the end of the Ordered list.

Querying Ordered Documents

Now when writing a GROQ Query for Documents, use the orderRank field value to return ordered results:

*[_type == "category"]|order(orderRank)

Notes

To get this first version out the door there are few configuration settings and a lot of opinions. Such as:

  • The name of the orderRank field is constant
  • The ability to only sort across all Documents of a type

Feedback and PRs welcome :)

How it works

Uses kvandakes's TypeScript implementation of Jira's Lexorank to create a "lexographical" Document order.

Put simply it updates the position of an individual – or many – Documents in an ordered list without updating any others. It's fast.

License

MIT © Simeon Griggs See LICENSE

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.