@springmicro/rte 0.1.3
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/.eslintrc.cjs +18 -0
- package/README.md +15 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +63921 -0
- package/dist/index.umd.cjs +469 -0
- package/dist/style.css +1 -0
- package/index.html +13 -0
- package/package.json +54 -0
- package/src/App.css +42 -0
- package/src/App.tsx +10 -0
- package/src/contexts/color-context.tsx +53 -0
- package/src/hooks/useSimpleFormik.tsx +74 -0
- package/src/index.css +68 -0
- package/src/index.tsx +3 -0
- package/src/main.tsx +10 -0
- package/src/slate/base-editor.stories.tsx +16 -0
- package/src/slate/base-editor.tsx +116 -0
- package/src/slate/blog-rte.stories.tsx +16 -0
- package/src/slate/blog-rte.tsx +126 -0
- package/src/slate/common/button.tsx +35 -0
- package/src/slate/common/element.tsx +13 -0
- package/src/slate/common/icon.jsx +97 -0
- package/src/slate/components/code-to-text/CodeToTextButton.jsx +19 -0
- package/src/slate/components/code-to-text/HtmlCode.jsx +64 -0
- package/src/slate/components/code-to-text/HtmlContextMenu.jsx +39 -0
- package/src/slate/components/code-to-text/index.jsx +111 -0
- package/src/slate/components/color-picker/color-cursor.stories.tsx +16 -0
- package/src/slate/components/color-picker/color-cursor.tsx +34 -0
- package/src/slate/components/color-picker/color-formats-view.stories.tsx +25 -0
- package/src/slate/components/color-picker/color-formats-view.tsx +115 -0
- package/src/slate/components/color-picker/color-gradient.stories.tsx +48 -0
- package/src/slate/components/color-picker/color-gradient.tsx +128 -0
- package/src/slate/components/color-picker/color-hue.stories.tsx +41 -0
- package/src/slate/components/color-picker/color-hue.tsx +110 -0
- package/src/slate/components/color-picker/color-picker.stories.tsx +25 -0
- package/src/slate/components/color-picker/color-picker.tsx +41 -0
- package/src/slate/components/color-picker/color-popover.stories.tsx +26 -0
- package/src/slate/components/color-picker/color-popover.tsx +58 -0
- package/src/slate/components/color-picker/color-swatch.stories.tsx +16 -0
- package/src/slate/components/color-picker/color-swatch.tsx +76 -0
- package/src/slate/components/color-picker/default-colors.ts +38 -0
- package/src/slate/components/color-picker/slate-color-button.tsx +128 -0
- package/src/slate/components/embed/Embed.jsx +96 -0
- package/src/slate/components/embed/Image.jsx +45 -0
- package/src/slate/components/embed/Video.jsx +65 -0
- package/src/slate/components/equation/Equation.jsx +19 -0
- package/src/slate/components/equation/EquationButton.jsx +68 -0
- package/src/slate/components/id/Id.jsx +57 -0
- package/src/slate/components/image/image.stories.tsx +17 -0
- package/src/slate/components/image/image.tsx +62 -0
- package/src/slate/components/image/insert-image-button.stories.tsx +83 -0
- package/src/slate/components/image/insert-image-button.tsx +132 -0
- package/src/slate/components/image/types.ts +9 -0
- package/src/slate/components/link/Link.jsx +56 -0
- package/src/slate/components/link/LinkButton.tsx +106 -0
- package/src/slate/components/table/Table.jsx +11 -0
- package/src/slate/components/table/TableSelector.jsx +97 -0
- package/src/slate/components/table-context-menu/TableContextMenu.tsx +106 -0
- package/src/slate/custom-types.d.ts +152 -0
- package/src/slate/editor.module.css +226 -0
- package/src/slate/paper-rte.stories.tsx +16 -0
- package/src/slate/paper-rte.tsx +47 -0
- package/src/slate/plugins/withEmbeds.js +33 -0
- package/src/slate/plugins/withEquation.js +8 -0
- package/src/slate/plugins/withImages.ts +69 -0
- package/src/slate/plugins/withLinks.js +9 -0
- package/src/slate/plugins/withTable.js +74 -0
- package/src/slate/serializers/generic.ts +44 -0
- package/src/slate/serializers/types.ts +20 -0
- package/src/slate/toolbar/index.tsx +186 -0
- package/src/slate/toolbar/paper-toolbar.tsx +494 -0
- package/src/slate/toolbar/shortcuts.tsx +77 -0
- package/src/slate/toolbar/toolbar-groups.ts +213 -0
- package/src/slate/types/index.ts +0 -0
- package/src/slate/utils/customHooks/useContextMenu.js +42 -0
- package/src/slate/utils/customHooks/useFormat.js +26 -0
- package/src/slate/utils/customHooks/usePopup.jsx +26 -0
- package/src/slate/utils/customHooks/useResize.js +27 -0
- package/src/slate/utils/embed.js +18 -0
- package/src/slate/utils/equation.js +22 -0
- package/src/slate/utils/index.jsx +267 -0
- package/src/slate/utils/link.js +44 -0
- package/src/slate/utils/p.js +4 -0
- package/src/slate/utils/table.js +131 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +32 -0
- package/tsconfig.node.json +10 -0
- package/vite.config.ts +41 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
export type ToolbarGroup = {
|
|
2
|
+
id: number;
|
|
3
|
+
format: string;
|
|
4
|
+
type: string;
|
|
5
|
+
options?: Array<{ text: string; value: string }>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const fonts: ToolbarGroup[] = [
|
|
9
|
+
{
|
|
10
|
+
id: 1,
|
|
11
|
+
format: "fontFamily",
|
|
12
|
+
type: "dropdown",
|
|
13
|
+
options: [
|
|
14
|
+
{ text: "Sans Serif", value: "sans-serif" },
|
|
15
|
+
{ text: "Serif", value: "serif" },
|
|
16
|
+
{ text: "MonoSpace", value: "monospace" },
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: 2,
|
|
21
|
+
format: "fontSize",
|
|
22
|
+
type: "dropdown",
|
|
23
|
+
options: [
|
|
24
|
+
{ text: "Small", value: "small" },
|
|
25
|
+
{ text: "Normal", value: "normal" },
|
|
26
|
+
{ text: "Medium", value: "medium" },
|
|
27
|
+
{ text: "Huge", value: "huge" },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const fontStyles: ToolbarGroup[] = [
|
|
33
|
+
{
|
|
34
|
+
id: 3,
|
|
35
|
+
format: "bold",
|
|
36
|
+
type: "mark",
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: 4,
|
|
40
|
+
format: "italic",
|
|
41
|
+
type: "mark",
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: 5,
|
|
45
|
+
format: "underline",
|
|
46
|
+
type: "mark",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 6,
|
|
50
|
+
format: "strikethrough",
|
|
51
|
+
type: "mark",
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
const colors: ToolbarGroup[] = [
|
|
56
|
+
{
|
|
57
|
+
id: 7,
|
|
58
|
+
format: "color",
|
|
59
|
+
type: "color-picker",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: 8,
|
|
63
|
+
format: "bgColor",
|
|
64
|
+
type: "color-picker",
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
const subsuper: ToolbarGroup[] = [
|
|
69
|
+
{
|
|
70
|
+
id: 9,
|
|
71
|
+
format: "superscript",
|
|
72
|
+
type: "mark",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
id: 10,
|
|
76
|
+
format: "subscript",
|
|
77
|
+
type: "mark",
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const headings: ToolbarGroup[] = [
|
|
82
|
+
{
|
|
83
|
+
id: 11,
|
|
84
|
+
format: "h1",
|
|
85
|
+
type: "block",
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: 12,
|
|
89
|
+
format: "h2",
|
|
90
|
+
type: "block",
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
id: 13,
|
|
94
|
+
format: "h3",
|
|
95
|
+
type: "block",
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: 14,
|
|
99
|
+
format: "blockquote",
|
|
100
|
+
type: "block",
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
const lists: ToolbarGroup[] = [
|
|
105
|
+
{
|
|
106
|
+
id: 15,
|
|
107
|
+
format: "orderedList",
|
|
108
|
+
type: "block",
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: 16,
|
|
112
|
+
format: "unorderedList",
|
|
113
|
+
type: "block",
|
|
114
|
+
},
|
|
115
|
+
];
|
|
116
|
+
|
|
117
|
+
const align: ToolbarGroup[] = [
|
|
118
|
+
{
|
|
119
|
+
id: 17,
|
|
120
|
+
format: "alignLeft",
|
|
121
|
+
type: "block",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 18,
|
|
125
|
+
format: "alignCenter",
|
|
126
|
+
type: "block",
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: 19,
|
|
130
|
+
format: "alignRight",
|
|
131
|
+
type: "block",
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
const links: ToolbarGroup[] = [
|
|
135
|
+
{
|
|
136
|
+
id: 20,
|
|
137
|
+
format: "link",
|
|
138
|
+
type: "link",
|
|
139
|
+
},
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
const images: ToolbarGroup[] = [
|
|
143
|
+
{
|
|
144
|
+
id: 21,
|
|
145
|
+
format: "image",
|
|
146
|
+
type: "image",
|
|
147
|
+
},
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
// const embed = [
|
|
151
|
+
// {
|
|
152
|
+
// id: 21,
|
|
153
|
+
// format: "image",
|
|
154
|
+
// type: "embed",
|
|
155
|
+
// },
|
|
156
|
+
// {
|
|
157
|
+
// id: 22,
|
|
158
|
+
// format: "video",
|
|
159
|
+
// type: "embed",
|
|
160
|
+
// },
|
|
161
|
+
// ]
|
|
162
|
+
const tables: ToolbarGroup[] = [
|
|
163
|
+
{
|
|
164
|
+
id: 23,
|
|
165
|
+
type: "table",
|
|
166
|
+
format: "table",
|
|
167
|
+
},
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
// Not currently serialized
|
|
171
|
+
// const mathAndCode: ToolbarGroup[] = [
|
|
172
|
+
// {
|
|
173
|
+
// id: 25,
|
|
174
|
+
// type: "id",
|
|
175
|
+
// },
|
|
176
|
+
// {
|
|
177
|
+
// id: 26,
|
|
178
|
+
// type: "equation",
|
|
179
|
+
// },
|
|
180
|
+
// {
|
|
181
|
+
// id: 27,
|
|
182
|
+
// type: "codeToText",
|
|
183
|
+
// },
|
|
184
|
+
// ];
|
|
185
|
+
|
|
186
|
+
export const allToolbarGroups = [
|
|
187
|
+
fonts,
|
|
188
|
+
headings,
|
|
189
|
+
fontStyles,
|
|
190
|
+
colors,
|
|
191
|
+
subsuper,
|
|
192
|
+
lists,
|
|
193
|
+
align,
|
|
194
|
+
links,
|
|
195
|
+
images,
|
|
196
|
+
tables,
|
|
197
|
+
// mathAndCode,
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
export const issuesToolbarGroups = [headings, fontStyles, lists, links];
|
|
201
|
+
|
|
202
|
+
export const blogToolbarGroups = [
|
|
203
|
+
headings,
|
|
204
|
+
fonts,
|
|
205
|
+
align,
|
|
206
|
+
subsuper,
|
|
207
|
+
tables,
|
|
208
|
+
fontStyles,
|
|
209
|
+
colors,
|
|
210
|
+
lists,
|
|
211
|
+
links,
|
|
212
|
+
images,
|
|
213
|
+
];
|
|
File without changes
|
|
@@ -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,27 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
const useResize = ()=>{
|
|
4
|
+
const [size,setSize] = useState({width:300,height:300});
|
|
5
|
+
const [resizing,setResizing] = useState(false);
|
|
6
|
+
const onMouseDown = ()=>{
|
|
7
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
8
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
9
|
+
setResizing(true);
|
|
10
|
+
}
|
|
11
|
+
const onMouseUp = () =>{
|
|
12
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
13
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
14
|
+
setResizing(false);
|
|
15
|
+
}
|
|
16
|
+
const onMouseMove = (e) => {
|
|
17
|
+
setSize(currentSize => ({
|
|
18
|
+
width: currentSize.width + e.movementX ,
|
|
19
|
+
height: currentSize.height + e.movementY
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return [size,onMouseDown,resizing];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export default useResize;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Transforms } from "slate"
|
|
2
|
+
|
|
3
|
+
import { createParagraph } from "./p"
|
|
4
|
+
export const createEmbedNode = (type, { url, alt }) => ({
|
|
5
|
+
type,
|
|
6
|
+
alt,
|
|
7
|
+
url,
|
|
8
|
+
children: [{ text: "" }],
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
export const insertEmbed = (editor, embedData, format) => {
|
|
12
|
+
const { url } = embedData
|
|
13
|
+
if (!url) return
|
|
14
|
+
const embed = createEmbedNode(format, embedData)
|
|
15
|
+
|
|
16
|
+
Transforms.insertNodes(editor, embed, { select: true })
|
|
17
|
+
Transforms.insertNodes(editor, createParagraph(""))
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Transforms, Range } from "slate";
|
|
2
|
+
|
|
3
|
+
const createEquationNode = (math,inline) =>({
|
|
4
|
+
type:'equation',
|
|
5
|
+
inline,
|
|
6
|
+
math,
|
|
7
|
+
children:[{text:''}]
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
export const insertEquation = (editor,math,inline) =>{
|
|
11
|
+
const equation = createEquationNode(math,inline);
|
|
12
|
+
|
|
13
|
+
const {selection} = editor;
|
|
14
|
+
if(!!selection){
|
|
15
|
+
if(Range.isExpanded(selection)) Transforms.collapse(editor,{edge:'end'});
|
|
16
|
+
|
|
17
|
+
Transforms.insertNodes(editor,equation,{select:true})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Editor, Transforms, Element as SlateElement } from "slate";
|
|
2
|
+
import { useSlateStatic } from "slate-react";
|
|
3
|
+
import Link from "../components/link/Link";
|
|
4
|
+
import Image from "../components/image/image";
|
|
5
|
+
import Video from "../components/embed/Video";
|
|
6
|
+
import Equation from "../components/equation/Equation";
|
|
7
|
+
import HtmlCode from "../components/code-to-text/HtmlCode";
|
|
8
|
+
import Table from "../components/table/Table";
|
|
9
|
+
|
|
10
|
+
const alignment = ["alignLeft", "alignRight", "alignCenter"];
|
|
11
|
+
const list_types = ["orderedList", "unorderedList"];
|
|
12
|
+
|
|
13
|
+
export const sizeMap = {
|
|
14
|
+
small: "0.75em",
|
|
15
|
+
normal: "1em",
|
|
16
|
+
medium: "1.75em",
|
|
17
|
+
huge: "2.5em",
|
|
18
|
+
};
|
|
19
|
+
export const fontFamilyMap = {
|
|
20
|
+
sans: "Helvetica, Arial, sans serif",
|
|
21
|
+
serif: "Georgia, Times New Roman, serif",
|
|
22
|
+
monospace: "Monaco, Courier New, monospace",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const defaultMarkData = {
|
|
26
|
+
color: "black",
|
|
27
|
+
bgColor: "yellow",
|
|
28
|
+
fontSize: "normal",
|
|
29
|
+
fontFamily: "sans-serif",
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const toggleBlock = (editor, format) => {
|
|
33
|
+
const isActive = isBlockActive(editor, format);
|
|
34
|
+
const isList = list_types.includes(format);
|
|
35
|
+
const isIndent = alignment.includes(format);
|
|
36
|
+
const isAligned = alignment.some((alignmentType) =>
|
|
37
|
+
isBlockActive(editor, alignmentType)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/*If the node is already aligned and change in indent is called we should unwrap it first and split the node to prevent
|
|
41
|
+
messy, nested DOM structure and bugs due to that.*/
|
|
42
|
+
if (isAligned && isIndent) {
|
|
43
|
+
Transforms.unwrapNodes(editor, {
|
|
44
|
+
match: (n) =>
|
|
45
|
+
alignment.includes(
|
|
46
|
+
!Editor.isEditor(n) && SlateElement.isElement(n) && n.type
|
|
47
|
+
),
|
|
48
|
+
split: true,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* Wraping the nodes for alignment, to allow it to co-exist with other block level operations*/
|
|
53
|
+
if (isIndent) {
|
|
54
|
+
Transforms.wrapNodes(editor, {
|
|
55
|
+
type: format,
|
|
56
|
+
children: [],
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
Transforms.unwrapNodes(editor, {
|
|
61
|
+
match: (n) =>
|
|
62
|
+
list_types.includes(
|
|
63
|
+
!Editor.isEditor(n) && SlateElement.isElement(n) && n.type
|
|
64
|
+
),
|
|
65
|
+
split: true,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
Transforms.setNodes(editor, {
|
|
69
|
+
type: isActive ? "p" : isList ? "list-item" : format,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (isList && !isActive) {
|
|
73
|
+
Transforms.wrapNodes(editor, {
|
|
74
|
+
type: format,
|
|
75
|
+
children: [],
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
export const addMarkData = (editor, data) => {
|
|
80
|
+
Editor.addMark(editor, data.format, data.value);
|
|
81
|
+
};
|
|
82
|
+
export const toggleMark = (editor, format) => {
|
|
83
|
+
const isActive = isMarkActive(editor, format);
|
|
84
|
+
|
|
85
|
+
if (isActive) {
|
|
86
|
+
Editor.removeMark(editor, format);
|
|
87
|
+
} else {
|
|
88
|
+
Editor.addMark(editor, format, true);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
export const isMarkActive = (editor, format) => {
|
|
92
|
+
const marks = Editor.marks(editor);
|
|
93
|
+
|
|
94
|
+
return marks ? marks[format] === true : false;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const isBlockActive = (editor, format) => {
|
|
98
|
+
const [match] = Editor.nodes(editor, {
|
|
99
|
+
match: (n) =>
|
|
100
|
+
!Editor.isEditor(n) && SlateElement.isElement(n) && n.type === format,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return !!match;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const activeMark = (editor, format) => {
|
|
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 "h1":
|
|
163
|
+
return (
|
|
164
|
+
<h1 {...attributes} {...element.attr}>
|
|
165
|
+
{children}
|
|
166
|
+
</h1>
|
|
167
|
+
);
|
|
168
|
+
case "h2":
|
|
169
|
+
return (
|
|
170
|
+
<h2 {...attributes} {...element.attr}>
|
|
171
|
+
{children}
|
|
172
|
+
</h2>
|
|
173
|
+
);
|
|
174
|
+
case "h3":
|
|
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
|
+
default:
|
|
261
|
+
return (
|
|
262
|
+
<div {...element.attr} {...attributes}>
|
|
263
|
+
{children}
|
|
264
|
+
</div>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Editor, Transforms, Path, Range, Element } from "slate"
|
|
2
|
+
|
|
3
|
+
export const createLinkNode = (href, showInNewTab, text) => ({
|
|
4
|
+
type: "link",
|
|
5
|
+
href,
|
|
6
|
+
target: showInNewTab ? "_blank" : "_self",
|
|
7
|
+
children: [{ text }],
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
export const insertLink = (editor, { url, showInNewTab }) => {
|
|
11
|
+
if (!url) return
|
|
12
|
+
|
|
13
|
+
const { selection } = editor
|
|
14
|
+
const link = createLinkNode(url, showInNewTab, "Link")
|
|
15
|
+
if (!!selection) {
|
|
16
|
+
const [parent, parentPath] = Editor.parent(editor, selection.focus.path)
|
|
17
|
+
if (parent.type === "link") {
|
|
18
|
+
removeLink(editor)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (editor.isVoid(parent)) {
|
|
22
|
+
Transforms.insertNodes(
|
|
23
|
+
editor,
|
|
24
|
+
{ type: "p", children: [link] },
|
|
25
|
+
{
|
|
26
|
+
at: Path.next(parentPath),
|
|
27
|
+
select: true,
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
} else if (Range.isCollapsed(selection)) {
|
|
31
|
+
Transforms.insertNodes(editor, link, { select: true })
|
|
32
|
+
} else {
|
|
33
|
+
Transforms.wrapNodes(editor, link, { split: true })
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
Transforms.insertNodes(editor, { type: "p", children: [link] })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const removeLink = (editor) => {
|
|
41
|
+
Transforms.unwrapNodes(editor, {
|
|
42
|
+
match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.type === "link",
|
|
43
|
+
})
|
|
44
|
+
}
|