Files
Docwell/Phone-App/lib/editor/editor_screen.dart
T
tajniak81andClaude Opus 5.5 d28f1e02db Add tables, to-dos, callouts and the rest of editor parity
The editor gains the blocks and formatting it lacked next to AFFiNE:

- Tables with a header row, resizable columns, a toolbar to add, delete,
  merge and split, and rows that move by a drag grip, toolbar buttons or
  Ctrl+Alt+Up/Down.
- To-do lists (also from "[] "), callouts with an emoji and colour, and
  headings down to level 6 (Ctrl+Shift+0-6 for text and headings).
- Text colour and highlights from a fixed palette that follows the theme,
  and left, centre and right alignment.
- Optional line numbers on code blocks, with a default hosts can set.
- PDF attachments show their pages inline; pdf.js loads only then, and
  the mobile bundle leaves it out.
- The selection toolbar adds underline, colour, align, inline math, make a
  linked page and copy block link. A grip beside each block drags it and
  opens a menu: turn into, move, duplicate, copy, copy link, delete.
- The slash menu adds Today, Tomorrow, Yesterday, Now and New page.

The web app translates the new words and makes linked pages as children
of the open page. The mobile bridge gets underline, to-do, callout and
table commands, so its protocol moves to version 2 and the bundle is
rebuilt.

docwell-server's converters learn the new nodes: Markdown writes task
lists, GitHub alerts and tables and reads them back; HTML keeps colours
and alignment and reads its own output back; PDF draws tables, to-dos,
callouts and coloured text; Word import keeps tables, alignment, H4-H6
and colours; Notion import turns databases into tables and keeps
callouts and to-dos. Imported tables and H4-H6 headings no longer
collapse into paragraphs and H3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 12:20:08 +01:00

184 lines
5.8 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.underline, Icons.format_underline, 'Underline'),
(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.taskList, Icons.checklist, 'To-do list'),
(EditorCommand.callout, Icons.lightbulb_outline, 'Callout'),
(EditorCommand.table, Icons.table_chart_outlined, 'Table'),
(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,
),
],
),
),
),
);
}
}