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