This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
generated from IzK-ArcOS/v6-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.ts
181 lines (141 loc) · 4.64 KB
/
runtime.ts
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
import { AppRuntime } from "$ts/apps/runtime";
import { getAllImages } from "$ts/images";
import { BadStatusIcon } from "$ts/images/status";
import { tryJsonConvert } from "$ts/json";
import { Process } from "$ts/process";
import { createErrorDialog } from "$ts/process/error";
import { readFile } from "$ts/server/fs/file";
import { sleep } from "$ts/util";
import { Store } from "$ts/writable";
import type { App, AppMutator } from "$types/app";
import { ArcFile } from "$types/fs";
import { HelpSupportAccelerators } from "./accelerators";
import { HelpArticleIndex } from "./types";
export class Runtime extends AppRuntime {
public File = Store<ArcFile>();
public buffer = Store<string>();
public path = Store<string>();
public wrapper = Store<HTMLDivElement>();
public Index = Store<HelpArticleIndex>([]);
public renderArticle = Store<boolean>(false);
public readonly STARTPATH = "@client/help/Home.md";
constructor(app: App, mutator: AppMutator, process: Process) {
super(app, mutator, process);
this.openedFile.subscribe(async (v) => {
if (!v) return;
await this.readFile(v);
});
this.process.accelerator.store.push(...HelpSupportAccelerators(this));
this._init();
}
private async _init() {
await this.readIndex();
const args = this.process.args;
this.handleOpenFile(!args.length || typeof args[0] != "string" ? this.STARTPATH : args[0]);
this.process.handler.dispatch.subscribe<string>(this.process.pid, "change-article", (page) => {
this.handleOpenFile(page);
});
this.process.handler.dispatch.dispatchToPid(this.pid, "snapping-set", "right");
}
async readIndex() {
const index = await readFile(`@client/help/index.json`);
if (!index) return this.indexReadError();
const data = await index.data.text();
const json = tryJsonConvert<HelpArticleIndex>(data);
if (typeof json === "string") return this.indexReadError();
this.Index.set(json);
}
async readFile(v: string) {
this.renderArticle.set(false);
const file = await readFile(v);
if (!file) return this.articleReadError(v);
this.path.set(v);
const content = await file.data.text();
this.buffer.set("");
await sleep(10);
this.buffer.set(content);
this.File.set(file);
setTimeout(() => {
this.setAnchorRedirects();
this.replaceIconSources();
this.renderArticle.set(true);
}, 100);
}
public setAnchorRedirects() {
const path = this.path.get();
const wrapper = this.wrapper.get();
if (!path || !wrapper) {
return false;
}
const anchors = wrapper.querySelectorAll("a");
for (const anchor of anchors) {
anchor.addEventListener("click", (e) => {
e.preventDefault();
const href = anchor.getAttribute("href");
if (!href.startsWith("@client")) return;
this.handleOpenFile(href);
});
}
}
public replaceIconSources() {
const path = this.path.get();
const wrapper = this.wrapper.get();
if (!path || !wrapper) {
return false;
}
const images = wrapper.querySelectorAll("img");
const icons = getAllImages();
for (const image of images) {
const src = image.getAttribute("src");
for (const id in icons) {
if (src == `#${id}`) {
image.setAttribute("src", icons[id]);
}
}
if (src.startsWith("@client")) image.setAttribute("src", src.replace("@client", "."));
}
}
public indexReadError() {
createErrorDialog(
{
title: "Load failed!",
message:
"Help & Support can't find the Index file! Without it, it doesn't know what articles are available. Try restarting ArcOS, if that doesn't work, create a GitHub issue.",
buttons: [
{
caption: "Close",
action: () => this.closeApp(),
suggested: true,
},
],
image: BadStatusIcon,
sound: "arcos.dialog.error",
},
this.pid,
true
);
return false;
}
public articleReadError(path = this.path.get()) {
createErrorDialog(
{
title: "Wisdom out of reach!",
message: `It seems you've found a glitch in the matrix. It's either that, or you modified the frontend. Regardless, the help article you tried to open doesn't exist.<br/><br/>Tried to open <code>${path}</code>`,
buttons: [
{
caption: "Go Home",
action: () => {
this.handleOpenFile(this.STARTPATH);
},
suggested: true,
},
],
image: BadStatusIcon,
sound: "arcos.dialog.error",
},
this.pid,
true
);
return false;
}
}