@gcforms/editor 1.0.1 → 1.0.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/CHANGELOG.md CHANGED
@@ -5,6 +5,18 @@ 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.3] - 2025-05-13
9
+
10
+ ### Changed
11
+
12
+ - Bumped Lexical version to 0.31.1
13
+
14
+ ## [1.0.2] - 2025-04-17
15
+
16
+ ### Changed
17
+
18
+ - Add Draggable Block plugin
19
+
8
20
  ## [1.0.1] - 2025-04-17
9
21
 
10
22
  ### Changed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gcforms/editor",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "author": "Canadian Digital Service",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -8,8 +8,8 @@
8
8
  },
9
9
  "packageManager": "yarn@4.6.0",
10
10
  "dependencies": {
11
- "@lexical/react": "^0.30.0",
12
- "lexical": "^0.30.0"
11
+ "@lexical/react": "^0.31.1",
12
+ "lexical": "^0.31.1"
13
13
  },
14
14
  "peerDependencies": {
15
15
  "react": "^19.1.0",
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,6 +56,7 @@ 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
 
@@ -123,6 +126,7 @@ export const Editor = ({
123
126
  isLinkEditMode={isLinkEditMode}
124
127
  setIsLinkEditMode={setIsLinkEditMode}
125
128
  />
129
+ {enableDraggableBlocks && <DraggableBlockPlugin anchorElem={floatingAnchorElem} />}
126
130
  </>
127
131
  )}
128
132
  {maxLength && (
@@ -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
+ }
@@ -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
- <TreeView
19
- viewClassName="tree-view-output"
20
- timeTravelPanelClassName="debug-timetravel-panel"
21
- timeTravelButtonClassName="debug-timetravel-button"
22
- timeTravelPanelSliderClassName="debug-timetravel-panel-slider"
23
- timeTravelPanelButtonClassName="debug-timetravel-panel-button"
24
- treeTypeButtonClassName=""
25
- editor={editor}
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
  }
@@ -0,0 +1,8 @@
1
+ .gc-editor-treeview {
2
+ padding: 10px;
3
+ color: white;
4
+ font-size: 14px;
5
+ font-family: "Inter", sans-serif;
6
+ background-color: black;
7
+ outline: 1px solid black;
8
+ }