diff --git a/src/components/git/code-editor.tsx b/src/components/git/code-editor.tsx new file mode 100644 index 0000000..3b44e2f --- /dev/null +++ b/src/components/git/code-editor.tsx @@ -0,0 +1,171 @@ +import { useMemo, useState } from 'react'; +import { StyleSheet } from 'react-native'; +import { WebView } from 'react-native-webview'; + +import { useTheme } from '@/theme/theme-provider'; + +/** Minimal keyword sets for the languages we highlight in the WebView editor. */ +const KEYWORDS: Record = { + javascript: ['const', 'let', 'var', 'function', 'return', 'if', 'else', 'for', 'while', 'import', 'from', 'export', 'class', 'new', 'await', 'async', 'try', 'catch', 'throw', 'typeof', 'interface', 'type', 'extends', 'implements', 'public', 'private', 'void', 'null', 'true', 'false'], + python: ['def', 'return', 'if', 'elif', 'else', 'for', 'while', 'import', 'from', 'class', 'try', 'except', 'raise', 'with', 'as', 'lambda', 'None', 'True', 'False', 'and', 'or', 'not', 'in', 'is', 'async', 'await'], + go: ['package', 'import', 'func', 'return', 'if', 'else', 'for', 'range', 'var', 'const', 'type', 'struct', 'interface', 'map', 'chan', 'go', 'defer', 'nil', 'true', 'false', 'switch', 'case'], + ruby: ['def', 'end', 'if', 'else', 'elsif', 'unless', 'while', 'do', 'class', 'module', 'require', 'return', 'nil', 'true', 'false', 'yield', 'begin', 'rescue'], + rust: ['fn', 'let', 'mut', 'pub', 'use', 'struct', 'enum', 'impl', 'trait', 'return', 'if', 'else', 'for', 'while', 'match', 'self', 'true', 'false', 'async', 'await'], + java: ['class', 'public', 'private', 'protected', 'static', 'void', 'return', 'if', 'else', 'for', 'while', 'new', 'import', 'package', 'interface', 'extends', 'implements', 'try', 'catch', 'throw', 'null', 'true', 'false'], +}; + +const EXT_LANGUAGE: Record = { + js: 'javascript', + jsx: 'javascript', + ts: 'javascript', + tsx: 'javascript', + mjs: 'javascript', + cjs: 'javascript', + py: 'python', + go: 'go', + rb: 'ruby', + rs: 'rust', + java: 'java', + kt: 'java', + swift: 'java', + c: 'java', + h: 'java', + cpp: 'java', + hpp: 'java', + cs: 'java', + php: 'java', + json: 'javascript', + yml: 'python', + yaml: 'python', + toml: 'python', + sh: 'python', + bash: 'python', +}; + +export function languageForPath(path: string): string { + const ext = path.split('.').pop()?.toLowerCase() ?? ''; + return EXT_LANGUAGE[ext] ?? 'plain'; +} + +export interface CodeEditorProps { + /** Initial content — the WebView owns the text while editing. */ + value: string; + onChangeText: (value: string) => void; + language?: string; + editable?: boolean; +} + +function escapeHtml(value: string): string { + return value.replace(/&/g, '&').replace(//g, '>'); +} + +function buildHtml(value: string, language: string, editable: boolean, c: { + bg: string; + fg: string; + gutter: string; + keyword: string; + string: string; + comment: string; + number: string; + caret: string; +}): string { + const keywords = KEYWORDS[language] ?? []; + return ` + + +
+
+
+

+    
+  
+
+`; +} + +/** + * A self-contained WebView code editor with line numbers and lightweight syntax + * highlighting. It runs entirely from an inline HTML document (no remote code + * and no bundled editor assets), keeping the security surface minimal. + */ +export function CodeEditor({ value, onChangeText, language = 'plain', editable = true }: CodeEditorProps) { + const { colors } = useTheme(); + // The WebView owns the text while editing, so the HTML is built from the + // initial value only; callers remount the component (via `key`) to load a + // different file. + const [initialValue] = useState(value); + const html = useMemo( + () => + buildHtml(initialValue, language, editable, { + bg: colors.codeBackground, + fg: colors.text, + gutter: colors.textTertiary, + keyword: colors.primary, + string: colors.success, + comment: colors.textTertiary, + number: colors.info, + caret: colors.primary, + }), + // eslint-disable-next-line react-hooks/exhaustive-deps + [language, editable, colors.codeBackground, colors.text], + ); + + return ( + { + try { + const payload = JSON.parse(event.nativeEvent.data) as { type: string; value?: string }; + if (payload.type === 'change' && typeof payload.value === 'string') { + onChangeText(payload.value); + } + } catch { + // Ignore malformed messages from the WebView. + } + }} + keyboardDisplayRequiresUserAction={false} + hideKeyboardAccessoryView + setSupportMultipleWindows={false} + /> + ); +} + +const styles = StyleSheet.create({ + webview: { flex: 1, backgroundColor: 'transparent' }, +}); diff --git a/src/components/git/diff.tsx b/src/components/git/diff.tsx index 3d6902d..85ef972 100644 --- a/src/components/git/diff.tsx +++ b/src/components/git/diff.tsx @@ -6,7 +6,7 @@ * with the theme's diff colors. `parseDiff` is pure and unit-tested. */ -import { ScrollView, StyleSheet, Text, View } from 'react-native'; +import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'; import { AppText } from '@/components/ui/app-text'; import { useTheme } from '@/theme/theme-provider'; @@ -106,7 +106,7 @@ export function parseDiff(raw: string): DiffFile[] { return files; } -function DiffLineRow({ line }: { line: DiffLine }) { +function DiffLineRow({ line, onPress }: { line: DiffLine; onPress?: () => void }) { const { colors } = useTheme(); const style = { add: { backgroundColor: colors.diffAdd, color: colors.diffAddText, sign: '+' }, @@ -116,7 +116,7 @@ function DiffLineRow({ line }: { line: DiffLine }) { meta: { backgroundColor: 'transparent', color: colors.textTertiary, sign: '' }, }[line.type]; - return ( + const row = ( {style.sign} @@ -124,6 +124,13 @@ function DiffLineRow({ line }: { line: DiffLine }) { ); + + if (!onPress) return row; + return ( + + {row} + + ); } export interface DiffViewProps { @@ -131,10 +138,15 @@ export interface DiffViewProps { /** When set, only the matching file's hunks are rendered. */ pathFilter?: string; emptyLabel: string; + /** + * Called with the 1-based position of a tapped line within its file's diff + * (Gitea's `new_position`) — used to attach an inline review comment. + */ + onLinePress?: (filePath: string, position: number) => void; } /** Horizontally scrollable, color-coded unified diff. Place inside a non-scroll Screen. */ -export function DiffView({ raw, pathFilter, emptyLabel }: DiffViewProps) { +export function DiffView({ raw, pathFilter, emptyLabel, onLinePress }: DiffViewProps) { const files = parseDiff(raw).filter((file) => !pathFilter || file.path === pathFilter); if (files.length === 0) { @@ -161,7 +173,13 @@ export function DiffView({ raw, pathFilter, emptyLabel }: DiffViewProps) { {file.lines.map((line, index) => ( - + onLinePress(file.path, index + 1) : undefined + } + /> ))} diff --git a/src/features/repository/screens/file-edit-screen.tsx b/src/features/repository/screens/file-edit-screen.tsx index 51ce6be..cd0d0b1 100644 --- a/src/features/repository/screens/file-edit-screen.tsx +++ b/src/features/repository/screens/file-edit-screen.tsx @@ -3,6 +3,7 @@ import { Alert, StyleSheet, Switch, View } from 'react-native'; import { useTranslation } from 'react-i18next'; import { Screen } from '@/components/layout/screen'; +import { CodeEditor, languageForPath } from '@/components/git/code-editor'; import { AppText } from '@/components/ui/app-text'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; @@ -170,15 +171,13 @@ function FileEditForm({ owner, name, path, initialRef, entry, repo, branches, on ) : null} -