Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch dart:js and some dart:js_util usages to use static interop #3299

Merged
merged 6 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
16 changes: 16 additions & 0 deletions web/highlight.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
6B77B82B853707609123EE6D143030E9
18 changes: 18 additions & 0 deletions web/web_interop.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:js/js.dart';
import 'package:js/js_util.dart' as js_util;

@JS('Response')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This is fine and suggested to keep, but is unneeded, since FetchResponse has no constructor or static methods, which would be the only time we use this.

@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 {}