Skip to content

Commit 589adf7

Browse files
committed
Add text-diff view
1 parent 040b343 commit 589adf7

File tree

8 files changed

+140
-15
lines changed

8 files changed

+140
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- When `src` is not explicitly specified in the config and the input data is a string, the data value is processed through `imagesrc()` and used as the `src`
3333
- Config entries with a value of `undefined` are ignored
3434
- Added a default image-like content string detection annotation in the `struct` view: when a string value is identified as image-like content, a badge is displayed before the string, showing an image preview on hover
35+
- Added `text-diff` view
3536
- Added support for `diff` syntax in `source` view
3637
- Added `hideOnTriggerClick` option for popup and tooltip views
3738
- Reworked the computation graph on the discovery page and related features:

package-lock.json

Lines changed: 25 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"dependencies": {
6767
"@discoveryjs/json-ext": "^0.6.3",
6868
"codemirror": "^5.65.2",
69+
"diff": "^8.0.2",
6970
"github-slugger": "^2.0.0",
7071
"hitext": "^1.0.0-beta.1",
7172
"jora": "1.0.0-beta.15",

src/views/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
@import url('./text/indicator.css');
3131
@import url('./text/markdown.css');
3232
@import url('./text/source.css');
33+
@import url('./text/text-diff.css');
3334
@import url('./text/text-match.css');
3435
@import url('./text/text-numeric.css');
3536
@import url('./text-render.css');

src/views/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export { default as link } from './text/link.js';
4242
export { default as markdown } from './text/markdown.js';
4343
export { default as source } from './text/source.js';
4444
export { default as text } from './text/text.js';
45+
export { default as textDiff } from './text/text-diff.js';
4546
export { default as textMatch } from './text/text-match.js';
4647
export { default as textNumeric } from './text/text-numeric.js';
4748

src/views/text/text-diff.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.view-text-diff {
2+
display: inline-block;
3+
font-family: var(--discovery-monospace-font-family);
4+
font-size: var(--discovery-monospace-font-size, 12px);
5+
line-height: var(--discovery-monospace-line-height, 1.5);
6+
white-space: pre-wrap;
7+
word-break: break-word;
8+
}
9+
.view-text-diff .added {
10+
padding: 1.5px 0;
11+
background-color: color-mix(in srgb, #92da92 70%, var(--discovery-background-color));
12+
color: rgba(0, 0, 0, .75);
13+
}
14+
.view-text-diff .removed {
15+
padding: 1.5px 0;
16+
background-color: color-mix(in srgb, #de6666 65%, var(--discovery-background-color));
17+
color: rgba(0, 0, 0, .75);
18+
}

src/views/text/text-diff.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* eslint-env browser */
2+
import { createElement } from '../../core/utils';
3+
import usage from './text-diff.usage.js';
4+
import {
5+
diffChars,
6+
diffWords,
7+
diffWordsWithSpace,
8+
diffSentences,
9+
diffLines
10+
} from 'diff';
11+
12+
const diffType = {
13+
char: diffChars,
14+
word: diffWords,
15+
wordws: diffWordsWithSpace,
16+
sentence: diffSentences,
17+
line: diffLines
18+
};
19+
20+
const props = `is not array? | {
21+
before,
22+
after,
23+
delta: undefined,
24+
type: undefined
25+
} | overrideProps() | {
26+
before is string ?: '',
27+
after is string ?: '',
28+
delta | $ in ['added', 'removed', 'both'] ?: 'both',
29+
type | $ in ${JSON.stringify(Object.keys(diffType))} ?: 'wordws'
30+
}`;
31+
32+
export default function(host) {
33+
host.view.define('text-diff', function(el, config) {
34+
const { type, before, after, delta } = config;
35+
36+
const diff = diffType[type](before, after);
37+
const showAdded = delta === 'both' || delta === 'added';
38+
const showRemoved = delta === 'both' || delta === 'removed';
39+
40+
for (const { added, removed, value } of diff) {
41+
let textContainer = el;
42+
43+
if (added || removed) {
44+
if (added ? !showAdded : !showRemoved) {
45+
continue;
46+
}
47+
48+
textContainer = el.appendChild(createElement('span', added ? 'added' : 'removed'));
49+
}
50+
51+
textContainer.append(value);
52+
}
53+
}, { props, usage });
54+
}

src/views/text/text-diff.usage.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const types = ['char', 'word', 'wordws', 'sentence', 'line'];
2+
const deltas = ['both', 'added', 'removed'];
3+
const sameText = 'The sun is shining brightly. Birds are singing in the trees.';
4+
const beforeText = 'The quick brown fox jumps over the lazy dog. It lands softly and runs away.\n' + sameText;
5+
const afterText = 'A quick brown fox jumped over a lazy dog. It landed softly, then ran away.\n' + sameText;
6+
7+
export default {
8+
demo: {
9+
view: 'text-diff',
10+
before: 'Hello world!',
11+
after: 'Hello Discovery.js!'
12+
},
13+
examples: [
14+
{
15+
title: 'Types',
16+
demoData: types.map(type => ({ type, before: beforeText, after: afterText })),
17+
demo: {
18+
view: 'table',
19+
cols: [
20+
{ header: 'Type', content: ['text:type', { view: 'text', when: 'type="wordws"', text: ' (default)' }] },
21+
{ header: 'Preview', content: 'text-diff{ type }' }
22+
// { header: 'Example', content: { view: 'source', syntax: 'discovery-view', source: '=`{ view: "text-diff", type: "${type}" }`', lineNum: false } }
23+
]
24+
}
25+
},
26+
{
27+
title: 'Delta kinds',
28+
demoData: deltas.map(delta => ({ delta, before: beforeText, after: afterText })),
29+
demo: {
30+
view: 'table',
31+
cols: [
32+
{ header: 'Type', content: ['text:delta', { view: 'text', when: 'delta="both"', text: ' (default)' }] },
33+
{ header: 'Preview', content: 'text-diff{ delta }' }
34+
// { header: 'Example', content: { view: 'source', syntax: 'discovery-view', source: '=`{ view: "text-diff", delta: "${delta}" }`', lineNum: false } }
35+
]
36+
}
37+
}
38+
]
39+
};

0 commit comments

Comments
 (0)