forked from SolidOS/solid-panes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
322 lines (290 loc) · 8.41 KB
/
render.js
File metadata and controls
322 lines (290 loc) · 8.41 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Bookmark Pane - Full Implementation
*
* This file is lazy-loaded only when the user views a bookmark.
* It contains all the heavy logic: UI rendering, RDF queries, updates.
*/
/* global confirm, alert */
import * as UI from 'solid-ui-jss'
import * as $rdf from 'rdflib'
import { store } from 'solid-logic-jss'
const ns = UI.ns
const schema = $rdf.Namespace('http://schema.org/')
const dom = document
/**
* Render a single bookmark
*/
function renderBookmark (subject, context, container) {
const kb = context.session.store
const div = dom.createElement('div')
div.className = 'bookmark-item'
// Get bookmark properties
const name = kb.anyValue(subject, schema('name')) || 'Untitled'
const url = kb.any(subject, schema('url'))
const description = kb.anyValue(subject, schema('description'))
const dateCreated = kb.anyValue(subject, schema('dateCreated'))
// Title
const title = dom.createElement('h3')
title.className = 'bookmark-title'
if (url) {
const link = dom.createElement('a')
link.href = url.uri || url.value
link.target = '_blank'
link.rel = 'noopener noreferrer'
link.textContent = name
title.appendChild(link)
} else {
title.textContent = name
}
div.appendChild(title)
// URL display
if (url) {
const urlDiv = dom.createElement('div')
urlDiv.className = 'bookmark-url'
urlDiv.textContent = url.uri || url.value
div.appendChild(urlDiv)
}
// Description
if (description) {
const desc = dom.createElement('p')
desc.className = 'bookmark-description'
desc.textContent = description
div.appendChild(desc)
}
// Date
if (dateCreated) {
const date = dom.createElement('div')
date.className = 'bookmark-date'
date.textContent = 'Added: ' + new Date(dateCreated).toLocaleDateString()
div.appendChild(date)
}
// Delete button (if user has write access)
const deleteBtn = dom.createElement('button')
deleteBtn.className = 'bookmark-delete'
deleteBtn.textContent = '×'
deleteBtn.title = 'Delete bookmark'
deleteBtn.onclick = async () => {
if (confirm('Delete this bookmark?')) {
try {
// Remove all triples about this bookmark
const triples = kb.statementsMatching(subject, null, null, subject.doc())
await store.updater.update(triples, [])
div.remove()
} catch (e) {
alert('Failed to delete: ' + e.message)
}
}
}
div.appendChild(deleteBtn)
return div
}
/**
* Render the add bookmark form
*/
function renderAddForm (containerUri, context, onAdd) {
const form = dom.createElement('form')
form.className = 'bookmark-add-form'
form.innerHTML = `
<h4>Add Bookmark</h4>
<input type="text" name="name" placeholder="Title" required />
<input type="url" name="url" placeholder="https://example.com" required />
<textarea name="description" placeholder="Description (optional)"></textarea>
<button type="submit">Add Bookmark</button>
`
form.onsubmit = async (e) => {
e.preventDefault()
const formData = new FormData(form)
const name = formData.get('name')
const url = formData.get('url')
const description = formData.get('description')
try {
// Generate unique ID
const id = 'bookmark-' + Date.now()
const bookmarkUri = containerUri + '#' + id
const subject = $rdf.sym(bookmarkUri)
const doc = $rdf.sym(containerUri)
// Create triples
const ins = [
$rdf.st(subject, ns.rdf('type'), schema('Bookmark'), doc),
$rdf.st(subject, schema('name'), name, doc),
$rdf.st(subject, schema('url'), $rdf.sym(url), doc),
$rdf.st(subject, schema('dateCreated'), new Date().toISOString(), doc)
]
if (description) {
ins.push($rdf.st(subject, schema('description'), description, doc))
}
await store.updater.update([], ins)
// Clear form and refresh
form.reset()
if (onAdd) onAdd(subject)
} catch (e) {
alert('Failed to add bookmark: ' + e.message)
}
}
return form
}
/**
* Main render function
*/
export function render (subject, context, options) {
const kb = context.session.store
const div = dom.createElement('div')
div.className = 'bookmark-pane'
// Add styles
const style = dom.createElement('style')
style.textContent = `
.bookmark-pane {
padding: 1em;
font-family: system-ui, sans-serif;
}
.bookmark-item {
border: 1px solid #ddd;
border-radius: 8px;
padding: 1em;
margin-bottom: 1em;
position: relative;
background: #fafafa;
}
.bookmark-title {
margin: 0 0 0.5em 0;
font-size: 1.2em;
}
.bookmark-title a {
color: #0066cc;
text-decoration: none;
}
.bookmark-title a:hover {
text-decoration: underline;
}
.bookmark-url {
color: #666;
font-size: 0.9em;
margin-bottom: 0.5em;
}
.bookmark-description {
margin: 0.5em 0;
color: #333;
}
.bookmark-date {
font-size: 0.8em;
color: #999;
}
.bookmark-delete {
position: absolute;
top: 0.5em;
right: 0.5em;
background: #ff4444;
color: white;
border: none;
border-radius: 50%;
width: 24px;
height: 24px;
cursor: pointer;
font-size: 16px;
line-height: 1;
}
.bookmark-delete:hover {
background: #cc0000;
}
.bookmark-add-form {
border: 2px dashed #ccc;
border-radius: 8px;
padding: 1em;
margin-top: 1em;
}
.bookmark-add-form h4 {
margin: 0 0 1em 0;
}
.bookmark-add-form input,
.bookmark-add-form textarea {
display: block;
width: 100%;
padding: 0.5em;
margin-bottom: 0.5em;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.bookmark-add-form button {
background: #0066cc;
color: white;
border: none;
padding: 0.5em 1em;
border-radius: 4px;
cursor: pointer;
}
.bookmark-add-form button:hover {
background: #0055aa;
}
.bookmark-empty {
color: #666;
font-style: italic;
padding: 2em;
text-align: center;
}
`
div.appendChild(style)
// Header
const header = dom.createElement('h2')
header.textContent = '🔖 Bookmarks'
div.appendChild(header)
// Check what we're rendering
const types = kb.findTypeURIs(subject)
const isContainer = types[ns.ldp('Container').uri] || types[ns.ldp('BasicContainer').uri]
const isBookmark = types[schema('Bookmark').uri]
// Bookmark list container
const list = dom.createElement('div')
list.className = 'bookmark-list'
div.appendChild(list)
if (isBookmark) {
// Single bookmark view
list.appendChild(renderBookmark(subject, context, list))
} else if (isContainer) {
// Container with bookmarks
const contents = kb.each(subject, ns.ldp('contains'))
const bookmarks = contents.filter(item => {
const itemTypes = kb.findTypeURIs(item)
return itemTypes[schema('Bookmark').uri]
})
if (bookmarks.length === 0) {
const empty = dom.createElement('div')
empty.className = 'bookmark-empty'
empty.textContent = 'No bookmarks yet. Add one below!'
list.appendChild(empty)
} else {
bookmarks.forEach(bookmark => {
list.appendChild(renderBookmark(bookmark, context, list))
})
}
// Add form for containers
const addForm = renderAddForm(subject.uri, context, (newBookmark) => {
// Remove empty message if present
const empty = list.querySelector('.bookmark-empty')
if (empty) empty.remove()
// Add new bookmark to list
list.appendChild(renderBookmark(newBookmark, context, list))
})
div.appendChild(addForm)
}
return div
}
/**
* Create a new bookmark resource
*/
export async function mintNew (context, options) {
const newBase = options.newBase || context.session.store.any(null, ns.space('preferencesFile'))?.uri
if (!newBase) {
throw new Error('No location specified for new bookmark')
}
const bookmarkUri = newBase + 'bookmarks.ttl'
const subject = $rdf.sym(bookmarkUri + '#bookmark-' + Date.now())
const doc = $rdf.sym(bookmarkUri)
// Create minimal bookmark
const ins = [
$rdf.st(subject, ns.rdf('type'), schema('Bookmark'), doc),
$rdf.st(subject, schema('name'), 'New Bookmark', doc),
$rdf.st(subject, schema('dateCreated'), new Date().toISOString(), doc)
]
await store.updater.update([], ins)
return subject
}