nam-rich-text-editor 9.1.0 → 9.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,9 +48,211 @@ const MyEditor = () => {
48
48
 
49
49
  ## Features
50
50
 
51
- - Bold, Italic, Underline formatting
52
- - Bullet and numbered lists
53
- - HTML content editing
51
+ - **Text Formatting**: Bold, Italic, Underline, Strikethrough
52
+ - **Lists**: Bullet and numbered lists
53
+ - **Alignment**: Left, Center, Right, Justify
54
+ - **Headings**: H1, H2, H3, H4
55
+ - **Media**: Insert images and videos (including YouTube embeds)
56
+ - **Links**: Create hyperlinks
57
+ - **Quotes**: Insert formatted blockquotes
58
+ - **Colors**: Text and background colors
59
+ - **Fonts**: Multiple font families and sizes
60
+ - **Undo/Redo**: Full history support
61
+ - **Fullscreen Mode**: Focus writing mode
62
+ - **HTML Content**: Content is stored as HTML
63
+
64
+ ## Multilingual Support
65
+
66
+ The editor supports managing content for multiple languages. Here's how to preserve content when switching between language tabs:
67
+
68
+ ### Vanilla JavaScript Example
69
+
70
+ ```javascript
71
+ import { RichTextEditor } from "rich-text-editor";
72
+
73
+ // Create storage for different languages
74
+ const contentStorage = {
75
+ vi: "",
76
+ en: "",
77
+ zh: "",
78
+ };
79
+ let currentLanguage = "vi";
80
+
81
+ const editor = new RichTextEditor();
82
+ document.body.appendChild(editor.getElement());
83
+
84
+ // Function to switch language
85
+ function switchLanguage(newLang) {
86
+ // Save current content before switching
87
+ contentStorage[currentLanguage] = editor.getHTML();
88
+
89
+ // Update current language
90
+ currentLanguage = newLang;
91
+
92
+ // Load content for new language
93
+ editor.setHTML(contentStorage[newLang] || "");
94
+ }
95
+
96
+ // Auto-save on input (optional)
97
+ const editorDiv = editor.getElement().querySelector(".editor");
98
+ editorDiv.addEventListener("input", () => {
99
+ contentStorage[currentLanguage] = editor.getHTML();
100
+ });
101
+
102
+ // Save to localStorage
103
+ function saveAll() {
104
+ contentStorage[currentLanguage] = editor.getHTML();
105
+ localStorage.setItem("content-vi", contentStorage.vi);
106
+ localStorage.setItem("content-en", contentStorage.en);
107
+ localStorage.setItem("content-zh", contentStorage.zh);
108
+ }
109
+
110
+ // Load from localStorage
111
+ function loadAll() {
112
+ contentStorage.vi = localStorage.getItem("content-vi") || "";
113
+ contentStorage.en = localStorage.getItem("content-en") || "";
114
+ contentStorage.zh = localStorage.getItem("content-zh") || "";
115
+ editor.setHTML(contentStorage[currentLanguage]);
116
+ }
117
+ ```
118
+
119
+ ### React Example
120
+
121
+ ```jsx
122
+ import React, { useState, useEffect, useRef } from "react";
123
+ import { RichTextEditor } from "rich-text-editor";
124
+
125
+ const MultilingualEditor = () => {
126
+ const [currentLang, setCurrentLang] = useState("vi");
127
+ const [content, setContent] = useState({
128
+ vi: "<p>Nội dung tiếng Việt...</p>",
129
+ en: "<p>English content...</p>",
130
+ zh: "<p>中文内容...</p>",
131
+ });
132
+
133
+ const editorRef = useRef(null);
134
+ const containerRef = useRef(null);
135
+
136
+ useEffect(() => {
137
+ if (!editorRef.current) {
138
+ editorRef.current = new RichTextEditor();
139
+ containerRef.current.appendChild(editorRef.current.getElement());
140
+ editorRef.current.setHTML(content[currentLang]);
141
+ }
142
+ }, []);
143
+
144
+ const switchLanguage = (newLang) => {
145
+ // Save current content
146
+ const currentContent = editorRef.current.getHTML();
147
+ setContent((prev) => ({ ...prev, [currentLang]: currentContent }));
148
+
149
+ // Switch language
150
+ setCurrentLang(newLang);
151
+
152
+ // Load new content
153
+ setTimeout(() => {
154
+ editorRef.current.setHTML(content[newLang] || "");
155
+ }, 0);
156
+ };
157
+
158
+ return (
159
+ <div>
160
+ <div>
161
+ <button onClick={() => switchLanguage("vi")}>🇻🇳 Tiếng Việt</button>
162
+ <button onClick={() => switchLanguage("en")}>🇬🇧 English</button>
163
+ <button onClick={() => switchLanguage("zh")}>🇨🇳 中文</button>
164
+ </div>
165
+ <div ref={containerRef} />
166
+ </div>
167
+ );
168
+ };
169
+ ```
170
+
171
+ ### Important Notes for Multilingual Content
172
+
173
+ 1. **Always save before switching**: Call `editor.getHTML()` to save the current content before switching languages
174
+ 2. **Images and Videos**: All media (images, videos) are preserved in the HTML content
175
+ 3. **Storage Options**:
176
+ - **Memory**: Store in JavaScript object (lost on page refresh)
177
+ - **localStorage**: Persist data in browser (survives page refresh)
178
+ - **Backend**: Send to server for permanent storage
179
+ 4. **Auto-save**: Use input event listener for automatic saving
180
+ 5. **State Management**: In React, use `useState` or state management libraries (Redux, Zustand, etc.)
181
+
182
+ For complete examples, see:
183
+
184
+ - `/examples/multilingual-editor.html` - Vanilla JavaScript example with full UI
185
+ - `/examples/react-multilingual-example.tsx` - React component with custom hook
186
+
187
+ ## API Reference
188
+
189
+ ### RichTextEditor Class
190
+
191
+ #### Constructor
192
+
193
+ ```typescript
194
+ new RichTextEditor();
195
+ ```
196
+
197
+ Creates a new rich text editor instance.
198
+
199
+ #### Methods
200
+
201
+ ##### `getElement(): HTMLElement`
202
+
203
+ Returns the container element of the editor.
204
+
205
+ ```javascript
206
+ const editor = new RichTextEditor();
207
+ const element = editor.getElement();
208
+ document.body.appendChild(element);
209
+ ```
210
+
211
+ ##### `getHTML(): string`
212
+
213
+ Returns the current HTML content of the editor.
214
+
215
+ ```javascript
216
+ const htmlContent = editor.getHTML();
217
+ console.log(htmlContent);
218
+ ```
219
+
220
+ ##### `setHTML(html: string): void`
221
+
222
+ Sets the HTML content of the editor.
223
+
224
+ ```javascript
225
+ editor.setHTML("<h1>Hello World</h1><p>This is a paragraph.</p>");
226
+ ```
227
+
228
+ ### RichTextEditorComponent (React)
229
+
230
+ #### Props
231
+
232
+ | Prop | Type | Default | Description |
233
+ | -------------- | ------------------------ | ----------- | ----------------------------------- |
234
+ | `initialValue` | `string` | `''` | Initial HTML content |
235
+ | `onChange` | `(html: string) => void` | `undefined` | Callback fired when content changes |
236
+ | `className` | `string` | `undefined` | CSS class for the container |
237
+ | `style` | `React.CSSProperties` | `undefined` | Inline styles for the container |
238
+
239
+ #### Usage
240
+
241
+ ```jsx
242
+ import { RichTextEditorComponent } from "rich-text-editor";
243
+
244
+ function MyComponent() {
245
+ const [content, setContent] = useState("");
246
+
247
+ return (
248
+ <RichTextEditorComponent
249
+ initialValue="<p>Start typing...</p>"
250
+ onChange={(html) => setContent(html)}
251
+ className="my-editor"
252
+ />
253
+ );
254
+ }
255
+ ```
54
256
 
55
257
  ## Development
56
258