Skip to content

Commit

Permalink
revert: editor matching always received object
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 12, 2020
1 parent 79b0307 commit 0d4be59
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 94 deletions.
6 changes: 3 additions & 3 deletions demos/examples/NestedShapesIndividually/components.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { dash } from '@tpluscode/rdf-ns-builders'
import { Component, html } from '@hydrofoil/shaperone-wc'
import { html, SingleEditorComponent } from '@hydrofoil/shaperone-wc'

export const shapeLink: Component = {
export const shapeLink: SingleEditorComponent = {
editor: dash.DetailsEditor,

render({ value }, { focusOnObjectNode }) {
return html`<a href="javascript:void(0)" @click="${focusOnObjectNode}">Edit ${value.object.value}</a>`
return html`<a href="javascript:void(0)" @click="${focusOnObjectNode}">Edit ${value.object?.value}</a>`
},
}
14 changes: 7 additions & 7 deletions packages/core/DashEditors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const textField: SingleEditor = {
match(shape: PropertyShape, value) {
let datatype = shape.datatype?.id

if (value?.term.termType === 'Literal') {
if (value.term.termType === 'Literal') {
datatype = value.term.datatype
}

Expand All @@ -31,11 +31,11 @@ export const textArea: SingleEditor = {
match(shape: PropertyShape, value) {
const singleLine = shape.pointer.out(dash.singleLine).term

if (isString(value?.term)) {
if (isString(value.term)) {
if (singleLine?.equals(booleanTrue)) {
return 0
}
if (singleLine?.equals(booleanFalse) || value?.value.includes('\n')) {
if (singleLine?.equals(booleanFalse) || value.value.includes('\n')) {
return 20
}

Expand All @@ -57,7 +57,7 @@ export const shape: SingleEditor = {
return 20
}

if (value?.term.termType === 'BlankNode' || value?.term.termType === 'NamedNode') {
if (value.term.termType === 'BlankNode' || value.term.termType === 'NamedNode') {
return null
}

Expand All @@ -83,7 +83,7 @@ export const enumSelect: SingleEditor = {
export const datePicker: SingleEditor<Literal> = {
term: dash.DatePickerEditor,
match(shape, value) {
if (xsd.date.equals(value?.term.datatype)) {
if (xsd.date.equals(value.term.datatype)) {
return 15
}
if (shape.datatype?.equals(xsd.date)) {
Expand All @@ -97,7 +97,7 @@ export const datePicker: SingleEditor<Literal> = {
export const dateTimePicker: SingleEditor<Literal> = {
term: dash.DateTimePickerEditor,
match(shape, value) {
if (xsd.dateTime.equals(value?.term.datatype)) {
if (xsd.dateTime.equals(value.term.datatype)) {
return 15
}
if (shape.datatype?.equals(xsd.dateTime)) {
Expand All @@ -118,7 +118,7 @@ export const instancesSelectEditor: SingleEditor<NamedNode | BlankNode> = {
export const uriEditor: SingleEditor<NamedNode> = {
term: dash.URIEditor,
match(shape, object) {
if (object?.term.termType !== 'NamedNode' || shape.class) {
if (object.term.termType !== 'NamedNode' || shape.class) {
return 0
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/models/editors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface MultiEditor extends EditorMatcher {
}

export interface SingleEditor<T extends Term = Term> extends EditorMatcher {
match: (shape: PropertyShape, value?: GraphPointer<T>) => number | null
match: (shape: PropertyShape, value: GraphPointer<T>) => number | null
}

export type Editor<T extends EditorMatcher = SingleEditor | MultiEditor> = T & {
Expand Down
27 changes: 26 additions & 1 deletion packages/core/models/editors/lib/match.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PropertyShape } from '@rdfine/shacl'
import type { GraphPointer } from 'clownface'
import { xsd } from '@tpluscode/rdf-ns-builders'
import type { EditorsState, SingleEditor, SingleEditorMatch, Editor, MultiEditor } from '../index'

function toDefined<T>(arr: T[], next: T | undefined): T[] {
Expand All @@ -17,9 +18,33 @@ function byScore(left: { score: number | null }, right: { score: number | null }
return rightScore - leftScore
}

export function matchSingleEditors(this: EditorsState, { shape, object }: {shape: PropertyShape; object?: GraphPointer }): SingleEditorMatch[] {
function valuePlaceholder(shape: PropertyShape): GraphPointer {
switch (shape.nodeKind?.value) {
case 'http://www.w3.org/ns/shacl#BlankNode':
case 'http://www.w3.org/ns/shacl#BlankNodeOrIRI':
case 'http://www.w3.org/ns/shacl#BlankNodeOrLiteral':
return shape.pointer.blankNode()
case 'http://www.w3.org/ns/shacl#IRI':
case 'http://www.w3.org/ns/shacl#IRIOrLiteral':
return shape.pointer.namedNode('')
default: {
if (shape.languageIn.length || shape.datatype?.equals(xsd.langString)) {
return shape.pointer.literal('', shape.languageIn[0] || 'en')
}
if (shape.datatype?.id.termType === 'NamedNode') {
return shape.pointer.literal('', shape.datatype.id)
}

return shape.pointer.literal('')
}
}
}

export function matchSingleEditors(this: EditorsState, { shape, ...rest }: { shape: PropertyShape; object?: GraphPointer }): SingleEditorMatch[] {
const singleEditors = Object.values(this.singleEditors).reduce<Editor<SingleEditor>[]>(toDefined, [])

const object = rest.object || valuePlaceholder(shape)

return singleEditors.map(editor => ({ ...editor, score: editor.match(shape, object) }))
.filter(match => match.score === null || match.score > 0)
.sort(byScore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function (store: Store) {
}

const pointer = defaultValue(property, focusNode)
if (!pointer.values.length) {
if (!pointer) {
return
}

Expand Down
29 changes: 5 additions & 24 deletions packages/core/models/resources/lib/defaultValue.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import type { PropertyShape } from '@rdfine/shacl'
import type { AnyPointer, GraphPointer, MultiPointer } from 'clownface'
import type { GraphPointer, MultiPointer } from 'clownface'
import { dash, rdf, sh } from '@tpluscode/rdf-ns-builders'
import type { RdfResource, ResourceIdentifier } from '@tpluscode/rdfine'
import type { ResourceIdentifier } from '@tpluscode/rdfine'
import type { FocusNode } from '../../../index'
import * as datatypes from '../../../lib/datatypes'

function defaultNumericValue(datatype: RdfResource | undefined, graph: AnyPointer): GraphPointer | null {
const numericDatatype = datatypes.numericDatatype(datatype?.id)
if (numericDatatype) {
return graph.literal('0', numericDatatype)
}

return null
}

function defaultStringValue(datatype: RdfResource | undefined, graph: AnyPointer) {
if (datatype?.id.termType === 'NamedNode') {
return graph.literal('', datatype?.id)
}

return graph.literal('')
}

export function defaultValue(property: PropertyShape, focusNode: FocusNode): MultiPointer {
export function defaultValue(property: PropertyShape, focusNode: FocusNode): MultiPointer | null {
const { nodeKind } = property

if (property.defaultValue) {
return focusNode.node(property.defaultValue)
}

if (property.class || nodeKind?.equals(sh.IRI) || nodeKind?.equals(sh.BlankNode) || nodeKind?.equals(sh.BlankNodeOrIRI)) {
if (property.class && (nodeKind?.equals(sh.IRI) || nodeKind?.equals(sh.BlankNode) || nodeKind?.equals(sh.BlankNodeOrIRI))) {
const resourceNode: GraphPointer<ResourceIdentifier> = nodeKind?.equals(sh.IRI) ? focusNode.namedNode('') : focusNode.blankNode()
const propertyClass = property.class
if (propertyClass && !property.get(dash.editor)?.equals(dash.InstancesSelectEditor)) {
Expand All @@ -39,6 +21,5 @@ export function defaultValue(property: PropertyShape, focusNode: FocusNode): Mul
return resourceNode
}

const { datatype } = property
return defaultNumericValue(datatype, focusNode) || defaultStringValue(datatype, focusNode)
return null
}
48 changes: 4 additions & 44 deletions packages/core/test/models/resources/lib/defaultValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,12 @@ import { expect } from 'chai'
import cf from 'clownface'
import $rdf from 'rdf-ext'
import { literal } from '@rdf-esm/data-model'
import { xsd, sh, rdf, foaf, dash } from '@tpluscode/rdf-ns-builders'
import { xsd, rdf, foaf, dash } from '@tpluscode/rdf-ns-builders'
import { NodeKindEnum } from '@rdfine/shacl'
import { defaultValue } from '../../../../models/resources/lib/defaultValue'
import { propertyShape } from '../../../util'

describe('core/models/resources/lib/defaultValue', () => {
it('returns literal 0 for numeric values', () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
const property = propertyShape(graph.blankNode(), {
[sh.datatype.value]: xsd.nonNegativeInteger,
})

// when
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.term).to.deep.eq(literal('0', xsd.nonNegativeInteger))
})

it('returns empty string literal by default', () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
const property = propertyShape(graph.blankNode())

// when
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.term).to.deep.eq(literal(''))
})

it('returns empty string literal with datatype', () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
const property = propertyShape(graph.blankNode(), {
[sh.datatype.value]: xsd.anySimpleType,
})

// when
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.term).to.deep.eq(literal('', xsd.anySimpleType))
})

it('returns default value from property', () => {
// given
const graph = cf({ dataset: $rdf.dataset() })
Expand All @@ -60,7 +20,7 @@ describe('core/models/resources/lib/defaultValue', () => {
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.term).to.deep.eq(literal('foo', xsd.anySimpleType))
expect(pointer?.term).to.deep.eq(literal('foo', xsd.anySimpleType))
})

const resourceNodeKinds = [
Expand All @@ -82,7 +42,7 @@ describe('core/models/resources/lib/defaultValue', () => {
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.out(rdf.type).term).to.deep.eq(foaf.Agent)
expect(pointer?.out(rdf.type).term).to.deep.eq(foaf.Agent)
})

it(`does not add rdf:type when node kind is ${nodeKind.value} but editor is ${dash.InstancesSelectEditor.value}`, () => {
Expand All @@ -98,7 +58,7 @@ describe('core/models/resources/lib/defaultValue', () => {
const pointer = defaultValue(property, graph.blankNode())

// then
expect(pointer.out(rdf.type).term).to.be.undefined
expect(pointer?.out(rdf.type).term).to.be.undefined
})
})
})
6 changes: 3 additions & 3 deletions packages/wc-material/components/textArea.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Component } from '@hydrofoil/shaperone-wc'
import type { SingleEditorComponent } from '@hydrofoil/shaperone-wc'
import { html } from 'lit-html'
import { literal } from '@rdf-esm/data-model'
import { dash } from '@tpluscode/rdf-ns-builders'

export const textArea: Component = {
export const textArea: SingleEditorComponent = {
editor: dash.TextAreaEditor,

render({ value }, { update }) {
return html`
<mwc-textarea
.value="${value.object.value}"
.value="${value.object?.value || ''}"
required
@blur="${(e: any) => update(literal(e.target.value))}"
></mwc-textarea>
Expand Down
6 changes: 3 additions & 3 deletions packages/wc-vaadin/components/text-area.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Component } from '@hydrofoil/shaperone-wc'
import type { SingleEditorComponent } from '@hydrofoil/shaperone-wc'
import { html } from 'lit-html'
import { literal } from '@rdf-esm/data-model'
import { dash } from '@tpluscode/rdf-ns-builders'

export const textArea: Component = {
export const textArea: SingleEditorComponent = {
editor: dash.TextAreaEditor,

render({ value }, { update }) {
return html`
<vaadin-text-area
.value="${value.object.value}"
.value="${value.object?.value || ''}"
required
auto-validate
@blur="${(e: any) => update(literal(e.target.value))}"
Expand Down
6 changes: 3 additions & 3 deletions packages/wc-vaadin/components/text-field.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Component } from '@hydrofoil/shaperone-wc'
import type { SingleEditorComponent } from '@hydrofoil/shaperone-wc'
import { html } from 'lit-html'
import { spread } from '@open-wc/lit-helpers'
import { dash, xsd } from '@tpluscode/rdf-ns-builders'
import { numericDatatype } from '@hydrofoil/shaperone-core/lib/datatypes'

export const textField: Component = {
export const textField: SingleEditorComponent = {
editor: dash.TextFieldEditor,

render({ value, property }, { update }) {
const props = {
'.value': value.object.value,
'.value': value.object?.value || '',
required: true,
'?auto-validate': true,
'@blur': (e: any) => update(e.target.value),
Expand Down
6 changes: 3 additions & 3 deletions packages/wc-vaadin/components/url-editor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { Component } from '@hydrofoil/shaperone-wc'
import { SingleEditorComponent } from '@hydrofoil/shaperone-wc'
import { html } from 'lit-html'
import { spread } from '@open-wc/lit-helpers'
import { dash } from '@tpluscode/rdf-ns-builders'
import { namedNode } from '@rdf-esm/data-model'

export const urlEditor: Component = {
export const urlEditor: SingleEditorComponent = {
editor: dash.URIEditor,

render({ value }, { update }) {
const props = {
'.value': value.object.value,
'.value': value.object?.value || '',
required: true,
'?auto-validate': true,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/wc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import './ShaperoneForm'
export type { ShaperoneForm } from './ShaperoneForm'
export { html, css } from 'lit-element'

export type Component = Core.Component<TemplateResult>
export type SingleEditorComponent = Core.SingleEditorComponent<TemplateResult>
export type MultiEditorComponent = Core.MultiEditorComponent<TemplateResult>
export type Component = SingleEditorComponent | MultiEditorComponent

0 comments on commit 0d4be59

Please sign in to comment.