-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathcomponent.js
90 lines (69 loc) · 2.61 KB
/
component.js
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
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { bind } from '@ember/runloop';
import { computed } from '@ember/object';
import appFiles from 'ember-cli-addon-docs/app-files';
import addonFiles from 'ember-cli-addon-docs/addon-files';
import config from 'dummy/config/environment';
import { getOwner } from '@ember/application';
import layout from './template';
const { projectHref, primaryBranch } = config['ember-cli-addon-docs'];
const tagToSize = { H2: 'xs', H3: 'xs' };
const tagToIndent = { H2: '0', H3: '4' };
const tagToMarginTop = { H2: '2', H3: '2' };
const tagToMarginBottom = { H2: '0', H3: '0' };
export default Component.extend({
layout,
router: service(),
docsRoutes: service(),
tagName: 'main',
classNames: ['lg:docs-w-4/5', 'xl:docs-w-3/5', 'docs-max-w-md', 'lg:docs-max-w-none', 'docs-mx-auto', 'lg:docs-mx-0', 'docs-mt-6', 'docs-flex-no-shrink'],
didInsertElement() {
this._super(...arguments);
let target = this.element.querySelector('[data-page-index-target]')
this._mutationObserver = new MutationObserver(bind(this, this.reindex, target))
this._mutationObserver.observe(target, { subtree: true, childList: true });
this.reindex(target);
},
willDestroyElement() {
this._super(...arguments);
this._mutationObserver.disconnect();
},
reindex(target) {
let headers = Array.from(
target.querySelectorAll('.docs-h2, .docs-h3, .docs-md__h2, .docs-md__h3')
);
this.get('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],
};
})
);
},
editCurrentPageUrl: computed('router.currentRouteName', function() {
let path = this.get('router.currentRouteName');
if (!path) {
// `router` doesn't exist for old ember versions via ember-try
return;
}
path = path.replace(/\./g, '/');
if (path === 'docs/api/item') {
let { path } = getOwner(this).lookup('route:application').paramsFor('api.item');
let file = addonFiles.find(f => f.match(path));
if (file) {
return `${projectHref}/edit/${primaryBranch}/addon/${file}`;
}
} else {
let file = appFiles
.filter(file => file.match(/template.+(hbs|md)/))
.find(file => file.match(path));
return `${projectHref}/edit/${primaryBranch}/tests/dummy/app/${file}`;
}
})
});