forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.ts
More file actions
48 lines (39 loc) · 1.45 KB
/
Copy pathview.ts
File metadata and controls
48 lines (39 loc) · 1.45 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
import { getContents, getSetContentsStatements } from './data'
import { ViewParams } from '../types'
export function view ({ container, subject, store, user }: ViewParams) {
toViewMode()
function toViewMode () {
const content = getContents(store, subject)
container.innerHTML = ''
const lines = content.split('\n')
lines.forEach((line) => {
container.appendChild(document.createTextNode(line))
container.appendChild(document.createElement('br'))
})
if (user) {
const editButton = document.createElement('button')
editButton.textContent = 'Edit'
editButton.addEventListener('click', (event) => {
event.preventDefault()
toEditMode()
})
container.appendChild(editButton)
}
}
function toEditMode () {
if (!user) {
return
}
const content = getContents(store, subject)
container.innerHTML = '<form><textarea></textarea><button type="submit">Save</button></form>'
const textArea = container.getElementsByTagName('textarea')[0]
textArea.textContent = content
const form = container.getElementsByTagName('form')[0]
form.addEventListener('submit', (event) => {
event.preventDefault()
const creationDate = new Date()
const [setContentDeletions, setContentAdditions] = getSetContentsStatements(textArea.value, creationDate, subject, store, user)
store.updater.update(setContentDeletions, setContentAdditions, toViewMode)
})
}
}