-
Notifications
You must be signed in to change notification settings - Fork 166
Draft of how widgets could be organized #8035
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Copyright (c) 2024, 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. | ||
|
|
||
| // TODO: Create a function that can create an instance of Node on the server | ||
| // We might need to move a lot of code around since Node is currently | ||
| // defined in app/lib/frontend/dom/dom.dart | ||
| // It's also possible we can define the helper function somewhere else. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright (c) 2024, 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 'dart:js_interop'; | ||
|
|
||
| import 'package:collection/collection.dart'; | ||
| import 'package:web/web.dart'; | ||
|
|
||
| import '../web_util.dart'; | ||
| import 'completion/widget.dart' deferred as completion; | ||
|
|
||
| /// Widget entry | ||
| typedef _WidgetEntry = ({ | ||
| /// Name of the widget referenced in `data-widget="<name>"`. | ||
| String name, | ||
|
|
||
| /// Function to create an instance of the widget given an element and options. | ||
| /// | ||
| /// [element] which carries `data-widget="$name"`. | ||
| /// [options] a map from options to values, where options are specified as | ||
| /// `data-$name-$option="$value"`. | ||
| /// | ||
| /// Hence, a widget called `completion` is created on an element by adding | ||
| /// `data-widget="completion"`. And option `src` is specified with: | ||
| /// `data-completion-src="$value"`. | ||
| FutureOr<void> Function(Element element, Map<String, String> options) create, | ||
|
|
||
| /// Load widget library, if deferred (otherwise this can be `null`). | ||
| Future<void> Function()? loadLibrary, | ||
| }); | ||
|
|
||
| // List of registered widgets | ||
| final _widgets = <_WidgetEntry>[ | ||
| ( | ||
| name: 'completion', | ||
| create: completion.create, | ||
| loadLibrary: completion.loadLibrary, | ||
| ), | ||
| ]; | ||
|
|
||
| Future<void> setupWidgets() async { | ||
| await Future.wait(document | ||
| // query for all elements with the property `data-widget="..."` | ||
| .querySelectorAll('[data-widget]') | ||
| .toList() // Convert NodeList to List | ||
| // We only care about elements | ||
| .where((node) => node.isA<HTMLElement>()) | ||
| .map((node) => node as HTMLElement) | ||
| // group by widget | ||
| .groupListsBy((element) => element.getAttribute('data-widget') ?? '') | ||
| .entries | ||
| // For each (widget, elements) load widget and create widgets | ||
| .map((entry) async { | ||
jonasfj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Get widget name and elements which it should be created for | ||
| final MapEntry(key: name, value: elements) = entry; | ||
|
|
||
| // Find the widget create function and loadLibrary function | ||
| final (name: _, :create, :loadLibrary) = _widgets.firstWhere( | ||
| (widget) => widget.name == name, | ||
| orElse: () => ( | ||
| name: '', | ||
| create: (_, __) => throw AssertionError('no such widget'), | ||
| loadLibrary: null, | ||
| ), | ||
| ); | ||
|
|
||
| // Load library if this a deferred widget | ||
| if (loadLibrary != null) { | ||
| await loadLibrary(); | ||
| } | ||
|
|
||
| // Create widget for each element | ||
| await Future.wait(elements.map((element) async { | ||
| try { | ||
| final prefix = 'data-$name-'; | ||
| final options = Map.fromEntries(element | ||
| .getAttributeNames() | ||
| .iterable | ||
| .where((attr) => attr.startsWith(prefix)) | ||
| .map((attr) { | ||
| return MapEntry( | ||
| attr.substring(0, prefix.length), | ||
| element.getAttribute(attr) ?? '', | ||
| ); | ||
| })); | ||
|
|
||
| await create(element, options); | ||
| } catch (e, st) { | ||
| console.error('Failed to initialize data-widget="$name"'.toJS); | ||
| console.error('Triggered by element:'.toJS); | ||
| console.error(element); | ||
| console.error(e.toString().toJS); | ||
| console.error(st.toString().toJS); | ||
| } | ||
| })); | ||
| })); | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setupWidgetsbecame asynchronous, but inscript.dartwe still call it as a sync method, no indication that the future is not awaited.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm making it
voidbecause I don't want people to await it :DsetupWidgetsdoes what it does, you don't need to know if it failed, it'll report errors in console.