Skip to content

Commit de3a433

Browse files
committed
appData.appBar for app-bar items
1 parent 1087834 commit de3a433

File tree

6 files changed

+95
-13
lines changed

6 files changed

+95
-13
lines changed

packages/webdoc-default-template/helper/crawl/crawl-tutorials.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ function crawlTutorials(docTree /*: RootDoc */)/*: ?ExplorerTarget */ {
2828

2929
// No need for single top-level item
3030
if (Object.keys(rootTarget.children).length === 1) {
31-
rootTarget = { children: rootTarget.children[Object.keys(rootTarget.children)[0]].children };
31+
const implicitRootTarget = rootTarget.children[Object.keys(rootTarget.children)[0]];
32+
33+
rootTarget = {
34+
page: implicitRootTarget.page,
35+
children: implicitRootTarget.children,
36+
};
3237
}
3338

3439
return rootTarget;
@@ -49,7 +54,7 @@ function buildTutorialTargets(
4954
if (tutorial.members.length > 0) {
5055
if (tutorialTarget.page) {
5156
tutorialTarget.children.overview = {
52-
title: '(overview)',
57+
title: "(overview)",
5358
page: tutorialTarget.page,
5459
};
5560
}

packages/webdoc-default-template/publish.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ import type {
2727
} from "@webdoc/types";
2828
2929
import type {CrawlData} from './crawl';
30+
31+
type AppBarItem = {
32+
name: string;
33+
uri: string;
34+
};
35+
36+
type AppBarData = {
37+
current: string;
38+
items: { [id: string]: AppBarItem };
39+
};
3040
*/
3141

3242
Object.assign(linker.standaloneDocTypes, [
@@ -62,13 +72,31 @@ exports.publish = async function publish(options /*: PublishOptions */) {
6272
fse.ensureDir(outDir);
6373

6474
const crawlData = crawl(docTree, index);
75+
const appBarItems = {
76+
"reference": {
77+
name: "API Reference",
78+
uri: index,
79+
},
80+
...(crawlData.tutorials && {
81+
"tutorials": {
82+
name: "Tutorials",
83+
uri: crawlData.tutorials.page ||
84+
crawlData.tutorials.children[Object.keys(crawlData.tutorials.children)[0]].page,
85+
},
86+
}),
87+
};
6588
const renderer = new TemplateRenderer(path.join(__dirname, "tmpl"), null, docTree)
6689
.setLayoutTemplate("layout.tmpl")
6790
.installPlugin("linker", linker)
6891
.installPlugin("generateIndex", indexSorterPlugin)
6992
.installPlugin("signature", signaturePlugin)
7093
.installPlugin("categoryFilter", categoryFilterPlugin)
71-
.installPlugin("relations", RelationsPlugin);
94+
.installPlugin("relations", RelationsPlugin)
95+
.setGlobalTemplateData({
96+
appBar: {
97+
items: appBarItems,
98+
},
99+
});
72100

73101
const pipeline = new TemplatePipeline(renderer).pipe(new TemplateTagsResolver());
74102

@@ -190,6 +218,7 @@ async function outReadme(
190218
}
191219

192220
pipeline.render("pages/main-page.tmpl", {
221+
appBar: {current: "reference"},
193222
document: null,
194223
readme,
195224
title: "Documentation",
@@ -212,6 +241,7 @@ function outIndexes(
212241
const url = linker.processInternalURI(indexList.url, {outputRelative: true});
213242

214243
pipeline.render("pages/api-index.tmpl", {
244+
appBar: {current: "reference"},
215245
documentList: indexList,
216246
title,
217247
env: config,
@@ -262,6 +292,7 @@ function outReference(
262292
);
263293
} else {
264294
pipeline.render("document.tmpl", {
295+
appBar: {current: "reference"},
265296
document: doc,
266297
title: doc.name,
267298
env: config,
@@ -282,6 +313,7 @@ function outTutorials(
282313
const uri = linker.getURI(tutorial);
283314

284315
pipeline.render("tutorial.tmpl", {
316+
appBar: {current: "tutorials"},
285317
document: tutorial,
286318
title: tutorial.title,
287319
env: config,

packages/webdoc-default-template/src/app/components/Header/Header.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export default connect(({explorerOpen}) => ({
1111
explorerOpen,
1212
setExplorerOpen,
1313
}) {
14+
const items = Object.entries(appData.appBar.items);
15+
const currentItemId = appData.appBar.current;
16+
1417
return (
1518
<div className="header__container">
1619
{explorerOpen ? null :
@@ -19,8 +22,14 @@ export default connect(({explorerOpen}) => ({
1922
toggleOpen={() => setExplorerOpen(!explorerOpen)}
2023
/>}
2124
<div className="header__contents">
22-
<a className="header__link header__link__current">API Reference</a>
23-
{/* <a className="header__link">Guides</a> */ /* TODO: Add guides */}
25+
{items.map(([id, appBarItem], i) => (
26+
<a
27+
className={`header__link${id === currentItemId ? " header__link__current" : ""}`}
28+
href={appBarItem.uri}
29+
>
30+
{appBarItem.name}
31+
</a>
32+
))}
2433
{appData.integrations.search && <Search integration={appData.integrations.search} />}
2534
</div>
2635
</div>

packages/webdoc-default-template/static/scripts/default-template.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9657,6 +9657,18 @@ function Search(_ref) {
96579657
}));
96589658
}
96599659
// CONCATENATED MODULE: ./src/app/components/Header/Header.js
9660+
function Header_slicedToArray(arr, i) { return Header_arrayWithHoles(arr) || Header_iterableToArrayLimit(arr, i) || Header_unsupportedIterableToArray(arr, i) || Header_nonIterableRest(); }
9661+
9662+
function Header_nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
9663+
9664+
function Header_unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return Header_arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return Header_arrayLikeToArray(o, minLen); }
9665+
9666+
function Header_arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
9667+
9668+
function Header_iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
9669+
9670+
function Header_arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
9671+
96609672

96619673

96629674

@@ -9676,6 +9688,8 @@ function Search(_ref) {
96769688
})(function Header(_ref2) {
96779689
var explorerOpen = _ref2.explorerOpen,
96789690
setExplorerOpen = _ref2.setExplorerOpen;
9691+
var items = Object.entries(appData.appBar.items);
9692+
var currentItemId = appData.appBar.current;
96799693
return React.createElement("div", {
96809694
className: "header__container"
96819695
}, explorerOpen ? null : React.createElement(ExplorerHeader, {
@@ -9685,9 +9699,16 @@ function Search(_ref) {
96859699
}
96869700
}), React.createElement("div", {
96879701
className: "header__contents"
9688-
}, React.createElement("a", {
9689-
className: "header__link header__link__current"
9690-
}, "API Reference"), appData.integrations.search && React.createElement(Search, {
9702+
}, items.map(function (_ref3, i) {
9703+
var _ref4 = Header_slicedToArray(_ref3, 2),
9704+
id = _ref4[0],
9705+
appBarItem = _ref4[1];
9706+
9707+
return React.createElement("a", {
9708+
className: "header__link".concat(id === currentItemId ? " header__link__current" : ""),
9709+
href: appBarItem.uri
9710+
}, appBarItem.name);
9711+
}), appData.integrations.search && React.createElement(Search, {
96919712
integration: appData.integrations.search
96929713
})));
96939714
}));

packages/webdoc-default-template/tmpl/layout.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Global layout render component.
44
*/
55
const rootData = obj;
6+
const appBar = rootData.appBar;
67
const document = rootData.document;
78
const config = rootData.env.template;
89
const siteRoot = this.getPlugin("linker").siteRoot;
@@ -27,6 +28,7 @@ const integrations = JSON.stringify(config.integrations);
2728
<script>
2829
window.appData = {
2930
applicationName: "<?js= config.applicationName ?>",
31+
appBar: JSON.parse(`<?js= JSON.stringify(appBar) ?>`),
3032
explorerTree: "<?js= siteRoot + "/explorer/" + (document && document.type === "TutorialDoc" ? "tutorials" : "reference") + ".json" ?>",
3133
siteRoot: "<?js= siteRoot ?>",
3234
integrations: JSON.parse(`<?js= integrations ?>`),

packages/webdoc-template-library/src/TemplateRenderer.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
import {tag, templateLogger} from "./Logger";
99
import {SymbolLinks} from "./SymbolLinks";
1010
import fs from "fs";
11+
import {merge} from "lodash";
1112
import path from "path";
1213

1314
let printedDefaultLinker = false;
@@ -65,6 +66,11 @@ export class TemplateRenderer {
6566
};
6667
}
6768

69+
setGlobalTemplateData(data: any): TemplateRenderer {
70+
Object.assign(this.data, data);
71+
return this;
72+
}
73+
6874
setLayoutTemplate(templateFile: string) /*: TemplateRenderer */ {
6975
this.layout = templateFile;
7076
return this;
@@ -132,7 +138,7 @@ export class TemplateRenderer {
132138
try {
133139
return _.template(fs.readFileSync(filePath, "utf8"), this.settings);
134140
} catch (e) {
135-
templateLogger.error(tag.TemplateLibrary, 'Failure loading template ' + filePath);
141+
templateLogger.error(tag.TemplateLibrary, "Failure loading template " + filePath);
136142
console.error(e);
137143
throw e;
138144
}
@@ -167,7 +173,7 @@ export class TemplateRenderer {
167173
} catch (e) {
168174
console.error(`Rendering template: ${filePath}`);
169175
console.error(this.cache[filePath].source
170-
.split('\n')
176+
.split("\n")
171177
.map((line, i) => `${i}: ${line}`)
172178
.join('\n'));
173179
throw e;
@@ -182,7 +188,8 @@ export class TemplateRenderer {
182188
* This method automaticaly applies layout if set.
183189
*
184190
* @param {string} filePath - file path of the template
185-
* @param {object} data - dictionary of all the variables & their values used in the template
191+
* @param {object} data - dictionary of all the variables & their values used in the template. For the
192+
* layout, this is deep merged with {@link TemplateRenderer#data}.
186193
* @return {string} Rendered template.
187194
*/
188195
render(filePath: string, data: any) {
@@ -195,9 +202,15 @@ export class TemplateRenderer {
195202

196203
// apply layout
197204
if (this.layout) {
198-
data.content = content;
205+
const templateData = Array.isArray(data) ? [...data] : { ...data };
206+
207+
for (const key in this.data) {
208+
templateData[key] = merge({}, this.data[key], templateData[key] || {});
209+
}
210+
211+
templateData.content = content;
199212
templateLogger.info(tag.TemplateLibrary, `Request layout ${filePath} ${data}`);
200-
content = this.partial(this.layout, data);
213+
content = this.partial(this.layout, templateData);
201214
}
202215

203216
templateLogger.info(tag.TemplateLibrary, `Rendering2 template ${filePath} ${data}`);

0 commit comments

Comments
 (0)