forked from dart-lang/dart-pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground_context.dart
146 lines (107 loc) · 3.95 KB
/
playground_context.dart
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'context.dart';
import 'editing/editor.dart';
class PlaygroundContext extends Context {
final Editor editor;
final _modeController = StreamController<String>.broadcast();
final Document _dartDoc;
final Document _htmlDoc;
final Document _cssDoc;
final _cssDirtyController = StreamController.broadcast();
final _dartDirtyController = StreamController.broadcast();
final _htmlDirtyController = StreamController.broadcast();
final _cssReconcileController = StreamController.broadcast();
final _dartReconcileController = StreamController.broadcast();
final _htmlReconcileController = StreamController.broadcast();
PlaygroundContext(this.editor)
: _dartDoc = editor.document,
_htmlDoc = editor.createDocument(content: '', mode: 'html'),
_cssDoc = editor.createDocument(content: '', mode: 'css') {
editor.mode = 'dart';
_dartDoc.onChange.listen((_) => _dartDirtyController.add(null));
_htmlDoc.onChange.listen((_) => _htmlDirtyController.add(null));
_cssDoc.onChange.listen((_) => _cssDirtyController.add(null));
_createReconciler(_cssDoc, _cssReconcileController, 250);
_createReconciler(_dartDoc, _dartReconcileController, 1250);
_createReconciler(_htmlDoc, _htmlReconcileController, 250);
}
Document get dartDocument => _dartDoc;
Document get htmlDocument => _htmlDoc;
Document get cssDocument => _cssDoc;
@override
String get dartSource => _dartDoc.value;
@override
set dartSource(String value) {
_dartDoc.value = value;
}
@override
String get htmlSource => _htmlDoc.value;
@override
set htmlSource(String value) {
_htmlDoc.value = value;
}
@override
String get cssSource => _cssDoc.value;
@override
set cssSource(String value) {
_cssDoc.value = value;
}
@override
String get activeMode => editor.mode;
@override
Stream<String> get onModeChange => _modeController.stream;
bool hasWebContent() {
return htmlSource.trim().isNotEmpty || cssSource.trim().isNotEmpty;
}
@override
void switchTo(String name) {
final oldMode = activeMode;
if (name == 'dart') {
editor.swapDocument(_dartDoc);
} else if (name == 'html') {
editor.swapDocument(_htmlDoc);
} else if (name == 'css') {
editor.swapDocument(_cssDoc);
}
if (oldMode != name) _modeController.add(name);
editor.focus();
}
@override
String get focusedEditor {
if (editor.document == _htmlDoc) return 'html';
if (editor.document == _cssDoc) return 'css';
return 'dart';
}
Stream get onCssDirty => _cssDirtyController.stream;
Stream get onDartDirty => _dartDirtyController.stream;
Stream get onHtmlDirty => _htmlDirtyController.stream;
Stream get onCssReconcile => _cssReconcileController.stream;
Stream get onDartReconcile => _dartReconcileController.stream;
Stream get onHtmlReconcile => _htmlReconcileController.stream;
void markCssClean() => _cssDoc.markClean();
void markDartClean() => _dartDoc.markClean();
void markHtmlClean() => _htmlDoc.markClean();
/// Restore the focus to the last focused editor.
void focus() => editor.focus();
void _createReconciler(Document doc, StreamController controller, int delay) {
Timer? timer;
doc.onChange.listen((_) {
if (timer != null) timer!.cancel();
timer = Timer(Duration(milliseconds: delay), () {
controller.add(null);
});
});
}
/// Return true if the current cursor position is in a whitespace char.
bool cursorPositionIsWhitespace() {
final document = editor.document;
final str = document.value;
final index = document.indexFromPos(document.cursor);
if (index < 0 || index >= str.length) return false;
final char = str[index];
return char != char.trim();
}
}