Skip to content

Commit

Permalink
Remove popover tooling and update CLI tutorial (#5271)
Browse files Browse the repository at this point in the history
Continuing the goal of reducing site maintenance costs, removes the
unmaintained popover support and updates the one remaining use to no
longer be reliant on it.

This will also slightly reduce site loading time with less JS and CSS
downloaded on every page :)

We can revisit popover support in code snippets in the future if
desired. Browsers are currently implementing [standardized
popovers](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API)
that are more accessible and easier to implement. They have shipped in
WebKit and Chromium based browsers and are set to be enabled in Firefox
soon.

Closes #2316
Fixes #4987
Contributes to #5177

**Staged:**
https://dart-dev--pr5271-feature-remove-popup-farcxcqp.web.app/tutorials/server/cmdline
  • Loading branch information
parlough authored Oct 23, 2023
1 parent 458619d commit d3cdb25
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 653 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,6 @@ Most of the code used to create [DartPad][] examples is hosted on GitHub.
However, this repo also contains some `*.dart` files
responsible for DartPad example code.

# Refresh DartPad HTML tooltips

Files that require DartPad HTML to be manually updated
include instructions at the top that specify running:

```terminal
$ dart run tool/dart_tools/bin/create_code_with_tooltips.dart
```

Follow the instructions in those files to refresh the appropriate code.

### DartPad picker

The DartPad example picker must be manually compiled if changes are made.
Expand Down
1 change: 1 addition & 0 deletions examples/cli/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../analysis_options.yaml
53 changes: 17 additions & 36 deletions examples/misc/bin/dcat/dcat.dart → examples/cli/bin/dcat.dart
Original file line number Diff line number Diff line change
@@ -1,66 +1,47 @@
// Copyright (c) 2013, 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.

// IMPORTANT NOTE:
// 1. If you change this file, ensure that the associated tooltip data remains
// valid (*_tooltips.html).
// 2. Regenerate the HTML version of this sample, by running
// dart run tool/dart_tools/bin/create_code_with_tooltips.dart
// 3. To generate the DartPad version: (1) delete lines containing only tip
// instructions. (2) Trim //!foo markers from the end of the remaining lines,
// e.g., using this Perl regexp: / ?\/\/!.*//g
/// Simple implementation of the *nix cat utility.
///
/// Usage: `dart run dcat.dart [-n] patterns files`
///
/// `dcat` reads `files` sequentially, writing them to standard output.
/// The file operands are processed in command-line order.
/// If `files` is absent, `dcat` reads from the standard input until `EOF`.
/// Unlike the *nix `cat`, `dcat` does not support single dash ('-') arguments.
library;

// #docregion dcat-app
import 'dart:convert';
import 'dart:io';

import 'package:args/args.dart';

const lineNumber = 'line-number';

/// Simple implementation of the *nix cat utility. //!web-only
/// //!web-only
/// Usage: dart run dcat.dart [-n] patterns files //!web-only
/// //!web-only
/// `dcat` reads `files` sequentially, writing them to standard output. The //!web-only
/// file operands are processed in command-line order. //!web-only
/// If `files` is absent, `dcat` reads from the standard input until `EOF`. //!web-only
/// //!web-only
/// Unlike the *nix `cat`, `dcat` does not support single dash ('-') arguments. //!web-only
//!tip("List<String> arguments")
// #docregion arg-processing
void main(List<String> arguments) {
exitCode = 0; // presume success
exitCode = 0; // Presume success
final parser = ArgParser()..addFlag(lineNumber, negatable: false, abbr: 'n');

//!tip("parser.parse(arguments)")
ArgResults argResults = parser.parse(arguments);
final paths = argResults.rest;

dcat(paths, showLineNumbers: argResults[lineNumber] as bool);
}
// #enddocregion arg-processing

//!tip("async")
Future<void> dcat(List<String> paths, {bool showLineNumbers = false}) async {
if (paths.isEmpty) {
// No files provided as arguments. Read from stdin and print each line.
// #docregion pipe
//!tip("stdin.pipe(stdout)") //!tip("await")
await stdin.pipe(stdout);
// #enddocregion pipe
} else {
// #docregion for-path
for (final path in paths) {
var lineNumber = 1;
final lines = utf8.decoder
//!tip("openRead()") //!tip("File(path)")
.bind(File(path).openRead())
//!tip("transform(const LineSplitter())")
.transform(const LineSplitter());
//!tip("try")
try {
//!tip("await for (final line in lines)")
await for (final line in lines) {
// #docregion showLineNumbers
if (showLineNumbers) {
Expand All @@ -69,7 +50,6 @@ Future<void> dcat(List<String> paths, {bool showLineNumbers = false}) async {
stdout.writeln(line);
// #enddocregion showLineNumbers
}
//!tip("catch")
} catch (_) {
await _handleError(path);
}
Expand All @@ -78,14 +58,15 @@ Future<void> dcat(List<String> paths, {bool showLineNumbers = false}) async {
}
}

// #docregion _handleError
// #docregion handle-error
Future<void> _handleError(String path) async {
//!tip("await FileSystemEntity.isDirectory(path)")
// #docregion await-FileSystemEntity
// #docregion await-entity
if (await FileSystemEntity.isDirectory(path)) {
stderr.writeln('error: $path is a directory');
} else {
exitCode = 2; //!tip("exitCode = 2")
exitCode = 2;
}
// #enddocregion await-FileSystemEntity
// #enddocregion await-entity
}
// #enddocregion handle-error
// #enddocregion dcat-app
7 changes: 7 additions & 0 deletions examples/cli/bin/dcat_stdin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'dart:io';

void main() {
stdout.writeln('Type something');
final input = stdin.readLineSync();
stdout.writeln('You typed: $input');
}
1 change: 1 addition & 0 deletions examples/cli/dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: ../dart_test_base.yaml
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/cli/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: cli_examples
description: Examples for CLI tutorials on dart.dev

environment:
sdk: ^3.1.0

dependencies:
args: ^2.4.2

dev_dependencies:
lints: ^3.0.0
test: ^1.24.8
42 changes: 42 additions & 0 deletions examples/cli/test/dcat_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dart:convert';
import 'dart:io';

import 'package:test/test.dart';

void main() {
const pathToQuotes = 'test_data/quote.txt';

test('dcat', () async {
final procResult = await Process.run('dart', [
'run',
'--enable-asserts',
'bin/dcat.dart',
'-n',
pathToQuotes,
]);
expect(
procResult.stdout,
allOf(
startsWith('1'), // Line number
contains('Dr. Seuss'),
endsWith('Nietzsche\n'),
),
);
});

test('dcat1', () async {
const greeting = '¡Hola Dash!';
final process = await Process.start('dart', [
'run',
'--enable-asserts',
'bin/dcat_stdin.dart',
]);
process.stdin.writeln(greeting);
final output = await process.stdout.transform(const Utf8Decoder()).join('');
final errors = await process.stderr.transform(const Utf8Decoder()).join('');

expect(await process.exitCode, 0);
expect(output, contains('You typed: $greeting'));
expect(errors, isEmpty);
});
}
5 changes: 5 additions & 0 deletions examples/cli/test_data/quote.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Be yourself. Everyone else is taken. -Oscar Wilde
Don't cry because it's over, smile because it happened. -Dr. Seuss
You only live once, but if you do it right, once is enough. -Mae West
# This is a comment line.
That which does not kill us makes us stronger. -Nietzsche
12 changes: 0 additions & 12 deletions examples/misc/bin/dcat/dcat1.dart

This file was deleted.

53 changes: 0 additions & 53 deletions examples/misc/bin/dcat/dcat_tooltips.html

This file was deleted.

33 changes: 0 additions & 33 deletions examples/misc/test/dcat_test.dart

This file was deleted.

4 changes: 0 additions & 4 deletions src/_includes/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@
integrity="sha512-3gJwYpMe3QewGELv8k/BX9vcqhryRdzRMxVfq6ngyWXwo03GFEzjsUm8Q7RZcHPHksttq7/GFoxjCVUjkjvPdw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
crossorigin="anonymous"
referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.2/js/bootstrap.min.js"
integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+"
crossorigin="anonymous"
Expand Down
3 changes: 0 additions & 3 deletions src/_sass/core/_bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@
$dropdown-divider-bg: #bbbbbb,
$dropdown-link-active-color: transparent,
$dropdown-link-active-bg: transparent,

$popover-font-size: $font-size-base,
$popover-border-color: rgba(0, 0, 0, 0),
);

@use '../../../node_modules/bootstrap-scss/bootstrap';
Expand Down
28 changes: 0 additions & 28 deletions src/_sass/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -530,34 +530,6 @@ li.card {
}
}

// Popovers

h3.popover-header {
font-weight: $font-weight-light-bold;
margin-top: 0;
}

@mixin popover-open {
&.popover-open {
font-weight: 500;
border-color: $default-link;
}
}

[data-toggle="popover"] {
background: none;
border-bottom: 2px dotted $gray;
padding: 0;
cursor: pointer;
@include popover-open;
}

@media (hover: hover) {
[data-toggle="popover"]:hover {
border-color: $default-link !important;
}
}

.table {
width: 100%;
@extend .table-striped;
Expand Down
24 changes: 0 additions & 24 deletions src/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,6 @@ function createGallery() {
}
}

function setPopovers(root, viewport) {
const openPopClass = 'popover-open';
const popSelector = '[data-toggle="popover"]';
const popovers = root.find(popSelector);
// console.log('>> setPopovers: ' + popovers.length + ', ' + viewport);
popovers.popover({
container: viewport === 'body' ? 'body' : root.find(viewport),
html: true,
placement: 'top',
trigger: 'focus',
viewport: viewport,
}).on('shown.bs.popover', function () {
// _After_ this popover has been shown, add 'popover-open' class.
$(this).addClass(openPopClass);
}).on('hide.bs.popover', function () {
$(this).removeClass(openPopClass);
});
}

/**
* Activate the cookie notice footer
*/
Expand Down Expand Up @@ -197,11 +178,6 @@ $(function() {

adjustToc();

// Frontpage popovers inside the #code-sample should scroll with the enclosing <pre>.
setPopovers($('body.homepage #code-sample'), 'pre');
// All other popovers should scroll with the page.
setPopovers($(document.body), 'body');

// open - close mobile navigation
$('#menu-toggle').on('click', function (e) {
e.stopPropagation();
Expand Down
Loading

0 comments on commit d3cdb25

Please sign in to comment.