@prozilla-os/text-editor 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sieben De Beule
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ <div align="center">
2
+ <br />
3
+ <p>
4
+ <a href="https://os.prozilla.dev/"><img src="https://os.prozilla.dev/assets/logo.svg" height="200" alt="ProzillaOS" /></a>
5
+ </p>
6
+ <p>
7
+ <a href="https://github.com/prozilla-os/ProzillaOS/blob/main/LICENSE.md"><img alt="License" src="https://img.shields.io/github/license/Prozilla/ProzillaOS?style=flat-square&color=FF4D5B&label=License"></a>
8
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Stars" src="https://img.shields.io/github/stars/Prozilla/ProzillaOS?style=flat-square&color=FED24C&label=%E2%AD%90"></a>
9
+ <a href="https://github.com/prozilla-os/ProzillaOS"><img alt="Forks" src="https://img.shields.io/github/forks/Prozilla/ProzillaOS?style=flat-square&color=4D9CFF&label=Forks&logo=github"></a>
10
+ <a href="https://www.npmjs.com/package/prozilla-os"><img alt="NPM Version" src="https://img.shields.io/npm/v/prozilla-os?logo=npm&style=flat-square&label=prozilla-os&color=FF4D5B"></a>
11
+ </p>
12
+ </div>
13
+
14
+ ## About
15
+
16
+ `@prozilla-os/text-editor` is a text editor application for ProzillaOS.
17
+
18
+ ## Installation
19
+
20
+ `@prozilla-os/core` is required to run this application.
21
+
22
+ ```sh
23
+ $ npm install @prozilla-os/core @prozilla-os/text-editor
24
+ $ yarn add @prozilla-os/core @prozilla-os/text-editor
25
+ $ pnpm add @prozilla-os/core @prozilla-os/text-editor
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```tsx
31
+ import { Desktop, ModalsView, ProzillaOS, Taskbar, WindowsView, AppsConfig } from "@prozilla-os/core";
32
+ import { textEditor } from "@prozilla-os/text-editor";
33
+
34
+ function App() {
35
+ return (
36
+ <ProzillaOS
37
+ systemName="Example"
38
+ tagLine="Powered by ProzillaOS"
39
+ config={{
40
+ apps: new AppsConfig({
41
+ apps: [ textEditor ]
42
+ })
43
+ }}
44
+ >
45
+ <Taskbar/>
46
+ <WindowsView/>
47
+ <ModalsView/>
48
+ <Desktop/>
49
+ </ProzillaOS>
50
+ );
51
+ }
52
+ ```
53
+
54
+ ## Links
55
+
56
+ - [Website/demo][website]
57
+ - [GitHub][github]
58
+ - [npm][npm]
59
+ - [Ko-fi][ko-fi]
60
+
61
+ [website]: https://os.prozilla.dev/text-editor
62
+ [github]: https://github.com/prozilla-os/ProzillaOS/tree/convert-to-monorepo/packages/apps/text-editor
63
+ [npm]: https://www.npmjs.com/package/@prozilla-os/text-editor
64
+ [ko-fi]: https://ko-fi.com/prozilla
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@prozilla-os/text-editor",
3
+ "description": "A text editor application for ProzillaOS.",
4
+ "version": "1.0.1",
5
+ "homepage": "https://os.prozilla.dev/text-editor",
6
+ "author": {
7
+ "name": "Prozilla",
8
+ "email": "business@prozilla.dev",
9
+ "url": "https://prozilla.dev/"
10
+ },
11
+ "main": "src/index.ts",
12
+ "type": "module",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/prozilla-os/ProzillaOS.git",
16
+ "directory": "packages/apps/text-editor"
17
+ },
18
+ "license": "MIT",
19
+ "dependencies": {
20
+ "react": "^18.3.1",
21
+ "@prozilla-os/core": "1.0.11"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.4.5"
25
+ },
26
+ "files": [
27
+ "src/**/*"
28
+ ],
29
+ "publishConfig": {
30
+ "registry": "https://registry.npmjs.org/",
31
+ "access": "public"
32
+ },
33
+ "scripts": {
34
+ "test": "tsc --watch"
35
+ }
36
+ }
@@ -0,0 +1,195 @@
1
+ .TextEditor {
2
+ display: flex;
3
+ flex-direction: column;
4
+ width: 100%;
5
+ height: 100%;
6
+ text-align: start;
7
+ }
8
+
9
+ .TextEditor p,
10
+ .TextEditor div,
11
+ .TextEditor span,
12
+ .TextEditor textarea {
13
+ font-size: 1em;
14
+ }
15
+
16
+ .View, .Code {
17
+ width: 100%;
18
+ height: 100%;
19
+ padding: 1rem !important;
20
+ color: var(--foreground-color-0) !important;
21
+ background-color: var(--background-color-2) !important;
22
+ border: none;
23
+ outline: none;
24
+ font-size: 1rem;
25
+ font-family: var(--body-font-family);
26
+ resize: none;
27
+ overflow: auto;
28
+ }
29
+
30
+ .View {
31
+ z-index: 2;
32
+ }
33
+
34
+ .MarkdownImage {
35
+ max-width: 100%;
36
+ object-fit: contain;
37
+ border-radius: var(--border-radius-0);
38
+ margin: 1rem 0.5rem 1rem 0;
39
+ }
40
+
41
+ .MarkdownBlockquote {
42
+ display: flex;
43
+ gap: 1rem;
44
+ flex-direction: column;
45
+ margin: 0;
46
+ }
47
+
48
+ .AlertContainer {
49
+ --color-0: var(--color-1);
50
+ --color-1: var(--background-color-0);
51
+ --color-2: var(--background-color-1);
52
+
53
+ margin: 0;
54
+ padding: 1rem 1.5rem;
55
+ border-left: 1rem solid var(--color-1);
56
+ background-color: var(--color-2);
57
+ border-radius: var(--border-radius-1);
58
+ }
59
+
60
+ .Alert {
61
+ display: inline-flex;
62
+ gap: 0.5rem;
63
+ justify-content: flex-start;
64
+ align-items: center;
65
+ margin-bottom: 0.5rem;
66
+ color: var(--color-0);
67
+ font-weight: 600;
68
+ }
69
+
70
+ .Alert > svg * {
71
+ fill: var(--color-0);
72
+ }
73
+
74
+ .ImportantAlert {
75
+ --color-0: var(--purple-0);
76
+ --color-1: var(--purple-1);
77
+ --color-2: var(--purple-2);
78
+ }
79
+
80
+ .NoteAlert {
81
+ --color-0: var(--blue-0);
82
+ --color-1: var(--blue-1);
83
+ --color-2: var(--blue-2);
84
+ }
85
+
86
+ .TipAlert {
87
+ --color-0: var(--green-0);
88
+ --color-1: var(--green-1);
89
+ --color-2: var(--green-2);
90
+ }
91
+
92
+ .WarningAlert {
93
+ --color-0: var(--yellow-0);
94
+ --color-1: var(--yellow-1);
95
+ --color-2: var(--yellow-2);
96
+ }
97
+
98
+ .CautionAlert {
99
+ --color-0: var(--red-0);
100
+ --color-1: var(--red-1);
101
+ --color-2: var(--red-2);
102
+ }
103
+
104
+
105
+ .View code {
106
+ position: relative;
107
+ font-family: var(--mono-font-family);
108
+ border-radius: var(--border-radius-1);
109
+ padding: 0.125rem 0.25rem;
110
+ font-size: 0.875rem;
111
+ letter-spacing: -0.01rem;
112
+ }
113
+
114
+ .View code::after {
115
+ content: "";
116
+ position: absolute;
117
+ top: 0;
118
+ left: 0;
119
+ width: 100%;
120
+ height: 100%;
121
+ background-color: var(--background-color-1);
122
+ border-radius: inherit;
123
+ z-index: -1;
124
+ }
125
+
126
+ .MarkdownLink {
127
+ position: relative;
128
+ display: inline-block;
129
+ color: var(--light-blue-0);
130
+ transition: color 200ms ease-in-out;
131
+ }
132
+
133
+ .MarkdownLink:hover, .MarkdownLink:focus-visible {
134
+ color: var(--light-blue-1);
135
+ }
136
+
137
+ .MarkdownLink > * {
138
+ color: inherit;
139
+ }
140
+
141
+ div > .MarkdownLink {
142
+ margin: 1rem 0;
143
+ }
144
+
145
+ .MarkdownLink > .MarkdownImage {
146
+ margin: 0;
147
+ }
148
+
149
+ .View h1,
150
+ .View h2,
151
+ .View h3,
152
+ .View h4,
153
+ .View h5,
154
+ .View h6 {
155
+ margin-top: 1.75rem;
156
+ margin-bottom: -0.25rem;
157
+ }
158
+
159
+ .View p {
160
+ line-height: 1.375rem;
161
+ }
162
+
163
+ .View li > ul {
164
+ margin: 1rem 0;
165
+ }
166
+
167
+ .View table {
168
+ margin: 1rem 0;
169
+ border-collapse: collapse;
170
+ }
171
+
172
+ .View table, .View th, .View td {
173
+ border: 0.2rem solid var(--background-color-0);
174
+ }
175
+
176
+ .View th, .View td {
177
+ padding: 0.5rem;
178
+ }
179
+
180
+ .Code {
181
+ margin: 0;
182
+ cursor: default;
183
+ }
184
+
185
+ .Code *[class*="hljs-string"] { color: var(--green-0); }
186
+ .Code *[class*="hljs-literal"] { color: var(--yellow-0); }
187
+ .Code *[class*="hljs-number"] { color: var(--yellow-0); }
188
+ .Code *[class*="hljs-keyword"] { color: var(--blue-0); }
189
+ .Code *[class*="hljs-title"] { color: var(--cyan-0); }
190
+ .Code *[class*="hljs-name"] { color: var(---red-0); }
191
+ .Code *[class*="hljs-attr"] { color: var(--blue-0); }
192
+ .Code *[class*="hljs-param"] { color: var(---red-0); }
193
+ .Code *[class*="hljs-comment"] { color: var(--black-0); }
194
+ .Code *[class*="linenumber"] { color: var(--black-0); }
195
+ .Code *[class*="hljs-meta"] { color: var(--blue-0); }
@@ -0,0 +1,208 @@
1
+ import { ReactNode, Ref, useEffect, useRef, useState } from "react";
2
+ import styles from "./TextEditor.module.css";
3
+ import Markdown from "markdown-to-jsx";
4
+ import { MarkdownLink } from "./overrides/MarkdownLink";
5
+ import { MarkdownImage } from "./overrides/MarkdownImage";
6
+ import SyntaxHighlighter from "react-syntax-highlighter";
7
+ import { MarkdownBlockquote } from "./overrides/MarkdownBlockquote";
8
+ import { App, ClickAction, CODE_EXTENSIONS, Divider, DropdownAction, FileSelector, FileSelectorMode, HeaderMenu, ModalsManager, useSystemManager, useVirtualRoot, useWindowedModal, useWindowsManager, VirtualFile, WindowProps, WindowsManager } from "@prozilla-os/core";
9
+ import { DEFAULT_ZOOM, EXTENSION_TO_LANGUAGE, ZOOM_FACTOR } from "../constants/textEditor.const";
10
+
11
+ const OVERRIDES = {
12
+ a: MarkdownLink,
13
+ img: MarkdownImage,
14
+ blockquote: MarkdownBlockquote,
15
+ };
16
+
17
+ export interface MarkdownProps {
18
+ modalsManager: ModalsManager;
19
+ setCurrentFile: Function;
20
+ currentFile: VirtualFile;
21
+ app: App;
22
+ windowsManager: WindowsManager;
23
+ children?: ReactNode;
24
+ }
25
+
26
+ interface TextEditorProps extends WindowProps {
27
+ file?: VirtualFile;
28
+ mode?: "view" | "edit";
29
+ path?: string;
30
+ }
31
+
32
+ export function TextEditor({ file, path, setTitle, setIconUrl, close, mode, app, modalsManager }: TextEditorProps) {
33
+ const { windowsConfig, modalsConfig } = useSystemManager();
34
+ const ref = useRef<HTMLDivElement | HTMLTextAreaElement>();
35
+ const windowsManager = useWindowsManager();
36
+ const virtualRoot = useVirtualRoot();
37
+ const [currentFile, setCurrentFile] = useState<VirtualFile | null>(file as VirtualFile);
38
+ const [currentMode, setCurrentMode] = useState<TextEditorProps["mode"]>(mode);
39
+ const [content, setContent] = useState(file?.content ?? "");
40
+ const [unsavedChanges, setUnsavedChanges] = useState(file == null);
41
+ const [zoom, setZoom] = useState(DEFAULT_ZOOM);
42
+ const [initialised, setInitialised] = useState(false);
43
+ const { openWindowedModal } = useWindowedModal();
44
+
45
+ useEffect(() => {
46
+ void (async () => {
47
+ let newContent = "";
48
+
49
+ // Load file
50
+ if (currentFile) {
51
+ if (currentFile.content) {
52
+ newContent = currentFile.content;
53
+ } else if (currentFile.source) {
54
+ await fetch(currentFile.source).then((response) =>
55
+ response.text()
56
+ ).then((response) => {
57
+ newContent = response;
58
+ });
59
+ }
60
+
61
+ const iconUrl = currentFile.getIconUrl();
62
+ if (iconUrl)
63
+ setIconUrl?.(iconUrl);
64
+
65
+ if (newContent?.trim() === "")
66
+ setCurrentMode("edit");
67
+ } else if (app != null && app.iconUrl != null) {
68
+ setIconUrl?.(app.iconUrl);
69
+ }
70
+
71
+ setContent(newContent);
72
+
73
+ if (ref.current) {
74
+ (ref.current as HTMLElement).scrollTo(0, 0);
75
+ }
76
+ })();
77
+ }, [app?.id, currentFile, setIconUrl]);
78
+
79
+ useEffect(() => {
80
+ // Update title
81
+ let label = currentFile?.id ?? "Untitled";
82
+
83
+ if (unsavedChanges)
84
+ label += "*";
85
+
86
+ if (currentMode === "view")
87
+ label += " (preview)";
88
+
89
+ setTitle?.(app != null ? `${label} ${windowsConfig.titleSeparator} ${app.name}` : label);
90
+ }, [currentFile, setTitle, unsavedChanges, currentMode, app?.name]);
91
+
92
+ useEffect(() => {
93
+ if (!initialised && currentFile == null && path != null) {
94
+ const newFile = virtualRoot?.navigate(path);
95
+
96
+ if (newFile == null || !newFile.isFile())
97
+ return;
98
+
99
+ setCurrentFile(newFile as VirtualFile);
100
+ setInitialised(true);
101
+ }
102
+ }, [path, currentFile]);
103
+
104
+ const newText = () => {
105
+ setCurrentFile(null);
106
+ setCurrentMode("edit");
107
+ setUnsavedChanges(true);
108
+ };
109
+
110
+ const saveTextAs = () => {
111
+ onChange({ target: { value: content } });
112
+ };
113
+
114
+ const saveText = () => {
115
+ if (currentFile == null)
116
+ return saveTextAs();
117
+
118
+ currentFile.setContent(content);
119
+ onChange({ target: { value: content } });
120
+ };
121
+
122
+ const onChange = (event: Event | { target: { value: string } }) => {
123
+ const value = (event.target as HTMLInputElement).value;
124
+
125
+ if (currentFile != null) {
126
+ setUnsavedChanges(currentFile.content !== value);
127
+ } else {
128
+ setUnsavedChanges(true);
129
+ }
130
+
131
+ return setContent(value);
132
+ };
133
+
134
+ const overrides: Record<string, object> = {};
135
+ for (const [key, value] of Object.entries(OVERRIDES)) {
136
+ overrides[key] = {
137
+ component: value,
138
+ props: {
139
+ modalsManager,
140
+ setCurrentFile,
141
+ currentFile,
142
+ app,
143
+ windowsManager
144
+ } as MarkdownProps
145
+ };
146
+ }
147
+
148
+ return <div className={styles.TextEditor} style={{ fontSize: zoom }}>
149
+ <HeaderMenu>
150
+ <DropdownAction label="File" showOnHover={false}>
151
+ <ClickAction label="New" onTrigger={() => { newText(); }} shortcut={["Control", "e"]}/>
152
+ <ClickAction label="Open" onTrigger={() => {
153
+ openWindowedModal({
154
+ size: modalsConfig.defaultFileSelectorSize,
155
+ Modal: (props: object) => <FileSelector
156
+ type={FileSelectorMode.Single}
157
+ onFinish={(file) => {
158
+ setCurrentFile(file as VirtualFile);
159
+ setUnsavedChanges(false);
160
+ }}
161
+ {...props}
162
+ />
163
+ });
164
+ }} shortcut={["Control", "o"]}/>
165
+ <Divider/>
166
+ <ClickAction label="Save" onTrigger={() => { saveText(); }} shortcut={["Control", "s"]}/>
167
+ {/* <ClickAction label={`Reveal in ${APP_NAMES.FILE_EXPLORER}`} disabled={currentFile == null} onTrigger={() => {
168
+ if (windowsManager != null) currentFile?.parent?.open(windowsManager);
169
+ }}/> */}
170
+ <Divider/>
171
+ <ClickAction label="Quit" onTrigger={() => { close?.(); }} shortcut={["Control", "q"]}/>
172
+ </DropdownAction>
173
+ <DropdownAction label="View" showOnHover={false}>
174
+ <ClickAction label={currentMode === "view" ? "Edit mode" : "Preview mode"} onTrigger={() => {
175
+ setCurrentMode(currentMode === "view" ? "edit" : "view");
176
+ }} shortcut={["Control", "u"]}/>
177
+ <Divider/>
178
+ <ClickAction label="Zoom in" onTrigger={() => { setZoom(zoom + ZOOM_FACTOR); }} shortcut={["Control", "+"]}/>
179
+ <ClickAction label="Zoom out" onTrigger={() => { setZoom(zoom - ZOOM_FACTOR); }} shortcut={["Control", "-"]}/>
180
+ <ClickAction label="Reset Zoom" disabled={zoom == DEFAULT_ZOOM} onTrigger={() => { setZoom(DEFAULT_ZOOM); }} shortcut={["Control", "0"]}/>
181
+ </DropdownAction>
182
+ </HeaderMenu>
183
+ {currentMode === "view"
184
+ ? currentFile?.extension != null && CODE_EXTENSIONS.includes(currentFile?.extension)
185
+ ? <SyntaxHighlighter
186
+ language={EXTENSION_TO_LANGUAGE[currentFile?.extension] ?? currentFile?.extension}
187
+ className={styles.Code}
188
+ useInlineStyles={false}
189
+ showLineNumbers={true}
190
+ >{content}</SyntaxHighlighter>
191
+ : <div ref={ref as Ref<HTMLDivElement>} className={styles.View}>
192
+ {currentFile?.extension === "md"
193
+ ? <Markdown options={{ overrides } as object}>{content}</Markdown>
194
+ : <pre><p>{content}</p></pre>
195
+ }
196
+ </div>
197
+ : <textarea
198
+ ref={ref as Ref<HTMLTextAreaElement>}
199
+ className={styles.View}
200
+ value={content}
201
+ onChange={onChange}
202
+ spellCheck={false}
203
+ autoComplete="off"
204
+ autoFocus
205
+ />
206
+ }
207
+ </div>;
208
+ }
@@ -0,0 +1,85 @@
1
+ import { MarkdownProps } from "../TextEditor";
2
+ import { Attributes, Children, cloneElement, DetailedHTMLProps, isValidElement, ReactNode } from "react";
3
+ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
4
+ import { faExclamationCircle, faExclamationTriangle, faInfoCircle, faLightbulb } from "@fortawesome/free-solid-svg-icons";
5
+ import styles from "../TextEditor.module.css";
6
+ import { sanitizeProps } from "../../core/_utils/sanitizeProps";
7
+
8
+ type Props = Partial<unknown> & Attributes & { children: ReactNode, className?: string };
9
+
10
+ export function MarkdownBlockquote({ children, ...props }: MarkdownProps) {
11
+ sanitizeProps(props);
12
+
13
+ const formatContent = (children: ReactNode): [ReactNode, string | null] => {
14
+ if (!children) return [null, null];
15
+
16
+ let alertType: string | null = null;
17
+
18
+ children = Children.map(children, (child) => {
19
+ if (isValidElement(child)) {
20
+ const { children, ...props } = child.props as Props;
21
+
22
+ const [newChildren, childAlertType] = formatContent(children);
23
+
24
+ if (childAlertType != null)
25
+ props.className = `${styles.AlertContainer} ${styles[childAlertType + "Alert"]}`;
26
+
27
+ return cloneElement(child, {
28
+ ...props,
29
+ children: newChildren,
30
+ } as Props);
31
+ } else if (typeof child !== "string") {
32
+ return child;
33
+ }
34
+
35
+ switch (child) {
36
+ case "[!IMPORTANT]":
37
+ child = <span className={styles.Alert}>
38
+ <FontAwesomeIcon icon={faExclamationCircle}/>
39
+ Important
40
+ </span>;
41
+ alertType = "Important";
42
+ break;
43
+ case "[!NOTE]":
44
+ child = <span className={styles.Alert}>
45
+ <FontAwesomeIcon icon={faInfoCircle}/>
46
+ Note
47
+ </span>;
48
+ alertType = "Note";
49
+ break;
50
+ case "[!TIP]":
51
+ child = <span className={styles.Alert}>
52
+ <FontAwesomeIcon icon={faLightbulb}/>
53
+ Tip
54
+ </span>;
55
+ alertType = "Tip";
56
+ break;
57
+ case "[!WARNING]":
58
+ child = <span className={styles.Alert}>
59
+ <FontAwesomeIcon icon={faExclamationTriangle}/>
60
+ Warning
61
+ </span>;
62
+ alertType = "Warning";
63
+ break;
64
+ case "[!CAUTION]":
65
+ child = <span className={styles.Alert}>
66
+ <FontAwesomeIcon icon={faExclamationTriangle}/>
67
+ Caution
68
+ </span>;
69
+ alertType = "Caution";
70
+ break;
71
+ }
72
+
73
+ return child;
74
+ });
75
+
76
+ return [children, alertType];
77
+ };
78
+
79
+ return <blockquote
80
+ {...(props as DetailedHTMLProps<React.BlockquoteHTMLAttributes<HTMLQuoteElement>, HTMLQuoteElement>)}
81
+ className={styles.MarkdownBlockquote}
82
+ >
83
+ {formatContent(children)[0]}
84
+ </blockquote>;
85
+ }
@@ -0,0 +1,28 @@
1
+ import { useMemo } from "react";
2
+ import { MarkdownProps } from "../TextEditor";
3
+ import styles from "../TextEditor.module.css";
4
+ import { sanitizeProps } from "../../core/_utils/sanitizeProps";
5
+
6
+ interface MarkdownImageProps extends MarkdownProps {
7
+ src: string;
8
+ alt: string
9
+ }
10
+
11
+ export function MarkdownImage({ alt, src, ...props }: MarkdownImageProps) {
12
+ const source = useMemo(() => {
13
+ if (src.startsWith("public")) {
14
+ return src.replace(/^public\//g, "/");
15
+ }
16
+
17
+ return src;
18
+ }, [src]);
19
+
20
+ sanitizeProps(props);
21
+
22
+ return <img
23
+ {...props}
24
+ src={source}
25
+ className={styles.MarkdownImage}
26
+ alt={alt}
27
+ />;
28
+ }
@@ -0,0 +1,80 @@
1
+ import { MouseEventHandler, ReactNode, useMemo } from "react";
2
+ import { faClipboard, faExternalLink } from "@fortawesome/free-solid-svg-icons";
3
+ import { MarkdownProps } from "../TextEditor";
4
+ import styles from "../TextEditor.module.css";
5
+ import { Actions, ClickAction, copyToClipboard, DIALOG_CONTENT_TYPES, DialogBox, ModalProps, removeUrlProtocol, TextDisplay, useContextMenu, useWindowedModal, Vector2 } from "@prozilla-os/core";
6
+ import { sanitizeProps } from "../../core/_utils/sanitizeProps";
7
+
8
+ interface MarkdownLinkProps extends MarkdownProps {
9
+ href: string;
10
+ children: ReactNode;
11
+ }
12
+
13
+ export function MarkdownLink({ href, children, windowsManager, currentFile, setCurrentFile, app, ...props }: MarkdownLinkProps) {
14
+ const { openWindowedModal } = useWindowedModal();
15
+
16
+ const onClick = (event: MouseEvent) => {
17
+ event.preventDefault();
18
+
19
+ if (!href.startsWith("http://") && !href.startsWith("https://")) {
20
+ const target = currentFile.parent?.navigate(href);
21
+ if (target != null) {
22
+ if (target.isFile()) {
23
+ setCurrentFile(target);
24
+ } else {
25
+ windowsManager.open("file-explorer", { path: target.path });
26
+ }
27
+ } else {
28
+ openWindowedModal({
29
+ iconUrl: app?.iconUrl as string | undefined,
30
+ title: "Failed to open link",
31
+ size: new Vector2(450, 150),
32
+ Modal: (props: ModalProps) =>
33
+ <DialogBox {...props}>
34
+ <p>Target not found: "{href}"</p>
35
+ <button data-type={DIALOG_CONTENT_TYPES.closeButton}>Ok</button>
36
+ </DialogBox>
37
+ });
38
+ }
39
+ } else {
40
+ window.open(href, "_blank")?.focus();
41
+ }
42
+ };
43
+
44
+ const { onContextMenu } = useContextMenu({ Actions: (props) =>
45
+ <Actions {...props}>
46
+ <TextDisplay>{removeUrlProtocol(href)}</TextDisplay>
47
+ <ClickAction label="Open link" icon={faExternalLink} onTrigger={(event) => {
48
+ onClick(event as MouseEvent);
49
+ }}/>
50
+ <ClickAction label="Copy link" icon={faClipboard} onTrigger={() => {
51
+ copyToClipboard(href);
52
+ }}/>
53
+ </Actions>
54
+ });
55
+
56
+ const title = useMemo<string>(() => {
57
+ let title: string = "";
58
+ try {
59
+ title = new URL(href).hostname;
60
+ } catch (error) {
61
+ title = href.split("/").pop() ?? "";
62
+ }
63
+ return title;
64
+ }, [href]);
65
+
66
+ sanitizeProps(props);
67
+
68
+ return <a
69
+ {...props}
70
+ className={styles.MarkdownLink}
71
+ target="_blank"
72
+ rel="noreferrer"
73
+ href={href}
74
+ onContextMenu={onContextMenu as unknown as MouseEventHandler}
75
+ onClick={onClick as unknown as MouseEventHandler}
76
+ title={title}
77
+ >
78
+ {children}
79
+ </a>;
80
+ }
@@ -0,0 +1,10 @@
1
+ export const DEFAULT_ZOOM = 16;
2
+ export const ZOOM_FACTOR = 4;
3
+
4
+ export const EXTENSION_TO_LANGUAGE: Record<string, string> = {
5
+ "js": "javascript",
6
+ "jsx": "javascript",
7
+ "ts": "typescript",
8
+ "tsx": "typescript",
9
+ "yml": "yaml",
10
+ };
@@ -0,0 +1,7 @@
1
+ export function sanitizeProps(props: Record<string, unknown>) {
2
+ delete props.modalsManager;
3
+ delete props.setCurrentFile;
4
+ delete props.currentFile;
5
+ delete props.app;
6
+ delete props.windowsManager;
7
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { App } from "@prozilla-os/core";
2
+ import { TextEditor } from "./components/TextEditor";
3
+
4
+ const textEditor = new App("Text Editor", "text-editor", TextEditor)
5
+ .setIconUrl("https://os.prozilla.dev/assets/apps/icons/text-editor.svg");
6
+
7
+ export { textEditor };