Skip to content

Commit

Permalink
[diff] Reworked string segments to match other diff types
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2020
1 parent aea76ed commit b676fe3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
14 changes: 8 additions & 6 deletions packages/@sanity/diff/src/calculate/diffString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function diffString<A>(
isChanged: false,
fromValue,
toValue,
segments: [{type: 'unchanged', text: fromValue}]
segments: [{type: 'stringSegment', action: 'unchanged', text: fromValue}]
}
}

Expand Down Expand Up @@ -59,14 +59,15 @@ function buildSegments<A>(
for (let [op, text] of dmpDiffs) {
switch (op) {
case DIFF_EQUAL:
segments.push({type: 'unchanged', text})
segments.push({type: 'stringSegment', action: 'unchanged', text})
fromIdx += text.length
toIdx += text.length
break
case DIFF_DELETE:
for (let segment of fromInput.sliceAnnotation(fromIdx, fromIdx + text.length)) {
segments.push({
type: 'removed',
type: 'stringSegment',
action: 'removed',
text: segment.text,
annotation: segment.annotation
})
Expand All @@ -76,7 +77,8 @@ function buildSegments<A>(
case DIFF_INSERT:
for (let segment of toInput.sliceAnnotation(toIdx, toIdx + text.length)) {
segments.push({
type: 'added',
type: 'stringSegment',
action: 'added',
text: segment.text,
annotation: segment.annotation
})
Expand Down Expand Up @@ -105,7 +107,7 @@ export function removedString<A>(
get segments(): StringDiffSegment<A>[] {
const segments: StringDiffSegment<A>[] = input
.sliceAnnotation(0, input.value.length)
.map(segment => ({type: 'removed', ...segment}))
.map(segment => ({type: 'stringSegment', action: 'removed', ...segment}))

return replaceProperty(this, 'segments', segments)
}
Expand All @@ -128,7 +130,7 @@ export function addedString<A>(
get segments(): StringDiffSegment<A>[] {
const segments: StringDiffSegment<A>[] = input
.sliceAnnotation(0, input.value.length)
.map(segment => ({type: 'added', ...segment}))
.map(segment => ({type: 'stringSegment', action: 'added', ...segment}))

return replaceProperty(this, 'segments', segments)
}
Expand Down
6 changes: 4 additions & 2 deletions packages/@sanity/diff/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ export type Diff<A> =
export type StringDiffSegment<A> = StringSegmentChanged<A> | StringSegmentUnchanged

export type StringSegmentChanged<A> = {
type: 'added' | 'removed'
type: 'stringSegment'
action: 'added' | 'removed'
text: string
annotation: A
}

export type StringSegmentUnchanged = {
type: 'unchanged'
type: 'stringSegment'
action: 'unchanged'
text: string
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export function AnnotatedStringDiffSegment({
}: {
segment: StringDiffSegment
}): React.ReactElement {
if (segment.type === 'added') {
if (segment.action === 'added') {
return (
<DiffAnnotation annotation={segment.annotation} as="ins" className={styles.add}>
{segment.text}
</DiffAnnotation>
)
}

if (segment.type === 'removed') {
if (segment.action === 'removed') {
return (
<DiffAnnotation annotation={segment.annotation} as="del" className={styles.remove}>
{segment.text}
Expand Down

0 comments on commit b676fe3

Please sign in to comment.