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/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/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 a20cf43..827858b 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
@@ -680,6 +684,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..73c0523 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,20 +463,16 @@ class ImageClipboard {
static const _richChannel = MethodChannel('kapynotes/rich_clipboard');
- /// A Kapy Notes rich fragment, when the clipboard carries one.
- Future readFragment() async {
- try {
- final html = await _richChannel.invokeMethod('readHtml');
- return NoteClipboardFragment.fromHtml(html);
- } catch (error) {
- debugPrint('KapyNotes: could not read rich clipboard data: $error');
- return null;
- }
- }
+ /// Everything the clipboard holds, read at once.
+ Future readSnapshot() => ClipboardSnapshot.read();
- /// 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..84ce7c9
--- /dev/null
+++ b/lib/paste/clipboard_snapshot.dart
@@ -0,0 +1,196 @@
+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;
+
+ /// The plain text as the platform gave it, Windows' line ends and all, so
+ /// it matches what the platform's own text call gives.
+ 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'),
+ 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..7a4bed0
--- /dev/null
+++ b/lib/paste/html_reader.dart
@@ -0,0 +1,1959 @@
+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.addList(list);
+ return;
+ case 'li':
+ // An item with no list around it: what a selection that starts
+ // inside a list serialises as.
+ out.flush();
+ out.addList(
+ 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();
+
+ /// Whether a line of nothing but breaks and spaces, an empty line on the
+ /// page, has come since the last list.
+ bool _emptyLine = false;
+
+ void flush() {
+ final blank = line.isBlank;
+ final inlines = line.take();
+ if (inlines.isNotEmpty) blocks.add(PasteParagraph(inlines));
+ if (blank) _emptyLine = true;
+ }
+
+ void addList(PasteList list) {
+ blocks.add(
+ _emptyLine
+ ? PasteList(
+ ordered: list.ordered,
+ items: list.items,
+ start: list.start,
+ afterEmptyLine: true,
+ )
+ : list,
+ );
+ _emptyLine = false;
+ }
+}
+
+/// 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 = [];
+
+ /// Whether the line holds something, but only breaks and spaces.
+ bool get isBlank =>
+ _inlines.isNotEmpty &&
+ _inlines.every(
+ (inline) =>
+ inline is PasteBreak ||
+ inline is PasteText && inline.text.trim().isEmpty,
+ );
+
+ 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..d1da9d6
--- /dev/null
+++ b/lib/paste/html_writer.dart
@@ -0,0 +1,239 @@
+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) {
+ PasteBlock? previous;
+ for (final block in blocks) {
+ if (block is PasteList && block.afterEmptyLine && previous is PasteList) {
+ out.write('
');
+ }
+ _block(block);
+ previous = 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..1449b4c
--- /dev/null
+++ b/lib/paste/markdown_text.dart
@@ -0,0 +1,316 @@
+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\$';
+ });
+}
+
+/// A note's own markdown read into blocks, the way the note's editor reads
+/// it: math is only ever between dollars, a `*` between two operands is
+/// multiplication, not emphasis, and 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 readNoteMarkdown(String source) {
+ final clean = source.replaceAll(_marks, '');
+ final text = _markEmptyLinesBetweenLists(clean);
+ 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))
+ ..write(text.substring(from, to));
+ cursor = to;
+ }
+ masked.write(_setMathAside(text.substring(cursor), maths));
+ html = _parser()
+ .parse(maskOperatorAsterisks(masked.toString(), mask: _operator))
+ .toHtml()
+ .replaceAllMapped(_mathToken, (match) {
+ final math = maths[int.parse(match[1]!)];
+ final kind = math.display ? 'math-display' : 'math-inline';
+ return ' ';
+ })
+ .replaceAll(String.fromCharCode(_operator), '*')
+ .replaceAll(_operatorInAddress, '*')
+ .replaceAll('$_emptyLine
', '
')
+ .replaceAll(_breakBeforeBlock, '');
+ } on Object {
+ return [
+ for (final paragraph in clean.split(RegExp(r'\n{2,}')))
+ if (paragraph.trim().isNotEmpty) PasteParagraph([PasteText(paragraph)]),
+ ];
+ }
+ return HtmlPasteReader().read(
+ '$html
',
+ );
+}
+
+/// [text] with a paragraph of [_emptyLine] where an empty line keeps a list
+/// apart from a list of the other kind after it. Markdown makes two lists
+/// of them either way, so without the mark the empty line would be lost.
+String _markEmptyLinesBetweenLists(String text) {
+ final out = [];
+ bool? ordered;
+ var afterEmptyLine = false;
+ String? fence;
+ for (final line in text.split('\n')) {
+ final trimmed = line.trimLeft();
+ if (fence != null) {
+ if (trimmed.startsWith(fence)) fence = null;
+ } else if (trimmed.isEmpty) {
+ afterEmptyLine = true;
+ } else {
+ if (line.length - trimmed.length < 2) {
+ final item = _listMarker.firstMatch(trimmed);
+ final itemOrdered = item == null ? null : item[1] == null;
+ if (itemOrdered != null &&
+ afterEmptyLine &&
+ ordered != null &&
+ itemOrdered != ordered) {
+ out
+ ..add(_emptyLine)
+ ..add('');
+ }
+ ordered = itemOrdered;
+ fence = _fenceOpen.firstMatch(trimmed)?[1];
+ }
+ afterEmptyLine = false;
+ }
+ out.add(line);
+ }
+ return out.join('\n');
+}
+
+/// A line break the markdown renderer writes between two blocks, which the
+/// note's `pre-line` would otherwise read as an empty line between them.
+final _breakBeforeBlock = RegExp(
+ r'\n(?=?(?:p|ul|ol|li|h[1-6]|blockquote|pre|table|thead|tbody|tr|th|td|hr)\b)',
+);
+final _listMarker = RegExp(r'^(?:([-*+])|\d{1,9}[.)])(?:[ \t]|$)');
+final _fenceOpen = RegExp(r'^(`{3,}|~{3,})');
+
+/// Private-use characters the text is marked with while it is read: the
+/// ends of a math token, a `*` that is an operator, and an empty line
+/// between two lists.
+final _marks = RegExp('[\uE000-\uE003]');
+final _mathToken = RegExp('\uE000(\\d+)\uE001');
+const _operator = 0xE002;
+const _emptyLine = '\uE003';
+
+/// [_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) {
+ if (!prose.contains('\$')) 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';
+ };
+ return prose
+ .replaceAllMapped(_dollarDisplayMath, aside(display: true))
+ .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..bc03f1b
--- /dev/null
+++ b/lib/paste/paste_converter.dart
@@ -0,0 +1,143 @@
+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 the markdown that goes into a
+/// markdown note.
+///
+/// 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) {
+ 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,
+ writeMarkdownPaste([
+ 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,
+ writeMarkdownPaste([PasteCode(_trimCode(text))]),
+ );
+ }
+ final blocks = reader.read(markup);
+ if (hasText && !_hasPictures(blocks) && _isMarkdownSource(text, blocks)) {
+ break;
+ }
+ if (!hasText || _hasStructure(blocks)) {
+ return PasteConversion(kind, writeMarkdownPaste(blocks));
+ }
+ }
+
+ if (!hasText) {
+ return const PasteConversion(PasteKind.nothing, PasteOutput(text: ''));
+ }
+ if (looksLikeMarkdown(text)) {
+ return PasteConversion(PasteKind.markdown, normalizeMarkdownPaste(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) {
+ final size =
+ (snapshot.html?.length ?? 0) +
+ (snapshot.rtfHtml?.length ?? 0) +
+ (snapshot.text?.length ?? 0);
+ if (size < _backgroundThreshold) {
+ return Future.value(convertPaste(snapshot));
+ }
+ return Isolate.run(() => convertPaste(snapshot));
+}
+
+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..4bcc95b
--- /dev/null
+++ b/lib/paste/paste_insertion.dart
@@ -0,0 +1,254 @@
+import 'package:flutter/services.dart' show TextSelection;
+
+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.images = const [],
+ });
+
+ final int start;
+ final int end;
+ final String text;
+
+ /// Pictures, relative to [start].
+ final List images;
+}
+
+/// Fits [output] into the markdown [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,
+ 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 (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 = _Line.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);
+ 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)) 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 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,
+ 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) {
+ final trimmed = line.trimLeft();
+ return _isPictureLine(line) ||
+ trimmed.startsWith('|') ||
+ _fence.hasMatch(line) ||
+ trimmed.startsWith(r'$$') ||
+ RegExp(r'^([-*_])(\s*\1){2,}\s*$').hasMatch(trimmed);
+}
+
+/// The structure of the line a paste lands on.
+class _Line {
+ _Line._(this._note, this._prefix);
+
+ factory _Line.at(String note, int offset) =>
+ _Line._(note, markdownLinePrefix(note, offset));
+
+ final String _note;
+ final MarkdownLinePrefix _prefix;
+
+ /// Where the line's words begin, after its structure.
+ int get contentStart => _prefix.headingEnd;
+
+ bool get isItem => _prefix.isListItem;
+
+ /// 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 =>
+ isItem || _prefix.isQuoted || _prefix.headingLevel > 0;
+
+ /// Where [continuation] ends in the note.
+ int get continuationEnd => isItem ? _prefix.markerStart : _prefix.quoteEnd;
+
+ /// What a pasted line keeps in front of it to stay in this line's
+ /// container: indentation and quote markers.
+ String get continuation =>
+ _note.substring(_prefix.lineStart, continuationEnd);
+
+ /// The marker for the item [count] after this one.
+ 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';
+ }
+
+ /// How much of a pasted [line] is its own structure, to cut when it joins
+ /// this one.
+ int structureOf(String line) =>
+ _pastedMarkdownStructure.firstMatch(line)?.end ?? 0;
+}
+
+final _pastedMarkdownStructure = RegExp(
+ r'^[ \t]*((>[ \t]?)+)?([-*+][ \t]+|\d{1,9}[.)][ \t]+)?(\[[ xX]\][ \t]+)?(#{1,6}[ \t]+)?',
+);
diff --git a/lib/paste/paste_tree.dart b/lib/paste/paste_tree.dart
new file mode 100644
index 0000000..4c97b23
--- /dev/null
+++ b/lib/paste/paste_tree.dart
@@ -0,0 +1,254 @@
+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,
+ this.afterEmptyLine = false,
+ });
+
+ final bool ordered;
+
+ /// The first item's number, for an ordered list.
+ final int start;
+ final List items;
+
+ /// Whether the source had an empty line just before this list, which
+ /// keeps it apart from a list just before that.
+ final bool afterEmptyLine;
+}
+
+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..114e8b3
--- /dev/null
+++ b/lib/paste/paste_writers.dart
@@ -0,0 +1,832 @@
+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, and a
+ // list straight after another goes on down the page, as a bulleted
+ // list running into a numbered one does: a blank line in either place
+ // would loosen what the source kept tight.
+ final tight =
+ block is PasteList &&
+ (inItem && previous is PasteParagraph ||
+ previous is PasteList && !block.afterEmptyLine);
+ 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/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 5f91754..ae19b49 100644
--- a/lib/ui/editor/markdown_syntax.dart
+++ b/lib/ui/editor/markdown_syntax.dart
@@ -685,8 +685,9 @@ class MarkdownAnalysis {
/// 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, and
- /// the `*` and `_` around emphasis — the markers only, not the words.
+ /// 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.
@@ -1402,6 +1403,11 @@ List _chunkStarts(String text) {
return starts;
}
+bool _isShortDashes(String underline) {
+ final dashes = underline.trim();
+ return dashes.length < 3 && dashes.replaceAll('-', '').isEmpty;
+}
+
bool _isIndentOrBreak(int unit) =>
unit == 0x20 || unit == 0x09 || unit == 0x0A || unit == 0x0D;
@@ -1473,7 +1479,10 @@ const _asteriskMask = 0xE000;
/// than becoming an italic four that the calculator would then be told to
/// read past. It costs `un*frigging*believable`, which is a price worth
/// paying here.
-String _maskOperatorAsterisks(String source) {
+///
+/// Each hidden `*` becomes [mask], one code unit for one, so every offset
+/// stays where it was.
+String maskOperatorAsterisks(String source, {int mask = _asteriskMask}) {
if (!source.contains('*')) return source;
List? units;
var i = 0;
@@ -1491,7 +1500,7 @@ String _maskOperatorAsterisks(String source) {
_endsOperand(source.codeUnitAt(i - 1)) &&
_startsOperand(source.codeUnitAt(end))) {
units ??= List.of(source.codeUnits);
- units.fillRange(i, end, _asteriskMask);
+ units.fillRange(i, end, mask);
}
i = end;
}
@@ -1536,7 +1545,7 @@ _Parts _parse(String source) {
);
final List nodes;
try {
- nodes = document.parse(_maskOperatorAsterisks(source));
+ nodes = document.parse(maskOperatorAsterisks(source));
} catch (error, stack) {
// A parser fault must cost the note its styling, never its text: the
// piece simply draws as plain writing.
@@ -2017,6 +2026,13 @@ class _Collector {
case 'setextHeading':
// The underline is the last marker; the heading is what it underlines.
final underline = markers.isEmpty ? null : markers.last;
+ // `-` and `--` under a line of words are a bullet or a divider being
+ // typed, not an underline: read as one, the line above would turn
+ // into a heading and back with every key.
+ if (underline != null && _isShortDashes(underline.text)) {
+ claimed.add(underline.start.offset);
+ break;
+ }
_style(
start,
underline?.start.offset ?? end,
@@ -2262,8 +2278,10 @@ class _Collector {
if (claimed.contains(marker.start.offset)) continue;
_style(marker.start.offset, marker.end.offset, MarkdownStyle.syntax);
// What is left is an escape's backslash, or a hard line break's: shown
- // with the caret against it, like any other inline marker.
+ // with the caret against it, like any other inline marker, and not
+ // read by the calculator, so `\- 5` is the minus five it shows.
if (marker.text == r'\') {
+ _record(_unread, marker.start.offset, marker.end.offset);
_conceal(
marker.start.offset,
marker.end.offset,
diff --git a/lib/ui/editor/note_editor.dart b/lib/ui/editor/note_editor.dart
index 28e77c1..764b67d 100644
--- a/lib/ui/editor/note_editor.dart
+++ b/lib/ui/editor/note_editor.dart
@@ -1,4 +1,6 @@
import 'dart:async';
+import 'dart:convert' show utf8;
+import 'dart:io' show Directory, File;
import 'dart:math' as math;
import 'dart:ui' show BoxHeightStyle, BoxWidthStyle, Locale;
@@ -52,6 +54,13 @@ import '../../data/note_attachment.dart';
import 'package:file_selector/file_selector.dart';
import '../../images/image_clipboard.dart';
+import '../../paste/clipboard_snapshot.dart';
+import '../../paste/image_download.dart';
+import '../../paste/note_text.dart';
+import '../../paste/paste_converter.dart';
+import '../../paste/paste_insertion.dart';
+import '../../paste/paste_tree.dart';
+import '../../sync/aead.dart' show randomKey;
import '../../images/image_codec.dart';
import '../../images/image_ingest.dart';
import '../../images/image_picker.dart';
@@ -490,6 +499,10 @@ class NoteEditorState extends State with WidgetsBindingObserver {
/// Bold switched on for the next word, and the like. See [MarkdownTyping].
final MarkdownTyping _markdownTyping = MarkdownTyping();
+ /// The shortcut just typed, which Backspace or Undo gives back as typed.
+ /// See [MarkdownShortcut].
+ final MarkdownShortcut _markdownShortcut = MarkdownShortcut();
+
/// Whether inline markdown stays hidden even beside the caret: while the
/// writer is typing, so markers disappear as they are completed, and until
/// they next move the caret themselves.
@@ -1180,6 +1193,7 @@ class NoteEditorState extends State with WidgetsBindingObserver {
// The writer moved the caret: anything waiting to be typed is
// off, and the syntax beside the caret may show again.
_markdownTyping.clear();
+ _markdownShortcut.clear();
_markdownQuiet = false;
}
}
@@ -1434,6 +1448,7 @@ class NoteEditorState extends State with WidgetsBindingObserver {
for (final ref in after) {
survivors[ref.hash] = (survivors[ref.hash] ?? 0) + 1;
}
+ var removed = 0;
for (final ref in before) {
final left = survivors[ref.hash] ?? 0;
if (left > 0) {
@@ -1441,8 +1456,9 @@ class NoteEditorState extends State with WidgetsBindingObserver {
continue;
}
_removedRefs.add((textBefore: textBefore, ref: ref));
+ removed++;
}
- while (_removedRefs.length > _maxRemovedRefs) {
+ while (_removedRefs.length > math.max(_maxRemovedRefs, removed)) {
_removedRefs.removeAt(0);
}
}
@@ -1529,6 +1545,18 @@ class NoteEditorState extends State with WidgetsBindingObserver {
? canIndentMarkdownSelection(_controller.value, outdent: outdent)
: canIndentSelection(_controller.value, outdent: outdent);
+ /// What Undo does straight after a markdown shortcut: gives back what was
+ /// typed. False when the last thing typed was not one.
+ bool _undoMarkdownShortcut() {
+ final typed = _markdownShortcut.typedFor(_controller.value);
+ if (typed == null || widget.readOnly || !widget.markdownEnabled) {
+ return false;
+ }
+ _markdownShortcut.clear();
+ _controller.value = typed;
+ return true;
+ }
+
/// Puts a markdown control's edit into the note.
///
/// The edit says where it moved everything, so attachments and any styles
@@ -1817,20 +1845,30 @@ class NoteEditorState extends State with WidgetsBindingObserver {
final startingValue = _editableTextState()?.textEditingValue;
final store = widget.images;
- // Kapy Notes rich HTML carries the positions of every selected image, so it
- // outranks the clipboard's native bitmap: reading only that would reduce a
- // mixed selection to its first picture and lose the text around it.
- final (capturedText, fragment, paths) = await (
- _clipboardText(),
- store == null
- ? Future.value()
- : widget.clipboard.readFragment(),
+ // A note without markdown takes its words from the platform's own text
+ // call, as it always has; a markdown note takes them from the same read
+ // as the rest of what it converts.
+ final markdown = widget.markdownEnabled;
+ final (plainText, snapshot, paths) = await (
+ markdown ? Future.value() : _clipboardText(),
+ markdown || store != null
+ ? widget.clipboard.readSnapshot()
+ : Future.value(const ClipboardSnapshot()),
store == null
? Future>.value(const [])
: widget.clipboard.readImageFiles(),
).wait;
if (!mounted) return;
+ final capturedText = markdown ? snapshot.text : plainText;
+ // A selection copied out of a note carries everything that was in it, so
+ // it outranks the rest of the clipboard: the native bitmap alone would
+ // reduce a mixed selection to its first picture, and the HTML would lose
+ // what only a note holds.
+ final fragment = store == null
+ ? null
+ : await NoteClipboardFragment.fromSnapshot(snapshot);
+ if (!mounted) return;
if (fragment != null) {
await _insertClipboardFragment(
fragment,
@@ -1840,6 +1878,29 @@ class NoteEditorState extends State with WidgetsBindingObserver {
return;
}
+ // In a markdown note, anything richer than plain words — a page, a
+ // document, an AI's answer, an editor's code — is read for what it says,
+ // as markdown, which only reads well drawn. A note without markdown
+ // pastes as it always has. Lone pictures and plain words take the paths
+ // below either way.
+ final converted = markdown
+ ? await convertPasteInBackground(snapshot)
+ : null;
+ if (!mounted) return;
+ final rich = switch (converted?.kind) {
+ null || PasteKind.text || PasteKind.nothing => null,
+ _ => converted!.output,
+ };
+ if (rich != null && rich.hasWords) {
+ _insertConvertedPaste(
+ rich,
+ cause,
+ clipboardText: capturedText,
+ startingValue: startingValue,
+ );
+ return;
+ }
+
// Raw bitmap data — a screenshot tool, a browser's "copy image".
if (store != null) {
final pasted = await widget.clipboard.readImage();
@@ -1903,10 +1964,238 @@ class NoteEditorState extends State with WidgetsBindingObserver {
await insertFiles([for (final path in paths) XFile(path)]);
return;
}
- if (!mounted || capturedText == null) return;
+ if (!mounted) return;
+ // Pictures and nothing else, from markup that only pointed at them.
+ if (rich != null && rich.images.isNotEmpty) {
+ _insertConvertedPaste(
+ rich,
+ cause,
+ clipboardText: capturedText,
+ startingValue: startingValue,
+ );
+ return;
+ }
+ if (capturedText == null) return;
+ if (converted case PasteConversion(kind: PasteKind.text, :final output)) {
+ _insertConvertedPaste(
+ output,
+ cause,
+ clipboardText: capturedText,
+ startingValue: startingValue,
+ );
+ return;
+ }
_insertPastedText(capturedText, cause, startingValue: startingValue);
}
+ /// Puts a converted paste into the note as one edit, so one undo takes all
+ /// of it back out, fitted to the line it lands on (see [planPaste]).
+ ///
+ /// Pictures go in as placeholders at once — the clipboard's own bytes, or a
+ /// quiet box while they download — and are prepared afterwards. Only their
+ /// refs change when they are ready, never the text, so they add no undo
+ /// steps of their own.
+ void _insertConvertedPaste(
+ PasteOutput output,
+ SelectionChangedCause cause, {
+ String? clipboardText,
+ TextEditingValue? startingValue,
+ }) {
+ if (widget.readOnly) return;
+ final editable = _editableTextState();
+ if (editable == null) return;
+ final store = widget.images;
+ final value = editable.textEditingValue;
+ final selection = _selectionForPaste(value, startingValue);
+ final insertion = planPaste(
+ note: value.text,
+ selection: selection,
+ output: store == null ? output.withoutPictures() : output,
+ target: _pasteTargetAt(value.text, selection.start),
+ clipboardText: clipboardText,
+ );
+ final nextText = value.text.replaceRange(
+ insertion.start,
+ insertion.end,
+ insertion.text,
+ );
+ final delta = insertion.text.length - (insertion.end - insertion.start);
+ final pictures = [
+ for (final image in insertion.images)
+ (
+ ref: _pastedImagePlaceholder(
+ image.image,
+ insertion.start + image.offset,
+ ),
+ image: image.image,
+ ),
+ ];
+ _nextAttachments = normalizeNoteAttachments([
+ for (final ref in _attachments)
+ if (ref.offset < insertion.start)
+ ref
+ else if (ref.offset >= insertion.end)
+ ref.copyWith(offset: ref.offset + delta),
+ for (final picture in pictures) picture.ref,
+ ], nextText);
+ _nextInsertedFormats = const {};
+ editable.userUpdateTextEditingValue(
+ value.copyWith(
+ text: nextText,
+ selection: TextSelection.collapsed(
+ offset: insertion.start + insertion.text.length,
+ ),
+ composing: TextRange.empty,
+ ),
+ cause,
+ );
+ _focusNode.requestFocus();
+ if (store != null && pictures.isNotEmpty) {
+ unawaited(_preparePastedImages(pictures, store));
+ }
+ }
+
+ /// What a paste may write where it lands: only words in a table, only the
+ /// clipboard's own text in a code block.
+ PasteTarget _pasteTargetAt(String text, int offset) {
+ final analysis = _controller.markdownFor(text);
+ for (final table in analysis.tables) {
+ if (offset > table.start && offset < table.end) return PasteTarget.table;
+ }
+ final fence = RegExp(r'^\s*(```|~~~)');
+ for (final block in analysis.blocks) {
+ if (block.kind != MarkdownBlockKind.code) continue;
+ final firstLineEnd = text.indexOf('\n', block.start);
+ if (firstLineEnd < 0 || firstLineEnd >= block.end) continue;
+ final fenced = fence.hasMatch(text.substring(block.start, firstLineEnd));
+ final contentStart = fenced ? firstLineEnd + 1 : block.start;
+ var contentEnd = block.end;
+ final lastLineStart = text.lastIndexOf('\n', block.end - 1) + 1;
+ if (fenced &&
+ lastLineStart > firstLineEnd &&
+ fence.hasMatch(text.substring(lastLineStart, block.end))) {
+ contentEnd = lastLineStart - 1;
+ }
+ if (offset >= contentStart && offset <= contentEnd) {
+ return PasteTarget.code;
+ }
+ }
+ return PasteTarget.text;
+ }
+
+ /// What holds a pasted picture's place until it is prepared: the bytes
+ /// themselves when the clipboard carried them, and otherwise a quiet box
+ /// that remembers where to fetch them from.
+ NoteImageRef _pastedImagePlaceholder(PasteImage image, int offset) {
+ final bytes = image.bytes;
+ final width = (image.width ?? 0).round();
+ final height = (image.height ?? 0).round();
+ final sized = width > 0 && height > 0;
+ return NoteImageRef(
+ offset: offset,
+ hash: BlobStore.hashOf(
+ bytes ?? Uint8List.fromList(utf8.encode(image.src ?? '')),
+ ),
+ key: randomKey(),
+ mime: image.mime ?? 'image/png',
+ width: sized ? width : 4,
+ height: sized ? height : 3,
+ bytes: bytes?.length ?? 0,
+ widthFactor: image.widthFactor,
+ isPreparing: true,
+ previewBytes: bytes,
+ src: bytes == null ? image.src : null,
+ );
+ }
+
+ /// Fetches what pasted pictures still need and prepares them in order:
+ /// downloads run side by side, compression one at a time.
+ Future _preparePastedImages(
+ List<({NoteImageRef ref, PasteImage image})> pictures,
+ BlobStore store,
+ ) async {
+ final fetched = [
+ for (final picture in pictures)
+ picture.image.hasBytes
+ ? Future.value((
+ bytes: picture.image.bytes!,
+ mime: picture.image.mime ?? 'image/png',
+ ))
+ : PastedImageDownloader.shared.fetch(picture.ref.src ?? ''),
+ ];
+ for (var i = 0; i < pictures.length; i++) {
+ final placeholder = pictures[i].ref;
+ final source = await fetched[i];
+ final staged = source == null
+ ? null
+ : (await stageImage(
+ source: source.bytes,
+ sourceMime: source.mime,
+ )).staged;
+ if (staged == null) {
+ // Nothing usable came back. The place stays, with the address to try
+ // again from.
+ _publishPreparedImage(
+ placeholder,
+ placeholder.copyWith(isPreparing: false, previewBytes: null),
+ );
+ continue;
+ }
+ final local = NoteImageRef(
+ offset: placeholder.offset,
+ hash: staged.ref.hash,
+ key: placeholder.key,
+ mime: staged.ref.mime,
+ width: staged.ref.width,
+ height: staged.ref.height,
+ bytes: staged.ref.bytes,
+ widthFactor: placeholder.widthFactor,
+ isPreparing: true,
+ previewBytes: staged.source,
+ );
+ final keyed = StagedImage(
+ ref: local,
+ source: staged.source,
+ sourceMime: staged.sourceMime,
+ );
+ try {
+ await store.put(keyed.source);
+ _publishPreparedImage(placeholder, local);
+ final result = await (widget.imageFinalizer ?? _finishStagedImage)(
+ keyed,
+ store,
+ );
+ final ready = result.isOk && result.image!.ref is NoteImageRef
+ ? result.image!.ref as NoteImageRef
+ : await storeStagedImageAsIs(keyed, store: store);
+ _publishPreparedImage(local, ready);
+ } catch (error, stack) {
+ FlutterError.reportError(
+ FlutterErrorDetails(
+ exception: error,
+ stack: stack,
+ library: 'Kapy Notes editor',
+ context: ErrorDescription('while preparing a pasted image'),
+ ),
+ );
+ }
+ }
+ }
+
+ /// Tries a pasted picture's download again.
+ void _retryPastedImage(NoteImageRef ref) {
+ final store = widget.images;
+ final address = ref.src;
+ if (widget.readOnly || store == null || address == null) return;
+ final loading = ref.copyWith(isPreparing: true);
+ _publishPreparedImage(ref, loading);
+ unawaited(
+ _preparePastedImages([
+ (ref: loading, image: PasteImage(src: address)),
+ ], store),
+ );
+ }
+
/// Pastes only the clipboard's plain-text representation.
///
/// Normal Paste understands Kapy Notes fragments and images. This path
@@ -1947,17 +2236,49 @@ class NoteEditorState extends State with WidgetsBindingObserver {
Future _clipboardStillHolds(String? capturedText) async =>
await _clipboardText() == capturedText;
+ /// Puts a selection copied out of a note back as it was: its words, their
+ /// styles and every attachment it carried, stored here anew, all as one
+ /// edit and so one undo step. A selection from a note of the other kind is
+ /// rewritten for this one on the way in.
Future _insertClipboardFragment(
NoteClipboardFragment fragment,
SelectionChangedCause cause, {
TextEditingValue? startingValue,
}) async {
final store = widget.images;
- if (store == null || fragment.images.isEmpty) return;
+ if (store == null) return;
+ var body = fragment.body;
+ var formats = fragment.formats;
+ Map? anchors;
+ final markdown = fragment.markdown;
+ if (markdown != null && markdown != widget.markdownEnabled) {
+ final converted = convertNote(
+ body,
+ toMarkdown: widget.markdownEnabled,
+ formats: formats,
+ );
+ body = converted.text;
+ formats = converted.formats;
+ anchors = converted.anchors;
+ }
+ if (fragment.images.isEmpty && fragment.attachments.isEmpty) {
+ _replaceSelectionWithFragment(
+ body,
+ const [],
+ formats,
+ cause,
+ startingValue: startingValue,
+ );
+ return;
+ }
if (!_beginImageAction()) return;
- final progress = Toast.showProgress(context, 'Pasting with images…');
+ final picturesOnly = fragment.attachments.isEmpty;
+ final progress = Toast.showProgress(
+ context,
+ picturesOnly ? 'Pasting with images…' : 'Pasting with attachments…',
+ );
try {
- final incoming = [];
+ final incoming = [];
for (final image in fragment.images) {
final result = await ingestImage(
source: image.bytes,
@@ -1979,16 +2300,36 @@ class NoteEditorState extends State with WidgetsBindingObserver {
),
);
}
+ for (final attachment in fragment.attachments) {
+ final ref = await _storeCarriedAttachment(attachment, store);
+ if (!mounted) {
+ progress.dismiss();
+ return;
+ }
+ if (ref == null) {
+ progress.error('Could not paste one of those attachments');
+ return;
+ }
+ incoming.add(ref);
+ }
if (!_replaceSelectionWithFragment(
- fragment.body,
- incoming,
+ body,
+ [
+ for (final ref in incoming)
+ if ((anchors == null ? ref.offset : anchors[ref.offset])
+ case final at?)
+ ref.copyWith(offset: at),
+ ],
+ formats,
cause,
startingValue: startingValue,
)) {
progress.error('Could not paste that selection');
return;
}
- progress.success('Pasted with images');
+ progress.success(
+ picturesOnly ? 'Pasted with images' : 'Pasted with attachments',
+ );
} catch (error, stack) {
FlutterError.reportError(
FlutterErrorDetails(
@@ -2004,6 +2345,74 @@ class NoteEditorState extends State with WidgetsBindingObserver {
}
}
+ /// Stores a file, voice note or video a paste carried, and returns its ref
+ /// at its place in the fragment, or null when it could not be stored.
+ ///
+ /// A file or a video goes in the way a dropped one does, from a copy on
+ /// disk under its own name, so it is held to the same limits and a video
+ /// this device cannot play is refused. A voice note is stored as a
+ /// recording is, keeping its transcript, summary and posts.
+ Future _storeCarriedAttachment(
+ ClipboardFragmentAttachment attachment,
+ BlobStore store,
+ ) async {
+ final copied = attachment.ref;
+ if (copied is NoteVoiceRef) {
+ return NoteVoiceRef(
+ offset: copied.offset,
+ hash: await store.put(
+ attachment.bytes,
+ extension: NoteVoiceRef.voiceExtension,
+ ),
+ key: randomKey(),
+ mime: copied.mime,
+ bytes: attachment.bytes.length,
+ durationMs: copied.durationMs,
+ peaks: copied.peaks,
+ transcript: copied.transcript,
+ summary: copied.summary,
+ takes: copied.takes,
+ );
+ }
+ final name = switch (copied) {
+ NoteFileRef(:final name) => name,
+ NoteVideoRef(:final extension) => 'video$extension',
+ _ => null,
+ };
+ if (name == null) return null;
+ final attachmentMaxBytes =
+ widget.videoAttachmentMaxBytes?.call() ?? freeAttachmentMaxBytes;
+ final directory = await Directory.systemTemp.createTemp('kapy-paste');
+ try {
+ final path = '${directory.path}/$name';
+ await File(path).writeAsBytes(attachment.bytes);
+ final files = [XFile(path, mimeType: copied.mime)];
+ if (copied is NoteVideoRef) {
+ final batch = widget.videoIngestor == null
+ ? await ingestVideoFiles(
+ files,
+ store: store,
+ attachmentMaxBytes: attachmentMaxBytes,
+ )
+ : await widget.videoIngestor!(files, store);
+ return batch.videos.firstOrNull?.copyWith(
+ offset: copied.offset,
+ widthFactor: copied.widthFactor,
+ );
+ }
+ final batch = widget.fileIngestor == null
+ ? await ingestFileAttachments(
+ files,
+ store: store,
+ attachmentMaxBytes: attachmentMaxBytes,
+ )
+ : await widget.fileIngestor!(files, store);
+ return batch.files.firstOrNull?.copyWith(offset: copied.offset);
+ } finally {
+ await directory.delete(recursive: true);
+ }
+ }
+
TextSelection _selectionForPaste(
TextEditingValue value,
TextEditingValue? startingValue,
@@ -2022,7 +2431,8 @@ class NoteEditorState extends State with WidgetsBindingObserver {
bool _replaceSelectionWithFragment(
String body,
- List images,
+ List attachments,
+ List formats,
SelectionChangedCause cause, {
TextEditingValue? startingValue,
}) {
@@ -2042,9 +2452,25 @@ class NoteEditorState extends State with WidgetsBindingObserver {
ref
else if (ref.offset >= selection.end)
ref.copyWith(offset: ref.offset + delta),
- for (final image in images)
- image.copyWith(offset: selection.start + image.offset),
+ for (final ref in attachments)
+ ref.copyWith(offset: selection.start + ref.offset),
], nextText);
+ if (formats.isNotEmpty) {
+ _nextFormats = normalizeNoteFormats([
+ ...rebaseNoteFormats(
+ oldText: value.text,
+ newText: nextText,
+ formats: _formats,
+ insertedFormats: const {},
+ ),
+ for (final range in formats)
+ NoteFormatRange(
+ start: selection.start + range.start,
+ end: selection.start + range.end,
+ format: range.format,
+ ),
+ ], nextText.length);
+ }
_nextInsertedFormats = const {};
editable.userUpdateTextEditingValue(
value.copyWith(
@@ -3414,6 +3840,7 @@ class NoteEditorState extends State with WidgetsBindingObserver {
// what is drawn changes with focus.
if (widget.markdownEnabled && mounted) {
_markdownTyping.clear();
+ _markdownShortcut.clear();
setState(() => _markdownQuiet = true);
}
if (_focusNode.hasFocus) {
@@ -3888,9 +4315,9 @@ class NoteEditorState extends State with WidgetsBindingObserver {
onChecklistPressed: _toggleChecklist,
onOpenLink: link == null ? null : () => unawaited(_openLink(link)),
onCopyLink: link == null ? null : () => unawaited(_copyLink(link)),
- onCopy: _selectionContainsImage(selection)
- ? () => unawaited(_copyRichSelection(selection))
- : null,
+ onCopy: widget.images == null
+ ? null
+ : () => unawaited(_copyRichSelection(selection)),
// The same rule as the menu's Paste: through the editor wherever a
// picture could be on the clipboard, and the field's own paste where
// one could not.
@@ -4069,16 +4496,6 @@ class NoteEditorState extends State with WidgetsBindingObserver {
_focusNode.requestFocus();
}
- bool _selectionContainsImage(TextSelection selection) =>
- selection.isValid &&
- !selection.isCollapsed &&
- _attachments.any(
- (ref) =>
- ref is NoteImageRef &&
- ref.offset >= selection.start &&
- ref.offset < selection.end,
- );
-
/// Makes a picture a real one-character editor selection.
///
/// A selection that already includes it is preserved, which is what makes
@@ -4099,15 +4516,16 @@ class NoteEditorState extends State with WidgetsBindingObserver {
}
void _copyImageFromMenu(NoteImageRef ref) {
+ ContextMenuController.removeAny();
_selectImage(ref);
unawaited(_copyRichSelection(_controller.selection));
}
- Future _clipboardBytes(NoteImageRef ref) async {
+ Future _clipboardBytes(NoteAttachmentRef ref) async {
final store = widget.images;
if (store == null) return null;
var bytes = await store.read(ref.hash);
- if (bytes != null) return bytes;
+ if (bytes != null || ref is! NoteImageRef) return bytes;
final fetch = widget.imageFetch;
if (fetch == null) return null;
bytes = await fetch(ref.hash);
@@ -4116,45 +4534,38 @@ class NoteEditorState extends State with WidgetsBindingObserver {
return bytes;
}
- /// Materialises only the images inside [selection], preserving their exact
- /// positions among the selected text. Non-image attachments become readable
- /// labels because this clipboard contract is intentionally image-specific.
+ /// Everything in [selection] a paste needs to rebuild it: its words, the
+ /// styles over them, every picture with its bytes, and with
+ /// [withAttachments] its files, voice notes and videos with theirs. An
+ /// attachment that does not travel becomes the label it pastes as. Null
+ /// when a picture's bytes cannot be had.
Future _clipboardFragment(
- TextSelection selection,
- ) async {
- if (!selection.isValid || selection.isCollapsed) return null;
+ TextSelection selection, {
+ required bool withAttachments,
+ }) async {
final text = _controller.text;
- if (selection.start < 0 || selection.end > text.length) return null;
-
- final selected = [
- for (final ref in _attachments)
- if (ref.offset >= selection.start && ref.offset < selection.end) ref,
- ]..sort((a, b) => a.offset.compareTo(b.offset));
- final selectedImages = selected.whereType().toList();
- if (selectedImages.isEmpty ||
- selectedImages.length > maxClipboardFragmentImages ||
- selectedImages.fold(0, (sum, ref) => sum + ref.bytes) >
- maxClipboardFragmentBytes) {
- return null;
- }
-
+ final refs = {for (final ref in _attachments) ref.offset: ref};
final body = StringBuffer();
final images = [];
+ final attachments = [];
+ final labels = <({int at, int growth})>[];
var cursor = selection.start;
- for (final ref in selected) {
- body.write(
- text
- .substring(cursor, ref.offset)
- .replaceAll(NoteAttachmentRef.placeholder, '[Attachment]'),
- );
+ for (
+ var at = text.indexOf(NoteAttachmentRef.placeholder, cursor);
+ at >= 0 && at < selection.end;
+ at = text.indexOf(NoteAttachmentRef.placeholder, at + 1)
+ ) {
+ body.write(text.substring(cursor, at));
+ cursor = at + 1;
+ final ref = refs[at];
+ final offset = body.length;
if (ref is NoteImageRef) {
final bytes = await _clipboardBytes(ref);
if (bytes == null) return null;
- final relativeOffset = body.length;
body.write(NoteAttachmentRef.placeholder);
images.add(
ClipboardFragmentImage(
- offset: relativeOffset,
+ offset: offset,
bytes: bytes,
mime: ref.mime,
width: ref.width,
@@ -4162,36 +4573,68 @@ class NoteEditorState extends State with WidgetsBindingObserver {
widthFactor: ref.widthFactor,
),
);
- } else if (ref is NoteVoiceRef) {
- body.write('[Voice note]');
- } else {
- body.write('[Attachment]');
+ continue;
}
- cursor = ref.offset + 1;
- }
- body.write(
- text
- .substring(cursor, selection.end)
- .replaceAll(NoteAttachmentRef.placeholder, '[Attachment]'),
+ if (withAttachments && ref != null && ref is! NoteUnknownRef) {
+ final bytes = await _clipboardBytes(ref);
+ if (bytes != null) {
+ body.write(NoteAttachmentRef.placeholder);
+ attachments.add(
+ ClipboardFragmentAttachment(
+ ref: ref.copyWith(offset: offset),
+ bytes: bytes,
+ ),
+ );
+ continue;
+ }
+ }
+ final label = ref == null
+ ? '[Attachment]'
+ : NoteClipboardFragment.labelFor(ref);
+ body.write(label);
+ labels.add((at: at, growth: label.length - 1));
+ }
+ body.write(text.substring(cursor, selection.end));
+
+ int moved(int offset) =>
+ offset -
+ selection.start +
+ labels
+ .where((label) => label.at < offset)
+ .fold(0, (sum, label) => sum + label.growth);
+ return NoteClipboardFragment(
+ body: body.toString(),
+ images: images,
+ attachments: attachments,
+ formats: normalizeNoteFormats([
+ for (final range in _formats)
+ if (range.end > selection.start && range.start < selection.end)
+ NoteFormatRange(
+ start: moved(math.max(range.start, selection.start)),
+ end: moved(math.min(range.end, selection.end)),
+ format: range.format,
+ ),
+ ], body.length),
+ markdown: widget.markdownEnabled,
);
- return NoteClipboardFragment(body: body.toString(), images: images);
}
+ /// Copies [selection] in every form a clipboard holds at once: its
+ /// markdown, or its text in a note without markdown; its HTML; and Kapy
+ /// Notes' own fragment, which puts back every style and attachment when it
+ /// is pasted into a note.
Future _copyRichSelection(TextSelection selection) async {
- ContextMenuController.removeAny();
if (!selection.isValid ||
selection.isCollapsed ||
selection.start < 0 ||
selection.end > _controller.text.length) {
return false;
}
- final selectedImages = [
+ final selected = [
for (final ref in _attachments)
- if (ref is NoteImageRef &&
- ref.offset >= selection.start &&
- ref.offset < selection.end)
- ref,
+ if (ref.offset >= selection.start && ref.offset < selection.end) ref,
];
+ final selectedImages = selected.whereType().toList();
final selectionBytes = selectedImages.fold(
0,
(sum, ref) => sum + ref.bytes,
@@ -4210,10 +4653,19 @@ class NoteEditorState extends State