1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
import { action, makeObservable, observable } from 'mobx';
import { Doc, Opt } from '../../fields/Doc';
import { DocViews } from '../../fields/DocSymbols';
import { List } from '../../fields/List';
import { listSpec } from '../../fields/Schema';
import { Cast, DocCast } from '../../fields/Types';
import { CollectionViewType, DocumentType } from '../documents/DocumentTypes';
import { DocumentView } from '../views/nodes/DocumentView';
import { LinkManager } from './LinkManager';
import { ScriptingGlobals } from './ScriptingGlobals';
import { UndoManager } from './UndoManager';
export class SelectionManager {
private static _manager: SelectionManager;
private static get Instance() {
return SelectionManager._manager ?? new SelectionManager();
}
@observable.shallow SelectedViews: DocumentView[] = [];
@observable IsDragging: boolean = false;
@observable SelectedSchemaDocument: Doc | undefined = undefined;
private constructor() {
SelectionManager._manager = this;
makeObservable(this);
}
@action
public static SelectSchemaViewDoc(doc: Opt<Doc>, deselectAllFirst?: boolean) {
if (deselectAllFirst) this.DeselectAll();
this.Instance.SelectedSchemaDocument = doc;
}
@action
public static SelectView(docView: DocumentView, extendSelection: boolean): void {
if (!docView) this.DeselectAll();
else if (!docView.SELECTED) {
if (!extendSelection) this.DeselectAll();
this.Instance.SelectedViews.push(docView);
docView.SELECTED = true;
docView._props.whenChildContentsActiveChanged(true);
}
}
@action
public static DeselectView(docView?: DocumentView): void {
if (docView && this.Instance.SelectedViews.includes(docView)) {
docView.SELECTED = false;
this.Instance.SelectedViews.splice(this.Instance.SelectedViews.indexOf(docView), 1);
docView._props.whenChildContentsActiveChanged(false);
}
}
@action
public static DeselectAll(except?: Doc): void {
const found = this.Instance.SelectedViews.find(dv => dv.Document === except);
LinkManager.currentLink = undefined;
LinkManager.currentLinkAnchor = undefined;
this.Instance.SelectedSchemaDocument = undefined;
this.Instance.SelectedViews.forEach(dv => {
dv.SELECTED = false;
dv._props.whenChildContentsActiveChanged(false);
});
this.Instance.SelectedViews.length = 0;
if (found) this.SelectView(found, false);
}
public static IsSelected = (doc?: Doc) => Array.from(doc?.[DocViews] ?? []).some(dv => dv?.SELECTED);
public static get Views() { return this.Instance.SelectedViews; } // prettier-ignore
public static get SelectedSchemaDoc() { return this.Instance.SelectedSchemaDocument; } // prettier-ignore
public static get Docs() { return this.Instance.SelectedViews.map(dv => dv.Document).filter(doc => doc?._type_collection !== CollectionViewType.Docking); } // prettier-ignore
}
ScriptingGlobals.add(function SelectionManager_selectedDocType(type: string, expertMode: boolean, checkContext?: boolean) {
if (Doc.noviceMode && expertMode) return false;
if (type === 'tab') {
return SelectionManager.Views.lastElement()?._props.renderDepth === 0;
}
let selected = (sel => (checkContext ? DocCast(sel?.embedContainer) : sel))(SelectionManager.SelectedSchemaDoc ?? SelectionManager.Docs.lastElement());
return selected?.type === type || selected?.type_collection === type || !type;
});
ScriptingGlobals.add(function deselectAll() {
SelectionManager.DeselectAll();
});
ScriptingGlobals.add(function undo() {
SelectionManager.DeselectAll();
return UndoManager.Undo();
});
export function ShowUndoStack() {
SelectionManager.DeselectAll();
var buffer = '';
UndoManager.undoStack.forEach((batch, i) => {
buffer += 'Batch => ' + UndoManager.undoStackNames[i] + '\n';
///batch.forEach(undo => (buffer += ' ' + undo.prop + '\n'));
});
alert(buffer);
}
ScriptingGlobals.add(function redo() {
SelectionManager.DeselectAll();
return UndoManager.Redo();
});
ScriptingGlobals.add(function selectedDocs(container: Doc, excludeCollections: boolean, prevValue: any) {
const docs = SelectionManager.Views.map(dv => dv.Document).filter(
d => !Doc.AreProtosEqual(d, container) && !d.annotationOn && d.type !== DocumentType.KVP && (!excludeCollections || d.type !== DocumentType.COL || !Cast(d.data, listSpec(Doc), null))
);
return docs.length ? new List(docs) : prevValue;
});
|