@lblod/ember-rdfa-editor-lblod-plugins 22.4.0 → 22.4.1-dev.231e70cba3693ffca0bf8b8cfa53c06efaf0e451

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/.changeset/mighty-guests-peel.md +5 -0
  2. package/.changeset/yellow-tables-live.md +5 -0
  3. package/CHANGELOG.md +6 -0
  4. package/addon/components/lmb-plugin/list.hbs +1 -1
  5. package/addon/components/lmb-plugin/search-modal.hbs +10 -7
  6. package/addon/components/lmb-plugin/search-modal.ts +8 -1
  7. package/addon/components/snippet-plugin/nodes/snippet.gts +17 -30
  8. package/addon/components/snippet-plugin/search-modal.ts +1 -1
  9. package/addon/components/snippet-plugin/snippet-insert-placeholder.gts +15 -14
  10. package/addon/components/snippet-plugin/snippet-insert-rdfa.gts +82 -0
  11. package/addon/components/snippet-plugin/snippet-insert.gts +109 -0
  12. package/addon/components/snippet-plugin/snippet-list/snippet-list-modal.hbs +1 -1
  13. package/addon/components/snippet-plugin/snippet-list/snippet-list-modal.ts +8 -11
  14. package/addon/components/snippet-plugin/snippet-list/snippet-list-view.hbs +4 -1
  15. package/addon/components/snippet-plugin/snippet-list-select-rdfa.gts +25 -0
  16. package/addon/components/snippet-plugin/snippet-list-select.gts +104 -0
  17. package/addon/plugins/lmb-plugin/utils/fetchMandatees.ts +7 -18
  18. package/addon/plugins/snippet-plugin/commands/index.ts +1 -1
  19. package/addon/plugins/snippet-plugin/commands/{update-snippet-ids.ts → update-snippet-placeholder.ts} +23 -12
  20. package/addon/plugins/snippet-plugin/index.ts +19 -11
  21. package/addon/plugins/snippet-plugin/nodes/snippet-placeholder.ts +27 -9
  22. package/addon/plugins/snippet-plugin/nodes/snippet.ts +160 -2
  23. package/addon/plugins/snippet-plugin/utils/collate-imported-resources.ts +22 -0
  24. package/addon/plugins/snippet-plugin/utils/fetch-data.ts +52 -36
  25. package/addon/plugins/snippet-plugin/utils/rdfa-predicate.ts +3 -15
  26. package/addon/utils/sparql-helpers.ts +3 -4
  27. package/addon/utils/strings.ts +13 -0
  28. package/declarations/addon/components/snippet-plugin/nodes/snippet.d.ts +1 -1
  29. package/declarations/addon/components/snippet-plugin/search-modal.d.ts +1 -1
  30. package/declarations/addon/components/snippet-plugin/snippet-insert-placeholder.d.ts +2 -2
  31. package/declarations/addon/components/snippet-plugin/snippet-insert-rdfa.d.ts +13 -13
  32. package/declarations/addon/components/snippet-plugin/snippet-insert.d.ts +13 -9
  33. package/declarations/addon/components/snippet-plugin/snippet-list/snippet-list-modal.d.ts +2 -2
  34. package/declarations/addon/components/snippet-plugin/snippet-list-select-rdfa.d.ts +15 -19
  35. package/declarations/addon/components/snippet-plugin/snippet-list-select.d.ts +13 -5
  36. package/declarations/addon/plugins/snippet-plugin/commands/index.d.ts +1 -1
  37. package/declarations/addon/plugins/snippet-plugin/commands/update-snippet-placeholder.d.ts +11 -0
  38. package/declarations/addon/plugins/snippet-plugin/index.d.ts +12 -8
  39. package/declarations/addon/plugins/snippet-plugin/nodes/snippet-placeholder.d.ts +5 -1
  40. package/declarations/addon/plugins/snippet-plugin/nodes/snippet.d.ts +27 -2
  41. package/declarations/addon/plugins/snippet-plugin/utils/collate-imported-resources.d.ts +2 -0
  42. package/declarations/addon/plugins/snippet-plugin/utils/fetch-data.d.ts +0 -1
  43. package/declarations/addon/plugins/snippet-plugin/utils/rdfa-predicate.d.ts +1 -1
  44. package/declarations/addon/utils/sparql-helpers.d.ts +5 -3
  45. package/declarations/addon/utils/strings.d.ts +2 -0
  46. package/package.json +3 -3
  47. package/pnpm-lock.yaml +29 -29
  48. package/translations/en-US.yaml +1 -0
  49. package/translations/nl-BE.yaml +1 -0
  50. package/addon/components/snippet-plugin/snippet-insert-rdfa.hbs +0 -7
  51. package/addon/components/snippet-plugin/snippet-insert-rdfa.ts +0 -78
  52. package/addon/components/snippet-plugin/snippet-insert.hbs +0 -20
  53. package/addon/components/snippet-plugin/snippet-insert.ts +0 -95
  54. package/addon/components/snippet-plugin/snippet-list-select-rdfa.hbs +0 -8
  55. package/addon/components/snippet-plugin/snippet-list-select-rdfa.ts +0 -68
  56. package/addon/components/snippet-plugin/snippet-list-select.hbs +0 -17
  57. package/addon/components/snippet-plugin/snippet-list-select.ts +0 -29
  58. package/declarations/addon/plugins/snippet-plugin/commands/update-snippet-ids.d.ts +0 -11
@@ -3,21 +3,26 @@ import { addProperty, removeProperty } from '@lblod/ember-rdfa-editor/commands';
3
3
  import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
4
4
  import { SNIPPET_LIST_RDFA_PREDICATE } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin/utils/rdfa-predicate';
5
5
  import { getSnippetUriFromId } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
6
- import { OutgoingTriple } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
7
- import { ResolvedPNode } from '@lblod/ember-rdfa-editor/utils/_private/types';
6
+ import { type OutgoingTriple } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
7
+ import { type ResolvedPNode } from '@lblod/ember-rdfa-editor/utils/_private/types';
8
+ import {
9
+ type ImportedResourceMap,
10
+ type SnippetList,
11
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
12
+ import { importedResourcesFromSnippetLists } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin/nodes/snippet-placeholder';
8
13
 
9
- const updateSnippetPlaceholder = ({
14
+ export const updateSnippetPlaceholder = ({
10
15
  resource,
11
16
  oldSnippetProperties,
12
- newSnippetIds,
17
+ newSnippetLists,
18
+ oldImportedResources,
13
19
  node,
14
- snippetNames,
15
20
  }: {
16
21
  resource: string;
17
22
  oldSnippetProperties: OutgoingTriple[];
18
- newSnippetIds: string[];
23
+ newSnippetLists: SnippetList[];
24
+ oldImportedResources: ImportedResourceMap;
19
25
  node: ResolvedPNode;
20
- snippetNames: string[];
21
26
  }): Command => {
22
27
  return (state, dispatch) => {
23
28
  if (dispatch) {
@@ -36,14 +41,14 @@ const updateSnippetPlaceholder = ({
36
41
  });
37
42
  });
38
43
 
39
- newSnippetIds.forEach((snippetId) => {
44
+ newSnippetLists.forEach((list) => {
40
45
  newState = state.apply(transaction);
41
46
 
42
47
  addProperty({
43
48
  resource,
44
49
  property: {
45
50
  predicate: SNIPPET_LIST_RDFA_PREDICATE.prefixed,
46
- object: sayDataFactory.namedNode(getSnippetUriFromId(snippetId)),
51
+ object: sayDataFactory.namedNode(getSnippetUriFromId(list.id)),
47
52
  },
48
53
  transaction,
49
54
  })(newState, (newTransaction) => {
@@ -53,7 +58,15 @@ const updateSnippetPlaceholder = ({
53
58
  transaction = transaction.setNodeAttribute(
54
59
  node.pos,
55
60
  'listNames',
56
- snippetNames,
61
+ newSnippetLists.map((list) => list.label),
62
+ );
63
+ transaction = transaction.setNodeAttribute(
64
+ node.pos,
65
+ 'importedResources',
66
+ importedResourcesFromSnippetLists(
67
+ newSnippetLists,
68
+ oldImportedResources,
69
+ ),
57
70
  );
58
71
 
59
72
  dispatch(transaction);
@@ -61,5 +74,3 @@ const updateSnippetPlaceholder = ({
61
74
  return true;
62
75
  };
63
76
  };
64
-
65
- export default updateSnippetPlaceholder;
@@ -1,6 +1,9 @@
1
1
  import { htmlSafe } from '@ember/template';
2
2
 
3
- import { optionMapOr } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
3
+ import {
4
+ type Option,
5
+ optionMapOr,
6
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
4
7
  import { dateValue } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/strings';
5
8
  import { SafeString } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/types';
6
9
 
@@ -26,11 +29,14 @@ export class Snippet {
26
29
  }
27
30
  }
28
31
 
29
- interface SnippetListArgs {
30
- id: string | null;
31
- label: string | null;
32
- createdOn: string | null;
33
- }
32
+ export type ImportedResourceMap = Record<string, Option<string>>;
33
+
34
+ export type SnippetListArgs = {
35
+ id: string;
36
+ label: string;
37
+ createdOn: string;
38
+ importedResources: string[];
39
+ };
34
40
 
35
41
  const snippetListBase = 'http://lblod.data.gift/snippet-lists/';
36
42
 
@@ -40,13 +46,15 @@ export const getSnippetIdFromUri = (uri: string) =>
40
46
  uri.replace(snippetListBase, '');
41
47
 
42
48
  export class SnippetList {
43
- id: string | null;
44
- label: string | null;
49
+ id: string;
50
+ label: string;
45
51
  createdOn: string | null;
52
+ importedResources: string[];
46
53
 
47
- constructor({ id, label, createdOn }: SnippetListArgs) {
48
- this.id = id ? getSnippetIdFromUri(id) : null;
54
+ constructor({ id, label, createdOn, importedResources }: SnippetListArgs) {
55
+ this.id = getSnippetIdFromUri(id);
49
56
  this.label = label;
50
- this.createdOn = dateValue(createdOn ?? undefined);
57
+ this.createdOn = dateValue(createdOn);
58
+ this.importedResources = importedResources;
51
59
  }
52
60
  }
@@ -18,30 +18,43 @@ import {
18
18
  } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
19
19
  import { hasOutgoingNamedNodeTriple } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace';
20
20
  import { getTranslationFunction } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/translation';
21
- import { getSnippetUriFromId } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
21
+ import { jsonParse } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/strings';
22
+ import {
23
+ getSnippetUriFromId,
24
+ type ImportedResourceMap,
25
+ type SnippetList,
26
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
22
27
  import { SNIPPET_LIST_RDFA_PREDICATE } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin/utils/rdfa-predicate';
23
28
 
24
- export function createSnippetPlaceholder(
25
- listIds: string[],
26
- listNames: string[],
27
- schema: Schema,
29
+ export function importedResourcesFromSnippetLists(
30
+ lists: SnippetList[],
31
+ existing: ImportedResourceMap = {},
28
32
  ) {
29
- const mappingResource = `http://example.net/lblod-snippet-placeholder/${uuidv4()}`;
33
+ return Object.fromEntries<ImportedResourceMap[string]>(
34
+ lists
35
+ .flatMap((list) => list.importedResources)
36
+ // Null is important (in place of undefined) in order to keep the unlinked entries
37
+ .map((res) => [res, existing[res] ?? null]),
38
+ );
39
+ }
30
40
 
41
+ export function createSnippetPlaceholder(lists: SnippetList[], schema: Schema) {
42
+ const mappingResource = `http://example.net/lblod-snippet-placeholder/${uuidv4()}`;
31
43
  return schema.nodes.snippet_placeholder.create({
32
44
  rdfaNodeType: 'resource',
33
- listNames,
45
+ listNames: lists.map((list) => list.label),
34
46
  subject: mappingResource,
35
47
  properties: [
36
48
  {
37
49
  predicate: RDF('type').full,
38
50
  object: sayDataFactory.namedNode(EXT('SnippetPlaceholder').full),
39
51
  },
40
- ...listIds.map((listId) => ({
52
+ ...lists.map((list) => ({
41
53
  predicate: SNIPPET_LIST_RDFA_PREDICATE.full,
42
- object: sayDataFactory.namedNode(getSnippetUriFromId(listId)),
54
+ object: sayDataFactory.namedNode(getSnippetUriFromId(list.id)),
43
55
  })),
44
56
  ],
57
+ importedResources: importedResourcesFromSnippetLists(lists),
45
58
  });
46
59
  }
47
60
 
@@ -56,6 +69,7 @@ const emberNodeConfig: EmberNodeConfig = {
56
69
  ...rdfaAttrSpec({ rdfaAware: true }),
57
70
  typeof: { default: EXT('SnippetPlaceholder') },
58
71
  listNames: { default: [] },
72
+ importedResources: { default: {} },
59
73
  },
60
74
  component: SnippetPlaceholderComponent,
61
75
  serialize(node, editorState) {
@@ -67,6 +81,7 @@ const emberNodeConfig: EmberNodeConfig = {
67
81
  ...node.attrs,
68
82
  class: 'say-snippet-placeholder-node',
69
83
  'data-list-names': (node.attrs.listNames as string[]).join(','),
84
+ 'data-imported-resources': JSON.stringify(node.attrs.importedResources),
70
85
  },
71
86
  content: [
72
87
  'text',
@@ -93,6 +108,9 @@ const emberNodeConfig: EmberNodeConfig = {
93
108
  return {
94
109
  ...rdfaAttrs,
95
110
  listNames: node.getAttribute('data-list-names')?.split(','),
111
+ importedResources: jsonParse(
112
+ node.getAttribute('data-imported-resources'),
113
+ ),
96
114
  };
97
115
  }
98
116
  return false;
@@ -1,21 +1,174 @@
1
- import { getRdfaAttrs, rdfaAttrSpec } from '@lblod/ember-rdfa-editor';
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import {
3
+ type Attrs,
4
+ getRdfaAttrs,
5
+ type PNode,
6
+ ProseParser,
7
+ rdfaAttrSpec,
8
+ type SayController,
9
+ type Transaction,
10
+ } from '@lblod/ember-rdfa-editor';
2
11
  import {
3
12
  renderRdfaAware,
4
13
  getRdfaContentElement,
5
14
  } from '@lblod/ember-rdfa-editor/core/schema';
6
15
  import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
16
+ import {
17
+ type IncomingTriple,
18
+ type OutgoingTriple,
19
+ } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
7
20
  import {
8
21
  createEmberNodeSpec,
9
22
  createEmberNodeView,
10
23
  type EmberNodeConfig,
11
24
  } from '@lblod/ember-rdfa-editor/utils/ember-node';
25
+ import { htmlToDoc } from '@lblod/ember-rdfa-editor/utils/_private/html-utils';
26
+ import { transactionCombinator } from '@lblod/ember-rdfa-editor/utils/transaction-utils';
27
+ import { addPropertyToNode } from '@lblod/ember-rdfa-editor/utils/rdfa-utils';
12
28
  import SnippetComponent from '@lblod/ember-rdfa-editor-lblod-plugins/components/snippet-plugin/nodes/snippet';
13
29
  import {
14
30
  EXT,
15
31
  RDF,
16
32
  } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
17
33
  import { hasOutgoingNamedNodeTriple } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace';
18
- import { SnippetPluginConfig } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
34
+ import { jsonParse } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/strings';
35
+ import {
36
+ type ImportedResourceMap,
37
+ type SnippetPluginConfig,
38
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
39
+ import {
40
+ isSome,
41
+ unwrap,
42
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
43
+
44
+ function outgoingFromBacklink(
45
+ backlink: IncomingTriple,
46
+ attrs: Attrs,
47
+ ): OutgoingTriple {
48
+ if (attrs.rdfaNodeType === 'resource') {
49
+ return {
50
+ predicate: backlink.predicate,
51
+ object: sayDataFactory.resourceNode(attrs.subject),
52
+ };
53
+ } else {
54
+ return {
55
+ predicate: backlink.predicate,
56
+ object: sayDataFactory.literalNode(
57
+ attrs.__rdfaId,
58
+ ('language' in backlink.subject && backlink.subject.language) ||
59
+ ('datatype' in backlink.subject && backlink.subject.datatype) ||
60
+ '',
61
+ ),
62
+ };
63
+ }
64
+ }
65
+
66
+ interface CreateSnippetArgs {
67
+ controller: SayController;
68
+ content: string;
69
+ title: string;
70
+ snippetListIds: string[];
71
+ importedResources: ImportedResourceMap;
72
+ }
73
+
74
+ /**
75
+ * Creates a Snippet node wrapping the given content while allowing for further snippets to be added
76
+ * or removed. Takes care of linking imported resources.
77
+ * @returns a tuple containing the Node and the map of imported resources to the new outgoing
78
+ * properties that these resources will have after the snippet is inserted into the document.
79
+ **/
80
+ export function createSnippet({
81
+ controller,
82
+ content,
83
+ title,
84
+ snippetListIds,
85
+ importedResources,
86
+ }: CreateSnippetArgs): [PNode, Map<string, OutgoingTriple[]>] {
87
+ // Replace instances of linked to uris with the resources that exist in the outer document.
88
+ let replacedContent = content;
89
+ for (const imported in importedResources) {
90
+ const linked = importedResources[imported];
91
+ if (linked) {
92
+ replacedContent = replacedContent.replaceAll(imported, linked);
93
+ }
94
+ }
95
+ // Create the new node
96
+ const parser = ProseParser.fromSchema(controller.schema);
97
+ const contentAsNode = htmlToDoc(replacedContent, {
98
+ schema: controller.schema,
99
+ parser,
100
+ editorView: controller.mainEditorView,
101
+ });
102
+ const node = controller.schema.node(
103
+ 'snippet',
104
+ {
105
+ assignedSnippetListsIds: snippetListIds,
106
+ title,
107
+ subject: `http://data.lblod.info/snippets/${uuidv4()}`,
108
+ importedResources,
109
+ },
110
+ contentAsNode.content,
111
+ );
112
+ // Find all the new backlinks that refer to imported resources and generate OutgoingLinks for them
113
+ const importedTriples: Map<string, OutgoingTriple[]> = new Map();
114
+ if (Object.keys(importedResources).length > 0) {
115
+ contentAsNode.descendants((node) => {
116
+ const backlinks = node.attrs.backlinks as IncomingTriple[] | undefined;
117
+ if (backlinks && backlinks.length > 0) {
118
+ for (const imported in importedResources) {
119
+ const linked = importedResources[imported];
120
+ if (linked) {
121
+ const backlink = backlinks.find(
122
+ (bl) => bl.subject.value === linked,
123
+ );
124
+ if (backlink) {
125
+ const collectedLinks = importedTriples.get(linked) || [];
126
+ collectedLinks.push(outgoingFromBacklink(backlink, node.attrs));
127
+ importedTriples.set(linked, collectedLinks);
128
+ }
129
+ }
130
+ }
131
+ }
132
+ });
133
+ }
134
+
135
+ return [node, importedTriples];
136
+ }
137
+
138
+ /**
139
+ * Creates a Snippet node wrapping the given content while allowing for further snippets to be added
140
+ * or removed.
141
+ * Takes the same arguments object as creating a snippet but also a generator that produces a
142
+ * transaction that inserts the snippet into the document, so that this can be done in a way
143
+ * specific to the situation.
144
+ **/
145
+ export function createAndInsertSnippet(
146
+ createSnippetArgs: CreateSnippetArgs,
147
+ insertTransactionGenerator: (tr: Transaction, snippet: PNode) => Transaction,
148
+ ) {
149
+ return createSnippetArgs.controller.withTransaction((tr, state) => {
150
+ const [snippet, importedTriples] = createSnippet(createSnippetArgs);
151
+ const result = transactionCombinator<boolean>(
152
+ state,
153
+ insertTransactionGenerator(tr, snippet),
154
+ )(
155
+ Object.values(createSnippetArgs.importedResources)
156
+ .map((linked) => {
157
+ const newProperties =
158
+ (isSome(linked) && importedTriples.get(linked)) || [];
159
+ return newProperties.map((newProp) =>
160
+ addPropertyToNode({
161
+ resource: unwrap(linked),
162
+ property: newProp,
163
+ }),
164
+ );
165
+ })
166
+ .flat(),
167
+ );
168
+
169
+ return result.transaction;
170
+ });
171
+ }
19
172
 
20
173
  const emberNodeConfig = (options: SnippetPluginConfig): EmberNodeConfig => ({
21
174
  name: 'snippet',
@@ -37,6 +190,7 @@ const emberNodeConfig = (options: SnippetPluginConfig): EmberNodeConfig => ({
37
190
  },
38
191
  rdfaNodeType: { default: 'resource' },
39
192
  assignedSnippetListsIds: { default: [] },
193
+ importedResources: { default: {} },
40
194
  title: { default: '' },
41
195
  config: { default: options },
42
196
  },
@@ -51,6 +205,7 @@ const emberNodeConfig = (options: SnippetPluginConfig): EmberNodeConfig => ({
51
205
  'data-assigned-snippet-ids': (
52
206
  node.attrs.assignedSnippetListsIds as string[]
53
207
  ).join(','),
208
+ 'data-imported-resources': JSON.stringify(node.attrs.importedResources),
54
209
  'data-snippet-title': node.attrs.title,
55
210
  },
56
211
  content: 0,
@@ -70,6 +225,9 @@ const emberNodeConfig = (options: SnippetPluginConfig): EmberNodeConfig => ({
70
225
  assignedSnippetListsIds: node
71
226
  .getAttribute('data-assigned-snippet-ids')
72
227
  ?.split(','),
228
+ importedResources: jsonParse(
229
+ node.getAttribute('data-imported-resources'),
230
+ ),
73
231
  title: node.getAttribute('data-snippet-title'),
74
232
  };
75
233
  }
@@ -0,0 +1,22 @@
1
+ import { IMPORTED_RESOURCES_ATTR } from '@lblod/ember-rdfa-editor/plugins/imported-resources';
2
+ import { jsonParse } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/strings';
3
+ import { type Snippet } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
4
+
5
+ export function collateImportedResources(snippets: Snippet[]): string[] {
6
+ const domParser = new DOMParser();
7
+ return [
8
+ ...new Set(
9
+ snippets.flatMap(({ content }) => {
10
+ const htmlNode =
11
+ content && domParser.parseFromString(content.toString(), 'text/html');
12
+ const imports = jsonParse(
13
+ htmlNode
14
+ ?.querySelector('[data-say-document]')
15
+ ?.getAttribute(IMPORTED_RESOURCES_ATTR),
16
+ );
17
+
18
+ return Array.isArray(imports) ? (imports as string[]) : [];
19
+ }),
20
+ ).values(),
21
+ ];
22
+ }
@@ -1,9 +1,10 @@
1
1
  import {
2
+ type BindingObject,
2
3
  executeCountQuery,
3
4
  executeQuery,
4
5
  sparqlEscapeString,
5
6
  } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/sparql-helpers';
6
- import { Snippet, SnippetList } from '../index';
7
+ import { Snippet, SnippetList, SnippetListArgs } from '../index';
7
8
 
8
9
  type Filter = { name?: string; assignedSnippetListIds?: string[] };
9
10
  export type OrderBy =
@@ -52,21 +53,21 @@ const buildSnippetCountQuery = ({ name, assignedSnippetListIds }: Filter) => {
52
53
  `;
53
54
  };
54
55
 
55
- const buildSnippetListCountQuery = ({ name }: Filter) => {
56
- return `
57
- PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
58
- SELECT (COUNT(?snippetLists) AS ?count)
59
- WHERE {
60
- ?snippetLists a ext:SnippetList;
61
- skos:prefLabel ?label.
62
- ${
63
- name
64
- ? `FILTER (CONTAINS(LCASE(?label), "${name.toLowerCase()}"))`
65
- : ''
66
- }
67
- }
68
- `;
69
- };
56
+ // const buildSnippetListCountQuery = ({ name }: Filter) => {
57
+ // return `
58
+ // PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
59
+ // SELECT (COUNT(?snippetLists) AS ?count)
60
+ // WHERE {
61
+ // ?snippetLists a ext:SnippetList;
62
+ // skos:prefLabel ?label.
63
+ // ${
64
+ // name
65
+ // ? `FILTER (CONTAINS(LCASE(?label), "${name.toLowerCase()}"))`
66
+ // : ''
67
+ // }
68
+ // }
69
+ // `;
70
+ // };
70
71
 
71
72
  const buildSnippetFetchQuery = ({
72
73
  filter: { name, assignedSnippetListIds },
@@ -141,11 +142,13 @@ const buildSnippetListFetchQuery = ({
141
142
  PREFIX pav: <http://purl.org/pav/>
142
143
  PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
143
144
  PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
145
+ PREFIX say: <https://say.data.gift/ns/>
144
146
 
145
- SELECT (?snippetLists as ?id) ?label ?createdOn WHERE {
147
+ SELECT (?snippetLists as ?id) ?label ?createdOn ?importedResources WHERE {
146
148
  ?snippetLists a ext:SnippetList;
147
149
  skos:prefLabel ?label;
148
150
  pav:createdOn ?createdOn.
151
+ OPTIONAL { ?snippetLists say:snippetImportedResource ?importedResources . }
149
152
  ${
150
153
  name
151
154
  ? `FILTER (CONTAINS(LCASE(?label), "${name.toLowerCase()}"))`
@@ -214,34 +217,47 @@ export const fetchSnippetLists = async ({
214
217
  filter: Filter;
215
218
  orderBy: OrderBy;
216
219
  }) => {
217
- const totalCount = await executeCountQuery({
218
- endpoint,
219
- query: buildSnippetListCountQuery(filter),
220
- abortSignal,
221
- });
220
+ // We don't currently use this count, so skip the query, but we'll need it if we make pagination
221
+ // work for the snippet list selector
222
+ // const totalCount = await executeCountQuery({
223
+ // endpoint,
224
+ // query: buildSnippetListCountQuery(filter),
225
+ // abortSignal,
226
+ // });
222
227
 
223
- if (totalCount === 0) {
224
- return { totalCount, results: [] };
225
- }
228
+ // if (totalCount === 0) {
229
+ // return { totalCount, results: [] };
230
+ // }
226
231
 
227
- const queryResult = await executeQuery<{
228
- id: { value: string };
229
- label: { value: string };
230
- createdOn: { value: string };
231
- }>({
232
+ const queryResult = await executeQuery<BindingObject<SnippetListArgs>>({
232
233
  endpoint,
233
234
  query: buildSnippetListFetchQuery({ filter, orderBy }),
234
235
  abortSignal,
235
236
  });
236
237
 
237
- const results = queryResult.results.bindings.map(
238
- (binding) =>
239
- new SnippetList({
238
+ const results = [
239
+ ...queryResult.results.bindings
240
+ .map((binding) => ({
240
241
  id: binding.id?.value,
241
242
  label: binding.label?.value,
242
243
  createdOn: binding.createdOn?.value,
243
- }),
244
- );
244
+ importedResources: binding.importedResources?.value,
245
+ }))
246
+ .reduce((mappedResults, bindings) => {
247
+ const existing = mappedResults.get(bindings.id) ?? {
248
+ ...bindings,
249
+ importedResources: [],
250
+ };
251
+ if (bindings.importedResources) {
252
+ existing.importedResources = [
253
+ ...existing.importedResources,
254
+ bindings.importedResources,
255
+ ];
256
+ }
257
+ return mappedResults.set(bindings.id, existing);
258
+ }, new Map<string, SnippetListArgs>())
259
+ .values(),
260
+ ].map((slArgs) => new SnippetList(slArgs));
245
261
 
246
- return { totalCount, results };
262
+ return { results };
247
263
  };
@@ -11,23 +11,11 @@ export const getSnippetListIdsProperties = (node: PNode) => {
11
11
  };
12
12
 
13
13
  export const getAssignedSnippetListsIdsFromProperties = (
14
- snippetListIdsProperty: OutgoingTriple[] | undefined,
14
+ snippetListIdsProperty: OutgoingTriple[] | undefined = [],
15
15
  ) => {
16
- if (!snippetListIdsProperty) {
17
- return [];
18
- }
19
-
20
- const snippetListUris = snippetListIdsProperty
16
+ return snippetListIdsProperty
21
17
  .map((property) => property.object.value)
22
- .filter((object) => object !== undefined);
23
-
24
- if (snippetListUris.length === 0) {
25
- return [];
26
- }
27
-
28
- const snippetListIds = snippetListUris
18
+ .filter((object) => object !== undefined)
29
19
  .map(getSnippetIdFromUri)
30
20
  .filter((id) => id !== undefined);
31
-
32
- return snippetListIds;
33
21
  };
@@ -1,10 +1,9 @@
1
1
  import * as RDF from '@rdfjs/types';
2
2
  import { optionMapOr } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
3
3
 
4
- export type BindingObject<Obj extends Record<string, string>> = Record<
5
- keyof Obj,
6
- { value: string }
7
- >;
4
+ export type BindingObject<Obj extends Record<string, string | string[]>> = {
5
+ [Prop in keyof Obj]: { value: string };
6
+ };
8
7
 
9
8
  export interface QueryResult<Binding = Record<string, RDF.Term>> {
10
9
  results: {
@@ -1,4 +1,5 @@
1
1
  import { warn } from '@ember/debug';
2
+ import { isNone, type Option } from './option';
2
3
 
3
4
  const STRING_CAPITALIZE_REGEXP = /(^|\/)([a-z\u00C0-\u024F])/g;
4
5
  export function capitalize(value: string): string {
@@ -48,3 +49,15 @@ export function isNumber(value: string | number | null | undefined): boolean {
48
49
  value !== '')
49
50
  );
50
51
  }
52
+
53
+ export function jsonParse<T = unknown>(json: Option<string>): T | undefined {
54
+ if (isNone(json)) {
55
+ return undefined;
56
+ }
57
+ try {
58
+ return JSON.parse(json);
59
+ } catch (err) {
60
+ console.warn('unable to parse JSON', json, err);
61
+ return undefined;
62
+ }
63
+ }
@@ -1,6 +1,6 @@
1
1
  import Component from '@glimmer/component';
2
2
  import { type EmberNodeArgs } from '@lblod/ember-rdfa-editor/utils/_private/ember-node';
3
- import { PNode, Selection, Slice } from '@lblod/ember-rdfa-editor';
3
+ import { type PNode, type Selection, Slice } from '@lblod/ember-rdfa-editor';
4
4
  interface Signature {
5
5
  Args: EmberNodeArgs;
6
6
  Blocks: {
@@ -2,7 +2,7 @@ import Component from '@glimmer/component';
2
2
  import { SnippetPluginConfig } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
3
3
  interface Args {
4
4
  config: SnippetPluginConfig;
5
- assignedSnippetListsIds: string[];
5
+ assignedSnippetListsIds: string[] | undefined;
6
6
  closeModal: () => void;
7
7
  open: boolean;
8
8
  onInsert: (content: string, title: string) => void;
@@ -1,6 +1,6 @@
1
1
  import Component from '@glimmer/component';
2
2
  import { type NodeType, type SayController } from '@lblod/ember-rdfa-editor';
3
- import { type SnippetPluginConfig } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
3
+ import { type SnippetPluginConfig, type SnippetList } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/snippet-plugin';
4
4
  interface Signature {
5
5
  Args: {
6
6
  controller: SayController;
@@ -12,6 +12,6 @@ export default class SnippetPluginSnippetInsertPlaceholder extends Component<Sig
12
12
  get placeholderNode(): NodeType | undefined;
13
13
  openModal(): void;
14
14
  closeModal(): void;
15
- insertPlaceholder(listIds: string[], listNames: string[]): void;
15
+ insertPlaceholder(lists: SnippetList[] | undefined): void;
16
16
  }
17
17
  export {};