Skip to content

Commit 302b360

Browse files
committed
addeda URL lit converter
1 parent d40ac8b commit 302b360

2 files changed

Lines changed: 33 additions & 37 deletions

File tree

src/components/rdf-form/RDFForm.ts

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ import { sym, Namespace, LiveStore } from 'rdflib'
88
import '@/components/rdf-input'
99
import { DEFAULT_STORE, storeContext, StoreContext } from '@/lib/forms/store/StoreContext'
1010

11+
const urlConverter = {
12+
fromAttribute (value: string | null): URL | null {
13+
if (!value) return null
14+
15+
try {
16+
return new URL(value)
17+
} catch {
18+
return null
19+
}
20+
},
21+
toAttribute (value: URL | null) {
22+
if (!value) return null
23+
return value
24+
}
25+
}
26+
27+
const hrefFromUrlValue = (value: URL | null): string =>
28+
value?.href ?? ''
29+
1130
@customElement('solid-ui-rdf-form')
1231
export default class RDFForm extends WebComponent {
1332
@consume({ context: storeContext, subscribe: true })
@@ -27,12 +46,6 @@ export default class RDFForm extends WebComponent {
2746
@state()
2847
private accessor entireDataIsReadonly: boolean = false
2948

30-
@state()
31-
private accessor _parsedUrl: URL | null = null
32-
33-
@state()
34-
private accessor _parsedUrl2: URL | null = null
35-
3649
@state()
3750
private accessor _loadVersion = 0
3851

@@ -48,18 +61,8 @@ export default class RDFForm extends WebComponent {
4861
@property({ type: String })
4962
accessor rdfName = ''
5063

51-
@property({ type: String })
52-
set rdfURI (value: string) {
53-
try {
54-
this._parsedUrl = new URL(value)
55-
} catch {
56-
this._parsedUrl = null // Handle invalid URL
57-
}
58-
}
59-
60-
get rdfURI (): string {
61-
return this._parsedUrl ? this._parsedUrl.href : ''
62-
}
64+
@property({ converter: urlConverter })
65+
accessor rdfURI: URL | null = null
6366

6467
@property({ type: String })
6568
accessor whichSubject = 'me'
@@ -70,33 +73,26 @@ export default class RDFForm extends WebComponent {
7073
@property({ type: String })
7174
accessor subjectName = ''
7275

73-
@property({ type: String })
74-
set subjectURI (value: string) {
75-
try {
76-
this._parsedUrl2 = new URL(value)
77-
} catch {
78-
this._parsedUrl2 = null // Handle invalid URL
79-
}
80-
}
81-
82-
get subjectURI (): string {
83-
return this._parsedUrl2 ? this._parsedUrl2.href : ''
84-
}
76+
@property({ converter: urlConverter })
77+
accessor subjectURI: URL | null = null
8578

8679
render () {
80+
console.log('subjectURI ', this.subjectURI)
81+
console.log('rdfURI ', this.rdfURI)
8782
if (!this._documentsLoaded) {
8883
return html``
8984
}
9085

9186
const store = this.currentStoreContext.store
87+
const subjectURI = hrefFromUrlValue(this.subjectURI)
9288

93-
if (store.updater?.editable(this.subjectURI) === false) {
89+
if (subjectURI && store.updater?.editable(subjectURI) === false) {
9490
this.entireDataIsReadonly = true
9591
}
9692

97-
const document = sym(this.rdfURI) // rdflib NamedNode for the document
93+
const document = sym(hrefFromUrlValue(this.rdfURI)) // rdflib NamedNode for the document
9894
const exactForm = this.whichForm // If there are more 'a ui:Form' elements in a form file
99-
const formThis = Namespace(this.rdfURI + '#')(exactForm) // NamedNode for #this in the form
95+
const formThis = Namespace(`${hrefFromUrlValue(this.rdfURI)}#`)(exactForm) // NamedNode for #this in the form
10096

10197
const parts = store.each(formThis, ns.ui('parts'), null, document)
10298
const partsBySequence = sortBySequence(store, parts)
@@ -116,7 +112,7 @@ export default class RDFForm extends WebComponent {
116112
fieldValue: hashIndex >= 0 ? value.slice(hashIndex + 1) : value
117113
}
118114
})
119-
const me = Namespace(this.subjectURI + '#')(this.whichSubject)
115+
const me = Namespace(`${hrefFromUrlValue(this.subjectURI)}#`)(this.whichSubject)
120116

121117
return html`
122118
<form>
@@ -187,8 +183,8 @@ export default class RDFForm extends WebComponent {
187183

188184
private async loadDocumentsIfNeeded () {
189185
const store = this.currentStoreContext.store
190-
const rdfURI = this.rdfURI
191-
const subjectURI = this.subjectURI
186+
const rdfURI = hrefFromUrlValue(this.rdfURI)
187+
const subjectURI = hrefFromUrlValue(this.subjectURI)
192188

193189
if (!rdfURI || !subjectURI) return
194190

src/components/rdf-form/RDForm.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const render = defineStoryRender<typeof meta.argTypes>(({ rdfTurtleFormatSource,
105105
return html`
106106
<solid-ui-rdf-form
107107
rdfTurtleFormatSource=${rdfTurtleFormatSource}
108-
rdfURI=${rdfURI}
108+
.rdfURI=${new URL(rdfURI)}
109109
whichForm=${whichForm}
110110
rdfName=${rdfName}
111111
subjectTurtleFormatSource=${subjectTurtleFormatSource}

0 commit comments

Comments
 (0)