@flozy/editor 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. package/.eslintignore +4 -0
  2. package/.eslintrc.json +6 -0
  3. package/.github/workflows/npm-publish.yml +33 -0
  4. package/.husky/pre-commit +1 -0
  5. package/.storybook/main.js +20 -0
  6. package/.storybook/preview.js +14 -0
  7. package/.vscode/extensions.json +7 -0
  8. package/.vscode/launch.json +15 -0
  9. package/.vscode/settings.json +22 -0
  10. package/README.md +2 -0
  11. package/craco.config.js +16 -0
  12. package/package.json +107 -0
  13. package/public/favicon.ico +0 -0
  14. package/public/index.html +43 -0
  15. package/public/logo192.png +0 -0
  16. package/public/logo512.png +0 -0
  17. package/public/manifest.json +25 -0
  18. package/public/robots.txt +3 -0
  19. package/src/components/Editor/CollaborativeEditor.js +119 -0
  20. package/src/components/Editor/CommonEditor.js +549 -0
  21. package/src/components/Editor/Editor.css +115 -0
  22. package/src/components/Editor/Elements/CodeToText/CodeToText.css +57 -0
  23. package/src/components/Editor/Elements/CodeToText/CodeToText.jsx +115 -0
  24. package/src/components/Editor/Elements/CodeToText/CodeToTextButton.jsx +16 -0
  25. package/src/components/Editor/Elements/CodeToText/HtmlCode.jsx +59 -0
  26. package/src/components/Editor/Elements/CodeToText/HtmlContextMenu.jsx +39 -0
  27. package/src/components/Editor/Elements/Color Picker/ColorPicker.css +38 -0
  28. package/src/components/Editor/Elements/Color Picker/ColorPicker.jsx +110 -0
  29. package/src/components/Editor/Elements/Color Picker/defaultColors.js +34 -0
  30. package/src/components/Editor/Elements/Embed/Embed.css +14 -0
  31. package/src/components/Editor/Elements/Embed/Embed.jsx +74 -0
  32. package/src/components/Editor/Elements/Embed/Image.jsx +82 -0
  33. package/src/components/Editor/Elements/Embed/Video.jsx +65 -0
  34. package/src/components/Editor/Elements/Equation/Equation.jsx +19 -0
  35. package/src/components/Editor/Elements/Equation/EquationButton.jsx +59 -0
  36. package/src/components/Editor/Elements/Equation/styles.css +4 -0
  37. package/src/components/Editor/Elements/Grid/Grid.js +48 -0
  38. package/src/components/Editor/Elements/Grid/GridButton.js +21 -0
  39. package/src/components/Editor/Elements/Grid/GridItem.js +57 -0
  40. package/src/components/Editor/Elements/ID/Id.jsx +56 -0
  41. package/src/components/Editor/Elements/Link/Link.jsx +24 -0
  42. package/src/components/Editor/Elements/Link/LinkButton.jsx +71 -0
  43. package/src/components/Editor/Elements/Link/styles.css +20 -0
  44. package/src/components/Editor/Elements/Mentions/Mentions.jsx +37 -0
  45. package/src/components/Editor/Elements/NewLine/NewLineButton.js +29 -0
  46. package/src/components/Editor/Elements/Table/Table.jsx +13 -0
  47. package/src/components/Editor/Elements/Table/TableSelector.css +18 -0
  48. package/src/components/Editor/Elements/Table/TableSelector.jsx +76 -0
  49. package/src/components/Editor/Elements/TableContextMenu/TableContextMenu.jsx +97 -0
  50. package/src/components/Editor/Elements/TableContextMenu/styles.css +18 -0
  51. package/src/components/Editor/RemoteCursorOverlay/Overlay.js +78 -0
  52. package/src/components/Editor/Toolbar/Toolbar.jsx +167 -0
  53. package/src/components/Editor/Toolbar/styles.css +28 -0
  54. package/src/components/Editor/Toolbar/toolbarGroups.js +167 -0
  55. package/src/components/Editor/Toolbar/toolbarIcons/align-center.svg +1 -0
  56. package/src/components/Editor/Toolbar/toolbarIcons/align-left.svg +1 -0
  57. package/src/components/Editor/Toolbar/toolbarIcons/align-right.svg +1 -0
  58. package/src/components/Editor/Toolbar/toolbarIcons/blockquote.svg +1 -0
  59. package/src/components/Editor/Toolbar/toolbarIcons/bold.png +0 -0
  60. package/src/components/Editor/Toolbar/toolbarIcons/fontColor.svg +4 -0
  61. package/src/components/Editor/Toolbar/toolbarIcons/headingOne.svg +3 -0
  62. package/src/components/Editor/Toolbar/toolbarIcons/headingTwo.svg +3 -0
  63. package/src/components/Editor/Toolbar/toolbarIcons/italic.png +0 -0
  64. package/src/components/Editor/Toolbar/toolbarIcons/link.svg +1 -0
  65. package/src/components/Editor/Toolbar/toolbarIcons/orderedList.svg +1 -0
  66. package/src/components/Editor/Toolbar/toolbarIcons/strikethrough.png +0 -0
  67. package/src/components/Editor/Toolbar/toolbarIcons/subscript.svg +1 -0
  68. package/src/components/Editor/Toolbar/toolbarIcons/superscript.svg +1 -0
  69. package/src/components/Editor/Toolbar/toolbarIcons/textColor.png +0 -0
  70. package/src/components/Editor/Toolbar/toolbarIcons/underline.png +0 -0
  71. package/src/components/Editor/Toolbar/toolbarIcons/unlink.svg +1 -0
  72. package/src/components/Editor/Toolbar/toolbarIcons/unorderedList.svg +1 -0
  73. package/src/components/Editor/YjsProvider.js +11 -0
  74. package/src/components/Editor/common/Button.jsx +12 -0
  75. package/src/components/Editor/common/Icon.jsx +82 -0
  76. package/src/components/Editor/common/MentionsPopup.jsx +56 -0
  77. package/src/components/Editor/hooks/useMentions.js +44 -0
  78. package/src/components/Editor/hooks/withCollaborative.js +15 -0
  79. package/src/components/Editor/hooks/withCommon.js +17 -0
  80. package/src/components/Editor/plugins/withEmbeds.js +36 -0
  81. package/src/components/Editor/plugins/withEquation.js +8 -0
  82. package/src/components/Editor/plugins/withLinks.js +9 -0
  83. package/src/components/Editor/plugins/withMentions.js +19 -0
  84. package/src/components/Editor/plugins/withTable.js +74 -0
  85. package/src/components/Editor/utils/SlateUtilityFunctions.js +273 -0
  86. package/src/components/Editor/utils/customHooks/useContextMenu.js +42 -0
  87. package/src/components/Editor/utils/customHooks/useFormat.js +26 -0
  88. package/src/components/Editor/utils/customHooks/usePopup.jsx +26 -0
  89. package/src/components/Editor/utils/customHooks/useResize.js +41 -0
  90. package/src/components/Editor/utils/draftToSlate.js +104 -0
  91. package/src/components/Editor/utils/embed.js +18 -0
  92. package/src/components/Editor/utils/equation.js +22 -0
  93. package/src/components/Editor/utils/events.js +56 -0
  94. package/src/components/Editor/utils/grid.js +12 -0
  95. package/src/components/Editor/utils/gridItem.js +19 -0
  96. package/src/components/Editor/utils/link.js +53 -0
  97. package/src/components/Editor/utils/mentions.js +11 -0
  98. package/src/components/Editor/utils/paragraph.js +4 -0
  99. package/src/components/Editor/utils/serializer.js +32 -0
  100. package/src/components/Editor/utils/table.js +151 -0
  101. package/src/components/index.js +5 -0
  102. package/src/index.js +1 -0
  103. package/src/stories/CollaborativeEditor.stories.js +30 -0
  104. package/src/stories/Editor.stories.js +24 -0
  105. package/src/stories/EditorSampleProps/ChatSample.js +43 -0
  106. package/src/stories/EditorSampleProps/LayoutOne.js +551 -0
@@ -0,0 +1,56 @@
1
+ import React, { useRef, useEffect } from "react";
2
+ import { Transforms } from "slate";
3
+ import { ReactEditor } from "slate-react";
4
+ import { insertMention } from "../utils/mentions";
5
+
6
+ const MentionsPopup = (props) => {
7
+ const { editor, index, target, chars, mentions, setMentions } = props;
8
+ const ref = useRef(null);
9
+
10
+ useEffect(() => {
11
+ if (target && chars.length > 0) {
12
+ const el = ref.current;
13
+ const domRange = ReactEditor.toDOMRange(editor, target);
14
+ const rect = domRange.getBoundingClientRect();
15
+ el.style.top = `${rect.top + window.scrollY + 24}px`;
16
+ el.style.left = `${rect.left + window.scrollX}px`;
17
+ }
18
+ }, [chars.length, editor, index, target]);
19
+
20
+ return target && chars.length > 0 ? (
21
+ <div
22
+ ref={ref}
23
+ style={{
24
+ top: "-9999px",
25
+ left: "-9999px",
26
+ position: "absolute",
27
+ zIndex: 1,
28
+ padding: "3px",
29
+ background: "white",
30
+ borderRadius: "4px",
31
+ boxShadow: "0 1px 5px rgba(0,0,0,.2)",
32
+ }}
33
+ data-cy="mentions-portal"
34
+ >
35
+ {chars.map((char, i) => (
36
+ <div
37
+ key={char}
38
+ onClick={() => {
39
+ Transforms.select(editor, target);
40
+ insertMention(editor, char);
41
+ setMentions({ ...mentions, target: null });
42
+ }}
43
+ style={{
44
+ padding: "1px 3px",
45
+ borderRadius: "3px",
46
+ background: i === index ? "#B4D5FF" : "transparent",
47
+ }}
48
+ >
49
+ {char}
50
+ </div>
51
+ ))}
52
+ </div>
53
+ ) : null;
54
+ };
55
+
56
+ export default MentionsPopup;
@@ -0,0 +1,44 @@
1
+ import { useEffect, useState } from "react";
2
+ import { Editor, Range } from "slate";
3
+
4
+ const useMentions = (props) => {
5
+ const { editor, selection } = props;
6
+ const [mentions, setMentions] = useState({
7
+ target: null,
8
+ index: null,
9
+ search: null,
10
+ });
11
+
12
+ useEffect(() => {
13
+ if (selection && Range.isCollapsed(selection)) {
14
+ const [start] = Range.edges(selection);
15
+ const wordBefore = Editor.before(editor, start, { unit: "word" });
16
+ const before = wordBefore && Editor.before(editor, wordBefore);
17
+ const beforeRange = before && Editor.range(editor, before, start);
18
+ const beforeText = beforeRange && Editor.string(editor, beforeRange);
19
+ const beforeMatch = beforeText && beforeText.match(/^@(\w+)$/);
20
+ const after = Editor.after(editor, start);
21
+ const afterRange = Editor.range(editor, start, after);
22
+ const afterText = Editor.string(editor, afterRange);
23
+ const afterMatch = afterText.match(/^(\s|$)/);
24
+
25
+ if (beforeMatch && afterMatch) {
26
+ setMentions({
27
+ target: beforeRange,
28
+ search: beforeMatch[1],
29
+ index: 0,
30
+ });
31
+ }
32
+ } else {
33
+ setMentions({
34
+ target: null,
35
+ search: null,
36
+ index: null,
37
+ });
38
+ }
39
+ }, [selection, editor]);
40
+
41
+ return [mentions, setMentions];
42
+ };
43
+
44
+ export default useMentions;
@@ -0,0 +1,15 @@
1
+ import { createEditor } from "slate";
2
+ import { withYjs, withCursors } from "@slate-yjs/core";
3
+
4
+ const withCollaborative = (props) => {
5
+ const { provider, sharedType, data } = props;
6
+ return withCursors(
7
+ withYjs(createEditor(), sharedType, { autoConnect: false }),
8
+ provider.awareness,
9
+ {
10
+ data,
11
+ }
12
+ );
13
+ };
14
+
15
+ export default withCollaborative;
@@ -0,0 +1,17 @@
1
+ import { withReact } from "slate-react";
2
+ import { withHistory } from "slate-history";
3
+ import withLinks from "../plugins/withLinks";
4
+ import withTables from "../plugins/withTable";
5
+ import withEmbeds from "../plugins/withEmbeds";
6
+ import withEquation from "../plugins/withEquation";
7
+ import withMentions from "../plugins/withMentions";
8
+
9
+ const withCommon = (props) => {
10
+ return withEquation(
11
+ withHistory(
12
+ withEmbeds(withTables(withLinks(withMentions(withReact(props)))))
13
+ )
14
+ );
15
+ };
16
+
17
+ export default withCommon;
@@ -0,0 +1,36 @@
1
+ import {Transforms, Path, Node} from 'slate'
2
+
3
+ const withEmbeds = (editor) =>{
4
+
5
+ const { isVoid, insertBreak } = editor;
6
+
7
+ editor.isVoid = (element) => ['video','image','htmlCode'].includes(element.type) ? true : isVoid(element);
8
+
9
+
10
+
11
+ editor.insertBreak = (...args) => {
12
+ const parentPath = Path.parent(editor.selection.focus.path);
13
+ const parentNode = Node.get(editor, parentPath);
14
+ // console.log(parentNode);
15
+ if (editor.isVoid(parentNode)) {
16
+ const nextPath = Path.next(parentPath);
17
+ Transforms.insertNodes(
18
+ editor,
19
+ {
20
+ type: 'paragraph',
21
+ children: [{ text: '' }]
22
+ },
23
+ {
24
+ at: nextPath,
25
+ select: true // Focus on this node once inserted
26
+ }
27
+ );
28
+ } else {
29
+ insertBreak(...args);
30
+ }
31
+ }
32
+ return editor;
33
+ }
34
+
35
+
36
+ export default withEmbeds
@@ -0,0 +1,8 @@
1
+ const withEquation = (editor) =>{
2
+ const { isInline } = editor;
3
+ editor.isInline = (element) =>
4
+ element.type === 'equation' && element.inline ? true : isInline(element)
5
+ return editor;
6
+ }
7
+
8
+ export default withEquation;
@@ -0,0 +1,9 @@
1
+ const withLinks = (editor)=>{
2
+
3
+ const { isInline } = editor;
4
+ editor.isInline = (element) =>
5
+ element.type === 'link' ? true :isInline(element);
6
+ return editor;
7
+ };
8
+
9
+ export default withLinks;
@@ -0,0 +1,19 @@
1
+ const withMentions = (editor) => {
2
+ const { isInline, isVoid, markableVoid } = editor;
3
+
4
+ editor.isInline = (element) => {
5
+ return element.type === "mention" ? true : isInline(element);
6
+ };
7
+
8
+ editor.isVoid = (element) => {
9
+ return element.type === "mention" ? true : isVoid(element);
10
+ };
11
+
12
+ editor.markableVoid = (element) => {
13
+ return element.type === "mention" || markableVoid(element);
14
+ };
15
+
16
+ return editor;
17
+ };
18
+
19
+ export default withMentions;
@@ -0,0 +1,74 @@
1
+ import { Editor, Range, Point, Element } from "slate";
2
+
3
+ const withTable = (editor) => {
4
+ const { deleteBackward, deleteForward } = editor;
5
+
6
+ editor.deleteBackward = (unit) => {
7
+ const { selection } = editor;
8
+ if (selection) {
9
+ const [cell] = Editor.nodes(editor, {
10
+ match: (n) =>
11
+ !Editor.isEditor(n) &&
12
+ Element.isElement(n) &&
13
+ n.type === "table-cell",
14
+ });
15
+ const prevNodePath = Editor.before(editor, selection);
16
+
17
+ const [tableNode] = Editor.nodes(editor, {
18
+ at: prevNodePath,
19
+ match: (n) =>
20
+ !Editor.isEditor(n) && Element.isElement && n.type === "table-cell",
21
+ });
22
+
23
+ if (cell) {
24
+ const [, cellPath] = cell;
25
+
26
+ const start = Editor.start(editor, cellPath);
27
+ if (Point.equals(selection.anchor, start)) {
28
+ return;
29
+ }
30
+ }
31
+ if (!cell && tableNode) {
32
+ return;
33
+ }
34
+ }
35
+
36
+ deleteBackward(unit);
37
+ };
38
+ editor.deleteForward = (unit) => {
39
+ const { selection } = editor;
40
+ if (selection && Range.isCollapsed(selection)) {
41
+ const [cell] = Editor.nodes(editor, {
42
+ match: (n) =>
43
+ !Editor.isEditor(n) &&
44
+ Element.isElement(n) &&
45
+ n.type === "table-cell",
46
+ });
47
+
48
+ const prevNodePath = Editor.after(editor, selection);
49
+ const [tableNode] = Editor.nodes(editor, {
50
+ at: prevNodePath,
51
+ match: (n) =>
52
+ !Editor.isEditor(n) && Element.isElement && n.type === "table-cell",
53
+ });
54
+
55
+ if (cell) {
56
+ const [, cellPath] = cell;
57
+ const end = Editor.end(editor, cellPath);
58
+
59
+ if (Point.equals(selection.anchor, end)) {
60
+ return;
61
+ }
62
+ }
63
+ if (!cell && tableNode) {
64
+ return;
65
+ }
66
+ }
67
+
68
+ deleteForward(unit);
69
+ };
70
+
71
+ return editor;
72
+ };
73
+
74
+ export default withTable;
@@ -0,0 +1,273 @@
1
+ import { Editor, Transforms, Element as SlateElement } from "slate";
2
+ import Link from "../Elements/Link/Link";
3
+ import Image from "../Elements/Embed/Image";
4
+ import Video from "../Elements/Embed/Video";
5
+ import Equation from "../Elements/Equation/Equation";
6
+ import HtmlCode from "../Elements/CodeToText/HtmlCode";
7
+ import Table from "../Elements/Table/Table";
8
+ import Mentions from "../Elements/Mentions/Mentions";
9
+ import Grid from "../Elements/Grid/Grid";
10
+ import GridItem from "../Elements/Grid/GridItem";
11
+
12
+ const alignment = ["alignLeft", "alignRight", "alignCenter"];
13
+ const list_types = ["orderedList", "unorderedList"];
14
+
15
+ export const sizeMap = {
16
+ small: "0.75em",
17
+ normal: "1em",
18
+ medium: "1.75em",
19
+ huge: "2.5em",
20
+ };
21
+ export const fontFamilyMap = {
22
+ sans: "Helvetica,Arial, sans serif",
23
+ serif: "Georgia, Times New Roaman,serif",
24
+ monospace: "Monaco, Courier New,monospace",
25
+ };
26
+ export const toggleBlock = (editor, format) => {
27
+ const isActive = isBlockActive(editor, format);
28
+ const isList = list_types.includes(format);
29
+ const isIndent = alignment.includes(format);
30
+ const isAligned = alignment.some((alignmentType) =>
31
+ isBlockActive(editor, alignmentType)
32
+ );
33
+
34
+ /*If the node is already aligned and change in indent is called we should unwrap it first and split the node to prevent
35
+ messy, nested DOM structure and bugs due to that.*/
36
+ if (isAligned && isIndent) {
37
+ Transforms.unwrapNodes(editor, {
38
+ match: (n) =>
39
+ alignment.includes(
40
+ !Editor.isEditor(n) && SlateElement.isElement(n) && n.type
41
+ ),
42
+ split: true,
43
+ });
44
+ }
45
+
46
+ /* Wraping the nodes for alignment, to allow it to co-exist with other block level operations*/
47
+ if (isIndent) {
48
+ Transforms.wrapNodes(editor, {
49
+ type: format,
50
+ children: [],
51
+ });
52
+ return;
53
+ }
54
+ Transforms.unwrapNodes(editor, {
55
+ match: (n) =>
56
+ list_types.includes(
57
+ !Editor.isEditor(n) && SlateElement.isElement(n) && n.type
58
+ ),
59
+ split: true,
60
+ });
61
+
62
+ Transforms.setNodes(editor, {
63
+ type: isActive ? "paragraph" : isList ? "list-item" : format,
64
+ });
65
+
66
+ if (isList && !isActive) {
67
+ Transforms.wrapNodes(editor, {
68
+ type: format,
69
+ children: [],
70
+ });
71
+ }
72
+ };
73
+ export const addMarkData = (editor, data) => {
74
+ Editor.addMark(editor, data.format, data.value);
75
+ };
76
+ export const toggleMark = (editor, format) => {
77
+ const isActive = isMarkActive(editor, format);
78
+
79
+ if (isActive) {
80
+ Editor.removeMark(editor, format);
81
+ } else {
82
+ Editor.addMark(editor, format, true);
83
+ }
84
+ };
85
+ export const isMarkActive = (editor, format) => {
86
+ const marks = Editor.marks(editor);
87
+
88
+ return marks ? marks[format] === true : false;
89
+ };
90
+
91
+ export const isBlockActive = (editor, format) => {
92
+ const [match] = Editor.nodes(editor, {
93
+ match: (n) =>
94
+ !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === format,
95
+ });
96
+
97
+ return !!match;
98
+ };
99
+
100
+ export const activeMark = (editor, format) => {
101
+ const defaultMarkData = {
102
+ color: "black",
103
+ bgColor: "black",
104
+ fontSize: "normal",
105
+ fontFamily: "sans",
106
+ };
107
+ const marks = Editor.marks(editor);
108
+ const defaultValue = defaultMarkData[format];
109
+ return marks?.[format] ?? defaultValue;
110
+ };
111
+
112
+ export const getMarked = (leaf, children) => {
113
+ if (leaf.bold) {
114
+ children = <strong>{children}</strong>;
115
+ }
116
+
117
+ if (leaf.code) {
118
+ children = <code>{children}</code>;
119
+ }
120
+
121
+ if (leaf.italic) {
122
+ children = <em>{children}</em>;
123
+ }
124
+ if (leaf.strikethrough) {
125
+ children = (
126
+ <span style={{ textDecoration: "line-through" }}>{children}</span>
127
+ );
128
+ }
129
+ if (leaf.underline) {
130
+ children = <u>{children}</u>;
131
+ }
132
+ if (leaf.superscript) {
133
+ children = <sup>{children}</sup>;
134
+ }
135
+ if (leaf.subscript) {
136
+ children = <sub>{children}</sub>;
137
+ }
138
+ if (leaf.color) {
139
+ children = <span style={{ color: leaf.color }}>{children}</span>;
140
+ }
141
+ if (leaf.bgColor) {
142
+ children = (
143
+ <span style={{ backgroundColor: leaf.bgColor }}>{children}</span>
144
+ );
145
+ }
146
+ if (leaf.fontSize) {
147
+ const size = sizeMap[leaf.fontSize];
148
+ children = <span style={{ fontSize: size }}>{children}</span>;
149
+ }
150
+ if (leaf.fontFamily) {
151
+ const family = fontFamilyMap[leaf.fontFamily];
152
+ children = <span style={{ fontFamily: family }}>{children}</span>;
153
+ }
154
+ return children;
155
+ };
156
+
157
+ export const getBlock = (props) => {
158
+ const { element, children } = props;
159
+ const attributes = props.attributes ?? {};
160
+
161
+ switch (element.type) {
162
+ case "headingOne":
163
+ return (
164
+ <h1 {...attributes} {...element.attr}>
165
+ {children}
166
+ </h1>
167
+ );
168
+ case "headingTwo":
169
+ return (
170
+ <h2 {...attributes} {...element.attr}>
171
+ {children}
172
+ </h2>
173
+ );
174
+ case "headingThree":
175
+ return (
176
+ <h3 {...attributes} {...element.attr}>
177
+ {children}
178
+ </h3>
179
+ );
180
+ case "blockquote":
181
+ return (
182
+ <blockquote {...attributes} {...element.attr}>
183
+ {children}
184
+ </blockquote>
185
+ );
186
+ case "alignLeft":
187
+ return (
188
+ <div
189
+ style={{ listStylePosition: "inside" }}
190
+ {...attributes}
191
+ {...element.attr}
192
+ >
193
+ {children}
194
+ </div>
195
+ );
196
+ case "alignCenter":
197
+ return (
198
+ <div
199
+ style={{
200
+ display: "flex",
201
+ alignItems: "center",
202
+ listStylePosition: "inside",
203
+ flexDirection: "column",
204
+ }}
205
+ {...attributes}
206
+ {...element.attr}
207
+ >
208
+ {children}
209
+ </div>
210
+ );
211
+ case "alignRight":
212
+ return (
213
+ <div
214
+ style={{
215
+ display: "flex",
216
+ alignItems: "flex-end",
217
+ listStylePosition: "inside",
218
+ flexDirection: "column",
219
+ }}
220
+ {...attributes}
221
+ {...element.attr}
222
+ >
223
+ {children}
224
+ </div>
225
+ );
226
+ case "list-item":
227
+ return (
228
+ <li {...attributes} {...element.attr}>
229
+ {children}
230
+ </li>
231
+ );
232
+ case "orderedList":
233
+ return (
234
+ <ol type="1" {...attributes}>
235
+ {children}
236
+ </ol>
237
+ );
238
+ case "unorderedList":
239
+ return <ul {...attributes}>{children}</ul>;
240
+ case "link":
241
+ return <Link {...props} />;
242
+ case "table":
243
+ return <Table {...props} />;
244
+ case "table-row":
245
+ return <tr {...attributes}>{children}</tr>;
246
+ case "table-cell":
247
+ return (
248
+ <td {...element.attr} {...attributes}>
249
+ {children}
250
+ </td>
251
+ );
252
+ case "image":
253
+ return <Image {...props} />;
254
+ case "video":
255
+ return <Video {...props} />;
256
+ case "equation":
257
+ return <Equation {...props} />;
258
+ case "htmlCode":
259
+ return <HtmlCode {...props} />;
260
+ case "mention":
261
+ return <Mentions {...props} />;
262
+ case "grid":
263
+ return <Grid {...props} />;
264
+ case "grid-item":
265
+ return <GridItem {...props} />;
266
+ default:
267
+ return (
268
+ <div {...element.attr} {...attributes}>
269
+ {children}
270
+ </div>
271
+ );
272
+ }
273
+ };
@@ -0,0 +1,42 @@
1
+ import { useState, useEffect } from 'react';
2
+ import useFormat from './useFormat.js';
3
+
4
+
5
+ //This hook returns should we show the custom context menu and where to show it.
6
+ const useContextMenu = (editor,format,setSelection) => {
7
+ const isFormat = useFormat(editor,format);
8
+ const [showMenu,setShowMenu] = useState(false);
9
+ const [menuLocation,setMenuLocation] = useState({
10
+ top:'0px',
11
+ left:'0px'
12
+ });
13
+
14
+ const handleClick = ()=>{
15
+ setShowMenu(false);
16
+ }
17
+ const handleContextMenu = (e) => {
18
+ if(!isFormat) return;
19
+ setSelection(editor.selection);
20
+ e.preventDefault();
21
+ setShowMenu(true);
22
+ const xPos = e.pageX + "px";
23
+ const yPos = e.pageY + "px";
24
+ setMenuLocation({
25
+ top:yPos,
26
+ left:xPos
27
+ })
28
+ }
29
+ useEffect(()=>{
30
+ document.addEventListener('click',handleClick);
31
+ document.addEventListener('contextmenu',handleContextMenu);
32
+
33
+ return ()=>{
34
+ document.removeEventListener('click',handleClick);
35
+ document.removeEventListener('contextmenu',handleContextMenu);
36
+ }
37
+ },[isFormat])
38
+
39
+ return [showMenu,menuLocation];
40
+ }
41
+
42
+ export default useContextMenu;
@@ -0,0 +1,26 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { Editor, Element } from 'slate'
3
+
4
+
5
+ // This hook returns if the node in the current selection matches the format passed to it.
6
+ const useFormat = (editor,format)=>{
7
+ const [isFormat,setIsFormat] = useState(false);
8
+ useEffect(()=>{
9
+ if(editor.selection){
10
+ // It matches at the editor.selection location by default, so if null handle it seperately.
11
+ const [node] = Editor.nodes(editor,{
12
+ match:n => !Editor.isEditor(n) && Element.isElement(n) && n.type === format
13
+ })
14
+
15
+ setIsFormat(!!node);
16
+ }
17
+ else{
18
+ setIsFormat(false);
19
+ }
20
+ // eslint-disable-next-line react-hooks/exhaustive-deps
21
+ },[editor.selection])
22
+
23
+ return isFormat;
24
+ }
25
+
26
+ export default useFormat;
@@ -0,0 +1,26 @@
1
+ import { useState, useEffect } from 'react'
2
+
3
+ //This hook returns if the click was inside the popUp ref or outside it .
4
+ function usePopup(popupRef) {
5
+ const [showPopup,setShowPopup] = useState(false);
6
+
7
+ useEffect(()=>{
8
+ const handleDocumentClick = (e)=>{
9
+ const clickedComponent = e.target;
10
+ if(!popupRef?.current?.contains(clickedComponent)){
11
+ setShowPopup(false);
12
+ }
13
+ }
14
+ document.addEventListener('click',handleDocumentClick);
15
+
16
+ return ()=>{
17
+ document.removeEventListener('click',handleDocumentClick)
18
+ }
19
+ // eslint-disable-next-line react-hooks/exhaustive-deps
20
+ }, [])
21
+
22
+ return [showPopup,setShowPopup];
23
+ }
24
+
25
+ export default usePopup
26
+
@@ -0,0 +1,41 @@
1
+ import { useState } from "react";
2
+
3
+ const useResize = ({ parentDOM, size: defaultSize }) => {
4
+ const { width } = parentDOM?.getBoundingClientRect() || { ...defaultSize };
5
+ const [size, setSize] = useState({
6
+ height: 300,
7
+ widthInPercent: 100,
8
+ ...defaultSize,
9
+ });
10
+ const [resizing, setResizing] = useState(false);
11
+
12
+ const onLoad = (dom) => {
13
+ setSize({ widthInPercent: 100, height: 300, ...defaultSize });
14
+ };
15
+
16
+ const onMouseDown = () => {
17
+ document.addEventListener("pointermove", onMouseMove);
18
+ document.addEventListener("pointerup", onMouseUp);
19
+ setResizing(true);
20
+ };
21
+ const onMouseUp = () => {
22
+ document.removeEventListener("pointermove", onMouseMove);
23
+ document.removeEventListener("pointerup", onMouseUp);
24
+ setResizing(false);
25
+ };
26
+ const onMouseMove = (e) => {
27
+ setSize((currentSize) => {
28
+ const widthInPX = (currentSize.widthInPercent / 100) * width;
29
+ const calcWidth = widthInPX + e.movementX;
30
+ return {
31
+ width: calcWidth,
32
+ height: currentSize.height + e.movementY,
33
+ widthInPercent: (calcWidth / width) * 100,
34
+ };
35
+ });
36
+ };
37
+
38
+ return [size, onMouseDown, resizing, onLoad];
39
+ };
40
+
41
+ export default useResize;