You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,59 +17,59 @@ Check http://fiduswriter.github.io/diffDOM for demo and tests.
17
17
## Usage
18
18
19
19
Include the diffDOM.js file in your HTML like this:
20
-
```
20
+
```html
21
21
<scriptsrc="diffDOM.js">
22
22
```
23
23
24
24
Or like this in node/browserify:
25
-
```
25
+
```js
26
26
var diffDOM =require("diff-dom");
27
27
```
28
28
29
29
Then create an instance of diffDOM within the javascript code:
30
-
```
30
+
```js
31
31
dd =newdiffDOM();
32
32
```
33
33
34
-
Now you can create a diff to get from dom elementA to dom elementB like this:
35
-
```
34
+
Now you can create a diff to get from dom `elementA` to dom `elementB` like this:
35
+
```js
36
36
diff =dd.diff(elementA, elementB);
37
37
```
38
38
39
39
You can now apply this diff like this:
40
-
```
40
+
```js
41
41
dd.apply(elementA, diff);
42
42
```
43
-
Now elementA will have been changed to be structurally equal to elementB.
43
+
Now `elementA` will have been changed to be structurally equal to `elementB`.
44
44
45
45
### Advanced uses
46
46
47
47
#### Undo
48
48
49
49
Continuing on from the previous example, you can also undo a diff, like this:
50
-
```
50
+
```js
51
51
dd.undo(elementA, diff);
52
52
```
53
53
Now elementA will be what it was like before applying the diff.
54
54
55
55
#### Remote changes
56
56
57
-
If you need to move diffs from one machine to another one, you will likely want to send the diffs through a websocket connection or as part of a form submit. In both cases you need to convert the diff to a json string.
57
+
If you need to move diffs from one machine to another one, you will likely want to send the diffs through a websocket connection or as part of a form submit. In both cases you need to convert the diff to a json `string`.
58
58
59
59
To convert a diff to a json string which you can send over the network, do:
60
-
```
60
+
```js
61
61
diffJson =JSON.stringify(diff);
62
62
```
63
63
64
64
On the receiving end you then need to unpack it like this:
65
-
```
65
+
```js
66
66
diff =JSON.parse(diffJson);
67
67
```
68
68
69
69
#### Error handling when patching/applying
70
70
71
-
Sometimes one may try to patch an elment without knowing whether the patch actually will apply cleanly. This should not be a problem. If diffDOM determines that a patch cannot be executed, it will simple return false. Else it will return true:
72
-
```
71
+
Sometimes one may try to patch an elment without knowing whether the patch actually will apply cleanly. This should not be a problem. If diffDOM determines that a patch cannot be executed, it will simple return `false`. Else it will return `true`:
72
+
```js
73
73
result =dd.apply(element, diff);
74
74
75
75
if (result) {
@@ -80,8 +80,8 @@ if (result) {
80
80
```
81
81
#### Advanced merging of text node changes
82
82
83
-
diffDOM does not include merging for changes to text nodes. However, it includes hooks so that you can add more advanced handling. Simple overwrite the textDiff function of the diffDOM instance. The functions TEXTDIFF and TEXTPATCH need to be defined in the code:
84
-
```
83
+
diffDOM does not include merging for changes to text nodes. However, it includes hooks so that you can add more advanced handling. Simple overwrite the `textDiff` function of the `diffDOM` instance. The functions TEXTDIFF and TEXTPATCH need to be defined in the code:
diffDOM provides extension points before and after virtual and actual diffs, exposing some of the internals of the diff algorithm, and allowing you to make additional decisions based on that information.
103
103
104
-
```
104
+
```js
105
105
dd =newdiffDOM({
106
106
preVirtualDiffApply:function (info) {
107
107
console.log(info);
@@ -118,9 +118,9 @@ dd = new diffDOM({
118
118
});
119
119
```
120
120
121
-
Additionally, the _pre_ hooks allow you to shortcircuit the standard behaviour of the diff by returning 'true' from this callback. This will cause the diffApply functions to return prematurely, skipping their standard behaviour.
121
+
Additionally, the _pre_ hooks allow you to shortcircuit the standard behaviour of the diff by returning `true` from this callback. This will cause the `diffApply` functions to return prematurely, skipping their standard behaviour.
122
122
123
-
```
123
+
```js
124
124
dd =newdiffDOM({
125
125
// prevent removal of attributes
126
126
preDiffApply:function (info) {
@@ -136,7 +136,7 @@ dd = new diffDOM({
136
136
137
137
diffDOM also provides a way to filter outer diff
138
138
139
-
```
139
+
```js
140
140
dd =newdiffDOM({
141
141
filterOuterDiff:function(t1, t2, diffs) {
142
142
// can change current outer diffs by returning a new array,
@@ -152,7 +152,7 @@ dd = new diffDOM({
152
152
#### Debugging
153
153
154
154
For debugging you might want to set a max number of diff changes between two elements before diffDOM gives up. To allow for a maximum of 500 differences between elements when diffing, initialize diffDOM like this:
155
-
```
155
+
```js
156
156
dd =newdiffDOM({
157
157
debug:true,
158
158
diffcap:500
@@ -163,7 +163,7 @@ dd = new diffDOM({
163
163
164
164
For forms that have been filled out by a user in ways that have changed which value is associated with an input field or which options are checked/selected without
165
165
the DOM having been updated, the values are diffed. For use cases in which no changes have been made to any of the form values, one may choose to skip diffing the values. To do this, hand `false` as a third configuration option to diffDOM:
0 commit comments