@lblod/ember-rdfa-editor-lblod-plugins 6.0.0 → 6.1.0

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [6.1.0] - 2023-05-11
11
+
12
+ ### Added
13
+ - make static TOC look the same in dynamic content
10
14
  ## [6.0.0] - 2023-05-05
11
15
  ### Changed
12
16
  - Use plugin configuration instead of ember environment in all the plugins
@@ -423,7 +427,8 @@ add onclick handler to pencil icon in variable plugin
423
427
 
424
428
  # Changelog
425
429
 
426
- [unreleased]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v6.0.0...HEAD
430
+ [unreleased]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v6.1.0...HEAD
431
+ [6.1.0]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v6.0.0...v6.1.0
427
432
  [6.0.0]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v5.0.1...v6.0.0
428
433
  [5.0.1]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v5.0.0...v5.0.1
429
434
  [5.0.0]: https://github.com/lblod/ember-rdfa-editor-lblod-plugins/compare/v4.0.2...v5.0.0
@@ -24,6 +24,7 @@ export default class TableOfContentsComponent extends Component<EmberNodeArgs> {
24
24
  node: this.controller.mainEditorState.doc,
25
25
  pos: -1,
26
26
  });
27
+
27
28
  return {
28
29
  entries,
29
30
  };
@@ -4,6 +4,7 @@ import {
4
4
  EmberNodeConfig,
5
5
  } from '@lblod/ember-rdfa-editor/utils/ember-node';
6
6
  import { TableOfContentsConfig } from '..';
7
+ import { createTableOfContents } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/table-of-contents-plugin/utils';
7
8
 
8
9
  export const emberNodeConfig: (
9
10
  config: TableOfContentsConfig
@@ -18,6 +19,30 @@ export const emberNodeConfig: (
18
19
  config: {
19
20
  default: config,
20
21
  },
22
+ entries: {
23
+ default: null,
24
+ },
25
+ },
26
+ toDOM(node) {
27
+ const { entries } = node.attrs;
28
+
29
+ if (!entries) {
30
+ return [
31
+ 'div',
32
+ {
33
+ 'data-ember-node': 'table-of-contents',
34
+ class: 'table-of-contents',
35
+ },
36
+ ['h3', {}, 'Table Of Contents'],
37
+ ];
38
+ }
39
+
40
+ return [
41
+ 'div',
42
+ { 'data-ember-node': 'table-of-contents', class: 'table-of-contents' },
43
+ ['h3', {}, 'Table Of Contents'],
44
+ createTableOfContents(entries),
45
+ ];
21
46
  },
22
47
  parseDOM: [
23
48
  {
@@ -0,0 +1,101 @@
1
+ import { DOMOutputSpec, PNode } from '@lblod/ember-rdfa-editor';
2
+ import { NodeWithPos } from '@curvenote/prosemirror-utils';
3
+
4
+ type OutlineEntry = {
5
+ content: string;
6
+ pos: number;
7
+ children?: OutlineEntry[];
8
+ };
9
+
10
+ type Mutable<T> = {
11
+ -readonly [P in keyof T]: T[P];
12
+ };
13
+
14
+ export function createTableOfContents(entries: OutlineEntry[]) {
15
+ const tableOfContents: Mutable<DOMOutputSpec> = [
16
+ 'ul',
17
+ {
18
+ class: 'au-u-background-gray-100 au-u-padding-tiny table-of-contents',
19
+ },
20
+ ];
21
+
22
+ for (let i = 0; i < entries.length; i++) {
23
+ const item: OutlineEntry = entries[i];
24
+ const li: Mutable<DOMOutputSpec> = [
25
+ 'li',
26
+ { class: 'au-c-list__item' },
27
+ [
28
+ 'button',
29
+ {
30
+ class: 'au-c-button au-c-button--link-secondary au-c-button--wrap',
31
+ type: 'button',
32
+ },
33
+ item.content,
34
+ ],
35
+ ];
36
+
37
+ if (item.children?.length) {
38
+ const children = createTableOfContents(item.children);
39
+ li.push(children);
40
+ }
41
+ tableOfContents.push(li);
42
+ }
43
+ return tableOfContents;
44
+ }
45
+
46
+ type Config = { nodeHierarchy: string[] }[];
47
+
48
+ export function extractOutline({
49
+ node,
50
+ pos,
51
+ config,
52
+ }: {
53
+ node: PNode;
54
+ pos: number;
55
+ config: Config;
56
+ }): OutlineEntry[] {
57
+ let result: OutlineEntry[] = [];
58
+ let parent: OutlineEntry | undefined;
59
+ for (const option of config) {
60
+ const { nodeHierarchy } = option;
61
+ if (RegExp(`^${nodeHierarchy[0]}$`).exec(node.type.name)) {
62
+ let i = 1;
63
+ let currentNode: NodeWithPos | undefined = { node, pos };
64
+ while (currentNode && i < nodeHierarchy.length) {
65
+ let newCurrentNode: NodeWithPos | undefined;
66
+ currentNode.node.forEach((child, offset) => {
67
+ if (RegExp(`^${nodeHierarchy[i]}$`).exec(child.type.name)) {
68
+ newCurrentNode = { pos: pos + offset, node: child };
69
+ return;
70
+ }
71
+ });
72
+ currentNode = newCurrentNode;
73
+ i++;
74
+ }
75
+ if (currentNode) {
76
+ const outlineText = currentNode.node.type.spec.outlineText as
77
+ | ((node: PNode) => string)
78
+ | undefined;
79
+ const content =
80
+ outlineText?.(currentNode.node) ?? currentNode.node.textContent;
81
+ parent = {
82
+ pos: currentNode.pos,
83
+ content,
84
+ };
85
+ }
86
+ }
87
+ }
88
+ const subResults: OutlineEntry[] = [];
89
+ node.forEach((child, offset) => {
90
+ subResults.push(
91
+ ...extractOutline({ node: child, pos: pos + 1 + offset, config })
92
+ );
93
+ });
94
+ if (parent) {
95
+ parent.children = subResults;
96
+ result = [parent];
97
+ } else {
98
+ result = subResults;
99
+ }
100
+ return result;
101
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lblod/ember-rdfa-editor-lblod-plugins",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "Ember addon providing lblod specific plugins for the ember-rdfa-editor",
5
5
  "keywords": [
6
6
  "ember-addon",
@@ -0,0 +1,16 @@
1
+ import { PNode } from '@lblod/ember-rdfa-editor';
2
+ type OutlineEntry = {
3
+ content: string;
4
+ pos: number;
5
+ children?: OutlineEntry[];
6
+ };
7
+ export declare function createTableOfContents(entries: OutlineEntry[]): [string, ...any[]];
8
+ type Config = {
9
+ nodeHierarchy: string[];
10
+ }[];
11
+ export declare function extractOutline({ node, pos, config, }: {
12
+ node: PNode;
13
+ pos: number;
14
+ config: Config;
15
+ }): OutlineEntry[];
16
+ export {};