@macrostrat/feedback-components 1.1.10 → 2.0.0
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/CHANGELOG.md +6 -0
- package/dist/extractions/index.cjs +198 -0
- package/dist/extractions/index.cjs.map +1 -0
- package/dist/extractions/index.d.ts +30 -0
- package/dist/extractions/index.js +198 -0
- package/dist/extractions/index.js.map +1 -0
- package/dist/extractions/main.module.sass.cjs +12 -0
- package/dist/extractions/main.module.sass.cjs.map +1 -0
- package/dist/extractions/main.module.sass.js +12 -0
- package/dist/extractions/main.module.sass.js.map +1 -0
- package/dist/extractions/types.d.ts +32 -0
- package/dist/feedback/edit-state.cjs +382 -0
- package/dist/feedback/edit-state.cjs.map +1 -0
- package/dist/feedback/edit-state.d.ts +127 -0
- package/dist/feedback/edit-state.js +382 -0
- package/dist/feedback/edit-state.js.map +1 -0
- package/dist/feedback/feedback.module.sass.cjs +47 -0
- package/dist/feedback/feedback.module.sass.cjs.map +1 -0
- package/dist/feedback/feedback.module.sass.js +47 -0
- package/dist/feedback/feedback.module.sass.js.map +1 -0
- package/dist/feedback/graph.cjs +124 -0
- package/dist/feedback/graph.cjs.map +1 -0
- package/dist/feedback/graph.d.ts +9 -0
- package/dist/feedback/graph.js +124 -0
- package/dist/feedback/graph.js.map +1 -0
- package/dist/feedback/index.cjs +347 -0
- package/dist/feedback/index.cjs.map +1 -0
- package/dist/feedback/index.d.ts +15 -0
- package/dist/feedback/index.js +348 -0
- package/dist/feedback/index.js.map +1 -0
- package/dist/feedback/matches.cjs +244 -0
- package/dist/feedback/matches.cjs.map +1 -0
- package/dist/feedback/matches.d.ts +17 -0
- package/dist/feedback/matches.js +244 -0
- package/dist/feedback/matches.js.map +1 -0
- package/dist/feedback/node.cjs +63 -0
- package/dist/feedback/node.cjs.map +1 -0
- package/dist/feedback/node.d.ts +5 -0
- package/dist/feedback/node.js +63 -0
- package/dist/feedback/node.js.map +1 -0
- package/dist/feedback/text-visualizer.cjs +288 -0
- package/dist/feedback/text-visualizer.cjs.map +1 -0
- package/dist/feedback/text-visualizer.d.ts +36 -0
- package/dist/feedback/text-visualizer.js +288 -0
- package/dist/feedback/text-visualizer.js.map +1 -0
- package/dist/feedback/type-selector/index.cjs +47 -0
- package/dist/feedback/type-selector/index.cjs.map +1 -0
- package/dist/feedback/type-selector/index.d.ts +20 -0
- package/dist/feedback/type-selector/index.js +47 -0
- package/dist/feedback/type-selector/index.js.map +1 -0
- package/dist/feedback/type-selector/main.module.sass.cjs +13 -0
- package/dist/feedback/type-selector/main.module.sass.cjs.map +1 -0
- package/dist/feedback/type-selector/main.module.sass.js +13 -0
- package/dist/feedback/type-selector/main.module.sass.js.map +1 -0
- package/dist/feedback/typelist.cjs +293 -0
- package/dist/feedback/typelist.cjs.map +1 -0
- package/dist/feedback/typelist.d.ts +8 -0
- package/dist/feedback/typelist.js +293 -0
- package/dist/feedback/typelist.js.map +1 -0
- package/dist/feedback/types.d.ts +64 -0
- package/dist/feedback-components.css +343 -0
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -41
- package/src/extractions/index.ts +2 -2
- package/src/feedback/text-visualizer.ts +0 -1
- package/src/feedback/types.ts +2 -2
- package/dist/esm/feedback-components.2364179b.js +0 -287
- package/dist/esm/feedback-components.2364179b.js.map +0 -1
- package/dist/esm/feedback-components.6d32ee91.js +0 -16
- package/dist/esm/feedback-components.6d32ee91.js.map +0 -1
- package/dist/esm/feedback-components.7dd42a9f.js +0 -248
- package/dist/esm/feedback-components.7dd42a9f.js.map +0 -1
- package/dist/esm/feedback-components.7f72e5e9.js +0 -64
- package/dist/esm/feedback-components.7f72e5e9.js.map +0 -1
- package/dist/esm/feedback-components.ad9f284e.js +0 -63
- package/dist/esm/feedback-components.ad9f284e.js.map +0 -1
- package/dist/esm/feedback-components.ba79c0ef.js +0 -204
- package/dist/esm/feedback-components.ba79c0ef.js.map +0 -1
- package/dist/esm/feedback-components.bf93773c.css +0 -17
- package/dist/esm/feedback-components.bf93773c.css.map +0 -1
- package/dist/esm/feedback-components.c6e2c296.js +0 -148
- package/dist/esm/feedback-components.c6e2c296.js.map +0 -1
- package/dist/esm/feedback-components.ca78c2d4.js +0 -287
- package/dist/esm/feedback-components.ca78c2d4.js.map +0 -1
- package/dist/esm/feedback-components.d769ffa5.css +0 -205
- package/dist/esm/feedback-components.d769ffa5.css.map +0 -1
- package/dist/esm/feedback-components.e273ed5b.css +0 -14
- package/dist/esm/feedback-components.e273ed5b.css.map +0 -1
- package/dist/esm/feedback-components.e702eece.js +0 -97
- package/dist/esm/feedback-components.e702eece.js.map +0 -1
- package/dist/esm/feedback-components.edc606bb.js +0 -360
- package/dist/esm/feedback-components.edc606bb.js.map +0 -1
- package/dist/esm/feedback-components.f9850d85.js +0 -19
- package/dist/esm/feedback-components.f9850d85.js.map +0 -1
- package/dist/esm/feedback-components.fa847634.js +0 -453
- package/dist/esm/feedback-components.fa847634.js.map +0 -1
- package/dist/esm/index.d.ts +0 -96
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js +0 -9
- package/dist/esm/index.js.map +0 -1
- package/dist/node/feedback-components.1bd49bf0.js +0 -2
- package/dist/node/feedback-components.1bd49bf0.js.map +0 -1
- package/dist/node/feedback-components.2f391fa4.js +0 -2
- package/dist/node/feedback-components.2f391fa4.js.map +0 -1
- package/dist/node/feedback-components.6a4a1290.js +0 -2
- package/dist/node/feedback-components.6a4a1290.js.map +0 -1
- package/dist/node/feedback-components.70780da4.js +0 -2
- package/dist/node/feedback-components.70780da4.js.map +0 -1
- package/dist/node/feedback-components.794f429b.js +0 -2
- package/dist/node/feedback-components.794f429b.js.map +0 -1
- package/dist/node/feedback-components.83c21466.css +0 -2
- package/dist/node/feedback-components.83c21466.css.map +0 -1
- package/dist/node/feedback-components.97518e90.css +0 -2
- package/dist/node/feedback-components.97518e90.css.map +0 -1
- package/dist/node/feedback-components.9eb1d41a.css +0 -2
- package/dist/node/feedback-components.9eb1d41a.css.map +0 -1
- package/dist/node/feedback-components.acac789b.js +0 -2
- package/dist/node/feedback-components.acac789b.js.map +0 -1
- package/dist/node/feedback-components.c84ff10e.js +0 -2
- package/dist/node/feedback-components.c84ff10e.js.map +0 -1
- package/dist/node/feedback-components.db72d0e1.js +0 -2
- package/dist/node/feedback-components.db72d0e1.js.map +0 -1
- package/dist/node/feedback-components.dc76072c.js +0 -2
- package/dist/node/feedback-components.dc76072c.js.map +0 -1
- package/dist/node/feedback-components.ddd11fe8.js +0 -2
- package/dist/node/feedback-components.ddd11fe8.js.map +0 -1
- package/dist/node/feedback-components.e59a061f.js +0 -2
- package/dist/node/feedback-components.e59a061f.js.map +0 -1
- package/dist/node/feedback-components.feab0bd0.js +0 -2
- package/dist/node/feedback-components.feab0bd0.js.map +0 -1
- package/dist/node/index.js +0 -2
- package/dist/node/index.js.map +0 -1
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const update = require("immutability-helper");
|
|
5
|
+
function useUpdatableTree(initialTree, entityTypes, viewOnly, matchMode2, autoSelect = []) {
|
|
6
|
+
const type = entityTypes.values().next().value;
|
|
7
|
+
let selectedNodes = [];
|
|
8
|
+
autoSelect = autoSelect.map((name) => name.toLowerCase());
|
|
9
|
+
if (autoSelect.length > 0) {
|
|
10
|
+
selectedNodes = initialTree.flatMap((node) => node.children ?? []).filter((node) => autoSelect.includes(node.name.toLowerCase())).map((node) => node.id);
|
|
11
|
+
}
|
|
12
|
+
return react.useReducer(treeReducer, {
|
|
13
|
+
initialTree,
|
|
14
|
+
tree: initialTree,
|
|
15
|
+
selectedNodes,
|
|
16
|
+
entityTypesMap: entityTypes,
|
|
17
|
+
selectedEntityType: type,
|
|
18
|
+
lastInternalId: 0,
|
|
19
|
+
isSelectingEntityType: false,
|
|
20
|
+
viewMode: "tree",
|
|
21
|
+
viewOnly,
|
|
22
|
+
matchMode: matchMode2
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
const TreeDispatchContext = react.createContext(null);
|
|
26
|
+
function useTreeDispatch() {
|
|
27
|
+
const dispatch = react.useContext(TreeDispatchContext);
|
|
28
|
+
if (dispatch == null) {
|
|
29
|
+
throw new Error("No dispatch context available");
|
|
30
|
+
}
|
|
31
|
+
return dispatch;
|
|
32
|
+
}
|
|
33
|
+
function treeReducer(state, action) {
|
|
34
|
+
if (action.type === "toggle-match-mode") {
|
|
35
|
+
return { ...state, matchMode: !state.matchMode };
|
|
36
|
+
}
|
|
37
|
+
if (action.type === "toggle-view-only") {
|
|
38
|
+
return { ...state, viewOnly: !state.viewOnly, selectedNodes: [] };
|
|
39
|
+
}
|
|
40
|
+
if (state.viewOnly) return viewMode(state, action);
|
|
41
|
+
if (state.matchMode) return matchMode(state, action);
|
|
42
|
+
switch (action.type) {
|
|
43
|
+
case "add-entity-type": {
|
|
44
|
+
const { name, description, color } = action.payload;
|
|
45
|
+
const newId2 = state.lastInternalId - 1;
|
|
46
|
+
const newType2 = {
|
|
47
|
+
id: newId2,
|
|
48
|
+
name,
|
|
49
|
+
description: description === "" ? null : description,
|
|
50
|
+
color
|
|
51
|
+
};
|
|
52
|
+
const newEntityTypesMap = new Map(state.entityTypesMap);
|
|
53
|
+
newEntityTypesMap.set(newId2, newType2);
|
|
54
|
+
return {
|
|
55
|
+
...state,
|
|
56
|
+
entityTypesMap: newEntityTypesMap,
|
|
57
|
+
selectedEntityType: newType2,
|
|
58
|
+
lastInternalId: newId2
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
case "update-entity-type": {
|
|
62
|
+
const { id, name, description, color } = action.payload;
|
|
63
|
+
const newEntityTypesMap = new Map(state.entityTypesMap);
|
|
64
|
+
const oldType = newEntityTypesMap.get(id);
|
|
65
|
+
if (!oldType) {
|
|
66
|
+
console.warn(`Entity type with id ${id} not found`);
|
|
67
|
+
return state;
|
|
68
|
+
}
|
|
69
|
+
const updatedType = {
|
|
70
|
+
...oldType,
|
|
71
|
+
name,
|
|
72
|
+
description: description === "" ? null : description,
|
|
73
|
+
color
|
|
74
|
+
};
|
|
75
|
+
newEntityTypesMap.set(id, updatedType);
|
|
76
|
+
const newTree3 = updateTreeTypes(state.tree, oldType, updatedType);
|
|
77
|
+
return {
|
|
78
|
+
...state,
|
|
79
|
+
tree: newTree3,
|
|
80
|
+
entityTypesMap: newEntityTypesMap,
|
|
81
|
+
selectedEntityType: updatedType
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
case "select-range":
|
|
85
|
+
const payloadIds = action.payload.ids;
|
|
86
|
+
const node1 = payloadIds[0];
|
|
87
|
+
const node2 = payloadIds[1];
|
|
88
|
+
const allNodes = flattenAndSort(state.tree);
|
|
89
|
+
const startIndex = allNodes.findIndex((node3) => node3.id === node1);
|
|
90
|
+
const endIndex = allNodes.findIndex((node3) => node3.id === node2);
|
|
91
|
+
const selectedNodes = allNodes.slice(startIndex, endIndex + 1);
|
|
92
|
+
console.log("Selecting range:", selectedNodes);
|
|
93
|
+
return {
|
|
94
|
+
...state,
|
|
95
|
+
selectedNodes: selectedNodes.map((node3) => node3.id)
|
|
96
|
+
};
|
|
97
|
+
case "move-node":
|
|
98
|
+
const [newTree, removedNodes] = removeNodes(
|
|
99
|
+
state.tree,
|
|
100
|
+
action.payload.dragIds
|
|
101
|
+
);
|
|
102
|
+
let keyPath = [];
|
|
103
|
+
if (action.payload.parentId) {
|
|
104
|
+
keyPath = findNode(newTree, action.payload.parentId);
|
|
105
|
+
keyPath.push("children");
|
|
106
|
+
}
|
|
107
|
+
let updateSpec = buildNestedSpec(keyPath, {
|
|
108
|
+
$splice: [[action.payload.index, 0, ...removedNodes]]
|
|
109
|
+
});
|
|
110
|
+
return { ...state, tree: update(newTree, updateSpec) };
|
|
111
|
+
case "delete-node":
|
|
112
|
+
const [newTree2, _removedNodes] = removeNodes(
|
|
113
|
+
state.tree,
|
|
114
|
+
action.payload.ids
|
|
115
|
+
);
|
|
116
|
+
const children = _removedNodes.flatMap((node3) => node3.children ?? []).filter((child) => !nodeIsInTree(newTree2, child.id));
|
|
117
|
+
return {
|
|
118
|
+
...state,
|
|
119
|
+
tree: [...newTree2, ...children],
|
|
120
|
+
selectedNodes: state.selectedNodes.filter(
|
|
121
|
+
(id) => !action.payload.ids.includes(id)
|
|
122
|
+
)
|
|
123
|
+
};
|
|
124
|
+
case "select-node":
|
|
125
|
+
const { ids } = action.payload;
|
|
126
|
+
const type = action.payload.ids.length > 0 ? findNodeById(state.tree, ids[0])?.type : null;
|
|
127
|
+
return { ...state, selectedNodes: ids, selectedEntityType: type };
|
|
128
|
+
// otherwise fall through to toggle-node-selected for a single ID
|
|
129
|
+
case "toggle-node-selected":
|
|
130
|
+
const nodesToAdd = action.payload.ids.filter(
|
|
131
|
+
(id) => !state.selectedNodes.includes(id)
|
|
132
|
+
);
|
|
133
|
+
const nodesToKeep = state.selectedNodes.filter(
|
|
134
|
+
(id) => !action.payload.ids.includes(id)
|
|
135
|
+
);
|
|
136
|
+
const newType = action.payload.ids.length > 0 ? findNodeById(state.tree, action.payload.ids[0])?.type : null;
|
|
137
|
+
return {
|
|
138
|
+
...state,
|
|
139
|
+
selectedNodes: [...nodesToKeep, ...nodesToAdd],
|
|
140
|
+
selectedEntityType: newType
|
|
141
|
+
};
|
|
142
|
+
case "create-node":
|
|
143
|
+
const newId = state.lastInternalId - 1;
|
|
144
|
+
const { text, start, end } = action.payload;
|
|
145
|
+
const node = {
|
|
146
|
+
id: newId,
|
|
147
|
+
name: text,
|
|
148
|
+
children: [],
|
|
149
|
+
indices: [start, end],
|
|
150
|
+
type: state.selectedEntityType
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
...state,
|
|
154
|
+
tree: [...state.tree, node],
|
|
155
|
+
selectedNodes: [newId],
|
|
156
|
+
lastInternalId: newId
|
|
157
|
+
};
|
|
158
|
+
case "delete-entity-type": {
|
|
159
|
+
const { id } = action.payload;
|
|
160
|
+
const newEntityTypesMap = new Map(state.entityTypesMap);
|
|
161
|
+
const oldType = newEntityTypesMap.get(id);
|
|
162
|
+
newEntityTypesMap.delete(id);
|
|
163
|
+
const defaultType = newEntityTypesMap.values().next().value;
|
|
164
|
+
const newTree3 = updateTreeTypes(state.tree, oldType, defaultType);
|
|
165
|
+
return {
|
|
166
|
+
...state,
|
|
167
|
+
tree: newTree3,
|
|
168
|
+
entityTypesMap: newEntityTypesMap,
|
|
169
|
+
selectedNodes: []
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** Entity type selection */
|
|
173
|
+
case "toggle-entity-type-selector":
|
|
174
|
+
return {
|
|
175
|
+
...state,
|
|
176
|
+
isSelectingEntityType: action.payload ?? !state.isSelectingEntityType
|
|
177
|
+
};
|
|
178
|
+
case "select-entity-type": {
|
|
179
|
+
let newTree22 = state.tree;
|
|
180
|
+
for (let id of state.selectedNodes) {
|
|
181
|
+
const keyPath2 = findNode(state.tree, id);
|
|
182
|
+
const nestedSpec = buildNestedSpec(keyPath2, {
|
|
183
|
+
type: { $set: action.payload }
|
|
184
|
+
});
|
|
185
|
+
newTree22 = update(newTree22, nestedSpec);
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
...state,
|
|
189
|
+
tree: newTree22,
|
|
190
|
+
selectedEntityType: action.payload
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
case "deselect":
|
|
194
|
+
return { ...state, selectedNodes: [] };
|
|
195
|
+
case "reset":
|
|
196
|
+
return {
|
|
197
|
+
...state,
|
|
198
|
+
tree: state.initialTree,
|
|
199
|
+
selectedNodes: []
|
|
200
|
+
};
|
|
201
|
+
case "set-view-mode":
|
|
202
|
+
return { ...state, viewMode: action.payload };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function nodeIsInTree(tree, id) {
|
|
206
|
+
for (let node of tree) {
|
|
207
|
+
if (node.id == id) {
|
|
208
|
+
return true;
|
|
209
|
+
} else if (node.children) {
|
|
210
|
+
if (nodeIsInTree(node.children, id)) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
function buildNestedSpec(keyPath, innerSpec) {
|
|
218
|
+
let spec = innerSpec;
|
|
219
|
+
for (let i = keyPath.length - 1; i >= 0; i--) {
|
|
220
|
+
spec = { [keyPath[i]]: spec };
|
|
221
|
+
}
|
|
222
|
+
return spec;
|
|
223
|
+
}
|
|
224
|
+
function findNode(tree, id) {
|
|
225
|
+
for (let i = 0; i < tree.length; i++) {
|
|
226
|
+
if (tree[i].id == id) {
|
|
227
|
+
return [i];
|
|
228
|
+
} else if (tree[i].children) {
|
|
229
|
+
let path = findNode(tree[i].children, id);
|
|
230
|
+
if (path != null) {
|
|
231
|
+
return [i, "children", ...path];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
function removeNodes(tree, ids) {
|
|
238
|
+
let newTree = [];
|
|
239
|
+
let removedNodes = [];
|
|
240
|
+
for (let node of tree) {
|
|
241
|
+
if (ids.includes(node.id)) {
|
|
242
|
+
removedNodes.push(node);
|
|
243
|
+
} else {
|
|
244
|
+
if (node.children) {
|
|
245
|
+
let [newChildren, removedChildren] = removeNodes(node.children, ids);
|
|
246
|
+
node = { ...node, children: newChildren };
|
|
247
|
+
removedNodes.push(...removedChildren);
|
|
248
|
+
}
|
|
249
|
+
newTree.push(node);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return [newTree, removedNodes];
|
|
253
|
+
}
|
|
254
|
+
function treeToGraph(tree) {
|
|
255
|
+
let nodes = [];
|
|
256
|
+
let edges = [];
|
|
257
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
258
|
+
for (let node of tree) {
|
|
259
|
+
if (nodeMap.has(node.id)) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const { indices, id, name, type, children } = node;
|
|
263
|
+
const nodeData = {
|
|
264
|
+
id,
|
|
265
|
+
type: type.id,
|
|
266
|
+
color: type.color,
|
|
267
|
+
name,
|
|
268
|
+
txt_range: [indices],
|
|
269
|
+
reasoning: null,
|
|
270
|
+
match: node.match,
|
|
271
|
+
children
|
|
272
|
+
};
|
|
273
|
+
nodeMap.set(node.id, node);
|
|
274
|
+
nodes.push(nodeData);
|
|
275
|
+
if (node.children) {
|
|
276
|
+
for (let child of node.children) {
|
|
277
|
+
edges.push({ source: node.id, dest: child.id });
|
|
278
|
+
}
|
|
279
|
+
const { nodes: childNodes, edges: childEdges } = treeToGraph(
|
|
280
|
+
node.children
|
|
281
|
+
);
|
|
282
|
+
nodes.push(...childNodes);
|
|
283
|
+
edges.push(...childEdges);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return { nodes, edges };
|
|
287
|
+
}
|
|
288
|
+
function findNodeById(tree, id) {
|
|
289
|
+
for (const node of tree) {
|
|
290
|
+
if (node.id === id) {
|
|
291
|
+
return node;
|
|
292
|
+
}
|
|
293
|
+
if (node.children) {
|
|
294
|
+
const found = findNodeById(node.children, id);
|
|
295
|
+
if (found) return found;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
function updateTreeTypes(tree, oldType, defaultType) {
|
|
301
|
+
return tree.map((node) => updateNodeType(node, oldType, defaultType));
|
|
302
|
+
}
|
|
303
|
+
function updateNodeType(node, oldType, defaultType) {
|
|
304
|
+
const type = node.type.id === oldType.id ? defaultType : node.type;
|
|
305
|
+
return {
|
|
306
|
+
...node,
|
|
307
|
+
type,
|
|
308
|
+
children: node.children ? updateTreeTypes(node.children, oldType, defaultType) : []
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function flattenAndSort(nodes) {
|
|
312
|
+
const result = [];
|
|
313
|
+
function traverse(nodeList) {
|
|
314
|
+
for (const node of nodeList) {
|
|
315
|
+
result.push(node);
|
|
316
|
+
if (Array.isArray(node.children) && node.children.length > 0) {
|
|
317
|
+
traverse(node.children);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
traverse(nodes);
|
|
322
|
+
return result.sort((a, b) => a.indices[0] - b.indices[0]);
|
|
323
|
+
}
|
|
324
|
+
function matchMode(state, action) {
|
|
325
|
+
if (action.type === "select-node" || action.type === "toggle-node-selected") {
|
|
326
|
+
const { ids } = action.payload;
|
|
327
|
+
if (ids.length != 1) return state;
|
|
328
|
+
if (state.selectedNodes.length === 1) {
|
|
329
|
+
if (ids[0] === state.selectedNodes[0]) {
|
|
330
|
+
return { ...state, selectedNodes: [] };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const type = action.payload.ids.length > 0 ? findNodeById(state.tree, ids[0])?.type : null;
|
|
334
|
+
return { ...state, selectedNodes: ids, selectedEntityType: type };
|
|
335
|
+
}
|
|
336
|
+
if (action.type === "add-match") {
|
|
337
|
+
const { id } = action.payload;
|
|
338
|
+
const keyPath = findNode(state.tree, id);
|
|
339
|
+
if (!keyPath) {
|
|
340
|
+
console.warn(`Node with id ${id} not found`);
|
|
341
|
+
return state;
|
|
342
|
+
}
|
|
343
|
+
const matchUpdateSpec = buildNestedSpec(keyPath, {
|
|
344
|
+
match: { $set: action.payload.payload }
|
|
345
|
+
});
|
|
346
|
+
const updatedTree = update(state.tree, matchUpdateSpec);
|
|
347
|
+
return {
|
|
348
|
+
...state,
|
|
349
|
+
tree: updatedTree
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
if (action.type === "remove-match") {
|
|
353
|
+
const { id } = action.payload;
|
|
354
|
+
console.log("Removing match for node with id:", id);
|
|
355
|
+
const keyPath = findNode(state.tree, id);
|
|
356
|
+
if (!keyPath) {
|
|
357
|
+
console.warn(`Node with id ${id} not found`);
|
|
358
|
+
return state;
|
|
359
|
+
}
|
|
360
|
+
const matchUpdateSpec = buildNestedSpec(keyPath, {
|
|
361
|
+
match: { $set: null }
|
|
362
|
+
});
|
|
363
|
+
const updatedTree = update(state.tree, matchUpdateSpec);
|
|
364
|
+
return {
|
|
365
|
+
...state,
|
|
366
|
+
tree: updatedTree
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
return state;
|
|
370
|
+
}
|
|
371
|
+
function viewMode(state, action) {
|
|
372
|
+
if (action.type === "set-view-mode") {
|
|
373
|
+
return { ...state, viewMode: action.payload };
|
|
374
|
+
}
|
|
375
|
+
return state;
|
|
376
|
+
}
|
|
377
|
+
exports.TreeDispatchContext = TreeDispatchContext;
|
|
378
|
+
exports.findNodeById = findNodeById;
|
|
379
|
+
exports.treeToGraph = treeToGraph;
|
|
380
|
+
exports.useTreeDispatch = useTreeDispatch;
|
|
381
|
+
exports.useUpdatableTree = useUpdatableTree;
|
|
382
|
+
//# sourceMappingURL=edit-state.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edit-state.cjs","sources":["../../src/feedback/edit-state.ts"],"sourcesContent":["import { TreeData } from \"./types\";\nimport { createContext, Dispatch, useContext, useReducer } from \"react\";\nimport update, { Spec } from \"immutability-helper\";\nimport { EntityType } from \"../extractions/types\";\n\nexport enum ViewMode {\n Tree = \"tree\",\n Graph = \"graph\",\n}\n\ninterface TreeState {\n initialTree: TreeData[];\n tree: TreeData[];\n selectedNodes: number[];\n entityTypesMap: Map<number, EntityType>;\n selectedEntityType: EntityType;\n lastInternalId: number;\n isSelectingEntityType: boolean;\n viewMode: ViewMode;\n viewOnly: boolean;\n matchMode: boolean;\n}\n\ntype TextRange = {\n start: number;\n end: number;\n text: string;\n};\n\ntype TreeAction =\n | {\n type: \"move-node\";\n payload: { dragIds: number[]; parentId: number; index: number };\n }\n | { type: \"delete-node\"; payload: { ids: number[] } }\n | { type: \"select-node\"; payload: { ids: number[] } }\n | { type: \"toggle-node-selected\"; payload: { ids: number[] } }\n | { type: \"set-view-mode\"; payload: ViewMode }\n | { type: \"create-node\"; payload: TextRange }\n | { type: \"select-entity-type\"; payload: EntityType }\n | { type: \"toggle-entity-type-selector\"; payload?: boolean | null }\n | { type: \"deselect\" }\n | { type: \"reset\" }\n | { type: \"delete-entity-type\"; payload: { id: number } }\n | {\n type: \"add-entity-type\";\n payload: { name: string; description: string; color: string };\n }\n | {\n type: \"update-entity-type\";\n payload: { id: number; name: string; description: string; color: string };\n }\n | { type: \"select-range\"; payload: { ids: number[] } }\n | { type: \"add-match\"; payload: { id: number; payload: any } }\n | { type: \"remove-match\"; payload: { id: number } }\n | { type: \"toggle-match-mode\" }\n | { type: \"toggle-view-only\" };\n\nexport type TreeDispatch = Dispatch<TreeAction>;\n\nexport function useUpdatableTree(\n initialTree: TreeData[],\n entityTypes: Map<number, EntityType>,\n viewOnly: boolean,\n matchMode: boolean,\n autoSelect: string[] = [],\n): [TreeState, TreeDispatch] {\n // Get the first entity type\n // issue: grabs second entity instead of selected one\n const type = entityTypes.values().next().value;\n\n let selectedNodes = [];\n autoSelect = autoSelect.map((name) => name.toLowerCase());\n\n if (autoSelect.length > 0) {\n // If autoSelect is provided, find the nodes with the matching names\n selectedNodes = initialTree\n .flatMap((node) => node.children ?? [])\n .filter((node) => autoSelect.includes(node.name.toLowerCase()))\n .map((node) => node.id);\n }\n\n return useReducer(treeReducer, {\n initialTree,\n tree: initialTree,\n selectedNodes,\n entityTypesMap: entityTypes,\n selectedEntityType: type,\n lastInternalId: 0,\n isSelectingEntityType: false,\n viewMode: ViewMode.Tree,\n viewOnly,\n matchMode,\n });\n}\n\nexport const TreeDispatchContext = createContext<TreeDispatch | null>(null);\n\nexport function useTreeDispatch() {\n const dispatch = useContext(TreeDispatchContext);\n if (dispatch == null) {\n throw new Error(\"No dispatch context available\");\n }\n return dispatch;\n}\n\nfunction treeReducer(state: TreeState, action: TreeAction) {\n if (action.type === \"toggle-match-mode\") {\n return { ...state, matchMode: !state.matchMode };\n }\n\n if (action.type === \"toggle-view-only\") {\n return { ...state, viewOnly: !state.viewOnly, selectedNodes: [] };\n }\n\n if (state.viewOnly) return viewMode(state, action);\n\n if (state.matchMode) return matchMode(state, action);\n\n switch (action.type) {\n case \"add-entity-type\": {\n // Add a new entity type to the map\n const { name, description, color } = action.payload;\n const newId = state.lastInternalId - 1;\n const newType: EntityType = {\n id: newId,\n name,\n description: description === \"\" ? null : description,\n color,\n };\n\n const newEntityTypesMap = new Map(state.entityTypesMap);\n newEntityTypesMap.set(newId, newType);\n\n return {\n ...state,\n entityTypesMap: newEntityTypesMap,\n selectedEntityType: newType,\n lastInternalId: newId,\n };\n }\n case \"update-entity-type\": {\n // Update an existing entity type in the map\n const { id, name, description, color } = action.payload;\n const newEntityTypesMap = new Map(state.entityTypesMap);\n const oldType = newEntityTypesMap.get(id);\n\n if (!oldType) {\n console.warn(`Entity type with id ${id} not found`);\n return state;\n }\n\n const updatedType: EntityType = {\n ...oldType,\n name,\n description: description === \"\" ? null : description,\n color,\n };\n\n newEntityTypesMap.set(id, updatedType);\n\n // Update the tree to reflect the new type\n const newTree = updateTreeTypes(state.tree, oldType, updatedType);\n\n return {\n ...state,\n tree: newTree,\n entityTypesMap: newEntityTypesMap,\n selectedEntityType: updatedType,\n };\n }\n case \"select-range\":\n // Select a range of nodes by their IDs\n const payloadIds = action.payload.ids;\n const node1 = payloadIds[0];\n const node2 = payloadIds[1];\n\n // make list of nodes in order\n const allNodes = flattenAndSort(state.tree);\n\n // select all nodes between node1 and node2\n const startIndex = allNodes.findIndex((node) => node.id === node1);\n const endIndex = allNodes.findIndex((node) => node.id === node2);\n\n const selectedNodes = allNodes.slice(startIndex, endIndex + 1);\n\n console.log(\"Selecting range:\", selectedNodes);\n return {\n ...state,\n selectedNodes: selectedNodes.map((node) => node.id),\n };\n\n case \"move-node\":\n // For each node in the tree, if the node is in the dragIds, remove it from the tree and collect it\n const [newTree, removedNodes] = removeNodes(\n state.tree,\n action.payload.dragIds,\n );\n\n let keyPath: (number | \"children\")[] = [];\n if (action.payload.parentId) {\n keyPath = findNode(newTree, action.payload.parentId);\n keyPath.push(\"children\");\n }\n\n // Add removed nodes to the new tree at the correct location\n let updateSpec = buildNestedSpec(keyPath, {\n $splice: [[action.payload.index, 0, ...removedNodes]],\n });\n\n return { ...state, tree: update(newTree, updateSpec) };\n case \"delete-node\":\n // For each node in the tree, if the node is in the ids, remove it from the tree\n const [newTree2, _removedNodes] = removeNodes(\n state.tree,\n action.payload.ids,\n );\n // Get children of the removed nodes\n // If children are not present elsewhere in the tree, insert them\n\n const children = _removedNodes\n .flatMap((node) => node.children ?? [])\n .filter((child) => !nodeIsInTree(newTree2, child.id));\n\n // Reset the selection\n\n return {\n ...state,\n tree: [...newTree2, ...children],\n selectedNodes: state.selectedNodes.filter(\n (id) => !action.payload.ids.includes(id),\n ),\n };\n case \"select-node\":\n const { ids } = action.payload;\n\n const type =\n action.payload.ids.length > 0\n ? findNodeById(state.tree, ids[0])?.type\n : null;\n\n return { ...state, selectedNodes: ids, selectedEntityType: type };\n // otherwise fall through to toggle-node-selected for a single ID\n case \"toggle-node-selected\":\n const nodesToAdd = action.payload.ids.filter(\n (id) => !state.selectedNodes.includes(id),\n );\n const nodesToKeep = state.selectedNodes.filter(\n (id) => !action.payload.ids.includes(id),\n );\n\n const newType =\n action.payload.ids.length > 0\n ? findNodeById(state.tree, action.payload.ids[0])?.type\n : null;\n\n return {\n ...state,\n selectedNodes: [...nodesToKeep, ...nodesToAdd],\n selectedEntityType: newType,\n };\n\n case \"create-node\":\n const newId = state.lastInternalId - 1;\n const { text, start, end } = action.payload;\n const node: TreeData = {\n id: newId,\n name: text,\n children: [],\n indices: [start, end],\n type: state.selectedEntityType,\n };\n\n return {\n ...state,\n tree: [...state.tree, node],\n selectedNodes: [newId],\n lastInternalId: newId,\n };\n\n case \"delete-entity-type\": {\n // Remove the entity type from the map\n const { id } = action.payload;\n const newEntityTypesMap = new Map(state.entityTypesMap);\n const oldType = newEntityTypesMap.get(id);\n newEntityTypesMap.delete(id);\n\n const defaultType = newEntityTypesMap.values().next().value;\n const newTree = updateTreeTypes(state.tree, oldType, defaultType);\n\n return {\n ...state,\n tree: newTree,\n entityTypesMap: newEntityTypesMap,\n selectedNodes: [],\n };\n }\n\n /** Entity type selection */\n case \"toggle-entity-type-selector\":\n return {\n ...state,\n isSelectingEntityType: action.payload ?? !state.isSelectingEntityType,\n };\n case \"select-entity-type\": {\n // For each selected node, update the type\n let newTree2 = state.tree;\n for (let id of state.selectedNodes) {\n const keyPath = findNode(state.tree, id);\n const nestedSpec = buildNestedSpec(keyPath, {\n type: { $set: action.payload },\n });\n newTree2 = update(newTree2, nestedSpec);\n }\n\n return {\n ...state,\n tree: newTree2,\n selectedEntityType: action.payload,\n };\n }\n case \"deselect\":\n return { ...state, selectedNodes: [] };\n case \"reset\":\n return {\n ...state,\n tree: state.initialTree,\n selectedNodes: [],\n };\n case \"set-view-mode\":\n return { ...state, viewMode: action.payload };\n }\n}\n\nfunction nodeIsInTree(tree: TreeData[], id: number): boolean {\n for (let node of tree) {\n if (node.id == id) {\n return true;\n } else if (node.children) {\n if (nodeIsInTree(node.children, id)) {\n return true;\n }\n }\n }\n return false;\n}\n\nfunction buildNestedSpec(\n keyPath: (number | \"children\")[],\n innerSpec: Spec<any>,\n): Spec<TreeData[]> {\n // Build a nested object from a key path\n\n let spec = innerSpec;\n for (let i = keyPath.length - 1; i >= 0; i--) {\n spec = { [keyPath[i]]: spec };\n }\n return spec as any;\n // Since we don't have a \"children\" key at the root, we make the top-level spec an array\n}\n\nfunction findNode(\n tree: TreeData[],\n id: number,\n): (number | \"children\")[] | null {\n // Find the index of the node with the given id in the tree, returning the key path\n for (let i = 0; i < tree.length; i++) {\n if (tree[i].id == id) {\n return [i];\n } else if (tree[i].children) {\n let path = findNode(tree[i].children, id);\n if (path != null) {\n return [i, \"children\", ...path];\n }\n }\n }\n return null;\n}\n\nfunction removeNodes(\n tree: TreeData[],\n ids: number[],\n): [TreeData[], TreeData[]] {\n /** Remove nodes with the given ids from the tree and return the new tree and the removed nodes */\n let newTree: TreeData[] = [];\n let removedNodes: TreeData[] = [];\n\n for (let node of tree) {\n if (ids.includes(node.id)) {\n removedNodes.push(node);\n } else {\n // Recurse into children\n if (node.children) {\n let [newChildren, removedChildren] = removeNodes(node.children, ids);\n node = { ...node, children: newChildren };\n removedNodes.push(...removedChildren);\n }\n newTree.push(node);\n }\n }\n\n return [newTree, removedNodes];\n}\n\nexport interface EntityOutput {\n id: number;\n type: number | null;\n txt_range: number[][];\n name: string;\n match: any | null;\n reasoning: string | null;\n color: string | null;\n children: any[] | null;\n}\n\nexport interface GraphData {\n nodes: EntityOutput[];\n edges: { source: number; dest: number }[];\n}\n\nexport function treeToGraph(tree: TreeData[]): GraphData {\n // Convert the tree to a graph\n let nodes: EntityOutput[] = [];\n let edges: { source: number; dest: number }[] = [];\n const nodeMap = new Map<number, TreeData>();\n\n for (let node of tree) {\n // If we've already found an instance of this node, we don't need to record\n // it again\n if (nodeMap.has(node.id)) {\n continue;\n }\n\n const { indices, id, name, type, children } = node;\n\n const nodeData: EntityOutput = {\n id,\n type: type.id,\n color: type.color,\n name,\n txt_range: [indices],\n reasoning: null,\n match: node.match,\n children,\n };\n\n nodeMap.set(node.id, node);\n nodes.push(nodeData);\n\n if (node.children) {\n for (let child of node.children) {\n edges.push({ source: node.id, dest: child.id });\n }\n\n // Now process the children\n const { nodes: childNodes, edges: childEdges } = treeToGraph(\n node.children,\n );\n nodes.push(...childNodes);\n edges.push(...childEdges);\n }\n }\n\n return { nodes, edges };\n}\n\nexport function findNodeById(tree, id) {\n for (const node of tree) {\n if (node.id === id) {\n return node;\n }\n if (node.children) {\n const found = findNodeById(node.children, id);\n if (found) return found;\n }\n }\n return null;\n}\n\nfunction updateTreeTypes(tree, oldType, defaultType) {\n return tree.map((node) => updateNodeType(node, oldType, defaultType));\n}\n\nfunction updateNodeType(node, oldType, defaultType) {\n const type = node.type.id === oldType.id ? defaultType : node.type;\n\n return {\n ...node,\n type,\n children: node.children\n ? updateTreeTypes(node.children, oldType, defaultType)\n : [],\n };\n}\n\nfunction flattenAndSort(nodes) {\n const result = [];\n\n function traverse(nodeList) {\n for (const node of nodeList) {\n result.push(node);\n if (Array.isArray(node.children) && node.children.length > 0) {\n traverse(node.children);\n }\n }\n }\n\n traverse(nodes);\n\n // sort by start\n return result.sort((a, b) => a.indices[0] - b.indices[0]);\n}\n\nfunction matchMode(state, action) {\n if (action.type === \"select-node\" || action.type === \"toggle-node-selected\") {\n const { ids } = action.payload;\n\n if (ids.length != 1) return state;\n\n if (state.selectedNodes.length === 1) {\n if (ids[0] === state.selectedNodes[0]) {\n // If the selected node is the same as the current selection, deselect it\n return { ...state, selectedNodes: [] };\n }\n }\n\n const type =\n action.payload.ids.length > 0\n ? findNodeById(state.tree, ids[0])?.type\n : null;\n\n return { ...state, selectedNodes: ids, selectedEntityType: type };\n }\n\n if (action.type === \"add-match\") {\n const { id } = action.payload;\n\n // Find the node path\n const keyPath = findNode(state.tree, id);\n if (!keyPath) {\n console.warn(`Node with id ${id} not found`);\n return state;\n }\n\n // Build update spec to set the `match` property\n const matchUpdateSpec = buildNestedSpec(keyPath, {\n match: { $set: action.payload.payload },\n });\n\n const updatedTree = update(state.tree, matchUpdateSpec);\n\n return {\n ...state,\n tree: updatedTree,\n };\n }\n\n if (action.type === \"remove-match\") {\n const { id } = action.payload;\n\n console.log(\"Removing match for node with id:\", id);\n\n // Find the node path\n const keyPath = findNode(state.tree, id);\n if (!keyPath) {\n console.warn(`Node with id ${id} not found`);\n return state;\n }\n\n // Build update spec to unset the `match` property\n const matchUpdateSpec = buildNestedSpec(keyPath, {\n match: { $set: null },\n });\n\n const updatedTree = update(state.tree, matchUpdateSpec);\n\n return {\n ...state,\n tree: updatedTree,\n };\n }\n\n return state;\n}\n\nfunction viewMode(state, action) {\n if (action.type === \"set-view-mode\") {\n return { ...state, viewMode: action.payload };\n }\n\n return state;\n}\n"],"names":["matchMode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AK,CACrB;AAED,UAAM,cAAc,OAAO,MAAM,MAAM,eAAe;AAEtD,WAAO;AAAA,MACL,GAAG;AAAA,MACH,MAAM;AAAA,IAAA;AAAA,EAEV;AAEA,SAAO;AACT;AAEA,SAAS,SAAS,OAAO,QAAQ;AAC/B,MAAI,OAAO,SAAS,iBAAiB;AACnC,WAAO,EAAE,GAAG,OAAO,UAAU,OAAO,QAAA;AAAA,EACtC;AAEA,SAAO;AACT;;;;;;"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { TreeData } from './types';
|
|
2
|
+
import { Dispatch } from 'react';
|
|
3
|
+
import { EntityType } from '../extractions/types';
|
|
4
|
+
export declare enum ViewMode {
|
|
5
|
+
Tree = "tree",
|
|
6
|
+
Graph = "graph"
|
|
7
|
+
}
|
|
8
|
+
interface TreeState {
|
|
9
|
+
initialTree: TreeData[];
|
|
10
|
+
tree: TreeData[];
|
|
11
|
+
selectedNodes: number[];
|
|
12
|
+
entityTypesMap: Map<number, EntityType>;
|
|
13
|
+
selectedEntityType: EntityType;
|
|
14
|
+
lastInternalId: number;
|
|
15
|
+
isSelectingEntityType: boolean;
|
|
16
|
+
viewMode: ViewMode;
|
|
17
|
+
viewOnly: boolean;
|
|
18
|
+
matchMode: boolean;
|
|
19
|
+
}
|
|
20
|
+
type TextRange = {
|
|
21
|
+
start: number;
|
|
22
|
+
end: number;
|
|
23
|
+
text: string;
|
|
24
|
+
};
|
|
25
|
+
type TreeAction = {
|
|
26
|
+
type: "move-node";
|
|
27
|
+
payload: {
|
|
28
|
+
dragIds: number[];
|
|
29
|
+
parentId: number;
|
|
30
|
+
index: number;
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
33
|
+
type: "delete-node";
|
|
34
|
+
payload: {
|
|
35
|
+
ids: number[];
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
type: "select-node";
|
|
39
|
+
payload: {
|
|
40
|
+
ids: number[];
|
|
41
|
+
};
|
|
42
|
+
} | {
|
|
43
|
+
type: "toggle-node-selected";
|
|
44
|
+
payload: {
|
|
45
|
+
ids: number[];
|
|
46
|
+
};
|
|
47
|
+
} | {
|
|
48
|
+
type: "set-view-mode";
|
|
49
|
+
payload: ViewMode;
|
|
50
|
+
} | {
|
|
51
|
+
type: "create-node";
|
|
52
|
+
payload: TextRange;
|
|
53
|
+
} | {
|
|
54
|
+
type: "select-entity-type";
|
|
55
|
+
payload: EntityType;
|
|
56
|
+
} | {
|
|
57
|
+
type: "toggle-entity-type-selector";
|
|
58
|
+
payload?: boolean | null;
|
|
59
|
+
} | {
|
|
60
|
+
type: "deselect";
|
|
61
|
+
} | {
|
|
62
|
+
type: "reset";
|
|
63
|
+
} | {
|
|
64
|
+
type: "delete-entity-type";
|
|
65
|
+
payload: {
|
|
66
|
+
id: number;
|
|
67
|
+
};
|
|
68
|
+
} | {
|
|
69
|
+
type: "add-entity-type";
|
|
70
|
+
payload: {
|
|
71
|
+
name: string;
|
|
72
|
+
description: string;
|
|
73
|
+
color: string;
|
|
74
|
+
};
|
|
75
|
+
} | {
|
|
76
|
+
type: "update-entity-type";
|
|
77
|
+
payload: {
|
|
78
|
+
id: number;
|
|
79
|
+
name: string;
|
|
80
|
+
description: string;
|
|
81
|
+
color: string;
|
|
82
|
+
};
|
|
83
|
+
} | {
|
|
84
|
+
type: "select-range";
|
|
85
|
+
payload: {
|
|
86
|
+
ids: number[];
|
|
87
|
+
};
|
|
88
|
+
} | {
|
|
89
|
+
type: "add-match";
|
|
90
|
+
payload: {
|
|
91
|
+
id: number;
|
|
92
|
+
payload: any;
|
|
93
|
+
};
|
|
94
|
+
} | {
|
|
95
|
+
type: "remove-match";
|
|
96
|
+
payload: {
|
|
97
|
+
id: number;
|
|
98
|
+
};
|
|
99
|
+
} | {
|
|
100
|
+
type: "toggle-match-mode";
|
|
101
|
+
} | {
|
|
102
|
+
type: "toggle-view-only";
|
|
103
|
+
};
|
|
104
|
+
export type TreeDispatch = Dispatch<TreeAction>;
|
|
105
|
+
export declare function useUpdatableTree(initialTree: TreeData[], entityTypes: Map<number, EntityType>, viewOnly: boolean, matchMode: boolean, autoSelect?: string[]): [TreeState, TreeDispatch];
|
|
106
|
+
export declare const TreeDispatchContext: import('react').Context<TreeDispatch>;
|
|
107
|
+
export declare function useTreeDispatch(): TreeDispatch;
|
|
108
|
+
export interface EntityOutput {
|
|
109
|
+
id: number;
|
|
110
|
+
type: number | null;
|
|
111
|
+
txt_range: number[][];
|
|
112
|
+
name: string;
|
|
113
|
+
match: any | null;
|
|
114
|
+
reasoning: string | null;
|
|
115
|
+
color: string | null;
|
|
116
|
+
children: any[] | null;
|
|
117
|
+
}
|
|
118
|
+
export interface GraphData {
|
|
119
|
+
nodes: EntityOutput[];
|
|
120
|
+
edges: {
|
|
121
|
+
source: number;
|
|
122
|
+
dest: number;
|
|
123
|
+
}[];
|
|
124
|
+
}
|
|
125
|
+
export declare function treeToGraph(tree: TreeData[]): GraphData;
|
|
126
|
+
export declare function findNodeById(tree: any, id: any): any;
|
|
127
|
+
export {};
|