-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
270 lines (236 loc) · 8.13 KB
/
Copy pathindex.ts
File metadata and controls
270 lines (236 loc) · 8.13 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
/**
* solid-shim - Minimal Solid data browser
*
* Drop-in replacement for mashlib using solid-panes-jss with solid-oidc authentication.
* Provides the same interface as mashlib for easy testing and migration.
*
* Supports @view proposal for JSON-LD (self-describing view hint)
* See: https://github.com/w3c/json-ld-syntax/issues/384
*/
import * as $rdf from 'rdflib'
import * as panes from 'solid-panes-jss'
import * as UI from 'solid-ui-jss'
import { authn, solidLogicSingleton, authSession, store } from 'solid-logic-jss'
import { versionInfo } from './versionInfo'
import { shimStyle, injectJssTheme } from './styles'
const global: any = window
const RDF_TYPE = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
/**
* Pane module interface for @view
*/
interface PaneModule {
name?: string
render: (subject: $rdf.NamedNode, context: PaneContext) => HTMLElement
}
interface PaneContext {
dom: Document
session: { store: $rdf.Store }
}
/**
* Parse JSON-LD data island from page into RDF store
*/
function parseJsonLdToStore(jsonld: any, baseUri: string, targetStore: $rdf.Store): $rdf.NamedNode {
const rootId = baseUri + (jsonld['@id'] || '#thing')
function addTriples(node: any, subjectUri: string) {
const subject = $rdf.sym(subjectUri)
// Add type
if (node['@type']) {
const typeUri = node['@type'].replace('schema:', 'http://schema.org/')
targetStore.add(subject, $rdf.sym(RDF_TYPE), $rdf.sym(typeUri))
}
// Add properties
Object.entries(node).forEach(([key, val]: [string, any]) => {
if (key.startsWith('@')) return
const pred = $rdf.sym(key.replace('schema:', 'http://schema.org/'))
if (Array.isArray(val)) {
val.forEach((item: any, i: number) => {
if (typeof item === 'object' && !item['@id']) {
const blankId = `${subjectUri}_${key}_${i}`
targetStore.add(subject, pred, $rdf.sym(blankId))
addTriples(item, blankId)
} else if (typeof item === 'object' && item['@id']) {
const uri = item['@id'].startsWith('http') ? item['@id'] : baseUri + item['@id']
targetStore.add(subject, pred, $rdf.sym(uri))
} else {
targetStore.add(subject, pred, item)
}
})
} else if (typeof val === 'object' && val['@id']) {
const uri = val['@id'].startsWith('http') ? val['@id'] : baseUri + val['@id']
targetStore.add(subject, pred, $rdf.sym(uri))
} else if (typeof val === 'object') {
const blankId = `${subjectUri}_${key}`
targetStore.add(subject, pred, $rdf.sym(blankId))
addTriples(val, blankId)
} else {
targetStore.add(subject, pred, val)
}
})
}
addTriples(jsonld, rootId)
return $rdf.sym(rootId)
}
/**
* Render JSON-LD using @view
*
* Implements the @view proposal: data specifies its own preferred renderer.
* See: https://github.com/w3c/json-ld-syntax/issues/384
*
* @param options.target - Element to render into (default: creates one after JSON-LD script)
* @param options.subject - Subject ID to render (default: @id from JSON-LD or '#thing')
* @param options.fallbackPane - Pane to use if @view fails or is missing
*/
async function renderView(options: {
target?: HTMLElement | string
subject?: string
fallbackPane?: PaneModule
} = {}): Promise<{ subject: $rdf.NamedNode; store: $rdf.Store } | null> {
// Find JSON-LD on page
const jsonLdScript = document.querySelector('script[type="application/ld+json"]')
if (!jsonLdScript) {
console.warn('[solid-shim] No JSON-LD found on page')
return null
}
let jsonld: any
try {
jsonld = JSON.parse(jsonLdScript.textContent || '')
} catch (e) {
console.error('[solid-shim] Invalid JSON-LD:', e)
return null
}
// Parse into store
const baseUri = window.location.href.split('#')[0]
const subjectId = options.subject || jsonld['@id'] || '#thing'
const subject = parseJsonLdToStore(jsonld, baseUri, store)
// Override subject if specified
const finalSubject = options.subject
? $rdf.sym(baseUri + options.subject)
: subject
// Load pane from @view or fallback
let pane: PaneModule | undefined
if (jsonld['@view']) {
try {
const viewUrl = new URL(jsonld['@view'], window.location.href).href
console.log(`[solid-shim] Loading @view: ${viewUrl}`)
const paneModule = await import(/* webpackIgnore: true */ viewUrl)
pane = paneModule.default || paneModule
} catch (err) {
console.warn(`[solid-shim] Failed to load @view "${jsonld['@view']}":`, err)
}
}
if (!pane && options.fallbackPane) {
pane = options.fallbackPane
}
if (!pane) {
console.warn('[solid-shim] No @view specified and no fallback pane provided')
return { subject: finalSubject, store }
}
// Find or create target element
let target: HTMLElement | null = null
if (typeof options.target === 'string') {
target = document.querySelector(options.target)
} else if (options.target) {
target = options.target
}
if (!target) {
target = document.createElement('div')
target.id = 'solid-shim-view'
jsonLdScript.parentNode?.insertBefore(target, jsonLdScript.nextSibling)
}
// Render
const context: PaneContext = {
dom: document,
session: { store }
}
try {
const rendered = pane.render(finalSubject, context)
target.appendChild(rendered)
} catch (e: any) {
console.error('[solid-shim] Render error:', e)
target.innerHTML = `<p style="color: #dc2626; padding: 1rem;">Error rendering: ${e.message}</p>`
}
return { subject: finalSubject, store }
}
// Inject JSS theme styles immediately
injectJssTheme()
// Expose globals - same interface as mashlib
global.$rdf = $rdf
global.panes = panes
global.UI = UI
global.SolidLogic = {
authn,
authSession,
store,
solidLogicSingleton
}
global.mashlib = {
versionInfo
}
// Expose @view renderer globally
global.solidShim = {
renderView,
parseJsonLdToStore
}
/**
* Initialize and run the Solid data browser
* @param uri - Optional URI to display initially
*/
global.panes.runDataBrowser = function (uri?: string | $rdf.NamedNode | null) {
// Apply layout styles
document.getElementById('PageBody')?.setAttribute('style', shimStyle.dbLayout)
document.getElementById('PageHeader')?.setAttribute('style', shimStyle.dbLayoutHeader)
document.getElementById('PageFooter')?.setAttribute('style', shimStyle.dbLayoutFooter)
document.getElementById('DummyUUID')?.setAttribute('style', shimStyle.dbLayoutContent)
// Set up cross-site proxy
const fetcher: any = $rdf.Fetcher
fetcher.crossSiteProxyTemplate = window.origin + '/xss/?uri={uri}'
// Add web monetization tag to page header
try {
const webMonetizationTag: HTMLElement = document.createElement('meta')
webMonetizationTag.setAttribute('name', 'monetization')
webMonetizationTag.setAttribute('content', `$${window.location.host}`)
document.head.appendChild(webMonetizationTag)
} catch (e) {
console.error('Failed to add web monetization tag to page header')
}
// Authenticate the user
authn.checkUser().then(function (_profile: any) {
const mainPage = panes.initMainPage(solidLogicSingleton.store, uri)
return mainPage
})
}
// Handle browser back/forward navigation
window.onpopstate = function (_event: any) {
global.document.outline?.GotoSubject?.(
$rdf.sym(window.document.location.href),
true,
undefined,
true,
undefined
)
}
// Legacy dump function for compatibility
function dump(msg: string[]) {
console.log(msg.slice(0, -1))
}
global.dump = dump
// Export for ES modules
export { versionInfo }
export { $rdf, panes, UI, authn, authSession, store, solidLogicSingleton }
export { renderView, parseJsonLdToStore }
export type { PaneModule, PaneContext }
// Auto-render: if JSON-LD with @view exists, render automatically
// No attribute needed - @view in data IS the opt-in
if (typeof document !== 'undefined') {
const jsonLdScript = document.querySelector('script[type="application/ld+json"]')
if (jsonLdScript) {
try {
const data = JSON.parse(jsonLdScript.textContent || '')
if (data['@view']) {
renderView()
}
} catch (e) {
// Invalid JSON-LD, skip auto-render
}
}
}