-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocview.js
More file actions
225 lines (171 loc) · 7.3 KB
/
docview.js
File metadata and controls
225 lines (171 loc) · 7.3 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
LiveG Docs
Copyright (C) LiveG. All Rights Reserved.
https://docs.liveg.tech
Licensed by the LiveG Open-Source Licence, which can be found at LICENCE.md.
*/
import * as $g from "https://opensource.liveg.tech/Adapt-UI/src/adaptui.js";
import * as astronaut from "https://opensource.liveg.tech/Adapt-UI/astronaut/astronaut.js";
import * as aside from "https://opensource.liveg.tech/Adapt-UI/src/aside.js";
import * as typeset from "https://opensource.liveg.tech/Typeset-Engine/src/typeset.js";
import * as markdown from "./markdown.js";
export const LANGUAGES = {
"en_GB": "English (United Kingdom)",
"fr_FR": "Français (France)"
};
export function getAllContentsPages(contents) {
var pages = [];
Object.keys(contents).forEach(function(key) {
var data = contents[key];
if (typeof(data) == "object") {
pages.push(...getAllContentsPages(data));
return;
}
pages.push(data);
});
return pages;
}
export var ContentsNode = astronaut.component("ContentsNode", function(props, children, inter) {
inter.pages = [];
var node = Container() (
Object.keys(props.data).map(function(key) {
var data = props.data[key];
if (typeof(data) == "object") {
var childNode = ContentsNode({
data,
productId: props.productId,
startingPage: props.startingPage,
root: props.root
}) ();
inter.pages.push(...childNode.inter.pages);
return Accordion (
Text(key),
childNode
);
}
var page = Page({showing: data == props.startingPage, classes: ["docView_page"]}) (
Section (
SkeletonLoader(_("docView_loadingArticle")) (
Heading(1) (),
astronaut.repeat(3, Paragraph() ()),
astronaut.repeat(3, Container (
Heading(2) (),
astronaut.repeat(3, Paragraph() ())
))
)
)
);
page.inter.load = function() {
$g.sel("title").setText(_("docView_title", {pageName: key}));
window.history.pushState({page: data}, window.title, `?product=${props.productId}&page=${data}`);
return fetch(`${props.root}/${data}`).then(function(response) {
return response.text();
}).then(function(contents) {
var renderedContents = $g.create("section").setHTML(markdown.toHtml(contents));
renderedContents.find("pre").setAttribute("dir", "ltr");
renderedContents.find("pre").items().forEach(function(element) {
var typesetElement = typeset.CodeEditor({
language: [...element.find("code").get().classList].find((name) => name.startsWith("language-"))?.replace(/^language-/, "") || "text",
code: element.getText().replace(/\n$/, ""),
readOnly: true,
adaptiveHeight: true
}) ();
element.get().replaceWith(typesetElement.get());
});
renderedContents.find("a").getAll().forEach(function(link) {
var element = $g.sel(link);
if (element.getAttribute("href")?.match(/^([a-zA-Z-]+:)?\/\/|^[a-zA-Z-]+:/)) {
element.setAttribute("target", "_blank");
} else {
element.setAttribute("href", `?product=${props.productId}&page=${element.getAttribute("href")}`);
}
});
renderedContents.find("img").getAll().forEach(function(link) {
var element = $g.sel(link);
if (!element.getAttribute("src")?.match(/^([a-zA-Z-]+:)?\/\/|^[a-zA-Z-]+:/)) {
element.setAttribute("src", `${props.root}/${element.getAttribute("src")}`);
}
});
page.clear().add(renderedContents);
return Promise.resolve();
});
};
if (data == props.startingPage) {
page.inter.load();
}
inter.pages.push(page);
if (key.startsWith("_")) {
return null;
}
var button = PageMenuButton({page, selected: data == props.startingPage}) ();
button.setHTML(markdown.toHtml(key));
button.setHTML(button.find("p").getHTML());
button.on("click", function() {
page.inter.load();
});
return button;
}).filter((button) => button != null)
);
return node;
});
export var DocViewScreen = astronaut.component("DocViewScreen", function(props, children) {
var menu = PageMenu() ();
var menuButton = HeaderPageMenuButton({alt: _("docView_showMenu")}) ();
var productsButton = IconButton({icon: "products", alt: _("docView_showProducts")}) ();
var languageMenu = Menu() ();
var languageMenuButton = IconButton({icon: "translate", alt: _("docView_switchLanguage")}) ();
var screen = Screen (
Header (
menuButton,
Text(props.product.name[props.locale] || props.product.name[props.fallbackLocale || "en_GB"]),
languageMenuButton,
productsButton
),
menu,
languageMenu
);
function loadContents(locale = props.locale) {
if (!props.product.name[locale]) {
locale = props.product.fallbackLocale;
}
fetch(`${props.product.docsRootUrl[locale]}/contents.json`).then(function(response) {
return response.json();
}).then(function(data) {
if (!getAllContentsPages(data).includes(props.startingPage)) {
data["_startingPage"] = props.startingPage;
}
screen.find(".docView_page").remove();
var contents = ContentsNode({
data,
productId: props.productId,
startingPage: props.startingPage,
root: props.product.docsRootUrl[locale]
}) ();
contents.inter.pages.map((page) => screen.add(page));
menu.clear().add(contents);
aside.addPages(menu.get());
menu.find("button[aui-selected]").ancestor("details").setAttribute("open", true);
});
}
languageMenu.add(
...Object.keys(props.product.docsRootUrl).map(function(locale) {
var button = MenuButton() (Text(LANGUAGES[locale] || locale));
button.on("click", function() {
loadContents(locale);
});
return button;
})
);
menuButton.on("click", function() {
menu.asideOpen();
});
languageMenuButton.on("click", function() {
languageMenu.menuOpen();
});
productsButton.on("click", function() {
screen.emit("showproducts");
});
loadContents();
return screen;
});
typeset.init();