Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/data-editor/generic.tsx
1691 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// "Editor" (really a read-only simple viewer) for generic data files
7
//
8
// See https://github.com/sagemathinc/cocalc/issues/2462
9
10
import { React, Rendered, useActions } from "@cocalc/frontend/app-framework";
11
import { register_file_editor } from "@cocalc/frontend/project-file";
12
import { Markdown } from "@cocalc/frontend/components";
13
import { webapp_client } from "@cocalc/frontend/webapp-client";
14
import { keys, filename_extension } from "@cocalc/util/misc";
15
import { COLORS } from "@cocalc/util/theme";
16
import { Button, Well } from "@cocalc/frontend/antd-bootstrap";
17
18
const hdf_file =
19
"Hierarchical Data Format (HDF file) -- you can open this file using a Python or R library.";
20
const excel =
21
'Microsoft Excel file -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Calc" application.';
22
const microsoft_word =
23
'Microsoft Word file -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Writer" application.';
24
const microsoft_ppt =
25
'Microsoft PowerPoint -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Impress" application.';
26
const windows_executable =
27
"Windows Executable -- you must download this program and run it on a computer";
28
const python_pickle =
29
"Python Pickle -- use Python's [pickle module](https://docs.python.org/3/library/pickle.html) to read this file.s";
30
const medical_imaging =
31
"This is a medical image file. You cannot open it directly in CoCalc, but you might be able to use it from a Python library.";
32
33
// ext: markdown string.
34
const INFO = {
35
p: python_pickle,
36
pkl: python_pickle,
37
pickle: python_pickle,
38
exe: windows_executable,
39
h4: hdf_file,
40
h5: hdf_file,
41
xlsx: excel,
42
xls: excel,
43
doc: microsoft_word,
44
docx: microsoft_word,
45
ppt: microsoft_ppt,
46
pptx: microsoft_ppt,
47
blend: "This is a [Blender](https://www.blender.org/) file. CoCalc can only [open it via X11 Desktop](https://github.com/sagemathinc/cocalc/issues/5107).",
48
kmz:
49
"Editing [KMZ files](https://developers.google.com/kml/documentation/kmzarchives) is not supported. You could `unzip` them in a [Terminal](https://doc.cocalc.com/terminal.html).",
50
jar:
51
"Run JAVA jar archives in a [Terminal](https://doc.cocalc.com/terminal.html) via `java -jar <filename.jar>`",
52
raw:
53
"You may be able to use this file via a Python library or use it in some other way.",
54
tiff:
55
'You may be able to use this file via a Python image manipulation library or via a tool like "Gimp" in an ["X11" file](https://doc.cocalc.com/x11.html).',
56
fit:
57
"You may be able to use this file from Python using the [fitparse](https://github.com/dtcooper/python-fitparse) library.",
58
odt:
59
'OpenDocument Text -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Writer" application.',
60
ods:
61
'OpenDocument Spreadsheet -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Calc" application.',
62
odp:
63
'OpenDocument Presentation -- Create an ["X11" file](https://doc.cocalc.com/x11.html) and open the "Impress" application.',
64
sobj:
65
'You can load an sobj file into **SageMath** by typing `load("filename.sobj")`.',
66
"noext-octave-workspace": `\
67
This is a data file that contains the state of your Octave workspace.
68
Read more: [Saving-Data-on-Unexpected-Exits](https://www.gnu.org/software/octave/doc/v4.2.1/Saving-Data-on-Unexpected-Exits.html).\
69
`,
70
"noext-a.out":
71
"This is a binary executable, which you can run in a Terminal by typing ./a.out.",
72
dcm: medical_imaging,
73
fif: medical_imaging,
74
nii: medical_imaging,
75
} as const;
76
77
interface Props {
78
project_id: string;
79
path: string;
80
}
81
82
const DataGeneric: React.FC<Props> = React.memo((props: Props) => {
83
const { project_id, path } = props;
84
const ext = filename_extension(path);
85
const src = webapp_client.project_client.read_file({ project_id, path });
86
const project_actions = useActions({ project_id });
87
88
function render_hint(): Rendered {
89
const hint = INFO[ext];
90
if (hint) {
91
return <Markdown value={`**Hint**: ${hint}`} />;
92
}
93
return (
94
<span style={{ color: COLORS.GRAY }}>
95
You may be able to use this file from another program, for example, as a
96
data file that is manipulated using a Jupyter notebook.
97
</span>
98
);
99
}
100
101
function render_docx() {
102
if (ext !== "docx") return;
103
return (
104
<>
105
<br />
106
<div>
107
It is possible to{" "}
108
<Button onClick={() => project_actions?.open_word_document(path)}>
109
convert this file to markdown
110
</Button>{" "}
111
.
112
</div>
113
</>
114
);
115
}
116
117
return (
118
<Well style={{ margin: "15px", fontSize: "12pt" }}>
119
<h2>Data File</h2>
120
CoCalc does not have a special viewer or editor for{" "}
121
<a href={src} target="_blank">
122
{path}
123
</a>
124
.{render_docx()}
125
<br />
126
<br />
127
{render_hint()}
128
</Well>
129
);
130
});
131
132
register_file_editor({
133
ext: keys(INFO),
134
icon: "question-circle",
135
component: DataGeneric,
136
});
137
138