Files
Docwell/Phone-App/lib/editor/editor_screen.dart
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

162 lines
4.8 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'bridge.dart';
/// Where Packages/editor-bridge copies its build (`npm run build -w @docwell/editor-bridge`).
const editorAsset = 'assets/editor/index.html';
/// The JavaScript channel the page posts to (see transport.ts).
const _channel = 'DocwellNative';
/// The shared Tiptap editor in a WebView, with a native toolbar (ADR-4).
class EditorScreen extends StatefulWidget {
const EditorScreen({
super.key,
required this.title,
this.content,
this.onChanged,
this.onBridge,
});
final String title;
final Map<String, dynamic>? content;
final ValueChanged<Map<String, dynamic>>? onChanged;
/// Hands out the bridge once the WebView exists (tests drive it).
final ValueChanged<EditorBridge>? onBridge;
@override
State<EditorScreen> createState() => _EditorScreenState();
}
class _EditorScreenState extends State<EditorScreen> {
final _web = WebViewController();
late final EditorBridge _bridge = EditorBridge(
content: widget.content,
evaluate: _web.runJavaScript,
);
@override
void initState() {
super.initState();
_bridge.addListener(() => setState(() {}));
_bridge.changes.listen((c) => widget.onChanged?.call(c));
_web
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(Colors.transparent)
..addJavaScriptChannel(
_channel,
onMessageReceived: (m) => _bridge.handleMessage(m.message),
)
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (_) async {
if (!mounted) return;
final dark = Theme.of(context).brightness == Brightness.dark;
await _bridge.setTheme(dark: dark);
await _bridge.hello();
},
// The page is bundled and never navigates; links open nowhere.
onNavigationRequest: (req) => req.url.startsWith('file:')
? NavigationDecision.navigate
: NavigationDecision.prevent,
),
)
..loadFlutterAsset(editorAsset);
widget.onBridge?.call(_bridge);
}
@override
void dispose() {
_bridge.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Column(
children: [
if (_bridge.error != null)
MaterialBanner(
content: Text(_bridge.error!),
actions: const [SizedBox.shrink()],
),
Expanded(
child: Stack(
children: [
WebViewWidget(controller: _web),
if (!_bridge.isReady)
const Center(child: CircularProgressIndicator()),
],
),
),
_Toolbar(bridge: _bridge),
],
),
);
}
}
/// Formatting buttons above the keyboard, lit up from the page's state.
class _Toolbar extends StatelessWidget {
const _Toolbar({required this.bridge});
final EditorBridge bridge;
static const _buttons = [
(EditorCommand.bold, Icons.format_bold, 'Bold'),
(EditorCommand.italic, Icons.format_italic, 'Italic'),
(EditorCommand.strike, Icons.format_strikethrough, 'Strikethrough'),
(EditorCommand.heading1, Icons.title, 'Heading'),
(EditorCommand.bulletList, Icons.format_list_bulleted, 'Bulleted list'),
(EditorCommand.orderedList, Icons.format_list_numbered, 'Numbered list'),
(EditorCommand.blockquote, Icons.format_quote, 'Quote'),
(EditorCommand.codeBlock, Icons.code, 'Code block'),
];
@override
Widget build(BuildContext context) {
final state = bridge.state;
return Material(
elevation: 2,
child: SafeArea(
top: false,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
IconButton(
tooltip: 'Undo',
icon: const Icon(Icons.undo),
onPressed: state.canUndo
? () => bridge.run(EditorCommand.undo)
: null,
),
IconButton(
tooltip: 'Redo',
icon: const Icon(Icons.redo),
onPressed: state.canRedo
? () => bridge.run(EditorCommand.redo)
: null,
),
for (final (command, icon, label) in _buttons)
IconButton(
key: Key('tool-${command.name}'),
tooltip: label,
icon: Icon(icon),
isSelected: state.active.contains(command),
onPressed: bridge.isReady ? () => bridge.run(command) : null,
),
],
),
),
),
);
}
}