|
1 | 1 | /** @odoo-module **/ |
2 | 2 | import { patch } from "@web/core/utils/patch"; |
3 | 3 | import { Message } from "@mail/core/common/message"; |
| 4 | +import { CopyButton } from "@web/core/copy_button/copy_button"; |
| 5 | +import { memoize } from "@web/core/utils/functions"; |
| 6 | + |
| 7 | +const diffMatchPatch = new diff_match_patch(); |
| 8 | + |
| 9 | +function makeDiff(text1, text2) { |
| 10 | + const { chars1, chars2, lineArray } = diffMatchPatch.diff_linesToChars_(text1, text2); |
| 11 | + const diffs = diffMatchPatch.diff_main(chars1, chars2, false); |
| 12 | + diffMatchPatch.diff_charsToLines_(diffs, lineArray); |
| 13 | + diffMatchPatch.diff_cleanupSemantic(diffs); |
| 14 | + return diffs; |
| 15 | +} |
| 16 | + |
| 17 | +function prepareForRendering(diffs) { |
| 18 | + let lines = []; |
| 19 | + let preLineCounter = 0 |
| 20 | + let postLineCounter = 0 |
| 21 | + for (const { 0: diffType, 1: data } of diffs) { |
| 22 | + for (let line of data.split("\n")) { |
| 23 | + line = line.replace(/&/g, "&"); |
| 24 | + line = line.replace(/</g, "<"); |
| 25 | + line = line.replace(/>/g, ">"); |
| 26 | + //text = text.replace(/\n/g, "<br>"); |
| 27 | + //text = text.replace(/ /g, "  "); |
| 28 | + let type; |
| 29 | + if (diffType === -1) { |
| 30 | + type = "removed"; |
| 31 | + preLineCounter += 1; |
| 32 | + } else if (diffType === 0) { |
| 33 | + type = "kept"; |
| 34 | + preLineCounter += 1; |
| 35 | + postLineCounter += 1; |
| 36 | + } else if (diffType === 1) { |
| 37 | + type = "added"; |
| 38 | + postLineCounter += 1; |
| 39 | + } |
| 40 | + lines.push({ type, preLineCounter, postLineCounter, line }); |
| 41 | + } |
| 42 | + } |
| 43 | + return lines; |
| 44 | +} |
| 45 | + |
| 46 | +function computeDiff({ oldValue, newValue }) { |
| 47 | + const diff = makeDiff(oldValue, newValue); |
| 48 | + return prepareForRendering(diff); |
| 49 | +} |
| 50 | + |
| 51 | +patch(Message, { |
| 52 | + components: { |
| 53 | + ...Message.components, |
| 54 | + CopyButton, |
| 55 | + }, |
| 56 | +}); |
4 | 57 |
|
5 | 58 | patch(Message.prototype, { |
6 | 59 | setup() { |
7 | 60 | super.setup(...arguments); |
8 | | - this.kept = false; |
| 61 | + this.state.showKept = false; |
9 | 62 | }, |
10 | | - isMultiline(trackingValue) { |
11 | | - const oldValue = trackingValue.oldValue; |
12 | | - const newValue = trackingValue.newValue; |
13 | | - return ((oldValue && typeof oldValue=== 'string' && oldValue.includes('\n')) && (newValue && typeof oldValue=== 'string' && newValue.includes('\n'))) |
14 | | - }, |
15 | | - formatTracking(trackingFieldInfo, trackingValue) { |
16 | | - return super.formatTracking(trackingFieldInfo, trackingValue) |
| 63 | + |
| 64 | + lines: memoize(computeDiff), |
| 65 | + |
| 66 | + isMultiline({ oldValue, newValue }) { |
| 67 | + return typeof oldValue === "string" && oldValue.includes("\n") && typeof oldValue === "string" && newValue.includes("\n"); |
17 | 68 | }, |
| 69 | + |
18 | 70 | toggleKept() { |
19 | | - this.kept = !this.kept; |
20 | | - }, |
21 | | - copyToClipboard(trackingValue) { |
22 | | - return function () { |
23 | | - navigator.clipboard.writeText(trackingValue); |
24 | | - }; |
| 71 | + this.state.showKept = !this.state.showKept; |
25 | 72 | }, |
26 | | - lines(trackingValue) { |
27 | | - const oldValue = trackingValue.oldValue; |
28 | | - const newValue = trackingValue.newValue; |
29 | | - const diff = this.makeDiff(oldValue, newValue); |
30 | | - const lines = this.prepareForRendering(diff); |
31 | | - return lines; |
32 | | - }, |
33 | | - makeDiff(text1, text2) { |
34 | | - var dmp = new diff_match_patch(); |
35 | | - var a = dmp.diff_linesToChars_(text1, text2); |
36 | | - var lineText1 = a.chars1; |
37 | | - var lineText2 = a.chars2; |
38 | | - var lineArray = a.lineArray; |
39 | | - var diffs = dmp.diff_main(lineText1, lineText2, false); |
40 | | - dmp.diff_charsToLines_(diffs, lineArray); |
41 | | - dmp.diff_cleanupSemantic(diffs); |
42 | | - return diffs; |
43 | | - }, |
44 | | - prepareForRendering(diffs) { |
45 | | - var lines = []; |
46 | | - var pre_line_counter = 0 |
47 | | - var post_line_counter = 0 |
48 | | - for (var x = 0; x < diffs.length; x++) { |
49 | | - var diff_type = diffs[x][0]; |
50 | | - var data = diffs[x][1]; |
51 | | - var data_lines = data.split('\n'); |
52 | | - for (var line_index in data_lines) { |
53 | | - var line = data_lines[line_index]; |
54 | | - line = line.replace(/&/g, '&'); |
55 | | - line = line.replace(/</g, '<'); |
56 | | - line = line.replace(/>/g, '>'); |
57 | | - //text = text.replace(/\n/g, '<br>'); |
58 | | - //text = text.replace(/ /g, '  '); |
59 | | - if (diff_type == -1) { |
60 | | - lines.push({type:'removed', pre_line_counter: pre_line_counter, post_line_counter: '-', line: line}) |
61 | | - pre_line_counter += 1 |
62 | | - } else if (diff_type == 0) { |
63 | | - lines.push({type:'kept', pre_line_counter: '', post_line_counter: post_line_counter, line: line}) |
64 | | - pre_line_counter += 1 |
65 | | - post_line_counter +=1 |
66 | | - } else if (diff_type == 1) { |
67 | | - lines.push({type:'added', pre_line_counter: '+', post_line_counter: post_line_counter, line: line}) |
68 | | - post_line_counter +=1 |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - return lines; |
73 | | - }, |
74 | 73 | }); |
0 commit comments