-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEditorPanel.jsx
More file actions
182 lines (161 loc) · 5.18 KB
/
Copy pathEditorPanel.jsx
File metadata and controls
182 lines (161 loc) · 5.18 KB
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
/* eslint-disable react-hooks/exhaustive-deps */
import "../../../assets/stylesheets/EditorPanel.scss";
import React, { useRef, useEffect, useContext, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
setCascadeUpdate,
updateProjectComponent,
} from "../../../redux/EditorSlice";
import { useCookies } from "react-cookie";
import { useTranslation } from "react-i18next";
import { basicSetup } from "codemirror";
import { EditorView, keymap } from "@codemirror/view";
import { EditorState } from "@codemirror/state";
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
import { indentationMarkers } from "@replit/codemirror-indentation-markers";
import { indentUnit } from "@codemirror/language";
import "material-symbols";
import { html } from "@codemirror/lang-html";
import { css } from "@codemirror/lang-css";
import { python } from "@codemirror/lang-python";
import { javascript } from "@codemirror/lang-javascript";
import { Alert } from "@raspberrypifoundation/design-system-react";
import { editorLightTheme } from "../../../assets/themes/editorLightTheme";
import { editorDarkTheme } from "../../../assets/themes/editorDarkTheme";
import { SettingsContext } from "../../../utils/settings";
const MAX_CHARACTERS = 8500000;
const EditorPanel = ({ extension = "html", fileName = "index" }) => {
const editor = useRef();
const editorViewRef = useRef();
const project = useSelector((state) => state.editor.project);
const readOnly = useSelector((state) => state.editor.readOnly);
const cascadeUpdate = useSelector((state) => state.editor.cascadeUpdate);
const [cookies] = useCookies(["theme", "fontSize"]);
const dispatch = useDispatch();
const { t } = useTranslation();
const settings = useContext(SettingsContext);
const [characterLimitExceeded, setCharacterLimitExceeded] = useState(false);
const updateStoredProject = (content) => {
dispatch(
updateProjectComponent({
extension: extension,
name: fileName,
content,
cascadeUpdate: false,
}),
);
};
const label = EditorView.contentAttributes.of({
"aria-label": t("editorPanel.ariaLabel"),
});
const onUpdate = EditorView.updateListener.of((viewUpdate) => {
if (viewUpdate.docChanged) {
updateStoredProject(viewUpdate.state.doc.toString());
}
});
const getMode = () => {
switch (extension) {
case "html":
return html();
case "css":
return css();
case "py":
return python();
case "js":
return javascript();
default:
return html();
}
};
const isDarkMode =
cookies.theme === "dark" ||
(!cookies.theme &&
window.matchMedia("(prefers-color-scheme:dark)").matches);
const editorTheme = isDarkMode ? editorDarkTheme : editorLightTheme;
const file = project.components.find(
(item) => item.extension === extension && item.name === fileName,
);
useEffect(() => {
if (!file) {
return;
}
const code = file.content;
const mode = getMode();
let customIndentUnit = " ";
if (extension === "py") {
customIndentUnit = " ";
}
const limitCharacters = EditorState.transactionFilter.of((transaction) => {
const newDoc = transaction.newDoc;
if (newDoc.length > MAX_CHARACTERS) {
setCharacterLimitExceeded(true);
return [];
}
setCharacterLimitExceeded(false);
return transaction;
});
const startState = EditorState.create({
doc: code,
extensions: [
basicSetup,
keymap.of([defaultKeymap, indentWithTab]),
mode,
label,
onUpdate,
editorTheme,
indentationMarkers(),
indentUnit.of(customIndentUnit),
EditorView.editable.of(!readOnly),
limitCharacters,
],
});
const view = new EditorView({
lineWrapping: true,
state: startState,
parent: editor.current,
});
editorViewRef.current = view;
// 'aria-hidden' to fix keyboard access accessibility error
view.scrollDOM.setAttribute("aria-hidden", "true");
// Add alt text to hidden images to fix accessibility error
const hiddenImages =
view.contentDOM.getElementsByClassName("cm-widgetBuffer");
for (let img of hiddenImages) {
img.setAttribute("role", "presentation");
}
return () => {
view.destroy();
};
}, [cookies]);
useEffect(() => {
if (
cascadeUpdate &&
editorViewRef.current &&
file.content !== editorViewRef.current.state.doc.toString()
) {
editorViewRef.current.dispatch({
changes: {
from: 0,
to: editorViewRef.current.state.doc.length,
insert: file.content,
},
});
dispatch(setCascadeUpdate(false));
}
}, [file, cascadeUpdate, editorViewRef]);
return (
<>
<div className={`editor editor--${settings.fontSize}`} ref={editor}></div>
{characterLimitExceeded && (
<Alert
title={t("editorPanel.characterLimitError")}
type="error"
text={t("editorPanel.characterLimitExplanation", {
count: MAX_CHARACTERS,
})}
/>
)}
</>
);
};
export default EditorPanel;