@bigbinary/neeto-editor 0.1.15 → 0.2.1
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/index.js +37 -14
- package/package.json +18 -4
- package/src/App.js +5 -28
- package/src/Common/Avatar.js +168 -0
- package/src/Common/Button.js +95 -0
- package/src/Common/CodeBlock.js +11 -0
- package/src/Common/Description.js +8 -0
- package/src/Common/Dropdown/index.js +122 -0
- package/src/Common/Heading.js +13 -0
- package/src/Common/HighlightText.js +7 -0
- package/src/Common/Icons/HashtagFilled.js +59 -0
- package/src/Common/Icons/TextColor.js +35 -0
- package/src/Common/Icons/index.js +2 -0
- package/src/Common/Input.js +70 -0
- package/src/Common/Label.js +45 -0
- package/src/Common/ListItems.js +17 -0
- package/src/Common/Modal.js +91 -0
- package/src/Common/Tab.js +79 -0
- package/src/Common/ToolTip.js +37 -0
- package/src/Editor/CustomExtensions/BubbleMenu/index.js +33 -26
- package/src/Editor/CustomExtensions/Embeds.js +5 -3
- package/src/Editor/CustomExtensions/FixedMenu/FontSizeOption.js +32 -0
- package/src/Editor/CustomExtensions/FixedMenu/TextColorOption.js +29 -0
- package/src/Editor/CustomExtensions/FixedMenu/constants.js +3 -0
- package/src/Editor/CustomExtensions/FixedMenu/index.js +183 -0
- package/src/Editor/CustomExtensions/Image/LinkUploader/URLForm.js +39 -0
- package/src/Editor/CustomExtensions/Image/LocalUploader.js +21 -0
- package/src/Editor/CustomExtensions/Image/ProgressBar.js +34 -0
- package/src/Editor/CustomExtensions/Image/Uploader.js +72 -31
- package/src/Editor/CustomExtensions/Image/constants.js +5 -0
- package/src/Editor/CustomExtensions/Mention/ExtensionConfig.js +66 -0
- package/src/Editor/CustomExtensions/Mention/MentionList.js +96 -0
- package/src/Editor/CustomExtensions/Mention/helpers.js +23 -0
- package/src/Editor/CustomExtensions/Placeholder/ExtensionConfig.js +81 -0
- package/src/Editor/CustomExtensions/Placeholder/helpers.js +18 -0
- package/src/Editor/CustomExtensions/SlashCommands/Commands.js +5 -10
- package/src/Editor/CustomExtensions/SlashCommands/CommandsList.js +15 -16
- package/src/Editor/CustomExtensions/SlashCommands/ExtensionConfig.js +200 -155
- package/src/Editor/CustomExtensions/Variable/ExtensionConfig.js +208 -0
- package/src/Editor/CustomExtensions/Variable/VariableList.js +45 -0
- package/src/Editor/CustomExtensions/Variable/VariableSuggestion.js +20 -0
- package/src/Editor/CustomExtensions/Variable/helpers.js +31 -0
- package/src/Editor/CustomExtensions/Variable/index.js +35 -0
- package/src/Editor/CustomExtensions/useCustomExtensions.js +88 -0
- package/src/Editor/index.js +59 -41
- package/src/constants/regexp.js +1 -0
- package/src/examples/constants.js +95 -0
- package/src/examples/index.js +186 -0
- package/src/hooks/useOutsideClick.js +19 -0
- package/src/hooks/useTabBar.js +9 -0
- package/src/index.scss +32 -12
- package/src/styles/abstracts/_mixins.scss +20 -0
- package/src/styles/abstracts/_neeto-ui-variables.scss +107 -0
- package/src/styles/abstracts/_variables.scss +13 -0
- package/src/styles/components/_avatar.scss +105 -0
- package/src/styles/components/_button.scss +161 -0
- package/src/styles/components/_codeblock.scss +16 -0
- package/src/{Editor/styles/CommandsList.scss → styles/components/_command-list.scss} +12 -1
- package/src/styles/components/_dropdown.scss +69 -0
- package/src/styles/components/_editor-variables.scss +12 -0
- package/src/{Editor/styles/EditorStyles.scss → styles/components/_editor.scss} +33 -7
- package/src/styles/components/_fixed-menu.scss +17 -0
- package/src/styles/components/_image-uploader.scss +109 -0
- package/src/styles/components/_input.scss +165 -0
- package/src/styles/components/_tab.scss +74 -0
- package/src/styles/components/_tooltip.scss +152 -0
- package/src/utils/common.js +13 -0
- package/webpack.config.js +7 -0
- package/webpack.dev.config.js +7 -0
- package/public/logo192.png +0 -0
- package/public/logo512.png +0 -0
- package/src/logo.svg +0 -1
|
@@ -1,164 +1,209 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Commands from "./Commands";
|
|
1
|
+
import { Extension } from "@tiptap/core";
|
|
3
2
|
import CommandsList from "./CommandsList";
|
|
3
|
+
import { PluginKey } from "prosemirror-state";
|
|
4
|
+
import Suggestion from "@tiptap/suggestion";
|
|
4
5
|
import tippy from "tippy.js";
|
|
5
6
|
import { ReactRenderer } from "@tiptap/react";
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from "
|
|
8
|
+
Paragraph,
|
|
9
|
+
TextH1,
|
|
10
|
+
TextH2,
|
|
11
|
+
ListDot,
|
|
12
|
+
ListNumber,
|
|
13
|
+
Blockquote,
|
|
14
|
+
Image,
|
|
15
|
+
Video,
|
|
16
|
+
Code,
|
|
17
|
+
} from "@bigbinary/neeto-icons";
|
|
17
18
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
description: "Add a plain text block",
|
|
28
|
-
Icon: BsCardText,
|
|
29
|
-
command: ({ editor, range }) => {
|
|
30
|
-
editor
|
|
31
|
-
.chain()
|
|
32
|
-
.focus()
|
|
33
|
-
.deleteRange(range)
|
|
34
|
-
.setNode("paragraph")
|
|
35
|
-
.run();
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
title: "H1",
|
|
40
|
-
description: "Add a big heading",
|
|
41
|
-
Icon: BsTypeH1,
|
|
42
|
-
command: ({ editor, range }) => {
|
|
43
|
-
editor
|
|
44
|
-
.chain()
|
|
45
|
-
.focus()
|
|
46
|
-
.deleteRange(range)
|
|
47
|
-
.setNode("heading", { level: 1 })
|
|
48
|
-
.run();
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
title: "H2",
|
|
53
|
-
description: "Add a sub-heading",
|
|
54
|
-
Icon: BsTypeH2,
|
|
55
|
-
command: ({ editor, range }) => {
|
|
56
|
-
editor
|
|
57
|
-
.chain()
|
|
58
|
-
.focus()
|
|
59
|
-
.deleteRange(range)
|
|
60
|
-
.setNode("heading", { level: 2 })
|
|
61
|
-
.run();
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
title: "Numbered list",
|
|
66
|
-
description: "Add a list with numbering",
|
|
67
|
-
Icon: BsListOl,
|
|
68
|
-
command: ({ editor, range }) => {
|
|
69
|
-
editor.chain().focus().deleteRange(range).toggleOrderedList().run();
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
title: "Bulleted list",
|
|
74
|
-
description: "Add an list bullets",
|
|
75
|
-
Icon: BsListUl,
|
|
76
|
-
command: ({ editor, range }) => {
|
|
77
|
-
editor.chain().focus().deleteRange(range).toggleBulletList().run();
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
{
|
|
81
|
-
title: "Blockquote",
|
|
82
|
-
description: "Add a quote",
|
|
83
|
-
Icon: BsBlockquoteLeft,
|
|
84
|
-
command: ({ editor, range }) => {
|
|
85
|
-
editor.chain().focus().deleteRange(range).toggleBlockquote().run();
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
title: "Image",
|
|
90
|
-
description: "Add an image",
|
|
91
|
-
Icon: BsCardImage,
|
|
92
|
-
command: ({ editor, range }) => {
|
|
93
|
-
sharedState.showImageUpload = true;
|
|
94
|
-
sharedState.range = range;
|
|
95
|
-
editor.chain().focus().deleteRange(range).run();
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
title: "Youtube/Vimeo",
|
|
100
|
-
description: "Embed a video from major services",
|
|
101
|
-
Icon: BsFillCameraVideoFill,
|
|
102
|
-
command: ({ editor, range }) => {
|
|
103
|
-
const embedURL = prompt("Please enter Youtube/Vimeo embed URL");
|
|
104
|
-
editor
|
|
105
|
-
.chain()
|
|
106
|
-
.focus()
|
|
107
|
-
.deleteRange(range)
|
|
108
|
-
.setExternalVideo({ src: embedURL })
|
|
109
|
-
.run();
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
{
|
|
113
|
-
title: "Code block",
|
|
114
|
-
description: "Add a code block with syntax highlighting",
|
|
115
|
-
Icon: BsCodeSlash,
|
|
116
|
-
command: ({ editor, range }) => {
|
|
117
|
-
editor.chain().focus().deleteRange(range).toggleCodeBlock().run();
|
|
19
|
+
export const CommandsPluginKey = new PluginKey("commands");
|
|
20
|
+
|
|
21
|
+
export default {
|
|
22
|
+
configure: ({ setImageUploadVisible }) =>
|
|
23
|
+
Extension.create({
|
|
24
|
+
addOptions() {
|
|
25
|
+
return {
|
|
26
|
+
HTMLAttributes: {
|
|
27
|
+
class: "commands",
|
|
118
28
|
},
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
29
|
+
suggestion: {
|
|
30
|
+
char: "/",
|
|
31
|
+
startOfLine: false,
|
|
32
|
+
pluginKey: CommandsPluginKey,
|
|
33
|
+
command: ({ editor, range, props }) => {
|
|
34
|
+
props.command({ editor, range });
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
items: () => {
|
|
38
|
+
return [
|
|
39
|
+
{
|
|
40
|
+
title: "Paragraph",
|
|
41
|
+
description: "Add a plain text block",
|
|
42
|
+
Icon: Paragraph,
|
|
43
|
+
command: ({ editor, range }) => {
|
|
44
|
+
editor
|
|
45
|
+
.chain()
|
|
46
|
+
.focus()
|
|
47
|
+
.deleteRange(range)
|
|
48
|
+
.setNode("paragraph")
|
|
49
|
+
.run();
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
title: "H1",
|
|
54
|
+
description: "Add a big heading",
|
|
55
|
+
Icon: TextH1,
|
|
56
|
+
command: ({ editor, range }) => {
|
|
57
|
+
editor
|
|
58
|
+
.chain()
|
|
59
|
+
.focus()
|
|
60
|
+
.deleteRange(range)
|
|
61
|
+
.setNode("heading", { level: 1 })
|
|
62
|
+
.run();
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
title: "H2",
|
|
67
|
+
description: "Add a sub-heading",
|
|
68
|
+
Icon: TextH2,
|
|
69
|
+
command: ({ editor, range }) => {
|
|
70
|
+
editor
|
|
71
|
+
.chain()
|
|
72
|
+
.focus()
|
|
73
|
+
.deleteRange(range)
|
|
74
|
+
.setNode("heading", { level: 2 })
|
|
75
|
+
.run();
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
title: "Numbered list",
|
|
80
|
+
description: "Add a list with numbering",
|
|
81
|
+
Icon: ListNumber,
|
|
82
|
+
command: ({ editor, range }) => {
|
|
83
|
+
editor
|
|
84
|
+
.chain()
|
|
85
|
+
.focus()
|
|
86
|
+
.deleteRange(range)
|
|
87
|
+
.toggleOrderedList()
|
|
88
|
+
.run();
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
title: "Bulleted list",
|
|
93
|
+
description: "Add an list bullets",
|
|
94
|
+
Icon: ListDot,
|
|
95
|
+
command: ({ editor, range }) => {
|
|
96
|
+
editor
|
|
97
|
+
.chain()
|
|
98
|
+
.focus()
|
|
99
|
+
.deleteRange(range)
|
|
100
|
+
.toggleBulletList()
|
|
101
|
+
.run();
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
title: "Blockquote",
|
|
106
|
+
description: "Add a quote",
|
|
107
|
+
Icon: Blockquote,
|
|
108
|
+
command: ({ editor, range }) => {
|
|
109
|
+
editor
|
|
110
|
+
.chain()
|
|
111
|
+
.focus()
|
|
112
|
+
.deleteRange(range)
|
|
113
|
+
.toggleBlockquote()
|
|
114
|
+
.run();
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
title: "Image",
|
|
119
|
+
description: "Add an image",
|
|
120
|
+
Icon: Image,
|
|
121
|
+
command: ({ editor, range }) => {
|
|
122
|
+
setImageUploadVisible(true);
|
|
123
|
+
editor.chain().focus().deleteRange(range).run();
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
title: "Youtube/Vimeo",
|
|
128
|
+
description: "Embed a video from major services",
|
|
129
|
+
Icon: Video,
|
|
130
|
+
command: ({ editor, range }) => {
|
|
131
|
+
const embedURL = prompt(
|
|
132
|
+
"Please enter Youtube/Vimeo embed URL"
|
|
133
|
+
);
|
|
134
|
+
editor
|
|
135
|
+
.chain()
|
|
136
|
+
.focus()
|
|
137
|
+
.deleteRange(range)
|
|
138
|
+
.setExternalVideo({ src: embedURL })
|
|
139
|
+
.run();
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
title: "Code block",
|
|
144
|
+
description: "Add a code block with syntax highlighting",
|
|
145
|
+
Icon: Code,
|
|
146
|
+
command: ({ editor, range }) => {
|
|
147
|
+
editor
|
|
148
|
+
.chain()
|
|
149
|
+
.focus()
|
|
150
|
+
.deleteRange(range)
|
|
151
|
+
.toggleCodeBlock()
|
|
152
|
+
.run();
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
},
|
|
129
157
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
props,
|
|
134
|
-
editor: props.editor,
|
|
135
|
-
});
|
|
158
|
+
render: () => {
|
|
159
|
+
let reactRenderer;
|
|
160
|
+
let popup;
|
|
136
161
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
162
|
+
return {
|
|
163
|
+
onStart: (props) => {
|
|
164
|
+
reactRenderer = new ReactRenderer(CommandsList, {
|
|
165
|
+
props,
|
|
166
|
+
editor: props.editor,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
popup = tippy("body", {
|
|
170
|
+
getReferenceClientRect: props.clientRect,
|
|
171
|
+
appendTo: () => document.body,
|
|
172
|
+
content: reactRenderer.element,
|
|
173
|
+
showOnCreate: true,
|
|
174
|
+
interactive: true,
|
|
175
|
+
trigger: "manual",
|
|
176
|
+
placement: "bottom-start",
|
|
177
|
+
arrow: false,
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
onUpdate(props) {
|
|
181
|
+
reactRenderer.updateProps(props);
|
|
182
|
+
|
|
183
|
+
popup[0].setProps({
|
|
184
|
+
getReferenceClientRect: props.clientRect,
|
|
185
|
+
});
|
|
186
|
+
},
|
|
187
|
+
onKeyDown(props) {
|
|
188
|
+
return reactRenderer.ref?.onKeyDown(props);
|
|
189
|
+
},
|
|
190
|
+
onExit() {
|
|
191
|
+
popup[0].destroy();
|
|
192
|
+
reactRenderer.destroy();
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
},
|
|
149
199
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
});
|
|
200
|
+
addProseMirrorPlugins() {
|
|
201
|
+
return [
|
|
202
|
+
Suggestion({
|
|
203
|
+
editor: this.editor,
|
|
204
|
+
...this.options.suggestion,
|
|
205
|
+
}),
|
|
206
|
+
];
|
|
207
|
+
},
|
|
208
|
+
}),
|
|
209
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import tippy from "tippy.js";
|
|
2
|
+
import Suggestion from "@tiptap/suggestion";
|
|
3
|
+
import { Node, mergeAttributes } from "@tiptap/core";
|
|
4
|
+
import { ReactRenderer } from "@tiptap/react";
|
|
5
|
+
import { PluginKey } from "prosemirror-state";
|
|
6
|
+
|
|
7
|
+
import VariableSuggestion from "./VariableSuggestion";
|
|
8
|
+
|
|
9
|
+
export const VariablePluginKey = new PluginKey("variables");
|
|
10
|
+
|
|
11
|
+
const Variable = Node.create({
|
|
12
|
+
name: "variable",
|
|
13
|
+
|
|
14
|
+
addOptions() {
|
|
15
|
+
return {
|
|
16
|
+
HTMLAttributes: { class: "variable__text" },
|
|
17
|
+
charOpen: "{{",
|
|
18
|
+
charClose: "}}",
|
|
19
|
+
renderLabel({ options, node }) {
|
|
20
|
+
return `${options.charOpen}${node.attrs.label || node.attrs.id}${
|
|
21
|
+
options.charClose
|
|
22
|
+
}`;
|
|
23
|
+
},
|
|
24
|
+
suggestion: {},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
group: "inline",
|
|
29
|
+
|
|
30
|
+
inline: true,
|
|
31
|
+
|
|
32
|
+
selectable: false,
|
|
33
|
+
|
|
34
|
+
atom: true,
|
|
35
|
+
|
|
36
|
+
addAttributes() {
|
|
37
|
+
return {
|
|
38
|
+
id: {
|
|
39
|
+
default: null,
|
|
40
|
+
parseHTML: (element) => element.getAttribute("data-id"),
|
|
41
|
+
renderHTML: (attributes) => {
|
|
42
|
+
if (!attributes.id) {
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
"data-id": attributes.id,
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
label: {
|
|
53
|
+
default: null,
|
|
54
|
+
parseHTML: (element) => element.getAttribute("data-label"),
|
|
55
|
+
renderHTML: (attributes) => {
|
|
56
|
+
if (!attributes.label) {
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
"data-label": attributes.label,
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
parseHTML() {
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
tag: "span[data-variable]",
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
renderHTML({ node, HTMLAttributes }) {
|
|
77
|
+
return [
|
|
78
|
+
"span",
|
|
79
|
+
mergeAttributes(
|
|
80
|
+
{ "data-variable": "" },
|
|
81
|
+
this.options.HTMLAttributes,
|
|
82
|
+
HTMLAttributes
|
|
83
|
+
),
|
|
84
|
+
this.options.renderLabel({
|
|
85
|
+
options: this.options,
|
|
86
|
+
node,
|
|
87
|
+
}),
|
|
88
|
+
];
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
renderText({ node }) {
|
|
92
|
+
return this.options.renderLabel({
|
|
93
|
+
options: this.options,
|
|
94
|
+
node,
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
addCommands() {
|
|
99
|
+
return {
|
|
100
|
+
setVariable:
|
|
101
|
+
(attributes) =>
|
|
102
|
+
({ chain }) => {
|
|
103
|
+
chain()
|
|
104
|
+
.focus()
|
|
105
|
+
.insertContent([
|
|
106
|
+
{
|
|
107
|
+
type: this.name,
|
|
108
|
+
attrs: attributes,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
type: "text",
|
|
112
|
+
text: " ",
|
|
113
|
+
},
|
|
114
|
+
])
|
|
115
|
+
.run();
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
addKeyboardShortcuts() {
|
|
121
|
+
return {
|
|
122
|
+
Backspace: () =>
|
|
123
|
+
this.editor.commands.command(({ tr, state }) => {
|
|
124
|
+
let isVariable = false;
|
|
125
|
+
const { selection } = state;
|
|
126
|
+
const { empty, anchor } = selection;
|
|
127
|
+
|
|
128
|
+
if (!empty) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
state.doc.nodesBetween(anchor - 1, anchor, (node, pos) => {
|
|
133
|
+
if (node.type.name === this.name) {
|
|
134
|
+
isVariable = true;
|
|
135
|
+
tr.insertText("", pos, pos + node.nodeSize);
|
|
136
|
+
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return isVariable;
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
addProseMirrorPlugins() {
|
|
147
|
+
return [
|
|
148
|
+
Suggestion({
|
|
149
|
+
editor: this.editor,
|
|
150
|
+
...this.options.suggestion,
|
|
151
|
+
}),
|
|
152
|
+
];
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const suggestionConfig = {
|
|
157
|
+
char: "{{",
|
|
158
|
+
startOfLine: false,
|
|
159
|
+
pluginKey: VariablePluginKey,
|
|
160
|
+
command: ({ editor, range, props }) => {
|
|
161
|
+
editor.chain().focus().deleteRange(range).setVariable(props).run();
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
items: () => [],
|
|
165
|
+
render: () => {
|
|
166
|
+
let reactRenderer;
|
|
167
|
+
let popup;
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
onStart: (props) => {
|
|
171
|
+
reactRenderer = new ReactRenderer(VariableSuggestion, {
|
|
172
|
+
props,
|
|
173
|
+
editor: props.editor,
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
popup = tippy("body", {
|
|
177
|
+
getReferenceClientRect: props.clientRect,
|
|
178
|
+
appendTo: () => document.body,
|
|
179
|
+
content: reactRenderer.element,
|
|
180
|
+
showOnCreate: true,
|
|
181
|
+
interactive: true,
|
|
182
|
+
trigger: "manual",
|
|
183
|
+
placement: "bottom-start",
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
onUpdate(props) {
|
|
187
|
+
reactRenderer.updateProps(props);
|
|
188
|
+
|
|
189
|
+
popup[0].setProps({
|
|
190
|
+
getReferenceClientRect: props.clientRect,
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
onExit() {
|
|
195
|
+
popup[0].destroy();
|
|
196
|
+
reactRenderer.destroy();
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export default {
|
|
203
|
+
configure: ({ suggestion = {} }) => {
|
|
204
|
+
return Variable.configure({
|
|
205
|
+
suggestion: { ...suggestionConfig, ...suggestion },
|
|
206
|
+
});
|
|
207
|
+
},
|
|
208
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import { parseVariables } from "./helpers";
|
|
4
|
+
|
|
5
|
+
const VariableList = ({ onClickVariable, variables }) => {
|
|
6
|
+
const parsedVariables = parseVariables(variables);
|
|
7
|
+
|
|
8
|
+
if (!(variables && variables.length)) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return (
|
|
13
|
+
<div className="p-3 bg-white">
|
|
14
|
+
{parsedVariables.map(({ label, variables }) => (
|
|
15
|
+
<VariableCategory
|
|
16
|
+
key={label}
|
|
17
|
+
title={label}
|
|
18
|
+
variables={variables}
|
|
19
|
+
onClickItem={onClickVariable}
|
|
20
|
+
/>
|
|
21
|
+
))}
|
|
22
|
+
</div>
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default VariableList;
|
|
27
|
+
|
|
28
|
+
const VariableCategory = ({ title, variables, onClickItem }) => {
|
|
29
|
+
return (
|
|
30
|
+
<div className="my-3">
|
|
31
|
+
{title ? <p className="text-xs font-semibold">{title}</p> : null}
|
|
32
|
+
<div className="mt-1 space-x-2">
|
|
33
|
+
{variables.map((item) => (
|
|
34
|
+
<button
|
|
35
|
+
onClick={() => onClickItem(item)}
|
|
36
|
+
key={`${item.label}--${item.value}`}
|
|
37
|
+
className="px-2 py-1 text-xs text-white bg-blue-400 rounded-sm"
|
|
38
|
+
>
|
|
39
|
+
{item.label}
|
|
40
|
+
</button>
|
|
41
|
+
))}
|
|
42
|
+
</div>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import VariableList from "./VariableList";
|
|
4
|
+
|
|
5
|
+
const VariableSuggestion = ({ items, command }) => {
|
|
6
|
+
return (
|
|
7
|
+
<div className="shadow-md">
|
|
8
|
+
<VariableList
|
|
9
|
+
variables={items}
|
|
10
|
+
onClickVariable={(variable) => {
|
|
11
|
+
const { category_key, key } = variable;
|
|
12
|
+
const variableName = category_key ? `${category_key}.${key}` : key;
|
|
13
|
+
command({ label: variableName, id: variableName });
|
|
14
|
+
}}
|
|
15
|
+
/>
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default VariableSuggestion;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* Helper function that accepts an array of variables and injects category to each item in the group
|
|
2
|
+
And also performs grouping of indvidual variables under common category as 'Others'. */
|
|
3
|
+
|
|
4
|
+
export const parseVariables = (variableArr = []) => {
|
|
5
|
+
const uncategorized = [];
|
|
6
|
+
const groupedVariables = [];
|
|
7
|
+
variableArr.forEach((variable) => {
|
|
8
|
+
const { category_key, category_label, variables } = variable;
|
|
9
|
+
if (category_key && variables) {
|
|
10
|
+
const parsedVariables = variables.map((categoryVariable) => ({
|
|
11
|
+
...categoryVariable,
|
|
12
|
+
category_key,
|
|
13
|
+
}));
|
|
14
|
+
groupedVariables.push({
|
|
15
|
+
label: category_label,
|
|
16
|
+
variables: parsedVariables,
|
|
17
|
+
});
|
|
18
|
+
} else uncategorized.push(variable);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/* If there are other categorised variables already present, group all uncategorized variables under title 'Others'.
|
|
22
|
+
otherwise, if all available variables are uncategorised, do not render 'Others' title */
|
|
23
|
+
|
|
24
|
+
if (uncategorized.length) {
|
|
25
|
+
groupedVariables.push({
|
|
26
|
+
label: groupedVariables.length ? "Others" : null,
|
|
27
|
+
variables: uncategorized,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return groupedVariables;
|
|
31
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import VariableList from "./VariableList";
|
|
4
|
+
import Dropdown from "common/Dropdown";
|
|
5
|
+
import { HashtagFilled } from "common/Icons";
|
|
6
|
+
|
|
7
|
+
import { MENU_ICON_SIZE } from "../FixedMenu/constants";
|
|
8
|
+
|
|
9
|
+
const Variables = ({ editor, variables }) => {
|
|
10
|
+
const handleClickItem = (item) => {
|
|
11
|
+
const { category_key, key } = item;
|
|
12
|
+
const variableName = category_key ? `${category_key}.${key}` : key;
|
|
13
|
+
editor.chain().focus().setVariable({ label: variableName }).run();
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
if (!(variables && variables.length)) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<Dropdown
|
|
22
|
+
customTarget={() => (
|
|
23
|
+
<button className="relative h-full p-3 editor-fixed-menu--item variable-selection-popup">
|
|
24
|
+
<HashtagFilled size={MENU_ICON_SIZE} />
|
|
25
|
+
</button>
|
|
26
|
+
)}
|
|
27
|
+
>
|
|
28
|
+
<div className="items-container">
|
|
29
|
+
<VariableList onClickVariable={handleClickItem} variables={variables} />
|
|
30
|
+
</div>
|
|
31
|
+
</Dropdown>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default Variables;
|