-
Notifications
You must be signed in to change notification settings - Fork 0
/
doki.js
286 lines (267 loc) · 9.25 KB
/
doki.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const dokiObjects = new Map;
let dokiReady = false
function waitForDokiReady(callback) {
var interval = setInterval(function() {
if (dokiReady) {
clearInterval(interval);
callback();
}
}, 200);
}
function enMapDokiObject(e) {
dokiItem = JSON.parse(e.content)
dokiObjects.set(dokiItem.UID, dokiItem)
//console.log(dokiObjects)
}
function displaySingleDoki(id) {
if ((id !== null) && (id !== undefined)) {
if (id.length === 64) {
document.getElementById("maincontent").replaceChildren(prepWindow("doki_id"))
waitForDokiReady(function () {
document.getElementById("content").replaceChildren(renderOneDoki(id), renderEditDoki(id))
})
}
}
}
function displayDoki() {
document.getElementById("maincontent").replaceChildren(prepWindow())
document.getElementById("heading").innerText = "Doki"
document.getElementById("description").innerText = "Wiki with Stackerstani characteristics"
document.getElementById("content").replaceChildren(renderAllDoki())
rewriteURL("doki")
}
function renderAllDoki() {
all = document.createElement("div")
dokiObjects.forEach(function (d) {
doc = document.createElement("div")
doc.className = "box"
// doc.innerText = d.GoalOrProblem
saz = dokiObjects.get(d.UID)
if (saz.Patches !== null ){
saz.Patches.some(patch =>{if ((patch.MergedBy.length !== 64) && (patch.RejectedBy.length !== 64)){
doc.innerHTML = '<strong>'+d.GoalOrProblem+'</strong>'
}else(doc.innerHTML = d.GoalOrProblem)})
}else{doc.innerHTML = d.GoalOrProblem}
// doc.innerHTML = d.GoalOrProblem
n = document.createElement("nav")
n.className = "level-item level-right"
a = document.createElement("a")
a.onclick = function () {
setURLID("doki_id", d.UID)
}
a.innerText = "VIEW"
n.appendChild(a)
doc.appendChild(n)
all.appendChild(doc)
})
all.appendChild(renderDokiNewDocumentForm())
return all
}
function renderEditDoki(id) {
form = document.createElement("div")
ta = document.createElement("textarea")
ta.rows = 20
ta.style.width = "100%"
saz = dokiObjects.get(id)
ta.textContent = saz.CurrentTip
ta.onfocus = function () {
if (!accountIsInIdentityTree(pubKeyMinus2)) {
alert("You must be in the Identity Tree to submit changes")
}
}
ta.onkeyup = function () {
var dmp = new diff_match_patch()
d = dmp.diff_main(saz.CurrentTip, ta.value)
dmp.diff_cleanupSemantic(d)
document.getElementById("details").innerHTML = dmp.diff_prettyHtml(d)
}
submit = document.createElement("button")
submit.innerText = "Submit"
submit.onclick = function () {
patch = makePatch(saz.CurrentTip, ta.value)
makeAndSendKind641202(saz.UID, patch, "replace with form", saz.Sequence)
}
form.appendChild(ta)
form.appendChild(submit)
return form
}
function makeAndSendKind641202(document_uid, patch, problem, sequence) {
content = JSON.stringify({
document_uid: document_uid,
patch: patch,
problem: problem,
sequence: sequence+1
})
p = makeEvent(content, "", 641202)
signHash(p.id).then(
function (result) {
p.sig = result
sendIt(p)
console.log(p)
location.reload()
}
)
}
function makePatch(original, modification) {
var dmp = new diff_match_patch()
d = dmp.diff_main(original, modification)
dmp.diff_cleanupSemantic(d)
//dmp.diff_prettyHtml(d)
// document.getElementById("details").innerHTML = dmp.diff_prettyHtml(d)
console.log(d)
p = dmp.patch_make(saz.CurrentTip, d)
console.log(p)
ps = dmp.patch_toText(p)
return ps
}
const classMap = {
h1: 'title is-1',
h2: 'title is-2',
h3: 'title is-3',
h4: 'title is-4',
h6: 'subtitle'
}
const bindings = Object.keys(classMap)
.map(key => ({
type: 'output',
regex: new RegExp(`<${key}(.*)>`, 'g'),
replace: `<${key} class="${classMap[key]}" $1>`
}));
function renderOneDoki(id) {
doc = document.createElement("div")
doc.className = "content is-medium"
//doc.className = "box"
saz = dokiObjects.get(id)
if (saz !== undefined) {
goal_or_problem = document.createElement("div")
goal_or_problem.className = "box"
goal_or_problem.innerText = saz.GoalOrProblem
// goal_or_problem.innerHTML = '<strong>'+saz.GoalOrProblem+'</strong>'
doc.appendChild(goal_or_problem)
md = new showdown.Converter({
extensions: [...bindings]
})
//console.log(showdown.getDefaultOptions(md))
ht = md.makeHtml(saz.CurrentTip)
mdht = document.createElement("div")
mdht.innerHTML = ht
doc.appendChild(mdht)
let rendered = false
if (saz.Patches !== null) {
saz.Patches.forEach(function (patch) {
if ((patch.MergedBy.length !== 64) && (patch.RejectedBy.length !== 64) && !rendered) {
var dmp = new diff_match_patch()
p = dmp.patch_fromText(patch.Patch)
pretty = document.createElement("div")
//console.log(p)
p.forEach(function (data) {
pretty.innerHTML += dmp.diff_prettyHtml(data.diffs)
pretty.innerHTML += '<br /><br />'
})
mrg = document.createElement("button")
mrg.innerText = "Merge"
mrg.onclick = function () {
if (!accountIsInMaintainerTree(pubKeyMinus2)) {
alert("You must be in the Maintainer Tree to merge changes")
} else {
makeAndSendKind641204(id, patch.EventID, 2, saz.Sequence, "")
}
//console.log(saz)
}
mrg.className = "button is-primary"
document.getElementById("details").replaceChildren(pretty, mrg)
rendered = true
}
})
}
return doc
}
doc.innerText = "Not found"
return doc
}
function makeAndSendKind641204(document_uid, patch_uid, operation, sequence, reason) {
content = JSON.stringify({
document_uid: document_uid,
patch_uid: patch_uid,
operation: operation,
reason: reason,
sequence: sequence+1
})
p = makeEvent(content, "", 641204)
signHash(p.id).then(
function (result) {
p.sig = result
sendIt(p)
console.log(p)
location.reload()
}
)
}
function renderDokiNewDocumentForm() {
let form = document.createElement("div")
form.innerHTML = `Create a new document<br /><textarea class="textarea is-link" id="document goal_or_problem input" maxlength="280" rows="4" cols="70" placeholder="the goal of this document, or the problem that it is solving. MUST begin with Goal: or Problem:" style="width: 100%; max-width: 600px;"></textarea>
<button class="button is-primary" onclick="createNewDocument( document.getElementById( 'document goal_or_problem input' ).value)">Create new Doki</button><br><br>`
return form
}
async function createNewPatch(document_id, patch, problem) {
content = JSON.stringify({
document_uid: document_id,
patch: patch,
problem: problem,
sequence: 1
})
p = makeEvent(content, "", 641202)
signHash(p.id).then(
function (result) {
p.sig = result
sendIt(p)
console.log(p)
return p.id
}
)
}
async function createNewDocument(goal_or_problem) {
if (!accountIsInIdentityTree(pubKeyMinus2)) {
alert("You must be in the Identity Tree to do that")
} else {
content = JSON.stringify({
goal_or_problem: goal_or_problem
})
p = makeEvent(content, "", 641200)
signHash(p.id).then(
function (result) {
p.sig = result
sendIt(p)
console.log(p)
location.reload()
}
)
}
}
function dokiInABubble(doki) {
saz = dokiObjects.get(doki)
if (saz !== undefined) {
md = new showdown.Converter({
extensions: [...bindings]
})
ht = md.makeHtml(saz.CurrentTip)
mdht = document.createElement("div")
mdht.innerHTML = ht
mdht.className = "content"
edit = document.createElement("button")
edit.className = "button is-link"
edit.innerText = "Edit this"
edit.onclick = function () {
if (!accountIsInIdentityTree(pubKeyMinus2)) {
alert("You must be in the Identity Tree to submit edits")
}
setURLID("doki_id", doki)
}
box = document.createElement("div")
box.className = "notification is-primary"
box.appendChild(mdht)
box.appendChild(edit)
return box
}
return
}