forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfe-symbol-link.js
More file actions
77 lines (65 loc) · 2.2 KB
/
Copy pathfe-symbol-link.js
File metadata and controls
77 lines (65 loc) · 2.2 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
// <fe-symbol-link> — Inline link to a documented Fe symbol.
//
// Usage:
// <fe-symbol-link symbol="mylib::Game/struct">Game</fe-symbol-link>
// <fe-symbol-link symbol="mylib::Game/struct"></fe-symbol-link>
//
// If no text content is provided, the symbol's display name is used.
// Links to the static docs site when FE_DOCS_BASE is set, otherwise
// renders as a hash link with hover info.
//
// Attributes:
// symbol — doc path (e.g. "mylib::Game/struct")
class FeSymbolLink extends HTMLElement {
static get observedAttributes() { return ["symbol", "src", "base"]; }
attributeChangedCallback(name, oldVal, newVal) {
if (oldVal === newVal) return;
if (name === "src") { this._loadSrc(); return; }
this._renderLink();
}
connectedCallback() {
if (this._userText == null) {
this._userText = this.textContent.trim();
}
this._loadSrc();
this._renderLink();
}
_loadSrc() {
var src = this.getAttribute("src");
if (!src) return;
var self = this;
feLoadSrc(src).then(function (result) {
self._index = result.index;
self._scip = result.scip;
self._renderLink();
});
}
_renderLink() {
var symbolPath = this.getAttribute("symbol");
if (!symbolPath) return;
var index = this._index || window.FE_DOC_INDEX;
if (!index || !index.items) {
if (!feWhenReady(this._renderLink.bind(this))) return;
}
var item = null;
if (index && index.items) {
for (var i = 0; i < index.items.length; i++) {
if (index.items[i].path === symbolPath) { item = index.items[i]; break; }
}
}
var displayText = this._userText || (item ? item.name : symbolPath.split("::").pop().split("/")[0]);
var docsBase = this.getAttribute("base") || window.FE_DOCS_BASE;
var a = document.createElement("a");
a.className = "fe-symbol-link type-link";
a.textContent = displayText;
a.href = (docsBase || "") + "#" + symbolPath;
feEnrichLink(a, symbolPath);
// Tooltip fallback from DocIndex
if (!a.title && item && item.docs && item.docs.summary) {
a.title = item.docs.summary;
}
this.innerHTML = "";
this.appendChild(a);
}
}
customElements.define("fe-symbol-link", FeSymbolLink);