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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
import { action, runInAction } from "mobx";
import { Document } from "../../../fields/Document";
import { ListField } from "../../../fields/ListField";
import React = require("react");
import { KeyStore } from "../../../fields/KeyStore";
import { FieldWaiting, Opt } from "../../../fields/Field";
import { undoBatch } from "../../util/UndoManager";
import { DragManager } from "../../util/DragManager";
import { Documents, DocumentOptions } from "../../documents/Documents";
import { Key } from "../../../fields/Key";
import { Transform } from "../../util/Transform";
import { CollectionView } from "./CollectionView";
import { RouteStore } from "../../../server/RouteStore";
import { TupleField } from "../../../fields/TupleField";
import { CurrentUserUtils } from "../../../server/authentication/models/current_user_utils";
import { NumberField } from "../../../fields/NumberField";
import { DocumentManager } from "../../util/DocumentManager";
import request = require("request");
import { ServerUtils } from "../../../server/ServerUtil";
import { addHiddenFinalProp } from "mobx/lib/internal";
export interface CollectionViewProps {
fieldKey: Key;
Document: Document;
ScreenToLocalTransform: () => Transform;
isSelected: () => boolean;
isTopMost: boolean;
select: (ctrlPressed: boolean) => void;
bindings: any;
panelWidth: () => number;
panelHeight: () => number;
focus: (doc: Document) => void;
}
export interface SubCollectionViewProps extends CollectionViewProps {
active: () => boolean;
addDocument: (doc: Document, allowDuplicates: boolean) => boolean;
removeDocument: (doc: Document) => boolean;
CollectionView: CollectionView;
}
export type CursorEntry = TupleField<[string, string], [number, number]>;
export class CollectionViewBase extends React.Component<SubCollectionViewProps> {
private dropDisposer?: DragManager.DragDropDisposer;
protected createDropTarget = (ele: HTMLDivElement) => {
if (this.dropDisposer) {
this.dropDisposer();
}
if (ele) {
this.dropDisposer = DragManager.MakeDropTarget(ele, { handlers: { drop: this.drop.bind(this) } });
}
}
@action
protected setCursorPosition(position: [number, number]) {
let ind;
let doc = this.props.Document;
let id = CurrentUserUtils.id;
let email = CurrentUserUtils.email;
if (id && email) {
let textInfo: [string, string] = [id, email];
doc.GetOrCreateAsync<ListField<CursorEntry>>(KeyStore.Cursors, ListField, field => {
let cursors = field.Data;
if (cursors.length > 0 && (ind = cursors.findIndex(entry => entry.Data[0][0] === id)) > -1) {
cursors[ind].Data[1] = position;
} else {
let entry = new TupleField<[string, string], [number, number]>([textInfo, position]);
cursors.push(entry);
}
})
}
}
protected getCursors(): CursorEntry[] {
let doc = this.props.Document;
let id = CurrentUserUtils.id;
let cursors = doc.GetList<CursorEntry>(KeyStore.Cursors, []);
let notMe = cursors.filter(entry => entry.Data[0][0] !== id);
return id ? notMe : [];
}
@undoBatch
@action
protected drop(e: Event, de: DragManager.DropEvent): boolean {
if (de.data instanceof DragManager.DocumentDragData) {
if (de.data.aliasOnDrop) {
[KeyStore.Width, KeyStore.Height, KeyStore.CurPage].map(key =>
de.data.draggedDocument.GetTAsync(key, NumberField, (f: Opt<NumberField>) => f ? de.data.droppedDocument.SetNumber(key, f.Data) : null));
}
let added = this.props.addDocument(de.data.droppedDocument, false);
if (added && de.data.removeDocument && !de.data.aliasOnDrop) {
de.data.removeDocument(this.props.CollectionView);
}
e.stopPropagation();
return added;
}
return false;
}
protected getDocumentFromType(type: string, path: string, options: DocumentOptions): Opt<Document> {
let ctor: ((path: string, options: DocumentOptions) => Document) | undefined;
if (type.indexOf("image") !== -1) {
ctor = Documents.ImageDocument;
}
if (type.indexOf("video") !== -1) {
ctor = Documents.VideoDocument;
}
if (type.indexOf("audio") !== -1) {
ctor = Documents.AudioDocument;
}
if (type.indexOf("pdf") !== -1) {
ctor = Documents.PdfDocument;
}
if (type.indexOf("html") !== -1) {
ctor = Documents.WebDocument;
options = { height: options.width, ...options, };
}
return ctor ? ctor(path, options) : undefined;
}
@action
protected onDrop(e: React.DragEvent, options: DocumentOptions): void {
let that = this;
let html = e.dataTransfer.getData("text/html");
let text = e.dataTransfer.getData("text/plain");
if (text && text.startsWith("<div")) {
return;
}
e.stopPropagation()
e.preventDefault()
if (html && html.indexOf("<img") != 0) {
console.log("not good");
let htmlDoc = Documents.HtmlDocument(html, { ...options, width: 300, height: 300 });
htmlDoc.SetText(KeyStore.DocumentText, text);
this.props.addDocument(htmlDoc, false);
return;
}
for (let i = 0; i < e.dataTransfer.items.length; i++) {
const upload = window.location.origin + RouteStore.upload;
let item = e.dataTransfer.items[i];
if (item.kind === "string" && item.type.indexOf("uri") != -1) {
e.dataTransfer.items[i].getAsString(action((s: string) => {
let document: Document;
request.head(ServerUtils.prepend(RouteStore.corsProxy + "/" + s), (err, res, body) => {
let type = res.headers["content-type"];
if (type) {
let doc = this.getDocumentFromType(type, s, { ...options, width: 300, nativeWidth: 300 })
if (doc) {
this.props.addDocument(doc, false);
}
}
});
// this.props.addDocument(Documents.WebDocument(s, { ...options, width: 300, height: 300 }), false)
}))
}
let type = item.type
if (item.kind == "file") {
let file = item.getAsFile();
let formData = new FormData()
if (file) {
formData.append('file', file)
}
fetch(upload, {
method: 'POST',
body: formData
}).then((res: Response) => {
return res.json()
}).then(json => {
json.map((file: any) => {
let path = window.location.origin + file
runInAction(() => {
let doc = this.getDocumentFromType(type, path, { ...options, nativeWidth: 300, width: 300 })
let docs = that.props.Document.GetT(KeyStore.Data, ListField);
if (docs != FieldWaiting) {
if (!docs) {
docs = new ListField<Document>();
that.props.Document.Set(KeyStore.Data, docs)
}
if (doc) {
docs.Data.push(doc);
}
}
})
})
})
}
}
}
}
|