Skip to content

Commit c0c1eab

Browse files
committed
wip: instances select editor
1 parent c36924e commit c0c1eab

48 files changed

Lines changed: 544 additions & 467 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

demos/storybook/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@coreui/react": "^5.4.1",
1212
"@coreui/coreui": "^5.2.0",
1313
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
14+
"@hydrofoil/shaperone-core": "^0.12.1",
1415
"@hydrofoil/shaperone-wc": "^0.8.0",
1516
"@hydrofoil/shaperone-wc-material": "^0.6.1",
1617
"@hydrofoil/shaperone-wc-shoelace": "^0.4.1",
@@ -34,11 +35,13 @@
3435
"react-syntax-highlighter": "^15.6.1",
3536
"remark-gfm": "^4.0.0",
3637
"rollup-plugin-polyfill-node": "^0.13.0",
38+
"sparql-http-client": "^3.0.1",
3739
"storybook": "^8.4.5",
3840
"string-to-stream": "^3.0.1",
3941
"ts-deepmerge": "^7"
4042
},
4143
"devDependencies": {
42-
"@types/react-syntax-highlighter": "^15.5.13"
44+
"@types/react-syntax-highlighter": "^15.5.13",
45+
"@types/sparql-http-client": "^3"
4346
}
4447
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PREFIX ex: <http://example.org/>
2+
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
3+
prefix wd: <http://www.wikidata.org/entity/>
4+
5+
ex:instance <http://schema.org/alumniOf> wd:Q184478 .
6+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
2+
PREFIX wd: <http://www.wikidata.org/entity/>
3+
PREFIX schema: <http://schema.org/>
4+
PREFIX sh: <http://www.w3.org/ns/shacl#>
5+
6+
<> a sh:NodeShape ;
7+
sh:property
8+
[
9+
sh:path schema:alumniOf ;
10+
sh:class wd:Q3918 ;
11+
sh:name "Alma mater" ;
12+
sh:minCount 1 ;
13+
sh:maxCount 1 ;
14+
] ;
15+
.
16+
17+
wd:Q184478 a wd:Q3918 .
18+
wd:Q1860208 a wd:Q3918 .
19+
wd:Q49108 a wd:Q3918 .
20+
wd:Q34433 a wd:Q3918 .
21+
wd:Q35794 a wd:Q3918 .
22+
wd:Q13371 a wd:Q3918 .

demos/storybook/stories/DASH/EnumSelectEditor.stories.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import type { StoryObj as Story } from '@storybook/web-components'
2-
import { defaultMeta } from '../../common.js'
3-
import { render } from '../../render.js'
2+
import { createStory, defaultMeta } from '../../common.js'
43
import instances from '../../../shapes/editors/dash/InstancesSelect/wikidata.ttl?raw'
4+
import instancesNoLabels from '../../../shapes/editors/dash/InstancesSelect/no-labels.ttl?raw'
5+
import noLabelsData from '../../../shapes/editors/dash/InstancesSelect/no-labels.data.ttl?raw'
6+
import { configure as configureFetch } from './InstancesSelectEditor/fetch.js'
57

68
const meta = {
79
...defaultMeta,
@@ -12,13 +14,27 @@ export default meta
1214
/**
1315
* Just as with enum select, `rdfs:label` is used for option text
1416
*/
15-
export const Wikidata: Story = {
17+
export const Wikidata: Story = createStory({
1618
name: 'Resources by sh:class',
17-
args: {
18-
shapes: instances,
19-
customPrefixes: {
20-
wd: 'http://www.wikidata.org/entity/',
21-
},
19+
shapes: instances,
20+
prefixes: ['rdfs'],
21+
customPrefixes: {
22+
wd: 'http://www.wikidata.org/entity/',
2223
},
23-
render,
24-
}
24+
})()
25+
26+
/**
27+
* If the Shapes Graph does not contain `rdfs:label` for the options, the editor can request
28+
* that the instance data can be fetched from the server and added to the Shapes Graph.
29+
*/
30+
export const WikidataNoLabels: Story = createStory({
31+
name: 'Resources without labels',
32+
shapes: instancesNoLabels,
33+
data: noLabelsData,
34+
focusNode: 'http://example.org/instance',
35+
prefixes: ['rdfs', 'schema'],
36+
customPrefixes: {
37+
wd: 'http://www.wikidata.org/entity/',
38+
ex: 'http://example.org/',
39+
},
40+
})(configureFetch)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { InstanceConfigCallback } from '@hydrofoil/shaperone-wc/configure.js'
2+
import $rdf from '@hydrofoil/shaperone-core/env.js'
3+
import SparqlClient from 'sparql-http-client/ParsingClient.js'
4+
5+
const wikidata = new SparqlClient({
6+
endpointUrl: 'https://query.wikidata.org/sparql',
7+
})
8+
9+
const labelQuery = `
10+
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
11+
12+
construct {
13+
?id rdfs:label ?l
14+
} where {
15+
?id rdfs:label ?l
16+
}`
17+
18+
export const configure: InstanceConfigCallback = ({ shapes }) => {
19+
const env = $rdf()
20+
21+
shapes.setFetchStrategy((resources, shapesGraph) => {
22+
const toFetch = resources
23+
// skip resources for which we already have data
24+
.filter(resource => !shapesGraph.node(resource).out(env.ns.rdfs.label).terms.length)
25+
26+
if (toFetch.length === 0) {
27+
return null
28+
}
29+
30+
return wikidata.query.construct(`${labelQuery}
31+
VALUES ?id {
32+
${toFetch.map(resource => `<${resource.value}>`).join(' ')}
33+
}`)
34+
})
35+
}

demos/storybook/stories/common.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { Meta, StoryObj as Story } from '@storybook/web-components'
2-
import type { ConfigCallback } from '@hydrofoil/shaperone-wc'
32
import { merge } from 'ts-deepmerge'
3+
import type { InstanceConfigCallback } from '@hydrofoil/shaperone-wc/configure.js'
44
import { render } from './render.js'
55
import groups from '../shapes/groups.ttl?raw'
66
import textEditors from '../shapes/text-editors.ttl?raw'
77

88
interface StoryFactory {
9-
(configure: ConfigCallback, overrides?: Partial<Omit<Story, 'render' | 'loaders'>>): Story
9+
(configure?: InstanceConfigCallback, overrides?: Partial<Omit<Story, 'render' | 'loaders'>>): Story
1010
}
1111

1212
export const defaultMeta: Meta = {
@@ -50,9 +50,10 @@ interface CreateStory {
5050
data?: string
5151
focusNode?: string
5252
prefixes?: string[]
53+
customPrefixes?: Record<string, string>
5354
}
5455

55-
export function createStory({ name, prefixes, ...args }: CreateStory) {
56+
export function createStory({ name, prefixes, customPrefixes, ...args }: CreateStory) {
5657
const defaults = {
5758
name,
5859
args,
@@ -62,6 +63,10 @@ export function createStory({ name, prefixes, ...args }: CreateStory) {
6263
defaults.args.prefixes = prefixes.join(',')
6364
}
6465

66+
if (customPrefixes) {
67+
defaults.args.customPrefixes = customPrefixes
68+
}
69+
6570
return createStoryObj(defaults)
6671
}
6772

package-lock.json

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)