@saltcorn/history-control 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/diffview.js +149 -0
- package/index.js +1 -0
- package/package.json +4 -2
package/diffview.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
const Field = require("@saltcorn/data/models/field");
|
|
2
|
+
const Table = require("@saltcorn/data/models/table");
|
|
3
|
+
const Form = require("@saltcorn/data/models/form");
|
|
4
|
+
const User = require("@saltcorn/data/models/user");
|
|
5
|
+
const View = require("@saltcorn/data/models/view");
|
|
6
|
+
const Workflow = require("@saltcorn/data/models/workflow");
|
|
7
|
+
const HtmlDiff = require("htmldiff-js");
|
|
8
|
+
const {
|
|
9
|
+
text,
|
|
10
|
+
div,
|
|
11
|
+
h3,
|
|
12
|
+
style,
|
|
13
|
+
a,
|
|
14
|
+
script,
|
|
15
|
+
pre,
|
|
16
|
+
domReady,
|
|
17
|
+
p,
|
|
18
|
+
i,
|
|
19
|
+
select,
|
|
20
|
+
option,
|
|
21
|
+
} = require("@saltcorn/markup/tags");
|
|
22
|
+
const { radio_group, checkbox_group } = require("@saltcorn/markup/helpers");
|
|
23
|
+
const moment = require("moment");
|
|
24
|
+
|
|
25
|
+
const get_state_fields = () => [
|
|
26
|
+
{
|
|
27
|
+
name: "id",
|
|
28
|
+
type: "Integer",
|
|
29
|
+
required: true,
|
|
30
|
+
primary_key: true,
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const configuration_workflow = (req) =>
|
|
35
|
+
new Workflow({
|
|
36
|
+
steps: [
|
|
37
|
+
{
|
|
38
|
+
name: "Difference Field",
|
|
39
|
+
form: async (context) => {
|
|
40
|
+
const table = await Table.findOne({ id: context.table_id });
|
|
41
|
+
const field_options = table.fields
|
|
42
|
+
.filter((f) => f.type?.name === "String" || f.type?.name === "HTML")
|
|
43
|
+
.map((f) => f.name);
|
|
44
|
+
return new Form({
|
|
45
|
+
fields: [
|
|
46
|
+
{
|
|
47
|
+
name: "diff_field",
|
|
48
|
+
label: "Difference field",
|
|
49
|
+
sublabel: "String or HTML field to show differences for",
|
|
50
|
+
type: "String",
|
|
51
|
+
attributes: {
|
|
52
|
+
options: field_options,
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
name: "date_format",
|
|
57
|
+
label: "Date format",
|
|
58
|
+
type: "String",
|
|
59
|
+
sublabel: "moment.js format specifier",
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const run = async (
|
|
69
|
+
table_id,
|
|
70
|
+
viewname,
|
|
71
|
+
{ diff_field, date_format },
|
|
72
|
+
state,
|
|
73
|
+
extraArgs
|
|
74
|
+
) => {
|
|
75
|
+
const table = await Table.findOne({ id: table_id });
|
|
76
|
+
|
|
77
|
+
const cmpMode = state.diffcmp || "Previous";
|
|
78
|
+
//compare to: Current, Previous, No comparison
|
|
79
|
+
const cmpSel = radio_group({
|
|
80
|
+
options: ["Previous", "Latest", "No comparison"],
|
|
81
|
+
name: "diff_cmp_sel",
|
|
82
|
+
value: cmpMode,
|
|
83
|
+
inline: true,
|
|
84
|
+
onChange: `change_cmp_sel(event)`,
|
|
85
|
+
});
|
|
86
|
+
const id = state[table.pk_name];
|
|
87
|
+
if (!id) return `Need id`;
|
|
88
|
+
let hist = await table.get_history(id);
|
|
89
|
+
if (!hist || !hist.length) return "No versions recorded";
|
|
90
|
+
hist = hist.reverse();
|
|
91
|
+
const chosen_version = state.version || hist[0]?._version;
|
|
92
|
+
|
|
93
|
+
const userIds = new Set(hist.map((h) => h._userid));
|
|
94
|
+
const users = await User.find({ id: { in: [...userIds] } });
|
|
95
|
+
const emails = {};
|
|
96
|
+
users.forEach((u) => (emails[u.id] = u.email));
|
|
97
|
+
const row = hist.find((h) => chosen_version == h._version);
|
|
98
|
+
const verSelect = select(
|
|
99
|
+
{
|
|
100
|
+
onChange: "change_diff_version(event)",
|
|
101
|
+
class: "form-select form-control mb-2",
|
|
102
|
+
},
|
|
103
|
+
hist.map((h) =>
|
|
104
|
+
option(
|
|
105
|
+
{ value: h._version, selected: chosen_version == h._version },
|
|
106
|
+
date_format ? moment(h._time).format(date_format) : h._time.toString(),
|
|
107
|
+
" - ",
|
|
108
|
+
emails[h._userid]
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
//restore to this version
|
|
114
|
+
|
|
115
|
+
let diff_html;
|
|
116
|
+
if (cmpMode === "No comparison") diff_html = row[diff_field];
|
|
117
|
+
else {
|
|
118
|
+
const cmpTo =
|
|
119
|
+
cmpMode === "Latest"
|
|
120
|
+
? hist[0]
|
|
121
|
+
: hist.find((h) => chosen_version - 1 == h._version);
|
|
122
|
+
if (!cmpTo || cmpTo._version == row._version) diff_html = row[diff_field];
|
|
123
|
+
else {
|
|
124
|
+
//const field = table.fields.find((f) => f.name === diff_field);
|
|
125
|
+
const oldH = cmpMode === "Latest" ? row[diff_field] : cmpTo[diff_field];
|
|
126
|
+
const newH = cmpMode === "Latest" ? cmpTo[diff_field] : row[diff_field];
|
|
127
|
+
diff_html = HtmlDiff.default.execute(oldH, newH);
|
|
128
|
+
console.log({ oldH, newH, diff_html });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return div(
|
|
133
|
+
div({ class: "d-flex" }, p({ class: "me-2" }, "Compare to:"), cmpSel),
|
|
134
|
+
verSelect,
|
|
135
|
+
diff_html,
|
|
136
|
+
script(
|
|
137
|
+
`function change_cmp_sel(e){set_state_field("diffcmp", e.target.value)}
|
|
138
|
+
function change_diff_version(e){set_state_field("version", e.target.value)}`
|
|
139
|
+
)
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
module.exports = {
|
|
144
|
+
name: "History Field Difference",
|
|
145
|
+
display_state_form: false,
|
|
146
|
+
get_state_fields,
|
|
147
|
+
configuration_workflow,
|
|
148
|
+
run,
|
|
149
|
+
};
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@saltcorn/history-control",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Allow users to interact with row history",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@saltcorn/data": "^0.8.7",
|
|
8
|
-
"@saltcorn/markup": "^0.8.7"
|
|
8
|
+
"@saltcorn/markup": "^0.8.7",
|
|
9
|
+
"htmldiff-js": "1.0.5",
|
|
10
|
+
"moment": "^2.29.4"
|
|
9
11
|
},
|
|
10
12
|
"author": "Tom Nielsen",
|
|
11
13
|
"license": "MIT",
|