Skip to content

Commit

Permalink
Switch dart:js and some dart:js_util usages to use static interop (#3299
Browse files Browse the repository at this point in the history
)
  • Loading branch information
parlough authored Jan 24, 2023
1 parent 9735d89 commit 758e185
Show file tree
Hide file tree
Showing 9 changed files with 3,037 additions and 3,277 deletions.
6,229 changes: 2,973 additions & 3,256 deletions lib/resources/docs.dart.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/resources/docs.dart.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: A non-interactive HTML documentation generator for Dart source code
repository: https://github.com/dart-lang/dartdoc

environment:
sdk: '>=2.17.0 <3.0.0'
sdk: '>=2.18.0 <3.0.0'

dependencies:
analyzer: "^5.2.0"
Expand Down Expand Up @@ -35,6 +35,7 @@ dev_dependencies:
dart_style: ^2.2.0
grinder: ^0.9.0
http: ^0.13.3
js: 0.6.5
lints: ^2.0.0
test: ^1.19.0
test_descriptor: ^2.0.0
Expand Down
10 changes: 2 additions & 8 deletions web/docs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
// 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:js' as js;

import 'highlight.dart' as highlight;
import 'search.dart' as search;
import 'sidenav.dart' as sidenav;
import 'theme.dart' as theme;

void main() {
inithighlightJS();
highlight.init();
sidenav.init();
search.init();
theme.init();
}

void inithighlightJS() {
js.JsObject hljs = js.context['hljs'];
hljs.callMethod('highlightAll');
}
20 changes: 20 additions & 0 deletions web/highlight.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2023, 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 'package:js/js.dart';

void init() {
highlight?.highlightAll();
}

@JS()
@staticInterop
class HighlightJs {}

extension HighlightJsExtension on HighlightJs {
external void highlightAll();
}

@JS('hljs')
external HighlightJs? get highlight;
11 changes: 6 additions & 5 deletions web/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import 'dart:convert';
import 'dart:html';
import 'dart:js_util' as js_util;

import 'web_interop.dart';

final String _htmlBase = () {
final body = document.querySelector('body')!;
Expand Down Expand Up @@ -34,14 +35,14 @@ void init() {
}

window.fetch('${_htmlBase}index.json').then((response) async {
int code = js_util.getProperty(response, 'status');
response = response as FetchResponse;
var code = response.status;
if (code == 404) {
disableSearch();
return;
}

var textPromise = js_util.callMethod<Object>(response, 'text', []);
var text = await promiseToFuture<String>(textPromise);
var text = await response.text;
var jsonIndex = (jsonDecode(text) as List).cast<Map<String, dynamic>>();
final index = jsonIndex.map(_IndexItem.fromMap).toList();

Expand Down Expand Up @@ -167,7 +168,7 @@ class _Search {
..append(moreResults)
..append(searchResults);

/// Element used in [listbox] to inform the functionality of hitting enter in
/// Element used in [listBox] to inform the functionality of hitting enter in
/// search box.
late final moreResults = document.createElement('div')
..classes.add('enter-search-message');
Expand Down
2 changes: 1 addition & 1 deletion web/sig.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C6415C6028582DE4EC42B10F54BE3554
6EB4A5A121E44E4224AB759B79E251E7
4 changes: 4 additions & 0 deletions web/theme.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2022, 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:html';

void init() {
Expand Down
23 changes: 23 additions & 0 deletions web/web_interop.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2023, 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:js_util' as js_util;

import 'package:js/js.dart';

@JS('Response')
@staticInterop
class FetchResponse {}

extension FetchResponseExtension on FetchResponse {
external int get status;

@JS('text')
external Promise _text();
Future<String> get text => js_util.promiseToFuture(_text());
}

@JS()
@staticInterop
class Promise {}

0 comments on commit 758e185

Please sign in to comment.