-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathcode_preview.dart
More file actions
75 lines (66 loc) · 2.23 KB
/
code_preview.dart
File metadata and controls
75 lines (66 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2025 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:jaspr/dom.dart';
import 'package:jaspr/jaspr.dart';
import 'package:jaspr_content/jaspr_content.dart';
import '../../util.dart';
import 'wrapped_code_block.dart';
/// A component that displays a preview area alongside a code block.
///
/// The `<CodePreview>` component takes in standard markdown content, but
/// expects at least two children, with the last child being a code block.
/// Any content before the last child is treated as the preview area.
class CodePreview extends CustomComponent {
const CodePreview() : super.base();
@override
Component? create(Node node, NodesBuilder builder) {
if (node case ElementNode(
tag: 'CodePreview',
:final attributes,
:final children?,
)) {
if (children.length < 2) {
throw Exception('CodePreview requires at least two child elements.');
}
final lastChild = children.last;
if (lastChild is! ComponentNode ||
lastChild.component is! WrappedCodeBlock) {
throw Exception(
'The last child of CodePreview must be a code block.',
);
}
final previewChildren = <Node>[];
for (var i = 0; i < children.length - 1; i++) {
if (children[i] case ElementNode(tag: 'p', :final children?)) {
// Unwrap paragraph nodes to avoid extra spacing.
previewChildren.addAll(children);
} else {
previewChildren.add(children[i]);
}
}
final direction = attributes['direction'] ?? 'column';
final previewColor = attributes['previewcolor'];
return div(
classes: 'code-preview',
attributes: {'data-direction': direction},
[
div(
classes: [
'preview-area',
if (previewColor != null) 'fixed-bg',
].toClasses,
styles: previewColor != null
? Styles(backgroundColor: Color(previewColor))
: null,
[
builder.build(previewChildren),
],
),
lastChild.component,
],
);
}
return null;
}
}