Skip to content

Commit

Permalink
Switch dart:js and dart:js_util usages to use static interop
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Jan 14, 2023
1 parent 263ea61 commit 8c59543
Show file tree
Hide file tree
Showing 8 changed files with 3,269 additions and 3,445 deletions.
6,640 changes: 3,218 additions & 3,422 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;
13 changes: 6 additions & 7 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 Expand Up @@ -516,10 +517,8 @@ void _mapToContainer(Element containerElement, Element suggestion) {

final element = _containerMap[containerInnerHtml];
if (element != null) {
print('appending! ${suggestion.innerHtml}');
element.append(suggestion);
} else {
print('appending2! ${suggestion.innerHtml}');
containerElement.append(suggestion);
_containerMap[containerInnerHtml] = containerElement;
}
Expand Down
2 changes: 1 addition & 1 deletion web/sig.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
469F5B013C1B0324C1F020C95013AC51
2D8E7D7722AE5937479C00C043788871
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()
@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 8c59543

Please sign in to comment.