Dartdoc snippet extension to inject full featured code snippets in to API docs. (#23281)

This creates a custom dartdoc tool that will generate snippet blocks in our API docs that allow the user to copy easily to the clipboard, and will also embed the snippet code into a template to show it in a larger context with an app.

This PR adds the snippet tool, a template, and a couple of HTML skeleton files, one for snippets that are designed to be in an application setting, and one where it simply puts a nice container around existing snippets, making them easier to copy to the clipboard.
This commit is contained in:
Greg Spencer
2018-10-23 13:50:24 -07:00
committed by GitHub
parent a3e0b0aee2
commit 65d3ddd5d1
21 changed files with 1367 additions and 170 deletions

View File

@@ -0,0 +1,139 @@
/* Overrides for dartdoc styles. */
body {
font-size: 15px;
font-family: Roboto, sans-serif;
line-height: 1.5;
color: #111;
background-color: #fdfdfd;
font-weight: 300;
-webkit-font-smoothing: auto;
}
header {
background-color: white;
color: #424242;
}
nav.navbar {
min-height: 57px;
padding: 6px 0;
}
header.header-fixed nav.navbar-fixed-top {
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.37);
}
h1, h2 {
font-weight: 300;
}
h3, h4, h5, h6 {
font-weight: 400;
}
h1 {
font-size: 42px !important;
letter-spacing: -1px;
}
header h1 {
font-weight: 300;
}
h2 {
color: #111;
font-size: 24px;
}
.markdown h2 {
font-size: 24px;
}
section.summary h2 {
font-size: 24px;
color: inherit;
border-bottom: none;
}
.sidebar ol,
.sidebar ol li.section-title {
font-size: inherit;
}
@media screen and (max-width: 768px) {
.sidebar-offcanvas-left.active {
padding: 10px;
}
}
.sidebar-offcanvas-left ol {
padding: 0 16px 16px 0;
}
.sidebar-offcanvas-left h5 {
display: none;
}
pre,
pre.prettyprint,
pre > code {
font-size: 14px;
}
pre,
pre.prettyprint {
background: #f5f2f0;
margin: 0 0 15px 0;
padding: 8px 12px;
border: 1px solid #cccccc;
border-radius: 4px;
}
code {
background-color: inherit;
font-size: 1em; /* browsers default to smaller font for code */
font-weight: 300;
padding-left: 0; /* otherwise we get ragged left margins */
padding-right: 0;
}
#search-box {
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 2px;
font-family: inherit;
padding: 4px 6px;
font-size: 15px;
}
input.form-control.typeahead {
padding: 4px 7px;
font-size: 15px;
}
dl.dl-horizontal dt {
color: inherit;
}
/* Line the material icons up with their labels */
i.material-icons.md-36,
i.material-icons.md-48 {
vertical-align: bottom;
}
/* thinify the inherited names in lists */
li.inherited a {
font-weight: 100;
}
/* address a style issue with the background of code sections */
code.hljs {
background: inherit;
}
footer {
font-size: 13px;
padding: 12px 20px;
}

View File

@@ -0,0 +1,108 @@
/* Styles for handling custom code snippets */
.snippet-container {
background-color: #45aae8;
padding: 10px;
overflow: auto;
}
.snippet-container pre {
max-height: 500px;
overflow: auto;
padding: 10px;
margin: 0px;
}
.snippet-container ::-webkit-scrollbar {
width: 12px;
}
.snippet-container ::-webkit-scrollbar-thumb {
width: 12px;
border-radius: 6px;
}
.snippet {
position: relative;
}
.snippet-description {
padding: 10px;
color: white;
}
.snippet-buttons button {
background-color: #45aae8;
border-style: none;
color: white;
padding: 10px 24px;
cursor: pointer;
float: left;
}
.snippet-buttons:after {
content: "";
clear: both;
display: table;
}
.snippet-buttons button:focus { outline: none; }
.snippet-buttons button:hover {
opacity: 1.0;
}
.snippet-buttons :not([selected]) {
opacity: 0.65;
}
.snippet-buttons [selected] {
opacity: 1.0;
}
.snippet-container [hidden] {
display: none;
}
.snippet-create-command {
text-align: end;
font-size: smaller;
font-style: normal;
font-family: courier, lucidia;
}
/* Styles for the copy-to-clipboard button */
.copyable-container {
position: relative;
}
.copy-button-overlay {
position: absolute;
top: 10px;
right: 14px;
height: 28px;
width: 28px;
transition: .3s ease;
background-color: #45aae8;
}
.copy-button {
border-style: none;
background: none;
cursor: pointer;
}
.copy-button :focus {
outline: 0px;
}
.copy-button :hover {
transition: .3s ease;
color: #222;
}
.copy-image {
opacity: 0.65;
color: #45aae8;
font-size: 28px;
padding-top: 4px;
}

View File

@@ -0,0 +1,93 @@
/**
* Scripting for handling custom code snippets
*/
const shortSnippet = 'shortSnippet';
const longSnippet = 'longSnippet';
var visibleSnippet = shortSnippet;
/**
* Shows the requested snippet. Values for "name" can be "shortSnippet" or
* "longSnippet".
*/
function showSnippet(name) {
if (visibleSnippet == name) return;
if (visibleSnippet != null) {
var shown = document.getElementById(visibleSnippet);
var attribute = document.createAttribute('hidden');
if (shown != null) {
shown.setAttributeNode(attribute);
}
var button = document.getElementById(visibleSnippet + 'Button');
if (button != null) {
button.removeAttribute('selected');
}
}
if (name == null || name == '') {
visibleSnippet = null;
return;
}
var newlyVisible = document.getElementById(name);
if (newlyVisible != null) {
visibleSnippet = name;
newlyVisible.removeAttribute('hidden');
} else {
visibleSnippet = null;
}
var button = document.getElementById(name + 'Button');
var selectedAttribute = document.createAttribute('selected');
if (button != null) {
button.setAttributeNode(selectedAttribute);
}
}
// Finds a sibling to given element with the given id.
function findSiblingWithId(element, id) {
var siblings = element.parentNode.children;
var siblingWithId = null;
for (var i = siblings.length; i--;) {
if (siblings[i] == element) continue;
if (siblings[i].id == id) {
siblingWithId = siblings[i];
break;
}
}
return siblingWithId;
};
// Returns true if the browser supports the "copy" command.
function supportsCopying() {
return !!document.queryCommandSupported &&
!!document.queryCommandSupported('copy');
}
// Copies the text inside the currently visible snippet to the clipboard, or the
// given element, if any.
function copyTextToClipboard(element) {
if (element == null) {
var elementSelector = '#' + visibleSnippet + ' .language-dart';
element = document.querySelector(elementSelector);
if (element == null) {
console.log(
'copyTextToClipboard: Unable to find element for "' +
elementSelector + '"');
return;
}
}
if (!supportsCopying()) {
alert('Unable to copy to clipboard (not supported by browser)');
return;
}
if (element.hasAttribute('contenteditable')) {
element.focus();
}
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
}

3
dev/docs/snippets.html Normal file
View File

@@ -0,0 +1,3 @@
<!-- Styles and scripting for handling custom code snippets -->
<link href="../assets/snippets.css" rel="stylesheet" type="text/css">
<script src="../assets/snippets.js"></script>

View File

@@ -1,148 +1,10 @@
<!-- style overrides for dartdoc -->
<style>
@import 'https://fonts.googleapis.com/css?family=Roboto:500,400italic,300,400,100i';
@import 'https://fonts.googleapis.com/css?family=Material+Icons';
</style>
<style>
body {
font-size: 15px;
font-family: Roboto, sans-serif;
line-height: 1.5;
color: #111;
background-color: #fdfdfd;
font-weight: 300;
-webkit-font-smoothing: auto;
}
header {
background-color: white;
color: #424242;
}
nav.navbar {
min-height: 57px;
padding: 6px 0;
}
header.header-fixed nav.navbar-fixed-top {
box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.37);
}
h1, h2 {
font-weight: 300;
}
h3, h4, h5, h6 {
font-weight: 400;
}
h1 {
font-size: 42px !important;
letter-spacing: -1px;
}
header h1 {
font-weight: 300;
}
h2 {
color: #111;
font-size: 24px;
}
.markdown h2 {
font-size: 24px;
}
section.summary h2 {
font-size: 24px;
color: inherit;
border-bottom: none;
}
.sidebar ol,
.sidebar ol li.section-title {
font-size: inherit;
}
@media screen and (max-width: 768px) {
.sidebar-offcanvas-left.active {
padding: 10px;
}
}
.sidebar-offcanvas-left ol {
padding: 0 16px 16px 0;
}
.sidebar-offcanvas-left h5 {
display: none;
}
pre,
pre.prettyprint,
pre > code {
font-size: 14px;
}
pre,
pre.prettyprint {
background: #f5f2f0;
margin: 0 0 15px 0;
padding: 8px 12px;
border: 1px solid #cccccc;
border-radius: 4px;
}
code {
background-color: inherit;
font-size: 1em; /* browsers default to smaller font for code */
font-weight: 300;
padding-left: 0; /* otherwise we get ragged left margins */
padding-right: 0;
}
#search-box {
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 2px;
font-family: inherit;
padding: 4px 6px;
font-size: 15px;
}
input.form-control.typeahead {
padding: 4px 7px;
font-size: 15px;
}
dl.dl-horizontal dt {
color: inherit;
}
/* Line the material icons up with their labels */
i.material-icons.md-36,
i.material-icons.md-48 {
vertical-align: bottom;
}
/* thinify the inherited names in lists */
li.inherited a {
font-weight: 100;
}
/* address a style issue with the background of code sections */
code.hljs {
background: inherit;
}
footer {
font-size: 13px;
padding: 12px 20px;
}
</style>
<link href="../assets/overrides.css" rel="stylesheet" type="text/css">
<!-- The following rules are from http://google.github.io/material-design-icons/ -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">