@doist/typist 10.0.0-next.1 → 10.0.0-next.2
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/dist/components/typist-editor.d.ts +126 -123
- package/dist/components/typist-editor.d.ts.map +1 -1
- package/dist/components/typist-editor.helper.d.ts +8 -11
- package/dist/components/typist-editor.helper.d.ts.map +1 -1
- package/dist/components/typist-editor.helper.js +21 -19
- package/dist/components/typist-editor.helper.js.map +1 -0
- package/dist/components/typist-editor.js +133 -135
- package/dist/components/typist-editor.js.map +1 -0
- package/dist/constants/common.js +9 -5
- package/dist/constants/common.js.map +1 -0
- package/dist/constants/extension-priorities.d.ts +3 -1
- package/dist/constants/extension-priorities.d.ts.map +1 -1
- package/dist/constants/extension-priorities.js +34 -30
- package/dist/constants/extension-priorities.js.map +1 -0
- package/dist/constants/regular-expressions.js +14 -10
- package/dist/constants/regular-expressions.js.map +1 -0
- package/dist/extensions/core/extra-editor-commands/commands/create-paragraph-end.d.ts +12 -9
- package/dist/extensions/core/extra-editor-commands/commands/create-paragraph-end.d.ts.map +1 -1
- package/dist/extensions/core/extra-editor-commands/commands/create-paragraph-end.js +16 -19
- package/dist/extensions/core/extra-editor-commands/commands/create-paragraph-end.js.map +1 -0
- package/dist/extensions/core/extra-editor-commands/commands/extend-word-range.d.ts +12 -9
- package/dist/extensions/core/extra-editor-commands/commands/extend-word-range.d.ts.map +1 -1
- package/dist/extensions/core/extra-editor-commands/commands/extend-word-range.js +25 -28
- package/dist/extensions/core/extra-editor-commands/commands/extend-word-range.js.map +1 -0
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content-at.d.ts +24 -22
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content-at.d.ts.map +1 -1
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content-at.js +34 -36
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content-at.js.map +1 -0
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content.d.ts +23 -20
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content.d.ts.map +1 -1
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content.js +15 -8
- package/dist/extensions/core/extra-editor-commands/commands/insert-markdown-content.js.map +1 -0
- package/dist/extensions/core/extra-editor-commands/extra-editor-commands.js +22 -18
- package/dist/extensions/core/extra-editor-commands/extra-editor-commands.js.map +1 -0
- package/dist/extensions/core/view-event-handlers.d.ts +14 -13
- package/dist/extensions/core/view-event-handlers.d.ts.map +1 -1
- package/dist/extensions/core/view-event-handlers.js +33 -31
- package/dist/extensions/core/view-event-handlers.js.map +1 -0
- package/dist/extensions/plain-text/paste-multiline-text.js +45 -59
- package/dist/extensions/plain-text/paste-multiline-text.js.map +1 -0
- package/dist/extensions/plain-text/plain-text-document.d.ts +7 -7
- package/dist/extensions/plain-text/plain-text-document.d.ts.map +1 -1
- package/dist/extensions/plain-text/plain-text-document.js +14 -13
- package/dist/extensions/plain-text/plain-text-document.js.map +1 -0
- package/dist/extensions/plain-text/plain-text-kit.d.ts +35 -33
- package/dist/extensions/plain-text/plain-text-kit.d.ts.map +1 -1
- package/dist/extensions/plain-text/plain-text-kit.js +33 -48
- package/dist/extensions/plain-text/plain-text-kit.js.map +1 -0
- package/dist/extensions/plain-text/plain-text-paragraph.d.ts +2 -9
- package/dist/extensions/plain-text/plain-text-paragraph.js +11 -11
- package/dist/extensions/plain-text/plain-text-paragraph.js.map +1 -0
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-lists.js +37 -81
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-lists.js.map +1 -0
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-select-wrap.js +32 -42
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-select-wrap.js.map +1 -0
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-url-pasting.js +24 -37
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-url-pasting.js.map +1 -0
- package/dist/extensions/plain-text/smart-markdown-typing/smart-markdown-typing.js +21 -13
- package/dist/extensions/plain-text/smart-markdown-typing/smart-markdown-typing.js.map +1 -0
- package/dist/extensions/rich-text/bold-and-italics.js +42 -40
- package/dist/extensions/rich-text/bold-and-italics.js.map +1 -0
- package/dist/extensions/rich-text/curvenote-codemark.js +16 -14
- package/dist/extensions/rich-text/curvenote-codemark.js.map +1 -0
- package/dist/extensions/rich-text/paste-emojis.js +20 -22
- package/dist/extensions/rich-text/paste-emojis.js.map +1 -0
- package/dist/extensions/rich-text/paste-markdown.js +46 -86
- package/dist/extensions/rich-text/paste-markdown.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-bullet-list.d.ts +10 -9
- package/dist/extensions/rich-text/rich-text-bullet-list.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-bullet-list.js +42 -56
- package/dist/extensions/rich-text/rich-text-bullet-list.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-code.js +34 -34
- package/dist/extensions/rich-text/rich-text-code.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-document.d.ts +7 -7
- package/dist/extensions/rich-text/rich-text-document.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-document.js +14 -13
- package/dist/extensions/rich-text/rich-text-document.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-heading.d.ts +5 -4
- package/dist/extensions/rich-text/rich-text-heading.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-heading.js +25 -25
- package/dist/extensions/rich-text/rich-text-heading.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-image.d.ts +53 -52
- package/dist/extensions/rich-text/rich-text-image.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-image.js +88 -105
- package/dist/extensions/rich-text/rich-text-image.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-kit.d.ts +127 -125
- package/dist/extensions/rich-text/rich-text-kit.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-kit.js +73 -135
- package/dist/extensions/rich-text/rich-text-kit.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-link.d.ts +5 -4
- package/dist/extensions/rich-text/rich-text-link.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-link.js +82 -90
- package/dist/extensions/rich-text/rich-text-link.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-ordered-list.d.ts +10 -9
- package/dist/extensions/rich-text/rich-text-ordered-list.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-ordered-list.js +42 -56
- package/dist/extensions/rich-text/rich-text-ordered-list.js.map +1 -0
- package/dist/extensions/rich-text/rich-text-strikethrough.d.ts +5 -4
- package/dist/extensions/rich-text/rich-text-strikethrough.d.ts.map +1 -1
- package/dist/extensions/rich-text/rich-text-strikethrough.js +10 -10
- package/dist/extensions/rich-text/rich-text-strikethrough.js.map +1 -0
- package/dist/extensions/shared/copy-markdown-source.js +22 -31
- package/dist/extensions/shared/copy-markdown-source.js.map +1 -0
- package/dist/extensions/shared/paste-html-table-as-string.js +47 -60
- package/dist/extensions/shared/paste-html-table-as-string.js.map +1 -0
- package/dist/extensions/shared/paste-singleline-text.js +31 -39
- package/dist/extensions/shared/paste-singleline-text.js.map +1 -0
- package/dist/factories/create-suggestion-extension.d.ts +74 -74
- package/dist/factories/create-suggestion-extension.d.ts.map +1 -1
- package/dist/factories/create-suggestion-extension.js +139 -161
- package/dist/factories/create-suggestion-extension.js.map +1 -0
- package/dist/helpers/dom.js +9 -5
- package/dist/helpers/dom.js.map +1 -0
- package/dist/helpers/schema.d.ts +5 -3
- package/dist/helpers/schema.d.ts.map +1 -1
- package/dist/helpers/schema.js +25 -21
- package/dist/helpers/schema.js.map +1 -0
- package/dist/helpers/serializer.js +24 -28
- package/dist/helpers/serializer.js.map +1 -0
- package/dist/helpers/unified.js +24 -17
- package/dist/helpers/unified.js.map +1 -0
- package/dist/hooks/use-editor.js +50 -53
- package/dist/hooks/use-editor.js.map +1 -0
- package/dist/index.d.ts +25 -31
- package/dist/index.js +24 -24
- package/dist/serializers/html/html.d.ts +12 -10
- package/dist/serializers/html/html.d.ts.map +1 -1
- package/dist/serializers/html/html.js +67 -127
- package/dist/serializers/html/html.js.map +1 -0
- package/dist/serializers/html/plugins/rehype-code-block.js +17 -17
- package/dist/serializers/html/plugins/rehype-code-block.js.map +1 -0
- package/dist/serializers/html/plugins/rehype-image.js +20 -30
- package/dist/serializers/html/plugins/rehype-image.js.map +1 -0
- package/dist/serializers/html/plugins/rehype-suggestions.js +31 -32
- package/dist/serializers/html/plugins/rehype-suggestions.js.map +1 -0
- package/dist/serializers/html/plugins/rehype-task-list.js +31 -32
- package/dist/serializers/html/plugins/rehype-task-list.js.map +1 -0
- package/dist/serializers/html/plugins/remark-autolink-literal.d.ts +4 -1
- package/dist/serializers/html/plugins/remark-autolink-literal.d.ts.map +1 -1
- package/dist/serializers/html/plugins/remark-autolink-literal.js +25 -21
- package/dist/serializers/html/plugins/remark-autolink-literal.js.map +1 -0
- package/dist/serializers/html/plugins/remark-disable-constructs.js +21 -41
- package/dist/serializers/html/plugins/remark-disable-constructs.js.map +1 -0
- package/dist/serializers/html/plugins/remark-strikethrough.d.ts +5 -2
- package/dist/serializers/html/plugins/remark-strikethrough.d.ts.map +1 -1
- package/dist/serializers/html/plugins/remark-strikethrough.js +25 -21
- package/dist/serializers/html/plugins/remark-strikethrough.js.map +1 -0
- package/dist/serializers/markdown/markdown.d.ts +13 -12
- package/dist/serializers/markdown/markdown.d.ts.map +1 -1
- package/dist/serializers/markdown/markdown.js +78 -154
- package/dist/serializers/markdown/markdown.js.map +1 -0
- package/dist/serializers/markdown/plugins/image.js +27 -24
- package/dist/serializers/markdown/plugins/image.js.map +1 -0
- package/dist/serializers/markdown/plugins/list-item.js +32 -37
- package/dist/serializers/markdown/plugins/list-item.js.map +1 -0
- package/dist/serializers/markdown/plugins/paragraph.js +19 -19
- package/dist/serializers/markdown/plugins/paragraph.js.map +1 -0
- package/dist/serializers/markdown/plugins/strikethrough.js +23 -19
- package/dist/serializers/markdown/plugins/strikethrough.js.map +1 -0
- package/dist/serializers/markdown/plugins/suggestion.js +21 -19
- package/dist/serializers/markdown/plugins/suggestion.js.map +1 -0
- package/dist/serializers/markdown/plugins/task-item.js +31 -35
- package/dist/serializers/markdown/plugins/task-item.js.map +1 -0
- package/dist/utilities/can-insert-node-at.d.ts +12 -5
- package/dist/utilities/can-insert-node-at.d.ts.map +1 -1
- package/dist/utilities/can-insert-node-at.js +10 -8
- package/dist/utilities/can-insert-node-at.js.map +1 -0
- package/dist/utilities/can-insert-suggestion.d.ts +11 -5
- package/dist/utilities/can-insert-suggestion.d.ts.map +1 -1
- package/dist/utilities/can-insert-suggestion.js +15 -12
- package/dist/utilities/can-insert-suggestion.js.map +1 -0
- package/package.json +4 -3
- package/dist/constants/common.d.ts +0 -10
- package/dist/constants/common.d.ts.map +0 -1
- package/dist/constants/regular-expressions.d.ts +0 -18
- package/dist/constants/regular-expressions.d.ts.map +0 -1
- package/dist/extensions/core/extra-editor-commands/extra-editor-commands.d.ts +0 -9
- package/dist/extensions/core/extra-editor-commands/extra-editor-commands.d.ts.map +0 -1
- package/dist/extensions/plain-text/paste-multiline-text.d.ts +0 -10
- package/dist/extensions/plain-text/paste-multiline-text.d.ts.map +0 -1
- package/dist/extensions/plain-text/plain-text-paragraph.d.ts.map +0 -1
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-lists.d.ts +0 -9
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-lists.d.ts.map +0 -1
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-select-wrap.d.ts +0 -9
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-select-wrap.d.ts.map +0 -1
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-url-pasting.d.ts +0 -9
- package/dist/extensions/plain-text/smart-markdown-typing/plugins/smart-url-pasting.d.ts.map +0 -1
- package/dist/extensions/plain-text/smart-markdown-typing/smart-markdown-typing.d.ts +0 -8
- package/dist/extensions/plain-text/smart-markdown-typing/smart-markdown-typing.d.ts.map +0 -1
- package/dist/extensions/rich-text/bold-and-italics.d.ts +0 -8
- package/dist/extensions/rich-text/bold-and-italics.d.ts.map +0 -1
- package/dist/extensions/rich-text/curvenote-codemark.d.ts +0 -11
- package/dist/extensions/rich-text/curvenote-codemark.d.ts.map +0 -1
- package/dist/extensions/rich-text/paste-emojis.d.ts +0 -9
- package/dist/extensions/rich-text/paste-emojis.d.ts.map +0 -1
- package/dist/extensions/rich-text/paste-markdown.d.ts +0 -11
- package/dist/extensions/rich-text/paste-markdown.d.ts.map +0 -1
- package/dist/extensions/rich-text/rich-text-code.d.ts +0 -17
- package/dist/extensions/rich-text/rich-text-code.d.ts.map +0 -1
- package/dist/extensions/shared/copy-markdown-source.d.ts +0 -20
- package/dist/extensions/shared/copy-markdown-source.d.ts.map +0 -1
- package/dist/extensions/shared/paste-html-table-as-string.d.ts +0 -20
- package/dist/extensions/shared/paste-html-table-as-string.d.ts.map +0 -1
- package/dist/extensions/shared/paste-singleline-text.d.ts +0 -10
- package/dist/extensions/shared/paste-singleline-text.d.ts.map +0 -1
- package/dist/helpers/dom.d.ts +0 -8
- package/dist/helpers/dom.d.ts.map +0 -1
- package/dist/helpers/serializer.d.ts +0 -21
- package/dist/helpers/serializer.d.ts.map +0 -1
- package/dist/helpers/unified.d.ts +0 -21
- package/dist/helpers/unified.d.ts.map +0 -1
- package/dist/hooks/use-editor.d.ts +0 -19
- package/dist/hooks/use-editor.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/serializers/html/plugins/rehype-code-block.d.ts +0 -10
- package/dist/serializers/html/plugins/rehype-code-block.d.ts.map +0 -1
- package/dist/serializers/html/plugins/rehype-image.d.ts +0 -11
- package/dist/serializers/html/plugins/rehype-image.d.ts.map +0 -1
- package/dist/serializers/html/plugins/rehype-suggestions.d.ts +0 -10
- package/dist/serializers/html/plugins/rehype-suggestions.d.ts.map +0 -1
- package/dist/serializers/html/plugins/rehype-task-list.d.ts +0 -7
- package/dist/serializers/html/plugins/rehype-task-list.d.ts.map +0 -1
- package/dist/serializers/html/plugins/remark-disable-constructs.d.ts +0 -11
- package/dist/serializers/html/plugins/remark-disable-constructs.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/image.d.ts +0 -12
- package/dist/serializers/markdown/plugins/image.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/list-item.d.ts +0 -14
- package/dist/serializers/markdown/plugins/list-item.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/paragraph.d.ts +0 -13
- package/dist/serializers/markdown/plugins/paragraph.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/strikethrough.d.ts +0 -13
- package/dist/serializers/markdown/plugins/strikethrough.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/suggestion.d.ts +0 -11
- package/dist/serializers/markdown/plugins/suggestion.d.ts.map +0 -1
- package/dist/serializers/markdown/plugins/task-item.d.ts +0 -14
- package/dist/serializers/markdown/plugins/task-item.d.ts.map +0 -1
|
@@ -1,164 +1,142 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
1
|
+
import { SUGGESTION_EXTENSION_PRIORITY } from "../constants/extension-priorities.js";
|
|
2
|
+
import { canInsertNodeAt } from "../utilities/can-insert-node-at.js";
|
|
3
|
+
import { canInsertSuggestion } from "../utilities/can-insert-suggestion.js";
|
|
4
|
+
import { Node, mergeAttributes } from "@tiptap/core";
|
|
5
|
+
import { PluginKey } from "@tiptap/pm/state";
|
|
6
|
+
import { camelCase, kebabCase } from "lodash-es";
|
|
7
|
+
import { Suggestion } from "@tiptap/suggestion";
|
|
8
|
+
//#region src/factories/create-suggestion-extension.ts
|
|
8
9
|
/**
|
|
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
|
-
function createSuggestionExtension(type, items = [],
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
range.to += 1;
|
|
138
|
-
}
|
|
139
|
-
editor
|
|
140
|
-
.chain()
|
|
141
|
-
.focus()
|
|
142
|
-
.insertContentAt(range, [
|
|
143
|
-
{
|
|
144
|
-
type: nodeType,
|
|
145
|
-
attrs: props,
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
type: 'text',
|
|
149
|
-
text: ' ',
|
|
150
|
-
},
|
|
151
|
-
])
|
|
152
|
-
.run();
|
|
153
|
-
const item = storage.itemsById[props.id];
|
|
154
|
-
if (item) {
|
|
155
|
-
onItemSelect?.(item);
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
render: dropdownRenderFn,
|
|
159
|
-
}),
|
|
160
|
-
];
|
|
161
|
-
},
|
|
162
|
-
});
|
|
10
|
+
* A factory function responsible for creating different types of suggestion extensions with
|
|
11
|
+
* flexibility and customizability in mind.
|
|
12
|
+
*
|
|
13
|
+
* Extensions created by this factory function render editor nodes with internal `data-id` and
|
|
14
|
+
* `data-label` attributes (as a way to save and restore the editor nodes data) based on properties
|
|
15
|
+
* of the same name (minus the `data-` prefix) from the source item type. However, in the event of
|
|
16
|
+
* unmatched properties between the internal attributes and the source item type, you should
|
|
17
|
+
* specify the source item type, and use the optional `attributesMapping` option to map the
|
|
18
|
+
* source properties to the internal `data-id` and `data-label` attributes.
|
|
19
|
+
*
|
|
20
|
+
* This factory function also stores the suggestion items internally in the editor storage (as-is,
|
|
21
|
+
* and indexed by an identifier), as a way to make sure that if a previously referenced suggestion
|
|
22
|
+
* changes its label, the editor will always render the most up-to-date label for the suggestion by
|
|
23
|
+
* reading it from the storage. An example use case for this is when a user mention is added to the
|
|
24
|
+
* editor, and the user changed its name afterwards, the editor will always render the most
|
|
25
|
+
* up-to-date user name for the mention.
|
|
26
|
+
*
|
|
27
|
+
* @param type A unique identifier for the suggestion extension type.
|
|
28
|
+
* @param items An array of suggestion items to be stored in the editor storage.
|
|
29
|
+
* @param attributesMapping An object to map the `data-id` and `data-label` attributes with the
|
|
30
|
+
* source item type properties.
|
|
31
|
+
*
|
|
32
|
+
* @returns A new suggestion extension tailored to a specific use case.
|
|
33
|
+
*/
|
|
34
|
+
function createSuggestionExtension(type, items = [], ...attributesMapping) {
|
|
35
|
+
const nodeType = `${camelCase(type)}Suggestion`;
|
|
36
|
+
const attributeType = kebabCase(type);
|
|
37
|
+
const idAttribute = String(attributesMapping[0]?.id ?? "id");
|
|
38
|
+
const labelAttribute = String(attributesMapping[0]?.label ?? "label");
|
|
39
|
+
return Node.create({
|
|
40
|
+
name: nodeType,
|
|
41
|
+
priority: SUGGESTION_EXTENSION_PRIORITY,
|
|
42
|
+
inline: true,
|
|
43
|
+
group: "inline",
|
|
44
|
+
selectable: false,
|
|
45
|
+
atom: true,
|
|
46
|
+
addOptions() {
|
|
47
|
+
return {
|
|
48
|
+
triggerChar: "@",
|
|
49
|
+
allowSpaces: false,
|
|
50
|
+
allowedPrefixes: [" "],
|
|
51
|
+
startOfLine: false
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
addStorage() {
|
|
55
|
+
return {
|
|
56
|
+
items,
|
|
57
|
+
itemsById: items.reduce((acc, item) => ({
|
|
58
|
+
...acc,
|
|
59
|
+
[String(item[idAttribute])]: item
|
|
60
|
+
}), {})
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
addAttributes() {
|
|
64
|
+
return {
|
|
65
|
+
id: {
|
|
66
|
+
default: null,
|
|
67
|
+
parseHTML: (element) => element.getAttribute("data-id"),
|
|
68
|
+
renderHTML: (attributes) => ({ "data-id": String(attributes.id) })
|
|
69
|
+
},
|
|
70
|
+
label: {
|
|
71
|
+
default: null,
|
|
72
|
+
parseHTML: (element) => {
|
|
73
|
+
const id = String(element.getAttribute("data-id"));
|
|
74
|
+
const labelValue = this.storage.itemsById[id]?.[labelAttribute] ?? element.getAttribute("data-label");
|
|
75
|
+
return typeof labelValue === "string" ? labelValue : "";
|
|
76
|
+
},
|
|
77
|
+
renderHTML: (attributes) => ({ "data-label": String(attributes.label) })
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
parseHTML() {
|
|
82
|
+
return [{ tag: `span[data-${attributeType}]` }];
|
|
83
|
+
},
|
|
84
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
85
|
+
return [
|
|
86
|
+
"span",
|
|
87
|
+
mergeAttributes({
|
|
88
|
+
[`data-${attributeType}`]: "",
|
|
89
|
+
"aria-label": this.options.renderAriaLabel?.({
|
|
90
|
+
id: String(node.attrs.id),
|
|
91
|
+
label: String(node.attrs.label)
|
|
92
|
+
})
|
|
93
|
+
}, HTMLAttributes),
|
|
94
|
+
`${String(this.options.triggerChar)}${String(node.attrs.label)}`
|
|
95
|
+
];
|
|
96
|
+
},
|
|
97
|
+
renderText({ node }) {
|
|
98
|
+
return `${String(this.options.triggerChar)}${String(node.attrs.label)}`;
|
|
99
|
+
},
|
|
100
|
+
addProseMirrorPlugins() {
|
|
101
|
+
const { options: { triggerChar, allowSpaces, allowedPrefixes, startOfLine, onSearchChange, onItemSelect, dropdownRenderFn }, storage } = this;
|
|
102
|
+
return [Suggestion({
|
|
103
|
+
pluginKey: new PluginKey(nodeType),
|
|
104
|
+
editor: this.editor,
|
|
105
|
+
char: triggerChar,
|
|
106
|
+
allowedPrefixes,
|
|
107
|
+
allowSpaces,
|
|
108
|
+
startOfLine,
|
|
109
|
+
items({ query, editor }) {
|
|
110
|
+
return onSearchChange?.(query, editor.storage[nodeType]) || [];
|
|
111
|
+
},
|
|
112
|
+
allow({ editor, range, state }) {
|
|
113
|
+
return canInsertNodeAt({
|
|
114
|
+
editor,
|
|
115
|
+
nodeType,
|
|
116
|
+
range
|
|
117
|
+
}) && canInsertSuggestion({
|
|
118
|
+
editor,
|
|
119
|
+
state
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
command({ editor, range, props }) {
|
|
123
|
+
if (editor.view.state.selection.$to.nodeAfter?.text?.startsWith(" ")) range.to += 1;
|
|
124
|
+
editor.chain().focus().insertContentAt(range, [{
|
|
125
|
+
type: nodeType,
|
|
126
|
+
attrs: props
|
|
127
|
+
}, {
|
|
128
|
+
type: "text",
|
|
129
|
+
text: " "
|
|
130
|
+
}]).run();
|
|
131
|
+
const item = storage.itemsById[props.id];
|
|
132
|
+
if (item) onItemSelect?.(item);
|
|
133
|
+
},
|
|
134
|
+
render: dropdownRenderFn
|
|
135
|
+
})];
|
|
136
|
+
}
|
|
137
|
+
});
|
|
163
138
|
}
|
|
139
|
+
//#endregion
|
|
164
140
|
export { createSuggestionExtension };
|
|
141
|
+
|
|
142
|
+
//# sourceMappingURL=create-suggestion-extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-suggestion-extension.js","names":["TiptapSuggestion"],"sources":["../../src/factories/create-suggestion-extension.ts"],"sourcesContent":["import { mergeAttributes, Node } from '@tiptap/core'\nimport { PluginKey } from '@tiptap/pm/state'\nimport { Suggestion as TiptapSuggestion } from '@tiptap/suggestion'\nimport { camelCase, kebabCase } from 'lodash-es'\n\nimport { SUGGESTION_EXTENSION_PRIORITY } from '../constants/extension-priorities'\nimport { canInsertNodeAt } from '../utilities/can-insert-node-at'\nimport { canInsertSuggestion } from '../utilities/can-insert-suggestion'\n\nimport type {\n SuggestionKeyDownProps as CoreSuggestionKeyDownProps,\n SuggestionOptions as CoreSuggestionOptions,\n SuggestionProps as CoreSuggestionProps,\n} from '@tiptap/suggestion'\nimport type { ConditionalKeys, RequireAtLeastOne } from 'type-fest'\n\n/**\n * A type that describes the suggestion node attributes.\n */\ntype SuggestionNodeAttributes = {\n /**\n * The suggestion node unique identifier to be rendered by the editor as a `data-id` attribute.\n */\n id: number | string\n\n /**\n * The suggestion node label to be rendered by the editor as a `data-label` attribute and the\n * display text itself.\n */\n label: string\n}\n\n/**\n * A type that describes the minimal props that an autocomplete dropdown must receive.\n */\ntype SuggestionRendererProps<TSuggestionItem> = {\n /**\n * The list of suggestion items to be rendered by the autocomplete dropdown.\n */\n items: CoreSuggestionProps<TSuggestionItem>['items']\n\n /**\n * The function that must be invoked when a suggestion item is selected.\n */\n command: CoreSuggestionProps<TSuggestionItem, SuggestionNodeAttributes>['command']\n}\n\n/**\n * A type that describes the forwarded ref that an autocomplete dropdown must implement with\n * `useImperativeHandle` to handle `keydown` events in the dropdown render function.\n */\ntype SuggestionRendererRef = {\n onKeyDown: (props: CoreSuggestionKeyDownProps) => boolean\n}\n\n/**\n * The options available to customize the extension created by the factory function.\n */\ntype SuggestionOptions<TSuggestionItem> = {\n /**\n * The character that triggers the autocomplete dropdown.\n */\n triggerChar: string\n\n /**\n * Allows or disallows spaces in suggested items.\n */\n allowSpaces: CoreSuggestionOptions['allowSpaces']\n\n /**\n * The prefix characters that are allowed to trigger a suggestion.\n */\n allowedPrefixes: CoreSuggestionOptions['allowedPrefixes']\n\n /**\n * Trigger the autocomplete dropdown at the start of a line only.\n */\n startOfLine: CoreSuggestionOptions['startOfLine']\n\n /**\n * Define how the suggestion item `aria-label` attribute should be rendered.\n */\n renderAriaLabel?: (attrs: SuggestionNodeAttributes) => string\n\n /**\n * A render function for the autocomplete dropdown.\n */\n dropdownRenderFn?: CoreSuggestionOptions<TSuggestionItem>['render']\n\n /**\n * The event handler that is fired when the search string has changed.\n */\n onSearchChange?: (\n query: string,\n storage: SuggestionStorage<TSuggestionItem>,\n ) => TSuggestionItem[] | Promise<TSuggestionItem[]>\n\n /**\n * The event handler that is fired when a suggestion item is selected.\n */\n onItemSelect?: (item: TSuggestionItem) => void\n}\n\n/**\n * The storage holding the suggestion items original array, and a collection indexed by the item id.\n */\ntype SuggestionStorage<TSuggestionItem> = Readonly<{\n /**\n * The original array of suggestion items.\n */\n items: TSuggestionItem[]\n\n /**\n * A collection of suggestion items indexed by the item id.\n */\n itemsById: { readonly [id: SuggestionNodeAttributes['id']]: TSuggestionItem | undefined }\n}>\n\n/**\n * The return type for a suggestion extension created by the factory function.\n */\ntype SuggestionExtensionResult<TSuggestionItem> = Node<SuggestionOptions<TSuggestionItem>>\n\n/**\n * A factory function responsible for creating different types of suggestion extensions with\n * flexibility and customizability in mind.\n *\n * Extensions created by this factory function render editor nodes with internal `data-id` and\n * `data-label` attributes (as a way to save and restore the editor nodes data) based on properties\n * of the same name (minus the `data-` prefix) from the source item type. However, in the event of\n * unmatched properties between the internal attributes and the source item type, you should\n * specify the source item type, and use the optional `attributesMapping` option to map the\n * source properties to the internal `data-id` and `data-label` attributes.\n *\n * This factory function also stores the suggestion items internally in the editor storage (as-is,\n * and indexed by an identifier), as a way to make sure that if a previously referenced suggestion\n * changes its label, the editor will always render the most up-to-date label for the suggestion by\n * reading it from the storage. An example use case for this is when a user mention is added to the\n * editor, and the user changed its name afterwards, the editor will always render the most\n * up-to-date user name for the mention.\n *\n * @param type A unique identifier for the suggestion extension type.\n * @param items An array of suggestion items to be stored in the editor storage.\n * @param attributesMapping An object to map the `data-id` and `data-label` attributes with the\n * source item type properties.\n *\n * @returns A new suggestion extension tailored to a specific use case.\n */\nfunction createSuggestionExtension<\n TSuggestionItem extends {\n [id: SuggestionNodeAttributes['id']]: unknown\n } = SuggestionNodeAttributes,\n>(\n type: string,\n items: TSuggestionItem[] = [],\n\n // This type makes sure that if a generic type variable is specified, the `attributesMapping`\n // is also defined (and vice versa) along with making sure that at least one attribute is\n // specified, and that all constraints are satisfied.\n ...attributesMapping: TSuggestionItem extends SuggestionNodeAttributes\n ? []\n : [\n RequireAtLeastOne<{\n id: ConditionalKeys<TSuggestionItem, SuggestionNodeAttributes['id']>\n label: ConditionalKeys<TSuggestionItem, SuggestionNodeAttributes['label']>\n }>,\n ]\n): SuggestionExtensionResult<TSuggestionItem> {\n // Normalize the node type and add the `Suggestion` suffix so that it can be easily identified\n // when parsing the editor schema programatically (useful for Markdown/HTML serialization)\n const nodeType = `${camelCase(type)}Suggestion`\n\n // Normalize the node type to kebab-case to be used as a `data-*` HTML attribute\n const attributeType = kebabCase(type)\n\n // Get the specified attributes, if available, or use the defaults\n const idAttribute = String(attributesMapping[0]?.id ?? 'id')\n const labelAttribute = String(attributesMapping[0]?.label ?? 'label')\n\n // Create a personalized suggestion extension\n return Node.create<SuggestionOptions<TSuggestionItem>, SuggestionStorage<TSuggestionItem>>({\n name: nodeType,\n priority: SUGGESTION_EXTENSION_PRIORITY,\n inline: true,\n group: 'inline',\n selectable: false,\n atom: true,\n addOptions() {\n return {\n triggerChar: '@',\n // Disable option by default until the following Tiptap issue is fixed:\n // https://github.com/ueberdosis/tiptap/issues/2159\n allowSpaces: false,\n allowedPrefixes: [' '],\n startOfLine: false,\n }\n },\n addStorage() {\n return {\n items,\n itemsById: items.reduce(\n (acc, item) => ({ ...acc, [String(item[idAttribute])]: item }),\n {},\n ),\n }\n },\n addAttributes() {\n return {\n id: {\n default: null,\n parseHTML: (element) => element.getAttribute('data-id'),\n renderHTML: (attributes) => ({\n 'data-id': String(attributes.id),\n }),\n },\n label: {\n default: null,\n parseHTML: (element: Element) => {\n const id = String(element.getAttribute('data-id'))\n const item = this.storage.itemsById[id]\n\n // Attempt to read the item label from the storage first (as a way to make\n // sure that a previously referenced suggestion always renders the most\n // up-to-date label for the suggestion), and fallback to the `data-label`\n // attribute if the item is not found in the storage\n const labelValue =\n item?.[labelAttribute] ?? element.getAttribute('data-label')\n return typeof labelValue === 'string' ? labelValue : ''\n },\n renderHTML: (attributes) => ({\n 'data-label': String(attributes.label),\n }),\n },\n }\n },\n parseHTML() {\n return [{ tag: `span[data-${attributeType}]` }]\n },\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'span',\n mergeAttributes(\n {\n [`data-${attributeType}`]: '',\n 'aria-label': this.options.renderAriaLabel?.({\n id: String(node.attrs.id),\n label: String(node.attrs.label),\n }),\n },\n HTMLAttributes,\n ),\n `${String(this.options.triggerChar)}${String(node.attrs.label)}`,\n ]\n },\n renderText({ node }) {\n return `${String(this.options.triggerChar)}${String(node.attrs.label)}`\n },\n addProseMirrorPlugins() {\n const {\n options: {\n triggerChar,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n onSearchChange,\n onItemSelect,\n dropdownRenderFn,\n },\n storage,\n } = this\n\n return [\n TiptapSuggestion<TSuggestionItem, SuggestionNodeAttributes>({\n pluginKey: new PluginKey(nodeType),\n editor: this.editor,\n char: triggerChar,\n allowedPrefixes,\n allowSpaces,\n startOfLine,\n items({ query, editor }) {\n return (\n onSearchChange?.(\n query,\n editor.storage[nodeType] as SuggestionStorage<TSuggestionItem>,\n ) || []\n )\n },\n allow({ editor, range, state }) {\n return (\n canInsertNodeAt({ editor, nodeType, range }) &&\n canInsertSuggestion({ editor, state })\n )\n },\n command({ editor, range, props }) {\n const nodeAfter = editor.view.state.selection.$to.nodeAfter\n const overrideSpace = nodeAfter?.text?.startsWith(' ')\n\n if (overrideSpace) {\n range.to += 1\n }\n\n editor\n .chain()\n .focus()\n .insertContentAt(range, [\n {\n type: nodeType,\n attrs: props,\n },\n {\n type: 'text',\n text: ' ',\n },\n ])\n .run()\n\n const item = storage.itemsById[props.id]\n\n if (item) {\n onItemSelect?.(item)\n }\n },\n render: dropdownRenderFn,\n }),\n ]\n },\n })\n}\n\nexport { createSuggestionExtension }\n\nexport type {\n SuggestionExtensionResult,\n SuggestionOptions,\n SuggestionRendererProps,\n SuggestionRendererRef,\n SuggestionStorage,\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoJA,SAAS,0BAKL,MACA,QAA2B,EAAE,EAK7B,GAAG,mBAQuC;CAG1C,MAAM,WAAW,GAAG,UAAU,KAAK,CAAC;CAGpC,MAAM,gBAAgB,UAAU,KAAK;CAGrC,MAAM,cAAc,OAAO,kBAAkB,IAAI,MAAM,KAAK;CAC5D,MAAM,iBAAiB,OAAO,kBAAkB,IAAI,SAAS,QAAQ;AAGrE,QAAO,KAAK,OAA+E;EACvF,MAAM;EACN,UAAU;EACV,QAAQ;EACR,OAAO;EACP,YAAY;EACZ,MAAM;EACN,aAAa;AACT,UAAO;IACH,aAAa;IAGb,aAAa;IACb,iBAAiB,CAAC,IAAI;IACtB,aAAa;IAChB;;EAEL,aAAa;AACT,UAAO;IACH;IACA,WAAW,MAAM,QACZ,KAAK,UAAU;KAAE,GAAG;MAAM,OAAO,KAAK,aAAa,GAAG;KAAM,GAC7D,EAAE,CACL;IACJ;;EAEL,gBAAgB;AACZ,UAAO;IACH,IAAI;KACA,SAAS;KACT,YAAY,YAAY,QAAQ,aAAa,UAAU;KACvD,aAAa,gBAAgB,EACzB,WAAW,OAAO,WAAW,GAAG,EACnC;KACJ;IACD,OAAO;KACH,SAAS;KACT,YAAY,YAAqB;MAC7B,MAAM,KAAK,OAAO,QAAQ,aAAa,UAAU,CAAC;MAOlD,MAAM,aANO,KAAK,QAAQ,UAAU,MAOzB,mBAAmB,QAAQ,aAAa,aAAa;AAChE,aAAO,OAAO,eAAe,WAAW,aAAa;;KAEzD,aAAa,gBAAgB,EACzB,cAAc,OAAO,WAAW,MAAM,EACzC;KACJ;IACJ;;EAEL,YAAY;AACR,UAAO,CAAC,EAAE,KAAK,aAAa,cAAc,IAAI,CAAC;;EAEnD,WAAW,EAAE,MAAM,kBAAkB;AACjC,UAAO;IACH;IACA,gBACI;MACK,QAAQ,kBAAkB;KAC3B,cAAc,KAAK,QAAQ,kBAAkB;MACzC,IAAI,OAAO,KAAK,MAAM,GAAG;MACzB,OAAO,OAAO,KAAK,MAAM,MAAM;MAClC,CAAC;KACL,EACD,eACH;IACD,GAAG,OAAO,KAAK,QAAQ,YAAY,GAAG,OAAO,KAAK,MAAM,MAAM;IACjE;;EAEL,WAAW,EAAE,QAAQ;AACjB,UAAO,GAAG,OAAO,KAAK,QAAQ,YAAY,GAAG,OAAO,KAAK,MAAM,MAAM;;EAEzE,wBAAwB;GACpB,MAAM,EACF,SAAS,EACL,aACA,aACA,iBACA,aACA,gBACA,cACA,oBAEJ,YACA;AAEJ,UAAO,CACHA,WAA4D;IACxD,WAAW,IAAI,UAAU,SAAS;IAClC,QAAQ,KAAK;IACb,MAAM;IACN;IACA;IACA;IACA,MAAM,EAAE,OAAO,UAAU;AACrB,YACI,iBACI,OACA,OAAO,QAAQ,UAClB,IAAI,EAAE;;IAGf,MAAM,EAAE,QAAQ,OAAO,SAAS;AAC5B,YACI,gBAAgB;MAAE;MAAQ;MAAU;MAAO,CAAC,IAC5C,oBAAoB;MAAE;MAAQ;MAAO,CAAC;;IAG9C,QAAQ,EAAE,QAAQ,OAAO,SAAS;AAI9B,SAHkB,OAAO,KAAK,MAAM,UAAU,IAAI,WACjB,MAAM,WAAW,IAAI,CAGlD,OAAM,MAAM;AAGhB,YACK,OAAO,CACP,OAAO,CACP,gBAAgB,OAAO,CACpB;MACI,MAAM;MACN,OAAO;MACV,EACD;MACI,MAAM;MACN,MAAM;MACT,CACJ,CAAC,CACD,KAAK;KAEV,MAAM,OAAO,QAAQ,UAAU,MAAM;AAErC,SAAI,KACA,gBAAe,KAAK;;IAG5B,QAAQ;IACX,CAAC,CACL;;EAER,CAAC"}
|
package/dist/helpers/dom.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
//#region src/helpers/dom.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
* Parse a given HTML string and returns the `HTMLElement` for the document body.
|
|
4
|
+
*
|
|
5
|
+
* @param html The HTML string to parse.
|
|
6
|
+
*/
|
|
6
7
|
function parseHtmlToElement(html) {
|
|
7
|
-
|
|
8
|
+
return new DOMParser().parseFromString(html, "text/html").body;
|
|
8
9
|
}
|
|
10
|
+
//#endregion
|
|
9
11
|
export { parseHtmlToElement };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=dom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dom.js","names":[],"sources":["../../src/helpers/dom.ts"],"sourcesContent":["/**\n * Parse a given HTML string and returns the `HTMLElement` for the document body.\n *\n * @param html The HTML string to parse.\n */\nfunction parseHtmlToElement(html: string) {\n return new DOMParser().parseFromString(html, 'text/html').body\n}\n\nexport { parseHtmlToElement }\n"],"mappings":";;;;;;AAKA,SAAS,mBAAmB,MAAc;AACtC,QAAO,IAAI,WAAW,CAAC,gBAAgB,MAAM,YAAY,CAAC"}
|
package/dist/helpers/schema.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Schema } from "@tiptap/pm/model";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers/schema.d.ts
|
|
2
4
|
/**
|
|
3
5
|
* Check if the document schema accepts multiple lines of input.
|
|
4
6
|
*
|
|
@@ -22,6 +24,6 @@ declare function isPlainTextDocument(schema: Schema): boolean;
|
|
|
22
24
|
*
|
|
23
25
|
* @returns A string ID matching the editor schema.
|
|
24
26
|
*/
|
|
25
|
-
|
|
26
|
-
export {
|
|
27
|
+
//#endregion
|
|
28
|
+
export { isMultilineDocument, isPlainTextDocument };
|
|
27
29
|
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","
|
|
1
|
+
{"version":3,"file":"schema.d.ts","names":[],"sources":["../../src/helpers/schema.ts"],"mappings":";;;;;AAA8C;;;;;iBASrC,mBAAA,CAAoB,MAAA,EAAQ,MAAA;;;;;;;;iBAW5B,mBAAA,CAAoB,MAAA,EAAQ,MAAA"}
|
package/dist/helpers/schema.js
CHANGED
|
@@ -1,31 +1,35 @@
|
|
|
1
|
+
//#region src/helpers/schema.ts
|
|
1
2
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
* Check if the document schema accepts multiple lines of input.
|
|
4
|
+
*
|
|
5
|
+
* @param schema The current editor document schema.
|
|
6
|
+
*
|
|
7
|
+
* @returns True if the schema accepts multiple lines of input, false otherwise.
|
|
8
|
+
*/
|
|
8
9
|
function isMultilineDocument(schema) {
|
|
9
|
-
|
|
10
|
+
return /(?:\+|\*)$/.test(schema.topNodeType.spec.content || "");
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
* Check if a document schema contains a plain-text document top node.
|
|
14
|
+
*
|
|
15
|
+
* @param schema The current editor document schema.
|
|
16
|
+
*
|
|
17
|
+
* @returns True if the schema contains a plain-text document, false otherwise.
|
|
18
|
+
*/
|
|
18
19
|
function isPlainTextDocument(schema) {
|
|
19
|
-
|
|
20
|
+
return Boolean(schema.topNodeType.spec.content?.startsWith("paragraph"));
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
* Computes a string ID that identifies a given editor schema which can be used for object mapping.
|
|
24
|
+
*
|
|
25
|
+
* @param schema The current editor document schema.
|
|
26
|
+
*
|
|
27
|
+
* @returns A string ID matching the editor schema.
|
|
28
|
+
*/
|
|
28
29
|
function computeSchemaId(schema) {
|
|
29
|
-
|
|
30
|
+
return [...Object.keys(schema.marks), ...Object.keys(schema.nodes)].join();
|
|
30
31
|
}
|
|
32
|
+
//#endregion
|
|
31
33
|
export { computeSchemaId, isMultilineDocument, isPlainTextDocument };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","names":[],"sources":["../../src/helpers/schema.ts"],"sourcesContent":["import type { Schema } from '@tiptap/pm/model'\n\n/**\n * Check if the document schema accepts multiple lines of input.\n *\n * @param schema The current editor document schema.\n *\n * @returns True if the schema accepts multiple lines of input, false otherwise.\n */\nfunction isMultilineDocument(schema: Schema): boolean {\n return /(?:\\+|\\*)$/.test(schema.topNodeType.spec.content || '')\n}\n\n/**\n * Check if a document schema contains a plain-text document top node.\n *\n * @param schema The current editor document schema.\n *\n * @returns True if the schema contains a plain-text document, false otherwise.\n */\nfunction isPlainTextDocument(schema: Schema): boolean {\n return Boolean(schema.topNodeType.spec.content?.startsWith('paragraph'))\n}\n\n/**\n * Computes a string ID that identifies a given editor schema which can be used for object mapping.\n *\n * @param schema The current editor document schema.\n *\n * @returns A string ID matching the editor schema.\n */\nfunction computeSchemaId(schema: Schema) {\n return [...Object.keys(schema.marks), ...Object.keys(schema.nodes)].join()\n}\n\nexport { computeSchemaId, isMultilineDocument, isPlainTextDocument }\n"],"mappings":";;;;;;;;AASA,SAAS,oBAAoB,QAAyB;AAClD,QAAO,aAAa,KAAK,OAAO,YAAY,KAAK,WAAW,GAAG;;;;;;;;;AAUnE,SAAS,oBAAoB,QAAyB;AAClD,QAAO,QAAQ,OAAO,YAAY,KAAK,SAAS,WAAW,YAAY,CAAC;;;;;;;;;AAU5E,SAAS,gBAAgB,QAAgB;AACrC,QAAO,CAAC,GAAG,OAAO,KAAK,OAAO,MAAM,EAAE,GAAG,OAAO,KAAK,OAAO,MAAM,CAAC,CAAC,MAAM"}
|
|
@@ -1,35 +1,31 @@
|
|
|
1
|
-
import { kebabCase } from
|
|
1
|
+
import { kebabCase } from "lodash-es";
|
|
2
|
+
//#region src/helpers/serializer.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
* Builds a partial regular expression that includes valid URL schemas used by all the available
|
|
5
|
+
* suggestion nodes from the given editor schema.
|
|
6
|
+
*
|
|
7
|
+
* @param schema The editor schema to be used for suggestion nodes detection.
|
|
8
|
+
*
|
|
9
|
+
* @returns A partial regular expression with valid URL schemas for the available suggestion nodes,
|
|
10
|
+
* `null` if there are no suggestion nodes in the editor schema.
|
|
11
|
+
*/
|
|
11
12
|
function buildSuggestionSchemaPartialRegex(schema) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return `(?:${suggestionNodes
|
|
17
|
-
.map((suggestionNode) => kebabCase(suggestionNode.name.replace(/Suggestion$/, '')))
|
|
18
|
-
.join('|')})://`;
|
|
13
|
+
const suggestionNodes = Object.values(schema.nodes).filter((node) => node.name.endsWith("Suggestion"));
|
|
14
|
+
if (suggestionNodes.length === 0) return null;
|
|
15
|
+
return `(?:${suggestionNodes.map((suggestionNode) => kebabCase(suggestionNode.name.replace(/Suggestion$/, ""))).join("|")})://`;
|
|
19
16
|
}
|
|
20
17
|
/**
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
* Extract all tags from the given parse rules argument, and returns an array of said tags.
|
|
19
|
+
*
|
|
20
|
+
* @param parseRules The parse rules for a DOM node or inline style.
|
|
21
|
+
*
|
|
22
|
+
* @returns An array of tags extracted from the parse rules.
|
|
23
|
+
*/
|
|
27
24
|
function extractTagsFromParseRules(parseRules) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return parseRules
|
|
32
|
-
.filter((rule) => rule.tag)
|
|
33
|
-
.map((rule) => rule.tag);
|
|
25
|
+
if (!parseRules || parseRules.length === 0) return [];
|
|
26
|
+
return parseRules.filter((rule) => rule.tag).map((rule) => rule.tag);
|
|
34
27
|
}
|
|
28
|
+
//#endregion
|
|
35
29
|
export { buildSuggestionSchemaPartialRegex, extractTagsFromParseRules };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializer.js","names":[],"sources":["../../src/helpers/serializer.ts"],"sourcesContent":["import { kebabCase } from 'lodash-es'\n\nimport type { ParseRule, Schema } from '@tiptap/pm/model'\n\n/**\n * Builds a partial regular expression that includes valid URL schemas used by all the available\n * suggestion nodes from the given editor schema.\n *\n * @param schema The editor schema to be used for suggestion nodes detection.\n *\n * @returns A partial regular expression with valid URL schemas for the available suggestion nodes,\n * `null` if there are no suggestion nodes in the editor schema.\n */\nfunction buildSuggestionSchemaPartialRegex(schema: Schema) {\n const suggestionNodes = Object.values(schema.nodes).filter((node) =>\n node.name.endsWith('Suggestion'),\n )\n\n if (suggestionNodes.length === 0) {\n return null\n }\n\n return `(?:${suggestionNodes\n .map((suggestionNode) => kebabCase(suggestionNode.name.replace(/Suggestion$/, '')))\n .join('|')})://`\n}\n\n/**\n * Extract all tags from the given parse rules argument, and returns an array of said tags.\n *\n * @param parseRules The parse rules for a DOM node or inline style.\n *\n * @returns An array of tags extracted from the parse rules.\n */\nfunction extractTagsFromParseRules(\n parseRules?: readonly ParseRule[],\n): (keyof HTMLElementTagNameMap)[] {\n if (!parseRules || parseRules.length === 0) {\n return []\n }\n\n return parseRules\n .filter((rule) => rule.tag)\n .map((rule) => rule.tag as keyof HTMLElementTagNameMap)\n}\n\nexport { buildSuggestionSchemaPartialRegex, extractTagsFromParseRules }\n"],"mappings":";;;;;;;;;;;AAaA,SAAS,kCAAkC,QAAgB;CACvD,MAAM,kBAAkB,OAAO,OAAO,OAAO,MAAM,CAAC,QAAQ,SACxD,KAAK,KAAK,SAAS,aAAa,CACnC;AAED,KAAI,gBAAgB,WAAW,EAC3B,QAAO;AAGX,QAAO,MAAM,gBACR,KAAK,mBAAmB,UAAU,eAAe,KAAK,QAAQ,eAAe,GAAG,CAAC,CAAC,CAClF,KAAK,IAAI,CAAC;;;;;;;;;AAUnB,SAAS,0BACL,YAC+B;AAC/B,KAAI,CAAC,cAAc,WAAW,WAAW,EACrC,QAAO,EAAE;AAGb,QAAO,WACF,QAAQ,SAAS,KAAK,IAAI,CAC1B,KAAK,SAAS,KAAK,IAAmC"}
|
package/dist/helpers/unified.js
CHANGED
|
@@ -1,24 +1,31 @@
|
|
|
1
|
-
import { is } from
|
|
1
|
+
import { is } from "unist-util-is";
|
|
2
|
+
//#region src/helpers/unified.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
* Determines whether a given hast node is an element node with a specific tag name.
|
|
5
|
+
*
|
|
6
|
+
* @param node The hast node to check.
|
|
7
|
+
* @param tagName The tag name to check for.
|
|
8
|
+
*
|
|
9
|
+
* @returns `true` if the hast node is an element node with the specified tag name, `false`
|
|
10
|
+
* otherwise.
|
|
11
|
+
*/
|
|
11
12
|
function isHastElementNode(node, tagName) {
|
|
12
|
-
|
|
13
|
+
return is(node, {
|
|
14
|
+
type: "element",
|
|
15
|
+
tagName
|
|
16
|
+
});
|
|
13
17
|
}
|
|
14
18
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
* Determines whether a given hast node is a text node.
|
|
20
|
+
*
|
|
21
|
+
* @param node The hast node to check.
|
|
22
|
+
*
|
|
23
|
+
* @returns `true` if the hast node is a text node, `false` otherwise.
|
|
24
|
+
*/
|
|
21
25
|
function isHastTextNode(node) {
|
|
22
|
-
|
|
26
|
+
return is(node, { type: "text" });
|
|
23
27
|
}
|
|
28
|
+
//#endregion
|
|
24
29
|
export { isHastElementNode, isHastTextNode };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=unified.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unified.js","names":[],"sources":["../../src/helpers/unified.ts"],"sourcesContent":["import { is } from 'unist-util-is'\n\nimport type { Element, Node as HastNode, Text } from 'hast'\n\n/**\n * Determines whether a given hast node is an element node with a specific tag name.\n *\n * @param node The hast node to check.\n * @param tagName The tag name to check for.\n *\n * @returns `true` if the hast node is an element node with the specified tag name, `false`\n * otherwise.\n */\nfunction isHastElementNode(node: HastNode, tagName: Element['tagName']): node is Element {\n return is(node, { type: 'element', tagName })\n}\n\n/**\n * Determines whether a given hast node is a text node.\n *\n * @param node The hast node to check.\n *\n * @returns `true` if the hast node is a text node, `false` otherwise.\n */\nfunction isHastTextNode(node: HastNode): node is Text {\n return is(node, { type: 'text' })\n}\n\nexport { isHastElementNode, isHastTextNode }\n"],"mappings":";;;;;;;;;;;AAaA,SAAS,kBAAkB,MAAgB,SAA8C;AACrF,QAAO,GAAG,MAAM;EAAE,MAAM;EAAW;EAAS,CAAC;;;;;;;;;AAUjD,SAAS,eAAe,MAA8B;AAClD,QAAO,GAAG,MAAM,EAAE,MAAM,QAAQ,CAAC"}
|