// 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? content; final ValueChanged>? onChanged; /// Hands out the bridge once the WebView exists (tests drive it). final ValueChanged? onBridge; @override State createState() => _EditorScreenState(); } class _EditorScreenState extends State { 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, ), ], ), ), ), ); } }