-
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.
[desk-tool] Begin basic array diff components
- Loading branch information
1 parent
dcff35a
commit 595294e
Showing
16 changed files
with
312 additions
and
26 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/@sanity/desk-tool/src/diffs/arrayDiff/ArrayDiff.tsx
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 @@ | ||
import React from 'react' | ||
import {DefaultArrayDiff} from './defaultArrayDiff' | ||
import {isPTSchemaType} from './helpers' | ||
import {PTDiff} from './ptDiff' | ||
import {ArrayDiffProps} from './types' | ||
|
||
export function ArrayDiff(props: ArrayDiffProps) { | ||
// console.log('ArrayDiff', props.schemaType) | ||
|
||
if (isPTSchemaType(props.schemaType)) { | ||
return <PTDiff diff={props.diff} items={props.items} schemaType={props.schemaType} /> | ||
} | ||
|
||
return <DefaultArrayDiff diff={props.diff} items={props.items} schemaType={props.schemaType} /> | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/@sanity/desk-tool/src/diffs/arrayDiff/defaultArrayDiff.css
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,55 @@ | ||
.root { | ||
/* border: 1px solid #ccc; */ | ||
} | ||
|
||
.itemList { | ||
display: grid; | ||
grid-template-columns: minmax(0, 1fr); | ||
grid-gap: 0.25em; | ||
} | ||
|
||
.diffItemContainer { | ||
display: flex; | ||
} | ||
|
||
.diffItemIndexes { | ||
background: #eee; | ||
width: 1.25em; | ||
font-size: 13px; | ||
line-height: 16px; | ||
font-weight: 600; | ||
} | ||
|
||
.diffItemBox { | ||
flex: 1; | ||
min-width: 0; | ||
/* border-left: 1px solid #ccc; */ | ||
padding-left: 0.25em; | ||
} | ||
|
||
.item { | ||
background: #f1f3f6; | ||
margin: 0; | ||
padding: 0.5em; | ||
font-size: 14px; | ||
border-radius: 4px; | ||
overflow: auto; | ||
} | ||
|
||
.addedItem { | ||
composes: item; | ||
background: #d0f4dc; | ||
} | ||
|
||
.removedItem { | ||
composes: item; | ||
background: #fcd8d5; | ||
} | ||
|
||
.arrayDiffIndexes { | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
} |
104 changes: 104 additions & 0 deletions
104
packages/@sanity/desk-tool/src/diffs/arrayDiff/defaultArrayDiff.tsx
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,104 @@ | ||
import {ItemDiff} from '@sanity/diff' | ||
import React from 'react' | ||
import {Annotation} from '../../panes/documentPane/history/types' | ||
import {ArrayDiffProps} from './types' | ||
|
||
import styles from './defaultArrayDiff.css' | ||
|
||
export function DefaultArrayDiff(props: ArrayDiffProps) { | ||
return ( | ||
<div className={styles.root}> | ||
<div className={styles.itemList}> | ||
{props.diff.items.map((diffItem, diffItemIndex) => ( | ||
<div className={styles.diffItemContainer} key={diffItemIndex}> | ||
<div className={styles.diffItemIndexes}> | ||
<ArrayDiffIndexes fromIndex={diffItem.fromIndex} toIndex={diffItem.toIndex} /> | ||
</div> | ||
<div className={styles.diffItemBox}> | ||
<DefaultArrayDiffItem | ||
diff={diffItem} | ||
metadata={props.items && props.items[diffItemIndex]} | ||
/> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
// eslint-disable-next-line complexity | ||
function ArrayDiffIndexes({fromIndex, toIndex}: {fromIndex?: number; toIndex?: number}) { | ||
if (fromIndex === undefined && toIndex === undefined) { | ||
return <span className={styles.arrayDiffIndexes} /> | ||
} | ||
|
||
if (fromIndex !== undefined && toIndex === undefined) { | ||
// `fromIndex` only (removed) | ||
return ( | ||
<span className={styles.arrayDiffIndexes}> | ||
<span>{fromIndex}</span> | ||
</span> | ||
) | ||
} | ||
|
||
if (fromIndex === undefined && toIndex !== undefined) { | ||
// `toIndex` only | ||
return ( | ||
<span className={styles.arrayDiffIndexes}> | ||
<span>{toIndex}</span> | ||
</span> | ||
) | ||
} | ||
|
||
if (fromIndex !== undefined && toIndex !== undefined && fromIndex < toIndex) { | ||
return ( | ||
<span className={styles.arrayDiffIndexes}> | ||
<span>{fromIndex}</span> | ||
<span>↓</span> | ||
<span>{toIndex}</span> | ||
</span> | ||
) | ||
} | ||
|
||
return ( | ||
<span className={styles.arrayDiffIndexes}> | ||
<span>{toIndex}</span> | ||
<span>↑</span> | ||
<span>{fromIndex}</span> | ||
</span> | ||
) | ||
} | ||
|
||
function DefaultArrayDiffItem(props: { | ||
diff: ItemDiff<Annotation> | ||
metadata?: {fromType?: string; toType?: string} | ||
}) { | ||
const {diff} = props | ||
const metadata = props.metadata || {fromType: undefined, toType: undefined} | ||
|
||
if (diff.type === 'added') { | ||
return ( | ||
<pre className={styles.addedItem}> | ||
Added array item ({metadata.toType}): {JSON.stringify(diff, null, 2)} | ||
</pre> | ||
) | ||
} | ||
|
||
if (diff.type === 'removed') { | ||
return ( | ||
<pre className={styles.removedItem}> | ||
Removed array item ({metadata.fromType}): {JSON.stringify(diff, null, 2)} | ||
</pre> | ||
) | ||
} | ||
|
||
// @todo: render moved items? | ||
return ( | ||
<pre className={styles.item}> | ||
Unchanged item ({metadata.toType}): {JSON.stringify(diff, null, 2)} | ||
</pre> | ||
) | ||
|
||
// return null | ||
} |
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,8 @@ | ||
export function isPTSchemaType(schemaType: any) { | ||
return ( | ||
schemaType.name === 'array' && | ||
schemaType.of && | ||
schemaType.of.length === 1 && | ||
schemaType.of.filter(t => t.name === 'block').length | ||
) | ||
} |
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 './arrayDiff' |
Empty file.
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,6 @@ | ||
import React from 'react' | ||
import {ArrayDiffProps} from './types' | ||
|
||
export function PTDiff(props: ArrayDiffProps) { | ||
return <div>Portable Text Diff</div> | ||
} |
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,8 @@ | ||
import {ArrayDiff} from '@sanity/diff' | ||
import {Annotation} from '../../panes/documentPane/history/types' | ||
|
||
export interface ArrayDiffProps { | ||
diff: ArrayDiff<Annotation> | ||
items?: {fromType?: string; toType?: string}[] | ||
schemaType: any | ||
} |
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
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
.container { | ||
display: flex; | ||
flex: 1; | ||
min-width: 0; | ||
align-items: stretch; | ||
overflow: hidden; | ||
} | ||
|
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
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
58 changes: 58 additions & 0 deletions
58
packages/@sanity/desk-tool/src/panes/documentPane/changesPanel/schemaUtils/resolveType.ts
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,58 @@ | ||
const toString = Object.prototype.toString | ||
// Copied from https://github.com/ForbesLindesay/type-of, but inlined to have fine grained control | ||
|
||
// eslint-disable-next-line complexity | ||
export function resolveJSType(val) { | ||
switch (toString.call(val)) { | ||
case '[object Function]': | ||
return 'function' | ||
case '[object Date]': | ||
return 'date' | ||
case '[object RegExp]': | ||
return 'regexp' | ||
case '[object Arguments]': | ||
return 'arguments' | ||
case '[object Array]': | ||
return 'array' | ||
case '[object String]': | ||
return 'string' | ||
default: | ||
} | ||
|
||
if (typeof val == 'object' && val && typeof val.length == 'number') { | ||
try { | ||
// eslint-disable-next-line max-depth | ||
if (typeof val.callee == 'function') { | ||
return 'arguments' | ||
} | ||
} catch (ex) { | ||
// eslint-disable-next-line max-depth | ||
if (ex instanceof TypeError) { | ||
return 'arguments' | ||
} | ||
} | ||
} | ||
|
||
if (val === null) { | ||
return 'null' | ||
} | ||
|
||
if (val === undefined) { | ||
return 'undefined' | ||
} | ||
|
||
if (val && val.nodeType === 1) { | ||
return 'element' | ||
} | ||
|
||
if (val === Object(val)) { | ||
return 'object' | ||
} | ||
|
||
return typeof val | ||
} | ||
|
||
export function resolveTypeName(value) { | ||
const jsType = resolveJSType(value) | ||
return (jsType === 'object' && '_type' in value && value._type) || jsType | ||
} |
Oops, something went wrong.