@gcforms/editor 1.0.0 → 1.0.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/CHANGELOG.md +13 -1
- package/package.json +1 -1
- package/src/Editor.tsx +8 -11
- package/src/icons/DraggableIcon.tsx +15 -0
- package/src/icons/PlusIcon.tsx +15 -0
- package/src/plugins/DraggableBlockPlugin/index.tsx +74 -0
- package/src/plugins/DraggableBlockPlugin/styles.css +29 -0
- package/src/plugins/MaxLengthPlugin/index.tsx +24 -7
- package/src/plugins/MaxLengthPlugin/styles.css +7 -0
- package/src/plugins/TreeViewPlugin/index.tsx +12 -9
- package/src/plugins/TreeViewPlugin/styles.css +8 -0
- package/src/styles.css +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [1.0.
|
|
8
|
+
## [1.0.2] - 2025-04-17
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- Add Draggable Block plugin
|
|
13
|
+
|
|
14
|
+
## [1.0.1] - 2025-04-17
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- Only show contentLength/maxLength indicator when contentLength reaches 80% of maxLength
|
|
19
|
+
|
|
20
|
+
## [1.0.0] - 2025-04-17
|
|
9
21
|
|
|
10
22
|
### Changed
|
|
11
23
|
|
package/package.json
CHANGED
package/src/Editor.tsx
CHANGED
|
@@ -28,6 +28,7 @@ import "./styles.css";
|
|
|
28
28
|
import { ToolbarContext } from "./context/ToolbarContext";
|
|
29
29
|
import ShortcutsPlugin from "./plugins/ShortcutsPlugin";
|
|
30
30
|
import { MaxLengthPlugin } from "./plugins/MaxLengthPlugin";
|
|
31
|
+
import DraggableBlockPlugin from "./plugins/DraggableBlockPlugin";
|
|
31
32
|
|
|
32
33
|
interface EditorProps {
|
|
33
34
|
id?: string;
|
|
@@ -39,6 +40,7 @@ interface EditorProps {
|
|
|
39
40
|
contentLocale?: string;
|
|
40
41
|
className?: string;
|
|
41
42
|
maxLength?: number;
|
|
43
|
+
enableDraggableBlocks?: boolean;
|
|
42
44
|
|
|
43
45
|
onChange?(...args: unknown[]): unknown;
|
|
44
46
|
}
|
|
@@ -54,12 +56,12 @@ export const Editor = ({
|
|
|
54
56
|
contentLocale = "en",
|
|
55
57
|
className,
|
|
56
58
|
maxLength,
|
|
59
|
+
enableDraggableBlocks = false,
|
|
57
60
|
}: EditorProps) => {
|
|
58
61
|
contentLocale = contentLocale || locale;
|
|
59
62
|
|
|
60
63
|
const [floatingAnchorElem, setFloatingAnchorElem] = useState<HTMLDivElement | null>(null);
|
|
61
64
|
const [isLinkEditMode, setIsLinkEditMode] = useState<boolean>(false);
|
|
62
|
-
const [contentLength, setContentLength] = useState<number>(0);
|
|
63
65
|
|
|
64
66
|
const randomId = useId();
|
|
65
67
|
const editorId = id || `editor-${randomId}`;
|
|
@@ -116,9 +118,7 @@ export const Editor = ({
|
|
|
116
118
|
<LinkPlugin />
|
|
117
119
|
<TabControlPlugin />
|
|
118
120
|
<ListMaxIndentLevelPlugin maxDepth={5} />
|
|
119
|
-
|
|
120
|
-
<MaxLengthPlugin maxLength={maxLength} setContentLength={setContentLength} />
|
|
121
|
-
)}
|
|
121
|
+
|
|
122
122
|
{floatingAnchorElem && (
|
|
123
123
|
<>
|
|
124
124
|
<FloatingLinkEditorPlugin
|
|
@@ -126,16 +126,13 @@ export const Editor = ({
|
|
|
126
126
|
isLinkEditMode={isLinkEditMode}
|
|
127
127
|
setIsLinkEditMode={setIsLinkEditMode}
|
|
128
128
|
/>
|
|
129
|
+
{enableDraggableBlocks && <DraggableBlockPlugin anchorElem={floatingAnchorElem} />}
|
|
129
130
|
</>
|
|
130
131
|
)}
|
|
131
132
|
{maxLength && (
|
|
132
|
-
|
|
133
|
-
{maxLength
|
|
134
|
-
|
|
135
|
-
{contentLength}/{maxLength}
|
|
136
|
-
</>
|
|
137
|
-
)}
|
|
138
|
-
</div>
|
|
133
|
+
<>
|
|
134
|
+
<MaxLengthPlugin maxLength={maxLength} />
|
|
135
|
+
</>
|
|
139
136
|
)}
|
|
140
137
|
</div>
|
|
141
138
|
</LexicalComposer>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const DraggableIcon = ({ className, title }: { className?: string; title?: string }) => (
|
|
2
|
+
<svg
|
|
3
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
4
|
+
height="24px"
|
|
5
|
+
width="24px"
|
|
6
|
+
viewBox="0 -960 960 960"
|
|
7
|
+
className={className}
|
|
8
|
+
focusable="false"
|
|
9
|
+
aria-hidden={title ? false : true}
|
|
10
|
+
role={title ? "img" : "presentation"}
|
|
11
|
+
>
|
|
12
|
+
{title && <title>{title}</title>}
|
|
13
|
+
<path d="M360-160q-33 0-56.5-23.5T280-240q0-33 23.5-56.5T360-320q33 0 56.5 23.5T440-240q0 33-23.5 56.5T360-160Zm240 0q-33 0-56.5-23.5T520-240q0-33 23.5-56.5T600-320q33 0 56.5 23.5T680-240q0 33-23.5 56.5T600-160ZM360-400q-33 0-56.5-23.5T280-480q0-33 23.5-56.5T360-560q33 0 56.5 23.5T440-480q0 33-23.5 56.5T360-400Zm240 0q-33 0-56.5-23.5T520-480q0-33 23.5-56.5T600-560q33 0 56.5 23.5T680-480q0 33-23.5 56.5T600-400ZM360-640q-33 0-56.5-23.5T280-720q0-33 23.5-56.5T360-800q33 0 56.5 23.5T440-720q0 33-23.5 56.5T360-640Zm240 0q-33 0-56.5-23.5T520-720q0-33 23.5-56.5T600-800q33 0 56.5 23.5T680-720q0 33-23.5 56.5T600-640Z" />
|
|
14
|
+
</svg>
|
|
15
|
+
);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const PlusIcon = ({ className, title }: { className?: string; title?: string }) => (
|
|
2
|
+
<svg
|
|
3
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
4
|
+
height="24px"
|
|
5
|
+
width="24px"
|
|
6
|
+
viewBox="0 -960 960 960"
|
|
7
|
+
className={className}
|
|
8
|
+
focusable="false"
|
|
9
|
+
aria-hidden={title ? false : true}
|
|
10
|
+
role={title ? "img" : "presentation"}
|
|
11
|
+
>
|
|
12
|
+
{title && <title>{title}</title>}
|
|
13
|
+
<path d="M440-440H200v-80h240v-240h80v240h240v80H520v240h-80v-240Z" />
|
|
14
|
+
</svg>
|
|
15
|
+
);
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
import type { JSX } from "react";
|
|
9
|
+
|
|
10
|
+
import "./styles.css";
|
|
11
|
+
|
|
12
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
13
|
+
import { DraggableBlockPlugin_EXPERIMENTAL } from "@lexical/react/LexicalDraggableBlockPlugin";
|
|
14
|
+
import { $createParagraphNode, $getNearestNodeFromDOMNode } from "lexical";
|
|
15
|
+
import { useRef, useState } from "react";
|
|
16
|
+
import { PlusIcon } from "../../icons/PlusIcon";
|
|
17
|
+
import { DraggableIcon } from "../../icons/DraggableIcon";
|
|
18
|
+
|
|
19
|
+
const DRAGGABLE_BLOCK_MENU_CLASSNAME = "draggable-block-menu";
|
|
20
|
+
|
|
21
|
+
function isOnMenu(element: HTMLElement): boolean {
|
|
22
|
+
return !!element.closest(`.${DRAGGABLE_BLOCK_MENU_CLASSNAME}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default function DraggableBlockPlugin({
|
|
26
|
+
anchorElem = document.body,
|
|
27
|
+
}: {
|
|
28
|
+
anchorElem?: HTMLElement;
|
|
29
|
+
}): JSX.Element {
|
|
30
|
+
const [editor] = useLexicalComposerContext();
|
|
31
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
32
|
+
const targetLineRef = useRef<HTMLDivElement>(null);
|
|
33
|
+
const [draggableElement, setDraggableElement] = useState<HTMLElement | null>(null);
|
|
34
|
+
|
|
35
|
+
function insertBlock(e: React.MouseEvent) {
|
|
36
|
+
if (!draggableElement || !editor) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
editor.update(() => {
|
|
41
|
+
const node = $getNearestNodeFromDOMNode(draggableElement);
|
|
42
|
+
if (!node) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const pNode = $createParagraphNode();
|
|
47
|
+
if (e.altKey || e.ctrlKey) {
|
|
48
|
+
node.insertBefore(pNode);
|
|
49
|
+
} else {
|
|
50
|
+
node.insertAfter(pNode);
|
|
51
|
+
}
|
|
52
|
+
pNode.select();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<DraggableBlockPlugin_EXPERIMENTAL
|
|
58
|
+
anchorElem={anchorElem}
|
|
59
|
+
menuRef={menuRef as React.RefObject<HTMLElement>}
|
|
60
|
+
targetLineRef={targetLineRef as React.RefObject<HTMLElement>}
|
|
61
|
+
menuComponent={
|
|
62
|
+
<div ref={menuRef} className="draggable-block-menu">
|
|
63
|
+
<button title="Click to add below" className="" onClick={insertBlock}>
|
|
64
|
+
<PlusIcon />
|
|
65
|
+
</button>
|
|
66
|
+
<DraggableIcon />
|
|
67
|
+
</div>
|
|
68
|
+
}
|
|
69
|
+
targetLineComponent={<div ref={targetLineRef} className="draggable-block-target-line" />}
|
|
70
|
+
isOnMenu={isOnMenu}
|
|
71
|
+
onElementChanged={setDraggableElement}
|
|
72
|
+
/>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
.draggable-block-menu {
|
|
2
|
+
border-radius: 4px;
|
|
3
|
+
padding: 2px 0px;
|
|
4
|
+
cursor: grab;
|
|
5
|
+
opacity: 0;
|
|
6
|
+
position: absolute;
|
|
7
|
+
left: -30px;
|
|
8
|
+
top: 0;
|
|
9
|
+
will-change: transform;
|
|
10
|
+
display: flex;
|
|
11
|
+
}
|
|
12
|
+
.draggable-block-menu:active {
|
|
13
|
+
cursor: grabbing;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.draggable-block-menu .icon:hover {
|
|
17
|
+
background-color: #efefef;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.draggable-block-target-line {
|
|
21
|
+
pointer-events: none;
|
|
22
|
+
background: deepskyblue;
|
|
23
|
+
height: 4px;
|
|
24
|
+
position: absolute;
|
|
25
|
+
left: 0;
|
|
26
|
+
top: 0;
|
|
27
|
+
opacity: 0;
|
|
28
|
+
will-change: transform;
|
|
29
|
+
}
|
|
@@ -10,16 +10,33 @@ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext
|
|
|
10
10
|
import { $trimTextContentFromAnchor } from "@lexical/selection";
|
|
11
11
|
import { $restoreEditorState } from "@lexical/utils";
|
|
12
12
|
import { $getSelection, $isRangeSelection, EditorState, RootNode } from "lexical";
|
|
13
|
-
import { useEffect } from "react";
|
|
13
|
+
import { JSX, useEffect, useState } from "react";
|
|
14
|
+
import "./styles.css";
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
const MaxLengthIndicator = ({
|
|
16
17
|
maxLength,
|
|
17
|
-
|
|
18
|
+
contentLength,
|
|
18
19
|
}: {
|
|
19
|
-
maxLength
|
|
20
|
-
|
|
21
|
-
})
|
|
20
|
+
maxLength?: number;
|
|
21
|
+
contentLength: number;
|
|
22
|
+
}) => {
|
|
23
|
+
if (!maxLength) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
// Only show if contentLength reaches 80% of maxLength
|
|
29
|
+
contentLength >= 0.8 * maxLength && (
|
|
30
|
+
<div className="gc-editor-max-length">
|
|
31
|
+
{contentLength >= maxLength ? maxLength : contentLength}/{maxLength}
|
|
32
|
+
</div>
|
|
33
|
+
)
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function MaxLengthPlugin({ maxLength }: { maxLength: number }): JSX.Element {
|
|
22
38
|
const [editor] = useLexicalComposerContext();
|
|
39
|
+
const [contentLength, setContentLength] = useState(0);
|
|
23
40
|
|
|
24
41
|
useEffect(() => {
|
|
25
42
|
let lastRestoredEditorState: EditorState | null = null;
|
|
@@ -52,5 +69,5 @@ export function MaxLengthPlugin({
|
|
|
52
69
|
});
|
|
53
70
|
}, [editor, maxLength, setContentLength]);
|
|
54
71
|
|
|
55
|
-
return
|
|
72
|
+
return <MaxLengthIndicator contentLength={contentLength} maxLength={maxLength} />;
|
|
56
73
|
}
|
|
@@ -11,18 +11,21 @@ import { TreeView } from "@lexical/react/LexicalTreeView";
|
|
|
11
11
|
import * as React from "react";
|
|
12
12
|
|
|
13
13
|
import type { JSX } from "react";
|
|
14
|
+
import "./styles.css";
|
|
14
15
|
|
|
15
16
|
export default function TreeViewPlugin(): JSX.Element {
|
|
16
17
|
const [editor] = useLexicalComposerContext();
|
|
17
18
|
return (
|
|
18
|
-
<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
<div className="gc-editor-treeview">
|
|
20
|
+
<TreeView
|
|
21
|
+
viewClassName="tree-view-output"
|
|
22
|
+
timeTravelPanelClassName="debug-timetravel-panel"
|
|
23
|
+
timeTravelButtonClassName="debug-timetravel-button"
|
|
24
|
+
timeTravelPanelSliderClassName="debug-timetravel-panel-slider"
|
|
25
|
+
timeTravelPanelButtonClassName="debug-timetravel-panel-button"
|
|
26
|
+
treeTypeButtonClassName=""
|
|
27
|
+
editor={editor}
|
|
28
|
+
/>
|
|
29
|
+
</div>
|
|
27
30
|
);
|
|
28
31
|
}
|