|
@@ -5,15 +5,14 @@
|
|
|
*/
|
|
|
|
|
|
import { Structure } from '../../mol-model/structure';
|
|
|
-import { State, StateObject, StateSelection, StateObjectCell, StateTransform } from '../../mol-state';
|
|
|
-import { PluginContext } from '../context';
|
|
|
import { PluginStateObject } from '../../mol-plugin-state/objects';
|
|
|
-import { arraySetAdd, arraySetRemove } from '../../mol-util/array';
|
|
|
+import { State, StateObject, StateObjectCell, StateSelection } from '../../mol-state';
|
|
|
+import { PluginContext } from '../context';
|
|
|
|
|
|
export { SubstructureParentHelper };
|
|
|
|
|
|
class SubstructureParentHelper {
|
|
|
- private decorators = new Map<string, string[]>();
|
|
|
+ // private decorators = new Map<string, string[]>();
|
|
|
private root = new Map<Structure, { ref: string, count: number }>();
|
|
|
private tracked = new Map<string, Structure>();
|
|
|
|
|
@@ -21,46 +20,7 @@ class SubstructureParentHelper {
|
|
|
get(s: Structure, ignoreDecorators = false): StateObjectCell<PluginStateObject.Molecule.Structure> | undefined {
|
|
|
const r = this.root.get(s);
|
|
|
if (!r) return;
|
|
|
- const decorators = this.decorators.get(r.ref);
|
|
|
- if (ignoreDecorators || !decorators) return this.plugin.state.data.cells.get(r.ref);
|
|
|
- return this.plugin.state.data.cells.get(this.findDeepestDecorator(r.ref, decorators));
|
|
|
- }
|
|
|
-
|
|
|
- private findDeepestDecorator(ref: string, decorators: string[]) {
|
|
|
- if (decorators.length === 0) return ref;
|
|
|
- if (decorators.length === 1) return decorators[0];
|
|
|
-
|
|
|
- const cells = this.plugin.state.data.cells;
|
|
|
- let depth = 0, ret = ref;
|
|
|
- for (const dr of decorators) {
|
|
|
- let c = cells.get(dr);
|
|
|
- let d = 0;
|
|
|
- while (c && c.transform.ref !== StateTransform.RootRef) {
|
|
|
- d++;
|
|
|
- c = cells.get(c.transform.parent);
|
|
|
- }
|
|
|
- if (d > depth) {
|
|
|
- ret = dr;
|
|
|
- depth = d;
|
|
|
- }
|
|
|
- }
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- private addDecorator(root: string, ref: string) {
|
|
|
- if (this.decorators.has(root)) {
|
|
|
- arraySetAdd(this.decorators.get(root)!, ref);
|
|
|
- } else {
|
|
|
- this.decorators.set(root, [ref]);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private tryRemoveDecorator(root: string, ref: string) {
|
|
|
- if (this.decorators.has(root)) {
|
|
|
- const xs = this.decorators.get(root)!;
|
|
|
- arraySetRemove(xs, ref);
|
|
|
- if (xs.length === 0) this.decorators.delete(root);
|
|
|
- }
|
|
|
+ return this.plugin.state.data.cells.get(r.ref);
|
|
|
}
|
|
|
|
|
|
private addMapping(state: State, ref: string, obj: StateObject) {
|
|
@@ -68,11 +28,9 @@ class SubstructureParentHelper {
|
|
|
|
|
|
this.tracked.set(ref, obj.data);
|
|
|
|
|
|
- let parentRef;
|
|
|
// if the structure is already present in the tree, do not rewrite the root.
|
|
|
if (this.root.has(obj.data)) {
|
|
|
const e = this.root.get(obj.data)!;
|
|
|
- parentRef = e.ref;
|
|
|
e.count++;
|
|
|
} else {
|
|
|
const parent = state.select(StateSelection.Generators.byRef(ref).rootOfType([PluginStateObject.Molecule.Structure]))[0];
|
|
@@ -80,17 +38,9 @@ class SubstructureParentHelper {
|
|
|
if (!parent) {
|
|
|
this.root.set(obj.data, { ref, count: 1 });
|
|
|
} else {
|
|
|
- parentRef = parent.transform.ref;
|
|
|
- this.root.set(obj.data, { ref: parentRef, count: 1 });
|
|
|
+ this.root.set(obj.data, { ref: parent.transform.ref, count: 1 });
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- if (!parentRef) return;
|
|
|
-
|
|
|
- const cell = state.cells.get(ref);
|
|
|
- if (cell?.transform.isDecorator) {
|
|
|
- this.addDecorator(parentRef, ref);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
private removeMapping(ref: string) {
|
|
@@ -101,8 +51,6 @@ class SubstructureParentHelper {
|
|
|
|
|
|
const root = this.root.get(s)!;
|
|
|
|
|
|
- this.tryRemoveDecorator(root.ref, ref);
|
|
|
-
|
|
|
if (root.count > 1) {
|
|
|
root.count--;
|
|
|
} else {
|