Files
Docwell/Phone-App/lib/editor/editor_screen.dart
T
tajniak81andClaude Opus 5.5 6b6c916825 Load the mobile editor page at an https address so YouTube embeds play
The page was loaded from a file: asset, which sends no Referer, and
YouTube's player refuses to play without one. Both editor screens now
load the same single-file page with loadHtmlString at
https://app.docwell.docwell_mobile/, the https://<application id> form
YouTube asks apps to use. Embedded players may navigate inside their own
frames; the page itself still never navigates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 15:40:36 +01:00

180 lines
5.6 KiB
Dart

// SPDX-License-Identifier: AGPL-3.0-or-later
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
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 address the editor page runs at. A file: page sends no Referer, and
/// YouTube's player refuses to play without one; YouTube asks apps to name
/// themselves as `https://<application id>` instead.
const editorBaseUrl = 'https://app.docwell.docwell_mobile/';
/// Loads the bundled editor page (one self-contained file) at [editorBaseUrl].
Future<void> loadEditorPage(WebViewController web) async {
final html = await rootBundle.loadString(editorAsset);
await web.loadHtmlString(html, baseUrl: editorBaseUrl);
}
/// The page itself never navigates; embedded players may load in their frames.
NavigationDecision editorNavigation(NavigationRequest req) =>
!req.isMainFrame || req.url.startsWith(editorBaseUrl)
? NavigationDecision.navigate
: NavigationDecision.prevent;
/// 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: editorNavigation,
),
);
unawaited(loadEditorPage(_web));
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()),
],
),
),
EditorToolbar(bridge: _bridge),
],
),
);
}
}
/// Formatting buttons above the keyboard, lit up from the page's state.
class EditorToolbar extends StatelessWidget {
const EditorToolbar({super.key, 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,
),
],
),
),
),
);
}
}