diff --git a/android/app/src/main/kotlin/com/kapybara/kapynotes/RichClipboard.kt b/android/app/src/main/kotlin/com/kapybara/kapynotes/RichClipboard.kt
index 0a13ca5..028ca7b 100644
--- a/android/app/src/main/kotlin/com/kapybara/kapynotes/RichClipboard.kt
+++ b/android/app/src/main/kotlin/com/kapybara/kapynotes/RichClipboard.kt
@@ -7,12 +7,14 @@ import android.content.Context
import android.net.Uri
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
+import java.io.ByteArrayOutputStream
import java.io.File
import kotlin.text.Charsets.UTF_8
/** Publishes one selection as text, HTML, and a native image at the same time. */
object RichClipboard {
private const val channelName = "kapynotes/rich_clipboard"
+ private const val MAX_IMAGE_BYTES = 25L * 1024 * 1024
fun register(engine: FlutterEngine, context: Context) {
val appContext = context.applicationContext
@@ -29,6 +31,14 @@ object RichClipboard {
}
}
+ "read" -> {
+ try {
+ result.success(read(appContext))
+ } catch (error: Exception) {
+ result.error("clipboard_read", error.message, null)
+ }
+ }
+
"readHtml" -> {
try {
result.success(readHtml(appContext))
@@ -103,6 +113,59 @@ object RichClipboard {
}
}
+ /**
+ * Everything the primary clip offers a paste, in the shape
+ * `ClipboardSnapshot.fromChannel` reads: the first item's HTML and text,
+ * and its content URI's bytes when that URI is a picture.
+ */
+ private fun read(context: Context): Map {
+ val manager = context.getSystemService(ClipboardManager::class.java)
+ val clip = manager.primaryClip ?: return emptyMap()
+ val description = clip.description
+ val snapshot = mutableMapOf(
+ "types" to List(description.mimeTypeCount) { description.getMimeType(it) },
+ )
+ if (clip.itemCount == 0) return snapshot
+ val item = clip.getItemAt(0)
+ val uri = item.uri
+ val image = uri
+ ?.let { runCatching { context.contentResolver.getType(it) }.getOrNull() }
+ ?.takeIf { it.startsWith("image/") }
+ val html = item.htmlText
+ ?: uri?.let { ClipboardImageProvider.htmlFileFor(context, it) }
+ ?.takeIf(File::isFile)
+ ?.readText(UTF_8)
+ html?.let { snapshot["html"] = it }
+ val text = if (image == null) item.coerceToText(context) else item.text
+ text?.toString()?.takeIf { it.isNotEmpty() }?.let { snapshot["text"] = it }
+ if (uri != null && image != null) {
+ readCapped(context, uri)?.let {
+ snapshot["image"] = it
+ snapshot["imageMime"] = image
+ }
+ }
+ return snapshot
+ }
+
+ /** The bytes behind [uri], or null when it can't be read or is too large to paste. */
+ private fun readCapped(context: Context, uri: Uri): ByteArray? = try {
+ context.contentResolver.openInputStream(uri)?.use { input ->
+ val output = ByteArrayOutputStream()
+ val buffer = ByteArray(64 * 1024)
+ var total = 0L
+ while (true) {
+ val read = input.read(buffer)
+ if (read < 0) break
+ total += read
+ if (total > MAX_IMAGE_BYTES) return@use null
+ output.write(buffer, 0, read)
+ }
+ output.toByteArray().takeIf { it.isNotEmpty() }
+ }
+ } catch (error: Exception) {
+ null
+ }
+
private fun readHtml(context: Context): String? {
val manager = context.getSystemService(ClipboardManager::class.java)
val clip = manager.primaryClip ?: return null
diff --git a/assets/fonts/README.md b/assets/fonts/README.md
index fecfa8a..52b027c 100644
--- a/assets/fonts/README.md
+++ b/assets/fonts/README.md
@@ -31,6 +31,16 @@ with the new ones, from the upstream files, or the change will not show.
License: [`ShantellSans-OFL.txt`](ShantellSans-OFL.txt)
+## Formula fonts
+
+Formulas written in TeX are drawn in KaTeX's fonts, which come with
+flutter_math_fork rather than living here: twenty files, about 660 KB in
+every install. They are MIT licensed by Khan Academy, and that notice ships
+as `packages/flutter_math_fork/katex_fonts/LICENSE`, listed in
+`pubspec.yaml`, since the package's own LICENSE does not carry it.
+
+Source:
+
## Brand font
The `Kapy Notes` wordmark uses Odin Rounded Bold by Frank Hemmekam.
diff --git a/benchmark/editor_typing.dart b/benchmark/editor_typing.dart
new file mode 100644
index 0000000..59b6073
--- /dev/null
+++ b/benchmark/editor_typing.dart
@@ -0,0 +1,246 @@
+// How fast does typing stay in a long markdown note?
+//
+// Five thousand lines of everything a markdown note holds — headings, lists,
+// tasks, quotes, code, tables and sums — with markdown on, typed into the way
+// a keyboard types: one editing-value update per key, about thirty a second,
+// two hundred keys at the end of the note and two hundred in the middle.
+// Then prints how long those frames took to build and to draw, and quits.
+//
+// Other sizes, plain notes and fewer keys come from defines, several sizes
+// and modes in one run:
+//
+// --dart-define=EDITOR_BENCHMARK_LINES=500,1000,5000
+// --dart-define=EDITOR_BENCHMARK_MODES=markdown,plain
+// --dart-define=EDITOR_BENCHMARK_KEYS=30
+//
+// A plain app rather than an integration test, because only profile mode
+// says anything about frame times, and Flutter 3.47.4's AOT compiler fails on
+// an entrypoint that leaves out the app's own windowing code ("Class with
+// illegal cid" in _window_macos.dart). This one keeps the app's `main`
+// reachable, never calling it, so it compiles the way the app does. Run it
+// under a bundle id of its own, never the real app's:
+//
+// FLUTTER_XCODE_PRODUCT_BUNDLE_IDENTIFIER=com.kapybara.kapynotes.typingperf \
+// flutter run --profile -d macos -t benchmark/editor_typing.dart
+//
+// The figures are the line starting EDITOR_TYPING_PERF in the log.
+import 'dart:async';
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:flutter/scheduler.dart';
+import 'package:kapy_notes/calc/engine.dart';
+import 'package:kapy_notes/calc/highlight.dart';
+import 'package:kapy_notes/core/editor_font.dart';
+import 'package:kapy_notes/core/theme.dart';
+import 'package:kapy_notes/data/local_store.dart';
+import 'package:kapy_notes/data/shortcut_prefs.dart';
+import 'package:kapy_notes/main.dart' as app;
+import 'package:kapy_notes/ui/editor/note_editor.dart';
+import 'package:material_ui/material_ui.dart';
+
+class _MemoryStore extends LocalStore {
+ _MemoryStore() : super(fileName: 'editor-typing-benchmark.json');
+
+ @override
+ Future load() async {}
+
+ @override
+ Future flush() async {}
+
+ @override
+ void put(String key, Object? value) => data[key] = value;
+}
+
+/// A long note in the shapes people write: every kind of block, over and
+/// over, [lines] lines of it.
+String longNote({int lines = 5000}) {
+ const pattern = [
+ '## Section {n}',
+ 'Plain words about item {n}, with **bold**, *italic* and a '
+ '[link](https://example.com/{n}).',
+ '- groceries {n}: 3 * 4.50',
+ '- [ ] call about invoice {n}',
+ '- [x] paid {n} = 120 + 35',
+ ' - nested detail {n}',
+ '1. first step {n}',
+ '2. second step',
+ '> a quote worth keeping {n}',
+ 'rent {n} = 1200 / 2',
+ '```dart',
+ 'final total = items.fold(0, (a, b) => a + b);',
+ '```',
+ '',
+ '| Item | Cost |',
+ '| --- | --- |',
+ '| Taxi | 14 |',
+ '',
+ 'Words with `inline code` and ~~struck~~ text and https://example.org/{n}',
+ '',
+ ];
+ return [
+ for (var i = 0; i < lines; i++)
+ pattern[i % pattern.length].replaceAll('{n}', '$i'),
+ ].join('\n');
+}
+
+/// [values] at the [p]th percentile, by nearest rank.
+double percentile(List values, double p) {
+ final sorted = [...values]..sort();
+ final rank = (p / 100 * sorted.length).ceil().clamp(1, sorted.length);
+ return sorted[rank - 1];
+}
+
+Map summary(List timings) {
+ double ms(Duration duration) => duration.inMicroseconds / 1000;
+ const budget = 1000 / 60;
+ final build = [for (final t in timings) ms(t.buildDuration)];
+ final raster = [for (final t in timings) ms(t.rasterDuration)];
+ Map stats(List values) => {
+ 'p50': percentile(values, 50),
+ 'p90': percentile(values, 90),
+ 'p99': percentile(values, 99),
+ 'max': percentile(values, 100),
+ 'over_16_7ms': values.where((value) => value > budget).length,
+ };
+ return {
+ 'frames': timings.length,
+ 'build_ms': stats(build),
+ 'raster_ms': stats(raster),
+ 'frames_over_16_7ms': [
+ for (var i = 0; i < timings.length; i++)
+ if (build[i] > budget || raster[i] > budget) i,
+ ].length,
+ };
+}
+
+void main() {
+ WidgetsFlutterBinding.ensureInitialized();
+ unawaited(_runAll());
+}
+
+Future _runAll() async {
+ const lines = String.fromEnvironment(
+ 'EDITOR_BENCHMARK_LINES',
+ defaultValue: '5000',
+ );
+ const modes = String.fromEnvironment(
+ 'EDITOR_BENCHMARK_MODES',
+ defaultValue: 'markdown',
+ );
+ final reports = [
+ for (final mode in modes.split(','))
+ for (final size in lines.split(','))
+ await _run(lines: int.parse(size), markdown: mode == 'markdown'),
+ ];
+ // ignore: avoid_print
+ print('EDITOR_TYPING_PERF ${jsonEncode(reports)}');
+ if (reports.isEmpty) await app.main();
+ exit(0);
+}
+
+void _show(String body, {required bool markdown}) {
+ final engine = CalcEngine();
+ runApp(
+ MaterialApp(
+ key: UniqueKey(),
+ theme: KapyTheme.dark(),
+ home: Scaffold(
+ body: NoteEditor(
+ noteId: 'benchmark',
+ initialBody: body,
+ engine: engine,
+ highlighter: Highlighter(engine.registry),
+ gutterWidth: 200,
+ resultsVisible: true,
+ markdownEnabled: markdown,
+ autofocus: true,
+ onDocumentChanged: (_, _, _) {},
+ onGutterWidthChanged: (_) {},
+ onResultsVisibilityChanged: (_) {},
+ onGutterWidthReset: () {},
+ onSettingsPressed: () {},
+ writingFont: WritingFont.handwritten,
+ shortcuts: ShortcutPrefs(_MemoryStore())..load(),
+ ),
+ ),
+ ),
+ );
+}
+
+EditableTextState _field() {
+ EditableTextState? found;
+ void visit(Element element) {
+ if (found != null) return;
+ if (element is StatefulElement && element.state is EditableTextState) {
+ found = element.state as EditableTextState;
+ return;
+ }
+ element.visitChildElements(visit);
+ }
+
+ WidgetsBinding.instance.rootElement!.visitChildElements(visit);
+ return found!;
+}
+
+Future> _run({
+ required int lines,
+ required bool markdown,
+}) async {
+ final body = longNote(lines: lines);
+ _show(body, markdown: markdown);
+ await Future.delayed(const Duration(seconds: 3));
+ final field = _field();
+ const sentence = 'The quick brown fox jumps over the lazy dog, 12 * 3. ';
+ final keys = (sentence * 4).substring(
+ 0,
+ const int.fromEnvironment('EDITOR_BENCHMARK_KEYS', defaultValue: 200),
+ );
+
+ Future> typeAt(int caret) async {
+ field.userUpdateTextEditingValue(
+ field.textEditingValue.copyWith(
+ selection: TextSelection.collapsed(offset: caret),
+ ),
+ SelectionChangedCause.tap,
+ );
+ await Future.delayed(const Duration(seconds: 1));
+ final timings = [];
+ void record(List batch) => timings.addAll(batch);
+ SchedulerBinding.instance.addTimingsCallback(record);
+ for (final key in keys.split('')) {
+ final value = field.textEditingValue;
+ final at = value.selection.start;
+ // What the platform's text input sends for a key.
+ field.updateEditingValue(
+ TextEditingValue(
+ text: value.text.replaceRange(at, at, key),
+ selection: TextSelection.collapsed(offset: at + 1),
+ ),
+ );
+ await Future.delayed(const Duration(milliseconds: 33));
+ }
+ // Timings arrive in batches, about once a second in profile mode.
+ await Future.delayed(const Duration(seconds: 2));
+ SchedulerBinding.instance.removeTimingsCallback(record);
+ return summary(timings);
+ }
+
+ final atEnd = await typeAt(field.textEditingValue.text.length);
+ final middle = field.textEditingValue.text
+ .split('\n')
+ .take(lines ~/ 2)
+ .join('\n')
+ .length;
+ final inMiddle = await typeAt(middle);
+ return {
+ 'lines': lines,
+ 'markdown': markdown,
+ 'characters': body.length,
+ 'keys_typed': keys.length,
+ 'typed_correctly':
+ field.textEditingValue.text.length == body.length + keys.length * 2,
+ 'at_end': atEnd,
+ 'in_middle': inMiddle,
+ };
+}
diff --git a/benchmark/markdown_rendering.dart b/benchmark/markdown_rendering.dart
new file mode 100644
index 0000000..c25f563
--- /dev/null
+++ b/benchmark/markdown_rendering.dart
@@ -0,0 +1,365 @@
+// How long does a long note with formulas and code take to open, and how fast
+// does typing in it stay?
+//
+// Five thousand lines with markdown on: 30 code blocks in Python, TypeScript
+// and SQL, 300 formulas among words and 50 set apart, and prose and sums
+// between them. The note is opened three times, then typed into the way a
+// keyboard types, one editing-value update a key about thirty times a
+// second for a second at a time: in prose, in a code block and right after a
+// formula. The semantics tree is on throughout, as it always is in the macOS
+// app, which switches it on at launch so that dictation tools can write into
+// the editor (see AccessibilityTree.swift). Prints how long each opening took
+// to its first frame, and how long the typing frames took to build and to
+// draw, and quits.
+//
+// A plain app rather than an integration test, because only profile mode
+// says anything about frame times. It keeps the app's `main` reachable without
+// calling it, so that it is compiled together with the app's own code. Run it
+// under a bundle id of its own, never the real app's:
+//
+// FLUTTER_XCODE_PRODUCT_BUNDLE_IDENTIFIER=com.kapybara.kapynotes.renderperf \
+// flutter run --profile -d macos -t benchmark/markdown_rendering.dart
+//
+// The figures are the line starting MARKDOWN_RENDERING_PERF in the log, also
+// written to markdown_rendering_perf.json in the app's temporary directory.
+import 'dart:async';
+import 'dart:convert';
+import 'dart:io';
+
+import 'package:flutter/rendering.dart';
+import 'package:flutter/scheduler.dart';
+import 'package:kapy_notes/calc/engine.dart';
+import 'package:kapy_notes/calc/highlight.dart';
+import 'package:kapy_notes/core/editor_font.dart';
+import 'package:kapy_notes/core/theme.dart';
+import 'package:kapy_notes/data/local_store.dart';
+import 'package:kapy_notes/data/shortcut_prefs.dart';
+import 'package:kapy_notes/main.dart' as app;
+import 'package:kapy_notes/ui/editor/note_editor.dart';
+import 'package:material_ui/material_ui.dart';
+import 'package:window_manager/window_manager.dart';
+
+class _MemoryStore extends LocalStore {
+ _MemoryStore() : super(fileName: 'markdown-rendering-benchmark.json');
+
+ @override
+ Future load() async {}
+
+ @override
+ Future flush() async {}
+
+ @override
+ void put(String key, Object? value) => data[key] = value;
+}
+
+const _python = [
+ 'import math',
+ 'from dataclasses import dataclass',
+ '',
+ '@dataclass',
+ 'class Point:',
+ ' x: float',
+ ' y: float',
+ '',
+ ' def norm(self) -> float:',
+ ' # Euclidean length of the vector',
+ ' return math.sqrt(self.x ** 2 + self.y ** 2)',
+ '',
+ 'def mean(values: list[float]) -> float:',
+ ' """The arithmetic mean, or zero for nothing."""',
+ ' if not values:',
+ ' return 0.0',
+ ' return sum(values) / len(values)',
+ '',
+ 'for i in range(10):',
+ ' print(f"step {i}: {mean([i, i * 2, 3.5])}")',
+];
+
+const _typescript = [
+ "import { readFile } from 'node:fs/promises';",
+ '',
+ 'interface Invoice {',
+ ' id: string;',
+ ' total: number;',
+ ' paid?: boolean;',
+ '}',
+ '',
+ 'export async function load(path: string): Promise {',
+ " const raw = await readFile(path, 'utf8');",
+ ' // One invoice per line, as JSON.',
+ ' return raw.split("\\n").filter(Boolean).map((line) => JSON.parse(line));',
+ '}',
+ '',
+ 'const unpaid = (all: Invoice[]) => all.filter((i) => !i.paid);',
+ 'let sum = 0;',
+ 'for (const invoice of unpaid([])) {',
+ ' sum += invoice.total * 1.2;',
+ '}',
+ r'console.log(`owed: ${sum.toFixed(2)}`);',
+];
+
+const _sql = [
+ '-- Monthly revenue by customer',
+ 'SELECT c.name,',
+ " date_trunc('month', o.created_at) AS month,",
+ ' SUM(o.total) AS revenue,',
+ ' COUNT(*) AS orders',
+ 'FROM customers c',
+ 'JOIN orders o ON o.customer_id = c.id',
+ "WHERE o.status = 'paid'",
+ ' AND o.created_at >= NOW() - INTERVAL 1 YEAR',
+ 'GROUP BY c.name, month',
+ 'HAVING SUM(o.total) > 1000',
+ 'ORDER BY revenue DESC',
+ 'LIMIT 50;',
+ '',
+ 'UPDATE orders SET status = NULL WHERE id IN (1, 2, 3);',
+ 'CREATE INDEX orders_customer ON orders (customer_id);',
+ "INSERT INTO audit (event, at) VALUES ('refresh', NOW());",
+ 'DELETE FROM sessions WHERE expires_at < NOW();',
+ "SELECT * FROM customers WHERE email LIKE '%@example.com';",
+ 'DROP TABLE IF EXISTS scratch;',
+];
+
+/// The note: 5,000 lines in 30 sections, each with five lines of two
+/// formulas among words, one or two formulas set apart, a sum and a code
+/// block, and prose after them.
+String longNote() {
+ final lines = [];
+ const languages = ['python', 'typescript', 'sql'];
+ const bodies = [_python, _typescript, _sql];
+ for (var section = 0; section < 30; section++) {
+ final start = lines.length;
+ lines
+ ..add('## Section $section')
+ ..add('');
+ for (var i = 0; i < 5; i++) {
+ lines.add(
+ 'Line $i of $section says \$x_{$i}^{2} + \\frac{a}{b}\$ grows '
+ 'faster than \$\\sqrt{n}\$ here.',
+ );
+ }
+ lines.add('');
+ final displays = section < 20 ? 2 : 1;
+ for (var d = 0; d < displays; d++) {
+ lines
+ ..add('\$\$')
+ ..add('\\sum_{k=1}^{$section} k^$d = \\int_0^1 f(t)\\,dt')
+ ..add('\$\$')
+ ..add('');
+ }
+ lines
+ ..add('price = ${section + 3} usd')
+ ..add('price * 3')
+ ..add('')
+ ..add('```${languages[section % 3]}');
+ final body = bodies[section % 3];
+ lines
+ ..addAll(body)
+ ..addAll(body)
+ ..add('```')
+ ..add('');
+ while (lines.length - start < 166) {
+ lines.add(
+ 'Plain prose for section $section, line ${lines.length - start}, '
+ 'with a total of 4 + 5 in it.',
+ );
+ }
+ }
+ while (lines.length < 5000) {
+ lines.add('closing line ${lines.length}');
+ }
+ return lines.take(5000).join('\n');
+}
+
+/// [values] at the [p]th percentile, by nearest rank.
+double percentile(List values, double p) {
+ final sorted = [...values]..sort();
+ final rank = (p / 100 * sorted.length).ceil().clamp(1, sorted.length);
+ return sorted[rank - 1];
+}
+
+double _ms(Duration duration) => duration.inMicroseconds / 1000;
+
+Map summary(List timings) {
+ const budget = 1000 / 60;
+ final build = [for (final t in timings) _ms(t.buildDuration)];
+ final raster = [for (final t in timings) _ms(t.rasterDuration)];
+ Map stats(List values) => {
+ 'p50': percentile(values, 50),
+ 'p90': percentile(values, 90),
+ 'max': percentile(values, 100),
+ };
+ return {
+ 'frames': timings.length,
+ 'build_ms': stats(build),
+ 'raster_ms': stats(raster),
+ 'frames_over_16_7ms': [
+ for (var i = 0; i < timings.length; i++)
+ if (build[i] > budget || raster[i] > budget) i,
+ ].length,
+ };
+}
+
+void main() {
+ WidgetsFlutterBinding.ensureInitialized();
+ unawaited(_runAll());
+}
+
+Future _runAll() async {
+ // The app's window waits hidden for the app's main to show it, and a
+ // hidden window draws no frames.
+ await windowManager.ensureInitialized();
+ await windowManager.setSize(const Size(1280, 900));
+ await windowManager.show();
+ final report = await _run();
+ final line = jsonEncode(report);
+ File(
+ '${Directory.systemTemp.path}/markdown_rendering_perf.json',
+ ).writeAsStringSync(line);
+ _say('MARKDOWN_RENDERING_PERF $line');
+ if (report.isEmpty) await app.main();
+ exit(0);
+}
+
+/// Prints [line] to the log, which is where a run that is taking its time
+/// shows how far it has got.
+void _say(String line) {
+ // ignore: avoid_print
+ print(line);
+}
+
+void _show(Widget home) {
+ runApp(MaterialApp(key: UniqueKey(), theme: KapyTheme.dark(), home: home));
+}
+
+Widget _editor(String body) {
+ final engine = CalcEngine();
+ return Scaffold(
+ body: NoteEditor(
+ noteId: 'benchmark',
+ initialBody: body,
+ engine: engine,
+ highlighter: Highlighter(engine.registry),
+ gutterWidth: 200,
+ resultsVisible: true,
+ markdownEnabled: true,
+ autofocus: true,
+ onDocumentChanged: (_, _, _) {},
+ onGutterWidthChanged: (_) {},
+ onResultsVisibilityChanged: (_) {},
+ onGutterWidthReset: () {},
+ onSettingsPressed: () {},
+ writingFont: WritingFont.handwritten,
+ shortcuts: ShortcutPrefs(_MemoryStore())..load(),
+ ),
+ );
+}
+
+EditableTextState _field() {
+ EditableTextState? found;
+ void visit(Element element) {
+ if (found != null) return;
+ if (element is StatefulElement && element.state is EditableTextState) {
+ found = element.state as EditableTextState;
+ return;
+ }
+ element.visitChildElements(visit);
+ }
+
+ WidgetsBinding.instance.rootElement!.visitChildElements(visit);
+ return found!;
+}
+
+double _slowest(Iterable values) =>
+ values.fold(0, (a, b) => a > b ? a : b);
+
+/// How long [body] takes to open in an empty window: from showing the
+/// editor to the end of its first frame, which builds the note, lays it out
+/// and paints it; and the slowest build and raster of the frames it takes.
+Future> _open(String body) async {
+ _show(const SizedBox.shrink());
+ await WidgetsBinding.instance.endOfFrame;
+ await Future.delayed(const Duration(seconds: 1));
+ final timings = [];
+ void record(List batch) => timings.addAll(batch);
+ SchedulerBinding.instance.addTimingsCallback(record);
+ final stopwatch = Stopwatch()..start();
+ _show(_editor(body));
+ await WidgetsBinding.instance.endOfFrame;
+ final firstFrame = stopwatch.elapsedMicroseconds / 1000;
+ await Future.delayed(const Duration(seconds: 2));
+ SchedulerBinding.instance.removeTimingsCallback(record);
+ return {
+ 'first_frame_ms': firstFrame,
+ 'slowest_build_ms': _slowest([
+ for (final t in timings) _ms(t.buildDuration),
+ ]),
+ 'slowest_raster_ms': _slowest([
+ for (final t in timings) _ms(t.rasterDuration),
+ ]),
+ };
+}
+
+Future> _run() async {
+ final body = longNote();
+ final opens = >[];
+ for (var i = 1; i <= 3; i++) {
+ opens.add(await _open(body));
+ _say(
+ 'opened $i of 3: ${opens.last['first_frame_ms']} ms to the first frame',
+ );
+ }
+ await Future.delayed(const Duration(seconds: 1));
+ final field = _field();
+ const keys = 30;
+
+ Future> typeAt(int caret) async {
+ field.userUpdateTextEditingValue(
+ field.textEditingValue.copyWith(
+ selection: TextSelection.collapsed(offset: caret),
+ ),
+ SelectionChangedCause.tap,
+ );
+ await Future.delayed(const Duration(seconds: 1));
+ final timings = [];
+ void record(List batch) => timings.addAll(batch);
+ SchedulerBinding.instance.addTimingsCallback(record);
+ for (var i = 0; i < keys; i++) {
+ final value = field.textEditingValue;
+ final at = value.selection.start;
+ // What the platform's text input sends for a key.
+ field.updateEditingValue(
+ TextEditingValue(
+ text: value.text.replaceRange(at, at, 'a'),
+ selection: TextSelection.collapsed(offset: at + 1),
+ ),
+ );
+ await Future.delayed(const Duration(milliseconds: 33));
+ }
+ // Timings arrive in batches, about once a second in profile mode.
+ await Future.delayed(const Duration(seconds: 2));
+ SchedulerBinding.instance.removeTimingsCallback(record);
+ _say('typed $keys keys at $caret');
+ return summary(timings);
+ }
+
+ int after(String needle, String from) {
+ final text = field.textEditingValue.text;
+ return text.indexOf(needle, text.indexOf(from));
+ }
+
+ final prose = await typeAt(
+ after('Plain prose for section 15, line 100', '## Section 15'),
+ );
+ final code = await typeAt(after('return sum(values)', '## Section 15'));
+ final formula = await typeAt(after(' grows', 'Line 2 of 15'));
+ return {
+ 'semantics': SemanticsBinding.instance.semanticsEnabled,
+ 'characters': body.length,
+ 'open': opens,
+ 'typing_in_prose': prose,
+ 'typing_in_code': code,
+ 'typing_after_a_formula': formula,
+ };
+}
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index 3122bba..4e7648b 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -20,6 +20,7 @@
A1B2C0D000000000000000D4 /* QuickActionsWidget.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C0D000000000000000D3 /* QuickActionsWidget.swift */; };
A1B2C0D000000000000000A5 /* WriteNoteWidgetExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = A1B2C0D000000000000000A1 /* WriteNoteWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; };
+ C1A0B0F20000000000000002 /* RichClipboardTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1A0B0F20000000000000001 /* RichClipboardTests.swift */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */; };
@@ -75,6 +76,7 @@
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; };
331C807B294A618700263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = ""; };
+ C1A0B0F20000000000000001 /* RichClipboardTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RichClipboardTests.swift; sourceTree = ""; };
331C8081294A63A400263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = ""; };
@@ -127,6 +129,7 @@
isa = PBXGroup;
children = (
331C807B294A618700263BE5 /* RunnerTests.swift */,
+ C1A0B0F20000000000000001 /* RichClipboardTests.swift */,
);
path = RunnerTests;
sourceTree = "";
@@ -392,6 +395,7 @@
buildActionMask = 2147483647;
files = (
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */,
+ C1A0B0F20000000000000002 /* RichClipboardTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/ios/Runner/RichClipboard.swift b/ios/Runner/RichClipboard.swift
index b0a71c4..09fc824 100644
--- a/ios/Runner/RichClipboard.swift
+++ b/ios/Runner/RichClipboard.swift
@@ -2,16 +2,30 @@ import Flutter
import UIKit
import UniformTypeIdentifiers
-/// The system clipboard representation for a note selection containing images.
+/// The system clipboard representation for a note selection containing
+/// images, and every representation a paste can use, read in one call.
enum RichClipboard {
private static let channelName = "kapynotes/rich_clipboard"
+ /// Kapy Notes' own fragment, which only Kapy Notes reads.
+ static let kapyType = "com.kapybara.kapynotes.fragment"
+ private static let sourceUrlType = "org.chromium.source-url"
+ private static let vscodeType = "vscode-editor-data"
+ private static let webArchiveTypes = ["com.apple.webarchive", "Apple Web Archive pasteboard type"]
+
static func register(with messenger: FlutterBinaryMessenger) -> FlutterMethodChannel {
let channel = FlutterMethodChannel(name: channelName, binaryMessenger: messenger)
channel.setMethodCallHandler { call, result in
switch call.method {
case "write":
write(call.arguments, result: result)
+ case "read":
+ let name = (call.arguments as? [String: Any])?["pasteboard"] as? String
+ if let name, !name.isEmpty {
+ result(UIPasteboard(name: UIPasteboard.Name(name), create: false).map(read(from:)) ?? [:])
+ } else {
+ result(read(from: .general))
+ }
case "readHtml":
result(readHtml())
default:
@@ -28,19 +42,42 @@ enum RichClipboard {
result(FlutterError(code: "clipboard_arguments", message: "Missing clipboard data", details: nil))
return
}
+ let name = arguments["pasteboard"] as? String
+ let pasteboard = name.flatMap { $0.isEmpty ? nil : UIPasteboard(name: UIPasteboard.Name($0), create: true) }
+ write(
+ text: text,
+ html: html,
+ kapy: arguments["kapy"] as? String,
+ image: (arguments["image"] as? FlutterStandardTypedData)?.data,
+ imageMime: arguments["imageMime"] as? String,
+ to: pasteboard ?? .general
+ )
+ result(nil)
+ }
+ /// Replaces what `pasteboard` holds with one item in every representation
+ /// given.
+ static func write(
+ text: String,
+ html: String,
+ kapy: String?,
+ image: Data?,
+ imageMime: String?,
+ to pasteboard: UIPasteboard
+ ) {
var item: [String: Any] = [
UTType.utf8PlainText.identifier: text,
UTType.html.identifier: html,
]
- if let typed = arguments["image"] as? FlutterStandardTypedData,
- !typed.data.isEmpty,
- let mime = arguments["imageMime"] as? String,
- let type = UTType(mimeType: mime) {
- item[type.identifier] = typed.data
+ if let kapy, !kapy.isEmpty {
+ item[kapyType] = Data(kapy.utf8)
}
- UIPasteboard.general.setItems([item], options: [:])
- result(nil)
+ if let image, !image.isEmpty,
+ let imageMime,
+ let type = UTType(mimeType: imageMime) {
+ item[type.identifier] = image
+ }
+ pasteboard.setItems([item], options: [:])
}
private static func readHtml() -> String? {
@@ -50,4 +87,296 @@ enum RichClipboard {
}
return pasteboard.value(forPasteboardType: UTType.html.identifier) as? String
}
+
+ /// Everything on `pasteboard` that a paste can use, in the shape
+ /// `ClipboardSnapshot.fromChannel` reads.
+ ///
+ /// The contents are fetched once, with `items`, so iOS asks for
+ /// permission to paste at most once. Rich text is converted to HTML only
+ /// when nothing else offers HTML, because the conversion is slow and HTML
+ /// from the source is better. The pictures in RTFD are still read when that
+ /// HTML shows them by a `file:///` URL, which is how Cocoa apps' own HTML
+ /// refers to them.
+ static func read(from pasteboard: UIPasteboard) -> [String: Any] {
+ guard pasteboard.numberOfItems > 0 else { return ["types": [String]()] }
+ let items = pasteboard.items
+ var types: [String] = []
+ for item in items {
+ for type in item.keys.sorted() where !types.contains(type) {
+ types.append(type)
+ }
+ }
+ func value(_ type: String) -> Any? {
+ items.lazy.compactMap { $0[type] }.first
+ }
+
+ var snapshot: [String: Any] = ["types": types]
+ snapshot["text"] = string(value(UTType.utf8PlainText.identifier))
+ ?? string(value(UTType.plainText.identifier))
+ snapshot["vscode"] = string(value(vscodeType))
+ snapshot["kapy"] = string(value(kapyType))
+
+ var html = string(value(UTType.html.identifier))
+ var sourceUrl = string(value(sourceUrlType))
+ var resources: [[String: Any]] = []
+ if let data = webArchiveTypes.lazy.compactMap({ data(value($0)) }).first,
+ let archive = webArchive(data) {
+ html = html ?? archive.html
+ sourceUrl = sourceUrl ?? archive.url
+ resources += archive.resources
+ }
+ let rtfd = data(value(UTType.flatRTFD.identifier))
+ if html == nil,
+ let richText = richText(rtfd: rtfd, rtf: data(value(UTType.rtf.identifier))),
+ let rtfHtml = convertToHtml(richText) {
+ snapshot["rtfHtml"] = rtfHtml
+ resources += attachmentResources(of: richText, namedBy: rtfHtml)
+ } else if let html, let rtfd,
+ imageSources(in: html).contains(where: { $0.hasPrefix("file:///") }),
+ let richText = richText(rtfd: rtfd, rtf: nil) {
+ resources += attachmentResources(of: richText, namedBy: html)
+ }
+ snapshot["html"] = html
+ snapshot["sourceUrl"] = sourceUrl
+ if !resources.isEmpty {
+ snapshot["resources"] = resources
+ }
+
+ let files = items.compactMap { fileUrl($0[UTType.fileURL.identifier])?.path }
+ if !files.isEmpty {
+ snapshot["files"] = files
+ }
+
+ if let png = imageData(value(UTType.png.identifier)) {
+ snapshot["image"] = FlutterStandardTypedData(bytes: png)
+ snapshot["imageMime"] = "image/png"
+ } else if let jpeg = imageData(value(UTType.jpeg.identifier)) {
+ snapshot["image"] = FlutterStandardTypedData(bytes: jpeg)
+ snapshot["imageMime"] = "image/jpeg"
+ } else if let tiff = data(value(UTType.tiff.identifier)), let png = pngData(tiff, type: .tiff) {
+ snapshot["image"] = FlutterStandardTypedData(bytes: png)
+ snapshot["imageMime"] = "image/png"
+ }
+ return snapshot
+ }
+
+ /// A pasteboard value as text: `items` returns text as a string or as data.
+ private static func string(_ value: Any?) -> String? {
+ switch value {
+ case let text as String:
+ return text
+ case let data as Data:
+ return String(data: data, encoding: .utf8)
+ case let url as URL:
+ return url.absoluteString
+ default:
+ return nil
+ }
+ }
+
+ private static func data(_ value: Any?) -> Data? {
+ switch value {
+ case let data as Data:
+ return data
+ case let text as String:
+ return Data(text.utf8)
+ default:
+ return nil
+ }
+ }
+
+ /// A picture's bytes: `items` returns pictures as data or as a `UIImage`.
+ private static func imageData(_ value: Any?) -> Data? {
+ switch value {
+ case let data as Data:
+ return data.isEmpty ? nil : data
+ case let image as UIImage:
+ return image.pngData()
+ default:
+ return nil
+ }
+ }
+
+ private static func fileUrl(_ value: Any?) -> URL? {
+ let url: URL?
+ switch value {
+ case let value as URL:
+ url = value
+ case let data as Data:
+ url = URL(dataRepresentation: data, relativeTo: nil)
+ case let text as String:
+ url = URL(string: text)
+ default:
+ url = nil
+ }
+ return url?.isFileURL == true ? url : nil
+ }
+
+ /// Safari's copy: the page's markup, the page's address, and the bytes of
+ /// everything the markup loaded, by the URL it loaded them from.
+ private static func webArchive(
+ _ data: Data
+ ) -> (html: String?, url: String?, resources: [[String: Any]])? {
+ guard let archive = try? PropertyListSerialization.propertyList(from: data, format: nil)
+ as? [String: Any] else {
+ return nil
+ }
+ let main = archive["WebMainResource"] as? [String: Any]
+ let html = (main?["WebResourceData"] as? Data).flatMap {
+ decodeText($0, encodingName: main?["WebResourceTextEncodingName"] as? String)
+ }
+ let resources = (archive["WebSubresources"] as? [[String: Any]] ?? []).compactMap {
+ resource -> [String: Any]? in
+ guard let url = resource["WebResourceURL"] as? String,
+ let bytes = resource["WebResourceData"] as? Data,
+ !bytes.isEmpty else {
+ return nil
+ }
+ return [
+ "name": url,
+ "mime": resource["WebResourceMIMEType"] as? String ?? "application/octet-stream",
+ "data": FlutterStandardTypedData(bytes: bytes),
+ ]
+ }
+ return (html, main?["WebResourceURL"] as? String, resources)
+ }
+
+ private static func decodeText(_ data: Data, encodingName: String?) -> String? {
+ if let encodingName {
+ let encoding = CFStringConvertIANACharSetNameToEncoding(encodingName as CFString)
+ if encoding != kCFStringEncodingInvalidId,
+ let text = String(
+ data: data,
+ encoding: String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(encoding))
+ ) {
+ return text
+ }
+ }
+ return String(data: data, encoding: .utf8)
+ }
+
+ /// RTFD when the source offered it, for its pictures, else plain RTF.
+ private static func richText(rtfd: Data?, rtf: Data?) -> NSAttributedString? {
+ if let rtfd,
+ let text = try? NSAttributedString(
+ data: rtfd,
+ options: [.documentType: NSAttributedString.DocumentType.rtfd],
+ documentAttributes: nil
+ ) {
+ return text
+ }
+ if let rtf,
+ let text = try? NSAttributedString(
+ data: rtf,
+ options: [.documentType: NSAttributedString.DocumentType.rtf],
+ documentAttributes: nil
+ ) {
+ return text
+ }
+ return nil
+ }
+
+ private static func convertToHtml(_ text: NSAttributedString) -> String? {
+ guard let data = try? text.data(
+ from: NSRange(location: 0, length: text.length),
+ documentAttributes: [.documentType: NSAttributedString.DocumentType.html]
+ ) else {
+ return nil
+ }
+ return String(data: data, encoding: .utf8)
+ }
+
+ /// The bytes of `text`'s attachments, each named by the `src` of the
+ /// ` ` that shows it in `html`.
+ ///
+ /// The Cocoa HTML writer, both ours and the one an app used for its own
+ /// `public.html`, names an attachment by a `file:///` URL of its file
+ /// name, which no reader can open. So attachments are matched to those
+ /// images by that name, then in order, and other images are left alone.
+ private static func attachmentResources(
+ of text: NSAttributedString,
+ namedBy html: String
+ ) -> [[String: Any]] {
+ var attachments: [(name: String?, bytes: Data, type: UTType?)] = []
+ text.enumerateAttribute(.attachment, in: NSRange(location: 0, length: text.length)) { value, _, _ in
+ guard let attachment = value as? NSTextAttachment else { return }
+ let wrapper = attachment.fileWrapper
+ guard let bytes = wrapper?.regularFileContents ?? attachment.contents else { return }
+ let name = wrapper?.preferredFilename ?? wrapper?.filename
+ let type = attachment.fileType.flatMap { UTType($0) }
+ ?? name.flatMap { UTType(filenameExtension: ($0 as NSString).pathExtension) }
+ attachments.append((name, bytes, type))
+ }
+
+ var unused = Array(attachments.indices)
+ var named = Set()
+ var resources: [[String: Any]] = []
+ for source in imageSources(in: html)
+ where source.hasPrefix("file:///") && named.insert(source).inserted {
+ let name = String(source.dropFirst("file:///".count)).removingPercentEncoding
+ guard let index = unused.first(where: { attachments[$0].name == name }) ?? unused.first else {
+ break
+ }
+ unused.removeAll { $0 == index }
+ let attachment = attachments[index]
+ if let resource = imageResource(attachment.bytes, type: attachment.type) {
+ resources.append([
+ "name": source,
+ "mime": resource.mime,
+ "data": FlutterStandardTypedData(bytes: resource.bytes),
+ ])
+ }
+ }
+ return resources
+ }
+
+ /// Every ` ` in `html`, as the attribute's value.
+ private static func imageSources(in html: String) -> [String] {
+ guard let pattern = try? NSRegularExpression(
+ pattern: " ]*?\\ssrc=\"([^\"]*)\"",
+ options: [.caseInsensitive]
+ ) else {
+ return []
+ }
+ return pattern.matches(in: html, range: NSRange(html.startIndex..., in: html)).compactMap {
+ Range($0.range(at: 1), in: html).map {
+ String(html[$0])
+ .replacingOccurrences(of: """, with: "\"")
+ .replacingOccurrences(of: "'", with: "'")
+ .replacingOccurrences(of: "<", with: "<")
+ .replacingOccurrences(of: ">", with: ">")
+ .replacingOccurrences(of: "&", with: "&")
+ }
+ }
+ }
+
+ /// A picture as bytes a web view or Flutter can show: TIFF and PDF become
+ /// PNG, other pictures stay as they are, and anything else is left out.
+ private static func imageResource(_ bytes: Data, type: UTType?) -> (bytes: Data, mime: String)? {
+ guard let type, type.conforms(to: .image) || type.conforms(to: .pdf) else { return nil }
+ if type.conforms(to: .tiff) || type.conforms(to: .pdf) {
+ return pngData(bytes, type: type).map { ($0, "image/png") }
+ }
+ return type.preferredMIMEType.map { (bytes, $0) }
+ }
+
+ /// A TIFF, or a PDF's first page, as PNG.
+ private static func pngData(_ data: Data, type: UTType) -> Data? {
+ guard type.conforms(to: .pdf) else { return UIImage(data: data)?.pngData() }
+ guard let provider = CGDataProvider(data: data as CFData),
+ let page = CGPDFDocument(provider)?.page(at: 1) else {
+ return nil
+ }
+ let bounds = page.getBoxRect(.mediaBox)
+ guard bounds.width > 0, bounds.height > 0 else { return nil }
+ let format = UIGraphicsImageRendererFormat()
+ format.scale = 1
+ return UIGraphicsImageRenderer(size: bounds.size, format: format).pngData { context in
+ let cg = context.cgContext
+ cg.translateBy(x: 0, y: bounds.height)
+ cg.scaleBy(x: 1, y: -1)
+ cg.translateBy(x: -bounds.minX, y: -bounds.minY)
+ cg.drawPDFPage(page)
+ }
+ }
}
diff --git a/ios/RunnerTests/RichClipboardTests.swift b/ios/RunnerTests/RichClipboardTests.swift
new file mode 100644
index 0000000..102d04e
--- /dev/null
+++ b/ios/RunnerTests/RichClipboardTests.swift
@@ -0,0 +1,257 @@
+import Flutter
+import UIKit
+import UniformTypeIdentifiers
+import XCTest
+
+@testable import Runner
+
+/// Reads and writes a private pasteboard, never the user's clipboard.
+class RichClipboardTests: XCTestCase {
+ private var pasteboard: UIPasteboard!
+ private var generalChangeCount = 0
+
+ override func setUp() {
+ super.setUp()
+ generalChangeCount = UIPasteboard.general.changeCount
+ pasteboard = UIPasteboard.withUniqueName()
+ }
+
+ override func tearDown() {
+ UIPasteboard.remove(withName: pasteboard.name)
+ XCTAssertEqual(UIPasteboard.general.changeCount, generalChangeCount)
+ super.tearDown()
+ }
+
+ private func read() -> [String: Any] {
+ RichClipboard.read(from: pasteboard)
+ }
+
+ private func bytes(_ value: Any?) -> Data? {
+ (value as? FlutterStandardTypedData)?.data
+ }
+
+ private func resources(_ snapshot: [String: Any]) -> [String: (mime: String, data: Data)] {
+ var named: [String: (mime: String, data: Data)] = [:]
+ for resource in snapshot["resources"] as? [[String: Any]] ?? [] {
+ named[resource["name"] as! String] = (resource["mime"] as! String, bytes(resource["data"])!)
+ }
+ return named
+ }
+
+ private func picture(_ color: UIColor) -> UIImage {
+ let format = UIGraphicsImageRendererFormat()
+ format.scale = 1
+ return UIGraphicsImageRenderer(size: CGSize(width: 4, height: 3), format: format).image {
+ color.setFill()
+ $0.fill(CGRect(x: 0, y: 0, width: 4, height: 3))
+ }
+ }
+
+ private func tiff(_ image: UIImage) -> Data {
+ let data = NSMutableData()
+ let destination = CGImageDestinationCreateWithData(data, UTType.tiff.identifier as CFString, 1, nil)!
+ CGImageDestinationAddImage(destination, image.cgImage!, nil)
+ XCTAssertTrue(CGImageDestinationFinalize(destination))
+ return data as Data
+ }
+
+ private func isPng(_ data: Data?) -> Bool {
+ data?.starts(with: [0x89, 0x50, 0x4E, 0x47]) == true
+ }
+
+ func testReadsHtmlTextAndTheirCompanions() {
+ let vscode = #"{"version":1,"mode":"dart"}"#
+ pasteboard.setItems([[
+ UTType.html.identifier: "Hello world ✓
",
+ UTType.utf8PlainText.identifier: "Hello world ✓",
+ "org.chromium.source-url": Data("https://example.com/page".utf8),
+ "vscode-editor-data": Data(vscode.utf8),
+ RichClipboard.kapyType: Data(#"{"kapy":1}"#.utf8),
+ UTType.rtf.identifier: Data(#"{\rtf1\ansi Hello}"#.utf8),
+ ]])
+
+ let snapshot = read()
+
+ XCTAssertEqual(snapshot["html"] as? String, "Hello world ✓
")
+ XCTAssertEqual(snapshot["text"] as? String, "Hello world ✓")
+ XCTAssertEqual(snapshot["sourceUrl"] as? String, "https://example.com/page")
+ XCTAssertEqual(snapshot["vscode"] as? String, vscode)
+ XCTAssertEqual(snapshot["kapy"] as? String, #"{"kapy":1}"#)
+ XCTAssertNil(snapshot["rtfHtml"], "RTF is not converted when there is HTML")
+ let types = snapshot["types"] as? [String] ?? []
+ XCTAssertTrue(types.contains(UTType.html.identifier))
+ XCTAssertTrue(types.contains(RichClipboard.kapyType))
+ XCTAssertNil(snapshot["image"])
+ XCTAssertNil(snapshot["files"])
+ }
+
+ func testConvertsRtfdWithItsPicturesNamedAsTheHtmlNamesThem() throws {
+ let red = picture(.red).pngData()!
+ let blue = tiff(picture(.blue))
+ let text = NSMutableAttributedString(string: "Shopping ")
+ for (bytes, name) in [(red, "photo one.png"), (blue, "Pasted Graphic.tiff")] {
+ let wrapper = FileWrapper(regularFileWithContents: bytes)
+ wrapper.preferredFilename = name
+ let attachment = NSTextAttachment()
+ attachment.fileWrapper = wrapper
+ text.append(NSAttributedString(attachment: attachment))
+ }
+ text.append(NSAttributedString(
+ string: " list\n",
+ attributes: [.font: UIFont.boldSystemFont(ofSize: 14)]
+ ))
+ let rtfd = try text.data(
+ from: NSRange(location: 0, length: text.length),
+ documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd]
+ )
+ pasteboard.setItems([[
+ UTType.flatRTFD.identifier: rtfd,
+ UTType.utf8PlainText.identifier: "Shopping list",
+ ]])
+
+ let snapshot = read()
+
+ XCTAssertNil(snapshot["html"])
+ let html = snapshot["rtfHtml"] as? String ?? ""
+ XCTAssertTrue(html.contains(#" Chart
"#
+ pasteboard.setItems([[UTType.html.identifier: html, UTType.flatRTFD.identifier: rtfd]])
+
+ let snapshot = read()
+
+ XCTAssertEqual(snapshot["html"] as? String, html)
+ XCTAssertNil(snapshot["rtfHtml"])
+ let named = resources(snapshot)
+ XCTAssertEqual(Array(named.keys), ["file:///chart.png"])
+ XCTAssertEqual(named["file:///chart.png"]?.data, red)
+ }
+
+ func testConvertsPlainRtfWhenThereIsNoHtml() {
+ pasteboard.setItems([[UTType.rtf.identifier: Data(#"{\rtf1\ansi Plain {\b bold}}"#.utf8)]])
+
+ let snapshot = read()
+
+ let html = snapshot["rtfHtml"] as? String ?? ""
+ XCTAssertTrue(html.contains("bold"), html)
+ XCTAssertNil(snapshot["resources"])
+ }
+
+ func testReadsSafarisWebArchive() throws {
+ let photo = picture(.green).pngData()!
+ let archive: [String: Any] = [
+ "WebMainResource": [
+ "WebResourceData": Data("From Safari
".utf8),
+ "WebResourceURL": "https://example.com/a/page.html",
+ "WebResourceMIMEType": "text/html",
+ "WebResourceTextEncodingName": "UTF-8",
+ "WebResourceFrameName": "",
+ ],
+ "WebSubresources": [
+ [
+ "WebResourceData": photo,
+ "WebResourceURL": "https://example.com/a/photo.png",
+ "WebResourceMIMEType": "image/png",
+ ],
+ ],
+ ]
+ let data = try PropertyListSerialization.data(fromPropertyList: archive, format: .binary, options: 0)
+ pasteboard.setItems([[
+ "com.apple.webarchive": data,
+ UTType.utf8PlainText.identifier: "From Safari",
+ ]])
+
+ let snapshot = read()
+
+ XCTAssertEqual(snapshot["html"] as? String, "From Safari
")
+ XCTAssertEqual(snapshot["sourceUrl"] as? String, "https://example.com/a/page.html")
+ let named = resources(snapshot)
+ XCTAssertEqual(named["https://example.com/a/photo.png"]?.mime, "image/png")
+ XCTAssertEqual(named["https://example.com/a/photo.png"]?.data, photo)
+ }
+
+ func testReadsFileUrls() {
+ let file = URL(fileURLWithPath: "/tmp/kapy clipboard.txt")
+ pasteboard.setItems([[UTType.fileURL.identifier: file]])
+
+ let snapshot = read()
+
+ XCTAssertEqual(snapshot["files"] as? [String], [file.path])
+ }
+
+ func testReadsPngAsItIs() {
+ let png = picture(.red).pngData()!
+ pasteboard.setItems([[UTType.png.identifier: png]])
+
+ let snapshot = read()
+
+ XCTAssertTrue(isPng(bytes(snapshot["image"])))
+ XCTAssertEqual(snapshot["imageMime"] as? String, "image/png")
+ }
+
+ func testReadsTiffAsPng() {
+ pasteboard.setItems([[UTType.tiff.identifier: tiff(picture(.blue))]])
+
+ let snapshot = read()
+
+ XCTAssertTrue(isPng(bytes(snapshot["image"])))
+ XCTAssertEqual(snapshot["imageMime"] as? String, "image/png")
+ }
+
+ func testReadsJpeg() {
+ pasteboard.setItems([[UTType.jpeg.identifier: picture(.red).jpegData(compressionQuality: 0.9)!]])
+
+ let snapshot = read()
+
+ XCTAssertEqual(bytes(snapshot["image"])?.starts(with: [0xFF, 0xD8]), true)
+ XCTAssertEqual(snapshot["imageMime"] as? String, "image/jpeg")
+ }
+
+ func testWritesKapysFragmentBesideTheOtherRepresentations() {
+ let png = picture(.red).pngData()!
+ RichClipboard.write(
+ text: "Groceries",
+ html: "Groceries
",
+ kapy: #"{"body":"Groceries"}"#,
+ image: png,
+ imageMime: "image/png",
+ to: pasteboard
+ )
+
+ XCTAssertEqual(pasteboard.numberOfItems, 1)
+ let snapshot = read()
+ XCTAssertEqual(snapshot["text"] as? String, "Groceries")
+ XCTAssertEqual(snapshot["html"] as? String, "Groceries
")
+ XCTAssertEqual(snapshot["kapy"] as? String, #"{"body":"Groceries"}"#)
+ XCTAssertTrue(isPng(bytes(snapshot["image"])))
+ }
+
+ func testEmptyPasteboardReadsAsNothing() {
+ pasteboard.items = []
+
+ let snapshot = read()
+
+ XCTAssertEqual(snapshot["types"] as? [String], [])
+ XCTAssertEqual(snapshot.count, 1)
+ }
+}
diff --git a/lib/data/layout_prefs.dart b/lib/data/layout_prefs.dart
index c78f801..fb78fcc 100644
--- a/lib/data/layout_prefs.dart
+++ b/lib/data/layout_prefs.dart
@@ -145,7 +145,7 @@ class LayoutPrefs extends ChangeNotifier {
bool _readyToTypeOnOpen = true;
bool _dailySeparatorsEnabled = true;
bool _spellCheckEnabled = true;
- bool _markdownEnabled = false;
+ bool _markdownEnabled = true;
NumberSystem _numberSystem = NumberSystem.auto;
WritingFont _writingFont = WritingFont.clean;
double _editorTextScale = defaultEditorTextScale;
@@ -199,9 +199,11 @@ class LayoutPrefs extends ChangeNotifier {
/// Whether notes are written in markdown: `# `, `**`, `- ` and the rest
/// drawn as what they mean, and the formatting controls writing them.
///
- /// Off by default, and a matter of how this device shows and edits notes
+ /// On by default, and a matter of how this device shows and edits notes
/// rather than of the notes: nothing is converted either way, so switching
- /// it back leaves every note exactly as it was written.
+ /// it back leaves every note exactly as it was written. The setting is only
+ /// stored once the writer changes it, so a device that has never stored it
+ /// gets markdown, and one that switched it off keeps it off.
bool get markdownEnabled => _markdownEnabled;
WritingFont get writingFont => _writingFont;
@@ -340,7 +342,7 @@ class LayoutPrefs extends ChangeNotifier {
_readyToTypeOnOpen = _store.read(_readyToTypeOnOpenKey) ?? true;
_dailySeparatorsEnabled = _store.read(_dailySeparatorsKey) ?? true;
_spellCheckEnabled = _store.read(_spellCheckKey) ?? true;
- _markdownEnabled = _store.read(_markdownKey) ?? false;
+ _markdownEnabled = _store.read(_markdownKey) ?? true;
_numberSystem = _readNumberSystem();
_writingFont = _readWritingFont();
_editorTextScale = _normalizeEditorTextScale(
diff --git a/lib/data/note_attachment.dart b/lib/data/note_attachment.dart
index 67f3d7e..34a0a56 100644
--- a/lib/data/note_attachment.dart
+++ b/lib/data/note_attachment.dart
@@ -212,6 +212,15 @@ final class NoteImageRef extends NoteAttachmentRef {
final bool isPreparing;
final Uint8List? previewBytes;
+ /// Where a pasted picture is still to be fetched from, while its bytes are
+ /// not on this device: being downloaded, or waiting to be tried again.
+ ///
+ /// Saved with the note, so a download cut short by quitting the app can
+ /// still be retried. It never leaves this device: a note waits to sync
+ /// until every picture in it is uploaded, and this one has nothing to
+ /// upload until it is fetched.
+ final String? src;
+
const NoteImageRef({
required super.offset,
required super.hash,
@@ -226,6 +235,7 @@ final class NoteImageRef extends NoteAttachmentRef {
this.thumbId,
this.isPreparing = false,
this.previewBytes,
+ this.src,
});
/// Narrower than this and an image stops being a picture and starts being a
@@ -261,6 +271,7 @@ final class NoteImageRef extends NoteAttachmentRef {
final thumbHash = raw['thumbHash'];
final widthFactor = raw['widthFactor'];
final thumbId = raw['thumbId'];
+ final src = raw['src'];
return NoteImageRef(
offset: offset,
hash: hash,
@@ -275,6 +286,7 @@ final class NoteImageRef extends NoteAttachmentRef {
: 1,
attachmentId: attachmentId,
thumbId: thumbId is String ? thumbId : null,
+ src: src is String && src.isNotEmpty ? src : null,
);
}
@@ -302,6 +314,7 @@ final class NoteImageRef extends NoteAttachmentRef {
previewBytes: identical(previewBytes, _keepPreview)
? this.previewBytes
: previewBytes as Uint8List?,
+ src: src,
);
@override
@@ -322,6 +335,7 @@ final class NoteImageRef extends NoteAttachmentRef {
if (widthFactor < 1) 'widthFactor': widthFactor,
if (attachmentId != null) 'attachmentId': attachmentId,
if (thumbId != null) 'thumbId': thumbId,
+ if (src != null) 'src': src,
};
@override
@@ -337,7 +351,8 @@ final class NoteImageRef extends NoteAttachmentRef {
other.widthFactor == widthFactor &&
other.attachmentId == attachmentId &&
other.thumbId == thumbId &&
- other.isPreparing == isPreparing;
+ other.isPreparing == isPreparing &&
+ other.src == src;
@override
int get hashCode => Object.hash(
@@ -352,6 +367,7 @@ final class NoteImageRef extends NoteAttachmentRef {
attachmentId,
thumbId,
isPreparing,
+ src,
);
}
diff --git a/lib/data/shortcut_prefs.dart b/lib/data/shortcut_prefs.dart
index 9102dd7..0b6846d 100644
--- a/lib/data/shortcut_prefs.dart
+++ b/lib/data/shortcut_prefs.dart
@@ -32,6 +32,7 @@ enum ShortcutAction {
formatBullets,
formatChecklist,
recordVoiceNote,
+ pasteAsPlainText,
}
extension ShortcutActionCopy on ShortcutAction {
@@ -63,6 +64,7 @@ extension ShortcutActionCopy on ShortcutAction {
ShortcutAction.formatBullets => 'Bulleted list',
ShortcutAction.formatChecklist => 'Checklist',
ShortcutAction.recordVoiceNote => 'Record a voice note',
+ ShortcutAction.pasteAsPlainText => 'Paste as plain text',
};
String get description => switch (this) {
@@ -108,6 +110,8 @@ extension ShortcutActionCopy on ShortcutAction {
ShortcutAction.formatChecklist => 'Toggle a checklist on the current lines',
ShortcutAction.recordVoiceNote =>
'Start recording into this note, or stop the one running',
+ ShortcutAction.pasteAsPlainText =>
+ 'Paste only the words, without the formatting they came with',
};
/// Registered with the operating system rather than the widget tree, so it
@@ -665,6 +669,14 @@ class ShortcutPrefs extends ChangeNotifier {
control: !useMeta,
shift: true,
),
+ // The chord browsers, Google Docs and Slack all use for it.
+ ShortcutAction.pasteAsPlainText => ShortcutBinding(
+ logicalKey: LogicalKeyboardKey.keyV,
+ physicalKey: PhysicalKeyboardKey.keyV,
+ meta: useMeta,
+ control: !useMeta,
+ shift: true,
+ ),
};
}
}
diff --git a/lib/images/image_clipboard.dart b/lib/images/image_clipboard.dart
index fd29a06..3a2350d 100644
--- a/lib/images/image_clipboard.dart
+++ b/lib/images/image_clipboard.dart
@@ -1,15 +1,27 @@
import 'dart:convert';
+import 'dart:isolate';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:pasteboard/pasteboard.dart';
+import '../data/blob_store.dart';
+import '../data/note_attachment.dart';
+import '../data/note_format.dart';
+import '../paste/clipboard_snapshot.dart';
+import '../paste/note_text.dart';
+import '../paste/paste_tree.dart' show PasteImage;
+import '../sync/aead.dart' show randomKey;
import 'image_codec.dart';
/// Keeps base64 expansion from turning a broad selection into a memory spike.
const int maxClipboardFragmentImages = 20;
const int maxClipboardFragmentBytes = 24 * 1024 * 1024;
+/// A fragment this large, counting its attachments' bytes, is written and
+/// read on another isolate, so its base64 and its markdown cost no frames.
+const int backgroundClipboardBytes = 32 * 1024;
+
/// What was on the clipboard, if it was a picture.
class ClipboardImage {
const ClipboardImage({required this.bytes, required this.name});
@@ -43,42 +55,129 @@ class ClipboardFragmentImage {
final double widthFactor;
}
-/// Text and pictures copied out of one editor selection, in document order.
+/// A file, voice note or video inside a rich clipboard fragment.
+///
+/// [ref] is the attachment as its note holds it, anchored at its own U+FFFC
+/// in the fragment's [NoteClipboardFragment.body]. Where its note stores it
+/// is no part of it: [bytes] are what a paste stores anew.
+class ClipboardFragmentAttachment {
+ const ClipboardFragmentAttachment({required this.ref, required this.bytes});
+
+ final NoteAttachmentRef ref;
+ final Uint8List bytes;
+}
+
+/// Text and attachments copied out of one editor selection, in document
+/// order.
class NoteClipboardFragment {
- const NoteClipboardFragment({required this.body, required this.images});
+ const NoteClipboardFragment({
+ required this.body,
+ required this.images,
+ this.attachments = const [],
+ this.formats = const [],
+ this.markdown,
+ });
final String body;
final List images;
+ /// The files, voice notes and videos that travel with the words.
+ final List attachments;
+
+ /// The bold, italic, heading and subtitle ranges over [body].
+ final List formats;
+
+ /// Whether [body] was written in a markdown note. Null when the fragment
+ /// does not say, as an older build's does not.
+ final bool? markdown;
+
static const _placeholder = '\uFFFC';
static const _marker = 'kapynotes:';
- /// The representation a plain-text-only destination receives.
+ /// Where a ref is stored and how it is sealed, which a paste makes anew.
+ /// The key in particular never goes near a clipboard.
+ static const _storageFields = {'hash', 'key', 'bytes', 'attachmentId'};
+
+ /// What an attachment that cannot be pasted as itself is pasted as.
+ static String labelFor(NoteAttachmentRef ref) =>
+ ref is NoteVoiceRef ? '[Voice note]' : '[Attachment]';
+
+ /// The words and every attachment's bytes, which decide whether the
+ /// fragment is written and read on another isolate.
+ int get size =>
+ body.length +
+ images.fold(0, (sum, image) => sum + image.bytes.length) +
+ attachments.fold(0, (sum, item) => sum + item.bytes.length);
+
+ /// The representation a plain-text-only destination receives, and the one
+ /// ChatGPT, Slack and GitHub take: a markdown note's own markdown, with
+ /// `![image]` for each picture, which has no address to give; a plain
+ /// note's own text, with each picture as `[Image]`.
String get plainText {
- final byOffset = {for (final image in images) image.offset: image};
+ final labels = {
+ for (final image in images)
+ image.offset: markdown == true ? '![image]' : '[Image]',
+ for (final attachment in attachments)
+ attachment.ref.offset: labelFor(attachment.ref),
+ };
return body.replaceAllMapped(
RegExp(_placeholder),
- (match) => byOffset.containsKey(match.start) ? '[Image]' : '[Attachment]',
+ (match) => labels[match.start] ?? '[Attachment]',
);
}
- /// The rich representation understood by mail, documents, and web editors.
+ /// The rich representation understood by mail, documents, and web editors:
+ /// the note's words as HTML, each picture an img whose data URI holds its
+ /// bytes, sized as the note shows it.
///
- /// A compact marker describes the Kapy Notes offsets without duplicating
- /// image bytes. The pixels live once, in each img data URI, which also makes
- /// this ordinary HTML useful to every other app rather than a private-only
- /// clipboard flavor.
+ /// Ahead of it, a compact marker describes the pictures' places for older
+ /// builds, which read only this HTML and only a fragment with pictures. It
+ /// points at the img tags rather than repeating their bytes.
String get html {
final ordered = [...images]..sort((a, b) => a.offset.compareTo(b.offset));
- final descriptor = base64Url.encode(
+ final words = noteHtml(
+ body,
+ markdown: markdown ?? false,
+ formats: formats,
+ embeds: {
+ for (final attachment in attachments)
+ attachment.ref.offset: _escapeHtml(labelFor(attachment.ref)),
+ for (var index = 0; index < ordered.length; index++)
+ ordered[index].offset: _imageHtml(index, ordered[index]),
+ },
+ );
+ if (ordered.isEmpty) return words;
+ return '$words';
+ }
+
+ /// The marker older builds read: the body with every attachment but the
+ /// pictures written as its label, since those builds carry only pictures,
+ /// and where in it each picture is.
+ String _describe(List ordered) {
+ final labels = {
+ for (final attachment in attachments)
+ attachment.ref.offset: labelFor(attachment.ref),
+ };
+ final moved = {};
+ var growth = 0;
+ final labelled = body.replaceAllMapped(RegExp(_placeholder), (match) {
+ final label = labels[match.start];
+ if (label == null) {
+ moved[match.start] = match.start + growth;
+ return _placeholder;
+ }
+ growth += label.length - 1;
+ return label;
+ });
+ return base64Url.encode(
utf8.encode(
jsonEncode({
'v': 1,
- 'body': body,
+ 'body': labelled,
'images': [
for (final image in ordered)
{
- 'o': image.offset,
+ 'o': moved[image.offset],
'm': image.mime,
'w': image.width,
'h': image.height,
@@ -88,52 +187,171 @@ class NoteClipboardFragment {
}),
),
);
- final byOffset = {
- for (var index = 0; index < ordered.length; index++)
- ordered[index].offset: (index: index, image: ordered[index]),
- };
- final result = StringBuffer(
- ''
- '',
- );
- var cursor = 0;
- for (final entry in byOffset.entries) {
- final offset = entry.key;
- if (offset < cursor ||
- offset >= body.length ||
- body.codeUnitAt(offset) != 0xFFFC) {
- continue;
- }
- result.write(_htmlText(body.substring(cursor, offset)));
- {
- final image = entry.value.image;
- result
- ..write('
');
- }
- cursor = offset + 1;
- }
- result.write(_htmlText(body.substring(cursor)));
- result.write('
');
- return result.toString();
}
+ /// A picture at the width the note shows it, against the column a paste
+ /// reads a width against, so pasting it back gives it the same width.
+ static String _imageHtml(int index, ClipboardFragmentImage image) {
+ final width =
+ (image.widthFactor.clamp(NoteImageRef.minWidthFactor, 1) *
+ PasteImage.referenceColumnWidth)
+ .round();
+ final height = image.width > 0 && image.height > 0
+ ? ' height="${(width * image.height / image.width).round()}"'
+ : '';
+ return ' ';
+ }
+
+ /// Everything a paste into a note needs to rebuild the selection exactly,
+ /// for Kapy Notes' own clipboard type: the body, its styles, whether it was
+ /// markdown, and each attachment's ref with the bytes it stores.
+ String get kapy => jsonEncode({
+ 'v': 2,
+ 'body': body,
+ 'markdown': ?markdown,
+ 'formats': [for (final range in formats) range.toJson()],
+ 'attachments': [
+ for (final image in images)
+ {
+ 'kind': 'image',
+ 'offset': image.offset,
+ 'mime': image.mime,
+ 'width': image.width,
+ 'height': image.height,
+ 'widthFactor': image.widthFactor,
+ 'data': base64Encode(image.bytes),
+ },
+ for (final attachment in attachments)
+ {
+ for (final MapEntry(:key, :value) in attachment.ref.toJson().entries)
+ if (!_storageFields.contains(key)) key: value,
+ 'data': base64Encode(attachment.bytes),
+ },
+ ],
+ });
+
Map get platformData {
final ordered = [...images]..sort((a, b) => a.offset.compareTo(b.offset));
return {
'text': plainText,
'html': html,
+ 'kapy': kapy,
if (ordered.isNotEmpty) 'image': ordered.first.bytes,
if (ordered.isNotEmpty) 'imageMime': ordered.first.mime,
};
}
+ /// The fragment a clipboard [snapshot] carries: Kapy Notes' own type
+ /// first, then the marker in the HTML, which is all an older build wrote.
+ static Future fromSnapshot(
+ ClipboardSnapshot snapshot,
+ ) async {
+ final kapy = snapshot.kapy;
+ final fragment = kapy == null
+ ? null
+ : kapy.length < backgroundClipboardBytes
+ ? fromKapy(kapy)
+ : await Isolate.run(() => fromKapy(kapy));
+ return fragment ?? fromHtml(snapshot.html);
+ }
+
+ /// Reads [kapy] back. Null unless it is whole and well formed, every U+FFFC
+ /// in its body is one of its attachments, and it keeps to the caps a copy
+ /// keeps to.
+ static NoteClipboardFragment? fromKapy(String? source) {
+ if (source == null || source.isEmpty) return null;
+ try {
+ final raw = jsonDecode(source);
+ if (raw is! Map || raw['v'] != 2) return null;
+ final body = raw['body'];
+ final markdown = raw['markdown'];
+ final entries = raw['attachments'];
+ if (body is! String ||
+ (markdown != null && markdown is! bool) ||
+ entries is! List ||
+ entries.length > maxClipboardFragmentImages) {
+ return null;
+ }
+
+ final images = [];
+ final attachments = [];
+ final offsets = {};
+ var budget = maxClipboardFragmentBytes;
+ for (final entry in entries) {
+ if (entry is! Map) return null;
+ final offset = entry['offset'];
+ final data = entry['data'];
+ if (offset is! int ||
+ offset < 0 ||
+ offset >= body.length ||
+ body.codeUnitAt(offset) != 0xFFFC ||
+ !offsets.add(offset) ||
+ data is! String ||
+ data.length > ((budget + 2) ~/ 3) * 4) {
+ return null;
+ }
+ final bytes = base64Decode(data);
+ budget -= bytes.length;
+ if (bytes.isEmpty || budget < 0) return null;
+
+ if (entry['kind'] == 'image') {
+ final mime = entry['mime'];
+ final width = entry['width'];
+ final height = entry['height'];
+ final factor = entry['widthFactor'];
+ if (mime is! String ||
+ !supportedImageMimes.contains(mime.toLowerCase()) ||
+ width is! int ||
+ width <= 0 ||
+ height is! int ||
+ height <= 0 ||
+ factor is! num) {
+ return null;
+ }
+ images.add(
+ ClipboardFragmentImage(
+ offset: offset,
+ bytes: bytes,
+ mime: mime,
+ width: width,
+ height: height,
+ widthFactor: factor.toDouble().clamp(
+ NoteImageRef.minWidthFactor,
+ 1,
+ ),
+ ),
+ );
+ continue;
+ }
+
+ final ref = NoteAttachmentRef.fromJson({
+ for (final MapEntry(:key, :value) in entry.entries)
+ if (key != 'data') '$key': value,
+ 'hash': BlobStore.hashOf(bytes),
+ 'key': base64Encode(randomKey()),
+ 'bytes': bytes.length,
+ });
+ if (ref == null || ref is NoteImageRef || ref is NoteUnknownRef) {
+ return null;
+ }
+ attachments.add(ClipboardFragmentAttachment(ref: ref, bytes: bytes));
+ }
+ if (_placeholder.allMatches(body).length != offsets.length) return null;
+
+ return NoteClipboardFragment(
+ body: body,
+ images: images,
+ attachments: attachments,
+ formats: noteFormatsFromJson(raw['formats'], body.length),
+ markdown: markdown as bool?,
+ );
+ } catch (_) {
+ return null;
+ }
+ }
+
/// Recovers a fragment only from HTML emitted by this app.
///
/// Foreign HTML is deliberately ignored: it should continue through the
@@ -227,10 +445,6 @@ class NoteClipboardFragment {
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"');
-
- static String _htmlText(String source) => _escapeHtml(
- source.replaceAll(_placeholder, '[Attachment]'),
- ).replaceAll('\n', ' ');
}
/// Reads an image from the system clipboard, or returns null.
@@ -249,6 +463,9 @@ class ImageClipboard {
static const _richChannel = MethodChannel('kapynotes/rich_clipboard');
+ /// Everything the clipboard holds, read at once.
+ Future readSnapshot() => ClipboardSnapshot.read();
+
/// A Kapy Notes rich fragment, when the clipboard carries one.
Future readFragment() async {
try {
@@ -260,9 +477,13 @@ class ImageClipboard {
}
}
- /// Publishes the rich fragment in native, HTML, and plain-text forms.
+ /// Publishes the rich fragment in every form at once: plain text, HTML,
+ /// Kapy Notes' own type, and its first picture as a native image.
Future writeFragment(NoteClipboardFragment fragment) async {
- await _richChannel.invokeMethod('write', fragment.platformData);
+ final data = fragment.size < backgroundClipboardBytes
+ ? fragment.platformData
+ : await Isolate.run(() => fragment.platformData);
+ await _richChannel.invokeMethod('write', data);
}
/// Bitmap data on the clipboard, already decoded to bytes.
diff --git a/lib/paste/clipboard_snapshot.dart b/lib/paste/clipboard_snapshot.dart
new file mode 100644
index 0000000..ad9ce1a
--- /dev/null
+++ b/lib/paste/clipboard_snapshot.dart
@@ -0,0 +1,193 @@
+import 'dart:convert';
+
+import 'package:flutter/foundation.dart';
+import 'package:flutter/services.dart';
+
+import 'html_reader.dart' show PasteResources;
+
+/// Everything the clipboard held at one moment.
+///
+/// Read in one native call, so its representations agree with each other
+/// (a dictation app can't swap the text out between two reads) and iOS asks
+/// for permission at most once.
+class ClipboardSnapshot {
+ const ClipboardSnapshot({
+ this.types = const [],
+ this.text,
+ this.html,
+ this.sourceUrl,
+ this.rtfHtml,
+ this.resources = const {},
+ this.vscode,
+ this.kapy,
+ this.files = const [],
+ this.image,
+ this.imageMime,
+ });
+
+ /// The platform's own names for what was there.
+ final List types;
+ final String? text;
+ final String? html;
+
+ /// The page the copy came from, for resolving its relative links.
+ final String? sourceUrl;
+
+ /// Rich text (RTF, RTFD) converted to HTML by the platform.
+ final String? rtfHtml;
+
+ /// Picture bytes the clipboard carried beside its markup, by the name or
+ /// URL the markup refers to them by.
+ final PasteResources resources;
+
+ /// VS Code's description of a copy, as JSON.
+ final String? vscode;
+
+ /// Kapy Notes' own fragment, from its private clipboard type.
+ final String? kapy;
+
+ /// Paths of copied files.
+ final List files;
+ final Uint8List? image;
+ final String? imageMime;
+
+ /// The language VS Code says the copied code is in.
+ String? get vscodeMode {
+ final raw = vscode;
+ if (raw == null) return null;
+ try {
+ final decoded = jsonDecode(raw);
+ if (decoded is Map && decoded['mode'] is String) {
+ final mode = (decoded['mode'] as String).trim();
+ return mode.isEmpty || mode == 'plaintext' ? null : mode;
+ }
+ } on FormatException {
+ return null;
+ }
+ return null;
+ }
+
+ static const _channel = MethodChannel('kapynotes/rich_clipboard');
+
+ /// Reads the clipboard, or the private pasteboard named [pasteboard] on
+ /// Apple platforms, which is how tests avoid the user's own clipboard.
+ static Future read({String? pasteboard}) async {
+ try {
+ final raw = await _channel.invokeMethod('read', {
+ 'pasteboard': ?pasteboard,
+ });
+ return fromChannel(raw);
+ } on MissingPluginException {
+ return _readLegacy();
+ } on PlatformException catch (error) {
+ debugPrint('KapyNotes: could not read the clipboard: $error');
+ return _readLegacy();
+ }
+ }
+
+ /// What an older native side offers: the text and the HTML, read apart.
+ static Future _readLegacy() async {
+ String? html;
+ try {
+ html = await _channel.invokeMethod('readHtml');
+ } on Object {
+ html = null;
+ }
+ final text = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
+ return fromChannel({'text': text, 'html': html});
+ }
+
+ @visibleForTesting
+ static ClipboardSnapshot fromChannel(Object? raw) {
+ if (raw is! Map) return const ClipboardSnapshot();
+ String? string(String key) {
+ final value = raw[key];
+ return value is String && value.isNotEmpty ? value : null;
+ }
+
+ var html = string('html');
+ var sourceUrl = string('sourceUrl');
+ if (html != null && html.startsWith('Version:')) {
+ final parsed = _parseWindowsHtml(html);
+ html = parsed.html;
+ sourceUrl ??= parsed.sourceUrl;
+ }
+ final resources = {};
+ final rawResources = raw['resources'];
+ if (rawResources is List) {
+ for (final entry in rawResources) {
+ if (entry is! Map) continue;
+ final name = entry['name'];
+ final data = entry['data'];
+ final mime = entry['mime'];
+ if (name is String && data is Uint8List && data.isNotEmpty) {
+ resources[name] = (
+ bytes: data,
+ mime: mime is String && mime.isNotEmpty
+ ? mime.toLowerCase()
+ : 'application/octet-stream',
+ );
+ }
+ }
+ }
+ final types = raw['types'];
+ final files = raw['files'];
+ final image = raw['image'];
+ return ClipboardSnapshot(
+ types: types is List ? [...types.whereType()] : const [],
+ text: string('text')?.replaceAll('\r\n', '\n'),
+ html: html,
+ sourceUrl: sourceUrl,
+ rtfHtml: string('rtfHtml'),
+ resources: resources,
+ vscode: string('vscode'),
+ kapy: string('kapy'),
+ files: files is List ? [...files.whereType()] : const [],
+ image: image is Uint8List && image.isNotEmpty ? image : null,
+ imageMime: string('imageMime'),
+ );
+ }
+
+ /// Windows' "HTML Format": a header of byte offsets and the page's
+ /// address, then the markup.
+ static ({String html, String? sourceUrl}) _parseWindowsHtml(String raw) {
+ String? sourceUrl;
+ int? startHtml;
+ int? endHtml;
+ final markup = raw.indexOf('<');
+ for (final line in const LineSplitter().convert(
+ markup < 0 ? raw : raw.substring(0, markup),
+ )) {
+ final colon = line.indexOf(':');
+ if (colon <= 0) continue;
+ final name = line.substring(0, colon);
+ final value = line.substring(colon + 1).trim();
+ switch (name) {
+ case 'StartHTML':
+ startHtml = int.tryParse(value);
+ case 'EndHTML':
+ endHtml = int.tryParse(value);
+ case 'SourceURL':
+ if (value.isNotEmpty) sourceUrl = value;
+ }
+ }
+ final bytes = utf8.encode(raw);
+ if (startHtml != null &&
+ endHtml != null &&
+ startHtml >= 0 &&
+ startHtml < endHtml &&
+ endHtml <= bytes.length) {
+ return (
+ html: utf8.decode(
+ bytes.sublist(startHtml, endHtml),
+ allowMalformed: true,
+ ),
+ sourceUrl: sourceUrl,
+ );
+ }
+ return (
+ html: markup < 0 ? '' : raw.substring(markup),
+ sourceUrl: sourceUrl,
+ );
+ }
+}
diff --git a/lib/paste/html_reader.dart b/lib/paste/html_reader.dart
new file mode 100644
index 0000000..a401dea
--- /dev/null
+++ b/lib/paste/html_reader.dart
@@ -0,0 +1,1930 @@
+import 'dart:convert';
+import 'dart:typed_data';
+
+import 'package:html/dom.dart' as dom;
+import 'package:html/parser.dart' as html_parser;
+
+import 'mathml_tex.dart';
+import 'paste_tree.dart';
+
+/// Image bytes a clipboard carried alongside its markup, by the URL or file
+/// name the markup refers to them by: Safari's web archive, an RTFD's
+/// attachments.
+typedef PasteResources = Map;
+
+/// Reads clipboard HTML into [PasteBlock]s.
+///
+/// HTML on a clipboard is whatever the source app thought would survive a
+/// trip into another app: a browser's serialization of the selection with
+/// computed styles inlined, Google Docs' spans with weights in their style
+/// attributes, Word's paragraphs pretending to be lists, KaTeX's three copies
+/// of every formula. This keeps what was meant — structure, emphasis, links,
+/// math, code, pictures — and throws away how it was drawn.
+class HtmlPasteReader {
+ HtmlPasteReader({this.baseUrl, this.resources = const {}});
+
+ /// The page the selection came from, for its relative links and pictures.
+ final Uri? baseUrl;
+ final PasteResources resources;
+
+ final _CssSheet _css = _CssSheet();
+
+ /// The size most of the words are set in, for markup that says what is a
+ /// heading only by its size: Apple's, from Notes, Pages and TextEdit.
+ double? _textSize;
+
+ /// Whether the markup is a word processor's (Google Docs, Word), where
+ /// code is written as paragraphs set in a monospace face.
+ var _editorDocument = false;
+
+ List read(String html) {
+ final document = html_parser.parse(_withoutDownlevelConditionals(html));
+ for (final style in document.querySelectorAll('style')) {
+ _css.add(style.text);
+ }
+ final root = document.body ?? document.documentElement;
+ if (root == null) return const [];
+ final generator = document
+ .querySelector('meta[name="Generator"]')
+ ?.attributes['content'];
+ _textSize = generator != null && generator.startsWith('Cocoa HTML Writer')
+ ? _commonSize(root)
+ : null;
+ _editorDocument =
+ html.contains('docs-internal-guid') ||
+ (generator?.startsWith('Microsoft') ?? false);
+ _rewriteWordLists(root);
+ _splitTaskItems(root);
+ final out = _Builder();
+ _walkChildren(root, const _Context(), out);
+ out.flush();
+ return _checklists(_mergeAdjacentCode(out.blocks));
+ }
+
+ /// Whether [html] is code that a code editor copied: every word set in a
+ /// monospace face and nothing else to say — no headings, lists, links,
+ /// tables or pictures. JetBrains, Xcode's rich text and terminals write
+ /// their colours this way, and the words are the code.
+ bool readsAsCode(String html) {
+ final document = html_parser.parse(html);
+ for (final style in document.querySelectorAll('style')) {
+ _css.add(style.text);
+ }
+ final root = document.body;
+ if (root == null) return false;
+ const structure =
+ 'pre, code, h1, h2, h3, h4, h5, h6, li, table, img, a[href], '
+ 'blockquote';
+ if (root.querySelector(structure) != null) return false;
+ var words = false;
+ final pending = [root];
+ while (pending.isNotEmpty) {
+ final node = pending.removeLast();
+ if (node is dom.Text) {
+ if (node.data.trim().isEmpty) continue;
+ words = true;
+ if (!_monospaced(node.parent)) return false;
+ continue;
+ }
+ if (node is dom.Element && _dropped.contains(node.localName)) continue;
+ pending.addAll(node.nodes);
+ }
+ return words;
+ }
+
+ bool _monospaced(dom.Element? element) {
+ for (var at = element; at != null; at = at.parent) {
+ final declarations = _css.declarationsFor(at);
+ final face =
+ declarations['font-family'] ??
+ declarations['font'] ??
+ at.attributes['face'];
+ if (face != null) {
+ return _monospace.hasMatch(face.split(',').first);
+ }
+ }
+ return false;
+ }
+
+ /// Whether a word processor's paragraph is a line of code: every word in
+ /// it set in a monospace face, with nothing in it a code block can't hold.
+ bool _isCodeParagraph(dom.Element paragraph) {
+ if (paragraph.querySelector('a[href], img') != null) return false;
+ var words = false;
+ final pending = [paragraph];
+ while (pending.isNotEmpty) {
+ final node = pending.removeLast();
+ if (node is dom.Text) {
+ if (node.data.trim().isEmpty) continue;
+ words = true;
+ if (!_monospaced(node.parent)) return false;
+ continue;
+ }
+ pending.addAll(node.nodes);
+ }
+ return words;
+ }
+
+ static final _monospace = RegExp(
+ r'mono|menlo|monaco|consolas|courier|jetbrains|fira ?code|source ?code|'
+ r'cascadia|inconsolata|hack\b',
+ caseSensitive: false,
+ );
+
+ // ---------------------------------------------------------------------
+ // Walking
+
+ void _walkChildren(dom.Node parent, _Context context, _Builder out) {
+ for (final node in parent.nodes) {
+ _walk(node, context, out);
+ }
+ }
+
+ void _walk(dom.Node node, _Context context, _Builder out) {
+ if (node is dom.Text) {
+ out.line.addText(node.data, context);
+ return;
+ }
+ if (node is! dom.Element) return;
+ final element = node;
+ final tag = element.localName ?? '';
+ if (_dropped.contains(tag) || _droppedClass(element)) return;
+ final declarations = _css.declarationsFor(element);
+ if (_isHidden(element, declarations) &&
+ !_isMath(element) &&
+ element.querySelector('math') == null) {
+ return;
+ }
+
+ if (_isMath(element)) {
+ final math = _readMath(element);
+ if (math != null) {
+ if (math.display) {
+ out.flush();
+ out.blocks.add(PasteMathBlock(math.tex));
+ } else {
+ out.line.add(PasteMath(math.tex));
+ }
+ return;
+ }
+ }
+
+ // Perplexity's source chip: its source's name, linked, and set apart
+ // from the sentence it ends.
+ final citation = element.attributes['data-pplx-citation-url'];
+ if (citation != null) {
+ final label = element.text.replaceAll(RegExp(r'\s*\+\d+\s*$'), '').trim();
+ final href = _resolveLink(citation);
+ if (label.isNotEmpty && href != null) {
+ out.line
+ ..addText(' ', context)
+ ..addText(label, _Context(styles: context.styles, href: href));
+ }
+ return;
+ }
+
+ final inner = _styled(element, declarations, context);
+
+ if (tag == 'p' && _editorDocument && _isCodeParagraph(element)) {
+ out.flush();
+ out.blocks.add(PasteCode(_codeText(element).trimRight()));
+ return;
+ }
+
+ final wordTitle = tag == 'p'
+ ? _wordTitleLevel(element) ?? _sizedTitleLevel(element)
+ : null;
+ if (wordTitle != null) {
+ out.flush();
+ final inlines = _inlinesOf(element, inner);
+ if (_hasWords(inlines)) {
+ out.blocks.add(PasteHeading(wordTitle, _unboldedHeading(inlines)));
+ }
+ return;
+ }
+
+ switch (tag) {
+ case 'h1' || 'h2' || 'h3' || 'h4' || 'h5' || 'h6':
+ out.flush();
+ final inlines = _inlinesOf(element, inner);
+ if (_hasWords(inlines)) {
+ out.blocks.add(
+ PasteHeading(
+ int.parse(tag.substring(1)),
+ _unboldedHeading(inlines),
+ ),
+ );
+ }
+ return;
+ case 'ul' || 'ol' || 'menu' || 'dir':
+ out.flush();
+ final list = _readList(element, inner);
+ if (list.items.isNotEmpty) out.blocks.add(list);
+ return;
+ case 'li':
+ // An item with no list around it: what a selection that starts
+ // inside a list serialises as.
+ out.flush();
+ out.blocks.add(
+ PasteList(ordered: false, items: [_readItem(element, inner)]),
+ );
+ return;
+ case 'blockquote':
+ out.flush();
+ final blocks = _blocksOf(element, inner);
+ if (blocks.isNotEmpty) out.blocks.add(PasteQuote(blocks));
+ return;
+ case 'pre':
+ out.flush();
+ final code = _readPre(element);
+ if (code != null) out.blocks.add(code);
+ return;
+ case 'code' || 'kbd' || 'samp' || 'tt':
+ final text = _codeText(element);
+ if (text.contains('\n') && text.trim().isNotEmpty) {
+ out.flush();
+ out.blocks.add(
+ PasteCode(_trimCode(text), language: _languageOf(element)),
+ );
+ } else if (text.isNotEmpty) {
+ out.line.add(
+ PasteCodeSpan(text, styles: inner.styles, href: inner.href),
+ );
+ }
+ return;
+ case 'table':
+ out.flush();
+ _readTable(element, inner, out);
+ return;
+ case 'hr':
+ out.flush();
+ out.blocks.add(const PasteRule());
+ return;
+ case 'br':
+ out.line.addBreak(inner);
+ return;
+ case 'img':
+ _readImage(element, inner, out);
+ return;
+ case 'input':
+ final type = element.attributes['type']?.toLowerCase();
+ if (type == 'checkbox') {
+ final checked = element.attributes.containsKey('checked');
+ out.line.addText(checked ? '☑ ' : '☐ ', inner);
+ }
+ return;
+ case 'rt':
+ out.line.addText('(', inner);
+ _walkChildren(element, inner, out);
+ out.line.addText(')', inner);
+ return;
+ case 'dt' || 'summary':
+ out.flush();
+ _walkChildren(element, inner.withStyle(PasteStyle.bold), out);
+ out.flush();
+ return;
+ }
+
+ if (_isBlock(tag, declarations)) {
+ out.flush();
+ _walkChildren(element, inner, out);
+ out.flush();
+ return;
+ }
+ _walkChildren(element, inner, out);
+ }
+
+ List _blocksOf(dom.Element element, _Context context) {
+ final out = _Builder();
+ _walkChildren(element, context, out);
+ out.flush();
+ return out.blocks;
+ }
+
+ /// A heading's or a cell's words: its blocks flattened into one run, each
+ /// block after the first on a line of its own.
+ List _inlinesOf(dom.Element element, _Context context) {
+ final out = _Builder();
+ _walkChildren(element, context, out);
+ out.flush();
+ return _flatten(out.blocks);
+ }
+
+ List _flatten(List blocks, {String bullet = ''}) {
+ final result = [];
+ void lineBreak() {
+ if (result.isNotEmpty && result.last is! PasteBreak) {
+ result.add(const PasteBreak());
+ }
+ }
+
+ for (final block in blocks) {
+ switch (block) {
+ case PasteParagraph(:final inlines) || PasteHeading(:final inlines):
+ lineBreak();
+ if (bullet.isNotEmpty) result.add(PasteText(bullet));
+ result.addAll(inlines);
+ case PasteList(:final items, :final ordered, :final start):
+ for (var i = 0; i < items.length; i++) {
+ final item = items[i];
+ final marker = item.checked != null
+ ? (item.checked! ? '☑ ' : '☐ ')
+ : ordered
+ ? '${start + i}. '
+ : '• ';
+ lineBreak();
+ result.addAll(_flatten(item.blocks, bullet: marker));
+ }
+ case PasteQuote(:final blocks):
+ lineBreak();
+ result.addAll(_flatten(blocks));
+ case PasteCode(:final text):
+ lineBreak();
+ result.add(PasteCodeSpan(text.replaceAll('\n', ' ')));
+ case PasteMathBlock(:final tex):
+ lineBreak();
+ result.add(PasteMath(tex));
+ case PasteTable(:final rows):
+ for (final row in rows) {
+ lineBreak();
+ for (var c = 0; c < row.length; c++) {
+ if (c > 0) result.add(const PasteText(' · '));
+ result.addAll(row[c]);
+ }
+ }
+ case PasteRule():
+ lineBreak();
+ }
+ }
+ while (result.isNotEmpty && result.last is PasteBreak) {
+ result.removeLast();
+ }
+ return result;
+ }
+
+ // ---------------------------------------------------------------------
+ // Styles
+
+ _Context _styled(
+ dom.Element element,
+ Map declarations,
+ _Context context,
+ ) {
+ final styles = {...context.styles};
+ switch (element.localName) {
+ case 'b' || 'strong':
+ styles.add(PasteStyle.bold);
+ case 'i' || 'em' || 'cite' || 'var' || 'dfn':
+ styles.add(PasteStyle.italic);
+ case 'u' || 'ins':
+ styles.add(PasteStyle.underline);
+ case 's' || 'strike' || 'del':
+ styles.add(PasteStyle.strike);
+ case 'sup':
+ styles.add(PasteStyle.superscript);
+ case 'sub':
+ styles.add(PasteStyle.subscript);
+ }
+ final weight = declarations['font-weight'];
+ if (weight != null) {
+ final numeric = int.tryParse(weight);
+ if (weight == 'bold' ||
+ weight == 'bolder' ||
+ (numeric != null && numeric >= 600)) {
+ styles.add(PasteStyle.bold);
+ } else if (weight == 'normal' ||
+ weight == 'lighter' ||
+ (numeric != null && numeric < 600)) {
+ styles.remove(PasteStyle.bold);
+ }
+ }
+ final fontStyle = declarations['font-style'];
+ if (fontStyle == 'italic' || fontStyle == 'oblique') {
+ styles.add(PasteStyle.italic);
+ } else if (fontStyle == 'normal') {
+ styles.remove(PasteStyle.italic);
+ }
+ final decoration =
+ '${declarations['text-decoration'] ?? ''} '
+ '${declarations['text-decoration-line'] ?? ''}';
+ if (decoration.contains('underline')) styles.add(PasteStyle.underline);
+ if (decoration.contains('line-through')) styles.add(PasteStyle.strike);
+ final vertical = declarations['vertical-align'];
+ if (vertical == 'super') styles.add(PasteStyle.superscript);
+ if (vertical == 'sub') styles.add(PasteStyle.subscript);
+
+ var href = context.href;
+ if (element.localName == 'a') {
+ href = _resolveLink(element.attributes['href']);
+ }
+ // A link says it is one by being a link; the underline a page draws under
+ // it is not emphasis the writer chose.
+ if (href != null) styles.remove(PasteStyle.underline);
+
+ final whiteSpace = declarations['white-space'];
+ var pre = context.pre;
+ var preLine = context.preLine;
+ if (whiteSpace != null) {
+ preLine = whiteSpace == 'pre-line';
+ pre =
+ !preLine &&
+ (whiteSpace.startsWith('pre') || whiteSpace == 'break-spaces');
+ }
+ return _Context(styles: styles, href: href, pre: pre, preLine: preLine);
+ }
+
+ bool _isHidden(dom.Element element, Map declarations) {
+ if (element.attributes.containsKey('hidden')) return true;
+ if (element.attributes['aria-hidden'] == 'true') return true;
+ if (declarations['display'] == 'none') return true;
+ if (declarations['visibility'] == 'hidden') return true;
+ for (final name in element.classes) {
+ if (_screenReaderOnly.contains(name)) return true;
+ }
+ return false;
+ }
+
+ static bool _isBlock(String tag, Map declarations) {
+ final display = declarations['display'];
+ if (display != null) {
+ if (display.startsWith('inline')) return false;
+ if (display == 'block' ||
+ display == 'flex' ||
+ display == 'grid' ||
+ display == 'list-item' ||
+ display.startsWith('table') ||
+ display == 'flow-root') {
+ return true;
+ }
+ }
+ return _blockTags.contains(tag);
+ }
+
+ // ---------------------------------------------------------------------
+ // Lists
+
+ PasteList _readList(dom.Element list, _Context context) {
+ final ordered = list.localName == 'ol';
+ final start = int.tryParse(list.attributes['start'] ?? '') ?? 1;
+ final items = [];
+ final levels = [];
+ for (final child in list.nodes) {
+ if (child is dom.Text) {
+ if (child.data.trim().isEmpty) continue;
+ items.add(
+ PasteListItem([
+ PasteParagraph([PasteText(child.data.trim())]),
+ ]),
+ );
+ levels.add(null);
+ continue;
+ }
+ if (child is! dom.Element) continue;
+ final declarations = _css.declarationsFor(child);
+ if (_isHidden(child, declarations)) continue;
+ final tag = child.localName;
+ if (tag == 'ul' || tag == 'ol') {
+ // A list straight inside a list — Google Docs and hand-written HTML
+ // both do it — belongs to the item before it.
+ final nested = _readList(child, _styled(child, declarations, context));
+ if (nested.items.isEmpty) continue;
+ if (items.isEmpty) {
+ items.add(PasteListItem([nested]));
+ levels.add(null);
+ } else {
+ final last = items.removeLast();
+ items.add(
+ PasteListItem([...last.blocks, nested], checked: last.checked),
+ );
+ }
+ continue;
+ }
+ items.add(_readItem(child, _styled(child, declarations, context)));
+ levels.add(int.tryParse(child.attributes['aria-level'] ?? ''));
+ }
+ final nestedByLevel = _nestByLevel(items, levels, ordered);
+ return PasteList(
+ ordered: ordered,
+ start: start,
+ items: nestedByLevel ?? items,
+ );
+ }
+
+ /// Google Docs writes one flat list and says each item's depth in
+ /// `aria-level`. Rebuilds the nesting it means, or returns null when the
+ /// levels say nothing.
+ List? _nestByLevel(
+ List items,
+ List levels,
+ bool ordered,
+ ) {
+ final known = levels.whereType().toSet();
+ if (known.length < 2 || levels.contains(null)) return null;
+ final base = known.reduce((a, b) => a < b ? a : b);
+ List build(int from, int to, int level) {
+ final result = [];
+ var i = from;
+ while (i < to) {
+ final item = items[i];
+ var j = i + 1;
+ while (j < to && levels[j]! > level) {
+ j++;
+ }
+ if (j > i + 1) {
+ final children = build(i + 1, j, levels[i + 1]!);
+ result.add(
+ PasteListItem([
+ ...item.blocks,
+ PasteList(ordered: ordered, items: children),
+ ], checked: item.checked),
+ );
+ } else {
+ result.add(item);
+ }
+ i = j;
+ }
+ return result;
+ }
+
+ return build(0, items.length, base);
+ }
+
+ PasteListItem _readItem(dom.Element item, _Context context) {
+ bool? checked;
+ final role = item.attributes['role'];
+ if (role == 'checkbox') {
+ checked = item.attributes['aria-checked'] == 'true';
+ }
+ final box = item.querySelector(
+ 'input[type=checkbox], input[type="checkbox"]',
+ );
+ if (box != null && _belongsToItem(box, item)) {
+ checked = box.attributes.containsKey('checked');
+ box.remove();
+ }
+ final notionBox = item.querySelector('.checkbox');
+ if (checked == null && notionBox != null) {
+ checked = notionBox.classes.contains('checkbox-on');
+ notionBox.remove();
+ }
+ var blocks = _blocksOf(item, context);
+ if (checked == null && blocks.isNotEmpty) {
+ final (marker, rest) = _leadingTaskMarker(blocks.first);
+ if (marker != null) {
+ checked = marker;
+ blocks = [?rest, ...blocks.skip(1)];
+ }
+ }
+ return PasteListItem(blocks, checked: checked);
+ }
+
+ /// Whether a checkbox is this item's own rather than one in a list nested
+ /// inside it.
+ static bool _belongsToItem(dom.Element box, dom.Element item) {
+ for (var parent = box.parent; parent != null; parent = parent.parent) {
+ if (identical(parent, item)) return true;
+ if (parent.localName == 'li') return false;
+ }
+ return false;
+ }
+
+ /// An item whose words open with a box drawn as a character — `☐`, `☑`,
+ /// `[ ]`, `[x]` — which is how many apps export a checklist.
+ static (bool?, PasteBlock?) _leadingTaskMarker(PasteBlock block) {
+ if (block is! PasteParagraph || block.inlines.isEmpty) return (null, block);
+ final first = block.inlines.first;
+ if (first is! PasteText) return (null, block);
+ final match = RegExp(
+ r'^\s*(☐|☑|☒|✅|\[ \]|\[[xX]\])\s+',
+ ).firstMatch(first.text);
+ if (match == null) return (null, block);
+ final marker = match.group(1)!;
+ final checked = marker != '☐' && marker != '[ ]';
+ final remaining = first.text.substring(match.end);
+ return (
+ checked,
+ PasteParagraph([
+ if (remaining.isNotEmpty)
+ PasteText(remaining, styles: first.styles, href: first.href),
+ ...block.inlines.skip(1),
+ ]),
+ );
+ }
+
+ // ---------------------------------------------------------------------
+ // Code
+
+ PasteCode? _readPre(dom.Element pre) {
+ final codes = pre.querySelectorAll('code');
+ final code = codes.length == 1 ? codes.single : null;
+ final text = _codeText(code ?? pre);
+ if (text.trim().isEmpty) return null;
+ return PasteCode(
+ _trimCode(text),
+ language:
+ (code == null ? null : _languageOf(code)) ??
+ _languageOf(pre) ??
+ _languageOfAncestors(pre) ??
+ _toolbarLanguage(pre, code),
+ );
+ }
+
+ /// The text of a code element as it reads: line breaks kept, toolbars and
+ /// line numbers left out.
+ String _codeText(dom.Element element) {
+ final buffer = StringBuffer();
+ void visit(dom.Node node) {
+ if (node is dom.Text) {
+ buffer.write(node.data);
+ return;
+ }
+ if (node is! dom.Element) return;
+ final tag = node.localName;
+ if (tag == 'br') {
+ buffer.write('\n');
+ return;
+ }
+ if (_dropped.contains(tag) || _droppedClass(node)) return;
+ if (_isHidden(node, _css.declarationsFor(node))) return;
+ if (_lineNumberClass(node)) return;
+ final block =
+ _isBlock(tag ?? '', _css.declarationsFor(node)) && tag != 'code';
+ if (block && buffer.isNotEmpty && !_endsWithNewline(buffer)) {
+ buffer.write('\n');
+ }
+ for (final child in node.nodes) {
+ visit(child);
+ }
+ if (block && !_endsWithNewline(buffer)) buffer.write('\n');
+ }
+
+ for (final child in element.nodes) {
+ visit(child);
+ }
+ return buffer.toString().replaceAll('\r\n', '\n').replaceAll('\u00a0', ' ');
+ }
+
+ static bool _endsWithNewline(StringBuffer buffer) =>
+ buffer.isNotEmpty && buffer.toString().endsWith('\n');
+
+ static String _trimCode(String text) {
+ var result = text;
+ while (result.startsWith('\n')) {
+ result = result.substring(1);
+ }
+ return result.trimRight();
+ }
+
+ static final _languageClass = RegExp(
+ r'(?:^|\s)(?:language|lang|highlight-source|brush:?)-?([A-Za-z0-9_+#.-]+)',
+ );
+
+ String? _languageOf(dom.Element element) {
+ final declared =
+ element.attributes['data-language'] ??
+ element.attributes['data-lang'] ??
+ element.attributes['lang'];
+ if (declared != null && _plausibleLanguage(declared)) {
+ return declared.toLowerCase();
+ }
+ final match = _languageClass.firstMatch(element.className);
+ final language = match?.group(1);
+ if (language == null) return null;
+ return _normalizedLanguage(language);
+ }
+
+ String? _languageOfAncestors(dom.Element element) {
+ var parent = element.parent;
+ for (var depth = 0; parent != null && depth < 3; depth++) {
+ final language = _languageOf(parent);
+ if (language != null) return language;
+ parent = parent.parent;
+ }
+ return null;
+ }
+
+ /// The label above a code block in ChatGPT, Claude and Gemini: a short
+ /// word on its own in the block's header, outside the code itself.
+ String? _toolbarLanguage(dom.Element pre, dom.Element? code) {
+ final container = code == null ? pre : _blockContainerOf(pre);
+ for (final candidate in container.querySelectorAll('div, span')) {
+ if (code != null && _contains(candidate, code)) continue;
+ if (code != null && _contains(code, candidate)) continue;
+ if (candidate.children.isNotEmpty) continue;
+ final text = candidate.text.trim();
+ if (_plausibleLanguage(text)) return _normalizedLanguage(text);
+ }
+ return null;
+ }
+
+ static dom.Element _blockContainerOf(dom.Element pre) {
+ final parent = pre.parent;
+ if (parent != null && parent.localName != 'body') {
+ final label = parent.querySelectorAll('pre');
+ if (label.length == 1) return parent;
+ }
+ return pre;
+ }
+
+ static bool _contains(dom.Element outer, dom.Element inner) {
+ for (dom.Node? node = inner; node != null; node = node.parent) {
+ if (identical(node, outer)) return true;
+ }
+ return false;
+ }
+
+ static bool _plausibleLanguage(String text) =>
+ text.isNotEmpty &&
+ text.length <= 24 &&
+ RegExp(r'^[A-Za-z][A-Za-z0-9_+#.\- ]*$').hasMatch(text) &&
+ !_notLanguages.contains(text.toLowerCase());
+
+ static String _normalizedLanguage(String raw) {
+ final lower = raw.trim().toLowerCase();
+ return switch (lower) {
+ 'c++' => 'cpp',
+ 'c#' => 'csharp',
+ 'f#' => 'fsharp',
+ 'objective-c' => 'objectivec',
+ 'shell' || 'bash' || 'sh' || 'zsh' || 'console' => lower,
+ _ => lower.replaceAll(' ', '-'),
+ };
+ }
+
+ static bool _lineNumberClass(dom.Element element) {
+ for (final name in element.classes) {
+ if (name == 'linenumber' ||
+ name == 'line-number' ||
+ name == 'lineno' ||
+ name == 'gutter' ||
+ name == 'react-syntax-highlighter-line-number') {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /// Code a source splits into one `pre` per line — Notion, some
+ /// highlighters — read back as one block.
+ static List _mergeAdjacentCode(List blocks) {
+ final merged = [];
+ var lines = false;
+ for (final block in blocks) {
+ final line =
+ block is PasteCode &&
+ block.language == null &&
+ !block.text.contains('\n');
+ final last = merged.isEmpty ? null : merged.last;
+ if (line && lines && last is PasteCode) {
+ merged[merged.length - 1] = PasteCode('${last.text}\n${block.text}');
+ continue;
+ }
+ merged.add(block);
+ lines = line;
+ }
+ return merged;
+ }
+
+ /// Paragraphs that open with a ballot box, which is how Pages and Notes
+ /// write a checklist into rich text, read as the to-dos they are.
+ static List _checklists(List blocks) {
+ final result = [];
+ for (final block in blocks) {
+ final task = _taskOf(block);
+ if (task == null) {
+ result.add(block);
+ continue;
+ }
+ final last = result.isEmpty ? null : result.last;
+ if (last is PasteList &&
+ !last.ordered &&
+ last.items.every((item) => item.checked != null)) {
+ result[result.length - 1] = PasteList(
+ ordered: false,
+ items: [...last.items, task],
+ );
+ } else {
+ result.add(PasteList(ordered: false, items: [task]));
+ }
+ }
+ return result;
+ }
+
+ static PasteListItem? _taskOf(PasteBlock block) {
+ if (block is! PasteParagraph) return null;
+ final first = block.inlines.firstOrNull;
+ if (first is! PasteText) return null;
+ final box = _ballotBox.firstMatch(first.text);
+ if (box == null) return null;
+ final rest = first.text.substring(box.end);
+ return PasteListItem([
+ PasteParagraph([
+ if (rest.isNotEmpty)
+ PasteText(rest, styles: first.styles, href: first.href),
+ ...block.inlines.skip(1),
+ ]),
+ ], checked: box.group(1) != '\u2610');
+ }
+
+ static final _ballotBox = RegExp('^\\s*([\u2610\u2611\u2612])\\s+');
+
+ /// GitHub draws a checklist as one list item holding every task. Each task
+ /// becomes an item of its own here, so each keeps its own box.
+ static void _splitTaskItems(dom.Element root) {
+ for (final item in root.querySelectorAll('li')) {
+ final parent = item.parent;
+ if (parent == null) continue;
+ final boxes = [
+ for (final box in item.querySelectorAll('input[type="checkbox"]'))
+ if (identical(_closestItem(box), item)) box,
+ ];
+ if (boxes.length < 2) continue;
+ final tasks = [];
+ for (final box in boxes) {
+ var task = box;
+ while (task.parent != null &&
+ !identical(task.parent, item) &&
+ task.parent!.querySelectorAll('input[type="checkbox"]').length ==
+ 1) {
+ task = task.parent!;
+ }
+ tasks.add(task);
+ }
+ var after = _nextElementSibling(item);
+ for (final task in tasks.reversed) {
+ final split = dom.Element.tag('li')..append(task);
+ parent.insertBefore(split, after);
+ after = split;
+ }
+ if (item.text.trim().isEmpty) item.remove();
+ }
+ }
+
+ static dom.Element? _closestItem(dom.Element element) {
+ for (var at = element.parent; at != null; at = at.parent) {
+ if (at.localName == 'li') return at;
+ }
+ return null;
+ }
+
+ // ---------------------------------------------------------------------
+ // Tables
+
+ void _readTable(dom.Element table, _Context context, _Builder out) {
+ final rows = _ownRows(table);
+ if (_isLayoutTable(table, rows)) {
+ for (final row in rows) {
+ for (final cell in row.children) {
+ if (cell.localName != 'td' && cell.localName != 'th') continue;
+ out.flush();
+ _walkChildren(cell, context, out);
+ out.flush();
+ }
+ }
+ return;
+ }
+
+ final grid = >>[];
+ final aligns = [];
+ final pending = value})>{};
+ for (var r = 0; r < rows.length; r++) {
+ final cells = >[];
+ var column = 0;
+ void fillSpans() {
+ while (pending.containsKey(column)) {
+ final span = pending[column]!;
+ cells.add(const []);
+ if (span.rows <= 1) {
+ pending.remove(column);
+ } else {
+ pending[column] = (rows: span.rows - 1, value: span.value);
+ }
+ column++;
+ }
+ }
+
+ for (final cell in rows[r].children) {
+ if (cell.localName != 'td' && cell.localName != 'th') continue;
+ final declarations = _css.declarationsFor(cell);
+ if (_isHidden(cell, declarations)) continue;
+ fillSpans();
+ final read = _inlinesOf(cell, _styled(cell, declarations, context));
+ final inlines = r == 0 ? _withoutBold(read) : read;
+ final colspan = (int.tryParse(cell.attributes['colspan'] ?? '') ?? 1)
+ .clamp(1, 64);
+ final rowspan = (int.tryParse(cell.attributes['rowspan'] ?? '') ?? 1)
+ .clamp(1, 512);
+ while (aligns.length <= column + colspan - 1) {
+ aligns.add(PasteAlign.none);
+ }
+ if (r == 0 || aligns[column] == PasteAlign.none) {
+ final align = _alignOf(cell, declarations);
+ if (align != PasteAlign.none) aligns[column] = align;
+ }
+ for (var c = 0; c < colspan; c++) {
+ cells.add(c == 0 ? inlines : const []);
+ if (rowspan > 1) {
+ pending[column] = (rows: rowspan - 1, value: inlines);
+ }
+ column++;
+ }
+ }
+ fillSpans();
+ grid.add(cells);
+ }
+ while (grid.isNotEmpty && grid.last.every((cell) => !_hasWords(cell))) {
+ grid.removeLast();
+ }
+ if (grid.isEmpty) return;
+ final width = grid.fold(
+ 0,
+ (most, row) => row.length > most ? row.length : most,
+ );
+ if (width == 0) return;
+ for (final row in grid) {
+ while (row.length < width) {
+ row.add(const []);
+ }
+ }
+ while (aligns.length < width) {
+ aligns.add(PasteAlign.none);
+ }
+ final caption = table.querySelector('caption');
+ if (caption != null) {
+ final words = _inlinesOf(caption, context.withStyle(PasteStyle.bold));
+ if (_hasWords(words)) out.blocks.add(PasteParagraph(words));
+ }
+ out.blocks.add(PasteTable(grid, aligns.sublist(0, width)));
+ }
+
+ /// Rows that belong to this table, not to one nested in it.
+ static List _ownRows(dom.Element table) {
+ final rows = [];
+ void collect(dom.Element parent) {
+ for (final child in parent.children) {
+ switch (child.localName) {
+ case 'tr':
+ rows.add(child);
+ case 'thead' || 'tbody' || 'tfoot':
+ collect(child);
+ }
+ }
+ }
+
+ collect(table);
+ return rows;
+ }
+
+ /// A table used to lay a page out — an email's columns, a site's sidebar —
+ /// rather than to hold data. Its cells are read as the blocks they hold.
+ bool _isLayoutTable(dom.Element table, List rows) {
+ final role = table.attributes['role'];
+ if (role == 'presentation' || role == 'none') return true;
+ if (rows.isEmpty) return true;
+ if (table.querySelector('table') != null) return true;
+ var widest = 0;
+ for (final row in rows) {
+ final count = row.children
+ .where((cell) => cell.localName == 'td' || cell.localName == 'th')
+ .length;
+ if (count > widest) widest = count;
+ }
+ if (widest <= 1) return true;
+ // A cell holding headings or several paragraphs of prose is a column of
+ // a page, not a value.
+ for (final cell in table.querySelectorAll('td')) {
+ if (cell.querySelector('h1, h2, h3, h4, blockquote, pre') != null) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /// A header cell's words without the bold a header is drawn in anyway.
+ static List _withoutBold(List inlines) => [
+ for (final inline in inlines)
+ switch (inline) {
+ PasteText(:final text, :final styles, :final href)
+ when styles.contains(PasteStyle.bold) =>
+ PasteText(
+ text,
+ styles: {...styles}..remove(PasteStyle.bold),
+ href: href,
+ ),
+ PasteCodeSpan(:final code, :final styles, :final href)
+ when styles.contains(PasteStyle.bold) =>
+ PasteCodeSpan(
+ code,
+ styles: {...styles}..remove(PasteStyle.bold),
+ href: href,
+ ),
+ _ => inline,
+ },
+ ];
+
+ static PasteAlign _alignOf(
+ dom.Element cell,
+ Map declarations,
+ ) {
+ final value = (declarations['text-align'] ?? cell.attributes['align'] ?? '')
+ .toLowerCase();
+ return switch (value) {
+ 'center' => PasteAlign.center,
+ 'right' || 'end' => PasteAlign.right,
+ _ => PasteAlign.none,
+ };
+ }
+
+ // ---------------------------------------------------------------------
+ // Images
+
+ void _readImage(dom.Element image, _Context context, _Builder out) {
+ final alt = (image.attributes['alt'] ?? '').trim();
+ if (_isEmojiImage(image, alt)) {
+ out.line.addText(_emojiOf(image, alt), context);
+ return;
+ }
+ final declarations = _css.declarationsFor(image);
+ final width =
+ _pixels(image.attributes['width']) ?? _pixels(declarations['width']);
+ final height =
+ _pixels(image.attributes['height']) ?? _pixels(declarations['height']);
+ // Tracking pixels, spacers, favicons and the little icons beside a
+ // citation: furniture, not content.
+ if ((width != null && width <= 24) || (height != null && height <= 24)) {
+ return;
+ }
+ final source = _imageSource(image);
+ if (source == null) {
+ if (alt.isNotEmpty) out.line.addText(alt, context);
+ return;
+ }
+ if (source.contains('favicon')) return;
+ if (_isAvatar(image, alt, source) &&
+ (width ?? 0) <= 64 &&
+ (height ?? 0) <= 64) {
+ return;
+ }
+ if (_isVector(source)) {
+ if (alt.isNotEmpty) out.line.addText(alt, context);
+ return;
+ }
+ PasteImage picture;
+ if (source.startsWith('data:')) {
+ final decoded = _decodeDataUri(source);
+ if (decoded == null) {
+ if (alt.isNotEmpty) out.line.addText(alt, context);
+ return;
+ }
+ picture = PasteImage(
+ bytes: decoded.bytes,
+ mime: decoded.mime,
+ alt: alt,
+ width: width,
+ height: height,
+ );
+ } else {
+ final resolved = _resolve(source);
+ final carried =
+ resources[source] ?? (resolved == null ? null : resources[resolved]);
+ if (carried != null) {
+ picture = PasteImage(
+ src: resolved,
+ bytes: carried.bytes,
+ mime: carried.mime,
+ alt: alt,
+ width: width,
+ height: height,
+ );
+ } else if (resolved != null) {
+ picture = PasteImage(
+ src: resolved,
+ alt: alt,
+ width: width,
+ height: height,
+ );
+ } else {
+ if (alt.isNotEmpty) out.line.addText(alt, context);
+ return;
+ }
+ }
+ out.line.add(PasteImageInline(picture, href: context.href));
+ }
+
+ /// Whether a picture is drawn in SVG, which a note can't hold: badges,
+ /// logos. Read from its file name or data type, or for GitHub's image
+ /// proxy from the address it hides in hex.
+ static bool _isVector(String source) {
+ if (source.toLowerCase().startsWith('data:image/svg')) return true;
+ var address = Uri.tryParse(source);
+ if (address == null) return false;
+ if (address.host == 'camo.githubusercontent.com') {
+ final hex = address.pathSegments.lastOrNull ?? '';
+ if (RegExp(r'^(?:[0-9a-f]{2})+$').hasMatch(hex)) {
+ final original = Uri.tryParse(
+ utf8.decode([
+ for (var i = 0; i < hex.length; i += 2)
+ int.parse(hex.substring(i, i + 2), radix: 16),
+ ], allowMalformed: true),
+ );
+ if (original != null) address = original;
+ }
+ }
+ return address.path.toLowerCase().endsWith('.svg') ||
+ address.host == 'img.shields.io' ||
+ address.host == 'badgen.net' ||
+ address.pathSegments.contains('badge');
+ }
+
+ /// A person's face beside their name on a comment or an answer: who said
+ /// it rather than what they said.
+ static bool _isAvatar(dom.Element image, String alt, String source) {
+ final host = Uri.tryParse(source)?.host ?? '';
+ return alt.toLowerCase().contains('avatar') ||
+ image.className.toLowerCase().contains('avatar') ||
+ host == 'gravatar.com' ||
+ host.endsWith('.gravatar.com');
+ }
+
+ String? _imageSource(dom.Element image) {
+ final attributes = image.attributes;
+ final src = attributes['src'];
+ // A lazy-loaded picture keeps a 1x1 stand-in in src and the real one in
+ // a data attribute until it scrolls into view.
+ final lazy =
+ attributes['data-src'] ??
+ attributes['data-original'] ??
+ attributes['data-lazy-src'];
+ if (lazy != null &&
+ (src == null || src.startsWith('data:image/gif') || src.isEmpty)) {
+ return lazy;
+ }
+ if (src != null && src.isNotEmpty) return src;
+ final srcset = attributes['srcset'];
+ if (srcset != null && srcset.isNotEmpty) {
+ return srcset.split(',').last.trim().split(RegExp(r'\s+')).first;
+ }
+ return null;
+ }
+
+ static bool _isEmojiImage(dom.Element image, String alt) {
+ final classes = image.className.toLowerCase();
+ if (classes.contains('emoji') ||
+ image.attributes.containsKey('data-stringify-emoji')) {
+ return alt.isNotEmpty;
+ }
+ // A picture whose alt text is a single emoji, or a :shortcode:, is an
+ // emoji drawn as a picture.
+ if (RegExp(r'^:[a-z0-9_+\-]+:$').hasMatch(alt)) return true;
+ return alt.isNotEmpty &&
+ alt.runes.length <= 2 &&
+ alt.runes.every((rune) => rune >= 0x2190);
+ }
+
+ /// The emoji a picture of one stands for: the code points its file is
+ /// named after, as Slack, GitHub and Twemoji name them, or else its alt
+ /// text, which for a custom emoji is its :shortcode:.
+ static String _emojiOf(dom.Element image, String alt) {
+ final file = Uri.tryParse(
+ image.attributes['src'] ?? '',
+ )?.pathSegments.lastOrNull;
+ final match = _codePointFile.firstMatch(file ?? '');
+ if (match == null) return alt;
+ final points = [
+ for (final part in match.group(1)!.split('-')) int.parse(part, radix: 16),
+ ];
+ final emoji = points.any((point) => point >= 0x2000 && point <= 0x10FFFF);
+ return emoji ? String.fromCharCodes(points) : alt;
+ }
+
+ static final _codePointFile = RegExp(
+ r'^([0-9a-f]{2,6}(?:-[0-9a-f]{2,6})*)(?:@\dx)?\.(?:png|svg|gif|webp)$',
+ caseSensitive: false,
+ );
+
+ static ({Uint8List bytes, String mime})? _decodeDataUri(String uri) {
+ final comma = uri.indexOf(',');
+ if (comma < 0) return null;
+ final header = uri.substring(5, comma);
+ final mime = header.split(';').first.toLowerCase();
+ if (!mime.startsWith('image/')) return null;
+ try {
+ final payload = uri.substring(comma + 1);
+ final bytes = header.contains(';base64')
+ ? base64.decode(payload.replaceAll(RegExp(r'\s'), ''))
+ : Uint8List.fromList(utf8.encode(Uri.decodeComponent(payload)));
+ if (bytes.isEmpty) return null;
+ return (bytes: bytes, mime: mime);
+ } on FormatException {
+ return null;
+ }
+ }
+
+ static double? _pixels(String? value) {
+ if (value == null) return null;
+ final match = RegExp(
+ r'^\s*([0-9.]+)\s*(px|pt|r?em)?\s*$',
+ ).firstMatch(value);
+ final number = match == null ? null : double.tryParse(match.group(1)!);
+ if (number == null) return null;
+ return switch (match!.group(2)) {
+ 'pt' => number * 4 / 3,
+ 'em' || 'rem' => number * 16,
+ _ => number,
+ };
+ }
+
+ // ---------------------------------------------------------------------
+ // Links
+
+ String? _resolveLink(String? raw) {
+ if (raw == null) return null;
+ final href = raw.trim();
+ if (href.isEmpty || href.startsWith('#')) return null;
+ final lower = href.toLowerCase();
+ if (lower.startsWith('javascript:') ||
+ lower.startsWith('data:') ||
+ lower.startsWith('vbscript:')) {
+ return null;
+ }
+ final resolved = _resolve(href);
+ if (resolved == null) return null;
+ final uri = Uri.tryParse(resolved);
+ // Google wraps every link in a document in a redirect through itself.
+ if (uri != null &&
+ (uri.host == 'www.google.com' || uri.host == 'google.com') &&
+ uri.path == '/url') {
+ final target = uri.queryParameters['q'] ?? uri.queryParameters['url'];
+ if (target != null && target.startsWith('http')) return target;
+ }
+ return resolved;
+ }
+
+ /// An absolute address for [raw], or null when it is relative and there is
+ /// nothing to resolve it against.
+ String? _resolve(String raw) {
+ final uri = Uri.tryParse(raw.trim());
+ if (uri == null) return null;
+ if (uri.hasScheme) {
+ const allowed = {'http', 'https', 'mailto', 'tel', 'file', 'ftp'};
+ return allowed.contains(uri.scheme.toLowerCase()) ? uri.toString() : null;
+ }
+ final base = baseUrl;
+ if (base == null) return null;
+ return base.resolveUri(uri).toString();
+ }
+
+ // ---------------------------------------------------------------------
+ // Math
+
+ static bool _isMath(dom.Element element) {
+ final tag = element.localName;
+ if (tag == 'math' || tag == 'mjx-container') return true;
+ if (tag == 'script') {
+ final type = element.attributes['type'] ?? '';
+ return type.startsWith('math/tex');
+ }
+ final classes = element.classes;
+ return classes.contains('katex') ||
+ classes.contains('katex-display') ||
+ classes.contains('mwe-math-element') ||
+ classes.contains('MathJax_Display') ||
+ (classes.contains('math-display') ||
+ classes.contains('math-block') ||
+ classes.contains('math-inline')) &&
+ (element.attributes.containsKey('data-latex') ||
+ element.attributes.containsKey('data-math'));
+ }
+
+ ({String tex, bool display})? _readMath(dom.Element element) {
+ final tag = element.localName;
+ final classes = element.classes;
+ if (tag == 'script') {
+ final tex = element.text.trim();
+ if (tex.isEmpty) return null;
+ return (
+ tex: tex,
+ display: (element.attributes['type'] ?? '').contains('display'),
+ );
+ }
+ final latex =
+ element.attributes['data-latex'] ?? element.attributes['data-math'];
+ if (latex != null && latex.trim().isNotEmpty) {
+ return (
+ tex: latex.trim(),
+ display:
+ classes.contains('math-display') || classes.contains('math-block'),
+ );
+ }
+ final annotation = element.querySelector(
+ 'annotation[encoding="application/x-tex"], annotation[encoding="TeX"]',
+ );
+ final display =
+ classes.contains('katex-display') ||
+ element.querySelector('.katex-display') != null ||
+ element.attributes['display'] == 'true' ||
+ element.attributes['display'] == 'block' ||
+ element.querySelector('math[display="block"]') != null ||
+ element.querySelector('.mwe-math-fallback-image-display') != null ||
+ classes.contains('MathJax_Display');
+ if (annotation != null && annotation.text.trim().isNotEmpty) {
+ return (tex: _cleanTex(annotation.text), display: display);
+ }
+ final math = tag == 'math' ? element : element.querySelector('math');
+ final alttext = math?.attributes['alttext'];
+ if (alttext != null && alttext.trim().isNotEmpty) {
+ return (tex: _cleanTex(alttext), display: display);
+ }
+ final source = _parsoidTex(math);
+ if (source != null) return (tex: _cleanTex(source), display: display);
+ final fallback = element.querySelector('img[alt]');
+ final alt = fallback?.attributes['alt'];
+ if (alt != null && alt.trim().isNotEmpty) {
+ return (tex: _cleanTex(alt), display: display);
+ }
+ if (math != null) {
+ final tex = mathmlToTex(math);
+ if (tex.isNotEmpty) return (tex: tex, display: display);
+ }
+ // Drawn math with no TeX and no MathML: its text is the nearest honest
+ // thing.
+ final text = element.text.replaceAll(RegExp(r'\s+'), ' ').trim();
+ if (text.isEmpty) return null;
+ return (tex: text, display: display);
+ }
+
+ static String _cleanTex(String raw) {
+ var tex = raw.trim();
+ final wrapper = RegExp(
+ r'^\{\\(?:displaystyle|textstyle)\s*(.*)\}$',
+ dotAll: true,
+ ).firstMatch(tex);
+ if (wrapper != null) tex = wrapper.group(1)!.trim();
+ return tex.replaceFirst(
+ RegExp(r'^\\(?:displaystyle|textstyle)(?![A-Za-z])\s*'),
+ '',
+ );
+ }
+
+ /// The TeX Wikipedia's Parsoid keeps in a formula's data-mw.
+ static String? _parsoidTex(dom.Element? math) {
+ final raw = math?.attributes['data-mw'];
+ if (raw == null) return null;
+ try {
+ final decoded = jsonDecode(raw);
+ final body = decoded is Map ? decoded['body'] : null;
+ final source = body is Map ? body['extsrc'] : null;
+ return source is String && source.trim().isNotEmpty ? source : null;
+ } on FormatException {
+ return null;
+ }
+ }
+
+ // ---------------------------------------------------------------------
+ // Word
+
+ /// Word writes a list as paragraphs, each saying in its style which list
+ /// and level it is on and carrying its bullet as text. Turns runs of them
+ /// into real lists before the walk, so the rest of the reader never has to
+ /// know.
+ void _rewriteWordLists(dom.Element root) {
+ final paragraphs = root
+ .querySelectorAll('p, h1, h2, h3, h4, h5, h6, div')
+ .where((p) => _msoListLevel(p) != null)
+ .toList();
+ if (paragraphs.isEmpty) return;
+ var index = 0;
+ while (index < paragraphs.length) {
+ final first = paragraphs[index];
+ final run = [first];
+ var next = index + 1;
+ while (next < paragraphs.length &&
+ identical(_nextElementSibling(run.last), paragraphs[next]) &&
+ !(_msoListLevel(paragraphs[next]) == 1 &&
+ _msoListId(paragraphs[next]) != _msoListId(first))) {
+ run.add(paragraphs[next]);
+ next++;
+ }
+ _replaceWithList(run);
+ index = next;
+ }
+ }
+
+ static dom.Element? _nextElementSibling(dom.Element element) {
+ final parent = element.parent;
+ if (parent == null) return null;
+ final siblings = parent.children;
+ final at = siblings.indexOf(element);
+ return at >= 0 && at + 1 < siblings.length ? siblings[at + 1] : null;
+ }
+
+ /// Which of the document's lists a paragraph belongs to, so that a
+ /// numbered list right after a bulleted one stays a list of its own.
+ static String? _msoListId(dom.Element element) => RegExp(
+ r'mso-list:\s*(l\d+)\s+level',
+ ).firstMatch(element.attributes['style'] ?? '')?.group(1);
+
+ static int? _msoListLevel(dom.Element element) {
+ final style = element.attributes['style'] ?? '';
+ final match = RegExp(r'mso-list:\s*l\d+\s+level(\d+)').firstMatch(style);
+ if (match == null) return null;
+ return int.tryParse(match.group(1)!);
+ }
+
+ void _replaceWithList(List paragraphs) {
+ final parent = paragraphs.first.parent;
+ if (parent == null) return;
+ // (list element, level) from the outermost in.
+ final stack = <(dom.Element, int)>[];
+ dom.Element? outermost;
+ for (final paragraph in paragraphs) {
+ final level = _msoListLevel(paragraph) ?? 1;
+ final marker = _takeWordMarker(paragraph);
+ final ordered =
+ marker != null && RegExp(r'^[0-9A-Za-z]{1,4}[.)]$').hasMatch(marker);
+ while (stack.isNotEmpty && stack.last.$2 > level) {
+ stack.removeLast();
+ }
+ if (stack.isEmpty || stack.last.$2 < level) {
+ final list = dom.Element.tag(ordered ? 'ol' : 'ul');
+ if (ordered) {
+ final number = int.tryParse(marker.substring(0, marker.length - 1));
+ if (number != null && number != 1) {
+ list.attributes['start'] = '$number';
+ }
+ }
+ if (stack.isEmpty) {
+ outermost = list;
+ } else {
+ final parentList = stack.last.$1;
+ final lastItem = parentList.children.isEmpty
+ ? null
+ : parentList.children.last;
+ (lastItem ?? parentList).append(list);
+ }
+ stack.add((list, level));
+ }
+ final item = dom.Element.tag('li');
+ for (final node in [...paragraph.nodes]) {
+ item.append(node);
+ }
+ stack.last.$1.append(item);
+ }
+ parent.insertBefore(outermost!, paragraphs.first);
+ for (final paragraph in paragraphs) {
+ paragraph.remove();
+ }
+ }
+
+ /// Removes the bullet or number Word wrote as text in front of an item, and
+ /// returns it.
+ static String? _takeWordMarker(dom.Element paragraph) {
+ for (final span in paragraph.querySelectorAll('span')) {
+ final style = span.attributes['style'] ?? '';
+ if (style.contains('mso-list:Ignore') ||
+ style.contains('mso-list: Ignore')) {
+ final text = span.text.replaceAll('\u00a0', ' ').trim();
+ // The span that holds the marker can sit inside one that styles it.
+ var outer = span;
+ while (outer.parent != null &&
+ !identical(outer.parent, paragraph) &&
+ outer.parent!.text.replaceAll('\u00a0', ' ').trim() == text) {
+ outer = outer.parent!;
+ }
+ outer.remove();
+ return text;
+ }
+ }
+ return null;
+ }
+
+ /// A paragraph set well above the size of the words around it, in markup
+ /// with no other way of saying it is a heading.
+ int? _sizedTitleLevel(dom.Element paragraph) {
+ final textSize = _textSize;
+ if (textSize == null) return null;
+ final size = _fontSize(paragraph);
+ final words = paragraph.text.trim();
+ if (size == null || words.isEmpty || words.length > 120) return null;
+ final ratio = size / textSize;
+ if (ratio >= 1.6) return 1;
+ if (ratio >= 1.2) return 2;
+ return null;
+ }
+
+ double? _commonSize(dom.Element root) {
+ final bySize = {};
+ for (final element in root.querySelectorAll('p, li')) {
+ final size = _fontSize(element);
+ if (size == null) continue;
+ bySize[size] = (bySize[size] ?? 0) + element.text.trim().length;
+ }
+ if (bySize.isEmpty) return null;
+ return bySize.entries.reduce((a, b) => b.value > a.value ? b : a).key;
+ }
+
+ double? _fontSize(dom.Element element) {
+ final declarations = _css.declarationsFor(element);
+ final value = declarations['font-size'] ?? declarations['font'];
+ final match = RegExp(r'([0-9.]+)px').firstMatch(value ?? '');
+ return match == null ? null : double.tryParse(match.group(1)!);
+ }
+
+ /// A heading's words without the bold every heading is drawn in, when
+ /// all of them were bold.
+ static List _unboldedHeading(List inlines) {
+ final words = inlines.whereType().where(
+ (text) => text.text.trim().isNotEmpty,
+ );
+ if (words.isEmpty ||
+ !words.every((text) => text.styles.contains(PasteStyle.bold))) {
+ return inlines;
+ }
+ return _withoutBold(inlines);
+ }
+
+ /// A Word title or subtitle paragraph, which Word does not write as a
+ /// heading element.
+ static int? _wordTitleLevel(dom.Element paragraph) {
+ for (final name in paragraph.classes) {
+ if (name == 'MsoTitle') return 1;
+ if (name == 'MsoSubtitle') return 2;
+ final match = RegExp(r'^MsoHeading([1-6])$').firstMatch(name);
+ if (match != null) return int.parse(match.group(1)!);
+ }
+ return null;
+ }
+
+ /// `` and its kind are not comments to an HTML5
+ /// parser, which keeps the text between them; that text is the marker the
+ /// list rewrite reads, so only the conditional markers themselves go.
+ static String _withoutDownlevelConditionals(String html) =>
+ html.replaceAll(RegExp(r''), '');
+
+ // ---------------------------------------------------------------------
+
+ bool _droppedClass(dom.Element element) {
+ for (final name in element.classes) {
+ if (_droppedClasses.contains(name)) return true;
+ }
+ // Wikipedia's footnote marks; the same class is a real link elsewhere.
+ if (element.localName == 'sup' && element.classes.contains('reference')) {
+ return true;
+ }
+ // ChatGPT's "Thought for 12s" above an answer.
+ if (element.attributes.containsKey('data-cot-label')) return true;
+ final testId = element.attributes['data-testid'] ?? '';
+ if (testId.contains('copy') || testId.contains('citation-pill')) {
+ return element.localName == 'button' || testId.contains('copy');
+ }
+ return false;
+ }
+
+ static bool _hasWords(List inlines) => inlines.any(
+ (inline) => switch (inline) {
+ PasteText(:final text) => text.trim().isNotEmpty,
+ PasteBreak() => false,
+ _ => true,
+ },
+ );
+}
+
+const _dropped = {
+ 'script',
+ 'style',
+ 'noscript',
+ 'template',
+ 'iframe',
+ 'object',
+ 'embed',
+ 'canvas',
+ 'video',
+ 'audio',
+ 'button',
+ 'select',
+ 'option',
+ 'textarea',
+ 'svg',
+ 'head',
+ 'title',
+ 'meta',
+ 'link',
+ 'base',
+ 'map',
+ 'area',
+ 'track',
+ 'source',
+ 'rp',
+ 'wbr',
+ 'clipboard-copy',
+ 'tool-tip',
+ 'o:p',
+ 'xml',
+ 'v:shape',
+ 'v:imagedata',
+};
+
+const _droppedClasses = {
+ // Wikipedia: section edit links, footnote marks, jump links.
+ 'mw-editsection',
+ 'mw-cite-backlink',
+ 'mw-jump-link',
+ 'noprint',
+ // Wikipedia's picture of a formula, beside the MathML it is read from.
+ 'mwe-math-fallback-image-inline',
+ 'mwe-math-fallback-image-display',
+ // GitHub: the link icon beside every heading, copy buttons.
+ 'anchor',
+ 'octicon',
+ 'zeroclipboard-container',
+ // Copy buttons and code-block toolbars of AI chats and docs sites.
+ 'copy-button',
+ 'copy-code-button',
+ 'code-block-decoration',
+};
+
+const _screenReaderOnly = {
+ 'sr-only',
+ 'visually-hidden',
+ 'visuallyhidden',
+ 'screen-reader-text',
+ 'screenreader-only',
+ 'a11y-hidden',
+ 'assistive-text',
+};
+
+const _blockTags = {
+ 'address',
+ 'article',
+ 'aside',
+ 'blockquote',
+ 'body',
+ 'caption',
+ 'center',
+ 'dd',
+ 'details',
+ 'dialog',
+ 'div',
+ 'dl',
+ 'dt',
+ 'fieldset',
+ 'figcaption',
+ 'figure',
+ 'footer',
+ 'form',
+ 'h1',
+ 'h2',
+ 'h3',
+ 'h4',
+ 'h5',
+ 'h6',
+ 'header',
+ 'hgroup',
+ 'hr',
+ 'html',
+ 'legend',
+ 'li',
+ 'main',
+ 'nav',
+ 'ol',
+ 'p',
+ 'pre',
+ 'section',
+ 'summary',
+ 'table',
+ 'tbody',
+ 'td',
+ 'tfoot',
+ 'th',
+ 'thead',
+ 'tr',
+ 'ul',
+};
+
+const _notLanguages = {
+ 'copy',
+ 'copy code',
+ 'copied',
+ 'copied!',
+ 'code',
+ 'edit',
+ 'run',
+ 'download',
+ 'wrap',
+ 'expand',
+ 'collapse',
+ 'preview',
+};
+
+/// What the walk carries down the tree: the emphasis in force, the link the
+/// words are inside, and whether white space is kept as written.
+class _Context {
+ const _Context({
+ this.styles = const {},
+ this.href,
+ this.pre = false,
+ this.preLine = false,
+ });
+
+ final Set styles;
+ final String? href;
+
+ /// White space kept as written.
+ final bool pre;
+
+ /// Line breaks kept as written, while runs of spaces still collapse.
+ final bool preLine;
+
+ _Context withStyle(PasteStyle style) => _Context(
+ styles: {...styles, style},
+ href: href,
+ pre: pre,
+ preLine: preLine,
+ );
+}
+
+class _Builder {
+ final List blocks = [];
+ final _Line line = _Line();
+
+ void flush() {
+ final inlines = line.take();
+ if (inlines.isNotEmpty) blocks.add(PasteParagraph(inlines));
+ }
+}
+
+/// The words of the block being read, with HTML's white space rules applied
+/// as they arrive: runs of spaces and line breaks in the markup are one
+/// space, and none at all at the start of a line.
+class _Line {
+ final List _inlines = [];
+
+ bool get _atLineStart =>
+ _inlines.isEmpty ||
+ _inlines.last is PasteBreak ||
+ (_inlines.last is PasteText &&
+ (_inlines.last as PasteText).text.endsWith(' '));
+
+ void addText(String raw, _Context context) {
+ var text = raw.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
+ text = text.replaceAll(RegExp('[\u200b\u2060\ufeff\u00ad\ufffc]'), '');
+ if (context.pre) {
+ final lines = text.split('\n');
+ for (var i = 0; i < lines.length; i++) {
+ if (i > 0) _inlines.add(const PasteBreak());
+ _append(lines[i].replaceAll('\u00a0', ' '), context);
+ }
+ return;
+ }
+ if (context.preLine && text.contains('\n')) {
+ final lines = text.split('\n');
+ for (var i = 0; i < lines.length; i++) {
+ if (i > 0) addBreak(context);
+ addText(lines[i], _Context(styles: context.styles, href: context.href));
+ }
+ return;
+ }
+ text = text.replaceAll(RegExp(r'[ \t\n\f]+'), ' ');
+ if (text.startsWith(' ') && _atLineStart) text = text.substring(1);
+ text = text.replaceAll('\u00a0', ' ');
+ if (text.isEmpty) return;
+ _append(text, context);
+ }
+
+ void _append(String text, _Context context) {
+ if (text.isEmpty) return;
+ final last = _inlines.isEmpty ? null : _inlines.last;
+ if (last is PasteText &&
+ last.href == context.href &&
+ _sameStyles(last.styles, context.styles)) {
+ _inlines[_inlines.length - 1] = PasteText(
+ last.text + text,
+ styles: last.styles,
+ href: last.href,
+ );
+ return;
+ }
+ _inlines.add(PasteText(text, styles: context.styles, href: context.href));
+ }
+
+ void addBreak(_Context context) {
+ _trimTrailingSpace();
+ _inlines.add(const PasteBreak());
+ }
+
+ void add(PasteInline inline) => _inlines.add(inline);
+
+ void _trimTrailingSpace() {
+ if (_inlines.isEmpty) return;
+ final last = _inlines.last;
+ if (last is PasteText && last.text.endsWith(' ')) {
+ final trimmed = last.text.trimRight();
+ if (trimmed.isEmpty) {
+ _inlines.removeLast();
+ } else {
+ _inlines[_inlines.length - 1] = PasteText(
+ trimmed,
+ styles: last.styles,
+ href: last.href,
+ );
+ }
+ }
+ }
+
+ List take() {
+ _trimTrailingSpace();
+ while (_inlines.isNotEmpty && _inlines.last is PasteBreak) {
+ _inlines.removeLast();
+ _trimTrailingSpace();
+ }
+ while (_inlines.isNotEmpty && _inlines.first is PasteBreak) {
+ _inlines.removeAt(0);
+ }
+ if (_inlines.isNotEmpty && _inlines.first is PasteText) {
+ final first = _inlines.first as PasteText;
+ final trimmed = first.text.trimLeft();
+ if (trimmed.isEmpty) {
+ _inlines.removeAt(0);
+ } else if (trimmed.length != first.text.length) {
+ _inlines[0] = PasteText(
+ trimmed,
+ styles: first.styles,
+ href: first.href,
+ );
+ }
+ }
+ final hasContent = _inlines.any(
+ (inline) =>
+ inline is! PasteBreak &&
+ (inline is! PasteText || inline.text.trim().isNotEmpty),
+ );
+ final result = hasContent
+ ? List.of(_inlines)
+ : const [];
+ _inlines.clear();
+ return result;
+ }
+
+ static bool _sameStyles(Set a, Set b) =>
+ a.length == b.length && a.containsAll(b);
+}
+
+/// The few CSS rules clipboard HTML relies on: Apple's RTF conversion, Excel
+/// and Word put bold and italic in classes defined in a style block.
+class _CssSheet {
+ final Map> _byClass = {};
+ final Map> _byTagClass = {};
+ final Map> _cache = {};
+
+ static final _rule = RegExp(r'([^{}]+)\{([^}]*)\}');
+
+ void add(String css) {
+ final source = css.replaceAll(RegExp(r'/\*.*?\*/', dotAll: true), '');
+ for (final match in _rule.allMatches(source)) {
+ final declarations = parseDeclarations(match.group(2)!);
+ if (declarations.isEmpty) continue;
+ for (final raw in match.group(1)!.split(',')) {
+ final selector = raw.trim();
+ final simple = RegExp(
+ r'^([a-zA-Z0-9]*)\.([A-Za-z0-9_-]+)$',
+ ).firstMatch(selector);
+ if (simple == null) continue;
+ final tag = simple.group(1)!.toLowerCase();
+ final name = simple.group(2)!;
+ final target = tag.isEmpty
+ ? _byClass.putIfAbsent(name, () => {})
+ : _byTagClass.putIfAbsent('$tag.$name', () => {});
+ target.addAll(declarations);
+ }
+ }
+ }
+
+ Map declarationsFor(dom.Element element) {
+ final cached = _cache[element];
+ if (cached != null) return cached;
+ final result = {};
+ if (_byClass.isNotEmpty || _byTagClass.isNotEmpty) {
+ for (final name in element.classes) {
+ result.addAll(_byClass[name] ?? const {});
+ result.addAll(_byTagClass['${element.localName}.$name'] ?? const {});
+ }
+ }
+ final inline = element.attributes['style'];
+ if (inline != null) result.addAll(parseDeclarations(inline));
+ return _cache[element] = result;
+ }
+
+ static Map parseDeclarations(String block) {
+ final result = {};
+ for (final part in block.split(';')) {
+ final colon = part.indexOf(':');
+ if (colon <= 0) continue;
+ final name = part.substring(0, colon).trim().toLowerCase();
+ var value = part.substring(colon + 1).trim().toLowerCase();
+ value = value.replaceAll('!important', '').trim();
+ if (name.isEmpty || value.isEmpty) continue;
+ result[name] = value;
+ }
+ return result;
+ }
+}
diff --git a/lib/paste/html_writer.dart b/lib/paste/html_writer.dart
new file mode 100644
index 0000000..ee84488
--- /dev/null
+++ b/lib/paste/html_writer.dart
@@ -0,0 +1,234 @@
+import 'dart:convert';
+
+import 'paste_tree.dart';
+
+/// Writes blocks as the HTML documents, mail and web editors paste: Google
+/// Docs, Word, Gmail, Notion.
+///
+/// It says what [HtmlPasteReader] reads, so a note copied out and pasted back
+/// in reads as the blocks it was. Where those apps differ from a browser, they
+/// decide: a to-do's box is the ☐ or ☑ it looks like, which they keep where
+/// they would drop a form's checkbox, and math is its TeX between dollars,
+/// marked with the TeX so an editor that draws math can find it.
+///
+/// [embeds] stand in for characters of the words. Each key, a single
+/// character, is written as the HTML it maps to wherever the words hold it:
+/// the pictures of a note, which hold their place in its text as one
+/// character each.
+String writeHtml(
+ List blocks, {
+ Map embeds = const {},
+}) {
+ final writer = _HtmlWriter(embeds);
+ writer.blocks(blocks);
+ return writer.out.toString();
+}
+
+class _HtmlWriter {
+ _HtmlWriter(this.embeds);
+
+ final Map embeds;
+ final out = StringBuffer();
+
+ void blocks(List blocks) {
+ for (final block in blocks) {
+ _block(block);
+ }
+ }
+
+ void _block(PasteBlock block) {
+ switch (block) {
+ case PasteParagraph(:final inlines):
+ out.write('');
+ _inlines(inlines);
+ out.write('
');
+ case PasteHeading(:final level, :final inlines):
+ final tag = 'h${level.clamp(1, 6)}';
+ out.write('<$tag>');
+ _inlines(inlines);
+ out.write('$tag>');
+ case PasteList():
+ _list(block);
+ case PasteQuote(:final blocks):
+ out.write('');
+ this.blocks(blocks);
+ out.write(' ');
+ case PasteCode(:final text, :final language):
+ out.write('')
+ ..write(_words(text, keepSpaces: false))
+ ..write(' ');
+ case PasteTable():
+ _table(block);
+ case PasteRule():
+ out.write(' ');
+ case PasteMathBlock(:final tex):
+ out.write(_math(tex.trim(), display: true));
+ }
+ }
+
+ void _list(PasteList list) {
+ final tag = list.ordered ? 'ol' : 'ul';
+ out.write(
+ list.ordered && list.start != 1 ? '' : '<$tag>',
+ );
+ for (final item in list.items) {
+ final checked = item.checked;
+ if (checked == null) {
+ out.write('');
+ } else {
+ out
+ ..write(' ')
+ ..write(checked ? '☑ ' : '☐ ');
+ }
+ var rest = item.blocks;
+ if (rest.firstOrNull case PasteParagraph(:final inlines)) {
+ _inlines(inlines);
+ rest = rest.sublist(1);
+ }
+ blocks(rest);
+ out.write(' ');
+ }
+ out.write('$tag>');
+ }
+
+ void _table(PasteTable table) {
+ out.write(
+ '',
+ );
+ for (var r = 0; r < table.rows.length; r++) {
+ if (r == 0) out.write('');
+ if (r == 1) out.write(' ');
+ out.write('');
+ final cells = table.rows[r];
+ for (var c = 0; c < cells.length; c++) {
+ final tag = r == 0 ? 'th' : 'td';
+ final align = c < table.aligns.length
+ ? table.aligns[c]
+ : PasteAlign.none;
+ out.write(switch (align) {
+ PasteAlign.none => '<$tag>',
+ PasteAlign.center => '<$tag style="text-align:center">',
+ PasteAlign.right => '<$tag style="text-align:right">',
+ });
+ _inlines(cells[c]);
+ out.write('$tag>');
+ }
+ out.write(' ');
+ if (r == 0) out.write('');
+ }
+ if (table.rows.length > 1) out.write(' ');
+ out.write('
');
+ }
+
+ void _inlines(List inlines) {
+ String? link;
+ for (final inline in inlines) {
+ final href = switch (inline) {
+ PasteText(:final href) ||
+ PasteCodeSpan(:final href) ||
+ PasteImageInline(:final href) => href,
+ PasteMath() => link,
+ PasteBreak() => null,
+ };
+ if (href != link) {
+ if (link != null) out.write('');
+ if (href != null) out.write('');
+ link = href;
+ }
+ switch (inline) {
+ case PasteText(:final text, :final styles):
+ _styled(styles, _words(text, keepSpaces: true));
+ case PasteCodeSpan(:final code, :final styles):
+ _styled(styles, '${_words(code, keepSpaces: true)}');
+ case PasteMath(:final tex):
+ out.write(_math(tex.trim(), display: false));
+ case PasteBreak():
+ out.write(' ');
+ case PasteImageInline(:final image):
+ _image(image);
+ }
+ }
+ if (link != null) out.write(' ');
+ }
+
+ void _styled(Set styles, String html) {
+ final tags = [
+ for (final (style, tag) in _styleTags)
+ if (styles.contains(style)) tag,
+ ];
+ for (final tag in tags) {
+ out.write('<$tag>');
+ }
+ out.write(html);
+ for (final tag in tags.reversed) {
+ out.write('$tag>');
+ }
+ }
+
+ void _image(PasteImage image) {
+ final bytes = image.bytes;
+ final source = bytes != null && bytes.isNotEmpty
+ ? 'data:${image.mime ?? 'image/png'};base64,${base64Encode(bytes)}'
+ : image.src;
+ if (source == null) {
+ out.write(_words(image.alt, keepSpaces: false));
+ return;
+ }
+ out.write(
+ ' 0) out.write(' width="${width.round()}"');
+ final height = image.height;
+ if (height != null && height > 0) out.write(' height="${height.round()}"');
+ out.write('>');
+ }
+
+ String _math(String tex, {required bool display}) {
+ final latex = _attribute(tex);
+ final shown = _words(tex, keepSpaces: false, embedded: false);
+ return display
+ ? '\$\$$shown\$\$
'
+ : '\$$shown\$ ';
+ }
+
+ /// Words as HTML text, with their [embeds] in place. A run of spaces keeps
+ /// its width with [keepSpaces], where HTML would close it up to one.
+ String _words(String text, {required bool keepSpaces, bool embedded = true}) {
+ final result = StringBuffer();
+ for (var i = 0; i < text.length; i++) {
+ final char = text[i];
+ final embed = embeds[char];
+ if (embed != null) {
+ if (embedded) result.write(embed);
+ continue;
+ }
+ result.write(switch (char) {
+ '&' => '&',
+ '<' => '<',
+ '>' => '>',
+ ' ' when keepSpaces && i > 0 && text[i - 1] == ' ' => ' ',
+ _ => char,
+ });
+ }
+ return result.toString();
+ }
+
+ String _attribute(String value) => const HtmlEscape(
+ HtmlEscapeMode.attribute,
+ ).convert(value.split('').where((char) => !embeds.containsKey(char)).join());
+}
+
+const _styleTags = [
+ (PasteStyle.bold, 'b'),
+ (PasteStyle.italic, 'i'),
+ (PasteStyle.strike, 's'),
+ (PasteStyle.underline, 'u'),
+ (PasteStyle.superscript, 'sup'),
+ (PasteStyle.subscript, 'sub'),
+];
diff --git a/lib/paste/image_download.dart b/lib/paste/image_download.dart
new file mode 100644
index 0000000..60928ec
--- /dev/null
+++ b/lib/paste/image_download.dart
@@ -0,0 +1,111 @@
+import 'dart:async';
+import 'dart:collection';
+import 'dart:io';
+import 'dart:typed_data';
+
+import 'package:flutter/foundation.dart';
+import 'package:http/http.dart' as http;
+
+import '../images/image_ingest.dart' show mimeForFilename;
+
+/// Fetches the pictures a paste links to, so they become the note's own
+/// before the links expire — ChatGPT's, Notion's and Google Docs' do.
+///
+/// This is the only thing a paste sends anywhere: one GET per picture, with
+/// no cookies and no referrer, at most four at a time, each given 20 seconds
+/// and 25 MB, and kept only when the server says it is an image.
+class PastedImageDownloader {
+ PastedImageDownloader({http.Client? client}) : _client = client;
+
+ static final shared = PastedImageDownloader();
+
+ static const maxConcurrent = 4;
+ static const timeout = Duration(seconds: 20);
+ static const maxBytes = 25 * 1024 * 1024;
+
+ final http.Client? _client;
+ var _active = 0;
+ final _waiting = Queue>();
+
+ Future<({Uint8List bytes, String mime})?> fetch(String address) async {
+ final uri = Uri.tryParse(address);
+ if (uri == null) return null;
+ if (uri.scheme == 'file') return _readFile(uri);
+ if (uri.scheme != 'https' && uri.scheme != 'http') return null;
+ await _acquire();
+ try {
+ return await _download(uri).timeout(timeout, onTimeout: () => null);
+ } on Object catch (error) {
+ debugPrint('KapyNotes: could not fetch a pasted image: $error');
+ return null;
+ } finally {
+ _release();
+ }
+ }
+
+ Future<({Uint8List bytes, String mime})?> _download(Uri uri) async {
+ final client = _client ?? http.Client();
+ try {
+ final request = http.Request('GET', uri)
+ ..headers['Accept'] = 'image/*'
+ ..followRedirects = true
+ ..maxRedirects = 5;
+ final response = await client.send(request);
+ final mime = (response.headers['content-type'] ?? '')
+ .split(';')
+ .first
+ .trim()
+ .toLowerCase();
+ final declared = response.contentLength;
+ if (response.statusCode != 200 ||
+ !mime.startsWith('image/') ||
+ (declared != null && declared > maxBytes)) {
+ unawaited(response.stream.drain().catchError((_) {}));
+ return null;
+ }
+ final bytes = BytesBuilder(copy: false);
+ await for (final chunk in response.stream) {
+ bytes.add(chunk);
+ if (bytes.length > maxBytes) return null;
+ }
+ if (bytes.isEmpty) return null;
+ return (bytes: bytes.takeBytes(), mime: mime);
+ } finally {
+ if (_client == null) client.close();
+ }
+ }
+
+ /// A picture a desktop app left on disk for its clipboard, as Word does.
+ /// Readable only where the app's sandbox allows; otherwise null, like any
+ /// other failed fetch.
+ static Future<({Uint8List bytes, String mime})?> _readFile(Uri uri) async {
+ try {
+ final file = File(uri.toFilePath());
+ if (await file.length() > maxBytes) return null;
+ final bytes = await file.readAsBytes();
+ final mime = mimeForFilename(file.path);
+ if (bytes.isEmpty || !mime.startsWith('image/')) return null;
+ return (bytes: bytes, mime: mime);
+ } on Object {
+ return null;
+ }
+ }
+
+ Future _acquire() async {
+ if (_active < maxConcurrent) {
+ _active++;
+ return;
+ }
+ final turn = Completer();
+ _waiting.add(turn);
+ await turn.future;
+ }
+
+ void _release() {
+ if (_waiting.isNotEmpty) {
+ _waiting.removeFirst().complete();
+ } else {
+ _active--;
+ }
+ }
+}
diff --git a/lib/paste/markdown_text.dart b/lib/paste/markdown_text.dart
new file mode 100644
index 0000000..69e2b26
--- /dev/null
+++ b/lib/paste/markdown_text.dart
@@ -0,0 +1,297 @@
+import 'dart:convert';
+
+import 'package:dart_markdown/dart_markdown.dart' as md;
+
+import '../ui/editor/markdown_syntax.dart' show maskOperatorAsterisks;
+import 'html_reader.dart';
+import 'paste_tree.dart';
+
+final _strongSignals = [
+ RegExp(r'^ {0,3}(`{3,}|~{3,})', multiLine: true),
+ RegExp(
+ r'^ {0,3}\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)+\|?\s*$',
+ multiLine: true,
+ ),
+ RegExp(r'!\[[^\]\n]*\]\([^)\s]+\)'),
+];
+final _headingLine = RegExp(r'^#{1,6} \S', multiLine: true);
+final _listLine = RegExp(r'^ {0,8}([-*+]|\d{1,3}[.)]) \S', multiLine: true);
+final _quoteLine = RegExp(r'^ {0,3}> ', multiLine: true);
+final _strong = RegExp(r'(\*\*|__)\S[^*\n]*?\S?\1');
+final _inlineLink = RegExp(r'\[[^\]\n]+\]\((https?://|mailto:|/|#)[^)\s]*\)');
+final _codeSpan = RegExp(r'`[^`\n]+`');
+final _texMath = RegExp(r'\$\$|\\\[|\\\(');
+
+/// Whether plain text was written in markdown — an AI's copy button, a
+/// README, another markdown app — rather than merely containing a star.
+///
+/// One unmistakable sign is enough (a fence, a table's delimiter row, a
+/// linked picture); otherwise the signs have to add up, so that a pasted
+/// "- 5" or a lone *word* stays the plain text it was.
+bool looksLikeMarkdown(String text) {
+ final sample = text.length > 200000 ? text.substring(0, 200000) : text;
+ if (_strongSignals.any((signal) => signal.hasMatch(sample))) return true;
+ var score = 0;
+ score += _headingLine.allMatches(sample).take(2).length * 2;
+ score += _listLine.allMatches(sample).take(3).length;
+ score += _quoteLine.allMatches(sample).take(2).length;
+ score += _strong.allMatches(sample).take(2).length * 2;
+ score += _inlineLink.allMatches(sample).take(2).length * 2;
+ score += _codeSpan.allMatches(sample).take(2).length;
+ score += _texMath.hasMatch(sample) ? 1 : 0;
+ return score >= 3;
+}
+
+md.Markdown _parser() => md.Markdown(
+ enableTaskList: true,
+ enableEmoji: false,
+ enableFencedBlockquote: false,
+);
+
+/// Markdown text as it goes into a markdown note: as written, except that
+/// the pictures it links become the note's own, each on a line of its own,
+/// and the \(…\) and \[…\] math ChatGPT writes becomes the $…$ and $$…$$
+/// the editor draws. Code is never touched.
+PasteOutput normalizeMarkdownPaste(String source) {
+ final text = cleanPastedText(source);
+ final ({List<(int, int)> kept, Map pictures}) ranges;
+ try {
+ ranges = _readRanges(text);
+ } on Object {
+ return PasteOutput(text: text);
+ }
+
+ final out = StringBuffer();
+ final images = [];
+ var cursor = 0;
+ void prose(int end) {
+ if (end <= cursor) return;
+ out.write(_withDollarMath(text.substring(cursor, end)));
+ cursor = end;
+ }
+
+ for (final (from, to) in ranges.kept) {
+ if (from < cursor) continue;
+ prose(from);
+ final picture = ranges.pictures[from];
+ if (picture == null) {
+ out.write(text.substring(from, to));
+ } else {
+ final written = out.toString();
+ final lineStart = written.lastIndexOf('\n') + 1;
+ if (written.substring(lineStart).trim().isNotEmpty) {
+ out
+ ..clear()
+ ..write(written.trimRight())
+ ..write('\n');
+ }
+ out.write('\uFFFC');
+ images.add(picture);
+ final lineEnd = text.indexOf('\n', to);
+ final rest = text.substring(to, lineEnd < 0 ? text.length : lineEnd);
+ if (rest.trim().isNotEmpty) {
+ out.write('\n');
+ cursor = to + rest.length - rest.trimLeft().length;
+ continue;
+ }
+ }
+ cursor = to;
+ }
+ prose(text.length);
+ final result = out.toString();
+ return PasteOutput(text: result, images: placeImages(result, images));
+}
+
+/// Where markdown [text] is not prose, as parsed: code, raw HTML, autolinks
+/// and linked pictures, whose characters are never rewritten, in order. The
+/// pictures a note can hold are also kept by where they start.
+({List<(int, int)> kept, Map pictures}) _readRanges(
+ String text,
+) {
+ final kept = <(int, int)>[];
+ final pictures = {};
+ void visit(md.Node node) {
+ if (node is! md.Element) return;
+ switch (node.type) {
+ case 'codeSpan' ||
+ 'fencedCodeBlock' ||
+ 'indentedCodeBlock' ||
+ 'htmlBlock' ||
+ 'rawHtml' ||
+ 'autolink':
+ kept.add((node.start.offset, node.end.offset));
+ return;
+ case 'image':
+ final image = _linkedImage(
+ node.attributes['destination'],
+ node.attributes['description'] ?? '',
+ );
+ if (image != null) pictures[node.start.offset] = image;
+ kept.add((node.start.offset, node.end.offset));
+ return;
+ }
+ for (final child in node.children) {
+ visit(child);
+ }
+ }
+
+ for (final node in _parser().parse(text)) {
+ visit(node);
+ }
+ kept.sort((a, b) => a.$1.compareTo(b.$1));
+ return (kept: kept, pictures: pictures);
+}
+
+/// Text from the clipboard with what a note can't hold taken out: CRLF line
+/// ends, and the U+FFFC a note reserves for its attachments.
+String cleanPastedText(String text) => text
+ .replaceAll('\r\n', '\n')
+ .replaceAll('\r', '\n')
+ .replaceAll('\uFFFC', '');
+
+PasteImage? _linkedImage(String? destination, String alt) {
+ if (destination == null) return null;
+ final source = destination.trim();
+ if (source.startsWith('https://') || source.startsWith('http://')) {
+ return PasteImage(src: source, alt: alt);
+ }
+ if (source.startsWith('data:image/')) {
+ final comma = source.indexOf(',');
+ if (comma < 0 || !source.substring(0, comma).contains(';base64')) {
+ return null;
+ }
+ try {
+ final bytes = base64.decode(source.substring(comma + 1));
+ if (bytes.isEmpty) return null;
+ return PasteImage(
+ bytes: bytes,
+ mime: source.substring(5, comma).split(';').first,
+ alt: alt,
+ );
+ } on FormatException {
+ return null;
+ }
+ }
+ return null;
+}
+
+final _displayMath = RegExp(r'\\\[([\s\S]{1,4000}?)\\\]');
+final _inlineMath = RegExp(r'\\\(([^\n]{1,1000}?)\\\)');
+
+String _withDollarMath(String prose) {
+ if (!prose.contains(r'\[') && !prose.contains(r'\(')) return prose;
+ return prose
+ .replaceAllMapped(_displayMath, (match) {
+ final tex = match[1]!.trim();
+ if (tex.isEmpty) return match[0]!;
+ return tex.contains('\n') ? '\$\$\n$tex\n\$\$' : '\$\$$tex\$\$';
+ })
+ .replaceAllMapped(_inlineMath, (match) {
+ final tex = match[1]!.trim();
+ return tex.isEmpty ? match[0]! : '\$$tex\$';
+ });
+}
+
+/// Markdown text read into blocks, for a note written without markdown.
+///
+/// Read through the same HTML reader as everything else, with its line
+/// breaks kept: in a note a line ends where it was ended, where CommonMark
+/// would run the lines of a paragraph together. Math is set aside before
+/// the markdown is read, since TeX is full of what markdown would take as
+/// its own escapes and emphasis, and comes back as the math it was.
+List readMarkdownPaste(String source) =>
+ _readMarkdown(cleanPastedText(source), note: false);
+
+/// A note's own markdown read into blocks, the way the note's editor reads
+/// it rather than the way an AI's answer is read: math is only ever between
+/// dollars, and a `*` between two operands is multiplication, not emphasis.
+List readNoteMarkdown(String text) =>
+ _readMarkdown(text, note: true);
+
+List _readMarkdown(String source, {required bool note}) {
+ final text = source.replaceAll(_marks, '');
+ final String html;
+ try {
+ final maths = <({String tex, bool display})>[];
+ final masked = StringBuffer();
+ var cursor = 0;
+ for (final (from, to) in _readRanges(text).kept) {
+ if (from < cursor) continue;
+ masked
+ ..write(
+ _setMathAside(text.substring(cursor, from), maths, brackets: !note),
+ )
+ ..write(text.substring(from, to));
+ cursor = to;
+ }
+ masked.write(_setMathAside(text.substring(cursor), maths, brackets: !note));
+ final parsed = note
+ ? maskOperatorAsterisks(masked.toString(), mask: _operator)
+ : masked.toString();
+ final rendered = _parser().parse(parsed).toHtml().replaceAllMapped(
+ _mathToken,
+ (match) {
+ final math = maths[int.parse(match[1]!)];
+ final kind = math.display ? 'math-display' : 'math-inline';
+ return ' ';
+ },
+ );
+ html = note
+ ? rendered
+ .replaceAll(String.fromCharCode(_operator), '*')
+ .replaceAll(_operatorInAddress, '*')
+ : rendered;
+ } on Object {
+ return [
+ for (final paragraph in text.split(RegExp(r'\n{2,}')))
+ if (paragraph.trim().isNotEmpty) PasteParagraph([PasteText(paragraph)]),
+ ];
+ }
+ return HtmlPasteReader().read(
+ '$html
',
+ );
+}
+
+/// Private-use characters the text is marked with while it is read: the
+/// ends of a math token, and a `*` that is an operator.
+final _marks = RegExp('[\uE000-\uE002]');
+final _mathToken = RegExp('\uE000(\\d+)\uE001');
+const _operator = 0xE002;
+
+/// [_operator] as an address writes it.
+const _operatorInAddress = '%EE%80%82';
+final _dollarDisplayMath = RegExp(r'\$\$([\s\S]{1,4000}?)\$\$');
+
+/// Pandoc's rule for single-dollar math, which keeps "$5 and $10" money:
+/// no space just inside either dollar, and no digit straight after the
+/// closing one.
+final _dollarMath = RegExp(
+ r'(? maths, {
+ required bool brackets,
+}) {
+ if (!prose.contains('\$') &&
+ !(brackets && (prose.contains(r'\[') || prose.contains(r'\(')))) {
+ return prose;
+ }
+ String Function(Match) aside({required bool display}) => (match) {
+ final tex = match[1]!.trim();
+ if (tex.isEmpty) return match[0]!;
+ maths.add((tex: tex, display: display));
+ return '\uE000${maths.length - 1}\uE001';
+ };
+ var result = prose.replaceAllMapped(_dollarDisplayMath, aside(display: true));
+ if (brackets) {
+ result = result
+ .replaceAllMapped(_displayMath, aside(display: true))
+ .replaceAllMapped(_inlineMath, aside(display: false));
+ }
+ return result.replaceAllMapped(_dollarMath, aside(display: false));
+}
diff --git a/lib/paste/mathml_tex.dart b/lib/paste/mathml_tex.dart
new file mode 100644
index 0000000..a7a2be9
--- /dev/null
+++ b/lib/paste/mathml_tex.dart
@@ -0,0 +1,384 @@
+import 'package:html/dom.dart' as dom;
+
+/// TeX for presentation MathML, for math a page drew without keeping the
+/// TeX it was written in: KaTeX once a selection has lost its annotation,
+/// or MathML a page wrote by hand.
+///
+/// The aim is TeX that draws the same, not the TeX the author typed. A
+/// symbol with no TeX name stays the character it is.
+String mathmlToTex(dom.Element math) => _node(math).trim();
+
+String _node(dom.Element element) {
+ final children = element.children;
+ switch (element.localName) {
+ case 'annotation' || 'annotation-xml' || 'none' || 'mprescripts':
+ return '';
+ case 'semantics' || 'maction':
+ return children.isEmpty ? '' : _node(children.first);
+ case 'mi':
+ return _identifier(
+ element.text.trim(),
+ element.attributes['mathvariant'],
+ );
+ case 'mn':
+ return _symbols(element.text.trim());
+ case 'mo':
+ return _operator(element.text.trim());
+ case 'mtext':
+ return _text(element.text);
+ case 'ms':
+ return _text('"${element.text}"');
+ case 'mspace':
+ return _space(element.attributes['width']);
+ case 'msup':
+ return '${_base(children)}^${_argument(children, 1)}';
+ case 'msub':
+ return '${_base(children)}_${_argument(children, 1)}';
+ case 'msubsup':
+ return '${_base(children)}_${_argument(children, 1)}'
+ '^${_argument(children, 2)}';
+ case 'mfrac':
+ final thickness = element.attributes['linethickness']?.trim();
+ final binomial =
+ thickness == '0' || thickness == '0px' || thickness == '0em';
+ return '${binomial ? r'\binom' : r'\frac'}'
+ '${_argument(children, 0)}${_argument(children, 1)}';
+ case 'msqrt':
+ return '\\sqrt{${_join(children)}}';
+ case 'mroot':
+ final index = children.length > 1 ? _node(children[1]) : '';
+ return '\\sqrt[$index]${_argument(children, 0)}';
+ case 'mover':
+ return _over(children);
+ case 'munder':
+ return _under(children);
+ case 'munderover':
+ return _underOver(children);
+ case 'mtable':
+ if (_inScript(element) &&
+ children.every((row) => row.children.length <= 1)) {
+ final rows = [for (final row in children) _node(row).trim()];
+ return '\\substack{${rows.join(r' \\ ')}}';
+ }
+ final align = element.attributes['columnalign'] ?? '';
+ return _table(
+ element,
+ align.startsWith('right left') ? 'aligned' : 'matrix',
+ );
+ case 'mphantom':
+ return '\\phantom{${_join(children)}}';
+ case 'menclose':
+ final notation = element.attributes['notation'] ?? '';
+ if (notation.contains('box')) return '\\boxed{${_join(children)}}';
+ if (notation.contains('diagonalstrike')) {
+ return '\\cancel{${_join(children)}}';
+ }
+ return _join(children);
+ case 'mfenced':
+ final open = element.attributes['open'] ?? '(';
+ final close = element.attributes['close'] ?? ')';
+ final separators = (element.attributes['separators'] ?? ',').trim();
+ final parts = [for (final child in children) _node(child)];
+ return '\\left${_fence(open)}'
+ '${parts.join(separators.isEmpty ? ' ' : separators[0])}'
+ '\\right${_fence(close)}';
+ default:
+ return _row(children);
+ }
+}
+
+/// A row, with a fenced table read as the matrix it draws and any other
+/// pair of stretchy fences as \left and \right.
+String _row(List children) {
+ if (children.length < 2 ||
+ !_isFence(children.first) ||
+ !_isFence(children.last)) {
+ return _join(children);
+ }
+ final open = children.first.text.trim();
+ final close = children.last.text.trim();
+ final inner = children.sublist(1, children.length - 1);
+ if (inner.length == 1 && inner.single.localName == 'mtable') {
+ final environment = switch ((open, close)) {
+ ('(', ')') => 'pmatrix',
+ ('[', ']') => 'bmatrix',
+ ('{', '}') => 'Bmatrix',
+ ('|', '|') => 'vmatrix',
+ ('‖', '‖') || ('∥', '∥') => 'Vmatrix',
+ ('{', '') => 'cases',
+ _ => null,
+ };
+ if (environment != null) return _table(inner.single, environment);
+ }
+ return '\\left${_fence(open)}${_join(inner)}\\right${_fence(close)}';
+}
+
+/// Whether [element] is a script rather than a base — the conditions under
+/// a sum, the index beside a letter — where a table of one column is the
+/// stacked lines \substack draws.
+bool _inScript(dom.Element element) {
+ dom.Element child = element;
+ var parent = element.parent;
+ while (parent != null &&
+ (parent.localName == 'mrow' || parent.localName == 'mstyle')) {
+ child = parent;
+ parent = parent.parent;
+ }
+ if (parent == null || !_scripts.contains(parent.localName)) return false;
+ return parent.children.indexOf(child) > 0;
+}
+
+const _scripts = {'msub', 'msup', 'msubsup', 'munder', 'mover', 'munderover'};
+
+bool _isFence(dom.Element element) =>
+ element.localName == 'mo' && element.attributes['fence'] == 'true';
+
+String _table(dom.Element table, String environment) {
+ final rows = [
+ for (final row in table.children)
+ if (row.localName == 'mtr' || row.localName == 'mlabeledtr')
+ [
+ for (final cell in row.children)
+ if (cell.localName == 'mtd') _node(cell).trim(),
+ ].join(' & '),
+ ];
+ return '\\begin{$environment}${rows.join(r' \\ ')}\\end{$environment}';
+}
+
+/// Pieces of TeX side by side, kept apart only where a command's name would
+/// otherwise run into the letter after it.
+String _join(Iterable elements) {
+ final out = StringBuffer();
+ var previous = '';
+ for (final element in elements) {
+ final piece = _node(element);
+ if (piece.isEmpty) continue;
+ if (_endsInCommand.hasMatch(previous) &&
+ _startsWithLetter.hasMatch(piece)) {
+ out.write(' ');
+ }
+ out.write(piece);
+ previous = piece;
+ }
+ return out.toString();
+}
+
+final _endsInCommand = RegExp(r'\\[A-Za-z]+$');
+final _startsWithLetter = RegExp(r'^[A-Za-z]');
+
+String _base(List children) {
+ if (children.isEmpty) return '{}';
+ final base = _node(children.first);
+ return _isAtom(base) ? base : '{$base}';
+}
+
+String _argument(List children, int index) =>
+ index < children.length ? '{${_node(children[index])}}' : '{}';
+
+bool _isAtom(String tex) => _atom.hasMatch(tex);
+
+final _atom = RegExp(r'^(\\[A-Za-z]+|\\.|[^\\{}\s]|\{[^{}]*\})$');
+
+String _over(List children) {
+ if (children.length < 2) return _join(children);
+ final base = _node(children[0]);
+ final mark = children[1].localName == 'mo' ? children[1].text.trim() : null;
+ final single = _isAtom(base);
+ final accent = switch (mark) {
+ '^' || 'ˆ' || '\u0302' => single ? 'hat' : 'widehat',
+ '~' || '˜' || '\u0303' => single ? 'tilde' : 'widetilde',
+ '¯' ||
+ '‾' ||
+ 'ˉ' ||
+ '_' ||
+ '\u0304' ||
+ '\u0305' => single ? 'bar' : 'overline',
+ '→' || '\u20D7' => single ? 'vec' : 'overrightarrow',
+ '←' => 'overleftarrow',
+ '˙' || '\u0307' => 'dot',
+ '¨' || '\u0308' => 'ddot',
+ 'ˇ' || '\u030C' => 'check',
+ '˘' || '\u0306' => 'breve',
+ '⏞' || '︷' => 'overbrace',
+ _ => null,
+ };
+ if (accent != null) return '\\$accent{$base}';
+ if (_isBigOperator(base)) return '$base^{${_node(children[1])}}';
+ return '\\overset{${_node(children[1])}}{$base}';
+}
+
+String _under(List children) {
+ if (children.length < 2) return _join(children);
+ final base = _node(children[0]);
+ final mark = children[1].localName == 'mo' ? children[1].text.trim() : null;
+ if (mark == '_' || mark == '‾' || mark == '¯' || mark == '\u0332') {
+ return '\\underline{$base}';
+ }
+ if (mark == '⏟' || mark == '︸') return '\\underbrace{$base}';
+ if (_isBigOperator(base)) return '${base}_{${_node(children[1])}}';
+ return '\\underset{${_node(children[1])}}{$base}';
+}
+
+String _underOver(List children) {
+ if (children.length < 3) return _under(children);
+ final base = _node(children[0]);
+ final under = _node(children[1]);
+ final over = _node(children[2]);
+ if (_isBigOperator(base)) return '${base}_{$under}^{$over}';
+ return '\\overset{$over}{\\underset{$under}{$base}}';
+}
+
+bool _isBigOperator(String tex) =>
+ _bigOperators.contains(tex) || tex.startsWith(r'\operatorname');
+
+const _bigOperators = {
+ r'\sum', r'\prod', r'\coprod', r'\int', r'\iint', r'\iiint', r'\oint', //
+ r'\bigcup', r'\bigcap', r'\bigoplus', r'\bigotimes', r'\bigvee', //
+ r'\bigwedge', r'\bigsqcup', r'\lim', r'\limsup', r'\liminf', r'\max', //
+ r'\min', r'\sup', r'\inf', r'\det', r'\Pr', r'\gcd', r'\arg',
+};
+
+String _identifier(String text, String? variant) {
+ if (text.isEmpty) return '';
+ if (text.runes.length > 1) {
+ if (_functions.contains(text)) return '\\$text';
+ return _word.hasMatch(text)
+ ? '\\operatorname{$text}'
+ : '\\mathrm{${_symbols(text)}}';
+ }
+ final symbol = _symbols(text);
+ final command = switch (variant) {
+ 'normal' when _word.hasMatch(text) => 'mathrm',
+ 'bold' => 'mathbf',
+ 'double-struck' => 'mathbb',
+ 'script' => 'mathcal',
+ 'fraktur' => 'mathfrak',
+ 'bold-italic' => 'boldsymbol',
+ 'sans-serif' => 'mathsf',
+ 'monospace' => 'mathtt',
+ _ => null,
+ };
+ return command == null ? symbol : '\\$command{$symbol}';
+}
+
+final _word = RegExp(r'^[A-Za-z]+$');
+
+String _operator(String text) {
+ if (text.isEmpty) return '';
+ if (_functions.contains(text)) return '\\$text';
+ if (text.length > 1 && _word.hasMatch(text)) return '\\operatorname{$text}';
+ return _symbols(text);
+}
+
+const _functions = {
+ 'sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'sinh', 'cosh', 'tanh', 'coth', //
+ 'arcsin', 'arccos', 'arctan', 'log', 'ln', 'lg', 'exp', 'lim', 'limsup', //
+ 'liminf', 'max', 'min', 'sup', 'inf', 'det', 'dim', 'ker', 'deg', 'gcd', //
+ 'hom', 'arg', 'Pr',
+};
+
+String _text(String text) {
+ if (text.trim().isEmpty) {
+ if (text.isEmpty) return '';
+ return switch (text.runes.first) {
+ 0x2009 || 0x202F => r'\,',
+ 0x2005 => r'\:',
+ 0x2004 => r'\;',
+ 0x2003 => r'\quad',
+ _ => r'\ ',
+ };
+ }
+ final escaped = text
+ .replaceAll('\u00A0', ' ')
+ .replaceAll('\\', r'\textbackslash{}')
+ .replaceAllMapped(RegExp(r'[{}$%#&_]'), (match) => '\\${match[0]}');
+ return '\\text{$escaped}';
+}
+
+String _space(String? width) {
+ final em = double.tryParse(
+ (width ?? '').replaceAll(RegExp(r'em$'), '').trim(),
+ );
+ if (em == null) return '';
+ if (em >= 2) return r'\qquad';
+ if (em >= 1) return r'\quad';
+ if (em >= 0.27) return r'\;';
+ if (em >= 0.22) return r'\:';
+ if (em >= 0.16) return r'\,';
+ if (em < 0) return r'\!';
+ return '';
+}
+
+String _fence(String fence) => switch (fence) {
+ '' => '.',
+ '{' => r'\{',
+ '}' => r'\}',
+ '⟨' || '〈' => r'\langle',
+ '⟩' || '〉' => r'\rangle',
+ '‖' || '∥' => r'\Vert',
+ '⌊' => r'\lfloor',
+ '⌋' => r'\rfloor',
+ '⌈' => r'\lceil',
+ '⌉' => r'\rceil',
+ _ => fence,
+};
+
+/// [text] with every character TeX has a name for written by that name.
+String _symbols(String text) {
+ final out = StringBuffer();
+ var previous = '';
+ for (final rune in text.runes) {
+ final character = String.fromCharCode(rune);
+ final piece = _symbolNames[character] ?? character;
+ if (piece.isEmpty) continue;
+ if (_endsInCommand.hasMatch(previous) &&
+ _startsWithLetter.hasMatch(piece)) {
+ out.write(' ');
+ }
+ out.write(piece);
+ previous = piece;
+ }
+ return out.toString();
+}
+
+const _symbolNames = {
+ '\u2061': '', '\u2062': '', '\u2063': '', '\u2064': '', //
+ '\u2212': '-', '#': r'\#', '\$': r'\$', '%': r'\%', '&': r'\&', //
+ '_': r'\_', '{': r'\{', '}': r'\}', '~': r'\sim', '\\': r'\backslash', //
+ 'α': r'\alpha', 'β': r'\beta', 'γ': r'\gamma', 'δ': r'\delta', //
+ 'ϵ': r'\epsilon', 'ε': r'\varepsilon', 'ζ': r'\zeta', 'η': r'\eta', //
+ 'θ': r'\theta', 'ϑ': r'\vartheta', 'ι': r'\iota', 'κ': r'\kappa', //
+ 'λ': r'\lambda', 'μ': r'\mu', 'ν': r'\nu', 'ξ': r'\xi', 'π': r'\pi', //
+ 'ϖ': r'\varpi', 'ρ': r'\rho', 'ϱ': r'\varrho', 'σ': r'\sigma', //
+ 'ς': r'\varsigma', 'τ': r'\tau', 'υ': r'\upsilon', 'ϕ': r'\phi', //
+ 'φ': r'\varphi', 'χ': r'\chi', 'ψ': r'\psi', 'ω': r'\omega', //
+ 'Γ': r'\Gamma', 'Δ': r'\Delta', 'Θ': r'\Theta', 'Λ': r'\Lambda', //
+ 'Ξ': r'\Xi', 'Π': r'\Pi', 'Σ': r'\Sigma', 'Υ': r'\Upsilon', //
+ 'Φ': r'\Phi', 'Ψ': r'\Psi', 'Ω': r'\Omega', //
+ '∀': r'\forall', '∃': r'\exists', '∄': r'\nexists', '∂': r'\partial', //
+ '∇': r'\nabla', '∞': r'\infty', '∅': r'\emptyset', '∈': r'\in', //
+ '∉': r'\notin', '∋': r'\ni', '⊂': r'\subset', '⊃': r'\supset', //
+ '⊆': r'\subseteq', '⊇': r'\supseteq', '⊊': r'\subsetneq', //
+ '∪': r'\cup', '∩': r'\cap', '∖': r'\setminus', '∧': r'\land', //
+ '∨': r'\lor', '¬': r'\neg', '⊕': r'\oplus', '⊗': r'\otimes', //
+ '⊙': r'\odot', '×': r'\times', '÷': r'\div', '·': r'\cdot', //
+ '⋅': r'\cdot', '∘': r'\circ', '∗': r'\ast', '±': r'\pm', '∓': r'\mp', //
+ '≤': r'\le', '≥': r'\ge', '≦': r'\leqq', '≧': r'\geqq', '≪': r'\ll', //
+ '≫': r'\gg', '≠': r'\ne', '≈': r'\approx', '≃': r'\simeq', //
+ '≅': r'\cong', '≡': r'\equiv', '∼': r'\sim', '∝': r'\propto', //
+ '⊥': r'\perp', '∥': r'\parallel', '‖': r'\Vert', '∣': r'\mid', //
+ '∤': r'\nmid', '→': r'\to', '←': r'\leftarrow', '↔': r'\leftrightarrow', //
+ '⇒': r'\Rightarrow', '⇐': r'\Leftarrow', '⇔': r'\Leftrightarrow', //
+ '↦': r'\mapsto', '⟶': r'\longrightarrow', '⟹': r'\Longrightarrow', //
+ '⟺': r'\Longleftrightarrow', '↑': r'\uparrow', '↓': r'\downarrow', //
+ '…': r'\ldots', '⋯': r'\cdots', '⋮': r'\vdots', '⋱': r'\ddots', //
+ '∑': r'\sum', '∏': r'\prod', '∐': r'\coprod', '∫': r'\int', //
+ '∬': r'\iint', '∭': r'\iiint', '∮': r'\oint', '⋃': r'\bigcup', //
+ '⋂': r'\bigcap', '⨁': r'\bigoplus', '⨂': r'\bigotimes', //
+ '⌊': r'\lfloor', '⌋': r'\rfloor', '⌈': r'\lceil', '⌉': r'\rceil', //
+ '⟨': r'\langle', '⟩': r'\rangle', '′': "'", '″': "''", '‴': "'''", //
+ 'ℝ': r'\mathbb{R}', 'ℕ': r'\mathbb{N}', 'ℤ': r'\mathbb{Z}', //
+ 'ℚ': r'\mathbb{Q}', 'ℂ': r'\mathbb{C}', 'ℓ': r'\ell', 'ℏ': r'\hbar', //
+ 'ℜ': r'\Re', 'ℑ': r'\Im', 'ℵ': r'\aleph', '∠': r'\angle', //
+ '△': r'\triangle', '□': r'\square', '°': r'^\circ',
+};
diff --git a/lib/paste/note_text.dart b/lib/paste/note_text.dart
new file mode 100644
index 0000000..06d6e80
--- /dev/null
+++ b/lib/paste/note_text.dart
@@ -0,0 +1,350 @@
+import '../data/note_format.dart';
+import '../ui/editor/editor_formatting.dart'
+ show bulletPrefixes, checkedPrefix, listIndentUnit, uncheckedPrefix;
+import 'html_writer.dart';
+import 'markdown_text.dart';
+import 'paste_tree.dart';
+import 'paste_writers.dart';
+
+/// What a note's [text] looks like to the documents, mail and web editors it
+/// is copied into: its HTML, read the way the note's editor reads it. That is
+/// as markdown when [markdown], and otherwise as the editor's own plain
+/// format, with the bold, italic, heading and subtitle [formats] it keeps
+/// beside the text.
+///
+/// Each U+FFFC an attachment holds its place with is written as the HTML
+/// [embeds] gives for its offset, and left out where it gives none.
+String noteHtml(
+ String text, {
+ required bool markdown,
+ List formats = const [],
+ Map embeds = const {},
+}) {
+ final anchored = _Anchored(text);
+ return writeHtml(
+ _readNote(anchored.text, markdown: markdown, formats: formats),
+ embeds: {
+ for (var i = 0; i < anchored.offsets.length; i++)
+ anchored.token(i): embeds[anchored.offsets[i]] ?? '',
+ },
+ );
+}
+
+/// A note's [text] rewritten for a note of the other kind, through the same
+/// writers a paste goes through: as markdown when [toMarkdown], and otherwise
+/// in the editor's own plain format, with the styles it keeps beside the
+/// text.
+///
+/// The attachments keep their places: [anchors] maps the offset of each
+/// U+FFFC in [text] to where it is in the rewritten text.
+({String text, List formats, Map anchors})
+convertNote(
+ String text, {
+ required bool toMarkdown,
+ List formats = const [],
+}) {
+ final anchored = _Anchored(text);
+ final blocks = _readNote(
+ anchored.text,
+ markdown: !toMarkdown,
+ formats: formats,
+ );
+ // Pictures a note's markdown only links to have no attachment to become.
+ final written =
+ (toMarkdown ? writeMarkdownPaste(blocks) : writePlainPaste(blocks))
+ .withoutPictures();
+ final units = List.of(written.text.codeUnits);
+ final anchors = {};
+ for (var i = 0; i < units.length; i++) {
+ final index = anchored.indexOf(units[i]);
+ if (index == null) continue;
+ anchors[anchored.offsets[index]] = i;
+ units[i] = 0xFFFC;
+ }
+ return (
+ text: String.fromCharCodes(units),
+ formats: written.formats,
+ anchors: anchors,
+ );
+}
+
+List _readNote(
+ String text, {
+ required bool markdown,
+ required List formats,
+}) =>
+ markdown ? readNoteMarkdown(text) : _PlainNoteReader(text, formats).read();
+
+/// A note's text with the U+FFFC of each attachment swapped for a private-use
+/// character of its own, which every reader and writer carries as one more
+/// character of the words. One for one, so every offset stays where it was.
+class _Anchored {
+ factory _Anchored(String source) {
+ final offsets = [
+ for (var i = 0; i < source.length; i++)
+ if (source.codeUnitAt(i) == 0xFFFC) i,
+ ];
+ var base = _firstToken;
+ bool taken(int base) => source.codeUnits.any(
+ (unit) => unit >= base && unit < base + offsets.length,
+ );
+ while (offsets.isNotEmpty &&
+ base + offsets.length * 2 <= _lastToken &&
+ taken(base)) {
+ base += offsets.length;
+ }
+ final units = List.of(source.codeUnits);
+ for (var i = 0; i < offsets.length; i++) {
+ units[offsets[i]] = base + i;
+ }
+ return _Anchored._(String.fromCharCodes(units), offsets, base);
+ }
+
+ _Anchored._(this.text, this.offsets, this._base);
+
+ /// Past the private-use characters the markdown reader marks with.
+ static const _firstToken = 0xE100;
+ static const _lastToken = 0xF8FF;
+
+ final String text;
+
+ /// Where each U+FFFC was, in order.
+ final List offsets;
+ final int _base;
+
+ String token(int index) => String.fromCharCode(_base + index);
+
+ /// Which attachment [unit] stands for, or null when it is a character of
+ /// the words.
+ int? indexOf(int unit) =>
+ unit >= _base && unit < _base + offsets.length ? unit - _base : null;
+}
+
+/// A note written without markdown, read into blocks: its paragraphs and
+/// line breaks, its heading and subtitle lines, its bold and italic, the
+/// lists it writes with glyphs and numbers, and its tables, which draw as
+/// grids with the setting off too.
+class _PlainNoteReader {
+ _PlainNoteReader(this.text, List formats)
+ : _inline = [
+ for (final range in formats)
+ if (range.format.isInline) range,
+ ],
+ _paragraph = [
+ for (final range in formats)
+ if (range.format.isParagraph) range,
+ ];
+
+ final String text;
+ final List _inline;
+ final List _paragraph;
+ final _blocks = [];
+ final _lists = <_ListBuilder>[];
+ List? _words;
+
+ static final _number = RegExp(r'^(\d{1,9})\. ');
+
+ List read() {
+ final lines = <(int, int)>[];
+ for (var start = 0; ;) {
+ final end = text.indexOf('\n', start);
+ if (end < 0) {
+ lines.add((start, text.length));
+ break;
+ }
+ lines.add((start, end));
+ start = end + 1;
+ }
+ var i = 0;
+ while (i < lines.length) {
+ final (start, end) = lines[i];
+ if (text.substring(start, end).trim().isEmpty) {
+ _endWords();
+ _endLists();
+ i++;
+ continue;
+ }
+ final table = _tableAt(lines, i);
+ if (table != null) {
+ _endWords();
+ _endLists();
+ _blocks.add(table.table);
+ i = table.next;
+ continue;
+ }
+ if (_addItem(start, end)) {
+ _endWords();
+ i++;
+ continue;
+ }
+ _endLists();
+ final style = _paragraphStyle(start, end);
+ if (style != null) {
+ _endWords();
+ _blocks.add(
+ PasteHeading(
+ style == NoteFormat.heading ? 1 : 2,
+ _inlines(start, end),
+ ),
+ );
+ } else {
+ final words = _words ??= [];
+ if (words.isNotEmpty) words.add(const PasteBreak());
+ words.addAll(_inlines(start, end));
+ }
+ i++;
+ }
+ _endWords();
+ _endLists();
+ return _blocks;
+ }
+
+ /// Rows the note draws as a table, read the way it draws them, starting at
+ /// line [i]; null when line [i] does not start one.
+ ({PasteTable table, int next})? _tableAt(List<(int, int)> lines, int i) {
+ bool row(int at) =>
+ at < lines.length &&
+ text.substring(lines[at].$1, lines[at].$2).trimLeft().startsWith('|');
+ if (!row(i) || !row(i + 1)) return null;
+ var next = i;
+ while (row(next)) {
+ next++;
+ }
+ final read = readNoteMarkdown(
+ text.substring(lines[i].$1, lines[next - 1].$2),
+ );
+ if (read case [PasteTable table]) return (table: table, next: next);
+ return null;
+ }
+
+ /// Adds the line from [start] to [end] to the lists being read when it is
+ /// a list item, and says whether it was.
+ bool _addItem(int start, int end) {
+ final line = text.substring(start, end);
+ var indent = 0;
+ while (indent < line.length &&
+ (line[indent] == ' ' || line[indent] == '\t')) {
+ indent++;
+ }
+ final rest = line.substring(indent);
+ final number = _number.firstMatch(rest);
+ final marker =
+ [
+ ...bulletPrefixes,
+ uncheckedPrefix,
+ checkedPrefix,
+ ].where(rest.startsWith).firstOrNull ??
+ number?[0];
+ if (marker == null) return false;
+ final depth = indent ~/ listIndentUnit.length;
+ final ordered = number != null;
+ final words = _inlines(start + indent + marker.length, end);
+ final item = (
+ blocks: [if (words.isNotEmpty) PasteParagraph(words)],
+ checked: marker == uncheckedPrefix
+ ? false
+ : marker == checkedPrefix
+ ? true
+ : null,
+ );
+ while (_lists.isNotEmpty && _lists.last.depth > depth) {
+ _endList();
+ }
+ final open = _lists.lastOrNull;
+ if (open != null && open.depth == depth) {
+ if (open.ordered == ordered) {
+ open.items.add(item);
+ return true;
+ }
+ _endList();
+ }
+ _lists.add(
+ _ListBuilder(
+ depth: depth,
+ ordered: ordered,
+ start: number == null ? 1 : int.parse(number[1]!),
+ )..items.add(item),
+ );
+ return true;
+ }
+
+ /// The heading or subtitle the line from [start] to [end] is set in.
+ NoteFormat? _paragraphStyle(int start, int end) {
+ NoteFormat? style;
+ for (final range in _paragraph) {
+ if (range.start >= end || range.end <= start) continue;
+ if (range.format == NoteFormat.heading) return NoteFormat.heading;
+ style = range.format;
+ }
+ return style;
+ }
+
+ /// The words from [start] to [end], cut where their bold and italic do.
+ List _inlines(int start, int end) {
+ if (end <= start) return const [];
+ final cuts = {start, end};
+ for (final range in _inline) {
+ if (range.start > start && range.start < end) cuts.add(range.start);
+ if (range.end > start && range.end < end) cuts.add(range.end);
+ }
+ final ordered = cuts.toList()..sort();
+ return [
+ for (var i = 0; i + 1 < ordered.length; i++)
+ PasteText(
+ text.substring(ordered[i], ordered[i + 1]),
+ styles: {
+ for (final range in _inline)
+ if (range.start <= ordered[i] && range.end >= ordered[i + 1])
+ range.format == NoteFormat.bold
+ ? PasteStyle.bold
+ : PasteStyle.italic,
+ },
+ ),
+ ];
+ }
+
+ void _endWords() {
+ final words = _words;
+ if (words != null && words.isNotEmpty) _blocks.add(PasteParagraph(words));
+ _words = null;
+ }
+
+ void _endList() {
+ final list = _lists.removeLast().build();
+ final parent = _lists.lastOrNull;
+ if (parent == null) {
+ _blocks.add(list);
+ return;
+ }
+ if (parent.items.isEmpty) parent.items.add((blocks: [], checked: null));
+ parent.items.last.blocks.add(list);
+ }
+
+ void _endLists() {
+ while (_lists.isNotEmpty) {
+ _endList();
+ }
+ }
+}
+
+class _ListBuilder {
+ _ListBuilder({
+ required this.depth,
+ required this.ordered,
+ required this.start,
+ });
+
+ final int depth;
+ final bool ordered;
+ final int start;
+ final items = <({List blocks, bool? checked})>[];
+
+ PasteList build() => PasteList(
+ ordered: ordered,
+ start: start,
+ items: [
+ for (final item in items)
+ PasteListItem(item.blocks, checked: item.checked),
+ ],
+ );
+}
diff --git a/lib/paste/paste_converter.dart b/lib/paste/paste_converter.dart
new file mode 100644
index 0000000..90bc866
--- /dev/null
+++ b/lib/paste/paste_converter.dart
@@ -0,0 +1,153 @@
+import 'dart:isolate';
+
+import 'clipboard_snapshot.dart';
+import 'html_reader.dart';
+import 'markdown_text.dart';
+import 'paste_tree.dart';
+import 'paste_writers.dart';
+
+/// Which of the clipboard's representations a paste was read from.
+enum PasteKind { code, html, rtf, markdown, text, nothing }
+
+class PasteConversion {
+ const PasteConversion(this.kind, this.output);
+
+ final PasteKind kind;
+ final PasteOutput output;
+}
+
+/// Reads what the clipboard holds into what goes into the note, written as
+/// markdown when [markdown] is on and in the editor's plain format when not.
+///
+/// The richest representation that says something wins, in this order:
+/// code an editor copied (it is code, whatever its colours look like), HTML
+/// that carries structure, rich text, plain text written in markdown, and
+/// plain text. Two exceptions keep the obvious answer: the markdown an AI's
+/// copy button writes beats the HTML it renders alongside, since it says
+/// the same and is what the writer had; and HTML that is only a wrapper
+/// around words loses to the words.
+PasteConversion convertPaste(
+ ClipboardSnapshot snapshot, {
+ required bool markdown,
+}) {
+ final write = markdown ? writeMarkdownPaste : writePlainPaste;
+ final text = snapshot.text == null ? null : cleanPastedText(snapshot.text!);
+ final hasText = text != null && text.trim().isNotEmpty;
+
+ if (snapshot.vscode != null && hasText) {
+ return PasteConversion(
+ PasteKind.code,
+ write([PasteCode(_trimCode(text), language: snapshot.vscodeMode)]),
+ );
+ }
+
+ final baseUrl = Uri.tryParse(snapshot.sourceUrl ?? '');
+ for (final (kind, markup) in [
+ (PasteKind.html, snapshot.html),
+ (PasteKind.rtf, snapshot.rtfHtml),
+ ]) {
+ if (markup == null || markup.trim().isEmpty) continue;
+ final reader = HtmlPasteReader(
+ baseUrl: baseUrl != null && baseUrl.hasScheme ? baseUrl : null,
+ resources: snapshot.resources,
+ );
+ if (hasText && reader.readsAsCode(markup)) {
+ return PasteConversion(
+ PasteKind.code,
+ write([PasteCode(_trimCode(text))]),
+ );
+ }
+ final blocks = reader.read(markup);
+ if (hasText && !_hasPictures(blocks) && _isMarkdownSource(text, blocks)) {
+ break;
+ }
+ if (!hasText || _hasStructure(blocks)) {
+ return PasteConversion(kind, write(blocks));
+ }
+ }
+
+ if (!hasText) {
+ return const PasteConversion(PasteKind.nothing, PasteOutput(text: ''));
+ }
+ if (looksLikeMarkdown(text)) {
+ return PasteConversion(
+ PasteKind.markdown,
+ markdown
+ ? normalizeMarkdownPaste(text)
+ : writePlainPaste(readMarkdownPaste(text)),
+ );
+ }
+ return PasteConversion(PasteKind.text, PasteOutput(text: text));
+}
+
+/// [convertPaste] away from the UI isolate when the clipboard is big enough
+/// for reading it to cost a frame.
+Future convertPasteInBackground(
+ ClipboardSnapshot snapshot, {
+ required bool markdown,
+}) {
+ final size =
+ (snapshot.html?.length ?? 0) +
+ (snapshot.rtfHtml?.length ?? 0) +
+ (snapshot.text?.length ?? 0);
+ if (size < _backgroundThreshold) {
+ return Future.value(convertPaste(snapshot, markdown: markdown));
+ }
+ return Isolate.run(() => convertPaste(snapshot, markdown: markdown));
+}
+
+const _backgroundThreshold = 16 * 1024;
+
+String _trimCode(String text) {
+ var code = text;
+ while (code.startsWith('\n')) {
+ code = code.substring(1);
+ }
+ return code.trimRight();
+}
+
+bool _hasPictures(List blocks) => blocks.any(
+ (block) => switch (block) {
+ PasteParagraph(:final inlines) || PasteHeading(:final inlines) =>
+ inlines.any((inline) => inline is PasteImageInline),
+ PasteList(:final items) => items.any((item) => _hasPictures(item.blocks)),
+ PasteQuote(:final blocks) => _hasPictures(blocks),
+ PasteTable(:final rows) => rows.any(
+ (row) =>
+ row.any((cell) => cell.any((inline) => inline is PasteImageInline)),
+ ),
+ _ => false,
+ },
+);
+
+/// Whether [text] is the markdown an AI's copy button writes beside what it
+/// drew from it, rather than the drawing's own words, as a selection's text
+/// is. Code and stars on the page are in both; only the source has the
+/// marks markdown draws with, so it has more of them.
+bool _isMarkdownSource(String text, List blocks) {
+ if (!looksLikeMarkdown(text)) return false;
+ return _marks(text) - _marks(writePlainPaste(blocks).text) >= 4;
+}
+
+int _marks(String text) {
+ var count = 0;
+ for (final unit in text.codeUnits) {
+ if (unit == 0x23 || unit == 0x2A || unit == 0x60 || unit == 0x7C) count++;
+ }
+ return count;
+}
+
+/// Whether HTML said more than its words: anything but plain paragraphs.
+bool _hasStructure(List blocks) => blocks.any(
+ (block) => switch (block) {
+ PasteParagraph(:final inlines) => inlines.any(
+ (inline) => switch (inline) {
+ PasteText(:final styles, :final href) =>
+ styles.isNotEmpty || href != null,
+ PasteBreak() => false,
+ _ => true,
+ },
+ ),
+ _ => true,
+ },
+);
diff --git a/lib/paste/paste_insertion.dart b/lib/paste/paste_insertion.dart
new file mode 100644
index 0000000..cfef478
--- /dev/null
+++ b/lib/paste/paste_insertion.dart
@@ -0,0 +1,381 @@
+import 'package:flutter/services.dart' show TextSelection;
+
+import '../data/note_format.dart';
+import '../ui/editor/editor_formatting.dart'
+ show bulletPrefixes, checkedPrefix, uncheckedPrefix;
+import '../ui/editor/markdown_editing.dart'
+ show MarkdownLinePrefix, markdownLinePrefix;
+import 'markdown_text.dart' show cleanPastedText;
+import 'paste_tree.dart';
+
+/// What the note holds where a paste lands, as far as it changes what the
+/// paste may write there.
+enum PasteTarget {
+ /// Ordinary writing.
+ text,
+
+ /// Inside a fenced code block: only the clipboard's own text, as is.
+ code,
+
+ /// A table row: only words, with line breaks as .
+ table,
+}
+
+/// A paste worked out against the note it lands in: the range of the note
+/// it replaces, and what replaces it.
+class PasteInsertion {
+ const PasteInsertion({
+ required this.start,
+ required this.end,
+ required this.text,
+ this.formats = const [],
+ this.images = const [],
+ });
+
+ final int start;
+ final int end;
+ final String text;
+
+ /// Plain-format styles over [text], relative to [start].
+ final List formats;
+
+ /// Pictures, relative to [start].
+ final List images;
+}
+
+/// Fits [output] into [note] at [selection] the way Notion does, so that a
+/// paste reads as if it had been written there.
+///
+/// The first pasted line joins the words before the caret and takes on its
+/// line's kind — pasting a heading into a list item adds words to the item —
+/// and the last pasted line joins the words after it. Lines pasted into a
+/// list become more items of it, as Return would make them, and lines pasted
+/// into a quote stay in the quote. A block that can't share a line (a
+/// fence, a table row, a picture) gets a line of its own.
+PasteInsertion planPaste({
+ required String note,
+ required TextSelection selection,
+ required PasteOutput output,
+ required bool markdown,
+ PasteTarget target = PasteTarget.text,
+ String? clipboardText,
+}) {
+ final start = selection.start.clamp(0, note.length);
+ final end = selection.end.clamp(start, note.length);
+
+ switch (target) {
+ case PasteTarget.code:
+ return PasteInsertion(
+ start: start,
+ end: end,
+ text: cleanPastedText(clipboardText ?? _words(output.text)),
+ );
+ case PasteTarget.table:
+ final words = cleanPastedText(clipboardText ?? _words(output.text))
+ .split('\n')
+ .map((line) => line.trim())
+ .where((line) => line.isNotEmpty)
+ .join(' ')
+ .replaceAllMapped(
+ RegExp(r'(\\)?\|'),
+ (match) => match[1] == null ? r'\|' : match[0]!,
+ );
+ return PasteInsertion(start: start, end: end, text: words);
+ case PasteTarget.text:
+ break;
+ }
+
+ final address = (clipboardText ?? output.text).trim();
+ if (markdown &&
+ end > start &&
+ _webAddress.hasMatch(address) &&
+ !note.substring(start, end).contains('\n')) {
+ return PasteInsertion(
+ start: start,
+ end: end,
+ text:
+ '[${note.substring(start, end)}](${address.replaceAll(' ', '%20')})',
+ );
+ }
+
+ final line = markdown
+ ? _MarkdownLine.at(note, start)
+ : _PlainLine.at(note, start);
+ final lineEnd = note.indexOf('\n', end) < 0
+ ? note.length
+ : note.indexOf('\n', end);
+ final wordsBefore = note
+ .substring(line.contentStart, start)
+ .trim()
+ .isNotEmpty;
+ final wordsAfter = note.substring(end, lineEnd).trim().isNotEmpty;
+
+ final pasted = output.text.split('\n');
+ final out = StringBuffer();
+ // For each pasted line: where it began in the output, where it begins in
+ // what is written, and how much was cut from or added to its front.
+ final moves = <({int from, int to, int cut, int added})>[];
+ var replaceFrom = start;
+ var inFence = false;
+ var item = 0;
+ var from = 0;
+
+ for (var index = 0; index < pasted.length; index++) {
+ final source = pasted[index];
+ final lineFrom = from;
+ from += source.length + 1;
+ var cut = 0;
+ var added = '';
+ final fence = _fence.hasMatch(source);
+
+ if (index == 0) {
+ final standsAlone = _standsAlone(source, markdown: markdown);
+ if (standsAlone) {
+ if (wordsBefore) {
+ added = '\n${line.continuation}';
+ } else if (line.isItem && !wordsAfter) {
+ // An empty item: the block replaces the marker rather than
+ // hanging off it.
+ replaceFrom = line.continuationEnd;
+ }
+ } else if (wordsBefore || line.hasStructure) {
+ cut = line.structureOf(source);
+ }
+ } else if (inFence) {
+ added = line.continuation;
+ } else if (source.trim().isEmpty) {
+ if (line.isItem) {
+ moves.add((from: lineFrom, to: -1, cut: 0, added: 0));
+ continue;
+ }
+ added = line.continuation.trimRight();
+ } else if (_isPictureLine(source)) {
+ added = '';
+ } else if (line.isItem && line.structureOf(source) == 0 && !fence) {
+ added = line.continuation + line.nextMarker(++item);
+ } else {
+ added = line.continuation;
+ }
+ if (fence) inFence = !inFence;
+
+ if (index > 0) out.write('\n');
+ final to = out.length + added.length;
+ out
+ ..write(added)
+ ..write(source.substring(cut));
+ moves.add((from: lineFrom, to: to, cut: cut, added: added.length));
+ }
+ final lastLine = pasted.last;
+ if (wordsAfter && _standsAlone(lastLine, markdown: markdown)) out.write('\n');
+
+ final text = out.toString();
+ int? map(int offset) {
+ for (var i = moves.length - 1; i >= 0; i--) {
+ final move = moves[i];
+ if (offset < move.from) continue;
+ if (move.to < 0) return null;
+ final into = offset - move.from;
+ return move.to + (into < move.cut ? 0 : into - move.cut);
+ }
+ return null;
+ }
+
+ final shift = start - replaceFrom;
+ final formats = [];
+ for (final range in output.formats) {
+ // The first line's own kind gives way to the line it joins.
+ if (range.format.isParagraph &&
+ range.start < pasted.first.length &&
+ (wordsBefore || line.hasStructure)) {
+ continue;
+ }
+ final a = map(range.start);
+ final b = map(range.end);
+ if (a == null || b == null || b <= a) continue;
+ formats.add(
+ NoteFormatRange(start: a + shift, end: b + shift, format: range.format),
+ );
+ }
+ final images = [];
+ for (final image in output.images) {
+ final at = map(image.offset);
+ if (at != null) images.add(PastedImage(at + shift, image.image));
+ }
+ return PasteInsertion(
+ start: replaceFrom,
+ end: end,
+ text: text,
+ formats: formats,
+ images: images,
+ );
+}
+
+final _webAddress = RegExp(r'^(https?://|mailto:)\S+$');
+final _fence = RegExp(r'^\s*(`{3,}|~{3,})');
+
+String _words(String text) => text.replaceAll('', '');
+
+bool _isPictureLine(String line) =>
+ line.isNotEmpty && line.replaceAll('', '').isEmpty;
+
+/// A line that can't share its line with other words: a fence, a table
+/// row, a rule, display math, a picture.
+bool _standsAlone(String line, {required bool markdown}) {
+ if (_isPictureLine(line)) return true;
+ final trimmed = line.trimLeft();
+ if (trimmed.startsWith('|')) return true;
+ if (!markdown) return false;
+ return _fence.hasMatch(line) ||
+ trimmed.startsWith(r'$$') ||
+ RegExp(r'^([-*_])(\s*\1){2,}\s*$').hasMatch(trimmed);
+}
+
+/// The structure of the line a paste lands on.
+abstract class _Line {
+ /// Where the line's words begin, after its structure.
+ int get contentStart;
+
+ /// What a pasted line keeps in front of it to stay in this line's
+ /// container: indentation and quote markers.
+ String get continuation;
+
+ /// Where [continuation] ends in the note.
+ int get continuationEnd;
+
+ bool get isItem;
+
+ /// Whether the line is a list item, a quote or a heading — anything with a
+ /// kind of its own for pasted words to take on.
+ bool get hasStructure;
+
+ /// The marker for the item [count] after this one.
+ String nextMarker(int count);
+
+ /// How much of a pasted [line] is its own structure, to cut when it joins
+ /// this one.
+ int structureOf(String line);
+}
+
+final _pastedMarkdownStructure = RegExp(
+ r'^[ \t]*((>[ \t]?)+)?([-*+][ \t]+|\d{1,9}[.)][ \t]+)?(\[[ xX]\][ \t]+)?(#{1,6}[ \t]+)?',
+);
+
+class _MarkdownLine implements _Line {
+ _MarkdownLine._(this._note, this._prefix);
+
+ factory _MarkdownLine.at(String note, int offset) =>
+ _MarkdownLine._(note, markdownLinePrefix(note, offset));
+
+ final String _note;
+ final MarkdownLinePrefix _prefix;
+
+ @override
+ int get contentStart => _prefix.headingEnd;
+
+ @override
+ bool get isItem => _prefix.isListItem;
+
+ @override
+ bool get hasStructure =>
+ isItem || _prefix.isQuoted || _prefix.headingLevel > 0;
+
+ @override
+ int get continuationEnd => isItem ? _prefix.markerStart : _prefix.quoteEnd;
+
+ @override
+ String get continuation =>
+ _note.substring(_prefix.lineStart, continuationEnd);
+
+ @override
+ String nextMarker(int count) {
+ final marker = _note.substring(_prefix.markerStart, _prefix.markerEnd);
+ final box = _prefix.isTask && !_prefix.glyph ? '[ ] ' : '';
+ if (_prefix.isOrdered) {
+ final digits = RegExp(r'^\d+').firstMatch(marker)![0]!;
+ final delimiter = marker.substring(digits.length);
+ return '${int.parse(digits) + count}$delimiter $box';
+ }
+ if (marker == '☑') return '☐ ';
+ return '$marker $box';
+ }
+
+ @override
+ int structureOf(String line) =>
+ _pastedMarkdownStructure.firstMatch(line)?.end ?? 0;
+}
+
+final _plainNumber = RegExp(r'^\d{1,9}\. ');
+
+class _PlainLine implements _Line {
+ _PlainLine._({
+ required this.lineStart,
+ required this.indent,
+ required this.marker,
+ required this.note,
+ });
+
+ factory _PlainLine.at(String note, int offset) {
+ final lineStart = offset == 0 ? 0 : note.lastIndexOf('\n', offset - 1) + 1;
+ var indent = lineStart;
+ while (indent < note.length &&
+ (note[indent] == ' ' || note[indent] == '\t')) {
+ indent++;
+ }
+ final rest = note.substring(indent);
+ final marker =
+ _glyphPrefixes.where(rest.startsWith).firstOrNull ??
+ _plainNumber.firstMatch(rest)?[0] ??
+ '';
+ return _PlainLine._(
+ lineStart: lineStart,
+ indent: indent,
+ marker: marker,
+ note: note,
+ );
+ }
+
+ final String note;
+ final int lineStart;
+ final int indent;
+ final String marker;
+
+ @override
+ int get contentStart => indent + marker.length;
+
+ @override
+ bool get isItem => marker.isNotEmpty;
+
+ @override
+ bool get hasStructure => isItem;
+
+ @override
+ int get continuationEnd => indent;
+
+ @override
+ String get continuation => note.substring(lineStart, indent);
+
+ @override
+ String nextMarker(int count) {
+ if (marker == checkedPrefix) return uncheckedPrefix;
+ final number = _plainNumber.firstMatch(marker);
+ if (number != null) {
+ return '${int.parse(marker.substring(0, marker.length - 2)) + count}. ';
+ }
+ return marker;
+ }
+
+ @override
+ int structureOf(String line) {
+ var at = 0;
+ while (at < line.length && (line[at] == ' ' || line[at] == '\t')) {
+ at++;
+ }
+ final rest = line.substring(at);
+ final glyph =
+ _glyphPrefixes.where(rest.startsWith).firstOrNull ??
+ _plainNumber.firstMatch(rest)?[0];
+ return glyph == null ? 0 : at + glyph.length;
+ }
+}
+
+final _glyphPrefixes = [...bulletPrefixes, uncheckedPrefix, checkedPrefix];
diff --git a/lib/paste/paste_tree.dart b/lib/paste/paste_tree.dart
new file mode 100644
index 0000000..240152c
--- /dev/null
+++ b/lib/paste/paste_tree.dart
@@ -0,0 +1,245 @@
+import 'dart:typed_data';
+
+import '../data/note_format.dart';
+
+/// What a paste reads as, between the clipboard's markup and the note.
+///
+/// Every source — a page's HTML, the HTML Apple builds from RTF, the
+/// markdown an AI's copy button writes — is read into this one shape, and
+/// the note's text is written from it: as markdown in a markdown note, as
+/// the editor's own plain format in one that is not. Keeping the reading and
+/// the writing apart is what lets the two writers agree on what a paste
+/// contains while disagreeing about how to say it.
+sealed class PasteBlock {
+ const PasteBlock();
+}
+
+final class PasteParagraph extends PasteBlock {
+ const PasteParagraph(this.inlines);
+
+ final List inlines;
+}
+
+final class PasteHeading extends PasteBlock {
+ const PasteHeading(this.level, this.inlines);
+
+ /// 1 to 6.
+ final int level;
+ final List inlines;
+}
+
+final class PasteList extends PasteBlock {
+ const PasteList({required this.ordered, required this.items, this.start = 1});
+
+ final bool ordered;
+
+ /// The first item's number, for an ordered list.
+ final int start;
+ final List items;
+}
+
+final class PasteListItem {
+ const PasteListItem(this.blocks, {this.checked});
+
+ final List blocks;
+
+ /// Null for an ordinary item; true or false for a to-do.
+ final bool? checked;
+}
+
+final class PasteQuote extends PasteBlock {
+ const PasteQuote(this.blocks);
+
+ final List blocks;
+}
+
+final class PasteCode extends PasteBlock {
+ const PasteCode(this.text, {this.language});
+
+ final String text;
+
+ /// The info string a fence carries: `python`, `ts`. Null when the source
+ /// did not say.
+ final String? language;
+}
+
+enum PasteAlign { none, center, right }
+
+final class PasteTable extends PasteBlock {
+ const PasteTable(this.rows, this.aligns);
+
+ /// The first row is the header. Every row has [aligns].length cells; a
+ /// merged cell's value is in its first cell and the rest are empty.
+ final List>> rows;
+ final List aligns;
+}
+
+final class PasteRule extends PasteBlock {
+ const PasteRule();
+}
+
+final class PasteMathBlock extends PasteBlock {
+ const PasteMathBlock(this.tex);
+
+ final String tex;
+}
+
+/// One run of a block's words.
+sealed class PasteInline {
+ const PasteInline();
+}
+
+enum PasteStyle { bold, italic, underline, strike, superscript, subscript }
+
+final class PasteText extends PasteInline {
+ const PasteText(this.text, {this.styles = const {}, this.href});
+
+ final String text;
+ final Set styles;
+
+ /// Where the words link to, already resolved against the page.
+ final String? href;
+}
+
+final class PasteCodeSpan extends PasteInline {
+ const PasteCodeSpan(this.code, {this.styles = const {}, this.href});
+
+ final String code;
+ final Set styles;
+ final String? href;
+}
+
+final class PasteMath extends PasteInline {
+ const PasteMath(this.tex);
+
+ final String tex;
+}
+
+/// A line break inside a block.
+final class PasteBreak extends PasteInline {
+ const PasteBreak();
+}
+
+final class PasteImageInline extends PasteInline {
+ const PasteImageInline(this.image, {this.href});
+
+ final PasteImage image;
+ final String? href;
+}
+
+/// A picture the paste carries, before it is anything in the note.
+///
+/// Either its bytes came with the clipboard — a data URI, an RTFD attachment,
+/// a Safari web archive's resource — or [src] says where to fetch them.
+class PasteImage {
+ const PasteImage({
+ this.src,
+ this.bytes,
+ this.mime,
+ this.alt = '',
+ this.width,
+ this.height,
+ });
+
+ /// An absolute http(s) or file URL, or null when [bytes] are here.
+ final String? src;
+ final Uint8List? bytes;
+ final String? mime;
+ final String alt;
+
+ /// The size the source displayed it at, in CSS pixels, when it said.
+ final double? width;
+ final double? height;
+
+ bool get hasBytes => bytes != null && bytes!.isNotEmpty;
+
+ /// How much of the writing column the picture takes: its displayed width
+ /// against a 720px column, so a logo stays a logo and a figure stays wide.
+ double get widthFactor {
+ final shown = width;
+ if (shown == null || shown <= 0) return 1;
+ return (shown / referenceColumnWidth).clamp(0.25, 1.0);
+ }
+
+ static const double referenceColumnWidth = 720;
+
+ Map toMessage() => {
+ 'src': src,
+ 'bytes': bytes,
+ 'mime': mime,
+ 'alt': alt,
+ 'width': width,
+ 'height': height,
+ };
+
+ static PasteImage fromMessage(Map raw) => PasteImage(
+ src: raw['src'] as String?,
+ bytes: raw['bytes'] as Uint8List?,
+ mime: raw['mime'] as String?,
+ alt: raw['alt'] as String? ?? '',
+ width: (raw['width'] as num?)?.toDouble(),
+ height: (raw['height'] as num?)?.toDouble(),
+ );
+}
+
+/// What a paste puts into a note: its text, the styles a note without
+/// markdown holds beside the text, and its pictures.
+class PasteOutput {
+ const PasteOutput({
+ required this.text,
+ this.formats = const [],
+ this.images = const [],
+ });
+
+ /// One U+FFFC stands for each of [images], on a line of its own.
+ final String text;
+
+ /// Bold, italic, heading and subtitle over [text], for a note written
+ /// without markdown. Always empty for markdown, which says it in the text.
+ final List formats;
+ final List images;
+
+ bool get isEmpty => text.isEmpty;
+
+ /// Whether there is anything here besides pictures.
+ bool get hasWords => text.replaceAll('\uFFFC', '').trim().isNotEmpty;
+
+ /// The same paste for a note that can't hold pictures, with every style
+ /// moved along with the words.
+ PasteOutput withoutPictures() {
+ if (images.isEmpty) return this;
+ int moved(int offset) =>
+ offset - '\uFFFC'.allMatches(text.substring(0, offset)).length;
+ return PasteOutput(
+ text: text.replaceAll('\uFFFC', ''),
+ formats: [
+ for (final range in formats)
+ NoteFormatRange(
+ start: moved(range.start),
+ end: moved(range.end),
+ format: range.format,
+ ),
+ ],
+ );
+ }
+}
+
+/// A picture and the offset of its U+FFFC in [PasteOutput.text].
+class PastedImage {
+ const PastedImage(this.offset, this.image);
+
+ final int offset;
+ final PasteImage image;
+}
+
+/// Pairs each U+FFFC in [text] with the next of [images], in order.
+List placeImages(String text, List images) {
+ final placed = [];
+ var next = 0;
+ for (var i = 0; i < text.length && next < images.length; i++) {
+ if (text.codeUnitAt(i) == 0xFFFC) {
+ placed.add(PastedImage(i, images[next++]));
+ }
+ }
+ return placed;
+}
diff --git a/lib/paste/paste_writers.dart b/lib/paste/paste_writers.dart
new file mode 100644
index 0000000..f85393d
--- /dev/null
+++ b/lib/paste/paste_writers.dart
@@ -0,0 +1,827 @@
+import '../data/note_format.dart';
+import '../ui/editor/editor_formatting.dart'
+ show bulletPrefixForDepth, checkedPrefix, listIndentUnit, uncheckedPrefix;
+import 'paste_tree.dart';
+
+const _picture = '\uFFFC';
+
+/// Writes a paste as the markdown a note is written in.
+///
+/// What the source meant is said in the editor's own dialect — CommonMark
+/// with GitHub's tables, task lists and strikethrough, plus $…$ math and
+/// underline — and only what would otherwise be read as markdown it
+/// never was is escaped, so the text stays the text. Every picture is one
+/// U+FFFC on a line of its own, which is how a note holds pictures.
+PasteOutput writeMarkdownPaste(List blocks) {
+ final writer = _MarkdownWriter();
+ final text = _withoutEdgeBlankLines(writer.blocks(blocks)).join('\n');
+ return PasteOutput(text: text, images: placeImages(text, writer.images));
+}
+
+/// Writes a paste in the editor's own plain format, for a note written
+/// without markdown.
+///
+/// Bullets and boxes become the glyphs the editor draws, headings and
+/// bold and italic become its own styles beside the text, a table stays a
+/// table (tables draw as grids either way), and everything else is its
+/// words. No markdown syntax appears.
+PasteOutput writePlainPaste(List blocks) {
+ final writer = _PlainWriter();
+ writer.blocks(blocks, depth: 0);
+ return writer.finish();
+}
+
+List _withoutEdgeBlankLines(List lines) {
+ var start = 0;
+ var end = lines.length;
+ while (start < end && lines[start].trim().isEmpty) {
+ start++;
+ }
+ while (end > start && lines[end - 1].trim().isEmpty) {
+ end--;
+ }
+ return lines.sublist(start, end);
+}
+
+bool _isPictureLine(String line) =>
+ line.isNotEmpty && line.replaceAll(_picture, '').isEmpty;
+
+/// One line of a block's words, or a row of pictures.
+class _Line {
+ const _Line(this.inlines) : pictures = const [];
+ const _Line.pictures(this.pictures) : inlines = const [];
+
+ final List inlines;
+ final List pictures;
+
+ bool get isPictures => pictures.isNotEmpty;
+}
+
+/// Cuts a block's words into lines: at its line breaks, and around its
+/// pictures, which a note keeps on lines of their own — side by side when
+/// they came side by side.
+List<_Line> _linesOf(List inlines, {bool oneLine = false}) {
+ final lines = <_Line>[];
+ var words = [];
+ var pictures = [];
+ void endWords() {
+ if (_hasWords(words)) lines.add(_Line(words));
+ words = [];
+ }
+
+ void endPictures() {
+ if (pictures.isNotEmpty) lines.add(_Line.pictures(pictures));
+ pictures = [];
+ }
+
+ for (final inline in inlines) {
+ switch (inline) {
+ case PasteImageInline(:final image):
+ endWords();
+ pictures.add(image);
+ case PasteBreak():
+ if (pictures.isNotEmpty) {
+ endPictures();
+ } else if (oneLine) {
+ words.add(const PasteText(' '));
+ } else {
+ endWords();
+ }
+ case PasteText(:final text)
+ when text.trim().isEmpty && pictures.isNotEmpty:
+ break;
+ default:
+ endPictures();
+ words.add(inline);
+ }
+ }
+ endWords();
+ endPictures();
+ return lines;
+}
+
+bool _hasWords(List inlines) => inlines.any(
+ (inline) => switch (inline) {
+ PasteText(:final text) => text.trim().isNotEmpty,
+ PasteCodeSpan(:final code) => code.isNotEmpty,
+ PasteMath(:final tex) => tex.trim().isNotEmpty,
+ _ => false,
+ },
+);
+
+// -------------------------------------------------------------------------
+// Markdown
+
+class _MarkdownWriter {
+ final images = [];
+
+ List blocks(List blocks, {bool inItem = false}) {
+ final lines = [];
+ PasteBlock? previous;
+ for (final block in blocks) {
+ final written = _block(block);
+ if (written.isEmpty) continue;
+ // A list straight under its item's words is part of the item; a
+ // blank line there would make the whole list loose.
+ final tight = inItem && block is PasteList && previous is PasteParagraph;
+ if (lines.isNotEmpty && !tight) lines.add('');
+ lines.addAll(written);
+ previous = block;
+ }
+ return lines;
+ }
+
+ List _block(PasteBlock block) => switch (block) {
+ PasteParagraph(:final inlines) => _paragraph(inlines),
+ PasteHeading(:final level, :final inlines) => _heading(level, inlines),
+ PasteList() => _list(block),
+ PasteQuote(:final blocks) => _quote(blocks),
+ PasteCode(:final text, :final language) => _code(text, language),
+ PasteTable() => _table(block),
+ PasteRule() => const ['---'],
+ PasteMathBlock(:final tex) => _mathBlock(tex),
+ };
+
+ List _paragraph(List inlines) {
+ final result = [];
+ var afterWords = false;
+ for (final line in _linesOf(inlines)) {
+ if (line.isPictures) {
+ images.addAll(line.pictures);
+ result.add(_picture * line.pictures.length);
+ afterWords = false;
+ continue;
+ }
+ var text = _renderInlines(line.inlines);
+ if (!afterWords) text = text.trimLeft();
+ if (text.isEmpty) continue;
+ result.add(_escapeLineStart(text, afterWords: afterWords));
+ afterWords = true;
+ }
+ return result;
+ }
+
+ List _heading(int level, List inlines) {
+ final words = [];
+ final pictures = [];
+ for (final line in _linesOf(inlines, oneLine: true)) {
+ if (line.isPictures) {
+ images.addAll(line.pictures);
+ pictures.add(_picture * line.pictures.length);
+ } else {
+ words.add(_renderInlines(line.inlines).trim());
+ }
+ }
+ var text = words.where((word) => word.isNotEmpty).join(' ');
+ // A heading's closing run of #s is not part of its words.
+ text = text.replaceFirstMapped(
+ RegExp(r'(^|\s)(#+)$'),
+ (match) => '${match[1]}\\${match[2]}',
+ );
+ return [
+ if (text.isNotEmpty) '${'#' * level.clamp(1, 6)} $text',
+ ...pictures,
+ ];
+ }
+
+ List _list(PasteList list) {
+ final lines = [];
+ for (var i = 0; i < list.items.length; i++) {
+ final item = list.items[i];
+ final marker = list.ordered ? '${list.start + i}. ' : '- ';
+ final box = switch (item.checked) {
+ null => '',
+ true => '[x] ',
+ false => '[ ] ',
+ };
+ final indent = ' ' * marker.length;
+ final body = blocks(item.blocks, inItem: true);
+ if (body.isEmpty) {
+ if (box.isNotEmpty) lines.add('$marker$box');
+ continue;
+ }
+ for (var j = 0; j < body.length; j++) {
+ final line = body[j];
+ if (j == 0) {
+ if (_isPictureLine(line)) {
+ lines
+ ..add('$marker$box'.trimRight())
+ ..add(line);
+ } else {
+ lines.add('$marker$box$line');
+ }
+ } else if (line.isEmpty || _isPictureLine(line)) {
+ lines.add(line);
+ } else {
+ lines.add('$indent$line');
+ }
+ }
+ }
+ return lines;
+ }
+
+ List _quote(List blocks) => [
+ for (final line in this.blocks(blocks))
+ if (line.isEmpty) '>' else if (_isPictureLine(line)) line else '> $line',
+ ];
+
+ List _code(String text, String? language) {
+ final code = text.replaceAll(_picture, '');
+ final fence = '`' * _fenceLength(code, '`');
+ final info = language == null
+ ? ''
+ : language.trim().split(RegExp(r'\s+')).first.replaceAll('`', '');
+ return ['$fence$info', ...code.split('\n'), fence];
+ }
+
+ List _table(PasteTable table) {
+ final cellPictures = [];
+ String cell(List inlines) {
+ final parts = [];
+ for (final line in _linesOf(inlines)) {
+ if (line.isPictures) {
+ cellPictures.addAll(line.pictures);
+ } else {
+ parts.add(_renderInlines(line.inlines, table: true).trim());
+ }
+ }
+ return parts.where((part) => part.isNotEmpty).join(' ');
+ }
+
+ String row(List> cells) =>
+ '| ${cells.map(cell).join(' | ')} |';
+
+ final lines = [
+ row(table.rows.first),
+ '| ${table.aligns.map(_delimiter).join(' | ')} |',
+ for (final cells in table.rows.skip(1)) row(cells),
+ ];
+ if (cellPictures.isNotEmpty) {
+ images.addAll(cellPictures);
+ lines
+ ..add('')
+ ..add(_picture * cellPictures.length);
+ }
+ return lines;
+ }
+
+ static String _delimiter(PasteAlign align) => switch (align) {
+ PasteAlign.none => '---',
+ PasteAlign.center => ':-:',
+ PasteAlign.right => '--:',
+ };
+
+ static List _mathBlock(String tex) {
+ final source = tex.trim();
+ if (source.isEmpty) return const [];
+ return source.contains('\n')
+ ? [r'$$', ...source.split('\n'), r'$$']
+ : ['\$\$$source\$\$'];
+ }
+}
+
+int _fenceLength(String code, String mark) {
+ final longest = _longestRun(code, mark);
+ return longest < 3 ? 3 : longest + 1;
+}
+
+int _longestRun(String text, String mark) {
+ var longest = 0;
+ var run = 0;
+ for (var i = 0; i < text.length; i++) {
+ run = text[i] == mark ? run + 1 : 0;
+ if (run > longest) longest = run;
+ }
+ return longest;
+}
+
+const _styleOrder = [
+ PasteStyle.bold,
+ PasteStyle.italic,
+ PasteStyle.strike,
+ PasteStyle.underline,
+ PasteStyle.superscript,
+ PasteStyle.subscript,
+];
+
+String _opener(PasteStyle style) => switch (style) {
+ PasteStyle.bold => '**',
+ PasteStyle.italic => '*',
+ PasteStyle.strike => '~~',
+ PasteStyle.underline => '',
+ PasteStyle.superscript => '',
+ PasteStyle.subscript => '',
+};
+
+String _closer(PasteStyle style) => switch (style) {
+ PasteStyle.bold => '**',
+ PasteStyle.italic => '*',
+ PasteStyle.strike => '~~',
+ PasteStyle.underline => ' ',
+ PasteStyle.superscript => '',
+ PasteStyle.subscript => '',
+};
+
+/// One line's words as markdown.
+///
+/// Emphasis is opened and closed where the styles change rather than
+/// around every run, so bold that turns italic halfway reads as one bold
+/// span; and white space is kept outside the markers, where CommonMark
+/// needs it to be for them to count.
+String _renderInlines(List inlines, {bool table = false}) {
+ final out = StringBuffer();
+ final open = [];
+ String? link;
+ var pending = '';
+
+ void closeStyles(int depth) {
+ while (open.length > depth) {
+ out.write(_closer(open.removeLast()));
+ }
+ }
+
+ void closeLink() {
+ closeStyles(0);
+ if (link != null) {
+ out.write('](${_destination(link!)})');
+ link = null;
+ }
+ }
+
+ for (final inline in inlines) {
+ final String lead;
+ final String core;
+ final String trail;
+ final Set styles;
+ final String? href;
+ switch (inline) {
+ case PasteText(:final text, styles: final textStyles, href: final h):
+ final (shown, kept) = _scripted(text, textStyles);
+ final start = shown.length - shown.trimLeft().length;
+ final end = shown.trimRight().length;
+ lead = shown.substring(0, start);
+ trail = end < start ? '' : shown.substring(end);
+ final words = end < start ? '' : shown.substring(start, end);
+ if (h != null && words == h && _isWebAddress(h)) {
+ closeLink();
+ out
+ ..write(pending)
+ ..write(lead)
+ ..write('<$h>');
+ pending = trail;
+ continue;
+ }
+ core = _escapeText(words, table: table, inLink: h != null);
+ styles = kept;
+ href = h;
+ case PasteCodeSpan(:final code, styles: final codeStyles, href: final h):
+ lead = '';
+ trail = '';
+ core = _codeSpan(code, table: table);
+ styles = codeStyles;
+ href = h;
+ case PasteMath(:final tex):
+ lead = '';
+ trail = '';
+ final source = tex.trim().replaceAll('\n', ' ');
+ core = source.isEmpty ? '' : '\$$source\$';
+ styles = open.toSet();
+ href = link;
+ case PasteBreak() || PasteImageInline():
+ continue;
+ }
+ if (core.isEmpty) {
+ pending += lead + trail;
+ continue;
+ }
+ if (href != link) {
+ closeLink();
+ } else {
+ final firstGone = open.indexWhere((style) => !styles.contains(style));
+ if (firstGone >= 0) closeStyles(firstGone);
+ }
+ out
+ ..write(pending)
+ ..write(lead);
+ pending = '';
+ if (href != null && link == null) {
+ out.write('[');
+ link = href;
+ }
+ for (final style in _styleOrder) {
+ if (styles.contains(style) && !open.contains(style)) {
+ out.write(_opener(style));
+ open.add(style);
+ }
+ }
+ out.write(core);
+ pending = trail;
+ }
+ closeLink();
+ return out.toString();
+}
+
+bool _isWebAddress(String href) =>
+ href.startsWith('https://') || href.startsWith('http://');
+
+String _destination(String href) {
+ final spaced = href.replaceAll(' ', '%20');
+ if (!RegExp(r'[()<>]').hasMatch(spaced)) return spaced;
+ return '<${spaced.replaceAll('<', '%3C').replaceAll('>', '%3E')}>';
+}
+
+String _codeSpan(String code, {required bool table}) {
+ var text = code.replaceAll('\n', ' ').replaceAll(_picture, '');
+ if (text.isEmpty) return '';
+ if (table) text = text.replaceAll('|', r'\|');
+ final fence = '`' * (_longestRun(text, '`') + 1);
+ final pad =
+ text.startsWith('`') ||
+ text.endsWith('`') ||
+ (text.startsWith(' ') && text.endsWith(' ') && text.trim().isNotEmpty)
+ ? ' '
+ : '';
+ return '$fence$pad$text$pad$fence';
+}
+
+/// Words with superscript and subscript turned into Unicode where every
+/// character has a form, which reads the same everywhere (H₂O, x²); the
+/// styles left are the ones markup still has to say.
+(String, Set) _scripted(String text, Set styles) {
+ for (final (style, table) in [
+ (PasteStyle.superscript, _superscripts),
+ (PasteStyle.subscript, _subscripts),
+ ]) {
+ if (!styles.contains(style)) continue;
+ final words = text.trim();
+ if (words.isEmpty || !words.split('').every(table.containsKey)) continue;
+ final converted = text.split('').map((c) => table[c] ?? c).join();
+ return (converted, {...styles}..remove(style));
+ }
+ return (text, styles);
+}
+
+const _superscripts = {
+ '0': '⁰', '1': '¹', '2': '²', '3': '³', '4': '⁴', '5': '⁵', '6': '⁶', //
+ '7': '⁷', '8': '⁸', '9': '⁹', '+': '⁺', '-': '⁻', '=': '⁼', '(': '⁽', //
+ ')': '⁾', 'n': 'ⁿ', 'i': 'ⁱ',
+};
+
+const _subscripts = {
+ '0': '₀', '1': '₁', '2': '₂', '3': '₃', '4': '₄', '5': '₅', '6': '₆', //
+ '7': '₇', '8': '₈', '9': '₉', '+': '₊', '-': '₋', '=': '₌', '(': '₍', //
+ ')': '₎', 'a': 'ₐ', 'e': 'ₑ', 'o': 'ₒ', 'x': 'ₓ', 'h': 'ₕ', 'k': 'ₖ', //
+ 'l': 'ₗ', 'm': 'ₘ', 'n': 'ₙ', 'p': 'ₚ', 's': 'ₛ', 't': 'ₜ',
+};
+
+final _mathPair = RegExp(r'\$(?![\s$])[^$]*?(?') ||
+ _bulletStart.hasMatch(line) ||
+ _thematicBreak.hasMatch(line) ||
+ (afterWords && _setextUnderline.hasMatch(line))) {
+ return '\\$line';
+ }
+ final ordered = _orderedStart.firstMatch(line);
+ if (ordered != null) {
+ return '${ordered[1]}\\${line.substring(ordered[1]!.length)}';
+ }
+ return line;
+}
+
+bool _isSpace(String c) => c.trim().isEmpty;
+
+bool _isAsciiPunctuation(String c) =>
+ RegExp(r'''[!-/:-@\[-`{-~]''').hasMatch(c);
+
+final _wordCharacter = RegExp(r'[\p{L}\p{N}]', unicode: true);
+
+bool _isWordCharacter(String c) => _wordCharacter.hasMatch(c);
+
+bool _endsOperand(String c) =>
+ _isWordCharacter(c) || c == ')' || c == ']' || c == '%';
+
+bool _startsOperand(String c) => _isWordCharacter(c) || c == '(' || c == '[';
+
+// -------------------------------------------------------------------------
+// Plain
+
+class _PlainWriter {
+ final _out = StringBuffer();
+ final _formats = [];
+ final _images = [];
+ var _blankPending = false;
+
+ PasteOutput finish() {
+ final text = _out.toString();
+ return PasteOutput(
+ text: text,
+ formats: normalizeNoteFormats(_formats, text.length),
+ images: placeImages(text, _images),
+ );
+ }
+
+ void blocks(List blocks, {required int depth, String? indent}) {
+ for (final block in blocks) {
+ _block(block, depth: depth, indent: indent ?? '');
+ }
+ }
+
+ /// Starts a new line, after a blank one when the previous block asked for
+ /// it.
+ void _newLine() {
+ if (_out.isEmpty) {
+ _blankPending = false;
+ return;
+ }
+ _out.write(_blankPending ? '\n\n' : '\n');
+ _blankPending = false;
+ }
+
+ void _block(PasteBlock block, {required int depth, required String indent}) {
+ switch (block) {
+ case PasteParagraph(:final inlines):
+ _lines(inlines, indent: indent);
+ _blankPending = depth == 0;
+ case PasteHeading(:final level, :final inlines):
+ _lines(
+ inlines,
+ indent: indent,
+ oneLine: true,
+ paragraph: level <= 1 ? NoteFormat.heading : NoteFormat.subtitle,
+ );
+ _blankPending = depth == 0;
+ case PasteList(:final ordered, :final items, :final start):
+ for (var i = 0; i < items.length; i++) {
+ final item = items[i];
+ final itemIndent = listIndentUnit * depth;
+ final marker = switch (item.checked) {
+ true => checkedPrefix,
+ false => uncheckedPrefix,
+ null => ordered ? '${start + i}. ' : bulletPrefixForDepth(depth),
+ };
+ var first = true;
+ for (final child in item.blocks) {
+ if (child is PasteList) {
+ _block(child, depth: depth + 1, indent: '');
+ continue;
+ }
+ if (first && (child is PasteParagraph || child is PasteHeading)) {
+ final inlines = switch (child) {
+ PasteParagraph(:final inlines) => inlines,
+ PasteHeading(:final inlines) => inlines,
+ _ => const [],
+ };
+ _lines(inlines, indent: itemIndent, marker: marker);
+ } else {
+ if (first) {
+ _newLine();
+ _out.write('$itemIndent${marker.trimRight()}');
+ }
+ _block(
+ child,
+ depth: depth + 1,
+ indent: itemIndent + listIndentUnit,
+ );
+ }
+ first = false;
+ }
+ if (first) {
+ _newLine();
+ _out.write('$itemIndent${marker.trimRight()}');
+ }
+ }
+ _blankPending = depth == 0;
+ case PasteQuote(:final blocks):
+ this.blocks(blocks, depth: depth, indent: indent);
+ _blankPending = depth == 0;
+ case PasteCode(:final text):
+ for (final line in text.replaceAll(_picture, '').split('\n')) {
+ _newLine();
+ _out.write('$indent$line');
+ }
+ _blankPending = depth == 0;
+ case PasteTable():
+ _table(block, indent: indent);
+ _blankPending = depth == 0;
+ case PasteRule():
+ _blankPending = true;
+ case PasteMathBlock(:final tex):
+ for (final line in tex.trim().split('\n')) {
+ _newLine();
+ _out.write('$indent$line');
+ }
+ _blankPending = depth == 0;
+ }
+ }
+
+ /// Writes a block's words, line by line, with [marker] in front of the
+ /// first and [paragraph] over each.
+ void _lines(
+ List inlines, {
+ required String indent,
+ String marker = '',
+ bool oneLine = false,
+ NoteFormat? paragraph,
+ }) {
+ var first = true;
+ for (final line in _linesOf(inlines, oneLine: oneLine)) {
+ _newLine();
+ if (line.isPictures) {
+ if (first && marker.isNotEmpty) {
+ _out.write('$indent${marker.trimRight()}');
+ _newLine();
+ }
+ _images.addAll(line.pictures);
+ _out.write(_picture * line.pictures.length);
+ first = false;
+ continue;
+ }
+ _out.write(indent);
+ if (first) _out.write(marker);
+ final start = _out.length;
+ _words(line.inlines, trimStart: true);
+ final end = _out.length;
+ if (paragraph != null && end > start) {
+ _formats.add(
+ NoteFormatRange(start: start, end: end, format: paragraph),
+ );
+ }
+ first = false;
+ }
+ }
+
+ void _words(List inlines, {bool trimStart = false}) {
+ var atStart = trimStart;
+ void write(String text, Set styles) {
+ var words = text.replaceAll(_picture, '');
+ if (atStart) words = words.trimLeft();
+ if (words.isEmpty) return;
+ atStart = false;
+ final start = _out.length;
+ _out.write(words);
+ for (final style in styles) {
+ final format = switch (style) {
+ PasteStyle.bold => NoteFormat.bold,
+ PasteStyle.italic => NoteFormat.italic,
+ _ => null,
+ };
+ if (format != null) {
+ _formats.add(
+ NoteFormatRange(start: start, end: _out.length, format: format),
+ );
+ }
+ }
+ }
+
+ String? openLink;
+ final linkWords = StringBuffer();
+ void endLink() {
+ final href = openLink;
+ if (href == null) return;
+ final words = linkWords.toString().trim();
+ final shown = href.startsWith('mailto:') ? href.substring(7) : href;
+ if (words != href && words != shown) write(' ($shown)', const {});
+ openLink = null;
+ linkWords.clear();
+ }
+
+ for (final inline in inlines) {
+ final href = switch (inline) {
+ PasteText(:final href) || PasteCodeSpan(:final href) => href,
+ _ => null,
+ };
+ if (href != openLink) endLink();
+ openLink = href;
+ switch (inline) {
+ case PasteText(:final text, :final styles):
+ final (shown, kept) = _scripted(text, styles);
+ if (href != null) linkWords.write(shown);
+ write(shown, kept);
+ case PasteCodeSpan(:final code, :final styles):
+ if (href != null) linkWords.write(code);
+ write(code, styles);
+ case PasteMath(:final tex):
+ write(tex.trim(), const {});
+ case PasteBreak() || PasteImageInline():
+ break;
+ }
+ }
+ endLink();
+ final text = _out.toString();
+ final trimmed = text.trimRight().length;
+ if (trimmed < text.length) {
+ final kept = text.substring(0, trimmed);
+ _out
+ ..clear()
+ ..write(kept);
+ }
+ }
+
+ void _table(PasteTable table, {required String indent}) {
+ final pictures = [];
+ String cell(List inlines) {
+ final parts = [];
+ for (final line in _linesOf(inlines)) {
+ if (line.isPictures) {
+ pictures.addAll(line.pictures);
+ continue;
+ }
+ final words = StringBuffer();
+ for (final inline in line.inlines) {
+ switch (inline) {
+ case PasteText(:final text, :final styles):
+ words.write(_scripted(text, styles).$1);
+ case PasteCodeSpan(:final code):
+ words.write(code);
+ case PasteMath(:final tex):
+ words.write(tex.trim());
+ case PasteBreak() || PasteImageInline():
+ break;
+ }
+ }
+ parts.add(words.toString().trim().replaceAll('|', r'\|'));
+ }
+ return parts.where((part) => part.isNotEmpty).join(' ');
+ }
+
+ final rows = [
+ '| ${table.rows.first.map(cell).join(' | ')} |',
+ '| ${table.aligns.map(_MarkdownWriter._delimiter).join(' | ')} |',
+ for (final cells in table.rows.skip(1))
+ '| ${cells.map(cell).join(' | ')} |',
+ ];
+ for (final row in rows) {
+ _newLine();
+ _out.write('$indent$row');
+ }
+ if (pictures.isNotEmpty) {
+ _blankPending = true;
+ _newLine();
+ _images.addAll(pictures);
+ _out.write(_picture * pictures.length);
+ }
+ }
+}
diff --git a/lib/sync/image_sync.dart b/lib/sync/image_sync.dart
index 465e44e..21eafb6 100644
--- a/lib/sync/image_sync.dart
+++ b/lib/sync/image_sync.dart
@@ -109,7 +109,11 @@ class ImageSync {
) async {
// The preview is visible, but compression has not produced the durable
// object yet. Uploading this temporary source would race the replacement.
- if (ref case NoteImageRef(isPreparing: true)) return null;
+ // A pasted picture still at its address has no bytes here to upload.
+ if (ref
+ case NoteImageRef(isPreparing: true) || NoteImageRef(src: String())) {
+ return null;
+ }
var current = ref;
try {
diff --git a/lib/ui/editor/code_highlighting.dart b/lib/ui/editor/code_highlighting.dart
new file mode 100644
index 0000000..b2fc3d1
--- /dev/null
+++ b/lib/ui/editor/code_highlighting.dart
@@ -0,0 +1,237 @@
+import 'package:flutter/foundation.dart';
+import 'package:re_highlight/languages/bash.dart';
+import 'package:re_highlight/languages/c.dart';
+import 'package:re_highlight/languages/cpp.dart';
+import 'package:re_highlight/languages/csharp.dart';
+import 'package:re_highlight/languages/css.dart';
+import 'package:re_highlight/languages/dart.dart';
+import 'package:re_highlight/languages/diff.dart';
+import 'package:re_highlight/languages/dockerfile.dart';
+import 'package:re_highlight/languages/go.dart';
+import 'package:re_highlight/languages/graphql.dart';
+import 'package:re_highlight/languages/ini.dart';
+import 'package:re_highlight/languages/java.dart';
+import 'package:re_highlight/languages/javascript.dart';
+import 'package:re_highlight/languages/json.dart';
+import 'package:re_highlight/languages/kotlin.dart';
+import 'package:re_highlight/languages/latex.dart';
+import 'package:re_highlight/languages/lua.dart';
+import 'package:re_highlight/languages/makefile.dart';
+import 'package:re_highlight/languages/markdown.dart';
+import 'package:re_highlight/languages/objectivec.dart';
+import 'package:re_highlight/languages/perl.dart';
+import 'package:re_highlight/languages/php.dart';
+import 'package:re_highlight/languages/plaintext.dart';
+import 'package:re_highlight/languages/powershell.dart';
+import 'package:re_highlight/languages/python.dart';
+import 'package:re_highlight/languages/r.dart';
+import 'package:re_highlight/languages/ruby.dart';
+import 'package:re_highlight/languages/rust.dart';
+import 'package:re_highlight/languages/scala.dart';
+import 'package:re_highlight/languages/scss.dart';
+import 'package:re_highlight/languages/sql.dart';
+import 'package:re_highlight/languages/swift.dart';
+import 'package:re_highlight/languages/typescript.dart';
+import 'package:re_highlight/languages/xml.dart';
+import 'package:re_highlight/languages/yaml.dart';
+import 'package:re_highlight/re_highlight.dart';
+
+import 'markdown_syntax.dart';
+
+/// What a stretch of code is, as far as colouring it goes.
+enum CodeTokenKind {
+ keyword,
+ string,
+ number,
+ comment,
+ title,
+ type,
+ attribute,
+ variable,
+ punctuation,
+}
+
+/// [start, end) of the note, coloured as [kind].
+typedef CodeToken = ({int start, int end, CodeTokenKind kind});
+
+/// Fenced code coloured the way the language its info string names reads,
+/// by highlight.js's grammars.
+///
+/// The languages a note is likely to hold, under the names and aliases
+/// highlight.js gives them. A block in any other language, or in none, stays
+/// plain code. Each block is coloured once for as long as its language and
+/// its code stay the same, so typing in one block leaves the rest alone.
+class CodeHighlighter {
+ static final Highlight _grammars = Highlight()
+ ..registerLanguage('bash', langBash)
+ ..registerLanguage('c', langC)
+ ..registerLanguage('cpp', langCpp)
+ ..registerLanguage('csharp', langCsharp)
+ ..registerLanguage('css', langCss)
+ ..registerLanguage('dart', langDart)
+ ..registerLanguage('diff', langDiff)
+ ..registerLanguage('dockerfile', langDockerfile)
+ ..registerLanguage('go', langGo)
+ ..registerLanguage('graphql', langGraphql)
+ ..registerLanguage('ini', langIni)
+ ..registerLanguage('java', langJava)
+ ..registerLanguage('javascript', langJavascript)
+ ..registerLanguage('json', langJson)
+ ..registerLanguage('kotlin', langKotlin)
+ ..registerLanguage('latex', langLatex)
+ ..registerLanguage('lua', langLua)
+ ..registerLanguage('makefile', langMakefile)
+ ..registerLanguage('markdown', langMarkdown)
+ ..registerLanguage('objectivec', langObjectivec)
+ ..registerLanguage('perl', langPerl)
+ ..registerLanguage('php', langPhp)
+ ..registerLanguage('plaintext', langPlaintext)
+ ..registerLanguage('powershell', langPowershell)
+ ..registerLanguage('python', langPython)
+ ..registerLanguage('r', langR)
+ ..registerLanguage('ruby', langRuby)
+ ..registerLanguage('rust', langRust)
+ ..registerLanguage('scala', langScala)
+ ..registerLanguage('scss', langScss)
+ ..registerLanguage('sql', langSql)
+ ..registerLanguage('swift', langSwift)
+ ..registerLanguage('typescript', langTypescript)
+ ..registerLanguage('xml', langXml)
+ ..registerLanguage('yaml', langYaml)
+ ..registerAliases(['zsh', 'shell'], 'bash');
+
+ Map<(String, String), List> _coloured = const {};
+
+ /// Every token of every block in [blocks], in order, in [text]'s offsets.
+ List tokensIn(String text, List blocks) {
+ final coloured = <(String, String), List>{};
+ final tokens = [];
+ for (final block in blocks) {
+ final language = block.language.toLowerCase();
+ if (_grammars.getLanguage(language) == null) continue;
+ final code = block.code
+ .map((range) => text.substring(range.start, range.end))
+ .join();
+ final key = (language, code);
+ final relative = coloured[key] ??=
+ _coloured[key] ?? _highlight(language, code);
+ // The code of a quoted or indented block is in pieces, and a token can
+ // run from one into the next.
+ var piece = 0;
+ var pieceStart = 0;
+ for (final token in relative) {
+ var from = token.start;
+ while (from < token.end && piece < block.code.length) {
+ final range = block.code[piece];
+ final pieceEnd = pieceStart + range.end - range.start;
+ if (from >= pieceEnd) {
+ piece++;
+ pieceStart = pieceEnd;
+ continue;
+ }
+ final to = token.end < pieceEnd ? token.end : pieceEnd;
+ tokens.add((
+ start: range.start + from - pieceStart,
+ end: range.start + to - pieceStart,
+ kind: token.kind,
+ ));
+ from = to;
+ }
+ }
+ }
+ _coloured = coloured;
+ return tokens;
+ }
+
+ static List _highlight(String language, String code) {
+ final tokens = _Tokens();
+ try {
+ _grammars.highlight(code: code, language: language).render(tokens);
+ } catch (error, stack) {
+ // A grammar that trips over some code costs that block its colours,
+ // never the note its text.
+ FlutterError.reportError(
+ FlutterErrorDetails(
+ exception: error,
+ stack: stack,
+ library: 'Kapy Notes editor',
+ context: ErrorDescription('while colouring $language code'),
+ ),
+ );
+ return const [];
+ }
+ return tokens.length == code.length ? tokens.tokens : const [];
+ }
+}
+
+/// Reads highlight.js's tree of scopes into flat tokens, each coloured by the
+/// innermost scope around it that has a colour.
+class _Tokens implements HighlightRenderer {
+ final tokens = [];
+ final _open = [];
+ var length = 0;
+
+ @override
+ void addText(String text) {
+ final kind = _open.lastWhere((kind) => kind != null, orElse: () => null);
+ if (kind != null && text.isNotEmpty) {
+ final last = tokens.lastOrNull;
+ if (last != null && last.end == length && last.kind == kind) {
+ tokens.last = (
+ start: last.start,
+ end: length + text.length,
+ kind: kind,
+ );
+ } else {
+ tokens.add((start: length, end: length + text.length, kind: kind));
+ }
+ }
+ length += text.length;
+ }
+
+ @override
+ void openNode(DataNode node) => _open.add(_kindOf(node.scope));
+
+ @override
+ void closeNode(DataNode node) => _open.removeLast();
+
+ static CodeTokenKind? _kindOf(String? scope) {
+ if (scope == null) return null;
+ if (scope.startsWith('title.class')) return CodeTokenKind.type;
+ return switch (scope.split('.').first) {
+ 'keyword' ||
+ 'built_in' ||
+ 'literal' ||
+ 'name' ||
+ 'selector-tag' ||
+ 'section' ||
+ 'doctag' ||
+ 'deletion' => CodeTokenKind.keyword,
+ 'string' ||
+ 'regexp' ||
+ 'symbol' ||
+ 'char' ||
+ 'addition' ||
+ 'code' ||
+ 'link' => CodeTokenKind.string,
+ 'number' => CodeTokenKind.number,
+ 'comment' || 'quote' => CodeTokenKind.comment,
+ 'title' || 'function' => CodeTokenKind.title,
+ 'type' || 'class' => CodeTokenKind.type,
+ 'attr' ||
+ 'attribute' ||
+ 'property' ||
+ 'selector-attr' ||
+ 'selector-class' ||
+ 'selector-id' ||
+ 'selector-pseudo' => CodeTokenKind.attribute,
+ 'variable' || 'template-variable' => CodeTokenKind.variable,
+ 'operator' ||
+ 'punctuation' ||
+ 'meta' ||
+ 'tag' ||
+ 'bullet' => CodeTokenKind.punctuation,
+ _ => null,
+ };
+ }
+}
diff --git a/lib/ui/editor/highlighting_controller.dart b/lib/ui/editor/highlighting_controller.dart
index e9e074a..2f73af8 100644
--- a/lib/ui/editor/highlighting_controller.dart
+++ b/lib/ui/editor/highlighting_controller.dart
@@ -9,8 +9,10 @@ import '../../core/editor_font.dart';
import '../../core/note_link.dart';
import '../../core/theme.dart';
import '../../data/note_format.dart';
+import 'code_highlighting.dart';
import 'editor_formatting.dart';
import 'markdown_syntax.dart';
+import 'note_math.dart';
/// One image, already sized and built, ready to be dropped into the span tree
/// at its placeholder.
@@ -47,6 +49,10 @@ class HighlightingController extends TextEditingController {
bool _markdown;
final MarkdownAnalyzer _markdownAnalyzer = MarkdownAnalyzer();
Map _imageSpans = const {};
+ final NoteMath _math = NoteMath();
+ final CodeHighlighter _codeHighlighter = CodeHighlighter();
+ double _mathWidth = 0;
+ List<(int, PlaceholderDimensions)> _formulaPlaceholders = const [];
/// Native spelling results currently painted by this rich text controller.
///
@@ -139,6 +145,7 @@ class HighlightingController extends TextEditingController {
selection,
revealBlocks: markdownReveal.blocks,
revealEdges: markdownReveal.edges,
+ drawable: (math) => _math.draws(math.tex),
);
_concealment = concealment;
_concealedFor = analysis;
@@ -163,17 +170,30 @@ class HighlightingController extends TextEditingController {
_imageSpans = value;
}
+ /// Deliberately silent, for the same reason as [setImageSpansDuringLayout].
+ ///
+ /// The width of the writing column a formula is drawn in: display math is
+ /// centred across it, and any formula wider than it is drawn smaller.
+ void setMathWidthDuringLayout(double value) {
+ _mathWidth = value;
+ }
+
/// The placeholder boxes [TextPainter] must be told about before it can lay
- /// out a span containing [WidgetSpan]s, in the order they appear.
+ /// out a span containing [WidgetSpan]s, in the order they appear: pictures,
+ /// table rows, and the formulas the last [buildTextSpan] drew.
List placeholderDimensions() {
- final offsets = _imageSpans.keys.toList()..sort();
- return [
- for (final offset in offsets)
- PlaceholderDimensions(
- size: Size(_imageSpans[offset]!.width, _imageSpans[offset]!.height),
- alignment: PlaceholderAlignment.top,
+ final placeholders = [
+ for (final MapEntry(key: offset, value: image) in _imageSpans.entries)
+ (
+ offset,
+ PlaceholderDimensions(
+ size: Size(image.width, image.height),
+ alignment: PlaceholderAlignment.top,
+ ),
),
- ];
+ ..._formulaPlaceholders,
+ ]..sort((a, b) => a.$1.compareTo(b.$1));
+ return [for (final (_, dimensions) in placeholders) dimensions];
}
void _invalidate() {
@@ -235,11 +255,11 @@ class HighlightingController extends TextEditingController {
final concealment = markdownConcealment();
final hidden = concealment.hidden;
final transparent = concealment.transparent;
- final stretches = _listStretches(
- analysis,
- base,
- MediaQuery.maybeTextScalerOf(context) ?? TextScaler.noScaling,
- );
+ final scaler =
+ MediaQuery.maybeTextScalerOf(context) ?? TextScaler.noScaling;
+ final stretches = _listStretches(analysis, base, scaler);
+ final formulas = concealment.formulas;
+ final code = _codeTokens(analysis);
final composing = withComposing && value.isComposingRangeValid
? value.composing
: null;
@@ -285,6 +305,13 @@ class HighlightingController extends TextEditingController {
boundaries.add(stretch.start);
boundaries.add(stretch.end);
}
+ for (final formula in formulas) {
+ boundaries.add(formula.start + 1);
+ }
+ for (final token in code) {
+ boundaries.add(token.start);
+ boundaries.add(token.end);
+ }
if (composing != null) {
boundaries.add(composing.start.clamp(0, source.length));
boundaries.add(composing.end.clamp(0, source.length));
@@ -306,6 +333,10 @@ class HighlightingController extends TextEditingController {
var hiddenIndex = 0;
var transparentIndex = 0;
var stretchIndex = 0;
+ var formulaIndex = 0;
+ final formulaCopies = {};
+ var codeIndex = 0;
+ final placeholders = <(int, PlaceholderDimensions)>[];
for (var i = 0; i < cuts.length - 1; i++) {
final start = cuts[i];
@@ -356,6 +387,41 @@ class HighlightingController extends TextEditingController {
continue;
}
+ // A formula drawn as itself stands in the place of its first character,
+ // the way a picture does, with the rest of its TeX hidden after it.
+ while (formulaIndex < formulas.length &&
+ formulas[formulaIndex].end <= start) {
+ formulaIndex++;
+ }
+ final formula =
+ formulaIndex < formulas.length &&
+ formulas[formulaIndex].start <= start
+ ? formulas[formulaIndex]
+ : null;
+ if (formula != null && start == formula.start) {
+ final (span, placeholder) = _formulaSpan(
+ formula,
+ copy: formulaCopies.update(
+ formula.tex,
+ (copy) => copy + 1,
+ ifAbsent: () => 0,
+ ),
+ words: _markdownBlockStyle(
+ base,
+ base,
+ (style) => style != MarkdownStyle.math && marked(style),
+ ),
+ base: base,
+ scaler: scaler,
+ indent: formula.display
+ ? _indent(formula.start, hidden, stretches, base, scaler)
+ : 0,
+ );
+ children.add(span);
+ placeholders.add((formula.start, placeholder));
+ continue;
+ }
+
while (spanIndex < spans.length && spans[spanIndex].end <= start) {
spanIndex++;
}
@@ -399,6 +465,14 @@ class HighlightingController extends TextEditingController {
if (markdown != 0) {
segmentStyle = _markdownBlockStyle(segmentStyle, base, marked);
}
+ while (codeIndex < code.length && code[codeIndex].end <= start) {
+ codeIndex++;
+ }
+ if (codeIndex < code.length && code[codeIndex].start <= start) {
+ segmentStyle = segmentStyle.merge(
+ _codeTokenStyle(code[codeIndex].kind),
+ );
+ }
final matchingFormats = _formats.where(
(format) => format.start <= start && format.end >= end,
);
@@ -427,10 +501,18 @@ class HighlightingController extends TextEditingController {
};
}
if (markdown != 0) {
+ final script = openMarkdown
+ .where(
+ (span) =>
+ span.style == MarkdownStyle.superscript ||
+ span.style == MarkdownStyle.subscript,
+ )
+ .lastOrNull;
segmentStyle = _markdownInlineStyle(
segmentStyle,
marked,
segmentLinkColor,
+ script == null ? '' : source.substring(script.start, script.end),
);
}
final done = checked || marked(MarkdownStyle.doneTask);
@@ -497,12 +579,169 @@ class HighlightingController extends TextEditingController {
}
}
+ // A line break inside a formula drawn as itself would still break the
+ // line, hidden or not, and leave an empty line under the formula for
+ // each one; the span the field draws is not the note's text, so here it
+ // is a space.
+ final words = source.substring(start, end);
children.add(
- TextSpan(text: source.substring(start, end), style: segmentStyle),
+ TextSpan(
+ text: formula == null ? words : words.replaceAll(_lineEnd, ' '),
+ style: segmentStyle,
+ ),
);
}
- return TextSpan(style: base, children: children);
+ _formulaPlaceholders = placeholders;
+ return _NoteSpan(style: base, children: children);
+ }
+
+ static final _lineEnd = RegExp('[\r\n]');
+
+ /// How much bigger a formula is set than the words around it. KaTeX's own
+ /// face runs small beside a sans serif at the same size.
+ static const double _mathScale = 1.1;
+
+ /// [formula] as a placeholder, and the box [placeholderDimensions] reports
+ /// for it.
+ ///
+ /// Among words a formula sits on their baseline. Set apart, it is centred
+ /// across what is left of the writing column after [indent], in the middle
+ /// of a line of its own. Both are drawn in the size and colour of the words
+ /// they stand among, in [words].
+ ///
+ /// A placeholder's widget is scaled with the text by the field, so it is
+ /// built at the size the text is before scaling, and the box is reported
+ /// at the size it comes out at. [copy] is which of the formulas drawn with
+ /// the same TeX this one is: see [NoteMath.formula].
+ (WidgetSpan, PlaceholderDimensions) _formulaSpan(
+ MarkdownMath formula, {
+ required int copy,
+ required TextStyle words,
+ required TextStyle base,
+ required TextScaler scaler,
+ required double indent,
+ }) {
+ final size = base.fontSize ?? kDefaultFontSize;
+ final scale = scaler.scale(size) / size;
+ final room = math.max(0.0, _mathWidth - indent) / scale;
+ final drawn = _math.formula(
+ formula.tex,
+ display: formula.display,
+ fontSize: (words.fontSize ?? size) * _mathScale,
+ color: words.color ?? _palette.textPrimary,
+ maxWidth: room,
+ copy: copy,
+ );
+ if (!formula.display) {
+ return (
+ WidgetSpan(
+ alignment: PlaceholderAlignment.baseline,
+ baseline: TextBaseline.alphabetic,
+ child: drawn.child,
+ ),
+ PlaceholderDimensions(
+ size: Size(drawn.width * scale, drawn.height * scale),
+ alignment: PlaceholderAlignment.baseline,
+ baseline: TextBaseline.alphabetic,
+ baselineOffset: drawn.baseline * scale,
+ ),
+ );
+ }
+ final width = math.max(room, drawn.width);
+ return (
+ WidgetSpan(
+ alignment: PlaceholderAlignment.middle,
+ child: SizedBox(
+ width: width,
+ height: drawn.height,
+ child: Center(child: drawn.child),
+ ),
+ ),
+ PlaceholderDimensions(
+ size: Size(width * scale, drawn.height * scale),
+ alignment: PlaceholderAlignment.middle,
+ ),
+ );
+ }
+
+ /// How far into its line [offset] is drawn: the advance of what is shown
+ /// before it, with the room any list marker there is spaced out to.
+ double _indent(
+ int offset,
+ List hidden,
+ List<_Stretch> stretches,
+ TextStyle base,
+ TextScaler scaler,
+ ) {
+ final source = text;
+ final lineStart = offset == 0
+ ? 0
+ : source.lastIndexOf('\n', offset - 1) + 1;
+ if (lineStart >= offset) return 0;
+ final painter = TextPainter(
+ text: TextSpan(
+ style: base,
+ children: [
+ for (var i = lineStart; i < offset; i++)
+ if (!hidden.any((range) => range.start <= i && i < range.end))
+ TextSpan(
+ text: source[i],
+ style: TextStyle(
+ letterSpacing: stretches
+ .where((stretch) => stretch.start <= i && i < stretch.end)
+ .firstOrNull
+ ?.spacing,
+ ),
+ ),
+ ],
+ ),
+ textDirection: TextDirection.ltr,
+ textScaler: scaler,
+ )..layout();
+ final width = painter
+ .getOffsetForCaret(
+ TextPosition(offset: painter.plainText.length),
+ Rect.zero,
+ )
+ .dx;
+ painter.dispose();
+ return width;
+ }
+
+ MarkdownAnalysis? _colouredFor;
+ List _colouredCode = const [];
+
+ /// The colours of [analysis]'s fenced code, worked out once per analysis.
+ List _codeTokens(MarkdownAnalysis analysis) {
+ if (identical(analysis, _colouredFor)) return _colouredCode;
+ _colouredFor = analysis;
+ return _colouredCode = analysis.fencedCode.isEmpty
+ ? const []
+ : _codeHighlighter.tokensIn(analysis.text, analysis.fencedCode);
+ }
+
+ /// Code coloured from the note's own palette, so that it sits in the note
+ /// in light and in dark alike rather than in some editor's theme.
+ TextStyle _codeTokenStyle(CodeTokenKind kind) => switch (kind) {
+ CodeTokenKind.keyword => TextStyle(color: _palette.keyword),
+ CodeTokenKind.string => TextStyle(color: _palette.chipCurrency),
+ CodeTokenKind.number => TextStyle(color: _palette.number),
+ CodeTokenKind.comment => TextStyle(
+ color: _palette.comment,
+ fontStyle: FontStyle.italic,
+ ),
+ CodeTokenKind.title => TextStyle(color: _palette.function),
+ CodeTokenKind.type ||
+ CodeTokenKind.attribute => TextStyle(color: _palette.unit),
+ CodeTokenKind.variable => TextStyle(color: _palette.variable),
+ CodeTokenKind.punctuation => TextStyle(color: _palette.operator),
+ };
+
+ @override
+ void dispose() {
+ _math.dispose();
+ super.dispose();
}
/// The room a list marker takes — a bullet, a box or a number, with the
@@ -649,9 +888,12 @@ class HighlightingController extends TextEditingController {
if (marked(MarkdownStyle.quote)) {
result = result.copyWith(color: _palette.textSecondary);
}
- if (marked(MarkdownStyle.codeBlock) || marked(MarkdownStyle.code)) {
+ if (marked(MarkdownStyle.codeBlock) ||
+ marked(MarkdownStyle.code) ||
+ marked(MarkdownStyle.math)) {
// The panel under a code block and the pill under inline code are drawn
// behind the field, where the selection highlight still shows over them.
+ // A formula's TeX, shown as written, is code too.
result = _codeStyle(result);
}
return result;
@@ -682,26 +924,30 @@ class HighlightingController extends TextEditingController {
);
}
- /// How words under [styles] are drawn somewhere other than the field — in
- /// a table drawn as a grid — in the faces and colours they have in it.
+ /// How [words] under [styles] are drawn somewhere other than the field —
+ /// in a table drawn as a grid — in the faces and colours they have in it.
TextStyle markdownRunStyle(
TextStyle base,
Set styles,
- Color linkColor,
- ) {
+ Color linkColor, {
+ String words = '',
+ }) {
bool marked(MarkdownStyle style) => styles.contains(style);
return _markdownInlineStyle(
_markdownBlockStyle(base, base, marked),
marked,
linkColor,
+ words,
);
}
- /// The markdown that changes a word rather than a block.
+ /// The markdown that changes a word rather than a block. [script] is all
+ /// of the superscript or subscript the words are part of, if they are.
TextStyle _markdownInlineStyle(
TextStyle style,
bool Function(MarkdownStyle) marked,
Color linkColor,
+ String script,
) {
var result = style;
if (marked(MarkdownStyle.strong) || marked(MarkdownStyle.tableHeader)) {
@@ -710,12 +956,23 @@ class HighlightingController extends TextEditingController {
if (marked(MarkdownStyle.emphasis)) {
result = result.copyWith(fontStyle: FontStyle.italic);
}
- if (marked(MarkdownStyle.strikethrough)) {
+ final lines = [
+ if (marked(MarkdownStyle.underline)) TextDecoration.underline,
+ if (marked(MarkdownStyle.strikethrough)) TextDecoration.lineThrough,
+ ];
+ if (lines.isNotEmpty) {
result = result.copyWith(
- decoration: TextDecoration.lineThrough,
+ decoration: TextDecoration.combine(lines),
decorationColor: result.color,
);
}
+ if (marked(MarkdownStyle.superscript) || marked(MarkdownStyle.subscript)) {
+ result = _scripted(
+ result,
+ script,
+ superscript: marked(MarkdownStyle.superscript),
+ );
+ }
if (marked(MarkdownStyle.link)) {
result = result.copyWith(color: linkColor);
}
@@ -728,6 +985,49 @@ class HighlightingController extends TextEditingController {
return result;
}
+ /// Superscript or subscript: the face's own raised or lowered glyphs where
+ /// it has one for every character of [script], and otherwise the same words
+ /// smaller. A text engine cannot move words off their baseline, and the
+ /// line keeps its height either way.
+ ///
+ /// Which characters a face has these for is known for the faces a note is
+ /// written in: Shantell Sans for digits, the system face on Apple's
+ /// platforms for digits, small letters and `+=()`. Anywhere else the words
+ /// are drawn smaller.
+ TextStyle _scripted(
+ TextStyle style,
+ String script, {
+ required bool superscript,
+ }) {
+ final (feature, drawn) = switch (style.fontFamily) {
+ 'Shantell Sans' => (superscript ? 'sups' : 'sinf', _digits),
+ '.AppleSystemUIFont' => (
+ superscript ? 'sups' : 'subs',
+ _appleScriptCharacters,
+ ),
+ _ => (null, const {}),
+ };
+ if (feature != null &&
+ script.trim().isNotEmpty &&
+ script.runes.every((rune) => rune == 0x20 || drawn.contains(rune))) {
+ return style.copyWith(
+ fontFeatures: [...?style.fontFeatures, FontFeature.enable(feature)],
+ );
+ }
+ final size = style.fontSize;
+ final height = style.height;
+ if (size == null) return style;
+ return style.copyWith(
+ fontSize: size * _scriptScale,
+ height: height == null ? null : height / _scriptScale,
+ );
+ }
+
+ static const double _scriptScale = 0.75;
+ static final _digits = '0123456789'.runes.toSet();
+ static final _appleScriptCharacters =
+ '0123456789abcdefghijklmnopqrstuvwxyz+=()'.runes.toSet();
+
static List _checkedTextRanges(String source) {
final ranges = [];
var lineStart = 0;
@@ -783,3 +1083,21 @@ class HighlightingController extends TextEditingController {
/// Letter spacing over [start, end): see
/// [HighlightingController._listStretches].
typedef _Stretch = ({int start, int end, double spacing});
+
+/// The text the field draws, which works out its semantics information once
+/// and keeps it.
+///
+/// The field works that information out afresh, over every run in the note,
+/// each time its semantics are marked dirty, and taking in a widget marks
+/// them. So without this, opening a note with hundreds of formulas walked the
+/// whole note that many times over whenever the semantics tree was on, and on
+/// macOS it always is, for dictation: see `AccessibilityTree.swift`.
+class _NoteSpan extends TextSpan {
+ _NoteSpan({super.style, super.children});
+
+ late final List _semantics =
+ List.unmodifiable(super.getSemanticsInformation());
+
+ @override
+ List getSemanticsInformation() => _semantics;
+}
diff --git a/lib/ui/editor/line_metrics.dart b/lib/ui/editor/line_metrics.dart
index b50b873..11d1a67 100644
--- a/lib/ui/editor/line_metrics.dart
+++ b/lib/ui/editor/line_metrics.dart
@@ -1,3 +1,4 @@
+import 'package:flutter/foundation.dart';
import 'package:material_ui/material_ui.dart';
/// Where each logical line of the note sits vertically inside the text field.
@@ -23,81 +24,307 @@ class LineOffsets {
);
}
-/// Measures line positions by laying the note out exactly the way the text
-/// field does.
+/// Words in one style, or a placeholder of a known size: a piece of a line as
+/// it is laid out.
+@immutable
+class _Run {
+ const _Run(this.text, this.style)
+ : placeholder = null,
+ alignment = PlaceholderAlignment.bottom,
+ baseline = null;
+
+ const _Run.placeholder(
+ this.style,
+ PlaceholderDimensions this.placeholder, {
+ required this.alignment,
+ required this.baseline,
+ }) : text = null;
+
+ final String? text;
+ final TextStyle? style;
+ final PlaceholderDimensions? placeholder;
+
+ /// Where the placeholder sits on its row: the span's own say, which is
+ /// what the paragraph goes by.
+ final PlaceholderAlignment alignment;
+ final TextBaseline? baseline;
+
+ int get length => text?.length ?? 1;
+
+ InlineSpan get span => placeholder == null
+ ? TextSpan(text: text, style: style)
+ : WidgetSpan(
+ child: const SizedBox.shrink(),
+ alignment: alignment,
+ baseline: baseline,
+ style: style,
+ );
+
+ @override
+ bool operator ==(Object other) =>
+ other is _Run &&
+ other.text == text &&
+ other.style == style &&
+ other.alignment == alignment &&
+ other.baseline == baseline &&
+ other.placeholder?.size == placeholder?.size &&
+ other.placeholder?.baselineOffset == placeholder?.baselineOffset;
+
+ @override
+ int get hashCode => Object.hash(text, style, placeholder?.size);
+}
+
+/// One logical line: its runs, and the style of the line break that ends it.
+@immutable
+class _Line {
+ const _Line(this.runs, {required this.broken, this.breakStyle});
+
+ final List<_Run> runs;
+
+ /// False on the last line, which has no line break after it.
+ final bool broken;
+ final TextStyle? breakStyle;
+
+ int get length => runs.fold(0, (sum, run) => sum + run.length);
+
+ @override
+ bool operator ==(Object other) =>
+ other is _Line &&
+ other.broken == broken &&
+ other.breakStyle == breakStyle &&
+ listEquals(other.runs, runs);
+
+ @override
+ int get hashCode => Object.hash(broken, breakStyle, Object.hashAll(runs));
+}
+
+/// Measures line positions by laying the note out the way the text field
+/// does, a line at a time.
///
/// This is what keeps a result aligned to its line even when the line wraps:
/// wrapped rows push everything below them down, and the measurement sees it
-/// because it uses the same width, style and strut as the real field.
+/// because it uses the same width, styles and strut as the real field.
+///
+/// A line at a time because a line break is hard: how one line wraps never
+/// depends on another, so an edit only needs the lines it changed laid out
+/// again. The whole note laid out as one paragraph instead costs as much as
+/// the field's own layout, on every keystroke, and in a long note that is
+/// most of a second.
class LineMeasurer {
- String? _text;
double? _width;
TextScaler? _scaler;
- Object? _layoutKey;
- int? _placeholderKey;
- LineOffsets? _cached;
+ StrutStyle? _strut;
+ TextStyle? _style;
+ double _lineHeight = 0;
+ List<_Line> _lines = const [];
+ List<({double height, double caret})> _heights = const [];
+ final Map _letterCarets = {};
/// [span] must be the very span the field renders — styled runs can differ
/// in width from the same characters in the base style.
///
- /// [placeholders] must describe every [WidgetSpan] in [span], in order.
- /// [TextPainter] refuses to lay out a span containing placeholders it has
- /// not been given dimensions for, and an image is a placeholder — so a note
- /// with pictures measures wrong, or not at all, without this.
+ /// [placeholders] must describe every [WidgetSpan] in [span], in order. An
+ /// image is a placeholder, and a line holding one is as tall as the image,
+ /// so a note with pictures measures wrong without this.
+ ///
+ /// [text] is the note itself, which [span] can draw on fewer lines: a
+ /// formula drawn in place of its TeX shows the line breaks inside it as
+ /// spaces. A line of the note that starts inside a drawn line shares that
+ /// line's top.
LineOffsets measure({
required InlineSpan span,
required String text,
required double maxWidth,
required StrutStyle strut,
required TextScaler textScaler,
- required Object layoutKey,
List placeholders = const [],
}) {
- final placeholderKey = Object.hashAll([
- for (final placeholder in placeholders)
- Object.hash(placeholder.size.width, placeholder.size.height),
- ]);
- if (_text == text &&
- _width == maxWidth &&
- _scaler == textScaler &&
- _layoutKey == layoutKey &&
- _placeholderKey == placeholderKey &&
- _cached != null) {
- return _cached!;
- }
if (maxWidth <= 0 || !maxWidth.isFinite) return LineOffsets.empty;
+ if (_width != maxWidth ||
+ _scaler != textScaler ||
+ _strut != strut ||
+ _style != span.style) {
+ _width = maxWidth;
+ _scaler = textScaler;
+ _strut = strut;
+ _style = span.style;
+ _lines = const [];
+ _heights = const [];
+ _letterCarets.clear();
+ final row = TextPainter(
+ text: TextSpan(style: span.style),
+ textDirection: TextDirection.ltr,
+ strutStyle: strut,
+ textScaler: textScaler,
+ );
+ _lineHeight = row.preferredLineHeight;
+ row.dispose();
+ }
- final painter = TextPainter(
- text: span,
- textDirection: TextDirection.ltr,
- strutStyle: strut,
- textScaler: textScaler,
+ final lines = _linesOf(span, placeholders);
+ // The lines an edit left alone keep their heights: those before the first
+ // one that differs, and those after the last.
+ var before = 0;
+ while (before < lines.length &&
+ before < _lines.length &&
+ lines[before] == _lines[before]) {
+ before++;
+ }
+ var after = 0;
+ while (after < lines.length - before &&
+ after < _lines.length - before &&
+ lines[lines.length - 1 - after] == _lines[_lines.length - 1 - after]) {
+ after++;
+ }
+ final heights = [
+ ..._heights.take(before),
+ for (var i = before; i < lines.length - after; i++)
+ _heightOf(lines[i], span.style),
+ ..._heights.skip(_heights.length - after),
+ ];
+ _lines = lines;
+ _heights = heights;
+
+ // A line's top is where the caret stands at its start, as the field
+ // reports it: below the rows above, and as far down its own first row as
+ // its first characters sit.
+ final tops = [];
+ var top = 0.0;
+ for (final line in heights) {
+ tops.add(top + line.caret);
+ top += line.height;
+ }
+ return LineOffsets(
+ tops: _noteLineTops(text, lines, tops),
+ lineHeight: _lineHeight,
+ totalHeight: top,
);
- if (placeholders.isNotEmpty) painter.setPlaceholderDimensions(placeholders);
- painter.layout(maxWidth: maxWidth);
+ }
+ /// [drawnTops], one for each of [lines], as one for each line of [text]:
+ /// the same list unless a drawn line holds several of the note's.
+ static List _noteLineTops(
+ String text,
+ List<_Line> lines,
+ List drawnTops,
+ ) {
+ if ('\n'.allMatches(text).length + 1 == lines.length) return drawnTops;
final tops = [];
- var offset = 0;
- for (final line in text.split('\n')) {
- tops.add(
- painter.getOffsetForCaret(TextPosition(offset: offset), Rect.zero).dy,
- );
- offset += line.length + 1;
+ var line = 0;
+ var lineEnd = lines.first.length;
+ for (var start = 0; ; start = text.indexOf('\n', start) + 1) {
+ while (line < lines.length - 1 && start > lineEnd) {
+ line++;
+ lineEnd += 1 + lines[line].length;
+ }
+ tops.add(drawnTops[line]);
+ if (text.indexOf('\n', start) < 0) return tops;
}
+ }
- final result = LineOffsets(
- tops: tops,
- lineHeight: painter.preferredLineHeight,
- totalHeight: painter.height,
+ /// How tall [line]'s rows are, its line break included, and how far down
+ /// the first of them the caret stands at its start.
+ ({double height, double caret}) _heightOf(_Line line, TextStyle? style) {
+ final painter = TextPainter(
+ text: TextSpan(
+ style: style,
+ children: [
+ for (final run in line.runs) run.span,
+ if (line.broken) TextSpan(text: '\nx', style: line.breakStyle),
+ ],
+ ),
+ textDirection: TextDirection.ltr,
+ strutStyle: _strut,
+ textScaler: _scaler!,
);
+ final placeholders = [for (final run in line.runs) ?run.placeholder];
+ if (placeholders.isNotEmpty) painter.setPlaceholderDimensions(placeholders);
+ painter.layout(maxWidth: _width!);
+ var height = painter.height;
+ if (line.broken) {
+ // Laid out with its break and a letter after it, which stands in for
+ // the next line: the caret before that letter is as far down its row
+ // as it is on a row of the letter alone.
+ height =
+ _caretTop(painter, line.length + 1) -
+ _letterCarets.putIfAbsent(
+ line.breakStyle,
+ () => _letterCaret(style, line.breakStyle),
+ );
+ }
+ final caret = _caretTop(painter, 0);
painter.dispose();
+ return (height: height, caret: caret);
+ }
+
+ /// How far down a row of one letter in [breakStyle] the caret stands.
+ double _letterCaret(TextStyle? style, TextStyle? breakStyle) {
+ final painter = TextPainter(
+ text: TextSpan(
+ style: style,
+ children: [TextSpan(text: 'x', style: breakStyle)],
+ ),
+ textDirection: TextDirection.ltr,
+ strutStyle: _strut,
+ textScaler: _scaler!,
+ )..layout(maxWidth: _width!);
+ final caret = _caretTop(painter, 0);
+ painter.dispose();
+ return caret;
+ }
+
+ static double _caretTop(TextPainter painter, int offset) =>
+ painter.getOffsetForCaret(TextPosition(offset: offset), Rect.zero).dy;
+
+ /// [span] cut at its line breaks, every run with the style it inherits.
+ static List<_Line> _linesOf(
+ InlineSpan span,
+ List placeholders,
+ ) {
+ final lines = <_Line>[];
+ var runs = <_Run>[];
+ var placeholder = 0;
+ void visit(InlineSpan node, TextStyle? inherited) {
+ final own = node.style;
+ final style = own == null
+ ? inherited
+ : inherited == null
+ ? own
+ : inherited.merge(own);
+ if (node is TextSpan) {
+ final text = node.text;
+ if (text != null && text.isNotEmpty) {
+ var start = 0;
+ while (true) {
+ final newline = text.indexOf('\n', start);
+ final end = newline < 0 ? text.length : newline;
+ if (end > start) runs.add(_Run(text.substring(start, end), style));
+ if (newline < 0) break;
+ lines.add(_Line(runs, broken: true, breakStyle: style));
+ runs = <_Run>[];
+ start = newline + 1;
+ }
+ }
+ for (final child in node.children ?? const []) {
+ visit(child, style);
+ }
+ } else if (node is PlaceholderSpan) {
+ runs.add(
+ _Run.placeholder(
+ style,
+ placeholder < placeholders.length
+ ? placeholders[placeholder]
+ : PlaceholderDimensions.empty,
+ alignment: node.alignment,
+ baseline: node.baseline,
+ ),
+ );
+ placeholder++;
+ }
+ }
- _text = text;
- _width = maxWidth;
- _scaler = textScaler;
- _layoutKey = layoutKey;
- _placeholderKey = placeholderKey;
- _cached = result;
- return result;
+ visit(span, null);
+ lines.add(_Line(runs, broken: false));
+ return lines;
}
}
diff --git a/lib/ui/editor/markdown_editing.dart b/lib/ui/editor/markdown_editing.dart
index de93fa7..eba4f82 100644
--- a/lib/ui/editor/markdown_editing.dart
+++ b/lib/ui/editor/markdown_editing.dart
@@ -1286,8 +1286,6 @@ TextEditingValue? _typedAtCaret(
/// the next line's hidden structure.
/// * Return at the start of a heading's words opens a line above it, rather
/// than splitting the heading from its own words.
-/// * `[] ` or `[ ] ` typed at the start of a line makes a task, as it always
-/// has in this app.
TextEditingValue? markdownStructureEdit(
TextEditingValue oldValue,
TextEditingValue newValue,
@@ -1355,26 +1353,270 @@ TextEditingValue? markdownStructureEdit(
);
}
}
+ return null;
+}
- // `[] ` and `[ ] ` at the start of a line.
- if (text == old.replaceRange(caret, caret, ' ')) {
- final line = markdownLinePrefix(old, caret);
- if (!line.isListItem) {
- final typed = old.substring(line.contentStart, caret);
- if (typed == '[]' || typed == '[ ]') {
- const task = '- [ ] ';
- return TextEditingValue(
- text: old.replaceRange(line.contentStart, caret, task),
- selection: TextSelection.collapsed(
- offset: line.contentStart + task.length,
- ),
- );
- }
- }
+/// A markdown shortcut just typed, remembered for one keystroke so that
+/// Backspace or Undo straight after it can give back what was typed, the way
+/// Notion does: `# ` that became a heading comes back as the two characters,
+/// escaped (`\# `) so that they stay characters.
+///
+/// Also remembers a code fence while it is being typed, so that Return at
+/// the end of it can tell a fence the writer is opening from one that was
+/// already there.
+class MarkdownShortcut {
+ TextEditingValue? _made;
+ TextEditingValue? _typed;
+ TextEditingValue? _fence;
+
+ /// What the writer typed, as plain text, if [value] is still exactly what
+ /// the shortcut made of it.
+ TextEditingValue? typedFor(TextEditingValue value) =>
+ _isAt(value, _made) ? _typed : null;
+
+ void clear() {
+ _made = null;
+ _typed = null;
+ _fence = null;
+ }
+}
+
+/// Whether [value] is [state]: the same text, and the caret in the same
+/// place.
+bool _isAt(TextEditingValue value, TextEditingValue? state) =>
+ state != null &&
+ value.text == state.text &&
+ value.selection.isCollapsed &&
+ value.selection.baseOffset == state.selection.baseOffset;
+
+/// A line's start, typed up to a marker that a space makes into structure:
+/// a heading's `#`, a bullet, a number, a quote's `>` or a task's box. Only
+/// at the start of the words, after any indentation and quote markers.
+final _shortcutMarker = RegExp(
+ r'^[ \t]*(?:> ?)*(#{1,6}|[-*+]|[0-9]{1,9}[.)]|>|\[[ xX]?\])$',
+);
+
+/// A line that opens a fenced code block, from its first column.
+final _fenceLine = RegExp(r'^ {0,3}(?:`{3,}[^`]*|~{3,}.*)$');
+
+/// [marker] as characters rather than markdown: a backslash before the one
+/// character that makes it syntax.
+String _escaped(String marker) {
+ final last = marker.length - 1;
+ return _isDigit(marker.codeUnitAt(0))
+ ? '${marker.substring(0, last)}\\${marker.substring(last)}'
+ : '\\$marker';
+}
+
+/// What a keystroke becomes when it completes a markdown shortcut. Null
+/// leaves it as it was.
+///
+/// * A space after a marker at the start of a line — `# `, `- `, `* `,
+/// `+ `, `1. `, `> ` — is left as typed, since markdown already reads it
+/// as structure. `[] `, `[ ] ` and `[x] ` become a task, ticked for `x`.
+/// * Return at the end of a code fence the writer has just typed writes the
+/// closing fence too, with the caret between, so the rest of the note does
+/// not turn into code.
+/// * `---` straight under a line of words is a divider rather than the
+/// underline CommonMark would read it as, which would make the words above
+/// a heading: a blank line goes in above it.
+/// * Backspace straight after any of these, while the shortcut turned into
+/// formatting, gives back what was typed as plain text. See
+/// [MarkdownShortcut].
+///
+/// None of it happens while an input method is composing: the text is the
+/// input method's until it commits.
+TextEditingValue? markdownShortcutEdit(
+ TextEditingValue oldValue,
+ TextEditingValue newValue,
+ MarkdownAnalysis Function(String text) markdown,
+ MarkdownShortcut shortcut,
+) {
+ final typedBack = shortcut.typedFor(oldValue);
+ final fenceOpen = _isAt(oldValue, shortcut._fence);
+ shortcut.clear();
+ if (newValue.composing.isValid && !newValue.composing.isCollapsed) {
+ return null;
+ }
+ final selection = oldValue.selection;
+ if (!selection.isValid || !selection.isCollapsed) return null;
+ final old = oldValue.text;
+ final text = newValue.text;
+ final caret = selection.start;
+
+ if (typedBack != null &&
+ caret > 0 &&
+ text == old.replaceRange(caret - 1, caret, '')) {
+ return typedBack;
+ }
+
+ final added = text.length - old.length;
+ if (added <= 0) return null;
+ final inserted = text.substring(caret, caret + added);
+ if (text != old.replaceRange(caret, caret, inserted)) return null;
+ final lineStart = caret == 0 ? 0 : old.lastIndexOf('\n', caret - 1) + 1;
+ final end = caret + added;
+
+ if (inserted == '\n') {
+ if (!fenceOpen) return null;
+ return _closeFence(oldValue, caret, lineStart, markdown(old), shortcut);
+ }
+
+ final newline = text.indexOf('\n', end);
+ final lineEnd = newline < 0 ? text.length : newline;
+ if (end == lineEnd &&
+ !inserted.contains('\n') &&
+ _fenceLine.hasMatch(text.substring(lineStart, lineEnd)) &&
+ (fenceOpen || !_fenceLine.hasMatch(old.substring(lineStart, caret)))) {
+ shortcut._fence = TextEditingValue(
+ text: text,
+ selection: TextSelection.collapsed(offset: end),
+ );
+ return null;
+ }
+
+ if (inserted == ' ') {
+ return _markerTyped(
+ oldValue,
+ newValue,
+ caret,
+ lineStart,
+ markdown,
+ shortcut,
+ );
+ }
+ if (inserted == '-' && end == lineEnd) {
+ return _dividerTyped(
+ oldValue,
+ newValue,
+ caret,
+ lineStart,
+ markdown,
+ shortcut,
+ );
}
return null;
}
+TextEditingValue? _markerTyped(
+ TextEditingValue oldValue,
+ TextEditingValue newValue,
+ int caret,
+ int lineStart,
+ MarkdownAnalysis Function(String text) markdown,
+ MarkdownShortcut shortcut,
+) {
+ final old = oldValue.text;
+ final marker = _shortcutMarker.firstMatch(
+ old.substring(lineStart, caret),
+ )?[1];
+ if (marker == null) return null;
+ final markerStart = caret - marker.length;
+ var made = newValue;
+ if (marker.startsWith('[')) {
+ final task = marker.length == 3 && marker[1] != ' ' ? '- [x] ' : '- [ ] ';
+ made = TextEditingValue(
+ text: old.replaceRange(markerStart, caret, task),
+ selection: TextSelection.collapsed(offset: markerStart + task.length),
+ );
+ }
+ final at = made.selection.baseOffset;
+ if (markdown(made.text).atomicPrefixAt(at)?.end == at) {
+ final typed = '${_escaped(marker)} ';
+ shortcut
+ .._made = made
+ .._typed = TextEditingValue(
+ text: old.replaceRange(markerStart, caret, typed),
+ selection: TextSelection.collapsed(offset: markerStart + typed.length),
+ );
+ }
+ return identical(made, newValue) ? null : made;
+}
+
+/// The closing fence for the one the caret is at the end of, if that line
+/// opens a code block rather than closing one.
+TextEditingValue? _closeFence(
+ TextEditingValue oldValue,
+ int caret,
+ int lineStart,
+ MarkdownAnalysis analysis,
+ MarkdownShortcut shortcut,
+) {
+ final old = oldValue.text;
+ final opensBlock = analysis.codeBlocks.any(
+ (block) => block.start <= lineStart + 3 && block.start >= lineStart,
+ );
+ if (!opensBlock) return null;
+ final line = old.substring(lineStart, caret);
+ final fenceStart = lineStart + line.length - line.trimLeft().length;
+ final char = old[fenceStart];
+ var run = fenceStart;
+ while (run < caret && old[run] == char) {
+ run++;
+ }
+ final closing = '\n${old.substring(lineStart, run)}';
+ final made = TextEditingValue(
+ text: old.replaceRange(caret, caret, '\n$closing'),
+ selection: TextSelection.collapsed(offset: caret + 1),
+ );
+ shortcut
+ .._made = made
+ .._typed = TextEditingValue(
+ text: old
+ .replaceRange(caret, caret, '\n')
+ .replaceRange(fenceStart, fenceStart, '\\'),
+ selection: TextSelection.collapsed(offset: caret + 2),
+ );
+ return made;
+}
+
+/// A blank line above `---` typed straight under a line of words.
+TextEditingValue? _dividerTyped(
+ TextEditingValue oldValue,
+ TextEditingValue newValue,
+ int caret,
+ int lineStart,
+ MarkdownAnalysis Function(String text) markdown,
+ MarkdownShortcut shortcut,
+) {
+ final text = newValue.text;
+ if (text.substring(lineStart, caret + 1) != '---') return null;
+ final analysis = markdown(text);
+ final underlines = analysis.spans.any(
+ (span) => span.end == lineStart && _isHeading(span.style),
+ );
+ final TextEditingValue made;
+ if (underlines) {
+ made = TextEditingValue(
+ text: text.replaceRange(lineStart, lineStart, '\n'),
+ selection: TextSelection.collapsed(offset: caret + 2),
+ );
+ } else if (analysis.blocks.any(
+ (block) => block.kind == MarkdownBlockKind.rule && block.start == lineStart,
+ )) {
+ made = newValue;
+ } else {
+ return null;
+ }
+ shortcut
+ .._made = made
+ .._typed = TextEditingValue(
+ text: text.replaceRange(lineStart, lineStart, '\\'),
+ selection: TextSelection.collapsed(offset: caret + 2),
+ );
+ return identical(made, newValue) ? null : made;
+}
+
+bool _isHeading(MarkdownStyle style) => switch (style) {
+ MarkdownStyle.heading1 ||
+ MarkdownStyle.heading2 ||
+ MarkdownStyle.heading3 ||
+ MarkdownStyle.heading4 ||
+ MarkdownStyle.heading5 ||
+ MarkdownStyle.heading6 => true,
+ _ => false,
+};
+
// ---------------------------------------------------------------------------
// Tables
//
diff --git a/lib/ui/editor/markdown_syntax.dart b/lib/ui/editor/markdown_syntax.dart
index cd848cb..ae19b49 100644
--- a/lib/ui/editor/markdown_syntax.dart
+++ b/lib/ui/editor/markdown_syntax.dart
@@ -19,12 +19,26 @@ enum MarkdownStyle {
emphasis,
strikethrough,
+ /// Words between `` and ` `, or `` and ` `.
+ underline,
+
+ /// Words between `` and ` `.
+ superscript,
+
+ /// Words between `` and ` `.
+ subscript,
+
/// Inline code, its backticks included.
code,
/// A fenced or indented code block, fences included.
codeBlock,
+ /// A formula in TeX, its dollar signs included. Drawn as the formula it
+ /// writes, and as its TeX in code's face while the caret is at it; see
+ /// [MarkdownMath].
+ math,
+
/// Everything inside a block quote.
quote,
@@ -133,6 +147,95 @@ class MarkdownTask {
int get hashCode => Object.hash(box, checked);
}
+/// A formula written in TeX between dollar signs: `$…$` among words, or
+/// `$$…$$` set apart on lines of its own.
+///
+/// [start] to [end] takes the dollar signs in, and [tex] is what they hold,
+/// trimmed. A formula is literal, the way code is: the calculator does not read
+/// it and spelling leaves it alone.
+@immutable
+class MarkdownMath {
+ const MarkdownMath(this.start, this.end, this.tex, {required this.display});
+
+ final int start;
+ final int end;
+ final String tex;
+
+ /// Set apart on lines of its own rather than written among words.
+ final bool display;
+
+ MarkdownMath _shifted(int by) =>
+ MarkdownMath(start + by, end + by, tex, display: display);
+
+ /// Whether the formula shows its TeX rather than itself, with the selection
+ /// at [selection].
+ ///
+ /// Only in a note being edited, which is what [revealBlocks] says. A caret
+ /// or a selection's end anywhere inside always shows it: that is where the
+ /// TeX is typed. Against its edges, display math shows, as a block's syntax
+ /// does; inline math as inline markers do, so only with [revealEdges] — and
+ /// a formula whose closing `$` has just been typed is drawn at once. A
+ /// selection reaching over the whole of one leaves it drawn.
+ bool revealedAt(
+ TextSelection? selection, {
+ required bool revealBlocks,
+ required bool revealEdges,
+ }) {
+ if (!revealBlocks || selection == null || !selection.isValid) return false;
+ bool inside(int offset) => offset > start && offset < end;
+ if (inside(selection.baseOffset) || inside(selection.extentOffset)) {
+ return true;
+ }
+ if (!selection.isCollapsed) return false;
+ final caret = selection.baseOffset;
+ if (caret != start && caret != end) return false;
+ return display || revealEdges;
+ }
+
+ @override
+ bool operator ==(Object other) =>
+ other is MarkdownMath &&
+ other.start == start &&
+ other.end == end &&
+ other.tex == tex &&
+ other.display == display;
+
+ @override
+ int get hashCode => Object.hash(start, end, tex, display);
+
+ @override
+ String toString() =>
+ 'MarkdownMath($start, $end, ${display ? 'display' : 'inline'}: $tex)';
+}
+
+/// A fenced code block whose info string names a language, for colouring the
+/// code the way that language reads.
+@immutable
+class MarkdownCodeBlock {
+ const MarkdownCodeBlock(this.language, this.code);
+
+ /// The info string's first word, as written: `python`, `ts`, `C++`.
+ final String language;
+
+ /// Where the code sits, fences left out. In more than one piece when the
+ /// block is indented or quoted, since a line's indentation or `>` is not
+ /// code.
+ final List code;
+
+ MarkdownCodeBlock _shifted(int by) => MarkdownCodeBlock(language, [
+ for (final range in code) _shift(range, by),
+ ]);
+
+ @override
+ bool operator ==(Object other) =>
+ other is MarkdownCodeBlock &&
+ other.language == language &&
+ listEquals(other.code, code);
+
+ @override
+ int get hashCode => Object.hash(language, Object.hashAll(code));
+}
+
enum MarkdownBlockKind { code, quote, rule }
/// A block that is drawn behind the text as well as in it: the panel under a
@@ -504,12 +607,14 @@ class MarkdownConcealment {
const MarkdownConcealment._({
required this.hidden,
required this.transparent,
+ required this.formulas,
required this.key,
});
static const none = MarkdownConcealment._(
hidden: [],
transparent: [],
+ formulas: [],
key: 0,
);
@@ -520,6 +625,11 @@ class MarkdownConcealment {
/// place: a bullet, a checkbox, a table's grid. Sorted, never overlapping.
final List transparent;
+ /// The formulas drawn as formulas rather than as their TeX. Each is in
+ /// [hidden] whole; the editor draws the formula in the place of its first
+ /// character. Sorted.
+ final List formulas;
+
/// Changes whenever what is shown does, for anything that caches a layout.
final int key;
}
@@ -564,8 +674,20 @@ class MarkdownAnalysis {
/// Fenced and indented code blocks, which the calculator does not read.
List get codeBlocks => _parts.codeBlocks;
- /// The rest of what the calculator does not read: struck-through text, and
- /// the `*` and `_` around emphasis — the markers only, not the words.
+ /// The fenced code blocks that name a language, for colouring their code.
+ List get fencedCode => _parts.fencedCode;
+
+ /// Every formula outside a table, in order. One in a table's cell is drawn
+ /// as its TeX, in the grid.
+ List get maths => _parts.maths;
+
+ /// ` ` tags, which a table's cell draws as the line break they ask for.
+ /// Anywhere else a tag is shown as written, as any other inline HTML is.
+ List get lineBreaks => _parts.lineBreaks;
+
+ /// The rest of what the calculator does not read: struck-through text, the
+ /// `*` and `_` around emphasis — the markers only, not the words — and
+ /// the backslash of an escape.
List get unread => _parts.unread;
/// Where the text is literal rather than prose: code, code blocks, HTML.
@@ -601,6 +723,9 @@ class MarkdownAnalysis {
/// this far: the parser is not shown it as emphasis in the first place.
/// * Struck-through text is blanked, words and all: crossed out is not
/// counted, and `Total ~~12~~ 14` is 14.
+ /// * Formulas are blanked, dollar signs and all. `$x^2 + 1$` is written to
+ /// be read, not worked out, and a price never gets this far: `$5 and $10`
+ /// is not a formula, by the rules [MarkdownMath] is read by.
/// * A table's rows are blanked. `| 2+2 | 4 |` is a row of a grid rather than
/// a sum, and a result chip beside a tall wrapped row reads as a mistake.
late final String calculatorText = _neutralise();
@@ -635,6 +760,9 @@ class MarkdownAnalysis {
/// The words of [start, end) with every marker in it hidden, trimmed, and
/// cut wherever their inline markdown changes: what a table's cell shows
/// when the table is drawn as a grid rather than as its text.
+ ///
+ /// A ` ` becomes the line break it asks for, the spaces either side of
+ /// it dropped, since a cell has nowhere else to put one.
List runsIn(int start, int end) {
final hidden = [
for (final conceal in conceals)
@@ -647,6 +775,10 @@ class MarkdownAnalysis {
_runStyles.contains(span.style))
span,
];
+ final breaks = [
+ for (final tag in lineBreaks)
+ if (tag.start >= start && tag.end <= end) tag,
+ ];
final cuts = {start, end};
for (final conceal in hidden) {
cuts
@@ -658,6 +790,11 @@ class MarkdownAnalysis {
..add(span.start.clamp(start, end))
..add(span.end.clamp(start, end));
}
+ for (final tag in breaks) {
+ cuts
+ ..add(tag.start)
+ ..add(tag.end);
+ }
final sorted = cuts.toList()..sort();
final runs = [];
@@ -668,11 +805,15 @@ class MarkdownAnalysis {
hidden.any((conceal) => conceal.start <= from && to <= conceal.end)) {
continue;
}
+ final lineBreak = breaks.any((tag) => tag.start <= from && to <= tag.end);
final styles = {
for (final span in styled)
- if (span.start <= from && to <= span.end) span.style,
+ if (span.start <= from &&
+ to <= span.end &&
+ !(lineBreak && span.style == MarkdownStyle.html))
+ span.style,
};
- final words = text.substring(from, to);
+ final words = lineBreak ? '\n' : text.substring(from, to);
if (runs.isNotEmpty && setEquals(runs.last.styles, styles)) {
final last = runs.removeLast();
runs.add((text: last.text + words, styles: styles));
@@ -680,6 +821,15 @@ class MarkdownAnalysis {
runs.add((text: words, styles: styles));
}
}
+ if (breaks.isNotEmpty) {
+ for (var i = 0; i < runs.length; i++) {
+ final run = runs[i];
+ runs[i] = (
+ text: run.text.replaceAll(_spacedBreak, '\n'),
+ styles: run.styles,
+ );
+ }
+ }
while (runs.isNotEmpty && runs.first.text.trimLeft().isEmpty) {
runs.removeAt(0);
@@ -695,12 +845,18 @@ class MarkdownAnalysis {
return runs;
}
+ static final _spacedBreak = RegExp(r'[ \t]*\n[ \t]*');
+
/// The markdown that styles words wherever they are drawn.
static const _runStyles = {
MarkdownStyle.strong,
MarkdownStyle.emphasis,
MarkdownStyle.strikethrough,
+ MarkdownStyle.underline,
+ MarkdownStyle.superscript,
+ MarkdownStyle.subscript,
MarkdownStyle.code,
+ MarkdownStyle.math,
MarkdownStyle.link,
MarkdownStyle.html,
};
@@ -722,10 +878,15 @@ class MarkdownAnalysis {
/// [revealEdges] is false in the same cases and also while the writer is
/// typing, so markers they have just completed disappear as they complete
/// them rather than lingering beside the caret.
+ ///
+ /// A formula is drawn as itself unless [MarkdownMath.revealedAt] says
+ /// otherwise, or [drawable] says it cannot be: TeX the editor cannot draw
+ /// stays in sight as written.
MarkdownConcealment concealFor(
TextSelection? selection, {
required bool revealBlocks,
required bool revealEdges,
+ bool Function(MarkdownMath math)? drawable,
}) {
final valid = selection != null && selection.isValid;
final caret = valid && selection.isCollapsed ? selection.baseOffset : null;
@@ -782,6 +943,24 @@ class MarkdownAnalysis {
}
}
+ final formulas = [];
+ for (var i = 0; i < maths.length; i++) {
+ final math = maths[i];
+ final drawn =
+ (drawable?.call(math) ?? true) &&
+ !math.revealedAt(
+ selection,
+ revealBlocks: revealBlocks,
+ revealEdges: revealEdges,
+ );
+ if (drawn) {
+ formulas.add(math);
+ hidden.add(TextRange(start: math.start, end: math.end));
+ } else {
+ key = Object.hash(key, conceals.length + i);
+ }
+ }
+
final transparent = [
for (final ornament in ornaments)
if (ornament.kind != MarkdownOrnamentKind.codeSpan)
@@ -808,6 +987,7 @@ class MarkdownAnalysis {
return MarkdownConcealment._(
hidden: _merged(hidden),
transparent: _merged(transparent),
+ formulas: List.unmodifiable(formulas),
key: key,
);
}
@@ -848,6 +1028,7 @@ class MarkdownAnalysis {
String _neutralise() {
if (codeBlocks.isEmpty &&
unread.isEmpty &&
+ maths.isEmpty &&
containerMarkers.isEmpty &&
tables.isEmpty) {
return text;
@@ -861,7 +1042,11 @@ class MarkdownAnalysis {
if (unit != 0x0A && unit != 0x0D) units[i] = 0x20;
}
}
- for (final range in [...codeBlocks, ...unread]) {
+ for (final range in [
+ ...codeBlocks,
+ ...unread,
+ for (final math in maths) TextRange(start: math.start, end: math.end),
+ ]) {
for (var i = range.start; i < range.end && i < units.length; i++) {
final unit = units[i];
if (unit != 0x0A && unit != 0x0D) units[i] = 0x20;
@@ -1044,6 +1229,9 @@ class _Parts {
required this.listItems,
required this.atomicPrefixes,
required this.codeBlocks,
+ required this.fencedCode,
+ required this.maths,
+ required this.lineBreaks,
required this.unread,
required this.literals,
required this.addresses,
@@ -1064,6 +1252,9 @@ class _Parts {
listItems: [],
atomicPrefixes: [],
codeBlocks: [],
+ fencedCode: [],
+ maths: [],
+ lineBreaks: [],
unread: [],
literals: [],
addresses: [],
@@ -1081,6 +1272,9 @@ class _Parts {
final List listItems;
final List