|
1 | 1 | import PropTypes from "prop-types";
|
2 | 2 | import React from "react";
|
3 |
| -import jsdiff from "diff"; |
| 3 | +import { View, StyleSheet, Text } from "react-native"; |
| 4 | +import { diffChars, diffWords, diffSentences } from "diff"; |
4 | 5 |
|
5 | 6 | const fnMap = {
|
6 |
| - chars: jsdiff.diffChars, |
7 |
| - words: jsdiff.diffWords, |
8 |
| - sentences: jsdiff.diffSentences, |
9 |
| - json: jsdiff.diffJson |
| 7 | + chars: diffChars, |
| 8 | + words: diffWords, |
| 9 | + sentences: diffSentences |
10 | 10 | };
|
11 | 11 |
|
12 |
| -export default function Diff({ inputA = "", inputB = "", type = "chars" }) { |
| 12 | +export default function Diff({ |
| 13 | + inputA = "", |
| 14 | + inputB = "", |
| 15 | + type = "chars", |
| 16 | + textStyle = {}, |
| 17 | + addedText = {}, |
| 18 | + removedText = {}, |
| 19 | + unchangedText = {}, |
| 20 | + containerStyle = {} |
| 21 | +}) { |
| 22 | + let val = 1; |
13 | 23 | var diff = fnMap[type](inputA, inputB);
|
| 24 | + console.log(diff); |
14 | 25 | var result = diff.map(function(part, index) {
|
15 |
| - var spanStyle = { |
16 |
| - backgroundColor: part.added |
17 |
| - ? "lightgreen" |
18 |
| - : part.removed |
19 |
| - ? "salmon" |
20 |
| - : "lightgrey" |
21 |
| - }; |
| 26 | + var spanStyle = part.added |
| 27 | + ? { ...styles.addedText, ...addedText } |
| 28 | + : part.removed |
| 29 | + ? { ...styles.removedText, ...removedText } |
| 30 | + : { ...styles.defaultText, ...unchangedText }; |
| 31 | + |
22 | 32 | return (
|
23 |
| - <span key={index} style={spanStyle}> |
| 33 | + <Text key={index} style={[styles.defaultText, spanStyle, textStyle]}> |
24 | 34 | {part.value}
|
25 |
| - </span> |
| 35 | + </Text> |
26 | 36 | );
|
27 | 37 | });
|
28 |
| - return <pre className="diff-result">{result}</pre>; |
| 38 | + // console.log(result); |
| 39 | + return ( |
| 40 | + <View style={[styles.defaultContainerStyle, containerStyle]}>{result}</View> |
| 41 | + ); |
29 | 42 | }
|
30 | 43 |
|
31 | 44 | Diff.propTypes = {
|
32 |
| - inputA: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), |
33 |
| - inputB: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), |
34 |
| - type: PropTypes.oneOf(["chars", "words", "sentences", "json"]) |
| 45 | + inputA: PropTypes.oneOfType([PropTypes.string]), |
| 46 | + inputB: PropTypes.oneOfType([PropTypes.string]), |
| 47 | + type: PropTypes.oneOf(["chars", "words", "sentences"]), |
| 48 | + textStyle: PropTypes.object, |
| 49 | + containerStyle: PropTypes.object, |
| 50 | + addedText: PropTypes.object, |
| 51 | + removedText: PropTypes.object, |
| 52 | + unchangedText: PropTypes.object |
35 | 53 | };
|
| 54 | + |
| 55 | +var styles = StyleSheet.create({ |
| 56 | + defaultText: {}, |
| 57 | + addedText: { |
| 58 | + backgroundColor: "lightgreen", |
| 59 | + color: "#000000" |
| 60 | + }, |
| 61 | + removedText: { |
| 62 | + backgroundColor: "salmon", |
| 63 | + textDecorationLine: "line-through", |
| 64 | + textDecorationStyle: "solid", |
| 65 | + color: "#000000" |
| 66 | + }, |
| 67 | + defaultContainerStyle: { |
| 68 | + flexDirection: "row", |
| 69 | + flexWrap: "wrap", |
| 70 | + alignItems: "flex-end" |
| 71 | + } |
| 72 | +}); |
0 commit comments