reverse-name/dart-js/test.dart

258 lines
5.0 KiB
Dart
Raw Normal View History

2024-03-04 19:03:46 +08:00
// js
2024-03-04 22:46:12 +08:00
import 'dart:js' as Js;
import 'dart:html';
2024-03-04 19:03:46 +08:00
2024-03-05 22:25:31 +08:00
abstract class IPlr {}
class NPlr extends IPlr {}
2024-03-05 20:48:58 +08:00
SpanElement Span(String cls) {
return new SpanElement()..classes.add(cls);
}
DivElement Div(String cls) {
return new DivElement()..classes.add(cls);
}
2024-03-05 22:25:31 +08:00
TableCellElement TD(TableRowElement tr) {
2024-03-05 20:48:58 +08:00
TableCellElement td = new TableCellElement();
tr.append(td);
return td;
}
ParagraphElement P(String cls) {
return new ParagraphElement()..classes.add(cls);
}
String b(String str) {
return str;
}
2024-03-05 22:25:31 +08:00
2024-03-05 20:48:58 +08:00
String smile(String str) {
return '<div class="smile s_$str"></div>';
}
String l(String str, String key) {
return str;
}
2024-03-05 22:25:31 +08:00
num drawText(CanvasRenderingContext2D ctx, String txt, int x, int y, int w,
bool center) {
TextMetrics tm = ctx.measureText(txt);
ctx.fillText(txt, x, y + 15, w);
return 1;
}
2024-03-05 22:34:27 +08:00
class Dt {
static String ex = b('!');
static String add = b('+');
static String at = b('@');
}
2024-03-17 17:42:30 +08:00
List<String> test_list(String str) {
// spilt with ""
str = str.split("").join("");
// if empty -> return []
if (str.isEmpty) {
return [];
}
return ["a", "b", "c"];
}
2024-03-05 22:34:27 +08:00
2024-03-17 18:59:57 +08:00
b_run() async {
print("object in b_run");
await md5run();
2024-03-17 19:02:41 +08:00
await async_catch();
2024-03-17 18:59:57 +08:00
print("object after 1s");
}
2024-03-19 22:32:17 +08:00
md5run() async {
2024-03-17 18:54:36 +08:00
print("object in md5run");
await Future.delayed(Duration(milliseconds: 1000));
print("object in md5run after 1s");
}
2024-03-17 19:02:41 +08:00
a_async_throw() async {
throw "a_async_throw";
}
async_catch() async {
try {
await a_async_throw();
} catch (e) {
print(e);
}
}
2024-03-19 22:32:17 +08:00
a_run() async {
2024-03-17 18:54:36 +08:00
print("object");
await md5run();
2024-03-17 18:59:57 +08:00
await b_run();
2024-03-17 18:54:36 +08:00
print("object after 1s");
}
2024-03-24 16:19:18 +08:00
void test() {
print("test");
}
2024-03-04 12:29:26 +08:00
void main() {
2024-03-17 17:42:30 +08:00
print(test_list("abc"));
print(test_list(""));
2024-03-06 19:09:40 +08:00
print(Dt.at);
2024-03-05 22:34:27 +08:00
2024-03-19 22:32:17 +08:00
var reg_exp = RegExp(r"^\d{1,2}$");
print(reg_exp.hasMatch("1"));
2024-03-04 22:52:37 +08:00
// Find an element by id (an-id).
Element idElement = querySelector('#an-id')!;
2024-03-19 22:32:17 +08:00
// Find an element by class (a-class).
2024-03-04 22:52:37 +08:00
Element classElement = querySelector('.a-class')!;
2024-04-05 12:41:24 +08:00
Comment comment = Comment("comment");
// canvas
CanvasElement canvas = CanvasElement(width: 100, height: 100);
2024-04-05 23:06:04 +08:00
CanvasRenderingContext2D ctx =
canvas.getContext('2d') as CanvasRenderingContext2D;
2024-04-05 12:41:24 +08:00
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, 100, 100);
ctx.fillStyle = 'white';
drawText(ctx, 'Hello, World!', 0, 0, 100, true);
// Add a click event listener to the class element.
comment.addEventListener('click', (event) => null);
2024-03-24 16:19:18 +08:00
classElement.addEventListener('click', (event) {
print("click");
});
2024-03-19 22:32:17 +08:00
// Find all elements by tag (<div>).
2024-03-04 22:52:37 +08:00
List<Element> divElements = querySelectorAll('div');
2024-04-05 00:09:14 +08:00
List<double> double_list = [1.0, 2.0, 3.0, 5.0, 5.002];
// range in list
print(double_list.getRange(0, 2).toList());
// range
var range = List.generate(5, (index) => index);
print(range);
print(double_list.getRange(Range.END_TO_START, Range.START_TO_END).toList());
2024-03-23 11:21:43 +08:00
// 增删改查
double_list.add(4.0);
double_list.removeAt(0);
double_list[0] = 5.0;
print(double_list[0]);
// 遍历
double_list.forEach((element) {
print(element);
});
2024-03-23 09:57:06 +08:00
// call setTimeout
2024-04-05 23:06:04 +08:00
Js.context.callMethod('setTimeout', [
Js.allowInterop(() {
print("延时1秒执行");
}),
1000
]);
2024-03-23 09:57:06 +08:00
2024-03-17 15:09:48 +08:00
Map<String, int> test_map = {
2024-03-06 19:09:40 +08:00
"a": 1,
};
2024-03-17 18:54:36 +08:00
a_run();
2024-03-17 14:42:27 +08:00
// 插入
test_map["b"] = 2;
2024-03-19 22:32:17 +08:00
2024-03-17 14:42:27 +08:00
// 删除
test_map.remove("a");
2024-03-19 22:32:17 +08:00
2024-03-17 14:42:27 +08:00
// 修改
test_map["b"] = 3;
2024-03-19 22:32:17 +08:00
2024-04-05 23:06:04 +08:00
if (test_map.containsKey("b")) {
test_map["b"] = 4;
}
2024-03-17 14:42:27 +08:00
// 查找
print(test_map["b"]);
// 遍历
test_map.forEach((key, value) {
print("$key : $value");
});
// 长度
print(test_map.length);
// 是否为空
print(test_map.isEmpty);
// 是否不为空
print(test_map.isNotEmpty);
// 是否包含key
print(test_map.containsKey("b"));
// 是否包含value
print(test_map.containsValue(3));
// 转换为list
print(test_map.keys.toList());
print(test_map.values.toList());
// 转换为json
print(test_map.toString());
// 清空
test_map.clear();
2024-03-23 11:21:43 +08:00
// window.parent.postMessage
window.parent?.postMessage("什么奇怪东西", "*");
2024-03-17 14:42:27 +08:00
2024-03-06 19:09:40 +08:00
print(test_map);
2024-03-05 22:25:31 +08:00
TableRowElement tr = new TableRowElement();
TD(tr)
..text = l('击杀', 'killedCount')
..style.width = '44px';
TD(tr)
..text = l('致命一击', 'killerName')
..style.minWidth = '112px';
var plr = new NPlr();
2024-03-05 20:48:58 +08:00
DivElement plist = document.querySelector('.plist') as DivElement;
DivElement pbody = document.querySelector('.pbody') as DivElement;
2024-03-05 12:29:56 +08:00
2024-03-05 22:25:31 +08:00
DivElement p = Div('p');
p.append(tr);
pbody.append(p);
plist.append(pbody);
2024-03-05 12:29:56 +08:00
print(idElement);
print(classElement);
print(divElements);
querySelector('#inputs')!.nodes.add(idElement);
2024-03-04 12:29:26 +08:00
print("Hello, World!");
var a = "Hello, World!";
for (var i = 0; i < 10; i++) {
a += " " + i.toString();
}
2024-03-04 22:52:37 +08:00
int b = 124242424;
2024-03-04 12:29:26 +08:00
for (var i = 0; i < 10; i++) {
2024-03-04 22:52:37 +08:00
b += b * i;
2024-03-04 12:29:26 +08:00
}
2024-03-04 22:52:37 +08:00
// js sleep
Future.delayed(Duration(milliseconds: 1000)).then((value) {
print("延时1秒执行 then ");
print(value);
});
2024-03-04 22:46:12 +08:00
var closuer = () {
print("closuer");
};
closuer();
2024-03-04 12:29:26 +08:00
print(a);
2024-03-04 22:52:37 +08:00
}