-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocumentations.ts
119 lines (116 loc) · 3.54 KB
/
documentations.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
import {
DocumentationMarkdownView,
StorageSettingsManager,
addCommand,
anyToError,
deepFreeze,
printError,
revealPrivate,
typedKeys,
} from "@polyipseity/obsidian-plugin-library"
import { DOMClasses2 } from "./magic.js"
import type { PLACEHOLDERPlugin } from "./main.js"
import changelogMd from "../CHANGELOG.md"
import readmeMd from "../README.md"
import semverLt from "semver/functions/lt.js"
export const DOCUMENTATIONS = deepFreeze({
async changelog(view: DocumentationMarkdownView.Registered, active: boolean) {
await view.open(active, {
data: await changelogMd,
displayTextI18nKey: "translation:generic.documentations.changelog",
iconI18nKey: "asset:generic.documentations.changelog-icon",
})
},
donate(view: DocumentationMarkdownView.Registered) {
const { context, context: { app, manifest } } = view
revealPrivate(context, [app], app0 => {
const { setting: { settingTabs } } = app0
for (const tab of settingTabs) {
const { id, containerEl: { ownerDocument } } = tab
if (id !== "community-plugins") { continue }
const div = ownerDocument.createElement("div")
tab.renderInstalledPlugin(manifest, div)
const element = div.querySelector(
`.${DOMClasses2.SVG_ICON}.${DOMClasses2.LUCIDE_HEART}`,
)?.parentElement
if (!element) { throw new Error(String(div)) }
element.click()
return
}
throw new Error(settingTabs.toString())
}, error => { throw error })
},
async readme(view: DocumentationMarkdownView.Registered, active: boolean) {
await view.open(active, {
data: await readmeMd,
displayTextI18nKey: "translation:generic.documentations.readme",
iconI18nKey: "asset:generic.documentations.readme-icon",
})
},
})
export type DocumentationKeys = readonly ["changelog", "donate", "readme"]
export const DOCUMENTATION_KEYS = typedKeys<DocumentationKeys>()(DOCUMENTATIONS)
class Loaded0 {
public constructor(
public readonly context: PLACEHOLDERPlugin,
public readonly docMdView: DocumentationMarkdownView.Registered,
) { }
public open(key: DocumentationKeys[number], active = true): void {
const {
context,
context: { version, language: { value: i18n }, localSettings },
docMdView,
} = this;
(async (): Promise<void> => {
try {
await DOCUMENTATIONS[key](docMdView, active)
if (key === "changelog" && version !== null) {
localSettings.mutate(lsm => {
lsm.lastReadChangelogVersion = version
}).then(async () => localSettings.write())
.catch((error: unknown) => { self.console.error(error) })
}
} catch (error) {
printError(
anyToError(error),
() => i18n.t("errors.error-opening-documentation"),
context,
)
}
})()
}
}
export function loadDocumentations(
context: PLACEHOLDERPlugin,
readme = false,
): loadDocumentations.Loaded {
const
{
version,
language: { value: i18n },
localSettings,
settings,
} = context,
ret = new Loaded0(
context,
DocumentationMarkdownView.register(context),
)
for (const doc of DOCUMENTATION_KEYS) {
addCommand(context, () => i18n.t(`commands.open-documentation-${doc}`), {
callback() { ret.open(doc) },
icon: i18n.t(`asset:commands.open-documentation-${doc}-icon`),
id: `open-documentation.${doc}`,
})
}
if (readme) { ret.open("readme", false) }
if (version !== null &&
settings.value.openChangelogOnUpdate &&
!StorageSettingsManager.hasFailed(localSettings.value) &&
semverLt(localSettings.value.lastReadChangelogVersion, version)) {
ret.open("changelog", false)
}
return ret
}
export namespace loadDocumentations {
export type Loaded = Loaded0
}