Skip to content

Commit

Permalink
Fixed eslint of JSEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxi-std committed Jan 24, 2025
1 parent 333d6ac commit d947408
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/components/JSEditor.module.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.filterEditor {
.jsEditorContainer {
border: 1px solid var(--theme-border);
border-radius: 2px;
height: 100%;
Expand Down
44 changes: 33 additions & 11 deletions src/components/JSEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memo, useEffect, useState } from 'react';
/* eslint-disable react/prop-types */
import { memo, useEffect, useState, useRef, useCallback } from 'react';
import MonacoEditor, { Monaco } from '@monaco-editor/react';
import styles from './JSEditor.module.less';

Expand All @@ -13,17 +14,23 @@ const JSEditor: React.FC<JSEditorProps> = ({
storageKey,
defaultValue = '',
jsExtraLib = '',
onChange = () => {},
onChange,
}) => {
const [theme, setTheme] = useState(
window.matchMedia('(prefers-color-scheme: dark)').matches
? 'vs-dark'
: 'light'
);

const saveTimeout = useRef<NodeJS.Timeout | null>(null);
const handleChange = useCallback(
(value: string | undefined) => onChange?.(value),
[onChange]
);

useEffect(() => {
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const handleChange = (event) => {
const handleChange = (event: MediaQueryListEvent) => {
setTheme(event.matches ? 'vs-dark' : 'light');
};

Expand All @@ -34,26 +41,41 @@ const JSEditor: React.FC<JSEditorProps> = ({
}, []);

const handleEditorBeforeMount = (monaco: Monaco) => {
monaco.languages.typescript.javascriptDefaults.addExtraLib(jsExtraLib);
if (jsExtraLib.trim()) {
monaco.languages.typescript.javascriptDefaults.addExtraLib(jsExtraLib);
}
};

let saveTimeout: NodeJS.Timeout;
const handleEditorChange = (value: string | undefined) => {
onChange(value);
clearTimeout(saveTimeout);
saveTimeout = setTimeout(() => {
handleChange(value);
if (saveTimeout.current) {
clearTimeout(saveTimeout.current);
}
saveTimeout.current = setTimeout(() => {
if (value != null) {
localStorage.setItem(storageKey, value);
try {
localStorage.setItem(storageKey, value);
} catch {
console.error('Failed to save to localStorage');
}
}
}, 300);
};

const getStoredValue = (key: string, fallback: string) => {
try {
return localStorage.getItem(key) || fallback;
} catch {
return fallback;
}
};

return (
<div className={styles.filterEditor}>
<div className={styles.jsEditorContainer}>
<MonacoEditor
height="200px"
language="javascript"
defaultValue={localStorage.getItem(storageKey) || defaultValue}
defaultValue={getStoredValue(storageKey, defaultValue)}
onChange={handleEditorChange}
beforeMount={handleEditorBeforeMount}
theme={theme}
Expand Down

0 comments on commit d947408

Please sign in to comment.