@mdaemon/html-editor-react 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # MDHTMLEditor React
2
+
3
+ React wrapper for MDHTMLEditor, providing a TinyMCE-compatible React component.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mdaemon/html-editor-react
9
+ ```
10
+
11
+ Note: This package requires `@mdaemon/html-editor` as a peer dependency.
12
+
13
+ ## Usage
14
+
15
+ ### Basic Usage
16
+
17
+ ```tsx
18
+ import React, { useRef } from 'react';
19
+ import { MDEditor, EditorRef } from '@mdaemon/html-editor-react';
20
+
21
+ function MyComponent() {
22
+ const editorRef = useRef<EditorRef>(null);
23
+
24
+ const handleInit = (editor) => {
25
+ console.log('Editor ready:', editor);
26
+ };
27
+
28
+ const handleChange = (content) => {
29
+ console.log('Content changed:', content);
30
+ };
31
+
32
+ return (
33
+ <MDEditor
34
+ ref={editorRef}
35
+ init={{
36
+ height: 400,
37
+ basicEditor: false,
38
+ }}
39
+ initialValue="<p>Hello World</p>"
40
+ onInit={handleInit}
41
+ onEditorChange={handleChange}
42
+ />
43
+ );
44
+ }
45
+ ```
46
+
47
+ ### useEditor Hook
48
+
49
+ For more control, use the `useEditor` hook:
50
+
51
+ ```tsx
52
+ import { useEditor } from '@mdaemon/html-editor-react';
53
+
54
+ function MyComponent() {
55
+ const { containerRef, editor, content, setContent, isDirty } = useEditor({
56
+ initialValue: '<p>Hello</p>',
57
+ onChange: (html) => console.log('Changed:', html),
58
+ });
59
+
60
+ const handleSave = () => {
61
+ if (isDirty) {
62
+ saveContent(content);
63
+ editor?.setDirty(false);
64
+ }
65
+ };
66
+
67
+ return (
68
+ <div>
69
+ <div ref={containerRef} />
70
+ <button onClick={handleSave}>Save</button>
71
+ </div>
72
+ );
73
+ }
74
+ ```
75
+
76
+ ### Global Access
77
+
78
+ For legacy compatibility, editors can be accessed globally:
79
+
80
+ ```tsx
81
+ import { getEditorContent, setEditorContent } from '@mdaemon/html-editor-react';
82
+
83
+ // Get content from any editor by ID
84
+ const content = getEditorContent('myEditor');
85
+
86
+ // Set content
87
+ setEditorContent('myEditor', '<p>New content</p>');
88
+ ```
89
+
90
+ ## Props
91
+
92
+ | Prop | Type | Description |
93
+ |------|------|-------------|
94
+ | `id` | string | Editor identifier for global access |
95
+ | `init` | EditorConfig | Configuration object passed to HTMLEditor |
96
+ | `initialValue` | string | Initial HTML content |
97
+ | `value` | string | Controlled content value |
98
+ | `disabled` | boolean | Disable editing |
99
+ | `onInit` | (editor) => void | Called when editor initializes |
100
+ | `onEditorChange` | (content) => void | Called on content change |
101
+ | `onDirtyChange` | (isDirty) => void | Called when dirty state changes |
102
+ | `onFocus` | () => void | Called on focus |
103
+ | `onBlur` | () => void | Called on blur |
104
+
105
+ ## EditorRef Methods
106
+
107
+ When using the `ref` prop, you have access to:
108
+
109
+ - `getContent(): string` - Get current HTML
110
+ - `setContent(html: string): void` - Set HTML content
111
+ - `insertContent(html: string): void` - Insert at cursor
112
+ - `execCommand(cmd, ui?, value?): boolean` - Run command
113
+ - `focus(): void` - Focus editor
114
+ - `isDirty(): boolean` - Check dirty state
115
+ - `setDirty(state): void` - Set dirty state
116
+
117
+ ## Configuration
118
+
119
+ All configuration options from `@mdaemon/html-editor` are supported in the `init` prop:
120
+
121
+ ```tsx
122
+ <MDEditor
123
+ init={{
124
+ basicEditor: false,
125
+ includeTemplates: true,
126
+ templates: [
127
+ { title: 'Signature', content: '<p>Best regards,<br/>John</p>' }
128
+ ],
129
+ dropbox: true,
130
+ images_upload_url: '/api/upload',
131
+ font_family_formats: 'Arial=arial;Times=times new roman',
132
+ font_size_formats: '10pt 12pt 14pt 16pt',
133
+ fontName: 'Arial',
134
+ fontSize: '12pt',
135
+ directionality: 'ltr',
136
+ skin: 'oxide',
137
+ height: 400,
138
+ }}
139
+ />
140
+ ```
141
+
142
+ ## Migration from @tinymce/tinymce-react
143
+
144
+ This package provides a similar API to `@tinymce/tinymce-react`:
145
+
146
+ ```tsx
147
+ // Before (TinyMCE)
148
+ import { Editor } from '@tinymce/tinymce-react';
149
+
150
+ <Editor
151
+ init={{ height: 400, plugins: 'link image' }}
152
+ onEditorChange={handleChange}
153
+ />
154
+
155
+ // After (MDHTMLEditor)
156
+ import { MDEditor } from '@mdaemon/html-editor-react';
157
+
158
+ <MDEditor
159
+ init={{ height: 400 }}
160
+ onEditorChange={handleChange}
161
+ />
162
+ ```
163
+
164
+ Key differences:
165
+ - No `licenseKey` or `apiKey` required
166
+ - `plugins` option not needed (features built-in)
167
+ - Toolbar customization uses `basicEditor` boolean instead of `toolbar` string
168
+
169
+ ## License
170
+
171
+ LGPL 3.0 or later - MDaemon Technologies
@@ -0,0 +1,168 @@
1
+ import { EditorConfig } from '@mdaemon/html-editor';
2
+ import { EditorEvents } from '@mdaemon/html-editor';
3
+ import { fontNames } from '@mdaemon/html-editor';
4
+ import { ForwardRefExoticComponent } from 'react';
5
+ import { HTMLEditor } from '@mdaemon/html-editor';
6
+ import { RefAttributes } from 'react';
7
+ import { setGetFileSrc } from '@mdaemon/html-editor';
8
+ import { setTranslate } from '@mdaemon/html-editor';
9
+ import { Template } from '@mdaemon/html-editor';
10
+ import { ToolbarButtonAPI } from '@mdaemon/html-editor';
11
+ import { ToolbarButtonSpec } from '@mdaemon/html-editor';
12
+
13
+ /**
14
+ * MDHTMLEditor React Component
15
+ */
16
+ declare const Editor: ForwardRefExoticComponent<EditorProps & RefAttributes<EditorRef>>;
17
+ export { Editor }
18
+ export { Editor as MDEditor }
19
+
20
+ export { EditorConfig }
21
+
22
+ export { EditorEvents }
23
+
24
+ export declare interface EditorProps {
25
+ /**
26
+ * Editor configuration - TinyMCE compatible
27
+ */
28
+ config: EditorConfig;
29
+ /**
30
+ * Initial HTML content (also known as 'body' for TinyMCE compat)
31
+ */
32
+ body?: string;
33
+ /**
34
+ * Initial HTML content (alias for body)
35
+ */
36
+ initialValue?: string;
37
+ /**
38
+ * Name attribute for the hidden textarea
39
+ */
40
+ name?: string;
41
+ /**
42
+ * Called when content is modified
43
+ */
44
+ onDirty?: (dirty: boolean) => void;
45
+ /**
46
+ * Called on content change (debounced)
47
+ */
48
+ onChange?: (content: string) => void;
49
+ /**
50
+ * Called when editor is fully initialized
51
+ */
52
+ onInit?: (editor: HTMLEditor) => void;
53
+ /**
54
+ * Called when editor receives focus
55
+ */
56
+ onFocus?: () => void;
57
+ /**
58
+ * Called when editor loses focus
59
+ */
60
+ onBlur?: () => void;
61
+ /**
62
+ * Translation function
63
+ */
64
+ translate?: (key: string) => string;
65
+ /**
66
+ * Function to resolve file paths (like TinyMCE's tinymceScriptSrc path)
67
+ */
68
+ getFileSrc?: (path: string) => string;
69
+ /**
70
+ * Disable the editor
71
+ */
72
+ disabled?: boolean;
73
+ }
74
+
75
+ export declare interface EditorRef {
76
+ /**
77
+ * Get the underlying HTMLEditor instance
78
+ */
79
+ getEditor: () => HTMLEditor | null;
80
+ /**
81
+ * Get the editor content as HTML
82
+ */
83
+ getContent: () => string;
84
+ /**
85
+ * Set the editor content
86
+ */
87
+ setContent: (html: string) => void;
88
+ /**
89
+ * Insert content at cursor position
90
+ */
91
+ insertContent: (html: string) => void;
92
+ /**
93
+ * Focus the editor
94
+ */
95
+ focus: () => void;
96
+ }
97
+
98
+ export { fontNames }
99
+
100
+ /**
101
+ * Get content from the global editor instance
102
+ * Note: Only works with single editor on the page
103
+ */
104
+ export declare function getEditorContent(): string;
105
+
106
+ /**
107
+ * Set content on the global editor instance
108
+ * Note: Only works with single editor on the page
109
+ */
110
+ export declare function setEditorContent(html: string): void;
111
+
112
+ export { setGetFileSrc }
113
+
114
+ export { setTranslate }
115
+
116
+ export { Template }
117
+
118
+ export { ToolbarButtonAPI }
119
+
120
+ export { ToolbarButtonSpec }
121
+
122
+ /**
123
+ * Hook for programmatic editor control
124
+ */
125
+ export declare function useEditor(options?: UseEditorOptions): UseEditorReturn;
126
+
127
+ export declare interface UseEditorOptions {
128
+ config?: EditorConfig;
129
+ content?: string;
130
+ onUpdate?: (html: string) => void;
131
+ }
132
+
133
+ export declare interface UseEditorReturn {
134
+ /**
135
+ * The editor instance
136
+ */
137
+ editor: HTMLEditor | null;
138
+ /**
139
+ * Ref to attach to the container element
140
+ */
141
+ containerRef: React.RefObject<HTMLDivElement | null>;
142
+ /**
143
+ * Whether the editor is ready
144
+ */
145
+ ready: boolean;
146
+ /**
147
+ * Get the current HTML content
148
+ */
149
+ getContent: () => string;
150
+ /**
151
+ * Set the HTML content
152
+ */
153
+ setContent: (html: string) => void;
154
+ /**
155
+ * Insert HTML at cursor
156
+ */
157
+ insertContent: (html: string) => void;
158
+ /**
159
+ * Focus the editor
160
+ */
161
+ focus: () => void;
162
+ /**
163
+ * Check if editor is dirty
164
+ */
165
+ isDirty: () => boolean;
166
+ }
167
+
168
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ const react = require("react");
5
+ const htmlEditor = require("@mdaemon/html-editor");
6
+ let globalEditor = null;
7
+ function getEditorContent() {
8
+ return (globalEditor == null ? void 0 : globalEditor.getContent()) ?? "";
9
+ }
10
+ function setEditorContent(html) {
11
+ globalEditor == null ? void 0 : globalEditor.setContent(html);
12
+ }
13
+ const Editor = react.forwardRef(function Editor2(props, ref) {
14
+ var _a;
15
+ const {
16
+ config,
17
+ body,
18
+ initialValue,
19
+ name,
20
+ onDirty,
21
+ onChange,
22
+ onInit,
23
+ onFocus,
24
+ onBlur,
25
+ translate,
26
+ getFileSrc,
27
+ disabled
28
+ } = props;
29
+ const containerRef = react.useRef(null);
30
+ const editorRef = react.useRef(null);
31
+ const [ready, setReady] = react.useState(false);
32
+ react.useEffect(() => {
33
+ if (translate) {
34
+ htmlEditor.setTranslate(translate);
35
+ }
36
+ }, [translate]);
37
+ react.useEffect(() => {
38
+ if (getFileSrc) {
39
+ htmlEditor.setGetFileSrc(getFileSrc);
40
+ }
41
+ }, [getFileSrc]);
42
+ react.useEffect(() => {
43
+ if (!containerRef.current) return;
44
+ const editor = new htmlEditor.HTMLEditor(containerRef.current, {
45
+ ...config,
46
+ // Pass setup through to allow custom buttons
47
+ setup: (ed) => {
48
+ var _a2;
49
+ (_a2 = config.setup) == null ? void 0 : _a2.call(config, ed);
50
+ }
51
+ });
52
+ editorRef.current = editor;
53
+ globalEditor = editor;
54
+ const content = body ?? initialValue ?? "";
55
+ if (content) {
56
+ editor.setContent(content);
57
+ }
58
+ editor.on("init", () => {
59
+ setReady(true);
60
+ onInit == null ? void 0 : onInit(editor);
61
+ });
62
+ editor.on("dirty", (dirty) => {
63
+ onDirty == null ? void 0 : onDirty(dirty);
64
+ });
65
+ editor.on("change", (content2) => {
66
+ onChange == null ? void 0 : onChange(content2);
67
+ editor.setDirty(false);
68
+ });
69
+ editor.on("focus", () => {
70
+ onFocus == null ? void 0 : onFocus();
71
+ });
72
+ editor.on("blur", () => {
73
+ onBlur == null ? void 0 : onBlur();
74
+ });
75
+ return () => {
76
+ editor.destroy();
77
+ editorRef.current = null;
78
+ if (globalEditor === editor) {
79
+ globalEditor = null;
80
+ }
81
+ };
82
+ }, []);
83
+ react.useEffect(() => {
84
+ if (containerRef.current) {
85
+ containerRef.current.style.pointerEvents = disabled ? "none" : "";
86
+ containerRef.current.style.opacity = disabled ? "0.6" : "";
87
+ }
88
+ }, [disabled]);
89
+ react.useImperativeHandle(ref, () => ({
90
+ getEditor: () => editorRef.current,
91
+ getContent: () => {
92
+ var _a2;
93
+ return ((_a2 = editorRef.current) == null ? void 0 : _a2.getContent()) ?? "";
94
+ },
95
+ setContent: (html) => {
96
+ var _a2;
97
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.setContent(html);
98
+ },
99
+ insertContent: (html) => {
100
+ var _a2;
101
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.insertContent(html);
102
+ },
103
+ focus: () => {
104
+ var _a2;
105
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
106
+ }
107
+ }), []);
108
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "md-editor-react-wrapper", children: [
109
+ /* @__PURE__ */ jsxRuntime.jsx("div", { ref: containerRef, className: "md-editor-container" }),
110
+ name && ready && /* @__PURE__ */ jsxRuntime.jsx(
111
+ "input",
112
+ {
113
+ type: "hidden",
114
+ name,
115
+ value: ((_a = editorRef.current) == null ? void 0 : _a.getContent()) ?? ""
116
+ }
117
+ )
118
+ ] });
119
+ });
120
+ Editor.displayName = "MDHTMLEditor";
121
+ function useEditor(options = {}) {
122
+ const { config = {}, content = "", onUpdate } = options;
123
+ const containerRef = react.useRef(null);
124
+ const editorRef = react.useRef(null);
125
+ const [ready, setReady] = react.useState(false);
126
+ react.useEffect(() => {
127
+ if (!containerRef.current) return;
128
+ const editor = new htmlEditor.HTMLEditor(containerRef.current, config);
129
+ editorRef.current = editor;
130
+ if (content) {
131
+ editor.setContent(content);
132
+ }
133
+ editor.on("init", () => {
134
+ setReady(true);
135
+ });
136
+ if (onUpdate) {
137
+ editor.on("change", onUpdate);
138
+ }
139
+ return () => {
140
+ editor.destroy();
141
+ editorRef.current = null;
142
+ setReady(false);
143
+ };
144
+ }, []);
145
+ const getContent = react.useCallback(() => {
146
+ var _a;
147
+ return ((_a = editorRef.current) == null ? void 0 : _a.getContent()) ?? "";
148
+ }, []);
149
+ const setContent = react.useCallback((html) => {
150
+ var _a;
151
+ (_a = editorRef.current) == null ? void 0 : _a.setContent(html);
152
+ }, []);
153
+ const insertContent = react.useCallback((html) => {
154
+ var _a;
155
+ (_a = editorRef.current) == null ? void 0 : _a.insertContent(html);
156
+ }, []);
157
+ const focus = react.useCallback(() => {
158
+ var _a;
159
+ (_a = editorRef.current) == null ? void 0 : _a.focus();
160
+ }, []);
161
+ const isDirty = react.useCallback(() => {
162
+ var _a;
163
+ return ((_a = editorRef.current) == null ? void 0 : _a.isDirty()) ?? false;
164
+ }, []);
165
+ return {
166
+ editor: editorRef.current,
167
+ containerRef,
168
+ ready,
169
+ getContent,
170
+ setContent,
171
+ insertContent,
172
+ focus,
173
+ isDirty
174
+ };
175
+ }
176
+ Object.defineProperty(exports, "fontNames", {
177
+ enumerable: true,
178
+ get: () => htmlEditor.fontNames
179
+ });
180
+ Object.defineProperty(exports, "setGetFileSrc", {
181
+ enumerable: true,
182
+ get: () => htmlEditor.setGetFileSrc
183
+ });
184
+ Object.defineProperty(exports, "setTranslate", {
185
+ enumerable: true,
186
+ get: () => htmlEditor.setTranslate
187
+ });
188
+ exports.Editor = Editor;
189
+ exports.MDEditor = Editor;
190
+ exports.getEditorContent = getEditorContent;
191
+ exports.setEditorContent = setEditorContent;
192
+ exports.useEditor = useEditor;
193
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/Editor.tsx","../src/useEditor.ts"],"sourcesContent":["/**\r\n * MDHTMLEditor React Component\r\n * A TinyMCE-compatible React wrapper for MDHTMLEditor\r\n */\r\n\r\nimport {\r\n useEffect,\r\n useRef,\r\n useState,\r\n useImperativeHandle,\r\n forwardRef,\r\n} from 'react';\r\nimport {\r\n HTMLEditor,\r\n EditorConfig,\r\n setTranslate,\r\n setGetFileSrc,\r\n} from '@mdaemon/html-editor';\r\n\r\nexport interface EditorProps {\r\n /**\r\n * Editor configuration - TinyMCE compatible\r\n */\r\n config: EditorConfig;\r\n \r\n /**\r\n * Initial HTML content (also known as 'body' for TinyMCE compat)\r\n */\r\n body?: string;\r\n \r\n /**\r\n * Initial HTML content (alias for body)\r\n */\r\n initialValue?: string;\r\n \r\n /**\r\n * Name attribute for the hidden textarea\r\n */\r\n name?: string;\r\n \r\n /**\r\n * Called when content is modified\r\n */\r\n onDirty?: (dirty: boolean) => void;\r\n \r\n /**\r\n * Called on content change (debounced)\r\n */\r\n onChange?: (content: string) => void;\r\n \r\n /**\r\n * Called when editor is fully initialized\r\n */\r\n onInit?: (editor: HTMLEditor) => void;\r\n \r\n /**\r\n * Called when editor receives focus\r\n */\r\n onFocus?: () => void;\r\n \r\n /**\r\n * Called when editor loses focus\r\n */\r\n onBlur?: () => void;\r\n \r\n /**\r\n * Translation function\r\n */\r\n translate?: (key: string) => string;\r\n \r\n /**\r\n * Function to resolve file paths (like TinyMCE's tinymceScriptSrc path)\r\n */\r\n getFileSrc?: (path: string) => string;\r\n \r\n /**\r\n * Disable the editor\r\n */\r\n disabled?: boolean;\r\n}\r\n\r\nexport interface EditorRef {\r\n /**\r\n * Get the underlying HTMLEditor instance\r\n */\r\n getEditor: () => HTMLEditor | null;\r\n \r\n /**\r\n * Get the editor content as HTML\r\n */\r\n getContent: () => string;\r\n \r\n /**\r\n * Set the editor content\r\n */\r\n setContent: (html: string) => void;\r\n \r\n /**\r\n * Insert content at cursor position\r\n */\r\n insertContent: (html: string) => void;\r\n \r\n /**\r\n * Focus the editor\r\n */\r\n focus: () => void;\r\n}\r\n\r\n// Global editor instance for single-editor APIs\r\nlet globalEditor: HTMLEditor | null = null;\r\n\r\n/**\r\n * Get content from the global editor instance\r\n * Note: Only works with single editor on the page\r\n */\r\nexport function getEditorContent(): string {\r\n return globalEditor?.getContent() ?? '';\r\n}\r\n\r\n/**\r\n * Set content on the global editor instance\r\n * Note: Only works with single editor on the page\r\n */\r\nexport function setEditorContent(html: string): void {\r\n globalEditor?.setContent(html);\r\n}\r\n\r\n/**\r\n * MDHTMLEditor React Component\r\n */\r\nexport const Editor = forwardRef<EditorRef, EditorProps>(function Editor(\r\n props,\r\n ref\r\n) {\r\n const {\r\n config,\r\n body,\r\n initialValue,\r\n name,\r\n onDirty,\r\n onChange,\r\n onInit,\r\n onFocus,\r\n onBlur,\r\n translate,\r\n getFileSrc,\r\n disabled,\r\n } = props;\r\n \r\n const containerRef = useRef<HTMLDivElement>(null);\r\n const editorRef = useRef<HTMLEditor | null>(null);\r\n const [ready, setReady] = useState(false);\r\n \r\n // Set global translation function if provided\r\n useEffect(() => {\r\n if (translate) {\r\n setTranslate(translate);\r\n }\r\n }, [translate]);\r\n \r\n // Set global file source resolver if provided\r\n useEffect(() => {\r\n if (getFileSrc) {\r\n setGetFileSrc(getFileSrc);\r\n }\r\n }, [getFileSrc]);\r\n \r\n // Initialize editor\r\n useEffect(() => {\r\n if (!containerRef.current) return;\r\n \r\n // Create the editor\r\n const editor = new HTMLEditor(containerRef.current, {\r\n ...config,\r\n // Pass setup through to allow custom buttons\r\n setup: (ed) => {\r\n config.setup?.(ed);\r\n },\r\n });\r\n \r\n editorRef.current = editor;\r\n globalEditor = editor;\r\n \r\n // Set initial content\r\n const content = body ?? initialValue ?? '';\r\n if (content) {\r\n editor.setContent(content);\r\n }\r\n \r\n // Bind events\r\n editor.on('init', () => {\r\n setReady(true);\r\n onInit?.(editor);\r\n });\r\n \r\n editor.on('dirty', (dirty) => {\r\n onDirty?.(dirty);\r\n });\r\n \r\n editor.on('change', (content) => {\r\n onChange?.(content);\r\n editor.setDirty(false);\r\n });\r\n \r\n editor.on('focus', () => {\r\n onFocus?.();\r\n });\r\n \r\n editor.on('blur', () => {\r\n onBlur?.();\r\n });\r\n \r\n // Cleanup\r\n return () => {\r\n editor.destroy();\r\n editorRef.current = null;\r\n if (globalEditor === editor) {\r\n globalEditor = null;\r\n }\r\n };\r\n }, []); // Only run once on mount\r\n \r\n // Update disabled state\r\n useEffect(() => {\r\n // Note: TipTap has setEditable() but our wrapper would need to expose it\r\n // For now, we apply a visual disabled state\r\n if (containerRef.current) {\r\n containerRef.current.style.pointerEvents = disabled ? 'none' : '';\r\n containerRef.current.style.opacity = disabled ? '0.6' : '';\r\n }\r\n }, [disabled]);\r\n \r\n // Expose methods via ref\r\n useImperativeHandle(ref, () => ({\r\n getEditor: () => editorRef.current,\r\n getContent: () => editorRef.current?.getContent() ?? '',\r\n setContent: (html: string) => editorRef.current?.setContent(html),\r\n insertContent: (html: string) => editorRef.current?.insertContent(html),\r\n focus: () => editorRef.current?.focus(),\r\n }), []);\r\n \r\n return (\r\n <div className=\"md-editor-react-wrapper\">\r\n <div ref={containerRef} className=\"md-editor-container\" />\r\n {/* Hidden textarea for form submission */}\r\n {name && ready && (\r\n <input\r\n type=\"hidden\"\r\n name={name}\r\n value={editorRef.current?.getContent() ?? ''}\r\n />\r\n )}\r\n </div>\r\n );\r\n});\r\n\r\nEditor.displayName = 'MDHTMLEditor';\r\n\r\nexport default Editor;\r\n","/**\r\n * useEditor Hook\r\n * Custom hook for advanced editor usage\r\n */\r\n\r\nimport { useEffect, useRef, useState, useCallback } from 'react';\r\nimport { HTMLEditor, EditorConfig } from '@mdaemon/html-editor';\r\n\r\nexport interface UseEditorOptions {\r\n config?: EditorConfig;\r\n content?: string;\r\n onUpdate?: (html: string) => void;\r\n}\r\n\r\nexport interface UseEditorReturn {\r\n /**\r\n * The editor instance\r\n */\r\n editor: HTMLEditor | null;\r\n \r\n /**\r\n * Ref to attach to the container element\r\n */\r\n containerRef: React.RefObject<HTMLDivElement | null>;\r\n \r\n /**\r\n * Whether the editor is ready\r\n */\r\n ready: boolean;\r\n \r\n /**\r\n * Get the current HTML content\r\n */\r\n getContent: () => string;\r\n \r\n /**\r\n * Set the HTML content\r\n */\r\n setContent: (html: string) => void;\r\n \r\n /**\r\n * Insert HTML at cursor\r\n */\r\n insertContent: (html: string) => void;\r\n \r\n /**\r\n * Focus the editor\r\n */\r\n focus: () => void;\r\n \r\n /**\r\n * Check if editor is dirty\r\n */\r\n isDirty: () => boolean;\r\n}\r\n\r\n/**\r\n * Hook for programmatic editor control\r\n */\r\nexport function useEditor(options: UseEditorOptions = {}): UseEditorReturn {\r\n const { config = {}, content = '', onUpdate } = options;\r\n \r\n const containerRef = useRef<HTMLDivElement | null>(null);\r\n const editorRef = useRef<HTMLEditor | null>(null);\r\n const [ready, setReady] = useState(false);\r\n \r\n useEffect(() => {\r\n if (!containerRef.current) return;\r\n \r\n const editor = new HTMLEditor(containerRef.current, config);\r\n editorRef.current = editor;\r\n \r\n if (content) {\r\n editor.setContent(content);\r\n }\r\n \r\n editor.on('init', () => {\r\n setReady(true);\r\n });\r\n \r\n if (onUpdate) {\r\n editor.on('change', onUpdate);\r\n }\r\n \r\n return () => {\r\n editor.destroy();\r\n editorRef.current = null;\r\n setReady(false);\r\n };\r\n }, []); // Only mount once\r\n \r\n const getContent = useCallback(() => {\r\n return editorRef.current?.getContent() ?? '';\r\n }, []);\r\n \r\n const setContent = useCallback((html: string) => {\r\n editorRef.current?.setContent(html);\r\n }, []);\r\n \r\n const insertContent = useCallback((html: string) => {\r\n editorRef.current?.insertContent(html);\r\n }, []);\r\n \r\n const focus = useCallback(() => {\r\n editorRef.current?.focus();\r\n }, []);\r\n \r\n const isDirty = useCallback(() => {\r\n return editorRef.current?.isDirty() ?? false;\r\n }, []);\r\n \r\n return {\r\n editor: editorRef.current,\r\n containerRef,\r\n ready,\r\n getContent,\r\n setContent,\r\n insertContent,\r\n focus,\r\n isDirty,\r\n };\r\n}\r\n"],"names":["forwardRef","Editor","useRef","useState","useEffect","setTranslate","setGetFileSrc","HTMLEditor","_a","content","useImperativeHandle","jsxs","jsx","useCallback"],"mappings":";;;;;AA6GA,IAAI,eAAkC;AAM/B,SAAS,mBAA2B;AACzC,UAAO,6CAAc,iBAAgB;AACvC;AAMO,SAAS,iBAAiB,MAAoB;AACnD,+CAAc,WAAW;AAC3B;AAKO,MAAM,SAASA,MAAAA,WAAmC,SAASC,QAChE,OACA,KACA;;AACA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,IACE;AAEJ,QAAM,eAAeC,MAAAA,OAAuB,IAAI;AAChD,QAAM,YAAYA,MAAAA,OAA0B,IAAI;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAIC,MAAAA,SAAS,KAAK;AAGxCC,QAAAA,UAAU,MAAM;AACd,QAAI,WAAW;AACbC,iBAAAA,aAAa,SAAS;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,SAAS,CAAC;AAGdD,QAAAA,UAAU,MAAM;AACd,QAAI,YAAY;AACdE,iBAAAA,cAAc,UAAU;A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S,KAAK;AAAA,IAChB;AAAA,EACF,GAAG,CAAA,CAAE;AAEL,QAAM,aAAaM,MAAAA,YAAY,MAAM;;AACnC,aAAO,eAAU,YAAV,mBAAmB,iBAAgB;AAAA,EAC5C,GAAG,CAAA,CAAE;AAEL,QAAM,aAAaA,kBAAY,CAAC,SAAiB;;AAC/C,oBAAU,YAAV,mBAAmB,WAAW;AAAA,EAChC,GAAG,CAAA,CAAE;AAEL,QAAM,gBAAgBA,kBAAY,CAAC,SAAiB;;AAClD,oBAAU,YAAV,mBAAmB,cAAc;AAAA,EACnC,GAAG,CAAA,CAAE;AAEL,QAAM,QAAQA,MAAAA,YAAY,MAAM;;AAC9B,oBAAU,YAAV,mBAAmB;AAAA,EACrB,GAAG,CAAA,CAAE;AAEL,QAAM,UAAUA,MAAAA,YAAY,MAAM;;AAChC,aAAO,eAAU,YAAV,mBAAmB,cAAa;AAAA,EACzC,GAAG,CAAA,CAAE;AAEL,SAAO;AAAA,IACL,QAAQ,UAAU;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;;;;;;;;;;;;;;;;;;"}
package/dist/index.mjs ADDED
@@ -0,0 +1,185 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { forwardRef, useRef, useState, useEffect, useImperativeHandle, useCallback } from "react";
3
+ import { setTranslate, setGetFileSrc, HTMLEditor } from "@mdaemon/html-editor";
4
+ import { fontNames, setGetFileSrc as setGetFileSrc2, setTranslate as setTranslate2 } from "@mdaemon/html-editor";
5
+ let globalEditor = null;
6
+ function getEditorContent() {
7
+ return (globalEditor == null ? void 0 : globalEditor.getContent()) ?? "";
8
+ }
9
+ function setEditorContent(html) {
10
+ globalEditor == null ? void 0 : globalEditor.setContent(html);
11
+ }
12
+ const Editor = forwardRef(function Editor2(props, ref) {
13
+ var _a;
14
+ const {
15
+ config,
16
+ body,
17
+ initialValue,
18
+ name,
19
+ onDirty,
20
+ onChange,
21
+ onInit,
22
+ onFocus,
23
+ onBlur,
24
+ translate,
25
+ getFileSrc,
26
+ disabled
27
+ } = props;
28
+ const containerRef = useRef(null);
29
+ const editorRef = useRef(null);
30
+ const [ready, setReady] = useState(false);
31
+ useEffect(() => {
32
+ if (translate) {
33
+ setTranslate(translate);
34
+ }
35
+ }, [translate]);
36
+ useEffect(() => {
37
+ if (getFileSrc) {
38
+ setGetFileSrc(getFileSrc);
39
+ }
40
+ }, [getFileSrc]);
41
+ useEffect(() => {
42
+ if (!containerRef.current) return;
43
+ const editor = new HTMLEditor(containerRef.current, {
44
+ ...config,
45
+ // Pass setup through to allow custom buttons
46
+ setup: (ed) => {
47
+ var _a2;
48
+ (_a2 = config.setup) == null ? void 0 : _a2.call(config, ed);
49
+ }
50
+ });
51
+ editorRef.current = editor;
52
+ globalEditor = editor;
53
+ const content = body ?? initialValue ?? "";
54
+ if (content) {
55
+ editor.setContent(content);
56
+ }
57
+ editor.on("init", () => {
58
+ setReady(true);
59
+ onInit == null ? void 0 : onInit(editor);
60
+ });
61
+ editor.on("dirty", (dirty) => {
62
+ onDirty == null ? void 0 : onDirty(dirty);
63
+ });
64
+ editor.on("change", (content2) => {
65
+ onChange == null ? void 0 : onChange(content2);
66
+ editor.setDirty(false);
67
+ });
68
+ editor.on("focus", () => {
69
+ onFocus == null ? void 0 : onFocus();
70
+ });
71
+ editor.on("blur", () => {
72
+ onBlur == null ? void 0 : onBlur();
73
+ });
74
+ return () => {
75
+ editor.destroy();
76
+ editorRef.current = null;
77
+ if (globalEditor === editor) {
78
+ globalEditor = null;
79
+ }
80
+ };
81
+ }, []);
82
+ useEffect(() => {
83
+ if (containerRef.current) {
84
+ containerRef.current.style.pointerEvents = disabled ? "none" : "";
85
+ containerRef.current.style.opacity = disabled ? "0.6" : "";
86
+ }
87
+ }, [disabled]);
88
+ useImperativeHandle(ref, () => ({
89
+ getEditor: () => editorRef.current,
90
+ getContent: () => {
91
+ var _a2;
92
+ return ((_a2 = editorRef.current) == null ? void 0 : _a2.getContent()) ?? "";
93
+ },
94
+ setContent: (html) => {
95
+ var _a2;
96
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.setContent(html);
97
+ },
98
+ insertContent: (html) => {
99
+ var _a2;
100
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.insertContent(html);
101
+ },
102
+ focus: () => {
103
+ var _a2;
104
+ return (_a2 = editorRef.current) == null ? void 0 : _a2.focus();
105
+ }
106
+ }), []);
107
+ return /* @__PURE__ */ jsxs("div", { className: "md-editor-react-wrapper", children: [
108
+ /* @__PURE__ */ jsx("div", { ref: containerRef, className: "md-editor-container" }),
109
+ name && ready && /* @__PURE__ */ jsx(
110
+ "input",
111
+ {
112
+ type: "hidden",
113
+ name,
114
+ value: ((_a = editorRef.current) == null ? void 0 : _a.getContent()) ?? ""
115
+ }
116
+ )
117
+ ] });
118
+ });
119
+ Editor.displayName = "MDHTMLEditor";
120
+ function useEditor(options = {}) {
121
+ const { config = {}, content = "", onUpdate } = options;
122
+ const containerRef = useRef(null);
123
+ const editorRef = useRef(null);
124
+ const [ready, setReady] = useState(false);
125
+ useEffect(() => {
126
+ if (!containerRef.current) return;
127
+ const editor = new HTMLEditor(containerRef.current, config);
128
+ editorRef.current = editor;
129
+ if (content) {
130
+ editor.setContent(content);
131
+ }
132
+ editor.on("init", () => {
133
+ setReady(true);
134
+ });
135
+ if (onUpdate) {
136
+ editor.on("change", onUpdate);
137
+ }
138
+ return () => {
139
+ editor.destroy();
140
+ editorRef.current = null;
141
+ setReady(false);
142
+ };
143
+ }, []);
144
+ const getContent = useCallback(() => {
145
+ var _a;
146
+ return ((_a = editorRef.current) == null ? void 0 : _a.getContent()) ?? "";
147
+ }, []);
148
+ const setContent = useCallback((html) => {
149
+ var _a;
150
+ (_a = editorRef.current) == null ? void 0 : _a.setContent(html);
151
+ }, []);
152
+ const insertContent = useCallback((html) => {
153
+ var _a;
154
+ (_a = editorRef.current) == null ? void 0 : _a.insertContent(html);
155
+ }, []);
156
+ const focus = useCallback(() => {
157
+ var _a;
158
+ (_a = editorRef.current) == null ? void 0 : _a.focus();
159
+ }, []);
160
+ const isDirty = useCallback(() => {
161
+ var _a;
162
+ return ((_a = editorRef.current) == null ? void 0 : _a.isDirty()) ?? false;
163
+ }, []);
164
+ return {
165
+ editor: editorRef.current,
166
+ containerRef,
167
+ ready,
168
+ getContent,
169
+ setContent,
170
+ insertContent,
171
+ focus,
172
+ isDirty
173
+ };
174
+ }
175
+ export {
176
+ Editor,
177
+ Editor as MDEditor,
178
+ fontNames,
179
+ getEditorContent,
180
+ setEditorContent,
181
+ setGetFileSrc2 as setGetFileSrc,
182
+ setTranslate2 as setTranslate,
183
+ useEditor
184
+ };
185
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../src/Editor.tsx","../src/useEditor.ts"],"sourcesContent":["/**\r\n * MDHTMLEditor React Component\r\n * A TinyMCE-compatible React wrapper for MDHTMLEditor\r\n */\r\n\r\nimport {\r\n useEffect,\r\n useRef,\r\n useState,\r\n useImperativeHandle,\r\n forwardRef,\r\n} from 'react';\r\nimport {\r\n HTMLEditor,\r\n EditorConfig,\r\n setTranslate,\r\n setGetFileSrc,\r\n} from '@mdaemon/html-editor';\r\n\r\nexport interface EditorProps {\r\n /**\r\n * Editor configuration - TinyMCE compatible\r\n */\r\n config: EditorConfig;\r\n \r\n /**\r\n * Initial HTML content (also known as 'body' for TinyMCE compat)\r\n */\r\n body?: string;\r\n \r\n /**\r\n * Initial HTML content (alias for body)\r\n */\r\n initialValue?: string;\r\n \r\n /**\r\n * Name attribute for the hidden textarea\r\n */\r\n name?: string;\r\n \r\n /**\r\n * Called when content is modified\r\n */\r\n onDirty?: (dirty: boolean) => void;\r\n \r\n /**\r\n * Called on content change (debounced)\r\n */\r\n onChange?: (content: string) => void;\r\n \r\n /**\r\n * Called when editor is fully initialized\r\n */\r\n onInit?: (editor: HTMLEditor) => void;\r\n \r\n /**\r\n * Called when editor receives focus\r\n */\r\n onFocus?: () => void;\r\n \r\n /**\r\n * Called when editor loses focus\r\n */\r\n onBlur?: () => void;\r\n \r\n /**\r\n * Translation function\r\n */\r\n translate?: (key: string) => string;\r\n \r\n /**\r\n * Function to resolve file paths (like TinyMCE's tinymceScriptSrc path)\r\n */\r\n getFileSrc?: (path: string) => string;\r\n \r\n /**\r\n * Disable the editor\r\n */\r\n disabled?: boolean;\r\n}\r\n\r\nexport interface EditorRef {\r\n /**\r\n * Get the underlying HTMLEditor instance\r\n */\r\n getEditor: () => HTMLEditor | null;\r\n \r\n /**\r\n * Get the editor content as HTML\r\n */\r\n getContent: () => string;\r\n \r\n /**\r\n * Set the editor content\r\n */\r\n setContent: (html: string) => void;\r\n \r\n /**\r\n * Insert content at cursor position\r\n */\r\n insertContent: (html: string) => void;\r\n \r\n /**\r\n * Focus the editor\r\n */\r\n focus: () => void;\r\n}\r\n\r\n// Global editor instance for single-editor APIs\r\nlet globalEditor: HTMLEditor | null = null;\r\n\r\n/**\r\n * Get content from the global editor instance\r\n * Note: Only works with single editor on the page\r\n */\r\nexport function getEditorContent(): string {\r\n return globalEditor?.getContent() ?? '';\r\n}\r\n\r\n/**\r\n * Set content on the global editor instance\r\n * Note: Only works with single editor on the page\r\n */\r\nexport function setEditorContent(html: string): void {\r\n globalEditor?.setContent(html);\r\n}\r\n\r\n/**\r\n * MDHTMLEditor React Component\r\n */\r\nexport const Editor = forwardRef<EditorRef, EditorProps>(function Editor(\r\n props,\r\n ref\r\n) {\r\n const {\r\n config,\r\n body,\r\n initialValue,\r\n name,\r\n onDirty,\r\n onChange,\r\n onInit,\r\n onFocus,\r\n onBlur,\r\n translate,\r\n getFileSrc,\r\n disabled,\r\n } = props;\r\n \r\n const containerRef = useRef<HTMLDivElement>(null);\r\n const editorRef = useRef<HTMLEditor | null>(null);\r\n const [ready, setReady] = useState(false);\r\n \r\n // Set global translation function if provided\r\n useEffect(() => {\r\n if (translate) {\r\n setTranslate(translate);\r\n }\r\n }, [translate]);\r\n \r\n // Set global file source resolver if provided\r\n useEffect(() => {\r\n if (getFileSrc) {\r\n setGetFileSrc(getFileSrc);\r\n }\r\n }, [getFileSrc]);\r\n \r\n // Initialize editor\r\n useEffect(() => {\r\n if (!containerRef.current) return;\r\n \r\n // Create the editor\r\n const editor = new HTMLEditor(containerRef.current, {\r\n ...config,\r\n // Pass setup through to allow custom buttons\r\n setup: (ed) => {\r\n config.setup?.(ed);\r\n },\r\n });\r\n \r\n editorRef.current = editor;\r\n globalEditor = editor;\r\n \r\n // Set initial content\r\n const content = body ?? initialValue ?? '';\r\n if (content) {\r\n editor.setContent(content);\r\n }\r\n \r\n // Bind events\r\n editor.on('init', () => {\r\n setReady(true);\r\n onInit?.(editor);\r\n });\r\n \r\n editor.on('dirty', (dirty) => {\r\n onDirty?.(dirty);\r\n });\r\n \r\n editor.on('change', (content) => {\r\n onChange?.(content);\r\n editor.setDirty(false);\r\n });\r\n \r\n editor.on('focus', () => {\r\n onFocus?.();\r\n });\r\n \r\n editor.on('blur', () => {\r\n onBlur?.();\r\n });\r\n \r\n // Cleanup\r\n return () => {\r\n editor.destroy();\r\n editorRef.current = null;\r\n if (globalEditor === editor) {\r\n globalEditor = null;\r\n }\r\n };\r\n }, []); // Only run once on mount\r\n \r\n // Update disabled state\r\n useEffect(() => {\r\n // Note: TipTap has setEditable() but our wrapper would need to expose it\r\n // For now, we apply a visual disabled state\r\n if (containerRef.current) {\r\n containerRef.current.style.pointerEvents = disabled ? 'none' : '';\r\n containerRef.current.style.opacity = disabled ? '0.6' : '';\r\n }\r\n }, [disabled]);\r\n \r\n // Expose methods via ref\r\n useImperativeHandle(ref, () => ({\r\n getEditor: () => editorRef.current,\r\n getContent: () => editorRef.current?.getContent() ?? '',\r\n setContent: (html: string) => editorRef.current?.setContent(html),\r\n insertContent: (html: string) => editorRef.current?.insertContent(html),\r\n focus: () => editorRef.current?.focus(),\r\n }), []);\r\n \r\n return (\r\n <div className=\"md-editor-react-wrapper\">\r\n <div ref={containerRef} className=\"md-editor-container\" />\r\n {/* Hidden textarea for form submission */}\r\n {name && ready && (\r\n <input\r\n type=\"hidden\"\r\n name={name}\r\n value={editorRef.current?.getContent() ?? ''}\r\n />\r\n )}\r\n </div>\r\n );\r\n});\r\n\r\nEditor.displayName = 'MDHTMLEditor';\r\n\r\nexport default Editor;\r\n","/**\r\n * useEditor Hook\r\n * Custom hook for advanced editor usage\r\n */\r\n\r\nimport { useEffect, useRef, useState, useCallback } from 'react';\r\nimport { HTMLEditor, EditorConfig } from '@mdaemon/html-editor';\r\n\r\nexport interface UseEditorOptions {\r\n config?: EditorConfig;\r\n content?: string;\r\n onUpdate?: (html: string) => void;\r\n}\r\n\r\nexport interface UseEditorReturn {\r\n /**\r\n * The editor instance\r\n */\r\n editor: HTMLEditor | null;\r\n \r\n /**\r\n * Ref to attach to the container element\r\n */\r\n containerRef: React.RefObject<HTMLDivElement | null>;\r\n \r\n /**\r\n * Whether the editor is ready\r\n */\r\n ready: boolean;\r\n \r\n /**\r\n * Get the current HTML content\r\n */\r\n getContent: () => string;\r\n \r\n /**\r\n * Set the HTML content\r\n */\r\n setContent: (html: string) => void;\r\n \r\n /**\r\n * Insert HTML at cursor\r\n */\r\n insertContent: (html: string) => void;\r\n \r\n /**\r\n * Focus the editor\r\n */\r\n focus: () => void;\r\n \r\n /**\r\n * Check if editor is dirty\r\n */\r\n isDirty: () => boolean;\r\n}\r\n\r\n/**\r\n * Hook for programmatic editor control\r\n */\r\nexport function useEditor(options: UseEditorOptions = {}): UseEditorReturn {\r\n const { config = {}, content = '', onUpdate } = options;\r\n \r\n const containerRef = useRef<HTMLDivElement | null>(null);\r\n const editorRef = useRef<HTMLEditor | null>(null);\r\n const [ready, setReady] = useState(false);\r\n \r\n useEffect(() => {\r\n if (!containerRef.current) return;\r\n \r\n const editor = new HTMLEditor(containerRef.current, config);\r\n editorRef.current = editor;\r\n \r\n if (content) {\r\n editor.setContent(content);\r\n }\r\n \r\n editor.on('init', () => {\r\n setReady(true);\r\n });\r\n \r\n if (onUpdate) {\r\n editor.on('change', onUpdate);\r\n }\r\n \r\n return () => {\r\n editor.destroy();\r\n editorRef.current = null;\r\n setReady(false);\r\n };\r\n }, []); // Only mount once\r\n \r\n const getContent = useCallback(() => {\r\n return editorRef.current?.getContent() ?? '';\r\n }, []);\r\n \r\n const setContent = useCallback((html: string) => {\r\n editorRef.current?.setContent(html);\r\n }, []);\r\n \r\n const insertContent = useCallback((html: string) => {\r\n editorRef.current?.insertContent(html);\r\n }, []);\r\n \r\n const focus = useCallback(() => {\r\n editorRef.current?.focus();\r\n }, []);\r\n \r\n const isDirty = useCallback(() => {\r\n return editorRef.current?.isDirty() ?? false;\r\n }, []);\r\n \r\n return {\r\n editor: editorRef.current,\r\n containerRef,\r\n ready,\r\n getContent,\r\n setContent,\r\n insertContent,\r\n focus,\r\n isDirty,\r\n };\r\n}\r\n"],"names":["Editor","_a","content"],"mappings":";;;;AA6GA,IAAI,eAAkC;AAM/B,SAAS,mBAA2B;AACzC,UAAO,6CAAc,iBAAgB;AACvC;AAMO,SAAS,iBAAiB,MAAoB;AACnD,+CAAc,WAAW;AAC3B;AAKO,MAAM,SAAS,WAAmC,SAASA,QAChE,OACA,KACA;;AACA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,IACE;AAEJ,QAAM,eAAe,OAAuB,IAAI;AAChD,QAAM,YAAY,OAA0B,IAAI;AAChD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,KAAK;AAGxC,YAAU,MAAM;AACd,QAAI,WAAW;AACb,mBAAa,SAAS;AAAA,IACxB;AAAA,EACF,GAAG,CAAC,SAAS,CAAC;AAGd,YAAU,MAAM;AACd,QAAI,YAAY;AACd,oBAAc,UAAU;AAAA,IAC1B;AAAA,EACF,GA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AAA,EAC5C,GAAG,CAAA,CAAE;AAEL,QAAM,aAAa,YAAY,CAAC,SAAiB;;AAC/C,oBAAU,YAAV,mBAAmB,WAAW;AAAA,EAChC,GAAG,CAAA,CAAE;AAEL,QAAM,gBAAgB,YAAY,CAAC,SAAiB;;AAClD,oBAAU,YAAV,mBAAmB,cAAc;AAAA,EACnC,GAAG,CAAA,CAAE;AAEL,QAAM,QAAQ,YAAY,MAAM;;AAC9B,oBAAU,YAAV,mBAAmB;AAAA,EACrB,GAAG,CAAA,CAAE;AAEL,QAAM,UAAU,YAAY,MAAM;;AAChC,aAAO,eAAU,YAAV,mBAAmB,cAAa;AAAA,EACzC,GAAG,CAAA,CAAE;AAEL,SAAO;AAAA,IACL,QAAQ,UAAU;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAEJ;"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@mdaemon/html-editor-react",
3
+ "version": "1.0.0",
4
+ "description": "React wrapper for MDHTMLEditor",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "vite build",
23
+ "dev": "vite build --watch",
24
+ "test": "jest",
25
+ "test:coverage": "jest --coverage",
26
+ "lint": "eslint src --ext .ts,.tsx",
27
+ "typecheck": "tsc --noEmit"
28
+ },
29
+ "keywords": [
30
+ "html-editor",
31
+ "wysiwyg",
32
+ "react",
33
+ "tiptap"
34
+ ],
35
+ "author": {
36
+ "name": "MDaemon Technologies, Ltd.",
37
+ "email": "jonathan.ehman@mdaemon.com",
38
+ "url": "https://www.mdaemon.com"
39
+ },
40
+ "license": "LGPL-3.0-or-later",
41
+ "peerDependencies": {
42
+ "react": "^18.0.0 || ^17.0.0",
43
+ "react-dom": "^18.0.0 || ^17.0.0"
44
+ },
45
+ "dependencies": {
46
+ "@tiptap/react": "^3.19.0"
47
+ },
48
+ "devDependencies": {
49
+ "@mdaemon/html-editor": "^1.0.2",
50
+ "@testing-library/jest-dom": "^6.9.1",
51
+ "@testing-library/react": "^16.3.2",
52
+ "@types/jest": "^29.5.0",
53
+ "@types/react": "^18.2.0",
54
+ "@types/react-dom": "^18.2.0",
55
+ "@vitejs/plugin-react": "^5.1.4",
56
+ "jest": "^29.5.0",
57
+ "jest-environment-jsdom": "^29.5.0",
58
+ "react": "^18.2.0",
59
+ "react-dom": "^18.2.0",
60
+ "ts-jest": "^29.4.6",
61
+ "typescript": "^5.9.3",
62
+ "vite": "^7.3.1",
63
+ "vite-plugin-dts": "^4.5.4"
64
+ }
65
+ }