Phase F follow-ups (ADR-16): - Calendars: events are now stored in calendar_events by a calendars.sync job (every 15 minutes per account, 90 days back and 400 ahead), so journals open at once and keep showing events while a source is down. Google accounts connect with OAuth (calendar.readonly) through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access. - OneNote: whole notebooks import through Microsoft Graph (Notes.Read, DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with section groups, sections, subpages, pictures and attached files. The Microsoft sign-in lives in memory for an hour and is never stored. - Readwise: an hourly sync with the person's sealed access token; new books get a page under "Readwise", new highlights are appended once. New readwise_syncs collection. - internal/oauth: signed states and one callback route per provider, routed by purpose. directory gains ImportConverted and CallerOf. - Phone app: "Today's journal" on the home screen and calendar events on journal pages. - Web clipper: `npm run package` builds Chrome/Edge, Firefox and source zips; store/ holds listing texts, permission notes and a privacy policy. - OpenAPI, generated clients, translations, compose and .env examples, and user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
364 lines
11 KiB
Dart
364 lines
11 KiB
Dart
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart' hide Notification, Page;
|
|
import 'package:webview_flutter/webview_flutter.dart';
|
|
|
|
import '../api/docwell_api.dart';
|
|
import '../app.dart';
|
|
import '../editor/bridge.dart';
|
|
import '../editor/editor_screen.dart'
|
|
show
|
|
EditorBackHandler,
|
|
EditorToolbar,
|
|
editorNavigation,
|
|
editorView,
|
|
loadEditorPage;
|
|
import '../editor/ai_write_sheet.dart';
|
|
import 'ai_chat_screen.dart';
|
|
import 'comments_screen.dart';
|
|
import 'journal_events.dart';
|
|
import 'widgets.dart';
|
|
import '../l10n.dart';
|
|
|
|
/// A page in the shared editor (ADR-4). It opens read-only from the last
|
|
/// saved content, which is kept for reading offline; Edit joins the live
|
|
/// document so changes sync with everyone else on the page.
|
|
class PageScreen extends StatefulWidget {
|
|
const PageScreen({
|
|
super.key,
|
|
required this.session,
|
|
required this.pageId,
|
|
this.startEditing = false,
|
|
this.openComment,
|
|
this.target,
|
|
});
|
|
|
|
final AppSession session;
|
|
final String pageId;
|
|
final bool startEditing;
|
|
|
|
/// Opens the comments on this thread (from a notification).
|
|
final String? openComment;
|
|
|
|
/// A block (`#b-…`) or database record (`#r-…`) to show, e.g. a search hit.
|
|
final String? target;
|
|
|
|
@override
|
|
State<PageScreen> createState() => _PageScreenState();
|
|
}
|
|
|
|
class _PageScreenState extends State<PageScreen> {
|
|
DocwellApi get _api => widget.session.api;
|
|
final _web = WebViewController();
|
|
late final EditorBridge _bridge = EditorBridge(evaluate: _web.runJavaScript);
|
|
StreamSubscription<String>? _links;
|
|
|
|
Page? _page;
|
|
// The page's workspace has an AI chat (and so writing actions).
|
|
bool _ai = false;
|
|
Map<String, dynamic>? _content;
|
|
String? _error;
|
|
bool _offline = false;
|
|
bool _editing = false;
|
|
bool _connected = false;
|
|
|
|
/// Showing the page as its edgeless canvas.
|
|
bool _canvas = false;
|
|
|
|
bool get _canEdit {
|
|
final p = _page;
|
|
return p != null &&
|
|
!p.inTrash &&
|
|
!p.archived &&
|
|
p.role.canEdit &&
|
|
!_offline;
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_bridge.addListener(() {
|
|
if (mounted) setState(() {});
|
|
});
|
|
_links = _bridge.navigations.listen(_follow);
|
|
_web
|
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
|
..setBackgroundColor(Colors.transparent)
|
|
..addJavaScriptChannel(
|
|
'DocwellNative',
|
|
onMessageReceived: (m) => _bridge.handleMessage(m.message),
|
|
)
|
|
..setNavigationDelegate(
|
|
NavigationDelegate(
|
|
onPageFinished: (_) async {
|
|
if (!mounted) return;
|
|
await _bridge.setTheme(
|
|
dark: Theme.of(context).brightness == Brightness.dark,
|
|
);
|
|
await _bridge.hello();
|
|
},
|
|
// The page is bundled; links open nowhere (page links come
|
|
// through the bridge as navigate messages).
|
|
onNavigationRequest: editorNavigation,
|
|
),
|
|
);
|
|
unawaited(loadEditorPage(_web, server: _api.baseUrl));
|
|
_bridge.onTranscribe = (id, src, _) =>
|
|
transcribeForEditor(_api, _bridge, id, src);
|
|
_load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
try {
|
|
final page = await _api.page(widget.pageId);
|
|
var offline = _api.offline;
|
|
final content = await _api.content(widget.pageId);
|
|
offline |= _api.offline;
|
|
if (!offline) unawaited(_api.visit(widget.pageId).catchError((_) {}));
|
|
if (!offline) {
|
|
unawaited(
|
|
_api
|
|
.aiAvailability(page.workspace)
|
|
.then((a) async {
|
|
if (!mounted) return;
|
|
setState(() => _ai = a.chat);
|
|
await _bridge.ready;
|
|
await _bridge.setAi(
|
|
transcribe: a.transcribe,
|
|
words: {
|
|
'transcribe': tr('aiTranscribe'),
|
|
'transcribeHint': tr('aiTranscribeHint'),
|
|
'transcribing': tr('aiTranscribing'),
|
|
'transcribeFailed': tr('aiTranscribeFailed'),
|
|
'summaryTitle': tr('aiSummaryOf'),
|
|
'transcript': tr('aiTranscript'),
|
|
},
|
|
);
|
|
})
|
|
.catchError((_) {}),
|
|
);
|
|
}
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_page = page;
|
|
_content = content.content;
|
|
_offline = offline;
|
|
_error = null;
|
|
_canvas = page.defaultMode == PageMode.edgeless;
|
|
});
|
|
await _bridge.ready;
|
|
await _bridge.setLayout(
|
|
fullWidth: page.fullWidth,
|
|
font: page.font.toJson(),
|
|
);
|
|
if (!_connected) {
|
|
await _bridge.setEditable(false);
|
|
await _bridge.setContent(_content);
|
|
}
|
|
if (_canvas) await _bridge.setMode('edgeless', title: _pageTitle);
|
|
if (widget.target case final target?) await _bridge.follow(target);
|
|
if (widget.startEditing && _canEdit && !_editing) await _edit();
|
|
if (widget.openComment != null && mounted) _comments();
|
|
} catch (e) {
|
|
if (mounted) setState(() => _error = describeError(e));
|
|
}
|
|
}
|
|
|
|
Future<void> _edit() async {
|
|
final token = _api.token;
|
|
if (token == null) return;
|
|
final user = widget.session.user;
|
|
if (!_connected) {
|
|
await _bridge.connect(
|
|
url: _api.collabUrl,
|
|
page: widget.pageId,
|
|
token: token,
|
|
userName: user.name.isEmpty ? user.email : user.name,
|
|
userColor: userColor(user.id),
|
|
);
|
|
_connected = true;
|
|
}
|
|
await _bridge.setEditable(true);
|
|
// On the canvas, people pick what to change; the text has no cursor to put.
|
|
if (!_canvas) await _bridge.focus();
|
|
setState(() => _editing = true);
|
|
}
|
|
|
|
Future<void> _done() async {
|
|
await _bridge.setEditable(false);
|
|
setState(() => _editing = false);
|
|
}
|
|
|
|
/// The title shown on the page's note on the canvas.
|
|
String get _pageTitle {
|
|
final t = _page?.title ?? '';
|
|
return t.isEmpty ? tr('untitled') : t;
|
|
}
|
|
|
|
Future<void> _toggleCanvas() async {
|
|
final canvas = !_canvas;
|
|
await _bridge.setMode(canvas ? 'edgeless' : 'page', title: _pageTitle);
|
|
setState(() => _canvas = canvas);
|
|
}
|
|
|
|
void _follow(String route) {
|
|
final m = RegExp(r'^/p/([A-Za-z0-9_-]+)').firstMatch(route);
|
|
if (m == null) return;
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => PageScreen(session: widget.session, pageId: m[1]!),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _comments() {
|
|
final page = _page;
|
|
if (page == null) return;
|
|
Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => CommentsScreen(
|
|
session: widget.session,
|
|
page: page,
|
|
highlight: widget.openComment,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// Keep the offline copy current with what was just edited.
|
|
if (_connected) {
|
|
unawaited(_api.content(widget.pageId).then((_) {}, onError: (_) {}));
|
|
}
|
|
_links?.cancel();
|
|
_bridge.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String? get _status => switch (_bridge.collabStatus) {
|
|
'offline' => tr('reconnecting'),
|
|
'denied' => tr('cantEdit'),
|
|
'connecting' => tr('connecting'),
|
|
_ => null,
|
|
};
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final page = _page;
|
|
final title = page == null
|
|
? ''
|
|
: '${page.icon.isEmpty ? '' : '${page.icon} '}${page.title.isEmpty ? tr('untitled') : page.title}';
|
|
return EditorBackHandler(
|
|
bridge: _bridge,
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(title, overflow: TextOverflow.ellipsis),
|
|
actions: [
|
|
if (page != null)
|
|
IconButton(
|
|
key: const Key('canvas'),
|
|
tooltip: _canvas ? tr('showAsPage') : tr('showAsCanvas'),
|
|
icon: Icon(
|
|
_canvas ? Icons.article_outlined : Icons.dashboard_outlined,
|
|
),
|
|
onPressed: _bridge.isReady ? _toggleCanvas : null,
|
|
),
|
|
if (page != null && !_offline && _ai)
|
|
IconButton(
|
|
key: const Key('ai-chat'),
|
|
tooltip: tr('aiChat'),
|
|
icon: const Icon(Icons.auto_awesome_outlined),
|
|
onPressed: () => Navigator.of(context).push(
|
|
MaterialPageRoute<void>(
|
|
builder: (_) => AiChatScreen(
|
|
session: widget.session,
|
|
workspace: page.workspace,
|
|
page: page.id,
|
|
pageTitle: page.title,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (page != null && !_offline)
|
|
IconButton(
|
|
tooltip: tr('comments'),
|
|
icon: const Icon(Icons.mode_comment_outlined),
|
|
onPressed: _comments,
|
|
),
|
|
if (_editing)
|
|
TextButton(onPressed: _done, child: Text(tr('done')))
|
|
else if (_canEdit)
|
|
IconButton(
|
|
key: const Key('edit'),
|
|
tooltip: tr('edit'),
|
|
icon: const Icon(Icons.edit_outlined),
|
|
onPressed: _bridge.isReady ? _edit : null,
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
if (_offline) const OfflineBanner(),
|
|
if (page?.archived == true)
|
|
MaterialBanner(
|
|
content: Text(tr('archived')),
|
|
actions: [SizedBox.shrink()],
|
|
),
|
|
if (_editing && _status != null)
|
|
MaterialBanner(
|
|
content: Text(_status!),
|
|
actions: const [SizedBox.shrink()],
|
|
),
|
|
if (_bridge.error != null)
|
|
MaterialBanner(
|
|
content: Text(_bridge.error!),
|
|
actions: const [SizedBox.shrink()],
|
|
),
|
|
if (page != null &&
|
|
page.journalDate.isNotEmpty &&
|
|
!_offline &&
|
|
!_editing)
|
|
JournalEvents(api: _api, date: page.journalDate),
|
|
if (_error != null)
|
|
Expanded(
|
|
child: ErrorTile(message: _error!, onRetry: _load),
|
|
)
|
|
else
|
|
Expanded(
|
|
child: Stack(
|
|
children: [
|
|
editorView(_web),
|
|
if (!_bridge.isReady || page == null)
|
|
const Center(child: CircularProgressIndicator())
|
|
// The live document replaces the saved copy once it has
|
|
// synced; until then the editor would look empty.
|
|
else if (_connected && _bridge.collabStatus != 'connected')
|
|
ColoredBox(
|
|
color: Theme.of(context).colorScheme.surface,
|
|
child: const Center(child: CircularProgressIndicator()),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (_editing)
|
|
EditorToolbar(
|
|
bridge: _bridge,
|
|
onAi: _ai && page != null
|
|
? () => AiWriteSheet.open(
|
|
context,
|
|
api: _api,
|
|
bridge: _bridge,
|
|
workspace: page.workspace,
|
|
)
|
|
: null,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|