Follow-ups to Phase E (ADR-15): - Recordings over 25 MB are cut into pieces and transcribed in turn: with ffmpeg (now in the server and all-in-one images) any format is re-encoded to 16 kHz mono MP3 in 20-minute pieces; without it, MP3 is cut at frame boundaries and WAV at sample boundaries in Go. - PDFs without a text layer are written out by a document-capable chat model (Anthropic document input, OpenAI file input); Ollama is skipped. - Phone app: AI answers render as Markdown with tappable citations; the editing toolbar opens the writing actions on the selection; audio files get Transcribe. Editor bridge protocol 4 adds setAi, getSelection, insertMarkdown and transcribe/transcribed. - The transcript HTML builder moves to @docwell/editor, shared by the web app and the phone editor. - An integration test runs chat, Transcribe and writing actions on a device against a live server; the editor test no longer expects headings to have only a level. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
250 lines
8.2 KiB
Dart
250 lines
8.2 KiB
Dart
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart' show Factory;
|
|
import 'package:flutter/gestures.dart'
|
|
show EagerGestureRecognizer, OneSequenceGestureRecognizer;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart' show rootBundle;
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
import 'package:webview_flutter_android/webview_flutter_android.dart'
|
|
show AndroidWebViewController, MixedContentMode;
|
|
|
|
import 'bridge.dart';
|
|
import '../l10n.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].
|
|
///
|
|
/// The page's address is https, so Android blocks a plain http [server]
|
|
/// (its ws:// collab socket and its images) as mixed content; a server the
|
|
/// person signed in to over http is allowed explicitly.
|
|
Future<void> loadEditorPage(WebViewController web, {String? server}) async {
|
|
final android = web.platform;
|
|
if (android is AndroidWebViewController &&
|
|
server != null &&
|
|
server.startsWith('http://')) {
|
|
await android.setMixedContentMode(MixedContentMode.alwaysAllow);
|
|
}
|
|
final html = await rootBundle.loadString(editorAsset);
|
|
await web.loadHtmlString(html, baseUrl: editorBaseUrl);
|
|
}
|
|
|
|
/// The editor's WebView. It takes every touch at once: otherwise Flutter's
|
|
/// gesture arena cancels a press held past the long-press delay, and the
|
|
/// page could not drag records with a finger (touchdrag.ts).
|
|
Widget editorView(WebViewController web) => WebViewWidget(
|
|
controller: web,
|
|
gestureRecognizers: {
|
|
Factory<OneSequenceGestureRecognizer>(EagerGestureRecognizer.new),
|
|
},
|
|
);
|
|
|
|
/// 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 EditorBackHandler(
|
|
bridge: _bridge,
|
|
child: 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: [
|
|
editorView(_web),
|
|
if (!_bridge.isReady)
|
|
const Center(child: CircularProgressIndicator()),
|
|
],
|
|
),
|
|
),
|
|
EditorToolbar(bridge: _bridge),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Lets the back button close a panel or menu the editor shows (a database
|
|
/// record, a property picker) before it leaves the screen.
|
|
class EditorBackHandler extends StatelessWidget {
|
|
const EditorBackHandler({
|
|
super.key,
|
|
required this.bridge,
|
|
required this.child,
|
|
});
|
|
|
|
final EditorBridge bridge;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
listenable: bridge,
|
|
builder: (context, child) => PopScope(
|
|
canPop: !bridge.overlayOpen,
|
|
onPopInvokedWithResult: (didPop, _) {
|
|
if (!didPop) unawaited(bridge.dismiss());
|
|
},
|
|
child: child!,
|
|
),
|
|
child: child,
|
|
);
|
|
}
|
|
|
|
/// Formatting buttons above the keyboard, lit up from the page's state.
|
|
class EditorToolbar extends StatelessWidget {
|
|
const EditorToolbar({super.key, required this.bridge, this.onAi});
|
|
|
|
final EditorBridge bridge;
|
|
|
|
/// Opens the AI writing actions; without it there is no AI button.
|
|
final VoidCallback? onAi;
|
|
|
|
// A getter, so the labels follow the language.
|
|
static List<(EditorCommand, IconData, String)> get _buttons => [
|
|
(EditorCommand.bold, Icons.format_bold, tr('bold')),
|
|
(EditorCommand.italic, Icons.format_italic, tr('italic')),
|
|
(EditorCommand.underline, Icons.format_underline, tr('underline')),
|
|
(EditorCommand.strike, Icons.format_strikethrough, tr('strike')),
|
|
(EditorCommand.heading1, Icons.title, tr('heading')),
|
|
(EditorCommand.bulletList, Icons.format_list_bulleted, tr('bulletList')),
|
|
(EditorCommand.orderedList, Icons.format_list_numbered, tr('orderedList')),
|
|
(EditorCommand.taskList, Icons.checklist, tr('taskList')),
|
|
(EditorCommand.callout, Icons.lightbulb_outline, tr('callout')),
|
|
(EditorCommand.table, Icons.table_chart_outlined, tr('table')),
|
|
(EditorCommand.blockquote, Icons.format_quote, tr('quote')),
|
|
(EditorCommand.codeBlock, Icons.code, tr('codeBlock')),
|
|
];
|
|
|
|
@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: [
|
|
if (onAi != null)
|
|
IconButton(
|
|
key: const Key('tool-ai'),
|
|
tooltip: tr('aiWriteTitle'),
|
|
icon: const Icon(Icons.auto_awesome_outlined),
|
|
onPressed: bridge.isReady ? onAi : null,
|
|
),
|
|
IconButton(
|
|
tooltip: tr('undo'),
|
|
icon: const Icon(Icons.undo),
|
|
onPressed: state.canUndo
|
|
? () => bridge.run(EditorCommand.undo)
|
|
: null,
|
|
),
|
|
IconButton(
|
|
tooltip: tr('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,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|