Compare commits
1 Commits
feat/wrap
...
feat/lang-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddc2030a42 |
39
.gitignore
vendored
39
.gitignore
vendored
@@ -1,24 +1,25 @@
|
|||||||
# Logs
|
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||||
logs
|
|
||||||
*.log
|
# dependencies
|
||||||
|
/node_modules
|
||||||
|
/.pnp
|
||||||
|
.pnp.js
|
||||||
|
|
||||||
|
# testing
|
||||||
|
/coverage
|
||||||
|
|
||||||
|
# production
|
||||||
|
/build
|
||||||
|
|
||||||
|
# misc
|
||||||
|
.DS_Store
|
||||||
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
yarn-debug.log*
|
yarn-debug.log*
|
||||||
yarn-error.log*
|
yarn-error.log*
|
||||||
pnpm-debug.log*
|
|
||||||
lerna-debug.log*
|
|
||||||
|
|
||||||
node_modules
|
|
||||||
dist
|
|
||||||
dist-ssr
|
|
||||||
*.local
|
|
||||||
|
|
||||||
# Editor directories and files
|
|
||||||
.vscode/*
|
|
||||||
!.vscode/extensions.json
|
|
||||||
.idea
|
.idea
|
||||||
.DS_Store
|
|
||||||
*.suo
|
|
||||||
*.ntvs*
|
|
||||||
*.njsproj
|
|
||||||
*.sln
|
|
||||||
*.sw?
|
|
||||||
|
|||||||
@@ -5,6 +5,5 @@
|
|||||||
"singleQuote": true,
|
"singleQuote": true,
|
||||||
"trailingComma": "es5",
|
"trailingComma": "es5",
|
||||||
"arrowParens": "avoid",
|
"arrowParens": "avoid",
|
||||||
"quoteProps": "consistent",
|
"quoteProps": "consistent"
|
||||||
"plugins": ["prettier-plugin-organize-imports"]
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
# Build stage
|
# Build stage
|
||||||
FROM node:lts AS build
|
FROM node:lts as build
|
||||||
|
|
||||||
ARG BYTEBIN_URL="data/"
|
ARG BYTEBIN_URL="data/"
|
||||||
ENV VITE_BYTEBIN_URL="${BYTEBIN_URL}"
|
ENV REACT_APP_BYTEBIN_URL="${BYTEBIN_URL}"
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package.json yarn.lock ./
|
COPY package.json yarn.lock ./
|
||||||
@@ -13,6 +13,6 @@ RUN yarn build
|
|||||||
# Run stage
|
# Run stage
|
||||||
FROM nginx:alpine
|
FROM nginx:alpine
|
||||||
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
|
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
COPY --from=build /app/dist /usr/share/nginx/html
|
COPY --from=build /app/build /usr/share/nginx/html
|
||||||
CMD ["nginx", "-g", "daemon off;"]
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
EXPOSE 80/tcp
|
EXPOSE 80/tcp
|
||||||
21
README.md
21
README.md
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
**paste is a simple web app for writing & sharing code.** It's my own take on conventional pastebin sites like _pastebin.com_ or _hastebin_.
|
**paste is a simple web app for writing & sharing code.** It's my own take on conventional pastebin sites like _pastebin.com_ or _hastebin_.
|
||||||
|
|
||||||
Anyone can use paste! The official/public instance can be accessed using the endpoints listed below, but you can also [host your own](#self-hosting) if you like!
|
Anyone can use paste! The official/public instance can be accessed using the endpoints listed below, but you can also [host your own](#host-your-own) if you like!
|
||||||
|
|
||||||
##### 1) In a Web Browser
|
##### 1) In a Web Browser
|
||||||
Just go to https://pastes.dev!
|
Just go to https://pastes.dev!
|
||||||
@@ -90,13 +90,28 @@ The API is powered by the [bytebin](https://github.com/lucko/bytebin) service, s
|
|||||||
|
|
||||||
___
|
___
|
||||||
|
|
||||||
### Self-hosting
|
### Host your own
|
||||||
|
|
||||||
The easiest way to self-host is using Docker (& Docker Compose). You can run the following commands to get started:
|
It's quite simple to host your own version.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/lucko/paste
|
git clone https://github.com/lucko/paste
|
||||||
cd paste
|
cd paste
|
||||||
|
yarn install
|
||||||
|
|
||||||
|
# Outputs html/css/js files to /build
|
||||||
|
yarn build
|
||||||
|
|
||||||
|
# Start a webserver for testing/development
|
||||||
|
yarn start
|
||||||
|
```
|
||||||
|
|
||||||
|
You can then follow the [create-react-app deployment documentation](https://create-react-app.dev/docs/deployment/) for how to host the build output. I personally recommend deploying to the cloud using a service like Netlify instead of hosting on your own webserver.
|
||||||
|
|
||||||
|
If you really want to self-host (including the bytebin data storage part), I suggest using Docker:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/lucko/paste
|
||||||
docker compose up -d
|
docker compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
services:
|
version: '3.8'
|
||||||
|
|
||||||
# frontend service
|
services:
|
||||||
paste:
|
paste:
|
||||||
image: ghcr.io/lucko/paste
|
image: ghcr.io/lucko/paste
|
||||||
|
|
||||||
# backend service
|
|
||||||
bytebin:
|
bytebin:
|
||||||
image: ghcr.io/lucko/bytebin
|
image: ghcr.io/lucko/bytebin
|
||||||
volumes:
|
volumes:
|
||||||
@@ -12,7 +11,6 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
BYTEBIN_MISC_KEYLENGTH: 5
|
BYTEBIN_MISC_KEYLENGTH: 5
|
||||||
|
|
||||||
# reverse proxy
|
|
||||||
nginx:
|
nginx:
|
||||||
image: nginx:alpine
|
image: nginx:alpine
|
||||||
command: ['nginx', '-g', 'daemon off;']
|
command: ['nginx', '-g', 'daemon off;']
|
||||||
|
|||||||
@@ -1,29 +1,29 @@
|
|||||||
# nginx reverse proxy configuration for paste frontend & bytebin backend
|
# nginx reverse proxy configuration for paste+bytebin
|
||||||
server {
|
server {
|
||||||
listen 80 default_server;
|
listen 80 default_server;
|
||||||
|
|
||||||
# proxy / path to paste frontend
|
# paste app
|
||||||
location / {
|
location / {
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
proxy_set_header X-Forwarded-Host $server_name;
|
proxy_set_header X-Forwarded-Host $server_name;
|
||||||
|
|
||||||
proxy_pass http://paste;
|
proxy_pass http://paste;
|
||||||
}
|
}
|
||||||
|
|
||||||
# disable bytebin frontend
|
# disable bytebin frontend
|
||||||
location = /data/ {
|
location = /data/ {
|
||||||
return 404;
|
return 404;
|
||||||
}
|
}
|
||||||
|
|
||||||
# proxy /data path to bytebin backend
|
# proxy /data endpoint to bytebin
|
||||||
location /data/ {
|
location /data/ {
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
proxy_set_header X-Forwarded-Host $server_name;
|
proxy_set_header X-Forwarded-Host $server_name;
|
||||||
|
|
||||||
proxy_pass http://bytebin:8080/;
|
proxy_pass http://bytebin:8080/;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
import js from '@eslint/js'
|
|
||||||
import globals from 'globals'
|
|
||||||
import reactHooks from 'eslint-plugin-react-hooks'
|
|
||||||
import reactRefresh from 'eslint-plugin-react-refresh'
|
|
||||||
import tseslint from 'typescript-eslint'
|
|
||||||
|
|
||||||
export default tseslint.config(
|
|
||||||
{ ignores: ['dist'] },
|
|
||||||
{
|
|
||||||
extends: [js.configs.recommended, ...tseslint.configs.recommended],
|
|
||||||
files: ['**/*.{ts,tsx}'],
|
|
||||||
languageOptions: {
|
|
||||||
ecmaVersion: 2020,
|
|
||||||
globals: globals.browser,
|
|
||||||
},
|
|
||||||
plugins: {
|
|
||||||
'react-hooks': reactHooks,
|
|
||||||
'react-refresh': reactRefresh,
|
|
||||||
},
|
|
||||||
rules: {
|
|
||||||
...reactHooks.configs.recommended.rules,
|
|
||||||
'react-refresh/only-export-components': [
|
|
||||||
'warn',
|
|
||||||
{ allowConstantExport: true },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
68
package.json
68
package.json
@@ -2,43 +2,59 @@
|
|||||||
"name": "paste",
|
"name": "paste",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
|
||||||
"dev": "vite",
|
|
||||||
"build": "tsc -b && vite build",
|
|
||||||
"lint": "eslint .",
|
|
||||||
"preview": "vite preview",
|
|
||||||
"format": "prettier --write '**/*.ts' '**/*.tsx' '**/*.css'"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^18.3.1",
|
|
||||||
"react-dom": "^18.3.1",
|
|
||||||
"@monaco-editor/react": "^4.6.0",
|
"@monaco-editor/react": "^4.6.0",
|
||||||
"@catppuccin/palette": "^1.7.1",
|
|
||||||
"copy-to-clipboard": "^3.3.3",
|
"copy-to-clipboard": "^3.3.3",
|
||||||
"history": "^5.3.0",
|
"history": "^5.3.0",
|
||||||
"monaco-editor": "^0.52.0",
|
"local-storage": "^2.0.0",
|
||||||
"monaco-themes": "^0.4.4",
|
"monaco-themes": "^0.4.4",
|
||||||
"pako": "^2.1.0",
|
"pako": "^2.1.0",
|
||||||
|
"react": "^18.2.0",
|
||||||
"react-device-detect": "^2.2.3",
|
"react-device-detect": "^2.2.3",
|
||||||
"styled-components": "^6.1.13",
|
"react-dom": "^18.2.0",
|
||||||
|
"react-scripts": "^5.0.1",
|
||||||
|
"styled-components": "^6.1.1",
|
||||||
|
"typescript": "^4.4.2",
|
||||||
"whatwg-mimetype": "^3.0.0"
|
"whatwg-mimetype": "^3.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^9.17.0",
|
"@testing-library/jest-dom": "^5.14.1",
|
||||||
"@types/react": "^18.3.18",
|
"@testing-library/react": "^13.0.0",
|
||||||
"@types/react-dom": "^18.3.5",
|
"@testing-library/user-event": "^13.2.1",
|
||||||
|
"@types/jest": "^27.0.1",
|
||||||
|
"@types/node": "^18.11.9",
|
||||||
"@types/pako": "^2.0.3",
|
"@types/pako": "^2.0.3",
|
||||||
|
"@types/react": "^18.2.0",
|
||||||
|
"@types/react-dom": "^18.2.0",
|
||||||
"@types/whatwg-mimetype": "^3.0.2",
|
"@types/whatwg-mimetype": "^3.0.2",
|
||||||
"@vitejs/plugin-react-swc": "^3.5.0",
|
"monaco-editor": "^0.44.0",
|
||||||
"eslint": "^9.17.0",
|
"prettier": "^3.1.0",
|
||||||
"eslint-plugin-react-hooks": "^5.0.0",
|
"prettier-plugin-organize-imports": "^3.2.4"
|
||||||
"eslint-plugin-react-refresh": "^0.4.16",
|
},
|
||||||
"globals": "^15.14.0",
|
"scripts": {
|
||||||
"typescript": "~5.6.2",
|
"start": "react-scripts start",
|
||||||
"typescript-eslint": "^8.18.2",
|
"build": "react-scripts build",
|
||||||
"vite": "^6.0.5",
|
"test": "react-scripts test",
|
||||||
"prettier": "^3.4.2",
|
"eject": "react-scripts eject",
|
||||||
"prettier-plugin-organize-imports": "^4.1.0"
|
"format": "prettier --write '**/*.ts' '**/*.tsx' '**/*.css'",
|
||||||
|
"compile": "tsc"
|
||||||
|
},
|
||||||
|
"eslintConfig": {
|
||||||
|
"extends": [
|
||||||
|
"react-app",
|
||||||
|
"react-app/jest"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"browserslist": {
|
||||||
|
"production": [
|
||||||
|
">0.2%",
|
||||||
|
"not dead",
|
||||||
|
"not op_mini all"
|
||||||
|
],
|
||||||
|
"development": [
|
||||||
|
"last 1 chrome version",
|
||||||
|
"last 1 firefox version",
|
||||||
|
"last 1 safari version"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,21 +11,20 @@
|
|||||||
<meta name="twitter:card" content="summary" />
|
<meta name="twitter:card" content="summary" />
|
||||||
<meta name="twitter:title" content="pastes" />
|
<meta name="twitter:title" content="pastes" />
|
||||||
<meta name="twitter:description" content="a simple pastebin." />
|
<meta name="twitter:description" content="a simple pastebin." />
|
||||||
<meta name="twitter:image" content="https://pastes.dev/assets/logo256.png" />
|
<meta name="twitter:image" content="%PUBLIC_URL%/assets/logo256.png" />
|
||||||
|
|
||||||
<meta property="og:title" content="pastes" />
|
<meta property="og:title" content="pastes" />
|
||||||
<meta property="og:description" content="a simple pastebin." />
|
<meta property="og:description" content="a simple pastebin." />
|
||||||
<meta property="og:type" content="product" />
|
<meta property="og:type" content="product" />
|
||||||
<meta property="og:image" content="https://pastes.dev/assets/logo256.png" />
|
<meta property="og:image" content="%PUBLIC_URL%/assets/logo256.png" />
|
||||||
<meta property="og:url" content="https://pastes.dev" />
|
<meta property="og:url" content="%PUBLIC_URL%" />
|
||||||
|
|
||||||
<link rel="icon" href="/favicon.ico" />
|
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||||
<link rel="shortcut icon" href="/assets/logo512.png" sizes="512x512" type="image/png">
|
<link href="%PUBLIC_URL%/assets/logo512.png" rel="shortcut icon" sizes="512x512" type="image/png">
|
||||||
<link rel="apple-touch-icon" href="/assets/logo256.png" />
|
<link rel="apple-touch-icon" href="%PUBLIC_URL%/assets/logo256.png" />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
<script type="module" src="/src/main.tsx"></script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
12
src/App.tsx
12
src/App.tsx
@@ -1,6 +1,8 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import Editor from './components/Editor';
|
import Editor from './components/Editor';
|
||||||
import { loadFromBytebin } from './util/storage';
|
import { loadFromBytebin } from './util/storage';
|
||||||
|
import { Language } from './util/language';
|
||||||
|
import { detectLanguage } from './util/detect-language';
|
||||||
|
|
||||||
const INITIAL = Symbol();
|
const INITIAL = Symbol();
|
||||||
const LOADING = Symbol();
|
const LOADING = Symbol();
|
||||||
@@ -13,7 +15,7 @@ export default function App() {
|
|||||||
const [state, setState] = useState<LoadingState>(INITIAL);
|
const [state, setState] = useState<LoadingState>(INITIAL);
|
||||||
const [forcedContent, setForcedContent] = useState<string>('');
|
const [forcedContent, setForcedContent] = useState<string>('');
|
||||||
const [actualContent, setActualContent] = useState<string>('');
|
const [actualContent, setActualContent] = useState<string>('');
|
||||||
const [contentType, setContentType] = useState<string>();
|
const [contentType, setContentType] = useState<Language>();
|
||||||
|
|
||||||
function setContent(content: string) {
|
function setContent(content: string) {
|
||||||
setActualContent(content);
|
setActualContent(content);
|
||||||
@@ -28,8 +30,14 @@ export default function App() {
|
|||||||
loadFromBytebin(pasteId).then(({ ok, content, type }) => {
|
loadFromBytebin(pasteId).then(({ ok, content, type }) => {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
setContent(content);
|
setContent(content);
|
||||||
if (type) {
|
if (type !== 'plain') {
|
||||||
setContentType(type);
|
setContentType(type);
|
||||||
|
} else {
|
||||||
|
detectLanguage(pasteId).then(detectedLanguage => {
|
||||||
|
if (detectedLanguage) {
|
||||||
|
setContentType(detectedLanguage);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setContent(get404Message(pasteId));
|
setContent(get404Message(pasteId));
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import themes, { Themes } from '../style/themes';
|
|||||||
import EditorControls from './EditorControls';
|
import EditorControls from './EditorControls';
|
||||||
import EditorGlobalStyle from './EditorGlobalStyle';
|
import EditorGlobalStyle from './EditorGlobalStyle';
|
||||||
import EditorTextArea from './EditorTextArea';
|
import EditorTextArea from './EditorTextArea';
|
||||||
|
import { Language } from '../util/language';
|
||||||
|
|
||||||
export interface EditorProps {
|
export interface EditorProps {
|
||||||
forcedContent: string;
|
forcedContent: string;
|
||||||
actualContent: string;
|
actualContent: string;
|
||||||
setActualContent: (value: string) => void;
|
setActualContent: (value: string) => void;
|
||||||
contentType?: string;
|
contentType?: Language;
|
||||||
pasteId?: string;
|
pasteId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ export default function Editor({
|
|||||||
}: EditorProps) {
|
}: EditorProps) {
|
||||||
const [language, setLanguage] = useState<string>('plain');
|
const [language, setLanguage] = useState<string>('plain');
|
||||||
const [readOnly, setReadOnly] = useState<boolean>(isMobile && !!pasteId);
|
const [readOnly, setReadOnly] = useState<boolean>(isMobile && !!pasteId);
|
||||||
const resetFunction = useRef<ResetFunction>(null);
|
const resetFunction = useRef<ResetFunction>();
|
||||||
|
|
||||||
const [theme, setTheme] = usePreference<keyof Themes>(
|
const [theme, setTheme] = usePreference<keyof Themes>(
|
||||||
'theme',
|
'theme',
|
||||||
@@ -40,8 +41,6 @@ export default function Editor({
|
|||||||
pref => pref >= 10 && pref <= 22
|
pref => pref >= 10 && pref <= 22
|
||||||
);
|
);
|
||||||
|
|
||||||
const [wordWrap, setWordWrap] = usePreference<boolean>('wordwrap-enabled', true, (value) => true)
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (contentType) {
|
if (contentType) {
|
||||||
setLanguage(contentType);
|
setLanguage(contentType);
|
||||||
@@ -68,8 +67,6 @@ export default function Editor({
|
|||||||
setReadOnly={setReadOnly}
|
setReadOnly={setReadOnly}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
setTheme={setTheme}
|
setTheme={setTheme}
|
||||||
wordWrap={wordWrap}
|
|
||||||
setWordWrap={setWordWrap}
|
|
||||||
zoom={zoom}
|
zoom={zoom}
|
||||||
/>
|
/>
|
||||||
<EditorTextArea
|
<EditorTextArea
|
||||||
@@ -80,7 +77,6 @@ export default function Editor({
|
|||||||
language={language}
|
language={language}
|
||||||
fontSize={fontSize}
|
fontSize={fontSize}
|
||||||
readOnly={readOnly}
|
readOnly={readOnly}
|
||||||
wordWrap={wordWrap}
|
|
||||||
resetFunction={resetFunction}
|
resetFunction={resetFunction}
|
||||||
/>
|
/>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import copy from 'copy-to-clipboard';
|
import copy from 'copy-to-clipboard';
|
||||||
import history from 'history/browser';
|
import history from 'history/browser';
|
||||||
import { RefObject, useCallback, useEffect, useState } from 'react';
|
import { MutableRefObject, useCallback, useEffect, useState } from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import themes, { Themes } from '../style/themes';
|
import themes, { Themes } from '../style/themes';
|
||||||
import { languages } from '../util/highlighting';
|
import { languages, unknownLanguage } from '../util/language';
|
||||||
import { saveToBytebin } from '../util/storage';
|
import { saveToBytebin } from '../util/storage';
|
||||||
import Button from './Button';
|
import Button from './Button';
|
||||||
import { ResetFunction } from './Editor';
|
import { ResetFunction } from './Editor';
|
||||||
@@ -12,15 +12,13 @@ import MenuButton from './MenuButton';
|
|||||||
|
|
||||||
export interface EditorControlsProps {
|
export interface EditorControlsProps {
|
||||||
actualContent: string;
|
actualContent: string;
|
||||||
resetFunction: RefObject<ResetFunction | null>;
|
resetFunction: MutableRefObject<ResetFunction | undefined>;
|
||||||
language: string;
|
language: string;
|
||||||
setLanguage: (value: string) => void;
|
setLanguage: (value: string) => void;
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
setReadOnly: (value: boolean) => void;
|
setReadOnly: (value: boolean) => void;
|
||||||
theme: keyof Themes;
|
theme: keyof Themes;
|
||||||
setTheme: (value: keyof Themes) => void;
|
setTheme: (value: keyof Themes) => void;
|
||||||
wordWrap: boolean
|
|
||||||
setWordWrap: (value: boolean) => void;
|
|
||||||
zoom: (delta: number) => void;
|
zoom: (delta: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,8 +31,6 @@ export default function EditorControls({
|
|||||||
setReadOnly,
|
setReadOnly,
|
||||||
theme,
|
theme,
|
||||||
setTheme,
|
setTheme,
|
||||||
wordWrap,
|
|
||||||
setWordWrap,
|
|
||||||
zoom,
|
zoom,
|
||||||
}: EditorControlsProps) {
|
}: EditorControlsProps) {
|
||||||
const [saving, setSaving] = useState<boolean>(false);
|
const [saving, setSaving] = useState<boolean>(false);
|
||||||
@@ -108,16 +104,15 @@ export default function EditorControls({
|
|||||||
</Button>
|
</Button>
|
||||||
<MenuButton
|
<MenuButton
|
||||||
label="language"
|
label="language"
|
||||||
value={language}
|
value={language === unknownLanguage ? '?' : language}
|
||||||
setValue={setLanguage}
|
setValue={setLanguage}
|
||||||
ids={languages}
|
ids={languages as unknown as Record<string, string[]>}
|
||||||
/>
|
/>
|
||||||
{readOnly && <Button onClick={unsetReadOnly}>[edit]</Button>}
|
{readOnly && <Button onClick={unsetReadOnly}>[edit]</Button>}
|
||||||
</Section>
|
</Section>
|
||||||
<Section>
|
<Section>
|
||||||
<Button onClick={() => zoom(1)}>[+ </Button>
|
<Button onClick={() => zoom(1)}>[+ </Button>
|
||||||
<Button onClick={() => zoom(-1)}> -]</Button>
|
<Button onClick={() => zoom(-1)}> -]</Button>
|
||||||
<Button onClick={() => setWordWrap(!wordWrap)}>[<span className='optional'>wrap:</span>{wordWrap ? "on" : "off"}]</Button>
|
|
||||||
<MenuButton
|
<MenuButton
|
||||||
label="theme"
|
label="theme"
|
||||||
value={theme}
|
value={theme}
|
||||||
@@ -141,7 +136,7 @@ export default function EditorControls({
|
|||||||
const Header = styled.header`
|
const Header = styled.header`
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 0;
|
top: 0;
|
||||||
z-index: 10;
|
z-index: 2;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 2em;
|
height: 2em;
|
||||||
color: ${props => props.theme.primary};
|
color: ${props => props.theme.primary};
|
||||||
|
|||||||
@@ -5,9 +5,8 @@ import Editor, {
|
|||||||
OnMount,
|
OnMount,
|
||||||
} from '@monaco-editor/react';
|
} from '@monaco-editor/react';
|
||||||
import history from 'history/browser';
|
import history from 'history/browser';
|
||||||
import {
|
import React, {
|
||||||
MutableRefObject,
|
MutableRefObject,
|
||||||
RefObject,
|
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
@@ -17,42 +16,8 @@ import styled from 'styled-components';
|
|||||||
import themes, { Theme } from '../style/themes';
|
import themes, { Theme } from '../style/themes';
|
||||||
|
|
||||||
import type { editor } from 'monaco-editor';
|
import type { editor } from 'monaco-editor';
|
||||||
import { diffLanguage } from '../util/languages/diff';
|
|
||||||
import { logLanguage } from '../util/languages/log';
|
|
||||||
import { ResetFunction } from './Editor';
|
import { ResetFunction } from './Editor';
|
||||||
|
import { logLanguage } from '../util/log-language';
|
||||||
import { loader } from '@monaco-editor/react';
|
|
||||||
import * as monaco from 'monaco-editor';
|
|
||||||
|
|
||||||
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
|
|
||||||
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker';
|
|
||||||
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker';
|
|
||||||
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
|
|
||||||
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
|
|
||||||
|
|
||||||
self.MonacoEnvironment = {
|
|
||||||
getWorker(_: string, label: string): Promise<Worker> | Worker {
|
|
||||||
switch (label) {
|
|
||||||
case 'json':
|
|
||||||
return new jsonWorker();
|
|
||||||
case 'css':
|
|
||||||
case 'scss':
|
|
||||||
case 'less':
|
|
||||||
return new cssWorker();
|
|
||||||
case 'html':
|
|
||||||
case 'handlebars':
|
|
||||||
case 'razor':
|
|
||||||
return new htmlWorker();
|
|
||||||
case 'typescript':
|
|
||||||
case 'javascript':
|
|
||||||
return new tsWorker();
|
|
||||||
default:
|
|
||||||
return new editorWorker();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
loader.config({ monaco });
|
|
||||||
|
|
||||||
export interface EditorTextAreaProps {
|
export interface EditorTextAreaProps {
|
||||||
forcedContent: string;
|
forcedContent: string;
|
||||||
@@ -62,8 +27,7 @@ export interface EditorTextAreaProps {
|
|||||||
language: string;
|
language: string;
|
||||||
fontSize: number;
|
fontSize: number;
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
wordWrap: boolean;
|
resetFunction: MutableRefObject<ResetFunction | undefined>;
|
||||||
resetFunction: MutableRefObject<ResetFunction | null>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function EditorTextArea({
|
export default function EditorTextArea({
|
||||||
@@ -74,7 +38,6 @@ export default function EditorTextArea({
|
|||||||
language,
|
language,
|
||||||
fontSize,
|
fontSize,
|
||||||
readOnly,
|
readOnly,
|
||||||
wordWrap,
|
|
||||||
resetFunction,
|
resetFunction,
|
||||||
}: EditorTextAreaProps) {
|
}: EditorTextAreaProps) {
|
||||||
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
|
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
|
||||||
@@ -100,8 +63,6 @@ export default function EditorTextArea({
|
|||||||
|
|
||||||
monaco.languages.register({ id: 'log' });
|
monaco.languages.register({ id: 'log' });
|
||||||
monaco.languages.setMonarchTokensProvider('log', logLanguage);
|
monaco.languages.setMonarchTokensProvider('log', logLanguage);
|
||||||
monaco.languages.register({ id: 'diff' });
|
|
||||||
monaco.languages.setMonarchTokensProvider('diff', diffLanguage);
|
|
||||||
|
|
||||||
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
|
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
|
||||||
noSemanticValidation: true,
|
noSemanticValidation: true,
|
||||||
@@ -168,7 +129,7 @@ export default function EditorTextArea({
|
|||||||
fontFamily: 'JetBrains Mono',
|
fontFamily: 'JetBrains Mono',
|
||||||
fontSize: fontSize,
|
fontSize: fontSize,
|
||||||
fontLigatures: true,
|
fontLigatures: true,
|
||||||
wordWrap: wordWrap ? 'on' : 'off',
|
wordWrap: 'on',
|
||||||
renderLineHighlight: 'none',
|
renderLineHighlight: 'none',
|
||||||
renderValidationDecorations: 'off',
|
renderValidationDecorations: 'off',
|
||||||
readOnly,
|
readOnly,
|
||||||
@@ -285,7 +246,7 @@ function useSelectedLine(): [SelectedLine, ToggleSelectedFunction] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function useLineNumberMagic(
|
function useLineNumberMagic(
|
||||||
editorAreaRef: RefObject<HTMLDivElement | null>,
|
editorAreaRef: React.RefObject<HTMLDivElement>,
|
||||||
selected: SelectedLine,
|
selected: SelectedLine,
|
||||||
toggleSelected: ToggleSelectedFunction,
|
toggleSelected: ToggleSelectedFunction,
|
||||||
forcedContent: string,
|
forcedContent: string,
|
||||||
@@ -300,13 +261,13 @@ function useLineNumberMagic(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handler = (click: MouseEvent) => {
|
const handler = (click: MouseEvent) => {
|
||||||
const element = document.elementFromPoint(click.x, click.y);
|
const target = click?.target as HTMLElement;
|
||||||
if (
|
if (
|
||||||
element &&
|
target &&
|
||||||
element.classList.contains('line-numbers') &&
|
target.classList.contains('line-numbers') &&
|
||||||
element.textContent
|
target.textContent
|
||||||
) {
|
) {
|
||||||
toggleSelected(parseInt(element.textContent), click);
|
toggleSelected(parseInt(target.textContent), click);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export default function MenuButton<T extends string>({
|
|||||||
|
|
||||||
// close the menu when a click is made elsewhere
|
// close the menu when a click is made elsewhere
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const listener = () => setOpen(false);
|
const listener = (e: MouseEvent) => setOpen(false);
|
||||||
window.addEventListener('click', listener);
|
window.addEventListener('click', listener);
|
||||||
return () => window.removeEventListener('click', listener);
|
return () => window.removeEventListener('click', listener);
|
||||||
}, [setOpen]);
|
}, [setOpen]);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { get as lsGet, remove as lsRemove, set as lsSet } from 'local-storage';
|
||||||
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
import { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
||||||
|
|
||||||
// hook used to load "preference" settings from local storage, or fall back to a default value.
|
// hook used to load "preference" settings from local storage, or fall back to a default value.
|
||||||
@@ -7,9 +8,8 @@ export default function usePreference<T>(
|
|||||||
valid: (value: T) => boolean
|
valid: (value: T) => boolean
|
||||||
): [T, Dispatch<SetStateAction<T>>, (value: T) => boolean] {
|
): [T, Dispatch<SetStateAction<T>>, (value: T) => boolean] {
|
||||||
const [value, setValue] = useState<T>(() => {
|
const [value, setValue] = useState<T>(() => {
|
||||||
const prefRaw = localStorage.getItem(id);
|
const pref = lsGet(id) as T;
|
||||||
const pref = prefRaw !== null ? (JSON.parse(prefRaw) as T) : undefined;
|
if (pref && valid(pref)) {
|
||||||
if (pref !== null && pref !== undefined && valid(pref)) {
|
|
||||||
return pref;
|
return pref;
|
||||||
} else {
|
} else {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
@@ -18,9 +18,9 @@ export default function usePreference<T>(
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (value === defaultValue) {
|
if (value === defaultValue) {
|
||||||
localStorage.removeItem(id);
|
lsRemove(id);
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem(id, JSON.stringify(value));
|
lsSet(id, value);
|
||||||
}
|
}
|
||||||
}, [value, id, defaultValue]);
|
}, [value, id, defaultValue]);
|
||||||
|
|
||||||
|
|||||||
1
src/style/styled.d.ts
vendored
1
src/style/styled.d.ts
vendored
@@ -2,6 +2,5 @@ import 'styled-components';
|
|||||||
import { Theme } from './themes';
|
import { Theme } from './themes';
|
||||||
|
|
||||||
declare module 'styled-components' {
|
declare module 'styled-components' {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
||||||
export interface DefaultTheme extends Theme {}
|
export interface DefaultTheme extends Theme {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import type { editor } from 'monaco-editor';
|
import type { editor } from 'monaco-editor';
|
||||||
|
|
||||||
import { CatppuccinFlavor, ColorFormat, flavors } from '@catppuccin/palette';
|
|
||||||
import dracula from 'monaco-themes/themes/Dracula.json';
|
import dracula from 'monaco-themes/themes/Dracula.json';
|
||||||
import monokai from 'monaco-themes/themes/Monokai.json';
|
import monokai from 'monaco-themes/themes/Monokai.json';
|
||||||
import solarizedDark from 'monaco-themes/themes/Solarized-dark.json';
|
import solarizedDark from 'monaco-themes/themes/Solarized-dark.json';
|
||||||
import solarizedLight from 'monaco-themes/themes/Solarized-light.json';
|
import solarizedLight from 'monaco-themes/themes/Solarized-light.json';
|
||||||
|
|
||||||
type Color = `#${string}`;
|
type Color = `#${string}`;
|
||||||
type ColorRecord = Record<string, Color>;
|
|
||||||
|
|
||||||
export interface Theme {
|
export interface Theme {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -30,10 +28,6 @@ export interface Themes {
|
|||||||
'monokai': Theme;
|
'monokai': Theme;
|
||||||
'solarized': Theme;
|
'solarized': Theme;
|
||||||
'solarized-light': Theme;
|
'solarized-light': Theme;
|
||||||
'latte': Theme;
|
|
||||||
'frappe': Theme;
|
|
||||||
'macchiato': Theme;
|
|
||||||
'mocha': Theme;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const themes: Themes = {
|
const themes: Themes = {
|
||||||
@@ -50,34 +44,28 @@ const themes: Themes = {
|
|||||||
backgroundColor: '#161b22', // canvas.overlay
|
backgroundColor: '#161b22', // canvas.overlay
|
||||||
},
|
},
|
||||||
|
|
||||||
editor: makeMonacoTheme(
|
editor: makeMonacoTheme({
|
||||||
{
|
base: 'vs-dark',
|
||||||
base: 'vs-dark',
|
colors: {
|
||||||
colors: {
|
primary: '#c9d1d9', // fg.default
|
||||||
primary: '#c9d1d9', // fg.default
|
background: '#0d1117', // canvas.default
|
||||||
background: '#0d1117', // canvas.default
|
comment: '#8b949e',
|
||||||
comment: '#8b949e',
|
delimiter: '#d2a8ff',
|
||||||
delimiter: '#d2a8ff',
|
annotation: '#a5d6ff',
|
||||||
annotation: '#a5d6ff',
|
constant: '#ff7b72',
|
||||||
constant: '#ff7b72',
|
number: '#f2cc60',
|
||||||
number: '#f2cc60',
|
string: '#79c0ff',
|
||||||
string: '#79c0ff',
|
operator: '#ff7b72',
|
||||||
operator: '#ff7b72',
|
keyword: '#ff7b72',
|
||||||
keyword: '#ff7b72',
|
type: '#ffa657',
|
||||||
type: '#ffa657',
|
variable: '#ffa657',
|
||||||
variable: '#ffa657',
|
logInfo: '#3fb950', // green.3
|
||||||
logInfo: '#3fb950', // green.3
|
logError: '#f85149', // red.4
|
||||||
logError: '#f85149', // red.4
|
logWarning: '#d29922', // yellow.3
|
||||||
logWarning: '#d29922', // yellow.3
|
logDate: '#33B3AE', // teal.3
|
||||||
logDate: '#33B3AE', // teal.3
|
logException: '#f8e3a1', // yellow.0
|
||||||
logException: '#f8e3a1', // yellow.0
|
|
||||||
diffMeta: '#33B3AE', // teal.3
|
|
||||||
diffAddition: '#3fb950', // green.3
|
|
||||||
diffDeletion: '#f85149', // red.4
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{}
|
}),
|
||||||
),
|
|
||||||
},
|
},
|
||||||
'light': {
|
'light': {
|
||||||
id: 'light',
|
id: 'light',
|
||||||
@@ -92,34 +80,28 @@ const themes: Themes = {
|
|||||||
backgroundColor: '#e0f6ff',
|
backgroundColor: '#e0f6ff',
|
||||||
},
|
},
|
||||||
|
|
||||||
editor: makeMonacoTheme(
|
editor: makeMonacoTheme({
|
||||||
{
|
base: 'vs',
|
||||||
base: 'vs',
|
colors: {
|
||||||
colors: {
|
primary: '#000000',
|
||||||
primary: '#000000',
|
background: '#ffffff',
|
||||||
background: '#ffffff',
|
comment: '#708090',
|
||||||
comment: '#708090',
|
delimiter: '#999999',
|
||||||
delimiter: '#999999',
|
annotation: '#999999',
|
||||||
annotation: '#999999',
|
constant: '#990055',
|
||||||
constant: '#990055',
|
number: '#990055',
|
||||||
number: '#990055',
|
string: '#669900',
|
||||||
string: '#669900',
|
operator: '#9a6e3a',
|
||||||
operator: '#9a6e3a',
|
keyword: '#0077aa',
|
||||||
keyword: '#0077aa',
|
type: '#DD4A68',
|
||||||
type: '#DD4A68',
|
variable: '#ee9900',
|
||||||
variable: '#ee9900',
|
logInfo: '#2da44e', // green.4
|
||||||
logInfo: '#2da44e', // green.4
|
logError: '#cf222e', // red.5
|
||||||
logError: '#cf222e', // red.5
|
logWarning: '#d4a72c', // yellow.3
|
||||||
logWarning: '#d4a72c', // yellow.3
|
logDate: '#136061', // teal.6
|
||||||
logDate: '#136061', // teal.6
|
logException: '#7d4e00', // yellow.6
|
||||||
logException: '#7d4e00', // yellow.6
|
|
||||||
diffMeta: '#136061', // teal.6
|
|
||||||
diffAddition: '#2da44e', // green.4
|
|
||||||
diffDeletion: '#cf222e', // red.5
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{}
|
}),
|
||||||
),
|
|
||||||
},
|
},
|
||||||
'dracula': {
|
'dracula': {
|
||||||
id: 'dracula',
|
id: 'dracula',
|
||||||
@@ -132,15 +114,12 @@ const themes: Themes = {
|
|||||||
color: '#586e75',
|
color: '#586e75',
|
||||||
backgroundColor: '#44475a',
|
backgroundColor: '#44475a',
|
||||||
},
|
},
|
||||||
editor: addExtraColors(dracula as editor.IStandaloneThemeData, {
|
editor: addLogColors(dracula as editor.IStandaloneThemeData, {
|
||||||
logInfo: '#50FA7B', // green
|
info: '#50FA7B', // green
|
||||||
logError: '#FF5555', // red
|
error: '#FF5555', // red
|
||||||
logWarning: '#FFB86C', // orange
|
warning: '#FFB86C', // orange
|
||||||
logDate: '#BD93F9', // purple
|
date: '#BD93F9', // purple
|
||||||
logException: '#F1FA8C', // yellow
|
exception: '#F1FA8C', // yellow
|
||||||
diffMeta: '#BD93F9', // purple
|
|
||||||
diffAddition: '#50FA7B', // green
|
|
||||||
diffDeletion: '#FF5555', // red
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
'monokai': {
|
'monokai': {
|
||||||
@@ -154,15 +133,12 @@ const themes: Themes = {
|
|||||||
color: '#49483E',
|
color: '#49483E',
|
||||||
backgroundColor: '#3E3D32',
|
backgroundColor: '#3E3D32',
|
||||||
},
|
},
|
||||||
editor: addExtraColors(monokai as editor.IStandaloneThemeData, {
|
editor: addLogColors(monokai as editor.IStandaloneThemeData, {
|
||||||
logInfo: '#a6e22e', // green
|
info: '#a6e22e', // green
|
||||||
logError: '#f92672', // red
|
error: '#f92672', // red
|
||||||
logWarning: '#fd971f', // orange
|
warning: '#fd971f', // orange
|
||||||
logDate: '#AB9DF2', // purple
|
date: '#AB9DF2', // purple
|
||||||
logException: '#F1FA8C', // yellow
|
exception: '#F1FA8C', // yellow
|
||||||
diffMeta: '#AB9DF2', // purple
|
|
||||||
diffAddition: '#a6e22e', // green
|
|
||||||
diffDeletion: '#f92672', // red
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
'solarized': {
|
'solarized': {
|
||||||
@@ -176,15 +152,12 @@ const themes: Themes = {
|
|||||||
color: '#93a1a1', // base1
|
color: '#93a1a1', // base1
|
||||||
backgroundColor: '#073642', // base02
|
backgroundColor: '#073642', // base02
|
||||||
},
|
},
|
||||||
editor: addExtraColors(solarizedDark as editor.IStandaloneThemeData, {
|
editor: addLogColors(solarizedDark as editor.IStandaloneThemeData, {
|
||||||
logInfo: '#268bd2', // blue
|
info: '#268bd2', // blue
|
||||||
logError: '#dc322f', // red
|
error: '#dc322f', // red
|
||||||
logWarning: '#b58900', // yellow
|
warning: '#b58900', // yellow
|
||||||
logDate: '#2aa198', // cyan
|
date: '#2aa198', // cyan
|
||||||
logException: '#859900', // green
|
exception: '#859900', // green
|
||||||
diffMeta: '#2aa198', // cyan
|
|
||||||
diffAddition: '#859900', // green
|
|
||||||
diffDeletion: '#dc322f', // red
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
'solarized-light': {
|
'solarized-light': {
|
||||||
@@ -198,36 +171,18 @@ const themes: Themes = {
|
|||||||
color: '#586e75', // base01
|
color: '#586e75', // base01
|
||||||
backgroundColor: '#eee8d5', // base2
|
backgroundColor: '#eee8d5', // base2
|
||||||
},
|
},
|
||||||
editor: addExtraColors(solarizedLight as editor.IStandaloneThemeData, {
|
editor: addLogColors(solarizedLight as editor.IStandaloneThemeData, {
|
||||||
logInfo: '#268bd2', // blue
|
info: '#268bd2', // blue
|
||||||
logError: '#dc322f', // red
|
error: '#dc322f', // red
|
||||||
logWarning: '#b58900', // yellow
|
warning: '#b58900', // yellow
|
||||||
logDate: '#2aa198', // cyan
|
date: '#2aa198', // cyan
|
||||||
logException: '#859900', // green
|
exception: '#859900', // green
|
||||||
diffMeta: '#2aa198', // cyan
|
|
||||||
diffAddition: '#859900', // green
|
|
||||||
diffDeletion: '#dc322f', // red
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
'latte': createCatppuccinTheme(flavors.latte),
|
|
||||||
'frappe': createCatppuccinTheme(flavors.frappe),
|
|
||||||
'macchiato': createCatppuccinTheme(flavors.macchiato),
|
|
||||||
'mocha': createCatppuccinTheme(flavors.mocha),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default themes;
|
export default themes;
|
||||||
|
|
||||||
interface ExtraColors {
|
|
||||||
logInfo: Color;
|
|
||||||
logError: Color;
|
|
||||||
logWarning: Color;
|
|
||||||
logDate: Color;
|
|
||||||
logException: Color;
|
|
||||||
diffMeta: Color;
|
|
||||||
diffAddition: Color;
|
|
||||||
diffDeletion: Color;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface MonacoThemeProps {
|
interface MonacoThemeProps {
|
||||||
base: 'vs' | 'vs-dark';
|
base: 'vs' | 'vs-dark';
|
||||||
colors: {
|
colors: {
|
||||||
@@ -243,12 +198,16 @@ interface MonacoThemeProps {
|
|||||||
keyword: Color;
|
keyword: Color;
|
||||||
type: Color;
|
type: Color;
|
||||||
variable: Color;
|
variable: Color;
|
||||||
} & ExtraColors;
|
logInfo: Color;
|
||||||
|
logError: Color;
|
||||||
|
logWarning: Color;
|
||||||
|
logDate: Color;
|
||||||
|
logException: Color;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makeMonacoTheme(
|
export function makeMonacoTheme(
|
||||||
props: MonacoThemeProps,
|
props: MonacoThemeProps
|
||||||
extraColors: ColorRecord
|
|
||||||
): editor.IStandaloneThemeData {
|
): editor.IStandaloneThemeData {
|
||||||
const colors = Object.fromEntries(
|
const colors = Object.fromEntries(
|
||||||
Object.entries(props.colors).map(([key, color]) => [
|
Object.entries(props.colors).map(([key, color]) => [
|
||||||
@@ -257,11 +216,6 @@ export function makeMonacoTheme(
|
|||||||
])
|
])
|
||||||
) as Record<keyof MonacoThemeProps['colors'], string>;
|
) as Record<keyof MonacoThemeProps['colors'], string>;
|
||||||
|
|
||||||
const editorColors: ColorRecord = {
|
|
||||||
'editor.background': `#${colors.background}`,
|
|
||||||
'editor.foreground': `#${colors.primary}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
base: props.base,
|
base: props.base,
|
||||||
inherit: true,
|
inherit: true,
|
||||||
@@ -292,94 +246,37 @@ export function makeMonacoTheme(
|
|||||||
{ token: 'warning.log', foreground: colors.logWarning },
|
{ token: 'warning.log', foreground: colors.logWarning },
|
||||||
{ token: 'date.log', foreground: colors.logDate },
|
{ token: 'date.log', foreground: colors.logDate },
|
||||||
{ token: 'exception.log', foreground: colors.logException },
|
{ token: 'exception.log', foreground: colors.logException },
|
||||||
{ token: 'meta.diff', foreground: colors.diffMeta },
|
|
||||||
{ token: 'addition.diff', foreground: colors.diffAddition },
|
|
||||||
{ token: 'deletion.diff', foreground: colors.diffDeletion },
|
|
||||||
],
|
],
|
||||||
colors: { ...editorColors, ...extraColors },
|
colors: {
|
||||||
|
'editor.background': `#${colors.background}`,
|
||||||
|
'editor.foreground': `#${colors.primary}`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function addExtraColors(
|
interface LogColors {
|
||||||
|
info: Color;
|
||||||
|
error: Color;
|
||||||
|
warning: Color;
|
||||||
|
date: Color;
|
||||||
|
exception: Color;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function addLogColors(
|
||||||
theme: editor.IStandaloneThemeData,
|
theme: editor.IStandaloneThemeData,
|
||||||
extraColors: ExtraColors
|
logColors: LogColors
|
||||||
): editor.IStandaloneThemeData {
|
): editor.IStandaloneThemeData {
|
||||||
const colors = Object.fromEntries(
|
const colors = Object.fromEntries(
|
||||||
Object.entries(extraColors).map(([key, color]) => [key, color.substring(1)])
|
Object.entries(logColors).map(([key, color]) => [key, color.substring(1)])
|
||||||
) as Record<keyof ExtraColors, string>;
|
) as Record<keyof LogColors, string>;
|
||||||
theme.rules.push(
|
theme.rules.push(
|
||||||
...[
|
...[
|
||||||
{ token: 'info.log', foreground: colors.logInfo },
|
{ token: 'info.log', foreground: colors.info },
|
||||||
{ token: 'error.log', foreground: colors.logError, fontStyle: 'bold' },
|
{ token: 'error.log', foreground: colors.error, fontStyle: 'bold' },
|
||||||
{ token: 'warning.log', foreground: colors.logWarning },
|
{ token: 'warning.log', foreground: colors.warning },
|
||||||
{ token: 'date.log', foreground: colors.logDate },
|
{ token: 'date.log', foreground: colors.date },
|
||||||
{ token: 'exception.log', foreground: colors.logException },
|
{ token: 'exception.log', foreground: colors.exception },
|
||||||
{ token: 'meta.diff', foreground: colors.diffMeta },
|
|
||||||
{ token: 'addition.diff', foreground: colors.diffAddition },
|
|
||||||
{ token: 'deletion.diff', foreground: colors.diffDeletion },
|
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
return theme;
|
return theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createCatppuccinTheme(flavor: CatppuccinFlavor): Theme {
|
|
||||||
const color = (color: ColorFormat) => color.hex as Color;
|
|
||||||
const nameToId: Record<string, string> = {
|
|
||||||
[flavors.latte.name]: 'latte',
|
|
||||||
[flavors.frappe.name]: 'frappe',
|
|
||||||
[flavors.macchiato.name]: 'macchiato',
|
|
||||||
[flavors.mocha.name]: 'mocha',
|
|
||||||
};
|
|
||||||
|
|
||||||
const editorTheme = makeMonacoTheme(
|
|
||||||
{
|
|
||||||
base: flavor.dark ? 'vs-dark' : 'vs',
|
|
||||||
colors: {
|
|
||||||
// Monaco
|
|
||||||
primary: color(flavor.colors.text),
|
|
||||||
background: color(flavor.colors.mantle),
|
|
||||||
string: color(flavor.colors.green),
|
|
||||||
comment: color(flavor.colors.overlay2),
|
|
||||||
delimiter: color(flavor.colors.overlay2),
|
|
||||||
annotation: color(flavor.colors.yellow),
|
|
||||||
constant: color(flavor.colors.peach),
|
|
||||||
number: color(flavor.colors.peach),
|
|
||||||
operator: color(flavor.colors.sky),
|
|
||||||
keyword: color(flavor.colors.mauve),
|
|
||||||
type: color(flavor.colors.yellow),
|
|
||||||
variable: color(flavor.colors.text),
|
|
||||||
|
|
||||||
// Log Files
|
|
||||||
logDate: color(flavor.colors.mauve),
|
|
||||||
logInfo: color(flavor.colors.green),
|
|
||||||
logWarning: color(flavor.colors.yellow),
|
|
||||||
logError: color(flavor.colors.red),
|
|
||||||
logException: color(flavor.colors.yellow),
|
|
||||||
|
|
||||||
// Diff Files
|
|
||||||
diffMeta: color(flavor.colors.sky),
|
|
||||||
diffAddition: color(flavor.colors.green),
|
|
||||||
diffDeletion: color(flavor.colors.red),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'editorBracketHighlight.foreground1': color(flavor.colors.overlay2),
|
|
||||||
'editorBracketHighlight.foreground2': color(flavor.colors.overlay2),
|
|
||||||
'editorBracketHighlight.foreground3': color(flavor.colors.overlay2),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: nameToId[flavor.name],
|
|
||||||
lightOrDark: flavor.dark ? 'dark' : 'light',
|
|
||||||
primary: color(flavor.colors.text),
|
|
||||||
secondary: color(flavor.colors.base),
|
|
||||||
highlight: color(flavor.colors.surface0),
|
|
||||||
background: color(flavor.colors.mantle),
|
|
||||||
highlightedLine: {
|
|
||||||
color: color(flavor.colors.rosewater),
|
|
||||||
backgroundColor: color(flavor.colors.surface2),
|
|
||||||
},
|
|
||||||
editor: editorTheme,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
export const bytebinUrl =
|
export const bytebinUrl =
|
||||||
import.meta.env.VITE_BYTEBIN_URL || 'https://bytebin.lucko.me/';
|
process.env.REACT_APP_BYTEBIN_URL || 'https://bytebin.lucko.me/';
|
||||||
|
|
||||||
export const postUrl = bytebinUrl + 'post';
|
export const postUrl = bytebinUrl + 'post';
|
||||||
|
|
||||||
|
export const languageDetectionUrl =
|
||||||
|
process.env.REACT_APP_LANG_DETECT_URL ||
|
||||||
|
'https://language-detection-service.pastes.dev/';
|
||||||
|
|||||||
49
src/util/detect-language.ts
Normal file
49
src/util/detect-language.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { languageDetectionUrl } from './constants';
|
||||||
|
import { Language } from './language';
|
||||||
|
|
||||||
|
interface DetectedLanguage {
|
||||||
|
languageId: string;
|
||||||
|
confidence: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function detectLanguage(id: string): Promise<Language | null> {
|
||||||
|
try {
|
||||||
|
const resp = await fetch(languageDetectionUrl + id);
|
||||||
|
if (resp.ok) {
|
||||||
|
const results = (await resp.json()) as DetectedLanguage[];
|
||||||
|
for (const { languageId, confidence } of results) {
|
||||||
|
if (confidence > 0.5 && lookup[languageId]) {
|
||||||
|
return lookup[languageId];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lookup: Record<string, Language> = {
|
||||||
|
ini: 'log', // the model seems to confidently guess log files as ini - log is the more likely option
|
||||||
|
yaml: 'yaml',
|
||||||
|
md: 'markdown',
|
||||||
|
rb: 'ruby',
|
||||||
|
kt: 'kotlin',
|
||||||
|
xml: 'xml',
|
||||||
|
js: 'javascript',
|
||||||
|
html: 'html',
|
||||||
|
ts: 'typescript',
|
||||||
|
json: 'json',
|
||||||
|
php: 'php',
|
||||||
|
py: 'python',
|
||||||
|
rs: 'rust',
|
||||||
|
sql: 'sql',
|
||||||
|
sh: 'shell',
|
||||||
|
cpp: 'cpp',
|
||||||
|
go: 'go',
|
||||||
|
scala: 'scala',
|
||||||
|
dockerfile: 'dockerfile',
|
||||||
|
java: 'java',
|
||||||
|
cs: 'csharp',
|
||||||
|
css: 'css',
|
||||||
|
groovy: 'java',
|
||||||
|
};
|
||||||
|
// missing: csv, ml, ex, pas, bat, lua, groovy, v, jl, pm, prolog, matlab, clj, f90, c, tex, coffee, ps1, hs, mm, cmake, erl, dm, dart, asm, makefile, r, swift, lisp, vba, toml, cbl
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
export const languages = {
|
|
||||||
text: ['plain', 'log'],
|
|
||||||
config: ['yaml', 'json', 'xml', 'ini'],
|
|
||||||
code: [
|
|
||||||
'java',
|
|
||||||
'javascript',
|
|
||||||
'typescript',
|
|
||||||
'python',
|
|
||||||
'kotlin',
|
|
||||||
'scala',
|
|
||||||
'cpp',
|
|
||||||
'csharp',
|
|
||||||
'shell',
|
|
||||||
'ruby',
|
|
||||||
'rust',
|
|
||||||
'sql',
|
|
||||||
'go',
|
|
||||||
'lua',
|
|
||||||
'swift',
|
|
||||||
'c',
|
|
||||||
],
|
|
||||||
web: ['html', 'css', 'scss', 'php', 'graphql'],
|
|
||||||
misc: ['diff', 'dockerfile', 'markdown', 'proto'],
|
|
||||||
};
|
|
||||||
|
|
||||||
export const languageIds = Object.values(languages).flat(1);
|
|
||||||
70
src/util/language.ts
Normal file
70
src/util/language.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
export type Language =
|
||||||
|
| 'plain'
|
||||||
|
| 'plaintext'
|
||||||
|
| 'log'
|
||||||
|
| 'yaml'
|
||||||
|
| 'json'
|
||||||
|
| 'xml'
|
||||||
|
| 'ini'
|
||||||
|
| 'java'
|
||||||
|
| 'javascript'
|
||||||
|
| 'typescript'
|
||||||
|
| 'python'
|
||||||
|
| 'kotlin'
|
||||||
|
| 'scala'
|
||||||
|
| 'cpp'
|
||||||
|
| 'csharp'
|
||||||
|
| 'shell'
|
||||||
|
| 'ruby'
|
||||||
|
| 'rust'
|
||||||
|
| 'sql'
|
||||||
|
| 'go'
|
||||||
|
| 'html'
|
||||||
|
| 'css'
|
||||||
|
| 'scss'
|
||||||
|
| 'php'
|
||||||
|
| 'graphql'
|
||||||
|
| 'dockerfile'
|
||||||
|
| 'markdown'
|
||||||
|
| 'proto';
|
||||||
|
|
||||||
|
export const unknownLanguage: Language & 'plain' = 'plain';
|
||||||
|
|
||||||
|
export interface Languages {
|
||||||
|
text: Language[];
|
||||||
|
config: Language[];
|
||||||
|
code: Language[];
|
||||||
|
web: Language[];
|
||||||
|
misc: Language[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const languages: Languages = {
|
||||||
|
text: ['plaintext', 'log'],
|
||||||
|
config: ['yaml', 'json', 'xml', 'ini'],
|
||||||
|
code: [
|
||||||
|
'java',
|
||||||
|
'javascript',
|
||||||
|
'typescript',
|
||||||
|
'python',
|
||||||
|
'kotlin',
|
||||||
|
'scala',
|
||||||
|
'cpp',
|
||||||
|
'csharp',
|
||||||
|
'shell',
|
||||||
|
'ruby',
|
||||||
|
'rust',
|
||||||
|
'sql',
|
||||||
|
'go',
|
||||||
|
],
|
||||||
|
web: ['html', 'css', 'scss', 'php', 'graphql'],
|
||||||
|
misc: ['dockerfile', 'markdown', 'proto'],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const languageIds: Language[] = [
|
||||||
|
...Object.values(languages).flat(1),
|
||||||
|
unknownLanguage,
|
||||||
|
];
|
||||||
|
|
||||||
|
export function isLanguage(lang: string): lang is Language {
|
||||||
|
return languageIds.includes(lang as Language);
|
||||||
|
}
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
import { languages } from 'monaco-editor';
|
|
||||||
|
|
||||||
export const diffLanguage: languages.IMonarchLanguage = {
|
|
||||||
defaultToken: '',
|
|
||||||
tokenizer: {
|
|
||||||
root: [
|
|
||||||
// Meta lines (e.g., @@ -1,2 +3,4 @@)
|
|
||||||
[/@@@ +-\d+,\d+ +\+\d+,\d+ +@@@/, 'meta'],
|
|
||||||
[/^\*\*\* +\d+,\d+ +\*\*\*\*$/, 'meta'],
|
|
||||||
[/^--- +\d+,\d+ +----$/, 'meta'],
|
|
||||||
|
|
||||||
// Comments
|
|
||||||
[/Index: .*/, 'comment'],
|
|
||||||
[/^index.*/, 'comment'],
|
|
||||||
[/={3,}/, 'comment'],
|
|
||||||
[/^-{3}.*/, 'comment'],
|
|
||||||
[/^\*{3} .*/, 'comment'],
|
|
||||||
[/^\+{3}.*/, 'comment'],
|
|
||||||
[/^diff --git.*/, 'comment'],
|
|
||||||
[/^\*{15}$/, 'comment'],
|
|
||||||
|
|
||||||
// Additions
|
|
||||||
[/^\+.*/, 'addition'],
|
|
||||||
[/^!.*/, 'addition'],
|
|
||||||
|
|
||||||
// Deletions
|
|
||||||
[/^-.*/, 'deletion'],
|
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import { gzip } from 'pako';
|
import { gzip } from 'pako';
|
||||||
import MIMEType from 'whatwg-mimetype';
|
import MIMEType from 'whatwg-mimetype';
|
||||||
import { bytebinUrl, postUrl } from './constants';
|
import { bytebinUrl, postUrl } from './constants';
|
||||||
import { languageIds } from './highlighting';
|
import { isLanguage, Language } from './language';
|
||||||
|
|
||||||
interface LoadResultSuccess {
|
interface LoadResultSuccess {
|
||||||
ok: true;
|
ok: true;
|
||||||
content: string;
|
content: string;
|
||||||
type?: string;
|
type?: Language;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LoadResultFail {
|
interface LoadResultFail {
|
||||||
@@ -31,7 +31,7 @@ export async function loadFromBytebin(id: string): Promise<LoadResult> {
|
|||||||
} else {
|
} else {
|
||||||
return { ok: false };
|
return { ok: false };
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e) {
|
||||||
return { ok: false };
|
return { ok: false };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,13 +64,21 @@ export async function saveToBytebin(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function contentTypeToLanguage(contentType: string) {
|
export function contentTypeToLanguage(
|
||||||
|
contentType: string
|
||||||
|
): Language | undefined {
|
||||||
const { type, subtype: subType } = new MIMEType(contentType);
|
const { type, subtype: subType } = new MIMEType(contentType);
|
||||||
if (type === 'application' && subType === 'json') {
|
if (type === 'application' && subType === 'json') {
|
||||||
return 'json';
|
return 'json';
|
||||||
}
|
}
|
||||||
if (type === 'text' && languageIds.includes(subType.toLowerCase())) {
|
|
||||||
return subType.toLowerCase();
|
let subTypeLower = subType.toLowerCase();
|
||||||
|
if (subTypeLower.startsWith('x-')) {
|
||||||
|
subTypeLower = subTypeLower.substring(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'text' && isLanguage(subTypeLower)) {
|
||||||
|
return subTypeLower;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
src/vite-env.d.ts
vendored
9
src/vite-env.d.ts
vendored
@@ -1,9 +0,0 @@
|
|||||||
/// <reference types="vite/client" />
|
|
||||||
|
|
||||||
interface ImportMetaEnv {
|
|
||||||
readonly VITE_BYTEBIN_URL?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ImportMeta {
|
|
||||||
readonly env: ImportMetaEnv;
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
|
||||||
"target": "ES2020",
|
|
||||||
"useDefineForClassFields": true,
|
|
||||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
||||||
"module": "ESNext",
|
|
||||||
"skipLibCheck": true,
|
|
||||||
|
|
||||||
/* Bundler mode */
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"moduleDetection": "force",
|
|
||||||
"noEmit": true,
|
|
||||||
"jsx": "react-jsx",
|
|
||||||
|
|
||||||
/* Linting */
|
|
||||||
"strict": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
"noUncheckedSideEffectImports": true
|
|
||||||
},
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,27 @@
|
|||||||
{
|
{
|
||||||
"files": [],
|
"compilerOptions": {
|
||||||
"references": [
|
"target": "es5",
|
||||||
{ "path": "./tsconfig.app.json" },
|
"lib": [
|
||||||
{ "path": "./tsconfig.node.json" }
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"esnext"
|
||||||
|
],
|
||||||
|
"allowJs": false,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
|
"strict": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"module": "esnext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"jsx": "react-jsx"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/*.ts",
|
||||||
|
"src/*.tsx"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
|
||||||
"target": "ES2022",
|
|
||||||
"lib": ["ES2023"],
|
|
||||||
"module": "ESNext",
|
|
||||||
"skipLibCheck": true,
|
|
||||||
|
|
||||||
/* Bundler mode */
|
|
||||||
"moduleResolution": "bundler",
|
|
||||||
"allowImportingTsExtensions": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"moduleDetection": "force",
|
|
||||||
"noEmit": true,
|
|
||||||
|
|
||||||
/* Linting */
|
|
||||||
"strict": true,
|
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
|
||||||
"noUncheckedSideEffectImports": true
|
|
||||||
},
|
|
||||||
"include": ["vite.config.ts"]
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
import react from '@vitejs/plugin-react-swc';
|
|
||||||
import { defineConfig } from 'vite';
|
|
||||||
|
|
||||||
// https://vite.dev/config/
|
|
||||||
export default defineConfig({
|
|
||||||
plugins: [react()],
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user