Skip to content

Commit 520b653

Browse files
authored
Add Edit this page button to docs (#751)
* Add Edit this page button to docs
1 parent fc00f83 commit 520b653

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

packages/dev/docs/src/Layout.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function isBlogSection(section) {
109109
return section === 'blog' || section === 'releases';
110110
}
111111

112-
function Page({children, currentPage, publicUrl, styles, scripts}) {
112+
function Page({children, currentPage, publicUrl, styles, scripts, pathToPage}) {
113113
let parts = currentPage.name.split('/');
114114
let isBlog = isBlogSection(parts[0]);
115115
let isSubpage = parts.length > 1 && !INDEX_RE.test(currentPage.name);
@@ -198,6 +198,7 @@ function Page({children, currentPage, publicUrl, styles, scripts}) {
198198
<meta property="og:image" content={heroUrl} />
199199
<meta property="og:description" content={description} />
200200
<meta property="og:locale" content="en_US" />
201+
<meta data-github-src={pathToPage} />
201202
<script
202203
type="application/ld+json"
203204
dangerouslySetInnerHTML={{__html: JSON.stringify(
@@ -365,7 +366,6 @@ function Nav({currentPageName, pages}) {
365366
</li>
366367
);
367368
}
368-
369369
let isActive = (pageMap) => {
370370
for (let key in pageMap) {
371371
if (pageMap[key].some(p => p.name === currentPageName)) {
@@ -482,11 +482,11 @@ function Footer() {
482482
</footer>
483483
);
484484
}
485-
486485
export const PageContext = React.createContext();
487486
export function BaseLayout({scripts, styles, pages, currentPage, publicUrl, children, toc}) {
487+
let pathToPage = currentPage.filePath.substring(currentPage.filePath.indexOf('packages/'), currentPage.filePath.length);
488488
return (
489-
<Page scripts={scripts} styles={styles} publicUrl={publicUrl} currentPage={currentPage}>
489+
<Page scripts={scripts} styles={styles} publicUrl={publicUrl} currentPage={currentPage} pathToPage={pathToPage}>
490490
<div style={{isolation: 'isolate'}}>
491491
<header className={docStyles.pageHeader} />
492492
<Nav currentPageName={currentPage.name} pages={pages} />
@@ -503,6 +503,7 @@ export function BaseLayout({scripts, styles, pages, currentPage, publicUrl, chil
503503
</ImageContext.Provider>
504504
</MDXProvider>
505505
{toc.length ? <ToC toc={toc} /> : null}
506+
{!pathToPage.includes('index.mdx') && <div id="edit-page" className={docStyles.editPageContainer} />}
506507
<Footer />
507508
</main>
508509
</div>

packages/dev/docs/src/client.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {ActionButton} from '@adobe/react-spectrum';
13+
import {ActionButton, Flex, Link} from '@adobe/react-spectrum';
1414
import DocSearch from './DocSearch';
1515
import docsStyle from './docs.css';
16+
import LinkOut from '@spectrum-icons/workflow/LinkOut';
1617
import {listen} from 'quicklink';
1718
import React, {useEffect, useRef, useState} from 'react';
1819
import ReactDOM from 'react-dom';
@@ -187,6 +188,22 @@ ReactDOM.render(<>
187188
<ThemeSwitcher />
188189
</>, document.querySelector('.' + docsStyle.pageHeader));
189190

191+
let pathToPage = document.querySelector('[data-github-src]').getAttribute('data-github-src');
192+
if (pathToPage) {
193+
ReactDOM.render(
194+
<Link>
195+
<a
196+
href={encodeURI(`https://github.com/adobe/react-spectrum/tree/main/${encodeURI(pathToPage)}`)}
197+
target="_blank">
198+
<Flex gap="size-100" alignItems="center">
199+
<span>Edit this page</span><LinkOut size="S" />
200+
</Flex>
201+
</a>
202+
</Link>,
203+
document.querySelector('#edit-page')
204+
);
205+
}
206+
190207
document.addEventListener('mousedown', (e) => {
191208
// Prevent focusing on links to other pages with the mouse to avoid flash of focus ring during navigation.
192209
let link = e.target.closest('a');

packages/dev/docs/src/docs.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,13 @@ html, body {
228228
vertical-align: text-bottom;
229229
}
230230
}
231-
231+
.editPageContainer {
232+
display: flex;
233+
justify-content: flex-end;
234+
padding: 20px 0;
235+
width: 100%;
236+
max-width: 967px;
237+
}
232238
.highlights {
233239
display: flex;
234240
flex-wrap: wrap;

packages/dev/parcel-optimizer-ssg/SSGOptimizer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module.exports = new Optimizer({
6666
})),
6767
pages,
6868
currentPage: {
69+
filePath: mainAsset.filePath,
6970
category: mainAsset.meta.category,
7071
name,
7172
title: mainAsset.meta.title,

0 commit comments

Comments
 (0)