forked from ULB-Darmstadt/shacl-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
61 lines (51 loc) · 1.99 KB
/
plugin.ts
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
import { ShaclPropertyTemplate } from './property-template'
import { Term } from '@rdfjs/types'
export class Plugins {
private plugins: Record<string, Plugin> = {}
register(plugin: Plugin) {
if (plugin.predicate === undefined && plugin.datatype === undefined) {
console.warn('not registering plugin because it does neither define "predicate" nor "datatype"', plugin)
} else {
this.plugins[`${plugin.predicate}^${plugin.datatype}`] = plugin
}
}
list(): Plugin[] {
return Object.entries(this.plugins).map((value: [_: string, plugin: Plugin]) => { return value[1] })
}
find(predicate: string | undefined, datatype: string | undefined): Plugin | undefined {
// first try to find plugin with matching predicate and datatype
let plugin = this.plugins[`${predicate}^${datatype}`]
if (plugin) {
return plugin
}
// now prefer predicate over datatype
plugin = this.plugins[`${predicate}^${undefined}`]
if (plugin) {
return plugin
}
// last, try to find plugin with matching datatype
return this.plugins[`${undefined}^${datatype}`]
}
}
export type PluginOptions = {
predicate?: string
datatype?: string
}
export abstract class Plugin {
predicate: string | undefined
datatype: string | undefined
stylesheet: CSSStyleSheet | undefined
constructor(options: PluginOptions, css?: string) {
this.predicate = options.predicate
this.datatype = options.datatype
if (css) {
this.stylesheet = new CSSStyleSheet()
this.stylesheet.replaceSync(css)
}
}
abstract createEditor(template: ShaclPropertyTemplate, value?: Term): HTMLElement
createViewer(template: ShaclPropertyTemplate, value: Term): HTMLElement {
return template.config.theme.createViewer(template.label, value, template)
}
}
export type ClassInstanceProvider = (clazz: string) => Promise<string>