Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion specs/wonky_write/11-wonky-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ covered by the frame backgrounds), from the component's selection rectangles, on
line. `PillSpacingStylePhase` adds a presentation-only `PillGapAttribution` (letter spacing,
never saved): 0.45 em after a pill's last character, 0.6 em on a space next to a pill and
1.05 em after an arrow link's last character, where its ↗ is painted; so neighbouring pills
never touch. Line heights inside callouts grow to fit button pills.
never touch. A pill reaches 0.55 em left of its first character (into the widened space
before it), so a left-to-right, start-aligned paragraph, heading or quote line whose text
starts with a chip or button is inset by 0.55 em (`leadingPillInset`, inside the quote bar and
the step badge): the pill starts at the text margin instead of hanging past it. The em of a
pill is the font size of its first character's text style, the same for the inset and the
painter. Line heights inside callouts grow to fit button pills.

### 7.3 Details collapse

Expand Down
69 changes: 43 additions & 26 deletions wonky_write/lib/features/editor/components/inline_pills.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import 'dart:math' as math;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shad/shad.dart' show LucideIcons;
import 'package:super_editor/super_editor.dart';

import '../attributions/wonky_attributions.dart';
import '../styles/editor_theme.dart';

/// A pill's padding left of its first character, in em (the right padding
/// is its last character's letter spacing, [PillGapAttribution.pill]).
const pillPaddingLeft = 0.55;

/// The inset of a left-to-right, start-aligned paragraph whose text starts
/// with a chip or button (Write 11 §7.2): the pill's left padding, so the
/// pill starts at the text margin instead of hanging over it (and over the
/// page or a container's edge). Zero for any other paragraph.
EdgeInsets leadingPillInset(ParagraphComponentViewModel viewModel) {
final text = viewModel.text;
if (text.isEmpty ||
viewModel.textDirection != TextDirection.ltr ||
!const {
TextAlign.left,
TextAlign.start,
TextAlign.justify,
}.contains(viewModel.textAlignment)) {
return EdgeInsets.zero;
}
final attributions = text.getAllAttributionsAt(0);
if (!attributions.any(
(a) => a is ChipAttribution || (a is LinkStyleAttribution && a.isButton),
)) {
return EdgeInsets.zero;
}
final em = viewModel.textStyleBuilder(attributions).fontSize ?? 14.67;
return EdgeInsets.only(left: em * pillPaddingLeft);
}

/// Paints the Wonky Markdown inline elements of a text component (Write 11
/// §7.2): the pills behind chips and buttons and the arrow after arrow
/// links, behind the text and above a container's background. The text
Expand Down Expand Up @@ -43,14 +71,21 @@ class InlinePillComponentBuilder implements ComponentBuilder {
range: SpanRange(0, text.length),
)) {
final a = span.attribution;
// The font size at the pill's first character, as [leadingPillInset]
// reads it (the editor's text isn't scaled).
double em() =>
viewModel
.textStyleBuilder(text.getAllAttributionsAt(span.start))
.fontSize ??
14.67;
pills.add(switch (a) {
ChipAttribution() => _PillSpan(
span.start,
span.end,
fill: style.chipBackground,
radius: style.chipRadius,
padY: 0.28,
lineHeight: 1.8,
em: em(),
),
LinkStyleAttribution(isButton: true) => _PillSpan(
span.start,
Expand All @@ -60,7 +95,7 @@ class InlinePillComponentBuilder implements ComponentBuilder {
: style.buttonBackground,
radius: style.buttonRadius,
padY: 0.5,
lineHeight: 2.3,
em: em(),
),
_ => _PillSpan(span.start, span.end, arrow: style.arrowColor),
});
Expand All @@ -81,7 +116,7 @@ class _PillSpan {
this.fill,
this.radius = 0,
this.padY = 0,
this.lineHeight = 1,
this.em = 0,
this.arrow,
});

Expand All @@ -96,9 +131,8 @@ class _PillSpan {
/// Vertical padding of the pill in em.
final double padY;

/// The line height of the pill's text (`write_stylesheet.dart`): its
/// character boxes are this many em high.
final double lineHeight;
/// The font size of the pill's text in px, the unit of its padding.
final double em;

/// The arrow color of an arrow link.
final Color? arrow;
Expand Down Expand Up @@ -164,11 +198,11 @@ class _PillPainter extends CustomPainter {
final rects = _lineRects(component, composable, pill.start, pill.end + 1);
if (rects.isEmpty) continue;
if (pill.fill != null) {
final em = _emAt(component, pill, rects.first);
final em = pill.em;
for (final r in rects) {
final rect = r.shift(origin);
final pillRect = Rect.fromLTRB(
rect.left - em * 0.55,
rect.left - em * pillPaddingLeft,
rect.top - em * pill.padY,
// The last character's letter spacing (0.45 em) is in `rect`.
rect.right - em * 0.05,
Expand Down Expand Up @@ -207,23 +241,6 @@ class _PillPainter extends CustomPainter {
}
}

/// The font size at the pill's first character: its caret is 1.2 em high
/// (SE-11; the character boxes take the pill text's taller line height).
/// On Android and iOS, where the caret spans the line, from the box.
static double _emAt(DocumentComponent component, _PillSpan pill, Rect box) {
final start = pill.start;
final mobile =
defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS;
if (!mobile) {
final edge = component.getEdgeForPosition(
TextNodePosition(offset: start + 1),
);
if (edge.height > 0) return edge.height / 1.2;
}
return box.height / pill.lineHeight;
}

/// Per-line rectangles of `[from, to)` in the component's coordinates.
static List<Rect> _lineRects(
DocumentComponent component,
Expand Down
11 changes: 9 additions & 2 deletions wonky_write/lib/features/editor/components/wonky_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import '../model/embeds.dart';
import '../styles/editor_theme.dart';
import 'editor_services.dart';
import 'image_size_cache.dart';
import 'inline_pills.dart';

/// Stable widget keys (specs/15 §7).
abstract final class WonkyEditorKeys {
Expand Down Expand Up @@ -808,13 +809,19 @@ class WonkyParagraphComponentBuilder implements ComponentBuilder {
// styler renders as inline code (pill background, smaller size, one
// color over the syntax colors): the block's look comes from the
// stylesheet's code rule and the box instead.
final component = _inner.createComponent(
final text = _inner.createComponent(
componentContext,
blockType == codeAttribution
? (viewModel.copy()..blockType = null)
: viewModel,
);
if (component == null) return null;
if (text == null) return null;
// Inside the quote bar and the step badge: they stay where they are.
final pillInset = leadingPillInset(viewModel);
final component =
blockType == codeAttribution || pillInset == EdgeInsets.zero
? text
: Padding(padding: pillInset, child: text);
if (blockType == blockquoteAttribution) {
return quoteBar(
componentContext.context,
Expand Down
161 changes: 161 additions & 0 deletions wonky_write/test/features/editor/components/inline_pills_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import 'package:dart_quill_delta/dart_quill_delta.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:super_editor/super_editor.dart';
import 'package:super_editor/super_editor_test.dart';
import 'package:wonky_write/features/editor/commands/write_editor_commands.dart';
import 'package:wonky_write/features/editor/controller/editor_types.dart';
import 'package:wonky_write_core/wonky_write_core.dart' show MarkdownFlavor;

import '../ui/wonky_editor_test.dart' show pumpEditor;

DocumentPosition _at(String nodeId, int offset) => DocumentPosition(
nodeId: nodeId,
nodePosition: TextNodePosition(offset: offset),
);

/// The global left edge of the character at [offset] in [nodeId].
double _charLeft(String nodeId, int offset) {
final layout = SuperEditorInspector.findDocumentLayout();
final rect = layout.getRectForSelection(
_at(nodeId, offset),
_at(nodeId, offset + 1),
)!;
return layout.getGlobalOffsetFromDocumentOffset(rect.topLeft).dx;
}

/// The global rectangles of the pills the pill layer paints (Write 11 §7.2).
List<Rect> _pills(WidgetTester tester) {
final rects = <Rect>[];
final painters = find.byWidgetPredicate(
(w) =>
w is CustomPaint && w.painter.runtimeType.toString() == '_PillPainter',
);
for (final element in painters.evaluate()) {
final box = element.renderObject! as RenderBox;
expect(
box,
paints..everything((method, arguments) {
if (method == #drawRRect) {
final r = (arguments.first as RRect).outerRect;
rects.add(
Rect.fromPoints(
box.localToGlobal(r.topLeft),
box.localToGlobal(r.bottomRight),
),
);
}
return true;
}),
);
}
return rects;
}

void main() {
final macOS = TargetPlatformVariant.only(TargetPlatform.macOS);

for (final brightness in Brightness.values) {
for (final (name, attributes) in [
('button', {'link': 'https://x.dev', 'linkStyle': 'button'}),
(
'secondary button',
{'link': 'https://x.dev', 'linkStyle': 'buttonSecondary'},
),
('chip', {'chip': true}),
]) {
testWidgets(
'Given a $name at the start of a line (${brightness.name}) then its '
'pill starts at the text margin and its label is inset (T-24)',
(tester) async {
final h = await pumpEditor(
tester,
Delta()
..insert('Docs', attributes)
..insert(' and more\nPlain line\n'),
brightness: brightness,
);
final pillLine = h.controller.document.getNodeAt(0)!.id;
final plain = h.controller.document.getNodeAt(1)!.id;
final margin = _charLeft(plain, 0);

final pill = _pills(tester).single;

expect(pill.left, moreOrLessEquals(margin, epsilon: 0.5));
expect(_charLeft(pillLine, 0), greaterThan(margin + 5));
},
variant: macOS,
);
}
}

testWidgets(
'Given a button in the middle of a line then the line\'s text stays at '
'the margin and the pill sits in the space before it',
(tester) async {
final h = await pumpEditor(
tester,
Delta()
..insert('See ')
..insert('Docs', {'link': 'https://x.dev', 'linkStyle': 'button'})
..insert('\nPlain line\n'),
);
final pillLine = h.controller.document.getNodeAt(0)!.id;
final plain = h.controller.document.getNodeAt(1)!.id;
final margin = _charLeft(plain, 0);

final pill = _pills(tester).single;

expect(_charLeft(pillLine, 0), moreOrLessEquals(margin, epsilon: 0.5));
expect(pill.left, greaterThan(_charLeft(pillLine, 3)));
expect(pill.left, lessThan(_charLeft(pillLine, 4)));
},
variant: macOS,
);

testWidgets(
'Given a link at the start of a line when it becomes a button, is undone '
'and redone then the line is at the margin without the pill and the pill '
'at the margin with it',
(tester) async {
final h = await pumpEditor(
tester,
Delta()
..insert('Docs', {'link': 'https://x.dev'})
..insert(' and more\nPlain line\n'),
);
h.controller.markdownFlavor = () => MarkdownFlavor.wonky;
final commands = WriteEditorCommands(h.controller);
final line = h.controller.document.getNodeAt(0)!.id;
final plain = h.controller.document.getNodeAt(1)!.id;
final margin = _charLeft(plain, 0);
Future<void> expectButton(bool button) async {
await tester.pumpAndSettle();
final pills = _pills(tester);
if (button) {
expect(pills.single.left, moreOrLessEquals(margin, epsilon: 0.5));
expect(_charLeft(line, 0), greaterThan(margin + 5));
} else {
expect(pills, isEmpty);
expect(_charLeft(line, 0), moreOrLessEquals(margin, epsilon: 0.5));
}
}

h.select(1);
expect(commands.setLinkStyle('button'), isTrue);
await expectButton(true);

final change = h.changes.last;
h.undo(change);
await expectButton(false);

h.controller.applyLocal(
change.delta,
label: 'Redo',
source: LocalChangeSource.undo,
);
await expectButton(true);
},
variant: macOS,
);
}
Loading