forked from breck7/pldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditApp.js
141 lines (123 loc) · 3.96 KB
/
editApp.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
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
const defaultAuthor = "Anon <[email protected]>"
const htmlEscaped = content => content.replace(/</g, "<")
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
class EditApp {
start() {
if (document.getElementById("submitButton")) this.startForm()
if (this.route === "create") this.startCreateForm()
const urlParams = new URLSearchParams(window.location.hash.replace("#", ""))
const commit = urlParams.get("commit")
if (commit) {
const base = "https://github.com/breck7/pldb/commit/"
document.getElementById(
"successLink"
).innerHTML = `<a target="_pldb" href="${base +
commit}">Success! Changes published as ${commit.substring(0, 7)}</a>`
window.location.hash = ""
}
}
startForm() {
Mousetrap.bind("mod+s", evt => {
document.getElementById("submitButton").click()
evt.preventDefault()
return false
})
this.updateAuthor()
this.updateQuickLinks()
this.startCodeMirror()
}
startCreateForm() {
document.getElementById(
"exampleSection"
).innerHTML = `Example:<br><pre>title Ruby
appeared 1995
type pl
creators Yukihiro Matsumoto
website https://www.ruby-lang.org</pre>`
}
async startCodeMirror() {
this.codeMirrorInstance = new jtree.TreeNotationCodeMirrorMode(
"custom",
() => pldbNode,
undefined,
CodeMirror
)
.register()
.fromTextAreaWithAutocomplete(document.getElementById("content"), {
lineWrapping: false,
lineNumbers: true
})
this.codeMirrorInstance.setSize(window.innerWidth / 2 - 50, 500)
// this.codeMirrorInstance.on("keyup", () => this._onCodeKeyUp())
}
updateAuthor() {
document.getElementById("authorLabel").innerHTML = htmlEscaped(this.author)
document.getElementById("author").value = this.author
}
get store() {
return window.localStorage
}
get author() {
let author = defaultAuthor
try {
const changedAuthor = this.store.getItem("author")
author = changedAuthor ?? author
} catch (err) {
console.error(err)
}
return author
}
setAuthor(name) {
this.store.setItem("author", name)
this.updateAuthor()
}
changeAuthor() {
const newValue = prompt(
`Enter new author name in format like ${defaultAuthor}`,
this.author || defaultAuthor
)
if (newValue === "") this.setAuthor(defaultAuthor)
if (newValue) this.setAuthor(newValue)
}
get content() {
return document.getElementById("content").value
}
get route() {
return location.pathname.split("/").pop()
}
updateQuickLinks() {
const { content } = this
if (!content) return
const tree = new jtree.TreeNode(content)
const id = tree.get("title")
const references = tree
.findNodes("reference")
.map(node => "Reference: " + node.getContent())
const links = ["website", "githubRepo", "wikipedia"]
.filter(key => tree.has(key))
.map(key => `${capitalizeFirstLetter(key)}: ${tree.get(key)}`)
const permalink = this.route
document.getElementById("quickLinks").innerHTML =
TreeUtils.linkify(`<b>PLDB on ${id}:</b><br>
Git: https://github.com/breck7/pldb/blob/main/database/things/${permalink}.pldb<br>
HTML page: https://pldb.com/languages/${permalink}.html
<br><br>
<b>Links about ${id}:</b><br>
${links.join("<br>")}
${references.join("<br>")}<br><br>
<b>Search for more information about ${id}:</b><br>
Google: https://www.google.com/search?q=${id}+programming+language<br>
Google w/time: https://www.google.com/search?q=${id}+programming+language&tbs=cdr%3A1%2Ccd_min%3A1%2F1%2F1980%2Ccd_max%3A12%2F31%2F1995&tbm=<br>
Google Scholar: https://scholar.google.com/scholar?q=${id}<br>
Google Groups: https://groups.google.com/forum/#!search/${id}<br>
Google Trends: https://trends.google.com/trends/explore?date=all&q=${id}<br>
DDG: https://duckduckgo.com/?q=${id}<br>`) +
`Wayback Machine: <a target="_blank" href="https://web.archive.org/web/20220000000000*/${id}">https://web.archive.org/web/20220000000000*/${id}</a>`
}
}
document.addEventListener("DOMContentLoaded", function(event) {
window.app = new EditApp()
window.app.start()
})