Skip to content

Commit dedc2ab

Browse files
committed
Add additional inline comments and fix error message
1 parent 6455b04 commit dedc2ab

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

pkgs/inject_dartpad/example/inject_dartpad_example.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@ import 'package:inject_dartpad/inject_dartpad.dart';
22
import 'package:web/web.dart' as web;
33

44
void main() async {
5+
// Create the embedded DartPad instance manager.
56
final dartPad = EmbeddedDartPad.create(
67
iframeId: 'my-dartpad',
78
theme: DartPadTheme.light,
89
);
910

11+
// Initialize the embedded DartPad.
1012
await dartPad.initialize(
1113
addToDocument: (iframe) {
14+
// Add any extra styles or attributes to the created iframe.
1215
iframe.style.height = '560';
1316

17+
// Add the iframe to the document body.
18+
// This is necessary for the embed to load.
1419
web.document.body!.append(iframe);
1520
},
1621
);
1722

18-
dartPad.updateCode('''
23+
// After awaiting initialization, you can update the code in the DartPad.
24+
dartPad.updateCode(r'''
1925
void main() {
20-
print("Hello, I'm Dash!");
26+
print('Hello, I am Dash!');
2127
}''');
2228
}

pkgs/inject_dartpad/lib/inject_dartpad.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,20 @@ final class EmbeddedDartPad {
114114
}) async {
115115
if (_initialized) return;
116116

117+
// Start listening for the 'ready' message from the embedded DartPad.
117118
late final JSExportedDartFunction readyHandler;
118119
readyHandler = (web.MessageEvent event) {
119120
if (event.data case _EmbedReadyMessage(type: 'ready', :final sender?)) {
121+
// Verify the message is sent from the corresponding iframe,
122+
// in case there are multiple DartPads being embedded at the same time.
120123
if (sender != iframeId) {
121124
return;
122125
}
123126

124127
web.window.removeEventListener('message', readyHandler);
125128
if (!_initialized) {
129+
// Signal to the caller that the DartPad is ready
130+
// for Dart code to be injected.
126131
_initializedCompleter.complete();
127132
}
128133
}
@@ -136,6 +141,9 @@ final class EmbeddedDartPad {
136141
..name = iframeId
137142
..loading = 'lazy'
138143
..allow = 'clipboard-write';
144+
145+
// Give the caller a chance to modify other attributes of the iframe and
146+
// attach it to their desired location in the document.
139147
addToDocument(iframe);
140148

141149
await _initializedCompleter.future;

pkgs/inject_dartpad/lib/inject_dartpad.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ try{r=s
15851585
r.a=A.H(r.a,a,", ")}finally{$.p.pop()}s.a+=c
15861586
r=s.a
15871587
return r.charCodeAt(0)==0?r:r},
1588+
mp(a){A.qw(a)},
15881589
Hh(a,b,c){var s,r,q,p,o,n,m="IPv4 address should contain exactly 4 parts",l="each part must be in the range 0..255",k=new A.cS(a),j=new Uint8Array(4)
15891590
for(s=b,r=s,q=0;s<c;++s){p=a.charCodeAt(s)
15901591
if(p!==46){if((p^48)>9)k.$2("invalid character",s)}else{if(q===3)k.$2(m,s)
@@ -1866,7 +1867,8 @@ var $async$E=A.l(function(a,b){if(a===1)return A.x(b,r)
18661867
while(true)switch(s){case 0:o=v.G.document.querySelectorAll("pre > code[data-dartpad]:only-child")
18671868
n=A.J([],t.F)
18681869
for(q=0;q<o.length;++q){p=o.item(q)
1869-
n.push(A.f(p==null?A.A(p):p,q))}s=2
1870+
if(p==null)p=A.A(p)
1871+
n.push(A.f(p,"embedded-dartpad-"+q))}s=2
18701872
return A.j(A.v(n,t.U),$async$E)
18711873
case 2:return A.y(null,r)}})
18721874
return A.D($async$E,r)},
@@ -1912,7 +1914,7 @@ a1=new A.Dn(h,g,m&&B.xB.nC(a0,"//")?"":f,c,a0,e,d).gnD()
19121914
k=new A.vs($.X3,t.D)
19131915
a2=new A.NG(o,a1,new A.Zf(k,t.h))
19141916
s=3
1915-
return A.j(a2.qw(new A.i6(a5,a4)),$async$f)
1917+
return A.j(a2.qw(new A.i6(a5,a4,p)),$async$f)
19161918
case 3:if((k.a&30)===0)A.vh(A.PV("EmbeddedDartPad.initialize must be called and awaited before updating the embedded source code."))
19171919
a3=v.G.document.getElementById(o)
19181920
if(a3==null)A.vh(A.PV("Failed to find iframe with an id of "+o+" in the document. Have you added the iframe to the document?"))
@@ -1924,8 +1926,9 @@ s=1
19241926
break
19251927
case 1:return A.y(q,r)}})
19261928
return A.D($async$f,r)},
1927-
i6:function i6(a,b){this.a=a
1928-
this.b=b},
1929+
i6:function i6(a,b,c){this.a=a
1930+
this.b=b
1931+
this.c=c},
19291932
qw(a){if(typeof dartPrint=="function"){dartPrint(a)
19301933
return}if(typeof console=="object"&&typeof console.log!="undefined"){console.log(a)
19311934
return}if(typeof print=="function"){print(a)
@@ -2853,7 +2856,8 @@ k.appendChild(a)
28532856
s=this.b
28542857
s.replaceWith(k)
28552858
if(a.contentWindow==null){k.replaceWith(s)
2856-
A.qw("Failed to inject embedded DartPad with content:\n")}},
2859+
A.mp("Failed to inject embedded DartPad with content:\n")
2860+
A.mp(this.c)}},
28572861
$S:20};(function aliases(){var s=J.zh.prototype
28582862
s.u=s["["]})();(function installTearOffs(){var s=hunkHelpers._static_1,r=hunkHelpers._static_0
28592863
s(A,"EX","ZV",1)

pkgs/inject_dartpad/web/inject_dartpad.dart

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@ void main() async {
1515

1616
await [
1717
for (var index = 0; index < codeElements.length; index += 1)
18-
_injectEmbed(codeElements.item(index) as web.HTMLElement, index),
18+
_injectEmbed(
19+
codeElements.item(index) as web.HTMLElement,
20+
'embedded-dartpad-$index',
21+
),
1922
].wait;
2023
}
2124

25+
/// Extract the code from the element,
26+
/// replace it with an embedded DartPad iframe,
27+
/// and inject the extracted code.
28+
///
29+
/// Each embed on a single should have a unique [iframeId].
2230
Future<EmbeddedDartPad?> _injectEmbed(
2331
web.HTMLElement codeElement,
24-
int embedNumber,
32+
String iframeId,
2533
) async {
2634
final parent = codeElement.parentElement;
2735
if (parent == null) return null;
@@ -32,7 +40,7 @@ Future<EmbeddedDartPad?> _injectEmbed(
3240
if (content.isEmpty) return null;
3341

3442
final embeddedDartPad = EmbeddedDartPad.create(
35-
iframeId: 'embedded-dartpad-$embedNumber',
43+
iframeId: 'embedded-dartpad-$iframeId',
3644
host: switch (codeElement.getAttribute('data-url')) {
3745
final specifiedHost? when specifiedHost.isNotEmpty => specifiedHost,
3846
_ => null,
@@ -46,6 +54,9 @@ Future<EmbeddedDartPad?> _injectEmbed(
4654
await embeddedDartPad.initialize(
4755
addToDocument: (iframe) {
4856
iframe.classList.add('embedded-dartpad');
57+
58+
// Extract the configuration options specified on
59+
// the sites embedding DartPad (dart.dev and docs.flutter.dev).
4960
if (codeElement.getAttribute('title') case final title?
5061
when title.isNotEmpty) {
5162
iframe.setAttribute('title', title);
@@ -63,16 +74,22 @@ Future<EmbeddedDartPad?> _injectEmbed(
6374

6475
final host = web.HTMLDivElement();
6576
host.appendChild(iframe);
77+
// Add the iframe to the DOM so it has a chance to load.
6678
parent.replaceWith(host);
6779

6880
final contentWindow = iframe.contentWindow;
6981
if (contentWindow == null) {
82+
// If the iframe wasn't initialized correctly,
83+
// fall back to the original code block.
7084
host.replaceWith(parent);
85+
7186
print('Failed to inject embedded DartPad with content:\n');
87+
print(content);
7288
}
7389
},
7490
);
7591

92+
// Now that the embedded DartPad is initialized, inject the extracted code.
7693
embeddedDartPad.updateCode(content);
7794

7895
return embeddedDartPad;

0 commit comments

Comments
 (0)