@lblod/ember-rdfa-editor-lblod-plugins 26.3.0 → 26.3.1-dev.0425a0927cdcb0fddfc50cf066a29539f400e92a

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.
@@ -0,0 +1,5 @@
1
+ ---
2
+ '@lblod/ember-rdfa-editor-lblod-plugins': patch
3
+ ---
4
+
5
+ Removed the need of specify the type in the citaten plugin config
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @lblod/ember-rdfa-editor-lblod-plugins
2
2
 
3
+ ## 26.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [`18068ff`](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/commit/18068ffae2fda5ba4f34568d9e03c06ffe7b4b2f) Thanks [@lagartoverde](https://github.com/lagartoverde)! - Trigger release because of a npm error
8
+
3
9
  ## 26.3.0
4
10
 
5
11
  ### Minor Changes
package/README.md CHANGED
@@ -326,7 +326,6 @@ Make `this.citationPlugin` a tracked reference to the plugin created with the fu
326
326
  import { citationPlugin } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/citation-plugin';
327
327
 
328
328
  @tracked citationPlugin = citationPlugin({
329
- type: 'nodes',
330
329
  activeInNodeTypes(schema) {
331
330
  return new Set([schema.nodes.motivering]);
332
331
  },
@@ -335,10 +334,10 @@ Make `this.citationPlugin` a tracked reference to the plugin created with the fu
335
334
 
336
335
  Configuration:
337
336
 
338
- - type (optional): it can either be 'nodes', or 'ranges'
339
- * if 'nodes' is selected, you are expected to pass the `activeInNode` function. It's a function which expects an instance of a prosemirror node and returns whether it should be active in that node. The previously expected `activeInNodeTypes` is marked as deprecated and will be removed in a future release.
340
- * if 'ranges' is selected, you are expected to pass the `activeInRanges` function. It's a function that gets the state of the actual instance of the editor and returns an array of ranges for the plugin to be active in, for example `[[0,50], [70,100]]`
341
- * if no type is provided, the citation plugin will be activated document-wide
337
+ - You are expected to set one of two functions
338
+ * `activeInNode` function. It's a function which expects an instance of a prosemirror node and returns whether it should be active in that node.
339
+ * `activeInRanges` function. It's a function that gets the state of the actual instance of the editor and returns an array of ranges for the plugin to be active in, for example `[[0,50], [70,100]]`.
340
+ * If no function is provided, the citation plugin will be activated document-wide, if both are provided, it will try to do it best in combining the two but this is not encouraged.
342
341
 
343
342
  - regex: you can provide your custom regex to detect citations, if not the default one will be used
344
343
 
@@ -348,14 +347,12 @@ A common usecase is to have the plugin active in the entire document. Here's how
348
347
  import { citationPlugin } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/citation-plugin';
349
348
 
350
349
  const configA = {
351
- type: 'nodes',
352
350
  activeInNode(node, state) {
353
351
  return node.type === state.schema.nodes.doc;
354
352
  },
355
353
  };
356
354
 
357
355
  const configB = {
358
- type: 'ranges',
359
356
  activeInRanges(state) {
360
357
  // a node's nodeSize follows the Prosemirror definition
361
358
  // a non-leaf node's size is the sum of its children's sizes + 2
@@ -64,35 +64,43 @@ export default class EditorPluginsCitationInsertComponent extends Component<Args
64
64
  }
65
65
  const { selection } = this.controller.mainEditorState;
66
66
  const config = this.config;
67
- if (!('type' in config)) {
68
- // Enable plugin document wide
69
- return false;
70
- }
71
- if (config.type === 'ranges') {
67
+ if (config.activeInRanges) {
68
+ let activeInRange;
72
69
  const ranges = config.activeInRanges(this.controller.mainEditorState);
73
70
  for (const range of ranges) {
74
71
  if (selection.from > range[0] && selection.from < range[1]) {
75
- return false;
72
+ activeInRange = true;
76
73
  }
77
74
  }
78
- return true;
79
- } else {
80
- let condition: (node: PNode) => boolean;
81
- if ('activeInNodeTypes' in config) {
82
- const nodeTypes = config.activeInNodeTypes(
83
- this.controller.schema,
84
- this.controller.mainEditorState,
85
- );
86
- condition = (node) => nodeTypes.has(node.type);
87
- } else {
88
- condition = (node) =>
89
- config.activeInNode(node, this.controller.mainEditorState);
90
- }
75
+ if (!activeInRange) return true;
76
+ }
77
+ let condition: ((node: PNode) => boolean) | undefined;
78
+ const { activeInNode, activeInNodeTypes } = config;
79
+ if (activeInNodeTypes && activeInNode) {
80
+ const nodeTypes = activeInNodeTypes(
81
+ this.controller.schema,
82
+ this.controller.mainEditorState,
83
+ );
84
+ condition = (node) =>
85
+ nodeTypes.has(node.type) &&
86
+ activeInNode(node, this.controller.mainEditorState);
87
+ } else if (activeInNodeTypes) {
88
+ const nodeTypes = activeInNodeTypes(
89
+ this.controller.schema,
90
+ this.controller.mainEditorState,
91
+ );
92
+ condition = (node) => nodeTypes.has(node.type);
93
+ } else if (activeInNode) {
94
+ condition = (node) => activeInNode(node, this.controller.mainEditorState);
95
+ }
91
96
 
97
+ if (condition) {
92
98
  if (condition(this.controller.mainEditorState.doc)) {
93
99
  return false;
94
100
  }
95
101
  return !findParentNode(condition)(selection);
102
+ } else {
103
+ return false;
96
104
  }
97
105
  }
98
106
 
@@ -120,34 +120,13 @@ export interface CitationPluginState {
120
120
 
121
121
  export type CitationPlugin = ProsePlugin<CitationPluginState>;
122
122
 
123
- export type CitationPluginNodeConfig = CitationPluginBaseConfig & {
124
- type: 'nodes';
125
- } & (
126
- | {
127
- /**
128
- * @deprecated use the `activeInNode` property instead
129
- */
130
- activeInNodeTypes(schema: Schema, state: EditorState): Set<NodeType>;
131
- }
132
- | {
133
- activeInNode(node: PNode, state?: EditorState): boolean;
134
- }
135
- );
136
-
137
- export type CitationPluginRangeConfig = CitationPluginBaseConfig & {
138
- type: 'ranges';
139
- activeInRanges(state: EditorState): [number, number][];
140
- };
141
-
142
- export type CitationPluginBaseConfig = {
123
+ export type CitationPluginConfig = {
143
124
  regex?: RegExp;
125
+ activeInRanges?: (state: EditorState) => [number, number][];
126
+ activeInNodeTypes?: (schema: Schema, state: EditorState) => Set<NodeType>;
127
+ activeInNode?: (node: PNode, state?: EditorState) => boolean;
144
128
  };
145
129
 
146
- export type CitationPluginConfig =
147
- | CitationPluginBaseConfig
148
- | CitationPluginNodeConfig
149
- | CitationPluginRangeConfig;
150
-
151
130
  export type CitationPluginEmberComponentConfig = CitationPluginConfig & {
152
131
  endpoint: string;
153
132
  decisionsEndpoint?: string;
@@ -190,40 +169,56 @@ function calculateCitationPluginState(
190
169
  oldDecs?: DecorationSet,
191
170
  ) {
192
171
  const { doc, schema } = state;
193
- let activeRanges;
194
- let highlights;
195
- if ('type' in config) {
196
- if (config.type === 'ranges') {
172
+ let activeRanges: [number, number][] = [];
173
+ let highlights: DecorationSet | undefined;
174
+
175
+ if (config.activeInNodeTypes || config.activeInNode) {
176
+ let condition: (node: PNode) => boolean = () => false;
177
+ const { activeInNode, activeInNodeTypes } = config;
178
+ if (activeInNodeTypes && activeInNode) {
179
+ const nodeTypes = activeInNodeTypes(schema, state);
180
+ condition = (node) => {
181
+ return nodeTypes.has(node.type) || activeInNode(node, state);
182
+ };
183
+ } else if (activeInNodeTypes) {
184
+ const nodeTypes = activeInNodeTypes(schema, state);
185
+ condition = (node) => {
186
+ return nodeTypes.has(node.type);
187
+ };
188
+ } else if (activeInNode) {
189
+ condition = (node) => activeInNode(node, state);
190
+ }
191
+ if (config.activeInRanges) {
197
192
  activeRanges = config.activeInRanges(state);
198
193
  const calculatedDecs = calculateDecorationsInRanges(
199
194
  config,
200
195
  schema,
201
196
  doc,
202
197
  activeRanges,
198
+ condition,
203
199
  );
204
200
  highlights = calculatedDecs.decorations;
205
201
  } else {
206
- let condition: (node: PNode) => boolean;
207
- if ('activeInNodeTypes' in config) {
208
- const nodeTypes = config.activeInNodeTypes(schema, state);
209
- condition = (node) => {
210
- return nodeTypes.has(node.type);
211
- };
212
- } else {
213
- condition = (node) => config.activeInNode(node, state);
214
- }
215
-
216
202
  const calculatedDecs = calculateDecorationsInNodes(
217
203
  config,
218
204
  schema,
219
205
  condition,
220
206
  doc,
221
207
  oldState?.doc,
222
- oldDecs,
208
+ highlights || oldDecs,
223
209
  );
224
210
  activeRanges = calculatedDecs.activeRanges;
225
211
  highlights = calculatedDecs.decorations;
226
212
  }
213
+ } else if (config.activeInRanges) {
214
+ activeRanges = config.activeInRanges(state);
215
+ const calculatedDecs = calculateDecorationsInRanges(
216
+ config,
217
+ schema,
218
+ doc,
219
+ activeRanges,
220
+ );
221
+ highlights = calculatedDecs.decorations;
227
222
  } else {
228
223
  const calculatedDecs = calculateDecorationsInNodes(
229
224
  config,
@@ -236,7 +231,9 @@ function calculateCitationPluginState(
236
231
  activeRanges = calculatedDecs.activeRanges;
237
232
  highlights = calculatedDecs.decorations;
238
233
  }
239
-
234
+ if (!highlights) {
235
+ highlights = DecorationSet.create(doc, []);
236
+ }
240
237
  return {
241
238
  highlights,
242
239
  activeRanges,
@@ -305,6 +302,7 @@ function calculateDecorationsInRanges(
305
302
  schema: CitationSchema,
306
303
  doc: PNode,
307
304
  activeRanges: [number, number][],
305
+ condition?: (node: PNode) => boolean,
308
306
  ): { decorations: DecorationSet; activeRanges: [number, number][] } {
309
307
  const decorationsToAdd: Decoration[] = [];
310
308
  const collector = collectDecorations(
@@ -315,7 +313,12 @@ function calculateDecorationsInRanges(
315
313
  );
316
314
 
317
315
  for (const [start, end] of activeRanges) {
318
- doc.nodesBetween(start, end, collector);
316
+ doc.nodesBetween(start, end, (node, pos) => {
317
+ if (condition && !condition(node)) {
318
+ return false;
319
+ }
320
+ return collector(node, pos);
321
+ });
319
322
  }
320
323
  return {
321
324
  decorations: DecorationSet.create(doc, decorationsToAdd),
@@ -20,24 +20,12 @@ export interface CitationPluginState {
20
20
  activeRanges: [number, number][];
21
21
  }
22
22
  export type CitationPlugin = ProsePlugin<CitationPluginState>;
23
- export type CitationPluginNodeConfig = CitationPluginBaseConfig & {
24
- type: 'nodes';
25
- } & ({
26
- /**
27
- * @deprecated use the `activeInNode` property instead
28
- */
29
- activeInNodeTypes(schema: Schema, state: EditorState): Set<NodeType>;
30
- } | {
31
- activeInNode(node: PNode, state?: EditorState): boolean;
32
- });
33
- export type CitationPluginRangeConfig = CitationPluginBaseConfig & {
34
- type: 'ranges';
35
- activeInRanges(state: EditorState): [number, number][];
36
- };
37
- export type CitationPluginBaseConfig = {
23
+ export type CitationPluginConfig = {
38
24
  regex?: RegExp;
25
+ activeInRanges?: (state: EditorState) => [number, number][];
26
+ activeInNodeTypes?: (schema: Schema, state: EditorState) => Set<NodeType>;
27
+ activeInNode?: (node: PNode, state?: EditorState) => boolean;
39
28
  };
40
- export type CitationPluginConfig = CitationPluginBaseConfig | CitationPluginNodeConfig | CitationPluginRangeConfig;
41
29
  export type CitationPluginEmberComponentConfig = CitationPluginConfig & {
42
30
  endpoint: string;
43
31
  decisionsEndpoint?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lblod/ember-rdfa-editor-lblod-plugins",
3
- "version": "26.3.0",
3
+ "version": "26.3.1-dev.0425a0927cdcb0fddfc50cf066a29539f400e92a",
4
4
  "description": "Ember addon providing lblod specific plugins for the ember-rdfa-editor",
5
5
  "keywords": [
6
6
  "ember-addon",