-
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (90 loc) · 2.93 KB
/
Copy pathindex.js
File metadata and controls
106 lines (90 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import Component from '@glimmer/component';
import { bind } from '@ember/runloop';
import appFiles from 'ember-cli-addon-docs/app-files';
import addonFiles from 'ember-cli-addon-docs/addon-files';
import { getOwner } from '@ember/application';
import { addonDocsConfig } from 'ember-cli-addon-docs/-private/config';
const tagToSize = { H2: 'xxs', H3: 'xxs' };
const tagToIndent = { H2: '0', H3: '4' };
const tagToMarginTop = { H2: '2', H3: '2' };
const tagToMarginBottom = { H2: '0', H3: '0' };
export default class XMain extends Component {
@service router;
@service docsRoutes;
@addonDocsConfig config;
@action
setupElement(element) {
if (typeof MutationObserver === 'undefined') return;
let target = element.querySelector('[data-current-page-index-target]');
this._mutationObserver = new MutationObserver(
bind(this, this.reindex, target),
);
this._mutationObserver.observe(target, { subtree: true, childList: true });
this.reindex(target);
}
@action
teardownElement() {
this._mutationObserver.disconnect();
}
reindex(target) {
let headers = Array.from(
target.querySelectorAll('.docs-h2, .docs-h3, .docs-md__h2, .docs-md__h3'),
);
this.args.onReindex(
headers.map((header) => {
return {
id: header.id,
text: header.dataset.text || header.textContent,
size: tagToSize[header.tagName],
indent: tagToIndent[header.tagName],
marginTop: tagToMarginTop[header.tagName],
marginBottom: tagToMarginBottom[header.tagName],
};
}),
);
}
get editCurrentPageUrl() {
let path = this.router.currentRouteName;
if (!path) {
// `router` doesn't exist for old ember versions via ember-try
return null;
}
let match = this._locateFile(path);
if (match) {
let { projectHref, addonPathInRepo, docsAppPathInRepo, primaryBranch } =
this.config;
let parts = [projectHref, 'edit', primaryBranch];
if (match.inTree === 'addon') {
parts.push(addonPathInRepo);
} else {
parts.push(docsAppPathInRepo);
}
parts.push(match.file);
return parts.filter(Boolean).join('/');
}
return null;
}
_locateFile(path) {
path = path.replace(/\./g, '/');
if (path === 'docs/api/item') {
let { projectName } = this.config;
let model = getOwner(this)
.lookup('route:application')
.modelFor('docs.api.item');
let filename = model.file.replace(new RegExp(`^${projectName}/`), '');
let file = addonFiles.find((f) => f.match(filename));
if (file) {
return { file, inTree: 'addon' };
}
} else {
let file = appFiles
.filter((file) => file.match(/\.(hbs|md)$/))
.find((file) => file.match(path));
if (file) {
return { file, inTree: 'app' };
}
}
}
}