@moldable-ai/editor 0.1.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.
Files changed (36) hide show
  1. package/LICENSE +101 -0
  2. package/dist/components/floating-toolbar.d.ts +2 -0
  3. package/dist/components/floating-toolbar.d.ts.map +1 -0
  4. package/dist/components/floating-toolbar.js +109 -0
  5. package/dist/components/markdown-editor.d.ts +15 -0
  6. package/dist/components/markdown-editor.d.ts.map +1 -0
  7. package/dist/components/markdown-editor.js +80 -0
  8. package/dist/index.d.ts +10 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +12 -0
  11. package/dist/lib/lexical/auto-link-config.d.ts +21 -0
  12. package/dist/lib/lexical/auto-link-config.d.ts.map +1 -0
  13. package/dist/lib/lexical/auto-link-config.js +23 -0
  14. package/dist/lib/lexical/clickable-link-plugin.d.ts +11 -0
  15. package/dist/lib/lexical/clickable-link-plugin.d.ts.map +1 -0
  16. package/dist/lib/lexical/clickable-link-plugin.js +97 -0
  17. package/dist/lib/lexical/editor-theme.d.ts +38 -0
  18. package/dist/lib/lexical/editor-theme.d.ts.map +1 -0
  19. package/dist/lib/lexical/editor-theme.js +37 -0
  20. package/dist/lib/lexical/floating-toolbar-plugin.d.ts +30 -0
  21. package/dist/lib/lexical/floating-toolbar-plugin.d.ts.map +1 -0
  22. package/dist/lib/lexical/floating-toolbar-plugin.js +237 -0
  23. package/dist/lib/lexical/headless-editor.d.ts +6 -0
  24. package/dist/lib/lexical/headless-editor.d.ts.map +1 -0
  25. package/dist/lib/lexical/headless-editor.js +30 -0
  26. package/dist/lib/lexical/markdown-transformers.d.ts +40 -0
  27. package/dist/lib/lexical/markdown-transformers.d.ts.map +1 -0
  28. package/dist/lib/lexical/markdown-transformers.js +52 -0
  29. package/dist/lib/lexical/split-node-at-selection.d.ts +9 -0
  30. package/dist/lib/lexical/split-node-at-selection.d.ts.map +1 -0
  31. package/dist/lib/lexical/split-node-at-selection.js +224 -0
  32. package/dist/lib/lexical/sync-plugin.d.ts +11 -0
  33. package/dist/lib/lexical/sync-plugin.d.ts.map +1 -0
  34. package/dist/lib/lexical/sync-plugin.js +45 -0
  35. package/package.json +81 -0
  36. package/src/styles/index.css +71 -0
package/LICENSE ADDED
@@ -0,0 +1,101 @@
1
+ Copyright (c) 2026-present Desiderata LLC
2
+
3
+ Portions of this software are licensed as follows:
4
+
5
+ - All third party components incorporated into the Moldable Software are licensed
6
+ under the original license provided by the owner of the applicable component.
7
+ - All other content is available under the "Elastic License 2.0" license as defined below.
8
+
9
+ Elastic License 2.0
10
+
11
+ URL: https://www.elastic.co/licensing/elastic-license
12
+
13
+ ## Acceptance
14
+
15
+ By using the software, you agree to all of the terms and conditions below.
16
+
17
+ ## Copyright License
18
+
19
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
20
+ non-sublicensable, non-transferable license to use, copy, distribute, make
21
+ available, and prepare derivative works of the software, in each case subject to
22
+ the limitations and conditions below.
23
+
24
+ ## Limitations
25
+
26
+ You may not provide the software to third parties as a hosted or managed
27
+ service, where the service provides users with access to any substantial set of
28
+ the features or functionality of the software.
29
+
30
+ You may not move, change, disable, or circumvent the license key functionality
31
+ in the software, and you may not remove or obscure any functionality in the
32
+ software that is protected by the license key.
33
+
34
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
35
+ of the licensor in the software. Any use of the licensor's trademarks is subject
36
+ to applicable law.
37
+
38
+ ## Patents
39
+
40
+ The licensor grants you a license, under any patent claims the licensor can
41
+ license, or becomes able to license, to make, have made, use, sell, offer for
42
+ sale, import and have imported the software, in each case subject to the
43
+ limitations and conditions in this license. This license does not cover any
44
+ patent claims that you cause to be infringed by modifications or additions to
45
+ the software. If you or your company make any written claim that the software
46
+ infringes or contributes to infringement of any patent, your patent license for
47
+ the software granted under these terms ends immediately. If your company makes
48
+ such a claim, your patent license ends immediately for work on behalf of your
49
+ company.
50
+
51
+ ## Notices
52
+
53
+ You must ensure that anyone who gets a copy of any part of the software from you
54
+ also gets a copy of these terms.
55
+
56
+ If you modify the software, you must include in any modified copies of the
57
+ software prominent notices stating that you have modified the software.
58
+
59
+ ## No Other Rights
60
+
61
+ These terms do not imply any licenses other than those expressly granted in
62
+ these terms.
63
+
64
+ ## Termination
65
+
66
+ If you use the software in violation of these terms, such use is not licensed,
67
+ and your licenses will automatically terminate. If the licensor provides you
68
+ with a notice of your violation, and you cease all violation of this license no
69
+ later than 30 days after you receive that notice, your licenses will be
70
+ reinstated retroactively. However, if you violate these terms after such
71
+ reinstatement, any additional violation of these terms will cause your licenses
72
+ to terminate automatically and permanently.
73
+
74
+ ## No Liability
75
+
76
+ _As far as the law allows, the software comes as is, without any warranty or
77
+ condition, and the licensor will not be liable to you for any damages arising
78
+ out of these terms or the use or nature of the software, under any kind of
79
+ legal claim._
80
+
81
+ ## Definitions
82
+
83
+ The **licensor** is the entity offering these terms, and the **software** is the
84
+ software the licensor makes available under these terms, including any portion
85
+ of it.
86
+
87
+ **you** refers to the individual or entity agreeing to these terms.
88
+
89
+ **your company** is any legal entity, sole proprietorship, or other kind of
90
+ organization that you work for, plus all organizations that have control over,
91
+ are under the control of, or are under common control with that
92
+ organization. **control** means ownership of substantially all the assets of an
93
+ entity, or the power to direct its management and policies by vote, contract, or
94
+ otherwise. Control can be direct or indirect.
95
+
96
+ **your licenses** are all the licenses granted to you for the software under
97
+ these terms.
98
+
99
+ **use** means anything you do with the software requiring one of your licenses.
100
+
101
+ **trademark** means trademarks, service marks, and similar rights.
@@ -0,0 +1,2 @@
1
+ export declare function FloatingToolbar(): import("react/jsx-runtime").JSX.Element | null;
2
+ //# sourceMappingURL=floating-toolbar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"floating-toolbar.d.ts","sourceRoot":"","sources":["../../src/components/floating-toolbar.tsx"],"names":[],"mappings":"AAsCA,wBAAgB,eAAe,mDAQ9B"}
@@ -0,0 +1,109 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Bold, ChevronDown, Code, Eraser, Heading1, Heading2, Heading3, Italic, List, ListChecks, ListOrdered, Quote, Strikethrough, Type, Underline, } from 'lucide-react';
4
+ import React, { useEffect, useRef, useState } from 'react';
5
+ import { createPortal } from 'react-dom';
6
+ import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, Tooltip, TooltipContent, TooltipTrigger, cn, } from '@moldable-ai/ui';
7
+ import { useFloatingToolbar, } from '../lib/lexical/floating-toolbar-plugin';
8
+ export function FloatingToolbar() {
9
+ const [anchorElem] = useState(() => typeof document !== 'undefined' ? document.body : null);
10
+ if (!anchorElem)
11
+ return null;
12
+ return _jsx(FloatingToolbarPortal, { anchorElem: anchorElem });
13
+ }
14
+ function FloatingToolbarPortal({ anchorElem }) {
15
+ const state = useFloatingToolbar();
16
+ const toolbarRef = useRef(null);
17
+ useEffect(() => {
18
+ const toolbar = toolbarRef.current;
19
+ if (!toolbar || !state.position) {
20
+ return;
21
+ }
22
+ toolbar.style.opacity = '1';
23
+ toolbar.style.transform = `translate(${state.position.left}px, ${state.position.top}px)`;
24
+ }, [state.position]);
25
+ // Handle keyboard shortcuts (undo/redo) when toolbar or its children have focus
26
+ useEffect(() => {
27
+ if (!state.isVisible)
28
+ return;
29
+ const toolbar = toolbarRef.current;
30
+ if (!toolbar)
31
+ return;
32
+ const handleKeyDown = (event) => {
33
+ // Check if the active element is within the toolbar
34
+ const activeElement = document.activeElement;
35
+ if (!activeElement || !toolbar.contains(activeElement)) {
36
+ return;
37
+ }
38
+ const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
39
+ const isUndo = (isMac ? event.metaKey : event.ctrlKey) &&
40
+ event.key === 'z' &&
41
+ !event.shiftKey;
42
+ const isRedo = (isMac ? event.metaKey : event.ctrlKey) &&
43
+ ((event.key === 'z' && event.shiftKey) || event.key === 'y');
44
+ if (isUndo || isRedo) {
45
+ event.preventDefault();
46
+ event.stopPropagation();
47
+ // Focus the editor root element so keyboard shortcuts work
48
+ const rootElement = state.editor.getRootElement();
49
+ if (rootElement) {
50
+ rootElement.focus();
51
+ // Dispatch the keyboard event to the editor
52
+ // The HistoryPlugin will handle it automatically
53
+ // We preserve all modifier keys and key properties
54
+ const keyboardEvent = new KeyboardEvent('keydown', {
55
+ key: event.key,
56
+ code: event.code,
57
+ metaKey: event.metaKey,
58
+ ctrlKey: event.ctrlKey,
59
+ shiftKey: event.shiftKey,
60
+ altKey: event.altKey,
61
+ bubbles: true,
62
+ cancelable: true,
63
+ });
64
+ // Use setTimeout to ensure focus has settled before dispatching
65
+ setTimeout(() => {
66
+ rootElement.dispatchEvent(keyboardEvent);
67
+ }, 0);
68
+ }
69
+ }
70
+ };
71
+ // Use capture phase to catch the event before it bubbles
72
+ window.addEventListener('keydown', handleKeyDown, true);
73
+ return () => {
74
+ window.removeEventListener('keydown', handleKeyDown, true);
75
+ };
76
+ }, [state.isVisible, state.editor]);
77
+ if (!state.isVisible) {
78
+ return null;
79
+ }
80
+ return createPortal(_jsx(FloatingToolbarUI, { state: state, ref: toolbarRef }), anchorElem);
81
+ }
82
+ const FloatingToolbarUI = React.forwardRef(({ state }, ref) => {
83
+ const { isBold, isItalic, isUnderline, isStrikethrough, isCode, blockType, onBold, onItalic, onUnderline, onStrikethrough, onCode, onParagraph, onHeading, onBulletList, onNumberedList, onCheckList, onQuote, onCodeBlock, onClearFormatting, } = state;
84
+ const getBlockTypeIcon = () => {
85
+ switch (blockType) {
86
+ case 'h1':
87
+ return _jsx(Heading1, { className: "size-4" });
88
+ case 'h2':
89
+ return _jsx(Heading2, { className: "size-4" });
90
+ case 'h3':
91
+ return _jsx(Heading3, { className: "size-4" });
92
+ case 'bullet':
93
+ return _jsx(List, { className: "size-4" });
94
+ case 'number':
95
+ return _jsx(ListOrdered, { className: "size-4" });
96
+ case 'check':
97
+ return _jsx(ListChecks, { className: "size-4" });
98
+ case 'quote':
99
+ return _jsx(Quote, { className: "size-4" });
100
+ case 'code':
101
+ return _jsx(Code, { className: "size-4" });
102
+ default:
103
+ return _jsx(Type, { className: "size-4" });
104
+ }
105
+ };
106
+ return (_jsxs("div", { ref: ref, className: "bg-popover fixed left-0 top-0 z-50 flex items-center gap-0.5 rounded-lg border p-1 opacity-0 shadow-md transition-opacity", style: { transform: 'translate(0, 12px)' }, children: [_jsx(Tooltip, { children: _jsxs(DropdownMenu, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(Button, { variant: "ghost", size: "sm", className: "h-7 gap-1 px-2", children: [getBlockTypeIcon(), _jsx(ChevronDown, { className: "size-3" })] }) }) }), _jsx(TooltipContent, { children: _jsx("p", { children: "Text styles" }) }), _jsxs(DropdownMenuContent, { align: "start", children: [_jsxs(DropdownMenuItem, { onClick: onParagraph, className: cn(blockType === 'paragraph' && 'relative'), children: [blockType === 'paragraph' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Type, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Text" })] }), _jsxs(DropdownMenuItem, { onClick: () => onHeading('h1'), className: cn(blockType === 'h1' && 'relative'), children: [blockType === 'h1' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Heading1, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Heading 1" })] }), _jsxs(DropdownMenuItem, { onClick: () => onHeading('h2'), className: cn(blockType === 'h2' && 'relative'), children: [blockType === 'h2' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Heading2, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Heading 2" })] }), _jsxs(DropdownMenuItem, { onClick: () => onHeading('h3'), className: cn(blockType === 'h3' && 'relative'), children: [blockType === 'h3' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Heading3, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Heading 3" })] }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: onBulletList, className: cn(blockType === 'bullet' && 'relative'), children: [blockType === 'bullet' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(List, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Bullet list" })] }), _jsxs(DropdownMenuItem, { onClick: onNumberedList, className: cn(blockType === 'number' && 'relative'), children: [blockType === 'number' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(ListOrdered, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Numbered list" })] }), _jsxs(DropdownMenuItem, { onClick: onCheckList, className: cn(blockType === 'check' && 'relative'), children: [blockType === 'check' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(ListChecks, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Checklist" })] }), _jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: onQuote, className: cn(blockType === 'quote' && 'relative'), children: [blockType === 'quote' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Quote, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Block quote" })] }), _jsxs(DropdownMenuItem, { onClick: onCodeBlock, className: cn(blockType === 'code' && 'relative'), children: [blockType === 'code' && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Code, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Code block" })] })] })] }) }), _jsx("div", { className: "bg-border mx-0.5 h-5 w-px" }), _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { variant: "ghost", size: "sm", className: cn('size-7 p-0', isBold && 'bg-accent text-accent-foreground'), onClick: onBold, children: _jsx(Bold, { className: "size-4" }) }) }), _jsx(TooltipContent, { children: _jsx("p", { children: "Bold" }) })] }), _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { variant: "ghost", size: "sm", className: cn('size-7 p-0', isItalic && 'bg-accent text-accent-foreground'), onClick: onItalic, children: _jsx(Italic, { className: "size-4" }) }) }), _jsx(TooltipContent, { children: _jsx("p", { children: "Italic" }) })] }), _jsx(Tooltip, { children: _jsxs(DropdownMenu, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(Button, { variant: "ghost", size: "sm", className: cn('h-7 gap-1 px-2', (isUnderline || isStrikethrough || isCode) &&
107
+ 'bg-accent text-accent-foreground'), children: [_jsx(Underline, { className: "size-4" }), _jsx(ChevronDown, { className: "size-3" })] }) }) }), _jsx(TooltipContent, { children: _jsx("p", { children: "More formatting" }) }), _jsxs(DropdownMenuContent, { align: "start", children: [_jsxs(DropdownMenuItem, { onClick: onUnderline, className: cn(isUnderline && 'relative'), children: [isUnderline && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Underline, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Underline" })] }), _jsxs(DropdownMenuItem, { onClick: onStrikethrough, className: cn(isStrikethrough && 'relative'), children: [isStrikethrough && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Strikethrough, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Strikethrough" })] }), _jsxs(DropdownMenuItem, { onClick: onCode, className: cn(isCode && 'relative'), children: [isCode && (_jsx("div", { className: "bg-primary absolute -left-1 top-1/2 h-2 w-[3px] -translate-y-1/2 rounded-r-sm" })), _jsx(Code, { className: "size-4" }), _jsx("span", { className: "text-muted-foreground ml-2", children: "Inline code" })] })] })] }) }), _jsx("div", { className: "bg-border mx-0.5 h-5 w-px" }), _jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { asChild: true, children: _jsx(Button, { variant: "ghost", size: "sm", className: "h-7 gap-1 px-2", onClick: onClearFormatting, children: _jsx(Eraser, { className: "size-4" }) }) }), _jsx(TooltipContent, { children: _jsx("p", { children: "Clear formatting" }) })] })] }));
108
+ });
109
+ FloatingToolbarUI.displayName = 'FloatingToolbarUI';
@@ -0,0 +1,15 @@
1
+ interface MarkdownEditorProps {
2
+ value: string;
3
+ onChange: (value: string) => void;
4
+ placeholder?: string;
5
+ disabled?: boolean;
6
+ autoFocus?: boolean;
7
+ className?: string;
8
+ contentClassName?: string;
9
+ minHeight?: string;
10
+ maxHeight?: string;
11
+ hideMarkdownHint?: boolean;
12
+ }
13
+ export declare function MarkdownEditor({ value, onChange, placeholder, disabled, autoFocus, className, contentClassName, minHeight, maxHeight, hideMarkdownHint, }: MarkdownEditorProps): import("react/jsx-runtime").JSX.Element;
14
+ export {};
15
+ //# sourceMappingURL=markdown-editor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"markdown-editor.d.ts","sourceRoot":"","sources":["../../src/components/markdown-editor.tsx"],"names":[],"mappings":"AAsCA,UAAU,mBAAmB;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IACjC,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B;AAED,wBAAgB,cAAc,CAAC,EAC7B,KAAK,EACL,QAAQ,EACR,WAAkC,EAClC,QAAgB,EAChB,SAAiB,EACjB,SAAS,EACT,gBAAgB,EAChB,SAAmB,EACnB,SAAkB,EAClB,gBAAwB,GACzB,EAAE,mBAAmB,2CAsGrB"}
@@ -0,0 +1,80 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin';
4
+ import { AutoLinkPlugin } from '@lexical/react/LexicalAutoLinkPlugin';
5
+ import { CheckListPlugin } from '@lexical/react/LexicalCheckListPlugin';
6
+ import { LexicalComposer } from '@lexical/react/LexicalComposer';
7
+ import { ContentEditable } from '@lexical/react/LexicalContentEditable';
8
+ import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';
9
+ import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
10
+ import { LinkPlugin } from '@lexical/react/LexicalLinkPlugin';
11
+ import { ListPlugin } from '@lexical/react/LexicalListPlugin';
12
+ import { MarkdownShortcutPlugin } from '@lexical/react/LexicalMarkdownShortcutPlugin';
13
+ import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
14
+ import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';
15
+ import { TabIndentationPlugin } from '@lexical/react/LexicalTabIndentationPlugin';
16
+ import { useCallback, useRef } from 'react';
17
+ import { cn } from '@moldable-ai/ui';
18
+ import { LINK_MATCHERS } from '../lib/lexical/auto-link-config';
19
+ import { ClickableLinkPlugin } from '../lib/lexical/clickable-link-plugin';
20
+ import { editorTheme } from '../lib/lexical/editor-theme';
21
+ import { $convertFromMarkdownString, $convertToMarkdownString, markdownTransformers, } from '../lib/lexical/markdown-transformers';
22
+ import { SyncPlugin } from '../lib/lexical/sync-plugin';
23
+ import { FloatingToolbar } from './floating-toolbar';
24
+ import { CodeHighlightNode, CodeNode } from '@lexical/code';
25
+ import { HorizontalRuleNode } from '@lexical/extension';
26
+ import { AutoLinkNode, LinkNode } from '@lexical/link';
27
+ import { ListItemNode, ListNode } from '@lexical/list';
28
+ import { HeadingNode, QuoteNode } from '@lexical/rich-text';
29
+ import { $createParagraphNode, $getRoot } from 'lexical';
30
+ export function MarkdownEditor({ value, onChange, placeholder = 'Write something...', disabled = false, autoFocus = false, className, contentClassName, minHeight = '150px', maxHeight = '50vh', hideMarkdownHint = false, }) {
31
+ const initialValueRef = useRef(value);
32
+ const initialConfig = {
33
+ namespace: 'MarkdownEditor',
34
+ editorState: () => {
35
+ const root = $getRoot();
36
+ if (value) {
37
+ $convertFromMarkdownString({
38
+ markdown: value,
39
+ transformers: markdownTransformers,
40
+ node: root,
41
+ shouldPreserveNewLines: true,
42
+ });
43
+ }
44
+ else {
45
+ root.append($createParagraphNode());
46
+ }
47
+ },
48
+ nodes: [
49
+ HeadingNode,
50
+ QuoteNode,
51
+ ListNode,
52
+ ListItemNode,
53
+ CodeNode,
54
+ CodeHighlightNode,
55
+ AutoLinkNode,
56
+ LinkNode,
57
+ HorizontalRuleNode,
58
+ ],
59
+ onError: (error) => {
60
+ console.error('MarkdownEditor error:', error);
61
+ },
62
+ theme: editorTheme,
63
+ editable: !disabled,
64
+ };
65
+ const handleChange = useCallback((editorState) => {
66
+ editorState.read(() => {
67
+ // Use shouldPreserveNewLines: true to preserve empty paragraphs as extra newlines
68
+ const markdown = $convertToMarkdownString({
69
+ transformers: markdownTransformers,
70
+ shouldPreserveNewLines: true,
71
+ });
72
+ // Only call onChange if the value actually changed
73
+ if (markdown !== initialValueRef.current) {
74
+ initialValueRef.current = markdown;
75
+ onChange(markdown);
76
+ }
77
+ });
78
+ }, [onChange]);
79
+ return (_jsx(LexicalComposer, { initialConfig: initialConfig, children: _jsxs("div", { className: cn('relative h-full', className), "data-lexical-editor-container": true, children: [_jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { className: cn('prose prose-sm caret-primary dark:prose-invert max-w-none resize-none overflow-auto bg-transparent outline-none', disabled && 'cursor-not-allowed opacity-50', contentClassName), style: { minHeight, maxHeight }, "aria-placeholder": placeholder, "data-lexical-editor": "true", placeholder: _jsx("div", { className: "text-muted-foreground pointer-events-none absolute left-0 top-1 text-sm opacity-75", children: placeholder }) }), ErrorBoundary: LexicalErrorBoundary }), _jsx(HistoryPlugin, {}), autoFocus && _jsx(AutoFocusPlugin, {}), _jsx(ListPlugin, {}), _jsx(CheckListPlugin, {}), _jsx(TabIndentationPlugin, {}), _jsx(LinkPlugin, {}), _jsx(AutoLinkPlugin, { matchers: LINK_MATCHERS }), _jsx(ClickableLinkPlugin, {}), _jsx(FloatingToolbar, {}), _jsx(MarkdownShortcutPlugin, { transformers: markdownTransformers }), _jsx(OnChangePlugin, { onChange: handleChange, ignoreSelectionChange: true }), _jsx(SyncPlugin, { value: value, initialValueRef: initialValueRef }), !hideMarkdownHint && (_jsx("div", { className: "mt-1 flex justify-end", children: _jsx("span", { className: "text-muted-foreground/60 text-xs", children: "Markdown supported" }) }))] }) }));
80
+ }
@@ -0,0 +1,10 @@
1
+ export { MarkdownEditor } from './components/markdown-editor';
2
+ export { FloatingToolbar } from './components/floating-toolbar';
3
+ export { $convertFromMarkdownString, $convertToMarkdownString, markdownTransformers, HR, } from './lib/lexical/markdown-transformers';
4
+ export { editorTheme } from './lib/lexical/editor-theme';
5
+ export { LINK_MATCHERS, URL_REGEX, EMAIL_REGEX, } from './lib/lexical/auto-link-config';
6
+ export { useFloatingToolbar, type FloatingToolbarState, } from './lib/lexical/floating-toolbar-plugin';
7
+ export { SyncPlugin } from './lib/lexical/sync-plugin';
8
+ export { ClickableLinkPlugin, isUrlSafe, } from './lib/lexical/clickable-link-plugin';
9
+ export { createMoldableHeadlessEditor } from './lib/lexical/headless-editor';
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAG/D,OAAO,EACL,0BAA0B,EAC1B,wBAAwB,EACxB,oBAAoB,EACpB,EAAE,GACH,MAAM,qCAAqC,CAAA;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EACL,aAAa,EACb,SAAS,EACT,WAAW,GACZ,MAAM,gCAAgC,CAAA;AACvC,OAAO,EACL,kBAAkB,EAClB,KAAK,oBAAoB,GAC1B,MAAM,uCAAuC,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EACL,mBAAmB,EACnB,SAAS,GACV,MAAM,qCAAqC,CAAA;AAG5C,OAAO,EAAE,4BAA4B,EAAE,MAAM,+BAA+B,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // Components
2
+ export { MarkdownEditor } from './components/markdown-editor';
3
+ export { FloatingToolbar } from './components/floating-toolbar';
4
+ // Lexical utilities (for advanced use cases)
5
+ export { $convertFromMarkdownString, $convertToMarkdownString, markdownTransformers, HR, } from './lib/lexical/markdown-transformers';
6
+ export { editorTheme } from './lib/lexical/editor-theme';
7
+ export { LINK_MATCHERS, URL_REGEX, EMAIL_REGEX, } from './lib/lexical/auto-link-config';
8
+ export { useFloatingToolbar, } from './lib/lexical/floating-toolbar-plugin';
9
+ export { SyncPlugin } from './lib/lexical/sync-plugin';
10
+ export { ClickableLinkPlugin, isUrlSafe, } from './lib/lexical/clickable-link-plugin';
11
+ // Headless editor for server-side operations
12
+ export { createMoldableHeadlessEditor } from './lib/lexical/headless-editor';
@@ -0,0 +1,21 @@
1
+ /**
2
+ * URL regex pattern for auto-linking
3
+ * Matches URLs with http/https protocol or www prefix
4
+ */
5
+ export declare const URL_REGEX: RegExp;
6
+ /**
7
+ * Email regex pattern for auto-linking
8
+ * Matches standard email address formats
9
+ */
10
+ export declare const EMAIL_REGEX: RegExp;
11
+ /**
12
+ * Link matchers for AutoLinkPlugin
13
+ * Automatically converts URLs and emails to clickable links
14
+ */
15
+ export declare const LINK_MATCHERS: ((text: string) => {
16
+ index: number;
17
+ length: number;
18
+ text: string;
19
+ url: string;
20
+ } | null)[];
21
+ //# sourceMappingURL=auto-link-config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auto-link-config.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/auto-link-config.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,SAAS,QAC2G,CAAA;AAEjI;;;GAGG;AACH,eAAO,MAAM,WAAW,QAC+H,CAAA;AAEvJ;;;GAGG;AACH,eAAO,MAAM,aAAa;;;;;WAOzB,CAAA"}
@@ -0,0 +1,23 @@
1
+ import { createLinkMatcherWithRegExp } from '@lexical/react/LexicalAutoLinkPlugin';
2
+ /**
3
+ * URL regex pattern for auto-linking
4
+ * Matches URLs with http/https protocol or www prefix
5
+ */
6
+ export const URL_REGEX = /((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)(?<![-.+():%])/;
7
+ /**
8
+ * Email regex pattern for auto-linking
9
+ * Matches standard email address formats
10
+ */
11
+ export const EMAIL_REGEX = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
12
+ /**
13
+ * Link matchers for AutoLinkPlugin
14
+ * Automatically converts URLs and emails to clickable links
15
+ */
16
+ export const LINK_MATCHERS = [
17
+ createLinkMatcherWithRegExp(URL_REGEX, (text) => {
18
+ return text.startsWith('http') ? text : `https://${text}`;
19
+ }),
20
+ createLinkMatcherWithRegExp(EMAIL_REGEX, (text) => {
21
+ return `mailto:${text}`;
22
+ }),
23
+ ];
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Validates if a URL is safe to open externally.
3
+ * Only allows http, https, and mailto protocols.
4
+ */
5
+ export declare function isUrlSafe(url: string): boolean;
6
+ /**
7
+ * Plugin that makes links clickable in the editor.
8
+ * Opens links in a new tab with security checks.
9
+ */
10
+ export declare function ClickableLinkPlugin(): null;
11
+ //# sourceMappingURL=clickable-link-plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clickable-link-plugin.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/clickable-link-plugin.tsx"],"names":[],"mappings":"AAeA;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAQ9C;AAgBD;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,IAAI,CA4E1C"}
@@ -0,0 +1,97 @@
1
+ 'use client';
2
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
3
+ import { useEffect } from 'react';
4
+ import { $isLinkNode } from '@lexical/link';
5
+ import { $findMatchingParent, isHTMLAnchorElement } from '@lexical/utils';
6
+ import { $getNearestNodeFromDOMNode, $getSelection, $isElementNode, $isRangeSelection, getNearestEditorFromDOMNode, isDOMNode, } from 'lexical';
7
+ /**
8
+ * Validates if a URL is safe to open externally.
9
+ * Only allows http, https, and mailto protocols.
10
+ */
11
+ export function isUrlSafe(url) {
12
+ try {
13
+ const parsedUrl = new URL(url);
14
+ const allowedProtocols = ['http:', 'https:', 'mailto:'];
15
+ return allowedProtocols.includes(parsedUrl.protocol);
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
21
+ function findMatchingDOM(startNode, predicate) {
22
+ let node = startNode;
23
+ while (node != null) {
24
+ if (predicate(node)) {
25
+ return node;
26
+ }
27
+ node = node.parentNode;
28
+ }
29
+ return null;
30
+ }
31
+ /**
32
+ * Plugin that makes links clickable in the editor.
33
+ * Opens links in a new tab with security checks.
34
+ */
35
+ export function ClickableLinkPlugin() {
36
+ const [editor] = useLexicalComposerContext();
37
+ useEffect(() => {
38
+ const onClick = (event) => {
39
+ const target = event.target;
40
+ if (!isDOMNode(target)) {
41
+ return;
42
+ }
43
+ const nearestEditor = getNearestEditorFromDOMNode(target);
44
+ if (nearestEditor === null) {
45
+ return;
46
+ }
47
+ let url = null;
48
+ nearestEditor.update(() => {
49
+ const clickedNode = $getNearestNodeFromDOMNode(target);
50
+ if (clickedNode !== null) {
51
+ const maybeLinkNode = $findMatchingParent(clickedNode, $isElementNode);
52
+ if ($isLinkNode(maybeLinkNode)) {
53
+ url = maybeLinkNode.sanitizeUrl(maybeLinkNode.getURL());
54
+ }
55
+ else {
56
+ const a = findMatchingDOM(target, isHTMLAnchorElement);
57
+ if (a !== null) {
58
+ url = a.href;
59
+ }
60
+ }
61
+ }
62
+ });
63
+ if (url === null || url === '') {
64
+ return;
65
+ }
66
+ // Allow user to select link text without following url
67
+ const selection = editor.getEditorState().read($getSelection);
68
+ if ($isRangeSelection(selection) && !selection.isCollapsed()) {
69
+ event.preventDefault();
70
+ return;
71
+ }
72
+ event.preventDefault();
73
+ if (!isUrlSafe(url)) {
74
+ console.warn(`Blocked potentially unsafe URL: ${url}`);
75
+ return;
76
+ }
77
+ const isMiddle = event.type === 'auxclick' && event.button === 1;
78
+ window.open(url, isMiddle || event.metaKey || event.ctrlKey ? '_blank' : '_blank', 'noopener,noreferrer');
79
+ };
80
+ const onMouseUp = (event) => {
81
+ if (event.button === 1) {
82
+ onClick(event);
83
+ }
84
+ };
85
+ return editor.registerRootListener((rootElement, prevRootElement) => {
86
+ if (prevRootElement !== null) {
87
+ prevRootElement.removeEventListener('click', onClick);
88
+ prevRootElement.removeEventListener('mouseup', onMouseUp);
89
+ }
90
+ if (rootElement !== null) {
91
+ rootElement.addEventListener('click', onClick);
92
+ rootElement.addEventListener('mouseup', onMouseUp);
93
+ }
94
+ });
95
+ }, [editor]);
96
+ return null;
97
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Lexical Editor Theme
3
+ * Tailwind CSS classes for styling the editor content
4
+ */
5
+ export declare const editorTheme: {
6
+ paragraph: string;
7
+ placeholder: string;
8
+ text: {
9
+ bold: string;
10
+ italic: string;
11
+ underline: string;
12
+ strikethrough: string;
13
+ underlineStrikethrough: string;
14
+ code: string;
15
+ };
16
+ heading: {
17
+ h1: string;
18
+ h2: string;
19
+ h3: string;
20
+ h4: string;
21
+ h5: string;
22
+ h6: string;
23
+ };
24
+ list: {
25
+ nested: {
26
+ listitem: string;
27
+ };
28
+ ol: string;
29
+ ul: string;
30
+ listitem: string;
31
+ listitemChecked: string;
32
+ listitemUnchecked: string;
33
+ };
34
+ link: string;
35
+ quote: string;
36
+ code: string;
37
+ };
38
+ //# sourceMappingURL=editor-theme.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"editor-theme.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/editor-theme.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCvB,CAAA"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Lexical Editor Theme
3
+ * Tailwind CSS classes for styling the editor content
4
+ */
5
+ export const editorTheme = {
6
+ paragraph: 'my-1',
7
+ placeholder: 'text-sm text-muted-foreground! opacity-50',
8
+ text: {
9
+ bold: 'font-bold',
10
+ italic: 'italic',
11
+ underline: 'underline',
12
+ strikethrough: 'line-through',
13
+ underlineStrikethrough: 'underline line-through',
14
+ code: 'bg-muted px-1.5 py-0.5 rounded font-mono text-sm',
15
+ },
16
+ heading: {
17
+ h1: 'text-2xl font-bold my-3',
18
+ h2: 'text-xl font-bold my-2',
19
+ h3: 'text-lg font-bold my-2',
20
+ h4: 'text-base font-bold my-1',
21
+ h5: 'text-sm font-bold my-1',
22
+ h6: 'text-xs font-bold my-1',
23
+ },
24
+ list: {
25
+ nested: {
26
+ listitem: 'lexical-nested-listitem',
27
+ },
28
+ ol: 'ml-4 list-outside list-decimal',
29
+ ul: 'ml-4 list-outside list-disc',
30
+ listitem: '',
31
+ listitemChecked: 'lexical-listitem-checked',
32
+ listitemUnchecked: 'lexical-listitem-unchecked',
33
+ },
34
+ link: 'text-primary underline hover:text-primary/80 cursor-pointer',
35
+ quote: 'border-l-4 border-muted px-4 py-2 my-2 text-muted-foreground',
36
+ code: 'bg-muted p-3 my-4 rounded font-mono text-sm',
37
+ };
@@ -0,0 +1,30 @@
1
+ import { LexicalEditor } from 'lexical';
2
+ export type FloatingToolbarState = {
3
+ isVisible: boolean;
4
+ isBold: boolean;
5
+ isItalic: boolean;
6
+ isUnderline: boolean;
7
+ isStrikethrough: boolean;
8
+ isCode: boolean;
9
+ blockType: string;
10
+ editor: LexicalEditor;
11
+ position: {
12
+ top: number;
13
+ left: number;
14
+ } | null;
15
+ onBold: () => void;
16
+ onItalic: () => void;
17
+ onUnderline: () => void;
18
+ onStrikethrough: () => void;
19
+ onCode: () => void;
20
+ onParagraph: () => void;
21
+ onHeading: (size: 'h1' | 'h2' | 'h3') => void;
22
+ onBulletList: () => void;
23
+ onNumberedList: () => void;
24
+ onCheckList: () => void;
25
+ onQuote: () => void;
26
+ onCodeBlock: () => void;
27
+ onClearFormatting: () => void;
28
+ };
29
+ export declare function useFloatingToolbar(): FloatingToolbarState;
30
+ //# sourceMappingURL=floating-toolbar-plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"floating-toolbar-plugin.d.ts","sourceRoot":"","sources":["../../../src/lib/lexical/floating-toolbar-plugin.tsx"],"names":[],"mappings":"AAoBA,OAAO,EAKL,aAAa,EAGd,MAAM,SAAS,CAAA;AAEhB,MAAM,MAAM,oBAAoB,GAAG;IACjC,SAAS,EAAE,OAAO,CAAA;IAClB,MAAM,EAAE,OAAO,CAAA;IACf,QAAQ,EAAE,OAAO,CAAA;IACjB,WAAW,EAAE,OAAO,CAAA;IACpB,eAAe,EAAE,OAAO,CAAA;IACxB,MAAM,EAAE,OAAO,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,aAAa,CAAA;IACrB,QAAQ,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAA;IAC9C,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,QAAQ,EAAE,MAAM,IAAI,CAAA;IACpB,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,eAAe,EAAE,MAAM,IAAI,CAAA;IAC3B,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,KAAK,IAAI,CAAA;IAC7C,YAAY,EAAE,MAAM,IAAI,CAAA;IACxB,cAAc,EAAE,MAAM,IAAI,CAAA;IAC1B,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,OAAO,EAAE,MAAM,IAAI,CAAA;IACnB,WAAW,EAAE,MAAM,IAAI,CAAA;IACvB,iBAAiB,EAAE,MAAM,IAAI,CAAA;CAC9B,CAAA;AAED,wBAAgB,kBAAkB,IAAI,oBAAoB,CAkQzD"}