Personal Telegram developer console for GitHub

feat/studio-g0-foundation
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
tsx 191 lines 6.04 KB
Raw
1 import { useEffect, useRef } from 'react';
2 import { Compartment, EditorState } from '@codemirror/state';
3 import {
4 EditorView,
5 highlightActiveLine,
6 highlightActiveLineGutter,
7 lineNumbers,
8 keymap,
9 drawSelection
10 } from '@codemirror/view';
11 import {
12 defaultKeymap,
13 history,
14 historyKeymap,
15 indentWithTab
16 } from '@codemirror/commands';
17 import {
18 bracketMatching,
19 defaultHighlightStyle,
20 syntaxHighlighting
21 } from '@codemirror/language';
22 import { javascript } from '@codemirror/lang-javascript';
23 import { json } from '@codemirror/lang-json';
24 import { css } from '@codemirror/lang-css';
25 import { html } from '@codemirror/lang-html';
26 import { search, searchKeymap, openSearchPanel } from '@codemirror/search';
27
28 function languageFor(path: string) {
29 if (/\.(jsx?|tsx?|mjs|cjs)$/i.test(path))
30 return javascript({
31 typescript: /\.tsx?$/i.test(path),
32 jsx: /\.[jt]sx$/i.test(path)
33 });
34 if (/\.json$/i.test(path)) return json();
35 if (/\.css$/i.test(path)) return css();
36 if (/\.(html?|svg)$/i.test(path)) return html();
37 return [];
38 }
39 export function CodeEditor({
40 value,
41 path,
42 readOnly,
43 fontSize,
44 onChange
45 }: {
46 value: string;
47 path: string;
48 readOnly: boolean;
49 fontSize: number;
50 onChange: (value: string) => void;
51 }) {
52 const host = useRef<HTMLDivElement>(null);
53 const editor = useRef<EditorView | null>(null);
54 const callback = useRef(onChange);
55 const editable = useRef(new Compartment());
56 const typography = useRef(new Compartment());
57 callback.current = onChange;
58 useEffect(() => {
59 if (!host.current) return;
60 const nonce =
61 document.querySelector<HTMLMetaElement>('meta[name="csp-nonce"]')
62 ?.content || '';
63 const view = new EditorView({
64 parent: host.current,
65 state: EditorState.create({
66 doc: value,
67 extensions: [
68 EditorView.cspNonce.of(nonce),
69 EditorState.phrases.of({
70 'Find': 'جست‌وجو',
71 'Replace': 'جایگزین',
72 'next': 'بعدی',
73 'previous': 'قبلی',
74 'all': 'همه',
75 'match case': 'حساس به حروف',
76 'regexp': 'عبارت منظم',
77 'by word': 'واژه کامل',
78 'replace': 'جایگزینی',
79 'replace all': 'جایگزینی همه',
80 'close': 'بستن',
81 'Go to line': 'رفتن به خط',
82 'go': 'برو',
83 'current match': 'نتیجه فعلی',
84 'on line': 'در خط',
85 'replaced match on line $': 'نتیجه خط $ جایگزین شد',
86 'replaced $ matches': '$ نتیجه جایگزین شد'
87 }),
88 lineNumbers(),
89 highlightActiveLineGutter(),
90 highlightActiveLine(),
91 drawSelection(),
92 history(),
93 bracketMatching(),
94 search({ top: true }),
95 syntaxHighlighting(defaultHighlightStyle),
96 languageFor(path),
97 keymap.of([
98 indentWithTab,
99 ...defaultKeymap,
100 ...historyKeymap,
101 ...searchKeymap
102 ]),
103 EditorView.contentAttributes.of({
104 'aria-label': `ویرایش ${path}`,
105 'dir': 'ltr',
106 'spellcheck': 'false',
107 'autocapitalize': 'off',
108 'autocomplete': 'off'
109 }),
110 editable.current.of(EditorState.readOnly.of(readOnly)),
111 typography.current.of(
112 EditorView.theme({ '&': { fontSize: `${fontSize}px` } })
113 ),
114 EditorView.theme(
115 {
116 '&': {
117 color: 'var(--text)',
118 backgroundColor: 'var(--page)',
119 height: '100%',
120 direction: 'ltr'
121 },
122 '.cm-content': {
123 fontFamily:
124 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
125 padding: '18px 0',
126 minHeight: '320px',
127 caretColor: 'var(--accent)'
128 },
129 '.cm-scroller': { overflow: 'auto', lineHeight: '1.8' },
130 '.cm-gutters': {
131 color: 'var(--muted)',
132 backgroundColor: 'var(--page)',
133 borderRight: '1px solid var(--line)'
134 },
135 '.cm-activeLine, .cm-activeLineGutter': {
136 backgroundColor: 'var(--soft)'
137 },
138 '.cm-selectionBackground, &.cm-focused .cm-selectionBackground': {
139 backgroundColor: '#305a6e80'
140 },
141 '.cm-cursor': { borderLeftColor: 'var(--accent)' },
142 '.cm-panels': {
143 backgroundColor: 'var(--surface)',
144 color: 'var(--text)'
145 },
146 '.cm-search input': {
147 color: 'var(--text)',
148 backgroundColor: 'var(--page)',
149 border: '1px solid var(--line)'
150 }
151 },
152 { dark: true }
153 ),
154 EditorView.updateListener.of((update) => {
155 if (update.docChanged)
156 callback.current(update.state.doc.toString());
157 })
158 ]
159 })
160 });
161 editor.current = view;
162 return () => {
163 editor.current = null;
164 view.destroy();
165 };
166 // A tab owns its own history. Value changes are applied without recreating the editor.
167 }, [path]);
168 useEffect(() => {
169 const view = editor.current;
170 if (view && view.state.doc.toString() !== value)
171 view.dispatch({
172 changes: { from: 0, to: view.state.doc.length, insert: value }
173 });
174 }, [value]);
175 useEffect(() => {
176 editor.current?.dispatch({
177 effects: [
178 editable.current.reconfigure(EditorState.readOnly.of(readOnly)),
179 typography.current.reconfigure(
180 EditorView.theme({ '&': { fontSize: `${fontSize}px` } })
181 )
182 ]
183 });
184 }, [readOnly, fontSize]);
185 return <div className="editor-host" ref={host} />;
186 }
187 export function searchEditor() {
188 const element = document.querySelector<HTMLElement>('.cm-editor');
189 const view = element ? EditorView.findFromDOM(element) : null;
190 if (view) openSearchPanel(view);
191 }