-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[field] Add new field module for field diff/input logic/types
- Loading branch information
Showing
14 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../../.babelrc", | ||
"presets": ["@babel/react", "@babel/typescript"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": ["../../../.eslintrc-ts.js"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage | ||
.grunt | ||
|
||
# Dependency directories | ||
node_modules | ||
|
||
# Compiled code | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.babelrc | ||
.editorconfig | ||
.eslintignore | ||
.eslintrc | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2016 - 2020 Sanity.io | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# @sanity/diff: Tools for diffing data structures | ||
|
||
`@sanity/diff` is a library for calculating and presenting _diffs_ of content. | ||
|
||
## Concepts and architecture | ||
|
||
- The main data structure is `Diff` which represents a difference between two versions. This is a | ||
nested data structure so if it's an `ObjectDiff`, then its children will have `Diff` as well. | ||
|
||
The `Diff` is built on top of the _unchanged_, _added_ and _removed_ primitives. This means that | ||
it will contain both versions at the same time and it's always trivial to recreate the old/new | ||
version (by ignoring the added/removed parts of the diff). | ||
|
||
Note that for arrays and objects, unchanged/added/removed only have a "shallow" meaning. An | ||
`ObjectDiff` will have a *unchanged field* if the field was present in both the old and new | ||
version - regardless of whether there's any internal changes. | ||
|
||
- `Diff` also supports _annotations_. These contain information about when a change was introduced | ||
and who was responsible for it. | ||
|
||
- To construct a `Diff` you need to represent the versions as `Input` types and use `diffInput(from,to)` | ||
to create the diff. The primary reason for a separate `Input` type is to support passing along | ||
annotations. In addition, this allows us to optimize based on the object equality of the inputs. | ||
|
||
- There are multiple ways of _presenting_ a diff: Sometimes you want to only show the fields that | ||
has changed, and other times you want to show the full new (or old!) document interspersed with the | ||
changes. | ||
|
||
This library does _not_ contain any UI components, but instead provides various | ||
presentation-related helper functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/diff') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './src/diff' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"name": "@sanity/field", | ||
"version": "1.150.1", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"author": "Sanity.io <hello@sanity.io>", | ||
"engines": { | ||
"node": ">=10.0.0" | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "jest", | ||
"posttest": "tsc --noEmit", | ||
"clean": "rimraf lib coverage" | ||
}, | ||
"keywords": [ | ||
"sanity", | ||
"cms", | ||
"headless", | ||
"realtime", | ||
"content", | ||
"diff" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@sanity/diff": "1.150.1", | ||
"@sanity/react-hooks": "1.150.1", | ||
"diff-match-patch": "^1.0.4" | ||
}, | ||
"devDependencies": { | ||
"rimraf": "^2.7.1", | ||
"typescript": "^3.7.3" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sanity-io/sanity.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/sanity-io/sanity/issues" | ||
}, | ||
"homepage": "https://www.sanity.io/", | ||
"jest": { | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)$", | ||
"testURL": "http://localhost", | ||
"transform": { | ||
"^.+\\.ts$": "ts-jest" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js", | ||
"json", | ||
"node" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"/lib/" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules/", | ||
"/test/", | ||
"/lib/" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.css' { | ||
const styleMap: {[key: string]: string} | ||
export = styleMap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declare module 'part:*' | ||
declare module 'all:part:*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import {ComponentType} from 'react' | ||
import { | ||
// Base diffs | ||
ArrayDiff as AgnosticArrayDiff, | ||
BooleanDiff as AgnosticBooleanDiff, | ||
NullDiff as AgnosticNullDiff, | ||
NumberDiff as AgnosticNumberDiff, | ||
ObjectDiff as AgnosticObjectDiff, | ||
StringDiff as AgnosticStringDiff, | ||
TypeChangeDiff as AgnosticTypeChangeDiff, | ||
|
||
// Diff internals | ||
ItemDiff as AgnosticItemDiff, | ||
StringSegmentChanged as AgnosticStringSegmentChanged, | ||
StringSegmentUnchanged as AgnosticStringSegmentUnchanged | ||
} from '@sanity/diff' | ||
|
||
/** | ||
* History timeline / chunking | ||
*/ | ||
export type ChunkType = 'create' | 'editDraft' | 'delete' | 'publish' | 'unpublish' | 'discardDraft' | ||
|
||
export type Chunk = { | ||
id: string | ||
type: ChunkType | ||
start: number | ||
end: number | ||
startTimestamp: Date | ||
endTimestamp: Date | ||
authors: Set<string> | ||
} | ||
|
||
/** | ||
* Annotation connected to a change | ||
*/ | ||
export type AnnotationDetails = { | ||
chunk: Chunk | ||
author: string | ||
} | ||
|
||
export type Annotation = AnnotationDetails | null | ||
|
||
/** | ||
* Diff types with annotation type set automatically | ||
*/ | ||
export type ArrayDiff<V = unknown> = AgnosticArrayDiff<Annotation, V> | ||
export type BooleanDiff = AgnosticBooleanDiff<Annotation> | ||
export type NullDiff = AgnosticNullDiff<Annotation> | ||
export type NumberDiff = AgnosticNumberDiff<Annotation> | ||
export type ObjectDiff<T extends object = object> = AgnosticObjectDiff<Annotation, T> | ||
export type StringDiff = AgnosticStringDiff<Annotation> | ||
export type ReferenceDiff = ObjectDiff<Reference> | ||
export type TypeChangeDiff = AgnosticTypeChangeDiff<Annotation> | ||
|
||
export type Diff<A = unknown, O extends object = object> = | ||
| ArrayDiff<A> | ||
| BooleanDiff | ||
| NullDiff | ||
| NumberDiff | ||
| ObjectDiff<O> | ||
| StringDiff | ||
| TypeChangeDiff | ||
|
||
export type StringDiffSegment = StringSegmentChanged | StringSegmentUnchanged | ||
export type StringSegmentChanged = AgnosticStringSegmentChanged<Annotation> | ||
export type StringSegmentUnchanged = AgnosticStringSegmentUnchanged | ||
|
||
export type ItemDiff = AgnosticItemDiff<Annotation> | ||
|
||
/** | ||
* Diff components | ||
*/ | ||
export type DiffComponent<T extends Diff = Diff> = ComponentType<DiffProps<T>> | ||
|
||
export type DiffProps<T extends Diff = Diff> = { | ||
diff: T | ||
schemaType: T extends ObjectDiff | ||
? ObjectSchemaType | ||
: T extends ArrayDiff | ||
? ArraySchemaType | ||
: T extends BooleanDiff | ||
? BooleanSchemaType | ||
: T extends StringDiff | ||
? StringSchemaType | ||
: T extends NumberDiff | ||
? NumberSchemaType | ||
: SchemaType | ||
} | ||
|
||
/** | ||
* Schema | ||
*/ | ||
|
||
// Note: INCOMPLETE, but good enough for diffs | ||
export interface BaseSchemaType { | ||
name: string | ||
title?: string | ||
type?: SchemaType | ||
diffComponent?: DiffComponent<any> | ||
} | ||
|
||
export interface StringSchemaType extends BaseSchemaType { | ||
jsonType: 'string' | ||
} | ||
|
||
export interface NumberSchemaType extends BaseSchemaType { | ||
jsonType: 'number' | ||
} | ||
|
||
export interface BooleanSchemaType extends BaseSchemaType { | ||
jsonType: 'boolean' | ||
} | ||
|
||
export interface ArraySchemaType<T = unknown> extends BaseSchemaType { | ||
jsonType: 'array' | ||
of: Exclude<SchemaType, ArraySchemaType>[] | ||
diffComponent?: DiffComponent<ArrayDiff<T>> | ||
} | ||
|
||
export interface ObjectField<T extends SchemaType = SchemaType> { | ||
name: string | ||
fieldset?: string | ||
type: T | ||
} | ||
|
||
export interface ObjectSchemaType<T extends object = object> extends BaseSchemaType { | ||
jsonType: 'object' | ||
fields: ObjectField[] | ||
diffComponent?: DiffComponent<ObjectDiff<T>> | ||
} | ||
|
||
export interface Reference { | ||
_ref: string | ||
_key?: string | ||
_weak?: boolean | ||
} | ||
|
||
export type ReferenceSchemaType = ObjectSchemaType<Reference> | ||
|
||
export type SchemaType<A = unknown, O extends object = object> = | ||
| ArraySchemaType<A> | ||
| BooleanSchemaType | ||
| NumberSchemaType | ||
| ObjectSchemaType<O> | ||
| StringSchemaType | ||
|
||
/** | ||
* Paths | ||
*/ | ||
export declare type PathSegment = string | number | {_key: string} | ||
export declare type Path = PathSegment[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "../../../tsconfig", | ||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.test.ts", "src/**/*.test.tsx"], | ||
"exclude": ["src/**/*.test.ts"], | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"strict": true, | ||
"jsx": "react", | ||
"plugins": [ | ||
{ | ||
"name": "typescript-plugin-css-modules", | ||
"options": { | ||
"customMatcher": "\\.css$" | ||
} | ||
} | ||
] | ||
} | ||
} |